f1a7db89b831cc89368581c3686d2d0cc140cc42
[openwrt/staging/dedeckeh.git] / target / linux / brcm2708 / patches-4.4 / 0087-rpi_display-add-backlight-driver-and-overlay.patch
1 From cabbd4561f8a04b087fb867cedb002cd5170375f Mon Sep 17 00:00:00 2001
2 From: P33M <P33M@github.com>
3 Date: Wed, 21 Oct 2015 14:55:21 +0100
4 Subject: [PATCH 087/423] rpi_display: add backlight driver and overlay
5
6 Add a mailbox-driven backlight controller for the Raspberry Pi DSI
7 touchscreen display. Requires updated GPU firmware to recognise the
8 mailbox request.
9
10 Signed-off-by: Gordon Hollingworth <gordon@raspberrypi.org>
11 ---
12 arch/arm/boot/dts/overlays/Makefile | 1 +
13 arch/arm/boot/dts/overlays/README | 6 ++
14 .../boot/dts/overlays/rpi-backlight-overlay.dts | 21 ++++
15 arch/arm/configs/bcm2709_defconfig | 1 +
16 arch/arm/configs/bcmrpi_defconfig | 1 +
17 drivers/video/backlight/Kconfig | 6 ++
18 drivers/video/backlight/Makefile | 1 +
19 drivers/video/backlight/rpi_backlight.c | 119 +++++++++++++++++++++
20 include/soc/bcm2835/raspberrypi-firmware.h | 1 +
21 9 files changed, 157 insertions(+)
22 create mode 100644 arch/arm/boot/dts/overlays/rpi-backlight-overlay.dts
23 create mode 100644 drivers/video/backlight/rpi_backlight.c
24
25 --- a/arch/arm/boot/dts/overlays/Makefile
26 +++ b/arch/arm/boot/dts/overlays/Makefile
27 @@ -38,6 +38,7 @@ dtb-$(RPI_DT_OVERLAYS) += pps-gpio-overl
28 dtb-$(RPI_DT_OVERLAYS) += pwm-overlay.dtb
29 dtb-$(RPI_DT_OVERLAYS) += pwm-2chan-overlay.dtb
30 dtb-$(RPI_DT_OVERLAYS) += raspidac3-overlay.dtb
31 +dtb-$(RPI_DT_OVERLAYS) += rpi-backlight-overlay.dtb
32 dtb-$(RPI_DT_OVERLAYS) += rpi-dac-overlay.dtb
33 dtb-$(RPI_DT_OVERLAYS) += rpi-display-overlay.dtb
34 dtb-$(RPI_DT_OVERLAYS) += rpi-ft5406-overlay.dtb
35 --- a/arch/arm/boot/dts/overlays/README
36 +++ b/arch/arm/boot/dts/overlays/README
37 @@ -462,6 +462,12 @@ Load: dtoverlay=raspidac3
38 Params: <None>
39
40
41 +Name: rpi-backlight
42 +Info: Raspberry Pi official display backlight driver
43 +Load: dtoverlay=rpi-backlight
44 +Params: <None>
45 +
46 +
47 Name: rpi-dac
48 Info: Configures the RPi DAC audio card
49 Load: dtoverlay=rpi-dac
50 --- /dev/null
51 +++ b/arch/arm/boot/dts/overlays/rpi-backlight-overlay.dts
52 @@ -0,0 +1,21 @@
53 +/*
54 + * Devicetree overlay for mailbox-driven Raspberry Pi DSI Display
55 + * backlight controller
56 + */
57 +/dts-v1/;
58 +/plugin/;
59 +
60 +/ {
61 + compatible = "brcm,bcm2708";
62 +
63 + fragment@0 {
64 + target-path = "/";
65 + __overlay__ {
66 + rpi_backlight: rpi_backlight {
67 + compatible = "raspberrypi,rpi-backlight";
68 + firmware = <&firmware>;
69 + status = "okay";
70 + };
71 + };
72 + };
73 +};
74 --- a/arch/arm/configs/bcm2709_defconfig
75 +++ b/arch/arm/configs/bcm2709_defconfig
76 @@ -808,6 +808,7 @@ CONFIG_FB_UDL=m
77 CONFIG_FB_SSD1307=m
78 CONFIG_FB_RPISENSE=m
79 # CONFIG_BACKLIGHT_GENERIC is not set
80 +CONFIG_BACKLIGHT_RPI=m
81 CONFIG_BACKLIGHT_GPIO=m
82 CONFIG_FRAMEBUFFER_CONSOLE=y
83 CONFIG_LOGO=y
84 --- a/arch/arm/configs/bcmrpi_defconfig
85 +++ b/arch/arm/configs/bcmrpi_defconfig
86 @@ -801,6 +801,7 @@ CONFIG_FB_UDL=m
87 CONFIG_FB_SSD1307=m
88 CONFIG_FB_RPISENSE=m
89 # CONFIG_BACKLIGHT_GENERIC is not set
90 +CONFIG_BACKLIGHT_RPI=m
91 CONFIG_BACKLIGHT_GPIO=m
92 CONFIG_FRAMEBUFFER_CONSOLE=y
93 CONFIG_LOGO=y
94 --- a/drivers/video/backlight/Kconfig
95 +++ b/drivers/video/backlight/Kconfig
96 @@ -265,6 +265,12 @@ config BACKLIGHT_PWM
97 If you have a LCD backlight adjustable by PWM, say Y to enable
98 this driver.
99
100 +config BACKLIGHT_RPI
101 + tristate "Raspberry Pi display firmware driven backlight"
102 + help
103 + If you have the Raspberry Pi DSI touchscreen display, say Y to
104 + enable the mailbox-controlled backlight driver.
105 +
106 config BACKLIGHT_DA903X
107 tristate "Backlight Driver for DA9030/DA9034 using WLED"
108 depends on PMIC_DA903X
109 --- a/drivers/video/backlight/Makefile
110 +++ b/drivers/video/backlight/Makefile
111 @@ -50,6 +50,7 @@ obj-$(CONFIG_BACKLIGHT_PANDORA) += pand
112 obj-$(CONFIG_BACKLIGHT_PCF50633) += pcf50633-backlight.o
113 obj-$(CONFIG_BACKLIGHT_PM8941_WLED) += pm8941-wled.o
114 obj-$(CONFIG_BACKLIGHT_PWM) += pwm_bl.o
115 +obj-$(CONFIG_BACKLIGHT_RPI) += rpi_backlight.o
116 obj-$(CONFIG_BACKLIGHT_SAHARA) += kb3886_bl.o
117 obj-$(CONFIG_BACKLIGHT_SKY81452) += sky81452-backlight.o
118 obj-$(CONFIG_BACKLIGHT_TOSA) += tosa_bl.o
119 --- /dev/null
120 +++ b/drivers/video/backlight/rpi_backlight.c
121 @@ -0,0 +1,119 @@
122 +/*
123 + * rpi_bl.c - Backlight controller through VPU
124 + *
125 + * This program is free software; you can redistribute it and/or modify
126 + * it under the terms of the GNU General Public License version 2 as
127 + * published by the Free Software Foundation.
128 + */
129 +
130 +#include <linux/backlight.h>
131 +#include <linux/err.h>
132 +#include <linux/fb.h>
133 +#include <linux/gpio.h>
134 +#include <linux/init.h>
135 +#include <linux/kernel.h>
136 +#include <linux/module.h>
137 +#include <linux/of.h>
138 +#include <linux/of_gpio.h>
139 +#include <linux/platform_device.h>
140 +#include <linux/slab.h>
141 +#include <soc/bcm2835/raspberrypi-firmware.h>
142 +
143 +struct rpi_backlight {
144 + struct device *dev;
145 + struct device *fbdev;
146 + struct rpi_firmware *fw;
147 +};
148 +
149 +static int rpi_backlight_update_status(struct backlight_device *bl)
150 +{
151 + struct rpi_backlight *gbl = bl_get_data(bl);
152 + int brightness = bl->props.brightness;
153 + int ret;
154 +
155 + if (bl->props.power != FB_BLANK_UNBLANK ||
156 + bl->props.fb_blank != FB_BLANK_UNBLANK ||
157 + bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
158 + brightness = 0;
159 +
160 + ret = rpi_firmware_property(gbl->fw,
161 + RPI_FIRMWARE_FRAMEBUFFER_SET_BACKLIGHT,
162 + &brightness, sizeof(brightness));
163 + if (ret) {
164 + dev_err(gbl->dev, "Failed to set brightness\n");
165 + return ret;
166 + }
167 +
168 + if (brightness < 0) {
169 + dev_err(gbl->dev, "Backlight change failed\n");
170 + return -EAGAIN;
171 + }
172 +
173 + return 0;
174 +}
175 +
176 +static const struct backlight_ops rpi_backlight_ops = {
177 + .options = BL_CORE_SUSPENDRESUME,
178 + .update_status = rpi_backlight_update_status,
179 +};
180 +
181 +static int rpi_backlight_probe(struct platform_device *pdev)
182 +{
183 + struct backlight_properties props;
184 + struct backlight_device *bl;
185 + struct rpi_backlight *gbl;
186 + struct device_node *fw_node;
187 +
188 + gbl = devm_kzalloc(&pdev->dev, sizeof(*gbl), GFP_KERNEL);
189 + if (gbl == NULL)
190 + return -ENOMEM;
191 +
192 + gbl->dev = &pdev->dev;
193 +
194 + fw_node = of_parse_phandle(pdev->dev.of_node, "firmware", 0);
195 + if (!fw_node) {
196 + dev_err(&pdev->dev, "Missing firmware node\n");
197 + return -ENOENT;
198 + }
199 +
200 + gbl->fw = rpi_firmware_get(fw_node);
201 + if (!gbl->fw)
202 + return -EPROBE_DEFER;
203 +
204 + memset(&props, 0, sizeof(props));
205 + props.type = BACKLIGHT_RAW;
206 + props.max_brightness = 255;
207 + bl = devm_backlight_device_register(&pdev->dev, dev_name(&pdev->dev),
208 + &pdev->dev, gbl, &rpi_backlight_ops,
209 + &props);
210 + if (IS_ERR(bl)) {
211 + dev_err(&pdev->dev, "failed to register backlight\n");
212 + return PTR_ERR(bl);
213 + }
214 +
215 + bl->props.brightness = 255;
216 + backlight_update_status(bl);
217 +
218 + platform_set_drvdata(pdev, bl);
219 + return 0;
220 +}
221 +
222 +static const struct of_device_id rpi_backlight_of_match[] = {
223 + { .compatible = "raspberrypi,rpi-backlight" },
224 + { /* sentinel */ }
225 +};
226 +MODULE_DEVICE_TABLE(of, rpi_backlight_of_match);
227 +
228 +static struct platform_driver rpi_backlight_driver = {
229 + .driver = {
230 + .name = "rpi-backlight",
231 + .of_match_table = of_match_ptr(rpi_backlight_of_match),
232 + },
233 + .probe = rpi_backlight_probe,
234 +};
235 +
236 +module_platform_driver(rpi_backlight_driver);
237 +
238 +MODULE_AUTHOR("Gordon Hollingworth <gordon@raspberrypi.org>");
239 +MODULE_DESCRIPTION("Raspberry Pi mailbox based Backlight Driver");
240 +MODULE_LICENSE("GPL");
241 --- a/include/soc/bcm2835/raspberrypi-firmware.h
242 +++ b/include/soc/bcm2835/raspberrypi-firmware.h
243 @@ -112,6 +112,7 @@ enum rpi_firmware_property_tag {
244 RPI_FIRMWARE_FRAMEBUFFER_SET_OVERSCAN = 0x0004800a,
245 RPI_FIRMWARE_FRAMEBUFFER_SET_PALETTE = 0x0004800b,
246 RPI_FIRMWARE_FRAMEBUFFER_SET_VSYNC = 0x0004800e,
247 + RPI_FIRMWARE_FRAMEBUFFER_SET_BACKLIGHT = 0x0004800f,
248
249 RPI_FIRMWARE_VCHIQ_INIT = 0x00048010,
250