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 * @atomic: can the driver's ->apply() be called in atomic context
275 * @driver_data: Private pointer for driver specific info
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);
289 /* only used internally by the PWM framework */
291 struct pwm_device *pwms;
294 static inline struct device *pwmchip_parent(const struct pwm_chip *chip)
299 static inline void *pwmchip_get_drvdata(struct pwm_chip *chip)
302 * After pwm_chip got a dedicated struct device, this can be replaced by
303 * dev_get_drvdata(&chip->dev);
305 return chip->driver_data;
308 static inline void pwmchip_set_drvdata(struct pwm_chip *chip, void *data)
311 * After pwm_chip got a dedicated struct device, this can be replaced by
312 * dev_set_drvdata(&chip->dev, data);
314 chip->driver_data = data;
317 #if IS_ENABLED(CONFIG_PWM)
319 int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state);
320 int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state);
321 int pwm_adjust_config(struct pwm_device *pwm);
324 * pwm_config() - change a PWM device configuration
326 * @duty_ns: "on" time (in nanoseconds)
327 * @period_ns: duration (in nanoseconds) of one cycle
329 * Returns: 0 on success or a negative error code on failure.
331 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
334 struct pwm_state state;
339 if (duty_ns < 0 || period_ns < 0)
342 pwm_get_state(pwm, &state);
343 if (state.duty_cycle == duty_ns && state.period == period_ns)
346 state.duty_cycle = duty_ns;
347 state.period = period_ns;
348 return pwm_apply_might_sleep(pwm, &state);
352 * pwm_enable() - start a PWM output toggling
355 * Returns: 0 on success or a negative error code on failure.
357 static inline int pwm_enable(struct pwm_device *pwm)
359 struct pwm_state state;
364 pwm_get_state(pwm, &state);
368 state.enabled = true;
369 return pwm_apply_might_sleep(pwm, &state);
373 * pwm_disable() - stop a PWM output toggling
376 static inline void pwm_disable(struct pwm_device *pwm)
378 struct pwm_state state;
383 pwm_get_state(pwm, &state);
387 state.enabled = false;
388 pwm_apply_might_sleep(pwm, &state);
392 * pwm_might_sleep() - is pwm_apply_atomic() supported?
395 * Returns: false if pwm_apply_atomic() can be called from atomic context.
397 static inline bool pwm_might_sleep(struct pwm_device *pwm)
399 return !pwm->chip->atomic;
402 /* PWM provider APIs */
403 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
404 unsigned long timeout);
406 void pwmchip_put(struct pwm_chip *chip);
407 struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv);
408 struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv);
410 int __pwmchip_add(struct pwm_chip *chip, struct module *owner);
411 #define pwmchip_add(chip) __pwmchip_add(chip, THIS_MODULE)
412 void pwmchip_remove(struct pwm_chip *chip);
414 int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner);
415 #define devm_pwmchip_add(dev, chip) __devm_pwmchip_add(dev, chip, THIS_MODULE)
417 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
421 struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *chip,
422 const struct of_phandle_args *args);
423 struct pwm_device *of_pwm_single_xlate(struct pwm_chip *chip,
424 const struct of_phandle_args *args);
426 struct pwm_device *pwm_get(struct device *dev, const char *con_id);
427 void pwm_put(struct pwm_device *pwm);
429 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
430 struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
431 struct fwnode_handle *fwnode,
434 static inline bool pwm_might_sleep(struct pwm_device *pwm)
439 static inline int pwm_apply_might_sleep(struct pwm_device *pwm,
440 const struct pwm_state *state)
446 static inline int pwm_apply_atomic(struct pwm_device *pwm,
447 const struct pwm_state *state)
452 static inline int pwm_adjust_config(struct pwm_device *pwm)
457 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
464 static inline int pwm_enable(struct pwm_device *pwm)
470 static inline void pwm_disable(struct pwm_device *pwm)
475 static inline int pwm_capture(struct pwm_device *pwm,
476 struct pwm_capture *result,
477 unsigned long timeout)
482 static inline void pwmchip_put(struct pwm_chip *chip)
486 static inline struct pwm_chip *pwmchip_alloc(struct device *parent,
490 return ERR_PTR(-EINVAL);
493 static inline struct pwm_chip *devm_pwmchip_alloc(struct device *parent,
497 return pwmchip_alloc(parent, npwm, sizeof_priv);
500 static inline int pwmchip_add(struct pwm_chip *chip)
505 static inline int pwmchip_remove(struct pwm_chip *chip)
510 static inline int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip)
515 static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
520 return ERR_PTR(-ENODEV);
523 static inline struct pwm_device *pwm_get(struct device *dev,
524 const char *consumer)
527 return ERR_PTR(-ENODEV);
530 static inline void pwm_put(struct pwm_device *pwm)
535 static inline struct pwm_device *devm_pwm_get(struct device *dev,
536 const char *consumer)
539 return ERR_PTR(-ENODEV);
542 static inline struct pwm_device *
543 devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode,
547 return ERR_PTR(-ENODEV);
551 static inline void pwm_apply_args(struct pwm_device *pwm)
553 struct pwm_state state = { };
556 * PWM users calling pwm_apply_args() expect to have a fresh config
557 * where the polarity and period are set according to pwm_args info.
558 * The problem is, polarity can only be changed when the PWM is
561 * PWM drivers supporting hardware readout may declare the PWM device
562 * as enabled, and prevent polarity setting, which changes from the
563 * existing behavior, where all PWM devices are declared as disabled
564 * at startup (even if they are actually enabled), thus authorizing
567 * To fulfill this requirement, we apply a new state which disables
568 * the PWM device and set the reference period and polarity config.
570 * Note that PWM users requiring a smooth handover between the
571 * bootloader and the kernel (like critical regulators controlled by
572 * PWM devices) will have to switch to the atomic API and avoid calling
576 state.enabled = false;
577 state.polarity = pwm->args.polarity;
578 state.period = pwm->args.period;
579 state.usage_power = false;
581 pwm_apply_might_sleep(pwm, &state);
584 /* only for backwards-compatibility, new code should not use this */
585 static inline int pwm_apply_state(struct pwm_device *pwm,
586 const struct pwm_state *state)
588 return pwm_apply_might_sleep(pwm, state);
592 struct list_head list;
593 const char *provider;
598 enum pwm_polarity polarity;
599 const char *module; /* optional, may be NULL */
602 #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \
603 _period, _polarity, _module) \
605 .provider = _provider, \
610 .polarity = _polarity, \
614 #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
615 PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
618 #if IS_ENABLED(CONFIG_PWM)
619 void pwm_add_table(struct pwm_lookup *table, size_t num);
620 void pwm_remove_table(struct pwm_lookup *table, size_t num);
622 static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
626 static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
631 #ifdef CONFIG_PWM_SYSFS
632 void pwmchip_sysfs_export(struct pwm_chip *chip);
633 void pwmchip_sysfs_unexport(struct pwm_chip *chip);
635 static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
639 static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
642 #endif /* CONFIG_PWM_SYSFS */
644 #endif /* __LINUX_PWM_H */