core.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. /*
  2. * Generic pwmlib implementation
  3. *
  4. * Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de>
  5. * Copyright (C) 2011-2012 Avionic Design GmbH
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; see the file COPYING. If not, write to
  19. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/pwm.h>
  23. #include <linux/radix-tree.h>
  24. #include <linux/list.h>
  25. #include <linux/mutex.h>
  26. #include <linux/err.h>
  27. #include <linux/slab.h>
  28. #include <linux/device.h>
  29. #include <linux/debugfs.h>
  30. #include <linux/seq_file.h>
  31. #include <dt-bindings/pwm/pwm.h>
  32. #define MAX_PWMS 1024
  33. static DEFINE_MUTEX(pwm_lookup_lock);
  34. static LIST_HEAD(pwm_lookup_list);
  35. static DEFINE_MUTEX(pwm_lock);
  36. static LIST_HEAD(pwm_chips);
  37. static DECLARE_BITMAP(allocated_pwms, MAX_PWMS);
  38. static RADIX_TREE(pwm_tree, GFP_KERNEL);
  39. static struct pwm_device *pwm_to_device(unsigned int pwm)
  40. {
  41. return radix_tree_lookup(&pwm_tree, pwm);
  42. }
  43. static int alloc_pwms(int pwm, unsigned int count)
  44. {
  45. unsigned int from = 0;
  46. unsigned int start;
  47. if (pwm >= MAX_PWMS)
  48. return -EINVAL;
  49. if (pwm >= 0)
  50. from = pwm;
  51. start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, from,
  52. count, 0);
  53. if (pwm >= 0 && start != pwm)
  54. return -EEXIST;
  55. if (start + count > MAX_PWMS)
  56. return -ENOSPC;
  57. return start;
  58. }
  59. static void free_pwms(struct pwm_chip *chip)
  60. {
  61. unsigned int i;
  62. for (i = 0; i < chip->npwm; i++) {
  63. struct pwm_device *pwm = &chip->pwms[i];
  64. radix_tree_delete(&pwm_tree, pwm->pwm);
  65. }
  66. bitmap_clear(allocated_pwms, chip->base, chip->npwm);
  67. kfree(chip->pwms);
  68. chip->pwms = NULL;
  69. }
  70. static struct pwm_chip *pwmchip_find_by_name(const char *name)
  71. {
  72. struct pwm_chip *chip;
  73. if (!name)
  74. return NULL;
  75. mutex_lock(&pwm_lock);
  76. list_for_each_entry(chip, &pwm_chips, list) {
  77. const char *chip_name = dev_name(chip->dev);
  78. if (chip_name && strcmp(chip_name, name) == 0) {
  79. mutex_unlock(&pwm_lock);
  80. return chip;
  81. }
  82. }
  83. mutex_unlock(&pwm_lock);
  84. return NULL;
  85. }
  86. static int pwm_device_request(struct pwm_device *pwm, const char *label)
  87. {
  88. int err;
  89. if (test_bit(PWMF_REQUESTED, &pwm->flags))
  90. return -EBUSY;
  91. if (!try_module_get(pwm->chip->ops->owner))
  92. return -ENODEV;
  93. if (pwm->chip->ops->request) {
  94. err = pwm->chip->ops->request(pwm->chip, pwm);
  95. if (err) {
  96. module_put(pwm->chip->ops->owner);
  97. return err;
  98. }
  99. }
  100. set_bit(PWMF_REQUESTED, &pwm->flags);
  101. pwm->label = label;
  102. return 0;
  103. }
  104. struct pwm_device *
  105. of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args)
  106. {
  107. struct pwm_device *pwm;
  108. if (pc->of_pwm_n_cells < 3)
  109. return ERR_PTR(-EINVAL);
  110. if (args->args[0] >= pc->npwm)
  111. return ERR_PTR(-EINVAL);
  112. pwm = pwm_request_from_chip(pc, args->args[0], NULL);
  113. if (IS_ERR(pwm))
  114. return pwm;
  115. pwm_set_period(pwm, args->args[1]);
  116. if (args->args[2] & PWM_POLARITY_INVERTED)
  117. pwm_set_polarity(pwm, PWM_POLARITY_INVERSED);
  118. else
  119. pwm_set_polarity(pwm, PWM_POLARITY_NORMAL);
  120. return pwm;
  121. }
  122. EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
  123. static struct pwm_device *
  124. of_pwm_simple_xlate(struct pwm_chip *pc, const struct of_phandle_args *args)
  125. {
  126. struct pwm_device *pwm;
  127. if (pc->of_pwm_n_cells < 2)
  128. return ERR_PTR(-EINVAL);
  129. if (args->args[0] >= pc->npwm)
  130. return ERR_PTR(-EINVAL);
  131. pwm = pwm_request_from_chip(pc, args->args[0], NULL);
  132. if (IS_ERR(pwm))
  133. return pwm;
  134. pwm_set_period(pwm, args->args[1]);
  135. return pwm;
  136. }
  137. static void of_pwmchip_add(struct pwm_chip *chip)
  138. {
  139. if (!chip->dev || !chip->dev->of_node)
  140. return;
  141. if (!chip->of_xlate) {
  142. chip->of_xlate = of_pwm_simple_xlate;
  143. chip->of_pwm_n_cells = 2;
  144. }
  145. of_node_get(chip->dev->of_node);
  146. }
  147. static void of_pwmchip_remove(struct pwm_chip *chip)
  148. {
  149. if (chip->dev)
  150. of_node_put(chip->dev->of_node);
  151. }
  152. /**
  153. * pwm_set_chip_data() - set private chip data for a PWM
  154. * @pwm: PWM device
  155. * @data: pointer to chip-specific data
  156. *
  157. * Returns: 0 on success or a negative error code on failure.
  158. */
  159. int pwm_set_chip_data(struct pwm_device *pwm, void *data)
  160. {
  161. if (!pwm)
  162. return -EINVAL;
  163. pwm->chip_data = data;
  164. return 0;
  165. }
  166. EXPORT_SYMBOL_GPL(pwm_set_chip_data);
  167. /**
  168. * pwm_get_chip_data() - get private chip data for a PWM
  169. * @pwm: PWM device
  170. *
  171. * Returns: A pointer to the chip-private data for the PWM device.
  172. */
  173. void *pwm_get_chip_data(struct pwm_device *pwm)
  174. {
  175. return pwm ? pwm->chip_data : NULL;
  176. }
  177. EXPORT_SYMBOL_GPL(pwm_get_chip_data);
  178. /**
  179. * pwmchip_add_with_polarity() - register a new PWM chip
  180. * @chip: the PWM chip to add
  181. * @polarity: initial polarity of PWM channels
  182. *
  183. * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
  184. * will be used. The initial polarity for all channels is specified by the
  185. * @polarity parameter.
  186. *
  187. * Returns: 0 on success or a negative error code on failure.
  188. */
  189. int pwmchip_add_with_polarity(struct pwm_chip *chip,
  190. enum pwm_polarity polarity)
  191. {
  192. struct pwm_device *pwm;
  193. unsigned int i;
  194. int ret;
  195. if (!chip || !chip->dev || !chip->ops || !chip->ops->config ||
  196. !chip->ops->enable || !chip->ops->disable || !chip->npwm)
  197. return -EINVAL;
  198. mutex_lock(&pwm_lock);
  199. ret = alloc_pwms(chip->base, chip->npwm);
  200. if (ret < 0)
  201. goto out;
  202. chip->pwms = kzalloc(chip->npwm * sizeof(*pwm), GFP_KERNEL);
  203. if (!chip->pwms) {
  204. ret = -ENOMEM;
  205. goto out;
  206. }
  207. chip->base = ret;
  208. for (i = 0; i < chip->npwm; i++) {
  209. pwm = &chip->pwms[i];
  210. pwm->chip = chip;
  211. pwm->pwm = chip->base + i;
  212. pwm->hwpwm = i;
  213. pwm->polarity = polarity;
  214. mutex_init(&pwm->lock);
  215. radix_tree_insert(&pwm_tree, pwm->pwm, pwm);
  216. }
  217. bitmap_set(allocated_pwms, chip->base, chip->npwm);
  218. INIT_LIST_HEAD(&chip->list);
  219. list_add(&chip->list, &pwm_chips);
  220. ret = 0;
  221. if (IS_ENABLED(CONFIG_OF))
  222. of_pwmchip_add(chip);
  223. pwmchip_sysfs_export(chip);
  224. out:
  225. mutex_unlock(&pwm_lock);
  226. return ret;
  227. }
  228. EXPORT_SYMBOL_GPL(pwmchip_add_with_polarity);
  229. /**
  230. * pwmchip_add() - register a new PWM chip
  231. * @chip: the PWM chip to add
  232. *
  233. * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
  234. * will be used. The initial polarity for all channels is normal.
  235. *
  236. * Returns: 0 on success or a negative error code on failure.
  237. */
  238. int pwmchip_add(struct pwm_chip *chip)
  239. {
  240. return pwmchip_add_with_polarity(chip, PWM_POLARITY_NORMAL);
  241. }
  242. EXPORT_SYMBOL_GPL(pwmchip_add);
  243. /**
  244. * pwmchip_remove() - remove a PWM chip
  245. * @chip: the PWM chip to remove
  246. *
  247. * Removes a PWM chip. This function may return busy if the PWM chip provides
  248. * a PWM device that is still requested.
  249. *
  250. * Returns: 0 on success or a negative error code on failure.
  251. */
  252. int pwmchip_remove(struct pwm_chip *chip)
  253. {
  254. unsigned int i;
  255. int ret = 0;
  256. pwmchip_sysfs_unexport_children(chip);
  257. mutex_lock(&pwm_lock);
  258. for (i = 0; i < chip->npwm; i++) {
  259. struct pwm_device *pwm = &chip->pwms[i];
  260. if (test_bit(PWMF_REQUESTED, &pwm->flags)) {
  261. ret = -EBUSY;
  262. goto out;
  263. }
  264. }
  265. list_del_init(&chip->list);
  266. if (IS_ENABLED(CONFIG_OF))
  267. of_pwmchip_remove(chip);
  268. free_pwms(chip);
  269. pwmchip_sysfs_unexport(chip);
  270. out:
  271. mutex_unlock(&pwm_lock);
  272. return ret;
  273. }
  274. EXPORT_SYMBOL_GPL(pwmchip_remove);
  275. /**
  276. * pwm_request() - request a PWM device
  277. * @pwm: global PWM device index
  278. * @label: PWM device label
  279. *
  280. * This function is deprecated, use pwm_get() instead.
  281. *
  282. * Returns: A pointer to a PWM device or an ERR_PTR()-encoded error code on
  283. * failure.
  284. */
  285. struct pwm_device *pwm_request(int pwm, const char *label)
  286. {
  287. struct pwm_device *dev;
  288. int err;
  289. if (pwm < 0 || pwm >= MAX_PWMS)
  290. return ERR_PTR(-EINVAL);
  291. mutex_lock(&pwm_lock);
  292. dev = pwm_to_device(pwm);
  293. if (!dev) {
  294. dev = ERR_PTR(-EPROBE_DEFER);
  295. goto out;
  296. }
  297. err = pwm_device_request(dev, label);
  298. if (err < 0)
  299. dev = ERR_PTR(err);
  300. out:
  301. mutex_unlock(&pwm_lock);
  302. return dev;
  303. }
  304. EXPORT_SYMBOL_GPL(pwm_request);
  305. /**
  306. * pwm_request_from_chip() - request a PWM device relative to a PWM chip
  307. * @chip: PWM chip
  308. * @index: per-chip index of the PWM to request
  309. * @label: a literal description string of this PWM
  310. *
  311. * Returns: A pointer to the PWM device at the given index of the given PWM
  312. * chip. A negative error code is returned if the index is not valid for the
  313. * specified PWM chip or if the PWM device cannot be requested.
  314. */
  315. struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
  316. unsigned int index,
  317. const char *label)
  318. {
  319. struct pwm_device *pwm;
  320. int err;
  321. if (!chip || index >= chip->npwm)
  322. return ERR_PTR(-EINVAL);
  323. mutex_lock(&pwm_lock);
  324. pwm = &chip->pwms[index];
  325. err = pwm_device_request(pwm, label);
  326. if (err < 0)
  327. pwm = ERR_PTR(err);
  328. mutex_unlock(&pwm_lock);
  329. return pwm;
  330. }
  331. EXPORT_SYMBOL_GPL(pwm_request_from_chip);
  332. /**
  333. * pwm_free() - free a PWM device
  334. * @pwm: PWM device
  335. *
  336. * This function is deprecated, use pwm_put() instead.
  337. */
  338. void pwm_free(struct pwm_device *pwm)
  339. {
  340. pwm_put(pwm);
  341. }
  342. EXPORT_SYMBOL_GPL(pwm_free);
  343. /**
  344. * pwm_config() - change a PWM device configuration
  345. * @pwm: PWM device
  346. * @duty_ns: "on" time (in nanoseconds)
  347. * @period_ns: duration (in nanoseconds) of one cycle
  348. *
  349. * Returns: 0 on success or a negative error code on failure.
  350. */
  351. int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
  352. {
  353. int err;
  354. if (!pwm || duty_ns < 0 || period_ns <= 0 || duty_ns > period_ns)
  355. return -EINVAL;
  356. err = pwm->chip->ops->config(pwm->chip, pwm, duty_ns, period_ns);
  357. if (err)
  358. return err;
  359. pwm->duty_cycle = duty_ns;
  360. pwm->period = period_ns;
  361. return 0;
  362. }
  363. EXPORT_SYMBOL_GPL(pwm_config);
  364. /**
  365. * pwm_set_polarity() - configure the polarity of a PWM signal
  366. * @pwm: PWM device
  367. * @polarity: new polarity of the PWM signal
  368. *
  369. * Note that the polarity cannot be configured while the PWM device is
  370. * enabled.
  371. *
  372. * Returns: 0 on success or a negative error code on failure.
  373. */
  374. int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity)
  375. {
  376. int err;
  377. if (!pwm || !pwm->chip->ops)
  378. return -EINVAL;
  379. if (!pwm->chip->ops->set_polarity)
  380. return -ENOSYS;
  381. mutex_lock(&pwm->lock);
  382. if (pwm_is_enabled(pwm)) {
  383. err = -EBUSY;
  384. goto unlock;
  385. }
  386. err = pwm->chip->ops->set_polarity(pwm->chip, pwm, polarity);
  387. if (err)
  388. goto unlock;
  389. pwm->polarity = polarity;
  390. unlock:
  391. mutex_unlock(&pwm->lock);
  392. return err;
  393. }
  394. EXPORT_SYMBOL_GPL(pwm_set_polarity);
  395. /**
  396. * pwm_enable() - start a PWM output toggling
  397. * @pwm: PWM device
  398. *
  399. * Returns: 0 on success or a negative error code on failure.
  400. */
  401. int pwm_enable(struct pwm_device *pwm)
  402. {
  403. int err = 0;
  404. if (!pwm)
  405. return -EINVAL;
  406. mutex_lock(&pwm->lock);
  407. if (!test_and_set_bit(PWMF_ENABLED, &pwm->flags)) {
  408. err = pwm->chip->ops->enable(pwm->chip, pwm);
  409. if (err)
  410. clear_bit(PWMF_ENABLED, &pwm->flags);
  411. }
  412. mutex_unlock(&pwm->lock);
  413. return err;
  414. }
  415. EXPORT_SYMBOL_GPL(pwm_enable);
  416. /**
  417. * pwm_disable() - stop a PWM output toggling
  418. * @pwm: PWM device
  419. */
  420. void pwm_disable(struct pwm_device *pwm)
  421. {
  422. if (pwm && test_and_clear_bit(PWMF_ENABLED, &pwm->flags))
  423. pwm->chip->ops->disable(pwm->chip, pwm);
  424. }
  425. EXPORT_SYMBOL_GPL(pwm_disable);
  426. static struct pwm_chip *of_node_to_pwmchip(struct device_node *np)
  427. {
  428. struct pwm_chip *chip;
  429. mutex_lock(&pwm_lock);
  430. list_for_each_entry(chip, &pwm_chips, list)
  431. if (chip->dev && chip->dev->of_node == np) {
  432. mutex_unlock(&pwm_lock);
  433. return chip;
  434. }
  435. mutex_unlock(&pwm_lock);
  436. return ERR_PTR(-EPROBE_DEFER);
  437. }
  438. /**
  439. * of_pwm_get() - request a PWM via the PWM framework
  440. * @np: device node to get the PWM from
  441. * @con_id: consumer name
  442. *
  443. * Returns the PWM device parsed from the phandle and index specified in the
  444. * "pwms" property of a device tree node or a negative error-code on failure.
  445. * Values parsed from the device tree are stored in the returned PWM device
  446. * object.
  447. *
  448. * If con_id is NULL, the first PWM device listed in the "pwms" property will
  449. * be requested. Otherwise the "pwm-names" property is used to do a reverse
  450. * lookup of the PWM index. This also means that the "pwm-names" property
  451. * becomes mandatory for devices that look up the PWM device via the con_id
  452. * parameter.
  453. *
  454. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  455. * error code on failure.
  456. */
  457. struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id)
  458. {
  459. struct pwm_device *pwm = NULL;
  460. struct of_phandle_args args;
  461. struct pwm_chip *pc;
  462. int index = 0;
  463. int err;
  464. if (con_id) {
  465. index = of_property_match_string(np, "pwm-names", con_id);
  466. if (index < 0)
  467. return ERR_PTR(index);
  468. }
  469. err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
  470. &args);
  471. if (err) {
  472. pr_debug("%s(): can't parse \"pwms\" property\n", __func__);
  473. return ERR_PTR(err);
  474. }
  475. pc = of_node_to_pwmchip(args.np);
  476. if (IS_ERR(pc)) {
  477. pr_debug("%s(): PWM chip not found\n", __func__);
  478. pwm = ERR_CAST(pc);
  479. goto put;
  480. }
  481. if (args.args_count != pc->of_pwm_n_cells) {
  482. pr_debug("%s: wrong #pwm-cells for %s\n", np->full_name,
  483. args.np->full_name);
  484. pwm = ERR_PTR(-EINVAL);
  485. goto put;
  486. }
  487. pwm = pc->of_xlate(pc, &args);
  488. if (IS_ERR(pwm))
  489. goto put;
  490. /*
  491. * If a consumer name was not given, try to look it up from the
  492. * "pwm-names" property if it exists. Otherwise use the name of
  493. * the user device node.
  494. */
  495. if (!con_id) {
  496. err = of_property_read_string_index(np, "pwm-names", index,
  497. &con_id);
  498. if (err < 0)
  499. con_id = np->name;
  500. }
  501. pwm->label = con_id;
  502. put:
  503. of_node_put(args.np);
  504. return pwm;
  505. }
  506. EXPORT_SYMBOL_GPL(of_pwm_get);
  507. /**
  508. * pwm_add_table() - register PWM device consumers
  509. * @table: array of consumers to register
  510. * @num: number of consumers in table
  511. */
  512. void pwm_add_table(struct pwm_lookup *table, size_t num)
  513. {
  514. mutex_lock(&pwm_lookup_lock);
  515. while (num--) {
  516. list_add_tail(&table->list, &pwm_lookup_list);
  517. table++;
  518. }
  519. mutex_unlock(&pwm_lookup_lock);
  520. }
  521. /**
  522. * pwm_remove_table() - unregister PWM device consumers
  523. * @table: array of consumers to unregister
  524. * @num: number of consumers in table
  525. */
  526. void pwm_remove_table(struct pwm_lookup *table, size_t num)
  527. {
  528. mutex_lock(&pwm_lookup_lock);
  529. while (num--) {
  530. list_del(&table->list);
  531. table++;
  532. }
  533. mutex_unlock(&pwm_lookup_lock);
  534. }
  535. /**
  536. * pwm_get() - look up and request a PWM device
  537. * @dev: device for PWM consumer
  538. * @con_id: consumer name
  539. *
  540. * Lookup is first attempted using DT. If the device was not instantiated from
  541. * a device tree, a PWM chip and a relative index is looked up via a table
  542. * supplied by board setup code (see pwm_add_table()).
  543. *
  544. * Once a PWM chip has been found the specified PWM device will be requested
  545. * and is ready to be used.
  546. *
  547. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  548. * error code on failure.
  549. */
  550. struct pwm_device *pwm_get(struct device *dev, const char *con_id)
  551. {
  552. struct pwm_device *pwm = ERR_PTR(-EPROBE_DEFER);
  553. const char *dev_id = dev ? dev_name(dev) : NULL;
  554. struct pwm_chip *chip = NULL;
  555. unsigned int best = 0;
  556. struct pwm_lookup *p, *chosen = NULL;
  557. unsigned int match;
  558. /* look up via DT first */
  559. if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
  560. return of_pwm_get(dev->of_node, con_id);
  561. /*
  562. * We look up the provider in the static table typically provided by
  563. * board setup code. We first try to lookup the consumer device by
  564. * name. If the consumer device was passed in as NULL or if no match
  565. * was found, we try to find the consumer by directly looking it up
  566. * by name.
  567. *
  568. * If a match is found, the provider PWM chip is looked up by name
  569. * and a PWM device is requested using the PWM device per-chip index.
  570. *
  571. * The lookup algorithm was shamelessly taken from the clock
  572. * framework:
  573. *
  574. * We do slightly fuzzy matching here:
  575. * An entry with a NULL ID is assumed to be a wildcard.
  576. * If an entry has a device ID, it must match
  577. * If an entry has a connection ID, it must match
  578. * Then we take the most specific entry - with the following order
  579. * of precedence: dev+con > dev only > con only.
  580. */
  581. mutex_lock(&pwm_lookup_lock);
  582. list_for_each_entry(p, &pwm_lookup_list, list) {
  583. match = 0;
  584. if (p->dev_id) {
  585. if (!dev_id || strcmp(p->dev_id, dev_id))
  586. continue;
  587. match += 2;
  588. }
  589. if (p->con_id) {
  590. if (!con_id || strcmp(p->con_id, con_id))
  591. continue;
  592. match += 1;
  593. }
  594. if (match > best) {
  595. chosen = p;
  596. if (match != 3)
  597. best = match;
  598. else
  599. break;
  600. }
  601. }
  602. if (!chosen) {
  603. pwm = ERR_PTR(-ENODEV);
  604. goto out;
  605. }
  606. chip = pwmchip_find_by_name(chosen->provider);
  607. if (!chip)
  608. goto out;
  609. pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
  610. if (IS_ERR(pwm))
  611. goto out;
  612. pwm_set_period(pwm, chosen->period);
  613. pwm_set_polarity(pwm, chosen->polarity);
  614. out:
  615. mutex_unlock(&pwm_lookup_lock);
  616. return pwm;
  617. }
  618. EXPORT_SYMBOL_GPL(pwm_get);
  619. /**
  620. * pwm_put() - release a PWM device
  621. * @pwm: PWM device
  622. */
  623. void pwm_put(struct pwm_device *pwm)
  624. {
  625. if (!pwm)
  626. return;
  627. mutex_lock(&pwm_lock);
  628. if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
  629. pr_warn("PWM device already freed\n");
  630. goto out;
  631. }
  632. if (pwm->chip->ops->free)
  633. pwm->chip->ops->free(pwm->chip, pwm);
  634. pwm->label = NULL;
  635. module_put(pwm->chip->ops->owner);
  636. out:
  637. mutex_unlock(&pwm_lock);
  638. }
  639. EXPORT_SYMBOL_GPL(pwm_put);
  640. static void devm_pwm_release(struct device *dev, void *res)
  641. {
  642. pwm_put(*(struct pwm_device **)res);
  643. }
  644. /**
  645. * devm_pwm_get() - resource managed pwm_get()
  646. * @dev: device for PWM consumer
  647. * @con_id: consumer name
  648. *
  649. * This function performs like pwm_get() but the acquired PWM device will
  650. * automatically be released on driver detach.
  651. *
  652. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  653. * error code on failure.
  654. */
  655. struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
  656. {
  657. struct pwm_device **ptr, *pwm;
  658. ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
  659. if (!ptr)
  660. return ERR_PTR(-ENOMEM);
  661. pwm = pwm_get(dev, con_id);
  662. if (!IS_ERR(pwm)) {
  663. *ptr = pwm;
  664. devres_add(dev, ptr);
  665. } else {
  666. devres_free(ptr);
  667. }
  668. return pwm;
  669. }
  670. EXPORT_SYMBOL_GPL(devm_pwm_get);
  671. /**
  672. * devm_of_pwm_get() - resource managed of_pwm_get()
  673. * @dev: device for PWM consumer
  674. * @np: device node to get the PWM from
  675. * @con_id: consumer name
  676. *
  677. * This function performs like of_pwm_get() but the acquired PWM device will
  678. * automatically be released on driver detach.
  679. *
  680. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  681. * error code on failure.
  682. */
  683. struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
  684. const char *con_id)
  685. {
  686. struct pwm_device **ptr, *pwm;
  687. ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
  688. if (!ptr)
  689. return ERR_PTR(-ENOMEM);
  690. pwm = of_pwm_get(np, con_id);
  691. if (!IS_ERR(pwm)) {
  692. *ptr = pwm;
  693. devres_add(dev, ptr);
  694. } else {
  695. devres_free(ptr);
  696. }
  697. return pwm;
  698. }
  699. EXPORT_SYMBOL_GPL(devm_of_pwm_get);
  700. static int devm_pwm_match(struct device *dev, void *res, void *data)
  701. {
  702. struct pwm_device **p = res;
  703. if (WARN_ON(!p || !*p))
  704. return 0;
  705. return *p == data;
  706. }
  707. /**
  708. * devm_pwm_put() - resource managed pwm_put()
  709. * @dev: device for PWM consumer
  710. * @pwm: PWM device
  711. *
  712. * Release a PWM previously allocated using devm_pwm_get(). Calling this
  713. * function is usually not needed because devm-allocated resources are
  714. * automatically released on driver detach.
  715. */
  716. void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
  717. {
  718. WARN_ON(devres_release(dev, devm_pwm_release, devm_pwm_match, pwm));
  719. }
  720. EXPORT_SYMBOL_GPL(devm_pwm_put);
  721. /**
  722. * pwm_can_sleep() - report whether PWM access will sleep
  723. * @pwm: PWM device
  724. *
  725. * Returns: True if accessing the PWM can sleep, false otherwise.
  726. */
  727. bool pwm_can_sleep(struct pwm_device *pwm)
  728. {
  729. return true;
  730. }
  731. EXPORT_SYMBOL_GPL(pwm_can_sleep);
  732. #ifdef CONFIG_DEBUG_FS
  733. static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
  734. {
  735. unsigned int i;
  736. for (i = 0; i < chip->npwm; i++) {
  737. struct pwm_device *pwm = &chip->pwms[i];
  738. seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
  739. if (test_bit(PWMF_REQUESTED, &pwm->flags))
  740. seq_puts(s, " requested");
  741. if (pwm_is_enabled(pwm))
  742. seq_puts(s, " enabled");
  743. seq_puts(s, "\n");
  744. }
  745. }
  746. static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
  747. {
  748. mutex_lock(&pwm_lock);
  749. s->private = "";
  750. return seq_list_start(&pwm_chips, *pos);
  751. }
  752. static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
  753. {
  754. s->private = "\n";
  755. return seq_list_next(v, &pwm_chips, pos);
  756. }
  757. static void pwm_seq_stop(struct seq_file *s, void *v)
  758. {
  759. mutex_unlock(&pwm_lock);
  760. }
  761. static int pwm_seq_show(struct seq_file *s, void *v)
  762. {
  763. struct pwm_chip *chip = list_entry(v, struct pwm_chip, list);
  764. seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private,
  765. chip->dev->bus ? chip->dev->bus->name : "no-bus",
  766. dev_name(chip->dev), chip->npwm,
  767. (chip->npwm != 1) ? "s" : "");
  768. if (chip->ops->dbg_show)
  769. chip->ops->dbg_show(chip, s);
  770. else
  771. pwm_dbg_show(chip, s);
  772. return 0;
  773. }
  774. static const struct seq_operations pwm_seq_ops = {
  775. .start = pwm_seq_start,
  776. .next = pwm_seq_next,
  777. .stop = pwm_seq_stop,
  778. .show = pwm_seq_show,
  779. };
  780. static int pwm_seq_open(struct inode *inode, struct file *file)
  781. {
  782. return seq_open(file, &pwm_seq_ops);
  783. }
  784. static const struct file_operations pwm_debugfs_ops = {
  785. .owner = THIS_MODULE,
  786. .open = pwm_seq_open,
  787. .read = seq_read,
  788. .llseek = seq_lseek,
  789. .release = seq_release,
  790. };
  791. static int __init pwm_debugfs_init(void)
  792. {
  793. debugfs_create_file("pwm", S_IFREG | S_IRUGO, NULL, NULL,
  794. &pwm_debugfs_ops);
  795. return 0;
  796. }
  797. subsys_initcall(pwm_debugfs_init);
  798. #endif /* CONFIG_DEBUG_FS */