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