core.c 20 KB

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