sysfs.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. struct pwm_state state;
  44. pwm_get_state(pwm, &state);
  45. return sprintf(buf, "%u\n", state.period);
  46. }
  47. static ssize_t period_store(struct device *child,
  48. struct device_attribute *attr,
  49. const char *buf, size_t size)
  50. {
  51. struct pwm_export *export = child_to_pwm_export(child);
  52. struct pwm_device *pwm = export->pwm;
  53. struct pwm_state state;
  54. unsigned int val;
  55. int ret;
  56. ret = kstrtouint(buf, 0, &val);
  57. if (ret)
  58. return ret;
  59. mutex_lock(&export->lock);
  60. pwm_get_state(pwm, &state);
  61. state.period = val;
  62. ret = pwm_apply_state(pwm, &state);
  63. mutex_unlock(&export->lock);
  64. return ret ? : size;
  65. }
  66. static ssize_t duty_cycle_show(struct device *child,
  67. struct device_attribute *attr,
  68. char *buf)
  69. {
  70. const struct pwm_device *pwm = child_to_pwm_device(child);
  71. struct pwm_state state;
  72. pwm_get_state(pwm, &state);
  73. return sprintf(buf, "%u\n", state.duty_cycle);
  74. }
  75. static ssize_t duty_cycle_store(struct device *child,
  76. struct device_attribute *attr,
  77. const char *buf, size_t size)
  78. {
  79. struct pwm_export *export = child_to_pwm_export(child);
  80. struct pwm_device *pwm = export->pwm;
  81. struct pwm_state state;
  82. unsigned int val;
  83. int ret;
  84. ret = kstrtouint(buf, 0, &val);
  85. if (ret)
  86. return ret;
  87. mutex_lock(&export->lock);
  88. pwm_get_state(pwm, &state);
  89. state.duty_cycle = val;
  90. ret = pwm_apply_state(pwm, &state);
  91. mutex_unlock(&export->lock);
  92. return ret ? : size;
  93. }
  94. static ssize_t enable_show(struct device *child,
  95. struct device_attribute *attr,
  96. char *buf)
  97. {
  98. const struct pwm_device *pwm = child_to_pwm_device(child);
  99. struct pwm_state state;
  100. pwm_get_state(pwm, &state);
  101. return sprintf(buf, "%d\n", state.enabled);
  102. }
  103. static ssize_t enable_store(struct device *child,
  104. struct device_attribute *attr,
  105. const char *buf, size_t size)
  106. {
  107. struct pwm_export *export = child_to_pwm_export(child);
  108. struct pwm_device *pwm = export->pwm;
  109. struct pwm_state state;
  110. int val, ret;
  111. ret = kstrtoint(buf, 0, &val);
  112. if (ret)
  113. return ret;
  114. mutex_lock(&export->lock);
  115. pwm_get_state(pwm, &state);
  116. switch (val) {
  117. case 0:
  118. state.enabled = false;
  119. break;
  120. case 1:
  121. state.enabled = true;
  122. break;
  123. default:
  124. ret = -EINVAL;
  125. goto unlock;
  126. }
  127. ret = pwm_apply_state(pwm, &state);
  128. unlock:
  129. mutex_unlock(&export->lock);
  130. return ret ? : size;
  131. }
  132. static ssize_t polarity_show(struct device *child,
  133. struct device_attribute *attr,
  134. char *buf)
  135. {
  136. const struct pwm_device *pwm = child_to_pwm_device(child);
  137. const char *polarity = "unknown";
  138. struct pwm_state state;
  139. pwm_get_state(pwm, &state);
  140. switch (state.polarity) {
  141. case PWM_POLARITY_NORMAL:
  142. polarity = "normal";
  143. break;
  144. case PWM_POLARITY_INVERSED:
  145. polarity = "inversed";
  146. break;
  147. }
  148. return sprintf(buf, "%s\n", polarity);
  149. }
  150. static ssize_t polarity_store(struct device *child,
  151. struct device_attribute *attr,
  152. const char *buf, size_t size)
  153. {
  154. struct pwm_export *export = child_to_pwm_export(child);
  155. struct pwm_device *pwm = export->pwm;
  156. enum pwm_polarity polarity;
  157. struct pwm_state state;
  158. int ret;
  159. if (sysfs_streq(buf, "normal"))
  160. polarity = PWM_POLARITY_NORMAL;
  161. else if (sysfs_streq(buf, "inversed"))
  162. polarity = PWM_POLARITY_INVERSED;
  163. else
  164. return -EINVAL;
  165. mutex_lock(&export->lock);
  166. pwm_get_state(pwm, &state);
  167. state.polarity = polarity;
  168. ret = pwm_apply_state(pwm, &state);
  169. mutex_unlock(&export->lock);
  170. return ret ? : size;
  171. }
  172. static ssize_t capture_show(struct device *child,
  173. struct device_attribute *attr,
  174. char *buf)
  175. {
  176. struct pwm_device *pwm = child_to_pwm_device(child);
  177. struct pwm_capture result;
  178. int ret;
  179. ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ));
  180. if (ret)
  181. return ret;
  182. return sprintf(buf, "%u %u\n", result.period, result.duty_cycle);
  183. }
  184. static DEVICE_ATTR_RW(period);
  185. static DEVICE_ATTR_RW(duty_cycle);
  186. static DEVICE_ATTR_RW(enable);
  187. static DEVICE_ATTR_RW(polarity);
  188. static DEVICE_ATTR_RO(capture);
  189. static struct attribute *pwm_attrs[] = {
  190. &dev_attr_period.attr,
  191. &dev_attr_duty_cycle.attr,
  192. &dev_attr_enable.attr,
  193. &dev_attr_polarity.attr,
  194. &dev_attr_capture.attr,
  195. NULL
  196. };
  197. ATTRIBUTE_GROUPS(pwm);
  198. static void pwm_export_release(struct device *child)
  199. {
  200. struct pwm_export *export = child_to_pwm_export(child);
  201. kfree(export);
  202. }
  203. static int pwm_export_child(struct device *parent, struct pwm_device *pwm)
  204. {
  205. struct pwm_export *export;
  206. int ret;
  207. if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags))
  208. return -EBUSY;
  209. export = kzalloc(sizeof(*export), GFP_KERNEL);
  210. if (!export) {
  211. clear_bit(PWMF_EXPORTED, &pwm->flags);
  212. return -ENOMEM;
  213. }
  214. export->pwm = pwm;
  215. mutex_init(&export->lock);
  216. export->child.release = pwm_export_release;
  217. export->child.parent = parent;
  218. export->child.devt = MKDEV(0, 0);
  219. export->child.groups = pwm_groups;
  220. dev_set_name(&export->child, "pwm%u", pwm->hwpwm);
  221. ret = device_register(&export->child);
  222. if (ret) {
  223. clear_bit(PWMF_EXPORTED, &pwm->flags);
  224. kfree(export);
  225. return ret;
  226. }
  227. return 0;
  228. }
  229. static int pwm_unexport_match(struct device *child, void *data)
  230. {
  231. return child_to_pwm_device(child) == data;
  232. }
  233. static int pwm_unexport_child(struct device *parent, struct pwm_device *pwm)
  234. {
  235. struct device *child;
  236. if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags))
  237. return -ENODEV;
  238. child = device_find_child(parent, pwm, pwm_unexport_match);
  239. if (!child)
  240. return -ENODEV;
  241. /* for device_find_child() */
  242. put_device(child);
  243. device_unregister(child);
  244. pwm_put(pwm);
  245. return 0;
  246. }
  247. static ssize_t export_store(struct device *parent,
  248. struct device_attribute *attr,
  249. const char *buf, size_t len)
  250. {
  251. struct pwm_chip *chip = dev_get_drvdata(parent);
  252. struct pwm_device *pwm;
  253. unsigned int hwpwm;
  254. int ret;
  255. ret = kstrtouint(buf, 0, &hwpwm);
  256. if (ret < 0)
  257. return ret;
  258. if (hwpwm >= chip->npwm)
  259. return -ENODEV;
  260. pwm = pwm_request_from_chip(chip, hwpwm, "sysfs");
  261. if (IS_ERR(pwm))
  262. return PTR_ERR(pwm);
  263. ret = pwm_export_child(parent, pwm);
  264. if (ret < 0)
  265. pwm_put(pwm);
  266. return ret ? : len;
  267. }
  268. static DEVICE_ATTR_WO(export);
  269. static ssize_t unexport_store(struct device *parent,
  270. struct device_attribute *attr,
  271. const char *buf, size_t len)
  272. {
  273. struct pwm_chip *chip = dev_get_drvdata(parent);
  274. unsigned int hwpwm;
  275. int ret;
  276. ret = kstrtouint(buf, 0, &hwpwm);
  277. if (ret < 0)
  278. return ret;
  279. if (hwpwm >= chip->npwm)
  280. return -ENODEV;
  281. ret = pwm_unexport_child(parent, &chip->pwms[hwpwm]);
  282. return ret ? : len;
  283. }
  284. static DEVICE_ATTR_WO(unexport);
  285. static ssize_t npwm_show(struct device *parent, struct device_attribute *attr,
  286. char *buf)
  287. {
  288. const struct pwm_chip *chip = dev_get_drvdata(parent);
  289. return sprintf(buf, "%u\n", chip->npwm);
  290. }
  291. static DEVICE_ATTR_RO(npwm);
  292. static struct attribute *pwm_chip_attrs[] = {
  293. &dev_attr_export.attr,
  294. &dev_attr_unexport.attr,
  295. &dev_attr_npwm.attr,
  296. NULL,
  297. };
  298. ATTRIBUTE_GROUPS(pwm_chip);
  299. static struct class pwm_class = {
  300. .name = "pwm",
  301. .owner = THIS_MODULE,
  302. .dev_groups = pwm_chip_groups,
  303. };
  304. static int pwmchip_sysfs_match(struct device *parent, const void *data)
  305. {
  306. return dev_get_drvdata(parent) == data;
  307. }
  308. void pwmchip_sysfs_export(struct pwm_chip *chip)
  309. {
  310. struct device *parent;
  311. /*
  312. * If device_create() fails the pwm_chip is still usable by
  313. * the kernel its just not exported.
  314. */
  315. parent = device_create(&pwm_class, chip->dev, MKDEV(0, 0), chip,
  316. "pwmchip%d", chip->base);
  317. if (IS_ERR(parent)) {
  318. dev_warn(chip->dev,
  319. "device_create failed for pwm_chip sysfs export\n");
  320. }
  321. }
  322. void pwmchip_sysfs_unexport(struct pwm_chip *chip)
  323. {
  324. struct device *parent;
  325. unsigned int i;
  326. parent = class_find_device(&pwm_class, NULL, chip,
  327. pwmchip_sysfs_match);
  328. if (!parent)
  329. return;
  330. for (i = 0; i < chip->npwm; i++) {
  331. struct pwm_device *pwm = &chip->pwms[i];
  332. if (test_bit(PWMF_EXPORTED, &pwm->flags))
  333. pwm_unexport_child(parent, pwm);
  334. }
  335. put_device(parent);
  336. device_unregister(parent);
  337. }
  338. static int __init pwm_sysfs_init(void)
  339. {
  340. return class_register(&pwm_class);
  341. }
  342. subsys_initcall(pwm_sysfs_init);