ath79: add support for COMFAST CF-E130N v2
[openwrt/openwrt.git] / target / linux / bcm27xx / patches-4.19 / 950-0087-rpi_display-add-backlight-driver-and-overlay.patch
1 From 5dcbf6d1aa6e33987082e9c8ac64e74f7b6bad58 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] 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 Add Raspberry Pi firmware driver to the dependencies of backlight driver
13
14 Otherwise the backlight driver fails to build if the firmware
15 loading driver is not in the kernel
16
17 Signed-off-by: Alex Riesen <alexander.riesen@cetitec.com>
18 ---
19 drivers/video/backlight/Kconfig | 7 ++
20 drivers/video/backlight/Makefile | 1 +
21 drivers/video/backlight/rpi_backlight.c | 119 ++++++++++++++++++++++++
22 3 files changed, 127 insertions(+)
23 create mode 100644 drivers/video/backlight/rpi_backlight.c
24
25 --- a/drivers/video/backlight/Kconfig
26 +++ b/drivers/video/backlight/Kconfig
27 @@ -272,6 +272,13 @@ config BACKLIGHT_PWM
28 If you have a LCD backlight adjustable by PWM, say Y to enable
29 this driver.
30
31 +config BACKLIGHT_RPI
32 + tristate "Raspberry Pi display firmware driven backlight"
33 + depends on RASPBERRYPI_FIRMWARE
34 + help
35 + If you have the Raspberry Pi DSI touchscreen display, say Y to
36 + enable the mailbox-controlled backlight driver.
37 +
38 config BACKLIGHT_DA903X
39 tristate "Backlight Driver for DA9030/DA9034 using WLED"
40 depends on PMIC_DA903X
41 --- a/drivers/video/backlight/Makefile
42 +++ b/drivers/video/backlight/Makefile
43 @@ -52,6 +52,7 @@ obj-$(CONFIG_BACKLIGHT_PANDORA) += pand
44 obj-$(CONFIG_BACKLIGHT_PCF50633) += pcf50633-backlight.o
45 obj-$(CONFIG_BACKLIGHT_PM8941_WLED) += pm8941-wled.o
46 obj-$(CONFIG_BACKLIGHT_PWM) += pwm_bl.o
47 +obj-$(CONFIG_BACKLIGHT_RPI) += rpi_backlight.o
48 obj-$(CONFIG_BACKLIGHT_SAHARA) += kb3886_bl.o
49 obj-$(CONFIG_BACKLIGHT_SKY81452) += sky81452-backlight.o
50 obj-$(CONFIG_BACKLIGHT_TOSA) += tosa_bl.o
51 --- /dev/null
52 +++ b/drivers/video/backlight/rpi_backlight.c
53 @@ -0,0 +1,119 @@
54 +/*
55 + * rpi_bl.c - Backlight controller through VPU
56 + *
57 + * This program is free software; you can redistribute it and/or modify
58 + * it under the terms of the GNU General Public License version 2 as
59 + * published by the Free Software Foundation.
60 + */
61 +
62 +#include <linux/backlight.h>
63 +#include <linux/err.h>
64 +#include <linux/fb.h>
65 +#include <linux/gpio.h>
66 +#include <linux/init.h>
67 +#include <linux/kernel.h>
68 +#include <linux/module.h>
69 +#include <linux/of.h>
70 +#include <linux/of_gpio.h>
71 +#include <linux/platform_device.h>
72 +#include <linux/slab.h>
73 +#include <soc/bcm2835/raspberrypi-firmware.h>
74 +
75 +struct rpi_backlight {
76 + struct device *dev;
77 + struct device *fbdev;
78 + struct rpi_firmware *fw;
79 +};
80 +
81 +static int rpi_backlight_update_status(struct backlight_device *bl)
82 +{
83 + struct rpi_backlight *gbl = bl_get_data(bl);
84 + int brightness = bl->props.brightness;
85 + int ret;
86 +
87 + if (bl->props.power != FB_BLANK_UNBLANK ||
88 + bl->props.fb_blank != FB_BLANK_UNBLANK ||
89 + bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
90 + brightness = 0;
91 +
92 + ret = rpi_firmware_property(gbl->fw,
93 + RPI_FIRMWARE_FRAMEBUFFER_SET_BACKLIGHT,
94 + &brightness, sizeof(brightness));
95 + if (ret) {
96 + dev_err(gbl->dev, "Failed to set brightness\n");
97 + return ret;
98 + }
99 +
100 + if (brightness < 0) {
101 + dev_err(gbl->dev, "Backlight change failed\n");
102 + return -EAGAIN;
103 + }
104 +
105 + return 0;
106 +}
107 +
108 +static const struct backlight_ops rpi_backlight_ops = {
109 + .options = BL_CORE_SUSPENDRESUME,
110 + .update_status = rpi_backlight_update_status,
111 +};
112 +
113 +static int rpi_backlight_probe(struct platform_device *pdev)
114 +{
115 + struct backlight_properties props;
116 + struct backlight_device *bl;
117 + struct rpi_backlight *gbl;
118 + struct device_node *fw_node;
119 +
120 + gbl = devm_kzalloc(&pdev->dev, sizeof(*gbl), GFP_KERNEL);
121 + if (gbl == NULL)
122 + return -ENOMEM;
123 +
124 + gbl->dev = &pdev->dev;
125 +
126 + fw_node = of_parse_phandle(pdev->dev.of_node, "firmware", 0);
127 + if (!fw_node) {
128 + dev_err(&pdev->dev, "Missing firmware node\n");
129 + return -ENOENT;
130 + }
131 +
132 + gbl->fw = rpi_firmware_get(fw_node);
133 + if (!gbl->fw)
134 + return -EPROBE_DEFER;
135 +
136 + memset(&props, 0, sizeof(props));
137 + props.type = BACKLIGHT_RAW;
138 + props.max_brightness = 255;
139 + bl = devm_backlight_device_register(&pdev->dev, dev_name(&pdev->dev),
140 + &pdev->dev, gbl, &rpi_backlight_ops,
141 + &props);
142 + if (IS_ERR(bl)) {
143 + dev_err(&pdev->dev, "failed to register backlight\n");
144 + return PTR_ERR(bl);
145 + }
146 +
147 + bl->props.brightness = 255;
148 + backlight_update_status(bl);
149 +
150 + platform_set_drvdata(pdev, bl);
151 + return 0;
152 +}
153 +
154 +static const struct of_device_id rpi_backlight_of_match[] = {
155 + { .compatible = "raspberrypi,rpi-backlight" },
156 + { /* sentinel */ }
157 +};
158 +MODULE_DEVICE_TABLE(of, rpi_backlight_of_match);
159 +
160 +static struct platform_driver rpi_backlight_driver = {
161 + .driver = {
162 + .name = "rpi-backlight",
163 + .of_match_table = of_match_ptr(rpi_backlight_of_match),
164 + },
165 + .probe = rpi_backlight_probe,
166 +};
167 +
168 +module_platform_driver(rpi_backlight_driver);
169 +
170 +MODULE_AUTHOR("Gordon Hollingworth <gordon@raspberrypi.org>");
171 +MODULE_DESCRIPTION("Raspberry Pi mailbox based Backlight Driver");
172 +MODULE_LICENSE("GPL");