1 // SPDX-License-Identifier: GPL-2.0-only
3 * gpio_backlight.c - Simple GPIO-controlled backlight
6 #include <linux/backlight.h>
9 #include <linux/gpio.h> /* Only for legacy support */
10 #include <linux/gpio/consumer.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
15 #include <linux/of_gpio.h>
16 #include <linux/platform_data/gpio_backlight.h>
17 #include <linux/platform_device.h>
18 #include <linux/property.h>
19 #include <linux/slab.h>
21 struct gpio_backlight {
25 struct gpio_desc *gpiod;
29 static int gpio_backlight_update_status(struct backlight_device *bl)
31 struct gpio_backlight *gbl = bl_get_data(bl);
32 int brightness = bl->props.brightness;
34 if (bl->props.power != FB_BLANK_UNBLANK ||
35 bl->props.fb_blank != FB_BLANK_UNBLANK ||
36 bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
39 gpiod_set_value_cansleep(gbl->gpiod, brightness);
44 static int gpio_backlight_check_fb(struct backlight_device *bl,
47 struct gpio_backlight *gbl = bl_get_data(bl);
49 return gbl->fbdev == NULL || gbl->fbdev == info->dev;
52 static const struct backlight_ops gpio_backlight_ops = {
53 .options = BL_CORE_SUSPENDRESUME,
54 .update_status = gpio_backlight_update_status,
55 .check_fb = gpio_backlight_check_fb,
58 static int gpio_backlight_probe_dt(struct platform_device *pdev,
59 struct gpio_backlight *gbl)
61 struct device *dev = &pdev->dev;
62 enum gpiod_flags flags;
65 gbl->def_value = device_property_read_bool(dev, "default-on");
66 flags = gbl->def_value ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
68 gbl->gpiod = devm_gpiod_get(dev, NULL, flags);
69 if (IS_ERR(gbl->gpiod)) {
70 ret = PTR_ERR(gbl->gpiod);
72 if (ret != -EPROBE_DEFER) {
74 "Error: The gpios parameter is missing or invalid.\n");
82 static int gpio_backlight_probe(struct platform_device *pdev)
84 struct gpio_backlight_platform_data *pdata =
85 dev_get_platdata(&pdev->dev);
86 struct backlight_properties props;
87 struct backlight_device *bl;
88 struct gpio_backlight *gbl;
91 gbl = devm_kzalloc(&pdev->dev, sizeof(*gbl), GFP_KERNEL);
95 gbl->dev = &pdev->dev;
97 if (pdev->dev.fwnode) {
98 ret = gpio_backlight_probe_dt(pdev, gbl);
103 * Legacy platform data GPIO retrieveal. Do not expand
104 * the use of this code path, currently only used by one
107 unsigned long flags = GPIOF_DIR_OUT;
109 gbl->fbdev = pdata->fbdev;
110 gbl->def_value = pdata->def_value;
111 flags |= gbl->def_value ? GPIOF_INIT_HIGH : GPIOF_INIT_LOW;
113 ret = devm_gpio_request_one(gbl->dev, pdata->gpio, flags,
114 pdata ? pdata->name : "backlight");
116 dev_err(&pdev->dev, "unable to request GPIO\n");
119 gbl->gpiod = gpio_to_desc(pdata->gpio);
124 "failed to find platform data or device tree node.\n");
128 memset(&props, 0, sizeof(props));
129 props.type = BACKLIGHT_RAW;
130 props.max_brightness = 1;
131 bl = devm_backlight_device_register(&pdev->dev, dev_name(&pdev->dev),
132 &pdev->dev, gbl, &gpio_backlight_ops,
135 dev_err(&pdev->dev, "failed to register backlight\n");
139 bl->props.brightness = gbl->def_value;
140 backlight_update_status(bl);
142 platform_set_drvdata(pdev, bl);
146 static struct of_device_id gpio_backlight_of_match[] = {
147 { .compatible = "gpio-backlight" },
151 MODULE_DEVICE_TABLE(of, gpio_backlight_of_match);
153 static struct platform_driver gpio_backlight_driver = {
155 .name = "gpio-backlight",
156 .of_match_table = gpio_backlight_of_match,
158 .probe = gpio_backlight_probe,
161 module_platform_driver(gpio_backlight_driver);
163 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
164 MODULE_DESCRIPTION("GPIO-based Backlight Driver");
165 MODULE_LICENSE("GPL");
166 MODULE_ALIAS("platform:gpio-backlight");