Lines Matching +full:- +full:pwm

1 /* SPDX-License-Identifier: GPL-2.0 */
12 * enum pwm_polarity - polarity of a PWM signal
13 * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
16 * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
26 * struct pwm_args - board-dependent PWM arguments
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.
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
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
89 * pwm_get_state() - retrieve the current PWM state
90 * @pwm: PWM device
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
98 static inline void pwm_get_state(const struct pwm_device *pwm, in pwm_get_state() argument
101 *state = pwm->state; in pwm_get_state()
104 static inline bool pwm_is_enabled(const struct pwm_device *pwm) in pwm_is_enabled() argument
108 pwm_get_state(pwm, &state); in pwm_is_enabled()
113 static inline u64 pwm_get_period(const struct pwm_device *pwm) in pwm_get_period() argument
117 pwm_get_state(pwm, &state); in pwm_get_period()
122 static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm) in pwm_get_duty_cycle() argument
126 pwm_get_state(pwm, &state); in pwm_get_duty_cycle()
131 static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm) in pwm_get_polarity() argument
135 pwm_get_state(pwm, &state); in pwm_get_polarity()
140 static inline void pwm_get_args(const struct pwm_device *pwm, in pwm_get_args() argument
143 *args = pwm->args; in pwm_get_args()
147 * pwm_init_state() - prepare a new state to be applied with pwm_apply_might_sleep()
148 * @pwm: PWM device
149 * @state: state to fill with the prepared PWM state
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
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, in pwm_init_state() argument
169 pwm_get_state(pwm, state); in pwm_init_state()
172 pwm_get_args(pwm, &args); in pwm_init_state()
174 state->period = args.period; in pwm_init_state()
175 state->polarity = args.polarity; in pwm_init_state()
176 state->duty_cycle = 0; in pwm_init_state()
177 state->usage_power = false; in pwm_init_state()
181 * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
182 * @state: PWM state to extract the duty cycle from
190 * pwm_get_state(pwm, &state);
196 if (!state->period) in pwm_get_relative_duty_cycle()
199 return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale, in pwm_get_relative_duty_cycle()
200 state->period); in pwm_get_relative_duty_cycle()
204 * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
205 * @state: PWM state to fill
210 * in nanoseconds), and puts the result in state->duty_cycle.
214 * pwm_init_state(pwm, &state);
216 * pwm_apply_might_sleep(pwm, &state);
218 * This functions returns -EINVAL if @duty_cycle and/or @scale are
226 return -EINVAL; in pwm_set_relative_duty_cycle()
228 state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle * in pwm_set_relative_duty_cycle()
229 state->period, in pwm_set_relative_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)
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,
260 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
262 int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
267 * struct pwm_chip - abstract a PWM controller
269 * @ops: callbacks for this PWM controller
271 * @id: unique number of this PWM chip
273 * @of_xlate: request a PWM device given a device tree PWM specifier
274 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
275 * @atomic: can the driver's ->apply() be called in atomic context
276 * @pwms: array of PWM devices allocated by the framework
290 /* only used internally by the PWM framework */
295 /* PWM user APIs */
296 int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state);
297 int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state);
298 int pwm_adjust_config(struct pwm_device *pwm);
301 * pwm_config() - change a PWM device configuration
302 * @pwm: PWM device
308 static inline int pwm_config(struct pwm_device *pwm, int duty_ns, in pwm_config() argument
313 if (!pwm) in pwm_config()
314 return -EINVAL; in pwm_config()
317 return -EINVAL; in pwm_config()
319 pwm_get_state(pwm, &state); in pwm_config()
325 return pwm_apply_might_sleep(pwm, &state); in pwm_config()
329 * pwm_enable() - start a PWM output toggling
330 * @pwm: PWM device
334 static inline int pwm_enable(struct pwm_device *pwm) in pwm_enable() argument
338 if (!pwm) in pwm_enable()
339 return -EINVAL; in pwm_enable()
341 pwm_get_state(pwm, &state); in pwm_enable()
346 return pwm_apply_might_sleep(pwm, &state); in pwm_enable()
350 * pwm_disable() - stop a PWM output toggling
351 * @pwm: PWM device
353 static inline void pwm_disable(struct pwm_device *pwm) in pwm_disable() argument
357 if (!pwm) in pwm_disable()
360 pwm_get_state(pwm, &state); in pwm_disable()
365 pwm_apply_might_sleep(pwm, &state); in pwm_disable()
369 * pwm_might_sleep() - is pwm_apply_atomic() supported?
370 * @pwm: PWM device
374 static inline bool pwm_might_sleep(struct pwm_device *pwm) in pwm_might_sleep() argument
376 return !pwm->chip->atomic; in pwm_might_sleep()
379 /* PWM provider APIs */
380 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
400 void pwm_put(struct pwm_device *pwm);
407 static inline bool pwm_might_sleep(struct pwm_device *pwm) in pwm_might_sleep() argument
412 static inline int pwm_apply_might_sleep(struct pwm_device *pwm, in pwm_apply_might_sleep() argument
416 return -EOPNOTSUPP; in pwm_apply_might_sleep()
419 static inline int pwm_apply_atomic(struct pwm_device *pwm, in pwm_apply_atomic() argument
422 return -EOPNOTSUPP; in pwm_apply_atomic()
425 static inline int pwm_adjust_config(struct pwm_device *pwm) in pwm_adjust_config() argument
427 return -EOPNOTSUPP; in pwm_adjust_config()
430 static inline int pwm_config(struct pwm_device *pwm, int duty_ns, in pwm_config() argument
434 return -EINVAL; in pwm_config()
437 static inline int pwm_enable(struct pwm_device *pwm) in pwm_enable() argument
440 return -EINVAL; in pwm_enable()
443 static inline void pwm_disable(struct pwm_device *pwm) in pwm_disable() argument
448 static inline int pwm_capture(struct pwm_device *pwm, in pwm_capture() argument
452 return -EINVAL; in pwm_capture()
457 return -EINVAL; in pwmchip_add()
462 return -EINVAL; in pwmchip_remove()
467 return -EINVAL; in devm_pwmchip_add()
475 return ERR_PTR(-ENODEV); in pwm_request_from_chip()
482 return ERR_PTR(-ENODEV); in pwm_get()
485 static inline void pwm_put(struct pwm_device *pwm) in pwm_put() argument
494 return ERR_PTR(-ENODEV); in devm_pwm_get()
502 return ERR_PTR(-ENODEV); in devm_fwnode_pwm_get()
506 static inline void pwm_apply_args(struct pwm_device *pwm) in pwm_apply_args() argument
511 * PWM users calling pwm_apply_args() expect to have a fresh config in pwm_apply_args()
513 * The problem is, polarity can only be changed when the PWM is in pwm_apply_args()
516 * PWM drivers supporting hardware readout may declare the PWM device in pwm_apply_args()
518 * existing behavior, where all PWM devices are declared as disabled in pwm_apply_args()
523 * the PWM device and set the reference period and polarity config. in pwm_apply_args()
525 * Note that PWM users requiring a smooth handover between the in pwm_apply_args()
527 * PWM devices) will have to switch to the atomic API and avoid calling in pwm_apply_args()
532 state.polarity = pwm->args.polarity; in pwm_apply_args()
533 state.period = pwm->args.period; in pwm_apply_args()
536 pwm_apply_might_sleep(pwm, &state); in pwm_apply_args()
539 /* only for backwards-compatibility, new code should not use this */
540 static inline int pwm_apply_state(struct pwm_device *pwm, in pwm_apply_state() argument
543 return pwm_apply_might_sleep(pwm, state); in pwm_apply_state()