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