hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/video/backlight/gpio_backlight.c
....@@ -1,44 +1,31 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * gpio_backlight.c - Simple GPIO-controlled backlight
3
- *
4
- * This program is free software; you can redistribute it and/or modify
5
- * it under the terms of the GNU General Public License version 2 as
6
- * published by the Free Software Foundation.
74 */
85
96 #include <linux/backlight.h>
107 #include <linux/err.h>
118 #include <linux/fb.h>
12
-#include <linux/gpio.h> /* Only for legacy support */
139 #include <linux/gpio/consumer.h>
1410 #include <linux/init.h>
1511 #include <linux/kernel.h>
1612 #include <linux/module.h>
1713 #include <linux/of.h>
18
-#include <linux/of_gpio.h>
1914 #include <linux/platform_data/gpio_backlight.h>
2015 #include <linux/platform_device.h>
16
+#include <linux/property.h>
2117 #include <linux/slab.h>
2218
2319 struct gpio_backlight {
24
- struct device *dev;
2520 struct device *fbdev;
26
-
2721 struct gpio_desc *gpiod;
28
- int def_value;
2922 };
3023
3124 static int gpio_backlight_update_status(struct backlight_device *bl)
3225 {
3326 struct gpio_backlight *gbl = bl_get_data(bl);
34
- int brightness = bl->props.brightness;
3527
36
- if (bl->props.power != FB_BLANK_UNBLANK ||
37
- bl->props.fb_blank != FB_BLANK_UNBLANK ||
38
- bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
39
- brightness = 0;
40
-
41
- gpiod_set_value_cansleep(gbl->gpiod, brightness);
28
+ gpiod_set_value_cansleep(gbl->gpiod, backlight_get_brightness(bl));
4229
4330 return 0;
4431 }
....@@ -48,7 +35,7 @@
4835 {
4936 struct gpio_backlight *gbl = bl_get_data(bl);
5037
51
- return gbl->fbdev == NULL || gbl->fbdev == info->dev;
38
+ return gbl->fbdev == NULL || gbl->fbdev == info->device;
5239 }
5340
5441 static const struct backlight_ops gpio_backlight_ops = {
....@@ -57,127 +44,78 @@
5744 .check_fb = gpio_backlight_check_fb,
5845 };
5946
60
-static int gpio_backlight_probe_dt(struct platform_device *pdev,
61
- struct gpio_backlight *gbl)
47
+static int gpio_backlight_probe(struct platform_device *pdev)
6248 {
6349 struct device *dev = &pdev->dev;
64
- struct device_node *np = dev->of_node;
65
- int ret;
50
+ struct gpio_backlight_platform_data *pdata = dev_get_platdata(dev);
51
+ struct device_node *of_node = dev->of_node;
52
+ struct backlight_properties props;
53
+ struct backlight_device *bl;
54
+ struct gpio_backlight *gbl;
55
+ int ret, init_brightness, def_value;
6656
67
- gbl->def_value = of_property_read_bool(np, "default-on");
57
+ gbl = devm_kzalloc(dev, sizeof(*gbl), GFP_KERNEL);
58
+ if (gbl == NULL)
59
+ return -ENOMEM;
60
+
61
+ if (pdata)
62
+ gbl->fbdev = pdata->fbdev;
63
+
64
+ def_value = device_property_read_bool(dev, "default-on");
6865
6966 gbl->gpiod = devm_gpiod_get(dev, NULL, GPIOD_ASIS);
7067 if (IS_ERR(gbl->gpiod)) {
7168 ret = PTR_ERR(gbl->gpiod);
72
-
73
- if (ret != -EPROBE_DEFER) {
69
+ if (ret != -EPROBE_DEFER)
7470 dev_err(dev,
7571 "Error: The gpios parameter is missing or invalid.\n");
76
- }
7772 return ret;
78
- }
79
-
80
- return 0;
81
-}
82
-
83
-static int gpio_backlight_initial_power_state(struct gpio_backlight *gbl)
84
-{
85
- struct device_node *node = gbl->dev->of_node;
86
-
87
- /* Not booted with device tree or no phandle link to the node */
88
- if (!node || !node->phandle)
89
- return gbl->def_value ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
90
-
91
- /* if the enable GPIO is disabled, do not enable the backlight */
92
- if (gpiod_get_value_cansleep(gbl->gpiod) == 0)
93
- return FB_BLANK_POWERDOWN;
94
-
95
- return FB_BLANK_UNBLANK;
96
-}
97
-
98
-
99
-static int gpio_backlight_probe(struct platform_device *pdev)
100
-{
101
- struct gpio_backlight_platform_data *pdata =
102
- dev_get_platdata(&pdev->dev);
103
- struct backlight_properties props;
104
- struct backlight_device *bl;
105
- struct gpio_backlight *gbl;
106
- struct device_node *np = pdev->dev.of_node;
107
- int ret;
108
-
109
- if (!pdata && !np) {
110
- dev_err(&pdev->dev,
111
- "failed to find platform data or device tree node.\n");
112
- return -ENODEV;
113
- }
114
-
115
- gbl = devm_kzalloc(&pdev->dev, sizeof(*gbl), GFP_KERNEL);
116
- if (gbl == NULL)
117
- return -ENOMEM;
118
-
119
- gbl->dev = &pdev->dev;
120
-
121
- if (np) {
122
- ret = gpio_backlight_probe_dt(pdev, gbl);
123
- if (ret)
124
- return ret;
125
- } else {
126
- /*
127
- * Legacy platform data GPIO retrieveal. Do not expand
128
- * the use of this code path, currently only used by one
129
- * SH board.
130
- */
131
- unsigned long flags = GPIOF_DIR_OUT;
132
-
133
- gbl->fbdev = pdata->fbdev;
134
- gbl->def_value = pdata->def_value;
135
- flags |= gbl->def_value ? GPIOF_INIT_HIGH : GPIOF_INIT_LOW;
136
-
137
- ret = devm_gpio_request_one(gbl->dev, pdata->gpio, flags,
138
- pdata ? pdata->name : "backlight");
139
- if (ret < 0) {
140
- dev_err(&pdev->dev, "unable to request GPIO\n");
141
- return ret;
142
- }
143
- gbl->gpiod = gpio_to_desc(pdata->gpio);
144
- if (!gbl->gpiod)
145
- return -EINVAL;
14673 }
14774
14875 memset(&props, 0, sizeof(props));
14976 props.type = BACKLIGHT_RAW;
15077 props.max_brightness = 1;
151
- bl = devm_backlight_device_register(&pdev->dev, dev_name(&pdev->dev),
152
- &pdev->dev, gbl, &gpio_backlight_ops,
153
- &props);
78
+ bl = devm_backlight_device_register(dev, dev_name(dev), dev, gbl,
79
+ &gpio_backlight_ops, &props);
15480 if (IS_ERR(bl)) {
155
- dev_err(&pdev->dev, "failed to register backlight\n");
81
+ dev_err(dev, "failed to register backlight\n");
15682 return PTR_ERR(bl);
15783 }
15884
159
- bl->props.power = gpio_backlight_initial_power_state(gbl);
85
+ /* Set the initial power state */
86
+ if (!of_node || !of_node->phandle)
87
+ /* Not booted with device tree or no phandle link to the node */
88
+ bl->props.power = def_value ? FB_BLANK_UNBLANK
89
+ : FB_BLANK_POWERDOWN;
90
+ else if (gpiod_get_value_cansleep(gbl->gpiod) == 0)
91
+ bl->props.power = FB_BLANK_POWERDOWN;
92
+ else
93
+ bl->props.power = FB_BLANK_UNBLANK;
94
+
16095 bl->props.brightness = 1;
16196
162
- backlight_update_status(bl);
97
+ init_brightness = backlight_get_brightness(bl);
98
+ ret = gpiod_direction_output(gbl->gpiod, init_brightness);
99
+ if (ret) {
100
+ dev_err(dev, "failed to set initial brightness\n");
101
+ return ret;
102
+ }
163103
164104 platform_set_drvdata(pdev, bl);
165105 return 0;
166106 }
167107
168
-#ifdef CONFIG_OF
169108 static struct of_device_id gpio_backlight_of_match[] = {
170109 { .compatible = "gpio-backlight" },
171110 { /* sentinel */ }
172111 };
173112
174113 MODULE_DEVICE_TABLE(of, gpio_backlight_of_match);
175
-#endif
176114
177115 static struct platform_driver gpio_backlight_driver = {
178116 .driver = {
179117 .name = "gpio-backlight",
180
- .of_match_table = of_match_ptr(gpio_backlight_of_match),
118
+ .of_match_table = gpio_backlight_of_match,
181119 },
182120 .probe = gpio_backlight_probe,
183121 };