blob: a2eba12e1cb78b36fcfd96a0f38ce98bce2406a2 [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>
12#include <linux/gpio.h>
13#include <linux/init.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
Denis Carikli9a6adb32014-02-27 15:28:33 +010016#include <linux/of.h>
17#include <linux/of_gpio.h>
Laurent Pinchart8b770e32013-07-04 21:13:24 +020018#include <linux/platform_data/gpio_backlight.h>
19#include <linux/platform_device.h>
20#include <linux/slab.h>
21
22struct gpio_backlight {
23 struct device *dev;
24 struct device *fbdev;
25
26 int gpio;
27 int active;
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
41 gpio_set_value(gbl->gpio, brightness ? gbl->active : !gbl->active);
42
43 return 0;
44}
45
46static int gpio_backlight_get_brightness(struct backlight_device *bl)
47{
48 return bl->props.brightness;
49}
50
51static int gpio_backlight_check_fb(struct backlight_device *bl,
52 struct fb_info *info)
53{
54 struct gpio_backlight *gbl = bl_get_data(bl);
55
56 return gbl->fbdev == NULL || gbl->fbdev == info->dev;
57}
58
59static const struct backlight_ops gpio_backlight_ops = {
60 .options = BL_CORE_SUSPENDRESUME,
61 .update_status = gpio_backlight_update_status,
62 .get_brightness = gpio_backlight_get_brightness,
63 .check_fb = gpio_backlight_check_fb,
64};
65
Denis Carikli9a6adb32014-02-27 15:28:33 +010066static int gpio_backlight_probe_dt(struct platform_device *pdev,
67 struct gpio_backlight *gbl)
68{
69 struct device_node *np = pdev->dev.of_node;
70 enum of_gpio_flags gpio_flags;
71
72 gbl->gpio = of_get_gpio_flags(np, 0, &gpio_flags);
73
74 if (!gpio_is_valid(gbl->gpio)) {
75 if (gbl->gpio != -EPROBE_DEFER) {
76 dev_err(&pdev->dev,
77 "Error: The gpios parameter is missing or invalid.\n");
78 }
79 return gbl->gpio;
80 }
81
82 gbl->active = (gpio_flags & OF_GPIO_ACTIVE_LOW) ? 0 : 1;
83
84 gbl->def_value = of_property_read_bool(np, "default-on");
85
86 return 0;
87}
88
Laurent Pinchart8b770e32013-07-04 21:13:24 +020089static int gpio_backlight_probe(struct platform_device *pdev)
90{
Jingoo Hanc512794c2013-11-12 15:09:04 -080091 struct gpio_backlight_platform_data *pdata =
92 dev_get_platdata(&pdev->dev);
Laurent Pinchart8b770e32013-07-04 21:13:24 +020093 struct backlight_properties props;
94 struct backlight_device *bl;
95 struct gpio_backlight *gbl;
Denis Carikli9a6adb32014-02-27 15:28:33 +010096 struct device_node *np = pdev->dev.of_node;
Laurent Pinchart8b770e32013-07-04 21:13:24 +020097 int ret;
98
Denis Carikli9a6adb32014-02-27 15:28:33 +010099 if (!pdata && !np) {
100 dev_err(&pdev->dev,
101 "failed to find platform data or device tree node.\n");
Laurent Pinchart8b770e32013-07-04 21:13:24 +0200102 return -ENODEV;
103 }
104
105 gbl = devm_kzalloc(&pdev->dev, sizeof(*gbl), GFP_KERNEL);
106 if (gbl == NULL)
107 return -ENOMEM;
108
109 gbl->dev = &pdev->dev;
Denis Carikli9a6adb32014-02-27 15:28:33 +0100110
111 if (np) {
112 ret = gpio_backlight_probe_dt(pdev, gbl);
113 if (ret)
114 return ret;
115 } else {
116 gbl->fbdev = pdata->fbdev;
117 gbl->gpio = pdata->gpio;
118 gbl->active = pdata->active_low ? 0 : 1;
119 gbl->def_value = pdata->def_value;
120 }
Laurent Pinchart8b770e32013-07-04 21:13:24 +0200121
122 ret = devm_gpio_request_one(gbl->dev, gbl->gpio, GPIOF_DIR_OUT |
123 (gbl->active ? GPIOF_INIT_LOW
124 : GPIOF_INIT_HIGH),
Denis Carikli9a6adb32014-02-27 15:28:33 +0100125 pdata ? pdata->name : "backlight");
Laurent Pinchart8b770e32013-07-04 21:13:24 +0200126 if (ret < 0) {
127 dev_err(&pdev->dev, "unable to request GPIO\n");
128 return ret;
129 }
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};
154#endif
155
Laurent Pinchart8b770e32013-07-04 21:13:24 +0200156static struct platform_driver gpio_backlight_driver = {
157 .driver = {
158 .name = "gpio-backlight",
159 .owner = THIS_MODULE,
Denis Carikli9a6adb32014-02-27 15:28:33 +0100160 .of_match_table = of_match_ptr(gpio_backlight_of_match),
Laurent Pinchart8b770e32013-07-04 21:13:24 +0200161 },
162 .probe = gpio_backlight_probe,
Laurent Pinchart8b770e32013-07-04 21:13:24 +0200163};
164
165module_platform_driver(gpio_backlight_driver);
166
167MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
168MODULE_DESCRIPTION("GPIO-based Backlight Driver");
169MODULE_LICENSE("GPL");
170MODULE_ALIAS("platform:gpio-backlight");