Lines Matching +full:- +full:pwm
1 /* SPDX-License-Identifier: GPL-2.0 */
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-
20 * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
30 * struct pwm_args - board-dependent PWM arguments
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.
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
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
93 * pwm_get_state() - retrieve the current PWM state
94 * @pwm: PWM device
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
102 static inline void pwm_get_state(const struct pwm_device *pwm, in pwm_get_state() argument
105 *state = pwm->state; in pwm_get_state()
108 static inline bool pwm_is_enabled(const struct pwm_device *pwm) in pwm_is_enabled() argument
112 pwm_get_state(pwm, &state); in pwm_is_enabled()
117 static inline u64 pwm_get_period(const struct pwm_device *pwm) in pwm_get_period() argument
121 pwm_get_state(pwm, &state); in pwm_get_period()
126 static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm) in pwm_get_duty_cycle() argument
130 pwm_get_state(pwm, &state); in pwm_get_duty_cycle()
135 static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm) in pwm_get_polarity() argument
139 pwm_get_state(pwm, &state); in pwm_get_polarity()
144 static inline void pwm_get_args(const struct pwm_device *pwm, in pwm_get_args() argument
147 *args = pwm->args; in pwm_get_args()
151 * pwm_init_state() - prepare a new state to be applied with pwm_apply_might_sleep()
152 * @pwm: PWM device
153 * @state: state to fill with the prepared PWM state
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
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, in pwm_init_state() argument
173 pwm_get_state(pwm, state); in pwm_init_state()
176 pwm_get_args(pwm, &args); in pwm_init_state()
178 state->period = args.period; in pwm_init_state()
179 state->polarity = args.polarity; in pwm_init_state()
180 state->duty_cycle = 0; in pwm_init_state()
181 state->usage_power = false; in pwm_init_state()
185 * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
186 * @state: PWM state to extract the duty cycle from
194 * pwm_get_state(pwm, &state);
200 if (!state->period) in pwm_get_relative_duty_cycle()
203 return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale, in pwm_get_relative_duty_cycle()
204 state->period); in pwm_get_relative_duty_cycle()
208 * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
209 * @state: PWM state to fill
214 * in nanoseconds), and puts the result in state->duty_cycle.
218 * pwm_init_state(pwm, &state);
220 * pwm_apply_might_sleep(pwm, &state);
222 * This functions returns -EINVAL if @duty_cycle and/or @scale are
230 return -EINVAL; in pwm_set_relative_duty_cycle()
232 state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle * in pwm_set_relative_duty_cycle()
233 state->period, in pwm_set_relative_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)
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,
262 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
264 int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
269 * struct pwm_chip - abstract a PWM controller
271 * @ops: callbacks for this PWM controller
273 * @id: unique number of this PWM 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
278 * @pwms: array of PWM devices allocated by the framework
291 /* only used internally by the PWM framework */
298 return chip->dev.parent; in pwmchip_parent()
303 return dev_get_drvdata(&chip->dev); in pwmchip_get_drvdata()
308 dev_set_drvdata(&chip->dev, data); in pwmchip_set_drvdata()
312 /* PWM user APIs */
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
319 * @pwm: PWM device
325 static inline int pwm_config(struct pwm_device *pwm, int duty_ns, in pwm_config() argument
330 if (!pwm) in pwm_config()
331 return -EINVAL; in pwm_config()
334 return -EINVAL; in pwm_config()
336 pwm_get_state(pwm, &state); in pwm_config()
342 return pwm_apply_might_sleep(pwm, &state); in pwm_config()
346 * pwm_enable() - start a PWM output toggling
347 * @pwm: PWM device
351 static inline int pwm_enable(struct pwm_device *pwm) in pwm_enable() argument
355 if (!pwm) in pwm_enable()
356 return -EINVAL; in pwm_enable()
358 pwm_get_state(pwm, &state); in pwm_enable()
363 return pwm_apply_might_sleep(pwm, &state); in pwm_enable()
367 * pwm_disable() - stop a PWM output toggling
368 * @pwm: PWM device
370 static inline void pwm_disable(struct pwm_device *pwm) in pwm_disable() argument
374 if (!pwm) in pwm_disable()
377 pwm_get_state(pwm, &state); in pwm_disable()
382 pwm_apply_might_sleep(pwm, &state); in pwm_disable()
386 * pwm_might_sleep() - is pwm_apply_atomic() supported?
387 * @pwm: PWM device
391 static inline bool pwm_might_sleep(struct pwm_device *pwm) in pwm_might_sleep() argument
393 return !pwm->chip->atomic; in pwm_might_sleep()
396 /* PWM provider APIs */
414 void pwm_put(struct pwm_device *pwm);
421 static inline bool pwm_might_sleep(struct pwm_device *pwm) in pwm_might_sleep() argument
426 static inline int pwm_apply_might_sleep(struct pwm_device *pwm, in pwm_apply_might_sleep() argument
430 return -EOPNOTSUPP; in pwm_apply_might_sleep()
433 static inline int pwm_apply_atomic(struct pwm_device *pwm, in pwm_apply_atomic() argument
436 return -EOPNOTSUPP; in pwm_apply_atomic()
439 static inline int pwm_adjust_config(struct pwm_device *pwm) in pwm_adjust_config() argument
441 return -EOPNOTSUPP; in pwm_adjust_config()
444 static inline int pwm_config(struct pwm_device *pwm, int duty_ns, in pwm_config() argument
448 return -EINVAL; in pwm_config()
451 static inline int pwm_enable(struct pwm_device *pwm) in pwm_enable() argument
454 return -EINVAL; in pwm_enable()
457 static inline void pwm_disable(struct pwm_device *pwm) in pwm_disable() argument
470 return ERR_PTR(-EINVAL); in pwmchip_alloc()
482 return -EINVAL; in pwmchip_add()
487 return -EINVAL; in pwmchip_remove()
492 return -EINVAL; in devm_pwmchip_add()
499 return ERR_PTR(-ENODEV); in pwm_get()
502 static inline void pwm_put(struct pwm_device *pwm) in pwm_put() argument
511 return ERR_PTR(-ENODEV); in devm_pwm_get()
519 return ERR_PTR(-ENODEV); in devm_fwnode_pwm_get()
523 static inline void pwm_apply_args(struct pwm_device *pwm) in pwm_apply_args() argument
528 * PWM users calling pwm_apply_args() expect to have a fresh config in pwm_apply_args()
530 * The problem is, polarity can only be changed when the PWM is in pwm_apply_args()
533 * PWM drivers supporting hardware readout may declare the PWM device in pwm_apply_args()
535 * existing behavior, where all PWM devices are declared as disabled in pwm_apply_args()
540 * the PWM device and set the reference period and polarity config. in pwm_apply_args()
542 * Note that PWM users requiring a smooth handover between the in pwm_apply_args()
544 * PWM devices) will have to switch to the atomic API and avoid calling in pwm_apply_args()
549 state.polarity = pwm->args.polarity; in pwm_apply_args()
550 state.period = pwm->args.period; in pwm_apply_args()
553 pwm_apply_might_sleep(pwm, &state); in pwm_apply_args()