blob: 6f78d928f054a11e4ab1e5cb1aeb977ee458867c [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * gpio_backlight.c - Simple GPIO-controlled backlight
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004 */
5
6#include <linux/backlight.h>
7#include <linux/err.h>
8#include <linux/fb.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009#include <linux/gpio/consumer.h>
10#include <linux/init.h>
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/of.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000014#include <linux/platform_data/gpio_backlight.h>
15#include <linux/platform_device.h>
David Brazdil0f672f62019-12-10 10:32:29 +000016#include <linux/property.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000017#include <linux/slab.h>
18
19struct gpio_backlight {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020 struct device *fbdev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000021 struct gpio_desc *gpiod;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000022};
23
24static int gpio_backlight_update_status(struct backlight_device *bl)
25{
26 struct gpio_backlight *gbl = bl_get_data(bl);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000027
Olivier Deprez157378f2022-04-04 15:47:50 +020028 gpiod_set_value_cansleep(gbl->gpiod, backlight_get_brightness(bl));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000029
30 return 0;
31}
32
33static int gpio_backlight_check_fb(struct backlight_device *bl,
34 struct fb_info *info)
35{
36 struct gpio_backlight *gbl = bl_get_data(bl);
37
38 return gbl->fbdev == NULL || gbl->fbdev == info->dev;
39}
40
41static 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,
45};
46
Olivier Deprez157378f2022-04-04 15:47:50 +020047static int gpio_backlight_probe(struct platform_device *pdev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000048{
49 struct device *dev = &pdev->dev;
Olivier Deprez157378f2022-04-04 15:47:50 +020050 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;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000056
Olivier Deprez157378f2022-04-04 15:47:50 +020057 gbl = devm_kzalloc(dev, sizeof(*gbl), GFP_KERNEL);
58 if (gbl == NULL)
59 return -ENOMEM;
60
61 if (pdata)
62 gbl->fbdev = pdata->fbdev;
63
64 def_value = device_property_read_bool(dev, "default-on");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000065
David Brazdil0f672f62019-12-10 10:32:29 +000066 gbl->gpiod = devm_gpiod_get(dev, NULL, GPIOD_ASIS);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000067 if (IS_ERR(gbl->gpiod)) {
68 ret = PTR_ERR(gbl->gpiod);
Olivier Deprez157378f2022-04-04 15:47:50 +020069 if (ret != -EPROBE_DEFER)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000070 dev_err(dev,
71 "Error: The gpios parameter is missing or invalid.\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000072 return ret;
73 }
74
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000075 memset(&props, 0, sizeof(props));
76 props.type = BACKLIGHT_RAW;
77 props.max_brightness = 1;
Olivier Deprez157378f2022-04-04 15:47:50 +020078 bl = devm_backlight_device_register(dev, dev_name(dev), dev, gbl,
79 &gpio_backlight_ops, &props);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000080 if (IS_ERR(bl)) {
Olivier Deprez157378f2022-04-04 15:47:50 +020081 dev_err(dev, "failed to register backlight\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000082 return PTR_ERR(bl);
83 }
84
Olivier Deprez157378f2022-04-04 15:47:50 +020085 /* 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
89 : FB_BLANK_POWERDOWN;
90 else if (gpiod_get_direction(gbl->gpiod) == 0 &&
91 gpiod_get_value_cansleep(gbl->gpiod) == 0)
92 bl->props.power = FB_BLANK_POWERDOWN;
93 else
94 bl->props.power = FB_BLANK_UNBLANK;
95
David Brazdil0f672f62019-12-10 10:32:29 +000096 bl->props.brightness = 1;
97
Olivier Deprez157378f2022-04-04 15:47:50 +020098 init_brightness = backlight_get_brightness(bl);
99 ret = gpiod_direction_output(gbl->gpiod, init_brightness);
100 if (ret) {
101 dev_err(dev, "failed to set initial brightness\n");
102 return ret;
103 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000104
105 platform_set_drvdata(pdev, bl);
106 return 0;
107}
108
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000109static struct of_device_id gpio_backlight_of_match[] = {
110 { .compatible = "gpio-backlight" },
111 { /* sentinel */ }
112};
113
114MODULE_DEVICE_TABLE(of, gpio_backlight_of_match);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000115
116static struct platform_driver gpio_backlight_driver = {
117 .driver = {
118 .name = "gpio-backlight",
David Brazdil0f672f62019-12-10 10:32:29 +0000119 .of_match_table = gpio_backlight_of_match,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000120 },
121 .probe = gpio_backlight_probe,
122};
123
124module_platform_driver(gpio_backlight_driver);
125
126MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
127MODULE_DESCRIPTION("GPIO-based Backlight Driver");
128MODULE_LICENSE("GPL");
129MODULE_ALIAS("platform:gpio-backlight");