From 62689ec6d3317abdc76cf791d5ba0af87811f3c7 Mon Sep 17 00:00:00 2001 From: Ondrej Jirman Date: Mon, 7 Jun 2021 20:27:07 +0200 Subject: [PATCH 327/478] video: pwm_bl: Allow to change lth_brightness via sysfs Read minimum duty cycle in % from DT, and allow to change it via sysfs. Minimum brightness calibration instructions: echo 1 > /sys/class/backlight/backlight/brightness echo 1 > /sys/class/backlight/backlight/device/lth_brightness echo 2 > /sys/class/backlight/backlight/device/lth_brightness echo 3 > /sys/class/backlight/backlight/device/lth_brightness .... increase values until you like the minimum brightness Then make sure to restore this value after each boot by writing it to /sys/class/backlight/backlight/device/lth_brightness Signed-off-by: Ondrej Jirman --- drivers/video/backlight/pwm_bl.c | 72 +++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/drivers/video/backlight/pwm_bl.c b/drivers/video/backlight/pwm_bl.c index fff92ccd1b5a..0c6d3be8c204 100644 --- a/drivers/video/backlight/pwm_bl.c +++ b/drivers/video/backlight/pwm_bl.c @@ -450,6 +450,61 @@ static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb) return active ? FB_BLANK_UNBLANK: FB_BLANK_POWERDOWN; } +static ssize_t lth_brightness_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct backlight_device *bl = platform_get_drvdata(to_platform_device(dev)); + struct pwm_bl_data *pb = bl ? bl_get_data(bl) : NULL; + struct pwm_state state; + unsigned val; + + if (!pb) + return -EBUSY; + + pwm_get_state(pb->pwm, &state); + val = div_u64(100 * pb->lth_brightness, state.period); + + return scnprintf(buf, PAGE_SIZE, "%u\n", val); +} + +static ssize_t lth_brightness_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t len) +{ + struct backlight_device *bl = platform_get_drvdata(to_platform_device(dev)); + struct pwm_bl_data *pb = bl ? bl_get_data(bl) : NULL; + struct pwm_state state; + unsigned val; + int ret; + + if (!pb) + return -EBUSY; + + ret = kstrtouint(buf, 10, &val); + if (ret) + return ret; + + if (val > 100) + val = 100; + + pwm_get_state(pb->pwm, &state); + pb->lth_brightness = div_u64(val * state.period, 100); + pwm_backlight_update_status(bl); + + return len; +} + +static DEVICE_ATTR_RW(lth_brightness); + +static struct attribute *pwm_bl_attrs[] = { + &dev_attr_lth_brightness.attr, + NULL, +}; + +static const struct attribute_group pwm_bl_group = { + .attrs = pwm_bl_attrs, +}; + static int pwm_backlight_probe(struct platform_device *pdev) { struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev); @@ -459,6 +514,7 @@ static int pwm_backlight_probe(struct platform_device *pdev) struct device_node *node = pdev->dev.of_node; struct pwm_bl_data *pb; struct pwm_state state, state_real; + u32 lth_brightness; unsigned int i; int ret; @@ -600,8 +656,20 @@ static int pwm_backlight_probe(struct platform_device *pdev) pb->scale = data->max_brightness; } - pb->lth_brightness = data->lth_brightness * (div_u64(state.period, - pb->scale)); + pb->lth_brightness = div_u64(data->lth_brightness * state.period, pb->scale); + + ret = of_property_read_u32(pdev->dev.of_node, "lth-brightness", + <h_brightness); + if (ret == 0) { + if (lth_brightness > 100) + lth_brightness = 100; + + pb->lth_brightness = div_u64(lth_brightness * state.period, 100); + } + + ret = devm_device_add_group(&pdev->dev, &pwm_bl_group); + if (ret) + goto err_alloc; props.type = BACKLIGHT_RAW; props.max_brightness = data->max_brightness; -- 2.35.3