sysfs.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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->period);
  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->duty_cycle, 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->duty_cycle);
  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->period);
  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 = test_bit(PWMF_ENABLED, &pwm->flags);
  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. return sprintf(buf, "%s\n", pwm->polarity ? "inversed" : "normal");
  113. }
  114. static ssize_t pwm_polarity_store(struct device *child,
  115. struct device_attribute *attr,
  116. const char *buf, size_t size)
  117. {
  118. struct pwm_device *pwm = child_to_pwm_device(child);
  119. enum pwm_polarity polarity;
  120. int ret;
  121. if (sysfs_streq(buf, "normal"))
  122. polarity = PWM_POLARITY_NORMAL;
  123. else if (sysfs_streq(buf, "inversed"))
  124. polarity = PWM_POLARITY_INVERSED;
  125. else
  126. return -EINVAL;
  127. ret = pwm_set_polarity(pwm, polarity);
  128. return ret ? : size;
  129. }
  130. static DEVICE_ATTR(period, 0644, pwm_period_show, pwm_period_store);
  131. static DEVICE_ATTR(duty_cycle, 0644, pwm_duty_cycle_show, pwm_duty_cycle_store);
  132. static DEVICE_ATTR(enable, 0644, pwm_enable_show, pwm_enable_store);
  133. static DEVICE_ATTR(polarity, 0644, pwm_polarity_show, pwm_polarity_store);
  134. static struct attribute *pwm_attrs[] = {
  135. &dev_attr_period.attr,
  136. &dev_attr_duty_cycle.attr,
  137. &dev_attr_enable.attr,
  138. &dev_attr_polarity.attr,
  139. NULL
  140. };
  141. static const struct attribute_group pwm_attr_group = {
  142. .attrs = pwm_attrs,
  143. };
  144. static const struct attribute_group *pwm_attr_groups[] = {
  145. &pwm_attr_group,
  146. NULL,
  147. };
  148. static void pwm_export_release(struct device *child)
  149. {
  150. struct pwm_export *export = child_to_pwm_export(child);
  151. kfree(export);
  152. }
  153. static int pwm_export_child(struct device *parent, struct pwm_device *pwm)
  154. {
  155. struct pwm_export *export;
  156. int ret;
  157. if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags))
  158. return -EBUSY;
  159. export = kzalloc(sizeof(*export), GFP_KERNEL);
  160. if (!export) {
  161. clear_bit(PWMF_EXPORTED, &pwm->flags);
  162. return -ENOMEM;
  163. }
  164. export->pwm = pwm;
  165. export->child.release = pwm_export_release;
  166. export->child.parent = parent;
  167. export->child.devt = MKDEV(0, 0);
  168. export->child.groups = pwm_attr_groups;
  169. dev_set_name(&export->child, "pwm%u", pwm->hwpwm);
  170. ret = device_register(&export->child);
  171. if (ret) {
  172. clear_bit(PWMF_EXPORTED, &pwm->flags);
  173. kfree(export);
  174. return ret;
  175. }
  176. return 0;
  177. }
  178. static int pwm_unexport_match(struct device *child, void *data)
  179. {
  180. return child_to_pwm_device(child) == data;
  181. }
  182. static int pwm_unexport_child(struct device *parent, struct pwm_device *pwm)
  183. {
  184. struct device *child;
  185. if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags))
  186. return -ENODEV;
  187. child = device_find_child(parent, pwm, pwm_unexport_match);
  188. if (!child)
  189. return -ENODEV;
  190. /* for device_find_child() */
  191. put_device(child);
  192. device_unregister(child);
  193. pwm_put(pwm);
  194. return 0;
  195. }
  196. static ssize_t pwm_export_store(struct device *parent,
  197. struct device_attribute *attr,
  198. const char *buf, size_t len)
  199. {
  200. struct pwm_chip *chip = dev_get_drvdata(parent);
  201. struct pwm_device *pwm;
  202. unsigned int hwpwm;
  203. int ret;
  204. ret = kstrtouint(buf, 0, &hwpwm);
  205. if (ret < 0)
  206. return ret;
  207. if (hwpwm >= chip->npwm)
  208. return -ENODEV;
  209. pwm = pwm_request_from_chip(chip, hwpwm, "sysfs");
  210. if (IS_ERR(pwm))
  211. return PTR_ERR(pwm);
  212. ret = pwm_export_child(parent, pwm);
  213. if (ret < 0)
  214. pwm_put(pwm);
  215. return ret ? : len;
  216. }
  217. static DEVICE_ATTR(export, 0200, NULL, pwm_export_store);
  218. static ssize_t pwm_unexport_store(struct device *parent,
  219. struct device_attribute *attr,
  220. const char *buf, size_t len)
  221. {
  222. struct pwm_chip *chip = dev_get_drvdata(parent);
  223. unsigned int hwpwm;
  224. int ret;
  225. ret = kstrtouint(buf, 0, &hwpwm);
  226. if (ret < 0)
  227. return ret;
  228. if (hwpwm >= chip->npwm)
  229. return -ENODEV;
  230. ret = pwm_unexport_child(parent, &chip->pwms[hwpwm]);
  231. return ret ? : len;
  232. }
  233. static DEVICE_ATTR(unexport, 0200, NULL, pwm_unexport_store);
  234. static ssize_t npwm_show(struct device *parent, struct device_attribute *attr,
  235. char *buf)
  236. {
  237. const struct pwm_chip *chip = dev_get_drvdata(parent);
  238. return sprintf(buf, "%u\n", chip->npwm);
  239. }
  240. static DEVICE_ATTR_RO(npwm);
  241. static struct attribute *pwm_chip_attrs[] = {
  242. &dev_attr_export.attr,
  243. &dev_attr_unexport.attr,
  244. &dev_attr_npwm.attr,
  245. NULL,
  246. };
  247. ATTRIBUTE_GROUPS(pwm_chip);
  248. static struct class pwm_class = {
  249. .name = "pwm",
  250. .owner = THIS_MODULE,
  251. .dev_groups = pwm_chip_groups,
  252. };
  253. static int pwmchip_sysfs_match(struct device *parent, const void *data)
  254. {
  255. return dev_get_drvdata(parent) == data;
  256. }
  257. void pwmchip_sysfs_export(struct pwm_chip *chip)
  258. {
  259. struct device *parent;
  260. /*
  261. * If device_create() fails the pwm_chip is still usable by
  262. * the kernel its just not exported.
  263. */
  264. parent = device_create(&pwm_class, chip->dev, MKDEV(0, 0), chip,
  265. "pwmchip%d", chip->base);
  266. if (IS_ERR(parent)) {
  267. dev_warn(chip->dev,
  268. "device_create failed for pwm_chip sysfs export\n");
  269. }
  270. }
  271. void pwmchip_sysfs_unexport(struct pwm_chip *chip)
  272. {
  273. struct device *parent;
  274. parent = class_find_device(&pwm_class, NULL, chip,
  275. pwmchip_sysfs_match);
  276. if (parent) {
  277. /* for class_find_device() */
  278. put_device(parent);
  279. device_unregister(parent);
  280. }
  281. }
  282. static int __init pwm_sysfs_init(void)
  283. {
  284. return class_register(&pwm_class);
  285. }
  286. subsys_initcall(pwm_sysfs_init);