2 * gpio_backlight.c - Simple GPIO-controlled backlight
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.
9 #include <linux/backlight.h>
10 #include <linux/err.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>
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>
23 struct gpio_backlight
{
27 struct gpio_desc
*gpiod
;
31 static int gpio_backlight_update_status(struct backlight_device
*bl
)
33 struct gpio_backlight
*gbl
= bl_get_data(bl
);
34 int brightness
= bl
->props
.brightness
;
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
))
41 gpiod_set_value_cansleep(gbl
->gpiod
, brightness
);
46 static int gpio_backlight_check_fb(struct backlight_device
*bl
,
49 struct gpio_backlight
*gbl
= bl_get_data(bl
);
51 return gbl
->fbdev
== NULL
|| gbl
->fbdev
== info
->dev
;
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
,
60 static int gpio_backlight_probe_dt(struct platform_device
*pdev
,
61 struct gpio_backlight
*gbl
)
63 struct device
*dev
= &pdev
->dev
;
64 struct device_node
*np
= dev
->of_node
;
65 enum gpiod_flags flags
;
68 gbl
->def_value
= of_property_read_bool(np
, "default-on");
69 flags
= gbl
->def_value
? GPIOD_OUT_HIGH
: GPIOD_OUT_LOW
;
71 gbl
->gpiod
= devm_gpiod_get(dev
, NULL
, flags
);
72 if (IS_ERR(gbl
->gpiod
)) {
73 ret
= PTR_ERR(gbl
->gpiod
);
75 if (ret
!= -EPROBE_DEFER
) {
77 "Error: The gpios parameter is missing or invalid.\n");
85 static int gpio_backlight_probe(struct platform_device
*pdev
)
87 struct gpio_backlight_platform_data
*pdata
=
88 dev_get_platdata(&pdev
->dev
);
89 struct backlight_properties props
;
90 struct backlight_device
*bl
;
91 struct gpio_backlight
*gbl
;
92 struct device_node
*np
= pdev
->dev
.of_node
;
97 "failed to find platform data or device tree node.\n");
101 gbl
= devm_kzalloc(&pdev
->dev
, sizeof(*gbl
), GFP_KERNEL
);
105 gbl
->dev
= &pdev
->dev
;
108 ret
= gpio_backlight_probe_dt(pdev
, gbl
);
113 * Legacy platform data GPIO retrieveal. Do not expand
114 * the use of this code path, currently only used by one
117 unsigned long flags
= GPIOF_DIR_OUT
;
119 gbl
->fbdev
= pdata
->fbdev
;
120 gbl
->def_value
= pdata
->def_value
;
121 flags
|= gbl
->def_value
? GPIOF_INIT_HIGH
: GPIOF_INIT_LOW
;
123 ret
= devm_gpio_request_one(gbl
->dev
, pdata
->gpio
, flags
,
124 pdata
? pdata
->name
: "backlight");
126 dev_err(&pdev
->dev
, "unable to request GPIO\n");
129 gbl
->gpiod
= gpio_to_desc(pdata
->gpio
);
134 memset(&props
, 0, sizeof(props
));
135 props
.type
= BACKLIGHT_RAW
;
136 props
.max_brightness
= 1;
137 bl
= devm_backlight_device_register(&pdev
->dev
, dev_name(&pdev
->dev
),
138 &pdev
->dev
, gbl
, &gpio_backlight_ops
,
141 dev_err(&pdev
->dev
, "failed to register backlight\n");
145 bl
->props
.brightness
= gbl
->def_value
;
146 backlight_update_status(bl
);
148 platform_set_drvdata(pdev
, bl
);
153 static struct of_device_id gpio_backlight_of_match
[] = {
154 { .compatible
= "gpio-backlight" },
158 MODULE_DEVICE_TABLE(of
, gpio_backlight_of_match
);
161 static struct platform_driver gpio_backlight_driver
= {
163 .name
= "gpio-backlight",
164 .of_match_table
= of_match_ptr(gpio_backlight_of_match
),
166 .probe
= gpio_backlight_probe
,
169 module_platform_driver(gpio_backlight_driver
);
171 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
172 MODULE_DESCRIPTION("GPIO-based Backlight Driver");
173 MODULE_LICENSE("GPL");
174 MODULE_ALIAS("platform:gpio-backlight");