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/consumer.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
14 #include <linux/platform_data/gpio_backlight.h>
15 #include <linux/platform_device.h>
16 #include <linux/property.h>
17 #include <linux/slab.h>
19 struct gpio_backlight {
21 struct gpio_desc *gpiod;
24 static int gpio_backlight_update_status(struct backlight_device *bl)
26 struct gpio_backlight *gbl = bl_get_data(bl);
28 gpiod_set_value_cansleep(gbl->gpiod, backlight_get_brightness(bl));
33 static int gpio_backlight_check_fb(struct backlight_device *bl,
36 struct gpio_backlight *gbl = bl_get_data(bl);
38 return gbl->fbdev == NULL || gbl->fbdev == info->dev;
41 static const struct backlight_ops gpio_backlight_ops = {
42 .options = BL_CORE_SUSPENDRESUME,
43 .update_status = gpio_backlight_update_status,
44 .check_fb = gpio_backlight_check_fb,
47 static int gpio_backlight_probe(struct platform_device *pdev)
49 struct device *dev = &pdev->dev;
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;
57 gbl = devm_kzalloc(dev, sizeof(*gbl), GFP_KERNEL);
62 gbl->fbdev = pdata->fbdev;
64 def_value = device_property_read_bool(dev, "default-on");
66 gbl->gpiod = devm_gpiod_get(dev, NULL, GPIOD_ASIS);
67 if (IS_ERR(gbl->gpiod)) {
68 ret = PTR_ERR(gbl->gpiod);
69 if (ret != -EPROBE_DEFER)
71 "Error: The gpios parameter is missing or invalid.\n");
75 memset(&props, 0, sizeof(props));
76 props.type = BACKLIGHT_RAW;
77 props.max_brightness = 1;
78 bl = devm_backlight_device_register(dev, dev_name(dev), dev, gbl,
79 &gpio_backlight_ops, &props);
81 dev_err(dev, "failed to register backlight\n");
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
90 else if (gpiod_get_direction(gbl->gpiod) == 0 &&
91 gpiod_get_value_cansleep(gbl->gpiod) == 0)
92 bl->props.power = FB_BLANK_POWERDOWN;
94 bl->props.power = FB_BLANK_UNBLANK;
96 bl->props.brightness = 1;
98 init_brightness = backlight_get_brightness(bl);
99 ret = gpiod_direction_output(gbl->gpiod, init_brightness);
101 dev_err(dev, "failed to set initial brightness\n");
105 platform_set_drvdata(pdev, bl);
109 static struct of_device_id gpio_backlight_of_match[] = {
110 { .compatible = "gpio-backlight" },
114 MODULE_DEVICE_TABLE(of, gpio_backlight_of_match);
116 static struct platform_driver gpio_backlight_driver = {
118 .name = "gpio-backlight",
119 .of_match_table = gpio_backlight_of_match,
121 .probe = gpio_backlight_probe,
124 module_platform_driver(gpio_backlight_driver);
127 MODULE_DESCRIPTION("GPIO-based Backlight Driver");
128 MODULE_LICENSE("GPL");
129 MODULE_ALIAS("platform:gpio-backlight");