12f733d61SEric Anholt /*
22f733d61SEric Anholt  * Copyright © 2016-2017 Broadcom
32f733d61SEric Anholt  *
42f733d61SEric Anholt  * This program is free software; you can redistribute it and/or modify
52f733d61SEric Anholt  * it under the terms of the GNU General Public License version 2 as
62f733d61SEric Anholt  * published by the Free Software Foundation.
72f733d61SEric Anholt  *
82f733d61SEric Anholt  * Portions of this file (derived from panel-simple.c) are:
92f733d61SEric Anholt  *
102f733d61SEric Anholt  * Copyright (C) 2013, NVIDIA Corporation.  All rights reserved.
112f733d61SEric Anholt  *
122f733d61SEric Anholt  * Permission is hereby granted, free of charge, to any person obtaining a
132f733d61SEric Anholt  * copy of this software and associated documentation files (the "Software"),
142f733d61SEric Anholt  * to deal in the Software without restriction, including without limitation
152f733d61SEric Anholt  * the rights to use, copy, modify, merge, publish, distribute, sub license,
162f733d61SEric Anholt  * and/or sell copies of the Software, and to permit persons to whom the
172f733d61SEric Anholt  * Software is furnished to do so, subject to the following conditions:
182f733d61SEric Anholt  *
192f733d61SEric Anholt  * The above copyright notice and this permission notice (including the
202f733d61SEric Anholt  * next paragraph) shall be included in all copies or substantial portions
212f733d61SEric Anholt  * of the Software.
222f733d61SEric Anholt  *
232f733d61SEric Anholt  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
242f733d61SEric Anholt  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
252f733d61SEric Anholt  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
262f733d61SEric Anholt  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
272f733d61SEric Anholt  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
282f733d61SEric Anholt  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
292f733d61SEric Anholt  * DEALINGS IN THE SOFTWARE.
302f733d61SEric Anholt  */
312f733d61SEric Anholt 
3281f9feddSLee Jones /*
332f733d61SEric Anholt  * Raspberry Pi 7" touchscreen panel driver.
342f733d61SEric Anholt  *
352f733d61SEric Anholt  * The 7" touchscreen consists of a DPI LCD panel, a Toshiba
362f733d61SEric Anholt  * TC358762XBG DSI-DPI bridge, and an I2C-connected Atmel ATTINY88-MUR
372f733d61SEric Anholt  * controlling power management, the LCD PWM, and initial register
382f733d61SEric Anholt  * setup of the Tohsiba.
392f733d61SEric Anholt  *
402f733d61SEric Anholt  * This driver controls the TC358762 and ATTINY88, presenting a DSI
412f733d61SEric Anholt  * device with a drm_panel.
422f733d61SEric Anholt  */
432f733d61SEric Anholt 
442f733d61SEric Anholt #include <linux/delay.h>
452f733d61SEric Anholt #include <linux/err.h>
462f733d61SEric Anholt #include <linux/i2c.h>
4772bd9ea3SVille Syrjälä #include <linux/media-bus-format.h>
482f733d61SEric Anholt #include <linux/module.h>
492f733d61SEric Anholt #include <linux/of.h>
502f733d61SEric Anholt #include <linux/of_graph.h>
512f733d61SEric Anholt #include <linux/pm.h>
522f733d61SEric Anholt 
532f733d61SEric Anholt #include <drm/drm_crtc.h>
54cb23eae3SSam Ravnborg #include <drm/drm_device.h>
552f733d61SEric Anholt #include <drm/drm_mipi_dsi.h>
56cb23eae3SSam Ravnborg #include <drm/drm_panel.h>
572f733d61SEric Anholt 
582f733d61SEric Anholt #define RPI_DSI_DRIVER_NAME "rpi-ts-dsi"
592f733d61SEric Anholt 
602f733d61SEric Anholt /* I2C registers of the Atmel microcontroller. */
612f733d61SEric Anholt enum REG_ADDR {
622f733d61SEric Anholt 	REG_ID = 0x80,
632f733d61SEric Anholt 	REG_PORTA, /* BIT(2) for horizontal flip, BIT(3) for vertical flip */
642f733d61SEric Anholt 	REG_PORTB,
652f733d61SEric Anholt 	REG_PORTC,
662f733d61SEric Anholt 	REG_PORTD,
672f733d61SEric Anholt 	REG_POWERON,
682f733d61SEric Anholt 	REG_PWM,
692f733d61SEric Anholt 	REG_DDRA,
702f733d61SEric Anholt 	REG_DDRB,
712f733d61SEric Anholt 	REG_DDRC,
722f733d61SEric Anholt 	REG_DDRD,
732f733d61SEric Anholt 	REG_TEST,
742f733d61SEric Anholt 	REG_WR_ADDRL,
752f733d61SEric Anholt 	REG_WR_ADDRH,
762f733d61SEric Anholt 	REG_READH,
772f733d61SEric Anholt 	REG_READL,
782f733d61SEric Anholt 	REG_WRITEH,
792f733d61SEric Anholt 	REG_WRITEL,
802f733d61SEric Anholt 	REG_ID2,
812f733d61SEric Anholt };
822f733d61SEric Anholt 
832f733d61SEric Anholt /* DSI D-PHY Layer Registers */
842f733d61SEric Anholt #define D0W_DPHYCONTTX		0x0004
852f733d61SEric Anholt #define CLW_DPHYCONTRX		0x0020
862f733d61SEric Anholt #define D0W_DPHYCONTRX		0x0024
872f733d61SEric Anholt #define D1W_DPHYCONTRX		0x0028
882f733d61SEric Anholt #define COM_DPHYCONTRX		0x0038
892f733d61SEric Anholt #define CLW_CNTRL		0x0040
902f733d61SEric Anholt #define D0W_CNTRL		0x0044
912f733d61SEric Anholt #define D1W_CNTRL		0x0048
922f733d61SEric Anholt #define DFTMODE_CNTRL		0x0054
932f733d61SEric Anholt 
942f733d61SEric Anholt /* DSI PPI Layer Registers */
952f733d61SEric Anholt #define PPI_STARTPPI		0x0104
962f733d61SEric Anholt #define PPI_BUSYPPI		0x0108
972f733d61SEric Anholt #define PPI_LINEINITCNT		0x0110
982f733d61SEric Anholt #define PPI_LPTXTIMECNT		0x0114
992f733d61SEric Anholt #define PPI_CLS_ATMR		0x0140
1002f733d61SEric Anholt #define PPI_D0S_ATMR		0x0144
1012f733d61SEric Anholt #define PPI_D1S_ATMR		0x0148
1022f733d61SEric Anholt #define PPI_D0S_CLRSIPOCOUNT	0x0164
1032f733d61SEric Anholt #define PPI_D1S_CLRSIPOCOUNT	0x0168
1042f733d61SEric Anholt #define CLS_PRE			0x0180
1052f733d61SEric Anholt #define D0S_PRE			0x0184
1062f733d61SEric Anholt #define D1S_PRE			0x0188
1072f733d61SEric Anholt #define CLS_PREP		0x01A0
1082f733d61SEric Anholt #define D0S_PREP		0x01A4
1092f733d61SEric Anholt #define D1S_PREP		0x01A8
1102f733d61SEric Anholt #define CLS_ZERO		0x01C0
1112f733d61SEric Anholt #define D0S_ZERO		0x01C4
1122f733d61SEric Anholt #define D1S_ZERO		0x01C8
1132f733d61SEric Anholt #define PPI_CLRFLG		0x01E0
1142f733d61SEric Anholt #define PPI_CLRSIPO		0x01E4
1152f733d61SEric Anholt #define HSTIMEOUT		0x01F0
1162f733d61SEric Anholt #define HSTIMEOUTENABLE		0x01F4
1172f733d61SEric Anholt 
1182f733d61SEric Anholt /* DSI Protocol Layer Registers */
1192f733d61SEric Anholt #define DSI_STARTDSI		0x0204
1202f733d61SEric Anholt #define DSI_BUSYDSI		0x0208
1212f733d61SEric Anholt #define DSI_LANEENABLE		0x0210
1222f733d61SEric Anholt # define DSI_LANEENABLE_CLOCK		BIT(0)
1232f733d61SEric Anholt # define DSI_LANEENABLE_D0		BIT(1)
1242f733d61SEric Anholt # define DSI_LANEENABLE_D1		BIT(2)
1252f733d61SEric Anholt 
1262f733d61SEric Anholt #define DSI_LANESTATUS0		0x0214
1272f733d61SEric Anholt #define DSI_LANESTATUS1		0x0218
1282f733d61SEric Anholt #define DSI_INTSTATUS		0x0220
1292f733d61SEric Anholt #define DSI_INTMASK		0x0224
1302f733d61SEric Anholt #define DSI_INTCLR		0x0228
1312f733d61SEric Anholt #define DSI_LPTXTO		0x0230
1322f733d61SEric Anholt #define DSI_MODE		0x0260
1332f733d61SEric Anholt #define DSI_PAYLOAD0		0x0268
1342f733d61SEric Anholt #define DSI_PAYLOAD1		0x026C
1352f733d61SEric Anholt #define DSI_SHORTPKTDAT		0x0270
1362f733d61SEric Anholt #define DSI_SHORTPKTREQ		0x0274
1372f733d61SEric Anholt #define DSI_BTASTA		0x0278
1382f733d61SEric Anholt #define DSI_BTACLR		0x027C
1392f733d61SEric Anholt 
1402f733d61SEric Anholt /* DSI General Registers */
1412f733d61SEric Anholt #define DSIERRCNT		0x0300
1422f733d61SEric Anholt #define DSISIGMOD		0x0304
1432f733d61SEric Anholt 
1442f733d61SEric Anholt /* DSI Application Layer Registers */
1452f733d61SEric Anholt #define APLCTRL			0x0400
1462f733d61SEric Anholt #define APLSTAT			0x0404
1472f733d61SEric Anholt #define APLERR			0x0408
1482f733d61SEric Anholt #define PWRMOD			0x040C
1492f733d61SEric Anholt #define RDPKTLN			0x0410
1502f733d61SEric Anholt #define PXLFMT			0x0414
1512f733d61SEric Anholt #define MEMWRCMD		0x0418
1522f733d61SEric Anholt 
1532f733d61SEric Anholt /* LCDC/DPI Host Registers */
1542f733d61SEric Anholt #define LCDCTRL			0x0420
1552f733d61SEric Anholt #define HSR			0x0424
1562f733d61SEric Anholt #define HDISPR			0x0428
1572f733d61SEric Anholt #define VSR			0x042C
1582f733d61SEric Anholt #define VDISPR			0x0430
1592f733d61SEric Anholt #define VFUEN			0x0434
1602f733d61SEric Anholt 
1612f733d61SEric Anholt /* DBI-B Host Registers */
1622f733d61SEric Anholt #define DBIBCTRL		0x0440
1632f733d61SEric Anholt 
1642f733d61SEric Anholt /* SPI Master Registers */
1652f733d61SEric Anholt #define SPICMR			0x0450
1662f733d61SEric Anholt #define SPITCR			0x0454
1672f733d61SEric Anholt 
1682f733d61SEric Anholt /* System Controller Registers */
1692f733d61SEric Anholt #define SYSSTAT			0x0460
1702f733d61SEric Anholt #define SYSCTRL			0x0464
1712f733d61SEric Anholt #define SYSPLL1			0x0468
1722f733d61SEric Anholt #define SYSPLL2			0x046C
1732f733d61SEric Anholt #define SYSPLL3			0x0470
1742f733d61SEric Anholt #define SYSPMCTRL		0x047C
1752f733d61SEric Anholt 
1762f733d61SEric Anholt /* GPIO Registers */
1772f733d61SEric Anholt #define GPIOC			0x0480
1782f733d61SEric Anholt #define GPIOO			0x0484
1792f733d61SEric Anholt #define GPIOI			0x0488
1802f733d61SEric Anholt 
1812f733d61SEric Anholt /* I2C Registers */
1822f733d61SEric Anholt #define I2CCLKCTRL		0x0490
1832f733d61SEric Anholt 
1842f733d61SEric Anholt /* Chip/Rev Registers */
1852f733d61SEric Anholt #define IDREG			0x04A0
1862f733d61SEric Anholt 
1872f733d61SEric Anholt /* Debug Registers */
1882f733d61SEric Anholt #define WCMDQUEUE		0x0500
1892f733d61SEric Anholt #define RCMDQUEUE		0x0504
1902f733d61SEric Anholt 
1912f733d61SEric Anholt struct rpi_touchscreen {
1922f733d61SEric Anholt 	struct drm_panel base;
1932f733d61SEric Anholt 	struct mipi_dsi_device *dsi;
1942f733d61SEric Anholt 	struct i2c_client *i2c;
1952f733d61SEric Anholt };
1962f733d61SEric Anholt 
1972f733d61SEric Anholt static const struct drm_display_mode rpi_touchscreen_modes[] = {
1982f733d61SEric Anholt 	{
1992f733d61SEric Anholt 		/* Modeline comes from the Raspberry Pi firmware, with HFP=1
2002f733d61SEric Anholt 		 * plugged in and clock re-computed from that.
2012f733d61SEric Anholt 		 */
2022f733d61SEric Anholt 		.clock = 25979400 / 1000,
2032f733d61SEric Anholt 		.hdisplay = 800,
2042f733d61SEric Anholt 		.hsync_start = 800 + 1,
2052f733d61SEric Anholt 		.hsync_end = 800 + 1 + 2,
2062f733d61SEric Anholt 		.htotal = 800 + 1 + 2 + 46,
2072f733d61SEric Anholt 		.vdisplay = 480,
2082f733d61SEric Anholt 		.vsync_start = 480 + 7,
2092f733d61SEric Anholt 		.vsync_end = 480 + 7 + 2,
2102f733d61SEric Anholt 		.vtotal = 480 + 7 + 2 + 21,
2112f733d61SEric Anholt 	},
2122f733d61SEric Anholt };
2132f733d61SEric Anholt 
panel_to_ts(struct drm_panel * panel)2142f733d61SEric Anholt static struct rpi_touchscreen *panel_to_ts(struct drm_panel *panel)
2152f733d61SEric Anholt {
2162f733d61SEric Anholt 	return container_of(panel, struct rpi_touchscreen, base);
2172f733d61SEric Anholt }
2182f733d61SEric Anholt 
rpi_touchscreen_i2c_read(struct rpi_touchscreen * ts,u8 reg)21985b4587fSDan Carpenter static int rpi_touchscreen_i2c_read(struct rpi_touchscreen *ts, u8 reg)
2202f733d61SEric Anholt {
2212f733d61SEric Anholt 	return i2c_smbus_read_byte_data(ts->i2c, reg);
2222f733d61SEric Anholt }
2232f733d61SEric Anholt 
rpi_touchscreen_i2c_write(struct rpi_touchscreen * ts,u8 reg,u8 val)2242f733d61SEric Anholt static void rpi_touchscreen_i2c_write(struct rpi_touchscreen *ts,
2252f733d61SEric Anholt 				      u8 reg, u8 val)
2262f733d61SEric Anholt {
2272f733d61SEric Anholt 	int ret;
2282f733d61SEric Anholt 
2292f733d61SEric Anholt 	ret = i2c_smbus_write_byte_data(ts->i2c, reg, val);
2302f733d61SEric Anholt 	if (ret)
231f92055aeSDave Stevenson 		dev_err(&ts->i2c->dev, "I2C write failed: %d\n", ret);
2322f733d61SEric Anholt }
2332f733d61SEric Anholt 
rpi_touchscreen_write(struct rpi_touchscreen * ts,u16 reg,u32 val)2342f733d61SEric Anholt static int rpi_touchscreen_write(struct rpi_touchscreen *ts, u16 reg, u32 val)
2352f733d61SEric Anholt {
2362f733d61SEric Anholt 	u8 msg[] = {
2372f733d61SEric Anholt 		reg,
2382f733d61SEric Anholt 		reg >> 8,
2392f733d61SEric Anholt 		val,
2402f733d61SEric Anholt 		val >> 8,
2412f733d61SEric Anholt 		val >> 16,
2422f733d61SEric Anholt 		val >> 24,
2432f733d61SEric Anholt 	};
2442f733d61SEric Anholt 
2453481fe76SEric Anholt 	mipi_dsi_generic_write(ts->dsi, msg, sizeof(msg));
2462f733d61SEric Anholt 
2472f733d61SEric Anholt 	return 0;
2482f733d61SEric Anholt }
2492f733d61SEric Anholt 
rpi_touchscreen_disable(struct drm_panel * panel)2502f733d61SEric Anholt static int rpi_touchscreen_disable(struct drm_panel *panel)
2512f733d61SEric Anholt {
2522f733d61SEric Anholt 	struct rpi_touchscreen *ts = panel_to_ts(panel);
2532f733d61SEric Anholt 
2542f733d61SEric Anholt 	rpi_touchscreen_i2c_write(ts, REG_PWM, 0);
2552f733d61SEric Anholt 
2562f733d61SEric Anholt 	rpi_touchscreen_i2c_write(ts, REG_POWERON, 0);
2572f733d61SEric Anholt 	udelay(1);
2582f733d61SEric Anholt 
2592f733d61SEric Anholt 	return 0;
2602f733d61SEric Anholt }
2612f733d61SEric Anholt 
rpi_touchscreen_noop(struct drm_panel * panel)2622f733d61SEric Anholt static int rpi_touchscreen_noop(struct drm_panel *panel)
2632f733d61SEric Anholt {
2642f733d61SEric Anholt 	return 0;
2652f733d61SEric Anholt }
2662f733d61SEric Anholt 
rpi_touchscreen_prepare(struct drm_panel * panel)2675f18c078SDave Stevenson static int rpi_touchscreen_prepare(struct drm_panel *panel)
2682f733d61SEric Anholt {
2692f733d61SEric Anholt 	struct rpi_touchscreen *ts = panel_to_ts(panel);
2702f733d61SEric Anholt 	int i;
2712f733d61SEric Anholt 
2722f733d61SEric Anholt 	rpi_touchscreen_i2c_write(ts, REG_POWERON, 1);
2732f733d61SEric Anholt 	/* Wait for nPWRDWN to go low to indicate poweron is done. */
2742f733d61SEric Anholt 	for (i = 0; i < 100; i++) {
2752f733d61SEric Anholt 		if (rpi_touchscreen_i2c_read(ts, REG_PORTB) & 1)
2762f733d61SEric Anholt 			break;
2772f733d61SEric Anholt 	}
2782f733d61SEric Anholt 
2792f733d61SEric Anholt 	rpi_touchscreen_write(ts, DSI_LANEENABLE,
2802f733d61SEric Anholt 			      DSI_LANEENABLE_CLOCK |
2812f733d61SEric Anholt 			      DSI_LANEENABLE_D0);
2822f733d61SEric Anholt 	rpi_touchscreen_write(ts, PPI_D0S_CLRSIPOCOUNT, 0x05);
2832f733d61SEric Anholt 	rpi_touchscreen_write(ts, PPI_D1S_CLRSIPOCOUNT, 0x05);
2842f733d61SEric Anholt 	rpi_touchscreen_write(ts, PPI_D0S_ATMR, 0x00);
2852f733d61SEric Anholt 	rpi_touchscreen_write(ts, PPI_D1S_ATMR, 0x00);
2862f733d61SEric Anholt 	rpi_touchscreen_write(ts, PPI_LPTXTIMECNT, 0x03);
2872f733d61SEric Anholt 
2882f733d61SEric Anholt 	rpi_touchscreen_write(ts, SPICMR, 0x00);
2892f733d61SEric Anholt 	rpi_touchscreen_write(ts, LCDCTRL, 0x00100150);
2902f733d61SEric Anholt 	rpi_touchscreen_write(ts, SYSCTRL, 0x040f);
2912f733d61SEric Anholt 	msleep(100);
2922f733d61SEric Anholt 
2932f733d61SEric Anholt 	rpi_touchscreen_write(ts, PPI_STARTPPI, 0x01);
2942f733d61SEric Anholt 	rpi_touchscreen_write(ts, DSI_STARTDSI, 0x01);
2952f733d61SEric Anholt 	msleep(100);
2962f733d61SEric Anholt 
2975f18c078SDave Stevenson 	return 0;
2985f18c078SDave Stevenson }
2995f18c078SDave Stevenson 
rpi_touchscreen_enable(struct drm_panel * panel)3005f18c078SDave Stevenson static int rpi_touchscreen_enable(struct drm_panel *panel)
3015f18c078SDave Stevenson {
3025f18c078SDave Stevenson 	struct rpi_touchscreen *ts = panel_to_ts(panel);
3035f18c078SDave Stevenson 
3042f733d61SEric Anholt 	/* Turn on the backlight. */
3052f733d61SEric Anholt 	rpi_touchscreen_i2c_write(ts, REG_PWM, 255);
3062f733d61SEric Anholt 
3072f733d61SEric Anholt 	/* Default to the same orientation as the closed source
3082f733d61SEric Anholt 	 * firmware used for the panel.  Runtime rotation
3092f733d61SEric Anholt 	 * configuration will be supported using VC4's plane
3102f733d61SEric Anholt 	 * orientation bits.
3112f733d61SEric Anholt 	 */
3122f733d61SEric Anholt 	rpi_touchscreen_i2c_write(ts, REG_PORTA, BIT(2));
3132f733d61SEric Anholt 
3142f733d61SEric Anholt 	return 0;
3152f733d61SEric Anholt }
3162f733d61SEric Anholt 
rpi_touchscreen_get_modes(struct drm_panel * panel,struct drm_connector * connector)3170ce8ddd8SSam Ravnborg static int rpi_touchscreen_get_modes(struct drm_panel *panel,
3180ce8ddd8SSam Ravnborg 				     struct drm_connector *connector)
3192f733d61SEric Anholt {
3202f733d61SEric Anholt 	unsigned int i, num = 0;
3212f733d61SEric Anholt 	static const u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
3222f733d61SEric Anholt 
3232f733d61SEric Anholt 	for (i = 0; i < ARRAY_SIZE(rpi_touchscreen_modes); i++) {
3242f733d61SEric Anholt 		const struct drm_display_mode *m = &rpi_touchscreen_modes[i];
3252f733d61SEric Anholt 		struct drm_display_mode *mode;
3262f733d61SEric Anholt 
327aa6c4364SSam Ravnborg 		mode = drm_mode_duplicate(connector->dev, m);
3282f733d61SEric Anholt 		if (!mode) {
329aa6c4364SSam Ravnborg 			dev_err(panel->dev, "failed to add mode %ux%u@%u\n",
3300425662fSVille Syrjälä 				m->hdisplay, m->vdisplay,
3310425662fSVille Syrjälä 				drm_mode_vrefresh(m));
3322f733d61SEric Anholt 			continue;
3332f733d61SEric Anholt 		}
3342f733d61SEric Anholt 
3352f733d61SEric Anholt 		mode->type |= DRM_MODE_TYPE_DRIVER;
3362f733d61SEric Anholt 
3372f733d61SEric Anholt 		if (i == 0)
3382f733d61SEric Anholt 			mode->type |= DRM_MODE_TYPE_PREFERRED;
3392f733d61SEric Anholt 
3402f733d61SEric Anholt 		drm_mode_set_name(mode);
3412f733d61SEric Anholt 
3422f733d61SEric Anholt 		drm_mode_probed_add(connector, mode);
3432f733d61SEric Anholt 		num++;
3442f733d61SEric Anholt 	}
3452f733d61SEric Anholt 
3462f733d61SEric Anholt 	connector->display_info.bpc = 8;
3472f733d61SEric Anholt 	connector->display_info.width_mm = 154;
3482f733d61SEric Anholt 	connector->display_info.height_mm = 86;
3492f733d61SEric Anholt 	drm_display_info_set_bus_formats(&connector->display_info,
3502f733d61SEric Anholt 					 &bus_format, 1);
3512f733d61SEric Anholt 
3522f733d61SEric Anholt 	return num;
3532f733d61SEric Anholt }
3542f733d61SEric Anholt 
3552f733d61SEric Anholt static const struct drm_panel_funcs rpi_touchscreen_funcs = {
3562f733d61SEric Anholt 	.disable = rpi_touchscreen_disable,
3572f733d61SEric Anholt 	.unprepare = rpi_touchscreen_noop,
3585f18c078SDave Stevenson 	.prepare = rpi_touchscreen_prepare,
3592f733d61SEric Anholt 	.enable = rpi_touchscreen_enable,
3602f733d61SEric Anholt 	.get_modes = rpi_touchscreen_get_modes,
3612f733d61SEric Anholt };
3622f733d61SEric Anholt 
rpi_touchscreen_probe(struct i2c_client * i2c)3630e4dcffdSUwe Kleine-König static int rpi_touchscreen_probe(struct i2c_client *i2c)
3642f733d61SEric Anholt {
3652f733d61SEric Anholt 	struct device *dev = &i2c->dev;
3662f733d61SEric Anholt 	struct rpi_touchscreen *ts;
3672f733d61SEric Anholt 	struct device_node *endpoint, *dsi_host_node;
3682f733d61SEric Anholt 	struct mipi_dsi_host *host;
369c3ee8c65SBernard Zhao 	int ver;
3702f733d61SEric Anholt 	struct mipi_dsi_device_info info = {
3712f733d61SEric Anholt 		.type = RPI_DSI_DRIVER_NAME,
3722f733d61SEric Anholt 		.channel = 0,
3732f733d61SEric Anholt 		.node = NULL,
3742f733d61SEric Anholt 	};
3752f733d61SEric Anholt 
3762f733d61SEric Anholt 	ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
3772f733d61SEric Anholt 	if (!ts)
3782f733d61SEric Anholt 		return -ENOMEM;
3792f733d61SEric Anholt 
3802f733d61SEric Anholt 	i2c_set_clientdata(i2c, ts);
3812f733d61SEric Anholt 
3822f733d61SEric Anholt 	ts->i2c = i2c;
3832f733d61SEric Anholt 
3842f733d61SEric Anholt 	ver = rpi_touchscreen_i2c_read(ts, REG_ID);
3852f733d61SEric Anholt 	if (ver < 0) {
3862f733d61SEric Anholt 		dev_err(dev, "Atmel I2C read failed: %d\n", ver);
3872f733d61SEric Anholt 		return -ENODEV;
3882f733d61SEric Anholt 	}
3892f733d61SEric Anholt 
3902f733d61SEric Anholt 	switch (ver) {
3912f733d61SEric Anholt 	case 0xde: /* ver 1 */
3922f733d61SEric Anholt 	case 0xc3: /* ver 2 */
3932f733d61SEric Anholt 		break;
3942f733d61SEric Anholt 	default:
3952f733d61SEric Anholt 		dev_err(dev, "Unknown Atmel firmware revision: 0x%02x\n", ver);
3962f733d61SEric Anholt 		return -ENODEV;
3972f733d61SEric Anholt 	}
3982f733d61SEric Anholt 
3992f733d61SEric Anholt 	/* Turn off at boot, so we can cleanly sequence powering on. */
4002f733d61SEric Anholt 	rpi_touchscreen_i2c_write(ts, REG_POWERON, 0);
4012f733d61SEric Anholt 
4022f733d61SEric Anholt 	/* Look up the DSI host.  It needs to probe before we do. */
4032f733d61SEric Anholt 	endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
404afd6d4f5SNavid Emamdoost 	if (!endpoint)
405afd6d4f5SNavid Emamdoost 		return -ENODEV;
406afd6d4f5SNavid Emamdoost 
4072f733d61SEric Anholt 	dsi_host_node = of_graph_get_remote_port_parent(endpoint);
408afd6d4f5SNavid Emamdoost 	if (!dsi_host_node)
409afd6d4f5SNavid Emamdoost 		goto error;
410afd6d4f5SNavid Emamdoost 
4112f733d61SEric Anholt 	host = of_find_mipi_dsi_host_by_node(dsi_host_node);
4122f733d61SEric Anholt 	of_node_put(dsi_host_node);
4132f733d61SEric Anholt 	if (!host) {
4142f733d61SEric Anholt 		of_node_put(endpoint);
4152f733d61SEric Anholt 		return -EPROBE_DEFER;
4162f733d61SEric Anholt 	}
4172f733d61SEric Anholt 
4182f733d61SEric Anholt 	info.node = of_graph_get_remote_port(endpoint);
419afd6d4f5SNavid Emamdoost 	if (!info.node)
420afd6d4f5SNavid Emamdoost 		goto error;
421afd6d4f5SNavid Emamdoost 
4222f733d61SEric Anholt 	of_node_put(endpoint);
4232f733d61SEric Anholt 
4242f733d61SEric Anholt 	ts->dsi = mipi_dsi_device_register_full(host, &info);
4252f733d61SEric Anholt 	if (IS_ERR(ts->dsi)) {
4262f733d61SEric Anholt 		dev_err(dev, "DSI device registration failed: %ld\n",
4272f733d61SEric Anholt 			PTR_ERR(ts->dsi));
4282f733d61SEric Anholt 		return PTR_ERR(ts->dsi);
4292f733d61SEric Anholt 	}
4302f733d61SEric Anholt 
4319a2654c0SLaurent Pinchart 	drm_panel_init(&ts->base, dev, &rpi_touchscreen_funcs,
4329a2654c0SLaurent Pinchart 		       DRM_MODE_CONNECTOR_DSI);
4332f733d61SEric Anholt 
4342f733d61SEric Anholt 	/* This appears last, as it's what will unblock the DSI host
4352f733d61SEric Anholt 	 * driver's component bind function.
4362f733d61SEric Anholt 	 */
437c3ee8c65SBernard Zhao 	drm_panel_add(&ts->base);
4382f733d61SEric Anholt 
4392f733d61SEric Anholt 	return 0;
440afd6d4f5SNavid Emamdoost 
441afd6d4f5SNavid Emamdoost error:
442afd6d4f5SNavid Emamdoost 	of_node_put(endpoint);
443afd6d4f5SNavid Emamdoost 	return -ENODEV;
4442f733d61SEric Anholt }
4452f733d61SEric Anholt 
rpi_touchscreen_remove(struct i2c_client * i2c)446ed5c2f5fSUwe Kleine-König static void rpi_touchscreen_remove(struct i2c_client *i2c)
4472f733d61SEric Anholt {
4482f733d61SEric Anholt 	struct rpi_touchscreen *ts = i2c_get_clientdata(i2c);
4492f733d61SEric Anholt 
4502f733d61SEric Anholt 	mipi_dsi_detach(ts->dsi);
4512f733d61SEric Anholt 
4522f733d61SEric Anholt 	drm_panel_remove(&ts->base);
4532f733d61SEric Anholt 
4542f733d61SEric Anholt 	mipi_dsi_device_unregister(ts->dsi);
4552f733d61SEric Anholt }
4562f733d61SEric Anholt 
rpi_touchscreen_dsi_probe(struct mipi_dsi_device * dsi)4572f733d61SEric Anholt static int rpi_touchscreen_dsi_probe(struct mipi_dsi_device *dsi)
4582f733d61SEric Anholt {
4592f733d61SEric Anholt 	int ret;
4602f733d61SEric Anholt 
4612f733d61SEric Anholt 	dsi->mode_flags = (MIPI_DSI_MODE_VIDEO |
4622f733d61SEric Anholt 			   MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
4632f733d61SEric Anholt 			   MIPI_DSI_MODE_LPM);
4642f733d61SEric Anholt 	dsi->format = MIPI_DSI_FMT_RGB888;
4652f733d61SEric Anholt 	dsi->lanes = 1;
4662f733d61SEric Anholt 
4672f733d61SEric Anholt 	ret = mipi_dsi_attach(dsi);
4682f733d61SEric Anholt 
4692f733d61SEric Anholt 	if (ret)
4702f733d61SEric Anholt 		dev_err(&dsi->dev, "failed to attach dsi to host: %d\n", ret);
4712f733d61SEric Anholt 
4722f733d61SEric Anholt 	return ret;
4732f733d61SEric Anholt }
4742f733d61SEric Anholt 
4752f733d61SEric Anholt static struct mipi_dsi_driver rpi_touchscreen_dsi_driver = {
4762f733d61SEric Anholt 	.driver.name = RPI_DSI_DRIVER_NAME,
4772f733d61SEric Anholt 	.probe = rpi_touchscreen_dsi_probe,
4782f733d61SEric Anholt };
4792f733d61SEric Anholt 
4802f733d61SEric Anholt static const struct of_device_id rpi_touchscreen_of_ids[] = {
4812f733d61SEric Anholt 	{ .compatible = "raspberrypi,7inch-touchscreen-panel" },
4822f733d61SEric Anholt 	{ } /* sentinel */
4832f733d61SEric Anholt };
4842f733d61SEric Anholt MODULE_DEVICE_TABLE(of, rpi_touchscreen_of_ids);
4852f733d61SEric Anholt 
4862f733d61SEric Anholt static struct i2c_driver rpi_touchscreen_driver = {
4872f733d61SEric Anholt 	.driver = {
4882f733d61SEric Anholt 		.name = "rpi_touchscreen",
4892f733d61SEric Anholt 		.of_match_table = rpi_touchscreen_of_ids,
4902f733d61SEric Anholt 	},
491*332af828SUwe Kleine-König 	.probe = rpi_touchscreen_probe,
4922f733d61SEric Anholt 	.remove = rpi_touchscreen_remove,
4932f733d61SEric Anholt };
4942f733d61SEric Anholt 
rpi_touchscreen_init(void)4952f733d61SEric Anholt static int __init rpi_touchscreen_init(void)
4962f733d61SEric Anholt {
4972f733d61SEric Anholt 	mipi_dsi_driver_register(&rpi_touchscreen_dsi_driver);
4982f733d61SEric Anholt 	return i2c_add_driver(&rpi_touchscreen_driver);
4992f733d61SEric Anholt }
5002f733d61SEric Anholt module_init(rpi_touchscreen_init);
5012f733d61SEric Anholt 
rpi_touchscreen_exit(void)5022f733d61SEric Anholt static void __exit rpi_touchscreen_exit(void)
5032f733d61SEric Anholt {
5042f733d61SEric Anholt 	i2c_del_driver(&rpi_touchscreen_driver);
5052f733d61SEric Anholt 	mipi_dsi_driver_unregister(&rpi_touchscreen_dsi_driver);
5062f733d61SEric Anholt }
5072f733d61SEric Anholt module_exit(rpi_touchscreen_exit);
5082f733d61SEric Anholt 
5092f733d61SEric Anholt MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
5102f733d61SEric Anholt MODULE_DESCRIPTION("Raspberry Pi 7-inch touchscreen driver");
5112f733d61SEric Anholt MODULE_LICENSE("GPL v2");
512