1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* gpio_backlight.c - Simple GPIO-controlled backlight
*/
#include <linux/backlight.h>
#include <linux/err.h>
#include <linux/fb.h>
#include <linux/gpio/consumer.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_data/gpio_backlight.h>
#include <linux/platform_device.h>
#include <linux/property.h>
#include <linux/slab.h>
struct gpio_backlight {
struct device *dev;
struct device *fbdev;
struct gpio_desc *gpiod;
int def_value;
};
static int gpio_backlight_get_next_brightness(struct backlight_device *bl)
{
int brightness = bl->props.brightness;
if (bl->props.power != FB_BLANK_UNBLANK ||
bl->props.fb_blank != FB_BLANK_UNBLANK ||
bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
brightness = 0;
return brightness;
}
static int gpio_backlight_update_status(struct backlight_device *bl)
{
struct gpio_backlight *gbl = bl_get_data(bl);
int brightness = gpio_backlight_get_next_brightness(bl);
gpiod_set_value_cansleep(gbl->gpiod, brightness);
return 0;
}
static int gpio_backlight_check_fb(struct backlight_device *bl,
struct fb_info *info)
{
struct gpio_backlight *gbl = bl_get_data(bl);
return gbl->fbdev == NULL || gbl->fbdev == info->dev;
}
static const struct backlight_ops gpio_backlight_ops = {
.options = BL_CORE_SUSPENDRESUME,
.update_status = gpio_backlight_update_status,
.check_fb = gpio_backlight_check_fb,
};
static int gpio_backlight_initial_power_state(struct gpio_backlight *gbl)
{
struct device_node *node = gbl->dev->of_node;
/* Not booted with device tree or no phandle link to the node */
if (!node || !node->phandle)
return gbl->def_value ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
/* if the enable GPIO is disabled, do not enable the backlight */
if (gpiod_get_direction(gbl->gpiod) == 0 &&
gpiod_get_value_cansleep(gbl->gpiod) == 0)
return FB_BLANK_POWERDOWN;
return FB_BLANK_UNBLANK;
}
static int gpio_backlight_probe(struct platform_device *pdev)
{
struct gpio_backlight_platform_data *pdata =
dev_get_platdata(&pdev->dev);
struct backlight_properties props;
struct backlight_device *bl;
struct gpio_backlight *gbl;
int ret, init_brightness;
gbl = devm_kzalloc(&pdev->dev, sizeof(*gbl), GFP_KERNEL);
if (gbl == NULL)
return -ENOMEM;
gbl->dev = &pdev->dev;
if (pdata)
gbl->fbdev = pdata->fbdev;
gbl->def_value = device_property_read_bool(&pdev->dev, "default-on");
gbl->gpiod = devm_gpiod_get(&pdev->dev, NULL, GPIOD_ASIS);
if (IS_ERR(gbl->gpiod)) {
ret = PTR_ERR(gbl->gpiod);
if (ret != -EPROBE_DEFER)
dev_err(&pdev->dev,
"Error: The gpios parameter is missing or invalid.\n");
return ret;
}
memset(&props, 0, sizeof(props));
props.type = BACKLIGHT_RAW;
props.max_brightness = 1;
bl = devm_backlight_device_register(&pdev->dev, dev_name(&pdev->dev),
&pdev->dev, gbl, &gpio_backlight_ops,
&props);
if (IS_ERR(bl)) {
dev_err(&pdev->dev, "failed to register backlight\n");
return PTR_ERR(bl);
}
bl->props.power = gpio_backlight_initial_power_state(gbl);
bl->props.brightness = 1;
init_brightness = gpio_backlight_get_next_brightness(bl);
ret = gpiod_direction_output(gbl->gpiod, init_brightness);
if (ret) {
dev_err(&pdev->dev, "failed to set initial brightness\n");
return ret;
}
platform_set_drvdata(pdev, bl);
return 0;
}
static struct of_device_id gpio_backlight_of_match[] = {
{ .compatible = "gpio-backlight" },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, gpio_backlight_of_match);
static struct platform_driver gpio_backlight_driver = {
.driver = {
.name = "gpio-backlight",
.of_match_table = gpio_backlight_of_match,
},
.probe = gpio_backlight_probe,
};
module_platform_driver(gpio_backlight_driver);
MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
MODULE_DESCRIPTION("GPIO-based Backlight Driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:gpio-backlight");
|