1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * gpio_backlight.c - Simple GPIO-controlled backlight
4  */
5 
6 #include <linux/backlight.h>
7 #include <linux/err.h>
8 #include <linux/fb.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>
14 #include <linux/of.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>
20 
21 struct gpio_backlight {
22 	struct device *dev;
23 	struct device *fbdev;
24 
25 	struct gpio_desc *gpiod;
26 	int def_value;
27 };
28 
29 static int gpio_backlight_update_status(struct backlight_device *bl)
30 {
31 	struct gpio_backlight *gbl = bl_get_data(bl);
32 	int brightness = bl->props.brightness;
33 
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))
37 		brightness = 0;
38 
39 	gpiod_set_value_cansleep(gbl->gpiod, brightness);
40 
41 	return 0;
42 }
43 
44 static int gpio_backlight_check_fb(struct backlight_device *bl,
45 				   struct fb_info *info)
46 {
47 	struct gpio_backlight *gbl = bl_get_data(bl);
48 
49 	return gbl->fbdev == NULL || gbl->fbdev == info->dev;
50 }
51 
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,
56 };
57 
58 static int gpio_backlight_probe_dt(struct platform_device *pdev,
59 				   struct gpio_backlight *gbl)
60 {
61 	struct device *dev = &pdev->dev;
62 	enum gpiod_flags flags;
63 	int ret;
64 
65 	gbl->def_value = device_property_read_bool(dev, "default-on");
66 	flags = gbl->def_value ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
67 
68 	gbl->gpiod = devm_gpiod_get(dev, NULL, flags);
69 	if (IS_ERR(gbl->gpiod)) {
70 		ret = PTR_ERR(gbl->gpiod);
71 
72 		if (ret != -EPROBE_DEFER) {
73 			dev_err(dev,
74 				"Error: The gpios parameter is missing or invalid.\n");
75 		}
76 		return ret;
77 	}
78 
79 	return 0;
80 }
81 
82 static int gpio_backlight_probe(struct platform_device *pdev)
83 {
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;
89 	int ret;
90 
91 	gbl = devm_kzalloc(&pdev->dev, sizeof(*gbl), GFP_KERNEL);
92 	if (gbl == NULL)
93 		return -ENOMEM;
94 
95 	gbl->dev = &pdev->dev;
96 
97 	if (pdev->dev.fwnode) {
98 		ret = gpio_backlight_probe_dt(pdev, gbl);
99 		if (ret)
100 			return ret;
101 	} else if (pdata) {
102 		/*
103 		 * Legacy platform data GPIO retrieveal. Do not expand
104 		 * the use of this code path, currently only used by one
105 		 * SH board.
106 		 */
107 		unsigned long flags = GPIOF_DIR_OUT;
108 
109 		gbl->fbdev = pdata->fbdev;
110 		gbl->def_value = pdata->def_value;
111 		flags |= gbl->def_value ? GPIOF_INIT_HIGH : GPIOF_INIT_LOW;
112 
113 		ret = devm_gpio_request_one(gbl->dev, pdata->gpio, flags,
114 					    pdata ? pdata->name : "backlight");
115 		if (ret < 0) {
116 			dev_err(&pdev->dev, "unable to request GPIO\n");
117 			return ret;
118 		}
119 		gbl->gpiod = gpio_to_desc(pdata->gpio);
120 		if (!gbl->gpiod)
121 			return -EINVAL;
122 	} else {
123 		dev_err(&pdev->dev,
124 			"failed to find platform data or device tree node.\n");
125 		return -ENODEV;
126 	}
127 
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,
133 					&props);
134 	if (IS_ERR(bl)) {
135 		dev_err(&pdev->dev, "failed to register backlight\n");
136 		return PTR_ERR(bl);
137 	}
138 
139 	bl->props.brightness = gbl->def_value;
140 	backlight_update_status(bl);
141 
142 	platform_set_drvdata(pdev, bl);
143 	return 0;
144 }
145 
146 static struct of_device_id gpio_backlight_of_match[] = {
147 	{ .compatible = "gpio-backlight" },
148 	{ /* sentinel */ }
149 };
150 
151 MODULE_DEVICE_TABLE(of, gpio_backlight_of_match);
152 
153 static struct platform_driver gpio_backlight_driver = {
154 	.driver		= {
155 		.name		= "gpio-backlight",
156 		.of_match_table = gpio_backlight_of_match,
157 	},
158 	.probe		= gpio_backlight_probe,
159 };
160 
161 module_platform_driver(gpio_backlight_driver);
162 
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");
167