core.c 17 KB

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