Lines Matching +full:pwm +full:- +full:period

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
218 * because hardware only supports coarse period resolution or no duty_offset.
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 err = __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()
389 * pwm_set_waveform_might_sleep - Apply a new waveform
391 * @pwm: PWM device
399 * exact = false you get a period of 93.5 ns (i.e. the biggest period not bigger
403 * exact = true, you get the hardware configured with period = 93.5 ns.
405 int pwm_set_waveform_might_sleep(struct pwm_device *pwm, in pwm_set_waveform_might_sleep() argument
408 struct pwm_chip *chip = pwm->chip; in pwm_set_waveform_might_sleep()
415 if (!chip->operational) in pwm_set_waveform_might_sleep()
416 return -ENODEV; in pwm_set_waveform_might_sleep()
418 if (IS_ENABLED(CONFIG_PWM_DEBUG) && chip->atomic) { in pwm_set_waveform_might_sleep()
424 err = __pwm_set_waveform(pwm, wf, exact); in pwm_set_waveform_might_sleep()
427 err = __pwm_set_waveform(pwm, wf, exact); in pwm_set_waveform_might_sleep()
434 static void pwm_apply_debug(struct pwm_device *pwm, in pwm_apply_debug() argument
437 struct pwm_state *last = &pwm->last; in pwm_apply_debug()
438 struct pwm_chip *chip = pwm->chip; in pwm_apply_debug()
446 if (!chip->ops->get_state) in pwm_apply_debug()
454 err = chip->ops->get_state(chip, pwm, &s1); in pwm_apply_debug()
455 trace_pwm_get(pwm, &s1, err); in pwm_apply_debug()
465 if (s1.enabled && s1.polarity != state->polarity) { in pwm_apply_debug()
466 s2.polarity = state->polarity; in pwm_apply_debug()
467 s2.duty_cycle = s1.period - s1.duty_cycle; in pwm_apply_debug()
468 s2.period = s1.period; in pwm_apply_debug()
474 if (s2.polarity != state->polarity && in pwm_apply_debug()
475 state->duty_cycle < state->period) in pwm_apply_debug()
478 if (state->enabled && s2.enabled && in pwm_apply_debug()
479 last->polarity == state->polarity && in pwm_apply_debug()
480 last->period > s2.period && in pwm_apply_debug()
481 last->period <= state->period) in pwm_apply_debug()
483 … ".apply didn't pick the best available period (requested: %llu, applied: %llu, possible: %llu)\n", in pwm_apply_debug()
484 state->period, s2.period, last->period); in pwm_apply_debug()
487 * Rounding period up is fine only if duty_cycle is 0 then, because a in pwm_apply_debug()
488 * flat line doesn't have a characteristic period. in pwm_apply_debug()
490 if (state->enabled && s2.enabled && state->period < s2.period && s2.duty_cycle) in pwm_apply_debug()
492 ".apply is supposed to round down period (requested: %llu, applied: %llu)\n", in pwm_apply_debug()
493 state->period, s2.period); in pwm_apply_debug()
495 if (state->enabled && in pwm_apply_debug()
496 last->polarity == state->polarity && in pwm_apply_debug()
497 last->period == s2.period && in pwm_apply_debug()
498 last->duty_cycle > s2.duty_cycle && in pwm_apply_debug()
499 last->duty_cycle <= state->duty_cycle) in pwm_apply_debug()
502 state->duty_cycle, state->period, in pwm_apply_debug()
503 s2.duty_cycle, s2.period, in pwm_apply_debug()
504 last->duty_cycle, last->period); in pwm_apply_debug()
506 if (state->enabled && s2.enabled && state->duty_cycle < s2.duty_cycle) in pwm_apply_debug()
509 state->duty_cycle, state->period, in pwm_apply_debug()
510 s2.duty_cycle, s2.period); in pwm_apply_debug()
512 if (!state->enabled && s2.enabled && s2.duty_cycle > 0) in pwm_apply_debug()
517 err = chip->ops->apply(chip, pwm, &s1); in pwm_apply_debug()
518 trace_pwm_apply(pwm, &s1, err); in pwm_apply_debug()
526 err = chip->ops->get_state(chip, pwm, last); in pwm_apply_debug()
527 trace_pwm_get(pwm, last, err); in pwm_apply_debug()
532 if (s1.enabled != last->enabled || in pwm_apply_debug()
533 s1.polarity != last->polarity || in pwm_apply_debug()
534 (s1.enabled && s1.period != last->period) || in pwm_apply_debug()
535 (s1.enabled && s1.duty_cycle != last->duty_cycle)) { in pwm_apply_debug()
537 ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n", in pwm_apply_debug()
538 s1.enabled, s1.polarity, s1.duty_cycle, s1.period, in pwm_apply_debug()
539 last->enabled, last->polarity, last->duty_cycle, in pwm_apply_debug()
540 last->period); in pwm_apply_debug()
551 if (state->enabled) in pwm_state_valid()
554 if (!state->period) in pwm_state_valid()
557 if (state->duty_cycle > state->period) in pwm_state_valid()
564 * __pwm_apply() - atomically apply a new state to a PWM device
565 * @pwm: PWM device
568 static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state) in __pwm_apply() argument
574 if (!pwm || !state) in __pwm_apply()
575 return -EINVAL; in __pwm_apply()
581 * the period is zero. (This happens on stm32 when the hardware in __pwm_apply()
586 if (!pwm_state_valid(&pwm->state)) { in __pwm_apply()
587 pwm->state = *state; in __pwm_apply()
591 return -EINVAL; in __pwm_apply()
594 chip = pwm->chip; in __pwm_apply()
595 ops = chip->ops; in __pwm_apply()
597 if (state->period == pwm->state.period && in __pwm_apply()
598 state->duty_cycle == pwm->state.duty_cycle && in __pwm_apply()
599 state->polarity == pwm->state.polarity && in __pwm_apply()
600 state->enabled == pwm->state.enabled && in __pwm_apply()
601 state->usage_power == pwm->state.usage_power) in __pwm_apply()
608 BUG_ON(WFHWSIZE < ops->sizeof_wfhw); in __pwm_apply()
615 * time from the start of the period to the inner edge), in __pwm_apply()
616 * .round_waveform_tohw() rounds down the time the PWM is high. in __pwm_apply()
621 err = __pwm_round_waveform_tohw(chip, pwm, &wf, &wfhw); in __pwm_apply()
626 * the requested period (or duty_offset) is in __pwm_apply()
629 return -EINVAL; in __pwm_apply()
637 err = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf_rounded); in __pwm_apply()
642 dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n", in __pwm_apply()
647 err = __pwm_write_waveform(chip, pwm, &wfhw); in __pwm_apply()
651 pwm->state = *state; in __pwm_apply()
654 err = ops->apply(chip, pwm, state); in __pwm_apply()
655 trace_pwm_apply(pwm, state, err); in __pwm_apply()
659 pwm->state = *state; in __pwm_apply()
662 * only do this after pwm->state was applied as some in __pwm_apply()
665 pwm_apply_debug(pwm, state); in __pwm_apply()
672 * pwm_apply_might_sleep() - atomically apply a new state to a PWM device
674 * @pwm: PWM device
677 int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state) in pwm_apply_might_sleep() argument
680 struct pwm_chip *chip = pwm->chip; in pwm_apply_might_sleep()
693 if (!chip->operational) in pwm_apply_might_sleep()
694 return -ENODEV; in pwm_apply_might_sleep()
696 if (IS_ENABLED(CONFIG_PWM_DEBUG) && chip->atomic) { in pwm_apply_might_sleep()
702 err = __pwm_apply(pwm, state); in pwm_apply_might_sleep()
705 err = __pwm_apply(pwm, state); in pwm_apply_might_sleep()
713 * pwm_apply_atomic() - apply a new state to a PWM device from atomic context
714 * Not all PWM devices support this function, check with pwm_might_sleep().
715 * @pwm: PWM device
718 int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state) in pwm_apply_atomic() argument
720 struct pwm_chip *chip = pwm->chip; in pwm_apply_atomic()
722 WARN_ONCE(!chip->atomic, in pwm_apply_atomic()
723 "sleeping PWM driver used in atomic context\n"); in pwm_apply_atomic()
727 if (!chip->operational) in pwm_apply_atomic()
728 return -ENODEV; in pwm_apply_atomic()
730 return __pwm_apply(pwm, state); in pwm_apply_atomic()
735 * pwm_get_state_hw() - get the current PWM state from hardware
736 * @pwm: PWM device
737 * @state: state to fill with the current PWM state
739 * Similar to pwm_get_state() but reads the current PWM state from hardware
745 int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state) in pwm_get_state_hw() argument
747 struct pwm_chip *chip = pwm->chip; in pwm_get_state_hw()
748 const struct pwm_ops *ops = chip->ops; in pwm_get_state_hw()
749 int ret = -EOPNOTSUPP; in pwm_get_state_hw()
755 if (!chip->operational) in pwm_get_state_hw()
756 return -ENODEV; in pwm_get_state_hw()
758 if (pwmchip_supports_waveform(chip) && ops->read_waveform) { in pwm_get_state_hw()
762 BUG_ON(WFHWSIZE < ops->sizeof_wfhw); in pwm_get_state_hw()
764 ret = __pwm_read_waveform(chip, pwm, &wfhw); in pwm_get_state_hw()
768 ret = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf); in pwm_get_state_hw()
774 } else if (ops->get_state) { in pwm_get_state_hw()
775 ret = ops->get_state(chip, pwm, state); in pwm_get_state_hw()
776 trace_pwm_get(pwm, state, ret); in pwm_get_state_hw()
784 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
785 * @pwm: PWM device
787 * This function will adjust the PWM config to the PWM arguments provided
788 * by the DT or PWM lookup table. This is particularly useful to adapt
791 int pwm_adjust_config(struct pwm_device *pwm) in pwm_adjust_config() argument
796 pwm_get_args(pwm, &pargs); in pwm_adjust_config()
797 pwm_get_state(pwm, &state); in pwm_adjust_config()
800 * If the current period is zero it means that either the PWM driver in pwm_adjust_config()
801 * does not support initial state retrieval or the PWM has not yet in pwm_adjust_config()
804 * In either case, we setup the new period and polarity, and assign a in pwm_adjust_config()
807 if (!state.period) { in pwm_adjust_config()
809 state.period = pargs.period; in pwm_adjust_config()
812 return pwm_apply_might_sleep(pwm, &state); in pwm_adjust_config()
816 * Adjust the PWM duty cycle/period based on the period value provided in pwm_adjust_config()
817 * in PWM args. in pwm_adjust_config()
819 if (pargs.period != state.period) { in pwm_adjust_config()
820 u64 dutycycle = (u64)state.duty_cycle * pargs.period; in pwm_adjust_config()
822 do_div(dutycycle, state.period); in pwm_adjust_config()
824 state.period = pargs.period; in pwm_adjust_config()
832 state.duty_cycle = state.period - state.duty_cycle; in pwm_adjust_config()
835 return pwm_apply_might_sleep(pwm, &state); in pwm_adjust_config()
840 * pwm_capture() - capture and report a PWM signal
841 * @pwm: PWM device
847 static int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, in pwm_capture() argument
850 struct pwm_chip *chip = pwm->chip; in pwm_capture()
851 const struct pwm_ops *ops = chip->ops; in pwm_capture()
853 if (!ops->capture) in pwm_capture()
854 return -ENOSYS; in pwm_capture()
865 if (!chip->operational) in pwm_capture()
866 return -ENODEV; in pwm_capture()
868 return ops->capture(chip, pwm, result, timeout); in pwm_capture()
889 static int pwm_device_request(struct pwm_device *pwm, const char *label) in pwm_device_request() argument
892 struct pwm_chip *chip = pwm->chip; in pwm_device_request()
893 const struct pwm_ops *ops = chip->ops; in pwm_device_request()
895 if (test_bit(PWMF_REQUESTED, &pwm->flags)) in pwm_device_request()
896 return -EBUSY; in pwm_device_request()
903 if (!chip->operational) in pwm_device_request()
904 return -ENODEV; in pwm_device_request()
906 if (!try_module_get(chip->owner)) in pwm_device_request()
907 return -ENODEV; in pwm_device_request()
909 if (!get_device(&chip->dev)) { in pwm_device_request()
910 err = -ENODEV; in pwm_device_request()
914 if (ops->request) { in pwm_device_request()
915 err = ops->request(chip, pwm); in pwm_device_request()
917 put_device(&chip->dev); in pwm_device_request()
919 module_put(chip->owner); in pwm_device_request()
924 if (ops->read_waveform || ops->get_state) { in pwm_device_request()
926 * Zero-initialize state because most drivers are unaware of in pwm_device_request()
934 err = pwm_get_state_hw(pwm, &state); in pwm_device_request()
936 pwm->state = state; in pwm_device_request()
939 pwm->last = pwm->state; in pwm_device_request()
942 set_bit(PWMF_REQUESTED, &pwm->flags); in pwm_device_request()
943 pwm->label = label; in pwm_device_request()
949 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
950 * @chip: PWM chip
951 * @index: per-chip index of the PWM to request
952 * @label: a literal description string of this PWM
954 * Returns: A pointer to the PWM device at the given index of the given PWM
956 * specified PWM chip or if the PWM device cannot be requested.
962 struct pwm_device *pwm; in pwm_request_from_chip() local
965 if (!chip || index >= chip->npwm) in pwm_request_from_chip()
966 return ERR_PTR(-EINVAL); in pwm_request_from_chip()
970 pwm = &chip->pwms[index]; in pwm_request_from_chip()
972 err = pwm_device_request(pwm, label); in pwm_request_from_chip()
976 return pwm; in pwm_request_from_chip()
982 struct pwm_device *pwm; in of_pwm_xlate_with_flags() local
984 /* period in the second cell and flags in the third cell are optional */ in of_pwm_xlate_with_flags()
985 if (args->args_count < 1) in of_pwm_xlate_with_flags()
986 return ERR_PTR(-EINVAL); in of_pwm_xlate_with_flags()
988 pwm = pwm_request_from_chip(chip, args->args[0], NULL); in of_pwm_xlate_with_flags()
989 if (IS_ERR(pwm)) in of_pwm_xlate_with_flags()
990 return pwm; in of_pwm_xlate_with_flags()
992 if (args->args_count > 1) in of_pwm_xlate_with_flags()
993 pwm->args.period = args->args[1]; in of_pwm_xlate_with_flags()
995 pwm->args.polarity = PWM_POLARITY_NORMAL; in of_pwm_xlate_with_flags()
996 if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED) in of_pwm_xlate_with_flags()
997 pwm->args.polarity = PWM_POLARITY_INVERSED; in of_pwm_xlate_with_flags()
999 return pwm; in of_pwm_xlate_with_flags()
1006 struct pwm_device *pwm; in of_pwm_single_xlate() local
1008 pwm = pwm_request_from_chip(chip, 0, NULL); in of_pwm_single_xlate()
1009 if (IS_ERR(pwm)) in of_pwm_single_xlate()
1010 return pwm; in of_pwm_single_xlate()
1012 if (args->args_count > 0) in of_pwm_single_xlate()
1013 pwm->args.period = args->args[0]; in of_pwm_single_xlate()
1015 pwm->args.polarity = PWM_POLARITY_NORMAL; in of_pwm_single_xlate()
1016 if (args->args_count > 1 && args->args[1] & PWM_POLARITY_INVERTED) in of_pwm_single_xlate()
1017 pwm->args.polarity = PWM_POLARITY_INVERSED; in of_pwm_single_xlate()
1019 return pwm; in of_pwm_single_xlate()
1025 struct pwm_device *pwm; member
1044 return export->pwm; in pwm_from_dev()
1051 const struct pwm_device *pwm = pwm_from_dev(pwm_dev); in period_show() local
1054 pwm_get_state(pwm, &state); in period_show()
1056 return sysfs_emit(buf, "%llu\n", state.period); in period_show()
1064 struct pwm_device *pwm = export->pwm; in period_store() local
1073 guard(mutex)(&export->lock); in period_store()
1075 pwm_get_state(pwm, &state); in period_store()
1076 state.period = val; in period_store()
1077 ret = pwm_apply_might_sleep(pwm, &state); in period_store()
1086 const struct pwm_device *pwm = pwm_from_dev(pwm_dev); in duty_cycle_show() local
1089 pwm_get_state(pwm, &state); in duty_cycle_show()
1099 struct pwm_device *pwm = export->pwm; in duty_cycle_store() local
1108 guard(mutex)(&export->lock); in duty_cycle_store()
1110 pwm_get_state(pwm, &state); in duty_cycle_store()
1112 ret = pwm_apply_might_sleep(pwm, &state); in duty_cycle_store()
1121 const struct pwm_device *pwm = pwm_from_dev(pwm_dev); in enable_show() local
1124 pwm_get_state(pwm, &state); in enable_show()
1134 struct pwm_device *pwm = export->pwm; in enable_store() local
1142 guard(mutex)(&export->lock); in enable_store()
1144 pwm_get_state(pwm, &state); in enable_store()
1154 return -EINVAL; in enable_store()
1157 ret = pwm_apply_might_sleep(pwm, &state); in enable_store()
1166 const struct pwm_device *pwm = pwm_from_dev(pwm_dev); in polarity_show() local
1170 pwm_get_state(pwm, &state); in polarity_show()
1190 struct pwm_device *pwm = export->pwm; in polarity_store() local
1200 return -EINVAL; in polarity_store()
1202 guard(mutex)(&export->lock); in polarity_store()
1204 pwm_get_state(pwm, &state); in polarity_store()
1206 ret = pwm_apply_might_sleep(pwm, &state); in polarity_store()
1215 struct pwm_device *pwm = pwm_from_dev(pwm_dev); in capture_show() local
1219 ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ)); in capture_show()
1223 return sysfs_emit(buf, "%u %u\n", result.period, result.duty_cycle); in capture_show()
1226 static DEVICE_ATTR_RW(period);
1240 ATTRIBUTE_GROUPS(pwm);
1249 static int pwm_export_child(struct device *pwmchip_dev, struct pwm_device *pwm) in pwm_export_child() argument
1255 if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags)) in pwm_export_child()
1256 return -EBUSY; in pwm_export_child()
1260 clear_bit(PWMF_EXPORTED, &pwm->flags); in pwm_export_child()
1261 return -ENOMEM; in pwm_export_child()
1264 export->pwm = pwm; in pwm_export_child()
1265 mutex_init(&export->lock); in pwm_export_child()
1267 export->pwm_dev.release = pwm_export_release; in pwm_export_child()
1268 export->pwm_dev.parent = pwmchip_dev; in pwm_export_child()
1269 export->pwm_dev.devt = MKDEV(0, 0); in pwm_export_child()
1270 export->pwm_dev.groups = pwm_groups; in pwm_export_child()
1271 dev_set_name(&export->pwm_dev, "pwm%u", pwm->hwpwm); in pwm_export_child()
1273 ret = device_register(&export->pwm_dev); in pwm_export_child()
1275 clear_bit(PWMF_EXPORTED, &pwm->flags); in pwm_export_child()
1276 put_device(&export->pwm_dev); in pwm_export_child()
1280 pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm); in pwm_export_child()
1282 kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop); in pwm_export_child()
1293 static int pwm_unexport_child(struct device *pwmchip_dev, struct pwm_device *pwm) in pwm_unexport_child() argument
1298 if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags)) in pwm_unexport_child()
1299 return -ENODEV; in pwm_unexport_child()
1301 pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match); in pwm_unexport_child()
1303 return -ENODEV; in pwm_unexport_child()
1305 pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm); in pwm_unexport_child()
1307 kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop); in pwm_unexport_child()
1313 pwm_put(pwm); in pwm_unexport_child()
1323 struct pwm_device *pwm; in export_store() local
1331 if (hwpwm >= chip->npwm) in export_store()
1332 return -ENODEV; in export_store()
1334 pwm = pwm_request_from_chip(chip, hwpwm, "sysfs"); in export_store()
1335 if (IS_ERR(pwm)) in export_store()
1336 return PTR_ERR(pwm); in export_store()
1338 ret = pwm_export_child(pwmchip_dev, pwm); in export_store()
1340 pwm_put(pwm); in export_store()
1358 if (hwpwm >= chip->npwm) in unexport_store()
1359 return -ENODEV; in unexport_store()
1361 ret = pwm_unexport_child(pwmchip_dev, &chip->pwms[hwpwm]); in unexport_store()
1372 return sysfs_emit(buf, "%u\n", chip->npwm); in npwm_show()
1384 /* takes export->lock on success */
1386 struct pwm_device *pwm, in pwm_class_get_state() argument
1392 if (!test_bit(PWMF_EXPORTED, &pwm->flags)) in pwm_class_get_state()
1395 pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match); in pwm_class_get_state()
1402 mutex_lock(&export->lock); in pwm_class_get_state()
1403 pwm_get_state(pwm, state); in pwm_class_get_state()
1409 struct pwm_device *pwm, in pwm_class_apply_state() argument
1412 int ret = pwm_apply_might_sleep(pwm, state); in pwm_class_apply_state()
1415 mutex_unlock(&export->lock); in pwm_class_apply_state()
1427 struct pwm_device *pwm = &chip->pwms[i]; in pwm_class_resume_npwm() local
1431 export = pwm_class_get_state(pwmchip_dev, pwm, &state); in pwm_class_resume_npwm()
1436 if (!export->suspend.enabled) { in pwm_class_resume_npwm()
1438 mutex_unlock(&export->lock); in pwm_class_resume_npwm()
1442 state.enabled = export->suspend.enabled; in pwm_class_resume_npwm()
1443 ret = pwm_class_apply_state(export, pwm, &state); in pwm_class_resume_npwm()
1457 for (i = 0; i < chip->npwm; i++) { in pwm_class_suspend()
1458 struct pwm_device *pwm = &chip->pwms[i]; in pwm_class_suspend() local
1462 export = pwm_class_get_state(pwmchip_dev, pwm, &state); in pwm_class_suspend()
1470 export->suspend = state; in pwm_class_suspend()
1473 mutex_unlock(&export->lock); in pwm_class_suspend()
1478 ret = pwm_class_apply_state(export, pwm, &state); in pwm_class_suspend()
1481 * roll back the PWM devices that were disabled by in pwm_class_suspend()
1496 return pwm_class_resume_npwm(pwmchip_dev, chip->npwm); in pwm_class_resume()
1502 .name = "pwm",
1511 for (i = 0; i < chip->npwm; i++) { in pwmchip_sysfs_unexport()
1512 struct pwm_device *pwm = &chip->pwms[i]; in pwmchip_sysfs_unexport() local
1514 if (test_bit(PWMF_EXPORTED, &pwm->flags)) in pwmchip_sysfs_unexport()
1515 pwm_unexport_child(&chip->dev, pwm); in pwmchip_sysfs_unexport()
1523 return (void *)chip + ALIGN(struct_size(chip, pwms, chip->npwm), PWMCHIP_ALIGN); in pwmchip_priv()
1529 put_device(&chip->dev); in pwmchip_put()
1552 return ERR_PTR(-ENOMEM); in pwmchip_alloc()
1554 chip->npwm = npwm; in pwmchip_alloc()
1555 chip->uses_pwmchip_alloc = true; in pwmchip_alloc()
1556 chip->operational = false; in pwmchip_alloc()
1558 pwmchip_dev = &chip->dev; in pwmchip_alloc()
1560 pwmchip_dev->class = &pwm_class; in pwmchip_alloc()
1561 pwmchip_dev->parent = parent; in pwmchip_alloc()
1562 pwmchip_dev->release = pwmchip_release; in pwmchip_alloc()
1566 for (i = 0; i < chip->npwm; i++) { in pwmchip_alloc()
1567 struct pwm_device *pwm = &chip->pwms[i]; in pwmchip_alloc() local
1568 pwm->chip = chip; in pwmchip_alloc()
1569 pwm->hwpwm = i; in pwmchip_alloc()
1602 if (!pwmchip_parent(chip) || !pwmchip_parent(chip)->of_node) in of_pwmchip_add()
1605 if (!chip->of_xlate) in of_pwmchip_add()
1606 chip->of_xlate = of_pwm_xlate_with_flags; in of_pwmchip_add()
1608 of_node_get(pwmchip_parent(chip)->of_node); in of_pwmchip_add()
1614 of_node_put(pwmchip_parent(chip)->of_node); in of_pwmchip_remove()
1619 const struct pwm_ops *ops = chip->ops; in pwm_ops_check()
1621 if (ops->write_waveform) { in pwm_ops_check()
1622 if (!ops->round_waveform_tohw || in pwm_ops_check()
1623 !ops->round_waveform_fromhw || in pwm_ops_check()
1624 !ops->write_waveform) in pwm_ops_check()
1627 if (WFHWSIZE < ops->sizeof_wfhw) { in pwm_ops_check()
1628 dev_warn(pwmchip_parent(chip), "WFHWSIZE < %zu\n", ops->sizeof_wfhw); in pwm_ops_check()
1632 if (!ops->apply) in pwm_ops_check()
1635 if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state) in pwm_ops_check()
1644 struct pwm_device *pwm) in pwm_device_link_add() argument
1650 * No device for the PWM consumer has been provided. It may in pwm_device_link_add()
1651 * impact the PM sequence ordering: the PWM supplier may get in pwm_device_link_add()
1654 dev_warn(pwmchip_parent(pwm->chip), in pwm_device_link_add()
1659 dl = device_link_add(dev, pwmchip_parent(pwm->chip), DL_FLAG_AUTOREMOVE_CONSUMER); in pwm_device_link_add()
1662 dev_name(pwmchip_parent(pwm->chip))); in pwm_device_link_add()
1663 return ERR_PTR(-EINVAL); in pwm_device_link_add()
1680 return ERR_PTR(-EPROBE_DEFER); in fwnode_to_pwmchip()
1684 * of_pwm_get() - request a PWM via the PWM framework
1685 * @dev: device for PWM consumer
1686 * @np: device node to get the PWM from
1689 * Returns the PWM device parsed from the phandle and index specified in the
1690 * "pwms" property of a device tree node or a negative error-code on failure.
1691 * Values parsed from the device tree are stored in the returned PWM device
1694 * If con_id is NULL, the first PWM device listed in the "pwms" property will
1695 * be requested. Otherwise the "pwm-names" property is used to do a reverse
1696 * lookup of the PWM index. This also means that the "pwm-names" property
1697 * becomes mandatory for devices that look up the PWM device via the con_id
1700 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1706 struct pwm_device *pwm = NULL; in of_pwm_get() local
1714 index = of_property_match_string(np, "pwm-names", con_id); in of_pwm_get()
1719 err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index, in of_pwm_get()
1728 if (PTR_ERR(chip) != -EPROBE_DEFER) in of_pwm_get()
1729 pr_err("%s(): PWM chip not found\n", __func__); in of_pwm_get()
1731 pwm = ERR_CAST(chip); in of_pwm_get()
1735 pwm = chip->of_xlate(chip, &args); in of_pwm_get()
1736 if (IS_ERR(pwm)) in of_pwm_get()
1739 dl = pwm_device_link_add(dev, pwm); in of_pwm_get()
1742 pwm_put(pwm); in of_pwm_get()
1743 pwm = ERR_CAST(dl); in of_pwm_get()
1749 * "pwm-names" property if it exists. Otherwise use the name of in of_pwm_get()
1753 err = of_property_read_string_index(np, "pwm-names", index, in of_pwm_get()
1756 con_id = np->name; in of_pwm_get()
1759 pwm->label = con_id; in of_pwm_get()
1764 return pwm; in of_pwm_get()
1768 * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
1771 * Returns the PWM device parsed from the fwnode and index specified in the
1772 * "pwms" property or a negative error-code on failure.
1773 * Values parsed from the device tree are stored in the returned PWM device
1779 * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
1781 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1786 struct pwm_device *pwm; in acpi_pwm_get() local
1798 return ERR_PTR(-EPROTO); in acpi_pwm_get()
1804 pwm = pwm_request_from_chip(chip, args.args[0], NULL); in acpi_pwm_get()
1805 if (IS_ERR(pwm)) in acpi_pwm_get()
1806 return pwm; in acpi_pwm_get()
1808 pwm->args.period = args.args[1]; in acpi_pwm_get()
1809 pwm->args.polarity = PWM_POLARITY_NORMAL; in acpi_pwm_get()
1812 pwm->args.polarity = PWM_POLARITY_INVERSED; in acpi_pwm_get()
1814 return pwm; in acpi_pwm_get()
1821 * pwm_get() - look up and request a PWM device
1822 * @dev: device for PWM consumer
1826 * a device tree, a PWM chip and a relative index is looked up via a table
1829 * Once a PWM chip has been found the specified PWM device will be requested
1832 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1839 struct pwm_device *pwm; in pwm_get() local
1853 pwm = acpi_pwm_get(fwnode); in pwm_get()
1854 if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT) in pwm_get()
1855 return pwm; in pwm_get()
1865 * If a match is found, the provider PWM chip is looked up by name in pwm_get()
1866 * and a PWM device is requested using the PWM device per-chip index. in pwm_get()
1875 * Then we take the most specific entry - with the following order in pwm_get()
1882 if (p->dev_id) { in pwm_get()
1883 if (!dev_id || strcmp(p->dev_id, dev_id)) in pwm_get()
1889 if (p->con_id) { in pwm_get()
1890 if (!con_id || strcmp(p->con_id, con_id)) in pwm_get()
1907 return ERR_PTR(-ENODEV); in pwm_get()
1909 chip = pwmchip_find_by_name(chosen->provider); in pwm_get()
1913 * the PWM chip lookup. This can be used to work around driver load in pwm_get()
1917 if (!chip && chosen->module) { in pwm_get()
1918 err = request_module(chosen->module); in pwm_get()
1920 chip = pwmchip_find_by_name(chosen->provider); in pwm_get()
1924 return ERR_PTR(-EPROBE_DEFER); in pwm_get()
1926 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id); in pwm_get()
1927 if (IS_ERR(pwm)) in pwm_get()
1928 return pwm; in pwm_get()
1930 dl = pwm_device_link_add(dev, pwm); in pwm_get()
1932 pwm_put(pwm); in pwm_get()
1936 pwm->args.period = chosen->period; in pwm_get()
1937 pwm->args.polarity = chosen->polarity; in pwm_get()
1939 return pwm; in pwm_get()
1944 * pwm_put() - release a PWM device
1945 * @pwm: PWM device
1947 void pwm_put(struct pwm_device *pwm) in pwm_put() argument
1951 if (!pwm) in pwm_put()
1954 chip = pwm->chip; in pwm_put()
1963 if (chip->operational && !test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) { in pwm_put()
1964 pr_warn("PWM device already freed\n"); in pwm_put()
1968 if (chip->operational && chip->ops->free) in pwm_put()
1969 pwm->chip->ops->free(pwm->chip, pwm); in pwm_put()
1971 pwm->label = NULL; in pwm_put()
1973 put_device(&chip->dev); in pwm_put()
1975 module_put(chip->owner); in pwm_put()
1979 static void devm_pwm_release(void *pwm) in devm_pwm_release() argument
1981 pwm_put(pwm); in devm_pwm_release()
1985 * devm_pwm_get() - resource managed pwm_get()
1986 * @dev: device for PWM consumer
1989 * This function performs like pwm_get() but the acquired PWM device will
1992 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1997 struct pwm_device *pwm; in devm_pwm_get() local
2000 pwm = pwm_get(dev, con_id); in devm_pwm_get()
2001 if (IS_ERR(pwm)) in devm_pwm_get()
2002 return pwm; in devm_pwm_get()
2004 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm); in devm_pwm_get()
2008 return pwm; in devm_pwm_get()
2013 * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
2014 * @dev: device for PWM consumer
2015 * @fwnode: firmware node to get the PWM from
2018 * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
2021 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
2028 struct pwm_device *pwm = ERR_PTR(-ENODEV); in devm_fwnode_pwm_get() local
2032 pwm = of_pwm_get(dev, to_of_node(fwnode), con_id); in devm_fwnode_pwm_get()
2034 pwm = acpi_pwm_get(fwnode); in devm_fwnode_pwm_get()
2035 if (IS_ERR(pwm)) in devm_fwnode_pwm_get()
2036 return pwm; in devm_fwnode_pwm_get()
2038 ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm); in devm_fwnode_pwm_get()
2042 return pwm; in devm_fwnode_pwm_get()
2047 * __pwmchip_add() - register a new PWM chip
2048 * @chip: the PWM chip to add
2051 * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the
2060 if (!chip || !pwmchip_parent(chip) || !chip->ops || !chip->npwm) in __pwmchip_add()
2061 return -EINVAL; in __pwmchip_add()
2069 if (!chip->uses_pwmchip_alloc) in __pwmchip_add()
2070 return -EINVAL; in __pwmchip_add()
2073 return -EINVAL; in __pwmchip_add()
2075 chip->owner = owner; in __pwmchip_add()
2077 if (chip->atomic) in __pwmchip_add()
2078 spin_lock_init(&chip->atomic_lock); in __pwmchip_add()
2080 mutex_init(&chip->nonatomic_lock); in __pwmchip_add()
2088 chip->id = ret; in __pwmchip_add()
2090 dev_set_name(&chip->dev, "pwmchip%u", chip->id); in __pwmchip_add()
2096 chip->operational = true; in __pwmchip_add()
2098 ret = device_add(&chip->dev); in __pwmchip_add()
2106 chip->operational = false; in __pwmchip_add()
2111 idr_remove(&pwm_chips, chip->id); in __pwmchip_add()
2118 * pwmchip_remove() - remove a PWM chip
2119 * @chip: the PWM chip to remove
2121 * Removes a PWM chip.
2131 chip->operational = false; in pwmchip_remove()
2133 for (i = 0; i < chip->npwm; ++i) { in pwmchip_remove()
2134 struct pwm_device *pwm = &chip->pwms[i]; in pwmchip_remove() local
2136 if (test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) { in pwmchip_remove()
2137 dev_warn(&chip->dev, "Freeing requested PWM #%u\n", i); in pwmchip_remove()
2138 if (pwm->chip->ops->free) in pwmchip_remove()
2139 pwm->chip->ops->free(pwm->chip, pwm); in pwmchip_remove()
2146 idr_remove(&pwm_chips, chip->id); in pwmchip_remove()
2149 device_del(&chip->dev); in pwmchip_remove()
2173 * pwm_add_table() - register PWM device consumers
2181 while (num--) { in pwm_add_table()
2182 list_add_tail(&table->list, &pwm_lookup_list); in pwm_add_table()
2188 * pwm_remove_table() - unregister PWM device consumers
2196 while (num--) { in pwm_remove_table()
2197 list_del(&table->list); in pwm_remove_table()
2206 for (i = 0; i < chip->npwm; i++) { in pwm_dbg_show()
2207 struct pwm_device *pwm = &chip->pwms[i]; in pwm_dbg_show() local
2210 pwm_get_state(pwm, &state); in pwm_dbg_show()
2212 seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label); in pwm_dbg_show()
2214 if (test_bit(PWMF_REQUESTED, &pwm->flags)) in pwm_dbg_show()
2220 seq_printf(s, " period: %llu ns", state.period); in pwm_dbg_show()
2238 s->private = ""; in pwm_seq_start()
2250 s->private = "\n"; in pwm_seq_next()
2266 seq_printf(s, "%s%d: %s/%s, %d PWM device%s\n", in pwm_seq_show()
2267 (char *)s->private, chip->id, in pwm_seq_show()
2268 pwmchip_parent(chip)->bus ? pwmchip_parent(chip)->bus->name : "no-bus", in pwm_seq_show()
2269 dev_name(pwmchip_parent(chip)), chip->npwm, in pwm_seq_show()
2270 (chip->npwm != 1) ? "s" : ""); in pwm_seq_show()
2292 pr_err("Failed to initialize PWM class (%pe)\n", ERR_PTR(ret)); in pwm_init()
2297 debugfs_create_file("pwm", 0444, NULL, NULL, &pwm_debugfs_fops); in pwm_init()