brcm2708: update linux 4.4 patches to latest version
[openwrt/openwrt.git] / target / linux / brcm2708 / patches-4.4 / 0444-drm-panel-Add-support-for-the-Raspberry-Pi-7-Touchsc.patch
1 From 4557de6d23737cf44fa5ddf1379be59d4bca2df7 Mon Sep 17 00:00:00 2001
2 From: Eric Anholt <eric@anholt.net>
3 Date: Tue, 26 Apr 2016 13:46:13 -0700
4 Subject: [PATCH] drm/panel: Add support for the Raspberry Pi 7" Touchscreen.
5
6 Signed-off-by: Eric Anholt <eric@anholt.net>
7 ---
8 drivers/gpu/drm/panel/Kconfig | 9 +
9 drivers/gpu/drm/panel/Makefile | 1 +
10 .../gpu/drm/panel/panel-raspberrypi-touchscreen.c | 347 +++++++++++++++++++++
11 3 files changed, 357 insertions(+)
12 create mode 100644 drivers/gpu/drm/panel/panel-raspberrypi-touchscreen.c
13
14 --- a/drivers/gpu/drm/panel/Kconfig
15 +++ b/drivers/gpu/drm/panel/Kconfig
16 @@ -31,6 +31,15 @@ config DRM_PANEL_LG_LG4573
17 Say Y here if you want to enable support for LG4573 RGB panel.
18 To compile this driver as a module, choose M here.
19
20 +config DRM_PANEL_RASPBERRYPI_TOUCHSCREEN
21 + tristate "Raspberry Pi 7-inch touchscreen panel"
22 + depends on DRM_MIPI_DSI
23 + depends on BACKLIGHT_CLASS_DEVICE
24 + help
25 + Say Y here if you want to enable support for the Raspberry
26 + Pi 7" Touchscreen. To compile this driver as a module,
27 + choose M here.
28 +
29 config DRM_PANEL_SAMSUNG_S6E8AA0
30 tristate "Samsung S6E8AA0 DSI video mode panel"
31 depends on OF
32 --- a/drivers/gpu/drm/panel/Makefile
33 +++ b/drivers/gpu/drm/panel/Makefile
34 @@ -1,5 +1,6 @@
35 obj-$(CONFIG_DRM_PANEL_SIMPLE) += panel-simple.o
36 obj-$(CONFIG_DRM_PANEL_LG_LG4573) += panel-lg-lg4573.o
37 +obj-$(CONFIG_DRM_PANEL_RASPBERRYPI_TOUCHSCREEN) += panel-raspberrypi-touchscreen.o
38 obj-$(CONFIG_DRM_PANEL_SAMSUNG_LD9040) += panel-samsung-ld9040.o
39 obj-$(CONFIG_DRM_PANEL_SAMSUNG_S6E8AA0) += panel-samsung-s6e8aa0.o
40 obj-$(CONFIG_DRM_PANEL_SHARP_LQ101R1SX01) += panel-sharp-lq101r1sx01.o
41 --- /dev/null
42 +++ b/drivers/gpu/drm/panel/panel-raspberrypi-touchscreen.c
43 @@ -0,0 +1,347 @@
44 +/*
45 + * Copyright © 2016 Broadcom Limited
46 + *
47 + * This program is free software; you can redistribute it and/or modify
48 + * it under the terms of the GNU General Public License version 2 as
49 + * published by the Free Software Foundation.
50 + *
51 + * Portions of this file (derived from panel-simple.c) are:
52 + *
53 + * Copyright (C) 2013, NVIDIA Corporation. All rights reserved.
54 + *
55 + * Permission is hereby granted, free of charge, to any person obtaining a
56 + * copy of this software and associated documentation files (the "Software"),
57 + * to deal in the Software without restriction, including without limitation
58 + * the rights to use, copy, modify, merge, publish, distribute, sub license,
59 + * and/or sell copies of the Software, and to permit persons to whom the
60 + * Software is furnished to do so, subject to the following conditions:
61 + *
62 + * The above copyright notice and this permission notice (including the
63 + * next paragraph) shall be included in all copies or substantial portions
64 + * of the Software.
65 + *
66 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
67 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
68 + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
69 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
70 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
71 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
72 + * DEALINGS IN THE SOFTWARE.
73 + */
74 +
75 +/**
76 + * DOC: Raspberry Pi 7" touchscreen panel driver.
77 + *
78 + * The 7" touchscreen consists of a DPI LCD panel, a Toshiba
79 + * TC358762XBG DSI-DPI bridge, and an I2C-connected Atmel ATTINY88-MUR
80 + * controlling power management, the LCD PWM, and the touchscreen.
81 + *
82 + * This driver presents this device as a MIPI DSI panel to the DRM
83 + * driver, and should expose the touchscreen as a HID device.
84 + */
85 +
86 +#include <linux/backlight.h>
87 +#include <linux/delay.h>
88 +#include <linux/err.h>
89 +#include <linux/fb.h>
90 +#include <linux/gpio.h>
91 +#include <linux/gpio/consumer.h>
92 +#include <linux/i2c.h>
93 +#include <linux/module.h>
94 +#include <linux/of.h>
95 +#include <linux/of_device.h>
96 +#include <linux/of_graph.h>
97 +#include <linux/pm.h>
98 +
99 +#include <drm/drm_panel.h>
100 +#include <drm/drmP.h>
101 +#include <drm/drm_crtc.h>
102 +#include <drm/drm_mipi_dsi.h>
103 +#include <drm/drm_panel.h>
104 +
105 +struct rpi_touchscreen {
106 + struct drm_panel base;
107 + struct mipi_dsi_device *dsi;
108 + struct i2c_client *bridge_i2c;
109 + struct backlight_device *backlight;
110 +
111 + bool prepared;
112 + bool enabled;
113 +
114 + /* Version of the firmware on the bridge chip */
115 + int atmel_ver;
116 +};
117 +
118 +static const struct drm_display_mode rpi_touchscreen_modes[] = {
119 + {
120 + /* This is assuming that we'll be running the DSI PLL
121 + * at 2Ghz / 3 (since we only get integer dividers),
122 + * so a pixel clock of 2Ghz / 3 / 8.
123 + */
124 + .clock = 83333,
125 + .hdisplay = 800,
126 + .hsync_start = 800 + 61,
127 + .hsync_end = 800 + 61 + 2,
128 + .htotal = 800 + 61 + 2 + 44,
129 + .vdisplay = 480,
130 + .vsync_start = 480 + 7,
131 + .vsync_end = 480 + 7 + 2,
132 + .vtotal = 480 + 7 + 2 + 21,
133 + .vrefresh = 60,
134 + },
135 +};
136 +
137 +static struct rpi_touchscreen *panel_to_ts(struct drm_panel *panel)
138 +{
139 + return container_of(panel, struct rpi_touchscreen, base);
140 +}
141 +
142 +struct regdump {
143 + const char *reg;
144 + u32 offset;
145 +};
146 +
147 +#define REGDUMP(reg) { #reg, reg }
148 +
149 +static int rpi_touchscreen_disable(struct drm_panel *panel)
150 +{
151 + struct rpi_touchscreen *ts = panel_to_ts(panel);
152 + pr_err("disable\n");
153 +
154 + if (!ts->enabled)
155 + return 0;
156 +
157 + if (ts->backlight) {
158 + ts->backlight->props.power = FB_BLANK_POWERDOWN;
159 + backlight_update_status(ts->backlight);
160 + }
161 +
162 + ts->enabled = false;
163 +
164 + return 0;
165 +}
166 +
167 +static int rpi_touchscreen_unprepare(struct drm_panel *panel)
168 +{
169 + struct rpi_touchscreen *ts = panel_to_ts(panel);
170 +
171 + if (!ts->prepared)
172 + return 0;
173 +
174 + ts->prepared = false;
175 +
176 + return 0;
177 +}
178 +
179 +static int rpi_touchscreen_prepare(struct drm_panel *panel)
180 +{
181 + struct rpi_touchscreen *ts = panel_to_ts(panel);
182 +
183 + if (ts->prepared)
184 + return 0;
185 +
186 + ts->prepared = true;
187 +
188 + return 0;
189 +}
190 +
191 +/*
192 + * Powers on the panel once the DSI link is up.
193 + *
194 + * The TC358762 is run in PLLOFF mode, where it usees the MIPI DSI
195 + * byte clock instead of an external reference clock. This means that
196 + * we need the DSI host to be on and transmitting before we start
197 + * talking to it.
198 + */
199 +static int rpi_touchscreen_enable(struct drm_panel *panel)
200 +{
201 + struct rpi_touchscreen *ts = panel_to_ts(panel);
202 +
203 + if (ts->enabled)
204 + return 0;
205 +
206 + if (ts->backlight) {
207 + ts->backlight->props.power = FB_BLANK_UNBLANK;
208 + backlight_update_status(ts->backlight);
209 + }
210 +
211 + ts->enabled = true;
212 +
213 + return 0;
214 +}
215 +
216 +static int rpi_touchscreen_get_modes(struct drm_panel *panel)
217 +{
218 + struct drm_connector *connector = panel->connector;
219 + struct drm_device *drm = panel->drm;
220 + unsigned int i, num = 0;
221 +
222 + for (i = 0; i < ARRAY_SIZE(rpi_touchscreen_modes); i++) {
223 + const struct drm_display_mode *m = &rpi_touchscreen_modes[i];
224 + struct drm_display_mode *mode;
225 +
226 + mode = drm_mode_duplicate(drm, m);
227 + if (!mode) {
228 + dev_err(drm->dev, "failed to add mode %ux%u@%u\n",
229 + m->hdisplay, m->vdisplay, m->vrefresh);
230 + continue;
231 + }
232 +
233 + mode->type |= DRM_MODE_TYPE_DRIVER;
234 +
235 + if (i == 0)
236 + mode->type |= DRM_MODE_TYPE_PREFERRED;
237 +
238 + drm_mode_set_name(mode);
239 +
240 + drm_mode_probed_add(connector, mode);
241 + num++;
242 + }
243 +
244 + connector->display_info.bpc = 8;
245 + connector->display_info.width_mm = 217; /* XXX */
246 + connector->display_info.height_mm = 136; /* XXX */
247 +
248 + return num;
249 +}
250 +
251 +static int rpi_touchscreen_backlight_update(struct backlight_device *bl)
252 +{
253 + int brightness = bl->props.brightness;
254 +
255 + if (bl->props.power != FB_BLANK_UNBLANK ||
256 + bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
257 + brightness = 0;
258 +
259 + return 0;
260 +}
261 +
262 +static const struct backlight_ops rpi_touchscreen_backlight_ops = {
263 + .update_status = rpi_touchscreen_backlight_update,
264 +};
265 +
266 +static const struct drm_panel_funcs rpi_touchscreen_funcs = {
267 + .disable = rpi_touchscreen_disable,
268 + .unprepare = rpi_touchscreen_unprepare,
269 + .prepare = rpi_touchscreen_prepare,
270 + .enable = rpi_touchscreen_enable,
271 + .get_modes = rpi_touchscreen_get_modes,
272 +};
273 +
274 +static struct i2c_client *rpi_touchscreen_get_i2c(struct device *dev,
275 + const char *name)
276 +{
277 + struct device_node *node;
278 + struct i2c_client *client;
279 +
280 + node = of_parse_phandle(dev->of_node, name, 0);
281 + if (!node)
282 + return ERR_PTR(-ENODEV);
283 +
284 + client = of_find_i2c_device_by_node(node);
285 +
286 + of_node_put(node);
287 +
288 + return client;
289 +}
290 +
291 +static int rpi_touchscreen_dsi_probe(struct mipi_dsi_device *dsi)
292 +{
293 + struct device *dev = &dsi->dev;
294 + struct rpi_touchscreen *ts;
295 + int ret;
296 +
297 + ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
298 + if (!ts)
299 + return -ENOMEM;
300 +
301 + dev_set_drvdata(dev, ts);
302 +
303 + ts->dsi = dsi;
304 + dsi->mode_flags = (MIPI_DSI_MODE_VIDEO |
305 + MIPI_DSI_MODE_VIDEO_SYNC_PULSE);
306 + dsi->format = MIPI_DSI_FMT_RGB888;
307 + dsi->lanes = 1;
308 +
309 + ts->bridge_i2c =
310 + rpi_touchscreen_get_i2c(dev, "raspberrypi,touchscreen-bridge");
311 + if (!ts->bridge_i2c) {
312 + ret = -EPROBE_DEFER;
313 + return ret;
314 + }
315 +
316 +#if 0
317 + ts->backlight =
318 + devm_backlight_device_register(dev,
319 + "raspberrypi-touchscreen-backlight",
320 + dev, ts,
321 + &rpi_touchscreen_backlight_ops,
322 + NULL);
323 + if (IS_ERR(ts->backlight)) {
324 + DRM_ERROR("failed to register backlight\n");
325 + return PTR_ERR(ts->backlight);
326 + }
327 + ts->backlight->props.max_brightness = RPI_TOUCHSCREEN_MAX_BRIGHTNESS;
328 + ts->backlight->props.brightness = RPI_TOUCHSCREEN_MAX_BRIGHTNESS;
329 +#endif
330 +
331 + drm_panel_init(&ts->base);
332 + ts->base.dev = dev;
333 + ts->base.funcs = &rpi_touchscreen_funcs;
334 +
335 + ret = drm_panel_add(&ts->base);
336 + if (ret < 0)
337 + goto err_release_bridge;
338 +
339 + return mipi_dsi_attach(dsi);
340 +
341 +err_release_bridge:
342 + put_device(&ts->bridge_i2c->dev);
343 + return ret;
344 +}
345 +
346 +static int rpi_touchscreen_dsi_remove(struct mipi_dsi_device *dsi)
347 +{
348 + struct device *dev = &dsi->dev;
349 + struct rpi_touchscreen *ts = dev_get_drvdata(dev);
350 + int ret;
351 +
352 + ret = mipi_dsi_detach(dsi);
353 + if (ret < 0) {
354 + dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", ret);
355 + return ret;
356 + }
357 +
358 + drm_panel_detach(&ts->base);
359 + drm_panel_remove(&ts->base);
360 +
361 + put_device(&ts->bridge_i2c->dev);
362 +
363 + return 0;
364 +}
365 +
366 +static void rpi_touchscreen_dsi_shutdown(struct mipi_dsi_device *dsi)
367 +{
368 + /* XXX: poweroff */
369 +}
370 +
371 +static const struct of_device_id rpi_touchscreen_of_match[] = {
372 + { .compatible = "raspberrypi,touchscreen" },
373 + { } /* sentinel */
374 +};
375 +MODULE_DEVICE_TABLE(of, rpi_touchscreen_of_match);
376 +
377 +static struct mipi_dsi_driver rpi_touchscreen_driver = {
378 + .driver = {
379 + .name = "raspberrypi-touchscreen",
380 + .of_match_table = rpi_touchscreen_of_match,
381 + },
382 + .probe = rpi_touchscreen_dsi_probe,
383 + .remove = rpi_touchscreen_dsi_remove,
384 + .shutdown = rpi_touchscreen_dsi_shutdown,
385 +};
386 +module_mipi_dsi_driver(rpi_touchscreen_driver);
387 +
388 +MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
389 +MODULE_DESCRIPTION("Raspberry Pi 7-inch touchscreen driver");
390 +MODULE_LICENSE("GPL v2");