core.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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. mutex_lock(&pwm_lock);
  257. for (i = 0; i < chip->npwm; i++) {
  258. struct pwm_device *pwm = &chip->pwms[i];
  259. if (test_bit(PWMF_REQUESTED, &pwm->flags)) {
  260. ret = -EBUSY;
  261. goto out;
  262. }
  263. }
  264. list_del_init(&chip->list);
  265. if (IS_ENABLED(CONFIG_OF))
  266. of_pwmchip_remove(chip);
  267. free_pwms(chip);
  268. pwmchip_sysfs_unexport(chip);
  269. out:
  270. mutex_unlock(&pwm_lock);
  271. return ret;
  272. }
  273. EXPORT_SYMBOL_GPL(pwmchip_remove);
  274. /**
  275. * pwm_request() - request a PWM device
  276. * @pwm: global PWM device index
  277. * @label: PWM device label
  278. *
  279. * This function is deprecated, use pwm_get() instead.
  280. *
  281. * Returns: A pointer to a PWM device or an ERR_PTR()-encoded error code on
  282. * failure.
  283. */
  284. struct pwm_device *pwm_request(int pwm, const char *label)
  285. {
  286. struct pwm_device *dev;
  287. int err;
  288. if (pwm < 0 || pwm >= MAX_PWMS)
  289. return ERR_PTR(-EINVAL);
  290. mutex_lock(&pwm_lock);
  291. dev = pwm_to_device(pwm);
  292. if (!dev) {
  293. dev = ERR_PTR(-EPROBE_DEFER);
  294. goto out;
  295. }
  296. err = pwm_device_request(dev, label);
  297. if (err < 0)
  298. dev = ERR_PTR(err);
  299. out:
  300. mutex_unlock(&pwm_lock);
  301. return dev;
  302. }
  303. EXPORT_SYMBOL_GPL(pwm_request);
  304. /**
  305. * pwm_request_from_chip() - request a PWM device relative to a PWM chip
  306. * @chip: PWM chip
  307. * @index: per-chip index of the PWM to request
  308. * @label: a literal description string of this PWM
  309. *
  310. * Returns: A pointer to the PWM device at the given index of the given PWM
  311. * chip. A negative error code is returned if the index is not valid for the
  312. * specified PWM chip or if the PWM device cannot be requested.
  313. */
  314. struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
  315. unsigned int index,
  316. const char *label)
  317. {
  318. struct pwm_device *pwm;
  319. int err;
  320. if (!chip || index >= chip->npwm)
  321. return ERR_PTR(-EINVAL);
  322. mutex_lock(&pwm_lock);
  323. pwm = &chip->pwms[index];
  324. err = pwm_device_request(pwm, label);
  325. if (err < 0)
  326. pwm = ERR_PTR(err);
  327. mutex_unlock(&pwm_lock);
  328. return pwm;
  329. }
  330. EXPORT_SYMBOL_GPL(pwm_request_from_chip);
  331. /**
  332. * pwm_free() - free a PWM device
  333. * @pwm: PWM device
  334. *
  335. * This function is deprecated, use pwm_put() instead.
  336. */
  337. void pwm_free(struct pwm_device *pwm)
  338. {
  339. pwm_put(pwm);
  340. }
  341. EXPORT_SYMBOL_GPL(pwm_free);
  342. /**
  343. * pwm_config() - change a PWM device configuration
  344. * @pwm: PWM device
  345. * @duty_ns: "on" time (in nanoseconds)
  346. * @period_ns: duration (in nanoseconds) of one cycle
  347. *
  348. * Returns: 0 on success or a negative error code on failure.
  349. */
  350. int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
  351. {
  352. int err;
  353. if (!pwm || duty_ns < 0 || period_ns <= 0 || duty_ns > period_ns)
  354. return -EINVAL;
  355. err = pwm->chip->ops->config(pwm->chip, pwm, duty_ns, period_ns);
  356. if (err)
  357. return err;
  358. pwm->duty_cycle = duty_ns;
  359. pwm->period = period_ns;
  360. return 0;
  361. }
  362. EXPORT_SYMBOL_GPL(pwm_config);
  363. /**
  364. * pwm_set_polarity() - configure the polarity of a PWM signal
  365. * @pwm: PWM device
  366. * @polarity: new polarity of the PWM signal
  367. *
  368. * Note that the polarity cannot be configured while the PWM device is
  369. * enabled.
  370. *
  371. * Returns: 0 on success or a negative error code on failure.
  372. */
  373. int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity)
  374. {
  375. int err;
  376. if (!pwm || !pwm->chip->ops)
  377. return -EINVAL;
  378. if (!pwm->chip->ops->set_polarity)
  379. return -ENOSYS;
  380. mutex_lock(&pwm->lock);
  381. if (pwm_is_enabled(pwm)) {
  382. err = -EBUSY;
  383. goto unlock;
  384. }
  385. err = pwm->chip->ops->set_polarity(pwm->chip, pwm, polarity);
  386. if (err)
  387. goto unlock;
  388. pwm->polarity = polarity;
  389. unlock:
  390. mutex_unlock(&pwm->lock);
  391. return err;
  392. }
  393. EXPORT_SYMBOL_GPL(pwm_set_polarity);
  394. /**
  395. * pwm_enable() - start a PWM output toggling
  396. * @pwm: PWM device
  397. *
  398. * Returns: 0 on success or a negative error code on failure.
  399. */
  400. int pwm_enable(struct pwm_device *pwm)
  401. {
  402. int err = 0;
  403. if (!pwm)
  404. return -EINVAL;
  405. mutex_lock(&pwm->lock);
  406. if (!test_and_set_bit(PWMF_ENABLED, &pwm->flags)) {
  407. err = pwm->chip->ops->enable(pwm->chip, pwm);
  408. if (err)
  409. clear_bit(PWMF_ENABLED, &pwm->flags);
  410. }
  411. mutex_unlock(&pwm->lock);
  412. return err;
  413. }
  414. EXPORT_SYMBOL_GPL(pwm_enable);
  415. /**
  416. * pwm_disable() - stop a PWM output toggling
  417. * @pwm: PWM device
  418. */
  419. void pwm_disable(struct pwm_device *pwm)
  420. {
  421. if (pwm && test_and_clear_bit(PWMF_ENABLED, &pwm->flags))
  422. pwm->chip->ops->disable(pwm->chip, pwm);
  423. }
  424. EXPORT_SYMBOL_GPL(pwm_disable);
  425. static struct pwm_chip *of_node_to_pwmchip(struct device_node *np)
  426. {
  427. struct pwm_chip *chip;
  428. mutex_lock(&pwm_lock);
  429. list_for_each_entry(chip, &pwm_chips, list)
  430. if (chip->dev && chip->dev->of_node == np) {
  431. mutex_unlock(&pwm_lock);
  432. return chip;
  433. }
  434. mutex_unlock(&pwm_lock);
  435. return ERR_PTR(-EPROBE_DEFER);
  436. }
  437. /**
  438. * of_pwm_get() - request a PWM via the PWM framework
  439. * @np: device node to get the PWM from
  440. * @con_id: consumer name
  441. *
  442. * Returns the PWM device parsed from the phandle and index specified in the
  443. * "pwms" property of a device tree node or a negative error-code on failure.
  444. * Values parsed from the device tree are stored in the returned PWM device
  445. * object.
  446. *
  447. * If con_id is NULL, the first PWM device listed in the "pwms" property will
  448. * be requested. Otherwise the "pwm-names" property is used to do a reverse
  449. * lookup of the PWM index. This also means that the "pwm-names" property
  450. * becomes mandatory for devices that look up the PWM device via the con_id
  451. * parameter.
  452. *
  453. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  454. * error code on failure.
  455. */
  456. struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id)
  457. {
  458. struct pwm_device *pwm = NULL;
  459. struct of_phandle_args args;
  460. struct pwm_chip *pc;
  461. int index = 0;
  462. int err;
  463. if (con_id) {
  464. index = of_property_match_string(np, "pwm-names", con_id);
  465. if (index < 0)
  466. return ERR_PTR(index);
  467. }
  468. err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
  469. &args);
  470. if (err) {
  471. pr_debug("%s(): can't parse \"pwms\" property\n", __func__);
  472. return ERR_PTR(err);
  473. }
  474. pc = of_node_to_pwmchip(args.np);
  475. if (IS_ERR(pc)) {
  476. pr_debug("%s(): PWM chip not found\n", __func__);
  477. pwm = ERR_CAST(pc);
  478. goto put;
  479. }
  480. if (args.args_count != pc->of_pwm_n_cells) {
  481. pr_debug("%s: wrong #pwm-cells for %s\n", np->full_name,
  482. args.np->full_name);
  483. pwm = ERR_PTR(-EINVAL);
  484. goto put;
  485. }
  486. pwm = pc->of_xlate(pc, &args);
  487. if (IS_ERR(pwm))
  488. goto put;
  489. /*
  490. * If a consumer name was not given, try to look it up from the
  491. * "pwm-names" property if it exists. Otherwise use the name of
  492. * the user device node.
  493. */
  494. if (!con_id) {
  495. err = of_property_read_string_index(np, "pwm-names", index,
  496. &con_id);
  497. if (err < 0)
  498. con_id = np->name;
  499. }
  500. pwm->label = con_id;
  501. put:
  502. of_node_put(args.np);
  503. return pwm;
  504. }
  505. EXPORT_SYMBOL_GPL(of_pwm_get);
  506. /**
  507. * pwm_add_table() - register PWM device consumers
  508. * @table: array of consumers to register
  509. * @num: number of consumers in table
  510. */
  511. void pwm_add_table(struct pwm_lookup *table, size_t num)
  512. {
  513. mutex_lock(&pwm_lookup_lock);
  514. while (num--) {
  515. list_add_tail(&table->list, &pwm_lookup_list);
  516. table++;
  517. }
  518. mutex_unlock(&pwm_lookup_lock);
  519. }
  520. /**
  521. * pwm_remove_table() - unregister PWM device consumers
  522. * @table: array of consumers to unregister
  523. * @num: number of consumers in table
  524. */
  525. void pwm_remove_table(struct pwm_lookup *table, size_t num)
  526. {
  527. mutex_lock(&pwm_lookup_lock);
  528. while (num--) {
  529. list_del(&table->list);
  530. table++;
  531. }
  532. mutex_unlock(&pwm_lookup_lock);
  533. }
  534. /**
  535. * pwm_get() - look up and request a PWM device
  536. * @dev: device for PWM consumer
  537. * @con_id: consumer name
  538. *
  539. * Lookup is first attempted using DT. If the device was not instantiated from
  540. * a device tree, a PWM chip and a relative index is looked up via a table
  541. * supplied by board setup code (see pwm_add_table()).
  542. *
  543. * Once a PWM chip has been found the specified PWM device will be requested
  544. * and is ready to be used.
  545. *
  546. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  547. * error code on failure.
  548. */
  549. struct pwm_device *pwm_get(struct device *dev, const char *con_id)
  550. {
  551. struct pwm_device *pwm = ERR_PTR(-EPROBE_DEFER);
  552. const char *dev_id = dev ? dev_name(dev) : NULL;
  553. struct pwm_chip *chip = NULL;
  554. unsigned int best = 0;
  555. struct pwm_lookup *p, *chosen = NULL;
  556. unsigned int match;
  557. /* look up via DT first */
  558. if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
  559. return of_pwm_get(dev->of_node, con_id);
  560. /*
  561. * We look up the provider in the static table typically provided by
  562. * board setup code. We first try to lookup the consumer device by
  563. * name. If the consumer device was passed in as NULL or if no match
  564. * was found, we try to find the consumer by directly looking it up
  565. * by name.
  566. *
  567. * If a match is found, the provider PWM chip is looked up by name
  568. * and a PWM device is requested using the PWM device per-chip index.
  569. *
  570. * The lookup algorithm was shamelessly taken from the clock
  571. * framework:
  572. *
  573. * We do slightly fuzzy matching here:
  574. * An entry with a NULL ID is assumed to be a wildcard.
  575. * If an entry has a device ID, it must match
  576. * If an entry has a connection ID, it must match
  577. * Then we take the most specific entry - with the following order
  578. * of precedence: dev+con > dev only > con only.
  579. */
  580. mutex_lock(&pwm_lookup_lock);
  581. list_for_each_entry(p, &pwm_lookup_list, list) {
  582. match = 0;
  583. if (p->dev_id) {
  584. if (!dev_id || strcmp(p->dev_id, dev_id))
  585. continue;
  586. match += 2;
  587. }
  588. if (p->con_id) {
  589. if (!con_id || strcmp(p->con_id, con_id))
  590. continue;
  591. match += 1;
  592. }
  593. if (match > best) {
  594. chosen = p;
  595. if (match != 3)
  596. best = match;
  597. else
  598. break;
  599. }
  600. }
  601. if (!chosen) {
  602. pwm = ERR_PTR(-ENODEV);
  603. goto out;
  604. }
  605. chip = pwmchip_find_by_name(chosen->provider);
  606. if (!chip)
  607. goto out;
  608. pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
  609. if (IS_ERR(pwm))
  610. goto out;
  611. pwm_set_period(pwm, chosen->period);
  612. pwm_set_polarity(pwm, chosen->polarity);
  613. out:
  614. mutex_unlock(&pwm_lookup_lock);
  615. return pwm;
  616. }
  617. EXPORT_SYMBOL_GPL(pwm_get);
  618. /**
  619. * pwm_put() - release a PWM device
  620. * @pwm: PWM device
  621. */
  622. void pwm_put(struct pwm_device *pwm)
  623. {
  624. if (!pwm)
  625. return;
  626. mutex_lock(&pwm_lock);
  627. if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
  628. pr_warn("PWM device already freed\n");
  629. goto out;
  630. }
  631. if (pwm->chip->ops->free)
  632. pwm->chip->ops->free(pwm->chip, pwm);
  633. pwm->label = NULL;
  634. module_put(pwm->chip->ops->owner);
  635. out:
  636. mutex_unlock(&pwm_lock);
  637. }
  638. EXPORT_SYMBOL_GPL(pwm_put);
  639. static void devm_pwm_release(struct device *dev, void *res)
  640. {
  641. pwm_put(*(struct pwm_device **)res);
  642. }
  643. /**
  644. * devm_pwm_get() - resource managed pwm_get()
  645. * @dev: device for PWM consumer
  646. * @con_id: consumer name
  647. *
  648. * This function performs like pwm_get() but the acquired PWM device will
  649. * automatically be released on driver detach.
  650. *
  651. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  652. * error code on failure.
  653. */
  654. struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
  655. {
  656. struct pwm_device **ptr, *pwm;
  657. ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
  658. if (!ptr)
  659. return ERR_PTR(-ENOMEM);
  660. pwm = pwm_get(dev, con_id);
  661. if (!IS_ERR(pwm)) {
  662. *ptr = pwm;
  663. devres_add(dev, ptr);
  664. } else {
  665. devres_free(ptr);
  666. }
  667. return pwm;
  668. }
  669. EXPORT_SYMBOL_GPL(devm_pwm_get);
  670. /**
  671. * devm_of_pwm_get() - resource managed of_pwm_get()
  672. * @dev: device for PWM consumer
  673. * @np: device node to get the PWM from
  674. * @con_id: consumer name
  675. *
  676. * This function performs like of_pwm_get() but the acquired PWM device will
  677. * automatically be released on driver detach.
  678. *
  679. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  680. * error code on failure.
  681. */
  682. struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
  683. const char *con_id)
  684. {
  685. struct pwm_device **ptr, *pwm;
  686. ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
  687. if (!ptr)
  688. return ERR_PTR(-ENOMEM);
  689. pwm = of_pwm_get(np, con_id);
  690. if (!IS_ERR(pwm)) {
  691. *ptr = pwm;
  692. devres_add(dev, ptr);
  693. } else {
  694. devres_free(ptr);
  695. }
  696. return pwm;
  697. }
  698. EXPORT_SYMBOL_GPL(devm_of_pwm_get);
  699. static int devm_pwm_match(struct device *dev, void *res, void *data)
  700. {
  701. struct pwm_device **p = res;
  702. if (WARN_ON(!p || !*p))
  703. return 0;
  704. return *p == data;
  705. }
  706. /**
  707. * devm_pwm_put() - resource managed pwm_put()
  708. * @dev: device for PWM consumer
  709. * @pwm: PWM device
  710. *
  711. * Release a PWM previously allocated using devm_pwm_get(). Calling this
  712. * function is usually not needed because devm-allocated resources are
  713. * automatically released on driver detach.
  714. */
  715. void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
  716. {
  717. WARN_ON(devres_release(dev, devm_pwm_release, devm_pwm_match, pwm));
  718. }
  719. EXPORT_SYMBOL_GPL(devm_pwm_put);
  720. /**
  721. * pwm_can_sleep() - report whether PWM access will sleep
  722. * @pwm: PWM device
  723. *
  724. * Returns: True if accessing the PWM can sleep, false otherwise.
  725. */
  726. bool pwm_can_sleep(struct pwm_device *pwm)
  727. {
  728. return true;
  729. }
  730. EXPORT_SYMBOL_GPL(pwm_can_sleep);
  731. #ifdef CONFIG_DEBUG_FS
  732. static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
  733. {
  734. unsigned int i;
  735. for (i = 0; i < chip->npwm; i++) {
  736. struct pwm_device *pwm = &chip->pwms[i];
  737. seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
  738. if (test_bit(PWMF_REQUESTED, &pwm->flags))
  739. seq_puts(s, " requested");
  740. if (pwm_is_enabled(pwm))
  741. seq_puts(s, " enabled");
  742. seq_puts(s, "\n");
  743. }
  744. }
  745. static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
  746. {
  747. mutex_lock(&pwm_lock);
  748. s->private = "";
  749. return seq_list_start(&pwm_chips, *pos);
  750. }
  751. static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
  752. {
  753. s->private = "\n";
  754. return seq_list_next(v, &pwm_chips, pos);
  755. }
  756. static void pwm_seq_stop(struct seq_file *s, void *v)
  757. {
  758. mutex_unlock(&pwm_lock);
  759. }
  760. static int pwm_seq_show(struct seq_file *s, void *v)
  761. {
  762. struct pwm_chip *chip = list_entry(v, struct pwm_chip, list);
  763. seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private,
  764. chip->dev->bus ? chip->dev->bus->name : "no-bus",
  765. dev_name(chip->dev), chip->npwm,
  766. (chip->npwm != 1) ? "s" : "");
  767. if (chip->ops->dbg_show)
  768. chip->ops->dbg_show(chip, s);
  769. else
  770. pwm_dbg_show(chip, s);
  771. return 0;
  772. }
  773. static const struct seq_operations pwm_seq_ops = {
  774. .start = pwm_seq_start,
  775. .next = pwm_seq_next,
  776. .stop = pwm_seq_stop,
  777. .show = pwm_seq_show,
  778. };
  779. static int pwm_seq_open(struct inode *inode, struct file *file)
  780. {
  781. return seq_open(file, &pwm_seq_ops);
  782. }
  783. static const struct file_operations pwm_debugfs_ops = {
  784. .owner = THIS_MODULE,
  785. .open = pwm_seq_open,
  786. .read = seq_read,
  787. .llseek = seq_lseek,
  788. .release = seq_release,
  789. };
  790. static int __init pwm_debugfs_init(void)
  791. {
  792. debugfs_create_file("pwm", S_IFREG | S_IRUGO, NULL, NULL,
  793. &pwm_debugfs_ops);
  794. return 0;
  795. }
  796. subsys_initcall(pwm_debugfs_init);
  797. #endif /* CONFIG_DEBUG_FS */