blob: b9300f3e1ee6397eb6ba768630eb84d5dd98288f [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Laurent Pinchart8b770e32013-07-04 21:13:24 +02002/*
3 * gpio_backlight.c - Simple GPIO-controlled backlight
Laurent Pinchart8b770e32013-07-04 21:13:24 +02004 */
5
6#include <linux/backlight.h>
7#include <linux/err.h>
8#include <linux/fb.h>
Linus Walleijde738902017-05-30 13:48:21 +02009#include <linux/gpio.h> /* Only for legacy support */
10#include <linux/gpio/consumer.h>
Laurent Pinchart8b770e32013-07-04 21:13:24 +020011#include <linux/init.h>
12#include <linux/kernel.h>
13#include <linux/module.h>
Denis Carikli9a6adb32014-02-27 15:28:33 +010014#include <linux/of.h>
15#include <linux/of_gpio.h>
Laurent Pinchart8b770e32013-07-04 21:13:24 +020016#include <linux/platform_data/gpio_backlight.h>
17#include <linux/platform_device.h>
18#include <linux/slab.h>
19
20struct gpio_backlight {
21 struct device *dev;
22 struct device *fbdev;
23
Linus Walleijde738902017-05-30 13:48:21 +020024 struct gpio_desc *gpiod;
Denis Carikli9a6adb32014-02-27 15:28:33 +010025 int def_value;
Laurent Pinchart8b770e32013-07-04 21:13:24 +020026};
27
28static int gpio_backlight_update_status(struct backlight_device *bl)
29{
30 struct gpio_backlight *gbl = bl_get_data(bl);
31 int brightness = bl->props.brightness;
32
33 if (bl->props.power != FB_BLANK_UNBLANK ||
34 bl->props.fb_blank != FB_BLANK_UNBLANK ||
35 bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
36 brightness = 0;
37
Linus Walleij26067062017-05-30 13:48:22 +020038 gpiod_set_value_cansleep(gbl->gpiod, brightness);
Laurent Pinchart8b770e32013-07-04 21:13:24 +020039
40 return 0;
41}
42
Laurent Pinchart8b770e32013-07-04 21:13:24 +020043static int gpio_backlight_check_fb(struct backlight_device *bl,
44 struct fb_info *info)
45{
46 struct gpio_backlight *gbl = bl_get_data(bl);
47
48 return gbl->fbdev == NULL || gbl->fbdev == info->dev;
49}
50
51static const struct backlight_ops gpio_backlight_ops = {
52 .options = BL_CORE_SUSPENDRESUME,
53 .update_status = gpio_backlight_update_status,
Laurent Pinchart8b770e32013-07-04 21:13:24 +020054 .check_fb = gpio_backlight_check_fb,
55};
56
Denis Carikli9a6adb32014-02-27 15:28:33 +010057static int gpio_backlight_probe_dt(struct platform_device *pdev,
58 struct gpio_backlight *gbl)
59{
Linus Walleijde738902017-05-30 13:48:21 +020060 struct device *dev = &pdev->dev;
61 struct device_node *np = dev->of_node;
62 enum gpiod_flags flags;
63 int ret;
Denis Carikli9a6adb32014-02-27 15:28:33 +010064
65 gbl->def_value = of_property_read_bool(np, "default-on");
Linus Walleijde738902017-05-30 13:48:21 +020066 flags = gbl->def_value ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
Linus Walleijde738902017-05-30 13:48:21 +020067
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 }
Denis Carikli9a6adb32014-02-27 15:28:33 +010078
79 return 0;
80}
81
Laurent Pinchart8b770e32013-07-04 21:13:24 +020082static int gpio_backlight_probe(struct platform_device *pdev)
83{
Jingoo Hanc512794c2013-11-12 15:09:04 -080084 struct gpio_backlight_platform_data *pdata =
85 dev_get_platdata(&pdev->dev);
Laurent Pinchart8b770e32013-07-04 21:13:24 +020086 struct backlight_properties props;
87 struct backlight_device *bl;
88 struct gpio_backlight *gbl;
Denis Carikli9a6adb32014-02-27 15:28:33 +010089 struct device_node *np = pdev->dev.of_node;
Laurent Pinchart8b770e32013-07-04 21:13:24 +020090 int ret;
91
Denis Carikli9a6adb32014-02-27 15:28:33 +010092 if (!pdata && !np) {
93 dev_err(&pdev->dev,
94 "failed to find platform data or device tree node.\n");
Laurent Pinchart8b770e32013-07-04 21:13:24 +020095 return -ENODEV;
96 }
97
98 gbl = devm_kzalloc(&pdev->dev, sizeof(*gbl), GFP_KERNEL);
99 if (gbl == NULL)
100 return -ENOMEM;
101
102 gbl->dev = &pdev->dev;
Denis Carikli9a6adb32014-02-27 15:28:33 +0100103
104 if (np) {
105 ret = gpio_backlight_probe_dt(pdev, gbl);
106 if (ret)
107 return ret;
108 } else {
Linus Walleijde738902017-05-30 13:48:21 +0200109 /*
110 * Legacy platform data GPIO retrieveal. Do not expand
111 * the use of this code path, currently only used by one
112 * SH board.
113 */
114 unsigned long flags = GPIOF_DIR_OUT;
115
Denis Carikli9a6adb32014-02-27 15:28:33 +0100116 gbl->fbdev = pdata->fbdev;
Denis Carikli9a6adb32014-02-27 15:28:33 +0100117 gbl->def_value = pdata->def_value;
Linus Walleij26067062017-05-30 13:48:22 +0200118 flags |= gbl->def_value ? GPIOF_INIT_HIGH : GPIOF_INIT_LOW;
Stefan Agner2f4647a2015-10-23 16:44:43 -0700119
Linus Walleijde738902017-05-30 13:48:21 +0200120 ret = devm_gpio_request_one(gbl->dev, pdata->gpio, flags,
121 pdata ? pdata->name : "backlight");
122 if (ret < 0) {
123 dev_err(&pdev->dev, "unable to request GPIO\n");
124 return ret;
125 }
126 gbl->gpiod = gpio_to_desc(pdata->gpio);
127 if (!gbl->gpiod)
128 return -EINVAL;
Laurent Pinchart8b770e32013-07-04 21:13:24 +0200129 }
130
131 memset(&props, 0, sizeof(props));
132 props.type = BACKLIGHT_RAW;
133 props.max_brightness = 1;
Jingoo Hanff472012013-11-12 15:09:16 -0800134 bl = devm_backlight_device_register(&pdev->dev, dev_name(&pdev->dev),
135 &pdev->dev, gbl, &gpio_backlight_ops,
136 &props);
Laurent Pinchart8b770e32013-07-04 21:13:24 +0200137 if (IS_ERR(bl)) {
138 dev_err(&pdev->dev, "failed to register backlight\n");
139 return PTR_ERR(bl);
140 }
141
Denis Carikli9a6adb32014-02-27 15:28:33 +0100142 bl->props.brightness = gbl->def_value;
Laurent Pinchart8b770e32013-07-04 21:13:24 +0200143 backlight_update_status(bl);
144
145 platform_set_drvdata(pdev, bl);
146 return 0;
147}
148
Denis Carikli9a6adb32014-02-27 15:28:33 +0100149#ifdef CONFIG_OF
150static struct of_device_id gpio_backlight_of_match[] = {
151 { .compatible = "gpio-backlight" },
152 { /* sentinel */ }
153};
Arun Bharadwaj4d866722015-04-15 17:21:24 -0700154
155MODULE_DEVICE_TABLE(of, gpio_backlight_of_match);
Denis Carikli9a6adb32014-02-27 15:28:33 +0100156#endif
157
Laurent Pinchart8b770e32013-07-04 21:13:24 +0200158static struct platform_driver gpio_backlight_driver = {
159 .driver = {
160 .name = "gpio-backlight",
Denis Carikli9a6adb32014-02-27 15:28:33 +0100161 .of_match_table = of_match_ptr(gpio_backlight_of_match),
Laurent Pinchart8b770e32013-07-04 21:13:24 +0200162 },
163 .probe = gpio_backlight_probe,
Laurent Pinchart8b770e32013-07-04 21:13:24 +0200164};
165
166module_platform_driver(gpio_backlight_driver);
167
168MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
169MODULE_DESCRIPTION("GPIO-based Backlight Driver");
170MODULE_LICENSE("GPL");
171MODULE_ALIAS("platform:gpio-backlight");