core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. /**
  104. * pwm_set_chip_data() - set private chip data for a PWM
  105. * @pwm: PWM device
  106. * @data: pointer to chip-specific data
  107. */
  108. int pwm_set_chip_data(struct pwm_device *pwm, void *data)
  109. {
  110. if (!pwm)
  111. return -EINVAL;
  112. pwm->chip_data = data;
  113. return 0;
  114. }
  115. /**
  116. * pwm_get_chip_data() - get private chip data for a PWM
  117. * @pwm: PWM device
  118. */
  119. void *pwm_get_chip_data(struct pwm_device *pwm)
  120. {
  121. return pwm ? pwm->chip_data : NULL;
  122. }
  123. /**
  124. * pwmchip_add() - register a new PWM chip
  125. * @chip: the PWM chip to add
  126. *
  127. * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
  128. * will be used.
  129. */
  130. int pwmchip_add(struct pwm_chip *chip)
  131. {
  132. struct pwm_device *pwm;
  133. unsigned int i;
  134. int ret;
  135. if (!chip || !chip->dev || !chip->ops || !chip->ops->config ||
  136. !chip->ops->enable || !chip->ops->disable)
  137. return -EINVAL;
  138. mutex_lock(&pwm_lock);
  139. ret = alloc_pwms(chip->base, chip->npwm);
  140. if (ret < 0)
  141. goto out;
  142. chip->pwms = kzalloc(chip->npwm * sizeof(*pwm), GFP_KERNEL);
  143. if (!chip->pwms) {
  144. ret = -ENOMEM;
  145. goto out;
  146. }
  147. chip->base = ret;
  148. for (i = 0; i < chip->npwm; i++) {
  149. pwm = &chip->pwms[i];
  150. pwm->chip = chip;
  151. pwm->pwm = chip->base + i;
  152. pwm->hwpwm = i;
  153. radix_tree_insert(&pwm_tree, pwm->pwm, pwm);
  154. }
  155. bitmap_set(allocated_pwms, chip->base, chip->npwm);
  156. INIT_LIST_HEAD(&chip->list);
  157. list_add(&chip->list, &pwm_chips);
  158. ret = 0;
  159. out:
  160. mutex_unlock(&pwm_lock);
  161. return ret;
  162. }
  163. EXPORT_SYMBOL_GPL(pwmchip_add);
  164. /**
  165. * pwmchip_remove() - remove a PWM chip
  166. * @chip: the PWM chip to remove
  167. *
  168. * Removes a PWM chip. This function may return busy if the PWM chip provides
  169. * a PWM device that is still requested.
  170. */
  171. int pwmchip_remove(struct pwm_chip *chip)
  172. {
  173. unsigned int i;
  174. int ret = 0;
  175. mutex_lock(&pwm_lock);
  176. for (i = 0; i < chip->npwm; i++) {
  177. struct pwm_device *pwm = &chip->pwms[i];
  178. if (test_bit(PWMF_REQUESTED, &pwm->flags)) {
  179. ret = -EBUSY;
  180. goto out;
  181. }
  182. }
  183. list_del_init(&chip->list);
  184. free_pwms(chip);
  185. out:
  186. mutex_unlock(&pwm_lock);
  187. return ret;
  188. }
  189. EXPORT_SYMBOL_GPL(pwmchip_remove);
  190. /**
  191. * pwm_request() - request a PWM device
  192. * @pwm_id: global PWM device index
  193. * @label: PWM device label
  194. *
  195. * This function is deprecated, use pwm_get() instead.
  196. */
  197. struct pwm_device *pwm_request(int pwm, const char *label)
  198. {
  199. struct pwm_device *dev;
  200. int err;
  201. if (pwm < 0 || pwm >= MAX_PWMS)
  202. return ERR_PTR(-EINVAL);
  203. mutex_lock(&pwm_lock);
  204. dev = pwm_to_device(pwm);
  205. if (!dev) {
  206. dev = ERR_PTR(-EPROBE_DEFER);
  207. goto out;
  208. }
  209. err = pwm_device_request(dev, label);
  210. if (err < 0)
  211. dev = ERR_PTR(err);
  212. out:
  213. mutex_unlock(&pwm_lock);
  214. return dev;
  215. }
  216. EXPORT_SYMBOL_GPL(pwm_request);
  217. /**
  218. * pwm_request_from_chip() - request a PWM device relative to a PWM chip
  219. * @chip: PWM chip
  220. * @index: per-chip index of the PWM to request
  221. * @label: a literal description string of this PWM
  222. *
  223. * Returns the PWM at the given index of the given PWM chip. A negative error
  224. * code is returned if the index is not valid for the specified PWM chip or
  225. * if the PWM device cannot be requested.
  226. */
  227. struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
  228. unsigned int index,
  229. const char *label)
  230. {
  231. struct pwm_device *pwm;
  232. int err;
  233. if (!chip || index >= chip->npwm)
  234. return ERR_PTR(-EINVAL);
  235. mutex_lock(&pwm_lock);
  236. pwm = &chip->pwms[index];
  237. err = pwm_device_request(pwm, label);
  238. if (err < 0)
  239. pwm = ERR_PTR(err);
  240. mutex_unlock(&pwm_lock);
  241. return pwm;
  242. }
  243. EXPORT_SYMBOL_GPL(pwm_request_from_chip);
  244. /**
  245. * pwm_free() - free a PWM device
  246. * @pwm: PWM device
  247. *
  248. * This function is deprecated, use pwm_put() instead.
  249. */
  250. void pwm_free(struct pwm_device *pwm)
  251. {
  252. pwm_put(pwm);
  253. }
  254. EXPORT_SYMBOL_GPL(pwm_free);
  255. /**
  256. * pwm_config() - change a PWM device configuration
  257. * @pwm: PWM device
  258. * @duty_ns: "on" time (in nanoseconds)
  259. * @period_ns: duration (in nanoseconds) of one cycle
  260. */
  261. int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
  262. {
  263. if (!pwm || period_ns == 0 || duty_ns > period_ns)
  264. return -EINVAL;
  265. return pwm->chip->ops->config(pwm->chip, pwm, duty_ns, period_ns);
  266. }
  267. EXPORT_SYMBOL_GPL(pwm_config);
  268. /**
  269. * pwm_enable() - start a PWM output toggling
  270. * @pwm: PWM device
  271. */
  272. int pwm_enable(struct pwm_device *pwm)
  273. {
  274. if (pwm && !test_and_set_bit(PWMF_ENABLED, &pwm->flags))
  275. return pwm->chip->ops->enable(pwm->chip, pwm);
  276. return pwm ? 0 : -EINVAL;
  277. }
  278. EXPORT_SYMBOL_GPL(pwm_enable);
  279. /**
  280. * pwm_disable() - stop a PWM output toggling
  281. * @pwm: PWM device
  282. */
  283. void pwm_disable(struct pwm_device *pwm)
  284. {
  285. if (pwm && test_and_clear_bit(PWMF_ENABLED, &pwm->flags))
  286. pwm->chip->ops->disable(pwm->chip, pwm);
  287. }
  288. EXPORT_SYMBOL_GPL(pwm_disable);
  289. /**
  290. * pwm_add_table() - register PWM device consumers
  291. * @table: array of consumers to register
  292. * @num: number of consumers in table
  293. */
  294. void __init pwm_add_table(struct pwm_lookup *table, size_t num)
  295. {
  296. mutex_lock(&pwm_lookup_lock);
  297. while (num--) {
  298. list_add_tail(&table->list, &pwm_lookup_list);
  299. table++;
  300. }
  301. mutex_unlock(&pwm_lookup_lock);
  302. }
  303. /**
  304. * pwm_get() - look up and request a PWM device
  305. * @dev: device for PWM consumer
  306. * @con_id: consumer name
  307. *
  308. * Look up a PWM chip and a relative index via a table supplied by board setup
  309. * code (see pwm_add_table()).
  310. *
  311. * Once a PWM chip has been found the specified PWM device will be requested
  312. * and is ready to be used.
  313. */
  314. struct pwm_device *pwm_get(struct device *dev, const char *con_id)
  315. {
  316. struct pwm_device *pwm = ERR_PTR(-EPROBE_DEFER);
  317. const char *dev_id = dev ? dev_name(dev): NULL;
  318. struct pwm_chip *chip = NULL;
  319. unsigned int best = 0;
  320. struct pwm_lookup *p;
  321. unsigned int index;
  322. unsigned int match;
  323. /*
  324. * We look up the provider in the static table typically provided by
  325. * board setup code. We first try to lookup the consumer device by
  326. * name. If the consumer device was passed in as NULL or if no match
  327. * was found, we try to find the consumer by directly looking it up
  328. * by name.
  329. *
  330. * If a match is found, the provider PWM chip is looked up by name
  331. * and a PWM device is requested using the PWM device per-chip index.
  332. *
  333. * The lookup algorithm was shamelessly taken from the clock
  334. * framework:
  335. *
  336. * We do slightly fuzzy matching here:
  337. * An entry with a NULL ID is assumed to be a wildcard.
  338. * If an entry has a device ID, it must match
  339. * If an entry has a connection ID, it must match
  340. * Then we take the most specific entry - with the following order
  341. * of precedence: dev+con > dev only > con only.
  342. */
  343. mutex_lock(&pwm_lookup_lock);
  344. list_for_each_entry(p, &pwm_lookup_list, list) {
  345. match = 0;
  346. if (p->dev_id) {
  347. if (!dev_id || strcmp(p->dev_id, dev_id))
  348. continue;
  349. match += 2;
  350. }
  351. if (p->con_id) {
  352. if (!con_id || strcmp(p->con_id, con_id))
  353. continue;
  354. match += 1;
  355. }
  356. if (match > best) {
  357. chip = pwmchip_find_by_name(p->provider);
  358. index = p->index;
  359. if (match != 3)
  360. best = match;
  361. else
  362. break;
  363. }
  364. }
  365. if (chip)
  366. pwm = pwm_request_from_chip(chip, index, con_id ?: dev_id);
  367. mutex_unlock(&pwm_lookup_lock);
  368. return pwm;
  369. }
  370. EXPORT_SYMBOL_GPL(pwm_get);
  371. /**
  372. * pwm_put() - release a PWM device
  373. * @pwm: PWM device
  374. */
  375. void pwm_put(struct pwm_device *pwm)
  376. {
  377. if (!pwm)
  378. return;
  379. mutex_lock(&pwm_lock);
  380. if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
  381. pr_warning("PWM device already freed\n");
  382. goto out;
  383. }
  384. if (pwm->chip->ops->free)
  385. pwm->chip->ops->free(pwm->chip, pwm);
  386. pwm->label = NULL;
  387. module_put(pwm->chip->ops->owner);
  388. out:
  389. mutex_unlock(&pwm_lock);
  390. }
  391. EXPORT_SYMBOL_GPL(pwm_put);
  392. #ifdef CONFIG_DEBUG_FS
  393. static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
  394. {
  395. unsigned int i;
  396. for (i = 0; i < chip->npwm; i++) {
  397. struct pwm_device *pwm = &chip->pwms[i];
  398. seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
  399. if (test_bit(PWMF_REQUESTED, &pwm->flags))
  400. seq_printf(s, " requested");
  401. if (test_bit(PWMF_ENABLED, &pwm->flags))
  402. seq_printf(s, " enabled");
  403. seq_printf(s, "\n");
  404. }
  405. }
  406. static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
  407. {
  408. mutex_lock(&pwm_lock);
  409. s->private = "";
  410. return seq_list_start(&pwm_chips, *pos);
  411. }
  412. static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
  413. {
  414. s->private = "\n";
  415. return seq_list_next(v, &pwm_chips, pos);
  416. }
  417. static void pwm_seq_stop(struct seq_file *s, void *v)
  418. {
  419. mutex_unlock(&pwm_lock);
  420. }
  421. static int pwm_seq_show(struct seq_file *s, void *v)
  422. {
  423. struct pwm_chip *chip = list_entry(v, struct pwm_chip, list);
  424. seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private,
  425. chip->dev->bus ? chip->dev->bus->name : "no-bus",
  426. dev_name(chip->dev), chip->npwm,
  427. (chip->npwm != 1) ? "s" : "");
  428. if (chip->ops->dbg_show)
  429. chip->ops->dbg_show(chip, s);
  430. else
  431. pwm_dbg_show(chip, s);
  432. return 0;
  433. }
  434. static const struct seq_operations pwm_seq_ops = {
  435. .start = pwm_seq_start,
  436. .next = pwm_seq_next,
  437. .stop = pwm_seq_stop,
  438. .show = pwm_seq_show,
  439. };
  440. static int pwm_seq_open(struct inode *inode, struct file *file)
  441. {
  442. return seq_open(file, &pwm_seq_ops);
  443. }
  444. static const struct file_operations pwm_debugfs_ops = {
  445. .owner = THIS_MODULE,
  446. .open = pwm_seq_open,
  447. .read = seq_read,
  448. .llseek = seq_lseek,
  449. .release = seq_release,
  450. };
  451. static int __init pwm_debugfs_init(void)
  452. {
  453. debugfs_create_file("pwm", S_IFREG | S_IRUGO, NULL, NULL,
  454. &pwm_debugfs_ops);
  455. return 0;
  456. }
  457. subsys_initcall(pwm_debugfs_init);
  458. #endif /* CONFIG_DEBUG_FS */