1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/device.h>
7 #include <linux/module.h>
8 #include <linux/mutex.h>
11 MODULE_IMPORT_NS("PWM");
16 * enum pwm_polarity - polarity of a PWM signal
17 * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
18 * cycle, followed by a low signal for the remainder of the pulse
20 * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
21 * cycle, followed by a high signal for the remainder of the pulse
26 PWM_POLARITY_INVERSED
,
30 * struct pwm_args - board-dependent PWM arguments
31 * @period: reference period
32 * @polarity: reference polarity
34 * This structure describes board-dependent arguments attached to a PWM
35 * device. These arguments are usually retrieved from the PWM lookup table or
38 * Do not confuse this with the PWM state: PWM arguments represent the initial
39 * configuration that users want to use on this PWM device rather than the
40 * current PWM hardware state.
44 enum pwm_polarity polarity
;
53 * struct pwm_waveform - description of a PWM waveform
54 * @period_length_ns: PWM period
55 * @duty_length_ns: PWM duty cycle
56 * @duty_offset_ns: offset of the rising edge from the period's start
58 * This is a representation of a PWM waveform alternative to struct pwm_state
59 * below. It's more expressive than struct pwm_state as it contains a
60 * duty_offset_ns and so can represent offsets other than zero (with .polarity =
61 * PWM_POLARITY_NORMAL) and period - duty_cycle (.polarity =
62 * PWM_POLARITY_INVERSED).
64 * Note there is no explicit bool for enabled. A "disabled" PWM is represented
65 * by .period_length_ns = 0. Note further that the behaviour of a "disabled" PWM
66 * is undefined. Depending on the hardware's capabilities it might drive the
67 * active or inactive level, go high-z or even continue to toggle.
69 * The unit for all three members is nanoseconds.
78 * struct pwm_state - state of a PWM channel
79 * @period: PWM period (in nanoseconds)
80 * @duty_cycle: PWM duty cycle (in nanoseconds)
81 * @polarity: PWM polarity
82 * @enabled: PWM enabled status
83 * @usage_power: If set, the PWM driver is only required to maintain the power
84 * output but has more freedom regarding signal form.
85 * If supported, the signal can be optimized, for example to
86 * improve EMI by phase shifting individual channels.
91 enum pwm_polarity polarity
;
97 * struct pwm_device - PWM channel object
98 * @label: name of the PWM device
99 * @flags: flags associated with the PWM device
100 * @hwpwm: per-chip relative index of the PWM device
101 * @chip: PWM chip providing this PWM device
102 * @args: PWM arguments
103 * @state: last applied state
104 * @last: last implemented state (for PWM_DEBUG)
110 struct pwm_chip
*chip
;
112 struct pwm_args args
;
113 struct pwm_state state
;
114 struct pwm_state last
;
118 * pwm_get_state() - retrieve the current PWM state
120 * @state: state to fill with the current PWM state
122 * The returned PWM state represents the state that was applied by a previous call to
123 * pwm_apply_might_sleep(). Drivers may have to slightly tweak that state before programming it to
124 * hardware. If pwm_apply_might_sleep() was never called, this returns either the current hardware
125 * state (if supported) or the default settings.
127 static inline void pwm_get_state(const struct pwm_device
*pwm
,
128 struct pwm_state
*state
)
133 static inline bool pwm_is_enabled(const struct pwm_device
*pwm
)
135 struct pwm_state state
;
137 pwm_get_state(pwm
, &state
);
139 return state
.enabled
;
142 static inline u64
pwm_get_period(const struct pwm_device
*pwm
)
144 struct pwm_state state
;
146 pwm_get_state(pwm
, &state
);
151 static inline u64
pwm_get_duty_cycle(const struct pwm_device
*pwm
)
153 struct pwm_state state
;
155 pwm_get_state(pwm
, &state
);
157 return state
.duty_cycle
;
160 static inline enum pwm_polarity
pwm_get_polarity(const struct pwm_device
*pwm
)
162 struct pwm_state state
;
164 pwm_get_state(pwm
, &state
);
166 return state
.polarity
;
169 static inline void pwm_get_args(const struct pwm_device
*pwm
,
170 struct pwm_args
*args
)
176 * pwm_init_state() - prepare a new state to be applied with pwm_apply_might_sleep()
178 * @state: state to fill with the prepared PWM state
180 * This functions prepares a state that can later be tweaked and applied
181 * to the PWM device with pwm_apply_might_sleep(). This is a convenient function
182 * that first retrieves the current PWM state and the replaces the period
183 * and polarity fields with the reference values defined in pwm->args.
184 * Once the function returns, you can adjust the ->enabled and ->duty_cycle
185 * fields according to your needs before calling pwm_apply_might_sleep().
187 * ->duty_cycle is initially set to zero to avoid cases where the current
188 * ->duty_cycle value exceed the pwm_args->period one, which would trigger
189 * an error if the user calls pwm_apply_might_sleep() without adjusting ->duty_cycle
192 static inline void pwm_init_state(const struct pwm_device
*pwm
,
193 struct pwm_state
*state
)
195 struct pwm_args args
;
197 /* First get the current state. */
198 pwm_get_state(pwm
, state
);
200 /* Then fill it with the reference config */
201 pwm_get_args(pwm
, &args
);
203 state
->period
= args
.period
;
204 state
->polarity
= args
.polarity
;
205 state
->duty_cycle
= 0;
206 state
->usage_power
= false;
210 * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
211 * @state: PWM state to extract the duty cycle from
212 * @scale: target scale of the relative duty cycle
214 * This functions converts the absolute duty cycle stored in @state (expressed
215 * in nanosecond) into a value relative to the period.
217 * For example if you want to get the duty_cycle expressed in percent, call:
219 * pwm_get_state(pwm, &state);
220 * duty = pwm_get_relative_duty_cycle(&state, 100);
222 static inline unsigned int
223 pwm_get_relative_duty_cycle(const struct pwm_state
*state
, unsigned int scale
)
228 return DIV_ROUND_CLOSEST_ULL((u64
)state
->duty_cycle
* scale
,
233 * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
234 * @state: PWM state to fill
235 * @duty_cycle: relative duty cycle value
236 * @scale: scale in which @duty_cycle is expressed
238 * This functions converts a relative into an absolute duty cycle (expressed
239 * in nanoseconds), and puts the result in state->duty_cycle.
241 * For example if you want to configure a 50% duty cycle, call:
243 * pwm_init_state(pwm, &state);
244 * pwm_set_relative_duty_cycle(&state, 50, 100);
245 * pwm_apply_might_sleep(pwm, &state);
247 * This functions returns -EINVAL if @duty_cycle and/or @scale are
248 * inconsistent (@scale == 0 or @duty_cycle > @scale).
251 pwm_set_relative_duty_cycle(struct pwm_state
*state
, unsigned int duty_cycle
,
254 if (!scale
|| duty_cycle
> scale
)
257 state
->duty_cycle
= DIV_ROUND_CLOSEST_ULL((u64
)duty_cycle
*
265 * struct pwm_capture - PWM capture data
266 * @period: period of the PWM signal (in nanoseconds)
267 * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
271 unsigned int duty_cycle
;
275 * struct pwm_ops - PWM controller operations
276 * @request: optional hook for requesting a PWM
277 * @free: optional hook for freeing a PWM
278 * @capture: capture and report PWM signal
279 * @sizeof_wfhw: size (in bytes) of driver specific waveform presentation
280 * @round_waveform_tohw: convert a struct pwm_waveform to driver specific presentation
281 * @round_waveform_fromhw: convert a driver specific waveform presentation to struct pwm_waveform
282 * @read_waveform: read driver specific waveform presentation from hardware
283 * @write_waveform: write driver specific waveform presentation to hardware
284 * @apply: atomically apply a new PWM config
285 * @get_state: get the current PWM state.
288 int (*request
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
);
289 void (*free
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
);
290 int (*capture
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
291 struct pwm_capture
*result
, unsigned long timeout
);
294 int (*round_waveform_tohw
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
295 const struct pwm_waveform
*wf
, void *wfhw
);
296 int (*round_waveform_fromhw
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
297 const void *wfhw
, struct pwm_waveform
*wf
);
298 int (*read_waveform
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
300 int (*write_waveform
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
303 int (*apply
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
304 const struct pwm_state
*state
);
305 int (*get_state
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
306 struct pwm_state
*state
);
310 * struct pwm_chip - abstract a PWM controller
311 * @dev: device providing the PWMs
312 * @ops: callbacks for this PWM controller
313 * @owner: module providing this chip
314 * @id: unique number of this PWM chip
315 * @npwm: number of PWMs controlled by this chip
316 * @of_xlate: request a PWM device given a device tree PWM specifier
317 * @atomic: can the driver's ->apply() be called in atomic context
318 * @uses_pwmchip_alloc: signals if pwmchip_allow was used to allocate this chip
319 * @operational: signals if the chip can be used (or is already deregistered)
320 * @nonatomic_lock: mutex for nonatomic chips
321 * @atomic_lock: mutex for atomic chips
322 * @pwms: array of PWM devices allocated by the framework
326 const struct pwm_ops
*ops
;
327 struct module
*owner
;
331 struct pwm_device
* (*of_xlate
)(struct pwm_chip
*chip
,
332 const struct of_phandle_args
*args
);
335 /* only used internally by the PWM framework */
336 bool uses_pwmchip_alloc
;
340 * depending on the chip being atomic or not either the mutex or
341 * the spinlock is used. It protects .operational and
342 * synchronizes the callbacks in .ops
344 struct mutex nonatomic_lock
;
345 spinlock_t atomic_lock
;
347 struct pwm_device pwms
[] __counted_by(npwm
);
350 static inline struct device
*pwmchip_parent(const struct pwm_chip
*chip
)
352 return chip
->dev
.parent
;
355 static inline void *pwmchip_get_drvdata(struct pwm_chip
*chip
)
357 return dev_get_drvdata(&chip
->dev
);
360 static inline void pwmchip_set_drvdata(struct pwm_chip
*chip
, void *data
)
362 dev_set_drvdata(&chip
->dev
, data
);
365 #if IS_ENABLED(CONFIG_PWM)
367 /* PWM consumer APIs */
368 int pwm_round_waveform_might_sleep(struct pwm_device
*pwm
, struct pwm_waveform
*wf
);
369 int pwm_get_waveform_might_sleep(struct pwm_device
*pwm
, struct pwm_waveform
*wf
);
370 int pwm_set_waveform_might_sleep(struct pwm_device
*pwm
, const struct pwm_waveform
*wf
, bool exact
);
371 int pwm_apply_might_sleep(struct pwm_device
*pwm
, const struct pwm_state
*state
);
372 int pwm_apply_atomic(struct pwm_device
*pwm
, const struct pwm_state
*state
);
373 int pwm_get_state_hw(struct pwm_device
*pwm
, struct pwm_state
*state
);
374 int pwm_adjust_config(struct pwm_device
*pwm
);
377 * pwm_config() - change a PWM device configuration
379 * @duty_ns: "on" time (in nanoseconds)
380 * @period_ns: duration (in nanoseconds) of one cycle
382 * Returns: 0 on success or a negative error code on failure.
384 static inline int pwm_config(struct pwm_device
*pwm
, int duty_ns
,
387 struct pwm_state state
;
392 if (duty_ns
< 0 || period_ns
< 0)
395 pwm_get_state(pwm
, &state
);
396 if (state
.duty_cycle
== duty_ns
&& state
.period
== period_ns
)
399 state
.duty_cycle
= duty_ns
;
400 state
.period
= period_ns
;
401 return pwm_apply_might_sleep(pwm
, &state
);
405 * pwm_enable() - start a PWM output toggling
408 * Returns: 0 on success or a negative error code on failure.
410 static inline int pwm_enable(struct pwm_device
*pwm
)
412 struct pwm_state state
;
417 pwm_get_state(pwm
, &state
);
421 state
.enabled
= true;
422 return pwm_apply_might_sleep(pwm
, &state
);
426 * pwm_disable() - stop a PWM output toggling
429 static inline void pwm_disable(struct pwm_device
*pwm
)
431 struct pwm_state state
;
436 pwm_get_state(pwm
, &state
);
440 state
.enabled
= false;
441 pwm_apply_might_sleep(pwm
, &state
);
445 * pwm_might_sleep() - is pwm_apply_atomic() supported?
448 * Returns: false if pwm_apply_atomic() can be called from atomic context.
450 static inline bool pwm_might_sleep(struct pwm_device
*pwm
)
452 return !pwm
->chip
->atomic
;
455 /* PWM provider APIs */
456 void pwmchip_put(struct pwm_chip
*chip
);
457 struct pwm_chip
*pwmchip_alloc(struct device
*parent
, unsigned int npwm
, size_t sizeof_priv
);
458 struct pwm_chip
*devm_pwmchip_alloc(struct device
*parent
, unsigned int npwm
, size_t sizeof_priv
);
460 int __pwmchip_add(struct pwm_chip
*chip
, struct module
*owner
);
461 #define pwmchip_add(chip) __pwmchip_add(chip, THIS_MODULE)
462 void pwmchip_remove(struct pwm_chip
*chip
);
464 int __devm_pwmchip_add(struct device
*dev
, struct pwm_chip
*chip
, struct module
*owner
);
465 #define devm_pwmchip_add(dev, chip) __devm_pwmchip_add(dev, chip, THIS_MODULE)
467 struct pwm_device
*of_pwm_xlate_with_flags(struct pwm_chip
*chip
,
468 const struct of_phandle_args
*args
);
469 struct pwm_device
*of_pwm_single_xlate(struct pwm_chip
*chip
,
470 const struct of_phandle_args
*args
);
472 struct pwm_device
*pwm_get(struct device
*dev
, const char *con_id
);
473 void pwm_put(struct pwm_device
*pwm
);
475 struct pwm_device
*devm_pwm_get(struct device
*dev
, const char *con_id
);
476 struct pwm_device
*devm_fwnode_pwm_get(struct device
*dev
,
477 struct fwnode_handle
*fwnode
,
480 static inline bool pwm_might_sleep(struct pwm_device
*pwm
)
485 static inline int pwm_apply_might_sleep(struct pwm_device
*pwm
,
486 const struct pwm_state
*state
)
492 static inline int pwm_apply_atomic(struct pwm_device
*pwm
,
493 const struct pwm_state
*state
)
498 static inline int pwm_get_state_hw(struct pwm_device
*pwm
, struct pwm_state
*state
)
503 static inline int pwm_adjust_config(struct pwm_device
*pwm
)
508 static inline int pwm_config(struct pwm_device
*pwm
, int duty_ns
,
515 static inline int pwm_enable(struct pwm_device
*pwm
)
521 static inline void pwm_disable(struct pwm_device
*pwm
)
526 static inline void pwmchip_put(struct pwm_chip
*chip
)
530 static inline struct pwm_chip
*pwmchip_alloc(struct device
*parent
,
534 return ERR_PTR(-EINVAL
);
537 static inline struct pwm_chip
*devm_pwmchip_alloc(struct device
*parent
,
541 return pwmchip_alloc(parent
, npwm
, sizeof_priv
);
544 static inline int pwmchip_add(struct pwm_chip
*chip
)
549 static inline int pwmchip_remove(struct pwm_chip
*chip
)
554 static inline int devm_pwmchip_add(struct device
*dev
, struct pwm_chip
*chip
)
559 static inline struct pwm_device
*pwm_get(struct device
*dev
,
560 const char *consumer
)
563 return ERR_PTR(-ENODEV
);
566 static inline void pwm_put(struct pwm_device
*pwm
)
571 static inline struct pwm_device
*devm_pwm_get(struct device
*dev
,
572 const char *consumer
)
575 return ERR_PTR(-ENODEV
);
578 static inline struct pwm_device
*
579 devm_fwnode_pwm_get(struct device
*dev
, struct fwnode_handle
*fwnode
,
583 return ERR_PTR(-ENODEV
);
587 static inline void pwm_apply_args(struct pwm_device
*pwm
)
589 struct pwm_state state
= { };
592 * PWM users calling pwm_apply_args() expect to have a fresh config
593 * where the polarity and period are set according to pwm_args info.
594 * The problem is, polarity can only be changed when the PWM is
597 * PWM drivers supporting hardware readout may declare the PWM device
598 * as enabled, and prevent polarity setting, which changes from the
599 * existing behavior, where all PWM devices are declared as disabled
600 * at startup (even if they are actually enabled), thus authorizing
603 * To fulfill this requirement, we apply a new state which disables
604 * the PWM device and set the reference period and polarity config.
606 * Note that PWM users requiring a smooth handover between the
607 * bootloader and the kernel (like critical regulators controlled by
608 * PWM devices) will have to switch to the atomic API and avoid calling
612 state
.enabled
= false;
613 state
.polarity
= pwm
->args
.polarity
;
614 state
.period
= pwm
->args
.period
;
615 state
.usage_power
= false;
617 pwm_apply_might_sleep(pwm
, &state
);
621 struct list_head list
;
622 const char *provider
;
627 enum pwm_polarity polarity
;
628 const char *module
; /* optional, may be NULL */
631 #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \
632 _period, _polarity, _module) \
634 .provider = _provider, \
639 .polarity = _polarity, \
643 #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
644 PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
647 #if IS_ENABLED(CONFIG_PWM)
648 void pwm_add_table(struct pwm_lookup
*table
, size_t num
);
649 void pwm_remove_table(struct pwm_lookup
*table
, size_t num
);
651 static inline void pwm_add_table(struct pwm_lookup
*table
, size_t num
)
655 static inline void pwm_remove_table(struct pwm_lookup
*table
, size_t num
)
660 #endif /* __LINUX_PWM_H */