Lines Matching +full:- +full:pwm
1 // SPDX-License-Identifier: GPL-2.0-or-later
6 * Copyright (C) 2011-2012 Avionic Design GmbH
9 #define DEFAULT_SYMBOL_NAMESPACE "PWM"
15 #include <linux/pwm.h>
24 #include <dt-bindings/pwm/pwm.h>
27 #include <trace/events/pwm.h>
36 if (chip->atomic) in pwmchip_lock()
37 spin_lock(&chip->atomic_lock); in pwmchip_lock()
39 mutex_lock(&chip->nonatomic_lock); in pwmchip_lock()
44 if (chip->atomic) in pwmchip_unlock()
45 spin_unlock(&chip->atomic_lock); in pwmchip_unlock()
47 mutex_unlock(&chip->nonatomic_lock); in pwmchip_unlock()
60 if (wf->period_length_ns > S64_MAX) in DEFINE_GUARD()
63 if (wf->duty_length_ns > wf->period_length_ns) in DEFINE_GUARD()
70 if (wf->duty_offset_ns && wf->duty_offset_ns >= wf->period_length_ns) in DEFINE_GUARD()
78 if (wf->period_length_ns) { in pwm_wf2state()
79 if (wf->duty_length_ns + wf->duty_offset_ns < wf->period_length_ns) in pwm_wf2state()
83 .period = wf->period_length_ns, in pwm_wf2state()
84 .duty_cycle = wf->duty_length_ns, in pwm_wf2state()
90 .period = wf->period_length_ns, in pwm_wf2state()
91 .duty_cycle = wf->period_length_ns - wf->duty_length_ns, in pwm_wf2state()
102 if (state->enabled) { in pwm_state2wf()
103 if (state->polarity == PWM_POLARITY_NORMAL) in pwm_state2wf()
105 .period_length_ns = state->period, in pwm_state2wf()
106 .duty_length_ns = state->duty_cycle, in pwm_state2wf()
111 .period_length_ns = state->period, in pwm_state2wf()
112 .duty_length_ns = state->period - state->duty_cycle, in pwm_state2wf()
113 .duty_offset_ns = state->duty_cycle, in pwm_state2wf()
124 if (a->period_length_ns > b->period_length_ns) in pwmwfcmp()
127 if (a->period_length_ns < b->period_length_ns) in pwmwfcmp()
128 return -1; in pwmwfcmp()
130 if (a->duty_length_ns > b->duty_length_ns) in pwmwfcmp()
133 if (a->duty_length_ns < b->duty_length_ns) in pwmwfcmp()
134 return -1; in pwmwfcmp()
136 if (a->duty_offset_ns > b->duty_offset_ns) in pwmwfcmp()
139 if (a->duty_offset_ns < b->duty_offset_ns) in pwmwfcmp()
140 return -1; in pwmwfcmp()
148 if (!wf->period_length_ns) in pwm_check_rounding()
151 if (wf->period_length_ns < wf_rounded->period_length_ns) in pwm_check_rounding()
154 if (wf->duty_length_ns < wf_rounded->duty_length_ns) in pwm_check_rounding()
157 if (wf->duty_offset_ns < wf_rounded->duty_offset_ns) in pwm_check_rounding()
163 static int __pwm_round_waveform_tohw(struct pwm_chip *chip, struct pwm_device *pwm, in __pwm_round_waveform_tohw() argument
166 const struct pwm_ops *ops = chip->ops; in __pwm_round_waveform_tohw()
169 ret = ops->round_waveform_tohw(chip, pwm, wf, wfhw); in __pwm_round_waveform_tohw()
170 trace_pwm_round_waveform_tohw(pwm, wf, wfhw, ret); in __pwm_round_waveform_tohw()
175 static int __pwm_round_waveform_fromhw(struct pwm_chip *chip, struct pwm_device *pwm, in __pwm_round_waveform_fromhw() argument
178 const struct pwm_ops *ops = chip->ops; in __pwm_round_waveform_fromhw()
181 ret = ops->round_waveform_fromhw(chip, pwm, wfhw, wf); in __pwm_round_waveform_fromhw()
182 trace_pwm_round_waveform_fromhw(pwm, wfhw, wf, ret); in __pwm_round_waveform_fromhw()
187 static int __pwm_read_waveform(struct pwm_chip *chip, struct pwm_device *pwm, void *wfhw) in __pwm_read_waveform() argument
189 const struct pwm_ops *ops = chip->ops; in __pwm_read_waveform()
192 ret = ops->read_waveform(chip, pwm, wfhw); in __pwm_read_waveform()
193 trace_pwm_read_waveform(pwm, wfhw, ret); in __pwm_read_waveform()
198 static int __pwm_write_waveform(struct pwm_chip *chip, struct pwm_device *pwm, const void *wfhw) in __pwm_write_waveform() argument
200 const struct pwm_ops *ops = chip->ops; in __pwm_write_waveform()
203 ret = ops->write_waveform(chip, pwm, wfhw); in __pwm_write_waveform()
204 trace_pwm_write_waveform(pwm, wfhw, ret); in __pwm_write_waveform()
212 * pwm_round_waveform_might_sleep - Query hardware capabilities
214 * @pwm: PWM device
233 * the input waveform under the PWM rounding rules or a negative errno.
235 int pwm_round_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *wf) in pwm_round_waveform_might_sleep() argument
237 struct pwm_chip *chip = pwm->chip; in pwm_round_waveform_might_sleep()
238 const struct pwm_ops *ops = chip->ops; in pwm_round_waveform_might_sleep()
243 BUG_ON(WFHWSIZE < ops->sizeof_wfhw); in pwm_round_waveform_might_sleep()
246 return -EOPNOTSUPP; in pwm_round_waveform_might_sleep()
249 return -EINVAL; in pwm_round_waveform_might_sleep()
253 if (!chip->operational) in pwm_round_waveform_might_sleep()
254 return -ENODEV; in pwm_round_waveform_might_sleep()
256 ret_tohw = __pwm_round_waveform_tohw(chip, pwm, wf, wfhw); in pwm_round_waveform_might_sleep()
261 …dev_err(&chip->dev, "Unexpected return value from __pwm_round_waveform_tohw: requested %llu/%llu [… in pwm_round_waveform_might_sleep()
264 ret_fromhw = __pwm_round_waveform_fromhw(chip, pwm, wfhw, wf); in pwm_round_waveform_might_sleep()
269 …dev_err(&chip->dev, "Unexpected return value from __pwm_round_waveform_fromhw: requested %llu/%llu… in pwm_round_waveform_might_sleep()
274 dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n", in pwm_round_waveform_might_sleep()
276 wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns); in pwm_round_waveform_might_sleep()
283 * pwm_get_waveform_might_sleep - Query hardware about current configuration
285 * @pwm: PWM device
288 * Stores the current configuration of the PWM in @wf. Note this is the
291 int pwm_get_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *wf) in pwm_get_waveform_might_sleep() argument
293 struct pwm_chip *chip = pwm->chip; in pwm_get_waveform_might_sleep()
294 const struct pwm_ops *ops = chip->ops; in pwm_get_waveform_might_sleep()
298 BUG_ON(WFHWSIZE < ops->sizeof_wfhw); in pwm_get_waveform_might_sleep()
300 if (!pwmchip_supports_waveform(chip) || !ops->read_waveform) in pwm_get_waveform_might_sleep()
301 return -EOPNOTSUPP; in pwm_get_waveform_might_sleep()
305 if (!chip->operational) in pwm_get_waveform_might_sleep()
306 return -ENODEV; in pwm_get_waveform_might_sleep()
308 err = __pwm_read_waveform(chip, pwm, &wfhw); in pwm_get_waveform_might_sleep()
312 return __pwm_round_waveform_fromhw(chip, pwm, &wfhw, wf); in pwm_get_waveform_might_sleep()
317 static int __pwm_set_waveform(struct pwm_device *pwm, in __pwm_set_waveform() argument
321 struct pwm_chip *chip = pwm->chip; in __pwm_set_waveform()
322 const struct pwm_ops *ops = chip->ops; in __pwm_set_waveform()
327 BUG_ON(WFHWSIZE < ops->sizeof_wfhw); in __pwm_set_waveform()
330 return -EOPNOTSUPP; in __pwm_set_waveform()
333 return -EINVAL; in __pwm_set_waveform()
335 ret_tohw = __pwm_round_waveform_tohw(chip, pwm, wf, &wfhw); in __pwm_set_waveform()
339 if ((IS_ENABLED(CONFIG_PWM_DEBUG) || exact) && wf->period_length_ns) { in __pwm_set_waveform()
340 err = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf_rounded); in __pwm_set_waveform()
345 dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n", in __pwm_set_waveform()
346 wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns, in __pwm_set_waveform()
350 dev_dbg(&chip->dev, "Requested no rounding, but %llu/%llu [+%llu] -> %llu/%llu [+%llu]\n", in __pwm_set_waveform()
351 wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns, in __pwm_set_waveform()
358 err = __pwm_write_waveform(chip, pwm, &wfhw); in __pwm_set_waveform()
363 pwm_wf2state(wf, &pwm->state); in __pwm_set_waveform()
365 if (IS_ENABLED(CONFIG_PWM_DEBUG) && ops->read_waveform && wf->period_length_ns) { in __pwm_set_waveform()
368 err = __pwm_read_waveform(chip, pwm, &wfhw); in __pwm_set_waveform()
373 err = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf_set); in __pwm_set_waveform()
379 dev_err(&chip->dev, in __pwm_set_waveform()
381 wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns, in __pwm_set_waveform()
390 * pwm_set_waveform_might_sleep - Apply a new waveform
392 * @pwm: PWM device
406 int pwm_set_waveform_might_sleep(struct pwm_device *pwm, in pwm_set_waveform_might_sleep() argument
409 struct pwm_chip *chip = pwm->chip; in pwm_set_waveform_might_sleep()
416 if (!chip->operational) in pwm_set_waveform_might_sleep()
417 return -ENODEV; in pwm_set_waveform_might_sleep()
419 if (IS_ENABLED(CONFIG_PWM_DEBUG) && chip->atomic) { in pwm_set_waveform_might_sleep()
425 err = __pwm_set_waveform(pwm, wf, exact); in pwm_set_waveform_might_sleep()
428 err = __pwm_set_waveform(pwm, wf, exact); in pwm_set_waveform_might_sleep()
435 static void pwm_apply_debug(struct pwm_device *pwm, in pwm_apply_debug() argument
438 struct pwm_state *last = &pwm->last; in pwm_apply_debug()
439 struct pwm_chip *chip = pwm->chip; in pwm_apply_debug()
447 if (!chip->ops->get_state) in pwm_apply_debug()
455 err = chip->ops->get_state(chip, pwm, &s1); in pwm_apply_debug()
456 trace_pwm_get(pwm, &s1, err); in pwm_apply_debug()
466 if (s1.enabled && s1.polarity != state->polarity) { in pwm_apply_debug()
467 s2.polarity = state->polarity; in pwm_apply_debug()
468 s2.duty_cycle = s1.period - s1.duty_cycle; in pwm_apply_debug()
475 if (s2.polarity != state->polarity && in pwm_apply_debug()
476 state->duty_cycle < state->period) in pwm_apply_debug()
479 if (state->enabled && s2.enabled && in pwm_apply_debug()
480 last->polarity == state->polarity && in pwm_apply_debug()
481 last->period > s2.period && in pwm_apply_debug()
482 last->period <= state->period) in pwm_apply_debug()
485 state->period, s2.period, last->period); in pwm_apply_debug()
491 if (state->enabled && s2.enabled && state->period < s2.period && s2.duty_cycle) in pwm_apply_debug()
494 state->period, s2.period); in pwm_apply_debug()
496 if (state->enabled && in pwm_apply_debug()
497 last->polarity == state->polarity && in pwm_apply_debug()
498 last->period == s2.period && in pwm_apply_debug()
499 last->duty_cycle > s2.duty_cycle && in pwm_apply_debug()
500 last->duty_cycle <= state->duty_cycle) in pwm_apply_debug()
503 state->duty_cycle, state->period, in pwm_apply_debug()
505 last->duty_cycle, last->period); in pwm_apply_debug()
507 if (state->enabled && s2.enabled && state->duty_cycle < s2.duty_cycle) in pwm_apply_debug()
510 state->duty_cycle, state->period, in pwm_apply_debug()
513 if (!state->enabled && s2.enabled && s2.duty_cycle > 0) in pwm_apply_debug()
518 err = chip->ops->apply(chip, pwm, &s1); in pwm_apply_debug()
519 trace_pwm_apply(pwm, &s1, err); in pwm_apply_debug()
527 err = chip->ops->get_state(chip, pwm, last); in pwm_apply_debug()
528 trace_pwm_get(pwm, last, err); in pwm_apply_debug()
533 if (s1.enabled != last->enabled || in pwm_apply_debug()
534 s1.polarity != last->polarity || in pwm_apply_debug()
535 (s1.enabled && s1.period != last->period) || in pwm_apply_debug()
536 (s1.enabled && s1.duty_cycle != last->duty_cycle)) { in pwm_apply_debug()
538 ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n", in pwm_apply_debug()
540 last->enabled, last->polarity, last->duty_cycle, in pwm_apply_debug()
541 last->period); in pwm_apply_debug()
552 if (state->enabled) in pwm_state_valid()
555 if (!state->period) in pwm_state_valid()
558 if (state->duty_cycle > state->period) in pwm_state_valid()
565 * __pwm_apply() - atomically apply a new state to a PWM device
566 * @pwm: PWM device
569 static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state) in __pwm_apply() argument
575 if (!pwm || !state) in __pwm_apply()
576 return -EINVAL; in __pwm_apply()
587 if (!pwm_state_valid(&pwm->state)) { in __pwm_apply()
588 pwm->state = *state; in __pwm_apply()
592 return -EINVAL; in __pwm_apply()
595 chip = pwm->chip; in __pwm_apply()
596 ops = chip->ops; in __pwm_apply()
598 if (state->period == pwm->state.period && in __pwm_apply()
599 state->duty_cycle == pwm->state.duty_cycle && in __pwm_apply()
600 state->polarity == pwm->state.polarity && in __pwm_apply()
601 state->enabled == pwm->state.enabled && in __pwm_apply()
602 state->usage_power == pwm->state.usage_power) in __pwm_apply()
609 BUG_ON(WFHWSIZE < ops->sizeof_wfhw); in __pwm_apply()
617 * .round_waveform_tohw() rounds down the time the PWM is high. in __pwm_apply()
622 err = __pwm_round_waveform_tohw(chip, pwm, &wf, &wfhw); in __pwm_apply()
630 return -EINVAL; in __pwm_apply()
638 err = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf_rounded); in __pwm_apply()
643 dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n", in __pwm_apply()
648 err = __pwm_write_waveform(chip, pwm, &wfhw); in __pwm_apply()
652 pwm->state = *state; in __pwm_apply()
655 err = ops->apply(chip, pwm, state); in __pwm_apply()
656 trace_pwm_apply(pwm, state, err); in __pwm_apply()
660 pwm->state = *state; in __pwm_apply()
663 * only do this after pwm->state was applied as some in __pwm_apply()
666 pwm_apply_debug(pwm, state); in __pwm_apply()
673 * pwm_apply_might_sleep() - atomically apply a new state to a PWM device
675 * @pwm: PWM device
678 int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state) in pwm_apply_might_sleep() argument
681 struct pwm_chip *chip = pwm->chip; in pwm_apply_might_sleep()
694 if (!chip->operational) in pwm_apply_might_sleep()
695 return -ENODEV; in pwm_apply_might_sleep()
697 if (IS_ENABLED(CONFIG_PWM_DEBUG) && chip->atomic) { in pwm_apply_might_sleep()
703 err = __pwm_apply(pwm, state); in pwm_apply_might_sleep()
706 err = __pwm_apply(pwm, state); in pwm_apply_might_sleep()
714 * pwm_apply_atomic() - apply a new state to a PWM device from atomic context
715 * Not all PWM devices support this function, check with pwm_might_sleep().
716 * @pwm: PWM device
719 int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state) in pwm_apply_atomic() argument
721 struct pwm_chip *chip = pwm->chip; in pwm_apply_atomic()
723 WARN_ONCE(!chip->atomic, in pwm_apply_atomic()
724 "sleeping PWM driver used in atomic context\n"); in pwm_apply_atomic()
728 if (!chip->operational) in pwm_apply_atomic()
729 return -ENODEV; in pwm_apply_atomic()
731 return __pwm_apply(pwm, state); in pwm_apply_atomic()
736 * pwm_get_state_hw() - get the current PWM state from hardware
737 * @pwm: PWM device
738 * @state: state to fill with the current PWM state
740 * Similar to pwm_get_state() but reads the current PWM state from hardware
746 int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state) in pwm_get_state_hw() argument
748 struct pwm_chip *chip = pwm->chip; in pwm_get_state_hw()
749 const struct pwm_ops *ops = chip->ops; in pwm_get_state_hw()
750 int ret = -EOPNOTSUPP; in pwm_get_state_hw()
756 if (!chip->operational) in pwm_get_state_hw()
757 return -ENODEV; in pwm_get_state_hw()
759 if (pwmchip_supports_waveform(chip) && ops->read_waveform) { in pwm_get_state_hw()
763 BUG_ON(WFHWSIZE < ops->sizeof_wfhw); in pwm_get_state_hw()
765 ret = __pwm_read_waveform(chip, pwm, &wfhw); in pwm_get_state_hw()
769 ret = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf); in pwm_get_state_hw()
775 } else if (ops->get_state) { in pwm_get_state_hw()
776 ret = ops->get_state(chip, pwm, state); in pwm_get_state_hw()
777 trace_pwm_get(pwm, state, ret); in pwm_get_state_hw()
785 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
786 * @pwm: PWM device
788 * This function will adjust the PWM config to the PWM arguments provided
789 * by the DT or PWM lookup table. This is particularly useful to adapt
792 int pwm_adjust_config(struct pwm_device *pwm) in pwm_adjust_config() argument
797 pwm_get_args(pwm, &pargs); in pwm_adjust_config()
798 pwm_get_state(pwm, &state); in pwm_adjust_config()
801 * If the current period is zero it means that either the PWM driver in pwm_adjust_config()
802 * does not support initial state retrieval or the PWM has not yet in pwm_adjust_config()
813 return pwm_apply_might_sleep(pwm, &state); in pwm_adjust_config()
817 * Adjust the PWM duty cycle/period based on the period value provided in pwm_adjust_config()
818 * in PWM args. in pwm_adjust_config()
833 state.duty_cycle = state.period - state.duty_cycle; in pwm_adjust_config()
836 return pwm_apply_might_sleep(pwm, &state); in pwm_adjust_config()
841 * pwm_capture() - capture and report a PWM signal
842 * @pwm: PWM device
848 static int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, in pwm_capture() argument
851 struct pwm_chip *chip = pwm->chip; in pwm_capture()
852 const struct pwm_ops *ops = chip->ops; in pwm_capture()
854 if (!ops->capture) in pwm_capture()
855 return -ENOSYS; in pwm_capture()
866 if (!chip->operational) in pwm_capture()
867 return -ENODEV; in pwm_capture()
869 return ops->capture(chip, pwm, result, timeout); in pwm_capture()
890 static int pwm_device_request(struct pwm_device *pwm, const char *label) in pwm_device_request() argument
893 struct pwm_chip *chip = pwm->chip; in pwm_device_request()
894 const struct pwm_ops *ops = chip->ops; in pwm_device_request()
896 if (test_bit(PWMF_REQUESTED, &pwm->flags)) in pwm_device_request()
897 return -EBUSY; in pwm_device_request()
904 if (!chip->operational) in pwm_device_request()
905 return -ENODEV; in pwm_device_request()
907 if (!try_module_get(chip->owner)) in pwm_device_request()
908 return -ENODEV; in pwm_device_request()
910 if (!get_device(&chip->dev)) { in pwm_device_request()
911 err = -ENODEV; in pwm_device_request()
915 if (ops->request) { in pwm_device_request()
916 err = ops->request(chip, pwm); in pwm_device_request()
918 put_device(&chip->dev); in pwm_device_request()
920 module_put(chip->owner); in pwm_device_request()
925 if (ops->read_waveform || ops->get_state) { in pwm_device_request()
927 * Zero-initialize state because most drivers are unaware of in pwm_device_request()
935 err = pwm_get_state_hw(pwm, &state); in pwm_device_request()
937 pwm->state = state; in pwm_device_request()
940 pwm->last = pwm->state; in pwm_device_request()
943 set_bit(PWMF_REQUESTED, &pwm->flags); in pwm_device_request()
944 pwm->label = label; in pwm_device_request()
950 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
951 * @chip: PWM chip
952 * @index: per-chip index of the PWM to request
953 * @label: a literal description string of this PWM
955 * Returns: A pointer to the PWM device at the given index of the given PWM
957 * specified PWM chip or if the PWM device cannot be requested.
963 struct pwm_device *pwm; in pwm_request_from_chip() local
966 if (!chip || index >= chip->npwm) in pwm_request_from_chip()
967 return ERR_PTR(-EINVAL); in pwm_request_from_chip()
971 pwm = &chip->pwms[index]; in pwm_request_from_chip()
973 err = pwm_device_request(pwm, label); in pwm_request_from_chip()
977 return pwm; in pwm_request_from_chip()
983 struct pwm_device *pwm; in of_pwm_xlate_with_flags() local
986 if (args->args_count < 1) in of_pwm_xlate_with_flags()
987 return ERR_PTR(-EINVAL); in of_pwm_xlate_with_flags()
989 pwm = pwm_request_from_chip(chip, args->args[0], NULL); in of_pwm_xlate_with_flags()
990 if (IS_ERR(pwm)) in of_pwm_xlate_with_flags()
991 return pwm; in of_pwm_xlate_with_flags()
993 if (args->args_count > 1) in of_pwm_xlate_with_flags()
994 pwm->args.period = args->args[1]; in of_pwm_xlate_with_flags()
996 pwm->args.polarity = PWM_POLARITY_NORMAL; in of_pwm_xlate_with_flags()
997 if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED) in of_pwm_xlate_with_flags()
998 pwm->args.polarity = PWM_POLARITY_INVERSED; in of_pwm_xlate_with_flags()
1000 return pwm; in of_pwm_xlate_with_flags()
1005 * This callback is used for PXA PWM chips that only have a single PWM line.
1011 * Note that if #pwm-cells = <3>, the semantic is the same as for
1013 * #pwm-cells = <3> without breaking the legacy binding.
1020 struct pwm_device *pwm; in of_pwm_single_xlate() local
1022 if (args->args_count >= 3) in of_pwm_single_xlate()
1025 pwm = pwm_request_from_chip(chip, 0, NULL); in of_pwm_single_xlate()
1026 if (IS_ERR(pwm)) in of_pwm_single_xlate()
1027 return pwm; in of_pwm_single_xlate()
1029 if (args->args_count > 0) in of_pwm_single_xlate()
1030 pwm->args.period = args->args[0]; in of_pwm_single_xlate()
1032 pwm->args.polarity = PWM_POLARITY_NORMAL; in of_pwm_single_xlate()
1033 if (args->args_count > 1 && args->args[1] & PWM_POLARITY_INVERTED) in of_pwm_single_xlate()
1034 pwm->args.polarity = PWM_POLARITY_INVERSED; in of_pwm_single_xlate()
1036 return pwm; in of_pwm_single_xlate()
1042 struct pwm_device *pwm; member
1061 return export->pwm; in pwm_from_dev()
1068 const struct pwm_device *pwm = pwm_from_dev(pwm_dev); in period_show() local
1071 pwm_get_state(pwm, &state); in period_show()
1081 struct pwm_device *pwm = export->pwm; in period_store() local
1090 guard(mutex)(&export->lock); in period_store()
1092 pwm_get_state(pwm, &state); in period_store()
1094 ret = pwm_apply_might_sleep(pwm, &state); in period_store()
1103 const struct pwm_device *pwm = pwm_from_dev(pwm_dev); in duty_cycle_show() local
1106 pwm_get_state(pwm, &state); in duty_cycle_show()
1116 struct pwm_device *pwm = export->pwm; in duty_cycle_store() local
1125 guard(mutex)(&export->lock); in duty_cycle_store()
1127 pwm_get_state(pwm, &state); in duty_cycle_store()
1129 ret = pwm_apply_might_sleep(pwm, &state); in duty_cycle_store()
1138 const struct pwm_device *pwm = pwm_from_dev(pwm_dev); in enable_show() local
1141 pwm_get_state(pwm, &state); in enable_show()
1151 struct pwm_device *pwm = export->pwm; in enable_store() local
1159 guard(mutex)(&export->lock); in enable_store()
1161 pwm_get_state(pwm, &state); in enable_store()
1171 return -EINVAL; in enable_store()
1174 ret = pwm_apply_might_sleep(pwm, &state); in enable_store()
1183 const struct pwm_device *pwm = pwm_from_dev(pwm_dev); in polarity_show() local
1187 pwm_get_state(pwm, &state); in polarity_show()
1207 struct pwm_device *pwm = export->pwm; in polarity_store() local
1217 return -EINVAL; in polarity_store()
1219 guard(mutex)(&export->lock); in polarity_store()
1221 pwm_get_state(pwm, &state); in polarity_store()
1223 ret = pwm_apply_might_sleep(pwm, &state); in polarity_store()
1232 struct pwm_device *pwm = pwm_from_dev(pwm_dev); in capture_show() local
1236 ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ)); in capture_show()
1257 ATTRIBUTE_GROUPS(pwm);
1266 static int pwm_export_child(struct device *pwmchip_dev, struct pwm_device *pwm) in pwm_export_child() argument
1272 if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags)) in pwm_export_child()
1273 return -EBUSY; in pwm_export_child()
1277 clear_bit(PWMF_EXPORTED, &pwm->flags); in pwm_export_child()
1278 return -ENOMEM; in pwm_export_child()
1281 export->pwm = pwm; in pwm_export_child()
1282 mutex_init(&export->lock); in pwm_export_child()
1284 export->pwm_dev.release = pwm_export_release; in pwm_export_child()
1285 export->pwm_dev.parent = pwmchip_dev; in pwm_export_child()
1286 export->pwm_dev.devt = MKDEV(0, 0); in pwm_export_child()
1287 export->pwm_dev.groups = pwm_groups; in pwm_export_child()
1288 dev_set_name(&export->pwm_dev, "pwm%u", pwm->hwpwm); in pwm_export_child()
1290 ret = device_register(&export->pwm_dev); in pwm_export_child()
1292 clear_bit(PWMF_EXPORTED, &pwm->flags); in pwm_export_child()
1293 put_device(&export->pwm_dev); in pwm_export_child()
1297 pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm); in pwm_export_child()
1299 kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop); in pwm_export_child()
1310 static int pwm_unexport_child(struct device *pwmchip_dev, struct pwm_device *pwm) in pwm_unexport_child() argument
1315 if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags)) in pwm_unexport_child()
1316 return -ENODEV; in pwm_unexport_child()
1318 pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match); in pwm_unexport_child()
1320 return -ENODEV; in pwm_unexport_child()
1322 pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm); in pwm_unexport_child()
1324 kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop); in pwm_unexport_child()
1330 pwm_put(pwm); in pwm_unexport_child()
1340 struct pwm_device *pwm; in export_store() local
1348 if (hwpwm >= chip->npwm) in export_store()
1349 return -ENODEV; in export_store()
1351 pwm = pwm_request_from_chip(chip, hwpwm, "sysfs"); in export_store()
1352 if (IS_ERR(pwm)) in export_store()
1353 return PTR_ERR(pwm); in export_store()
1355 ret = pwm_export_child(pwmchip_dev, pwm); in export_store()
1357 pwm_put(pwm); in export_store()
1375 if (hwpwm >= chip->npwm) in unexport_store()
1376 return -ENODEV; in unexport_store()
1378 ret = pwm_unexport_child(pwmchip_dev, &chip->pwms[hwpwm]); in unexport_store()
1389 return sysfs_emit(buf, "%u\n", chip->npwm); in npwm_show()
1401 /* takes export->lock on success */
1403 struct pwm_device *pwm, in pwm_class_get_state() argument
1409 if (!test_bit(PWMF_EXPORTED, &pwm->flags)) in pwm_class_get_state()
1412 pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match); in pwm_class_get_state()
1419 mutex_lock(&export->lock); in pwm_class_get_state()
1420 pwm_get_state(pwm, state); in pwm_class_get_state()
1426 struct pwm_device *pwm, in pwm_class_apply_state() argument
1429 int ret = pwm_apply_might_sleep(pwm, state); in pwm_class_apply_state()
1432 mutex_unlock(&export->lock); in pwm_class_apply_state()
1444 struct pwm_device *pwm = &chip->pwms[i]; in pwm_class_resume_npwm() local
1448 export = pwm_class_get_state(pwmchip_dev, pwm, &state); in pwm_class_resume_npwm()
1453 if (!export->suspend.enabled) { in pwm_class_resume_npwm()
1455 mutex_unlock(&export->lock); in pwm_class_resume_npwm()
1459 state.enabled = export->suspend.enabled; in pwm_class_resume_npwm()
1460 ret = pwm_class_apply_state(export, pwm, &state); in pwm_class_resume_npwm()
1474 for (i = 0; i < chip->npwm; i++) { in pwm_class_suspend()
1475 struct pwm_device *pwm = &chip->pwms[i]; in pwm_class_suspend() local
1479 export = pwm_class_get_state(pwmchip_dev, pwm, &state); in pwm_class_suspend()
1487 export->suspend = state; in pwm_class_suspend()
1490 mutex_unlock(&export->lock); in pwm_class_suspend()
1495 ret = pwm_class_apply_state(export, pwm, &state); in pwm_class_suspend()
1498 * roll back the PWM devices that were disabled by in pwm_class_suspend()
1513 return pwm_class_resume_npwm(pwmchip_dev, chip->npwm); in pwm_class_resume()
1519 .name = "pwm",
1528 for (i = 0; i < chip->npwm; i++) { in pwmchip_sysfs_unexport()
1529 struct pwm_device *pwm = &chip->pwms[i]; in pwmchip_sysfs_unexport() local
1531 if (test_bit(PWMF_EXPORTED, &pwm->flags)) in pwmchip_sysfs_unexport()
1532 pwm_unexport_child(&chip->dev, pwm); in pwmchip_sysfs_unexport()
1540 return (void *)chip + ALIGN(struct_size(chip, pwms, chip->npwm), PWMCHIP_ALIGN); in pwmchip_priv()
1546 put_device(&chip->dev); in pwmchip_put()
1569 return ERR_PTR(-ENOMEM); in pwmchip_alloc()
1571 chip->npwm = npwm; in pwmchip_alloc()
1572 chip->uses_pwmchip_alloc = true; in pwmchip_alloc()
1573 chip->operational = false; in pwmchip_alloc()
1575 pwmchip_dev = &chip->dev; in pwmchip_alloc()
1577 pwmchip_dev->class = &pwm_class; in pwmchip_alloc()
1578 pwmchip_dev->parent = parent; in pwmchip_alloc()
1579 pwmchip_dev->release = pwmchip_release; in pwmchip_alloc()
1583 for (i = 0; i < chip->npwm; i++) { in pwmchip_alloc()
1584 struct pwm_device *pwm = &chip->pwms[i]; in pwmchip_alloc() local
1585 pwm->chip = chip; in pwmchip_alloc()
1586 pwm->hwpwm = i; in pwmchip_alloc()
1619 if (!pwmchip_parent(chip) || !pwmchip_parent(chip)->of_node) in of_pwmchip_add()
1622 if (!chip->of_xlate) in of_pwmchip_add()
1623 chip->of_xlate = of_pwm_xlate_with_flags; in of_pwmchip_add()
1625 of_node_get(pwmchip_parent(chip)->of_node); in of_pwmchip_add()
1631 of_node_put(pwmchip_parent(chip)->of_node); in of_pwmchip_remove()
1636 const struct pwm_ops *ops = chip->ops; in pwm_ops_check()
1638 if (ops->write_waveform) { in pwm_ops_check()
1639 if (!ops->round_waveform_tohw || in pwm_ops_check()
1640 !ops->round_waveform_fromhw || in pwm_ops_check()
1641 !ops->write_waveform) in pwm_ops_check()
1644 if (WFHWSIZE < ops->sizeof_wfhw) { in pwm_ops_check()
1645 dev_warn(pwmchip_parent(chip), "WFHWSIZE < %zu\n", ops->sizeof_wfhw); in pwm_ops_check()
1649 if (!ops->apply) in pwm_ops_check()
1652 if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state) in pwm_ops_check()
1661 struct pwm_device *pwm) in pwm_device_link_add() argument
1667 * No device for the PWM consumer has been provided. It may in pwm_device_link_add()
1668 * impact the PM sequence ordering: the PWM supplier may get in pwm_device_link_add()
1671 dev_warn(pwmchip_parent(pwm->chip), in pwm_device_link_add()
1676 dl = device_link_add(dev, pwmchip_parent(pwm->chip), DL_FLAG_AUTOREMOVE_CONSUMER); in pwm_device_link_add()
1679 dev_name(pwmchip_parent(pwm->chip))); in pwm_device_link_add()
1680 return ERR_PTR(-EINVAL); in pwm_device_link_add()
1697 return ERR_PTR(-EPROBE_DEFER); in fwnode_to_pwmchip()
1701 * of_pwm_get() - request a PWM via the PWM framework
1702 * @dev: device for PWM consumer
1703 * @np: device node to get the PWM from
1706 * Returns the PWM device parsed from the phandle and index specified in the
1707 * "pwms" property of a device tree node or a negative error-code on failure.
1708 * Values parsed from the device tree are stored in the returned PWM device
1711 * If con_id is NULL, the first PWM device listed in the "pwms" property will
1712 * be requested. Otherwise the "pwm-names" property is used to do a reverse
1713 * lookup of the PWM index. This also means that the "pwm-names" property
1714 * becomes mandatory for devices that look up the PWM device via the con_id
1717 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1723 struct pwm_device *pwm = NULL; in of_pwm_get() local
1731 index = of_property_match_string(np, "pwm-names", con_id); in of_pwm_get()
1736 err = of_parse_phandle_with_args_map(np, "pwms", "pwm", index, &args); in of_pwm_get()
1744 if (PTR_ERR(chip) != -EPROBE_DEFER) in of_pwm_get()
1745 pr_err("%s(): PWM chip not found\n", __func__); in of_pwm_get()
1747 pwm = ERR_CAST(chip); in of_pwm_get()
1751 pwm = chip->of_xlate(chip, &args); in of_pwm_get()
1752 if (IS_ERR(pwm)) in of_pwm_get()
1755 dl = pwm_device_link_add(dev, pwm); in of_pwm_get()
1758 pwm_put(pwm); in of_pwm_get()
1759 pwm = ERR_CAST(dl); in of_pwm_get()
1765 * "pwm-names" property if it exists. Otherwise use the name of in of_pwm_get()
1769 err = of_property_read_string_index(np, "pwm-names", index, in of_pwm_get()
1772 con_id = np->name; in of_pwm_get()
1775 pwm->label = con_id; in of_pwm_get()
1780 return pwm; in of_pwm_get()
1784 * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
1787 * Returns the PWM device parsed from the fwnode and index specified in the
1788 * "pwms" property or a negative error-code on failure.
1789 * Values parsed from the device tree are stored in the returned PWM device
1795 * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
1797 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1802 struct pwm_device *pwm; in acpi_pwm_get() local
1814 return ERR_PTR(-EPROTO); in acpi_pwm_get()
1820 pwm = pwm_request_from_chip(chip, args.args[0], NULL); in acpi_pwm_get()
1821 if (IS_ERR(pwm)) in acpi_pwm_get()
1822 return pwm; in acpi_pwm_get()
1824 pwm->args.period = args.args[1]; in acpi_pwm_get()
1825 pwm->args.polarity = PWM_POLARITY_NORMAL; in acpi_pwm_get()
1828 pwm->args.polarity = PWM_POLARITY_INVERSED; in acpi_pwm_get()
1830 return pwm; in acpi_pwm_get()
1837 * pwm_get() - look up and request a PWM device
1838 * @dev: device for PWM consumer
1842 * a device tree, a PWM chip and a relative index is looked up via a table
1845 * Once a PWM chip has been found the specified PWM device will be requested
1848 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1855 struct pwm_device *pwm; in pwm_get() local
1869 pwm = acpi_pwm_get(fwnode); in pwm_get()
1870 if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT) in pwm_get()
1871 return pwm; in pwm_get()
1881 * If a match is found, the provider PWM chip is looked up by name in pwm_get()
1882 * and a PWM device is requested using the PWM device per-chip index. in pwm_get()
1891 * Then we take the most specific entry - with the following order in pwm_get()
1898 if (p->dev_id) { in pwm_get()
1899 if (!dev_id || strcmp(p->dev_id, dev_id)) in pwm_get()
1905 if (p->con_id) { in pwm_get()
1906 if (!con_id || strcmp(p->con_id, con_id)) in pwm_get()
1923 return ERR_PTR(-ENODEV); in pwm_get()
1925 chip = pwmchip_find_by_name(chosen->provider); in pwm_get()
1929 * the PWM chip lookup. This can be used to work around driver load in pwm_get()
1933 if (!chip && chosen->module) { in pwm_get()
1934 err = request_module(chosen->module); in pwm_get()
1936 chip = pwmchip_find_by_name(chosen->provider); in pwm_get()
1940 return ERR_PTR(-EPROBE_DEFER); in pwm_get()
1942 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id); in pwm_get()
1943 if (IS_ERR(pwm)) in pwm_get()
1944 return pwm; in pwm_get()
1946 dl = pwm_device_link_add(dev, pwm); in pwm_get()
1948 pwm_put(pwm); in pwm_get()
1952 pwm->args.period = chosen->period; in pwm_get()
1953 pwm->args.polarity = chosen->polarity; in pwm_get()
1955 return pwm; in pwm_get()
1960 * pwm_put() - release a PWM device
1961 * @pwm: PWM device
1963 void pwm_put(struct pwm_device *pwm) in pwm_put() argument
1967 if (!pwm) in pwm_put()
1970 chip = pwm->chip; in pwm_put()
1979 if (chip->operational && !test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) { in pwm_put()
1980 pr_warn("PWM device already freed\n"); in pwm_put()
1984 if (chip->operational && chip->ops->free) in pwm_put()
1985 pwm->chip->ops->free(pwm->chip, pwm); in pwm_put()
1987 pwm->label = NULL; in pwm_put()
1989 put_device(&chip->dev); in pwm_put()
1991 module_put(chip->owner); in pwm_put()
1995 static void devm_pwm_release(void *pwm) in devm_pwm_release() argument
1997 pwm_put(pwm); in devm_pwm_release()
2001 * devm_pwm_get() - resource managed pwm_get()
2002 * @dev: device for PWM consumer
2005 * This function performs like pwm_get() but the acquired PWM device will
2008 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
2013 struct pwm_device *pwm; in devm_pwm_get() local
2016 pwm = pwm_get(dev, con_id); in devm_pwm_get()
2017 if (IS_ERR(pwm)) in devm_pwm_get()
2018 return pwm; in devm_pwm_get()
2020 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm); in devm_pwm_get()
2024 return pwm; in devm_pwm_get()
2029 * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
2030 * @dev: device for PWM consumer
2031 * @fwnode: firmware node to get the PWM from
2034 * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
2037 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
2044 struct pwm_device *pwm = ERR_PTR(-ENODEV); in devm_fwnode_pwm_get() local
2048 pwm = of_pwm_get(dev, to_of_node(fwnode), con_id); in devm_fwnode_pwm_get()
2050 pwm = acpi_pwm_get(fwnode); in devm_fwnode_pwm_get()
2051 if (IS_ERR(pwm)) in devm_fwnode_pwm_get()
2052 return pwm; in devm_fwnode_pwm_get()
2054 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm); in devm_fwnode_pwm_get()
2058 return pwm; in devm_fwnode_pwm_get()
2063 * __pwmchip_add() - register a new PWM chip
2064 * @chip: the PWM chip to add
2067 * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the
2076 if (!chip || !pwmchip_parent(chip) || !chip->ops || !chip->npwm) in __pwmchip_add()
2077 return -EINVAL; in __pwmchip_add()
2085 if (!chip->uses_pwmchip_alloc) in __pwmchip_add()
2086 return -EINVAL; in __pwmchip_add()
2089 return -EINVAL; in __pwmchip_add()
2091 chip->owner = owner; in __pwmchip_add()
2093 if (chip->atomic) in __pwmchip_add()
2094 spin_lock_init(&chip->atomic_lock); in __pwmchip_add()
2096 mutex_init(&chip->nonatomic_lock); in __pwmchip_add()
2104 chip->id = ret; in __pwmchip_add()
2106 dev_set_name(&chip->dev, "pwmchip%u", chip->id); in __pwmchip_add()
2112 chip->operational = true; in __pwmchip_add()
2114 ret = device_add(&chip->dev); in __pwmchip_add()
2122 chip->operational = false; in __pwmchip_add()
2127 idr_remove(&pwm_chips, chip->id); in __pwmchip_add()
2134 * pwmchip_remove() - remove a PWM chip
2135 * @chip: the PWM chip to remove
2137 * Removes a PWM chip.
2147 chip->operational = false; in pwmchip_remove()
2149 for (i = 0; i < chip->npwm; ++i) { in pwmchip_remove()
2150 struct pwm_device *pwm = &chip->pwms[i]; in pwmchip_remove() local
2152 if (test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) { in pwmchip_remove()
2153 dev_warn(&chip->dev, "Freeing requested PWM #%u\n", i); in pwmchip_remove()
2154 if (pwm->chip->ops->free) in pwmchip_remove()
2155 pwm->chip->ops->free(pwm->chip, pwm); in pwmchip_remove()
2162 idr_remove(&pwm_chips, chip->id); in pwmchip_remove()
2165 device_del(&chip->dev); in pwmchip_remove()
2189 * pwm_add_table() - register PWM device consumers
2197 while (num--) { in pwm_add_table()
2198 list_add_tail(&table->list, &pwm_lookup_list); in pwm_add_table()
2204 * pwm_remove_table() - unregister PWM device consumers
2212 while (num--) { in pwm_remove_table()
2213 list_del(&table->list); in pwm_remove_table()
2222 for (i = 0; i < chip->npwm; i++) { in pwm_dbg_show()
2223 struct pwm_device *pwm = &chip->pwms[i]; in pwm_dbg_show() local
2226 pwm_get_state(pwm, &state); in pwm_dbg_show()
2228 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label); in pwm_dbg_show()
2230 if (test_bit(PWMF_REQUESTED, &pwm->flags)) in pwm_dbg_show()
2254 s->private = ""; in pwm_seq_start()
2266 s->private = "\n"; in pwm_seq_next()
2282 seq_printf(s, "%s%d: %s/%s, %d PWM device%s\n", in pwm_seq_show()
2283 (char *)s->private, chip->id, in pwm_seq_show()
2284 pwmchip_parent(chip)->bus ? pwmchip_parent(chip)->bus->name : "no-bus", in pwm_seq_show()
2285 dev_name(pwmchip_parent(chip)), chip->npwm, in pwm_seq_show()
2286 (chip->npwm != 1) ? "s" : ""); in pwm_seq_show()
2308 pr_err("Failed to initialize PWM class (%pe)\n", ERR_PTR(ret)); in pwm_init()
2313 debugfs_create_file("pwm", 0444, NULL, NULL, &pwm_debugfs_fops); in pwm_init()