1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Generic pwmlib implementation
5 * Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de>
6 * Copyright (C) 2011-2012 Avionic Design GmbH
9 #define DEFAULT_SYMBOL_NAMESPACE PWM
11 #include <linux/acpi.h>
12 #include <linux/module.h>
13 #include <linux/idr.h>
15 #include <linux/pwm.h>
16 #include <linux/list.h>
17 #include <linux/mutex.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
20 #include <linux/device.h>
21 #include <linux/debugfs.h>
22 #include <linux/seq_file.h>
24 #include <dt-bindings/pwm/pwm.h>
26 #define CREATE_TRACE_POINTS
27 #include <trace/events/pwm.h>
29 /* protects access to pwm_chips */
30 static DEFINE_MUTEX(pwm_lock
);
32 static DEFINE_IDR(pwm_chips
);
34 static void pwmchip_lock(struct pwm_chip
*chip
)
37 spin_lock(&chip
->atomic_lock
);
39 mutex_lock(&chip
->nonatomic_lock
);
42 static void pwmchip_unlock(struct pwm_chip
*chip
)
45 spin_unlock(&chip
->atomic_lock
);
47 mutex_unlock(&chip
->nonatomic_lock
);
50 DEFINE_GUARD(pwmchip
, struct pwm_chip
*, pwmchip_lock(_T
), pwmchip_unlock(_T
))
52 static bool pwm_wf_valid(const struct pwm_waveform
*wf
)
55 * For now restrict waveforms to period_length_ns <= S64_MAX to provide
56 * some space for future extensions. One possibility is to simplify
57 * representing waveforms with inverted polarity using negative values
60 if (wf
->period_length_ns
> S64_MAX
)
63 if (wf
->duty_length_ns
> wf
->period_length_ns
)
67 * .duty_offset_ns is supposed to be smaller than .period_length_ns, apart
68 * from the corner case .duty_offset_ns == 0 && .period_length_ns == 0.
70 if (wf
->duty_offset_ns
&& wf
->duty_offset_ns
>= wf
->period_length_ns
)
76 static void pwm_wf2state(const struct pwm_waveform
*wf
, struct pwm_state
*state
)
78 if (wf
->period_length_ns
) {
79 if (wf
->duty_length_ns
+ wf
->duty_offset_ns
< wf
->period_length_ns
)
80 *state
= (struct pwm_state
){
82 .polarity
= PWM_POLARITY_NORMAL
,
83 .period
= wf
->period_length_ns
,
84 .duty_cycle
= wf
->duty_length_ns
,
87 *state
= (struct pwm_state
){
89 .polarity
= PWM_POLARITY_INVERSED
,
90 .period
= wf
->period_length_ns
,
91 .duty_cycle
= wf
->period_length_ns
- wf
->duty_length_ns
,
94 *state
= (struct pwm_state
){
100 static void pwm_state2wf(const struct pwm_state
*state
, struct pwm_waveform
*wf
)
102 if (state
->enabled
) {
103 if (state
->polarity
== PWM_POLARITY_NORMAL
)
104 *wf
= (struct pwm_waveform
){
105 .period_length_ns
= state
->period
,
106 .duty_length_ns
= state
->duty_cycle
,
110 *wf
= (struct pwm_waveform
){
111 .period_length_ns
= state
->period
,
112 .duty_length_ns
= state
->period
- state
->duty_cycle
,
113 .duty_offset_ns
= state
->duty_cycle
,
116 *wf
= (struct pwm_waveform
){
117 .period_length_ns
= 0,
122 static int pwmwfcmp(const struct pwm_waveform
*a
, const struct pwm_waveform
*b
)
124 if (a
->period_length_ns
> b
->period_length_ns
)
127 if (a
->period_length_ns
< b
->period_length_ns
)
130 if (a
->duty_length_ns
> b
->duty_length_ns
)
133 if (a
->duty_length_ns
< b
->duty_length_ns
)
136 if (a
->duty_offset_ns
> b
->duty_offset_ns
)
139 if (a
->duty_offset_ns
< b
->duty_offset_ns
)
145 static bool pwm_check_rounding(const struct pwm_waveform
*wf
,
146 const struct pwm_waveform
*wf_rounded
)
148 if (!wf
->period_length_ns
)
151 if (wf
->period_length_ns
< wf_rounded
->period_length_ns
)
154 if (wf
->duty_length_ns
< wf_rounded
->duty_length_ns
)
157 if (wf
->duty_offset_ns
< wf_rounded
->duty_offset_ns
)
163 static int __pwm_round_waveform_tohw(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
164 const struct pwm_waveform
*wf
, void *wfhw
)
166 const struct pwm_ops
*ops
= chip
->ops
;
169 ret
= ops
->round_waveform_tohw(chip
, pwm
, wf
, wfhw
);
170 trace_pwm_round_waveform_tohw(pwm
, wf
, wfhw
, ret
);
175 static int __pwm_round_waveform_fromhw(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
176 const void *wfhw
, struct pwm_waveform
*wf
)
178 const struct pwm_ops
*ops
= chip
->ops
;
181 ret
= ops
->round_waveform_fromhw(chip
, pwm
, wfhw
, wf
);
182 trace_pwm_round_waveform_fromhw(pwm
, wfhw
, wf
, ret
);
187 static int __pwm_read_waveform(struct pwm_chip
*chip
, struct pwm_device
*pwm
, void *wfhw
)
189 const struct pwm_ops
*ops
= chip
->ops
;
192 ret
= ops
->read_waveform(chip
, pwm
, wfhw
);
193 trace_pwm_read_waveform(pwm
, wfhw
, ret
);
198 static int __pwm_write_waveform(struct pwm_chip
*chip
, struct pwm_device
*pwm
, const void *wfhw
)
200 const struct pwm_ops
*ops
= chip
->ops
;
203 ret
= ops
->write_waveform(chip
, pwm
, wfhw
);
204 trace_pwm_write_waveform(pwm
, wfhw
, ret
);
212 * pwm_round_waveform_might_sleep - Query hardware capabilities
213 * Cannot be used in atomic context.
215 * @wf: waveform to round and output parameter
217 * Typically a given waveform cannot be implemented exactly by hardware, e.g.
218 * because hardware only supports coarse period resolution or no duty_offset.
219 * This function returns the actually implemented waveform if you pass wf to
220 * pwm_set_waveform_might_sleep now.
222 * Note however that the world doesn't stop turning when you call it, so when
225 * pwm_round_waveform_might_sleep(mypwm, &wf);
226 * pwm_set_waveform_might_sleep(mypwm, &wf, true);
228 * the latter might fail, e.g. because an input clock changed its rate between
229 * these two calls and the waveform determined by
230 * pwm_round_waveform_might_sleep() cannot be implemented any more.
232 * Returns 0 on success, 1 if there is no valid hardware configuration matching
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
)
237 struct pwm_chip
*chip
= pwm
->chip
;
238 const struct pwm_ops
*ops
= chip
->ops
;
239 struct pwm_waveform wf_req
= *wf
;
241 int ret_tohw
, ret_fromhw
;
243 BUG_ON(WFHWSIZE
< ops
->sizeof_wfhw
);
245 if (!pwm_wf_valid(wf
))
248 guard(pwmchip
)(chip
);
250 if (!chip
->operational
)
253 ret_tohw
= __pwm_round_waveform_tohw(chip
, pwm
, wf
, wfhw
);
257 if (IS_ENABLED(CONFIG_PWM_DEBUG
) && ret_tohw
> 1)
258 dev_err(&chip
->dev
, "Unexpected return value from __pwm_round_waveform_tohw: requested %llu/%llu [+%llu], return value %d\n",
259 wf_req
.duty_length_ns
, wf_req
.period_length_ns
, wf_req
.duty_offset_ns
, ret_tohw
);
261 ret_fromhw
= __pwm_round_waveform_fromhw(chip
, pwm
, wfhw
, wf
);
265 if (IS_ENABLED(CONFIG_PWM_DEBUG
) && ret_fromhw
> 0)
266 dev_err(&chip
->dev
, "Unexpected return value from __pwm_round_waveform_fromhw: requested %llu/%llu [+%llu], return value %d\n",
267 wf_req
.duty_length_ns
, wf_req
.period_length_ns
, wf_req
.duty_offset_ns
, ret_tohw
);
269 if (IS_ENABLED(CONFIG_PWM_DEBUG
) &&
270 ret_tohw
== 0 && !pwm_check_rounding(&wf_req
, wf
))
271 dev_err(&chip
->dev
, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n",
272 wf_req
.duty_length_ns
, wf_req
.period_length_ns
, wf_req
.duty_offset_ns
,
273 wf
->duty_length_ns
, wf
->period_length_ns
, wf
->duty_offset_ns
);
277 EXPORT_SYMBOL_GPL(pwm_round_waveform_might_sleep
);
280 * pwm_get_waveform_might_sleep - Query hardware about current configuration
281 * Cannot be used in atomic context.
283 * @wf: output parameter
285 * Stores the current configuration of the PWM in @wf. Note this is the
286 * equivalent of pwm_get_state_hw() (and not pwm_get_state()) for pwm_waveform.
288 int pwm_get_waveform_might_sleep(struct pwm_device
*pwm
, struct pwm_waveform
*wf
)
290 struct pwm_chip
*chip
= pwm
->chip
;
291 const struct pwm_ops
*ops
= chip
->ops
;
295 BUG_ON(WFHWSIZE
< ops
->sizeof_wfhw
);
297 guard(pwmchip
)(chip
);
299 if (!chip
->operational
)
302 err
= __pwm_read_waveform(chip
, pwm
, &wfhw
);
306 return __pwm_round_waveform_fromhw(chip
, pwm
, &wfhw
, wf
);
308 EXPORT_SYMBOL_GPL(pwm_get_waveform_might_sleep
);
310 /* Called with the pwmchip lock held */
311 static int __pwm_set_waveform(struct pwm_device
*pwm
,
312 const struct pwm_waveform
*wf
,
315 struct pwm_chip
*chip
= pwm
->chip
;
316 const struct pwm_ops
*ops
= chip
->ops
;
318 struct pwm_waveform wf_rounded
;
321 BUG_ON(WFHWSIZE
< ops
->sizeof_wfhw
);
323 if (!pwm_wf_valid(wf
))
326 err
= __pwm_round_waveform_tohw(chip
, pwm
, wf
, &wfhw
);
330 if ((IS_ENABLED(CONFIG_PWM_DEBUG
) || exact
) && wf
->period_length_ns
) {
331 err
= __pwm_round_waveform_fromhw(chip
, pwm
, &wfhw
, &wf_rounded
);
335 if (IS_ENABLED(CONFIG_PWM_DEBUG
) && !pwm_check_rounding(wf
, &wf_rounded
))
336 dev_err(&chip
->dev
, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n",
337 wf
->duty_length_ns
, wf
->period_length_ns
, wf
->duty_offset_ns
,
338 wf_rounded
.duty_length_ns
, wf_rounded
.period_length_ns
, wf_rounded
.duty_offset_ns
);
340 if (exact
&& pwmwfcmp(wf
, &wf_rounded
)) {
341 dev_dbg(&chip
->dev
, "Requested no rounding, but %llu/%llu [+%llu] -> %llu/%llu [+%llu]\n",
342 wf
->duty_length_ns
, wf
->period_length_ns
, wf
->duty_offset_ns
,
343 wf_rounded
.duty_length_ns
, wf_rounded
.period_length_ns
, wf_rounded
.duty_offset_ns
);
349 err
= __pwm_write_waveform(chip
, pwm
, &wfhw
);
354 pwm_wf2state(wf
, &pwm
->state
);
356 if (IS_ENABLED(CONFIG_PWM_DEBUG
) && ops
->read_waveform
&& wf
->period_length_ns
) {
357 struct pwm_waveform wf_set
;
359 err
= __pwm_read_waveform(chip
, pwm
, &wfhw
);
364 err
= __pwm_round_waveform_fromhw(chip
, pwm
, &wfhw
, &wf_set
);
369 if (pwmwfcmp(&wf_set
, &wf_rounded
) != 0)
371 "Unexpected setting: requested %llu/%llu [+%llu], expected %llu/%llu [+%llu], set %llu/%llu [+%llu]\n",
372 wf
->duty_length_ns
, wf
->period_length_ns
, wf
->duty_offset_ns
,
373 wf_rounded
.duty_length_ns
, wf_rounded
.period_length_ns
, wf_rounded
.duty_offset_ns
,
374 wf_set
.duty_length_ns
, wf_set
.period_length_ns
, wf_set
.duty_offset_ns
);
380 * pwm_set_waveform_might_sleep - Apply a new waveform
381 * Cannot be used in atomic context.
383 * @wf: The waveform to apply
384 * @exact: If true no rounding is allowed
386 * Typically a requested waveform cannot be implemented exactly, e.g. because
387 * you requested .period_length_ns = 100 ns, but the hardware can only set
388 * periods that are a multiple of 8.5 ns. With that hardware passing exact =
389 * true results in pwm_set_waveform_might_sleep() failing and returning 1. If
390 * exact = false you get a period of 93.5 ns (i.e. the biggest period not bigger
391 * than the requested value).
392 * Note that even with exact = true, some rounding by less than 1 is
393 * possible/needed. In the above example requesting .period_length_ns = 94 and
394 * exact = true, you get the hardware configured with period = 93.5 ns.
396 int pwm_set_waveform_might_sleep(struct pwm_device
*pwm
,
397 const struct pwm_waveform
*wf
, bool exact
)
399 struct pwm_chip
*chip
= pwm
->chip
;
404 guard(pwmchip
)(chip
);
406 if (!chip
->operational
)
409 if (IS_ENABLED(CONFIG_PWM_DEBUG
) && chip
->atomic
) {
411 * Catch any drivers that have been marked as atomic but
412 * that will sleep anyway.
415 err
= __pwm_set_waveform(pwm
, wf
, exact
);
418 err
= __pwm_set_waveform(pwm
, wf
, exact
);
423 EXPORT_SYMBOL_GPL(pwm_set_waveform_might_sleep
);
425 static void pwm_apply_debug(struct pwm_device
*pwm
,
426 const struct pwm_state
*state
)
428 struct pwm_state
*last
= &pwm
->last
;
429 struct pwm_chip
*chip
= pwm
->chip
;
430 struct pwm_state s1
= { 0 }, s2
= { 0 };
433 if (!IS_ENABLED(CONFIG_PWM_DEBUG
))
436 /* No reasonable diagnosis possible without .get_state() */
437 if (!chip
->ops
->get_state
)
441 * *state was just applied. Read out the hardware state and do some
445 err
= chip
->ops
->get_state(chip
, pwm
, &s1
);
446 trace_pwm_get(pwm
, &s1
, err
);
448 /* If that failed there isn't much to debug */
452 * The lowlevel driver either ignored .polarity (which is a bug) or as
453 * best effort inverted .polarity and fixed .duty_cycle respectively.
454 * Undo this inversion and fixup for further tests.
456 if (s1
.enabled
&& s1
.polarity
!= state
->polarity
) {
457 s2
.polarity
= state
->polarity
;
458 s2
.duty_cycle
= s1
.period
- s1
.duty_cycle
;
459 s2
.period
= s1
.period
;
460 s2
.enabled
= s1
.enabled
;
465 if (s2
.polarity
!= state
->polarity
&&
466 state
->duty_cycle
< state
->period
)
467 dev_warn(pwmchip_parent(chip
), ".apply ignored .polarity\n");
469 if (state
->enabled
&& s2
.enabled
&&
470 last
->polarity
== state
->polarity
&&
471 last
->period
> s2
.period
&&
472 last
->period
<= state
->period
)
473 dev_warn(pwmchip_parent(chip
),
474 ".apply didn't pick the best available period (requested: %llu, applied: %llu, possible: %llu)\n",
475 state
->period
, s2
.period
, last
->period
);
478 * Rounding period up is fine only if duty_cycle is 0 then, because a
479 * flat line doesn't have a characteristic period.
481 if (state
->enabled
&& s2
.enabled
&& state
->period
< s2
.period
&& s2
.duty_cycle
)
482 dev_warn(pwmchip_parent(chip
),
483 ".apply is supposed to round down period (requested: %llu, applied: %llu)\n",
484 state
->period
, s2
.period
);
486 if (state
->enabled
&&
487 last
->polarity
== state
->polarity
&&
488 last
->period
== s2
.period
&&
489 last
->duty_cycle
> s2
.duty_cycle
&&
490 last
->duty_cycle
<= state
->duty_cycle
)
491 dev_warn(pwmchip_parent(chip
),
492 ".apply didn't pick the best available duty cycle (requested: %llu/%llu, applied: %llu/%llu, possible: %llu/%llu)\n",
493 state
->duty_cycle
, state
->period
,
494 s2
.duty_cycle
, s2
.period
,
495 last
->duty_cycle
, last
->period
);
497 if (state
->enabled
&& s2
.enabled
&& state
->duty_cycle
< s2
.duty_cycle
)
498 dev_warn(pwmchip_parent(chip
),
499 ".apply is supposed to round down duty_cycle (requested: %llu/%llu, applied: %llu/%llu)\n",
500 state
->duty_cycle
, state
->period
,
501 s2
.duty_cycle
, s2
.period
);
503 if (!state
->enabled
&& s2
.enabled
&& s2
.duty_cycle
> 0)
504 dev_warn(pwmchip_parent(chip
),
505 "requested disabled, but yielded enabled with duty > 0\n");
507 /* reapply the state that the driver reported being configured. */
508 err
= chip
->ops
->apply(chip
, pwm
, &s1
);
509 trace_pwm_apply(pwm
, &s1
, err
);
512 dev_err(pwmchip_parent(chip
), "failed to reapply current setting\n");
516 *last
= (struct pwm_state
){ 0 };
517 err
= chip
->ops
->get_state(chip
, pwm
, last
);
518 trace_pwm_get(pwm
, last
, err
);
522 /* reapplication of the current state should give an exact match */
523 if (s1
.enabled
!= last
->enabled
||
524 s1
.polarity
!= last
->polarity
||
525 (s1
.enabled
&& s1
.period
!= last
->period
) ||
526 (s1
.enabled
&& s1
.duty_cycle
!= last
->duty_cycle
)) {
527 dev_err(pwmchip_parent(chip
),
528 ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n",
529 s1
.enabled
, s1
.polarity
, s1
.duty_cycle
, s1
.period
,
530 last
->enabled
, last
->polarity
, last
->duty_cycle
,
535 static bool pwm_state_valid(const struct pwm_state
*state
)
538 * For a disabled state all other state description is irrelevant and
539 * and supposed to be ignored. So also ignore any strange values and
540 * consider the state ok.
548 if (state
->duty_cycle
> state
->period
)
555 * __pwm_apply() - atomically apply a new state to a PWM device
557 * @state: new state to apply
559 static int __pwm_apply(struct pwm_device
*pwm
, const struct pwm_state
*state
)
561 struct pwm_chip
*chip
;
562 const struct pwm_ops
*ops
;
568 if (!pwm_state_valid(state
)) {
570 * Allow to transition from one invalid state to another.
571 * This ensures that you can e.g. change the polarity while
572 * the period is zero. (This happens on stm32 when the hardware
573 * is in its poweron default state.) This greatly simplifies
574 * working with the sysfs API where you can only change one
575 * parameter at a time.
577 if (!pwm_state_valid(&pwm
->state
)) {
588 if (state
->period
== pwm
->state
.period
&&
589 state
->duty_cycle
== pwm
->state
.duty_cycle
&&
590 state
->polarity
== pwm
->state
.polarity
&&
591 state
->enabled
== pwm
->state
.enabled
&&
592 state
->usage_power
== pwm
->state
.usage_power
)
595 if (ops
->write_waveform
) {
596 struct pwm_waveform wf
;
599 BUG_ON(WFHWSIZE
< ops
->sizeof_wfhw
);
601 pwm_state2wf(state
, &wf
);
604 * The rounding is wrong here for states with inverted polarity.
605 * While .apply() rounds down duty_cycle (which represents the
606 * time from the start of the period to the inner edge),
607 * .round_waveform_tohw() rounds down the time the PWM is high.
608 * Can be fixed if the need arises, until reported otherwise
609 * let's assume that consumers don't care.
612 err
= __pwm_round_waveform_tohw(chip
, pwm
, &wf
, &wfhw
);
616 * This signals an invalid request, typically
617 * the requested period (or duty_offset) is
618 * smaller than possible with the hardware.
625 if (IS_ENABLED(CONFIG_PWM_DEBUG
)) {
626 struct pwm_waveform wf_rounded
;
628 err
= __pwm_round_waveform_fromhw(chip
, pwm
, &wfhw
, &wf_rounded
);
632 if (!pwm_check_rounding(&wf
, &wf_rounded
))
633 dev_err(&chip
->dev
, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n",
634 wf
.duty_length_ns
, wf
.period_length_ns
, wf
.duty_offset_ns
,
635 wf_rounded
.duty_length_ns
, wf_rounded
.period_length_ns
, wf_rounded
.duty_offset_ns
);
638 err
= __pwm_write_waveform(chip
, pwm
, &wfhw
);
645 err
= ops
->apply(chip
, pwm
, state
);
646 trace_pwm_apply(pwm
, state
, err
);
653 * only do this after pwm->state was applied as some
654 * implementations of .get_state() depend on this
656 pwm_apply_debug(pwm
, state
);
663 * pwm_apply_might_sleep() - atomically apply a new state to a PWM device
664 * Cannot be used in atomic context.
666 * @state: new state to apply
668 int pwm_apply_might_sleep(struct pwm_device
*pwm
, const struct pwm_state
*state
)
671 struct pwm_chip
*chip
= pwm
->chip
;
674 * Some lowlevel driver's implementations of .apply() make use of
675 * mutexes, also with some drivers only returning when the new
676 * configuration is active calling pwm_apply_might_sleep() from atomic context
677 * is a bad idea. So make it explicit that calling this function might
682 guard(pwmchip
)(chip
);
684 if (!chip
->operational
)
687 if (IS_ENABLED(CONFIG_PWM_DEBUG
) && chip
->atomic
) {
689 * Catch any drivers that have been marked as atomic but
690 * that will sleep anyway.
693 err
= __pwm_apply(pwm
, state
);
696 err
= __pwm_apply(pwm
, state
);
701 EXPORT_SYMBOL_GPL(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().
707 * @state: new state to apply
709 int pwm_apply_atomic(struct pwm_device
*pwm
, const struct pwm_state
*state
)
711 struct pwm_chip
*chip
= pwm
->chip
;
713 WARN_ONCE(!chip
->atomic
,
714 "sleeping PWM driver used in atomic context\n");
716 guard(pwmchip
)(chip
);
718 if (!chip
->operational
)
721 return __pwm_apply(pwm
, state
);
723 EXPORT_SYMBOL_GPL(pwm_apply_atomic
);
726 * pwm_get_state_hw() - get the current PWM state from hardware
728 * @state: state to fill with the current PWM state
730 * Similar to pwm_get_state() but reads the current PWM state from hardware
731 * instead of the requested state.
733 * Returns: 0 on success or a negative error code on failure.
734 * Context: May sleep.
736 int pwm_get_state_hw(struct pwm_device
*pwm
, struct pwm_state
*state
)
738 struct pwm_chip
*chip
= pwm
->chip
;
739 const struct pwm_ops
*ops
= chip
->ops
;
740 int ret
= -EOPNOTSUPP
;
744 guard(pwmchip
)(chip
);
746 if (!chip
->operational
)
749 if (ops
->read_waveform
) {
751 struct pwm_waveform wf
;
753 BUG_ON(WFHWSIZE
< ops
->sizeof_wfhw
);
755 ret
= __pwm_read_waveform(chip
, pwm
, &wfhw
);
759 ret
= __pwm_round_waveform_fromhw(chip
, pwm
, &wfhw
, &wf
);
763 pwm_wf2state(&wf
, state
);
765 } else if (ops
->get_state
) {
766 ret
= ops
->get_state(chip
, pwm
, state
);
767 trace_pwm_get(pwm
, state
, ret
);
772 EXPORT_SYMBOL_GPL(pwm_get_state_hw
);
775 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
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
780 * the bootloader config to the Linux one.
782 int pwm_adjust_config(struct pwm_device
*pwm
)
784 struct pwm_state state
;
785 struct pwm_args pargs
;
787 pwm_get_args(pwm
, &pargs
);
788 pwm_get_state(pwm
, &state
);
791 * If the current period is zero it means that either the PWM driver
792 * does not support initial state retrieval or the PWM has not yet
795 * In either case, we setup the new period and polarity, and assign a
799 state
.duty_cycle
= 0;
800 state
.period
= pargs
.period
;
801 state
.polarity
= pargs
.polarity
;
803 return pwm_apply_might_sleep(pwm
, &state
);
807 * Adjust the PWM duty cycle/period based on the period value provided
810 if (pargs
.period
!= state
.period
) {
811 u64 dutycycle
= (u64
)state
.duty_cycle
* pargs
.period
;
813 do_div(dutycycle
, state
.period
);
814 state
.duty_cycle
= dutycycle
;
815 state
.period
= pargs
.period
;
819 * If the polarity changed, we should also change the duty cycle.
821 if (pargs
.polarity
!= state
.polarity
) {
822 state
.polarity
= pargs
.polarity
;
823 state
.duty_cycle
= state
.period
- state
.duty_cycle
;
826 return pwm_apply_might_sleep(pwm
, &state
);
828 EXPORT_SYMBOL_GPL(pwm_adjust_config
);
831 * pwm_capture() - capture and report a PWM signal
833 * @result: structure to fill with capture result
834 * @timeout: time to wait, in milliseconds, before giving up on capture
836 * Returns: 0 on success or a negative error code on failure.
838 static int pwm_capture(struct pwm_device
*pwm
, struct pwm_capture
*result
,
839 unsigned long timeout
)
841 struct pwm_chip
*chip
= pwm
->chip
;
842 const struct pwm_ops
*ops
= chip
->ops
;
848 * Holding the pwm_lock is probably not needed. If you use pwm_capture()
849 * and you're interested to speed it up, please convince yourself it's
850 * really not needed, test and then suggest a patch on the mailing list.
852 guard(mutex
)(&pwm_lock
);
854 guard(pwmchip
)(chip
);
856 if (!chip
->operational
)
859 return ops
->capture(chip
, pwm
, result
, timeout
);
862 static struct pwm_chip
*pwmchip_find_by_name(const char *name
)
864 struct pwm_chip
*chip
;
865 unsigned long id
, tmp
;
870 guard(mutex
)(&pwm_lock
);
872 idr_for_each_entry_ul(&pwm_chips
, chip
, tmp
, id
) {
873 if (device_match_name(pwmchip_parent(chip
), name
))
880 static int pwm_device_request(struct pwm_device
*pwm
, const char *label
)
883 struct pwm_chip
*chip
= pwm
->chip
;
884 const struct pwm_ops
*ops
= chip
->ops
;
886 if (test_bit(PWMF_REQUESTED
, &pwm
->flags
))
890 * This function is called while holding pwm_lock. As .operational only
891 * changes while holding this lock, checking it here without holding the
894 if (!chip
->operational
)
897 if (!try_module_get(chip
->owner
))
900 if (!get_device(&chip
->dev
)) {
906 err
= ops
->request(chip
, pwm
);
908 put_device(&chip
->dev
);
910 module_put(chip
->owner
);
915 if (ops
->read_waveform
|| ops
->get_state
) {
917 * Zero-initialize state because most drivers are unaware of
918 * .usage_power. The other members of state are supposed to be
919 * set by lowlevel drivers. We still initialize the whole
920 * structure for simplicity even though this might paper over
921 * faulty implementations of .get_state().
923 struct pwm_state state
= { 0, };
925 err
= pwm_get_state_hw(pwm
, &state
);
929 if (IS_ENABLED(CONFIG_PWM_DEBUG
))
930 pwm
->last
= pwm
->state
;
933 set_bit(PWMF_REQUESTED
, &pwm
->flags
);
940 * pwm_request_from_chip() - request a PWM device relative to a 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
946 * chip. A negative error code is returned if the index is not valid for the
947 * specified PWM chip or if the PWM device cannot be requested.
949 static struct pwm_device
*pwm_request_from_chip(struct pwm_chip
*chip
,
953 struct pwm_device
*pwm
;
956 if (!chip
|| index
>= chip
->npwm
)
957 return ERR_PTR(-EINVAL
);
959 guard(mutex
)(&pwm_lock
);
961 pwm
= &chip
->pwms
[index
];
963 err
= pwm_device_request(pwm
, label
);
971 of_pwm_xlate_with_flags(struct pwm_chip
*chip
, const struct of_phandle_args
*args
)
973 struct pwm_device
*pwm
;
975 /* period in the second cell and flags in the third cell are optional */
976 if (args
->args_count
< 1)
977 return ERR_PTR(-EINVAL
);
979 pwm
= pwm_request_from_chip(chip
, args
->args
[0], NULL
);
983 if (args
->args_count
> 1)
984 pwm
->args
.period
= args
->args
[1];
986 pwm
->args
.polarity
= PWM_POLARITY_NORMAL
;
987 if (args
->args_count
> 2 && args
->args
[2] & PWM_POLARITY_INVERTED
)
988 pwm
->args
.polarity
= PWM_POLARITY_INVERSED
;
992 EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags
);
995 of_pwm_single_xlate(struct pwm_chip
*chip
, const struct of_phandle_args
*args
)
997 struct pwm_device
*pwm
;
999 pwm
= pwm_request_from_chip(chip
, 0, NULL
);
1003 if (args
->args_count
> 0)
1004 pwm
->args
.period
= args
->args
[0];
1006 pwm
->args
.polarity
= PWM_POLARITY_NORMAL
;
1007 if (args
->args_count
> 1 && args
->args
[1] & PWM_POLARITY_INVERTED
)
1008 pwm
->args
.polarity
= PWM_POLARITY_INVERSED
;
1012 EXPORT_SYMBOL_GPL(of_pwm_single_xlate
);
1015 struct device pwm_dev
;
1016 struct pwm_device
*pwm
;
1018 struct pwm_state suspend
;
1021 static inline struct pwm_chip
*pwmchip_from_dev(struct device
*pwmchip_dev
)
1023 return container_of(pwmchip_dev
, struct pwm_chip
, dev
);
1026 static inline struct pwm_export
*pwmexport_from_dev(struct device
*pwm_dev
)
1028 return container_of(pwm_dev
, struct pwm_export
, pwm_dev
);
1031 static inline struct pwm_device
*pwm_from_dev(struct device
*pwm_dev
)
1033 struct pwm_export
*export
= pwmexport_from_dev(pwm_dev
);
1038 static ssize_t
period_show(struct device
*pwm_dev
,
1039 struct device_attribute
*attr
,
1042 const struct pwm_device
*pwm
= pwm_from_dev(pwm_dev
);
1043 struct pwm_state state
;
1045 pwm_get_state(pwm
, &state
);
1047 return sysfs_emit(buf
, "%llu\n", state
.period
);
1050 static ssize_t
period_store(struct device
*pwm_dev
,
1051 struct device_attribute
*attr
,
1052 const char *buf
, size_t size
)
1054 struct pwm_export
*export
= pwmexport_from_dev(pwm_dev
);
1055 struct pwm_device
*pwm
= export
->pwm
;
1056 struct pwm_state state
;
1060 ret
= kstrtou64(buf
, 0, &val
);
1064 guard(mutex
)(&export
->lock
);
1066 pwm_get_state(pwm
, &state
);
1068 ret
= pwm_apply_might_sleep(pwm
, &state
);
1070 return ret
? : size
;
1073 static ssize_t
duty_cycle_show(struct device
*pwm_dev
,
1074 struct device_attribute
*attr
,
1077 const struct pwm_device
*pwm
= pwm_from_dev(pwm_dev
);
1078 struct pwm_state state
;
1080 pwm_get_state(pwm
, &state
);
1082 return sysfs_emit(buf
, "%llu\n", state
.duty_cycle
);
1085 static ssize_t
duty_cycle_store(struct device
*pwm_dev
,
1086 struct device_attribute
*attr
,
1087 const char *buf
, size_t size
)
1089 struct pwm_export
*export
= pwmexport_from_dev(pwm_dev
);
1090 struct pwm_device
*pwm
= export
->pwm
;
1091 struct pwm_state state
;
1095 ret
= kstrtou64(buf
, 0, &val
);
1099 guard(mutex
)(&export
->lock
);
1101 pwm_get_state(pwm
, &state
);
1102 state
.duty_cycle
= val
;
1103 ret
= pwm_apply_might_sleep(pwm
, &state
);
1105 return ret
? : size
;
1108 static ssize_t
enable_show(struct device
*pwm_dev
,
1109 struct device_attribute
*attr
,
1112 const struct pwm_device
*pwm
= pwm_from_dev(pwm_dev
);
1113 struct pwm_state state
;
1115 pwm_get_state(pwm
, &state
);
1117 return sysfs_emit(buf
, "%d\n", state
.enabled
);
1120 static ssize_t
enable_store(struct device
*pwm_dev
,
1121 struct device_attribute
*attr
,
1122 const char *buf
, size_t size
)
1124 struct pwm_export
*export
= pwmexport_from_dev(pwm_dev
);
1125 struct pwm_device
*pwm
= export
->pwm
;
1126 struct pwm_state state
;
1129 ret
= kstrtoint(buf
, 0, &val
);
1133 guard(mutex
)(&export
->lock
);
1135 pwm_get_state(pwm
, &state
);
1139 state
.enabled
= false;
1142 state
.enabled
= true;
1148 ret
= pwm_apply_might_sleep(pwm
, &state
);
1150 return ret
? : size
;
1153 static ssize_t
polarity_show(struct device
*pwm_dev
,
1154 struct device_attribute
*attr
,
1157 const struct pwm_device
*pwm
= pwm_from_dev(pwm_dev
);
1158 const char *polarity
= "unknown";
1159 struct pwm_state state
;
1161 pwm_get_state(pwm
, &state
);
1163 switch (state
.polarity
) {
1164 case PWM_POLARITY_NORMAL
:
1165 polarity
= "normal";
1168 case PWM_POLARITY_INVERSED
:
1169 polarity
= "inversed";
1173 return sysfs_emit(buf
, "%s\n", polarity
);
1176 static ssize_t
polarity_store(struct device
*pwm_dev
,
1177 struct device_attribute
*attr
,
1178 const char *buf
, size_t size
)
1180 struct pwm_export
*export
= pwmexport_from_dev(pwm_dev
);
1181 struct pwm_device
*pwm
= export
->pwm
;
1182 enum pwm_polarity polarity
;
1183 struct pwm_state state
;
1186 if (sysfs_streq(buf
, "normal"))
1187 polarity
= PWM_POLARITY_NORMAL
;
1188 else if (sysfs_streq(buf
, "inversed"))
1189 polarity
= PWM_POLARITY_INVERSED
;
1193 guard(mutex
)(&export
->lock
);
1195 pwm_get_state(pwm
, &state
);
1196 state
.polarity
= polarity
;
1197 ret
= pwm_apply_might_sleep(pwm
, &state
);
1199 return ret
? : size
;
1202 static ssize_t
capture_show(struct device
*pwm_dev
,
1203 struct device_attribute
*attr
,
1206 struct pwm_device
*pwm
= pwm_from_dev(pwm_dev
);
1207 struct pwm_capture result
;
1210 ret
= pwm_capture(pwm
, &result
, jiffies_to_msecs(HZ
));
1214 return sysfs_emit(buf
, "%u %u\n", result
.period
, result
.duty_cycle
);
1217 static DEVICE_ATTR_RW(period
);
1218 static DEVICE_ATTR_RW(duty_cycle
);
1219 static DEVICE_ATTR_RW(enable
);
1220 static DEVICE_ATTR_RW(polarity
);
1221 static DEVICE_ATTR_RO(capture
);
1223 static struct attribute
*pwm_attrs
[] = {
1224 &dev_attr_period
.attr
,
1225 &dev_attr_duty_cycle
.attr
,
1226 &dev_attr_enable
.attr
,
1227 &dev_attr_polarity
.attr
,
1228 &dev_attr_capture
.attr
,
1231 ATTRIBUTE_GROUPS(pwm
);
1233 static void pwm_export_release(struct device
*pwm_dev
)
1235 struct pwm_export
*export
= pwmexport_from_dev(pwm_dev
);
1240 static int pwm_export_child(struct device
*pwmchip_dev
, struct pwm_device
*pwm
)
1242 struct pwm_export
*export
;
1246 if (test_and_set_bit(PWMF_EXPORTED
, &pwm
->flags
))
1249 export
= kzalloc(sizeof(*export
), GFP_KERNEL
);
1251 clear_bit(PWMF_EXPORTED
, &pwm
->flags
);
1256 mutex_init(&export
->lock
);
1258 export
->pwm_dev
.release
= pwm_export_release
;
1259 export
->pwm_dev
.parent
= pwmchip_dev
;
1260 export
->pwm_dev
.devt
= MKDEV(0, 0);
1261 export
->pwm_dev
.groups
= pwm_groups
;
1262 dev_set_name(&export
->pwm_dev
, "pwm%u", pwm
->hwpwm
);
1264 ret
= device_register(&export
->pwm_dev
);
1266 clear_bit(PWMF_EXPORTED
, &pwm
->flags
);
1267 put_device(&export
->pwm_dev
);
1271 pwm_prop
[0] = kasprintf(GFP_KERNEL
, "EXPORT=pwm%u", pwm
->hwpwm
);
1273 kobject_uevent_env(&pwmchip_dev
->kobj
, KOBJ_CHANGE
, pwm_prop
);
1279 static int pwm_unexport_match(struct device
*pwm_dev
, void *data
)
1281 return pwm_from_dev(pwm_dev
) == data
;
1284 static int pwm_unexport_child(struct device
*pwmchip_dev
, struct pwm_device
*pwm
)
1286 struct device
*pwm_dev
;
1289 if (!test_and_clear_bit(PWMF_EXPORTED
, &pwm
->flags
))
1292 pwm_dev
= device_find_child(pwmchip_dev
, pwm
, pwm_unexport_match
);
1296 pwm_prop
[0] = kasprintf(GFP_KERNEL
, "UNEXPORT=pwm%u", pwm
->hwpwm
);
1298 kobject_uevent_env(&pwmchip_dev
->kobj
, KOBJ_CHANGE
, pwm_prop
);
1301 /* for device_find_child() */
1302 put_device(pwm_dev
);
1303 device_unregister(pwm_dev
);
1309 static ssize_t
export_store(struct device
*pwmchip_dev
,
1310 struct device_attribute
*attr
,
1311 const char *buf
, size_t len
)
1313 struct pwm_chip
*chip
= pwmchip_from_dev(pwmchip_dev
);
1314 struct pwm_device
*pwm
;
1318 ret
= kstrtouint(buf
, 0, &hwpwm
);
1322 if (hwpwm
>= chip
->npwm
)
1325 pwm
= pwm_request_from_chip(chip
, hwpwm
, "sysfs");
1327 return PTR_ERR(pwm
);
1329 ret
= pwm_export_child(pwmchip_dev
, pwm
);
1335 static DEVICE_ATTR_WO(export
);
1337 static ssize_t
unexport_store(struct device
*pwmchip_dev
,
1338 struct device_attribute
*attr
,
1339 const char *buf
, size_t len
)
1341 struct pwm_chip
*chip
= pwmchip_from_dev(pwmchip_dev
);
1345 ret
= kstrtouint(buf
, 0, &hwpwm
);
1349 if (hwpwm
>= chip
->npwm
)
1352 ret
= pwm_unexport_child(pwmchip_dev
, &chip
->pwms
[hwpwm
]);
1356 static DEVICE_ATTR_WO(unexport
);
1358 static ssize_t
npwm_show(struct device
*pwmchip_dev
, struct device_attribute
*attr
,
1361 const struct pwm_chip
*chip
= pwmchip_from_dev(pwmchip_dev
);
1363 return sysfs_emit(buf
, "%u\n", chip
->npwm
);
1365 static DEVICE_ATTR_RO(npwm
);
1367 static struct attribute
*pwm_chip_attrs
[] = {
1368 &dev_attr_export
.attr
,
1369 &dev_attr_unexport
.attr
,
1370 &dev_attr_npwm
.attr
,
1373 ATTRIBUTE_GROUPS(pwm_chip
);
1375 /* takes export->lock on success */
1376 static struct pwm_export
*pwm_class_get_state(struct device
*pwmchip_dev
,
1377 struct pwm_device
*pwm
,
1378 struct pwm_state
*state
)
1380 struct device
*pwm_dev
;
1381 struct pwm_export
*export
;
1383 if (!test_bit(PWMF_EXPORTED
, &pwm
->flags
))
1386 pwm_dev
= device_find_child(pwmchip_dev
, pwm
, pwm_unexport_match
);
1390 export
= pwmexport_from_dev(pwm_dev
);
1391 put_device(pwm_dev
); /* for device_find_child() */
1393 mutex_lock(&export
->lock
);
1394 pwm_get_state(pwm
, state
);
1399 static int pwm_class_apply_state(struct pwm_export
*export
,
1400 struct pwm_device
*pwm
,
1401 struct pwm_state
*state
)
1403 int ret
= pwm_apply_might_sleep(pwm
, state
);
1405 /* release lock taken in pwm_class_get_state */
1406 mutex_unlock(&export
->lock
);
1411 static int pwm_class_resume_npwm(struct device
*pwmchip_dev
, unsigned int npwm
)
1413 struct pwm_chip
*chip
= pwmchip_from_dev(pwmchip_dev
);
1417 for (i
= 0; i
< npwm
; i
++) {
1418 struct pwm_device
*pwm
= &chip
->pwms
[i
];
1419 struct pwm_state state
;
1420 struct pwm_export
*export
;
1422 export
= pwm_class_get_state(pwmchip_dev
, pwm
, &state
);
1426 /* If pwmchip was not enabled before suspend, do nothing. */
1427 if (!export
->suspend
.enabled
) {
1428 /* release lock taken in pwm_class_get_state */
1429 mutex_unlock(&export
->lock
);
1433 state
.enabled
= export
->suspend
.enabled
;
1434 ret
= pwm_class_apply_state(export
, pwm
, &state
);
1442 static int pwm_class_suspend(struct device
*pwmchip_dev
)
1444 struct pwm_chip
*chip
= pwmchip_from_dev(pwmchip_dev
);
1448 for (i
= 0; i
< chip
->npwm
; i
++) {
1449 struct pwm_device
*pwm
= &chip
->pwms
[i
];
1450 struct pwm_state state
;
1451 struct pwm_export
*export
;
1453 export
= pwm_class_get_state(pwmchip_dev
, pwm
, &state
);
1458 * If pwmchip was not enabled before suspend, save
1459 * state for resume time and do nothing else.
1461 export
->suspend
= state
;
1462 if (!state
.enabled
) {
1463 /* release lock taken in pwm_class_get_state */
1464 mutex_unlock(&export
->lock
);
1468 state
.enabled
= false;
1469 ret
= pwm_class_apply_state(export
, pwm
, &state
);
1472 * roll back the PWM devices that were disabled by
1473 * this suspend function.
1475 pwm_class_resume_npwm(pwmchip_dev
, i
);
1483 static int pwm_class_resume(struct device
*pwmchip_dev
)
1485 struct pwm_chip
*chip
= pwmchip_from_dev(pwmchip_dev
);
1487 return pwm_class_resume_npwm(pwmchip_dev
, chip
->npwm
);
1490 static DEFINE_SIMPLE_DEV_PM_OPS(pwm_class_pm_ops
, pwm_class_suspend
, pwm_class_resume
);
1492 static struct class pwm_class
= {
1494 .dev_groups
= pwm_chip_groups
,
1495 .pm
= pm_sleep_ptr(&pwm_class_pm_ops
),
1498 static void pwmchip_sysfs_unexport(struct pwm_chip
*chip
)
1502 for (i
= 0; i
< chip
->npwm
; i
++) {
1503 struct pwm_device
*pwm
= &chip
->pwms
[i
];
1505 if (test_bit(PWMF_EXPORTED
, &pwm
->flags
))
1506 pwm_unexport_child(&chip
->dev
, pwm
);
1510 #define PWMCHIP_ALIGN ARCH_DMA_MINALIGN
1512 static void *pwmchip_priv(struct pwm_chip
*chip
)
1514 return (void *)chip
+ ALIGN(struct_size(chip
, pwms
, chip
->npwm
), PWMCHIP_ALIGN
);
1517 /* This is the counterpart to pwmchip_alloc() */
1518 void pwmchip_put(struct pwm_chip
*chip
)
1520 put_device(&chip
->dev
);
1522 EXPORT_SYMBOL_GPL(pwmchip_put
);
1524 static void pwmchip_release(struct device
*pwmchip_dev
)
1526 struct pwm_chip
*chip
= pwmchip_from_dev(pwmchip_dev
);
1531 struct pwm_chip
*pwmchip_alloc(struct device
*parent
, unsigned int npwm
, size_t sizeof_priv
)
1533 struct pwm_chip
*chip
;
1534 struct device
*pwmchip_dev
;
1538 alloc_size
= size_add(ALIGN(struct_size(chip
, pwms
, npwm
), PWMCHIP_ALIGN
),
1541 chip
= kzalloc(alloc_size
, GFP_KERNEL
);
1543 return ERR_PTR(-ENOMEM
);
1546 chip
->uses_pwmchip_alloc
= true;
1547 chip
->operational
= false;
1549 pwmchip_dev
= &chip
->dev
;
1550 device_initialize(pwmchip_dev
);
1551 pwmchip_dev
->class = &pwm_class
;
1552 pwmchip_dev
->parent
= parent
;
1553 pwmchip_dev
->release
= pwmchip_release
;
1555 pwmchip_set_drvdata(chip
, pwmchip_priv(chip
));
1557 for (i
= 0; i
< chip
->npwm
; i
++) {
1558 struct pwm_device
*pwm
= &chip
->pwms
[i
];
1565 EXPORT_SYMBOL_GPL(pwmchip_alloc
);
1567 static void devm_pwmchip_put(void *data
)
1569 struct pwm_chip
*chip
= data
;
1574 struct pwm_chip
*devm_pwmchip_alloc(struct device
*parent
, unsigned int npwm
, size_t sizeof_priv
)
1576 struct pwm_chip
*chip
;
1579 chip
= pwmchip_alloc(parent
, npwm
, sizeof_priv
);
1583 ret
= devm_add_action_or_reset(parent
, devm_pwmchip_put
, chip
);
1585 return ERR_PTR(ret
);
1589 EXPORT_SYMBOL_GPL(devm_pwmchip_alloc
);
1591 static void of_pwmchip_add(struct pwm_chip
*chip
)
1593 if (!pwmchip_parent(chip
) || !pwmchip_parent(chip
)->of_node
)
1596 if (!chip
->of_xlate
)
1597 chip
->of_xlate
= of_pwm_xlate_with_flags
;
1599 of_node_get(pwmchip_parent(chip
)->of_node
);
1602 static void of_pwmchip_remove(struct pwm_chip
*chip
)
1604 if (pwmchip_parent(chip
))
1605 of_node_put(pwmchip_parent(chip
)->of_node
);
1608 static bool pwm_ops_check(const struct pwm_chip
*chip
)
1610 const struct pwm_ops
*ops
= chip
->ops
;
1612 if (ops
->write_waveform
) {
1613 if (!ops
->round_waveform_tohw
||
1614 !ops
->round_waveform_fromhw
||
1615 !ops
->write_waveform
)
1618 if (WFHWSIZE
< ops
->sizeof_wfhw
) {
1619 dev_warn(pwmchip_parent(chip
), "WFHWSIZE < %zu\n", ops
->sizeof_wfhw
);
1626 if (IS_ENABLED(CONFIG_PWM_DEBUG
) && !ops
->get_state
)
1627 dev_warn(pwmchip_parent(chip
),
1628 "Please implement the .get_state() callback\n");
1634 static struct device_link
*pwm_device_link_add(struct device
*dev
,
1635 struct pwm_device
*pwm
)
1637 struct device_link
*dl
;
1641 * No device for the PWM consumer has been provided. It may
1642 * impact the PM sequence ordering: the PWM supplier may get
1643 * suspended before the consumer.
1645 dev_warn(pwmchip_parent(pwm
->chip
),
1646 "No consumer device specified to create a link to\n");
1650 dl
= device_link_add(dev
, pwmchip_parent(pwm
->chip
), DL_FLAG_AUTOREMOVE_CONSUMER
);
1652 dev_err(dev
, "failed to create device link to %s\n",
1653 dev_name(pwmchip_parent(pwm
->chip
)));
1654 return ERR_PTR(-EINVAL
);
1660 static struct pwm_chip
*fwnode_to_pwmchip(struct fwnode_handle
*fwnode
)
1662 struct pwm_chip
*chip
;
1663 unsigned long id
, tmp
;
1665 guard(mutex
)(&pwm_lock
);
1667 idr_for_each_entry_ul(&pwm_chips
, chip
, tmp
, id
)
1668 if (pwmchip_parent(chip
) && device_match_fwnode(pwmchip_parent(chip
), fwnode
))
1671 return ERR_PTR(-EPROBE_DEFER
);
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
1678 * @con_id: consumer name
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
1692 * error code on failure.
1694 static struct pwm_device
*of_pwm_get(struct device
*dev
, struct device_node
*np
,
1697 struct pwm_device
*pwm
= NULL
;
1698 struct of_phandle_args args
;
1699 struct device_link
*dl
;
1700 struct pwm_chip
*chip
;
1705 index
= of_property_match_string(np
, "pwm-names", con_id
);
1707 return ERR_PTR(index
);
1710 err
= of_parse_phandle_with_args(np
, "pwms", "#pwm-cells", index
,
1713 pr_err("%s(): can't parse \"pwms\" property\n", __func__
);
1714 return ERR_PTR(err
);
1717 chip
= fwnode_to_pwmchip(of_fwnode_handle(args
.np
));
1719 if (PTR_ERR(chip
) != -EPROBE_DEFER
)
1720 pr_err("%s(): PWM chip not found\n", __func__
);
1722 pwm
= ERR_CAST(chip
);
1726 pwm
= chip
->of_xlate(chip
, &args
);
1730 dl
= pwm_device_link_add(dev
, pwm
);
1732 /* of_xlate ended up calling pwm_request_from_chip() */
1739 * If a consumer name was not given, try to look it up from the
1740 * "pwm-names" property if it exists. Otherwise use the name of
1741 * the user device node.
1744 err
= of_property_read_string_index(np
, "pwm-names", index
,
1750 pwm
->label
= con_id
;
1753 of_node_put(args
.np
);
1759 * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
1760 * @fwnode: firmware node to get the "pwms" property from
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
1767 * This is analogous to of_pwm_get() except con_id is not yet supported.
1768 * ACPI entries must look like
1769 * Package () {"pwms", Package ()
1770 * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
1772 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1773 * error code on failure.
1775 static struct pwm_device
*acpi_pwm_get(const struct fwnode_handle
*fwnode
)
1777 struct pwm_device
*pwm
;
1778 struct fwnode_reference_args args
;
1779 struct pwm_chip
*chip
;
1782 memset(&args
, 0, sizeof(args
));
1784 ret
= __acpi_node_get_property_reference(fwnode
, "pwms", 0, 3, &args
);
1786 return ERR_PTR(ret
);
1789 return ERR_PTR(-EPROTO
);
1791 chip
= fwnode_to_pwmchip(args
.fwnode
);
1793 return ERR_CAST(chip
);
1795 pwm
= pwm_request_from_chip(chip
, args
.args
[0], NULL
);
1799 pwm
->args
.period
= args
.args
[1];
1800 pwm
->args
.polarity
= PWM_POLARITY_NORMAL
;
1802 if (args
.nargs
> 2 && args
.args
[2] & PWM_POLARITY_INVERTED
)
1803 pwm
->args
.polarity
= PWM_POLARITY_INVERSED
;
1808 static DEFINE_MUTEX(pwm_lookup_lock
);
1809 static LIST_HEAD(pwm_lookup_list
);
1812 * pwm_get() - look up and request a PWM device
1813 * @dev: device for PWM consumer
1814 * @con_id: consumer name
1816 * Lookup is first attempted using DT. If the device was not instantiated from
1817 * a device tree, a PWM chip and a relative index is looked up via a table
1818 * supplied by board setup code (see pwm_add_table()).
1820 * Once a PWM chip has been found the specified PWM device will be requested
1821 * and is ready to be used.
1823 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1824 * error code on failure.
1826 struct pwm_device
*pwm_get(struct device
*dev
, const char *con_id
)
1828 const struct fwnode_handle
*fwnode
= dev
? dev_fwnode(dev
) : NULL
;
1829 const char *dev_id
= dev
? dev_name(dev
) : NULL
;
1830 struct pwm_device
*pwm
;
1831 struct pwm_chip
*chip
;
1832 struct device_link
*dl
;
1833 unsigned int best
= 0;
1834 struct pwm_lookup
*p
, *chosen
= NULL
;
1838 /* look up via DT first */
1839 if (is_of_node(fwnode
))
1840 return of_pwm_get(dev
, to_of_node(fwnode
), con_id
);
1842 /* then lookup via ACPI */
1843 if (is_acpi_node(fwnode
)) {
1844 pwm
= acpi_pwm_get(fwnode
);
1845 if (!IS_ERR(pwm
) || PTR_ERR(pwm
) != -ENOENT
)
1850 * We look up the provider in the static table typically provided by
1851 * board setup code. We first try to lookup the consumer device by
1852 * name. If the consumer device was passed in as NULL or if no match
1853 * was found, we try to find the consumer by directly looking it up
1856 * If a match is found, the provider PWM chip is looked up by name
1857 * and a PWM device is requested using the PWM device per-chip index.
1859 * The lookup algorithm was shamelessly taken from the clock
1862 * We do slightly fuzzy matching here:
1863 * An entry with a NULL ID is assumed to be a wildcard.
1864 * If an entry has a device ID, it must match
1865 * If an entry has a connection ID, it must match
1866 * Then we take the most specific entry - with the following order
1867 * of precedence: dev+con > dev only > con only.
1869 scoped_guard(mutex
, &pwm_lookup_lock
)
1870 list_for_each_entry(p
, &pwm_lookup_list
, list
) {
1874 if (!dev_id
|| strcmp(p
->dev_id
, dev_id
))
1881 if (!con_id
|| strcmp(p
->con_id
, con_id
))
1898 return ERR_PTR(-ENODEV
);
1900 chip
= pwmchip_find_by_name(chosen
->provider
);
1903 * If the lookup entry specifies a module, load the module and retry
1904 * the PWM chip lookup. This can be used to work around driver load
1905 * ordering issues if driver's can't be made to properly support the
1906 * deferred probe mechanism.
1908 if (!chip
&& chosen
->module
) {
1909 err
= request_module(chosen
->module
);
1911 chip
= pwmchip_find_by_name(chosen
->provider
);
1915 return ERR_PTR(-EPROBE_DEFER
);
1917 pwm
= pwm_request_from_chip(chip
, chosen
->index
, con_id
?: dev_id
);
1921 dl
= pwm_device_link_add(dev
, pwm
);
1924 return ERR_CAST(dl
);
1927 pwm
->args
.period
= chosen
->period
;
1928 pwm
->args
.polarity
= chosen
->polarity
;
1932 EXPORT_SYMBOL_GPL(pwm_get
);
1935 * pwm_put() - release a PWM device
1938 void pwm_put(struct pwm_device
*pwm
)
1940 struct pwm_chip
*chip
;
1947 guard(mutex
)(&pwm_lock
);
1950 * Trigger a warning if a consumer called pwm_put() twice.
1951 * If the chip isn't operational, PWMF_REQUESTED was already cleared in
1952 * pwmchip_remove(). So don't warn in this case.
1954 if (chip
->operational
&& !test_and_clear_bit(PWMF_REQUESTED
, &pwm
->flags
)) {
1955 pr_warn("PWM device already freed\n");
1959 if (chip
->operational
&& chip
->ops
->free
)
1960 pwm
->chip
->ops
->free(pwm
->chip
, pwm
);
1964 put_device(&chip
->dev
);
1966 module_put(chip
->owner
);
1968 EXPORT_SYMBOL_GPL(pwm_put
);
1970 static void devm_pwm_release(void *pwm
)
1976 * devm_pwm_get() - resource managed pwm_get()
1977 * @dev: device for PWM consumer
1978 * @con_id: consumer name
1980 * This function performs like pwm_get() but the acquired PWM device will
1981 * automatically be released on driver detach.
1983 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1984 * error code on failure.
1986 struct pwm_device
*devm_pwm_get(struct device
*dev
, const char *con_id
)
1988 struct pwm_device
*pwm
;
1991 pwm
= pwm_get(dev
, con_id
);
1995 ret
= devm_add_action_or_reset(dev
, devm_pwm_release
, pwm
);
1997 return ERR_PTR(ret
);
2001 EXPORT_SYMBOL_GPL(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
2007 * @con_id: consumer name
2009 * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
2010 * acpi_pwm_get() for a detailed description.
2012 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
2013 * error code on failure.
2015 struct pwm_device
*devm_fwnode_pwm_get(struct device
*dev
,
2016 struct fwnode_handle
*fwnode
,
2019 struct pwm_device
*pwm
= ERR_PTR(-ENODEV
);
2022 if (is_of_node(fwnode
))
2023 pwm
= of_pwm_get(dev
, to_of_node(fwnode
), con_id
);
2024 else if (is_acpi_node(fwnode
))
2025 pwm
= acpi_pwm_get(fwnode
);
2029 ret
= devm_add_action_or_reset(dev
, devm_pwm_release
, pwm
);
2031 return ERR_PTR(ret
);
2035 EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get
);
2038 * __pwmchip_add() - register a new PWM chip
2039 * @chip: the PWM chip to add
2040 * @owner: reference to the module providing the chip.
2042 * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the
2043 * pwmchip_add wrapper to do this right.
2045 * Returns: 0 on success or a negative error code on failure.
2047 int __pwmchip_add(struct pwm_chip
*chip
, struct module
*owner
)
2051 if (!chip
|| !pwmchip_parent(chip
) || !chip
->ops
|| !chip
->npwm
)
2055 * a struct pwm_chip must be allocated using (devm_)pwmchip_alloc,
2056 * otherwise the embedded struct device might disappear too early
2057 * resulting in memory corruption.
2058 * Catch drivers that were not converted appropriately.
2060 if (!chip
->uses_pwmchip_alloc
)
2063 if (!pwm_ops_check(chip
))
2066 chip
->owner
= owner
;
2069 spin_lock_init(&chip
->atomic_lock
);
2071 mutex_init(&chip
->nonatomic_lock
);
2073 guard(mutex
)(&pwm_lock
);
2075 ret
= idr_alloc(&pwm_chips
, chip
, 0, 0, GFP_KERNEL
);
2081 dev_set_name(&chip
->dev
, "pwmchip%u", chip
->id
);
2083 if (IS_ENABLED(CONFIG_OF
))
2084 of_pwmchip_add(chip
);
2086 scoped_guard(pwmchip
, chip
)
2087 chip
->operational
= true;
2089 ret
= device_add(&chip
->dev
);
2091 goto err_device_add
;
2096 scoped_guard(pwmchip
, chip
)
2097 chip
->operational
= false;
2099 if (IS_ENABLED(CONFIG_OF
))
2100 of_pwmchip_remove(chip
);
2102 idr_remove(&pwm_chips
, chip
->id
);
2106 EXPORT_SYMBOL_GPL(__pwmchip_add
);
2109 * pwmchip_remove() - remove a PWM chip
2110 * @chip: the PWM chip to remove
2112 * Removes a PWM chip.
2114 void pwmchip_remove(struct pwm_chip
*chip
)
2116 pwmchip_sysfs_unexport(chip
);
2118 scoped_guard(mutex
, &pwm_lock
) {
2121 scoped_guard(pwmchip
, chip
)
2122 chip
->operational
= false;
2124 for (i
= 0; i
< chip
->npwm
; ++i
) {
2125 struct pwm_device
*pwm
= &chip
->pwms
[i
];
2127 if (test_and_clear_bit(PWMF_REQUESTED
, &pwm
->flags
)) {
2128 dev_warn(&chip
->dev
, "Freeing requested PWM #%u\n", i
);
2129 if (pwm
->chip
->ops
->free
)
2130 pwm
->chip
->ops
->free(pwm
->chip
, pwm
);
2134 if (IS_ENABLED(CONFIG_OF
))
2135 of_pwmchip_remove(chip
);
2137 idr_remove(&pwm_chips
, chip
->id
);
2140 device_del(&chip
->dev
);
2142 EXPORT_SYMBOL_GPL(pwmchip_remove
);
2144 static void devm_pwmchip_remove(void *data
)
2146 struct pwm_chip
*chip
= data
;
2148 pwmchip_remove(chip
);
2151 int __devm_pwmchip_add(struct device
*dev
, struct pwm_chip
*chip
, struct module
*owner
)
2155 ret
= __pwmchip_add(chip
, owner
);
2159 return devm_add_action_or_reset(dev
, devm_pwmchip_remove
, chip
);
2161 EXPORT_SYMBOL_GPL(__devm_pwmchip_add
);
2164 * pwm_add_table() - register PWM device consumers
2165 * @table: array of consumers to register
2166 * @num: number of consumers in table
2168 void pwm_add_table(struct pwm_lookup
*table
, size_t num
)
2170 guard(mutex
)(&pwm_lookup_lock
);
2173 list_add_tail(&table
->list
, &pwm_lookup_list
);
2179 * pwm_remove_table() - unregister PWM device consumers
2180 * @table: array of consumers to unregister
2181 * @num: number of consumers in table
2183 void pwm_remove_table(struct pwm_lookup
*table
, size_t num
)
2185 guard(mutex
)(&pwm_lookup_lock
);
2188 list_del(&table
->list
);
2193 static void pwm_dbg_show(struct pwm_chip
*chip
, struct seq_file
*s
)
2197 for (i
= 0; i
< chip
->npwm
; i
++) {
2198 struct pwm_device
*pwm
= &chip
->pwms
[i
];
2199 struct pwm_state state
;
2201 pwm_get_state(pwm
, &state
);
2203 seq_printf(s
, " pwm-%-3d (%-20.20s):", i
, pwm
->label
);
2205 if (test_bit(PWMF_REQUESTED
, &pwm
->flags
))
2206 seq_puts(s
, " requested");
2209 seq_puts(s
, " enabled");
2211 seq_printf(s
, " period: %llu ns", state
.period
);
2212 seq_printf(s
, " duty: %llu ns", state
.duty_cycle
);
2213 seq_printf(s
, " polarity: %s",
2214 state
.polarity
? "inverse" : "normal");
2216 if (state
.usage_power
)
2217 seq_puts(s
, " usage_power");
2223 static void *pwm_seq_start(struct seq_file
*s
, loff_t
*pos
)
2225 unsigned long id
= *pos
;
2228 mutex_lock(&pwm_lock
);
2231 ret
= idr_get_next_ul(&pwm_chips
, &id
);
2236 static void *pwm_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
2238 unsigned long id
= *pos
+ 1;
2243 ret
= idr_get_next_ul(&pwm_chips
, &id
);
2248 static void pwm_seq_stop(struct seq_file
*s
, void *v
)
2250 mutex_unlock(&pwm_lock
);
2253 static int pwm_seq_show(struct seq_file
*s
, void *v
)
2255 struct pwm_chip
*chip
= v
;
2257 seq_printf(s
, "%s%d: %s/%s, %d PWM device%s\n",
2258 (char *)s
->private, chip
->id
,
2259 pwmchip_parent(chip
)->bus
? pwmchip_parent(chip
)->bus
->name
: "no-bus",
2260 dev_name(pwmchip_parent(chip
)), chip
->npwm
,
2261 (chip
->npwm
!= 1) ? "s" : "");
2263 pwm_dbg_show(chip
, s
);
2268 static const struct seq_operations pwm_debugfs_sops
= {
2269 .start
= pwm_seq_start
,
2270 .next
= pwm_seq_next
,
2271 .stop
= pwm_seq_stop
,
2272 .show
= pwm_seq_show
,
2275 DEFINE_SEQ_ATTRIBUTE(pwm_debugfs
);
2277 static int __init
pwm_init(void)
2281 ret
= class_register(&pwm_class
);
2283 pr_err("Failed to initialize PWM class (%pe)\n", ERR_PTR(ret
));
2287 if (IS_ENABLED(CONFIG_DEBUG_FS
))
2288 debugfs_create_file("pwm", 0444, NULL
, NULL
, &pwm_debugfs_fops
);
2292 subsys_initcall(pwm_init
);