1 /* SPDX-License-Identifier: GPL-2.0 */
6 #include <linux/mutex.h>
12 * enum pwm_polarity - polarity of a PWM signal
13 * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
14 * cycle, followed by a low signal for the remainder of the pulse
16 * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
17 * cycle, followed by a high signal for the remainder of the pulse
22 PWM_POLARITY_INVERSED,
26 * struct pwm_args - board-dependent PWM arguments
27 * @period: reference period
28 * @polarity: reference polarity
30 * This structure describes board-dependent arguments attached to a PWM
31 * device. These arguments are usually retrieved from the PWM lookup table or
34 * Do not confuse this with the PWM state: PWM arguments represent the initial
35 * configuration that users want to use on this PWM device rather than the
36 * current PWM hardware state.
40 enum pwm_polarity polarity;
49 * struct pwm_state - state of a PWM channel
50 * @period: PWM period (in nanoseconds)
51 * @duty_cycle: PWM duty cycle (in nanoseconds)
52 * @polarity: PWM polarity
53 * @enabled: PWM enabled status
54 * @usage_power: If set, the PWM driver is only required to maintain the power
55 * output but has more freedom regarding signal form.
56 * If supported, the signal can be optimized, for example to
57 * improve EMI by phase shifting individual channels.
62 enum pwm_polarity polarity;
68 * struct pwm_device - PWM channel object
69 * @label: name of the PWM device
70 * @flags: flags associated with the PWM device
71 * @hwpwm: per-chip relative index of the PWM device
72 * @chip: PWM chip providing this PWM device
73 * @args: PWM arguments
74 * @state: last applied state
75 * @last: last implemented state (for PWM_DEBUG)
81 struct pwm_chip *chip;
84 struct pwm_state state;
85 struct pwm_state last;
89 * pwm_get_state() - retrieve the current PWM state
91 * @state: state to fill with the current PWM state
93 * The returned PWM state represents the state that was applied by a previous call to
94 * pwm_apply_might_sleep(). Drivers may have to slightly tweak that state before programming it to
95 * hardware. If pwm_apply_might_sleep() was never called, this returns either the current hardware
96 * state (if supported) or the default settings.
98 static inline void pwm_get_state(const struct pwm_device *pwm,
99 struct pwm_state *state)
104 static inline bool pwm_is_enabled(const struct pwm_device *pwm)
106 struct pwm_state state;
108 pwm_get_state(pwm, &state);
110 return state.enabled;
113 static inline u64 pwm_get_period(const struct pwm_device *pwm)
115 struct pwm_state state;
117 pwm_get_state(pwm, &state);
122 static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm)
124 struct pwm_state state;
126 pwm_get_state(pwm, &state);
128 return state.duty_cycle;
131 static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
133 struct pwm_state state;
135 pwm_get_state(pwm, &state);
137 return state.polarity;
140 static inline void pwm_get_args(const struct pwm_device *pwm,
141 struct pwm_args *args)
147 * pwm_init_state() - prepare a new state to be applied with pwm_apply_might_sleep()
149 * @state: state to fill with the prepared PWM state
151 * This functions prepares a state that can later be tweaked and applied
152 * to the PWM device with pwm_apply_might_sleep(). This is a convenient function
153 * that first retrieves the current PWM state and the replaces the period
154 * and polarity fields with the reference values defined in pwm->args.
155 * Once the function returns, you can adjust the ->enabled and ->duty_cycle
156 * fields according to your needs before calling pwm_apply_might_sleep().
158 * ->duty_cycle is initially set to zero to avoid cases where the current
159 * ->duty_cycle value exceed the pwm_args->period one, which would trigger
160 * an error if the user calls pwm_apply_might_sleep() without adjusting ->duty_cycle
163 static inline void pwm_init_state(const struct pwm_device *pwm,
164 struct pwm_state *state)
166 struct pwm_args args;
168 /* First get the current state. */
169 pwm_get_state(pwm, state);
171 /* Then fill it with the reference config */
172 pwm_get_args(pwm, &args);
174 state->period = args.period;
175 state->polarity = args.polarity;
176 state->duty_cycle = 0;
177 state->usage_power = false;
181 * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
182 * @state: PWM state to extract the duty cycle from
183 * @scale: target scale of the relative duty cycle
185 * This functions converts the absolute duty cycle stored in @state (expressed
186 * in nanosecond) into a value relative to the period.
188 * For example if you want to get the duty_cycle expressed in percent, call:
190 * pwm_get_state(pwm, &state);
191 * duty = pwm_get_relative_duty_cycle(&state, 100);
193 static inline unsigned int
194 pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
199 return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale,
204 * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
205 * @state: PWM state to fill
206 * @duty_cycle: relative duty cycle value
207 * @scale: scale in which @duty_cycle is expressed
209 * This functions converts a relative into an absolute duty cycle (expressed
210 * in nanoseconds), and puts the result in state->duty_cycle.
212 * For example if you want to configure a 50% duty cycle, call:
214 * pwm_init_state(pwm, &state);
215 * pwm_set_relative_duty_cycle(&state, 50, 100);
216 * pwm_apply_might_sleep(pwm, &state);
218 * This functions returns -EINVAL if @duty_cycle and/or @scale are
219 * inconsistent (@scale == 0 or @duty_cycle > @scale).
222 pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
225 if (!scale || duty_cycle > scale)
228 state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
236 * struct pwm_capture - PWM capture data
237 * @period: period of the PWM signal (in nanoseconds)
238 * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
242 unsigned int duty_cycle;
246 * struct pwm_ops - PWM controller operations
247 * @request: optional hook for requesting a PWM
248 * @free: optional hook for freeing a PWM
249 * @capture: capture and report PWM signal
250 * @apply: atomically apply a new PWM config
251 * @get_state: get the current PWM state. This function is only
252 * called once per PWM device when the PWM chip is
256 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
257 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
258 int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
259 struct pwm_capture *result, unsigned long timeout);
260 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
261 const struct pwm_state *state);
262 int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
263 struct pwm_state *state);
267 * struct pwm_chip - abstract a PWM controller
268 * @dev: device providing the PWMs
269 * @ops: callbacks for this PWM controller
270 * @owner: module providing this chip
271 * @id: unique number of this PWM chip
272 * @npwm: number of PWMs controlled by this chip
273 * @of_xlate: request a PWM device given a device tree PWM specifier
274 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
275 * @atomic: can the driver's ->apply() be called in atomic context
276 * @pwms: array of PWM devices allocated by the framework
280 const struct pwm_ops *ops;
281 struct module *owner;
285 struct pwm_device * (*of_xlate)(struct pwm_chip *chip,
286 const struct of_phandle_args *args);
287 unsigned int of_pwm_n_cells;
290 /* only used internally by the PWM framework */
291 struct pwm_device *pwms;
294 #if IS_ENABLED(CONFIG_PWM)
296 int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state);
297 int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state);
298 int pwm_adjust_config(struct pwm_device *pwm);
301 * pwm_config() - change a PWM device configuration
303 * @duty_ns: "on" time (in nanoseconds)
304 * @period_ns: duration (in nanoseconds) of one cycle
306 * Returns: 0 on success or a negative error code on failure.
308 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
311 struct pwm_state state;
316 if (duty_ns < 0 || period_ns < 0)
319 pwm_get_state(pwm, &state);
320 if (state.duty_cycle == duty_ns && state.period == period_ns)
323 state.duty_cycle = duty_ns;
324 state.period = period_ns;
325 return pwm_apply_might_sleep(pwm, &state);
329 * pwm_enable() - start a PWM output toggling
332 * Returns: 0 on success or a negative error code on failure.
334 static inline int pwm_enable(struct pwm_device *pwm)
336 struct pwm_state state;
341 pwm_get_state(pwm, &state);
345 state.enabled = true;
346 return pwm_apply_might_sleep(pwm, &state);
350 * pwm_disable() - stop a PWM output toggling
353 static inline void pwm_disable(struct pwm_device *pwm)
355 struct pwm_state state;
360 pwm_get_state(pwm, &state);
364 state.enabled = false;
365 pwm_apply_might_sleep(pwm, &state);
369 * pwm_might_sleep() - is pwm_apply_atomic() supported?
372 * Returns: false if pwm_apply_atomic() can be called from atomic context.
374 static inline bool pwm_might_sleep(struct pwm_device *pwm)
376 return !pwm->chip->atomic;
379 /* PWM provider APIs */
380 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
381 unsigned long timeout);
383 int __pwmchip_add(struct pwm_chip *chip, struct module *owner);
384 #define pwmchip_add(chip) __pwmchip_add(chip, THIS_MODULE)
385 void pwmchip_remove(struct pwm_chip *chip);
387 int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner);
388 #define devm_pwmchip_add(dev, chip) __devm_pwmchip_add(dev, chip, THIS_MODULE)
390 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
394 struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *chip,
395 const struct of_phandle_args *args);
396 struct pwm_device *of_pwm_single_xlate(struct pwm_chip *chip,
397 const struct of_phandle_args *args);
399 struct pwm_device *pwm_get(struct device *dev, const char *con_id);
400 void pwm_put(struct pwm_device *pwm);
402 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
403 struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
404 struct fwnode_handle *fwnode,
407 static inline bool pwm_might_sleep(struct pwm_device *pwm)
412 static inline int pwm_apply_might_sleep(struct pwm_device *pwm,
413 const struct pwm_state *state)
419 static inline int pwm_apply_atomic(struct pwm_device *pwm,
420 const struct pwm_state *state)
425 static inline int pwm_adjust_config(struct pwm_device *pwm)
430 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
437 static inline int pwm_enable(struct pwm_device *pwm)
443 static inline void pwm_disable(struct pwm_device *pwm)
448 static inline int pwm_capture(struct pwm_device *pwm,
449 struct pwm_capture *result,
450 unsigned long timeout)
455 static inline int pwmchip_add(struct pwm_chip *chip)
460 static inline int pwmchip_remove(struct pwm_chip *chip)
465 static inline int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip)
470 static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
475 return ERR_PTR(-ENODEV);
478 static inline struct pwm_device *pwm_get(struct device *dev,
479 const char *consumer)
482 return ERR_PTR(-ENODEV);
485 static inline void pwm_put(struct pwm_device *pwm)
490 static inline struct pwm_device *devm_pwm_get(struct device *dev,
491 const char *consumer)
494 return ERR_PTR(-ENODEV);
497 static inline struct pwm_device *
498 devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode,
502 return ERR_PTR(-ENODEV);
506 static inline void pwm_apply_args(struct pwm_device *pwm)
508 struct pwm_state state = { };
511 * PWM users calling pwm_apply_args() expect to have a fresh config
512 * where the polarity and period are set according to pwm_args info.
513 * The problem is, polarity can only be changed when the PWM is
516 * PWM drivers supporting hardware readout may declare the PWM device
517 * as enabled, and prevent polarity setting, which changes from the
518 * existing behavior, where all PWM devices are declared as disabled
519 * at startup (even if they are actually enabled), thus authorizing
522 * To fulfill this requirement, we apply a new state which disables
523 * the PWM device and set the reference period and polarity config.
525 * Note that PWM users requiring a smooth handover between the
526 * bootloader and the kernel (like critical regulators controlled by
527 * PWM devices) will have to switch to the atomic API and avoid calling
531 state.enabled = false;
532 state.polarity = pwm->args.polarity;
533 state.period = pwm->args.period;
534 state.usage_power = false;
536 pwm_apply_might_sleep(pwm, &state);
539 /* only for backwards-compatibility, new code should not use this */
540 static inline int pwm_apply_state(struct pwm_device *pwm,
541 const struct pwm_state *state)
543 return pwm_apply_might_sleep(pwm, state);
547 struct list_head list;
548 const char *provider;
553 enum pwm_polarity polarity;
554 const char *module; /* optional, may be NULL */
557 #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \
558 _period, _polarity, _module) \
560 .provider = _provider, \
565 .polarity = _polarity, \
569 #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
570 PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
573 #if IS_ENABLED(CONFIG_PWM)
574 void pwm_add_table(struct pwm_lookup *table, size_t num);
575 void pwm_remove_table(struct pwm_lookup *table, size_t num);
577 static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
581 static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
586 #ifdef CONFIG_PWM_SYSFS
587 void pwmchip_sysfs_export(struct pwm_chip *chip);
588 void pwmchip_sysfs_unexport(struct pwm_chip *chip);
590 static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
594 static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
597 #endif /* CONFIG_PWM_SYSFS */
599 #endif /* __LINUX_PWM_H */