Lines Matching +full:- +full:pwm

1 /* SPDX-License-Identifier: GPL-2.0 */
15 * enum pwm_polarity - polarity of a PWM signal
16 * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
19 * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
29 * struct pwm_args - board-dependent PWM arguments
33 * This structure describes board-dependent arguments attached to a PWM
34 * device. These arguments are usually retrieved from the PWM lookup table or
37 * Do not confuse this with the PWM state: PWM arguments represent the initial
38 * configuration that users want to use on this PWM device rather than the
39 * current PWM hardware state.
52 * struct pwm_state - state of a PWM channel
53 * @period: PWM period (in nanoseconds)
54 * @duty_cycle: PWM duty cycle (in nanoseconds)
55 * @polarity: PWM polarity
56 * @enabled: PWM enabled status
66 * struct pwm_device - PWM channel object
67 * @label: name of the PWM device
68 * @flags: flags associated with the PWM device
69 * @hwpwm: per-chip relative index of the PWM device
70 * @pwm: global index of the PWM device
71 * @chip: PWM chip providing this PWM device
72 * @chip_data: chip-private data associated with the PWM device
73 * @args: PWM arguments
81 unsigned int pwm; member
91 * pwm_get_state() - retrieve the current PWM state
92 * @pwm: PWM device
93 * @state: state to fill with the current PWM state
95 static inline void pwm_get_state(const struct pwm_device *pwm, in pwm_get_state() argument
98 *state = pwm->state; in pwm_get_state()
101 static inline bool pwm_is_enabled(const struct pwm_device *pwm) in pwm_is_enabled() argument
105 pwm_get_state(pwm, &state); in pwm_is_enabled()
110 static inline void pwm_set_period(struct pwm_device *pwm, u64 period) in pwm_set_period() argument
112 if (pwm) in pwm_set_period()
113 pwm->state.period = period; in pwm_set_period()
116 static inline u64 pwm_get_period(const struct pwm_device *pwm) in pwm_get_period() argument
120 pwm_get_state(pwm, &state); in pwm_get_period()
125 static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty) in pwm_set_duty_cycle() argument
127 if (pwm) in pwm_set_duty_cycle()
128 pwm->state.duty_cycle = duty; in pwm_set_duty_cycle()
131 static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm) in pwm_get_duty_cycle() argument
135 pwm_get_state(pwm, &state); in pwm_get_duty_cycle()
140 static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm) in pwm_get_polarity() argument
144 pwm_get_state(pwm, &state); in pwm_get_polarity()
149 static inline void pwm_get_args(const struct pwm_device *pwm, in pwm_get_args() argument
152 *args = pwm->args; in pwm_get_args()
156 * pwm_init_state() - prepare a new state to be applied with pwm_apply_state()
157 * @pwm: PWM device
158 * @state: state to fill with the prepared PWM state
161 * to the PWM device with pwm_apply_state(). This is a convenient function
162 * that first retrieves the current PWM state and the replaces the period
163 * and polarity fields with the reference values defined in pwm->args.
164 * Once the function returns, you can adjust the ->enabled and ->duty_cycle
167 * ->duty_cycle is initially set to zero to avoid cases where the current
168 * ->duty_cycle value exceed the pwm_args->period one, which would trigger
169 * an error if the user calls pwm_apply_state() without adjusting ->duty_cycle
172 static inline void pwm_init_state(const struct pwm_device *pwm, in pwm_init_state() argument
178 pwm_get_state(pwm, state); in pwm_init_state()
181 pwm_get_args(pwm, &args); in pwm_init_state()
183 state->period = args.period; in pwm_init_state()
184 state->polarity = args.polarity; in pwm_init_state()
185 state->duty_cycle = 0; in pwm_init_state()
189 * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
190 * @state: PWM state to extract the duty cycle from
198 * pwm_get_state(pwm, &state);
204 if (!state->period) in pwm_get_relative_duty_cycle()
207 return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale, in pwm_get_relative_duty_cycle()
208 state->period); in pwm_get_relative_duty_cycle()
212 * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
213 * @state: PWM state to fill
218 * in nanoseconds), and puts the result in state->duty_cycle.
222 * pwm_init_state(pwm, &state);
224 * pwm_apply_state(pwm, &state);
226 * This functions returns -EINVAL if @duty_cycle and/or @scale are
234 return -EINVAL; in pwm_set_relative_duty_cycle()
236 state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle * in pwm_set_relative_duty_cycle()
237 state->period, in pwm_set_relative_duty_cycle()
244 * struct pwm_ops - PWM controller operations
245 * @request: optional hook for requesting a PWM
246 * @free: optional hook for freeing a PWM
247 * @capture: capture and report PWM signal
248 * @apply: atomically apply a new PWM config
249 * @get_state: get the current PWM state. This function is only
250 * called once per PWM device when the PWM chip is
253 * @config: configure duty cycles and period length for this PWM
254 * @set_polarity: configure the polarity of this PWM
255 * @enable: enable PWM output toggling
256 * @disable: disable PWM output toggling
259 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
260 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
261 int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
263 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
265 void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
270 int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
272 int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
274 int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
275 void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
279 * struct pwm_chip - abstract a PWM controller
281 * @ops: callbacks for this PWM controller
282 * @base: number of first PWM controlled by this chip
284 * @of_xlate: request a PWM device given a device tree PWM specifier
285 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
287 * @pwms: array of PWM devices allocated by the framework
299 /* only used internally by the PWM framework */
305 * struct pwm_capture - PWM capture data
306 * @period: period of the PWM signal (in nanoseconds)
307 * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
315 /* PWM user APIs */
317 void pwm_free(struct pwm_device *pwm);
318 int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state);
319 int pwm_adjust_config(struct pwm_device *pwm);
322 * pwm_config() - change a PWM device configuration
323 * @pwm: PWM device
329 static inline int pwm_config(struct pwm_device *pwm, int duty_ns, in pwm_config() argument
334 if (!pwm) in pwm_config()
335 return -EINVAL; in pwm_config()
338 return -EINVAL; in pwm_config()
340 pwm_get_state(pwm, &state); in pwm_config()
346 return pwm_apply_state(pwm, &state); in pwm_config()
350 * pwm_enable() - start a PWM output toggling
351 * @pwm: PWM device
355 static inline int pwm_enable(struct pwm_device *pwm) in pwm_enable() argument
359 if (!pwm) in pwm_enable()
360 return -EINVAL; in pwm_enable()
362 pwm_get_state(pwm, &state); in pwm_enable()
367 return pwm_apply_state(pwm, &state); in pwm_enable()
371 * pwm_disable() - stop a PWM output toggling
372 * @pwm: PWM device
374 static inline void pwm_disable(struct pwm_device *pwm) in pwm_disable() argument
378 if (!pwm) in pwm_disable()
381 pwm_get_state(pwm, &state); in pwm_disable()
386 pwm_apply_state(pwm, &state); in pwm_disable()
389 /* PWM provider APIs */
390 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
392 int pwm_set_chip_data(struct pwm_device *pwm, void *data);
393 void *pwm_get_chip_data(struct pwm_device *pwm);
409 void pwm_put(struct pwm_device *pwm);
417 void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
421 return ERR_PTR(-ENODEV); in pwm_request()
424 static inline void pwm_free(struct pwm_device *pwm) in pwm_free() argument
428 static inline int pwm_apply_state(struct pwm_device *pwm, in pwm_apply_state() argument
431 return -ENOTSUPP; in pwm_apply_state()
434 static inline int pwm_adjust_config(struct pwm_device *pwm) in pwm_adjust_config() argument
436 return -ENOTSUPP; in pwm_adjust_config()
439 static inline int pwm_config(struct pwm_device *pwm, int duty_ns, in pwm_config() argument
442 return -EINVAL; in pwm_config()
445 static inline int pwm_capture(struct pwm_device *pwm, in pwm_capture() argument
449 return -EINVAL; in pwm_capture()
452 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
461 static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data) in pwm_set_chip_data() argument
463 return -EINVAL; in pwm_set_chip_data()
466 static inline void *pwm_get_chip_data(struct pwm_device *pwm) in pwm_get_chip_data() argument
473 return -EINVAL; in pwmchip_add()
478 return -EINVAL; in pwmchip_add_inversed()
483 return -EINVAL; in pwmchip_remove()
490 return ERR_PTR(-ENODEV); in pwm_request_from_chip()
496 return ERR_PTR(-ENODEV); in pwm_get()
503 return ERR_PTR(-ENODEV); in of_pwm_get()
506 static inline void pwm_put(struct pwm_device *pwm) in pwm_put() argument
513 return ERR_PTR(-ENODEV); in devm_pwm_get()
520 return ERR_PTR(-ENODEV); in devm_of_pwm_get()
527 return ERR_PTR(-ENODEV); in devm_fwnode_pwm_get()
530 static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm) in devm_pwm_put() argument
535 static inline void pwm_apply_args(struct pwm_device *pwm) in pwm_apply_args() argument
540 * PWM users calling pwm_apply_args() expect to have a fresh config in pwm_apply_args()
542 * The problem is, polarity can only be changed when the PWM is in pwm_apply_args()
545 * PWM drivers supporting hardware readout may declare the PWM device in pwm_apply_args()
547 * existing behavior, where all PWM devices are declared as disabled in pwm_apply_args()
552 * the PWM device and set the reference period and polarity config. in pwm_apply_args()
554 * Note that PWM users requiring a smooth handover between the in pwm_apply_args()
556 * PWM devices) will have to switch to the atomic API and avoid calling in pwm_apply_args()
561 state.polarity = pwm->args.polarity; in pwm_apply_args()
562 state.period = pwm->args.period; in pwm_apply_args()
564 pwm_apply_state(pwm, &state); in pwm_apply_args()