sysfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * A simple sysfs interface for the generic PWM framework
  4. *
  5. * Copyright (C) 2013 H Hartley Sweeten <hsweeten@visionengravers.com>
  6. *
  7. * Based on previous work by Lars Poeschel <poeschel@lemonage.de>
  8. */
  9. #include <linux/device.h>
  10. #include <linux/mutex.h>
  11. #include <linux/err.h>
  12. #include <linux/slab.h>
  13. #include <linux/kdev_t.h>
  14. #include <linux/pwm.h>
  15. struct pwm_export {
  16. struct device child;
  17. struct pwm_device *pwm;
  18. struct mutex lock;
  19. struct pwm_state suspend;
  20. };
  21. static struct pwm_export *child_to_pwm_export(struct device *child)
  22. {
  23. return container_of(child, struct pwm_export, child);
  24. }
  25. static struct pwm_device *child_to_pwm_device(struct device *child)
  26. {
  27. struct pwm_export *export = child_to_pwm_export(child);
  28. return export->pwm;
  29. }
  30. static ssize_t period_show(struct device *child,
  31. struct device_attribute *attr,
  32. char *buf)
  33. {
  34. const struct pwm_device *pwm = child_to_pwm_device(child);
  35. struct pwm_state state;
  36. pwm_get_state(pwm, &state);
  37. return sprintf(buf, "%llu\n", state.period);
  38. }
  39. static ssize_t period_store(struct device *child,
  40. struct device_attribute *attr,
  41. const char *buf, size_t size)
  42. {
  43. struct pwm_export *export = child_to_pwm_export(child);
  44. struct pwm_device *pwm = export->pwm;
  45. struct pwm_state state;
  46. u64 val;
  47. int ret;
  48. ret = kstrtou64(buf, 0, &val);
  49. if (ret)
  50. return ret;
  51. mutex_lock(&export->lock);
  52. pwm_get_state(pwm, &state);
  53. state.period = val;
  54. ret = pwm_apply_state(pwm, &state);
  55. mutex_unlock(&export->lock);
  56. return ret ? : size;
  57. }
  58. static ssize_t duty_cycle_show(struct device *child,
  59. struct device_attribute *attr,
  60. char *buf)
  61. {
  62. const struct pwm_device *pwm = child_to_pwm_device(child);
  63. struct pwm_state state;
  64. pwm_get_state(pwm, &state);
  65. return sprintf(buf, "%llu\n", state.duty_cycle);
  66. }
  67. static ssize_t duty_cycle_store(struct device *child,
  68. struct device_attribute *attr,
  69. const char *buf, size_t size)
  70. {
  71. struct pwm_export *export = child_to_pwm_export(child);
  72. struct pwm_device *pwm = export->pwm;
  73. struct pwm_state state;
  74. u64 val;
  75. int ret;
  76. ret = kstrtou64(buf, 0, &val);
  77. if (ret)
  78. return ret;
  79. mutex_lock(&export->lock);
  80. pwm_get_state(pwm, &state);
  81. state.duty_cycle = val;
  82. ret = pwm_apply_state(pwm, &state);
  83. mutex_unlock(&export->lock);
  84. return ret ? : size;
  85. }
  86. static ssize_t enable_show(struct device *child,
  87. struct device_attribute *attr,
  88. char *buf)
  89. {
  90. const struct pwm_device *pwm = child_to_pwm_device(child);
  91. struct pwm_state state;
  92. pwm_get_state(pwm, &state);
  93. return sprintf(buf, "%d\n", state.enabled);
  94. }
  95. static ssize_t enable_store(struct device *child,
  96. struct device_attribute *attr,
  97. const char *buf, size_t size)
  98. {
  99. struct pwm_export *export = child_to_pwm_export(child);
  100. struct pwm_device *pwm = export->pwm;
  101. struct pwm_state state;
  102. int val, ret;
  103. ret = kstrtoint(buf, 0, &val);
  104. if (ret)
  105. return ret;
  106. mutex_lock(&export->lock);
  107. pwm_get_state(pwm, &state);
  108. switch (val) {
  109. case 0:
  110. state.enabled = false;
  111. break;
  112. case 1:
  113. state.enabled = true;
  114. break;
  115. default:
  116. ret = -EINVAL;
  117. goto unlock;
  118. }
  119. ret = pwm_apply_state(pwm, &state);
  120. unlock:
  121. mutex_unlock(&export->lock);
  122. return ret ? : size;
  123. }
  124. static ssize_t polarity_show(struct device *child,
  125. struct device_attribute *attr,
  126. char *buf)
  127. {
  128. const struct pwm_device *pwm = child_to_pwm_device(child);
  129. const char *polarity = "unknown";
  130. struct pwm_state state;
  131. pwm_get_state(pwm, &state);
  132. switch (state.polarity) {
  133. case PWM_POLARITY_NORMAL:
  134. polarity = "normal";
  135. break;
  136. case PWM_POLARITY_INVERSED:
  137. polarity = "inversed";
  138. break;
  139. }
  140. return sprintf(buf, "%s\n", polarity);
  141. }
  142. static ssize_t polarity_store(struct device *child,
  143. struct device_attribute *attr,
  144. const char *buf, size_t size)
  145. {
  146. struct pwm_export *export = child_to_pwm_export(child);
  147. struct pwm_device *pwm = export->pwm;
  148. enum pwm_polarity polarity;
  149. struct pwm_state state;
  150. int ret;
  151. if (sysfs_streq(buf, "normal"))
  152. polarity = PWM_POLARITY_NORMAL;
  153. else if (sysfs_streq(buf, "inversed"))
  154. polarity = PWM_POLARITY_INVERSED;
  155. else
  156. return -EINVAL;
  157. mutex_lock(&export->lock);
  158. pwm_get_state(pwm, &state);
  159. state.polarity = polarity;
  160. ret = pwm_apply_state(pwm, &state);
  161. mutex_unlock(&export->lock);
  162. return ret ? : size;
  163. }
  164. static ssize_t capture_show(struct device *child,
  165. struct device_attribute *attr,
  166. char *buf)
  167. {
  168. struct pwm_device *pwm = child_to_pwm_device(child);
  169. struct pwm_capture result;
  170. int ret;
  171. ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ));
  172. if (ret)
  173. return ret;
  174. return sprintf(buf, "%u %u\n", result.period, result.duty_cycle);
  175. }
  176. static ssize_t output_type_show(struct device *child,
  177. struct device_attribute *attr,
  178. char *buf)
  179. {
  180. const struct pwm_device *pwm = child_to_pwm_device(child);
  181. const char *output_type = "unknown";
  182. struct pwm_state state;
  183. pwm_get_state(pwm, &state);
  184. switch (state.output_type) {
  185. case PWM_OUTPUT_FIXED:
  186. output_type = "fixed";
  187. break;
  188. case PWM_OUTPUT_MODULATED:
  189. output_type = "modulated";
  190. break;
  191. default:
  192. break;
  193. }
  194. return snprintf(buf, PAGE_SIZE, "%s\n", output_type);
  195. }
  196. static DEVICE_ATTR_RW(period);
  197. static DEVICE_ATTR_RW(duty_cycle);
  198. static DEVICE_ATTR_RW(enable);
  199. static DEVICE_ATTR_RW(polarity);
  200. static DEVICE_ATTR_RO(capture);
  201. static DEVICE_ATTR_RO(output_type);
  202. static struct attribute *pwm_attrs[] = {
  203. &dev_attr_period.attr,
  204. &dev_attr_duty_cycle.attr,
  205. &dev_attr_enable.attr,
  206. &dev_attr_polarity.attr,
  207. &dev_attr_capture.attr,
  208. &dev_attr_output_type.attr,
  209. NULL
  210. };
  211. ATTRIBUTE_GROUPS(pwm);
  212. static void pwm_export_release(struct device *child)
  213. {
  214. struct pwm_export *export = child_to_pwm_export(child);
  215. kfree(export);
  216. }
  217. static int pwm_export_child(struct device *parent, struct pwm_device *pwm)
  218. {
  219. struct pwm_export *export;
  220. char *pwm_prop[2];
  221. int ret;
  222. if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags))
  223. return -EBUSY;
  224. export = kzalloc(sizeof(*export), GFP_KERNEL);
  225. if (!export) {
  226. clear_bit(PWMF_EXPORTED, &pwm->flags);
  227. return -ENOMEM;
  228. }
  229. export->pwm = pwm;
  230. mutex_init(&export->lock);
  231. export->child.release = pwm_export_release;
  232. export->child.parent = parent;
  233. export->child.devt = MKDEV(0, 0);
  234. export->child.groups = pwm_groups;
  235. dev_set_name(&export->child, "pwm%u", pwm->hwpwm);
  236. ret = device_register(&export->child);
  237. if (ret) {
  238. clear_bit(PWMF_EXPORTED, &pwm->flags);
  239. put_device(&export->child);
  240. export = NULL;
  241. return ret;
  242. }
  243. pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm);
  244. pwm_prop[1] = NULL;
  245. kobject_uevent_env(&parent->kobj, KOBJ_CHANGE, pwm_prop);
  246. kfree(pwm_prop[0]);
  247. return 0;
  248. }
  249. static int pwm_unexport_match(struct device *child, void *data)
  250. {
  251. return child_to_pwm_device(child) == data;
  252. }
  253. static int pwm_unexport_child(struct device *parent, struct pwm_device *pwm)
  254. {
  255. struct device *child;
  256. char *pwm_prop[2];
  257. if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags))
  258. return -ENODEV;
  259. child = device_find_child(parent, pwm, pwm_unexport_match);
  260. if (!child)
  261. return -ENODEV;
  262. pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm);
  263. pwm_prop[1] = NULL;
  264. kobject_uevent_env(&parent->kobj, KOBJ_CHANGE, pwm_prop);
  265. kfree(pwm_prop[0]);
  266. /* for device_find_child() */
  267. put_device(child);
  268. device_unregister(child);
  269. pwm_put(pwm);
  270. return 0;
  271. }
  272. static ssize_t export_store(struct device *parent,
  273. struct device_attribute *attr,
  274. const char *buf, size_t len)
  275. {
  276. struct pwm_chip *chip = dev_get_drvdata(parent);
  277. struct pwm_device *pwm;
  278. unsigned int hwpwm;
  279. int ret;
  280. ret = kstrtouint(buf, 0, &hwpwm);
  281. if (ret < 0)
  282. return ret;
  283. if (hwpwm >= chip->npwm)
  284. return -ENODEV;
  285. pwm = pwm_request_from_chip(chip, hwpwm, "sysfs");
  286. if (IS_ERR(pwm))
  287. return PTR_ERR(pwm);
  288. ret = pwm_export_child(parent, pwm);
  289. if (ret < 0)
  290. pwm_put(pwm);
  291. return ret ? : len;
  292. }
  293. static DEVICE_ATTR_WO(export);
  294. static ssize_t unexport_store(struct device *parent,
  295. struct device_attribute *attr,
  296. const char *buf, size_t len)
  297. {
  298. struct pwm_chip *chip = dev_get_drvdata(parent);
  299. unsigned int hwpwm;
  300. int ret;
  301. ret = kstrtouint(buf, 0, &hwpwm);
  302. if (ret < 0)
  303. return ret;
  304. if (hwpwm >= chip->npwm)
  305. return -ENODEV;
  306. ret = pwm_unexport_child(parent, &chip->pwms[hwpwm]);
  307. return ret ? : len;
  308. }
  309. static DEVICE_ATTR_WO(unexport);
  310. static ssize_t npwm_show(struct device *parent, struct device_attribute *attr,
  311. char *buf)
  312. {
  313. const struct pwm_chip *chip = dev_get_drvdata(parent);
  314. return sprintf(buf, "%u\n", chip->npwm);
  315. }
  316. static DEVICE_ATTR_RO(npwm);
  317. static struct attribute *pwm_chip_attrs[] = {
  318. &dev_attr_export.attr,
  319. &dev_attr_unexport.attr,
  320. &dev_attr_npwm.attr,
  321. NULL,
  322. };
  323. ATTRIBUTE_GROUPS(pwm_chip);
  324. /* takes export->lock on success */
  325. static struct pwm_export *pwm_class_get_state(struct device *parent,
  326. struct pwm_device *pwm,
  327. struct pwm_state *state)
  328. {
  329. struct device *child;
  330. struct pwm_export *export;
  331. if (!test_bit(PWMF_EXPORTED, &pwm->flags))
  332. return NULL;
  333. child = device_find_child(parent, pwm, pwm_unexport_match);
  334. if (!child)
  335. return NULL;
  336. export = child_to_pwm_export(child);
  337. put_device(child); /* for device_find_child() */
  338. mutex_lock(&export->lock);
  339. pwm_get_state(pwm, state);
  340. return export;
  341. }
  342. static int pwm_class_apply_state(struct pwm_export *export,
  343. struct pwm_device *pwm,
  344. struct pwm_state *state)
  345. {
  346. int ret = pwm_apply_state(pwm, state);
  347. /* release lock taken in pwm_class_get_state */
  348. mutex_unlock(&export->lock);
  349. return ret;
  350. }
  351. static int pwm_class_resume_npwm(struct device *parent, unsigned int npwm)
  352. {
  353. struct pwm_chip *chip = dev_get_drvdata(parent);
  354. unsigned int i;
  355. int ret = 0;
  356. for (i = 0; i < npwm; i++) {
  357. struct pwm_device *pwm = &chip->pwms[i];
  358. struct pwm_state state;
  359. struct pwm_export *export;
  360. export = pwm_class_get_state(parent, pwm, &state);
  361. if (!export)
  362. continue;
  363. state.enabled = export->suspend.enabled;
  364. ret = pwm_class_apply_state(export, pwm, &state);
  365. if (ret < 0)
  366. break;
  367. }
  368. return ret;
  369. }
  370. static int __maybe_unused pwm_class_suspend(struct device *parent)
  371. {
  372. struct pwm_chip *chip = dev_get_drvdata(parent);
  373. unsigned int i;
  374. int ret = 0;
  375. for (i = 0; i < chip->npwm; i++) {
  376. struct pwm_device *pwm = &chip->pwms[i];
  377. struct pwm_state state;
  378. struct pwm_export *export;
  379. export = pwm_class_get_state(parent, pwm, &state);
  380. if (!export)
  381. continue;
  382. export->suspend = state;
  383. state.enabled = false;
  384. ret = pwm_class_apply_state(export, pwm, &state);
  385. if (ret < 0) {
  386. /*
  387. * roll back the PWM devices that were disabled by
  388. * this suspend function.
  389. */
  390. pwm_class_resume_npwm(parent, i);
  391. break;
  392. }
  393. }
  394. return ret;
  395. }
  396. static int __maybe_unused pwm_class_resume(struct device *parent)
  397. {
  398. struct pwm_chip *chip = dev_get_drvdata(parent);
  399. return pwm_class_resume_npwm(parent, chip->npwm);
  400. }
  401. static SIMPLE_DEV_PM_OPS(pwm_class_pm_ops, pwm_class_suspend, pwm_class_resume);
  402. static struct class pwm_class = {
  403. .name = "pwm",
  404. .owner = THIS_MODULE,
  405. .dev_groups = pwm_chip_groups,
  406. .pm = &pwm_class_pm_ops,
  407. };
  408. static int pwmchip_sysfs_match(struct device *parent, const void *data)
  409. {
  410. return dev_get_drvdata(parent) == data;
  411. }
  412. void pwmchip_sysfs_export(struct pwm_chip *chip)
  413. {
  414. struct device *parent;
  415. /*
  416. * If device_create() fails the pwm_chip is still usable by
  417. * the kernel it's just not exported.
  418. */
  419. parent = device_create(&pwm_class, chip->dev, MKDEV(0, 0), chip,
  420. "pwmchip%d", chip->base);
  421. if (IS_ERR(parent)) {
  422. dev_warn(chip->dev,
  423. "device_create failed for pwm_chip sysfs export\n");
  424. }
  425. }
  426. void pwmchip_sysfs_unexport(struct pwm_chip *chip)
  427. {
  428. struct device *parent;
  429. unsigned int i;
  430. parent = class_find_device(&pwm_class, NULL, chip,
  431. pwmchip_sysfs_match);
  432. if (!parent)
  433. return;
  434. for (i = 0; i < chip->npwm; i++) {
  435. struct pwm_device *pwm = &chip->pwms[i];
  436. if (test_bit(PWMF_EXPORTED, &pwm->flags))
  437. pwm_unexport_child(parent, pwm);
  438. }
  439. put_device(parent);
  440. device_unregister(parent);
  441. }
  442. static int __init pwm_sysfs_init(void)
  443. {
  444. return class_register(&pwm_class);
  445. }
  446. subsys_initcall(pwm_sysfs_init);