core.c 19 KB

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