1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Generic pwmlib implementation
6 * Copyright (C) 2011-2012 Avionic Design GmbH
9 #define DEFAULT_SYMBOL_NAMESPACE PWM
11 #include <linux/acpi.h>
12 #include <linux/module.h>
13 #include <linux/idr.h>
15 #include <linux/pwm.h>
16 #include <linux/list.h>
17 #include <linux/mutex.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
20 #include <linux/device.h>
21 #include <linux/debugfs.h>
22 #include <linux/seq_file.h>
24 #include <dt-bindings/pwm/pwm.h>
26 #define CREATE_TRACE_POINTS
27 #include <trace/events/pwm.h>
29 /* protects access to pwm_chips */
30 static DEFINE_MUTEX(pwm_lock);
32 static DEFINE_IDR(pwm_chips);
34 static void pwm_apply_debug(struct pwm_device *pwm,
35 const struct pwm_state *state)
37 struct pwm_state *last = &pwm->last;
38 struct pwm_chip *chip = pwm->chip;
39 struct pwm_state s1 = { 0 }, s2 = { 0 };
42 if (!IS_ENABLED(CONFIG_PWM_DEBUG))
45 /* No reasonable diagnosis possible without .get_state() */
46 if (!chip->ops->get_state)
50 * *state was just applied. Read out the hardware state and do some
54 err = chip->ops->get_state(chip, pwm, &s1);
55 trace_pwm_get(pwm, &s1, err);
57 /* If that failed there isn't much to debug */
61 * The lowlevel driver either ignored .polarity (which is a bug) or as
62 * best effort inverted .polarity and fixed .duty_cycle respectively.
63 * Undo this inversion and fixup for further tests.
65 if (s1.enabled && s1.polarity != state->polarity) {
66 s2.polarity = state->polarity;
67 s2.duty_cycle = s1.period - s1.duty_cycle;
68 s2.period = s1.period;
69 s2.enabled = s1.enabled;
74 if (s2.polarity != state->polarity &&
75 state->duty_cycle < state->period)
76 dev_warn(pwmchip_parent(chip), ".apply ignored .polarity\n");
79 last->polarity == state->polarity &&
80 last->period > s2.period &&
81 last->period <= state->period)
82 dev_warn(pwmchip_parent(chip),
83 ".apply didn't pick the best available period (requested: %llu, applied: %llu, possible: %llu)\n",
84 state->period, s2.period, last->period);
86 if (state->enabled && state->period < s2.period)
87 dev_warn(pwmchip_parent(chip),
88 ".apply is supposed to round down period (requested: %llu, applied: %llu)\n",
89 state->period, s2.period);
92 last->polarity == state->polarity &&
93 last->period == s2.period &&
94 last->duty_cycle > s2.duty_cycle &&
95 last->duty_cycle <= state->duty_cycle)
96 dev_warn(pwmchip_parent(chip),
97 ".apply didn't pick the best available duty cycle (requested: %llu/%llu, applied: %llu/%llu, possible: %llu/%llu)\n",
98 state->duty_cycle, state->period,
99 s2.duty_cycle, s2.period,
100 last->duty_cycle, last->period);
102 if (state->enabled && state->duty_cycle < s2.duty_cycle)
103 dev_warn(pwmchip_parent(chip),
104 ".apply is supposed to round down duty_cycle (requested: %llu/%llu, applied: %llu/%llu)\n",
105 state->duty_cycle, state->period,
106 s2.duty_cycle, s2.period);
108 if (!state->enabled && s2.enabled && s2.duty_cycle > 0)
109 dev_warn(pwmchip_parent(chip),
110 "requested disabled, but yielded enabled with duty > 0\n");
112 /* reapply the state that the driver reported being configured. */
113 err = chip->ops->apply(chip, pwm, &s1);
114 trace_pwm_apply(pwm, &s1, err);
117 dev_err(pwmchip_parent(chip), "failed to reapply current setting\n");
121 *last = (struct pwm_state){ 0 };
122 err = chip->ops->get_state(chip, pwm, last);
123 trace_pwm_get(pwm, last, err);
127 /* reapplication of the current state should give an exact match */
128 if (s1.enabled != last->enabled ||
129 s1.polarity != last->polarity ||
130 (s1.enabled && s1.period != last->period) ||
131 (s1.enabled && s1.duty_cycle != last->duty_cycle)) {
132 dev_err(pwmchip_parent(chip),
133 ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n",
134 s1.enabled, s1.polarity, s1.duty_cycle, s1.period,
135 last->enabled, last->polarity, last->duty_cycle,
140 static bool pwm_state_valid(const struct pwm_state *state)
143 * For a disabled state all other state description is irrelevant and
144 * and supposed to be ignored. So also ignore any strange values and
145 * consider the state ok.
153 if (state->duty_cycle > state->period)
160 * __pwm_apply() - atomically apply a new state to a PWM device
162 * @state: new state to apply
164 static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state)
166 struct pwm_chip *chip;
172 if (!pwm_state_valid(state)) {
174 * Allow to transition from one invalid state to another.
175 * This ensures that you can e.g. change the polarity while
176 * the period is zero. (This happens on stm32 when the hardware
177 * is in its poweron default state.) This greatly simplifies
178 * working with the sysfs API where you can only change one
179 * parameter at a time.
181 if (!pwm_state_valid(&pwm->state)) {
191 if (state->period == pwm->state.period &&
192 state->duty_cycle == pwm->state.duty_cycle &&
193 state->polarity == pwm->state.polarity &&
194 state->enabled == pwm->state.enabled &&
195 state->usage_power == pwm->state.usage_power)
198 err = chip->ops->apply(chip, pwm, state);
199 trace_pwm_apply(pwm, state, err);
206 * only do this after pwm->state was applied as some
207 * implementations of .get_state depend on this
209 pwm_apply_debug(pwm, state);
215 * pwm_apply_might_sleep() - atomically apply a new state to a PWM device
216 * Cannot be used in atomic context.
218 * @state: new state to apply
220 int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state)
225 * Some lowlevel driver's implementations of .apply() make use of
226 * mutexes, also with some drivers only returning when the new
227 * configuration is active calling pwm_apply_might_sleep() from atomic context
228 * is a bad idea. So make it explicit that calling this function might
233 if (IS_ENABLED(CONFIG_PWM_DEBUG) && pwm->chip->atomic) {
235 * Catch any drivers that have been marked as atomic but
236 * that will sleep anyway.
239 err = __pwm_apply(pwm, state);
242 err = __pwm_apply(pwm, state);
247 EXPORT_SYMBOL_GPL(pwm_apply_might_sleep);
250 * pwm_apply_atomic() - apply a new state to a PWM device from atomic context
251 * Not all PWM devices support this function, check with pwm_might_sleep().
253 * @state: new state to apply
255 int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state)
257 WARN_ONCE(!pwm->chip->atomic,
258 "sleeping PWM driver used in atomic context\n");
260 return __pwm_apply(pwm, state);
262 EXPORT_SYMBOL_GPL(pwm_apply_atomic);
265 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
268 * This function will adjust the PWM config to the PWM arguments provided
269 * by the DT or PWM lookup table. This is particularly useful to adapt
270 * the bootloader config to the Linux one.
272 int pwm_adjust_config(struct pwm_device *pwm)
274 struct pwm_state state;
275 struct pwm_args pargs;
277 pwm_get_args(pwm, &pargs);
278 pwm_get_state(pwm, &state);
281 * If the current period is zero it means that either the PWM driver
282 * does not support initial state retrieval or the PWM has not yet
285 * In either case, we setup the new period and polarity, and assign a
289 state.duty_cycle = 0;
290 state.period = pargs.period;
291 state.polarity = pargs.polarity;
293 return pwm_apply_might_sleep(pwm, &state);
297 * Adjust the PWM duty cycle/period based on the period value provided
300 if (pargs.period != state.period) {
301 u64 dutycycle = (u64)state.duty_cycle * pargs.period;
303 do_div(dutycycle, state.period);
304 state.duty_cycle = dutycycle;
305 state.period = pargs.period;
309 * If the polarity changed, we should also change the duty cycle.
311 if (pargs.polarity != state.polarity) {
312 state.polarity = pargs.polarity;
313 state.duty_cycle = state.period - state.duty_cycle;
316 return pwm_apply_might_sleep(pwm, &state);
318 EXPORT_SYMBOL_GPL(pwm_adjust_config);
321 * pwm_capture() - capture and report a PWM signal
323 * @result: structure to fill with capture result
324 * @timeout: time to wait, in milliseconds, before giving up on capture
326 * Returns: 0 on success or a negative error code on failure.
328 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
329 unsigned long timeout)
331 if (!pwm || !pwm->chip->ops)
334 if (!pwm->chip->ops->capture)
337 guard(mutex)(&pwm_lock);
339 return pwm->chip->ops->capture(pwm->chip, pwm, result, timeout);
341 EXPORT_SYMBOL_GPL(pwm_capture);
343 static struct pwm_chip *pwmchip_find_by_name(const char *name)
345 struct pwm_chip *chip;
346 unsigned long id, tmp;
351 guard(mutex)(&pwm_lock);
353 idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) {
354 const char *chip_name = dev_name(pwmchip_parent(chip));
356 if (chip_name && strcmp(chip_name, name) == 0)
363 static int pwm_device_request(struct pwm_device *pwm, const char *label)
366 struct pwm_chip *chip = pwm->chip;
367 const struct pwm_ops *ops = chip->ops;
369 if (test_bit(PWMF_REQUESTED, &pwm->flags))
372 if (!try_module_get(chip->owner))
375 if (!get_device(&chip->dev)) {
381 err = ops->request(chip, pwm);
383 put_device(&chip->dev);
385 module_put(chip->owner);
390 if (ops->get_state) {
392 * Zero-initialize state because most drivers are unaware of
393 * .usage_power. The other members of state are supposed to be
394 * set by lowlevel drivers. We still initialize the whole
395 * structure for simplicity even though this might paper over
396 * faulty implementations of .get_state().
398 struct pwm_state state = { 0, };
400 err = ops->get_state(chip, pwm, &state);
401 trace_pwm_get(pwm, &state, err);
406 if (IS_ENABLED(CONFIG_PWM_DEBUG))
407 pwm->last = pwm->state;
410 set_bit(PWMF_REQUESTED, &pwm->flags);
417 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
419 * @index: per-chip index of the PWM to request
420 * @label: a literal description string of this PWM
422 * Returns: A pointer to the PWM device at the given index of the given PWM
423 * chip. A negative error code is returned if the index is not valid for the
424 * specified PWM chip or if the PWM device cannot be requested.
426 static struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
430 struct pwm_device *pwm;
433 if (!chip || index >= chip->npwm)
434 return ERR_PTR(-EINVAL);
436 guard(mutex)(&pwm_lock);
438 pwm = &chip->pwms[index];
440 err = pwm_device_request(pwm, label);
448 of_pwm_xlate_with_flags(struct pwm_chip *chip, const struct of_phandle_args *args)
450 struct pwm_device *pwm;
452 /* period in the second cell and flags in the third cell are optional */
453 if (args->args_count < 1)
454 return ERR_PTR(-EINVAL);
456 pwm = pwm_request_from_chip(chip, args->args[0], NULL);
460 if (args->args_count > 1)
461 pwm->args.period = args->args[1];
463 pwm->args.polarity = PWM_POLARITY_NORMAL;
464 if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED)
465 pwm->args.polarity = PWM_POLARITY_INVERSED;
469 EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
472 of_pwm_single_xlate(struct pwm_chip *chip, const struct of_phandle_args *args)
474 struct pwm_device *pwm;
476 pwm = pwm_request_from_chip(chip, 0, NULL);
480 if (args->args_count > 0)
481 pwm->args.period = args->args[0];
483 pwm->args.polarity = PWM_POLARITY_NORMAL;
484 if (args->args_count > 1 && args->args[1] & PWM_POLARITY_INVERTED)
485 pwm->args.polarity = PWM_POLARITY_INVERSED;
489 EXPORT_SYMBOL_GPL(of_pwm_single_xlate);
492 struct device pwm_dev;
493 struct pwm_device *pwm;
495 struct pwm_state suspend;
498 static inline struct pwm_chip *pwmchip_from_dev(struct device *pwmchip_dev)
500 return container_of(pwmchip_dev, struct pwm_chip, dev);
503 static inline struct pwm_export *pwmexport_from_dev(struct device *pwm_dev)
505 return container_of(pwm_dev, struct pwm_export, pwm_dev);
508 static inline struct pwm_device *pwm_from_dev(struct device *pwm_dev)
510 struct pwm_export *export = pwmexport_from_dev(pwm_dev);
515 static ssize_t period_show(struct device *pwm_dev,
516 struct device_attribute *attr,
519 const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
520 struct pwm_state state;
522 pwm_get_state(pwm, &state);
524 return sysfs_emit(buf, "%llu\n", state.period);
527 static ssize_t period_store(struct device *pwm_dev,
528 struct device_attribute *attr,
529 const char *buf, size_t size)
531 struct pwm_export *export = pwmexport_from_dev(pwm_dev);
532 struct pwm_device *pwm = export->pwm;
533 struct pwm_state state;
537 ret = kstrtou64(buf, 0, &val);
541 guard(mutex)(&export->lock);
543 pwm_get_state(pwm, &state);
545 ret = pwm_apply_might_sleep(pwm, &state);
550 static ssize_t duty_cycle_show(struct device *pwm_dev,
551 struct device_attribute *attr,
554 const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
555 struct pwm_state state;
557 pwm_get_state(pwm, &state);
559 return sysfs_emit(buf, "%llu\n", state.duty_cycle);
562 static ssize_t duty_cycle_store(struct device *pwm_dev,
563 struct device_attribute *attr,
564 const char *buf, size_t size)
566 struct pwm_export *export = pwmexport_from_dev(pwm_dev);
567 struct pwm_device *pwm = export->pwm;
568 struct pwm_state state;
572 ret = kstrtou64(buf, 0, &val);
576 guard(mutex)(&export->lock);
578 pwm_get_state(pwm, &state);
579 state.duty_cycle = val;
580 ret = pwm_apply_might_sleep(pwm, &state);
585 static ssize_t enable_show(struct device *pwm_dev,
586 struct device_attribute *attr,
589 const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
590 struct pwm_state state;
592 pwm_get_state(pwm, &state);
594 return sysfs_emit(buf, "%d\n", state.enabled);
597 static ssize_t enable_store(struct device *pwm_dev,
598 struct device_attribute *attr,
599 const char *buf, size_t size)
601 struct pwm_export *export = pwmexport_from_dev(pwm_dev);
602 struct pwm_device *pwm = export->pwm;
603 struct pwm_state state;
606 ret = kstrtoint(buf, 0, &val);
610 guard(mutex)(&export->lock);
612 pwm_get_state(pwm, &state);
616 state.enabled = false;
619 state.enabled = true;
625 ret = pwm_apply_might_sleep(pwm, &state);
630 static ssize_t polarity_show(struct device *pwm_dev,
631 struct device_attribute *attr,
634 const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
635 const char *polarity = "unknown";
636 struct pwm_state state;
638 pwm_get_state(pwm, &state);
640 switch (state.polarity) {
641 case PWM_POLARITY_NORMAL:
645 case PWM_POLARITY_INVERSED:
646 polarity = "inversed";
650 return sysfs_emit(buf, "%s\n", polarity);
653 static ssize_t polarity_store(struct device *pwm_dev,
654 struct device_attribute *attr,
655 const char *buf, size_t size)
657 struct pwm_export *export = pwmexport_from_dev(pwm_dev);
658 struct pwm_device *pwm = export->pwm;
659 enum pwm_polarity polarity;
660 struct pwm_state state;
663 if (sysfs_streq(buf, "normal"))
664 polarity = PWM_POLARITY_NORMAL;
665 else if (sysfs_streq(buf, "inversed"))
666 polarity = PWM_POLARITY_INVERSED;
670 guard(mutex)(&export->lock);
672 pwm_get_state(pwm, &state);
673 state.polarity = polarity;
674 ret = pwm_apply_might_sleep(pwm, &state);
679 static ssize_t capture_show(struct device *pwm_dev,
680 struct device_attribute *attr,
683 struct pwm_device *pwm = pwm_from_dev(pwm_dev);
684 struct pwm_capture result;
687 ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ));
691 return sysfs_emit(buf, "%u %u\n", result.period, result.duty_cycle);
694 static DEVICE_ATTR_RW(period);
695 static DEVICE_ATTR_RW(duty_cycle);
696 static DEVICE_ATTR_RW(enable);
697 static DEVICE_ATTR_RW(polarity);
698 static DEVICE_ATTR_RO(capture);
700 static struct attribute *pwm_attrs[] = {
701 &dev_attr_period.attr,
702 &dev_attr_duty_cycle.attr,
703 &dev_attr_enable.attr,
704 &dev_attr_polarity.attr,
705 &dev_attr_capture.attr,
708 ATTRIBUTE_GROUPS(pwm);
710 static void pwm_export_release(struct device *pwm_dev)
712 struct pwm_export *export = pwmexport_from_dev(pwm_dev);
717 static int pwm_export_child(struct device *pwmchip_dev, struct pwm_device *pwm)
719 struct pwm_export *export;
723 if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags))
726 export = kzalloc(sizeof(*export), GFP_KERNEL);
728 clear_bit(PWMF_EXPORTED, &pwm->flags);
733 mutex_init(&export->lock);
735 export->pwm_dev.release = pwm_export_release;
736 export->pwm_dev.parent = pwmchip_dev;
737 export->pwm_dev.devt = MKDEV(0, 0);
738 export->pwm_dev.groups = pwm_groups;
739 dev_set_name(&export->pwm_dev, "pwm%u", pwm->hwpwm);
741 ret = device_register(&export->pwm_dev);
743 clear_bit(PWMF_EXPORTED, &pwm->flags);
744 put_device(&export->pwm_dev);
748 pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm);
750 kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop);
756 static int pwm_unexport_match(struct device *pwm_dev, void *data)
758 return pwm_from_dev(pwm_dev) == data;
761 static int pwm_unexport_child(struct device *pwmchip_dev, struct pwm_device *pwm)
763 struct device *pwm_dev;
766 if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags))
769 pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match);
773 pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm);
775 kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop);
778 /* for device_find_child() */
780 device_unregister(pwm_dev);
786 static ssize_t export_store(struct device *pwmchip_dev,
787 struct device_attribute *attr,
788 const char *buf, size_t len)
790 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
791 struct pwm_device *pwm;
795 ret = kstrtouint(buf, 0, &hwpwm);
799 if (hwpwm >= chip->npwm)
802 pwm = pwm_request_from_chip(chip, hwpwm, "sysfs");
806 ret = pwm_export_child(pwmchip_dev, pwm);
812 static DEVICE_ATTR_WO(export);
814 static ssize_t unexport_store(struct device *pwmchip_dev,
815 struct device_attribute *attr,
816 const char *buf, size_t len)
818 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
822 ret = kstrtouint(buf, 0, &hwpwm);
826 if (hwpwm >= chip->npwm)
829 ret = pwm_unexport_child(pwmchip_dev, &chip->pwms[hwpwm]);
833 static DEVICE_ATTR_WO(unexport);
835 static ssize_t npwm_show(struct device *pwmchip_dev, struct device_attribute *attr,
838 const struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
840 return sysfs_emit(buf, "%u\n", chip->npwm);
842 static DEVICE_ATTR_RO(npwm);
844 static struct attribute *pwm_chip_attrs[] = {
845 &dev_attr_export.attr,
846 &dev_attr_unexport.attr,
850 ATTRIBUTE_GROUPS(pwm_chip);
852 /* takes export->lock on success */
853 static struct pwm_export *pwm_class_get_state(struct device *pwmchip_dev,
854 struct pwm_device *pwm,
855 struct pwm_state *state)
857 struct device *pwm_dev;
858 struct pwm_export *export;
860 if (!test_bit(PWMF_EXPORTED, &pwm->flags))
863 pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match);
867 export = pwmexport_from_dev(pwm_dev);
868 put_device(pwm_dev); /* for device_find_child() */
870 mutex_lock(&export->lock);
871 pwm_get_state(pwm, state);
876 static int pwm_class_apply_state(struct pwm_export *export,
877 struct pwm_device *pwm,
878 struct pwm_state *state)
880 int ret = pwm_apply_might_sleep(pwm, state);
882 /* release lock taken in pwm_class_get_state */
883 mutex_unlock(&export->lock);
888 static int pwm_class_resume_npwm(struct device *pwmchip_dev, unsigned int npwm)
890 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
894 for (i = 0; i < npwm; i++) {
895 struct pwm_device *pwm = &chip->pwms[i];
896 struct pwm_state state;
897 struct pwm_export *export;
899 export = pwm_class_get_state(pwmchip_dev, pwm, &state);
903 /* If pwmchip was not enabled before suspend, do nothing. */
904 if (!export->suspend.enabled) {
905 /* release lock taken in pwm_class_get_state */
906 mutex_unlock(&export->lock);
910 state.enabled = export->suspend.enabled;
911 ret = pwm_class_apply_state(export, pwm, &state);
919 static int pwm_class_suspend(struct device *pwmchip_dev)
921 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
925 for (i = 0; i < chip->npwm; i++) {
926 struct pwm_device *pwm = &chip->pwms[i];
927 struct pwm_state state;
928 struct pwm_export *export;
930 export = pwm_class_get_state(pwmchip_dev, pwm, &state);
935 * If pwmchip was not enabled before suspend, save
936 * state for resume time and do nothing else.
938 export->suspend = state;
939 if (!state.enabled) {
940 /* release lock taken in pwm_class_get_state */
941 mutex_unlock(&export->lock);
945 state.enabled = false;
946 ret = pwm_class_apply_state(export, pwm, &state);
949 * roll back the PWM devices that were disabled by
950 * this suspend function.
952 pwm_class_resume_npwm(pwmchip_dev, i);
960 static int pwm_class_resume(struct device *pwmchip_dev)
962 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
964 return pwm_class_resume_npwm(pwmchip_dev, chip->npwm);
967 static DEFINE_SIMPLE_DEV_PM_OPS(pwm_class_pm_ops, pwm_class_suspend, pwm_class_resume);
969 static struct class pwm_class = {
971 .dev_groups = pwm_chip_groups,
972 .pm = pm_sleep_ptr(&pwm_class_pm_ops),
975 static void pwmchip_sysfs_unexport(struct pwm_chip *chip)
979 for (i = 0; i < chip->npwm; i++) {
980 struct pwm_device *pwm = &chip->pwms[i];
982 if (test_bit(PWMF_EXPORTED, &pwm->flags))
983 pwm_unexport_child(&chip->dev, pwm);
987 #define PWMCHIP_ALIGN ARCH_DMA_MINALIGN
989 static void *pwmchip_priv(struct pwm_chip *chip)
991 return (void *)chip + ALIGN(struct_size(chip, pwms, chip->npwm), PWMCHIP_ALIGN);
994 /* This is the counterpart to pwmchip_alloc() */
995 void pwmchip_put(struct pwm_chip *chip)
997 put_device(&chip->dev);
999 EXPORT_SYMBOL_GPL(pwmchip_put);
1001 static void pwmchip_release(struct device *pwmchip_dev)
1003 struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
1008 struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv)
1010 struct pwm_chip *chip;
1011 struct device *pwmchip_dev;
1015 alloc_size = size_add(ALIGN(struct_size(chip, pwms, npwm), PWMCHIP_ALIGN),
1018 chip = kzalloc(alloc_size, GFP_KERNEL);
1020 return ERR_PTR(-ENOMEM);
1023 chip->uses_pwmchip_alloc = true;
1025 pwmchip_dev = &chip->dev;
1026 device_initialize(pwmchip_dev);
1027 pwmchip_dev->class = &pwm_class;
1028 pwmchip_dev->parent = parent;
1029 pwmchip_dev->release = pwmchip_release;
1031 pwmchip_set_drvdata(chip, pwmchip_priv(chip));
1033 for (i = 0; i < chip->npwm; i++) {
1034 struct pwm_device *pwm = &chip->pwms[i];
1041 EXPORT_SYMBOL_GPL(pwmchip_alloc);
1043 static void devm_pwmchip_put(void *data)
1045 struct pwm_chip *chip = data;
1050 struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv)
1052 struct pwm_chip *chip;
1055 chip = pwmchip_alloc(parent, npwm, sizeof_priv);
1059 ret = devm_add_action_or_reset(parent, devm_pwmchip_put, chip);
1061 return ERR_PTR(ret);
1065 EXPORT_SYMBOL_GPL(devm_pwmchip_alloc);
1067 static void of_pwmchip_add(struct pwm_chip *chip)
1069 if (!pwmchip_parent(chip) || !pwmchip_parent(chip)->of_node)
1072 if (!chip->of_xlate)
1073 chip->of_xlate = of_pwm_xlate_with_flags;
1075 of_node_get(pwmchip_parent(chip)->of_node);
1078 static void of_pwmchip_remove(struct pwm_chip *chip)
1080 if (pwmchip_parent(chip))
1081 of_node_put(pwmchip_parent(chip)->of_node);
1084 static bool pwm_ops_check(const struct pwm_chip *chip)
1086 const struct pwm_ops *ops = chip->ops;
1091 if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state)
1092 dev_warn(pwmchip_parent(chip),
1093 "Please implement the .get_state() callback\n");
1099 * __pwmchip_add() - register a new PWM chip
1100 * @chip: the PWM chip to add
1101 * @owner: reference to the module providing the chip.
1103 * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the
1104 * pwmchip_add wrapper to do this right.
1106 * Returns: 0 on success or a negative error code on failure.
1108 int __pwmchip_add(struct pwm_chip *chip, struct module *owner)
1112 if (!chip || !pwmchip_parent(chip) || !chip->ops || !chip->npwm)
1116 * a struct pwm_chip must be allocated using (devm_)pwmchip_alloc,
1117 * otherwise the embedded struct device might disappear too early
1118 * resulting in memory corruption.
1119 * Catch drivers that were not converted appropriately.
1121 if (!chip->uses_pwmchip_alloc)
1124 if (!pwm_ops_check(chip))
1127 chip->owner = owner;
1129 guard(mutex)(&pwm_lock);
1131 ret = idr_alloc(&pwm_chips, chip, 0, 0, GFP_KERNEL);
1137 dev_set_name(&chip->dev, "pwmchip%u", chip->id);
1139 if (IS_ENABLED(CONFIG_OF))
1140 of_pwmchip_add(chip);
1142 ret = device_add(&chip->dev);
1144 goto err_device_add;
1149 if (IS_ENABLED(CONFIG_OF))
1150 of_pwmchip_remove(chip);
1152 idr_remove(&pwm_chips, chip->id);
1156 EXPORT_SYMBOL_GPL(__pwmchip_add);
1159 * pwmchip_remove() - remove a PWM chip
1160 * @chip: the PWM chip to remove
1162 * Removes a PWM chip.
1164 void pwmchip_remove(struct pwm_chip *chip)
1166 pwmchip_sysfs_unexport(chip);
1168 if (IS_ENABLED(CONFIG_OF))
1169 of_pwmchip_remove(chip);
1171 scoped_guard(mutex, &pwm_lock)
1172 idr_remove(&pwm_chips, chip->id);
1174 device_del(&chip->dev);
1176 EXPORT_SYMBOL_GPL(pwmchip_remove);
1178 static void devm_pwmchip_remove(void *data)
1180 struct pwm_chip *chip = data;
1182 pwmchip_remove(chip);
1185 int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner)
1189 ret = __pwmchip_add(chip, owner);
1193 return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip);
1195 EXPORT_SYMBOL_GPL(__devm_pwmchip_add);
1197 static struct device_link *pwm_device_link_add(struct device *dev,
1198 struct pwm_device *pwm)
1200 struct device_link *dl;
1204 * No device for the PWM consumer has been provided. It may
1205 * impact the PM sequence ordering: the PWM supplier may get
1206 * suspended before the consumer.
1208 dev_warn(pwmchip_parent(pwm->chip),
1209 "No consumer device specified to create a link to\n");
1213 dl = device_link_add(dev, pwmchip_parent(pwm->chip), DL_FLAG_AUTOREMOVE_CONSUMER);
1215 dev_err(dev, "failed to create device link to %s\n",
1216 dev_name(pwmchip_parent(pwm->chip)));
1217 return ERR_PTR(-EINVAL);
1223 static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode)
1225 struct pwm_chip *chip;
1226 unsigned long id, tmp;
1228 guard(mutex)(&pwm_lock);
1230 idr_for_each_entry_ul(&pwm_chips, chip, tmp, id)
1231 if (pwmchip_parent(chip) && device_match_fwnode(pwmchip_parent(chip), fwnode))
1234 return ERR_PTR(-EPROBE_DEFER);
1238 * of_pwm_get() - request a PWM via the PWM framework
1239 * @dev: device for PWM consumer
1240 * @np: device node to get the PWM from
1241 * @con_id: consumer name
1243 * Returns the PWM device parsed from the phandle and index specified in the
1244 * "pwms" property of a device tree node or a negative error-code on failure.
1245 * Values parsed from the device tree are stored in the returned PWM device
1248 * If con_id is NULL, the first PWM device listed in the "pwms" property will
1249 * be requested. Otherwise the "pwm-names" property is used to do a reverse
1250 * lookup of the PWM index. This also means that the "pwm-names" property
1251 * becomes mandatory for devices that look up the PWM device via the con_id
1254 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1255 * error code on failure.
1257 static struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
1260 struct pwm_device *pwm = NULL;
1261 struct of_phandle_args args;
1262 struct device_link *dl;
1263 struct pwm_chip *chip;
1268 index = of_property_match_string(np, "pwm-names", con_id);
1270 return ERR_PTR(index);
1273 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
1276 pr_err("%s(): can't parse \"pwms\" property\n", __func__);
1277 return ERR_PTR(err);
1280 chip = fwnode_to_pwmchip(of_fwnode_handle(args.np));
1282 if (PTR_ERR(chip) != -EPROBE_DEFER)
1283 pr_err("%s(): PWM chip not found\n", __func__);
1285 pwm = ERR_CAST(chip);
1289 pwm = chip->of_xlate(chip, &args);
1293 dl = pwm_device_link_add(dev, pwm);
1295 /* of_xlate ended up calling pwm_request_from_chip() */
1302 * If a consumer name was not given, try to look it up from the
1303 * "pwm-names" property if it exists. Otherwise use the name of
1304 * the user device node.
1307 err = of_property_read_string_index(np, "pwm-names", index,
1313 pwm->label = con_id;
1316 of_node_put(args.np);
1322 * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
1323 * @fwnode: firmware node to get the "pwms" property from
1325 * Returns the PWM device parsed from the fwnode and index specified in the
1326 * "pwms" property or a negative error-code on failure.
1327 * Values parsed from the device tree are stored in the returned PWM device
1330 * This is analogous to of_pwm_get() except con_id is not yet supported.
1331 * ACPI entries must look like
1332 * Package () {"pwms", Package ()
1333 * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
1335 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1336 * error code on failure.
1338 static struct pwm_device *acpi_pwm_get(const struct fwnode_handle *fwnode)
1340 struct pwm_device *pwm;
1341 struct fwnode_reference_args args;
1342 struct pwm_chip *chip;
1345 memset(&args, 0, sizeof(args));
1347 ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args);
1349 return ERR_PTR(ret);
1352 return ERR_PTR(-EPROTO);
1354 chip = fwnode_to_pwmchip(args.fwnode);
1356 return ERR_CAST(chip);
1358 pwm = pwm_request_from_chip(chip, args.args[0], NULL);
1362 pwm->args.period = args.args[1];
1363 pwm->args.polarity = PWM_POLARITY_NORMAL;
1365 if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED)
1366 pwm->args.polarity = PWM_POLARITY_INVERSED;
1371 static DEFINE_MUTEX(pwm_lookup_lock);
1372 static LIST_HEAD(pwm_lookup_list);
1375 * pwm_add_table() - register PWM device consumers
1376 * @table: array of consumers to register
1377 * @num: number of consumers in table
1379 void pwm_add_table(struct pwm_lookup *table, size_t num)
1381 guard(mutex)(&pwm_lookup_lock);
1384 list_add_tail(&table->list, &pwm_lookup_list);
1390 * pwm_remove_table() - unregister PWM device consumers
1391 * @table: array of consumers to unregister
1392 * @num: number of consumers in table
1394 void pwm_remove_table(struct pwm_lookup *table, size_t num)
1396 guard(mutex)(&pwm_lookup_lock);
1399 list_del(&table->list);
1405 * pwm_get() - look up and request a PWM device
1406 * @dev: device for PWM consumer
1407 * @con_id: consumer name
1409 * Lookup is first attempted using DT. If the device was not instantiated from
1410 * a device tree, a PWM chip and a relative index is looked up via a table
1411 * supplied by board setup code (see pwm_add_table()).
1413 * Once a PWM chip has been found the specified PWM device will be requested
1414 * and is ready to be used.
1416 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1417 * error code on failure.
1419 struct pwm_device *pwm_get(struct device *dev, const char *con_id)
1421 const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
1422 const char *dev_id = dev ? dev_name(dev) : NULL;
1423 struct pwm_device *pwm;
1424 struct pwm_chip *chip;
1425 struct device_link *dl;
1426 unsigned int best = 0;
1427 struct pwm_lookup *p, *chosen = NULL;
1431 /* look up via DT first */
1432 if (is_of_node(fwnode))
1433 return of_pwm_get(dev, to_of_node(fwnode), con_id);
1435 /* then lookup via ACPI */
1436 if (is_acpi_node(fwnode)) {
1437 pwm = acpi_pwm_get(fwnode);
1438 if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT)
1443 * We look up the provider in the static table typically provided by
1444 * board setup code. We first try to lookup the consumer device by
1445 * name. If the consumer device was passed in as NULL or if no match
1446 * was found, we try to find the consumer by directly looking it up
1449 * If a match is found, the provider PWM chip is looked up by name
1450 * and a PWM device is requested using the PWM device per-chip index.
1452 * The lookup algorithm was shamelessly taken from the clock
1455 * We do slightly fuzzy matching here:
1456 * An entry with a NULL ID is assumed to be a wildcard.
1457 * If an entry has a device ID, it must match
1458 * If an entry has a connection ID, it must match
1459 * Then we take the most specific entry - with the following order
1460 * of precedence: dev+con > dev only > con only.
1462 scoped_guard(mutex, &pwm_lookup_lock)
1463 list_for_each_entry(p, &pwm_lookup_list, list) {
1467 if (!dev_id || strcmp(p->dev_id, dev_id))
1474 if (!con_id || strcmp(p->con_id, con_id))
1491 return ERR_PTR(-ENODEV);
1493 chip = pwmchip_find_by_name(chosen->provider);
1496 * If the lookup entry specifies a module, load the module and retry
1497 * the PWM chip lookup. This can be used to work around driver load
1498 * ordering issues if driver's can't be made to properly support the
1499 * deferred probe mechanism.
1501 if (!chip && chosen->module) {
1502 err = request_module(chosen->module);
1504 chip = pwmchip_find_by_name(chosen->provider);
1508 return ERR_PTR(-EPROBE_DEFER);
1510 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
1514 dl = pwm_device_link_add(dev, pwm);
1517 return ERR_CAST(dl);
1520 pwm->args.period = chosen->period;
1521 pwm->args.polarity = chosen->polarity;
1525 EXPORT_SYMBOL_GPL(pwm_get);
1528 * pwm_put() - release a PWM device
1531 void pwm_put(struct pwm_device *pwm)
1533 struct pwm_chip *chip;
1540 guard(mutex)(&pwm_lock);
1542 if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
1543 pr_warn("PWM device already freed\n");
1547 if (chip->ops->free)
1548 pwm->chip->ops->free(pwm->chip, pwm);
1552 put_device(&chip->dev);
1554 module_put(chip->owner);
1556 EXPORT_SYMBOL_GPL(pwm_put);
1558 static void devm_pwm_release(void *pwm)
1564 * devm_pwm_get() - resource managed pwm_get()
1565 * @dev: device for PWM consumer
1566 * @con_id: consumer name
1568 * This function performs like pwm_get() but the acquired PWM device will
1569 * automatically be released on driver detach.
1571 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1572 * error code on failure.
1574 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
1576 struct pwm_device *pwm;
1579 pwm = pwm_get(dev, con_id);
1583 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
1585 return ERR_PTR(ret);
1589 EXPORT_SYMBOL_GPL(devm_pwm_get);
1592 * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
1593 * @dev: device for PWM consumer
1594 * @fwnode: firmware node to get the PWM from
1595 * @con_id: consumer name
1597 * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
1598 * acpi_pwm_get() for a detailed description.
1600 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1601 * error code on failure.
1603 struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
1604 struct fwnode_handle *fwnode,
1607 struct pwm_device *pwm = ERR_PTR(-ENODEV);
1610 if (is_of_node(fwnode))
1611 pwm = of_pwm_get(dev, to_of_node(fwnode), con_id);
1612 else if (is_acpi_node(fwnode))
1613 pwm = acpi_pwm_get(fwnode);
1617 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
1619 return ERR_PTR(ret);
1623 EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get);
1625 static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
1629 for (i = 0; i < chip->npwm; i++) {
1630 struct pwm_device *pwm = &chip->pwms[i];
1631 struct pwm_state state;
1633 pwm_get_state(pwm, &state);
1635 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
1637 if (test_bit(PWMF_REQUESTED, &pwm->flags))
1638 seq_puts(s, " requested");
1641 seq_puts(s, " enabled");
1643 seq_printf(s, " period: %llu ns", state.period);
1644 seq_printf(s, " duty: %llu ns", state.duty_cycle);
1645 seq_printf(s, " polarity: %s",
1646 state.polarity ? "inverse" : "normal");
1648 if (state.usage_power)
1649 seq_puts(s, " usage_power");
1655 static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
1657 unsigned long id = *pos;
1660 mutex_lock(&pwm_lock);
1663 ret = idr_get_next_ul(&pwm_chips, &id);
1668 static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
1670 unsigned long id = *pos + 1;
1675 ret = idr_get_next_ul(&pwm_chips, &id);
1680 static void pwm_seq_stop(struct seq_file *s, void *v)
1682 mutex_unlock(&pwm_lock);
1685 static int pwm_seq_show(struct seq_file *s, void *v)
1687 struct pwm_chip *chip = v;
1689 seq_printf(s, "%s%d: %s/%s, %d PWM device%s\n",
1690 (char *)s->private, chip->id,
1691 pwmchip_parent(chip)->bus ? pwmchip_parent(chip)->bus->name : "no-bus",
1692 dev_name(pwmchip_parent(chip)), chip->npwm,
1693 (chip->npwm != 1) ? "s" : "");
1695 pwm_dbg_show(chip, s);
1700 static const struct seq_operations pwm_debugfs_sops = {
1701 .start = pwm_seq_start,
1702 .next = pwm_seq_next,
1703 .stop = pwm_seq_stop,
1704 .show = pwm_seq_show,
1707 DEFINE_SEQ_ATTRIBUTE(pwm_debugfs);
1709 static int __init pwm_init(void)
1713 ret = class_register(&pwm_class);
1715 pr_err("Failed to initialize PWM class (%pe)\n", ERR_PTR(ret));
1719 if (IS_ENABLED(CONFIG_DEBUG_FS))
1720 debugfs_create_file("pwm", 0444, NULL, NULL, &pwm_debugfs_fops);
1724 subsys_initcall(pwm_init);