diff options
author | Ahmad Fatoum <ahmad@a3f.at> | 2021-01-31 21:18:45 +0100 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2021-02-08 11:33:12 +0100 |
commit | 251a5988e58b9b5edbf4bfe2363087a4f07e6176 (patch) | |
tree | e410e866716a653ec56c397c972ef24468f91c12 /include/pwm.h | |
parent | b9c593face025942da2336156ccde46821aec1b7 (diff) | |
download | barebox-251a5988e58b9b5edbf4bfe2363087a4f07e6176.tar.gz |
sound: add PWM beeper support
This driver can be used to drive a piezo-buzzer attached to a PWM.
Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'include/pwm.h')
-rw-r--r-- | include/pwm.h | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/include/pwm.h b/include/pwm.h index b67ab13d2e..2bd59fb8d3 100644 --- a/include/pwm.h +++ b/include/pwm.h @@ -3,6 +3,7 @@ #define __PWM_H #include <dt-bindings/pwm/pwm.h> +#include <errno.h> struct pwm_device; struct device_d; @@ -63,6 +64,38 @@ void pwm_disable(struct pwm_device *pwm); unsigned int pwm_get_period(struct pwm_device *pwm); +/** + * pwm_set_relative_duty_cycle() - Set a relative duty cycle value + * @state: PWM state to fill + * @duty_cycle: relative duty cycle value + * @scale: scale in which @duty_cycle is expressed + * + * This functions converts a relative into an absolute duty cycle (expressed + * in nanoseconds), and puts the result in state->duty_cycle. + * + * For example if you want to configure a 50% duty cycle, call: + * + * pwm_init_state(pwm, &state); + * pwm_set_relative_duty_cycle(&state, 50, 100); + * pwm_apply_state(pwm, &state); + * + * This functions returns -EINVAL if @duty_cycle and/or @scale are + * inconsistent (@scale == 0 or @duty_cycle > @scale). + */ +static inline int +pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle, + unsigned int scale) +{ + if (!scale || duty_cycle > scale) + return -EINVAL; + + state->duty_ns = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle * + state->period_ns, + scale); + + return 0; +} + struct pwm_chip; /** |