sysfs.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. put_device(&export->child);
  225. export = NULL;
  226. return ret;
  227. }
  228. return 0;
  229. }
  230. static int pwm_unexport_match(struct device *child, void *data)
  231. {
  232. return child_to_pwm_device(child) == data;
  233. }
  234. static int pwm_unexport_child(struct device *parent, struct pwm_device *pwm)
  235. {
  236. struct device *child;
  237. if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags))
  238. return -ENODEV;
  239. child = device_find_child(parent, pwm, pwm_unexport_match);
  240. if (!child)
  241. return -ENODEV;
  242. /* for device_find_child() */
  243. put_device(child);
  244. device_unregister(child);
  245. pwm_put(pwm);
  246. return 0;
  247. }
  248. static ssize_t export_store(struct device *parent,
  249. struct device_attribute *attr,
  250. const char *buf, size_t len)
  251. {
  252. struct pwm_chip *chip = dev_get_drvdata(parent);
  253. struct pwm_device *pwm;
  254. unsigned int hwpwm;
  255. int ret;
  256. ret = kstrtouint(buf, 0, &hwpwm);
  257. if (ret < 0)
  258. return ret;
  259. if (hwpwm >= chip->npwm)
  260. return -ENODEV;
  261. pwm = pwm_request_from_chip(chip, hwpwm, "sysfs");
  262. if (IS_ERR(pwm))
  263. return PTR_ERR(pwm);
  264. ret = pwm_export_child(parent, pwm);
  265. if (ret < 0)
  266. pwm_put(pwm);
  267. return ret ? : len;
  268. }
  269. static DEVICE_ATTR_WO(export);
  270. static ssize_t unexport_store(struct device *parent,
  271. struct device_attribute *attr,
  272. const char *buf, size_t len)
  273. {
  274. struct pwm_chip *chip = dev_get_drvdata(parent);
  275. unsigned int hwpwm;
  276. int ret;
  277. ret = kstrtouint(buf, 0, &hwpwm);
  278. if (ret < 0)
  279. return ret;
  280. if (hwpwm >= chip->npwm)
  281. return -ENODEV;
  282. ret = pwm_unexport_child(parent, &chip->pwms[hwpwm]);
  283. return ret ? : len;
  284. }
  285. static DEVICE_ATTR_WO(unexport);
  286. static ssize_t npwm_show(struct device *parent, struct device_attribute *attr,
  287. char *buf)
  288. {
  289. const struct pwm_chip *chip = dev_get_drvdata(parent);
  290. return sprintf(buf, "%u\n", chip->npwm);
  291. }
  292. static DEVICE_ATTR_RO(npwm);
  293. static struct attribute *pwm_chip_attrs[] = {
  294. &dev_attr_export.attr,
  295. &dev_attr_unexport.attr,
  296. &dev_attr_npwm.attr,
  297. NULL,
  298. };
  299. ATTRIBUTE_GROUPS(pwm_chip);
  300. static struct class pwm_class = {
  301. .name = "pwm",
  302. .owner = THIS_MODULE,
  303. .dev_groups = pwm_chip_groups,
  304. };
  305. static int pwmchip_sysfs_match(struct device *parent, const void *data)
  306. {
  307. return dev_get_drvdata(parent) == data;
  308. }
  309. void pwmchip_sysfs_export(struct pwm_chip *chip)
  310. {
  311. struct device *parent;
  312. /*
  313. * If device_create() fails the pwm_chip is still usable by
  314. * the kernel its just not exported.
  315. */
  316. parent = device_create(&pwm_class, chip->dev, MKDEV(0, 0), chip,
  317. "pwmchip%d", chip->base);
  318. if (IS_ERR(parent)) {
  319. dev_warn(chip->dev,
  320. "device_create failed for pwm_chip sysfs export\n");
  321. }
  322. }
  323. void pwmchip_sysfs_unexport(struct pwm_chip *chip)
  324. {
  325. struct device *parent;
  326. unsigned int i;
  327. parent = class_find_device(&pwm_class, NULL, chip,
  328. pwmchip_sysfs_match);
  329. if (!parent)
  330. return;
  331. for (i = 0; i < chip->npwm; i++) {
  332. struct pwm_device *pwm = &chip->pwms[i];
  333. if (test_bit(PWMF_EXPORTED, &pwm->flags))
  334. pwm_unexport_child(parent, pwm);
  335. }
  336. put_device(parent);
  337. device_unregister(parent);
  338. }
  339. static int __init pwm_sysfs_init(void)
  340. {
  341. return class_register(&pwm_class);
  342. }
  343. subsys_initcall(pwm_sysfs_init);