GNU Linux-libre 4.19.245-gnu1
[releases.git] / drivers / video / backlight / gpio_backlight.c
1 /*
2  * 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.
7  */
8
9 #include <linux/backlight.h>
10 #include <linux/err.h>
11 #include <linux/fb.h>
12 #include <linux/gpio.h> /* Only for legacy support */
13 #include <linux/gpio/consumer.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <linux/of_gpio.h>
19 #include <linux/platform_data/gpio_backlight.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22
23 struct gpio_backlight {
24         struct device *dev;
25         struct device *fbdev;
26
27         struct gpio_desc *gpiod;
28         int def_value;
29 };
30
31 static int gpio_backlight_update_status(struct backlight_device *bl)
32 {
33         struct gpio_backlight *gbl = bl_get_data(bl);
34         int brightness = bl->props.brightness;
35
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);
42
43         return 0;
44 }
45
46 static int gpio_backlight_check_fb(struct backlight_device *bl,
47                                    struct fb_info *info)
48 {
49         struct gpio_backlight *gbl = bl_get_data(bl);
50
51         return gbl->fbdev == NULL || gbl->fbdev == info->dev;
52 }
53
54 static const struct backlight_ops gpio_backlight_ops = {
55         .options        = BL_CORE_SUSPENDRESUME,
56         .update_status  = gpio_backlight_update_status,
57         .check_fb       = gpio_backlight_check_fb,
58 };
59
60 static int gpio_backlight_probe_dt(struct platform_device *pdev,
61                                    struct gpio_backlight *gbl)
62 {
63         struct device *dev = &pdev->dev;
64         struct device_node *np = dev->of_node;
65         int ret;
66
67         gbl->def_value = of_property_read_bool(np, "default-on");
68
69         gbl->gpiod = devm_gpiod_get(dev, NULL, GPIOD_ASIS);
70         if (IS_ERR(gbl->gpiod)) {
71                 ret = PTR_ERR(gbl->gpiod);
72
73                 if (ret != -EPROBE_DEFER) {
74                         dev_err(dev,
75                                 "Error: The gpios parameter is missing or invalid.\n");
76                 }
77                 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;
146         }
147
148         memset(&props, 0, sizeof(props));
149         props.type = BACKLIGHT_RAW;
150         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);
154         if (IS_ERR(bl)) {
155                 dev_err(&pdev->dev, "failed to register backlight\n");
156                 return PTR_ERR(bl);
157         }
158
159         bl->props.power = gpio_backlight_initial_power_state(gbl);
160         bl->props.brightness = 1;
161
162         backlight_update_status(bl);
163
164         platform_set_drvdata(pdev, bl);
165         return 0;
166 }
167
168 #ifdef CONFIG_OF
169 static struct of_device_id gpio_backlight_of_match[] = {
170         { .compatible = "gpio-backlight" },
171         { /* sentinel */ }
172 };
173
174 MODULE_DEVICE_TABLE(of, gpio_backlight_of_match);
175 #endif
176
177 static struct platform_driver gpio_backlight_driver = {
178         .driver         = {
179                 .name           = "gpio-backlight",
180                 .of_match_table = of_match_ptr(gpio_backlight_of_match),
181         },
182         .probe          = gpio_backlight_probe,
183 };
184
185 module_platform_driver(gpio_backlight_driver);
186
187 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
188 MODULE_DESCRIPTION("GPIO-based Backlight Driver");
189 MODULE_LICENSE("GPL");
190 MODULE_ALIAS("platform:gpio-backlight");