sysfs.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * A simple sysfs interface for the generic PWM framework
  3. *
  4. * Copyright (C) 2013 H Hartley Sweeten <hsweeten@visionengravers.com>
  5. *
  6. * Based on previous work by Lars Poeschel <poeschel@lemonage.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/device.h>
  19. #include <linux/mutex.h>
  20. #include <linux/err.h>
  21. #include <linux/slab.h>
  22. #include <linux/kdev_t.h>
  23. #include <linux/pwm.h>
  24. struct pwm_export {
  25. struct device child;
  26. struct pwm_device *pwm;
  27. struct mutex lock;
  28. };
  29. static struct pwm_export *child_to_pwm_export(struct device *child)
  30. {
  31. return container_of(child, struct pwm_export, child);
  32. }
  33. static struct pwm_device *child_to_pwm_device(struct device *child)
  34. {
  35. struct pwm_export *export = child_to_pwm_export(child);
  36. return export->pwm;
  37. }
  38. static ssize_t period_show(struct device *child,
  39. struct device_attribute *attr,
  40. char *buf)
  41. {
  42. const struct pwm_device *pwm = child_to_pwm_device(child);
  43. return sprintf(buf, "%u\n", pwm_get_period(pwm));
  44. }
  45. static ssize_t period_store(struct device *child,
  46. struct device_attribute *attr,
  47. const char *buf, size_t size)
  48. {
  49. struct pwm_export *export = child_to_pwm_export(child);
  50. struct pwm_device *pwm = export->pwm;
  51. unsigned int val;
  52. int ret;
  53. ret = kstrtouint(buf, 0, &val);
  54. if (ret)
  55. return ret;
  56. mutex_lock(&export->lock);
  57. ret = pwm_config(pwm, pwm_get_duty_cycle(pwm), val);
  58. mutex_unlock(&export->lock);
  59. return ret ? : size;
  60. }
  61. static ssize_t duty_cycle_show(struct device *child,
  62. struct device_attribute *attr,
  63. char *buf)
  64. {
  65. const struct pwm_device *pwm = child_to_pwm_device(child);
  66. return sprintf(buf, "%u\n", pwm_get_duty_cycle(pwm));
  67. }
  68. static ssize_t duty_cycle_store(struct device *child,
  69. struct device_attribute *attr,
  70. const char *buf, size_t size)
  71. {
  72. struct pwm_export *export = child_to_pwm_export(child);
  73. struct pwm_device *pwm = export->pwm;
  74. unsigned int val;
  75. int ret;
  76. ret = kstrtouint(buf, 0, &val);
  77. if (ret)
  78. return ret;
  79. mutex_lock(&export->lock);
  80. ret = pwm_config(pwm, val, pwm_get_period(pwm));
  81. mutex_unlock(&export->lock);
  82. return ret ? : size;
  83. }
  84. static ssize_t enable_show(struct device *child,
  85. struct device_attribute *attr,
  86. char *buf)
  87. {
  88. const struct pwm_device *pwm = child_to_pwm_device(child);
  89. return sprintf(buf, "%d\n", pwm_is_enabled(pwm));
  90. }
  91. static ssize_t enable_store(struct device *child,
  92. struct device_attribute *attr,
  93. const char *buf, size_t size)
  94. {
  95. struct pwm_export *export = child_to_pwm_export(child);
  96. struct pwm_device *pwm = export->pwm;
  97. int val, ret;
  98. ret = kstrtoint(buf, 0, &val);
  99. if (ret)
  100. return ret;
  101. mutex_lock(&export->lock);
  102. switch (val) {
  103. case 0:
  104. pwm_disable(pwm);
  105. break;
  106. case 1:
  107. ret = pwm_enable(pwm);
  108. break;
  109. default:
  110. ret = -EINVAL;
  111. break;
  112. }
  113. mutex_unlock(&export->lock);
  114. return ret ? : size;
  115. }
  116. static ssize_t polarity_show(struct device *child,
  117. struct device_attribute *attr,
  118. char *buf)
  119. {
  120. const struct pwm_device *pwm = child_to_pwm_device(child);
  121. const char *polarity = "unknown";
  122. switch (pwm_get_polarity(pwm)) {
  123. case PWM_POLARITY_NORMAL:
  124. polarity = "normal";
  125. break;
  126. case PWM_POLARITY_INVERSED:
  127. polarity = "inversed";
  128. break;
  129. }
  130. return sprintf(buf, "%s\n", polarity);
  131. }
  132. static ssize_t polarity_store(struct device *child,
  133. struct device_attribute *attr,
  134. const char *buf, size_t size)
  135. {
  136. struct pwm_export *export = child_to_pwm_export(child);
  137. struct pwm_device *pwm = export->pwm;
  138. enum pwm_polarity polarity;
  139. int ret;
  140. if (sysfs_streq(buf, "normal"))
  141. polarity = PWM_POLARITY_NORMAL;
  142. else if (sysfs_streq(buf, "inversed"))
  143. polarity = PWM_POLARITY_INVERSED;
  144. else
  145. return -EINVAL;
  146. mutex_lock(&export->lock);
  147. ret = pwm_set_polarity(pwm, polarity);
  148. mutex_unlock(&export->lock);
  149. return ret ? : size;
  150. }
  151. static DEVICE_ATTR_RW(period);
  152. static DEVICE_ATTR_RW(duty_cycle);
  153. static DEVICE_ATTR_RW(enable);
  154. static DEVICE_ATTR_RW(polarity);
  155. static struct attribute *pwm_attrs[] = {
  156. &dev_attr_period.attr,
  157. &dev_attr_duty_cycle.attr,
  158. &dev_attr_enable.attr,
  159. &dev_attr_polarity.attr,
  160. NULL
  161. };
  162. ATTRIBUTE_GROUPS(pwm);
  163. static void pwm_export_release(struct device *child)
  164. {
  165. struct pwm_export *export = child_to_pwm_export(child);
  166. kfree(export);
  167. }
  168. static int pwm_export_child(struct device *parent, struct pwm_device *pwm)
  169. {
  170. struct pwm_export *export;
  171. int ret;
  172. if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags))
  173. return -EBUSY;
  174. export = kzalloc(sizeof(*export), GFP_KERNEL);
  175. if (!export) {
  176. clear_bit(PWMF_EXPORTED, &pwm->flags);
  177. return -ENOMEM;
  178. }
  179. export->pwm = pwm;
  180. mutex_init(&export->lock);
  181. export->child.release = pwm_export_release;
  182. export->child.parent = parent;
  183. export->child.devt = MKDEV(0, 0);
  184. export->child.groups = pwm_groups;
  185. dev_set_name(&export->child, "pwm%u", pwm->hwpwm);
  186. ret = device_register(&export->child);
  187. if (ret) {
  188. clear_bit(PWMF_EXPORTED, &pwm->flags);
  189. kfree(export);
  190. return ret;
  191. }
  192. return 0;
  193. }
  194. static int pwm_unexport_match(struct device *child, void *data)
  195. {
  196. return child_to_pwm_device(child) == data;
  197. }
  198. static int pwm_unexport_child(struct device *parent, struct pwm_device *pwm)
  199. {
  200. struct device *child;
  201. if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags))
  202. return -ENODEV;
  203. child = device_find_child(parent, pwm, pwm_unexport_match);
  204. if (!child)
  205. return -ENODEV;
  206. /* for device_find_child() */
  207. put_device(child);
  208. device_unregister(child);
  209. pwm_put(pwm);
  210. return 0;
  211. }
  212. static ssize_t export_store(struct device *parent,
  213. struct device_attribute *attr,
  214. const char *buf, size_t len)
  215. {
  216. struct pwm_chip *chip = dev_get_drvdata(parent);
  217. struct pwm_device *pwm;
  218. unsigned int hwpwm;
  219. int ret;
  220. ret = kstrtouint(buf, 0, &hwpwm);
  221. if (ret < 0)
  222. return ret;
  223. if (hwpwm >= chip->npwm)
  224. return -ENODEV;
  225. pwm = pwm_request_from_chip(chip, hwpwm, "sysfs");
  226. if (IS_ERR(pwm))
  227. return PTR_ERR(pwm);
  228. ret = pwm_export_child(parent, pwm);
  229. if (ret < 0)
  230. pwm_put(pwm);
  231. return ret ? : len;
  232. }
  233. static DEVICE_ATTR_WO(export);
  234. static ssize_t unexport_store(struct device *parent,
  235. struct device_attribute *attr,
  236. const char *buf, size_t len)
  237. {
  238. struct pwm_chip *chip = dev_get_drvdata(parent);
  239. unsigned int hwpwm;
  240. int ret;
  241. ret = kstrtouint(buf, 0, &hwpwm);
  242. if (ret < 0)
  243. return ret;
  244. if (hwpwm >= chip->npwm)
  245. return -ENODEV;
  246. ret = pwm_unexport_child(parent, &chip->pwms[hwpwm]);
  247. return ret ? : len;
  248. }
  249. static DEVICE_ATTR_WO(unexport);
  250. static ssize_t npwm_show(struct device *parent, struct device_attribute *attr,
  251. char *buf)
  252. {
  253. const struct pwm_chip *chip = dev_get_drvdata(parent);
  254. return sprintf(buf, "%u\n", chip->npwm);
  255. }
  256. static DEVICE_ATTR_RO(npwm);
  257. static struct attribute *pwm_chip_attrs[] = {
  258. &dev_attr_export.attr,
  259. &dev_attr_unexport.attr,
  260. &dev_attr_npwm.attr,
  261. NULL,
  262. };
  263. ATTRIBUTE_GROUPS(pwm_chip);
  264. static struct class pwm_class = {
  265. .name = "pwm",
  266. .owner = THIS_MODULE,
  267. .dev_groups = pwm_chip_groups,
  268. };
  269. static int pwmchip_sysfs_match(struct device *parent, const void *data)
  270. {
  271. return dev_get_drvdata(parent) == data;
  272. }
  273. void pwmchip_sysfs_export(struct pwm_chip *chip)
  274. {
  275. struct device *parent;
  276. /*
  277. * If device_create() fails the pwm_chip is still usable by
  278. * the kernel its just not exported.
  279. */
  280. parent = device_create(&pwm_class, chip->dev, MKDEV(0, 0), chip,
  281. "pwmchip%d", chip->base);
  282. if (IS_ERR(parent)) {
  283. dev_warn(chip->dev,
  284. "device_create failed for pwm_chip sysfs export\n");
  285. }
  286. }
  287. void pwmchip_sysfs_unexport(struct pwm_chip *chip)
  288. {
  289. struct device *parent;
  290. parent = class_find_device(&pwm_class, NULL, chip,
  291. pwmchip_sysfs_match);
  292. if (parent) {
  293. /* for class_find_device() */
  294. put_device(parent);
  295. device_unregister(parent);
  296. }
  297. }
  298. static int __init pwm_sysfs_init(void)
  299. {
  300. return class_register(&pwm_class);
  301. }
  302. subsys_initcall(pwm_sysfs_init);