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