core.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Generic pwmlib implementation
  4. *
  5. * Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de>
  6. * Copyright (C) 2011-2012 Avionic Design GmbH
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/module.h>
  10. #include <linux/pwm.h>
  11. #include <linux/radix-tree.h>
  12. #include <linux/list.h>
  13. #include <linux/mutex.h>
  14. #include <linux/err.h>
  15. #include <linux/slab.h>
  16. #include <linux/device.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/seq_file.h>
  19. #include <dt-bindings/pwm/pwm.h>
  20. #define CREATE_TRACE_POINTS
  21. #include <trace/events/pwm.h>
  22. #define MAX_PWMS 1024
  23. static DEFINE_MUTEX(pwm_lookup_lock);
  24. static LIST_HEAD(pwm_lookup_list);
  25. static DEFINE_MUTEX(pwm_lock);
  26. static LIST_HEAD(pwm_chips);
  27. static DECLARE_BITMAP(allocated_pwms, MAX_PWMS);
  28. static RADIX_TREE(pwm_tree, GFP_KERNEL);
  29. static struct pwm_device *pwm_to_device(unsigned int pwm)
  30. {
  31. return radix_tree_lookup(&pwm_tree, pwm);
  32. }
  33. static int alloc_pwms(int pwm, unsigned int count)
  34. {
  35. unsigned int from = 0;
  36. unsigned int start;
  37. if (pwm >= MAX_PWMS)
  38. return -EINVAL;
  39. if (pwm >= 0)
  40. from = pwm;
  41. start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, from,
  42. count, 0);
  43. if (pwm >= 0 && start != pwm)
  44. return -EEXIST;
  45. if (start + count > MAX_PWMS)
  46. return -ENOSPC;
  47. return start;
  48. }
  49. static void free_pwms(struct pwm_chip *chip)
  50. {
  51. unsigned int i;
  52. for (i = 0; i < chip->npwm; i++) {
  53. struct pwm_device *pwm = &chip->pwms[i];
  54. radix_tree_delete(&pwm_tree, pwm->pwm);
  55. }
  56. bitmap_clear(allocated_pwms, chip->base, chip->npwm);
  57. kfree(chip->pwms);
  58. chip->pwms = NULL;
  59. }
  60. static struct pwm_chip *pwmchip_find_by_name(const char *name)
  61. {
  62. struct pwm_chip *chip;
  63. if (!name)
  64. return NULL;
  65. mutex_lock(&pwm_lock);
  66. list_for_each_entry(chip, &pwm_chips, list) {
  67. const char *chip_name = dev_name(chip->dev);
  68. if (chip_name && strcmp(chip_name, name) == 0) {
  69. mutex_unlock(&pwm_lock);
  70. return chip;
  71. }
  72. }
  73. mutex_unlock(&pwm_lock);
  74. return NULL;
  75. }
  76. static int pwm_device_request(struct pwm_device *pwm, const char *label)
  77. {
  78. int err;
  79. if (test_bit(PWMF_REQUESTED, &pwm->flags))
  80. return -EBUSY;
  81. if (!try_module_get(pwm->chip->ops->owner))
  82. return -ENODEV;
  83. if (pwm->chip->ops->request) {
  84. err = pwm->chip->ops->request(pwm->chip, pwm);
  85. if (err) {
  86. module_put(pwm->chip->ops->owner);
  87. return err;
  88. }
  89. }
  90. if (pwm->chip->ops->get_state) {
  91. pwm->chip->ops->get_state(pwm->chip, pwm, &pwm->state);
  92. trace_pwm_get(pwm, &pwm->state);
  93. if (IS_ENABLED(CONFIG_PWM_DEBUG))
  94. pwm->last = pwm->state;
  95. }
  96. set_bit(PWMF_REQUESTED, &pwm->flags);
  97. pwm->label = label;
  98. return 0;
  99. }
  100. struct pwm_device *
  101. of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args)
  102. {
  103. struct pwm_device *pwm;
  104. /* check, whether the driver supports a third cell for flags */
  105. if (pc->of_pwm_n_cells < 3)
  106. return ERR_PTR(-EINVAL);
  107. /* flags in the third cell are optional */
  108. if (args->args_count < 2)
  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. pwm->args.polarity = PWM_POLARITY_NORMAL;
  117. if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED)
  118. pwm->args.polarity = PWM_POLARITY_INVERSED;
  119. return pwm;
  120. }
  121. EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
  122. static struct pwm_device *
  123. of_pwm_simple_xlate(struct pwm_chip *pc, const struct of_phandle_args *args)
  124. {
  125. struct pwm_device *pwm;
  126. /* sanity check driver support */
  127. if (pc->of_pwm_n_cells < 2)
  128. return ERR_PTR(-EINVAL);
  129. /* all cells are required */
  130. if (args->args_count != pc->of_pwm_n_cells)
  131. return ERR_PTR(-EINVAL);
  132. if (args->args[0] >= pc->npwm)
  133. return ERR_PTR(-EINVAL);
  134. pwm = pwm_request_from_chip(pc, args->args[0], NULL);
  135. if (IS_ERR(pwm))
  136. return pwm;
  137. pwm->args.period = args->args[1];
  138. return pwm;
  139. }
  140. static void of_pwmchip_add(struct pwm_chip *chip)
  141. {
  142. if (!chip->dev || !chip->dev->of_node)
  143. return;
  144. if (!chip->of_xlate) {
  145. chip->of_xlate = of_pwm_simple_xlate;
  146. chip->of_pwm_n_cells = 2;
  147. }
  148. of_node_get(chip->dev->of_node);
  149. }
  150. static void of_pwmchip_remove(struct pwm_chip *chip)
  151. {
  152. if (chip->dev)
  153. of_node_put(chip->dev->of_node);
  154. }
  155. /**
  156. * pwm_set_chip_data() - set private chip data for a PWM
  157. * @pwm: PWM device
  158. * @data: pointer to chip-specific data
  159. *
  160. * Returns: 0 on success or a negative error code on failure.
  161. */
  162. int pwm_set_chip_data(struct pwm_device *pwm, void *data)
  163. {
  164. if (!pwm)
  165. return -EINVAL;
  166. pwm->chip_data = data;
  167. return 0;
  168. }
  169. EXPORT_SYMBOL_GPL(pwm_set_chip_data);
  170. /**
  171. * pwm_get_chip_data() - get private chip data for a PWM
  172. * @pwm: PWM device
  173. *
  174. * Returns: A pointer to the chip-private data for the PWM device.
  175. */
  176. void *pwm_get_chip_data(struct pwm_device *pwm)
  177. {
  178. return pwm ? pwm->chip_data : NULL;
  179. }
  180. EXPORT_SYMBOL_GPL(pwm_get_chip_data);
  181. static bool pwm_ops_check(const struct pwm_chip *chip)
  182. {
  183. const struct pwm_ops *ops = chip->ops;
  184. /* driver supports legacy, non-atomic operation */
  185. if (ops->config && ops->enable && ops->disable) {
  186. if (IS_ENABLED(CONFIG_PWM_DEBUG))
  187. dev_warn(chip->dev,
  188. "Driver needs updating to atomic API\n");
  189. return true;
  190. }
  191. if (!ops->apply)
  192. return false;
  193. if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state)
  194. dev_warn(chip->dev,
  195. "Please implement the .get_state() callback\n");
  196. return true;
  197. }
  198. /**
  199. * pwmchip_add_with_polarity() - register a new PWM chip
  200. * @chip: the PWM chip to add
  201. * @polarity: initial polarity of PWM channels
  202. *
  203. * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
  204. * will be used. The initial polarity for all channels is specified by the
  205. * @polarity parameter.
  206. *
  207. * Returns: 0 on success or a negative error code on failure.
  208. */
  209. int pwmchip_add_with_polarity(struct pwm_chip *chip,
  210. enum pwm_polarity polarity)
  211. {
  212. struct pwm_device *pwm;
  213. unsigned int i;
  214. int ret;
  215. if (!chip || !chip->dev || !chip->ops || !chip->npwm)
  216. return -EINVAL;
  217. if (!pwm_ops_check(chip))
  218. return -EINVAL;
  219. mutex_lock(&pwm_lock);
  220. ret = alloc_pwms(chip->base, chip->npwm);
  221. if (ret < 0)
  222. goto out;
  223. chip->pwms = kcalloc(chip->npwm, sizeof(*pwm), GFP_KERNEL);
  224. if (!chip->pwms) {
  225. ret = -ENOMEM;
  226. goto out;
  227. }
  228. chip->base = ret;
  229. for (i = 0; i < chip->npwm; i++) {
  230. pwm = &chip->pwms[i];
  231. pwm->chip = chip;
  232. pwm->pwm = chip->base + i;
  233. pwm->hwpwm = i;
  234. pwm->state.polarity = polarity;
  235. pwm->state.output_type = PWM_OUTPUT_FIXED;
  236. radix_tree_insert(&pwm_tree, pwm->pwm, pwm);
  237. }
  238. bitmap_set(allocated_pwms, chip->base, chip->npwm);
  239. INIT_LIST_HEAD(&chip->list);
  240. list_add(&chip->list, &pwm_chips);
  241. ret = 0;
  242. if (IS_ENABLED(CONFIG_OF))
  243. of_pwmchip_add(chip);
  244. out:
  245. mutex_unlock(&pwm_lock);
  246. if (!ret)
  247. pwmchip_sysfs_export(chip);
  248. return ret;
  249. }
  250. EXPORT_SYMBOL_GPL(pwmchip_add_with_polarity);
  251. /**
  252. * pwmchip_add() - register a new PWM chip
  253. * @chip: the PWM chip to add
  254. *
  255. * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
  256. * will be used. The initial polarity for all channels is normal.
  257. *
  258. * Returns: 0 on success or a negative error code on failure.
  259. */
  260. int pwmchip_add(struct pwm_chip *chip)
  261. {
  262. return pwmchip_add_with_polarity(chip, PWM_POLARITY_NORMAL);
  263. }
  264. EXPORT_SYMBOL_GPL(pwmchip_add);
  265. /**
  266. * pwmchip_remove() - remove a PWM chip
  267. * @chip: the PWM chip to remove
  268. *
  269. * Removes a PWM chip. This function may return busy if the PWM chip provides
  270. * a PWM device that is still requested.
  271. *
  272. * Returns: 0 on success or a negative error code on failure.
  273. */
  274. int pwmchip_remove(struct pwm_chip *chip)
  275. {
  276. unsigned int i;
  277. int ret = 0;
  278. pwmchip_sysfs_unexport(chip);
  279. mutex_lock(&pwm_lock);
  280. for (i = 0; i < chip->npwm; i++) {
  281. struct pwm_device *pwm = &chip->pwms[i];
  282. if (test_bit(PWMF_REQUESTED, &pwm->flags)) {
  283. ret = -EBUSY;
  284. goto out;
  285. }
  286. }
  287. list_del_init(&chip->list);
  288. if (IS_ENABLED(CONFIG_OF))
  289. of_pwmchip_remove(chip);
  290. free_pwms(chip);
  291. out:
  292. mutex_unlock(&pwm_lock);
  293. return ret;
  294. }
  295. EXPORT_SYMBOL_GPL(pwmchip_remove);
  296. /**
  297. * pwm_request() - request a PWM device
  298. * @pwm: global PWM device index
  299. * @label: PWM device label
  300. *
  301. * This function is deprecated, use pwm_get() instead.
  302. *
  303. * Returns: A pointer to a PWM device or an ERR_PTR()-encoded error code on
  304. * failure.
  305. */
  306. struct pwm_device *pwm_request(int pwm, const char *label)
  307. {
  308. struct pwm_device *dev;
  309. int err;
  310. if (pwm < 0 || pwm >= MAX_PWMS)
  311. return ERR_PTR(-EINVAL);
  312. mutex_lock(&pwm_lock);
  313. dev = pwm_to_device(pwm);
  314. if (!dev) {
  315. dev = ERR_PTR(-EPROBE_DEFER);
  316. goto out;
  317. }
  318. err = pwm_device_request(dev, label);
  319. if (err < 0)
  320. dev = ERR_PTR(err);
  321. out:
  322. mutex_unlock(&pwm_lock);
  323. return dev;
  324. }
  325. EXPORT_SYMBOL_GPL(pwm_request);
  326. /**
  327. * pwm_request_from_chip() - request a PWM device relative to a PWM chip
  328. * @chip: PWM chip
  329. * @index: per-chip index of the PWM to request
  330. * @label: a literal description string of this PWM
  331. *
  332. * Returns: A pointer to the PWM device at the given index of the given PWM
  333. * chip. A negative error code is returned if the index is not valid for the
  334. * specified PWM chip or if the PWM device cannot be requested.
  335. */
  336. struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
  337. unsigned int index,
  338. const char *label)
  339. {
  340. struct pwm_device *pwm;
  341. int err;
  342. if (!chip || index >= chip->npwm)
  343. return ERR_PTR(-EINVAL);
  344. mutex_lock(&pwm_lock);
  345. pwm = &chip->pwms[index];
  346. err = pwm_device_request(pwm, label);
  347. if (err < 0)
  348. pwm = ERR_PTR(err);
  349. mutex_unlock(&pwm_lock);
  350. return pwm;
  351. }
  352. EXPORT_SYMBOL_GPL(pwm_request_from_chip);
  353. /**
  354. * pwm_free() - free a PWM device
  355. * @pwm: PWM device
  356. *
  357. * This function is deprecated, use pwm_put() instead.
  358. */
  359. void pwm_free(struct pwm_device *pwm)
  360. {
  361. pwm_put(pwm);
  362. }
  363. EXPORT_SYMBOL_GPL(pwm_free);
  364. static void pwm_apply_state_debug(struct pwm_device *pwm,
  365. const struct pwm_state *state)
  366. {
  367. struct pwm_state *last = &pwm->last;
  368. struct pwm_chip *chip = pwm->chip;
  369. struct pwm_state s1, s2;
  370. int err;
  371. if (!IS_ENABLED(CONFIG_PWM_DEBUG))
  372. return;
  373. /* No reasonable diagnosis possible without .get_state() */
  374. if (!chip->ops->get_state)
  375. return;
  376. /*
  377. * *state was just applied. Read out the hardware state and do some
  378. * checks.
  379. */
  380. chip->ops->get_state(chip, pwm, &s1);
  381. trace_pwm_get(pwm, &s1);
  382. /*
  383. * The lowlevel driver either ignored .polarity (which is a bug) or as
  384. * best effort inverted .polarity and fixed .duty_cycle respectively.
  385. * Undo this inversion and fixup for further tests.
  386. */
  387. if (s1.enabled && s1.polarity != state->polarity) {
  388. s2.polarity = state->polarity;
  389. s2.duty_cycle = s1.period - s1.duty_cycle;
  390. s2.period = s1.period;
  391. s2.enabled = s1.enabled;
  392. } else {
  393. s2 = s1;
  394. }
  395. if (s2.polarity != state->polarity &&
  396. state->duty_cycle < state->period)
  397. dev_warn(chip->dev, ".apply ignored .polarity\n");
  398. if (state->enabled &&
  399. last->polarity == state->polarity &&
  400. last->period > s2.period &&
  401. last->period <= state->period)
  402. dev_warn(chip->dev,
  403. ".apply didn't pick the best available period (requested: %llu, applied: %llu, possible: %llu)\n",
  404. state->period, s2.period, last->period);
  405. if (state->enabled && state->period < s2.period)
  406. dev_warn(chip->dev,
  407. ".apply is supposed to round down period (requested: %llu, applied: %llu)\n",
  408. state->period, s2.period);
  409. if (state->enabled &&
  410. last->polarity == state->polarity &&
  411. last->period == s2.period &&
  412. last->duty_cycle > s2.duty_cycle &&
  413. last->duty_cycle <= state->duty_cycle)
  414. dev_warn(chip->dev,
  415. ".apply didn't pick the best available duty cycle (requested: %llu/%llu, applied: %llu/%llu, possible: %llu/%llu)\n",
  416. state->duty_cycle, state->period,
  417. s2.duty_cycle, s2.period,
  418. last->duty_cycle, last->period);
  419. if (state->enabled && state->duty_cycle < s2.duty_cycle)
  420. dev_warn(chip->dev,
  421. ".apply is supposed to round down duty_cycle (requested: %llu/%llu, applied: %llu/%llu)\n",
  422. state->duty_cycle, state->period,
  423. s2.duty_cycle, s2.period);
  424. if (!state->enabled && s2.enabled && s2.duty_cycle > 0)
  425. dev_warn(chip->dev,
  426. "requested disabled, but yielded enabled with duty > 0\n");
  427. /* reapply the state that the driver reported being configured. */
  428. err = chip->ops->apply(chip, pwm, &s1);
  429. if (err) {
  430. *last = s1;
  431. dev_err(chip->dev, "failed to reapply current setting\n");
  432. return;
  433. }
  434. trace_pwm_apply(pwm, &s1);
  435. chip->ops->get_state(chip, pwm, last);
  436. trace_pwm_get(pwm, last);
  437. /* reapplication of the current state should give an exact match */
  438. if (s1.enabled != last->enabled ||
  439. s1.polarity != last->polarity ||
  440. (s1.enabled && s1.period != last->period) ||
  441. (s1.enabled && s1.duty_cycle != last->duty_cycle)) {
  442. dev_err(chip->dev,
  443. ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n",
  444. s1.enabled, s1.polarity, s1.duty_cycle, s1.period,
  445. last->enabled, last->polarity, last->duty_cycle,
  446. last->period);
  447. }
  448. }
  449. /**
  450. * pwm_apply_state() - atomically apply a new state to a PWM device
  451. * @pwm: PWM device
  452. * @state: new state to apply
  453. */
  454. int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state)
  455. {
  456. struct pwm_chip *chip;
  457. int err;
  458. if (!pwm || !state || !state->period ||
  459. state->duty_cycle > state->period)
  460. return -EINVAL;
  461. chip = pwm->chip;
  462. if (state->period == pwm->state.period &&
  463. state->duty_cycle == pwm->state.duty_cycle &&
  464. state->polarity == pwm->state.polarity &&
  465. state->enabled == pwm->state.enabled)
  466. return 0;
  467. if (chip->ops->apply) {
  468. err = chip->ops->apply(chip, pwm, state);
  469. if (err)
  470. return err;
  471. trace_pwm_apply(pwm, state);
  472. pwm->state = *state;
  473. /*
  474. * only do this after pwm->state was applied as some
  475. * implementations of .get_state depend on this
  476. */
  477. pwm_apply_state_debug(pwm, state);
  478. } else {
  479. /*
  480. * FIXME: restore the initial state in case of error.
  481. */
  482. if (state->polarity != pwm->state.polarity) {
  483. if (!chip->ops->set_polarity)
  484. return -ENOTSUPP;
  485. /*
  486. * Changing the polarity of a running PWM is
  487. * only allowed when the PWM driver implements
  488. * ->apply().
  489. */
  490. if (pwm->state.enabled) {
  491. chip->ops->disable(chip, pwm);
  492. pwm->state.enabled = false;
  493. }
  494. err = chip->ops->set_polarity(chip, pwm,
  495. state->polarity);
  496. if (err)
  497. return err;
  498. pwm->state.polarity = state->polarity;
  499. }
  500. if (state->period != pwm->state.period ||
  501. state->duty_cycle != pwm->state.duty_cycle) {
  502. err = chip->ops->config(pwm->chip, pwm,
  503. state->duty_cycle,
  504. state->period);
  505. if (err)
  506. return err;
  507. pwm->state.duty_cycle = state->duty_cycle;
  508. pwm->state.period = state->period;
  509. }
  510. if (state->enabled != pwm->state.enabled) {
  511. if (state->enabled) {
  512. err = chip->ops->enable(chip, pwm);
  513. if (err)
  514. return err;
  515. } else {
  516. chip->ops->disable(chip, pwm);
  517. }
  518. pwm->state.enabled = state->enabled;
  519. }
  520. }
  521. return 0;
  522. }
  523. EXPORT_SYMBOL_GPL(pwm_apply_state);
  524. /**
  525. * pwm_capture() - capture and report a PWM signal
  526. * @pwm: PWM device
  527. * @result: structure to fill with capture result
  528. * @timeout: time to wait, in milliseconds, before giving up on capture
  529. *
  530. * Returns: 0 on success or a negative error code on failure.
  531. */
  532. int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
  533. unsigned long timeout)
  534. {
  535. int err;
  536. if (!pwm || !pwm->chip->ops)
  537. return -EINVAL;
  538. if (!pwm->chip->ops->capture)
  539. return -ENOSYS;
  540. mutex_lock(&pwm_lock);
  541. err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout);
  542. mutex_unlock(&pwm_lock);
  543. return err;
  544. }
  545. EXPORT_SYMBOL_GPL(pwm_capture);
  546. /**
  547. * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
  548. * @pwm: PWM device
  549. *
  550. * This function will adjust the PWM config to the PWM arguments provided
  551. * by the DT or PWM lookup table. This is particularly useful to adapt
  552. * the bootloader config to the Linux one.
  553. */
  554. int pwm_adjust_config(struct pwm_device *pwm)
  555. {
  556. struct pwm_state state;
  557. struct pwm_args pargs;
  558. pwm_get_args(pwm, &pargs);
  559. pwm_get_state(pwm, &state);
  560. /*
  561. * If the current period is zero it means that either the PWM driver
  562. * does not support initial state retrieval or the PWM has not yet
  563. * been configured.
  564. *
  565. * In either case, we setup the new period and polarity, and assign a
  566. * duty cycle of 0.
  567. */
  568. if (!state.period) {
  569. state.duty_cycle = 0;
  570. state.period = pargs.period;
  571. state.polarity = pargs.polarity;
  572. return pwm_apply_state(pwm, &state);
  573. }
  574. /*
  575. * Adjust the PWM duty cycle/period based on the period value provided
  576. * in PWM args.
  577. */
  578. if (pargs.period != state.period) {
  579. u64 dutycycle = (u64)state.duty_cycle * pargs.period;
  580. do_div(dutycycle, state.period);
  581. state.duty_cycle = dutycycle;
  582. state.period = pargs.period;
  583. }
  584. /*
  585. * If the polarity changed, we should also change the duty cycle.
  586. */
  587. if (pargs.polarity != state.polarity) {
  588. state.polarity = pargs.polarity;
  589. state.duty_cycle = state.period - state.duty_cycle;
  590. }
  591. return pwm_apply_state(pwm, &state);
  592. }
  593. EXPORT_SYMBOL_GPL(pwm_adjust_config);
  594. static struct pwm_chip *of_node_to_pwmchip(struct device_node *np)
  595. {
  596. struct pwm_chip *chip;
  597. mutex_lock(&pwm_lock);
  598. list_for_each_entry(chip, &pwm_chips, list)
  599. if (chip->dev && chip->dev->of_node == np) {
  600. mutex_unlock(&pwm_lock);
  601. return chip;
  602. }
  603. mutex_unlock(&pwm_lock);
  604. return ERR_PTR(-EPROBE_DEFER);
  605. }
  606. static struct device_link *pwm_device_link_add(struct device *dev,
  607. struct pwm_device *pwm)
  608. {
  609. struct device_link *dl;
  610. if (!dev) {
  611. /*
  612. * No device for the PWM consumer has been provided. It may
  613. * impact the PM sequence ordering: the PWM supplier may get
  614. * suspended before the consumer.
  615. */
  616. dev_warn(pwm->chip->dev,
  617. "No consumer device specified to create a link to\n");
  618. return NULL;
  619. }
  620. dl = device_link_add(dev, pwm->chip->dev, DL_FLAG_AUTOREMOVE_CONSUMER);
  621. if (!dl) {
  622. dev_err(dev, "failed to create device link to %s\n",
  623. dev_name(pwm->chip->dev));
  624. return ERR_PTR(-EINVAL);
  625. }
  626. return dl;
  627. }
  628. /**
  629. * of_pwm_get() - request a PWM via the PWM framework
  630. * @dev: device for PWM consumer
  631. * @np: device node to get the PWM from
  632. * @con_id: consumer name
  633. *
  634. * Returns the PWM device parsed from the phandle and index specified in the
  635. * "pwms" property of a device tree node or a negative error-code on failure.
  636. * Values parsed from the device tree are stored in the returned PWM device
  637. * object.
  638. *
  639. * If con_id is NULL, the first PWM device listed in the "pwms" property will
  640. * be requested. Otherwise the "pwm-names" property is used to do a reverse
  641. * lookup of the PWM index. This also means that the "pwm-names" property
  642. * becomes mandatory for devices that look up the PWM device via the con_id
  643. * parameter.
  644. *
  645. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  646. * error code on failure.
  647. */
  648. struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
  649. const char *con_id)
  650. {
  651. struct pwm_device *pwm = NULL;
  652. struct of_phandle_args args;
  653. struct device_link *dl;
  654. struct pwm_chip *pc;
  655. int index = 0;
  656. int err;
  657. if (con_id) {
  658. index = of_property_match_string(np, "pwm-names", con_id);
  659. if (index < 0)
  660. return ERR_PTR(index);
  661. }
  662. err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
  663. &args);
  664. if (err) {
  665. pr_err("%s(): can't parse \"pwms\" property\n", __func__);
  666. return ERR_PTR(err);
  667. }
  668. pc = of_node_to_pwmchip(args.np);
  669. if (IS_ERR(pc)) {
  670. if (PTR_ERR(pc) != -EPROBE_DEFER)
  671. pr_err("%s(): PWM chip not found\n", __func__);
  672. pwm = ERR_CAST(pc);
  673. goto put;
  674. }
  675. pwm = pc->of_xlate(pc, &args);
  676. if (IS_ERR(pwm))
  677. goto put;
  678. dl = pwm_device_link_add(dev, pwm);
  679. if (IS_ERR(dl)) {
  680. /* of_xlate ended up calling pwm_request_from_chip() */
  681. pwm_free(pwm);
  682. pwm = ERR_CAST(dl);
  683. goto put;
  684. }
  685. /*
  686. * If a consumer name was not given, try to look it up from the
  687. * "pwm-names" property if it exists. Otherwise use the name of
  688. * the user device node.
  689. */
  690. if (!con_id) {
  691. err = of_property_read_string_index(np, "pwm-names", index,
  692. &con_id);
  693. if (err < 0)
  694. con_id = np->name;
  695. }
  696. pwm->label = con_id;
  697. put:
  698. of_node_put(args.np);
  699. return pwm;
  700. }
  701. EXPORT_SYMBOL_GPL(of_pwm_get);
  702. #if IS_ENABLED(CONFIG_ACPI)
  703. static struct pwm_chip *device_to_pwmchip(struct device *dev)
  704. {
  705. struct pwm_chip *chip;
  706. mutex_lock(&pwm_lock);
  707. list_for_each_entry(chip, &pwm_chips, list) {
  708. struct acpi_device *adev = ACPI_COMPANION(chip->dev);
  709. if ((chip->dev == dev) || (adev && &adev->dev == dev)) {
  710. mutex_unlock(&pwm_lock);
  711. return chip;
  712. }
  713. }
  714. mutex_unlock(&pwm_lock);
  715. return ERR_PTR(-EPROBE_DEFER);
  716. }
  717. #endif
  718. /**
  719. * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
  720. * @fwnode: firmware node to get the "pwm" property from
  721. *
  722. * Returns the PWM device parsed from the fwnode and index specified in the
  723. * "pwms" property or a negative error-code on failure.
  724. * Values parsed from the device tree are stored in the returned PWM device
  725. * object.
  726. *
  727. * This is analogous to of_pwm_get() except con_id is not yet supported.
  728. * ACPI entries must look like
  729. * Package () {"pwms", Package ()
  730. * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
  731. *
  732. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  733. * error code on failure.
  734. */
  735. static struct pwm_device *acpi_pwm_get(struct fwnode_handle *fwnode)
  736. {
  737. struct pwm_device *pwm = ERR_PTR(-ENODEV);
  738. #if IS_ENABLED(CONFIG_ACPI)
  739. struct fwnode_reference_args args;
  740. struct acpi_device *acpi;
  741. struct pwm_chip *chip;
  742. int ret;
  743. memset(&args, 0, sizeof(args));
  744. ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args);
  745. if (ret < 0)
  746. return ERR_PTR(ret);
  747. acpi = to_acpi_device_node(args.fwnode);
  748. if (!acpi)
  749. return ERR_PTR(-EINVAL);
  750. if (args.nargs < 2)
  751. return ERR_PTR(-EPROTO);
  752. chip = device_to_pwmchip(&acpi->dev);
  753. if (IS_ERR(chip))
  754. return ERR_CAST(chip);
  755. pwm = pwm_request_from_chip(chip, args.args[0], NULL);
  756. if (IS_ERR(pwm))
  757. return pwm;
  758. pwm->args.period = args.args[1];
  759. pwm->args.polarity = PWM_POLARITY_NORMAL;
  760. if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED)
  761. pwm->args.polarity = PWM_POLARITY_INVERSED;
  762. #endif
  763. return pwm;
  764. }
  765. /**
  766. * pwm_add_table() - register PWM device consumers
  767. * @table: array of consumers to register
  768. * @num: number of consumers in table
  769. */
  770. void pwm_add_table(struct pwm_lookup *table, size_t num)
  771. {
  772. mutex_lock(&pwm_lookup_lock);
  773. while (num--) {
  774. list_add_tail(&table->list, &pwm_lookup_list);
  775. table++;
  776. }
  777. mutex_unlock(&pwm_lookup_lock);
  778. }
  779. /**
  780. * pwm_remove_table() - unregister PWM device consumers
  781. * @table: array of consumers to unregister
  782. * @num: number of consumers in table
  783. */
  784. void pwm_remove_table(struct pwm_lookup *table, size_t num)
  785. {
  786. mutex_lock(&pwm_lookup_lock);
  787. while (num--) {
  788. list_del(&table->list);
  789. table++;
  790. }
  791. mutex_unlock(&pwm_lookup_lock);
  792. }
  793. /**
  794. * pwm_get() - look up and request a PWM device
  795. * @dev: device for PWM consumer
  796. * @con_id: consumer name
  797. *
  798. * Lookup is first attempted using DT. If the device was not instantiated from
  799. * a device tree, a PWM chip and a relative index is looked up via a table
  800. * supplied by board setup code (see pwm_add_table()).
  801. *
  802. * Once a PWM chip has been found the specified PWM device will be requested
  803. * and is ready to be used.
  804. *
  805. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  806. * error code on failure.
  807. */
  808. struct pwm_device *pwm_get(struct device *dev, const char *con_id)
  809. {
  810. const char *dev_id = dev ? dev_name(dev) : NULL;
  811. struct pwm_device *pwm;
  812. struct pwm_chip *chip;
  813. struct device_link *dl;
  814. unsigned int best = 0;
  815. struct pwm_lookup *p, *chosen = NULL;
  816. unsigned int match;
  817. int err;
  818. /* look up via DT first */
  819. if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
  820. return of_pwm_get(dev, dev->of_node, con_id);
  821. /* then lookup via ACPI */
  822. if (dev && is_acpi_node(dev->fwnode)) {
  823. pwm = acpi_pwm_get(dev->fwnode);
  824. if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT)
  825. return pwm;
  826. }
  827. /*
  828. * We look up the provider in the static table typically provided by
  829. * board setup code. We first try to lookup the consumer device by
  830. * name. If the consumer device was passed in as NULL or if no match
  831. * was found, we try to find the consumer by directly looking it up
  832. * by name.
  833. *
  834. * If a match is found, the provider PWM chip is looked up by name
  835. * and a PWM device is requested using the PWM device per-chip index.
  836. *
  837. * The lookup algorithm was shamelessly taken from the clock
  838. * framework:
  839. *
  840. * We do slightly fuzzy matching here:
  841. * An entry with a NULL ID is assumed to be a wildcard.
  842. * If an entry has a device ID, it must match
  843. * If an entry has a connection ID, it must match
  844. * Then we take the most specific entry - with the following order
  845. * of precedence: dev+con > dev only > con only.
  846. */
  847. mutex_lock(&pwm_lookup_lock);
  848. list_for_each_entry(p, &pwm_lookup_list, list) {
  849. match = 0;
  850. if (p->dev_id) {
  851. if (!dev_id || strcmp(p->dev_id, dev_id))
  852. continue;
  853. match += 2;
  854. }
  855. if (p->con_id) {
  856. if (!con_id || strcmp(p->con_id, con_id))
  857. continue;
  858. match += 1;
  859. }
  860. if (match > best) {
  861. chosen = p;
  862. if (match != 3)
  863. best = match;
  864. else
  865. break;
  866. }
  867. }
  868. mutex_unlock(&pwm_lookup_lock);
  869. if (!chosen)
  870. return ERR_PTR(-ENODEV);
  871. chip = pwmchip_find_by_name(chosen->provider);
  872. /*
  873. * If the lookup entry specifies a module, load the module and retry
  874. * the PWM chip lookup. This can be used to work around driver load
  875. * ordering issues if driver's can't be made to properly support the
  876. * deferred probe mechanism.
  877. */
  878. if (!chip && chosen->module) {
  879. err = request_module(chosen->module);
  880. if (err == 0)
  881. chip = pwmchip_find_by_name(chosen->provider);
  882. }
  883. if (!chip)
  884. return ERR_PTR(-EPROBE_DEFER);
  885. pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
  886. if (IS_ERR(pwm))
  887. return pwm;
  888. dl = pwm_device_link_add(dev, pwm);
  889. if (IS_ERR(dl)) {
  890. pwm_free(pwm);
  891. return ERR_CAST(dl);
  892. }
  893. pwm->args.period = chosen->period;
  894. pwm->args.polarity = chosen->polarity;
  895. return pwm;
  896. }
  897. EXPORT_SYMBOL_GPL(pwm_get);
  898. /**
  899. * pwm_put() - release a PWM device
  900. * @pwm: PWM device
  901. */
  902. void pwm_put(struct pwm_device *pwm)
  903. {
  904. if (!pwm)
  905. return;
  906. mutex_lock(&pwm_lock);
  907. if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
  908. pr_warn("PWM device already freed\n");
  909. goto out;
  910. }
  911. if (pwm->chip->ops->free)
  912. pwm->chip->ops->free(pwm->chip, pwm);
  913. pwm_set_chip_data(pwm, NULL);
  914. pwm->label = NULL;
  915. module_put(pwm->chip->ops->owner);
  916. out:
  917. mutex_unlock(&pwm_lock);
  918. }
  919. EXPORT_SYMBOL_GPL(pwm_put);
  920. static void devm_pwm_release(struct device *dev, void *res)
  921. {
  922. pwm_put(*(struct pwm_device **)res);
  923. }
  924. /**
  925. * devm_pwm_get() - resource managed pwm_get()
  926. * @dev: device for PWM consumer
  927. * @con_id: consumer name
  928. *
  929. * This function performs like pwm_get() but the acquired PWM device will
  930. * automatically be released on driver detach.
  931. *
  932. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  933. * error code on failure.
  934. */
  935. struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
  936. {
  937. struct pwm_device **ptr, *pwm;
  938. ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
  939. if (!ptr)
  940. return ERR_PTR(-ENOMEM);
  941. pwm = pwm_get(dev, con_id);
  942. if (!IS_ERR(pwm)) {
  943. *ptr = pwm;
  944. devres_add(dev, ptr);
  945. } else {
  946. devres_free(ptr);
  947. }
  948. return pwm;
  949. }
  950. EXPORT_SYMBOL_GPL(devm_pwm_get);
  951. /**
  952. * devm_of_pwm_get() - resource managed of_pwm_get()
  953. * @dev: device for PWM consumer
  954. * @np: device node to get the PWM from
  955. * @con_id: consumer name
  956. *
  957. * This function performs like of_pwm_get() but the acquired PWM device will
  958. * automatically be released on driver detach.
  959. *
  960. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  961. * error code on failure.
  962. */
  963. struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
  964. const char *con_id)
  965. {
  966. struct pwm_device **ptr, *pwm;
  967. ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
  968. if (!ptr)
  969. return ERR_PTR(-ENOMEM);
  970. pwm = of_pwm_get(dev, np, con_id);
  971. if (!IS_ERR(pwm)) {
  972. *ptr = pwm;
  973. devres_add(dev, ptr);
  974. } else {
  975. devres_free(ptr);
  976. }
  977. return pwm;
  978. }
  979. EXPORT_SYMBOL_GPL(devm_of_pwm_get);
  980. /**
  981. * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
  982. * @dev: device for PWM consumer
  983. * @fwnode: firmware node to get the PWM from
  984. * @con_id: consumer name
  985. *
  986. * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
  987. * acpi_pwm_get() for a detailed description.
  988. *
  989. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  990. * error code on failure.
  991. */
  992. struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
  993. struct fwnode_handle *fwnode,
  994. const char *con_id)
  995. {
  996. struct pwm_device **ptr, *pwm = ERR_PTR(-ENODEV);
  997. ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
  998. if (!ptr)
  999. return ERR_PTR(-ENOMEM);
  1000. if (is_of_node(fwnode))
  1001. pwm = of_pwm_get(dev, to_of_node(fwnode), con_id);
  1002. else if (is_acpi_node(fwnode))
  1003. pwm = acpi_pwm_get(fwnode);
  1004. if (!IS_ERR(pwm)) {
  1005. *ptr = pwm;
  1006. devres_add(dev, ptr);
  1007. } else {
  1008. devres_free(ptr);
  1009. }
  1010. return pwm;
  1011. }
  1012. EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get);
  1013. static int devm_pwm_match(struct device *dev, void *res, void *data)
  1014. {
  1015. struct pwm_device **p = res;
  1016. if (WARN_ON(!p || !*p))
  1017. return 0;
  1018. return *p == data;
  1019. }
  1020. /**
  1021. * devm_pwm_put() - resource managed pwm_put()
  1022. * @dev: device for PWM consumer
  1023. * @pwm: PWM device
  1024. *
  1025. * Release a PWM previously allocated using devm_pwm_get(). Calling this
  1026. * function is usually not needed because devm-allocated resources are
  1027. * automatically released on driver detach.
  1028. */
  1029. void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
  1030. {
  1031. WARN_ON(devres_release(dev, devm_pwm_release, devm_pwm_match, pwm));
  1032. }
  1033. EXPORT_SYMBOL_GPL(devm_pwm_put);
  1034. #ifdef CONFIG_DEBUG_FS
  1035. static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
  1036. {
  1037. unsigned int i;
  1038. for (i = 0; i < chip->npwm; i++) {
  1039. struct pwm_device *pwm = &chip->pwms[i];
  1040. struct pwm_state state;
  1041. pwm_get_state(pwm, &state);
  1042. seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
  1043. if (test_bit(PWMF_REQUESTED, &pwm->flags))
  1044. seq_puts(s, " requested");
  1045. if (state.enabled)
  1046. seq_puts(s, " enabled");
  1047. seq_printf(s, " period: %llu ns", state.period);
  1048. seq_printf(s, " duty: %llu ns", state.duty_cycle);
  1049. seq_printf(s, " polarity: %s",
  1050. state.polarity ? "inverse" : "normal");
  1051. seq_puts(s, "\n");
  1052. }
  1053. }
  1054. static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
  1055. {
  1056. mutex_lock(&pwm_lock);
  1057. s->private = "";
  1058. return seq_list_start(&pwm_chips, *pos);
  1059. }
  1060. static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1061. {
  1062. s->private = "\n";
  1063. return seq_list_next(v, &pwm_chips, pos);
  1064. }
  1065. static void pwm_seq_stop(struct seq_file *s, void *v)
  1066. {
  1067. mutex_unlock(&pwm_lock);
  1068. }
  1069. static int pwm_seq_show(struct seq_file *s, void *v)
  1070. {
  1071. struct pwm_chip *chip = list_entry(v, struct pwm_chip, list);
  1072. seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private,
  1073. chip->dev->bus ? chip->dev->bus->name : "no-bus",
  1074. dev_name(chip->dev), chip->npwm,
  1075. (chip->npwm != 1) ? "s" : "");
  1076. pwm_dbg_show(chip, s);
  1077. return 0;
  1078. }
  1079. static const struct seq_operations pwm_debugfs_sops = {
  1080. .start = pwm_seq_start,
  1081. .next = pwm_seq_next,
  1082. .stop = pwm_seq_stop,
  1083. .show = pwm_seq_show,
  1084. };
  1085. DEFINE_SEQ_ATTRIBUTE(pwm_debugfs);
  1086. static int __init pwm_debugfs_init(void)
  1087. {
  1088. debugfs_create_file("pwm", S_IFREG | S_IRUGO, NULL, NULL,
  1089. &pwm_debugfs_fops);
  1090. return 0;
  1091. }
  1092. subsys_initcall(pwm_debugfs_init);
  1093. #endif /* CONFIG_DEBUG_FS */