sysfs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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. #ifdef CONFIG_PWM_ROCKCHIP_ONESHOT
  87. static ssize_t oneshot_count_show(struct device *child,
  88. struct device_attribute *attr,
  89. char *buf)
  90. {
  91. const struct pwm_device *pwm = child_to_pwm_device(child);
  92. struct pwm_state state;
  93. pwm_get_state(pwm, &state);
  94. return sprintf(buf, "%llu\n", state.oneshot_count);
  95. }
  96. static ssize_t oneshot_count_store(struct device *child,
  97. struct device_attribute *attr,
  98. const char *buf, size_t size)
  99. {
  100. struct pwm_export *export = child_to_pwm_export(child);
  101. struct pwm_device *pwm = export->pwm;
  102. struct pwm_state state;
  103. unsigned int val;
  104. int ret;
  105. ret = kstrtouint(buf, 0, &val);
  106. if (ret)
  107. return ret;
  108. mutex_lock(&export->lock);
  109. pwm_get_state(pwm, &state);
  110. state.oneshot_count = val;
  111. ret = pwm_apply_state(pwm, &state);
  112. mutex_unlock(&export->lock);
  113. return ret ? : size;
  114. }
  115. static ssize_t oneshot_repeat_show(struct device *child,
  116. struct device_attribute *attr,
  117. char *buf)
  118. {
  119. const struct pwm_device *pwm = child_to_pwm_device(child);
  120. struct pwm_state state;
  121. pwm_get_state(pwm, &state);
  122. return sprintf(buf, "%u\n", state.oneshot_repeat);
  123. }
  124. static ssize_t oneshot_repeat_store(struct device *child,
  125. struct device_attribute *attr,
  126. const char *buf, size_t size)
  127. {
  128. struct pwm_export *export = child_to_pwm_export(child);
  129. struct pwm_device *pwm = export->pwm;
  130. struct pwm_state state;
  131. unsigned int val;
  132. int ret;
  133. ret = kstrtouint(buf, 0, &val);
  134. if (ret)
  135. return ret;
  136. mutex_lock(&export->lock);
  137. pwm_get_state(pwm, &state);
  138. state.oneshot_repeat = val;
  139. ret = pwm_apply_state(pwm, &state);
  140. mutex_unlock(&export->lock);
  141. return ret ? : size;
  142. }
  143. static ssize_t duty_offset_show(struct device *child,
  144. struct device_attribute *attr,
  145. char *buf)
  146. {
  147. const struct pwm_device *pwm = child_to_pwm_device(child);
  148. struct pwm_state state;
  149. pwm_get_state(pwm, &state);
  150. return sprintf(buf, "%llu\n", state.duty_offset);
  151. }
  152. static ssize_t duty_offset_store(struct device *child,
  153. struct device_attribute *attr,
  154. const char *buf, size_t size)
  155. {
  156. struct pwm_export *export = child_to_pwm_export(child);
  157. struct pwm_device *pwm = export->pwm;
  158. struct pwm_state state;
  159. u64 val;
  160. int ret;
  161. ret = kstrtou64(buf, 0, &val);
  162. if (ret)
  163. return ret;
  164. mutex_lock(&export->lock);
  165. pwm_get_state(pwm, &state);
  166. state.duty_offset = val;
  167. ret = pwm_apply_state(pwm, &state);
  168. mutex_unlock(&export->lock);
  169. return ret ? : size;
  170. }
  171. #endif
  172. static ssize_t enable_show(struct device *child,
  173. struct device_attribute *attr,
  174. char *buf)
  175. {
  176. const struct pwm_device *pwm = child_to_pwm_device(child);
  177. struct pwm_state state;
  178. pwm_get_state(pwm, &state);
  179. return sprintf(buf, "%d\n", state.enabled);
  180. }
  181. static ssize_t enable_store(struct device *child,
  182. struct device_attribute *attr,
  183. const char *buf, size_t size)
  184. {
  185. struct pwm_export *export = child_to_pwm_export(child);
  186. struct pwm_device *pwm = export->pwm;
  187. struct pwm_state state;
  188. int val, ret;
  189. ret = kstrtoint(buf, 0, &val);
  190. if (ret)
  191. return ret;
  192. mutex_lock(&export->lock);
  193. pwm_get_state(pwm, &state);
  194. switch (val) {
  195. case 0:
  196. state.enabled = false;
  197. break;
  198. case 1:
  199. state.enabled = true;
  200. break;
  201. default:
  202. ret = -EINVAL;
  203. goto unlock;
  204. }
  205. ret = pwm_apply_state(pwm, &state);
  206. unlock:
  207. mutex_unlock(&export->lock);
  208. return ret ? : size;
  209. }
  210. static ssize_t polarity_show(struct device *child,
  211. struct device_attribute *attr,
  212. char *buf)
  213. {
  214. const struct pwm_device *pwm = child_to_pwm_device(child);
  215. const char *polarity = "unknown";
  216. struct pwm_state state;
  217. pwm_get_state(pwm, &state);
  218. switch (state.polarity) {
  219. case PWM_POLARITY_NORMAL:
  220. polarity = "normal";
  221. break;
  222. case PWM_POLARITY_INVERSED:
  223. polarity = "inversed";
  224. break;
  225. }
  226. return sprintf(buf, "%s\n", polarity);
  227. }
  228. static ssize_t polarity_store(struct device *child,
  229. struct device_attribute *attr,
  230. const char *buf, size_t size)
  231. {
  232. struct pwm_export *export = child_to_pwm_export(child);
  233. struct pwm_device *pwm = export->pwm;
  234. enum pwm_polarity polarity;
  235. struct pwm_state state;
  236. int ret;
  237. if (sysfs_streq(buf, "normal"))
  238. polarity = PWM_POLARITY_NORMAL;
  239. else if (sysfs_streq(buf, "inversed"))
  240. polarity = PWM_POLARITY_INVERSED;
  241. else
  242. return -EINVAL;
  243. mutex_lock(&export->lock);
  244. pwm_get_state(pwm, &state);
  245. state.polarity = polarity;
  246. ret = pwm_apply_state(pwm, &state);
  247. mutex_unlock(&export->lock);
  248. return ret ? : size;
  249. }
  250. static ssize_t capture_show(struct device *child,
  251. struct device_attribute *attr,
  252. char *buf)
  253. {
  254. struct pwm_device *pwm = child_to_pwm_device(child);
  255. struct pwm_capture result;
  256. int ret;
  257. //ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ));
  258. ret = pwm_capture(pwm, &result, 5000);
  259. if (ret)
  260. return ret;
  261. return sprintf(buf, "%u %u\n", result.period, result.duty_cycle);
  262. }
  263. static ssize_t output_type_show(struct device *child,
  264. struct device_attribute *attr,
  265. char *buf)
  266. {
  267. const struct pwm_device *pwm = child_to_pwm_device(child);
  268. const char *output_type = "unknown";
  269. struct pwm_state state;
  270. pwm_get_state(pwm, &state);
  271. switch (state.output_type) {
  272. case PWM_OUTPUT_FIXED:
  273. output_type = "fixed";
  274. break;
  275. case PWM_OUTPUT_MODULATED:
  276. output_type = "modulated";
  277. break;
  278. default:
  279. break;
  280. }
  281. return snprintf(buf, PAGE_SIZE, "%s\n", output_type);
  282. }
  283. static DEVICE_ATTR_RW(period);
  284. static DEVICE_ATTR_RW(duty_cycle);
  285. #ifdef CONFIG_PWM_ROCKCHIP_ONESHOT
  286. static DEVICE_ATTR_RW(oneshot_count);
  287. static DEVICE_ATTR_RW(oneshot_repeat);
  288. static DEVICE_ATTR_RW(duty_offset);
  289. #endif
  290. static DEVICE_ATTR_RW(enable);
  291. static DEVICE_ATTR_RW(polarity);
  292. static DEVICE_ATTR_RO(capture);
  293. static DEVICE_ATTR_RO(output_type);
  294. static struct attribute *pwm_attrs[] = {
  295. &dev_attr_period.attr,
  296. &dev_attr_duty_cycle.attr,
  297. #ifdef CONFIG_PWM_ROCKCHIP_ONESHOT
  298. &dev_attr_oneshot_count.attr,
  299. &dev_attr_oneshot_repeat.attr,
  300. &dev_attr_duty_offset.attr,
  301. #endif
  302. &dev_attr_enable.attr,
  303. &dev_attr_polarity.attr,
  304. &dev_attr_capture.attr,
  305. &dev_attr_output_type.attr,
  306. NULL
  307. };
  308. ATTRIBUTE_GROUPS(pwm);
  309. static void pwm_export_release(struct device *child)
  310. {
  311. struct pwm_export *export = child_to_pwm_export(child);
  312. kfree(export);
  313. }
  314. static int pwm_export_child(struct device *parent, struct pwm_device *pwm)
  315. {
  316. struct pwm_export *export;
  317. char *pwm_prop[2];
  318. int ret;
  319. if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags))
  320. return -EBUSY;
  321. export = kzalloc(sizeof(*export), GFP_KERNEL);
  322. if (!export) {
  323. clear_bit(PWMF_EXPORTED, &pwm->flags);
  324. return -ENOMEM;
  325. }
  326. export->pwm = pwm;
  327. mutex_init(&export->lock);
  328. export->child.release = pwm_export_release;
  329. export->child.parent = parent;
  330. export->child.devt = MKDEV(0, 0);
  331. export->child.groups = pwm_groups;
  332. dev_set_name(&export->child, "pwm%u", pwm->hwpwm);
  333. ret = device_register(&export->child);
  334. if (ret) {
  335. clear_bit(PWMF_EXPORTED, &pwm->flags);
  336. put_device(&export->child);
  337. export = NULL;
  338. return ret;
  339. }
  340. pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm);
  341. pwm_prop[1] = NULL;
  342. kobject_uevent_env(&parent->kobj, KOBJ_CHANGE, pwm_prop);
  343. kfree(pwm_prop[0]);
  344. return 0;
  345. }
  346. static int pwm_unexport_match(struct device *child, void *data)
  347. {
  348. return child_to_pwm_device(child) == data;
  349. }
  350. static int pwm_unexport_child(struct device *parent, struct pwm_device *pwm)
  351. {
  352. struct device *child;
  353. char *pwm_prop[2];
  354. if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags))
  355. return -ENODEV;
  356. child = device_find_child(parent, pwm, pwm_unexport_match);
  357. if (!child)
  358. return -ENODEV;
  359. pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm);
  360. pwm_prop[1] = NULL;
  361. kobject_uevent_env(&parent->kobj, KOBJ_CHANGE, pwm_prop);
  362. kfree(pwm_prop[0]);
  363. /* for device_find_child() */
  364. put_device(child);
  365. device_unregister(child);
  366. pwm_put(pwm);
  367. return 0;
  368. }
  369. static ssize_t export_store(struct device *parent,
  370. struct device_attribute *attr,
  371. const char *buf, size_t len)
  372. {
  373. struct pwm_chip *chip = dev_get_drvdata(parent);
  374. struct pwm_device *pwm;
  375. unsigned int hwpwm;
  376. int ret;
  377. ret = kstrtouint(buf, 0, &hwpwm);
  378. if (ret < 0)
  379. return ret;
  380. if (hwpwm >= chip->npwm)
  381. return -ENODEV;
  382. pwm = pwm_request_from_chip(chip, hwpwm, "sysfs");
  383. if (IS_ERR(pwm))
  384. return PTR_ERR(pwm);
  385. ret = pwm_export_child(parent, pwm);
  386. if (ret < 0)
  387. pwm_put(pwm);
  388. return ret ? : len;
  389. }
  390. static DEVICE_ATTR_WO(export);
  391. static ssize_t unexport_store(struct device *parent,
  392. struct device_attribute *attr,
  393. const char *buf, size_t len)
  394. {
  395. struct pwm_chip *chip = dev_get_drvdata(parent);
  396. unsigned int hwpwm;
  397. int ret;
  398. ret = kstrtouint(buf, 0, &hwpwm);
  399. if (ret < 0)
  400. return ret;
  401. if (hwpwm >= chip->npwm)
  402. return -ENODEV;
  403. ret = pwm_unexport_child(parent, &chip->pwms[hwpwm]);
  404. return ret ? : len;
  405. }
  406. static DEVICE_ATTR_WO(unexport);
  407. static ssize_t npwm_show(struct device *parent, struct device_attribute *attr,
  408. char *buf)
  409. {
  410. const struct pwm_chip *chip = dev_get_drvdata(parent);
  411. return sprintf(buf, "%u\n", chip->npwm);
  412. }
  413. static DEVICE_ATTR_RO(npwm);
  414. static struct attribute *pwm_chip_attrs[] = {
  415. &dev_attr_export.attr,
  416. &dev_attr_unexport.attr,
  417. &dev_attr_npwm.attr,
  418. NULL,
  419. };
  420. ATTRIBUTE_GROUPS(pwm_chip);
  421. /* takes export->lock on success */
  422. static struct pwm_export *pwm_class_get_state(struct device *parent,
  423. struct pwm_device *pwm,
  424. struct pwm_state *state)
  425. {
  426. struct device *child;
  427. struct pwm_export *export;
  428. if (!test_bit(PWMF_EXPORTED, &pwm->flags))
  429. return NULL;
  430. child = device_find_child(parent, pwm, pwm_unexport_match);
  431. if (!child)
  432. return NULL;
  433. export = child_to_pwm_export(child);
  434. put_device(child); /* for device_find_child() */
  435. mutex_lock(&export->lock);
  436. pwm_get_state(pwm, state);
  437. return export;
  438. }
  439. static int pwm_class_apply_state(struct pwm_export *export,
  440. struct pwm_device *pwm,
  441. struct pwm_state *state)
  442. {
  443. int ret = pwm_apply_state(pwm, state);
  444. /* release lock taken in pwm_class_get_state */
  445. mutex_unlock(&export->lock);
  446. return ret;
  447. }
  448. static int pwm_class_resume_npwm(struct device *parent, unsigned int npwm)
  449. {
  450. struct pwm_chip *chip = dev_get_drvdata(parent);
  451. unsigned int i;
  452. int ret = 0;
  453. for (i = 0; i < npwm; i++) {
  454. struct pwm_device *pwm = &chip->pwms[i];
  455. struct pwm_state state;
  456. struct pwm_export *export;
  457. export = pwm_class_get_state(parent, pwm, &state);
  458. if (!export)
  459. continue;
  460. /* If pwmchip was not enabled before suspend, do nothing. */
  461. if (!export->suspend.enabled) {
  462. /* release lock taken in pwm_class_get_state */
  463. mutex_unlock(&export->lock);
  464. continue;
  465. }
  466. state.enabled = export->suspend.enabled;
  467. ret = pwm_class_apply_state(export, pwm, &state);
  468. if (ret < 0)
  469. break;
  470. }
  471. return ret;
  472. }
  473. static int __maybe_unused pwm_class_suspend(struct device *parent)
  474. {
  475. struct pwm_chip *chip = dev_get_drvdata(parent);
  476. unsigned int i;
  477. int ret = 0;
  478. for (i = 0; i < chip->npwm; i++) {
  479. struct pwm_device *pwm = &chip->pwms[i];
  480. struct pwm_state state;
  481. struct pwm_export *export;
  482. export = pwm_class_get_state(parent, pwm, &state);
  483. if (!export)
  484. continue;
  485. /*
  486. * If pwmchip was not enabled before suspend, save
  487. * state for resume time and do nothing else.
  488. */
  489. export->suspend = state;
  490. if (!state.enabled) {
  491. /* release lock taken in pwm_class_get_state */
  492. mutex_unlock(&export->lock);
  493. continue;
  494. }
  495. state.enabled = false;
  496. ret = pwm_class_apply_state(export, pwm, &state);
  497. if (ret < 0) {
  498. /*
  499. * roll back the PWM devices that were disabled by
  500. * this suspend function.
  501. */
  502. pwm_class_resume_npwm(parent, i);
  503. break;
  504. }
  505. }
  506. return ret;
  507. }
  508. static int __maybe_unused pwm_class_resume(struct device *parent)
  509. {
  510. struct pwm_chip *chip = dev_get_drvdata(parent);
  511. return pwm_class_resume_npwm(parent, chip->npwm);
  512. }
  513. static SIMPLE_DEV_PM_OPS(pwm_class_pm_ops, pwm_class_suspend, pwm_class_resume);
  514. static struct class pwm_class = {
  515. .name = "pwm",
  516. .owner = THIS_MODULE,
  517. .dev_groups = pwm_chip_groups,
  518. .pm = &pwm_class_pm_ops,
  519. };
  520. static int pwmchip_sysfs_match(struct device *parent, const void *data)
  521. {
  522. return dev_get_drvdata(parent) == data;
  523. }
  524. void pwmchip_sysfs_export(struct pwm_chip *chip)
  525. {
  526. struct device *parent;
  527. /*
  528. * If device_create() fails the pwm_chip is still usable by
  529. * the kernel it's just not exported.
  530. */
  531. parent = device_create(&pwm_class, chip->dev, MKDEV(0, 0), chip,
  532. "pwmchip%d", chip->base);
  533. if (IS_ERR(parent)) {
  534. dev_warn(chip->dev,
  535. "device_create failed for pwm_chip sysfs export\n");
  536. }
  537. }
  538. void pwmchip_sysfs_unexport(struct pwm_chip *chip)
  539. {
  540. struct device *parent;
  541. unsigned int i;
  542. parent = class_find_device(&pwm_class, NULL, chip,
  543. pwmchip_sysfs_match);
  544. if (!parent)
  545. return;
  546. for (i = 0; i < chip->npwm; i++) {
  547. struct pwm_device *pwm = &chip->pwms[i];
  548. if (test_bit(PWMF_EXPORTED, &pwm->flags))
  549. pwm_unexport_child(parent, pwm);
  550. }
  551. put_device(parent);
  552. device_unregister(parent);
  553. }
  554. static int __init pwm_sysfs_init(void)
  555. {
  556. return class_register(&pwm_class);
  557. }
  558. subsys_initcall(pwm_sysfs_init);