core.c 22 KB

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