1 From 7f745c7e13b50911424b9bbb4181d4dc72ef155f Mon Sep 17 00:00:00 2001
2 From: Kevin Quigley <kevin@kquigley.co.uk>
3 Date: Mon, 24 Sep 2018 12:14:41 +0000
4 Subject: [PATCH 4/4] drm/panel: Backport 4.15 support for the Raspberry Pi 7"
8 .../drm/panel/panel-raspberrypi-touchscreen.c | 242 ++++++++----------
9 1 file changed, 111 insertions(+), 131 deletions(-)
11 diff --git a/drivers/gpu/drm/panel/panel-raspberrypi-touchscreen.c b/drivers/gpu/drm/panel/panel-raspberrypi-touchscreen.c
12 index 128523d8d518..2c9c9722734f 100644
13 --- a/drivers/gpu/drm/panel/panel-raspberrypi-touchscreen.c
14 +++ b/drivers/gpu/drm/panel/panel-raspberrypi-touchscreen.c
17 - * Copyright © 2016 Broadcom
18 + * Copyright © 2016-2017 Broadcom
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License version 2 as
26 - * DOC: Raspberry Pi 7" touchscreen panel driver.
27 + * Raspberry Pi 7" touchscreen panel driver.
29 * The 7" touchscreen consists of a DPI LCD panel, a Toshiba
30 * TC358762XBG DSI-DPI bridge, and an I2C-connected Atmel ATTINY88-MUR
31 - * controlling power management, the LCD PWM, and the touchscreen.
32 + * controlling power management, the LCD PWM, and initial register
33 + * setup of the Tohsiba.
35 - * This driver presents this device as a MIPI DSI panel to the DRM
36 - * driver, and should expose the touchscreen as a HID device.
37 + * This driver controls the TC358762 and ATTINY88, presenting a DSI
38 + * device with a drm_panel.
41 #include <linux/delay.h>
43 #include <drm/drm_mipi_dsi.h>
44 #include <drm/drm_panel.h>
46 +#define RPI_DSI_DRIVER_NAME "rpi-ts-dsi"
48 /* I2C registers of the Atmel microcontroller. */
51 - REG_PORTA, // BIT(2) for horizontal flip, BIT(3) for vertical flip
52 + REG_PORTA, /* BIT(2) for horizontal flip, BIT(3) for vertical flip */
56 @@ -81,9 +84,6 @@ enum REG_ADDR {
60 -/* We only turn the PWM on or off, without varying values. */
61 -#define RPI_TOUCHSCREEN_MAX_BRIGHTNESS 1
63 /* DSI D-PHY Layer Registers */
64 #define D0W_DPHYCONTTX 0x0004
65 #define CLW_DPHYCONTRX 0x0020
66 @@ -100,8 +100,6 @@ enum REG_ADDR {
67 #define PPI_BUSYPPI 0x0108
68 #define PPI_LINEINITCNT 0x0110
69 #define PPI_LPTXTIMECNT 0x0114
70 -//#define PPI_LANEENABLE 0x0134
71 -//#define PPI_TX_RX_TA 0x013C
72 #define PPI_CLS_ATMR 0x0140
73 #define PPI_D0S_ATMR 0x0144
74 #define PPI_D1S_ATMR 0x0148
75 @@ -197,43 +195,23 @@ enum REG_ADDR {
76 struct rpi_touchscreen {
77 struct drm_panel base;
78 struct mipi_dsi_device *dsi;
79 - struct i2c_client *bridge_i2c;
81 - /* Version of the firmware on the bridge chip */
83 + struct i2c_client *i2c;
86 static const struct drm_display_mode rpi_touchscreen_modes[] = {
88 - /* The DSI PLL can only integer divide from the 2Ghz
89 - * PLLD, giving us few choices. We pick a divide by 3
90 - * as our DSI HS clock, giving us a pixel clock of
91 - * that divided by 24 bits. Pad out HFP to get our
92 - * panel to refresh at 60Hz, even if that doesn't
93 - * match the datasheet.
95 -#define PIXEL_CLOCK ((2000000000 / 3) / 24)
97 -#define VTOTAL (480 + 7 + 2 + 21)
101 -#define HFP ((PIXEL_CLOCK / (VTOTAL * VREFRESH)) - (HACT + HSW + HBP))
103 - /* Round up the pixel clock a bit (10khz), so that the
104 - * "don't run things faster than the requested clock
105 - * rate" rule of the clk driver doesn't reject the
106 - * divide-by-3 mode due to rounding error.
107 + /* Modeline comes from the Raspberry Pi firmware, with HFP=1
108 + * plugged in and clock re-computed from that.
110 - .clock = PIXEL_CLOCK / 1000 + 10,
112 - .hsync_start = HACT + HFP,
113 - .hsync_end = HACT + HFP + HSW,
114 - .htotal = HACT + HFP + HSW + HBP,
115 + .clock = 25979400 / 1000,
117 + .hsync_start = 800 + 1,
118 + .hsync_end = 800 + 1 + 2,
119 + .htotal = 800 + 1 + 2 + 46,
121 .vsync_start = 480 + 7,
122 .vsync_end = 480 + 7 + 2,
124 + .vtotal = 480 + 7 + 2 + 21,
128 @@ -245,7 +223,7 @@ static struct rpi_touchscreen *panel_to_ts(struct drm_panel *panel)
130 static int rpi_touchscreen_i2c_read(struct rpi_touchscreen *ts, u8 reg)
132 - return i2c_smbus_read_byte_data(ts->bridge_i2c, reg);
133 + return i2c_smbus_read_byte_data(ts->i2c, reg);
136 static void rpi_touchscreen_i2c_write(struct rpi_touchscreen *ts,
137 @@ -253,19 +231,13 @@ static void rpi_touchscreen_i2c_write(struct rpi_touchscreen *ts,
141 - ret = i2c_smbus_write_byte_data(ts->bridge_i2c, reg, val);
142 + ret = i2c_smbus_write_byte_data(ts->i2c, reg, val);
144 dev_err(&ts->dsi->dev, "I2C write failed: %d\n", ret);
147 static int rpi_touchscreen_write(struct rpi_touchscreen *ts, u16 reg, u32 val)
150 - /* The firmware uses LP DSI transactions like this to bring up
151 - * the hardware, which should be faster than using I2C to then
152 - * pass to the Toshiba. However, I was unable to get it to
158 @@ -275,13 +247,7 @@ static int rpi_touchscreen_write(struct rpi_touchscreen *ts, u16 reg, u32 val)
162 - mipi_dsi_dcs_write_buffer(ts->dsi, msg, sizeof(msg));
164 - rpi_touchscreen_i2c_write(ts, REG_WR_ADDRH, reg >> 8);
165 - rpi_touchscreen_i2c_write(ts, REG_WR_ADDRL, reg);
166 - rpi_touchscreen_i2c_write(ts, REG_WRITEH, val >> 8);
167 - rpi_touchscreen_i2c_write(ts, REG_WRITEL, val);
169 + mipi_dsi_generic_write(ts->dsi, msg, sizeof(msg));
173 @@ -317,8 +283,7 @@ static int rpi_touchscreen_enable(struct drm_panel *panel)
175 rpi_touchscreen_write(ts, DSI_LANEENABLE,
176 DSI_LANEENABLE_CLOCK |
177 - DSI_LANEENABLE_D0 |
178 - (ts->dsi->lanes > 1 ? DSI_LANEENABLE_D1 : 0));
179 + DSI_LANEENABLE_D0);
180 rpi_touchscreen_write(ts, PPI_D0S_CLRSIPOCOUNT, 0x05);
181 rpi_touchscreen_write(ts, PPI_D1S_CLRSIPOCOUNT, 0x05);
182 rpi_touchscreen_write(ts, PPI_D0S_ATMR, 0x00);
183 @@ -352,6 +317,7 @@ static int rpi_touchscreen_get_modes(struct drm_panel *panel)
184 struct drm_connector *connector = panel->connector;
185 struct drm_device *drm = panel->drm;
186 unsigned int i, num = 0;
187 + static const u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
189 for (i = 0; i < ARRAY_SIZE(rpi_touchscreen_modes); i++) {
190 const struct drm_display_mode *m = &rpi_touchscreen_modes[i];
191 @@ -378,6 +344,8 @@ static int rpi_touchscreen_get_modes(struct drm_panel *panel)
192 connector->display_info.bpc = 8;
193 connector->display_info.width_mm = 154;
194 connector->display_info.height_mm = 86;
195 + drm_display_info_set_bus_formats(&connector->display_info,
200 @@ -390,51 +358,27 @@ static const struct drm_panel_funcs rpi_touchscreen_funcs = {
201 .get_modes = rpi_touchscreen_get_modes,
204 -static struct i2c_client *rpi_touchscreen_get_i2c(struct device *dev,
207 - struct device_node *node;
208 - struct i2c_client *client;
210 - node = of_parse_phandle(dev->of_node, name, 0);
212 - return ERR_PTR(-ENODEV);
214 - client = of_find_i2c_device_by_node(node);
219 - return ERR_PTR(-EPROBE_DEFER);
224 -static int rpi_touchscreen_dsi_probe(struct mipi_dsi_device *dsi)
225 +static int rpi_touchscreen_probe(struct i2c_client *i2c,
226 + const struct i2c_device_id *id)
228 - struct device *dev = &dsi->dev;
229 + struct device *dev = &i2c->dev;
230 struct rpi_touchscreen *ts;
231 + struct device_node *endpoint, *dsi_host_node;
232 + struct mipi_dsi_host *host;
234 + struct mipi_dsi_device_info info = {
235 + .type = RPI_DSI_DRIVER_NAME,
240 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
244 - dev_set_drvdata(dev, ts);
245 + i2c_set_clientdata(i2c, ts);
248 - dsi->mode_flags = (MIPI_DSI_MODE_VIDEO |
249 - MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
250 - MIPI_DSI_MODE_LPM);
251 - dsi->format = MIPI_DSI_FMT_RGB888;
255 - rpi_touchscreen_get_i2c(dev, "raspberrypi,touchscreen-bridge");
256 - if (IS_ERR(ts->bridge_i2c)) {
257 - ret = -EPROBE_DEFER;
262 ver = rpi_touchscreen_i2c_read(ts, REG_ID);
264 @@ -443,11 +387,8 @@ static int rpi_touchscreen_dsi_probe(struct mipi_dsi_device *dsi)
273 + case 0xde: /* ver 1 */
274 + case 0xc3: /* ver 2 */
277 dev_err(dev, "Unknown Atmel firmware revision: 0x%02x\n", ver);
278 @@ -457,65 +398,104 @@ static int rpi_touchscreen_dsi_probe(struct mipi_dsi_device *dsi)
279 /* Turn off at boot, so we can cleanly sequence powering on. */
280 rpi_touchscreen_i2c_write(ts, REG_POWERON, 0);
282 - drm_panel_init(&ts->base);
283 + /* Look up the DSI host. It needs to probe before we do. */
284 + endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
285 + dsi_host_node = of_graph_get_remote_port_parent(endpoint);
286 + host = of_find_mipi_dsi_host_by_node(dsi_host_node);
287 + of_node_put(dsi_host_node);
289 + of_node_put(endpoint);
290 + return -EPROBE_DEFER;
293 + info.node = of_graph_get_remote_port(endpoint);
294 + of_node_put(endpoint);
296 + ts->dsi = mipi_dsi_device_register_full(host, &info);
297 + if (IS_ERR(ts->dsi)) {
298 + dev_err(dev, "DSI device registration failed: %ld\n",
300 + return PTR_ERR(ts->dsi);
304 ts->base.funcs = &rpi_touchscreen_funcs;
306 + /* This appears last, as it's what will unblock the DSI host
307 + * driver's component bind function.
309 ret = drm_panel_add(&ts->base);
311 - goto err_release_bridge;
313 - return mipi_dsi_attach(dsi);
318 - put_device(&ts->bridge_i2c->dev);
323 -static int rpi_touchscreen_dsi_remove(struct mipi_dsi_device *dsi)
324 +static int rpi_touchscreen_remove(struct i2c_client *i2c)
326 - struct device *dev = &dsi->dev;
327 - struct rpi_touchscreen *ts = dev_get_drvdata(dev);
329 + struct rpi_touchscreen *ts = i2c_get_clientdata(i2c);
331 - ret = mipi_dsi_detach(dsi);
333 - dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", ret);
336 + mipi_dsi_detach(ts->dsi);
338 - drm_panel_detach(&ts->base);
339 drm_panel_remove(&ts->base);
341 - put_device(&ts->bridge_i2c->dev);
342 + mipi_dsi_device_unregister(ts->dsi);
348 -static void rpi_touchscreen_dsi_shutdown(struct mipi_dsi_device *dsi)
349 +static int rpi_touchscreen_dsi_probe(struct mipi_dsi_device *dsi)
351 - struct device *dev = &dsi->dev;
352 - struct rpi_touchscreen *ts = dev_get_drvdata(dev);
355 - rpi_touchscreen_i2c_write(ts, REG_POWERON, 0);
356 + dsi->mode_flags = (MIPI_DSI_MODE_VIDEO |
357 + MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
358 + MIPI_DSI_MODE_LPM);
359 + dsi->format = MIPI_DSI_FMT_RGB888;
362 + ret = mipi_dsi_attach(dsi);
365 + dev_err(&dsi->dev, "failed to attach dsi to host: %d\n", ret);
370 -static const struct of_device_id rpi_touchscreen_of_match[] = {
371 - { .compatible = "raspberrypi,touchscreen" },
372 +static struct mipi_dsi_driver rpi_touchscreen_dsi_driver = {
373 + .driver.name = RPI_DSI_DRIVER_NAME,
374 + .probe = rpi_touchscreen_dsi_probe,
377 +static const struct of_device_id rpi_touchscreen_of_ids[] = {
378 + { .compatible = "raspberrypi,7inch-touchscreen-panel" },
381 -MODULE_DEVICE_TABLE(of, rpi_touchscreen_of_match);
382 +MODULE_DEVICE_TABLE(of, rpi_touchscreen_of_ids);
384 -static struct mipi_dsi_driver rpi_touchscreen_driver = {
385 +static struct i2c_driver rpi_touchscreen_driver = {
387 - .name = "raspberrypi-touchscreen",
388 - .of_match_table = rpi_touchscreen_of_match,
389 + .name = "rpi_touchscreen",
390 + .of_match_table = rpi_touchscreen_of_ids,
392 - .probe = rpi_touchscreen_dsi_probe,
393 - .remove = rpi_touchscreen_dsi_remove,
394 - .shutdown = rpi_touchscreen_dsi_shutdown,
395 + .probe = rpi_touchscreen_probe,
396 + .remove = rpi_touchscreen_remove,
398 -module_mipi_dsi_driver(rpi_touchscreen_driver);
400 +static int __init rpi_touchscreen_init(void)
402 + mipi_dsi_driver_register(&rpi_touchscreen_dsi_driver);
403 + return i2c_add_driver(&rpi_touchscreen_driver);
405 +module_init(rpi_touchscreen_init);
407 +static void __exit rpi_touchscreen_exit(void)
409 + i2c_del_driver(&rpi_touchscreen_driver);
410 + mipi_dsi_driver_unregister(&rpi_touchscreen_dsi_driver);
412 +module_exit(rpi_touchscreen_exit);
414 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
415 MODULE_DESCRIPTION("Raspberry Pi 7-inch touchscreen driver");