Lines Matching +full:pwm +full:- +full:number
1 /* SPDX-License-Identifier: GPL-2.0 */
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 * enum pwm_output_type - output type of the PWM signal
54 * @PWM_OUTPUT_FIXED: PWM output is fixed until a change request
55 * @PWM_OUTPUT_MODULATED: PWM output is modulated in hardware
64 * struct pwm_state - state of a PWM channel
65 * @period: PWM period (in nanoseconds)
66 * @duty_cycle: PWM duty cycle (in nanoseconds)
67 * @polarity: PWM polarity
68 * @enabled: PWM enabled status
82 * struct pwm_device - PWM channel object
83 * @label: name of the PWM device
84 * @flags: flags associated with the PWM device
85 * @hwpwm: per-chip relative index of the PWM device
86 * @pwm: global index of the PWM device
87 * @chip: PWM chip providing this PWM device
88 * @chip_data: chip-private data associated with the PWM device
89 * @args: PWM arguments
97 unsigned int pwm; member
109 * pwm_get_state() - retrieve the current PWM state
110 * @pwm: PWM device
111 * @state: state to fill with the current PWM state
113 static inline void pwm_get_state(const struct pwm_device *pwm, in pwm_get_state() argument
116 *state = pwm->state; in pwm_get_state()
119 static inline bool pwm_is_enabled(const struct pwm_device *pwm) in pwm_is_enabled() argument
123 pwm_get_state(pwm, &state); in pwm_is_enabled()
128 static inline void pwm_set_period(struct pwm_device *pwm, u64 period) in pwm_set_period() argument
130 if (pwm) in pwm_set_period()
131 pwm->state.period = period; in pwm_set_period()
134 static inline u64 pwm_get_period(const struct pwm_device *pwm) in pwm_get_period() argument
138 pwm_get_state(pwm, &state); in pwm_get_period()
143 static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty) in pwm_set_duty_cycle() argument
145 if (pwm) in pwm_set_duty_cycle()
146 pwm->state.duty_cycle = duty; in pwm_set_duty_cycle()
149 static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm) in pwm_get_duty_cycle() argument
153 pwm_get_state(pwm, &state); in pwm_get_duty_cycle()
158 static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm) in pwm_get_polarity() argument
162 pwm_get_state(pwm, &state); in pwm_get_polarity()
168 const struct pwm_device *pwm) in pwm_get_output_type() argument
172 pwm_get_state(pwm, &state); in pwm_get_output_type()
177 static inline void pwm_get_args(const struct pwm_device *pwm, in pwm_get_args() argument
180 *args = pwm->args; in pwm_get_args()
184 * pwm_init_state() - prepare a new state to be applied with pwm_apply_state()
185 * @pwm: PWM device
186 * @state: state to fill with the prepared PWM state
189 * to the PWM device with pwm_apply_state(). This is a convenient function
190 * that first retrieves the current PWM state and the replaces the period
191 * and polarity fields with the reference values defined in pwm->args.
192 * Once the function returns, you can adjust the ->enabled and ->duty_cycle
195 * ->duty_cycle is initially set to zero to avoid cases where the current
196 * ->duty_cycle value exceed the pwm_args->period one, which would trigger
197 * an error if the user calls pwm_apply_state() without adjusting ->duty_cycle
200 static inline void pwm_init_state(const struct pwm_device *pwm, in pwm_init_state() argument
206 pwm_get_state(pwm, state); in pwm_init_state()
209 pwm_get_args(pwm, &args); in pwm_init_state()
211 state->period = args.period; in pwm_init_state()
212 state->polarity = args.polarity; in pwm_init_state()
213 state->duty_cycle = 0; in pwm_init_state()
217 * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
218 * @state: PWM state to extract the duty cycle from
226 * pwm_get_state(pwm, &state);
232 if (!state->period) in pwm_get_relative_duty_cycle()
235 return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale, in pwm_get_relative_duty_cycle()
236 state->period); in pwm_get_relative_duty_cycle()
240 * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
241 * @state: PWM state to fill
246 * in nanoseconds), and puts the result in state->duty_cycle.
250 * pwm_init_state(pwm, &state);
252 * pwm_apply_state(pwm, &state);
254 * This functions returns -EINVAL if @duty_cycle and/or @scale are
262 return -EINVAL; in pwm_set_relative_duty_cycle()
264 state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle * in pwm_set_relative_duty_cycle()
265 state->period, in pwm_set_relative_duty_cycle()
272 * struct pwm_ops - PWM controller operations
273 * @request: optional hook for requesting a PWM
274 * @free: optional hook for freeing a PWM
275 * @capture: capture and report PWM signal
276 * @apply: atomically apply a new PWM config
277 * @get_state: get the current PWM state. This function is only
278 * called once per PWM device when the PWM chip is
280 * @get_output_type_supported: get the supported output type of this PWM
282 * @config: configure duty cycles and period length for this PWM
283 * @set_polarity: configure the polarity of this PWM
284 * @enable: enable PWM output toggling
285 * @disable: disable PWM output toggling
288 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
289 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
290 int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
292 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
294 void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
297 struct pwm_device *pwm);
301 int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
303 int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
305 int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
306 void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
312 * struct pwm_chip - abstract a PWM controller
314 * @ops: callbacks for this PWM controller
315 * @base: number of first PWM controlled by this chip
316 * @npwm: number of PWMs controlled by this chip
317 * @of_xlate: request a PWM device given a device tree PWM specifier
318 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
320 * @pwms: array of PWM devices allocated by the framework
332 /* only used internally by the PWM framework */
340 * struct pwm_capture - PWM capture data
341 * @period: period of the PWM signal (in nanoseconds)
342 * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
350 /* PWM user APIs */
352 void pwm_free(struct pwm_device *pwm);
353 int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state);
354 int pwm_adjust_config(struct pwm_device *pwm);
357 * pwm_get_output_type_supported() - obtain output type of a PWM device.
358 * @pwm: PWM device
360 * Returns: output type supported by the PWM device
362 static inline int pwm_get_output_type_supported(struct pwm_device *pwm) in pwm_get_output_type_supported() argument
364 if (!pwm) in pwm_get_output_type_supported()
365 return -EINVAL; in pwm_get_output_type_supported()
367 if (pwm->chip->ops->get_output_type_supported) in pwm_get_output_type_supported()
368 return pwm->chip->ops->get_output_type_supported(pwm->chip, in pwm_get_output_type_supported()
369 pwm); in pwm_get_output_type_supported()
375 * pwm_config() - change a PWM device configuration
376 * @pwm: PWM device
382 static inline int pwm_config(struct pwm_device *pwm, int duty_ns, in pwm_config() argument
387 if (!pwm) in pwm_config()
388 return -EINVAL; in pwm_config()
391 return -EINVAL; in pwm_config()
393 pwm_get_state(pwm, &state); in pwm_config()
399 return pwm_apply_state(pwm, &state); in pwm_config()
403 * pwm_enable() - start a PWM output toggling
404 * @pwm: PWM device
408 static inline int pwm_enable(struct pwm_device *pwm) in pwm_enable() argument
412 if (!pwm) in pwm_enable()
413 return -EINVAL; in pwm_enable()
415 pwm_get_state(pwm, &state); in pwm_enable()
420 return pwm_apply_state(pwm, &state); in pwm_enable()
424 * pwm_disable() - stop a PWM output toggling
425 * @pwm: PWM device
427 static inline void pwm_disable(struct pwm_device *pwm) in pwm_disable() argument
431 if (!pwm) in pwm_disable()
434 pwm_get_state(pwm, &state); in pwm_disable()
439 pwm_apply_state(pwm, &state); in pwm_disable()
442 /* PWM provider APIs */
443 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
445 int pwm_set_chip_data(struct pwm_device *pwm, void *data);
446 void *pwm_get_chip_data(struct pwm_device *pwm);
462 void pwm_put(struct pwm_device *pwm);
470 void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
474 return ERR_PTR(-ENODEV); in pwm_request()
477 static inline void pwm_free(struct pwm_device *pwm) in pwm_free() argument
481 static inline int pwm_apply_state(struct pwm_device *pwm, in pwm_apply_state() argument
484 return -ENOTSUPP; in pwm_apply_state()
487 static inline int pwm_adjust_config(struct pwm_device *pwm) in pwm_adjust_config() argument
489 return -ENOTSUPP; in pwm_adjust_config()
492 static inline int pwm_get_output_type_supported(struct pwm_device *pwm) in pwm_get_output_type_supported() argument
494 return -EINVAL; in pwm_get_output_type_supported()
497 static inline int pwm_config(struct pwm_device *pwm, int duty_ns, in pwm_config() argument
500 return -EINVAL; in pwm_config()
503 static inline int pwm_capture(struct pwm_device *pwm, in pwm_capture() argument
507 return -EINVAL; in pwm_capture()
510 static inline int pwm_enable(struct pwm_device *pwm) in pwm_enable() argument
512 return -EINVAL; in pwm_enable()
515 static inline void pwm_disable(struct pwm_device *pwm) in pwm_disable() argument
519 static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data) in pwm_set_chip_data() argument
521 return -EINVAL; in pwm_set_chip_data()
524 static inline void *pwm_get_chip_data(struct pwm_device *pwm) in pwm_get_chip_data() argument
531 return -EINVAL; in pwmchip_add()
536 return -EINVAL; in pwmchip_add_inversed()
541 return -EINVAL; in pwmchip_remove()
548 return ERR_PTR(-ENODEV); in pwm_request_from_chip()
554 return ERR_PTR(-ENODEV); in pwm_get()
561 return ERR_PTR(-ENODEV); in of_pwm_get()
564 static inline void pwm_put(struct pwm_device *pwm) in pwm_put() argument
571 return ERR_PTR(-ENODEV); in devm_pwm_get()
578 return ERR_PTR(-ENODEV); in devm_of_pwm_get()
585 return ERR_PTR(-ENODEV); in devm_fwnode_pwm_get()
588 static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm) in devm_pwm_put() argument
593 static inline void pwm_apply_args(struct pwm_device *pwm) in pwm_apply_args() argument
598 * PWM users calling pwm_apply_args() expect to have a fresh config in pwm_apply_args()
600 * The problem is, polarity can only be changed when the PWM is in pwm_apply_args()
603 * PWM drivers supporting hardware readout may declare the PWM device in pwm_apply_args()
605 * existing behavior, where all PWM devices are declared as disabled in pwm_apply_args()
610 * the PWM device and set the reference period and polarity config. in pwm_apply_args()
612 * Note that PWM users requiring a smooth handover between the in pwm_apply_args()
614 * PWM devices) will have to switch to the atomic API and avoid calling in pwm_apply_args()
619 state.polarity = pwm->args.polarity; in pwm_apply_args()
620 state.period = pwm->args.period; in pwm_apply_args()
622 pwm_apply_state(pwm, &state); in pwm_apply_args()