1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Generic pwmlib implementation
5 * Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de>
6 * Copyright (C) 2011-2012 Avionic Design GmbH
9 #include <linux/acpi.h>
10 #include <linux/module.h>
11 #include <linux/idr.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 };
40 if (!IS_ENABLED(CONFIG_PWM_DEBUG
))
43 /* No reasonable diagnosis possible without .get_state() */
44 if (!chip
->ops
->get_state
)
48 * *state was just applied. Read out the hardware state and do some
52 err
= chip
->ops
->get_state(chip
, pwm
, &s1
);
53 trace_pwm_get(pwm
, &s1
, err
);
55 /* If that failed there isn't much to debug */
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
;
72 if (s2
.polarity
!= state
->polarity
&&
73 state
->duty_cycle
< state
->period
)
74 dev_warn(pwmchip_parent(chip
), ".apply ignored .polarity\n");
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
);
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
);
115 dev_err(pwmchip_parent(chip
), "failed to reapply current setting\n");
119 *last
= (struct pwm_state
){ 0 };
120 err
= chip
->ops
->get_state(chip
, pwm
, last
);
121 trace_pwm_get(pwm
, last
, err
);
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
,
139 * __pwm_apply() - atomically apply a new state to a 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
;
148 if (!pwm
|| !state
|| !state
->period
||
149 state
->duty_cycle
> state
->period
)
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
)
161 err
= chip
->ops
->apply(chip
, pwm
, state
);
162 trace_pwm_apply(pwm
, state
, err
);
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
);
178 * pwm_apply_might_sleep() - atomically apply a new state to a PWM device
179 * Cannot be used in atomic context.
181 * @state: new state to apply
183 int pwm_apply_might_sleep(struct pwm_device
*pwm
, const struct pwm_state
*state
)
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
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.
202 err
= __pwm_apply(pwm
, state
);
205 err
= __pwm_apply(pwm
, state
);
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().
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
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
248 * In either case, we setup the new period and polarity, and assign a
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
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
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
)
296 if (!pwm
|| !pwm
->chip
->ops
)
299 if (!pwm
->chip
->ops
->capture
)
302 mutex_lock(&pwm_lock
);
303 err
= pwm
->chip
->ops
->capture(pwm
->chip
, pwm
, result
, timeout
);
304 mutex_unlock(&pwm_lock
);
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
;
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
);
329 mutex_unlock(&pwm_lock
);
334 static int pwm_device_request(struct pwm_device
*pwm
, const char *label
)
337 struct pwm_chip
*chip
= pwm
->chip
;
338 const struct pwm_ops
*ops
= chip
->ops
;
340 if (test_bit(PWMF_REQUESTED
, &pwm
->flags
))
343 if (!try_module_get(chip
->owner
))
346 if (!get_device(&chip
->dev
)) {
352 err
= ops
->request(chip
, pwm
);
354 put_device(&chip
->dev
);
356 module_put(chip
->owner
);
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
);
377 if (IS_ENABLED(CONFIG_PWM_DEBUG
))
378 pwm
->last
= pwm
->state
;
381 set_bit(PWMF_REQUESTED
, &pwm
->flags
);
388 * pwm_request_from_chip() - request a PWM device relative to a 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
,
401 struct pwm_device
*pwm
;
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
);
414 mutex_unlock(&pwm_lock
);
417 EXPORT_SYMBOL_GPL(pwm_request_from_chip
);
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
);
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
;
442 EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags
);
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
);
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
;
462 EXPORT_SYMBOL_GPL(of_pwm_single_xlate
);
465 struct device pwm_dev
;
466 struct pwm_device
*pwm
;
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
);
488 static ssize_t
period_show(struct device
*pwm_dev
,
489 struct device_attribute
*attr
,
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
;
510 ret
= kstrtou64(buf
, 0, &val
);
514 mutex_lock(&export
->lock
);
515 pwm_get_state(pwm
, &state
);
517 ret
= pwm_apply_might_sleep(pwm
, &state
);
518 mutex_unlock(&export
->lock
);
523 static ssize_t
duty_cycle_show(struct device
*pwm_dev
,
524 struct device_attribute
*attr
,
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
;
545 ret
= kstrtou64(buf
, 0, &val
);
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
);
558 static ssize_t
enable_show(struct device
*pwm_dev
,
559 struct device_attribute
*attr
,
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
;
579 ret
= kstrtoint(buf
, 0, &val
);
583 mutex_lock(&export
->lock
);
585 pwm_get_state(pwm
, &state
);
589 state
.enabled
= false;
592 state
.enabled
= true;
599 ret
= pwm_apply_might_sleep(pwm
, &state
);
602 mutex_unlock(&export
->lock
);
606 static ssize_t
polarity_show(struct device
*pwm_dev
,
607 struct device_attribute
*attr
,
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
:
621 case PWM_POLARITY_INVERSED
:
622 polarity
= "inversed";
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
;
639 if (sysfs_streq(buf
, "normal"))
640 polarity
= PWM_POLARITY_NORMAL
;
641 else if (sysfs_streq(buf
, "inversed"))
642 polarity
= PWM_POLARITY_INVERSED
;
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
);
655 static ssize_t
capture_show(struct device
*pwm_dev
,
656 struct device_attribute
*attr
,
659 struct pwm_device
*pwm
= pwm_from_dev(pwm_dev
);
660 struct pwm_capture result
;
663 ret
= pwm_capture(pwm
, &result
, jiffies_to_msecs(HZ
));
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
,
684 ATTRIBUTE_GROUPS(pwm
);
686 static void pwm_export_release(struct device
*pwm_dev
)
688 struct pwm_export
*export
= pwmexport_from_dev(pwm_dev
);
693 static int pwm_export_child(struct device
*pwmchip_dev
, struct pwm_device
*pwm
)
695 struct pwm_export
*export
;
699 if (test_and_set_bit(PWMF_EXPORTED
, &pwm
->flags
))
702 export
= kzalloc(sizeof(*export
), GFP_KERNEL
);
704 clear_bit(PWMF_EXPORTED
, &pwm
->flags
);
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
);
719 clear_bit(PWMF_EXPORTED
, &pwm
->flags
);
720 put_device(&export
->pwm_dev
);
724 pwm_prop
[0] = kasprintf(GFP_KERNEL
, "EXPORT=pwm%u", pwm
->hwpwm
);
726 kobject_uevent_env(&pwmchip_dev
->kobj
, KOBJ_CHANGE
, pwm_prop
);
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
;
742 if (!test_and_clear_bit(PWMF_EXPORTED
, &pwm
->flags
))
745 pwm_dev
= device_find_child(pwmchip_dev
, pwm
, pwm_unexport_match
);
749 pwm_prop
[0] = kasprintf(GFP_KERNEL
, "UNEXPORT=pwm%u", pwm
->hwpwm
);
751 kobject_uevent_env(&pwmchip_dev
->kobj
, KOBJ_CHANGE
, pwm_prop
);
754 /* for device_find_child() */
756 device_unregister(pwm_dev
);
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
;
771 ret
= kstrtouint(buf
, 0, &hwpwm
);
775 if (hwpwm
>= chip
->npwm
)
778 pwm
= pwm_request_from_chip(chip
, hwpwm
, "sysfs");
782 ret
= pwm_export_child(pwmchip_dev
, pwm
);
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
);
798 ret
= kstrtouint(buf
, 0, &hwpwm
);
802 if (hwpwm
>= chip
->npwm
)
805 ret
= pwm_unexport_child(pwmchip_dev
, &chip
->pwms
[hwpwm
]);
809 static DEVICE_ATTR_WO(unexport
);
811 static ssize_t
npwm_show(struct device
*pwmchip_dev
, struct device_attribute
*attr
,
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
,
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
))
839 pwm_dev
= device_find_child(pwmchip_dev
, pwm
, pwm_unexport_match
);
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
);
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
);
864 static int pwm_class_resume_npwm(struct device
*pwmchip_dev
, unsigned int npwm
)
866 struct pwm_chip
*chip
= pwmchip_from_dev(pwmchip_dev
);
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
);
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
);
886 state
.enabled
= export
->suspend
.enabled
;
887 ret
= pwm_class_apply_state(export
, pwm
, &state
);
895 static int pwm_class_suspend(struct device
*pwmchip_dev
)
897 struct pwm_chip
*chip
= pwmchip_from_dev(pwmchip_dev
);
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
);
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
);
921 state
.enabled
= false;
922 ret
= pwm_class_apply_state(export
, pwm
, &state
);
925 * roll back the PWM devices that were disabled by
926 * this suspend function.
928 pwm_class_resume_npwm(pwmchip_dev
, i
);
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
= {
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
)
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
);
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
;
991 alloc_size
= size_add(ALIGN(struct_size(chip
, pwms
, npwm
), PWMCHIP_ALIGN
),
994 chip
= kzalloc(alloc_size
, GFP_KERNEL
);
996 return ERR_PTR(-ENOMEM
);
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
];
1017 EXPORT_SYMBOL_GPL(pwmchip_alloc
);
1019 static void devm_pwmchip_put(void *data
)
1021 struct pwm_chip
*chip
= data
;
1026 struct pwm_chip
*devm_pwmchip_alloc(struct device
*parent
, unsigned int npwm
, size_t sizeof_priv
)
1028 struct pwm_chip
*chip
;
1031 chip
= pwmchip_alloc(parent
, npwm
, sizeof_priv
);
1035 ret
= devm_add_action_or_reset(parent
, devm_pwmchip_put
, chip
);
1037 return ERR_PTR(ret
);
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
)
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
;
1067 if (IS_ENABLED(CONFIG_PWM_DEBUG
) && !ops
->get_state
)
1068 dev_warn(pwmchip_parent(chip
),
1069 "Please implement the .get_state() callback\n");
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
)
1088 if (!chip
|| !pwmchip_parent(chip
) || !chip
->ops
|| !chip
->npwm
)
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
)
1100 if (!pwm_ops_check(chip
))
1103 chip
->owner
= owner
;
1105 mutex_lock(&pwm_lock
);
1107 ret
= idr_alloc(&pwm_chips
, chip
, 0, 0, GFP_KERNEL
);
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
);
1120 goto err_device_add
;
1122 mutex_unlock(&pwm_lock
);
1127 if (IS_ENABLED(CONFIG_OF
))
1128 of_pwmchip_remove(chip
);
1130 idr_remove(&pwm_chips
, chip
->id
);
1133 mutex_unlock(&pwm_lock
);
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
)
1173 ret
= __pwmchip_add(chip
, owner
);
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
;
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");
1197 dl
= device_link_add(dev
, pwmchip_parent(pwm
->chip
), DL_FLAG_AUTOREMOVE_CONSUMER
);
1199 dev_err(dev
, "failed to create device link to %s\n",
1200 dev_name(pwmchip_parent(pwm
->chip
)));
1201 return ERR_PTR(-EINVAL
);
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
);
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
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
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
,
1248 struct pwm_device
*pwm
= NULL
;
1249 struct of_phandle_args args
;
1250 struct device_link
*dl
;
1251 struct pwm_chip
*chip
;
1256 index
= of_property_match_string(np
, "pwm-names", con_id
);
1258 return ERR_PTR(index
);
1261 err
= of_parse_phandle_with_args(np
, "pwms", "#pwm-cells", index
,
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
));
1270 if (PTR_ERR(chip
) != -EPROBE_DEFER
)
1271 pr_err("%s(): PWM chip not found\n", __func__
);
1273 pwm
= ERR_CAST(chip
);
1277 pwm
= chip
->of_xlate(chip
, &args
);
1281 dl
= pwm_device_link_add(dev
, pwm
);
1283 /* of_xlate ended up calling pwm_request_from_chip() */
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.
1295 err
= of_property_read_string_index(np
, "pwm-names", index
,
1301 pwm
->label
= con_id
;
1304 of_node_put(args
.np
);
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
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
;
1333 memset(&args
, 0, sizeof(args
));
1335 ret
= __acpi_node_get_property_reference(fwnode
, "pwms", 0, 3, &args
);
1337 return ERR_PTR(ret
);
1340 return ERR_PTR(-EPROTO
);
1342 chip
= fwnode_to_pwmchip(args
.fwnode
);
1344 return ERR_CAST(chip
);
1346 pwm
= pwm_request_from_chip(chip
, args
.args
[0], NULL
);
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
;
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
);
1372 list_add_tail(&table
->list
, &pwm_lookup_list
);
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
);
1389 list_del(&table
->list
);
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
;
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
)
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
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
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
) {
1460 if (!dev_id
|| strcmp(p
->dev_id
, dev_id
))
1467 if (!con_id
|| strcmp(p
->con_id
, con_id
))
1483 mutex_unlock(&pwm_lookup_lock
);
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
);
1499 chip
= pwmchip_find_by_name(chosen
->provider
);
1503 return ERR_PTR(-EPROBE_DEFER
);
1505 pwm
= pwm_request_from_chip(chip
, chosen
->index
, con_id
?: dev_id
);
1509 dl
= pwm_device_link_add(dev
, pwm
);
1512 return ERR_CAST(dl
);
1515 pwm
->args
.period
= chosen
->period
;
1516 pwm
->args
.polarity
= chosen
->polarity
;
1520 EXPORT_SYMBOL_GPL(pwm_get
);
1523 * pwm_put() - release a PWM device
1526 void pwm_put(struct pwm_device
*pwm
)
1528 struct pwm_chip
*chip
;
1535 mutex_lock(&pwm_lock
);
1537 if (!test_and_clear_bit(PWMF_REQUESTED
, &pwm
->flags
)) {
1538 pr_warn("PWM device already freed\n");
1542 if (chip
->ops
->free
)
1543 pwm
->chip
->ops
->free(pwm
->chip
, pwm
);
1547 put_device(&chip
->dev
);
1549 module_put(chip
->owner
);
1551 mutex_unlock(&pwm_lock
);
1553 EXPORT_SYMBOL_GPL(pwm_put
);
1555 static void devm_pwm_release(void *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
;
1576 pwm
= pwm_get(dev
, con_id
);
1580 ret
= devm_add_action_or_reset(dev
, devm_pwm_release
, pwm
);
1582 return ERR_PTR(ret
);
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
,
1604 struct pwm_device
*pwm
= ERR_PTR(-ENODEV
);
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
);
1614 ret
= devm_add_action_or_reset(dev
, devm_pwm_release
, pwm
);
1616 return ERR_PTR(ret
);
1620 EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get
);
1622 static void pwm_dbg_show(struct pwm_chip
*chip
, struct seq_file
*s
)
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");
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");
1652 static void *pwm_seq_start(struct seq_file
*s
, loff_t
*pos
)
1654 unsigned long id
= *pos
;
1657 mutex_lock(&pwm_lock
);
1660 ret
= idr_get_next_ul(&pwm_chips
, &id
);
1665 static void *pwm_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
1667 unsigned long id
= *pos
+ 1;
1672 ret
= idr_get_next_ul(&pwm_chips
, &id
);
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
);
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
);