From fefa713b1843cf13d3132bfe0cf27710938c5d92 Mon Sep 17 00:00:00 2001 From: Alper Nebi Yasak Date: Thu, 22 Oct 2020 23:49:26 +0300 Subject: video: backlight: Support PWMs without a known period_ns The PWM device provided by Chrome OS EC doesn't really support anything other than setting a relative duty cycle. To support it as a backlight, this patch makes the PWM period optional in the device tree and pretends the valid brightness range is its period_ns. Also adds a sandbox test for a PWM channel that has a fixed period, checking that the resulting duty_cycle matches on a set_config() even if the requested period_ns can't be set. Signed-off-by: Alper Nebi Yasak Reviewed-by: Simon Glass --- include/pwm.h | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'include') diff --git a/include/pwm.h b/include/pwm.h index f9959706ceb..668551e4b8a 100644 --- a/include/pwm.h +++ b/include/pwm.h @@ -17,6 +17,10 @@ struct pwm_ops { /** * set_config() - Set the PWM configuration * + * Change both the PWM device's period and it's duty period if + * possible. Otherwise, set an appropriate duty period that best + * matches the given period_ns / duty_ns ratio for the device. + * * @dev: PWM device to update * @channel: PWM channel to update * @period_ns: PWM period in nanoseconds @@ -51,6 +55,10 @@ struct pwm_ops { /** * pwm_set_config() - Set the PWM configuration * + * Change both the PWM device's period and it's duty period if + * possible. Otherwise, set an appropriate duty period that best + * matches the given period_ns / duty_ns ratio for the device. + * * @dev: PWM device to update * @channel: PWM channel to update * @period_ns: PWM period in nanoseconds -- cgit v1.2.3 From 1b9ee2882e6bd1c17ab2ec86249aa0347af68242 Mon Sep 17 00:00:00 2001 From: Alper Nebi Yasak Date: Thu, 22 Oct 2020 23:49:27 +0300 Subject: pwm: Add a driver for Chrome OS EC PWM This PWM is used in rk3399-gru-bob and rk3399-gru-kevin to control the display brightness. We can only change the duty cycle, so on set_config() we just try to match the duty cycle that dividing duty_ns by period_ns gives us. To disable, we set the duty cycle to zero while keeping the old value for when we want to re-enable it. The cros_ec_set_pwm_duty() function is taken from Depthcharge's cros_ec_set_bl_pwm_duty() but modified to use the generic pwm type. The driver itself is very loosely based on rk_pwm.c for the general pwm driver structure. The devicetree binding file is from Linux, before it was converted to YAML at 5df5a577a6b4 ("dt-bindings: pwm: Convert google,cros-ec-pwm.txt to YAML format") in their repo. Signed-off-by: Alper Nebi Yasak Reviewed-by: Simon Glass --- include/cros_ec.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'include') diff --git a/include/cros_ec.h b/include/cros_ec.h index eddc23d48f8..9396b4d2466 100644 --- a/include/cros_ec.h +++ b/include/cros_ec.h @@ -512,6 +512,19 @@ int cros_ec_efs_verify(struct udevice *dev, enum ec_flash_region region); */ int cros_ec_battery_cutoff(struct udevice *dev, uint8_t flags); +/** + * cros_ec_set_pwm_duty() - Set duty cycle of a generic pwm + * + * Note that duty value needs to be passed to the EC as a 16 bit number + * for increased precision. + * + * @param dev CROS-EC device + * @param index Index of the pwm + * @param duty Desired duty cycle, in 0..EC_PWM_MAX_DUTY range. + * @return 0 if OK, -ve on error + */ +int cros_ec_set_pwm_duty(struct udevice *dev, uint8_t index, uint16_t duty); + /** * cros_ec_read_limit_power() - Check if power is limited by batter/charger * -- cgit v1.2.3