core.c 19 KB

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