1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/device.h>
7 #include <linux/mutex.h>
13 * enum pwm_polarity - polarity of a PWM signal
14 * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
15 * cycle, followed by a low signal for the remainder of the pulse
17 * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
18 * cycle, followed by a high signal for the remainder of the pulse
23 PWM_POLARITY_INVERSED,
27 * struct pwm_args - board-dependent PWM arguments
28 * @period: reference period
29 * @polarity: reference polarity
31 * This structure describes board-dependent arguments attached to a PWM
32 * device. These arguments are usually retrieved from the PWM lookup table or
35 * Do not confuse this with the PWM state: PWM arguments represent the initial
36 * configuration that users want to use on this PWM device rather than the
37 * current PWM hardware state.
41 enum pwm_polarity polarity;
50 * struct pwm_state - state of a PWM channel
51 * @period: PWM period (in nanoseconds)
52 * @duty_cycle: PWM duty cycle (in nanoseconds)
53 * @polarity: PWM polarity
54 * @enabled: PWM enabled status
55 * @usage_power: If set, the PWM driver is only required to maintain the power
56 * output but has more freedom regarding signal form.
57 * If supported, the signal can be optimized, for example to
58 * improve EMI by phase shifting individual channels.
63 enum pwm_polarity polarity;
69 * struct pwm_device - PWM channel object
70 * @label: name of the PWM device
71 * @flags: flags associated with the PWM device
72 * @hwpwm: per-chip relative 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)
82 struct pwm_chip *chip;
85 struct pwm_state state;
86 struct pwm_state last;
90 * pwm_get_state() - retrieve the current PWM state
92 * @state: state to fill with the current PWM state
94 * The returned PWM state represents the state that was applied by a previous call to
95 * pwm_apply_might_sleep(). Drivers may have to slightly tweak that state before programming it to
96 * hardware. If pwm_apply_might_sleep() was never called, this returns either the current hardware
97 * state (if supported) or the default settings.
99 static inline void pwm_get_state(const struct pwm_device *pwm,
100 struct pwm_state *state)
105 static inline bool pwm_is_enabled(const struct pwm_device *pwm)
107 struct pwm_state state;
109 pwm_get_state(pwm, &state);
111 return state.enabled;
114 static inline u64 pwm_get_period(const struct pwm_device *pwm)
116 struct pwm_state state;
118 pwm_get_state(pwm, &state);
123 static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm)
125 struct pwm_state state;
127 pwm_get_state(pwm, &state);
129 return state.duty_cycle;
132 static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
134 struct pwm_state state;
136 pwm_get_state(pwm, &state);
138 return state.polarity;
141 static inline void pwm_get_args(const struct pwm_device *pwm,
142 struct pwm_args *args)
148 * pwm_init_state() - prepare a new state to be applied with pwm_apply_might_sleep()
150 * @state: state to fill with the prepared PWM state
152 * This functions prepares a state that can later be tweaked and applied
153 * to the PWM device with pwm_apply_might_sleep(). This is a convenient function
154 * that first retrieves the current PWM state and the replaces the period
155 * and polarity fields with the reference values defined in pwm->args.
156 * Once the function returns, you can adjust the ->enabled and ->duty_cycle
157 * fields according to your needs before calling pwm_apply_might_sleep().
159 * ->duty_cycle is initially set to zero to avoid cases where the current
160 * ->duty_cycle value exceed the pwm_args->period one, which would trigger
161 * an error if the user calls pwm_apply_might_sleep() without adjusting ->duty_cycle
164 static inline void pwm_init_state(const struct pwm_device *pwm,
165 struct pwm_state *state)
167 struct pwm_args args;
169 /* First get the current state. */
170 pwm_get_state(pwm, state);
172 /* Then fill it with the reference config */
173 pwm_get_args(pwm, &args);
175 state->period = args.period;
176 state->polarity = args.polarity;
177 state->duty_cycle = 0;
178 state->usage_power = false;
182 * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
183 * @state: PWM state to extract the duty cycle from
184 * @scale: target scale of the relative duty cycle
186 * This functions converts the absolute duty cycle stored in @state (expressed
187 * in nanosecond) into a value relative to the period.
189 * For example if you want to get the duty_cycle expressed in percent, call:
191 * pwm_get_state(pwm, &state);
192 * duty = pwm_get_relative_duty_cycle(&state, 100);
194 static inline unsigned int
195 pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
200 return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale,
205 * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
206 * @state: PWM state to fill
207 * @duty_cycle: relative duty cycle value
208 * @scale: scale in which @duty_cycle is expressed
210 * This functions converts a relative into an absolute duty cycle (expressed
211 * in nanoseconds), and puts the result in state->duty_cycle.
213 * For example if you want to configure a 50% duty cycle, call:
215 * pwm_init_state(pwm, &state);
216 * pwm_set_relative_duty_cycle(&state, 50, 100);
217 * pwm_apply_might_sleep(pwm, &state);
219 * This functions returns -EINVAL if @duty_cycle and/or @scale are
220 * inconsistent (@scale == 0 or @duty_cycle > @scale).
223 pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
226 if (!scale || duty_cycle > scale)
229 state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
237 * struct pwm_capture - PWM capture data
238 * @period: period of the PWM signal (in nanoseconds)
239 * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
243 unsigned int duty_cycle;
247 * struct pwm_ops - PWM controller operations
248 * @request: optional hook for requesting a PWM
249 * @free: optional hook for freeing a PWM
250 * @capture: capture and report PWM signal
251 * @apply: atomically apply a new PWM config
252 * @get_state: get the current PWM state. This function is only
253 * called once per PWM device when the PWM chip is
257 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
258 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
259 int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
260 struct pwm_capture *result, unsigned long timeout);
261 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
262 const struct pwm_state *state);
263 int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
264 struct pwm_state *state);
268 * struct pwm_chip - abstract a PWM controller
269 * @dev: device providing the PWMs
270 * @ops: callbacks for this PWM controller
271 * @owner: module providing this chip
272 * @id: unique number of this PWM chip
273 * @npwm: number of PWMs controlled by this chip
274 * @of_xlate: request a PWM device given a device tree PWM specifier
275 * @atomic: can the driver's ->apply() be called in atomic context
276 * @uses_pwmchip_alloc: signals if pwmchip_allow was used to allocate this chip
277 * @pwms: array of PWM devices allocated by the framework
281 const struct pwm_ops *ops;
282 struct module *owner;
286 struct pwm_device * (*of_xlate)(struct pwm_chip *chip,
287 const struct of_phandle_args *args);
290 /* only used internally by the PWM framework */
291 bool uses_pwmchip_alloc;
292 struct pwm_device pwms[] __counted_by(npwm);
295 static inline struct device *pwmchip_parent(const struct pwm_chip *chip)
297 return chip->dev.parent;
300 static inline void *pwmchip_get_drvdata(struct pwm_chip *chip)
302 return dev_get_drvdata(&chip->dev);
305 static inline void pwmchip_set_drvdata(struct pwm_chip *chip, void *data)
307 dev_set_drvdata(&chip->dev, data);
310 #if IS_ENABLED(CONFIG_PWM)
312 int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state);
313 int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state);
314 int pwm_adjust_config(struct pwm_device *pwm);
317 * pwm_config() - change a PWM device configuration
319 * @duty_ns: "on" time (in nanoseconds)
320 * @period_ns: duration (in nanoseconds) of one cycle
322 * Returns: 0 on success or a negative error code on failure.
324 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
327 struct pwm_state state;
332 if (duty_ns < 0 || period_ns < 0)
335 pwm_get_state(pwm, &state);
336 if (state.duty_cycle == duty_ns && state.period == period_ns)
339 state.duty_cycle = duty_ns;
340 state.period = period_ns;
341 return pwm_apply_might_sleep(pwm, &state);
345 * pwm_enable() - start a PWM output toggling
348 * Returns: 0 on success or a negative error code on failure.
350 static inline int pwm_enable(struct pwm_device *pwm)
352 struct pwm_state state;
357 pwm_get_state(pwm, &state);
361 state.enabled = true;
362 return pwm_apply_might_sleep(pwm, &state);
366 * pwm_disable() - stop a PWM output toggling
369 static inline void pwm_disable(struct pwm_device *pwm)
371 struct pwm_state state;
376 pwm_get_state(pwm, &state);
380 state.enabled = false;
381 pwm_apply_might_sleep(pwm, &state);
385 * pwm_might_sleep() - is pwm_apply_atomic() supported?
388 * Returns: false if pwm_apply_atomic() can be called from atomic context.
390 static inline bool pwm_might_sleep(struct pwm_device *pwm)
392 return !pwm->chip->atomic;
395 /* PWM provider APIs */
396 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
397 unsigned long timeout);
399 void pwmchip_put(struct pwm_chip *chip);
400 struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv);
401 struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv);
403 int __pwmchip_add(struct pwm_chip *chip, struct module *owner);
404 #define pwmchip_add(chip) __pwmchip_add(chip, THIS_MODULE)
405 void pwmchip_remove(struct pwm_chip *chip);
407 int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner);
408 #define devm_pwmchip_add(dev, chip) __devm_pwmchip_add(dev, chip, THIS_MODULE)
410 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
414 struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *chip,
415 const struct of_phandle_args *args);
416 struct pwm_device *of_pwm_single_xlate(struct pwm_chip *chip,
417 const struct of_phandle_args *args);
419 struct pwm_device *pwm_get(struct device *dev, const char *con_id);
420 void pwm_put(struct pwm_device *pwm);
422 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
423 struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
424 struct fwnode_handle *fwnode,
427 static inline bool pwm_might_sleep(struct pwm_device *pwm)
432 static inline int pwm_apply_might_sleep(struct pwm_device *pwm,
433 const struct pwm_state *state)
439 static inline int pwm_apply_atomic(struct pwm_device *pwm,
440 const struct pwm_state *state)
445 static inline int pwm_adjust_config(struct pwm_device *pwm)
450 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
457 static inline int pwm_enable(struct pwm_device *pwm)
463 static inline void pwm_disable(struct pwm_device *pwm)
468 static inline int pwm_capture(struct pwm_device *pwm,
469 struct pwm_capture *result,
470 unsigned long timeout)
475 static inline void pwmchip_put(struct pwm_chip *chip)
479 static inline struct pwm_chip *pwmchip_alloc(struct device *parent,
483 return ERR_PTR(-EINVAL);
486 static inline struct pwm_chip *devm_pwmchip_alloc(struct device *parent,
490 return pwmchip_alloc(parent, npwm, sizeof_priv);
493 static inline int pwmchip_add(struct pwm_chip *chip)
498 static inline int pwmchip_remove(struct pwm_chip *chip)
503 static inline int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip)
508 static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
513 return ERR_PTR(-ENODEV);
516 static inline struct pwm_device *pwm_get(struct device *dev,
517 const char *consumer)
520 return ERR_PTR(-ENODEV);
523 static inline void pwm_put(struct pwm_device *pwm)
528 static inline struct pwm_device *devm_pwm_get(struct device *dev,
529 const char *consumer)
532 return ERR_PTR(-ENODEV);
535 static inline struct pwm_device *
536 devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode,
540 return ERR_PTR(-ENODEV);
544 static inline void pwm_apply_args(struct pwm_device *pwm)
546 struct pwm_state state = { };
549 * PWM users calling pwm_apply_args() expect to have a fresh config
550 * where the polarity and period are set according to pwm_args info.
551 * The problem is, polarity can only be changed when the PWM is
554 * PWM drivers supporting hardware readout may declare the PWM device
555 * as enabled, and prevent polarity setting, which changes from the
556 * existing behavior, where all PWM devices are declared as disabled
557 * at startup (even if they are actually enabled), thus authorizing
560 * To fulfill this requirement, we apply a new state which disables
561 * the PWM device and set the reference period and polarity config.
563 * Note that PWM users requiring a smooth handover between the
564 * bootloader and the kernel (like critical regulators controlled by
565 * PWM devices) will have to switch to the atomic API and avoid calling
569 state.enabled = false;
570 state.polarity = pwm->args.polarity;
571 state.period = pwm->args.period;
572 state.usage_power = false;
574 pwm_apply_might_sleep(pwm, &state);
577 /* only for backwards-compatibility, new code should not use this */
578 static inline int pwm_apply_state(struct pwm_device *pwm,
579 const struct pwm_state *state)
581 return pwm_apply_might_sleep(pwm, state);
585 struct list_head list;
586 const char *provider;
591 enum pwm_polarity polarity;
592 const char *module; /* optional, may be NULL */
595 #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \
596 _period, _polarity, _module) \
598 .provider = _provider, \
603 .polarity = _polarity, \
607 #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
608 PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
611 #if IS_ENABLED(CONFIG_PWM)
612 void pwm_add_table(struct pwm_lookup *table, size_t num);
613 void pwm_remove_table(struct pwm_lookup *table, size_t num);
615 static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
619 static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
624 #endif /* __LINUX_PWM_H */