Merge tag '6.10-rc6-smb3-server-fixes' of git://git.samba.org/ksmbd
[linux.git] / drivers / pwm / core.c
blob18574857641ed979b5c0b979e44e2c0f93cf0ba7
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Generic pwmlib implementation
5 * Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de>
6 * Copyright (C) 2011-2012 Avionic Design GmbH
7 */
9 #include <linux/acpi.h>
10 #include <linux/module.h>
11 #include <linux/idr.h>
12 #include <linux/of.h>
13 #include <linux/pwm.h>
14 #include <linux/list.h>
15 #include <linux/mutex.h>
16 #include <linux/err.h>
17 #include <linux/slab.h>
18 #include <linux/device.h>
19 #include <linux/debugfs.h>
20 #include <linux/seq_file.h>
22 #include <dt-bindings/pwm/pwm.h>
24 #define CREATE_TRACE_POINTS
25 #include <trace/events/pwm.h>
27 /* protects access to pwm_chips */
28 static DEFINE_MUTEX(pwm_lock);
30 static DEFINE_IDR(pwm_chips);
32 static void pwm_apply_debug(struct pwm_device *pwm,
33 const struct pwm_state *state)
35 struct pwm_state *last = &pwm->last;
36 struct pwm_chip *chip = pwm->chip;
37 struct pwm_state s1 = { 0 }, s2 = { 0 };
38 int err;
40 if (!IS_ENABLED(CONFIG_PWM_DEBUG))
41 return;
43 /* No reasonable diagnosis possible without .get_state() */
44 if (!chip->ops->get_state)
45 return;
48 * *state was just applied. Read out the hardware state and do some
49 * checks.
52 err = chip->ops->get_state(chip, pwm, &s1);
53 trace_pwm_get(pwm, &s1, err);
54 if (err)
55 /* If that failed there isn't much to debug */
56 return;
59 * The lowlevel driver either ignored .polarity (which is a bug) or as
60 * best effort inverted .polarity and fixed .duty_cycle respectively.
61 * Undo this inversion and fixup for further tests.
63 if (s1.enabled && s1.polarity != state->polarity) {
64 s2.polarity = state->polarity;
65 s2.duty_cycle = s1.period - s1.duty_cycle;
66 s2.period = s1.period;
67 s2.enabled = s1.enabled;
68 } else {
69 s2 = s1;
72 if (s2.polarity != state->polarity &&
73 state->duty_cycle < state->period)
74 dev_warn(pwmchip_parent(chip), ".apply ignored .polarity\n");
76 if (state->enabled &&
77 last->polarity == state->polarity &&
78 last->period > s2.period &&
79 last->period <= state->period)
80 dev_warn(pwmchip_parent(chip),
81 ".apply didn't pick the best available period (requested: %llu, applied: %llu, possible: %llu)\n",
82 state->period, s2.period, last->period);
84 if (state->enabled && state->period < s2.period)
85 dev_warn(pwmchip_parent(chip),
86 ".apply is supposed to round down period (requested: %llu, applied: %llu)\n",
87 state->period, s2.period);
89 if (state->enabled &&
90 last->polarity == state->polarity &&
91 last->period == s2.period &&
92 last->duty_cycle > s2.duty_cycle &&
93 last->duty_cycle <= state->duty_cycle)
94 dev_warn(pwmchip_parent(chip),
95 ".apply didn't pick the best available duty cycle (requested: %llu/%llu, applied: %llu/%llu, possible: %llu/%llu)\n",
96 state->duty_cycle, state->period,
97 s2.duty_cycle, s2.period,
98 last->duty_cycle, last->period);
100 if (state->enabled && state->duty_cycle < s2.duty_cycle)
101 dev_warn(pwmchip_parent(chip),
102 ".apply is supposed to round down duty_cycle (requested: %llu/%llu, applied: %llu/%llu)\n",
103 state->duty_cycle, state->period,
104 s2.duty_cycle, s2.period);
106 if (!state->enabled && s2.enabled && s2.duty_cycle > 0)
107 dev_warn(pwmchip_parent(chip),
108 "requested disabled, but yielded enabled with duty > 0\n");
110 /* reapply the state that the driver reported being configured. */
111 err = chip->ops->apply(chip, pwm, &s1);
112 trace_pwm_apply(pwm, &s1, err);
113 if (err) {
114 *last = s1;
115 dev_err(pwmchip_parent(chip), "failed to reapply current setting\n");
116 return;
119 *last = (struct pwm_state){ 0 };
120 err = chip->ops->get_state(chip, pwm, last);
121 trace_pwm_get(pwm, last, err);
122 if (err)
123 return;
125 /* reapplication of the current state should give an exact match */
126 if (s1.enabled != last->enabled ||
127 s1.polarity != last->polarity ||
128 (s1.enabled && s1.period != last->period) ||
129 (s1.enabled && s1.duty_cycle != last->duty_cycle)) {
130 dev_err(pwmchip_parent(chip),
131 ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n",
132 s1.enabled, s1.polarity, s1.duty_cycle, s1.period,
133 last->enabled, last->polarity, last->duty_cycle,
134 last->period);
139 * __pwm_apply() - atomically apply a new state to a PWM device
140 * @pwm: PWM device
141 * @state: new state to apply
143 static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state)
145 struct pwm_chip *chip;
146 int err;
148 if (!pwm || !state || !state->period ||
149 state->duty_cycle > state->period)
150 return -EINVAL;
152 chip = pwm->chip;
154 if (state->period == pwm->state.period &&
155 state->duty_cycle == pwm->state.duty_cycle &&
156 state->polarity == pwm->state.polarity &&
157 state->enabled == pwm->state.enabled &&
158 state->usage_power == pwm->state.usage_power)
159 return 0;
161 err = chip->ops->apply(chip, pwm, state);
162 trace_pwm_apply(pwm, state, err);
163 if (err)
164 return err;
166 pwm->state = *state;
169 * only do this after pwm->state was applied as some
170 * implementations of .get_state depend on this
172 pwm_apply_debug(pwm, state);
174 return 0;
178 * pwm_apply_might_sleep() - atomically apply a new state to a PWM device
179 * Cannot be used in atomic context.
180 * @pwm: PWM device
181 * @state: new state to apply
183 int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state)
185 int err;
188 * Some lowlevel driver's implementations of .apply() make use of
189 * mutexes, also with some drivers only returning when the new
190 * configuration is active calling pwm_apply_might_sleep() from atomic context
191 * is a bad idea. So make it explicit that calling this function might
192 * sleep.
194 might_sleep();
196 if (IS_ENABLED(CONFIG_PWM_DEBUG) && pwm->chip->atomic) {
198 * Catch any drivers that have been marked as atomic but
199 * that will sleep anyway.
201 non_block_start();
202 err = __pwm_apply(pwm, state);
203 non_block_end();
204 } else {
205 err = __pwm_apply(pwm, state);
208 return err;
210 EXPORT_SYMBOL_GPL(pwm_apply_might_sleep);
213 * pwm_apply_atomic() - apply a new state to a PWM device from atomic context
214 * Not all PWM devices support this function, check with pwm_might_sleep().
215 * @pwm: PWM device
216 * @state: new state to apply
218 int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state)
220 WARN_ONCE(!pwm->chip->atomic,
221 "sleeping PWM driver used in atomic context\n");
223 return __pwm_apply(pwm, state);
225 EXPORT_SYMBOL_GPL(pwm_apply_atomic);
228 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
229 * @pwm: PWM device
231 * This function will adjust the PWM config to the PWM arguments provided
232 * by the DT or PWM lookup table. This is particularly useful to adapt
233 * the bootloader config to the Linux one.
235 int pwm_adjust_config(struct pwm_device *pwm)
237 struct pwm_state state;
238 struct pwm_args pargs;
240 pwm_get_args(pwm, &pargs);
241 pwm_get_state(pwm, &state);
244 * If the current period is zero it means that either the PWM driver
245 * does not support initial state retrieval or the PWM has not yet
246 * been configured.
248 * In either case, we setup the new period and polarity, and assign a
249 * duty cycle of 0.
251 if (!state.period) {
252 state.duty_cycle = 0;
253 state.period = pargs.period;
254 state.polarity = pargs.polarity;
256 return pwm_apply_might_sleep(pwm, &state);
260 * Adjust the PWM duty cycle/period based on the period value provided
261 * in PWM args.
263 if (pargs.period != state.period) {
264 u64 dutycycle = (u64)state.duty_cycle * pargs.period;
266 do_div(dutycycle, state.period);
267 state.duty_cycle = dutycycle;
268 state.period = pargs.period;
272 * If the polarity changed, we should also change the duty cycle.
274 if (pargs.polarity != state.polarity) {
275 state.polarity = pargs.polarity;
276 state.duty_cycle = state.period - state.duty_cycle;
279 return pwm_apply_might_sleep(pwm, &state);
281 EXPORT_SYMBOL_GPL(pwm_adjust_config);
284 * pwm_capture() - capture and report a PWM signal
285 * @pwm: PWM device
286 * @result: structure to fill with capture result
287 * @timeout: time to wait, in milliseconds, before giving up on capture
289 * Returns: 0 on success or a negative error code on failure.
291 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
292 unsigned long timeout)
294 int err;
296 if (!pwm || !pwm->chip->ops)
297 return -EINVAL;
299 if (!pwm->chip->ops->capture)
300 return -ENOSYS;
302 mutex_lock(&pwm_lock);
303 err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout);
304 mutex_unlock(&pwm_lock);
306 return err;
308 EXPORT_SYMBOL_GPL(pwm_capture);
310 static struct pwm_chip *pwmchip_find_by_name(const char *name)
312 struct pwm_chip *chip;
313 unsigned long id, tmp;
315 if (!name)
316 return NULL;
318 mutex_lock(&pwm_lock);
320 idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) {
321 const char *chip_name = dev_name(pwmchip_parent(chip));
323 if (chip_name && strcmp(chip_name, name) == 0) {
324 mutex_unlock(&pwm_lock);
325 return chip;
329 mutex_unlock(&pwm_lock);
331 return NULL;
334 static int pwm_device_request(struct pwm_device *pwm, const char *label)
336 int err;
337 struct pwm_chip *chip = pwm->chip;
338 const struct pwm_ops *ops = chip->ops;
340 if (test_bit(PWMF_REQUESTED, &pwm->flags))
341 return -EBUSY;
343 if (!try_module_get(chip->owner))
344 return -ENODEV;
346 if (!get_device(&chip->dev)) {
347 err = -ENODEV;
348 goto err_get_device;
351 if (ops->request) {
352 err = ops->request(chip, pwm);
353 if (err) {
354 put_device(&chip->dev);
355 err_get_device:
356 module_put(chip->owner);
357 return err;
361 if (ops->get_state) {
363 * Zero-initialize state because most drivers are unaware of
364 * .usage_power. The other members of state are supposed to be
365 * set by lowlevel drivers. We still initialize the whole
366 * structure for simplicity even though this might paper over
367 * faulty implementations of .get_state().
369 struct pwm_state state = { 0, };
371 err = ops->get_state(chip, pwm, &state);
372 trace_pwm_get(pwm, &state, err);
374 if (!err)
375 pwm->state = state;
377 if (IS_ENABLED(CONFIG_PWM_DEBUG))
378 pwm->last = pwm->state;
381 set_bit(PWMF_REQUESTED, &pwm->flags);
382 pwm->label = label;
384 return 0;
388 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
389 * @chip: PWM chip
390 * @index: per-chip index of the PWM to request
391 * @label: a literal description string of this PWM
393 * Returns: A pointer to the PWM device at the given index of the given PWM
394 * chip. A negative error code is returned if the index is not valid for the
395 * specified PWM chip or if the PWM device cannot be requested.
397 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
398 unsigned int index,
399 const char *label)
401 struct pwm_device *pwm;
402 int err;
404 if (!chip || index >= chip->npwm)
405 return ERR_PTR(-EINVAL);
407 mutex_lock(&pwm_lock);
408 pwm = &chip->pwms[index];
410 err = pwm_device_request(pwm, label);
411 if (err < 0)
412 pwm = ERR_PTR(err);
414 mutex_unlock(&pwm_lock);
415 return pwm;
417 EXPORT_SYMBOL_GPL(pwm_request_from_chip);
420 struct pwm_device *
421 of_pwm_xlate_with_flags(struct pwm_chip *chip, const struct of_phandle_args *args)
423 struct pwm_device *pwm;
425 /* period in the second cell and flags in the third cell are optional */
426 if (args->args_count < 1)
427 return ERR_PTR(-EINVAL);
429 pwm = pwm_request_from_chip(chip, args->args[0], NULL);
430 if (IS_ERR(pwm))
431 return pwm;
433 if (args->args_count > 1)
434 pwm->args.period = args->args[1];
436 pwm->args.polarity = PWM_POLARITY_NORMAL;
437 if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED)
438 pwm->args.polarity = PWM_POLARITY_INVERSED;
440 return pwm;
442 EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
444 struct pwm_device *
445 of_pwm_single_xlate(struct pwm_chip *chip, const struct of_phandle_args *args)
447 struct pwm_device *pwm;
449 pwm = pwm_request_from_chip(chip, 0, NULL);
450 if (IS_ERR(pwm))
451 return pwm;
453 if (args->args_count > 0)
454 pwm->args.period = args->args[0];
456 pwm->args.polarity = PWM_POLARITY_NORMAL;
457 if (args->args_count > 1 && args->args[1] & PWM_POLARITY_INVERTED)
458 pwm->args.polarity = PWM_POLARITY_INVERSED;
460 return pwm;
462 EXPORT_SYMBOL_GPL(of_pwm_single_xlate);
464 struct pwm_export {
465 struct device pwm_dev;
466 struct pwm_device *pwm;
467 struct mutex lock;
468 struct pwm_state suspend;
471 static inline struct pwm_chip *pwmchip_from_dev(struct device *pwmchip_dev)
473 return container_of(pwmchip_dev, struct pwm_chip, dev);
476 static inline struct pwm_export *pwmexport_from_dev(struct device *pwm_dev)
478 return container_of(pwm_dev, struct pwm_export, pwm_dev);
481 static inline struct pwm_device *pwm_from_dev(struct device *pwm_dev)
483 struct pwm_export *export = pwmexport_from_dev(pwm_dev);
485 return export->pwm;
488 static ssize_t period_show(struct device *pwm_dev,
489 struct device_attribute *attr,
490 char *buf)
492 const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
493 struct pwm_state state;
495 pwm_get_state(pwm, &state);
497 return sysfs_emit(buf, "%llu\n", state.period);
500 static ssize_t period_store(struct device *pwm_dev,
501 struct device_attribute *attr,
502 const char *buf, size_t size)
504 struct pwm_export *export = pwmexport_from_dev(pwm_dev);
505 struct pwm_device *pwm = export->pwm;
506 struct pwm_state state;
507 u64 val;
508 int ret;
510 ret = kstrtou64(buf, 0, &val);
511 if (ret)
512 return ret;
514 mutex_lock(&export->lock);
515 pwm_get_state(pwm, &state);
516 state.period = val;
517 ret = pwm_apply_might_sleep(pwm, &state);
518 mutex_unlock(&export->lock);
520 return ret ? : size;
523 static ssize_t duty_cycle_show(struct device *pwm_dev,
524 struct device_attribute *attr,
525 char *buf)
527 const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
528 struct pwm_state state;
530 pwm_get_state(pwm, &state);
532 return sysfs_emit(buf, "%llu\n", state.duty_cycle);
535 static ssize_t duty_cycle_store(struct device *pwm_dev,
536 struct device_attribute *attr,
537 const char *buf, size_t size)
539 struct pwm_export *export = pwmexport_from_dev(pwm_dev);
540 struct pwm_device *pwm = export->pwm;
541 struct pwm_state state;
542 u64 val;
543 int ret;
545 ret = kstrtou64(buf, 0, &val);
546 if (ret)
547 return ret;
549 mutex_lock(&export->lock);
550 pwm_get_state(pwm, &state);
551 state.duty_cycle = val;
552 ret = pwm_apply_might_sleep(pwm, &state);
553 mutex_unlock(&export->lock);
555 return ret ? : size;
558 static ssize_t enable_show(struct device *pwm_dev,
559 struct device_attribute *attr,
560 char *buf)
562 const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
563 struct pwm_state state;
565 pwm_get_state(pwm, &state);
567 return sysfs_emit(buf, "%d\n", state.enabled);
570 static ssize_t enable_store(struct device *pwm_dev,
571 struct device_attribute *attr,
572 const char *buf, size_t size)
574 struct pwm_export *export = pwmexport_from_dev(pwm_dev);
575 struct pwm_device *pwm = export->pwm;
576 struct pwm_state state;
577 int val, ret;
579 ret = kstrtoint(buf, 0, &val);
580 if (ret)
581 return ret;
583 mutex_lock(&export->lock);
585 pwm_get_state(pwm, &state);
587 switch (val) {
588 case 0:
589 state.enabled = false;
590 break;
591 case 1:
592 state.enabled = true;
593 break;
594 default:
595 ret = -EINVAL;
596 goto unlock;
599 ret = pwm_apply_might_sleep(pwm, &state);
601 unlock:
602 mutex_unlock(&export->lock);
603 return ret ? : size;
606 static ssize_t polarity_show(struct device *pwm_dev,
607 struct device_attribute *attr,
608 char *buf)
610 const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
611 const char *polarity = "unknown";
612 struct pwm_state state;
614 pwm_get_state(pwm, &state);
616 switch (state.polarity) {
617 case PWM_POLARITY_NORMAL:
618 polarity = "normal";
619 break;
621 case PWM_POLARITY_INVERSED:
622 polarity = "inversed";
623 break;
626 return sysfs_emit(buf, "%s\n", polarity);
629 static ssize_t polarity_store(struct device *pwm_dev,
630 struct device_attribute *attr,
631 const char *buf, size_t size)
633 struct pwm_export *export = pwmexport_from_dev(pwm_dev);
634 struct pwm_device *pwm = export->pwm;
635 enum pwm_polarity polarity;
636 struct pwm_state state;
637 int ret;
639 if (sysfs_streq(buf, "normal"))
640 polarity = PWM_POLARITY_NORMAL;
641 else if (sysfs_streq(buf, "inversed"))
642 polarity = PWM_POLARITY_INVERSED;
643 else
644 return -EINVAL;
646 mutex_lock(&export->lock);
647 pwm_get_state(pwm, &state);
648 state.polarity = polarity;
649 ret = pwm_apply_might_sleep(pwm, &state);
650 mutex_unlock(&export->lock);
652 return ret ? : size;
655 static ssize_t capture_show(struct device *pwm_dev,
656 struct device_attribute *attr,
657 char *buf)
659 struct pwm_device *pwm = pwm_from_dev(pwm_dev);
660 struct pwm_capture result;
661 int ret;
663 ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ));
664 if (ret)
665 return ret;
667 return sysfs_emit(buf, "%u %u\n", result.period, result.duty_cycle);
670 static DEVICE_ATTR_RW(period);
671 static DEVICE_ATTR_RW(duty_cycle);
672 static DEVICE_ATTR_RW(enable);
673 static DEVICE_ATTR_RW(polarity);
674 static DEVICE_ATTR_RO(capture);
676 static struct attribute *pwm_attrs[] = {
677 &dev_attr_period.attr,
678 &dev_attr_duty_cycle.attr,
679 &dev_attr_enable.attr,
680 &dev_attr_polarity.attr,
681 &dev_attr_capture.attr,
682 NULL
684 ATTRIBUTE_GROUPS(pwm);
686 static void pwm_export_release(struct device *pwm_dev)
688 struct pwm_export *export = pwmexport_from_dev(pwm_dev);
690 kfree(export);
693 static int pwm_export_child(struct device *pwmchip_dev, struct pwm_device *pwm)
695 struct pwm_export *export;
696 char *pwm_prop[2];
697 int ret;
699 if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags))
700 return -EBUSY;
702 export = kzalloc(sizeof(*export), GFP_KERNEL);
703 if (!export) {
704 clear_bit(PWMF_EXPORTED, &pwm->flags);
705 return -ENOMEM;
708 export->pwm = pwm;
709 mutex_init(&export->lock);
711 export->pwm_dev.release = pwm_export_release;
712 export->pwm_dev.parent = pwmchip_dev;
713 export->pwm_dev.devt = MKDEV(0, 0);
714 export->pwm_dev.groups = pwm_groups;
715 dev_set_name(&export->pwm_dev, "pwm%u", pwm->hwpwm);
717 ret = device_register(&export->pwm_dev);
718 if (ret) {
719 clear_bit(PWMF_EXPORTED, &pwm->flags);
720 put_device(&export->pwm_dev);
721 export = NULL;
722 return ret;
724 pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm);
725 pwm_prop[1] = NULL;
726 kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop);
727 kfree(pwm_prop[0]);
729 return 0;
732 static int pwm_unexport_match(struct device *pwm_dev, void *data)
734 return pwm_from_dev(pwm_dev) == data;
737 static int pwm_unexport_child(struct device *pwmchip_dev, struct pwm_device *pwm)
739 struct device *pwm_dev;
740 char *pwm_prop[2];
742 if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags))
743 return -ENODEV;
745 pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match);
746 if (!pwm_dev)
747 return -ENODEV;
749 pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm);
750 pwm_prop[1] = NULL;
751 kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop);
752 kfree(pwm_prop[0]);
754 /* for device_find_child() */
755 put_device(pwm_dev);
756 device_unregister(pwm_dev);
757 pwm_put(pwm);
759 return 0;
762 static ssize_t export_store(struct device *pwmchip_dev,
763 struct device_attribute *attr,
764 const char *buf, size_t len)
766 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
767 struct pwm_device *pwm;
768 unsigned int hwpwm;
769 int ret;
771 ret = kstrtouint(buf, 0, &hwpwm);
772 if (ret < 0)
773 return ret;
775 if (hwpwm >= chip->npwm)
776 return -ENODEV;
778 pwm = pwm_request_from_chip(chip, hwpwm, "sysfs");
779 if (IS_ERR(pwm))
780 return PTR_ERR(pwm);
782 ret = pwm_export_child(pwmchip_dev, pwm);
783 if (ret < 0)
784 pwm_put(pwm);
786 return ret ? : len;
788 static DEVICE_ATTR_WO(export);
790 static ssize_t unexport_store(struct device *pwmchip_dev,
791 struct device_attribute *attr,
792 const char *buf, size_t len)
794 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
795 unsigned int hwpwm;
796 int ret;
798 ret = kstrtouint(buf, 0, &hwpwm);
799 if (ret < 0)
800 return ret;
802 if (hwpwm >= chip->npwm)
803 return -ENODEV;
805 ret = pwm_unexport_child(pwmchip_dev, &chip->pwms[hwpwm]);
807 return ret ? : len;
809 static DEVICE_ATTR_WO(unexport);
811 static ssize_t npwm_show(struct device *pwmchip_dev, struct device_attribute *attr,
812 char *buf)
814 const struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
816 return sysfs_emit(buf, "%u\n", chip->npwm);
818 static DEVICE_ATTR_RO(npwm);
820 static struct attribute *pwm_chip_attrs[] = {
821 &dev_attr_export.attr,
822 &dev_attr_unexport.attr,
823 &dev_attr_npwm.attr,
824 NULL,
826 ATTRIBUTE_GROUPS(pwm_chip);
828 /* takes export->lock on success */
829 static struct pwm_export *pwm_class_get_state(struct device *pwmchip_dev,
830 struct pwm_device *pwm,
831 struct pwm_state *state)
833 struct device *pwm_dev;
834 struct pwm_export *export;
836 if (!test_bit(PWMF_EXPORTED, &pwm->flags))
837 return NULL;
839 pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match);
840 if (!pwm_dev)
841 return NULL;
843 export = pwmexport_from_dev(pwm_dev);
844 put_device(pwm_dev); /* for device_find_child() */
846 mutex_lock(&export->lock);
847 pwm_get_state(pwm, state);
849 return export;
852 static int pwm_class_apply_state(struct pwm_export *export,
853 struct pwm_device *pwm,
854 struct pwm_state *state)
856 int ret = pwm_apply_might_sleep(pwm, state);
858 /* release lock taken in pwm_class_get_state */
859 mutex_unlock(&export->lock);
861 return ret;
864 static int pwm_class_resume_npwm(struct device *pwmchip_dev, unsigned int npwm)
866 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
867 unsigned int i;
868 int ret = 0;
870 for (i = 0; i < npwm; i++) {
871 struct pwm_device *pwm = &chip->pwms[i];
872 struct pwm_state state;
873 struct pwm_export *export;
875 export = pwm_class_get_state(pwmchip_dev, pwm, &state);
876 if (!export)
877 continue;
879 /* If pwmchip was not enabled before suspend, do nothing. */
880 if (!export->suspend.enabled) {
881 /* release lock taken in pwm_class_get_state */
882 mutex_unlock(&export->lock);
883 continue;
886 state.enabled = export->suspend.enabled;
887 ret = pwm_class_apply_state(export, pwm, &state);
888 if (ret < 0)
889 break;
892 return ret;
895 static int pwm_class_suspend(struct device *pwmchip_dev)
897 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
898 unsigned int i;
899 int ret = 0;
901 for (i = 0; i < chip->npwm; i++) {
902 struct pwm_device *pwm = &chip->pwms[i];
903 struct pwm_state state;
904 struct pwm_export *export;
906 export = pwm_class_get_state(pwmchip_dev, pwm, &state);
907 if (!export)
908 continue;
911 * If pwmchip was not enabled before suspend, save
912 * state for resume time and do nothing else.
914 export->suspend = state;
915 if (!state.enabled) {
916 /* release lock taken in pwm_class_get_state */
917 mutex_unlock(&export->lock);
918 continue;
921 state.enabled = false;
922 ret = pwm_class_apply_state(export, pwm, &state);
923 if (ret < 0) {
925 * roll back the PWM devices that were disabled by
926 * this suspend function.
928 pwm_class_resume_npwm(pwmchip_dev, i);
929 break;
933 return ret;
936 static int pwm_class_resume(struct device *pwmchip_dev)
938 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
940 return pwm_class_resume_npwm(pwmchip_dev, chip->npwm);
943 static DEFINE_SIMPLE_DEV_PM_OPS(pwm_class_pm_ops, pwm_class_suspend, pwm_class_resume);
945 static struct class pwm_class = {
946 .name = "pwm",
947 .dev_groups = pwm_chip_groups,
948 .pm = pm_sleep_ptr(&pwm_class_pm_ops),
951 static void pwmchip_sysfs_unexport(struct pwm_chip *chip)
953 unsigned int i;
955 for (i = 0; i < chip->npwm; i++) {
956 struct pwm_device *pwm = &chip->pwms[i];
958 if (test_bit(PWMF_EXPORTED, &pwm->flags))
959 pwm_unexport_child(&chip->dev, pwm);
963 #define PWMCHIP_ALIGN ARCH_DMA_MINALIGN
965 static void *pwmchip_priv(struct pwm_chip *chip)
967 return (void *)chip + ALIGN(struct_size(chip, pwms, chip->npwm), PWMCHIP_ALIGN);
970 /* This is the counterpart to pwmchip_alloc() */
971 void pwmchip_put(struct pwm_chip *chip)
973 put_device(&chip->dev);
975 EXPORT_SYMBOL_GPL(pwmchip_put);
977 static void pwmchip_release(struct device *pwmchip_dev)
979 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
981 kfree(chip);
984 struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv)
986 struct pwm_chip *chip;
987 struct device *pwmchip_dev;
988 size_t alloc_size;
989 unsigned int i;
991 alloc_size = size_add(ALIGN(struct_size(chip, pwms, npwm), PWMCHIP_ALIGN),
992 sizeof_priv);
994 chip = kzalloc(alloc_size, GFP_KERNEL);
995 if (!chip)
996 return ERR_PTR(-ENOMEM);
998 chip->npwm = npwm;
999 chip->uses_pwmchip_alloc = true;
1001 pwmchip_dev = &chip->dev;
1002 device_initialize(pwmchip_dev);
1003 pwmchip_dev->class = &pwm_class;
1004 pwmchip_dev->parent = parent;
1005 pwmchip_dev->release = pwmchip_release;
1007 pwmchip_set_drvdata(chip, pwmchip_priv(chip));
1009 for (i = 0; i < chip->npwm; i++) {
1010 struct pwm_device *pwm = &chip->pwms[i];
1011 pwm->chip = chip;
1012 pwm->hwpwm = i;
1015 return chip;
1017 EXPORT_SYMBOL_GPL(pwmchip_alloc);
1019 static void devm_pwmchip_put(void *data)
1021 struct pwm_chip *chip = data;
1023 pwmchip_put(chip);
1026 struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv)
1028 struct pwm_chip *chip;
1029 int ret;
1031 chip = pwmchip_alloc(parent, npwm, sizeof_priv);
1032 if (IS_ERR(chip))
1033 return chip;
1035 ret = devm_add_action_or_reset(parent, devm_pwmchip_put, chip);
1036 if (ret)
1037 return ERR_PTR(ret);
1039 return chip;
1041 EXPORT_SYMBOL_GPL(devm_pwmchip_alloc);
1043 static void of_pwmchip_add(struct pwm_chip *chip)
1045 if (!pwmchip_parent(chip) || !pwmchip_parent(chip)->of_node)
1046 return;
1048 if (!chip->of_xlate)
1049 chip->of_xlate = of_pwm_xlate_with_flags;
1051 of_node_get(pwmchip_parent(chip)->of_node);
1054 static void of_pwmchip_remove(struct pwm_chip *chip)
1056 if (pwmchip_parent(chip))
1057 of_node_put(pwmchip_parent(chip)->of_node);
1060 static bool pwm_ops_check(const struct pwm_chip *chip)
1062 const struct pwm_ops *ops = chip->ops;
1064 if (!ops->apply)
1065 return false;
1067 if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state)
1068 dev_warn(pwmchip_parent(chip),
1069 "Please implement the .get_state() callback\n");
1071 return true;
1075 * __pwmchip_add() - register a new PWM chip
1076 * @chip: the PWM chip to add
1077 * @owner: reference to the module providing the chip.
1079 * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the
1080 * pwmchip_add wrapper to do this right.
1082 * Returns: 0 on success or a negative error code on failure.
1084 int __pwmchip_add(struct pwm_chip *chip, struct module *owner)
1086 int ret;
1088 if (!chip || !pwmchip_parent(chip) || !chip->ops || !chip->npwm)
1089 return -EINVAL;
1092 * a struct pwm_chip must be allocated using (devm_)pwmchip_alloc,
1093 * otherwise the embedded struct device might disappear too early
1094 * resulting in memory corruption.
1095 * Catch drivers that were not converted appropriately.
1097 if (!chip->uses_pwmchip_alloc)
1098 return -EINVAL;
1100 if (!pwm_ops_check(chip))
1101 return -EINVAL;
1103 chip->owner = owner;
1105 mutex_lock(&pwm_lock);
1107 ret = idr_alloc(&pwm_chips, chip, 0, 0, GFP_KERNEL);
1108 if (ret < 0)
1109 goto err_idr_alloc;
1111 chip->id = ret;
1113 dev_set_name(&chip->dev, "pwmchip%u", chip->id);
1115 if (IS_ENABLED(CONFIG_OF))
1116 of_pwmchip_add(chip);
1118 ret = device_add(&chip->dev);
1119 if (ret)
1120 goto err_device_add;
1122 mutex_unlock(&pwm_lock);
1124 return 0;
1126 err_device_add:
1127 if (IS_ENABLED(CONFIG_OF))
1128 of_pwmchip_remove(chip);
1130 idr_remove(&pwm_chips, chip->id);
1131 err_idr_alloc:
1133 mutex_unlock(&pwm_lock);
1135 return ret;
1137 EXPORT_SYMBOL_GPL(__pwmchip_add);
1140 * pwmchip_remove() - remove a PWM chip
1141 * @chip: the PWM chip to remove
1143 * Removes a PWM chip.
1145 void pwmchip_remove(struct pwm_chip *chip)
1147 pwmchip_sysfs_unexport(chip);
1149 if (IS_ENABLED(CONFIG_OF))
1150 of_pwmchip_remove(chip);
1152 mutex_lock(&pwm_lock);
1154 idr_remove(&pwm_chips, chip->id);
1156 mutex_unlock(&pwm_lock);
1158 device_del(&chip->dev);
1160 EXPORT_SYMBOL_GPL(pwmchip_remove);
1162 static void devm_pwmchip_remove(void *data)
1164 struct pwm_chip *chip = data;
1166 pwmchip_remove(chip);
1169 int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner)
1171 int ret;
1173 ret = __pwmchip_add(chip, owner);
1174 if (ret)
1175 return ret;
1177 return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip);
1179 EXPORT_SYMBOL_GPL(__devm_pwmchip_add);
1181 static struct device_link *pwm_device_link_add(struct device *dev,
1182 struct pwm_device *pwm)
1184 struct device_link *dl;
1186 if (!dev) {
1188 * No device for the PWM consumer has been provided. It may
1189 * impact the PM sequence ordering: the PWM supplier may get
1190 * suspended before the consumer.
1192 dev_warn(pwmchip_parent(pwm->chip),
1193 "No consumer device specified to create a link to\n");
1194 return NULL;
1197 dl = device_link_add(dev, pwmchip_parent(pwm->chip), DL_FLAG_AUTOREMOVE_CONSUMER);
1198 if (!dl) {
1199 dev_err(dev, "failed to create device link to %s\n",
1200 dev_name(pwmchip_parent(pwm->chip)));
1201 return ERR_PTR(-EINVAL);
1204 return dl;
1207 static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode)
1209 struct pwm_chip *chip;
1210 unsigned long id, tmp;
1212 mutex_lock(&pwm_lock);
1214 idr_for_each_entry_ul(&pwm_chips, chip, tmp, id)
1215 if (pwmchip_parent(chip) && device_match_fwnode(pwmchip_parent(chip), fwnode)) {
1216 mutex_unlock(&pwm_lock);
1217 return chip;
1220 mutex_unlock(&pwm_lock);
1222 return ERR_PTR(-EPROBE_DEFER);
1226 * of_pwm_get() - request a PWM via the PWM framework
1227 * @dev: device for PWM consumer
1228 * @np: device node to get the PWM from
1229 * @con_id: consumer name
1231 * Returns the PWM device parsed from the phandle and index specified in the
1232 * "pwms" property of a device tree node or a negative error-code on failure.
1233 * Values parsed from the device tree are stored in the returned PWM device
1234 * object.
1236 * If con_id is NULL, the first PWM device listed in the "pwms" property will
1237 * be requested. Otherwise the "pwm-names" property is used to do a reverse
1238 * lookup of the PWM index. This also means that the "pwm-names" property
1239 * becomes mandatory for devices that look up the PWM device via the con_id
1240 * parameter.
1242 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1243 * error code on failure.
1245 static struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
1246 const char *con_id)
1248 struct pwm_device *pwm = NULL;
1249 struct of_phandle_args args;
1250 struct device_link *dl;
1251 struct pwm_chip *chip;
1252 int index = 0;
1253 int err;
1255 if (con_id) {
1256 index = of_property_match_string(np, "pwm-names", con_id);
1257 if (index < 0)
1258 return ERR_PTR(index);
1261 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
1262 &args);
1263 if (err) {
1264 pr_err("%s(): can't parse \"pwms\" property\n", __func__);
1265 return ERR_PTR(err);
1268 chip = fwnode_to_pwmchip(of_fwnode_handle(args.np));
1269 if (IS_ERR(chip)) {
1270 if (PTR_ERR(chip) != -EPROBE_DEFER)
1271 pr_err("%s(): PWM chip not found\n", __func__);
1273 pwm = ERR_CAST(chip);
1274 goto put;
1277 pwm = chip->of_xlate(chip, &args);
1278 if (IS_ERR(pwm))
1279 goto put;
1281 dl = pwm_device_link_add(dev, pwm);
1282 if (IS_ERR(dl)) {
1283 /* of_xlate ended up calling pwm_request_from_chip() */
1284 pwm_put(pwm);
1285 pwm = ERR_CAST(dl);
1286 goto put;
1290 * If a consumer name was not given, try to look it up from the
1291 * "pwm-names" property if it exists. Otherwise use the name of
1292 * the user device node.
1294 if (!con_id) {
1295 err = of_property_read_string_index(np, "pwm-names", index,
1296 &con_id);
1297 if (err < 0)
1298 con_id = np->name;
1301 pwm->label = con_id;
1303 put:
1304 of_node_put(args.np);
1306 return pwm;
1310 * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
1311 * @fwnode: firmware node to get the "pwms" property from
1313 * Returns the PWM device parsed from the fwnode and index specified in the
1314 * "pwms" property or a negative error-code on failure.
1315 * Values parsed from the device tree are stored in the returned PWM device
1316 * object.
1318 * This is analogous to of_pwm_get() except con_id is not yet supported.
1319 * ACPI entries must look like
1320 * Package () {"pwms", Package ()
1321 * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
1323 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1324 * error code on failure.
1326 static struct pwm_device *acpi_pwm_get(const struct fwnode_handle *fwnode)
1328 struct pwm_device *pwm;
1329 struct fwnode_reference_args args;
1330 struct pwm_chip *chip;
1331 int ret;
1333 memset(&args, 0, sizeof(args));
1335 ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args);
1336 if (ret < 0)
1337 return ERR_PTR(ret);
1339 if (args.nargs < 2)
1340 return ERR_PTR(-EPROTO);
1342 chip = fwnode_to_pwmchip(args.fwnode);
1343 if (IS_ERR(chip))
1344 return ERR_CAST(chip);
1346 pwm = pwm_request_from_chip(chip, args.args[0], NULL);
1347 if (IS_ERR(pwm))
1348 return pwm;
1350 pwm->args.period = args.args[1];
1351 pwm->args.polarity = PWM_POLARITY_NORMAL;
1353 if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED)
1354 pwm->args.polarity = PWM_POLARITY_INVERSED;
1356 return pwm;
1359 static DEFINE_MUTEX(pwm_lookup_lock);
1360 static LIST_HEAD(pwm_lookup_list);
1363 * pwm_add_table() - register PWM device consumers
1364 * @table: array of consumers to register
1365 * @num: number of consumers in table
1367 void pwm_add_table(struct pwm_lookup *table, size_t num)
1369 mutex_lock(&pwm_lookup_lock);
1371 while (num--) {
1372 list_add_tail(&table->list, &pwm_lookup_list);
1373 table++;
1376 mutex_unlock(&pwm_lookup_lock);
1380 * pwm_remove_table() - unregister PWM device consumers
1381 * @table: array of consumers to unregister
1382 * @num: number of consumers in table
1384 void pwm_remove_table(struct pwm_lookup *table, size_t num)
1386 mutex_lock(&pwm_lookup_lock);
1388 while (num--) {
1389 list_del(&table->list);
1390 table++;
1393 mutex_unlock(&pwm_lookup_lock);
1397 * pwm_get() - look up and request a PWM device
1398 * @dev: device for PWM consumer
1399 * @con_id: consumer name
1401 * Lookup is first attempted using DT. If the device was not instantiated from
1402 * a device tree, a PWM chip and a relative index is looked up via a table
1403 * supplied by board setup code (see pwm_add_table()).
1405 * Once a PWM chip has been found the specified PWM device will be requested
1406 * and is ready to be used.
1408 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1409 * error code on failure.
1411 struct pwm_device *pwm_get(struct device *dev, const char *con_id)
1413 const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
1414 const char *dev_id = dev ? dev_name(dev) : NULL;
1415 struct pwm_device *pwm;
1416 struct pwm_chip *chip;
1417 struct device_link *dl;
1418 unsigned int best = 0;
1419 struct pwm_lookup *p, *chosen = NULL;
1420 unsigned int match;
1421 int err;
1423 /* look up via DT first */
1424 if (is_of_node(fwnode))
1425 return of_pwm_get(dev, to_of_node(fwnode), con_id);
1427 /* then lookup via ACPI */
1428 if (is_acpi_node(fwnode)) {
1429 pwm = acpi_pwm_get(fwnode);
1430 if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT)
1431 return pwm;
1435 * We look up the provider in the static table typically provided by
1436 * board setup code. We first try to lookup the consumer device by
1437 * name. If the consumer device was passed in as NULL or if no match
1438 * was found, we try to find the consumer by directly looking it up
1439 * by name.
1441 * If a match is found, the provider PWM chip is looked up by name
1442 * and a PWM device is requested using the PWM device per-chip index.
1444 * The lookup algorithm was shamelessly taken from the clock
1445 * framework:
1447 * We do slightly fuzzy matching here:
1448 * An entry with a NULL ID is assumed to be a wildcard.
1449 * If an entry has a device ID, it must match
1450 * If an entry has a connection ID, it must match
1451 * Then we take the most specific entry - with the following order
1452 * of precedence: dev+con > dev only > con only.
1454 mutex_lock(&pwm_lookup_lock);
1456 list_for_each_entry(p, &pwm_lookup_list, list) {
1457 match = 0;
1459 if (p->dev_id) {
1460 if (!dev_id || strcmp(p->dev_id, dev_id))
1461 continue;
1463 match += 2;
1466 if (p->con_id) {
1467 if (!con_id || strcmp(p->con_id, con_id))
1468 continue;
1470 match += 1;
1473 if (match > best) {
1474 chosen = p;
1476 if (match != 3)
1477 best = match;
1478 else
1479 break;
1483 mutex_unlock(&pwm_lookup_lock);
1485 if (!chosen)
1486 return ERR_PTR(-ENODEV);
1488 chip = pwmchip_find_by_name(chosen->provider);
1491 * If the lookup entry specifies a module, load the module and retry
1492 * the PWM chip lookup. This can be used to work around driver load
1493 * ordering issues if driver's can't be made to properly support the
1494 * deferred probe mechanism.
1496 if (!chip && chosen->module) {
1497 err = request_module(chosen->module);
1498 if (err == 0)
1499 chip = pwmchip_find_by_name(chosen->provider);
1502 if (!chip)
1503 return ERR_PTR(-EPROBE_DEFER);
1505 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
1506 if (IS_ERR(pwm))
1507 return pwm;
1509 dl = pwm_device_link_add(dev, pwm);
1510 if (IS_ERR(dl)) {
1511 pwm_put(pwm);
1512 return ERR_CAST(dl);
1515 pwm->args.period = chosen->period;
1516 pwm->args.polarity = chosen->polarity;
1518 return pwm;
1520 EXPORT_SYMBOL_GPL(pwm_get);
1523 * pwm_put() - release a PWM device
1524 * @pwm: PWM device
1526 void pwm_put(struct pwm_device *pwm)
1528 struct pwm_chip *chip;
1530 if (!pwm)
1531 return;
1533 chip = pwm->chip;
1535 mutex_lock(&pwm_lock);
1537 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
1538 pr_warn("PWM device already freed\n");
1539 goto out;
1542 if (chip->ops->free)
1543 pwm->chip->ops->free(pwm->chip, pwm);
1545 pwm->label = NULL;
1547 put_device(&chip->dev);
1549 module_put(chip->owner);
1550 out:
1551 mutex_unlock(&pwm_lock);
1553 EXPORT_SYMBOL_GPL(pwm_put);
1555 static void devm_pwm_release(void *pwm)
1557 pwm_put(pwm);
1561 * devm_pwm_get() - resource managed pwm_get()
1562 * @dev: device for PWM consumer
1563 * @con_id: consumer name
1565 * This function performs like pwm_get() but the acquired PWM device will
1566 * automatically be released on driver detach.
1568 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1569 * error code on failure.
1571 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
1573 struct pwm_device *pwm;
1574 int ret;
1576 pwm = pwm_get(dev, con_id);
1577 if (IS_ERR(pwm))
1578 return pwm;
1580 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
1581 if (ret)
1582 return ERR_PTR(ret);
1584 return pwm;
1586 EXPORT_SYMBOL_GPL(devm_pwm_get);
1589 * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
1590 * @dev: device for PWM consumer
1591 * @fwnode: firmware node to get the PWM from
1592 * @con_id: consumer name
1594 * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
1595 * acpi_pwm_get() for a detailed description.
1597 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1598 * error code on failure.
1600 struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
1601 struct fwnode_handle *fwnode,
1602 const char *con_id)
1604 struct pwm_device *pwm = ERR_PTR(-ENODEV);
1605 int ret;
1607 if (is_of_node(fwnode))
1608 pwm = of_pwm_get(dev, to_of_node(fwnode), con_id);
1609 else if (is_acpi_node(fwnode))
1610 pwm = acpi_pwm_get(fwnode);
1611 if (IS_ERR(pwm))
1612 return pwm;
1614 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
1615 if (ret)
1616 return ERR_PTR(ret);
1618 return pwm;
1620 EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get);
1622 static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
1624 unsigned int i;
1626 for (i = 0; i < chip->npwm; i++) {
1627 struct pwm_device *pwm = &chip->pwms[i];
1628 struct pwm_state state;
1630 pwm_get_state(pwm, &state);
1632 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
1634 if (test_bit(PWMF_REQUESTED, &pwm->flags))
1635 seq_puts(s, " requested");
1637 if (state.enabled)
1638 seq_puts(s, " enabled");
1640 seq_printf(s, " period: %llu ns", state.period);
1641 seq_printf(s, " duty: %llu ns", state.duty_cycle);
1642 seq_printf(s, " polarity: %s",
1643 state.polarity ? "inverse" : "normal");
1645 if (state.usage_power)
1646 seq_puts(s, " usage_power");
1648 seq_puts(s, "\n");
1652 static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
1654 unsigned long id = *pos;
1655 void *ret;
1657 mutex_lock(&pwm_lock);
1658 s->private = "";
1660 ret = idr_get_next_ul(&pwm_chips, &id);
1661 *pos = id;
1662 return ret;
1665 static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
1667 unsigned long id = *pos + 1;
1668 void *ret;
1670 s->private = "\n";
1672 ret = idr_get_next_ul(&pwm_chips, &id);
1673 *pos = id;
1674 return ret;
1677 static void pwm_seq_stop(struct seq_file *s, void *v)
1679 mutex_unlock(&pwm_lock);
1682 static int pwm_seq_show(struct seq_file *s, void *v)
1684 struct pwm_chip *chip = v;
1686 seq_printf(s, "%s%d: %s/%s, %d PWM device%s\n",
1687 (char *)s->private, chip->id,
1688 pwmchip_parent(chip)->bus ? pwmchip_parent(chip)->bus->name : "no-bus",
1689 dev_name(pwmchip_parent(chip)), chip->npwm,
1690 (chip->npwm != 1) ? "s" : "");
1692 pwm_dbg_show(chip, s);
1694 return 0;
1697 static const struct seq_operations pwm_debugfs_sops = {
1698 .start = pwm_seq_start,
1699 .next = pwm_seq_next,
1700 .stop = pwm_seq_stop,
1701 .show = pwm_seq_show,
1704 DEFINE_SEQ_ATTRIBUTE(pwm_debugfs);
1706 static int __init pwm_init(void)
1708 if (IS_ENABLED(CONFIG_DEBUG_FS))
1709 debugfs_create_file("pwm", 0444, NULL, NULL, &pwm_debugfs_fops);
1711 return class_register(&pwm_class);
1713 subsys_initcall(pwm_init);