core.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. #define MAX_PWMS 1024
  30. static DEFINE_MUTEX(pwm_lock);
  31. static LIST_HEAD(pwm_chips);
  32. static DECLARE_BITMAP(allocated_pwms, MAX_PWMS);
  33. static RADIX_TREE(pwm_tree, GFP_KERNEL);
  34. static struct pwm_device *pwm_to_device(unsigned int pwm)
  35. {
  36. return radix_tree_lookup(&pwm_tree, pwm);
  37. }
  38. static int alloc_pwms(int pwm, unsigned int count)
  39. {
  40. unsigned int from = 0;
  41. unsigned int start;
  42. if (pwm >= MAX_PWMS)
  43. return -EINVAL;
  44. if (pwm >= 0)
  45. from = pwm;
  46. start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, from,
  47. count, 0);
  48. if (pwm >= 0 && start != pwm)
  49. return -EEXIST;
  50. if (start + count > MAX_PWMS)
  51. return -ENOSPC;
  52. return start;
  53. }
  54. static void free_pwms(struct pwm_chip *chip)
  55. {
  56. unsigned int i;
  57. for (i = 0; i < chip->npwm; i++) {
  58. struct pwm_device *pwm = &chip->pwms[i];
  59. radix_tree_delete(&pwm_tree, pwm->pwm);
  60. }
  61. bitmap_clear(allocated_pwms, chip->base, chip->npwm);
  62. kfree(chip->pwms);
  63. chip->pwms = NULL;
  64. }
  65. static int pwm_device_request(struct pwm_device *pwm, const char *label)
  66. {
  67. int err;
  68. if (test_bit(PWMF_REQUESTED, &pwm->flags))
  69. return -EBUSY;
  70. if (!try_module_get(pwm->chip->ops->owner))
  71. return -ENODEV;
  72. if (pwm->chip->ops->request) {
  73. err = pwm->chip->ops->request(pwm->chip, pwm);
  74. if (err) {
  75. module_put(pwm->chip->ops->owner);
  76. return err;
  77. }
  78. }
  79. set_bit(PWMF_REQUESTED, &pwm->flags);
  80. pwm->label = label;
  81. return 0;
  82. }
  83. /**
  84. * pwm_set_chip_data() - set private chip data for a PWM
  85. * @pwm: PWM device
  86. * @data: pointer to chip-specific data
  87. */
  88. int pwm_set_chip_data(struct pwm_device *pwm, void *data)
  89. {
  90. if (!pwm)
  91. return -EINVAL;
  92. pwm->chip_data = data;
  93. return 0;
  94. }
  95. /**
  96. * pwm_get_chip_data() - get private chip data for a PWM
  97. * @pwm: PWM device
  98. */
  99. void *pwm_get_chip_data(struct pwm_device *pwm)
  100. {
  101. return pwm ? pwm->chip_data : NULL;
  102. }
  103. /**
  104. * pwmchip_add() - register a new PWM chip
  105. * @chip: the PWM chip to add
  106. *
  107. * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base
  108. * will be used.
  109. */
  110. int pwmchip_add(struct pwm_chip *chip)
  111. {
  112. struct pwm_device *pwm;
  113. unsigned int i;
  114. int ret;
  115. if (!chip || !chip->dev || !chip->ops || !chip->ops->config ||
  116. !chip->ops->enable || !chip->ops->disable)
  117. return -EINVAL;
  118. mutex_lock(&pwm_lock);
  119. ret = alloc_pwms(chip->base, chip->npwm);
  120. if (ret < 0)
  121. goto out;
  122. chip->pwms = kzalloc(chip->npwm * sizeof(*pwm), GFP_KERNEL);
  123. if (!chip->pwms) {
  124. ret = -ENOMEM;
  125. goto out;
  126. }
  127. chip->base = ret;
  128. for (i = 0; i < chip->npwm; i++) {
  129. pwm = &chip->pwms[i];
  130. pwm->chip = chip;
  131. pwm->pwm = chip->base + i;
  132. pwm->hwpwm = i;
  133. radix_tree_insert(&pwm_tree, pwm->pwm, pwm);
  134. }
  135. bitmap_set(allocated_pwms, chip->base, chip->npwm);
  136. INIT_LIST_HEAD(&chip->list);
  137. list_add(&chip->list, &pwm_chips);
  138. ret = 0;
  139. out:
  140. mutex_unlock(&pwm_lock);
  141. return ret;
  142. }
  143. EXPORT_SYMBOL_GPL(pwmchip_add);
  144. /**
  145. * pwmchip_remove() - remove a PWM chip
  146. * @chip: the PWM chip to remove
  147. *
  148. * Removes a PWM chip. This function may return busy if the PWM chip provides
  149. * a PWM device that is still requested.
  150. */
  151. int pwmchip_remove(struct pwm_chip *chip)
  152. {
  153. unsigned int i;
  154. int ret = 0;
  155. mutex_lock(&pwm_lock);
  156. for (i = 0; i < chip->npwm; i++) {
  157. struct pwm_device *pwm = &chip->pwms[i];
  158. if (test_bit(PWMF_REQUESTED, &pwm->flags)) {
  159. ret = -EBUSY;
  160. goto out;
  161. }
  162. }
  163. list_del_init(&chip->list);
  164. free_pwms(chip);
  165. out:
  166. mutex_unlock(&pwm_lock);
  167. return ret;
  168. }
  169. EXPORT_SYMBOL_GPL(pwmchip_remove);
  170. /**
  171. * pwm_request() - request a PWM device
  172. * @pwm_id: global PWM device index
  173. * @label: PWM device label
  174. */
  175. struct pwm_device *pwm_request(int pwm, const char *label)
  176. {
  177. struct pwm_device *dev;
  178. int err;
  179. if (pwm < 0 || pwm >= MAX_PWMS)
  180. return ERR_PTR(-EINVAL);
  181. mutex_lock(&pwm_lock);
  182. dev = pwm_to_device(pwm);
  183. if (!dev) {
  184. dev = ERR_PTR(-EPROBE_DEFER);
  185. goto out;
  186. }
  187. err = pwm_device_request(dev, label);
  188. if (err < 0)
  189. dev = ERR_PTR(err);
  190. out:
  191. mutex_unlock(&pwm_lock);
  192. return dev;
  193. }
  194. EXPORT_SYMBOL_GPL(pwm_request);
  195. /**
  196. * pwm_request_from_chip() - request a PWM device relative to a PWM chip
  197. * @chip: PWM chip
  198. * @index: per-chip index of the PWM to request
  199. * @label: a literal description string of this PWM
  200. *
  201. * Returns the PWM at the given index of the given PWM chip. A negative error
  202. * code is returned if the index is not valid for the specified PWM chip or
  203. * if the PWM device cannot be requested.
  204. */
  205. struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
  206. unsigned int index,
  207. const char *label)
  208. {
  209. struct pwm_device *pwm;
  210. int err;
  211. if (!chip || index >= chip->npwm)
  212. return ERR_PTR(-EINVAL);
  213. mutex_lock(&pwm_lock);
  214. pwm = &chip->pwms[index];
  215. err = pwm_device_request(pwm, label);
  216. if (err < 0)
  217. pwm = ERR_PTR(err);
  218. mutex_unlock(&pwm_lock);
  219. return pwm;
  220. }
  221. EXPORT_SYMBOL_GPL(pwm_request_from_chip);
  222. /**
  223. * pwm_free() - free a PWM device
  224. * @pwm: PWM device
  225. */
  226. void pwm_free(struct pwm_device *pwm)
  227. {
  228. mutex_lock(&pwm_lock);
  229. if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
  230. pr_warning("PWM device already freed\n");
  231. goto out;
  232. }
  233. if (pwm->chip->ops->free)
  234. pwm->chip->ops->free(pwm->chip, pwm);
  235. pwm->label = NULL;
  236. module_put(pwm->chip->ops->owner);
  237. out:
  238. mutex_unlock(&pwm_lock);
  239. }
  240. EXPORT_SYMBOL_GPL(pwm_free);
  241. /**
  242. * pwm_config() - change a PWM device configuration
  243. * @pwm: PWM device
  244. * @duty_ns: "on" time (in nanoseconds)
  245. * @period_ns: duration (in nanoseconds) of one cycle
  246. */
  247. int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
  248. {
  249. if (!pwm || period_ns == 0 || duty_ns > period_ns)
  250. return -EINVAL;
  251. return pwm->chip->ops->config(pwm->chip, pwm, duty_ns, period_ns);
  252. }
  253. EXPORT_SYMBOL_GPL(pwm_config);
  254. /**
  255. * pwm_enable() - start a PWM output toggling
  256. * @pwm: PWM device
  257. */
  258. int pwm_enable(struct pwm_device *pwm)
  259. {
  260. if (pwm && !test_and_set_bit(PWMF_ENABLED, &pwm->flags))
  261. return pwm->chip->ops->enable(pwm->chip, pwm);
  262. return pwm ? 0 : -EINVAL;
  263. }
  264. EXPORT_SYMBOL_GPL(pwm_enable);
  265. /**
  266. * pwm_disable() - stop a PWM output toggling
  267. * @pwm: PWM device
  268. */
  269. void pwm_disable(struct pwm_device *pwm)
  270. {
  271. if (pwm && test_and_clear_bit(PWMF_ENABLED, &pwm->flags))
  272. pwm->chip->ops->disable(pwm->chip, pwm);
  273. }
  274. EXPORT_SYMBOL_GPL(pwm_disable);