blob: e470da95d806fb27f63cd647e65ddbcfd3057a43 [file] [log] [blame]
Laurent Pinchart8b770e32013-07-04 21:13:24 +02001/*
2 * gpio_backlight.c - Simple GPIO-controlled backlight
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include <linux/backlight.h>
10#include <linux/err.h>
11#include <linux/fb.h>
Linus Walleijde738902017-05-30 13:48:21 +020012#include <linux/gpio.h> /* Only for legacy support */
13#include <linux/gpio/consumer.h>
Laurent Pinchart8b770e32013-07-04 21:13:24 +020014#include <linux/init.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
Denis Carikli9a6adb32014-02-27 15:28:33 +010017#include <linux/of.h>
18#include <linux/of_gpio.h>
Laurent Pinchart8b770e32013-07-04 21:13:24 +020019#include <linux/platform_data/gpio_backlight.h>
20#include <linux/platform_device.h>
21#include <linux/slab.h>
22
23struct gpio_backlight {
24 struct device *dev;
25 struct device *fbdev;
26
Linus Walleijde738902017-05-30 13:48:21 +020027 struct gpio_desc *gpiod;
Denis Carikli9a6adb32014-02-27 15:28:33 +010028 int def_value;
Laurent Pinchart8b770e32013-07-04 21:13:24 +020029};
30
31static int gpio_backlight_update_status(struct backlight_device *bl)
32{
33 struct gpio_backlight *gbl = bl_get_data(bl);
34 int brightness = bl->props.brightness;
35
36 if (bl->props.power != FB_BLANK_UNBLANK ||
37 bl->props.fb_blank != FB_BLANK_UNBLANK ||
38 bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
39 brightness = 0;
40
Linus Walleij26067062017-05-30 13:48:22 +020041 gpiod_set_value_cansleep(gbl->gpiod, brightness);
Laurent Pinchart8b770e32013-07-04 21:13:24 +020042
43 return 0;
44}
45
Laurent Pinchart8b770e32013-07-04 21:13:24 +020046static 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
54static const struct backlight_ops gpio_backlight_ops = {
55 .options = BL_CORE_SUSPENDRESUME,
56 .update_status = gpio_backlight_update_status,
Laurent Pinchart8b770e32013-07-04 21:13:24 +020057 .check_fb = gpio_backlight_check_fb,
58};
59
Denis Carikli9a6adb32014-02-27 15:28:33 +010060static int gpio_backlight_probe_dt(struct platform_device *pdev,
61 struct gpio_backlight *gbl)
62{
Linus Walleijde738902017-05-30 13:48:21 +020063 struct device *dev = &pdev->dev;
64 struct device_node *np = dev->of_node;
65 enum gpiod_flags flags;
66 int ret;
Denis Carikli9a6adb32014-02-27 15:28:33 +010067
68 gbl->def_value = of_property_read_bool(np, "default-on");
Linus Walleijde738902017-05-30 13:48:21 +020069 flags = gbl->def_value ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
Linus Walleijde738902017-05-30 13:48:21 +020070
71 gbl->gpiod = devm_gpiod_get(dev, NULL, flags);
72 if (IS_ERR(gbl->gpiod)) {
73 ret = PTR_ERR(gbl->gpiod);
74
75 if (ret != -EPROBE_DEFER) {
76 dev_err(dev,
77 "Error: The gpios parameter is missing or invalid.\n");
78 }
79 return ret;
80 }
Denis Carikli9a6adb32014-02-27 15:28:33 +010081
82 return 0;
83}
84
Laurent Pinchart8b770e32013-07-04 21:13:24 +020085static int gpio_backlight_probe(struct platform_device *pdev)
86{
Jingoo Hanc512794c2013-11-12 15:09:04 -080087 struct gpio_backlight_platform_data *pdata =
88 dev_get_platdata(&pdev->dev);
Laurent Pinchart8b770e32013-07-04 21:13:24 +020089 struct backlight_properties props;
90 struct backlight_device *bl;
91 struct gpio_backlight *gbl;
Denis Carikli9a6adb32014-02-27 15:28:33 +010092 struct device_node *np = pdev->dev.of_node;
Laurent Pinchart8b770e32013-07-04 21:13:24 +020093 int ret;
94
Denis Carikli9a6adb32014-02-27 15:28:33 +010095 if (!pdata && !np) {
96 dev_err(&pdev->dev,
97 "failed to find platform data or device tree node.\n");
Laurent Pinchart8b770e32013-07-04 21:13:24 +020098 return -ENODEV;
99 }
100
101 gbl = devm_kzalloc(&pdev->dev, sizeof(*gbl), GFP_KERNEL);
102 if (gbl == NULL)
103 return -ENOMEM;
104
105 gbl->dev = &pdev->dev;
Denis Carikli9a6adb32014-02-27 15:28:33 +0100106
107 if (np) {
108 ret = gpio_backlight_probe_dt(pdev, gbl);
109 if (ret)
110 return ret;
111 } else {
Linus Walleijde738902017-05-30 13:48:21 +0200112 /*
113 * Legacy platform data GPIO retrieveal. Do not expand
114 * the use of this code path, currently only used by one
115 * SH board.
116 */
117 unsigned long flags = GPIOF_DIR_OUT;
118
Denis Carikli9a6adb32014-02-27 15:28:33 +0100119 gbl->fbdev = pdata->fbdev;
Denis Carikli9a6adb32014-02-27 15:28:33 +0100120 gbl->def_value = pdata->def_value;
Linus Walleij26067062017-05-30 13:48:22 +0200121 flags |= gbl->def_value ? GPIOF_INIT_HIGH : GPIOF_INIT_LOW;
Stefan Agner2f4647a2015-10-23 16:44:43 -0700122
Linus Walleijde738902017-05-30 13:48:21 +0200123 ret = devm_gpio_request_one(gbl->dev, pdata->gpio, flags,
124 pdata ? pdata->name : "backlight");
125 if (ret < 0) {
126 dev_err(&pdev->dev, "unable to request GPIO\n");
127 return ret;
128 }
129 gbl->gpiod = gpio_to_desc(pdata->gpio);
130 if (!gbl->gpiod)
131 return -EINVAL;
Laurent Pinchart8b770e32013-07-04 21:13:24 +0200132 }
133
134 memset(&props, 0, sizeof(props));
135 props.type = BACKLIGHT_RAW;
136 props.max_brightness = 1;
Jingoo Hanff472012013-11-12 15:09:16 -0800137 bl = devm_backlight_device_register(&pdev->dev, dev_name(&pdev->dev),
138 &pdev->dev, gbl, &gpio_backlight_ops,
139 &props);
Laurent Pinchart8b770e32013-07-04 21:13:24 +0200140 if (IS_ERR(bl)) {
141 dev_err(&pdev->dev, "failed to register backlight\n");
142 return PTR_ERR(bl);
143 }
144
Denis Carikli9a6adb32014-02-27 15:28:33 +0100145 bl->props.brightness = gbl->def_value;
Laurent Pinchart8b770e32013-07-04 21:13:24 +0200146 backlight_update_status(bl);
147
148 platform_set_drvdata(pdev, bl);
149 return 0;
150}
151
Denis Carikli9a6adb32014-02-27 15:28:33 +0100152#ifdef CONFIG_OF
153static struct of_device_id gpio_backlight_of_match[] = {
154 { .compatible = "gpio-backlight" },
155 { /* sentinel */ }
156};
Arun Bharadwaj4d866722015-04-15 17:21:24 -0700157
158MODULE_DEVICE_TABLE(of, gpio_backlight_of_match);
Denis Carikli9a6adb32014-02-27 15:28:33 +0100159#endif
160
Laurent Pinchart8b770e32013-07-04 21:13:24 +0200161static struct platform_driver gpio_backlight_driver = {
162 .driver = {
163 .name = "gpio-backlight",
Denis Carikli9a6adb32014-02-27 15:28:33 +0100164 .of_match_table = of_match_ptr(gpio_backlight_of_match),
Laurent Pinchart8b770e32013-07-04 21:13:24 +0200165 },
166 .probe = gpio_backlight_probe,
Laurent Pinchart8b770e32013-07-04 21:13:24 +0200167};
168
169module_platform_driver(gpio_backlight_driver);
170
171MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
172MODULE_DESCRIPTION("GPIO-based Backlight Driver");
173MODULE_LICENSE("GPL");
174MODULE_ALIAS("platform:gpio-backlight");