1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * A simple sysfs interface for the generic PWM framework
5 * Copyright (C) 2013 H Hartley Sweeten <hsweeten@visionengravers.com>
7 * Based on previous work by Lars Poeschel <poeschel@lemonage.de>
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>
19 struct pwm_device
*pwm
;
21 struct pwm_state suspend
;
24 static struct pwm_export
*child_to_pwm_export(struct device
*child
)
26 return container_of(child
, struct pwm_export
, child
);
29 static struct pwm_device
*child_to_pwm_device(struct device
*child
)
31 struct pwm_export
*export
= child_to_pwm_export(child
);
36 static ssize_t
period_show(struct device
*child
,
37 struct device_attribute
*attr
,
40 const struct pwm_device
*pwm
= child_to_pwm_device(child
);
41 struct pwm_state state
;
43 pwm_get_state(pwm
, &state
);
45 return sprintf(buf
, "%u\n", state
.period
);
48 static ssize_t
period_store(struct device
*child
,
49 struct device_attribute
*attr
,
50 const char *buf
, size_t size
)
52 struct pwm_export
*export
= child_to_pwm_export(child
);
53 struct pwm_device
*pwm
= export
->pwm
;
54 struct pwm_state state
;
58 ret
= kstrtouint(buf
, 0, &val
);
62 mutex_lock(&export
->lock
);
63 pwm_get_state(pwm
, &state
);
65 ret
= pwm_apply_state(pwm
, &state
);
66 mutex_unlock(&export
->lock
);
71 static ssize_t
duty_cycle_show(struct device
*child
,
72 struct device_attribute
*attr
,
75 const struct pwm_device
*pwm
= child_to_pwm_device(child
);
76 struct pwm_state state
;
78 pwm_get_state(pwm
, &state
);
80 return sprintf(buf
, "%u\n", state
.duty_cycle
);
83 static ssize_t
duty_cycle_store(struct device
*child
,
84 struct device_attribute
*attr
,
85 const char *buf
, size_t size
)
87 struct pwm_export
*export
= child_to_pwm_export(child
);
88 struct pwm_device
*pwm
= export
->pwm
;
89 struct pwm_state state
;
93 ret
= kstrtouint(buf
, 0, &val
);
97 mutex_lock(&export
->lock
);
98 pwm_get_state(pwm
, &state
);
99 state
.duty_cycle
= val
;
100 ret
= pwm_apply_state(pwm
, &state
);
101 mutex_unlock(&export
->lock
);
106 static ssize_t
enable_show(struct device
*child
,
107 struct device_attribute
*attr
,
110 const struct pwm_device
*pwm
= child_to_pwm_device(child
);
111 struct pwm_state state
;
113 pwm_get_state(pwm
, &state
);
115 return sprintf(buf
, "%d\n", state
.enabled
);
118 static ssize_t
enable_store(struct device
*child
,
119 struct device_attribute
*attr
,
120 const char *buf
, size_t size
)
122 struct pwm_export
*export
= child_to_pwm_export(child
);
123 struct pwm_device
*pwm
= export
->pwm
;
124 struct pwm_state state
;
127 ret
= kstrtoint(buf
, 0, &val
);
131 mutex_lock(&export
->lock
);
133 pwm_get_state(pwm
, &state
);
137 state
.enabled
= false;
140 state
.enabled
= true;
147 ret
= pwm_apply_state(pwm
, &state
);
150 mutex_unlock(&export
->lock
);
154 static ssize_t
polarity_show(struct device
*child
,
155 struct device_attribute
*attr
,
158 const struct pwm_device
*pwm
= child_to_pwm_device(child
);
159 const char *polarity
= "unknown";
160 struct pwm_state state
;
162 pwm_get_state(pwm
, &state
);
164 switch (state
.polarity
) {
165 case PWM_POLARITY_NORMAL
:
169 case PWM_POLARITY_INVERSED
:
170 polarity
= "inversed";
174 return sprintf(buf
, "%s\n", polarity
);
177 static ssize_t
polarity_store(struct device
*child
,
178 struct device_attribute
*attr
,
179 const char *buf
, size_t size
)
181 struct pwm_export
*export
= child_to_pwm_export(child
);
182 struct pwm_device
*pwm
= export
->pwm
;
183 enum pwm_polarity polarity
;
184 struct pwm_state state
;
187 if (sysfs_streq(buf
, "normal"))
188 polarity
= PWM_POLARITY_NORMAL
;
189 else if (sysfs_streq(buf
, "inversed"))
190 polarity
= PWM_POLARITY_INVERSED
;
194 mutex_lock(&export
->lock
);
195 pwm_get_state(pwm
, &state
);
196 state
.polarity
= polarity
;
197 ret
= pwm_apply_state(pwm
, &state
);
198 mutex_unlock(&export
->lock
);
203 static ssize_t
capture_show(struct device
*child
,
204 struct device_attribute
*attr
,
207 struct pwm_device
*pwm
= child_to_pwm_device(child
);
208 struct pwm_capture result
;
211 ret
= pwm_capture(pwm
, &result
, jiffies_to_msecs(HZ
));
215 return sprintf(buf
, "%u %u\n", result
.period
, result
.duty_cycle
);
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
);
222 static DEVICE_ATTR_RO(capture
);
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
,
229 &dev_attr_capture
.attr
,
232 ATTRIBUTE_GROUPS(pwm
);
234 static void pwm_export_release(struct device
*child
)
236 struct pwm_export
*export
= child_to_pwm_export(child
);
241 static int pwm_export_child(struct device
*parent
, struct pwm_device
*pwm
)
243 struct pwm_export
*export
;
247 if (test_and_set_bit(PWMF_EXPORTED
, &pwm
->flags
))
250 export
= kzalloc(sizeof(*export
), GFP_KERNEL
);
252 clear_bit(PWMF_EXPORTED
, &pwm
->flags
);
257 mutex_init(&export
->lock
);
259 export
->child
.release
= pwm_export_release
;
260 export
->child
.parent
= parent
;
261 export
->child
.devt
= MKDEV(0, 0);
262 export
->child
.groups
= pwm_groups
;
263 dev_set_name(&export
->child
, "pwm%u", pwm
->hwpwm
);
265 ret
= device_register(&export
->child
);
267 clear_bit(PWMF_EXPORTED
, &pwm
->flags
);
268 put_device(&export
->child
);
272 pwm_prop
[0] = kasprintf(GFP_KERNEL
, "EXPORT=pwm%u", pwm
->hwpwm
);
274 kobject_uevent_env(&parent
->kobj
, KOBJ_CHANGE
, pwm_prop
);
280 static int pwm_unexport_match(struct device
*child
, void *data
)
282 return child_to_pwm_device(child
) == data
;
285 static int pwm_unexport_child(struct device
*parent
, struct pwm_device
*pwm
)
287 struct device
*child
;
290 if (!test_and_clear_bit(PWMF_EXPORTED
, &pwm
->flags
))
293 child
= device_find_child(parent
, pwm
, pwm_unexport_match
);
297 pwm_prop
[0] = kasprintf(GFP_KERNEL
, "UNEXPORT=pwm%u", pwm
->hwpwm
);
299 kobject_uevent_env(&parent
->kobj
, KOBJ_CHANGE
, pwm_prop
);
302 /* for device_find_child() */
304 device_unregister(child
);
310 static ssize_t
export_store(struct device
*parent
,
311 struct device_attribute
*attr
,
312 const char *buf
, size_t len
)
314 struct pwm_chip
*chip
= dev_get_drvdata(parent
);
315 struct pwm_device
*pwm
;
319 ret
= kstrtouint(buf
, 0, &hwpwm
);
323 if (hwpwm
>= chip
->npwm
)
326 pwm
= pwm_request_from_chip(chip
, hwpwm
, "sysfs");
330 ret
= pwm_export_child(parent
, pwm
);
336 static DEVICE_ATTR_WO(export
);
338 static ssize_t
unexport_store(struct device
*parent
,
339 struct device_attribute
*attr
,
340 const char *buf
, size_t len
)
342 struct pwm_chip
*chip
= dev_get_drvdata(parent
);
346 ret
= kstrtouint(buf
, 0, &hwpwm
);
350 if (hwpwm
>= chip
->npwm
)
353 ret
= pwm_unexport_child(parent
, &chip
->pwms
[hwpwm
]);
357 static DEVICE_ATTR_WO(unexport
);
359 static ssize_t
npwm_show(struct device
*parent
, struct device_attribute
*attr
,
362 const struct pwm_chip
*chip
= dev_get_drvdata(parent
);
364 return sprintf(buf
, "%u\n", chip
->npwm
);
366 static DEVICE_ATTR_RO(npwm
);
368 static struct attribute
*pwm_chip_attrs
[] = {
369 &dev_attr_export
.attr
,
370 &dev_attr_unexport
.attr
,
374 ATTRIBUTE_GROUPS(pwm_chip
);
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
)
381 struct device
*child
;
382 struct pwm_export
*export
;
384 if (!test_bit(PWMF_EXPORTED
, &pwm
->flags
))
387 child
= device_find_child(parent
, pwm
, pwm_unexport_match
);
391 export
= child_to_pwm_export(child
);
392 put_device(child
); /* for device_find_child() */
394 mutex_lock(&export
->lock
);
395 pwm_get_state(pwm
, state
);
400 static int pwm_class_apply_state(struct pwm_export
*export
,
401 struct pwm_device
*pwm
,
402 struct pwm_state
*state
)
404 int ret
= pwm_apply_state(pwm
, state
);
406 /* release lock taken in pwm_class_get_state */
407 mutex_unlock(&export
->lock
);
412 static int pwm_class_resume_npwm(struct device
*parent
, unsigned int npwm
)
414 struct pwm_chip
*chip
= dev_get_drvdata(parent
);
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
;
423 export
= pwm_class_get_state(parent
, pwm
, &state
);
427 state
.enabled
= export
->suspend
.enabled
;
428 ret
= pwm_class_apply_state(export
, pwm
, &state
);
436 static int __maybe_unused
pwm_class_suspend(struct device
*parent
)
438 struct pwm_chip
*chip
= dev_get_drvdata(parent
);
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
;
447 export
= pwm_class_get_state(parent
, pwm
, &state
);
451 export
->suspend
= state
;
452 state
.enabled
= false;
453 ret
= pwm_class_apply_state(export
, pwm
, &state
);
456 * roll back the PWM devices that were disabled by
457 * this suspend function.
459 pwm_class_resume_npwm(parent
, i
);
467 static int __maybe_unused
pwm_class_resume(struct device
*parent
)
469 struct pwm_chip
*chip
= dev_get_drvdata(parent
);
471 return pwm_class_resume_npwm(parent
, chip
->npwm
);
474 static SIMPLE_DEV_PM_OPS(pwm_class_pm_ops
, pwm_class_suspend
, pwm_class_resume
);
476 static struct class pwm_class
= {
478 .owner
= THIS_MODULE
,
479 .dev_groups
= pwm_chip_groups
,
480 .pm
= &pwm_class_pm_ops
,
483 static int pwmchip_sysfs_match(struct device
*parent
, const void *data
)
485 return dev_get_drvdata(parent
) == data
;
488 void pwmchip_sysfs_export(struct pwm_chip
*chip
)
490 struct device
*parent
;
493 * If device_create() fails the pwm_chip is still usable by
494 * the kernel it's just not exported.
496 parent
= device_create(&pwm_class
, chip
->dev
, MKDEV(0, 0), chip
,
497 "pwmchip%d", chip
->base
);
498 if (IS_ERR(parent
)) {
500 "device_create failed for pwm_chip sysfs export\n");
504 void pwmchip_sysfs_unexport(struct pwm_chip
*chip
)
506 struct device
*parent
;
509 parent
= class_find_device(&pwm_class
, NULL
, chip
,
510 pwmchip_sysfs_match
);
514 for (i
= 0; i
< chip
->npwm
; i
++) {
515 struct pwm_device
*pwm
= &chip
->pwms
[i
];
517 if (test_bit(PWMF_EXPORTED
, &pwm
->flags
))
518 pwm_unexport_child(parent
, pwm
);
522 device_unregister(parent
);
525 static int __init
pwm_sysfs_init(void)
527 return class_register(&pwm_class
);
529 subsys_initcall(pwm_sysfs_init
);