core.c 27 KB

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