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