build/patch/kernel/archive/sunxi-6.1/patches.megous/video-pwm_bl-Allow-to-change-lth_brightness-via-sysfs.patch

128 lines
3.6 KiB
Diff

From fdaa8ec4c1e95d5018c58e2b3dacb79abbb7f7d5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ond=C5=99ej=20Jirman?= <megi@xff.cz>
Date: Mon, 7 Jun 2021 20:27:07 +0200
Subject: [PATCH 203/389] 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 <megi@xff.cz>
---
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 a912d7652093..cf759c6562ed 100644
--- a/drivers/video/backlight/pwm_bl.c
+++ b/drivers/video/backlight/pwm_bl.c
@@ -449,6 +449,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);
@@ -458,6 +513,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;
@@ -599,8 +655,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",
+ &lth_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