core.c 21 KB

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