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_state - state of a PWM channel
54 * @period: PWM period (in nanoseconds)
55 * @duty_cycle: PWM duty cycle (in nanoseconds)
56 * @polarity: PWM polarity
57 * @enabled: PWM enabled status
58 * @usage_power: If set, the PWM driver is only required to maintain the power
59 * output but has more freedom regarding signal form.
60 * If supported, the signal can be optimized, for example to
61 * improve EMI by phase shifting individual channels.
66 enum pwm_polarity polarity;
72 * struct pwm_device - PWM channel object
73 * @label: name of the PWM device
74 * @flags: flags associated with the PWM device
75 * @hwpwm: per-chip relative index of the PWM device
76 * @chip: PWM chip providing this PWM device
77 * @args: PWM arguments
78 * @state: last applied state
79 * @last: last implemented state (for PWM_DEBUG)
85 struct pwm_chip *chip;
88 struct pwm_state state;
89 struct pwm_state last;
93 * pwm_get_state() - retrieve the current PWM state
95 * @state: state to fill with the current PWM state
97 * The returned PWM state represents the state that was applied by a previous call to
98 * pwm_apply_might_sleep(). Drivers may have to slightly tweak that state before programming it to
99 * hardware. If pwm_apply_might_sleep() was never called, this returns either the current hardware
100 * state (if supported) or the default settings.
102 static inline void pwm_get_state(const struct pwm_device *pwm,
103 struct pwm_state *state)
108 static inline bool pwm_is_enabled(const struct pwm_device *pwm)
110 struct pwm_state state;
112 pwm_get_state(pwm, &state);
114 return state.enabled;
117 static inline u64 pwm_get_period(const struct pwm_device *pwm)
119 struct pwm_state state;
121 pwm_get_state(pwm, &state);
126 static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm)
128 struct pwm_state state;
130 pwm_get_state(pwm, &state);
132 return state.duty_cycle;
135 static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
137 struct pwm_state state;
139 pwm_get_state(pwm, &state);
141 return state.polarity;
144 static inline void pwm_get_args(const struct pwm_device *pwm,
145 struct pwm_args *args)
151 * pwm_init_state() - prepare a new state to be applied with pwm_apply_might_sleep()
153 * @state: state to fill with the prepared PWM state
155 * This functions prepares a state that can later be tweaked and applied
156 * to the PWM device with pwm_apply_might_sleep(). This is a convenient function
157 * that first retrieves the current PWM state and the replaces the period
158 * and polarity fields with the reference values defined in pwm->args.
159 * Once the function returns, you can adjust the ->enabled and ->duty_cycle
160 * fields according to your needs before calling pwm_apply_might_sleep().
162 * ->duty_cycle is initially set to zero to avoid cases where the current
163 * ->duty_cycle value exceed the pwm_args->period one, which would trigger
164 * an error if the user calls pwm_apply_might_sleep() without adjusting ->duty_cycle
167 static inline void pwm_init_state(const struct pwm_device *pwm,
168 struct pwm_state *state)
170 struct pwm_args args;
172 /* First get the current state. */
173 pwm_get_state(pwm, state);
175 /* Then fill it with the reference config */
176 pwm_get_args(pwm, &args);
178 state->period = args.period;
179 state->polarity = args.polarity;
180 state->duty_cycle = 0;
181 state->usage_power = false;
185 * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
186 * @state: PWM state to extract the duty cycle from
187 * @scale: target scale of the relative duty cycle
189 * This functions converts the absolute duty cycle stored in @state (expressed
190 * in nanosecond) into a value relative to the period.
192 * For example if you want to get the duty_cycle expressed in percent, call:
194 * pwm_get_state(pwm, &state);
195 * duty = pwm_get_relative_duty_cycle(&state, 100);
197 static inline unsigned int
198 pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
203 return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale,
208 * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
209 * @state: PWM state to fill
210 * @duty_cycle: relative duty cycle value
211 * @scale: scale in which @duty_cycle is expressed
213 * This functions converts a relative into an absolute duty cycle (expressed
214 * in nanoseconds), and puts the result in state->duty_cycle.
216 * For example if you want to configure a 50% duty cycle, call:
218 * pwm_init_state(pwm, &state);
219 * pwm_set_relative_duty_cycle(&state, 50, 100);
220 * pwm_apply_might_sleep(pwm, &state);
222 * This functions returns -EINVAL if @duty_cycle and/or @scale are
223 * inconsistent (@scale == 0 or @duty_cycle > @scale).
226 pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
229 if (!scale || duty_cycle > scale)
232 state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
240 * struct pwm_capture - PWM capture data
241 * @period: period of the PWM signal (in nanoseconds)
242 * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
246 unsigned int duty_cycle;
250 * struct pwm_ops - PWM controller operations
251 * @request: optional hook for requesting a PWM
252 * @free: optional hook for freeing a PWM
253 * @capture: capture and report PWM signal
254 * @apply: atomically apply a new PWM config
255 * @get_state: get the current PWM state.
258 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
259 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
260 int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
261 struct pwm_capture *result, unsigned long timeout);
262 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
263 const struct pwm_state *state);
264 int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
265 struct pwm_state *state);
269 * struct pwm_chip - abstract a PWM controller
270 * @dev: device providing the PWMs
271 * @ops: callbacks for this PWM controller
272 * @owner: module providing this chip
273 * @id: unique number of this PWM chip
274 * @npwm: number of PWMs controlled by this chip
275 * @of_xlate: request a PWM device given a device tree PWM specifier
276 * @atomic: can the driver's ->apply() be called in atomic context
277 * @uses_pwmchip_alloc: signals if pwmchip_allow was used to allocate this chip
278 * @pwms: array of PWM devices allocated by the framework
282 const struct pwm_ops *ops;
283 struct module *owner;
287 struct pwm_device * (*of_xlate)(struct pwm_chip *chip,
288 const struct of_phandle_args *args);
291 /* only used internally by the PWM framework */
292 bool uses_pwmchip_alloc;
293 struct pwm_device pwms[] __counted_by(npwm);
296 static inline struct device *pwmchip_parent(const struct pwm_chip *chip)
298 return chip->dev.parent;
301 static inline void *pwmchip_get_drvdata(struct pwm_chip *chip)
303 return dev_get_drvdata(&chip->dev);
306 static inline void pwmchip_set_drvdata(struct pwm_chip *chip, void *data)
308 dev_set_drvdata(&chip->dev, data);
311 #if IS_ENABLED(CONFIG_PWM)
313 int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state);
314 int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state);
315 int pwm_adjust_config(struct pwm_device *pwm);
318 * pwm_config() - change a PWM device configuration
320 * @duty_ns: "on" time (in nanoseconds)
321 * @period_ns: duration (in nanoseconds) of one cycle
323 * Returns: 0 on success or a negative error code on failure.
325 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
328 struct pwm_state state;
333 if (duty_ns < 0 || period_ns < 0)
336 pwm_get_state(pwm, &state);
337 if (state.duty_cycle == duty_ns && state.period == period_ns)
340 state.duty_cycle = duty_ns;
341 state.period = period_ns;
342 return pwm_apply_might_sleep(pwm, &state);
346 * pwm_enable() - start a PWM output toggling
349 * Returns: 0 on success or a negative error code on failure.
351 static inline int pwm_enable(struct pwm_device *pwm)
353 struct pwm_state state;
358 pwm_get_state(pwm, &state);
362 state.enabled = true;
363 return pwm_apply_might_sleep(pwm, &state);
367 * pwm_disable() - stop a PWM output toggling
370 static inline void pwm_disable(struct pwm_device *pwm)
372 struct pwm_state state;
377 pwm_get_state(pwm, &state);
381 state.enabled = false;
382 pwm_apply_might_sleep(pwm, &state);
386 * pwm_might_sleep() - is pwm_apply_atomic() supported?
389 * Returns: false if pwm_apply_atomic() can be called from atomic context.
391 static inline bool pwm_might_sleep(struct pwm_device *pwm)
393 return !pwm->chip->atomic;
396 /* PWM provider APIs */
397 void pwmchip_put(struct pwm_chip *chip);
398 struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv);
399 struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv);
401 int __pwmchip_add(struct pwm_chip *chip, struct module *owner);
402 #define pwmchip_add(chip) __pwmchip_add(chip, THIS_MODULE)
403 void pwmchip_remove(struct pwm_chip *chip);
405 int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner);
406 #define devm_pwmchip_add(dev, chip) __devm_pwmchip_add(dev, chip, THIS_MODULE)
408 struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *chip,
409 const struct of_phandle_args *args);
410 struct pwm_device *of_pwm_single_xlate(struct pwm_chip *chip,
411 const struct of_phandle_args *args);
413 struct pwm_device *pwm_get(struct device *dev, const char *con_id);
414 void pwm_put(struct pwm_device *pwm);
416 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
417 struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
418 struct fwnode_handle *fwnode,
421 static inline bool pwm_might_sleep(struct pwm_device *pwm)
426 static inline int pwm_apply_might_sleep(struct pwm_device *pwm,
427 const struct pwm_state *state)
433 static inline int pwm_apply_atomic(struct pwm_device *pwm,
434 const struct pwm_state *state)
439 static inline int pwm_adjust_config(struct pwm_device *pwm)
444 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
451 static inline int pwm_enable(struct pwm_device *pwm)
457 static inline void pwm_disable(struct pwm_device *pwm)
462 static inline void pwmchip_put(struct pwm_chip *chip)
466 static inline struct pwm_chip *pwmchip_alloc(struct device *parent,
470 return ERR_PTR(-EINVAL);
473 static inline struct pwm_chip *devm_pwmchip_alloc(struct device *parent,
477 return pwmchip_alloc(parent, npwm, sizeof_priv);
480 static inline int pwmchip_add(struct pwm_chip *chip)
485 static inline int pwmchip_remove(struct pwm_chip *chip)
490 static inline int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip)
495 static inline struct pwm_device *pwm_get(struct device *dev,
496 const char *consumer)
499 return ERR_PTR(-ENODEV);
502 static inline void pwm_put(struct pwm_device *pwm)
507 static inline struct pwm_device *devm_pwm_get(struct device *dev,
508 const char *consumer)
511 return ERR_PTR(-ENODEV);
514 static inline struct pwm_device *
515 devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode,
519 return ERR_PTR(-ENODEV);
523 static inline void pwm_apply_args(struct pwm_device *pwm)
525 struct pwm_state state = { };
528 * PWM users calling pwm_apply_args() expect to have a fresh config
529 * where the polarity and period are set according to pwm_args info.
530 * The problem is, polarity can only be changed when the PWM is
533 * PWM drivers supporting hardware readout may declare the PWM device
534 * as enabled, and prevent polarity setting, which changes from the
535 * existing behavior, where all PWM devices are declared as disabled
536 * at startup (even if they are actually enabled), thus authorizing
539 * To fulfill this requirement, we apply a new state which disables
540 * the PWM device and set the reference period and polarity config.
542 * Note that PWM users requiring a smooth handover between the
543 * bootloader and the kernel (like critical regulators controlled by
544 * PWM devices) will have to switch to the atomic API and avoid calling
548 state.enabled = false;
549 state.polarity = pwm->args.polarity;
550 state.period = pwm->args.period;
551 state.usage_power = false;
553 pwm_apply_might_sleep(pwm, &state);
557 struct list_head list;
558 const char *provider;
563 enum pwm_polarity polarity;
564 const char *module; /* optional, may be NULL */
567 #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \
568 _period, _polarity, _module) \
570 .provider = _provider, \
575 .polarity = _polarity, \
579 #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
580 PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
583 #if IS_ENABLED(CONFIG_PWM)
584 void pwm_add_table(struct pwm_lookup *table, size_t num);
585 void pwm_remove_table(struct pwm_lookup *table, size_t num);
587 static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
591 static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
596 #endif /* __LINUX_PWM_H */