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