sysfs.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 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 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 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 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 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. return sprintf(buf, "%d\n", pwm_is_enabled(pwm));
  83. }
  84. static ssize_t enable_store(struct device *child,
  85. struct device_attribute *attr,
  86. const char *buf, size_t size)
  87. {
  88. struct pwm_device *pwm = child_to_pwm_device(child);
  89. int val, ret;
  90. ret = kstrtoint(buf, 0, &val);
  91. if (ret)
  92. return ret;
  93. switch (val) {
  94. case 0:
  95. pwm_disable(pwm);
  96. break;
  97. case 1:
  98. ret = pwm_enable(pwm);
  99. break;
  100. default:
  101. ret = -EINVAL;
  102. break;
  103. }
  104. return ret ? : size;
  105. }
  106. static ssize_t polarity_show(struct device *child,
  107. struct device_attribute *attr,
  108. char *buf)
  109. {
  110. const struct pwm_device *pwm = child_to_pwm_device(child);
  111. const char *polarity = "unknown";
  112. switch (pwm_get_polarity(pwm)) {
  113. case PWM_POLARITY_NORMAL:
  114. polarity = "normal";
  115. break;
  116. case PWM_POLARITY_INVERSED:
  117. polarity = "inversed";
  118. break;
  119. }
  120. return sprintf(buf, "%s\n", polarity);
  121. }
  122. static ssize_t polarity_store(struct device *child,
  123. struct device_attribute *attr,
  124. const char *buf, size_t size)
  125. {
  126. struct pwm_device *pwm = child_to_pwm_device(child);
  127. enum pwm_polarity polarity;
  128. int ret;
  129. if (sysfs_streq(buf, "normal"))
  130. polarity = PWM_POLARITY_NORMAL;
  131. else if (sysfs_streq(buf, "inversed"))
  132. polarity = PWM_POLARITY_INVERSED;
  133. else
  134. return -EINVAL;
  135. ret = pwm_set_polarity(pwm, polarity);
  136. return ret ? : size;
  137. }
  138. static DEVICE_ATTR_RW(period);
  139. static DEVICE_ATTR_RW(duty_cycle);
  140. static DEVICE_ATTR_RW(enable);
  141. static DEVICE_ATTR_RW(polarity);
  142. static struct attribute *pwm_attrs[] = {
  143. &dev_attr_period.attr,
  144. &dev_attr_duty_cycle.attr,
  145. &dev_attr_enable.attr,
  146. &dev_attr_polarity.attr,
  147. NULL
  148. };
  149. ATTRIBUTE_GROUPS(pwm);
  150. static void pwm_export_release(struct device *child)
  151. {
  152. struct pwm_export *export = child_to_pwm_export(child);
  153. kfree(export);
  154. }
  155. static int pwm_export_child(struct device *parent, struct pwm_device *pwm)
  156. {
  157. struct pwm_export *export;
  158. int ret;
  159. if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags))
  160. return -EBUSY;
  161. export = kzalloc(sizeof(*export), GFP_KERNEL);
  162. if (!export) {
  163. clear_bit(PWMF_EXPORTED, &pwm->flags);
  164. return -ENOMEM;
  165. }
  166. export->pwm = pwm;
  167. export->child.release = pwm_export_release;
  168. export->child.parent = parent;
  169. export->child.devt = MKDEV(0, 0);
  170. export->child.groups = pwm_groups;
  171. dev_set_name(&export->child, "pwm%u", pwm->hwpwm);
  172. ret = device_register(&export->child);
  173. if (ret) {
  174. clear_bit(PWMF_EXPORTED, &pwm->flags);
  175. kfree(export);
  176. return ret;
  177. }
  178. return 0;
  179. }
  180. static int pwm_unexport_match(struct device *child, void *data)
  181. {
  182. return child_to_pwm_device(child) == data;
  183. }
  184. static int pwm_unexport_child(struct device *parent, struct pwm_device *pwm)
  185. {
  186. struct device *child;
  187. if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags))
  188. return -ENODEV;
  189. child = device_find_child(parent, pwm, pwm_unexport_match);
  190. if (!child)
  191. return -ENODEV;
  192. /* for device_find_child() */
  193. put_device(child);
  194. device_unregister(child);
  195. pwm_put(pwm);
  196. return 0;
  197. }
  198. static ssize_t export_store(struct device *parent,
  199. struct device_attribute *attr,
  200. const char *buf, size_t len)
  201. {
  202. struct pwm_chip *chip = dev_get_drvdata(parent);
  203. struct pwm_device *pwm;
  204. unsigned int hwpwm;
  205. int ret;
  206. ret = kstrtouint(buf, 0, &hwpwm);
  207. if (ret < 0)
  208. return ret;
  209. if (hwpwm >= chip->npwm)
  210. return -ENODEV;
  211. pwm = pwm_request_from_chip(chip, hwpwm, "sysfs");
  212. if (IS_ERR(pwm))
  213. return PTR_ERR(pwm);
  214. ret = pwm_export_child(parent, pwm);
  215. if (ret < 0)
  216. pwm_put(pwm);
  217. return ret ? : len;
  218. }
  219. static DEVICE_ATTR_WO(export);
  220. static ssize_t unexport_store(struct device *parent,
  221. struct device_attribute *attr,
  222. const char *buf, size_t len)
  223. {
  224. struct pwm_chip *chip = dev_get_drvdata(parent);
  225. unsigned int hwpwm;
  226. int ret;
  227. ret = kstrtouint(buf, 0, &hwpwm);
  228. if (ret < 0)
  229. return ret;
  230. if (hwpwm >= chip->npwm)
  231. return -ENODEV;
  232. ret = pwm_unexport_child(parent, &chip->pwms[hwpwm]);
  233. return ret ? : len;
  234. }
  235. static DEVICE_ATTR_WO(unexport);
  236. static ssize_t npwm_show(struct device *parent, struct device_attribute *attr,
  237. char *buf)
  238. {
  239. const struct pwm_chip *chip = dev_get_drvdata(parent);
  240. return sprintf(buf, "%u\n", chip->npwm);
  241. }
  242. static DEVICE_ATTR_RO(npwm);
  243. static struct attribute *pwm_chip_attrs[] = {
  244. &dev_attr_export.attr,
  245. &dev_attr_unexport.attr,
  246. &dev_attr_npwm.attr,
  247. NULL,
  248. };
  249. ATTRIBUTE_GROUPS(pwm_chip);
  250. static struct class pwm_class = {
  251. .name = "pwm",
  252. .owner = THIS_MODULE,
  253. .dev_groups = pwm_chip_groups,
  254. };
  255. static int pwmchip_sysfs_match(struct device *parent, const void *data)
  256. {
  257. return dev_get_drvdata(parent) == data;
  258. }
  259. void pwmchip_sysfs_export(struct pwm_chip *chip)
  260. {
  261. struct device *parent;
  262. /*
  263. * If device_create() fails the pwm_chip is still usable by
  264. * the kernel its just not exported.
  265. */
  266. parent = device_create(&pwm_class, chip->dev, MKDEV(0, 0), chip,
  267. "pwmchip%d", chip->base);
  268. if (IS_ERR(parent)) {
  269. dev_warn(chip->dev,
  270. "device_create failed for pwm_chip sysfs export\n");
  271. }
  272. }
  273. void pwmchip_sysfs_unexport(struct pwm_chip *chip)
  274. {
  275. struct device *parent;
  276. parent = class_find_device(&pwm_class, NULL, chip,
  277. pwmchip_sysfs_match);
  278. if (parent) {
  279. /* for class_find_device() */
  280. put_device(parent);
  281. device_unregister(parent);
  282. }
  283. }
  284. void pwmchip_sysfs_unexport_children(struct pwm_chip *chip)
  285. {
  286. struct device *parent;
  287. unsigned int i;
  288. parent = class_find_device(&pwm_class, NULL, chip,
  289. pwmchip_sysfs_match);
  290. if (!parent)
  291. return;
  292. for (i = 0; i < chip->npwm; i++) {
  293. struct pwm_device *pwm = &chip->pwms[i];
  294. if (test_bit(PWMF_EXPORTED, &pwm->flags))
  295. pwm_unexport_child(parent, pwm);
  296. }
  297. put_device(parent);
  298. }
  299. static int __init pwm_sysfs_init(void)
  300. {
  301. return class_register(&pwm_class);
  302. }
  303. subsys_initcall(pwm_sysfs_init);