core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. #define MAX_PWMS 1024
  32. static DEFINE_MUTEX(pwm_lookup_lock);
  33. static LIST_HEAD(pwm_lookup_list);
  34. static DEFINE_MUTEX(pwm_lock);
  35. static LIST_HEAD(pwm_chips);
  36. static DECLARE_BITMAP(allocated_pwms, MAX_PWMS);
  37. static RADIX_TREE(pwm_tree, GFP_KERNEL);
  38. static struct pwm_device *pwm_to_device(unsigned int pwm)
  39. {
  40. return radix_tree_lookup(&pwm_tree, pwm);
  41. }
  42. static int alloc_pwms(int pwm, unsigned int count)
  43. {
  44. unsigned int from = 0;
  45. unsigned int start;
  46. if (pwm >= MAX_PWMS)
  47. return -EINVAL;
  48. if (pwm >= 0)
  49. from = pwm;
  50. start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, from,
  51. count, 0);
  52. if (pwm >= 0 && start != pwm)
  53. return -EEXIST;
  54. if (start + count > MAX_PWMS)
  55. return -ENOSPC;
  56. return start;
  57. }
  58. static void free_pwms(struct pwm_chip *chip)
  59. {
  60. unsigned int i;
  61. for (i = 0; i < chip->npwm; i++) {
  62. struct pwm_device *pwm = &chip->pwms[i];
  63. radix_tree_delete(&pwm_tree, pwm->pwm);
  64. }
  65. bitmap_clear(allocated_pwms, chip->base, chip->npwm);
  66. kfree(chip->pwms);
  67. chip->pwms = NULL;
  68. }
  69. static struct pwm_chip *pwmchip_find_by_name(const char *name)
  70. {
  71. struct pwm_chip *chip;
  72. if (!name)
  73. return NULL;
  74. mutex_lock(&pwm_lock);
  75. list_for_each_entry(chip, &pwm_chips, list) {
  76. const char *chip_name = dev_name(chip->dev);
  77. if (chip_name && strcmp(chip_name, name) == 0) {
  78. mutex_unlock(&pwm_lock);
  79. return chip;
  80. }
  81. }
  82. mutex_unlock(&pwm_lock);
  83. return NULL;
  84. }
  85. static int pwm_device_request(struct pwm_device *pwm, const char *label)
  86. {
  87. int err;
  88. if (test_bit(PWMF_REQUESTED, &pwm->flags))
  89. return -EBUSY;
  90. if (!try_module_get(pwm->chip->ops->owner))
  91. return -ENODEV;
  92. if (pwm->chip->ops->request) {
  93. err = pwm->chip->ops->request(pwm->chip, pwm);
  94. if (err) {
  95. module_put(pwm->chip->ops->owner);
  96. return err;
  97. }
  98. }
  99. set_bit(PWMF_REQUESTED, &pwm->flags);
  100. pwm->label = label;
  101. return 0;
  102. }
  103. static struct pwm_device *of_pwm_simple_xlate(struct pwm_chip *pc,
  104. const struct of_phandle_args *args)
  105. {
  106. struct pwm_device *pwm;
  107. if (pc->of_pwm_n_cells < 2)
  108. return ERR_PTR(-EINVAL);
  109. if (args->args[0] >= pc->npwm)
  110. return ERR_PTR(-EINVAL);
  111. pwm = pwm_request_from_chip(pc, args->args[0], NULL);
  112. if (IS_ERR(pwm))
  113. return pwm;
  114. pwm_set_period(pwm, args->args[1]);
  115. return pwm;
  116. }
  117. void of_pwmchip_add(struct pwm_chip *chip)
  118. {
  119. if (!chip->dev || !chip->dev->of_node)
  120. return;
  121. if (!chip->of_xlate) {
  122. chip->of_xlate = of_pwm_simple_xlate;
  123. chip->of_pwm_n_cells = 2;
  124. }
  125. of_node_get(chip->dev->of_node);
  126. }
  127. void of_pwmchip_remove(struct pwm_chip *chip)
  128. {
  129. if (chip->dev && chip->dev->of_node)
  130. of_node_put(chip->dev->of_node);
  131. }
  132. /**
  133. * pwm_set_chip_data() - set private chip data for a PWM
  134. * @pwm: PWM device
  135. * @data: pointer to chip-specific data
  136. */
  137. int pwm_set_chip_data(struct pwm_device *pwm, void *data)
  138. {
  139. if (!pwm)
  140. return -EINVAL;
  141. pwm->chip_data = data;
  142. return 0;
  143. }
  144. /**
  145. * pwm_get_chip_data() - get private chip data for a PWM
  146. * @pwm: PWM device
  147. */
  148. void *pwm_get_chip_data(struct pwm_device *pwm)
  149. {
  150. return pwm ? pwm->chip_data : NULL;
  151. }
  152. /**
  153. * pwmchip_add() - register a new PWM chip
  154. * @chip: the PWM chip to add
  155. *
  156. * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
  157. * will be used.
  158. */
  159. int pwmchip_add(struct pwm_chip *chip)
  160. {
  161. struct pwm_device *pwm;
  162. unsigned int i;
  163. int ret;
  164. if (!chip || !chip->dev || !chip->ops || !chip->ops->config ||
  165. !chip->ops->enable || !chip->ops->disable)
  166. return -EINVAL;
  167. mutex_lock(&pwm_lock);
  168. ret = alloc_pwms(chip->base, chip->npwm);
  169. if (ret < 0)
  170. goto out;
  171. chip->pwms = kzalloc(chip->npwm * sizeof(*pwm), GFP_KERNEL);
  172. if (!chip->pwms) {
  173. ret = -ENOMEM;
  174. goto out;
  175. }
  176. chip->base = ret;
  177. for (i = 0; i < chip->npwm; i++) {
  178. pwm = &chip->pwms[i];
  179. pwm->chip = chip;
  180. pwm->pwm = chip->base + i;
  181. pwm->hwpwm = i;
  182. radix_tree_insert(&pwm_tree, pwm->pwm, pwm);
  183. }
  184. bitmap_set(allocated_pwms, chip->base, chip->npwm);
  185. INIT_LIST_HEAD(&chip->list);
  186. list_add(&chip->list, &pwm_chips);
  187. ret = 0;
  188. if (IS_ENABLED(CONFIG_OF))
  189. of_pwmchip_add(chip);
  190. out:
  191. mutex_unlock(&pwm_lock);
  192. return ret;
  193. }
  194. EXPORT_SYMBOL_GPL(pwmchip_add);
  195. /**
  196. * pwmchip_remove() - remove a PWM chip
  197. * @chip: the PWM chip to remove
  198. *
  199. * Removes a PWM chip. This function may return busy if the PWM chip provides
  200. * a PWM device that is still requested.
  201. */
  202. int pwmchip_remove(struct pwm_chip *chip)
  203. {
  204. unsigned int i;
  205. int ret = 0;
  206. mutex_lock(&pwm_lock);
  207. for (i = 0; i < chip->npwm; i++) {
  208. struct pwm_device *pwm = &chip->pwms[i];
  209. if (test_bit(PWMF_REQUESTED, &pwm->flags)) {
  210. ret = -EBUSY;
  211. goto out;
  212. }
  213. }
  214. list_del_init(&chip->list);
  215. if (IS_ENABLED(CONFIG_OF))
  216. of_pwmchip_remove(chip);
  217. free_pwms(chip);
  218. out:
  219. mutex_unlock(&pwm_lock);
  220. return ret;
  221. }
  222. EXPORT_SYMBOL_GPL(pwmchip_remove);
  223. /**
  224. * pwm_request() - request a PWM device
  225. * @pwm_id: global PWM device index
  226. * @label: PWM device label
  227. *
  228. * This function is deprecated, use pwm_get() instead.
  229. */
  230. struct pwm_device *pwm_request(int pwm, const char *label)
  231. {
  232. struct pwm_device *dev;
  233. int err;
  234. if (pwm < 0 || pwm >= MAX_PWMS)
  235. return ERR_PTR(-EINVAL);
  236. mutex_lock(&pwm_lock);
  237. dev = pwm_to_device(pwm);
  238. if (!dev) {
  239. dev = ERR_PTR(-EPROBE_DEFER);
  240. goto out;
  241. }
  242. err = pwm_device_request(dev, label);
  243. if (err < 0)
  244. dev = ERR_PTR(err);
  245. out:
  246. mutex_unlock(&pwm_lock);
  247. return dev;
  248. }
  249. EXPORT_SYMBOL_GPL(pwm_request);
  250. /**
  251. * pwm_request_from_chip() - request a PWM device relative to a PWM chip
  252. * @chip: PWM chip
  253. * @index: per-chip index of the PWM to request
  254. * @label: a literal description string of this PWM
  255. *
  256. * Returns the PWM at the given index of the given PWM chip. A negative error
  257. * code is returned if the index is not valid for the specified PWM chip or
  258. * if the PWM device cannot be requested.
  259. */
  260. struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
  261. unsigned int index,
  262. const char *label)
  263. {
  264. struct pwm_device *pwm;
  265. int err;
  266. if (!chip || index >= chip->npwm)
  267. return ERR_PTR(-EINVAL);
  268. mutex_lock(&pwm_lock);
  269. pwm = &chip->pwms[index];
  270. err = pwm_device_request(pwm, label);
  271. if (err < 0)
  272. pwm = ERR_PTR(err);
  273. mutex_unlock(&pwm_lock);
  274. return pwm;
  275. }
  276. EXPORT_SYMBOL_GPL(pwm_request_from_chip);
  277. /**
  278. * pwm_free() - free a PWM device
  279. * @pwm: PWM device
  280. *
  281. * This function is deprecated, use pwm_put() instead.
  282. */
  283. void pwm_free(struct pwm_device *pwm)
  284. {
  285. pwm_put(pwm);
  286. }
  287. EXPORT_SYMBOL_GPL(pwm_free);
  288. /**
  289. * pwm_config() - change a PWM device configuration
  290. * @pwm: PWM device
  291. * @duty_ns: "on" time (in nanoseconds)
  292. * @period_ns: duration (in nanoseconds) of one cycle
  293. */
  294. int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
  295. {
  296. if (!pwm || period_ns == 0 || duty_ns > period_ns)
  297. return -EINVAL;
  298. return pwm->chip->ops->config(pwm->chip, pwm, duty_ns, period_ns);
  299. }
  300. EXPORT_SYMBOL_GPL(pwm_config);
  301. /**
  302. * pwm_enable() - start a PWM output toggling
  303. * @pwm: PWM device
  304. */
  305. int pwm_enable(struct pwm_device *pwm)
  306. {
  307. if (pwm && !test_and_set_bit(PWMF_ENABLED, &pwm->flags))
  308. return pwm->chip->ops->enable(pwm->chip, pwm);
  309. return pwm ? 0 : -EINVAL;
  310. }
  311. EXPORT_SYMBOL_GPL(pwm_enable);
  312. /**
  313. * pwm_disable() - stop a PWM output toggling
  314. * @pwm: PWM device
  315. */
  316. void pwm_disable(struct pwm_device *pwm)
  317. {
  318. if (pwm && test_and_clear_bit(PWMF_ENABLED, &pwm->flags))
  319. pwm->chip->ops->disable(pwm->chip, pwm);
  320. }
  321. EXPORT_SYMBOL_GPL(pwm_disable);
  322. static struct pwm_chip *of_node_to_pwmchip(struct device_node *np)
  323. {
  324. struct pwm_chip *chip;
  325. mutex_lock(&pwm_lock);
  326. list_for_each_entry(chip, &pwm_chips, list)
  327. if (chip->dev && chip->dev->of_node == np) {
  328. mutex_unlock(&pwm_lock);
  329. return chip;
  330. }
  331. mutex_unlock(&pwm_lock);
  332. return ERR_PTR(-EPROBE_DEFER);
  333. }
  334. /**
  335. * of_pwm_request() - request a PWM via the PWM framework
  336. * @np: device node to get the PWM from
  337. * @con_id: consumer name
  338. *
  339. * Returns the PWM device parsed from the phandle and index specified in the
  340. * "pwms" property of a device tree node or a negative error-code on failure.
  341. * Values parsed from the device tree are stored in the returned PWM device
  342. * object.
  343. *
  344. * If con_id is NULL, the first PWM device listed in the "pwms" property will
  345. * be requested. Otherwise the "pwm-names" property is used to do a reverse
  346. * lookup of the PWM index. This also means that the "pwm-names" property
  347. * becomes mandatory for devices that look up the PWM device via the con_id
  348. * parameter.
  349. */
  350. static struct pwm_device *of_pwm_request(struct device_node *np,
  351. const char *con_id)
  352. {
  353. struct pwm_device *pwm = NULL;
  354. struct of_phandle_args args;
  355. struct pwm_chip *pc;
  356. int index = 0;
  357. int err;
  358. if (con_id) {
  359. index = of_property_match_string(np, "pwm-names", con_id);
  360. if (index < 0)
  361. return ERR_PTR(index);
  362. }
  363. err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
  364. &args);
  365. if (err) {
  366. pr_debug("%s(): can't parse \"pwms\" property\n", __func__);
  367. return ERR_PTR(err);
  368. }
  369. pc = of_node_to_pwmchip(args.np);
  370. if (IS_ERR(pc)) {
  371. pr_debug("%s(): PWM chip not found\n", __func__);
  372. pwm = ERR_CAST(pc);
  373. goto put;
  374. }
  375. if (args.args_count != pc->of_pwm_n_cells) {
  376. pr_debug("%s: wrong #pwm-cells for %s\n", np->full_name,
  377. args.np->full_name);
  378. pwm = ERR_PTR(-EINVAL);
  379. goto put;
  380. }
  381. pwm = pc->of_xlate(pc, &args);
  382. if (IS_ERR(pwm))
  383. goto put;
  384. /*
  385. * If a consumer name was not given, try to look it up from the
  386. * "pwm-names" property if it exists. Otherwise use the name of
  387. * the user device node.
  388. */
  389. if (!con_id) {
  390. err = of_property_read_string_index(np, "pwm-names", index,
  391. &con_id);
  392. if (err < 0)
  393. con_id = np->name;
  394. }
  395. pwm->label = con_id;
  396. put:
  397. of_node_put(args.np);
  398. return pwm;
  399. }
  400. /**
  401. * pwm_add_table() - register PWM device consumers
  402. * @table: array of consumers to register
  403. * @num: number of consumers in table
  404. */
  405. void __init pwm_add_table(struct pwm_lookup *table, size_t num)
  406. {
  407. mutex_lock(&pwm_lookup_lock);
  408. while (num--) {
  409. list_add_tail(&table->list, &pwm_lookup_list);
  410. table++;
  411. }
  412. mutex_unlock(&pwm_lookup_lock);
  413. }
  414. /**
  415. * pwm_get() - look up and request a PWM device
  416. * @dev: device for PWM consumer
  417. * @con_id: consumer name
  418. *
  419. * Lookup is first attempted using DT. If the device was not instantiated from
  420. * a device tree, a PWM chip and a relative index is looked up via a table
  421. * supplied by board setup code (see pwm_add_table()).
  422. *
  423. * Once a PWM chip has been found the specified PWM device will be requested
  424. * and is ready to be used.
  425. */
  426. struct pwm_device *pwm_get(struct device *dev, const char *con_id)
  427. {
  428. struct pwm_device *pwm = ERR_PTR(-EPROBE_DEFER);
  429. const char *dev_id = dev ? dev_name(dev): NULL;
  430. struct pwm_chip *chip = NULL;
  431. unsigned int index = 0;
  432. unsigned int best = 0;
  433. struct pwm_lookup *p;
  434. unsigned int match;
  435. /* look up via DT first */
  436. if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
  437. return of_pwm_request(dev->of_node, con_id);
  438. /*
  439. * We look up the provider in the static table typically provided by
  440. * board setup code. We first try to lookup the consumer device by
  441. * name. If the consumer device was passed in as NULL or if no match
  442. * was found, we try to find the consumer by directly looking it up
  443. * by name.
  444. *
  445. * If a match is found, the provider PWM chip is looked up by name
  446. * and a PWM device is requested using the PWM device per-chip index.
  447. *
  448. * The lookup algorithm was shamelessly taken from the clock
  449. * framework:
  450. *
  451. * We do slightly fuzzy matching here:
  452. * An entry with a NULL ID is assumed to be a wildcard.
  453. * If an entry has a device ID, it must match
  454. * If an entry has a connection ID, it must match
  455. * Then we take the most specific entry - with the following order
  456. * of precedence: dev+con > dev only > con only.
  457. */
  458. mutex_lock(&pwm_lookup_lock);
  459. list_for_each_entry(p, &pwm_lookup_list, list) {
  460. match = 0;
  461. if (p->dev_id) {
  462. if (!dev_id || strcmp(p->dev_id, dev_id))
  463. continue;
  464. match += 2;
  465. }
  466. if (p->con_id) {
  467. if (!con_id || strcmp(p->con_id, con_id))
  468. continue;
  469. match += 1;
  470. }
  471. if (match > best) {
  472. chip = pwmchip_find_by_name(p->provider);
  473. index = p->index;
  474. if (match != 3)
  475. best = match;
  476. else
  477. break;
  478. }
  479. }
  480. if (chip)
  481. pwm = pwm_request_from_chip(chip, index, con_id ?: dev_id);
  482. mutex_unlock(&pwm_lookup_lock);
  483. return pwm;
  484. }
  485. EXPORT_SYMBOL_GPL(pwm_get);
  486. /**
  487. * pwm_put() - release a PWM device
  488. * @pwm: PWM device
  489. */
  490. void pwm_put(struct pwm_device *pwm)
  491. {
  492. if (!pwm)
  493. return;
  494. mutex_lock(&pwm_lock);
  495. if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
  496. pr_warning("PWM device already freed\n");
  497. goto out;
  498. }
  499. if (pwm->chip->ops->free)
  500. pwm->chip->ops->free(pwm->chip, pwm);
  501. pwm->label = NULL;
  502. module_put(pwm->chip->ops->owner);
  503. out:
  504. mutex_unlock(&pwm_lock);
  505. }
  506. EXPORT_SYMBOL_GPL(pwm_put);
  507. #ifdef CONFIG_DEBUG_FS
  508. static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
  509. {
  510. unsigned int i;
  511. for (i = 0; i < chip->npwm; i++) {
  512. struct pwm_device *pwm = &chip->pwms[i];
  513. seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
  514. if (test_bit(PWMF_REQUESTED, &pwm->flags))
  515. seq_printf(s, " requested");
  516. if (test_bit(PWMF_ENABLED, &pwm->flags))
  517. seq_printf(s, " enabled");
  518. seq_printf(s, "\n");
  519. }
  520. }
  521. static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
  522. {
  523. mutex_lock(&pwm_lock);
  524. s->private = "";
  525. return seq_list_start(&pwm_chips, *pos);
  526. }
  527. static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
  528. {
  529. s->private = "\n";
  530. return seq_list_next(v, &pwm_chips, pos);
  531. }
  532. static void pwm_seq_stop(struct seq_file *s, void *v)
  533. {
  534. mutex_unlock(&pwm_lock);
  535. }
  536. static int pwm_seq_show(struct seq_file *s, void *v)
  537. {
  538. struct pwm_chip *chip = list_entry(v, struct pwm_chip, list);
  539. seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private,
  540. chip->dev->bus ? chip->dev->bus->name : "no-bus",
  541. dev_name(chip->dev), chip->npwm,
  542. (chip->npwm != 1) ? "s" : "");
  543. if (chip->ops->dbg_show)
  544. chip->ops->dbg_show(chip, s);
  545. else
  546. pwm_dbg_show(chip, s);
  547. return 0;
  548. }
  549. static const struct seq_operations pwm_seq_ops = {
  550. .start = pwm_seq_start,
  551. .next = pwm_seq_next,
  552. .stop = pwm_seq_stop,
  553. .show = pwm_seq_show,
  554. };
  555. static int pwm_seq_open(struct inode *inode, struct file *file)
  556. {
  557. return seq_open(file, &pwm_seq_ops);
  558. }
  559. static const struct file_operations pwm_debugfs_ops = {
  560. .owner = THIS_MODULE,
  561. .open = pwm_seq_open,
  562. .read = seq_read,
  563. .llseek = seq_lseek,
  564. .release = seq_release,
  565. };
  566. static int __init pwm_debugfs_init(void)
  567. {
  568. debugfs_create_file("pwm", S_IFREG | S_IRUGO, NULL, NULL,
  569. &pwm_debugfs_ops);
  570. return 0;
  571. }
  572. subsys_initcall(pwm_debugfs_init);
  573. #endif /* CONFIG_DEBUG_FS */