]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - include/linux/pwm.h
1 /* SPDX-License-Identifier: GPL-2.0 */
6 #include <linux/mutex.h>
15 * enum pwm_polarity - polarity of a PWM signal
16 * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
17 * cycle, followed by a low signal for the remainder of the pulse
19 * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
20 * cycle, followed by a high signal for the remainder of the pulse
25 PWM_POLARITY_INVERSED
,
29 * struct pwm_args - board-dependent PWM arguments
30 * @period: reference period
31 * @polarity: reference polarity
33 * This structure describes board-dependent arguments attached to a PWM
34 * device. These arguments are usually retrieved from the PWM lookup table or
37 * Do not confuse this with the PWM state: PWM arguments represent the initial
38 * configuration that users want to use on this PWM device rather than the
39 * current PWM hardware state.
43 enum pwm_polarity polarity
;
47 PWMF_REQUESTED
= 1 << 0,
48 PWMF_EXPORTED
= 1 << 1,
52 * struct pwm_state - state of a PWM channel
53 * @period: PWM period (in nanoseconds)
54 * @duty_cycle: PWM duty cycle (in nanoseconds)
55 * @polarity: PWM polarity
56 * @enabled: PWM enabled status
61 enum pwm_polarity polarity
;
66 * struct pwm_device - PWM channel object
67 * @label: name of the PWM device
68 * @flags: flags associated with the PWM device
69 * @hwpwm: per-chip relative index of the PWM device
70 * @pwm: global index of the PWM device
71 * @chip: PWM chip providing this PWM device
72 * @chip_data: chip-private data associated with the PWM device
73 * @args: PWM arguments
74 * @state: last applied state
75 * @last: last implemented state (for PWM_DEBUG)
82 struct pwm_chip
*chip
;
86 struct pwm_state state
;
87 struct pwm_state last
;
91 * pwm_get_state() - retrieve the current PWM state
93 * @state: state to fill with the current PWM state
95 static inline void pwm_get_state(const struct pwm_device
*pwm
,
96 struct pwm_state
*state
)
101 static inline bool pwm_is_enabled(const struct pwm_device
*pwm
)
103 struct pwm_state state
;
105 pwm_get_state(pwm
, &state
);
107 return state
.enabled
;
110 static inline void pwm_set_period(struct pwm_device
*pwm
, u64 period
)
113 pwm
->state
.period
= period
;
116 static inline u64
pwm_get_period(const struct pwm_device
*pwm
)
118 struct pwm_state state
;
120 pwm_get_state(pwm
, &state
);
125 static inline void pwm_set_duty_cycle(struct pwm_device
*pwm
, unsigned int duty
)
128 pwm
->state
.duty_cycle
= duty
;
131 static inline u64
pwm_get_duty_cycle(const struct pwm_device
*pwm
)
133 struct pwm_state state
;
135 pwm_get_state(pwm
, &state
);
137 return state
.duty_cycle
;
140 static inline enum pwm_polarity
pwm_get_polarity(const struct pwm_device
*pwm
)
142 struct pwm_state state
;
144 pwm_get_state(pwm
, &state
);
146 return state
.polarity
;
149 static inline void pwm_get_args(const struct pwm_device
*pwm
,
150 struct pwm_args
*args
)
156 * pwm_init_state() - prepare a new state to be applied with pwm_apply_state()
158 * @state: state to fill with the prepared PWM state
160 * This functions prepares a state that can later be tweaked and applied
161 * to the PWM device with pwm_apply_state(). This is a convenient function
162 * that first retrieves the current PWM state and the replaces the period
163 * and polarity fields with the reference values defined in pwm->args.
164 * Once the function returns, you can adjust the ->enabled and ->duty_cycle
165 * fields according to your needs before calling pwm_apply_state().
167 * ->duty_cycle is initially set to zero to avoid cases where the current
168 * ->duty_cycle value exceed the pwm_args->period one, which would trigger
169 * an error if the user calls pwm_apply_state() without adjusting ->duty_cycle
172 static inline void pwm_init_state(const struct pwm_device
*pwm
,
173 struct pwm_state
*state
)
175 struct pwm_args args
;
177 /* First get the current state. */
178 pwm_get_state(pwm
, state
);
180 /* Then fill it with the reference config */
181 pwm_get_args(pwm
, &args
);
183 state
->period
= args
.period
;
184 state
->polarity
= args
.polarity
;
185 state
->duty_cycle
= 0;
189 * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
190 * @state: PWM state to extract the duty cycle from
191 * @scale: target scale of the relative duty cycle
193 * This functions converts the absolute duty cycle stored in @state (expressed
194 * in nanosecond) into a value relative to the period.
196 * For example if you want to get the duty_cycle expressed in percent, call:
198 * pwm_get_state(pwm, &state);
199 * duty = pwm_get_relative_duty_cycle(&state, 100);
201 static inline unsigned int
202 pwm_get_relative_duty_cycle(const struct pwm_state
*state
, unsigned int scale
)
207 return DIV_ROUND_CLOSEST_ULL((u64
)state
->duty_cycle
* scale
,
212 * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
213 * @state: PWM state to fill
214 * @duty_cycle: relative duty cycle value
215 * @scale: scale in which @duty_cycle is expressed
217 * This functions converts a relative into an absolute duty cycle (expressed
218 * in nanoseconds), and puts the result in state->duty_cycle.
220 * For example if you want to configure a 50% duty cycle, call:
222 * pwm_init_state(pwm, &state);
223 * pwm_set_relative_duty_cycle(&state, 50, 100);
224 * pwm_apply_state(pwm, &state);
226 * This functions returns -EINVAL if @duty_cycle and/or @scale are
227 * inconsistent (@scale == 0 or @duty_cycle > @scale).
230 pwm_set_relative_duty_cycle(struct pwm_state
*state
, unsigned int duty_cycle
,
233 if (!scale
|| duty_cycle
> scale
)
236 state
->duty_cycle
= DIV_ROUND_CLOSEST_ULL((u64
)duty_cycle
*
244 * struct pwm_ops - PWM controller operations
245 * @request: optional hook for requesting a PWM
246 * @free: optional hook for freeing a PWM
247 * @capture: capture and report PWM signal
248 * @apply: atomically apply a new PWM config
249 * @get_state: get the current PWM state. This function is only
250 * called once per PWM device when the PWM chip is
252 * @owner: helps prevent removal of modules exporting active PWMs
253 * @config: configure duty cycles and period length for this PWM
254 * @set_polarity: configure the polarity of this PWM
255 * @enable: enable PWM output toggling
256 * @disable: disable PWM output toggling
259 int (*request
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
);
260 void (*free
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
);
261 int (*capture
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
262 struct pwm_capture
*result
, unsigned long timeout
);
263 int (*apply
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
264 const struct pwm_state
*state
);
265 void (*get_state
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
266 struct pwm_state
*state
);
267 struct module
*owner
;
269 /* Only used by legacy drivers */
270 int (*config
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
271 int duty_ns
, int period_ns
);
272 int (*set_polarity
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
273 enum pwm_polarity polarity
);
274 int (*enable
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
);
275 void (*disable
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
);
279 * struct pwm_chip - abstract a PWM controller
280 * @dev: device providing the PWMs
281 * @ops: callbacks for this PWM controller
282 * @base: number of first PWM controlled by this chip
283 * @npwm: number of PWMs controlled by this chip
284 * @of_xlate: request a PWM device given a device tree PWM specifier
285 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
286 * @list: list node for internal use
287 * @pwms: array of PWM devices allocated by the framework
291 const struct pwm_ops
*ops
;
295 struct pwm_device
* (*of_xlate
)(struct pwm_chip
*pc
,
296 const struct of_phandle_args
*args
);
297 unsigned int of_pwm_n_cells
;
299 /* only used internally by the PWM framework */
300 struct list_head list
;
301 struct pwm_device
*pwms
;
305 * struct pwm_capture - PWM capture data
306 * @period: period of the PWM signal (in nanoseconds)
307 * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
311 unsigned int duty_cycle
;
314 #if IS_ENABLED(CONFIG_PWM)
316 struct pwm_device
*pwm_request(int pwm_id
, const char *label
);
317 void pwm_free(struct pwm_device
*pwm
);
318 int pwm_apply_state(struct pwm_device
*pwm
, const struct pwm_state
*state
);
319 int pwm_adjust_config(struct pwm_device
*pwm
);
322 * pwm_config() - change a PWM device configuration
324 * @duty_ns: "on" time (in nanoseconds)
325 * @period_ns: duration (in nanoseconds) of one cycle
327 * Returns: 0 on success or a negative error code on failure.
329 static inline int pwm_config(struct pwm_device
*pwm
, int duty_ns
,
332 struct pwm_state state
;
337 if (duty_ns
< 0 || period_ns
< 0)
340 pwm_get_state(pwm
, &state
);
341 if (state
.duty_cycle
== duty_ns
&& state
.period
== period_ns
)
344 state
.duty_cycle
= duty_ns
;
345 state
.period
= period_ns
;
346 return pwm_apply_state(pwm
, &state
);
350 * pwm_enable() - start a PWM output toggling
353 * Returns: 0 on success or a negative error code on failure.
355 static inline int pwm_enable(struct pwm_device
*pwm
)
357 struct pwm_state state
;
362 pwm_get_state(pwm
, &state
);
366 state
.enabled
= true;
367 return pwm_apply_state(pwm
, &state
);
371 * pwm_disable() - stop a PWM output toggling
374 static inline void pwm_disable(struct pwm_device
*pwm
)
376 struct pwm_state state
;
381 pwm_get_state(pwm
, &state
);
385 state
.enabled
= false;
386 pwm_apply_state(pwm
, &state
);
389 /* PWM provider APIs */
390 int pwm_capture(struct pwm_device
*pwm
, struct pwm_capture
*result
,
391 unsigned long timeout
);
392 int pwm_set_chip_data(struct pwm_device
*pwm
, void *data
);
393 void *pwm_get_chip_data(struct pwm_device
*pwm
);
395 int pwmchip_add_with_polarity(struct pwm_chip
*chip
,
396 enum pwm_polarity polarity
);
397 int pwmchip_add(struct pwm_chip
*chip
);
398 int pwmchip_remove(struct pwm_chip
*chip
);
399 struct pwm_device
*pwm_request_from_chip(struct pwm_chip
*chip
,
403 struct pwm_device
*of_pwm_xlate_with_flags(struct pwm_chip
*pc
,
404 const struct of_phandle_args
*args
);
406 struct pwm_device
*pwm_get(struct device
*dev
, const char *con_id
);
407 struct pwm_device
*of_pwm_get(struct device
*dev
, struct device_node
*np
,
409 void pwm_put(struct pwm_device
*pwm
);
411 struct pwm_device
*devm_pwm_get(struct device
*dev
, const char *con_id
);
412 struct pwm_device
*devm_of_pwm_get(struct device
*dev
, struct device_node
*np
,
414 struct pwm_device
*devm_fwnode_pwm_get(struct device
*dev
,
415 struct fwnode_handle
*fwnode
,
417 void devm_pwm_put(struct device
*dev
, struct pwm_device
*pwm
);
419 static inline struct pwm_device
*pwm_request(int pwm_id
, const char *label
)
421 return ERR_PTR(-ENODEV
);
424 static inline void pwm_free(struct pwm_device
*pwm
)
428 static inline int pwm_apply_state(struct pwm_device
*pwm
,
429 const struct pwm_state
*state
)
434 static inline int pwm_adjust_config(struct pwm_device
*pwm
)
439 static inline int pwm_config(struct pwm_device
*pwm
, int duty_ns
,
445 static inline int pwm_capture(struct pwm_device
*pwm
,
446 struct pwm_capture
*result
,
447 unsigned long timeout
)
452 static inline int pwm_enable(struct pwm_device
*pwm
)
457 static inline void pwm_disable(struct pwm_device
*pwm
)
461 static inline int pwm_set_chip_data(struct pwm_device
*pwm
, void *data
)
466 static inline void *pwm_get_chip_data(struct pwm_device
*pwm
)
471 static inline int pwmchip_add(struct pwm_chip
*chip
)
476 static inline int pwmchip_remove(struct pwm_chip
*chip
)
481 static inline struct pwm_device
*pwm_request_from_chip(struct pwm_chip
*chip
,
485 return ERR_PTR(-ENODEV
);
488 static inline struct pwm_device
*pwm_get(struct device
*dev
,
489 const char *consumer
)
491 return ERR_PTR(-ENODEV
);
494 static inline struct pwm_device
*of_pwm_get(struct device
*dev
,
495 struct device_node
*np
,
498 return ERR_PTR(-ENODEV
);
501 static inline void pwm_put(struct pwm_device
*pwm
)
505 static inline struct pwm_device
*devm_pwm_get(struct device
*dev
,
506 const char *consumer
)
508 return ERR_PTR(-ENODEV
);
511 static inline struct pwm_device
*devm_of_pwm_get(struct device
*dev
,
512 struct device_node
*np
,
515 return ERR_PTR(-ENODEV
);
518 static inline struct pwm_device
*
519 devm_fwnode_pwm_get(struct device
*dev
, struct fwnode_handle
*fwnode
,
522 return ERR_PTR(-ENODEV
);
525 static inline void devm_pwm_put(struct device
*dev
, struct pwm_device
*pwm
)
530 static inline void pwm_apply_args(struct pwm_device
*pwm
)
532 struct pwm_state state
= { };
535 * PWM users calling pwm_apply_args() expect to have a fresh config
536 * where the polarity and period are set according to pwm_args info.
537 * The problem is, polarity can only be changed when the PWM is
540 * PWM drivers supporting hardware readout may declare the PWM device
541 * as enabled, and prevent polarity setting, which changes from the
542 * existing behavior, where all PWM devices are declared as disabled
543 * at startup (even if they are actually enabled), thus authorizing
546 * To fulfill this requirement, we apply a new state which disables
547 * the PWM device and set the reference period and polarity config.
549 * Note that PWM users requiring a smooth handover between the
550 * bootloader and the kernel (like critical regulators controlled by
551 * PWM devices) will have to switch to the atomic API and avoid calling
555 state
.enabled
= false;
556 state
.polarity
= pwm
->args
.polarity
;
557 state
.period
= pwm
->args
.period
;
559 pwm_apply_state(pwm
, &state
);
563 struct list_head list
;
564 const char *provider
;
569 enum pwm_polarity polarity
;
570 const char *module
; /* optional, may be NULL */
573 #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \
574 _period, _polarity, _module) \
576 .provider = _provider, \
581 .polarity = _polarity, \
585 #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
586 PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
589 #if IS_ENABLED(CONFIG_PWM)
590 void pwm_add_table(struct pwm_lookup
*table
, size_t num
);
591 void pwm_remove_table(struct pwm_lookup
*table
, size_t num
);
593 static inline void pwm_add_table(struct pwm_lookup
*table
, size_t num
)
597 static inline void pwm_remove_table(struct pwm_lookup
*table
, size_t num
)
602 #ifdef CONFIG_PWM_SYSFS
603 void pwmchip_sysfs_export(struct pwm_chip
*chip
);
604 void pwmchip_sysfs_unexport(struct pwm_chip
*chip
);
606 static inline void pwmchip_sysfs_export(struct pwm_chip
*chip
)
610 static inline void pwmchip_sysfs_unexport(struct pwm_chip
*chip
)
613 #endif /* CONFIG_PWM_SYSFS */
615 #endif /* __LINUX_PWM_H */