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 * @pwm: global index of the PWM device
73 * @chip: PWM chip providing this PWM device
74 * @args: PWM arguments
75 * @state: last applied state
76 * @last: last implemented state (for PWM_DEBUG)
83 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 * The returned PWM state represents the state that was applied by a previous call to
96 * pwm_apply_state(). Drivers may have to slightly tweak that state before programming it to
97 * hardware. If pwm_apply_state() was never called, this returns either the current hardware
98 * state (if supported) or the default settings.
100 static inline void pwm_get_state(const struct pwm_device *pwm,
101 struct pwm_state *state)
106 static inline bool pwm_is_enabled(const struct pwm_device *pwm)
108 struct pwm_state state;
110 pwm_get_state(pwm, &state);
112 return state.enabled;
115 static inline void pwm_set_period(struct pwm_device *pwm, u64 period)
118 pwm->state.period = period;
121 static inline u64 pwm_get_period(const struct pwm_device *pwm)
123 struct pwm_state state;
125 pwm_get_state(pwm, &state);
130 static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
133 pwm->state.duty_cycle = duty;
136 static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm)
138 struct pwm_state state;
140 pwm_get_state(pwm, &state);
142 return state.duty_cycle;
145 static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
147 struct pwm_state state;
149 pwm_get_state(pwm, &state);
151 return state.polarity;
154 static inline void pwm_get_args(const struct pwm_device *pwm,
155 struct pwm_args *args)
161 * pwm_init_state() - prepare a new state to be applied with pwm_apply_state()
163 * @state: state to fill with the prepared PWM state
165 * This functions prepares a state that can later be tweaked and applied
166 * to the PWM device with pwm_apply_state(). This is a convenient function
167 * that first retrieves the current PWM state and the replaces the period
168 * and polarity fields with the reference values defined in pwm->args.
169 * Once the function returns, you can adjust the ->enabled and ->duty_cycle
170 * fields according to your needs before calling pwm_apply_state().
172 * ->duty_cycle is initially set to zero to avoid cases where the current
173 * ->duty_cycle value exceed the pwm_args->period one, which would trigger
174 * an error if the user calls pwm_apply_state() without adjusting ->duty_cycle
177 static inline void pwm_init_state(const struct pwm_device *pwm,
178 struct pwm_state *state)
180 struct pwm_args args;
182 /* First get the current state. */
183 pwm_get_state(pwm, state);
185 /* Then fill it with the reference config */
186 pwm_get_args(pwm, &args);
188 state->period = args.period;
189 state->polarity = args.polarity;
190 state->duty_cycle = 0;
191 state->usage_power = false;
195 * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
196 * @state: PWM state to extract the duty cycle from
197 * @scale: target scale of the relative duty cycle
199 * This functions converts the absolute duty cycle stored in @state (expressed
200 * in nanosecond) into a value relative to the period.
202 * For example if you want to get the duty_cycle expressed in percent, call:
204 * pwm_get_state(pwm, &state);
205 * duty = pwm_get_relative_duty_cycle(&state, 100);
207 static inline unsigned int
208 pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
213 return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale,
218 * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
219 * @state: PWM state to fill
220 * @duty_cycle: relative duty cycle value
221 * @scale: scale in which @duty_cycle is expressed
223 * This functions converts a relative into an absolute duty cycle (expressed
224 * in nanoseconds), and puts the result in state->duty_cycle.
226 * For example if you want to configure a 50% duty cycle, call:
228 * pwm_init_state(pwm, &state);
229 * pwm_set_relative_duty_cycle(&state, 50, 100);
230 * pwm_apply_state(pwm, &state);
232 * This functions returns -EINVAL if @duty_cycle and/or @scale are
233 * inconsistent (@scale == 0 or @duty_cycle > @scale).
236 pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
239 if (!scale || duty_cycle > scale)
242 state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
250 * struct pwm_capture - PWM capture data
251 * @period: period of the PWM signal (in nanoseconds)
252 * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
256 unsigned int duty_cycle;
260 * struct pwm_ops - PWM controller operations
261 * @request: optional hook for requesting a PWM
262 * @free: optional hook for freeing a PWM
263 * @capture: capture and report PWM signal
264 * @apply: atomically apply a new PWM config
265 * @get_state: get the current PWM state. This function is only
266 * called once per PWM device when the PWM chip is
270 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
271 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
272 int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
273 struct pwm_capture *result, unsigned long timeout);
274 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
275 const struct pwm_state *state);
276 int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
277 struct pwm_state *state);
281 * struct pwm_chip - abstract a PWM controller
282 * @dev: device providing the PWMs
283 * @ops: callbacks for this PWM controller
284 * @owner: module providing this chip
285 * @base: number of first PWM controlled by this chip
286 * @npwm: number of PWMs controlled by this chip
287 * @of_xlate: request a PWM device given a device tree PWM specifier
288 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
289 * @list: list node for internal use
290 * @pwms: array of PWM devices allocated by the framework
294 const struct pwm_ops *ops;
295 struct module *owner;
299 struct pwm_device * (*of_xlate)(struct pwm_chip *chip,
300 const struct of_phandle_args *args);
301 unsigned int of_pwm_n_cells;
303 /* only used internally by the PWM framework */
304 struct list_head list;
305 struct pwm_device *pwms;
308 #if IS_ENABLED(CONFIG_PWM)
310 int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state);
311 int pwm_adjust_config(struct pwm_device *pwm);
314 * pwm_config() - change a PWM device configuration
316 * @duty_ns: "on" time (in nanoseconds)
317 * @period_ns: duration (in nanoseconds) of one cycle
319 * Returns: 0 on success or a negative error code on failure.
321 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
324 struct pwm_state state;
329 if (duty_ns < 0 || period_ns < 0)
332 pwm_get_state(pwm, &state);
333 if (state.duty_cycle == duty_ns && state.period == period_ns)
336 state.duty_cycle = duty_ns;
337 state.period = period_ns;
338 return pwm_apply_state(pwm, &state);
342 * pwm_enable() - start a PWM output toggling
345 * Returns: 0 on success or a negative error code on failure.
347 static inline int pwm_enable(struct pwm_device *pwm)
349 struct pwm_state state;
354 pwm_get_state(pwm, &state);
358 state.enabled = true;
359 return pwm_apply_state(pwm, &state);
363 * pwm_disable() - stop a PWM output toggling
366 static inline void pwm_disable(struct pwm_device *pwm)
368 struct pwm_state state;
373 pwm_get_state(pwm, &state);
377 state.enabled = false;
378 pwm_apply_state(pwm, &state);
381 /* PWM provider APIs */
382 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
383 unsigned long timeout);
385 int __pwmchip_add(struct pwm_chip *chip, struct module *owner);
386 #define pwmchip_add(chip) __pwmchip_add(chip, THIS_MODULE)
387 void pwmchip_remove(struct pwm_chip *chip);
389 int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner);
390 #define devm_pwmchip_add(dev, chip) __devm_pwmchip_add(dev, chip, THIS_MODULE)
392 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
396 struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *chip,
397 const struct of_phandle_args *args);
398 struct pwm_device *of_pwm_single_xlate(struct pwm_chip *chip,
399 const struct of_phandle_args *args);
401 struct pwm_device *pwm_get(struct device *dev, const char *con_id);
402 void pwm_put(struct pwm_device *pwm);
404 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
405 struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
406 struct fwnode_handle *fwnode,
409 static inline int pwm_apply_state(struct pwm_device *pwm,
410 const struct pwm_state *state)
416 static inline int pwm_adjust_config(struct pwm_device *pwm)
421 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
428 static inline int pwm_enable(struct pwm_device *pwm)
434 static inline void pwm_disable(struct pwm_device *pwm)
439 static inline int pwm_capture(struct pwm_device *pwm,
440 struct pwm_capture *result,
441 unsigned long timeout)
446 static inline int pwmchip_add(struct pwm_chip *chip)
451 static inline int pwmchip_remove(struct pwm_chip *chip)
456 static inline int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip)
461 static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
466 return ERR_PTR(-ENODEV);
469 static inline struct pwm_device *pwm_get(struct device *dev,
470 const char *consumer)
473 return ERR_PTR(-ENODEV);
476 static inline void pwm_put(struct pwm_device *pwm)
481 static inline struct pwm_device *devm_pwm_get(struct device *dev,
482 const char *consumer)
485 return ERR_PTR(-ENODEV);
488 static inline struct pwm_device *
489 devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode,
493 return ERR_PTR(-ENODEV);
497 static inline void pwm_apply_args(struct pwm_device *pwm)
499 struct pwm_state state = { };
502 * PWM users calling pwm_apply_args() expect to have a fresh config
503 * where the polarity and period are set according to pwm_args info.
504 * The problem is, polarity can only be changed when the PWM is
507 * PWM drivers supporting hardware readout may declare the PWM device
508 * as enabled, and prevent polarity setting, which changes from the
509 * existing behavior, where all PWM devices are declared as disabled
510 * at startup (even if they are actually enabled), thus authorizing
513 * To fulfill this requirement, we apply a new state which disables
514 * the PWM device and set the reference period and polarity config.
516 * Note that PWM users requiring a smooth handover between the
517 * bootloader and the kernel (like critical regulators controlled by
518 * PWM devices) will have to switch to the atomic API and avoid calling
522 state.enabled = false;
523 state.polarity = pwm->args.polarity;
524 state.period = pwm->args.period;
525 state.usage_power = false;
527 pwm_apply_state(pwm, &state);
531 struct list_head list;
532 const char *provider;
537 enum pwm_polarity polarity;
538 const char *module; /* optional, may be NULL */
541 #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \
542 _period, _polarity, _module) \
544 .provider = _provider, \
549 .polarity = _polarity, \
553 #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
554 PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
557 #if IS_ENABLED(CONFIG_PWM)
558 void pwm_add_table(struct pwm_lookup *table, size_t num);
559 void pwm_remove_table(struct pwm_lookup *table, size_t num);
561 static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
565 static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
570 #ifdef CONFIG_PWM_SYSFS
571 void pwmchip_sysfs_export(struct pwm_chip *chip);
572 void pwmchip_sysfs_unexport(struct pwm_chip *chip);
574 static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
578 static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
581 #endif /* CONFIG_PWM_SYSFS */
583 #endif /* __LINUX_PWM_H */