core.c 24 KB

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