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