Loading...
1/*
2 * Copyright © 2016-2017 Broadcom
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Portions of this file (derived from panel-simple.c) are:
9 *
10 * Copyright (C) 2013, NVIDIA Corporation. All rights reserved.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sub license,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice (including the
20 * next paragraph) shall be included in all copies or substantial portions
21 * of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
26 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 * DEALINGS IN THE SOFTWARE.
30 */
31
32/*
33 * Raspberry Pi 7" touchscreen panel driver.
34 *
35 * The 7" touchscreen consists of a DPI LCD panel, a Toshiba
36 * TC358762XBG DSI-DPI bridge, and an I2C-connected Atmel ATTINY88-MUR
37 * controlling power management, the LCD PWM, and initial register
38 * setup of the Tohsiba.
39 *
40 * This driver controls the TC358762 and ATTINY88, presenting a DSI
41 * device with a drm_panel.
42 */
43
44#include <linux/delay.h>
45#include <linux/err.h>
46#include <linux/i2c.h>
47#include <linux/media-bus-format.h>
48#include <linux/module.h>
49#include <linux/of.h>
50#include <linux/of_graph.h>
51#include <linux/pm.h>
52
53#include <drm/drm_crtc.h>
54#include <drm/drm_device.h>
55#include <drm/drm_mipi_dsi.h>
56#include <drm/drm_panel.h>
57
58#define RPI_DSI_DRIVER_NAME "rpi-ts-dsi"
59
60/* I2C registers of the Atmel microcontroller. */
61enum REG_ADDR {
62 REG_ID = 0x80,
63 REG_PORTA, /* BIT(2) for horizontal flip, BIT(3) for vertical flip */
64 REG_PORTB,
65 REG_PORTC,
66 REG_PORTD,
67 REG_POWERON,
68 REG_PWM,
69 REG_DDRA,
70 REG_DDRB,
71 REG_DDRC,
72 REG_DDRD,
73 REG_TEST,
74 REG_WR_ADDRL,
75 REG_WR_ADDRH,
76 REG_READH,
77 REG_READL,
78 REG_WRITEH,
79 REG_WRITEL,
80 REG_ID2,
81};
82
83/* DSI D-PHY Layer Registers */
84#define D0W_DPHYCONTTX 0x0004
85#define CLW_DPHYCONTRX 0x0020
86#define D0W_DPHYCONTRX 0x0024
87#define D1W_DPHYCONTRX 0x0028
88#define COM_DPHYCONTRX 0x0038
89#define CLW_CNTRL 0x0040
90#define D0W_CNTRL 0x0044
91#define D1W_CNTRL 0x0048
92#define DFTMODE_CNTRL 0x0054
93
94/* DSI PPI Layer Registers */
95#define PPI_STARTPPI 0x0104
96#define PPI_BUSYPPI 0x0108
97#define PPI_LINEINITCNT 0x0110
98#define PPI_LPTXTIMECNT 0x0114
99#define PPI_CLS_ATMR 0x0140
100#define PPI_D0S_ATMR 0x0144
101#define PPI_D1S_ATMR 0x0148
102#define PPI_D0S_CLRSIPOCOUNT 0x0164
103#define PPI_D1S_CLRSIPOCOUNT 0x0168
104#define CLS_PRE 0x0180
105#define D0S_PRE 0x0184
106#define D1S_PRE 0x0188
107#define CLS_PREP 0x01A0
108#define D0S_PREP 0x01A4
109#define D1S_PREP 0x01A8
110#define CLS_ZERO 0x01C0
111#define D0S_ZERO 0x01C4
112#define D1S_ZERO 0x01C8
113#define PPI_CLRFLG 0x01E0
114#define PPI_CLRSIPO 0x01E4
115#define HSTIMEOUT 0x01F0
116#define HSTIMEOUTENABLE 0x01F4
117
118/* DSI Protocol Layer Registers */
119#define DSI_STARTDSI 0x0204
120#define DSI_BUSYDSI 0x0208
121#define DSI_LANEENABLE 0x0210
122# define DSI_LANEENABLE_CLOCK BIT(0)
123# define DSI_LANEENABLE_D0 BIT(1)
124# define DSI_LANEENABLE_D1 BIT(2)
125
126#define DSI_LANESTATUS0 0x0214
127#define DSI_LANESTATUS1 0x0218
128#define DSI_INTSTATUS 0x0220
129#define DSI_INTMASK 0x0224
130#define DSI_INTCLR 0x0228
131#define DSI_LPTXTO 0x0230
132#define DSI_MODE 0x0260
133#define DSI_PAYLOAD0 0x0268
134#define DSI_PAYLOAD1 0x026C
135#define DSI_SHORTPKTDAT 0x0270
136#define DSI_SHORTPKTREQ 0x0274
137#define DSI_BTASTA 0x0278
138#define DSI_BTACLR 0x027C
139
140/* DSI General Registers */
141#define DSIERRCNT 0x0300
142#define DSISIGMOD 0x0304
143
144/* DSI Application Layer Registers */
145#define APLCTRL 0x0400
146#define APLSTAT 0x0404
147#define APLERR 0x0408
148#define PWRMOD 0x040C
149#define RDPKTLN 0x0410
150#define PXLFMT 0x0414
151#define MEMWRCMD 0x0418
152
153/* LCDC/DPI Host Registers */
154#define LCDCTRL 0x0420
155#define HSR 0x0424
156#define HDISPR 0x0428
157#define VSR 0x042C
158#define VDISPR 0x0430
159#define VFUEN 0x0434
160
161/* DBI-B Host Registers */
162#define DBIBCTRL 0x0440
163
164/* SPI Master Registers */
165#define SPICMR 0x0450
166#define SPITCR 0x0454
167
168/* System Controller Registers */
169#define SYSSTAT 0x0460
170#define SYSCTRL 0x0464
171#define SYSPLL1 0x0468
172#define SYSPLL2 0x046C
173#define SYSPLL3 0x0470
174#define SYSPMCTRL 0x047C
175
176/* GPIO Registers */
177#define GPIOC 0x0480
178#define GPIOO 0x0484
179#define GPIOI 0x0488
180
181/* I2C Registers */
182#define I2CCLKCTRL 0x0490
183
184/* Chip/Rev Registers */
185#define IDREG 0x04A0
186
187/* Debug Registers */
188#define WCMDQUEUE 0x0500
189#define RCMDQUEUE 0x0504
190
191struct rpi_touchscreen {
192 struct drm_panel base;
193 struct mipi_dsi_device *dsi;
194 struct i2c_client *i2c;
195};
196
197static const struct drm_display_mode rpi_touchscreen_modes[] = {
198 {
199 /* Modeline comes from the Raspberry Pi firmware, with HFP=1
200 * plugged in and clock re-computed from that.
201 */
202 .clock = 25979400 / 1000,
203 .hdisplay = 800,
204 .hsync_start = 800 + 1,
205 .hsync_end = 800 + 1 + 2,
206 .htotal = 800 + 1 + 2 + 46,
207 .vdisplay = 480,
208 .vsync_start = 480 + 7,
209 .vsync_end = 480 + 7 + 2,
210 .vtotal = 480 + 7 + 2 + 21,
211 },
212};
213
214static struct rpi_touchscreen *panel_to_ts(struct drm_panel *panel)
215{
216 return container_of(panel, struct rpi_touchscreen, base);
217}
218
219static int rpi_touchscreen_i2c_read(struct rpi_touchscreen *ts, u8 reg)
220{
221 return i2c_smbus_read_byte_data(ts->i2c, reg);
222}
223
224static void rpi_touchscreen_i2c_write(struct rpi_touchscreen *ts,
225 u8 reg, u8 val)
226{
227 int ret;
228
229 ret = i2c_smbus_write_byte_data(ts->i2c, reg, val);
230 if (ret)
231 dev_err(&ts->i2c->dev, "I2C write failed: %d\n", ret);
232}
233
234static int rpi_touchscreen_write(struct rpi_touchscreen *ts, u16 reg, u32 val)
235{
236 u8 msg[] = {
237 reg,
238 reg >> 8,
239 val,
240 val >> 8,
241 val >> 16,
242 val >> 24,
243 };
244
245 mipi_dsi_generic_write(ts->dsi, msg, sizeof(msg));
246
247 return 0;
248}
249
250static int rpi_touchscreen_disable(struct drm_panel *panel)
251{
252 struct rpi_touchscreen *ts = panel_to_ts(panel);
253
254 rpi_touchscreen_i2c_write(ts, REG_PWM, 0);
255
256 rpi_touchscreen_i2c_write(ts, REG_POWERON, 0);
257 udelay(1);
258
259 return 0;
260}
261
262static int rpi_touchscreen_noop(struct drm_panel *panel)
263{
264 return 0;
265}
266
267static int rpi_touchscreen_prepare(struct drm_panel *panel)
268{
269 struct rpi_touchscreen *ts = panel_to_ts(panel);
270 int i;
271
272 rpi_touchscreen_i2c_write(ts, REG_POWERON, 1);
273 /* Wait for nPWRDWN to go low to indicate poweron is done. */
274 for (i = 0; i < 100; i++) {
275 if (rpi_touchscreen_i2c_read(ts, REG_PORTB) & 1)
276 break;
277 }
278
279 rpi_touchscreen_write(ts, DSI_LANEENABLE,
280 DSI_LANEENABLE_CLOCK |
281 DSI_LANEENABLE_D0);
282 rpi_touchscreen_write(ts, PPI_D0S_CLRSIPOCOUNT, 0x05);
283 rpi_touchscreen_write(ts, PPI_D1S_CLRSIPOCOUNT, 0x05);
284 rpi_touchscreen_write(ts, PPI_D0S_ATMR, 0x00);
285 rpi_touchscreen_write(ts, PPI_D1S_ATMR, 0x00);
286 rpi_touchscreen_write(ts, PPI_LPTXTIMECNT, 0x03);
287
288 rpi_touchscreen_write(ts, SPICMR, 0x00);
289 rpi_touchscreen_write(ts, LCDCTRL, 0x00100150);
290 rpi_touchscreen_write(ts, SYSCTRL, 0x040f);
291 msleep(100);
292
293 rpi_touchscreen_write(ts, PPI_STARTPPI, 0x01);
294 rpi_touchscreen_write(ts, DSI_STARTDSI, 0x01);
295 msleep(100);
296
297 return 0;
298}
299
300static int rpi_touchscreen_enable(struct drm_panel *panel)
301{
302 struct rpi_touchscreen *ts = panel_to_ts(panel);
303
304 /* Turn on the backlight. */
305 rpi_touchscreen_i2c_write(ts, REG_PWM, 255);
306
307 /* Default to the same orientation as the closed source
308 * firmware used for the panel. Runtime rotation
309 * configuration will be supported using VC4's plane
310 * orientation bits.
311 */
312 rpi_touchscreen_i2c_write(ts, REG_PORTA, BIT(2));
313
314 return 0;
315}
316
317static int rpi_touchscreen_get_modes(struct drm_panel *panel,
318 struct drm_connector *connector)
319{
320 unsigned int i, num = 0;
321 static const u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
322
323 for (i = 0; i < ARRAY_SIZE(rpi_touchscreen_modes); i++) {
324 const struct drm_display_mode *m = &rpi_touchscreen_modes[i];
325 struct drm_display_mode *mode;
326
327 mode = drm_mode_duplicate(connector->dev, m);
328 if (!mode) {
329 dev_err(panel->dev, "failed to add mode %ux%u@%u\n",
330 m->hdisplay, m->vdisplay,
331 drm_mode_vrefresh(m));
332 continue;
333 }
334
335 mode->type |= DRM_MODE_TYPE_DRIVER;
336
337 if (i == 0)
338 mode->type |= DRM_MODE_TYPE_PREFERRED;
339
340 drm_mode_set_name(mode);
341
342 drm_mode_probed_add(connector, mode);
343 num++;
344 }
345
346 connector->display_info.bpc = 8;
347 connector->display_info.width_mm = 154;
348 connector->display_info.height_mm = 86;
349 drm_display_info_set_bus_formats(&connector->display_info,
350 &bus_format, 1);
351
352 return num;
353}
354
355static const struct drm_panel_funcs rpi_touchscreen_funcs = {
356 .disable = rpi_touchscreen_disable,
357 .unprepare = rpi_touchscreen_noop,
358 .prepare = rpi_touchscreen_prepare,
359 .enable = rpi_touchscreen_enable,
360 .get_modes = rpi_touchscreen_get_modes,
361};
362
363static int rpi_touchscreen_probe(struct i2c_client *i2c)
364{
365 struct device *dev = &i2c->dev;
366 struct rpi_touchscreen *ts;
367 struct device_node *endpoint, *dsi_host_node;
368 struct mipi_dsi_host *host;
369 int ver;
370 struct mipi_dsi_device_info info = {
371 .type = RPI_DSI_DRIVER_NAME,
372 .channel = 0,
373 .node = NULL,
374 };
375
376 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
377 if (!ts)
378 return -ENOMEM;
379
380 i2c_set_clientdata(i2c, ts);
381
382 ts->i2c = i2c;
383
384 ver = rpi_touchscreen_i2c_read(ts, REG_ID);
385 if (ver < 0) {
386 dev_err(dev, "Atmel I2C read failed: %d\n", ver);
387 return -ENODEV;
388 }
389
390 switch (ver) {
391 case 0xde: /* ver 1 */
392 case 0xc3: /* ver 2 */
393 break;
394 default:
395 dev_err(dev, "Unknown Atmel firmware revision: 0x%02x\n", ver);
396 return -ENODEV;
397 }
398
399 /* Turn off at boot, so we can cleanly sequence powering on. */
400 rpi_touchscreen_i2c_write(ts, REG_POWERON, 0);
401
402 /* Look up the DSI host. It needs to probe before we do. */
403 endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
404 if (!endpoint)
405 return -ENODEV;
406
407 dsi_host_node = of_graph_get_remote_port_parent(endpoint);
408 if (!dsi_host_node)
409 goto error;
410
411 host = of_find_mipi_dsi_host_by_node(dsi_host_node);
412 of_node_put(dsi_host_node);
413 if (!host) {
414 of_node_put(endpoint);
415 return -EPROBE_DEFER;
416 }
417
418 info.node = of_graph_get_remote_port(endpoint);
419 if (!info.node)
420 goto error;
421
422 of_node_put(endpoint);
423
424 ts->dsi = mipi_dsi_device_register_full(host, &info);
425 if (IS_ERR(ts->dsi)) {
426 dev_err(dev, "DSI device registration failed: %ld\n",
427 PTR_ERR(ts->dsi));
428 return PTR_ERR(ts->dsi);
429 }
430
431 drm_panel_init(&ts->base, dev, &rpi_touchscreen_funcs,
432 DRM_MODE_CONNECTOR_DSI);
433
434 /* This appears last, as it's what will unblock the DSI host
435 * driver's component bind function.
436 */
437 drm_panel_add(&ts->base);
438
439 return 0;
440
441error:
442 of_node_put(endpoint);
443 return -ENODEV;
444}
445
446static void rpi_touchscreen_remove(struct i2c_client *i2c)
447{
448 struct rpi_touchscreen *ts = i2c_get_clientdata(i2c);
449
450 mipi_dsi_detach(ts->dsi);
451
452 drm_panel_remove(&ts->base);
453
454 mipi_dsi_device_unregister(ts->dsi);
455}
456
457static int rpi_touchscreen_dsi_probe(struct mipi_dsi_device *dsi)
458{
459 int ret;
460
461 dsi->mode_flags = (MIPI_DSI_MODE_VIDEO |
462 MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
463 MIPI_DSI_MODE_LPM);
464 dsi->format = MIPI_DSI_FMT_RGB888;
465 dsi->lanes = 1;
466
467 ret = mipi_dsi_attach(dsi);
468
469 if (ret)
470 dev_err(&dsi->dev, "failed to attach dsi to host: %d\n", ret);
471
472 return ret;
473}
474
475static struct mipi_dsi_driver rpi_touchscreen_dsi_driver = {
476 .driver.name = RPI_DSI_DRIVER_NAME,
477 .probe = rpi_touchscreen_dsi_probe,
478};
479
480static const struct of_device_id rpi_touchscreen_of_ids[] = {
481 { .compatible = "raspberrypi,7inch-touchscreen-panel" },
482 { } /* sentinel */
483};
484MODULE_DEVICE_TABLE(of, rpi_touchscreen_of_ids);
485
486static struct i2c_driver rpi_touchscreen_driver = {
487 .driver = {
488 .name = "rpi_touchscreen",
489 .of_match_table = rpi_touchscreen_of_ids,
490 },
491 .probe = rpi_touchscreen_probe,
492 .remove = rpi_touchscreen_remove,
493};
494
495static int __init rpi_touchscreen_init(void)
496{
497 mipi_dsi_driver_register(&rpi_touchscreen_dsi_driver);
498 return i2c_add_driver(&rpi_touchscreen_driver);
499}
500module_init(rpi_touchscreen_init);
501
502static void __exit rpi_touchscreen_exit(void)
503{
504 i2c_del_driver(&rpi_touchscreen_driver);
505 mipi_dsi_driver_unregister(&rpi_touchscreen_dsi_driver);
506}
507module_exit(rpi_touchscreen_exit);
508
509MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
510MODULE_DESCRIPTION("Raspberry Pi 7-inch touchscreen driver");
511MODULE_LICENSE("GPL v2");
1/*
2 * Copyright © 2016-2017 Broadcom
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Portions of this file (derived from panel-simple.c) are:
9 *
10 * Copyright (C) 2013, NVIDIA Corporation. All rights reserved.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sub license,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice (including the
20 * next paragraph) shall be included in all copies or substantial portions
21 * of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
26 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 * DEALINGS IN THE SOFTWARE.
30 */
31
32/**
33 * Raspberry Pi 7" touchscreen panel driver.
34 *
35 * The 7" touchscreen consists of a DPI LCD panel, a Toshiba
36 * TC358762XBG DSI-DPI bridge, and an I2C-connected Atmel ATTINY88-MUR
37 * controlling power management, the LCD PWM, and initial register
38 * setup of the Tohsiba.
39 *
40 * This driver controls the TC358762 and ATTINY88, presenting a DSI
41 * device with a drm_panel.
42 */
43
44#include <linux/delay.h>
45#include <linux/err.h>
46#include <linux/fb.h>
47#include <linux/gpio.h>
48#include <linux/gpio/consumer.h>
49#include <linux/i2c.h>
50#include <linux/module.h>
51#include <linux/of.h>
52#include <linux/of_device.h>
53#include <linux/of_graph.h>
54#include <linux/pm.h>
55
56#include <drm/drm_panel.h>
57#include <drm/drmP.h>
58#include <drm/drm_crtc.h>
59#include <drm/drm_mipi_dsi.h>
60#include <drm/drm_panel.h>
61
62#define RPI_DSI_DRIVER_NAME "rpi-ts-dsi"
63
64/* I2C registers of the Atmel microcontroller. */
65enum REG_ADDR {
66 REG_ID = 0x80,
67 REG_PORTA, /* BIT(2) for horizontal flip, BIT(3) for vertical flip */
68 REG_PORTB,
69 REG_PORTC,
70 REG_PORTD,
71 REG_POWERON,
72 REG_PWM,
73 REG_DDRA,
74 REG_DDRB,
75 REG_DDRC,
76 REG_DDRD,
77 REG_TEST,
78 REG_WR_ADDRL,
79 REG_WR_ADDRH,
80 REG_READH,
81 REG_READL,
82 REG_WRITEH,
83 REG_WRITEL,
84 REG_ID2,
85};
86
87/* DSI D-PHY Layer Registers */
88#define D0W_DPHYCONTTX 0x0004
89#define CLW_DPHYCONTRX 0x0020
90#define D0W_DPHYCONTRX 0x0024
91#define D1W_DPHYCONTRX 0x0028
92#define COM_DPHYCONTRX 0x0038
93#define CLW_CNTRL 0x0040
94#define D0W_CNTRL 0x0044
95#define D1W_CNTRL 0x0048
96#define DFTMODE_CNTRL 0x0054
97
98/* DSI PPI Layer Registers */
99#define PPI_STARTPPI 0x0104
100#define PPI_BUSYPPI 0x0108
101#define PPI_LINEINITCNT 0x0110
102#define PPI_LPTXTIMECNT 0x0114
103#define PPI_CLS_ATMR 0x0140
104#define PPI_D0S_ATMR 0x0144
105#define PPI_D1S_ATMR 0x0148
106#define PPI_D0S_CLRSIPOCOUNT 0x0164
107#define PPI_D1S_CLRSIPOCOUNT 0x0168
108#define CLS_PRE 0x0180
109#define D0S_PRE 0x0184
110#define D1S_PRE 0x0188
111#define CLS_PREP 0x01A0
112#define D0S_PREP 0x01A4
113#define D1S_PREP 0x01A8
114#define CLS_ZERO 0x01C0
115#define D0S_ZERO 0x01C4
116#define D1S_ZERO 0x01C8
117#define PPI_CLRFLG 0x01E0
118#define PPI_CLRSIPO 0x01E4
119#define HSTIMEOUT 0x01F0
120#define HSTIMEOUTENABLE 0x01F4
121
122/* DSI Protocol Layer Registers */
123#define DSI_STARTDSI 0x0204
124#define DSI_BUSYDSI 0x0208
125#define DSI_LANEENABLE 0x0210
126# define DSI_LANEENABLE_CLOCK BIT(0)
127# define DSI_LANEENABLE_D0 BIT(1)
128# define DSI_LANEENABLE_D1 BIT(2)
129
130#define DSI_LANESTATUS0 0x0214
131#define DSI_LANESTATUS1 0x0218
132#define DSI_INTSTATUS 0x0220
133#define DSI_INTMASK 0x0224
134#define DSI_INTCLR 0x0228
135#define DSI_LPTXTO 0x0230
136#define DSI_MODE 0x0260
137#define DSI_PAYLOAD0 0x0268
138#define DSI_PAYLOAD1 0x026C
139#define DSI_SHORTPKTDAT 0x0270
140#define DSI_SHORTPKTREQ 0x0274
141#define DSI_BTASTA 0x0278
142#define DSI_BTACLR 0x027C
143
144/* DSI General Registers */
145#define DSIERRCNT 0x0300
146#define DSISIGMOD 0x0304
147
148/* DSI Application Layer Registers */
149#define APLCTRL 0x0400
150#define APLSTAT 0x0404
151#define APLERR 0x0408
152#define PWRMOD 0x040C
153#define RDPKTLN 0x0410
154#define PXLFMT 0x0414
155#define MEMWRCMD 0x0418
156
157/* LCDC/DPI Host Registers */
158#define LCDCTRL 0x0420
159#define HSR 0x0424
160#define HDISPR 0x0428
161#define VSR 0x042C
162#define VDISPR 0x0430
163#define VFUEN 0x0434
164
165/* DBI-B Host Registers */
166#define DBIBCTRL 0x0440
167
168/* SPI Master Registers */
169#define SPICMR 0x0450
170#define SPITCR 0x0454
171
172/* System Controller Registers */
173#define SYSSTAT 0x0460
174#define SYSCTRL 0x0464
175#define SYSPLL1 0x0468
176#define SYSPLL2 0x046C
177#define SYSPLL3 0x0470
178#define SYSPMCTRL 0x047C
179
180/* GPIO Registers */
181#define GPIOC 0x0480
182#define GPIOO 0x0484
183#define GPIOI 0x0488
184
185/* I2C Registers */
186#define I2CCLKCTRL 0x0490
187
188/* Chip/Rev Registers */
189#define IDREG 0x04A0
190
191/* Debug Registers */
192#define WCMDQUEUE 0x0500
193#define RCMDQUEUE 0x0504
194
195struct rpi_touchscreen {
196 struct drm_panel base;
197 struct mipi_dsi_device *dsi;
198 struct i2c_client *i2c;
199};
200
201static const struct drm_display_mode rpi_touchscreen_modes[] = {
202 {
203 /* Modeline comes from the Raspberry Pi firmware, with HFP=1
204 * plugged in and clock re-computed from that.
205 */
206 .clock = 25979400 / 1000,
207 .hdisplay = 800,
208 .hsync_start = 800 + 1,
209 .hsync_end = 800 + 1 + 2,
210 .htotal = 800 + 1 + 2 + 46,
211 .vdisplay = 480,
212 .vsync_start = 480 + 7,
213 .vsync_end = 480 + 7 + 2,
214 .vtotal = 480 + 7 + 2 + 21,
215 .vrefresh = 60,
216 },
217};
218
219static struct rpi_touchscreen *panel_to_ts(struct drm_panel *panel)
220{
221 return container_of(panel, struct rpi_touchscreen, base);
222}
223
224static int rpi_touchscreen_i2c_read(struct rpi_touchscreen *ts, u8 reg)
225{
226 return i2c_smbus_read_byte_data(ts->i2c, reg);
227}
228
229static void rpi_touchscreen_i2c_write(struct rpi_touchscreen *ts,
230 u8 reg, u8 val)
231{
232 int ret;
233
234 ret = i2c_smbus_write_byte_data(ts->i2c, reg, val);
235 if (ret)
236 dev_err(&ts->dsi->dev, "I2C write failed: %d\n", ret);
237}
238
239static int rpi_touchscreen_write(struct rpi_touchscreen *ts, u16 reg, u32 val)
240{
241#if 0
242 /* The firmware uses LP DSI transactions like this to bring up
243 * the hardware, which should be faster than using I2C to then
244 * pass to the Toshiba. However, I was unable to get it to
245 * work.
246 */
247 u8 msg[] = {
248 reg,
249 reg >> 8,
250 val,
251 val >> 8,
252 val >> 16,
253 val >> 24,
254 };
255
256 mipi_dsi_dcs_write_buffer(ts->dsi, msg, sizeof(msg));
257#else
258 rpi_touchscreen_i2c_write(ts, REG_WR_ADDRH, reg >> 8);
259 rpi_touchscreen_i2c_write(ts, REG_WR_ADDRL, reg);
260 rpi_touchscreen_i2c_write(ts, REG_WRITEH, val >> 8);
261 rpi_touchscreen_i2c_write(ts, REG_WRITEL, val);
262#endif
263
264 return 0;
265}
266
267static int rpi_touchscreen_disable(struct drm_panel *panel)
268{
269 struct rpi_touchscreen *ts = panel_to_ts(panel);
270
271 rpi_touchscreen_i2c_write(ts, REG_PWM, 0);
272
273 rpi_touchscreen_i2c_write(ts, REG_POWERON, 0);
274 udelay(1);
275
276 return 0;
277}
278
279static int rpi_touchscreen_noop(struct drm_panel *panel)
280{
281 return 0;
282}
283
284static int rpi_touchscreen_enable(struct drm_panel *panel)
285{
286 struct rpi_touchscreen *ts = panel_to_ts(panel);
287 int i;
288
289 rpi_touchscreen_i2c_write(ts, REG_POWERON, 1);
290 /* Wait for nPWRDWN to go low to indicate poweron is done. */
291 for (i = 0; i < 100; i++) {
292 if (rpi_touchscreen_i2c_read(ts, REG_PORTB) & 1)
293 break;
294 }
295
296 rpi_touchscreen_write(ts, DSI_LANEENABLE,
297 DSI_LANEENABLE_CLOCK |
298 DSI_LANEENABLE_D0);
299 rpi_touchscreen_write(ts, PPI_D0S_CLRSIPOCOUNT, 0x05);
300 rpi_touchscreen_write(ts, PPI_D1S_CLRSIPOCOUNT, 0x05);
301 rpi_touchscreen_write(ts, PPI_D0S_ATMR, 0x00);
302 rpi_touchscreen_write(ts, PPI_D1S_ATMR, 0x00);
303 rpi_touchscreen_write(ts, PPI_LPTXTIMECNT, 0x03);
304
305 rpi_touchscreen_write(ts, SPICMR, 0x00);
306 rpi_touchscreen_write(ts, LCDCTRL, 0x00100150);
307 rpi_touchscreen_write(ts, SYSCTRL, 0x040f);
308 msleep(100);
309
310 rpi_touchscreen_write(ts, PPI_STARTPPI, 0x01);
311 rpi_touchscreen_write(ts, DSI_STARTDSI, 0x01);
312 msleep(100);
313
314 /* Turn on the backlight. */
315 rpi_touchscreen_i2c_write(ts, REG_PWM, 255);
316
317 /* Default to the same orientation as the closed source
318 * firmware used for the panel. Runtime rotation
319 * configuration will be supported using VC4's plane
320 * orientation bits.
321 */
322 rpi_touchscreen_i2c_write(ts, REG_PORTA, BIT(2));
323
324 return 0;
325}
326
327static int rpi_touchscreen_get_modes(struct drm_panel *panel)
328{
329 struct drm_connector *connector = panel->connector;
330 struct drm_device *drm = panel->drm;
331 unsigned int i, num = 0;
332 static const u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
333
334 for (i = 0; i < ARRAY_SIZE(rpi_touchscreen_modes); i++) {
335 const struct drm_display_mode *m = &rpi_touchscreen_modes[i];
336 struct drm_display_mode *mode;
337
338 mode = drm_mode_duplicate(drm, m);
339 if (!mode) {
340 dev_err(drm->dev, "failed to add mode %ux%u@%u\n",
341 m->hdisplay, m->vdisplay, m->vrefresh);
342 continue;
343 }
344
345 mode->type |= DRM_MODE_TYPE_DRIVER;
346
347 if (i == 0)
348 mode->type |= DRM_MODE_TYPE_PREFERRED;
349
350 drm_mode_set_name(mode);
351
352 drm_mode_probed_add(connector, mode);
353 num++;
354 }
355
356 connector->display_info.bpc = 8;
357 connector->display_info.width_mm = 154;
358 connector->display_info.height_mm = 86;
359 drm_display_info_set_bus_formats(&connector->display_info,
360 &bus_format, 1);
361
362 return num;
363}
364
365static const struct drm_panel_funcs rpi_touchscreen_funcs = {
366 .disable = rpi_touchscreen_disable,
367 .unprepare = rpi_touchscreen_noop,
368 .prepare = rpi_touchscreen_noop,
369 .enable = rpi_touchscreen_enable,
370 .get_modes = rpi_touchscreen_get_modes,
371};
372
373static int rpi_touchscreen_probe(struct i2c_client *i2c,
374 const struct i2c_device_id *id)
375{
376 struct device *dev = &i2c->dev;
377 struct rpi_touchscreen *ts;
378 struct device_node *endpoint, *dsi_host_node;
379 struct mipi_dsi_host *host;
380 int ret, ver;
381 struct mipi_dsi_device_info info = {
382 .type = RPI_DSI_DRIVER_NAME,
383 .channel = 0,
384 .node = NULL,
385 };
386
387 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
388 if (!ts)
389 return -ENOMEM;
390
391 i2c_set_clientdata(i2c, ts);
392
393 ts->i2c = i2c;
394
395 ver = rpi_touchscreen_i2c_read(ts, REG_ID);
396 if (ver < 0) {
397 dev_err(dev, "Atmel I2C read failed: %d\n", ver);
398 return -ENODEV;
399 }
400
401 switch (ver) {
402 case 0xde: /* ver 1 */
403 case 0xc3: /* ver 2 */
404 break;
405 default:
406 dev_err(dev, "Unknown Atmel firmware revision: 0x%02x\n", ver);
407 return -ENODEV;
408 }
409
410 /* Turn off at boot, so we can cleanly sequence powering on. */
411 rpi_touchscreen_i2c_write(ts, REG_POWERON, 0);
412
413 /* Look up the DSI host. It needs to probe before we do. */
414 endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
415 dsi_host_node = of_graph_get_remote_port_parent(endpoint);
416 host = of_find_mipi_dsi_host_by_node(dsi_host_node);
417 of_node_put(dsi_host_node);
418 if (!host) {
419 of_node_put(endpoint);
420 return -EPROBE_DEFER;
421 }
422
423 info.node = of_graph_get_remote_port(endpoint);
424 of_node_put(endpoint);
425
426 ts->dsi = mipi_dsi_device_register_full(host, &info);
427 if (IS_ERR(ts->dsi)) {
428 dev_err(dev, "DSI device registration failed: %ld\n",
429 PTR_ERR(ts->dsi));
430 return PTR_ERR(ts->dsi);
431 }
432
433 ts->base.dev = dev;
434 ts->base.funcs = &rpi_touchscreen_funcs;
435
436 /* This appears last, as it's what will unblock the DSI host
437 * driver's component bind function.
438 */
439 ret = drm_panel_add(&ts->base);
440 if (ret)
441 return ret;
442
443 return 0;
444}
445
446static int rpi_touchscreen_remove(struct i2c_client *i2c)
447{
448 struct rpi_touchscreen *ts = i2c_get_clientdata(i2c);
449
450 mipi_dsi_detach(ts->dsi);
451
452 drm_panel_remove(&ts->base);
453
454 mipi_dsi_device_unregister(ts->dsi);
455 kfree(ts->dsi);
456
457 return 0;
458}
459
460static int rpi_touchscreen_dsi_probe(struct mipi_dsi_device *dsi)
461{
462 int ret;
463
464 dsi->mode_flags = (MIPI_DSI_MODE_VIDEO |
465 MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
466 MIPI_DSI_MODE_LPM);
467 dsi->format = MIPI_DSI_FMT_RGB888;
468 dsi->lanes = 1;
469
470 ret = mipi_dsi_attach(dsi);
471
472 if (ret)
473 dev_err(&dsi->dev, "failed to attach dsi to host: %d\n", ret);
474
475 return ret;
476}
477
478static struct mipi_dsi_driver rpi_touchscreen_dsi_driver = {
479 .driver.name = RPI_DSI_DRIVER_NAME,
480 .probe = rpi_touchscreen_dsi_probe,
481};
482
483static const struct of_device_id rpi_touchscreen_of_ids[] = {
484 { .compatible = "raspberrypi,7inch-touchscreen-panel" },
485 { } /* sentinel */
486};
487MODULE_DEVICE_TABLE(of, rpi_touchscreen_of_ids);
488
489static struct i2c_driver rpi_touchscreen_driver = {
490 .driver = {
491 .name = "rpi_touchscreen",
492 .of_match_table = rpi_touchscreen_of_ids,
493 },
494 .probe = rpi_touchscreen_probe,
495 .remove = rpi_touchscreen_remove,
496};
497
498static int __init rpi_touchscreen_init(void)
499{
500 mipi_dsi_driver_register(&rpi_touchscreen_dsi_driver);
501 return i2c_add_driver(&rpi_touchscreen_driver);
502}
503module_init(rpi_touchscreen_init);
504
505static void __exit rpi_touchscreen_exit(void)
506{
507 i2c_del_driver(&rpi_touchscreen_driver);
508 mipi_dsi_driver_unregister(&rpi_touchscreen_dsi_driver);
509}
510module_exit(rpi_touchscreen_exit);
511
512MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
513MODULE_DESCRIPTION("Raspberry Pi 7-inch touchscreen driver");
514MODULE_LICENSE("GPL v2");