sysfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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 <[email protected]>
  6. *
  7. * Based on previous work by Lars Poeschel <[email protected]>
  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 sysfs_emit(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 sysfs_emit(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 sysfs_emit(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 sysfs_emit(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 sysfs_emit(buf, "%u %u\n", result.period, result.duty_cycle);
  175. }
  176. static DEVICE_ATTR_RW(period);
  177. static DEVICE_ATTR_RW(duty_cycle);
  178. static DEVICE_ATTR_RW(enable);
  179. static DEVICE_ATTR_RW(polarity);
  180. static DEVICE_ATTR_RO(capture);
  181. static struct attribute *pwm_attrs[] = {
  182. &dev_attr_period.attr,
  183. &dev_attr_duty_cycle.attr,
  184. &dev_attr_enable.attr,
  185. &dev_attr_polarity.attr,
  186. &dev_attr_capture.attr,
  187. NULL
  188. };
  189. ATTRIBUTE_GROUPS(pwm);
  190. static void pwm_export_release(struct device *child)
  191. {
  192. struct pwm_export *export = child_to_pwm_export(child);
  193. kfree(export);
  194. }
  195. static int pwm_export_child(struct device *parent, struct pwm_device *pwm)
  196. {
  197. struct pwm_export *export;
  198. char *pwm_prop[2];
  199. int ret;
  200. if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags))
  201. return -EBUSY;
  202. export = kzalloc(sizeof(*export), GFP_KERNEL);
  203. if (!export) {
  204. clear_bit(PWMF_EXPORTED, &pwm->flags);
  205. return -ENOMEM;
  206. }
  207. export->pwm = pwm;
  208. mutex_init(&export->lock);
  209. export->child.release = pwm_export_release;
  210. export->child.parent = parent;
  211. export->child.devt = MKDEV(0, 0);
  212. export->child.groups = pwm_groups;
  213. dev_set_name(&export->child, "pwm%u", pwm->hwpwm);
  214. ret = device_register(&export->child);
  215. if (ret) {
  216. clear_bit(PWMF_EXPORTED, &pwm->flags);
  217. put_device(&export->child);
  218. export = NULL;
  219. return ret;
  220. }
  221. pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm);
  222. pwm_prop[1] = NULL;
  223. kobject_uevent_env(&parent->kobj, KOBJ_CHANGE, pwm_prop);
  224. kfree(pwm_prop[0]);
  225. return 0;
  226. }
  227. static int pwm_unexport_match(struct device *child, void *data)
  228. {
  229. return child_to_pwm_device(child) == data;
  230. }
  231. static int pwm_unexport_child(struct device *parent, struct pwm_device *pwm)
  232. {
  233. struct device *child;
  234. char *pwm_prop[2];
  235. if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags))
  236. return -ENODEV;
  237. child = device_find_child(parent, pwm, pwm_unexport_match);
  238. if (!child)
  239. return -ENODEV;
  240. pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm);
  241. pwm_prop[1] = NULL;
  242. kobject_uevent_env(&parent->kobj, KOBJ_CHANGE, pwm_prop);
  243. kfree(pwm_prop[0]);
  244. /* for device_find_child() */
  245. put_device(child);
  246. device_unregister(child);
  247. pwm_put(pwm);
  248. return 0;
  249. }
  250. static ssize_t export_store(struct device *parent,
  251. struct device_attribute *attr,
  252. const char *buf, size_t len)
  253. {
  254. struct pwm_chip *chip = dev_get_drvdata(parent);
  255. struct pwm_device *pwm;
  256. unsigned int hwpwm;
  257. int ret;
  258. ret = kstrtouint(buf, 0, &hwpwm);
  259. if (ret < 0)
  260. return ret;
  261. if (hwpwm >= chip->npwm)
  262. return -ENODEV;
  263. pwm = pwm_request_from_chip(chip, hwpwm, "sysfs");
  264. if (IS_ERR(pwm))
  265. return PTR_ERR(pwm);
  266. ret = pwm_export_child(parent, pwm);
  267. if (ret < 0)
  268. pwm_put(pwm);
  269. return ret ? : len;
  270. }
  271. static DEVICE_ATTR_WO(export);
  272. static ssize_t unexport_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. unsigned int hwpwm;
  278. int ret;
  279. ret = kstrtouint(buf, 0, &hwpwm);
  280. if (ret < 0)
  281. return ret;
  282. if (hwpwm >= chip->npwm)
  283. return -ENODEV;
  284. ret = pwm_unexport_child(parent, &chip->pwms[hwpwm]);
  285. return ret ? : len;
  286. }
  287. static DEVICE_ATTR_WO(unexport);
  288. static ssize_t npwm_show(struct device *parent, struct device_attribute *attr,
  289. char *buf)
  290. {
  291. const struct pwm_chip *chip = dev_get_drvdata(parent);
  292. return sysfs_emit(buf, "%u\n", chip->npwm);
  293. }
  294. static DEVICE_ATTR_RO(npwm);
  295. static struct attribute *pwm_chip_attrs[] = {
  296. &dev_attr_export.attr,
  297. &dev_attr_unexport.attr,
  298. &dev_attr_npwm.attr,
  299. NULL,
  300. };
  301. ATTRIBUTE_GROUPS(pwm_chip);
  302. /* takes export->lock on success */
  303. static struct pwm_export *pwm_class_get_state(struct device *parent,
  304. struct pwm_device *pwm,
  305. struct pwm_state *state)
  306. {
  307. struct device *child;
  308. struct pwm_export *export;
  309. if (!test_bit(PWMF_EXPORTED, &pwm->flags))
  310. return NULL;
  311. child = device_find_child(parent, pwm, pwm_unexport_match);
  312. if (!child)
  313. return NULL;
  314. export = child_to_pwm_export(child);
  315. put_device(child); /* for device_find_child() */
  316. mutex_lock(&export->lock);
  317. pwm_get_state(pwm, state);
  318. return export;
  319. }
  320. static int pwm_class_apply_state(struct pwm_export *export,
  321. struct pwm_device *pwm,
  322. struct pwm_state *state)
  323. {
  324. int ret = pwm_apply_state(pwm, state);
  325. /* release lock taken in pwm_class_get_state */
  326. mutex_unlock(&export->lock);
  327. return ret;
  328. }
  329. static int pwm_class_resume_npwm(struct device *parent, unsigned int npwm)
  330. {
  331. struct pwm_chip *chip = dev_get_drvdata(parent);
  332. unsigned int i;
  333. int ret = 0;
  334. for (i = 0; i < npwm; i++) {
  335. struct pwm_device *pwm = &chip->pwms[i];
  336. struct pwm_state state;
  337. struct pwm_export *export;
  338. export = pwm_class_get_state(parent, pwm, &state);
  339. if (!export)
  340. continue;
  341. /* If pwmchip was not enabled before suspend, do nothing. */
  342. if (!export->suspend.enabled) {
  343. /* release lock taken in pwm_class_get_state */
  344. mutex_unlock(&export->lock);
  345. continue;
  346. }
  347. state.enabled = export->suspend.enabled;
  348. ret = pwm_class_apply_state(export, pwm, &state);
  349. if (ret < 0)
  350. break;
  351. }
  352. return ret;
  353. }
  354. static int pwm_class_suspend(struct device *parent)
  355. {
  356. struct pwm_chip *chip = dev_get_drvdata(parent);
  357. unsigned int i;
  358. int ret = 0;
  359. for (i = 0; i < chip->npwm; i++) {
  360. struct pwm_device *pwm = &chip->pwms[i];
  361. struct pwm_state state;
  362. struct pwm_export *export;
  363. export = pwm_class_get_state(parent, pwm, &state);
  364. if (!export)
  365. continue;
  366. /*
  367. * If pwmchip was not enabled before suspend, save
  368. * state for resume time and do nothing else.
  369. */
  370. export->suspend = state;
  371. if (!state.enabled) {
  372. /* release lock taken in pwm_class_get_state */
  373. mutex_unlock(&export->lock);
  374. continue;
  375. }
  376. state.enabled = false;
  377. ret = pwm_class_apply_state(export, pwm, &state);
  378. if (ret < 0) {
  379. /*
  380. * roll back the PWM devices that were disabled by
  381. * this suspend function.
  382. */
  383. pwm_class_resume_npwm(parent, i);
  384. break;
  385. }
  386. }
  387. return ret;
  388. }
  389. static int pwm_class_resume(struct device *parent)
  390. {
  391. struct pwm_chip *chip = dev_get_drvdata(parent);
  392. return pwm_class_resume_npwm(parent, chip->npwm);
  393. }
  394. static DEFINE_SIMPLE_DEV_PM_OPS(pwm_class_pm_ops, pwm_class_suspend, pwm_class_resume);
  395. static struct class pwm_class = {
  396. .name = "pwm",
  397. .owner = THIS_MODULE,
  398. .dev_groups = pwm_chip_groups,
  399. .pm = pm_sleep_ptr(&pwm_class_pm_ops),
  400. };
  401. static int pwmchip_sysfs_match(struct device *parent, const void *data)
  402. {
  403. return dev_get_drvdata(parent) == data;
  404. }
  405. void pwmchip_sysfs_export(struct pwm_chip *chip)
  406. {
  407. struct device *parent;
  408. /*
  409. * If device_create() fails the pwm_chip is still usable by
  410. * the kernel it's just not exported.
  411. */
  412. parent = device_create(&pwm_class, chip->dev, MKDEV(0, 0), chip,
  413. "pwmchip%d", chip->base);
  414. if (IS_ERR(parent)) {
  415. dev_warn(chip->dev,
  416. "device_create failed for pwm_chip sysfs export\n");
  417. }
  418. }
  419. void pwmchip_sysfs_unexport(struct pwm_chip *chip)
  420. {
  421. struct device *parent;
  422. unsigned int i;
  423. parent = class_find_device(&pwm_class, NULL, chip,
  424. pwmchip_sysfs_match);
  425. if (!parent)
  426. return;
  427. for (i = 0; i < chip->npwm; i++) {
  428. struct pwm_device *pwm = &chip->pwms[i];
  429. if (test_bit(PWMF_EXPORTED, &pwm->flags))
  430. pwm_unexport_child(parent, pwm);
  431. }
  432. put_device(parent);
  433. device_unregister(parent);
  434. }
  435. static int __init pwm_sysfs_init(void)
  436. {
  437. return class_register(&pwm_class);
  438. }
  439. subsys_initcall(pwm_sysfs_init);