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;
44 PWMF_REQUESTED = 1 << 0,
45 PWMF_EXPORTED = 1 << 1,
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 * @chip_data: chip-private data associated with the PWM device
75 * @args: PWM arguments
76 * @state: last applied state
77 * @last: last implemented state (for PWM_DEBUG)
84 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_state(). Drivers may have to slightly tweak that state before programming it to
99 * hardware. If pwm_apply_state() 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 void pwm_set_period(struct pwm_device *pwm, u64 period)
120 pwm->state.period = period;
123 static inline u64 pwm_get_period(const struct pwm_device *pwm)
125 struct pwm_state state;
127 pwm_get_state(pwm, &state);
132 static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
135 pwm->state.duty_cycle = duty;
138 static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm)
140 struct pwm_state state;
142 pwm_get_state(pwm, &state);
144 return state.duty_cycle;
147 static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
149 struct pwm_state state;
151 pwm_get_state(pwm, &state);
153 return state.polarity;
156 static inline void pwm_get_args(const struct pwm_device *pwm,
157 struct pwm_args *args)
163 * pwm_init_state() - prepare a new state to be applied with pwm_apply_state()
165 * @state: state to fill with the prepared PWM state
167 * This functions prepares a state that can later be tweaked and applied
168 * to the PWM device with pwm_apply_state(). This is a convenient function
169 * that first retrieves the current PWM state and the replaces the period
170 * and polarity fields with the reference values defined in pwm->args.
171 * Once the function returns, you can adjust the ->enabled and ->duty_cycle
172 * fields according to your needs before calling pwm_apply_state().
174 * ->duty_cycle is initially set to zero to avoid cases where the current
175 * ->duty_cycle value exceed the pwm_args->period one, which would trigger
176 * an error if the user calls pwm_apply_state() without adjusting ->duty_cycle
179 static inline void pwm_init_state(const struct pwm_device *pwm,
180 struct pwm_state *state)
182 struct pwm_args args;
184 /* First get the current state. */
185 pwm_get_state(pwm, state);
187 /* Then fill it with the reference config */
188 pwm_get_args(pwm, &args);
190 state->period = args.period;
191 state->polarity = args.polarity;
192 state->duty_cycle = 0;
193 state->usage_power = false;
197 * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
198 * @state: PWM state to extract the duty cycle from
199 * @scale: target scale of the relative duty cycle
201 * This functions converts the absolute duty cycle stored in @state (expressed
202 * in nanosecond) into a value relative to the period.
204 * For example if you want to get the duty_cycle expressed in percent, call:
206 * pwm_get_state(pwm, &state);
207 * duty = pwm_get_relative_duty_cycle(&state, 100);
209 static inline unsigned int
210 pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
215 return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale,
220 * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
221 * @state: PWM state to fill
222 * @duty_cycle: relative duty cycle value
223 * @scale: scale in which @duty_cycle is expressed
225 * This functions converts a relative into an absolute duty cycle (expressed
226 * in nanoseconds), and puts the result in state->duty_cycle.
228 * For example if you want to configure a 50% duty cycle, call:
230 * pwm_init_state(pwm, &state);
231 * pwm_set_relative_duty_cycle(&state, 50, 100);
232 * pwm_apply_state(pwm, &state);
234 * This functions returns -EINVAL if @duty_cycle and/or @scale are
235 * inconsistent (@scale == 0 or @duty_cycle > @scale).
238 pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
241 if (!scale || duty_cycle > scale)
244 state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
252 * struct pwm_capture - PWM capture data
253 * @period: period of the PWM signal (in nanoseconds)
254 * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
258 unsigned int duty_cycle;
262 * struct pwm_ops - PWM controller operations
263 * @request: optional hook for requesting a PWM
264 * @free: optional hook for freeing a PWM
265 * @capture: capture and report PWM signal
266 * @apply: atomically apply a new PWM config
267 * @get_state: get the current PWM state. This function is only
268 * called once per PWM device when the PWM chip is
270 * @owner: helps prevent removal of modules exporting active PWMs
273 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
274 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
275 int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
276 struct pwm_capture *result, unsigned long timeout);
277 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
278 const struct pwm_state *state);
279 int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
280 struct pwm_state *state);
281 struct module *owner;
285 * struct pwm_chip - abstract a PWM controller
286 * @dev: device providing the PWMs
287 * @ops: callbacks for this PWM controller
288 * @base: number of first PWM controlled by this chip
289 * @npwm: number of PWMs controlled by this chip
290 * @of_xlate: request a PWM device given a device tree PWM specifier
291 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
292 * @list: list node for internal use
293 * @pwms: array of PWM devices allocated by the framework
297 const struct pwm_ops *ops;
301 struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
302 const struct of_phandle_args *args);
303 unsigned int of_pwm_n_cells;
305 /* only used internally by the PWM framework */
306 struct list_head list;
307 struct pwm_device *pwms;
310 #if IS_ENABLED(CONFIG_PWM)
312 int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state);
313 int pwm_adjust_config(struct pwm_device *pwm);
316 * pwm_config() - change a PWM device configuration
318 * @duty_ns: "on" time (in nanoseconds)
319 * @period_ns: duration (in nanoseconds) of one cycle
321 * Returns: 0 on success or a negative error code on failure.
323 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
326 struct pwm_state state;
331 if (duty_ns < 0 || period_ns < 0)
334 pwm_get_state(pwm, &state);
335 if (state.duty_cycle == duty_ns && state.period == period_ns)
338 state.duty_cycle = duty_ns;
339 state.period = period_ns;
340 return pwm_apply_state(pwm, &state);
344 * pwm_enable() - start a PWM output toggling
347 * Returns: 0 on success or a negative error code on failure.
349 static inline int pwm_enable(struct pwm_device *pwm)
351 struct pwm_state state;
356 pwm_get_state(pwm, &state);
360 state.enabled = true;
361 return pwm_apply_state(pwm, &state);
365 * pwm_disable() - stop a PWM output toggling
368 static inline void pwm_disable(struct pwm_device *pwm)
370 struct pwm_state state;
375 pwm_get_state(pwm, &state);
379 state.enabled = false;
380 pwm_apply_state(pwm, &state);
383 /* PWM provider APIs */
384 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
385 unsigned long timeout);
386 int pwm_set_chip_data(struct pwm_device *pwm, void *data);
387 void *pwm_get_chip_data(struct pwm_device *pwm);
389 int pwmchip_add(struct pwm_chip *chip);
390 void pwmchip_remove(struct pwm_chip *chip);
392 int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip);
394 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
398 struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
399 const struct of_phandle_args *args);
400 struct pwm_device *of_pwm_single_xlate(struct pwm_chip *pc,
401 const struct of_phandle_args *args);
403 struct pwm_device *pwm_get(struct device *dev, const char *con_id);
404 void pwm_put(struct pwm_device *pwm);
406 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
407 struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
408 struct fwnode_handle *fwnode,
411 static inline int pwm_apply_state(struct pwm_device *pwm,
412 const struct pwm_state *state)
418 static inline int pwm_adjust_config(struct pwm_device *pwm)
423 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
430 static inline int pwm_enable(struct pwm_device *pwm)
436 static inline void pwm_disable(struct pwm_device *pwm)
441 static inline int pwm_capture(struct pwm_device *pwm,
442 struct pwm_capture *result,
443 unsigned long timeout)
448 static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
453 static inline void *pwm_get_chip_data(struct pwm_device *pwm)
458 static inline int pwmchip_add(struct pwm_chip *chip)
463 static inline int pwmchip_remove(struct pwm_chip *chip)
468 static inline int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip)
473 static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
478 return ERR_PTR(-ENODEV);
481 static inline struct pwm_device *pwm_get(struct device *dev,
482 const char *consumer)
485 return ERR_PTR(-ENODEV);
488 static inline void pwm_put(struct pwm_device *pwm)
493 static inline struct pwm_device *devm_pwm_get(struct device *dev,
494 const char *consumer)
497 return ERR_PTR(-ENODEV);
500 static inline struct pwm_device *
501 devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode,
505 return ERR_PTR(-ENODEV);
509 static inline void pwm_apply_args(struct pwm_device *pwm)
511 struct pwm_state state = { };
514 * PWM users calling pwm_apply_args() expect to have a fresh config
515 * where the polarity and period are set according to pwm_args info.
516 * The problem is, polarity can only be changed when the PWM is
519 * PWM drivers supporting hardware readout may declare the PWM device
520 * as enabled, and prevent polarity setting, which changes from the
521 * existing behavior, where all PWM devices are declared as disabled
522 * at startup (even if they are actually enabled), thus authorizing
525 * To fulfill this requirement, we apply a new state which disables
526 * the PWM device and set the reference period and polarity config.
528 * Note that PWM users requiring a smooth handover between the
529 * bootloader and the kernel (like critical regulators controlled by
530 * PWM devices) will have to switch to the atomic API and avoid calling
534 state.enabled = false;
535 state.polarity = pwm->args.polarity;
536 state.period = pwm->args.period;
537 state.usage_power = false;
539 pwm_apply_state(pwm, &state);
543 struct list_head list;
544 const char *provider;
549 enum pwm_polarity polarity;
550 const char *module; /* optional, may be NULL */
553 #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \
554 _period, _polarity, _module) \
556 .provider = _provider, \
561 .polarity = _polarity, \
565 #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
566 PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
569 #if IS_ENABLED(CONFIG_PWM)
570 void pwm_add_table(struct pwm_lookup *table, size_t num);
571 void pwm_remove_table(struct pwm_lookup *table, size_t num);
573 static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
577 static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
582 #ifdef CONFIG_PWM_SYSFS
583 void pwmchip_sysfs_export(struct pwm_chip *chip);
584 void pwmchip_sysfs_unexport(struct pwm_chip *chip);
586 static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
590 static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
593 #endif /* CONFIG_PWM_SYSFS */
595 #endif /* __LINUX_PWM_H */