core.c 24 KB

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