d016cdaa43b86169a31a85ccfb328a3e6784b2fb
[openwrt/openwrt.git] /
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
5 the GPIO
6
7 commit 706dc68102bc7421a9e6573d149ab6d769d71cc7 upstream.
8
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.
14
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.
18
19 Also: check the current direction and only read the value if it's output.
20
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>
24 ---
25 drivers/video/backlight/gpio_backlight.c | 23 ++++++++++++++++++-----
26 1 file changed, 18 insertions(+), 5 deletions(-)
27
28 --- a/drivers/video/backlight/gpio_backlight.c
29 +++ b/drivers/video/backlight/gpio_backlight.c
30 @@ -26,9 +26,8 @@ struct gpio_backlight {
31 int def_value;
32 };
33
34 -static int gpio_backlight_update_status(struct backlight_device *bl)
35 +static int gpio_backlight_get_next_brightness(struct backlight_device *bl)
36 {
37 - struct gpio_backlight *gbl = bl_get_data(bl);
38 int brightness = bl->props.brightness;
39
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))
43 brightness = 0;
44
45 + return brightness;
46 +}
47 +
48 +static int gpio_backlight_update_status(struct backlight_device *bl)
49 +{
50 + struct gpio_backlight *gbl = bl_get_data(bl);
51 + int brightness = gpio_backlight_get_next_brightness(bl);
52 +
53 gpiod_set_value_cansleep(gbl->gpiod, brightness);
54
55 return 0;
56 @@ -86,7 +93,8 @@ static int gpio_backlight_initial_power_
57 return gbl->def_value ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
58
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;
64
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;
70 - int ret;
71 + int ret, init_brightness;
72
73 gbl = devm_kzalloc(&pdev->dev, sizeof(*gbl), GFP_KERNEL);
74 if (gbl == NULL)
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;
78
79 - backlight_update_status(bl);
80 + init_brightness = gpio_backlight_get_next_brightness(bl);
81 + ret = gpiod_direction_output(gbl->gpiod, init_brightness);
82 + if (ret) {
83 + dev_err(&pdev->dev, "failed to set initial brightness\n");
84 + return ret;
85 + }
86
87 platform_set_drvdata(pdev, bl);
88 return 0;