core.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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. pwmchip_sysfs_export(chip);
  243. out:
  244. mutex_unlock(&pwm_lock);
  245. return ret;
  246. }
  247. EXPORT_SYMBOL_GPL(pwmchip_add_with_polarity);
  248. /**
  249. * pwmchip_add() - register a new PWM chip
  250. * @chip: the PWM chip to add
  251. *
  252. * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
  253. * will be used. The initial polarity for all channels is normal.
  254. *
  255. * Returns: 0 on success or a negative error code on failure.
  256. */
  257. int pwmchip_add(struct pwm_chip *chip)
  258. {
  259. return pwmchip_add_with_polarity(chip, PWM_POLARITY_NORMAL);
  260. }
  261. EXPORT_SYMBOL_GPL(pwmchip_add);
  262. /**
  263. * pwmchip_remove() - remove a PWM chip
  264. * @chip: the PWM chip to remove
  265. *
  266. * Removes a PWM chip. This function may return busy if the PWM chip provides
  267. * a PWM device that is still requested.
  268. *
  269. * Returns: 0 on success or a negative error code on failure.
  270. */
  271. int pwmchip_remove(struct pwm_chip *chip)
  272. {
  273. unsigned int i;
  274. int ret = 0;
  275. pwmchip_sysfs_unexport_children(chip);
  276. mutex_lock(&pwm_lock);
  277. for (i = 0; i < chip->npwm; i++) {
  278. struct pwm_device *pwm = &chip->pwms[i];
  279. if (test_bit(PWMF_REQUESTED, &pwm->flags)) {
  280. ret = -EBUSY;
  281. goto out;
  282. }
  283. }
  284. list_del_init(&chip->list);
  285. if (IS_ENABLED(CONFIG_OF))
  286. of_pwmchip_remove(chip);
  287. free_pwms(chip);
  288. pwmchip_sysfs_unexport(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. pr_err("%s(): PWM chip not found\n", __func__);
  550. pwm = ERR_CAST(pc);
  551. goto put;
  552. }
  553. pwm = pc->of_xlate(pc, &args);
  554. if (IS_ERR(pwm))
  555. goto put;
  556. /*
  557. * If a consumer name was not given, try to look it up from the
  558. * "pwm-names" property if it exists. Otherwise use the name of
  559. * the user device node.
  560. */
  561. if (!con_id) {
  562. err = of_property_read_string_index(np, "pwm-names", index,
  563. &con_id);
  564. if (err < 0)
  565. con_id = np->name;
  566. }
  567. pwm->label = con_id;
  568. put:
  569. of_node_put(args.np);
  570. return pwm;
  571. }
  572. EXPORT_SYMBOL_GPL(of_pwm_get);
  573. /**
  574. * pwm_add_table() - register PWM device consumers
  575. * @table: array of consumers to register
  576. * @num: number of consumers in table
  577. */
  578. void pwm_add_table(struct pwm_lookup *table, size_t num)
  579. {
  580. mutex_lock(&pwm_lookup_lock);
  581. while (num--) {
  582. list_add_tail(&table->list, &pwm_lookup_list);
  583. table++;
  584. }
  585. mutex_unlock(&pwm_lookup_lock);
  586. }
  587. /**
  588. * pwm_remove_table() - unregister PWM device consumers
  589. * @table: array of consumers to unregister
  590. * @num: number of consumers in table
  591. */
  592. void pwm_remove_table(struct pwm_lookup *table, size_t num)
  593. {
  594. mutex_lock(&pwm_lookup_lock);
  595. while (num--) {
  596. list_del(&table->list);
  597. table++;
  598. }
  599. mutex_unlock(&pwm_lookup_lock);
  600. }
  601. /**
  602. * pwm_get() - look up and request a PWM device
  603. * @dev: device for PWM consumer
  604. * @con_id: consumer name
  605. *
  606. * Lookup is first attempted using DT. If the device was not instantiated from
  607. * a device tree, a PWM chip and a relative index is looked up via a table
  608. * supplied by board setup code (see pwm_add_table()).
  609. *
  610. * Once a PWM chip has been found the specified PWM device will be requested
  611. * and is ready to be used.
  612. *
  613. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  614. * error code on failure.
  615. */
  616. struct pwm_device *pwm_get(struct device *dev, const char *con_id)
  617. {
  618. const char *dev_id = dev ? dev_name(dev) : NULL;
  619. struct pwm_device *pwm;
  620. struct pwm_chip *chip;
  621. unsigned int best = 0;
  622. struct pwm_lookup *p, *chosen = NULL;
  623. unsigned int match;
  624. int err;
  625. /* look up via DT first */
  626. if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
  627. return of_pwm_get(dev->of_node, con_id);
  628. /*
  629. * We look up the provider in the static table typically provided by
  630. * board setup code. We first try to lookup the consumer device by
  631. * name. If the consumer device was passed in as NULL or if no match
  632. * was found, we try to find the consumer by directly looking it up
  633. * by name.
  634. *
  635. * If a match is found, the provider PWM chip is looked up by name
  636. * and a PWM device is requested using the PWM device per-chip index.
  637. *
  638. * The lookup algorithm was shamelessly taken from the clock
  639. * framework:
  640. *
  641. * We do slightly fuzzy matching here:
  642. * An entry with a NULL ID is assumed to be a wildcard.
  643. * If an entry has a device ID, it must match
  644. * If an entry has a connection ID, it must match
  645. * Then we take the most specific entry - with the following order
  646. * of precedence: dev+con > dev only > con only.
  647. */
  648. mutex_lock(&pwm_lookup_lock);
  649. list_for_each_entry(p, &pwm_lookup_list, list) {
  650. match = 0;
  651. if (p->dev_id) {
  652. if (!dev_id || strcmp(p->dev_id, dev_id))
  653. continue;
  654. match += 2;
  655. }
  656. if (p->con_id) {
  657. if (!con_id || strcmp(p->con_id, con_id))
  658. continue;
  659. match += 1;
  660. }
  661. if (match > best) {
  662. chosen = p;
  663. if (match != 3)
  664. best = match;
  665. else
  666. break;
  667. }
  668. }
  669. mutex_unlock(&pwm_lookup_lock);
  670. if (!chosen)
  671. return ERR_PTR(-ENODEV);
  672. chip = pwmchip_find_by_name(chosen->provider);
  673. /*
  674. * If the lookup entry specifies a module, load the module and retry
  675. * the PWM chip lookup. This can be used to work around driver load
  676. * ordering issues if driver's can't be made to properly support the
  677. * deferred probe mechanism.
  678. */
  679. if (!chip && chosen->module) {
  680. err = request_module(chosen->module);
  681. if (err == 0)
  682. chip = pwmchip_find_by_name(chosen->provider);
  683. }
  684. if (!chip)
  685. return ERR_PTR(-EPROBE_DEFER);
  686. pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
  687. if (IS_ERR(pwm))
  688. return pwm;
  689. pwm->args.period = chosen->period;
  690. pwm->args.polarity = chosen->polarity;
  691. return pwm;
  692. }
  693. EXPORT_SYMBOL_GPL(pwm_get);
  694. /**
  695. * pwm_put() - release a PWM device
  696. * @pwm: PWM device
  697. */
  698. void pwm_put(struct pwm_device *pwm)
  699. {
  700. if (!pwm)
  701. return;
  702. mutex_lock(&pwm_lock);
  703. if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
  704. pr_warn("PWM device already freed\n");
  705. goto out;
  706. }
  707. if (pwm->chip->ops->free)
  708. pwm->chip->ops->free(pwm->chip, pwm);
  709. pwm->label = NULL;
  710. module_put(pwm->chip->ops->owner);
  711. out:
  712. mutex_unlock(&pwm_lock);
  713. }
  714. EXPORT_SYMBOL_GPL(pwm_put);
  715. static void devm_pwm_release(struct device *dev, void *res)
  716. {
  717. pwm_put(*(struct pwm_device **)res);
  718. }
  719. /**
  720. * devm_pwm_get() - resource managed pwm_get()
  721. * @dev: device for PWM consumer
  722. * @con_id: consumer name
  723. *
  724. * This function performs like pwm_get() but the acquired PWM device will
  725. * automatically be released on driver detach.
  726. *
  727. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  728. * error code on failure.
  729. */
  730. struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
  731. {
  732. struct pwm_device **ptr, *pwm;
  733. ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
  734. if (!ptr)
  735. return ERR_PTR(-ENOMEM);
  736. pwm = pwm_get(dev, con_id);
  737. if (!IS_ERR(pwm)) {
  738. *ptr = pwm;
  739. devres_add(dev, ptr);
  740. } else {
  741. devres_free(ptr);
  742. }
  743. return pwm;
  744. }
  745. EXPORT_SYMBOL_GPL(devm_pwm_get);
  746. /**
  747. * devm_of_pwm_get() - resource managed of_pwm_get()
  748. * @dev: device for PWM consumer
  749. * @np: device node to get the PWM from
  750. * @con_id: consumer name
  751. *
  752. * This function performs like of_pwm_get() but the acquired PWM device will
  753. * automatically be released on driver detach.
  754. *
  755. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  756. * error code on failure.
  757. */
  758. struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
  759. const char *con_id)
  760. {
  761. struct pwm_device **ptr, *pwm;
  762. ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
  763. if (!ptr)
  764. return ERR_PTR(-ENOMEM);
  765. pwm = of_pwm_get(np, con_id);
  766. if (!IS_ERR(pwm)) {
  767. *ptr = pwm;
  768. devres_add(dev, ptr);
  769. } else {
  770. devres_free(ptr);
  771. }
  772. return pwm;
  773. }
  774. EXPORT_SYMBOL_GPL(devm_of_pwm_get);
  775. static int devm_pwm_match(struct device *dev, void *res, void *data)
  776. {
  777. struct pwm_device **p = res;
  778. if (WARN_ON(!p || !*p))
  779. return 0;
  780. return *p == data;
  781. }
  782. /**
  783. * devm_pwm_put() - resource managed pwm_put()
  784. * @dev: device for PWM consumer
  785. * @pwm: PWM device
  786. *
  787. * Release a PWM previously allocated using devm_pwm_get(). Calling this
  788. * function is usually not needed because devm-allocated resources are
  789. * automatically released on driver detach.
  790. */
  791. void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
  792. {
  793. WARN_ON(devres_release(dev, devm_pwm_release, devm_pwm_match, pwm));
  794. }
  795. EXPORT_SYMBOL_GPL(devm_pwm_put);
  796. #ifdef CONFIG_DEBUG_FS
  797. static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
  798. {
  799. unsigned int i;
  800. for (i = 0; i < chip->npwm; i++) {
  801. struct pwm_device *pwm = &chip->pwms[i];
  802. struct pwm_state state;
  803. pwm_get_state(pwm, &state);
  804. seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
  805. if (test_bit(PWMF_REQUESTED, &pwm->flags))
  806. seq_puts(s, " requested");
  807. if (state.enabled)
  808. seq_puts(s, " enabled");
  809. seq_printf(s, " period: %u ns", state.period);
  810. seq_printf(s, " duty: %u ns", state.duty_cycle);
  811. seq_printf(s, " polarity: %s",
  812. state.polarity ? "inverse" : "normal");
  813. seq_puts(s, "\n");
  814. }
  815. }
  816. static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
  817. {
  818. mutex_lock(&pwm_lock);
  819. s->private = "";
  820. return seq_list_start(&pwm_chips, *pos);
  821. }
  822. static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
  823. {
  824. s->private = "\n";
  825. return seq_list_next(v, &pwm_chips, pos);
  826. }
  827. static void pwm_seq_stop(struct seq_file *s, void *v)
  828. {
  829. mutex_unlock(&pwm_lock);
  830. }
  831. static int pwm_seq_show(struct seq_file *s, void *v)
  832. {
  833. struct pwm_chip *chip = list_entry(v, struct pwm_chip, list);
  834. seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private,
  835. chip->dev->bus ? chip->dev->bus->name : "no-bus",
  836. dev_name(chip->dev), chip->npwm,
  837. (chip->npwm != 1) ? "s" : "");
  838. if (chip->ops->dbg_show)
  839. chip->ops->dbg_show(chip, s);
  840. else
  841. pwm_dbg_show(chip, s);
  842. return 0;
  843. }
  844. static const struct seq_operations pwm_seq_ops = {
  845. .start = pwm_seq_start,
  846. .next = pwm_seq_next,
  847. .stop = pwm_seq_stop,
  848. .show = pwm_seq_show,
  849. };
  850. static int pwm_seq_open(struct inode *inode, struct file *file)
  851. {
  852. return seq_open(file, &pwm_seq_ops);
  853. }
  854. static const struct file_operations pwm_debugfs_ops = {
  855. .owner = THIS_MODULE,
  856. .open = pwm_seq_open,
  857. .read = seq_read,
  858. .llseek = seq_lseek,
  859. .release = seq_release,
  860. };
  861. static int __init pwm_debugfs_init(void)
  862. {
  863. debugfs_create_file("pwm", S_IFREG | S_IRUGO, NULL, NULL,
  864. &pwm_debugfs_ops);
  865. return 0;
  866. }
  867. subsys_initcall(pwm_debugfs_init);
  868. #endif /* CONFIG_DEBUG_FS */