core.c 18 KB

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