core.c 24 KB

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