core.c 19 KB

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