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 -EINVAL; in pwm_round_waveform_might_sleep()
250 if (!chip->operational) in pwm_round_waveform_might_sleep()
251 return -ENODEV; in pwm_round_waveform_might_sleep()
253 ret_tohw = __pwm_round_waveform_tohw(chip, pwm, wf, wfhw); in pwm_round_waveform_might_sleep()
258 …dev_err(&chip->dev, "Unexpected return value from __pwm_round_waveform_tohw: requested %llu/%llu [… in pwm_round_waveform_might_sleep()
261 ret_fromhw = __pwm_round_waveform_fromhw(chip, pwm, wfhw, wf); in pwm_round_waveform_might_sleep()
266 …dev_err(&chip->dev, "Unexpected return value from __pwm_round_waveform_fromhw: requested %llu/%llu… in pwm_round_waveform_might_sleep()
271 dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n", in pwm_round_waveform_might_sleep()
273 wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns); in pwm_round_waveform_might_sleep()
280 * pwm_get_waveform_might_sleep - Query hardware about current configuration
282 * @pwm: PWM device
285 * Stores the current configuration of the PWM in @wf. Note this is the
288 int pwm_get_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *wf) in pwm_get_waveform_might_sleep() argument
290 struct pwm_chip *chip = pwm->chip; in pwm_get_waveform_might_sleep()
291 const struct pwm_ops *ops = chip->ops; in pwm_get_waveform_might_sleep()
295 BUG_ON(WFHWSIZE < ops->sizeof_wfhw); in pwm_get_waveform_might_sleep()
299 if (!chip->operational) in pwm_get_waveform_might_sleep()
300 return -ENODEV; in pwm_get_waveform_might_sleep()
302 err = __pwm_read_waveform(chip, pwm, &wfhw); in pwm_get_waveform_might_sleep()
306 return __pwm_round_waveform_fromhw(chip, pwm, &wfhw, wf); in pwm_get_waveform_might_sleep()
311 static int __pwm_set_waveform(struct pwm_device *pwm, in __pwm_set_waveform() argument
315 struct pwm_chip *chip = pwm->chip; in __pwm_set_waveform()
316 const struct pwm_ops *ops = chip->ops; in __pwm_set_waveform()
321 BUG_ON(WFHWSIZE < ops->sizeof_wfhw); in __pwm_set_waveform()
324 return -EINVAL; in __pwm_set_waveform()
326 err = __pwm_round_waveform_tohw(chip, pwm, wf, &wfhw); in __pwm_set_waveform()
330 if ((IS_ENABLED(CONFIG_PWM_DEBUG) || exact) && wf->period_length_ns) { in __pwm_set_waveform()
331 err = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf_rounded); in __pwm_set_waveform()
336 dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n", in __pwm_set_waveform()
337 wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns, in __pwm_set_waveform()
341 dev_dbg(&chip->dev, "Requested no rounding, but %llu/%llu [+%llu] -> %llu/%llu [+%llu]\n", in __pwm_set_waveform()
342 wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns, in __pwm_set_waveform()
349 err = __pwm_write_waveform(chip, pwm, &wfhw); in __pwm_set_waveform()
354 pwm_wf2state(wf, &pwm->state); in __pwm_set_waveform()
356 if (IS_ENABLED(CONFIG_PWM_DEBUG) && ops->read_waveform && wf->period_length_ns) { in __pwm_set_waveform()
359 err = __pwm_read_waveform(chip, pwm, &wfhw); in __pwm_set_waveform()
364 err = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf_set); in __pwm_set_waveform()
370 dev_err(&chip->dev, in __pwm_set_waveform()
372 wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns, in __pwm_set_waveform()
380 * pwm_set_waveform_might_sleep - Apply a new waveform
382 * @pwm: PWM device
396 int pwm_set_waveform_might_sleep(struct pwm_device *pwm, in pwm_set_waveform_might_sleep() argument
399 struct pwm_chip *chip = pwm->chip; in pwm_set_waveform_might_sleep()
406 if (!chip->operational) in pwm_set_waveform_might_sleep()
407 return -ENODEV; in pwm_set_waveform_might_sleep()
409 if (IS_ENABLED(CONFIG_PWM_DEBUG) && chip->atomic) { in pwm_set_waveform_might_sleep()
415 err = __pwm_set_waveform(pwm, wf, exact); in pwm_set_waveform_might_sleep()
418 err = __pwm_set_waveform(pwm, wf, exact); in pwm_set_waveform_might_sleep()
425 static void pwm_apply_debug(struct pwm_device *pwm, in pwm_apply_debug() argument
428 struct pwm_state *last = &pwm->last; in pwm_apply_debug()
429 struct pwm_chip *chip = pwm->chip; in pwm_apply_debug()
437 if (!chip->ops->get_state) in pwm_apply_debug()
445 err = chip->ops->get_state(chip, pwm, &s1); in pwm_apply_debug()
446 trace_pwm_get(pwm, &s1, err); in pwm_apply_debug()
456 if (s1.enabled && s1.polarity != state->polarity) { in pwm_apply_debug()
457 s2.polarity = state->polarity; in pwm_apply_debug()
458 s2.duty_cycle = s1.period - s1.duty_cycle; in pwm_apply_debug()
465 if (s2.polarity != state->polarity && in pwm_apply_debug()
466 state->duty_cycle < state->period) in pwm_apply_debug()
469 if (state->enabled && s2.enabled && in pwm_apply_debug()
470 last->polarity == state->polarity && in pwm_apply_debug()
471 last->period > s2.period && in pwm_apply_debug()
472 last->period <= state->period) in pwm_apply_debug()
475 state->period, s2.period, last->period); in pwm_apply_debug()
481 if (state->enabled && s2.enabled && state->period < s2.period && s2.duty_cycle) in pwm_apply_debug()
484 state->period, s2.period); in pwm_apply_debug()
486 if (state->enabled && in pwm_apply_debug()
487 last->polarity == state->polarity && in pwm_apply_debug()
488 last->period == s2.period && in pwm_apply_debug()
489 last->duty_cycle > s2.duty_cycle && in pwm_apply_debug()
490 last->duty_cycle <= state->duty_cycle) in pwm_apply_debug()
493 state->duty_cycle, state->period, in pwm_apply_debug()
495 last->duty_cycle, last->period); in pwm_apply_debug()
497 if (state->enabled && s2.enabled && state->duty_cycle < s2.duty_cycle) in pwm_apply_debug()
500 state->duty_cycle, state->period, in pwm_apply_debug()
503 if (!state->enabled && s2.enabled && s2.duty_cycle > 0) in pwm_apply_debug()
508 err = chip->ops->apply(chip, pwm, &s1); in pwm_apply_debug()
509 trace_pwm_apply(pwm, &s1, err); in pwm_apply_debug()
517 err = chip->ops->get_state(chip, pwm, last); in pwm_apply_debug()
518 trace_pwm_get(pwm, last, err); in pwm_apply_debug()
523 if (s1.enabled != last->enabled || in pwm_apply_debug()
524 s1.polarity != last->polarity || in pwm_apply_debug()
525 (s1.enabled && s1.period != last->period) || in pwm_apply_debug()
526 (s1.enabled && s1.duty_cycle != last->duty_cycle)) { in pwm_apply_debug()
528 ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n", in pwm_apply_debug()
530 last->enabled, last->polarity, last->duty_cycle, in pwm_apply_debug()
531 last->period); in pwm_apply_debug()
542 if (state->enabled) in pwm_state_valid()
545 if (!state->period) in pwm_state_valid()
548 if (state->duty_cycle > state->period) in pwm_state_valid()
555 * __pwm_apply() - atomically apply a new state to a PWM device
556 * @pwm: PWM device
559 static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state) in __pwm_apply() argument
565 if (!pwm || !state) in __pwm_apply()
566 return -EINVAL; in __pwm_apply()
577 if (!pwm_state_valid(&pwm->state)) { in __pwm_apply()
578 pwm->state = *state; in __pwm_apply()
582 return -EINVAL; in __pwm_apply()
585 chip = pwm->chip; in __pwm_apply()
586 ops = chip->ops; in __pwm_apply()
588 if (state->period == pwm->state.period && in __pwm_apply()
589 state->duty_cycle == pwm->state.duty_cycle && in __pwm_apply()
590 state->polarity == pwm->state.polarity && in __pwm_apply()
591 state->enabled == pwm->state.enabled && in __pwm_apply()
592 state->usage_power == pwm->state.usage_power) in __pwm_apply()
595 if (ops->write_waveform) { in __pwm_apply()
599 BUG_ON(WFHWSIZE < ops->sizeof_wfhw); in __pwm_apply()
607 * .round_waveform_tohw() rounds down the time the PWM is high. in __pwm_apply()
612 err = __pwm_round_waveform_tohw(chip, pwm, &wf, &wfhw); in __pwm_apply()
620 return -EINVAL; in __pwm_apply()
628 err = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf_rounded); in __pwm_apply()
633 dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n", in __pwm_apply()
638 err = __pwm_write_waveform(chip, pwm, &wfhw); in __pwm_apply()
642 pwm->state = *state; in __pwm_apply()
645 err = ops->apply(chip, pwm, state); in __pwm_apply()
646 trace_pwm_apply(pwm, state, err); in __pwm_apply()
650 pwm->state = *state; in __pwm_apply()
653 * only do this after pwm->state was applied as some in __pwm_apply()
656 pwm_apply_debug(pwm, state); in __pwm_apply()
663 * pwm_apply_might_sleep() - atomically apply a new state to a PWM device
665 * @pwm: PWM device
668 int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state) in pwm_apply_might_sleep() argument
671 struct pwm_chip *chip = pwm->chip; in pwm_apply_might_sleep()
684 if (!chip->operational) in pwm_apply_might_sleep()
685 return -ENODEV; in pwm_apply_might_sleep()
687 if (IS_ENABLED(CONFIG_PWM_DEBUG) && chip->atomic) { in pwm_apply_might_sleep()
693 err = __pwm_apply(pwm, state); in pwm_apply_might_sleep()
696 err = __pwm_apply(pwm, state); in pwm_apply_might_sleep()
704 * pwm_apply_atomic() - apply a new state to a PWM device from atomic context
705 * Not all PWM devices support this function, check with pwm_might_sleep().
706 * @pwm: PWM device
709 int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state) in pwm_apply_atomic() argument
711 struct pwm_chip *chip = pwm->chip; in pwm_apply_atomic()
713 WARN_ONCE(!chip->atomic, in pwm_apply_atomic()
714 "sleeping PWM driver used in atomic context\n"); in pwm_apply_atomic()
718 if (!chip->operational) in pwm_apply_atomic()
719 return -ENODEV; in pwm_apply_atomic()
721 return __pwm_apply(pwm, state); in pwm_apply_atomic()
726 * pwm_get_state_hw() - get the current PWM state from hardware
727 * @pwm: PWM device
728 * @state: state to fill with the current PWM state
730 * Similar to pwm_get_state() but reads the current PWM state from hardware
736 int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state) in pwm_get_state_hw() argument
738 struct pwm_chip *chip = pwm->chip; in pwm_get_state_hw()
739 const struct pwm_ops *ops = chip->ops; in pwm_get_state_hw()
740 int ret = -EOPNOTSUPP; in pwm_get_state_hw()
746 if (!chip->operational) in pwm_get_state_hw()
747 return -ENODEV; in pwm_get_state_hw()
749 if (ops->read_waveform) { in pwm_get_state_hw()
753 BUG_ON(WFHWSIZE < ops->sizeof_wfhw); in pwm_get_state_hw()
755 ret = __pwm_read_waveform(chip, pwm, &wfhw); in pwm_get_state_hw()
759 ret = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf); in pwm_get_state_hw()
765 } else if (ops->get_state) { in pwm_get_state_hw()
766 ret = ops->get_state(chip, pwm, state); in pwm_get_state_hw()
767 trace_pwm_get(pwm, state, ret); in pwm_get_state_hw()
775 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
776 * @pwm: PWM device
778 * This function will adjust the PWM config to the PWM arguments provided
779 * by the DT or PWM lookup table. This is particularly useful to adapt
782 int pwm_adjust_config(struct pwm_device *pwm) in pwm_adjust_config() argument
787 pwm_get_args(pwm, &pargs); in pwm_adjust_config()
788 pwm_get_state(pwm, &state); in pwm_adjust_config()
791 * If the current period is zero it means that either the PWM driver in pwm_adjust_config()
792 * does not support initial state retrieval or the PWM has not yet in pwm_adjust_config()
803 return pwm_apply_might_sleep(pwm, &state); in pwm_adjust_config()
807 * Adjust the PWM duty cycle/period based on the period value provided in pwm_adjust_config()
808 * in PWM args. in pwm_adjust_config()
823 state.duty_cycle = state.period - state.duty_cycle; in pwm_adjust_config()
826 return pwm_apply_might_sleep(pwm, &state); in pwm_adjust_config()
831 * pwm_capture() - capture and report a PWM signal
832 * @pwm: PWM device
838 static int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, in pwm_capture() argument
841 struct pwm_chip *chip = pwm->chip; in pwm_capture()
842 const struct pwm_ops *ops = chip->ops; in pwm_capture()
844 if (!ops->capture) in pwm_capture()
845 return -ENOSYS; in pwm_capture()
856 if (!chip->operational) in pwm_capture()
857 return -ENODEV; in pwm_capture()
859 return ops->capture(chip, pwm, result, timeout); in pwm_capture()
880 static int pwm_device_request(struct pwm_device *pwm, const char *label) in pwm_device_request() argument
883 struct pwm_chip *chip = pwm->chip; in pwm_device_request()
884 const struct pwm_ops *ops = chip->ops; in pwm_device_request()
886 if (test_bit(PWMF_REQUESTED, &pwm->flags)) in pwm_device_request()
887 return -EBUSY; in pwm_device_request()
894 if (!chip->operational) in pwm_device_request()
895 return -ENODEV; in pwm_device_request()
897 if (!try_module_get(chip->owner)) in pwm_device_request()
898 return -ENODEV; in pwm_device_request()
900 if (!get_device(&chip->dev)) { in pwm_device_request()
901 err = -ENODEV; in pwm_device_request()
905 if (ops->request) { in pwm_device_request()
906 err = ops->request(chip, pwm); in pwm_device_request()
908 put_device(&chip->dev); in pwm_device_request()
910 module_put(chip->owner); in pwm_device_request()
915 if (ops->read_waveform || ops->get_state) { in pwm_device_request()
917 * Zero-initialize state because most drivers are unaware of in pwm_device_request()
925 err = pwm_get_state_hw(pwm, &state); in pwm_device_request()
927 pwm->state = state; in pwm_device_request()
930 pwm->last = pwm->state; in pwm_device_request()
933 set_bit(PWMF_REQUESTED, &pwm->flags); in pwm_device_request()
934 pwm->label = label; in pwm_device_request()
940 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
941 * @chip: PWM chip
942 * @index: per-chip index of the PWM to request
943 * @label: a literal description string of this PWM
945 * Returns: A pointer to the PWM device at the given index of the given PWM
947 * specified PWM chip or if the PWM device cannot be requested.
953 struct pwm_device *pwm; in pwm_request_from_chip() local
956 if (!chip || index >= chip->npwm) in pwm_request_from_chip()
957 return ERR_PTR(-EINVAL); in pwm_request_from_chip()
961 pwm = &chip->pwms[index]; in pwm_request_from_chip()
963 err = pwm_device_request(pwm, label); in pwm_request_from_chip()
967 return pwm; in pwm_request_from_chip()
973 struct pwm_device *pwm; in of_pwm_xlate_with_flags() local
976 if (args->args_count < 1) in of_pwm_xlate_with_flags()
977 return ERR_PTR(-EINVAL); in of_pwm_xlate_with_flags()
979 pwm = pwm_request_from_chip(chip, args->args[0], NULL); in of_pwm_xlate_with_flags()
980 if (IS_ERR(pwm)) in of_pwm_xlate_with_flags()
981 return pwm; in of_pwm_xlate_with_flags()
983 if (args->args_count > 1) in of_pwm_xlate_with_flags()
984 pwm->args.period = args->args[1]; in of_pwm_xlate_with_flags()
986 pwm->args.polarity = PWM_POLARITY_NORMAL; in of_pwm_xlate_with_flags()
987 if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED) in of_pwm_xlate_with_flags()
988 pwm->args.polarity = PWM_POLARITY_INVERSED; in of_pwm_xlate_with_flags()
990 return pwm; in of_pwm_xlate_with_flags()
997 struct pwm_device *pwm; in of_pwm_single_xlate() local
999 pwm = pwm_request_from_chip(chip, 0, NULL); in of_pwm_single_xlate()
1000 if (IS_ERR(pwm)) in of_pwm_single_xlate()
1001 return pwm; in of_pwm_single_xlate()
1003 if (args->args_count > 0) in of_pwm_single_xlate()
1004 pwm->args.period = args->args[0]; in of_pwm_single_xlate()
1006 pwm->args.polarity = PWM_POLARITY_NORMAL; in of_pwm_single_xlate()
1007 if (args->args_count > 1 && args->args[1] & PWM_POLARITY_INVERTED) in of_pwm_single_xlate()
1008 pwm->args.polarity = PWM_POLARITY_INVERSED; in of_pwm_single_xlate()
1010 return pwm; in of_pwm_single_xlate()
1016 struct pwm_device *pwm; member
1035 return export->pwm; in pwm_from_dev()
1042 const struct pwm_device *pwm = pwm_from_dev(pwm_dev); in period_show() local
1045 pwm_get_state(pwm, &state); in period_show()
1055 struct pwm_device *pwm = export->pwm; in period_store() local
1064 guard(mutex)(&export->lock); in period_store()
1066 pwm_get_state(pwm, &state); in period_store()
1068 ret = pwm_apply_might_sleep(pwm, &state); in period_store()
1077 const struct pwm_device *pwm = pwm_from_dev(pwm_dev); in duty_cycle_show() local
1080 pwm_get_state(pwm, &state); in duty_cycle_show()
1090 struct pwm_device *pwm = export->pwm; in duty_cycle_store() local
1099 guard(mutex)(&export->lock); in duty_cycle_store()
1101 pwm_get_state(pwm, &state); in duty_cycle_store()
1103 ret = pwm_apply_might_sleep(pwm, &state); in duty_cycle_store()
1112 const struct pwm_device *pwm = pwm_from_dev(pwm_dev); in enable_show() local
1115 pwm_get_state(pwm, &state); in enable_show()
1125 struct pwm_device *pwm = export->pwm; in enable_store() local
1133 guard(mutex)(&export->lock); in enable_store()
1135 pwm_get_state(pwm, &state); in enable_store()
1145 return -EINVAL; in enable_store()
1148 ret = pwm_apply_might_sleep(pwm, &state); in enable_store()
1157 const struct pwm_device *pwm = pwm_from_dev(pwm_dev); in polarity_show() local
1161 pwm_get_state(pwm, &state); in polarity_show()
1181 struct pwm_device *pwm = export->pwm; in polarity_store() local
1191 return -EINVAL; in polarity_store()
1193 guard(mutex)(&export->lock); in polarity_store()
1195 pwm_get_state(pwm, &state); in polarity_store()
1197 ret = pwm_apply_might_sleep(pwm, &state); in polarity_store()
1206 struct pwm_device *pwm = pwm_from_dev(pwm_dev); in capture_show() local
1210 ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ)); in capture_show()
1231 ATTRIBUTE_GROUPS(pwm);
1240 static int pwm_export_child(struct device *pwmchip_dev, struct pwm_device *pwm) in pwm_export_child() argument
1246 if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags)) in pwm_export_child()
1247 return -EBUSY; in pwm_export_child()
1251 clear_bit(PWMF_EXPORTED, &pwm->flags); in pwm_export_child()
1252 return -ENOMEM; in pwm_export_child()
1255 export->pwm = pwm; in pwm_export_child()
1256 mutex_init(&export->lock); in pwm_export_child()
1258 export->pwm_dev.release = pwm_export_release; in pwm_export_child()
1259 export->pwm_dev.parent = pwmchip_dev; in pwm_export_child()
1260 export->pwm_dev.devt = MKDEV(0, 0); in pwm_export_child()
1261 export->pwm_dev.groups = pwm_groups; in pwm_export_child()
1262 dev_set_name(&export->pwm_dev, "pwm%u", pwm->hwpwm); in pwm_export_child()
1264 ret = device_register(&export->pwm_dev); in pwm_export_child()
1266 clear_bit(PWMF_EXPORTED, &pwm->flags); in pwm_export_child()
1267 put_device(&export->pwm_dev); in pwm_export_child()
1271 pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm); in pwm_export_child()
1273 kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop); in pwm_export_child()
1284 static int pwm_unexport_child(struct device *pwmchip_dev, struct pwm_device *pwm) in pwm_unexport_child() argument
1289 if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags)) in pwm_unexport_child()
1290 return -ENODEV; in pwm_unexport_child()
1292 pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match); in pwm_unexport_child()
1294 return -ENODEV; in pwm_unexport_child()
1296 pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm); in pwm_unexport_child()
1298 kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop); in pwm_unexport_child()
1304 pwm_put(pwm); in pwm_unexport_child()
1314 struct pwm_device *pwm; in export_store() local
1322 if (hwpwm >= chip->npwm) in export_store()
1323 return -ENODEV; in export_store()
1325 pwm = pwm_request_from_chip(chip, hwpwm, "sysfs"); in export_store()
1326 if (IS_ERR(pwm)) in export_store()
1327 return PTR_ERR(pwm); in export_store()
1329 ret = pwm_export_child(pwmchip_dev, pwm); in export_store()
1331 pwm_put(pwm); in export_store()
1349 if (hwpwm >= chip->npwm) in unexport_store()
1350 return -ENODEV; in unexport_store()
1352 ret = pwm_unexport_child(pwmchip_dev, &chip->pwms[hwpwm]); in unexport_store()
1363 return sysfs_emit(buf, "%u\n", chip->npwm); in npwm_show()
1375 /* takes export->lock on success */
1377 struct pwm_device *pwm, in pwm_class_get_state() argument
1383 if (!test_bit(PWMF_EXPORTED, &pwm->flags)) in pwm_class_get_state()
1386 pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match); in pwm_class_get_state()
1393 mutex_lock(&export->lock); in pwm_class_get_state()
1394 pwm_get_state(pwm, state); in pwm_class_get_state()
1400 struct pwm_device *pwm, in pwm_class_apply_state() argument
1403 int ret = pwm_apply_might_sleep(pwm, state); in pwm_class_apply_state()
1406 mutex_unlock(&export->lock); in pwm_class_apply_state()
1418 struct pwm_device *pwm = &chip->pwms[i]; in pwm_class_resume_npwm() local
1422 export = pwm_class_get_state(pwmchip_dev, pwm, &state); in pwm_class_resume_npwm()
1427 if (!export->suspend.enabled) { in pwm_class_resume_npwm()
1429 mutex_unlock(&export->lock); in pwm_class_resume_npwm()
1433 state.enabled = export->suspend.enabled; in pwm_class_resume_npwm()
1434 ret = pwm_class_apply_state(export, pwm, &state); in pwm_class_resume_npwm()
1448 for (i = 0; i < chip->npwm; i++) { in pwm_class_suspend()
1449 struct pwm_device *pwm = &chip->pwms[i]; in pwm_class_suspend() local
1453 export = pwm_class_get_state(pwmchip_dev, pwm, &state); in pwm_class_suspend()
1461 export->suspend = state; in pwm_class_suspend()
1464 mutex_unlock(&export->lock); in pwm_class_suspend()
1469 ret = pwm_class_apply_state(export, pwm, &state); in pwm_class_suspend()
1472 * roll back the PWM devices that were disabled by in pwm_class_suspend()
1487 return pwm_class_resume_npwm(pwmchip_dev, chip->npwm); in pwm_class_resume()
1493 .name = "pwm",
1502 for (i = 0; i < chip->npwm; i++) { in pwmchip_sysfs_unexport()
1503 struct pwm_device *pwm = &chip->pwms[i]; in pwmchip_sysfs_unexport() local
1505 if (test_bit(PWMF_EXPORTED, &pwm->flags)) in pwmchip_sysfs_unexport()
1506 pwm_unexport_child(&chip->dev, pwm); in pwmchip_sysfs_unexport()
1514 return (void *)chip + ALIGN(struct_size(chip, pwms, chip->npwm), PWMCHIP_ALIGN); in pwmchip_priv()
1520 put_device(&chip->dev); in pwmchip_put()
1543 return ERR_PTR(-ENOMEM); in pwmchip_alloc()
1545 chip->npwm = npwm; in pwmchip_alloc()
1546 chip->uses_pwmchip_alloc = true; in pwmchip_alloc()
1547 chip->operational = false; in pwmchip_alloc()
1549 pwmchip_dev = &chip->dev; in pwmchip_alloc()
1551 pwmchip_dev->class = &pwm_class; in pwmchip_alloc()
1552 pwmchip_dev->parent = parent; in pwmchip_alloc()
1553 pwmchip_dev->release = pwmchip_release; in pwmchip_alloc()
1557 for (i = 0; i < chip->npwm; i++) { in pwmchip_alloc()
1558 struct pwm_device *pwm = &chip->pwms[i]; in pwmchip_alloc() local
1559 pwm->chip = chip; in pwmchip_alloc()
1560 pwm->hwpwm = i; in pwmchip_alloc()
1593 if (!pwmchip_parent(chip) || !pwmchip_parent(chip)->of_node) in of_pwmchip_add()
1596 if (!chip->of_xlate) in of_pwmchip_add()
1597 chip->of_xlate = of_pwm_xlate_with_flags; in of_pwmchip_add()
1599 of_node_get(pwmchip_parent(chip)->of_node); in of_pwmchip_add()
1605 of_node_put(pwmchip_parent(chip)->of_node); in of_pwmchip_remove()
1610 const struct pwm_ops *ops = chip->ops; in pwm_ops_check()
1612 if (ops->write_waveform) { in pwm_ops_check()
1613 if (!ops->round_waveform_tohw || in pwm_ops_check()
1614 !ops->round_waveform_fromhw || in pwm_ops_check()
1615 !ops->write_waveform) in pwm_ops_check()
1618 if (WFHWSIZE < ops->sizeof_wfhw) { in pwm_ops_check()
1619 dev_warn(pwmchip_parent(chip), "WFHWSIZE < %zu\n", ops->sizeof_wfhw); in pwm_ops_check()
1623 if (!ops->apply) in pwm_ops_check()
1626 if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state) in pwm_ops_check()
1635 struct pwm_device *pwm) in pwm_device_link_add() argument
1641 * No device for the PWM consumer has been provided. It may in pwm_device_link_add()
1642 * impact the PM sequence ordering: the PWM supplier may get in pwm_device_link_add()
1645 dev_warn(pwmchip_parent(pwm->chip), in pwm_device_link_add()
1650 dl = device_link_add(dev, pwmchip_parent(pwm->chip), DL_FLAG_AUTOREMOVE_CONSUMER); in pwm_device_link_add()
1653 dev_name(pwmchip_parent(pwm->chip))); in pwm_device_link_add()
1654 return ERR_PTR(-EINVAL); in pwm_device_link_add()
1671 return ERR_PTR(-EPROBE_DEFER); in fwnode_to_pwmchip()
1675 * of_pwm_get() - request a PWM via the PWM framework
1676 * @dev: device for PWM consumer
1677 * @np: device node to get the PWM from
1680 * Returns the PWM device parsed from the phandle and index specified in the
1681 * "pwms" property of a device tree node or a negative error-code on failure.
1682 * Values parsed from the device tree are stored in the returned PWM device
1685 * If con_id is NULL, the first PWM device listed in the "pwms" property will
1686 * be requested. Otherwise the "pwm-names" property is used to do a reverse
1687 * lookup of the PWM index. This also means that the "pwm-names" property
1688 * becomes mandatory for devices that look up the PWM device via the con_id
1691 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1697 struct pwm_device *pwm = NULL; in of_pwm_get() local
1705 index = of_property_match_string(np, "pwm-names", con_id); in of_pwm_get()
1710 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index, in of_pwm_get()
1719 if (PTR_ERR(chip) != -EPROBE_DEFER) in of_pwm_get()
1720 pr_err("%s(): PWM chip not found\n", __func__); in of_pwm_get()
1722 pwm = ERR_CAST(chip); in of_pwm_get()
1726 pwm = chip->of_xlate(chip, &args); in of_pwm_get()
1727 if (IS_ERR(pwm)) in of_pwm_get()
1730 dl = pwm_device_link_add(dev, pwm); in of_pwm_get()
1733 pwm_put(pwm); in of_pwm_get()
1734 pwm = ERR_CAST(dl); in of_pwm_get()
1740 * "pwm-names" property if it exists. Otherwise use the name of in of_pwm_get()
1744 err = of_property_read_string_index(np, "pwm-names", index, in of_pwm_get()
1747 con_id = np->name; in of_pwm_get()
1750 pwm->label = con_id; in of_pwm_get()
1755 return pwm; in of_pwm_get()
1759 * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
1762 * Returns the PWM device parsed from the fwnode and index specified in the
1763 * "pwms" property or a negative error-code on failure.
1764 * Values parsed from the device tree are stored in the returned PWM device
1770 * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
1772 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1777 struct pwm_device *pwm; in acpi_pwm_get() local
1789 return ERR_PTR(-EPROTO); in acpi_pwm_get()
1795 pwm = pwm_request_from_chip(chip, args.args[0], NULL); in acpi_pwm_get()
1796 if (IS_ERR(pwm)) in acpi_pwm_get()
1797 return pwm; in acpi_pwm_get()
1799 pwm->args.period = args.args[1]; in acpi_pwm_get()
1800 pwm->args.polarity = PWM_POLARITY_NORMAL; in acpi_pwm_get()
1803 pwm->args.polarity = PWM_POLARITY_INVERSED; in acpi_pwm_get()
1805 return pwm; in acpi_pwm_get()
1812 * pwm_get() - look up and request a PWM device
1813 * @dev: device for PWM consumer
1817 * a device tree, a PWM chip and a relative index is looked up via a table
1820 * Once a PWM chip has been found the specified PWM device will be requested
1823 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1830 struct pwm_device *pwm; in pwm_get() local
1844 pwm = acpi_pwm_get(fwnode); in pwm_get()
1845 if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT) in pwm_get()
1846 return pwm; in pwm_get()
1856 * If a match is found, the provider PWM chip is looked up by name in pwm_get()
1857 * and a PWM device is requested using the PWM device per-chip index. in pwm_get()
1866 * Then we take the most specific entry - with the following order in pwm_get()
1873 if (p->dev_id) { in pwm_get()
1874 if (!dev_id || strcmp(p->dev_id, dev_id)) in pwm_get()
1880 if (p->con_id) { in pwm_get()
1881 if (!con_id || strcmp(p->con_id, con_id)) in pwm_get()
1898 return ERR_PTR(-ENODEV); in pwm_get()
1900 chip = pwmchip_find_by_name(chosen->provider); in pwm_get()
1904 * the PWM chip lookup. This can be used to work around driver load in pwm_get()
1908 if (!chip && chosen->module) { in pwm_get()
1909 err = request_module(chosen->module); in pwm_get()
1911 chip = pwmchip_find_by_name(chosen->provider); in pwm_get()
1915 return ERR_PTR(-EPROBE_DEFER); in pwm_get()
1917 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id); in pwm_get()
1918 if (IS_ERR(pwm)) in pwm_get()
1919 return pwm; in pwm_get()
1921 dl = pwm_device_link_add(dev, pwm); in pwm_get()
1923 pwm_put(pwm); in pwm_get()
1927 pwm->args.period = chosen->period; in pwm_get()
1928 pwm->args.polarity = chosen->polarity; in pwm_get()
1930 return pwm; in pwm_get()
1935 * pwm_put() - release a PWM device
1936 * @pwm: PWM device
1938 void pwm_put(struct pwm_device *pwm) in pwm_put() argument
1942 if (!pwm) in pwm_put()
1945 chip = pwm->chip; in pwm_put()
1954 if (chip->operational && !test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) { in pwm_put()
1955 pr_warn("PWM device already freed\n"); in pwm_put()
1959 if (chip->operational && chip->ops->free) in pwm_put()
1960 pwm->chip->ops->free(pwm->chip, pwm); in pwm_put()
1962 pwm->label = NULL; in pwm_put()
1964 put_device(&chip->dev); in pwm_put()
1966 module_put(chip->owner); in pwm_put()
1970 static void devm_pwm_release(void *pwm) in devm_pwm_release() argument
1972 pwm_put(pwm); in devm_pwm_release()
1976 * devm_pwm_get() - resource managed pwm_get()
1977 * @dev: device for PWM consumer
1980 * This function performs like pwm_get() but the acquired PWM device will
1983 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1988 struct pwm_device *pwm; in devm_pwm_get() local
1991 pwm = pwm_get(dev, con_id); in devm_pwm_get()
1992 if (IS_ERR(pwm)) in devm_pwm_get()
1993 return pwm; in devm_pwm_get()
1995 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm); in devm_pwm_get()
1999 return pwm; in devm_pwm_get()
2004 * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
2005 * @dev: device for PWM consumer
2006 * @fwnode: firmware node to get the PWM from
2009 * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
2012 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
2019 struct pwm_device *pwm = ERR_PTR(-ENODEV); in devm_fwnode_pwm_get() local
2023 pwm = of_pwm_get(dev, to_of_node(fwnode), con_id); in devm_fwnode_pwm_get()
2025 pwm = acpi_pwm_get(fwnode); in devm_fwnode_pwm_get()
2026 if (IS_ERR(pwm)) in devm_fwnode_pwm_get()
2027 return pwm; in devm_fwnode_pwm_get()
2029 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm); in devm_fwnode_pwm_get()
2033 return pwm; in devm_fwnode_pwm_get()
2038 * __pwmchip_add() - register a new PWM chip
2039 * @chip: the PWM chip to add
2042 * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the
2051 if (!chip || !pwmchip_parent(chip) || !chip->ops || !chip->npwm) in __pwmchip_add()
2052 return -EINVAL; in __pwmchip_add()
2060 if (!chip->uses_pwmchip_alloc) in __pwmchip_add()
2061 return -EINVAL; in __pwmchip_add()
2064 return -EINVAL; in __pwmchip_add()
2066 chip->owner = owner; in __pwmchip_add()
2068 if (chip->atomic) in __pwmchip_add()
2069 spin_lock_init(&chip->atomic_lock); in __pwmchip_add()
2071 mutex_init(&chip->nonatomic_lock); in __pwmchip_add()
2079 chip->id = ret; in __pwmchip_add()
2081 dev_set_name(&chip->dev, "pwmchip%u", chip->id); in __pwmchip_add()
2087 chip->operational = true; in __pwmchip_add()
2089 ret = device_add(&chip->dev); in __pwmchip_add()
2097 chip->operational = false; in __pwmchip_add()
2102 idr_remove(&pwm_chips, chip->id); in __pwmchip_add()
2109 * pwmchip_remove() - remove a PWM chip
2110 * @chip: the PWM chip to remove
2112 * Removes a PWM chip.
2122 chip->operational = false; in pwmchip_remove()
2124 for (i = 0; i < chip->npwm; ++i) { in pwmchip_remove()
2125 struct pwm_device *pwm = &chip->pwms[i]; in pwmchip_remove() local
2127 if (test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) { in pwmchip_remove()
2128 dev_warn(&chip->dev, "Freeing requested PWM #%u\n", i); in pwmchip_remove()
2129 if (pwm->chip->ops->free) in pwmchip_remove()
2130 pwm->chip->ops->free(pwm->chip, pwm); in pwmchip_remove()
2137 idr_remove(&pwm_chips, chip->id); in pwmchip_remove()
2140 device_del(&chip->dev); in pwmchip_remove()
2164 * pwm_add_table() - register PWM device consumers
2172 while (num--) { in pwm_add_table()
2173 list_add_tail(&table->list, &pwm_lookup_list); in pwm_add_table()
2179 * pwm_remove_table() - unregister PWM device consumers
2187 while (num--) { in pwm_remove_table()
2188 list_del(&table->list); in pwm_remove_table()
2197 for (i = 0; i < chip->npwm; i++) { in pwm_dbg_show()
2198 struct pwm_device *pwm = &chip->pwms[i]; in pwm_dbg_show() local
2201 pwm_get_state(pwm, &state); in pwm_dbg_show()
2203 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label); in pwm_dbg_show()
2205 if (test_bit(PWMF_REQUESTED, &pwm->flags)) in pwm_dbg_show()
2229 s->private = ""; in pwm_seq_start()
2241 s->private = "\n"; in pwm_seq_next()
2257 seq_printf(s, "%s%d: %s/%s, %d PWM device%s\n", in pwm_seq_show()
2258 (char *)s->private, chip->id, in pwm_seq_show()
2259 pwmchip_parent(chip)->bus ? pwmchip_parent(chip)->bus->name : "no-bus", in pwm_seq_show()
2260 dev_name(pwmchip_parent(chip)), chip->npwm, in pwm_seq_show()
2261 (chip->npwm != 1) ? "s" : ""); in pwm_seq_show()
2283 pr_err("Failed to initialize PWM class (%pe)\n", ERR_PTR(ret)); in pwm_init()
2288 debugfs_create_file("pwm", 0444, NULL, NULL, &pwm_debugfs_fops); in pwm_init()