1 From a28a55876fa9df72d7e3c1bc0fc2ea4bd65337f0 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 126/454] drm/panel: Add support for the Raspberry Pi 7"
7 This driver communicates with the Atmel microcontroller for sequencing
8 the poweron of the TC358762 DSI-DPI bridge and controlling the
11 The following lines are required in config.txt, to keep the firmware
12 from trying to bash our I2C lines and steal the DSI interrupts:
16 mask_gpu_interrupt1=0x1000
18 This means that the firmware won't power on the panel at boot time (no
19 rainbow) and the touchscreen input won't work. The native input
20 driver for the touchscreen still needs to be written.
22 v2: Set the same default orientation as the closed source firmware
23 used, which is the best for viewing angle.
25 Signed-off-by: Eric Anholt <eric@anholt.net>
27 drivers/gpu/drm/panel/Kconfig | 8 +
28 drivers/gpu/drm/panel/Makefile | 1 +
29 .../drm/panel/panel-raspberrypi-touchscreen.c | 514 ++++++++++++++++++
30 3 files changed, 523 insertions(+)
31 create mode 100644 drivers/gpu/drm/panel/panel-raspberrypi-touchscreen.c
33 --- a/drivers/gpu/drm/panel/Kconfig
34 +++ b/drivers/gpu/drm/panel/Kconfig
35 @@ -73,6 +73,14 @@ config DRM_PANEL_PANASONIC_VVX10F034N00
36 WUXGA (1920x1200) Novatek NT1397-based DSI panel as found in some
39 +config DRM_PANEL_RASPBERRYPI_TOUCHSCREEN
40 + tristate "Raspberry Pi 7-inch touchscreen panel"
41 + depends on DRM_MIPI_DSI
43 + Say Y here if you want to enable support for the Raspberry
44 + Pi 7" Touchscreen. To compile this driver as a module,
47 config DRM_PANEL_SAMSUNG_S6E3HA2
48 tristate "Samsung S6E3HA2 DSI video mode panel"
50 --- a/drivers/gpu/drm/panel/Makefile
51 +++ b/drivers/gpu/drm/panel/Makefile
52 @@ -5,6 +5,7 @@ obj-$(CONFIG_DRM_PANEL_INNOLUX_P079ZCA)
53 obj-$(CONFIG_DRM_PANEL_JDI_LT070ME05000) += panel-jdi-lt070me05000.o
54 obj-$(CONFIG_DRM_PANEL_LG_LG4573) += panel-lg-lg4573.o
55 obj-$(CONFIG_DRM_PANEL_PANASONIC_VVX10F034N00) += panel-panasonic-vvx10f034n00.o
56 +obj-$(CONFIG_DRM_PANEL_RASPBERRYPI_TOUCHSCREEN) += panel-raspberrypi-touchscreen.o
57 obj-$(CONFIG_DRM_PANEL_SAMSUNG_LD9040) += panel-samsung-ld9040.o
58 obj-$(CONFIG_DRM_PANEL_SAMSUNG_S6E3HA2) += panel-samsung-s6e3ha2.o
59 obj-$(CONFIG_DRM_PANEL_SAMSUNG_S6E8AA0) += panel-samsung-s6e8aa0.o
61 +++ b/drivers/gpu/drm/panel/panel-raspberrypi-touchscreen.c
64 + * Copyright © 2016 Broadcom
66 + * This program is free software; you can redistribute it and/or modify
67 + * it under the terms of the GNU General Public License version 2 as
68 + * published by the Free Software Foundation.
70 + * Portions of this file (derived from panel-simple.c) are:
72 + * Copyright (C) 2013, NVIDIA Corporation. All rights reserved.
74 + * Permission is hereby granted, free of charge, to any person obtaining a
75 + * copy of this software and associated documentation files (the "Software"),
76 + * to deal in the Software without restriction, including without limitation
77 + * the rights to use, copy, modify, merge, publish, distribute, sub license,
78 + * and/or sell copies of the Software, and to permit persons to whom the
79 + * Software is furnished to do so, subject to the following conditions:
81 + * The above copyright notice and this permission notice (including the
82 + * next paragraph) shall be included in all copies or substantial portions
85 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
86 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
87 + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
88 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
89 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
90 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
91 + * DEALINGS IN THE SOFTWARE.
95 + * DOC: Raspberry Pi 7" touchscreen panel driver.
97 + * The 7" touchscreen consists of a DPI LCD panel, a Toshiba
98 + * TC358762XBG DSI-DPI bridge, and an I2C-connected Atmel ATTINY88-MUR
99 + * controlling power management, the LCD PWM, and the touchscreen.
101 + * This driver presents this device as a MIPI DSI panel to the DRM
102 + * driver, and should expose the touchscreen as a HID device.
105 +#include <linux/delay.h>
106 +#include <linux/err.h>
107 +#include <linux/fb.h>
108 +#include <linux/gpio.h>
109 +#include <linux/gpio/consumer.h>
110 +#include <linux/i2c.h>
111 +#include <linux/module.h>
112 +#include <linux/of.h>
113 +#include <linux/of_device.h>
114 +#include <linux/of_graph.h>
115 +#include <linux/pm.h>
117 +#include <drm/drm_panel.h>
118 +#include <drm/drmP.h>
119 +#include <drm/drm_crtc.h>
120 +#include <drm/drm_mipi_dsi.h>
121 +#include <drm/drm_panel.h>
123 +/* I2C registers of the Atmel microcontroller. */
126 + REG_PORTA, // BIT(2) for horizontal flip, BIT(3) for vertical flip
146 +/* We only turn the PWM on or off, without varying values. */
147 +#define RPI_TOUCHSCREEN_MAX_BRIGHTNESS 1
149 +/* DSI D-PHY Layer Registers */
150 +#define D0W_DPHYCONTTX 0x0004
151 +#define CLW_DPHYCONTRX 0x0020
152 +#define D0W_DPHYCONTRX 0x0024
153 +#define D1W_DPHYCONTRX 0x0028
154 +#define COM_DPHYCONTRX 0x0038
155 +#define CLW_CNTRL 0x0040
156 +#define D0W_CNTRL 0x0044
157 +#define D1W_CNTRL 0x0048
158 +#define DFTMODE_CNTRL 0x0054
160 +/* DSI PPI Layer Registers */
161 +#define PPI_STARTPPI 0x0104
162 +#define PPI_BUSYPPI 0x0108
163 +#define PPI_LINEINITCNT 0x0110
164 +#define PPI_LPTXTIMECNT 0x0114
165 +//#define PPI_LANEENABLE 0x0134
166 +//#define PPI_TX_RX_TA 0x013C
167 +#define PPI_CLS_ATMR 0x0140
168 +#define PPI_D0S_ATMR 0x0144
169 +#define PPI_D1S_ATMR 0x0148
170 +#define PPI_D0S_CLRSIPOCOUNT 0x0164
171 +#define PPI_D1S_CLRSIPOCOUNT 0x0168
172 +#define CLS_PRE 0x0180
173 +#define D0S_PRE 0x0184
174 +#define D1S_PRE 0x0188
175 +#define CLS_PREP 0x01A0
176 +#define D0S_PREP 0x01A4
177 +#define D1S_PREP 0x01A8
178 +#define CLS_ZERO 0x01C0
179 +#define D0S_ZERO 0x01C4
180 +#define D1S_ZERO 0x01C8
181 +#define PPI_CLRFLG 0x01E0
182 +#define PPI_CLRSIPO 0x01E4
183 +#define HSTIMEOUT 0x01F0
184 +#define HSTIMEOUTENABLE 0x01F4
186 +/* DSI Protocol Layer Registers */
187 +#define DSI_STARTDSI 0x0204
188 +#define DSI_BUSYDSI 0x0208
189 +#define DSI_LANEENABLE 0x0210
190 +# define DSI_LANEENABLE_CLOCK BIT(0)
191 +# define DSI_LANEENABLE_D0 BIT(1)
192 +# define DSI_LANEENABLE_D1 BIT(2)
194 +#define DSI_LANESTATUS0 0x0214
195 +#define DSI_LANESTATUS1 0x0218
196 +#define DSI_INTSTATUS 0x0220
197 +#define DSI_INTMASK 0x0224
198 +#define DSI_INTCLR 0x0228
199 +#define DSI_LPTXTO 0x0230
200 +#define DSI_MODE 0x0260
201 +#define DSI_PAYLOAD0 0x0268
202 +#define DSI_PAYLOAD1 0x026C
203 +#define DSI_SHORTPKTDAT 0x0270
204 +#define DSI_SHORTPKTREQ 0x0274
205 +#define DSI_BTASTA 0x0278
206 +#define DSI_BTACLR 0x027C
208 +/* DSI General Registers */
209 +#define DSIERRCNT 0x0300
210 +#define DSISIGMOD 0x0304
212 +/* DSI Application Layer Registers */
213 +#define APLCTRL 0x0400
214 +#define APLSTAT 0x0404
215 +#define APLERR 0x0408
216 +#define PWRMOD 0x040C
217 +#define RDPKTLN 0x0410
218 +#define PXLFMT 0x0414
219 +#define MEMWRCMD 0x0418
221 +/* LCDC/DPI Host Registers */
222 +#define LCDCTRL 0x0420
224 +#define HDISPR 0x0428
226 +#define VDISPR 0x0430
227 +#define VFUEN 0x0434
229 +/* DBI-B Host Registers */
230 +#define DBIBCTRL 0x0440
232 +/* SPI Master Registers */
233 +#define SPICMR 0x0450
234 +#define SPITCR 0x0454
236 +/* System Controller Registers */
237 +#define SYSSTAT 0x0460
238 +#define SYSCTRL 0x0464
239 +#define SYSPLL1 0x0468
240 +#define SYSPLL2 0x046C
241 +#define SYSPLL3 0x0470
242 +#define SYSPMCTRL 0x047C
244 +/* GPIO Registers */
245 +#define GPIOC 0x0480
246 +#define GPIOO 0x0484
247 +#define GPIOI 0x0488
250 +#define I2CCLKCTRL 0x0490
252 +/* Chip/Rev Registers */
253 +#define IDREG 0x04A0
255 +/* Debug Registers */
256 +#define WCMDQUEUE 0x0500
257 +#define RCMDQUEUE 0x0504
259 +struct rpi_touchscreen {
260 + struct drm_panel base;
261 + struct mipi_dsi_device *dsi;
262 + struct i2c_client *bridge_i2c;
264 + /* Version of the firmware on the bridge chip */
268 +static const struct drm_display_mode rpi_touchscreen_modes[] = {
270 + /* The DSI PLL can only integer divide from the 2Ghz
271 + * PLLD, giving us few choices. We pick a divide by 3
272 + * as our DSI HS clock, giving us a pixel clock of
273 + * that divided by 24 bits. Pad out HFP to get our
274 + * panel to refresh at 60Hz, even if that doesn't
275 + * match the datasheet.
277 +#define PIXEL_CLOCK ((2000000000 / 3) / 24)
279 +#define VTOTAL (480 + 7 + 2 + 21)
283 +#define HFP ((PIXEL_CLOCK / (VTOTAL * VREFRESH)) - (HACT + HSW + HBP))
285 + .clock = PIXEL_CLOCK / 1000,
287 + .hsync_start = HACT + HFP,
288 + .hsync_end = HACT + HFP + HSW,
289 + .htotal = HACT + HFP + HSW + HBP,
291 + .vsync_start = 480 + 7,
292 + .vsync_end = 480 + 7 + 2,
298 +static struct rpi_touchscreen *panel_to_ts(struct drm_panel *panel)
300 + return container_of(panel, struct rpi_touchscreen, base);
303 +static u8 rpi_touchscreen_i2c_read(struct rpi_touchscreen *ts, u8 reg)
305 + return i2c_smbus_read_byte_data(ts->bridge_i2c, reg);
308 +static void rpi_touchscreen_i2c_write(struct rpi_touchscreen *ts,
313 + ret = i2c_smbus_write_byte_data(ts->bridge_i2c, reg, val);
315 + dev_err(&ts->dsi->dev, "I2C write failed: %d\n", ret);
318 +static int rpi_touchscreen_write(struct rpi_touchscreen *ts, u16 reg, u32 val)
321 + /* The firmware uses LP DSI transactions like this to bring up
322 + * the hardware, which should be faster than using I2C to then
323 + * pass to the Toshiba. However, I was unable to get it to
335 + mipi_dsi_dcs_write_buffer(ts->dsi, msg, sizeof(msg));
337 + rpi_touchscreen_i2c_write(ts, REG_WR_ADDRH, reg >> 8);
338 + rpi_touchscreen_i2c_write(ts, REG_WR_ADDRL, reg);
339 + rpi_touchscreen_i2c_write(ts, REG_WRITEH, val >> 8);
340 + rpi_touchscreen_i2c_write(ts, REG_WRITEL, val);
346 +static int rpi_touchscreen_disable(struct drm_panel *panel)
348 + struct rpi_touchscreen *ts = panel_to_ts(panel);
350 + rpi_touchscreen_i2c_write(ts, REG_PWM, 0);
352 + rpi_touchscreen_i2c_write(ts, REG_POWERON, 0);
358 +static int rpi_touchscreen_noop(struct drm_panel *panel)
363 +static int rpi_touchscreen_enable(struct drm_panel *panel)
365 + struct rpi_touchscreen *ts = panel_to_ts(panel);
368 + rpi_touchscreen_i2c_write(ts, REG_POWERON, 1);
369 + /* Wait for nPWRDWN to go low to indicate poweron is done. */
370 + for (i = 0; i < 100; i++) {
371 + if (rpi_touchscreen_i2c_read(ts, REG_PORTB) & 1)
375 + rpi_touchscreen_write(ts, DSI_LANEENABLE,
376 + DSI_LANEENABLE_CLOCK |
377 + DSI_LANEENABLE_D0 |
378 + (ts->dsi->lanes > 1 ? DSI_LANEENABLE_D1 : 0));
379 + rpi_touchscreen_write(ts, PPI_D0S_CLRSIPOCOUNT, 0x05);
380 + rpi_touchscreen_write(ts, PPI_D1S_CLRSIPOCOUNT, 0x05);
381 + rpi_touchscreen_write(ts, PPI_D0S_ATMR, 0x00);
382 + rpi_touchscreen_write(ts, PPI_D1S_ATMR, 0x00);
383 + rpi_touchscreen_write(ts, PPI_LPTXTIMECNT, 0x03);
385 + rpi_touchscreen_write(ts, SPICMR, 0x00);
386 + rpi_touchscreen_write(ts, LCDCTRL, 0x00100150);
387 + rpi_touchscreen_write(ts, SYSCTRL, 0x040f);
390 + rpi_touchscreen_write(ts, PPI_STARTPPI, 0x01);
391 + rpi_touchscreen_write(ts, DSI_STARTDSI, 0x01);
394 + /* Turn on the backlight. */
395 + rpi_touchscreen_i2c_write(ts, REG_PWM, 255);
397 + /* Default to the same orientation as the closed source
398 + * firmware used for the panel. Runtime rotation
399 + * configuration will be supported using VC4's plane
400 + * orientation bits.
402 + rpi_touchscreen_i2c_write(ts, REG_PORTA, BIT(2));
407 +static int rpi_touchscreen_get_modes(struct drm_panel *panel)
409 + struct drm_connector *connector = panel->connector;
410 + struct drm_device *drm = panel->drm;
411 + unsigned int i, num = 0;
413 + for (i = 0; i < ARRAY_SIZE(rpi_touchscreen_modes); i++) {
414 + const struct drm_display_mode *m = &rpi_touchscreen_modes[i];
415 + struct drm_display_mode *mode;
417 + mode = drm_mode_duplicate(drm, m);
419 + dev_err(drm->dev, "failed to add mode %ux%u@%u\n",
420 + m->hdisplay, m->vdisplay, m->vrefresh);
424 + mode->type |= DRM_MODE_TYPE_DRIVER;
427 + mode->type |= DRM_MODE_TYPE_PREFERRED;
429 + drm_mode_set_name(mode);
431 + drm_mode_probed_add(connector, mode);
435 + connector->display_info.bpc = 8;
436 + connector->display_info.width_mm = 154;
437 + connector->display_info.height_mm = 86;
442 +static const struct drm_panel_funcs rpi_touchscreen_funcs = {
443 + .disable = rpi_touchscreen_disable,
444 + .unprepare = rpi_touchscreen_noop,
445 + .prepare = rpi_touchscreen_noop,
446 + .enable = rpi_touchscreen_enable,
447 + .get_modes = rpi_touchscreen_get_modes,
450 +static struct i2c_client *rpi_touchscreen_get_i2c(struct device *dev,
453 + struct device_node *node;
454 + struct i2c_client *client;
456 + node = of_parse_phandle(dev->of_node, name, 0);
458 + return ERR_PTR(-ENODEV);
460 + client = of_find_i2c_device_by_node(node);
467 +static int rpi_touchscreen_dsi_probe(struct mipi_dsi_device *dsi)
469 + struct device *dev = &dsi->dev;
470 + struct rpi_touchscreen *ts;
473 + ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
477 + dev_set_drvdata(dev, ts);
480 + dsi->mode_flags = (MIPI_DSI_MODE_VIDEO |
481 + MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
482 + MIPI_DSI_MODE_LPM);
483 + dsi->format = MIPI_DSI_FMT_RGB888;
487 + rpi_touchscreen_get_i2c(dev, "raspberrypi,touchscreen-bridge");
488 + if (IS_ERR(ts->bridge_i2c)) {
489 + ret = -EPROBE_DEFER;
493 + ver = rpi_touchscreen_i2c_read(ts, REG_ID);
495 + dev_err(dev, "Atmel I2C read failed: %d\n", ver);
507 + dev_err(dev, "Unknown Atmel firmware revision: 0x%02x\n", ver);
511 + /* Turn off at boot, so we can cleanly sequence powering on. */
512 + rpi_touchscreen_i2c_write(ts, REG_POWERON, 0);
514 + drm_panel_init(&ts->base);
515 + ts->base.dev = dev;
516 + ts->base.funcs = &rpi_touchscreen_funcs;
518 + ret = drm_panel_add(&ts->base);
520 + goto err_release_bridge;
522 + return mipi_dsi_attach(dsi);
525 + put_device(&ts->bridge_i2c->dev);
529 +static int rpi_touchscreen_dsi_remove(struct mipi_dsi_device *dsi)
531 + struct device *dev = &dsi->dev;
532 + struct rpi_touchscreen *ts = dev_get_drvdata(dev);
535 + ret = mipi_dsi_detach(dsi);
537 + dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", ret);
541 + drm_panel_detach(&ts->base);
542 + drm_panel_remove(&ts->base);
544 + put_device(&ts->bridge_i2c->dev);
549 +static void rpi_touchscreen_dsi_shutdown(struct mipi_dsi_device *dsi)
551 + struct device *dev = &dsi->dev;
552 + struct rpi_touchscreen *ts = dev_get_drvdata(dev);
554 + rpi_touchscreen_i2c_write(ts, REG_POWERON, 0);
557 +static const struct of_device_id rpi_touchscreen_of_match[] = {
558 + { .compatible = "raspberrypi,touchscreen" },
561 +MODULE_DEVICE_TABLE(of, rpi_touchscreen_of_match);
563 +static struct mipi_dsi_driver rpi_touchscreen_driver = {
565 + .name = "raspberrypi-touchscreen",
566 + .of_match_table = rpi_touchscreen_of_match,
568 + .probe = rpi_touchscreen_dsi_probe,
569 + .remove = rpi_touchscreen_dsi_remove,
570 + .shutdown = rpi_touchscreen_dsi_shutdown,
572 +module_mipi_dsi_driver(rpi_touchscreen_driver);
574 +MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
575 +MODULE_DESCRIPTION("Raspberry Pi 7-inch touchscreen driver");
576 +MODULE_LICENSE("GPL v2");