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/consumer.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/of.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>
18 
19 struct gpio_backlight {
20 	struct device *fbdev;
21 	struct gpio_desc *gpiod;
22 };
23 
24 static int gpio_backlight_get_next_brightness(struct backlight_device *bl)
25 {
26 	int brightness = bl->props.brightness;
27 
28 	if (bl->props.power != FB_BLANK_UNBLANK ||
29 	    bl->props.fb_blank != FB_BLANK_UNBLANK ||
30 	    bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
31 		brightness = 0;
32 
33 	return brightness;
34 }
35 
36 static int gpio_backlight_update_status(struct backlight_device *bl)
37 {
38 	struct gpio_backlight *gbl = bl_get_data(bl);
39 	int brightness = gpio_backlight_get_next_brightness(bl);
40 
41 	gpiod_set_value_cansleep(gbl->gpiod, brightness);
42 
43 	return 0;
44 }
45 
46 static int gpio_backlight_check_fb(struct backlight_device *bl,
47 				   struct fb_info *info)
48 {
49 	struct gpio_backlight *gbl = bl_get_data(bl);
50 
51 	return gbl->fbdev == NULL || gbl->fbdev == info->dev;
52 }
53 
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,
58 };
59 
60 static int gpio_backlight_probe(struct platform_device *pdev)
61 {
62 	struct device *dev = &pdev->dev;
63 	struct gpio_backlight_platform_data *pdata = dev_get_platdata(dev);
64 	struct device_node *of_node = dev->of_node;
65 	struct backlight_properties props;
66 	struct backlight_device *bl;
67 	struct gpio_backlight *gbl;
68 	int ret, init_brightness, def_value;
69 
70 	gbl = devm_kzalloc(dev, sizeof(*gbl), GFP_KERNEL);
71 	if (gbl == NULL)
72 		return -ENOMEM;
73 
74 	if (pdata)
75 		gbl->fbdev = pdata->fbdev;
76 
77 	def_value = device_property_read_bool(dev, "default-on");
78 
79 	gbl->gpiod = devm_gpiod_get(dev, NULL, GPIOD_ASIS);
80 	if (IS_ERR(gbl->gpiod)) {
81 		ret = PTR_ERR(gbl->gpiod);
82 		if (ret != -EPROBE_DEFER)
83 			dev_err(dev,
84 				"Error: The gpios parameter is missing or invalid.\n");
85 		return ret;
86 	}
87 
88 	memset(&props, 0, sizeof(props));
89 	props.type = BACKLIGHT_RAW;
90 	props.max_brightness = 1;
91 	bl = devm_backlight_device_register(dev, dev_name(dev), dev, gbl,
92 					    &gpio_backlight_ops, &props);
93 	if (IS_ERR(bl)) {
94 		dev_err(dev, "failed to register backlight\n");
95 		return PTR_ERR(bl);
96 	}
97 
98 	/* Set the initial power state */
99 	if (!of_node || !of_node->phandle)
100 		/* Not booted with device tree or no phandle link to the node */
101 		bl->props.power = def_value ? FB_BLANK_UNBLANK
102 					    : FB_BLANK_POWERDOWN;
103 	else if (gpiod_get_direction(gbl->gpiod) == 0 &&
104 		 gpiod_get_value_cansleep(gbl->gpiod) == 0)
105 		bl->props.power = FB_BLANK_POWERDOWN;
106 	else
107 		bl->props.power = FB_BLANK_UNBLANK;
108 
109 	bl->props.brightness = 1;
110 
111 	init_brightness = gpio_backlight_get_next_brightness(bl);
112 	ret = gpiod_direction_output(gbl->gpiod, init_brightness);
113 	if (ret) {
114 		dev_err(dev, "failed to set initial brightness\n");
115 		return ret;
116 	}
117 
118 	platform_set_drvdata(pdev, bl);
119 	return 0;
120 }
121 
122 static struct of_device_id gpio_backlight_of_match[] = {
123 	{ .compatible = "gpio-backlight" },
124 	{ /* sentinel */ }
125 };
126 
127 MODULE_DEVICE_TABLE(of, gpio_backlight_of_match);
128 
129 static struct platform_driver gpio_backlight_driver = {
130 	.driver		= {
131 		.name		= "gpio-backlight",
132 		.of_match_table = gpio_backlight_of_match,
133 	},
134 	.probe		= gpio_backlight_probe,
135 };
136 
137 module_platform_driver(gpio_backlight_driver);
138 
139 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
140 MODULE_DESCRIPTION("GPIO-based Backlight Driver");
141 MODULE_LICENSE("GPL");
142 MODULE_ALIAS("platform:gpio-backlight");
143