sysfs.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. };
  28. static struct pwm_export *child_to_pwm_export(struct device *child)
  29. {
  30. return container_of(child, struct pwm_export, child);
  31. }
  32. static struct pwm_device *child_to_pwm_device(struct device *child)
  33. {
  34. struct pwm_export *export = child_to_pwm_export(child);
  35. return export->pwm;
  36. }
  37. static ssize_t pwm_period_show(struct device *child,
  38. struct device_attribute *attr,
  39. char *buf)
  40. {
  41. const struct pwm_device *pwm = child_to_pwm_device(child);
  42. return sprintf(buf, "%u\n", pwm_get_period(pwm));
  43. }
  44. static ssize_t pwm_period_store(struct device *child,
  45. struct device_attribute *attr,
  46. const char *buf, size_t size)
  47. {
  48. struct pwm_device *pwm = child_to_pwm_device(child);
  49. unsigned int val;
  50. int ret;
  51. ret = kstrtouint(buf, 0, &val);
  52. if (ret)
  53. return ret;
  54. ret = pwm_config(pwm, pwm_get_duty_cycle(pwm), val);
  55. return ret ? : size;
  56. }
  57. static ssize_t pwm_duty_cycle_show(struct device *child,
  58. struct device_attribute *attr,
  59. char *buf)
  60. {
  61. const struct pwm_device *pwm = child_to_pwm_device(child);
  62. return sprintf(buf, "%u\n", pwm_get_duty_cycle(pwm));
  63. }
  64. static ssize_t pwm_duty_cycle_store(struct device *child,
  65. struct device_attribute *attr,
  66. const char *buf, size_t size)
  67. {
  68. struct pwm_device *pwm = child_to_pwm_device(child);
  69. unsigned int val;
  70. int ret;
  71. ret = kstrtouint(buf, 0, &val);
  72. if (ret)
  73. return ret;
  74. ret = pwm_config(pwm, val, pwm_get_period(pwm));
  75. return ret ? : size;
  76. }
  77. static ssize_t pwm_enable_show(struct device *child,
  78. struct device_attribute *attr,
  79. char *buf)
  80. {
  81. const struct pwm_device *pwm = child_to_pwm_device(child);
  82. int enabled = pwm_is_enabled(pwm);
  83. return sprintf(buf, "%d\n", enabled);
  84. }
  85. static ssize_t pwm_enable_store(struct device *child,
  86. struct device_attribute *attr,
  87. const char *buf, size_t size)
  88. {
  89. struct pwm_device *pwm = child_to_pwm_device(child);
  90. int val, ret;
  91. ret = kstrtoint(buf, 0, &val);
  92. if (ret)
  93. return ret;
  94. switch (val) {
  95. case 0:
  96. pwm_disable(pwm);
  97. break;
  98. case 1:
  99. ret = pwm_enable(pwm);
  100. break;
  101. default:
  102. ret = -EINVAL;
  103. break;
  104. }
  105. return ret ? : size;
  106. }
  107. static ssize_t pwm_polarity_show(struct device *child,
  108. struct device_attribute *attr,
  109. char *buf)
  110. {
  111. const struct pwm_device *pwm = child_to_pwm_device(child);
  112. const char *polarity = "unknown";
  113. switch (pwm_get_polarity(pwm)) {
  114. case PWM_POLARITY_NORMAL:
  115. polarity = "normal";
  116. break;
  117. case PWM_POLARITY_INVERSED:
  118. polarity = "inversed";
  119. break;
  120. }
  121. return sprintf(buf, "%s\n", polarity);
  122. }
  123. static ssize_t pwm_polarity_store(struct device *child,
  124. struct device_attribute *attr,
  125. const char *buf, size_t size)
  126. {
  127. struct pwm_device *pwm = child_to_pwm_device(child);
  128. enum pwm_polarity polarity;
  129. int ret;
  130. if (sysfs_streq(buf, "normal"))
  131. polarity = PWM_POLARITY_NORMAL;
  132. else if (sysfs_streq(buf, "inversed"))
  133. polarity = PWM_POLARITY_INVERSED;
  134. else
  135. return -EINVAL;
  136. ret = pwm_set_polarity(pwm, polarity);
  137. return ret ? : size;
  138. }
  139. static DEVICE_ATTR(period, 0644, pwm_period_show, pwm_period_store);
  140. static DEVICE_ATTR(duty_cycle, 0644, pwm_duty_cycle_show, pwm_duty_cycle_store);
  141. static DEVICE_ATTR(enable, 0644, pwm_enable_show, pwm_enable_store);
  142. static DEVICE_ATTR(polarity, 0644, pwm_polarity_show, pwm_polarity_store);
  143. static struct attribute *pwm_attrs[] = {
  144. &dev_attr_period.attr,
  145. &dev_attr_duty_cycle.attr,
  146. &dev_attr_enable.attr,
  147. &dev_attr_polarity.attr,
  148. NULL
  149. };
  150. ATTRIBUTE_GROUPS(pwm);
  151. static void pwm_export_release(struct device *child)
  152. {
  153. struct pwm_export *export = child_to_pwm_export(child);
  154. kfree(export);
  155. }
  156. static int pwm_export_child(struct device *parent, struct pwm_device *pwm)
  157. {
  158. struct pwm_export *export;
  159. int ret;
  160. if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags))
  161. return -EBUSY;
  162. export = kzalloc(sizeof(*export), GFP_KERNEL);
  163. if (!export) {
  164. clear_bit(PWMF_EXPORTED, &pwm->flags);
  165. return -ENOMEM;
  166. }
  167. export->pwm = pwm;
  168. export->child.release = pwm_export_release;
  169. export->child.parent = parent;
  170. export->child.devt = MKDEV(0, 0);
  171. export->child.groups = pwm_groups;
  172. dev_set_name(&export->child, "pwm%u", pwm->hwpwm);
  173. ret = device_register(&export->child);
  174. if (ret) {
  175. clear_bit(PWMF_EXPORTED, &pwm->flags);
  176. kfree(export);
  177. return ret;
  178. }
  179. return 0;
  180. }
  181. static int pwm_unexport_match(struct device *child, void *data)
  182. {
  183. return child_to_pwm_device(child) == data;
  184. }
  185. static int pwm_unexport_child(struct device *parent, struct pwm_device *pwm)
  186. {
  187. struct device *child;
  188. if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags))
  189. return -ENODEV;
  190. child = device_find_child(parent, pwm, pwm_unexport_match);
  191. if (!child)
  192. return -ENODEV;
  193. /* for device_find_child() */
  194. put_device(child);
  195. device_unregister(child);
  196. pwm_put(pwm);
  197. return 0;
  198. }
  199. static ssize_t pwm_export_store(struct device *parent,
  200. struct device_attribute *attr,
  201. const char *buf, size_t len)
  202. {
  203. struct pwm_chip *chip = dev_get_drvdata(parent);
  204. struct pwm_device *pwm;
  205. unsigned int hwpwm;
  206. int ret;
  207. ret = kstrtouint(buf, 0, &hwpwm);
  208. if (ret < 0)
  209. return ret;
  210. if (hwpwm >= chip->npwm)
  211. return -ENODEV;
  212. pwm = pwm_request_from_chip(chip, hwpwm, "sysfs");
  213. if (IS_ERR(pwm))
  214. return PTR_ERR(pwm);
  215. ret = pwm_export_child(parent, pwm);
  216. if (ret < 0)
  217. pwm_put(pwm);
  218. return ret ? : len;
  219. }
  220. static DEVICE_ATTR(export, 0200, NULL, pwm_export_store);
  221. static ssize_t pwm_unexport_store(struct device *parent,
  222. struct device_attribute *attr,
  223. const char *buf, size_t len)
  224. {
  225. struct pwm_chip *chip = dev_get_drvdata(parent);
  226. unsigned int hwpwm;
  227. int ret;
  228. ret = kstrtouint(buf, 0, &hwpwm);
  229. if (ret < 0)
  230. return ret;
  231. if (hwpwm >= chip->npwm)
  232. return -ENODEV;
  233. ret = pwm_unexport_child(parent, &chip->pwms[hwpwm]);
  234. return ret ? : len;
  235. }
  236. static DEVICE_ATTR(unexport, 0200, NULL, pwm_unexport_store);
  237. static ssize_t npwm_show(struct device *parent, struct device_attribute *attr,
  238. char *buf)
  239. {
  240. const struct pwm_chip *chip = dev_get_drvdata(parent);
  241. return sprintf(buf, "%u\n", chip->npwm);
  242. }
  243. static DEVICE_ATTR_RO(npwm);
  244. static struct attribute *pwm_chip_attrs[] = {
  245. &dev_attr_export.attr,
  246. &dev_attr_unexport.attr,
  247. &dev_attr_npwm.attr,
  248. NULL,
  249. };
  250. ATTRIBUTE_GROUPS(pwm_chip);
  251. static struct class pwm_class = {
  252. .name = "pwm",
  253. .owner = THIS_MODULE,
  254. .dev_groups = pwm_chip_groups,
  255. };
  256. static int pwmchip_sysfs_match(struct device *parent, const void *data)
  257. {
  258. return dev_get_drvdata(parent) == data;
  259. }
  260. void pwmchip_sysfs_export(struct pwm_chip *chip)
  261. {
  262. struct device *parent;
  263. /*
  264. * If device_create() fails the pwm_chip is still usable by
  265. * the kernel its just not exported.
  266. */
  267. parent = device_create(&pwm_class, chip->dev, MKDEV(0, 0), chip,
  268. "pwmchip%d", chip->base);
  269. if (IS_ERR(parent)) {
  270. dev_warn(chip->dev,
  271. "device_create failed for pwm_chip sysfs export\n");
  272. }
  273. }
  274. void pwmchip_sysfs_unexport(struct pwm_chip *chip)
  275. {
  276. struct device *parent;
  277. parent = class_find_device(&pwm_class, NULL, chip,
  278. pwmchip_sysfs_match);
  279. if (parent) {
  280. /* for class_find_device() */
  281. put_device(parent);
  282. device_unregister(parent);
  283. }
  284. }
  285. static int __init pwm_sysfs_init(void)
  286. {
  287. return class_register(&pwm_class);
  288. }
  289. subsys_initcall(pwm_sysfs_init);