1 From aa455fdc2495cb05b65cc03cc472de43df632c5c Mon Sep 17 00:00:00 2001
2 From: Bartosz Golaszewski <bgolaszewski@baylibre.com>
3 Date: Tue, 22 Oct 2019 10:36:24 +0200
4 Subject: [PATCH] backlight: gpio: Explicitly set the direction of
7 commit 706dc68102bc7421a9e6573d149ab6d769d71cc7 upstream.
9 The GPIO backlight driver currently requests the line 'as is', without
10 acively setting its direction. This can lead to problems: if the line
11 is in input mode by default, we won't be able to drive it later when
12 updating the status and also reading its initial value doesn't make
13 sense for backlight setting.
15 Request the line 'as is' initially, so that we can read its value
16 without affecting it but then change the direction to output explicitly
17 when setting the initial brightness.
19 Also: check the current direction and only read the value if it's output.
21 Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
22 Reviewed-by: Daniel Thompson <daniel.thompson@linaro.org>
23 Signed-off-by: Lee Jones <lee.jones@linaro.org>
25 drivers/video/backlight/gpio_backlight.c | 23 ++++++++++++++++++-----
26 1 file changed, 18 insertions(+), 5 deletions(-)
28 --- a/drivers/video/backlight/gpio_backlight.c
29 +++ b/drivers/video/backlight/gpio_backlight.c
30 @@ -26,9 +26,8 @@ struct gpio_backlight {
34 -static int gpio_backlight_update_status(struct backlight_device *bl)
35 +static int gpio_backlight_get_next_brightness(struct backlight_device *bl)
37 - struct gpio_backlight *gbl = bl_get_data(bl);
38 int brightness = bl->props.brightness;
40 if (bl->props.power != FB_BLANK_UNBLANK ||
41 @@ -36,6 +35,14 @@ static int gpio_backlight_update_status(
42 bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
48 +static int gpio_backlight_update_status(struct backlight_device *bl)
50 + struct gpio_backlight *gbl = bl_get_data(bl);
51 + int brightness = gpio_backlight_get_next_brightness(bl);
53 gpiod_set_value_cansleep(gbl->gpiod, brightness);
56 @@ -86,7 +93,8 @@ static int gpio_backlight_initial_power_
57 return gbl->def_value ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
59 /* if the enable GPIO is disabled, do not enable the backlight */
60 - if (gpiod_get_value_cansleep(gbl->gpiod) == 0)
61 + if (gpiod_get_direction(gbl->gpiod) == 0 &&
62 + gpiod_get_value_cansleep(gbl->gpiod) == 0)
63 return FB_BLANK_POWERDOWN;
65 return FB_BLANK_UNBLANK;
66 @@ -100,7 +108,7 @@ static int gpio_backlight_probe(struct p
67 struct backlight_properties props;
68 struct backlight_device *bl;
69 struct gpio_backlight *gbl;
71 + int ret, init_brightness;
73 gbl = devm_kzalloc(&pdev->dev, sizeof(*gbl), GFP_KERNEL);
75 @@ -153,7 +161,12 @@ static int gpio_backlight_probe(struct p
76 bl->props.power = gpio_backlight_initial_power_state(gbl);
77 bl->props.brightness = 1;
79 - backlight_update_status(bl);
80 + init_brightness = gpio_backlight_get_next_brightness(bl);
81 + ret = gpiod_direction_output(gbl->gpiod, init_brightness);
83 + dev_err(&pdev->dev, "failed to set initial brightness\n");
87 platform_set_drvdata(pdev, bl);