1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2016, Fuzhou Rockchip Electronics Co., Ltd
4  * Copyright (C) STMicroelectronics SA 2017
5  *
6  * Modified by Philippe Cornu <philippe.cornu@st.com>
7  * This generic Synopsys DesignWare MIPI DSI host driver is based on the
8  * Rockchip version from rockchip/dw-mipi-dsi.c with phy & bridge APIs.
9  */
10 
11 #include <linux/clk.h>
12 #include <linux/component.h>
13 #include <linux/debugfs.h>
14 #include <linux/iopoll.h>
15 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/reset.h>
19 
20 #include <video/mipi_display.h>
21 
22 #include <drm/bridge/dw_mipi_dsi.h>
23 #include <drm/drm_atomic_helper.h>
24 #include <drm/drm_bridge.h>
25 #include <drm/drm_crtc.h>
26 #include <drm/drm_mipi_dsi.h>
27 #include <drm/drm_modes.h>
28 #include <drm/drm_of.h>
29 #include <drm/drm_print.h>
30 
31 #define HWVER_131			0x31333100	/* IP version 1.31 */
32 
33 #define DSI_VERSION			0x00
34 #define VERSION				GENMASK(31, 8)
35 
36 #define DSI_PWR_UP			0x04
37 #define RESET				0
38 #define POWERUP				BIT(0)
39 
40 #define DSI_CLKMGR_CFG			0x08
41 #define TO_CLK_DIVISION(div)		(((div) & 0xff) << 8)
42 #define TX_ESC_CLK_DIVISION(div)	((div) & 0xff)
43 
44 #define DSI_DPI_VCID			0x0c
45 #define DPI_VCID(vcid)			((vcid) & 0x3)
46 
47 #define DSI_DPI_COLOR_CODING		0x10
48 #define LOOSELY18_EN			BIT(8)
49 #define DPI_COLOR_CODING_16BIT_1	0x0
50 #define DPI_COLOR_CODING_16BIT_2	0x1
51 #define DPI_COLOR_CODING_16BIT_3	0x2
52 #define DPI_COLOR_CODING_18BIT_1	0x3
53 #define DPI_COLOR_CODING_18BIT_2	0x4
54 #define DPI_COLOR_CODING_24BIT		0x5
55 
56 #define DSI_DPI_CFG_POL			0x14
57 #define COLORM_ACTIVE_LOW		BIT(4)
58 #define SHUTD_ACTIVE_LOW		BIT(3)
59 #define HSYNC_ACTIVE_LOW		BIT(2)
60 #define VSYNC_ACTIVE_LOW		BIT(1)
61 #define DATAEN_ACTIVE_LOW		BIT(0)
62 
63 #define DSI_DPI_LP_CMD_TIM		0x18
64 #define OUTVACT_LPCMD_TIME(p)		(((p) & 0xff) << 16)
65 #define INVACT_LPCMD_TIME(p)		((p) & 0xff)
66 
67 #define DSI_DBI_VCID			0x1c
68 #define DSI_DBI_CFG			0x20
69 #define DSI_DBI_PARTITIONING_EN		0x24
70 #define DSI_DBI_CMDSIZE			0x28
71 
72 #define DSI_PCKHDL_CFG			0x2c
73 #define CRC_RX_EN			BIT(4)
74 #define ECC_RX_EN			BIT(3)
75 #define BTA_EN				BIT(2)
76 #define EOTP_RX_EN			BIT(1)
77 #define EOTP_TX_EN			BIT(0)
78 
79 #define DSI_GEN_VCID			0x30
80 
81 #define DSI_MODE_CFG			0x34
82 #define ENABLE_VIDEO_MODE		0
83 #define ENABLE_CMD_MODE			BIT(0)
84 
85 #define DSI_VID_MODE_CFG		0x38
86 #define ENABLE_LOW_POWER		(0x3f << 8)
87 #define ENABLE_LOW_POWER_MASK		(0x3f << 8)
88 #define VID_MODE_TYPE_NON_BURST_SYNC_PULSES	0x0
89 #define VID_MODE_TYPE_NON_BURST_SYNC_EVENTS	0x1
90 #define VID_MODE_TYPE_BURST			0x2
91 #define VID_MODE_TYPE_MASK			0x3
92 #define ENABLE_LOW_POWER_CMD		BIT(15)
93 #define VID_MODE_VPG_ENABLE		BIT(16)
94 #define VID_MODE_VPG_MODE		BIT(20)
95 #define VID_MODE_VPG_HORIZONTAL		BIT(24)
96 
97 #define DSI_VID_PKT_SIZE		0x3c
98 #define VID_PKT_SIZE(p)			((p) & 0x3fff)
99 
100 #define DSI_VID_NUM_CHUNKS		0x40
101 #define VID_NUM_CHUNKS(c)		((c) & 0x1fff)
102 
103 #define DSI_VID_NULL_SIZE		0x44
104 #define VID_NULL_SIZE(b)		((b) & 0x1fff)
105 
106 #define DSI_VID_HSA_TIME		0x48
107 #define DSI_VID_HBP_TIME		0x4c
108 #define DSI_VID_HLINE_TIME		0x50
109 #define DSI_VID_VSA_LINES		0x54
110 #define DSI_VID_VBP_LINES		0x58
111 #define DSI_VID_VFP_LINES		0x5c
112 #define DSI_VID_VACTIVE_LINES		0x60
113 #define DSI_EDPI_CMD_SIZE		0x64
114 
115 #define DSI_CMD_MODE_CFG		0x68
116 #define MAX_RD_PKT_SIZE_LP		BIT(24)
117 #define DCS_LW_TX_LP			BIT(19)
118 #define DCS_SR_0P_TX_LP			BIT(18)
119 #define DCS_SW_1P_TX_LP			BIT(17)
120 #define DCS_SW_0P_TX_LP			BIT(16)
121 #define GEN_LW_TX_LP			BIT(14)
122 #define GEN_SR_2P_TX_LP			BIT(13)
123 #define GEN_SR_1P_TX_LP			BIT(12)
124 #define GEN_SR_0P_TX_LP			BIT(11)
125 #define GEN_SW_2P_TX_LP			BIT(10)
126 #define GEN_SW_1P_TX_LP			BIT(9)
127 #define GEN_SW_0P_TX_LP			BIT(8)
128 #define ACK_RQST_EN			BIT(1)
129 #define TEAR_FX_EN			BIT(0)
130 
131 #define CMD_MODE_ALL_LP			(MAX_RD_PKT_SIZE_LP | \
132 					 DCS_LW_TX_LP | \
133 					 DCS_SR_0P_TX_LP | \
134 					 DCS_SW_1P_TX_LP | \
135 					 DCS_SW_0P_TX_LP | \
136 					 GEN_LW_TX_LP | \
137 					 GEN_SR_2P_TX_LP | \
138 					 GEN_SR_1P_TX_LP | \
139 					 GEN_SR_0P_TX_LP | \
140 					 GEN_SW_2P_TX_LP | \
141 					 GEN_SW_1P_TX_LP | \
142 					 GEN_SW_0P_TX_LP)
143 
144 #define DSI_GEN_HDR			0x6c
145 #define DSI_GEN_PLD_DATA		0x70
146 
147 #define DSI_CMD_PKT_STATUS		0x74
148 #define GEN_RD_CMD_BUSY			BIT(6)
149 #define GEN_PLD_R_FULL			BIT(5)
150 #define GEN_PLD_R_EMPTY			BIT(4)
151 #define GEN_PLD_W_FULL			BIT(3)
152 #define GEN_PLD_W_EMPTY			BIT(2)
153 #define GEN_CMD_FULL			BIT(1)
154 #define GEN_CMD_EMPTY			BIT(0)
155 
156 #define DSI_TO_CNT_CFG			0x78
157 #define HSTX_TO_CNT(p)			(((p) & 0xffff) << 16)
158 #define LPRX_TO_CNT(p)			((p) & 0xffff)
159 
160 #define DSI_HS_RD_TO_CNT		0x7c
161 #define DSI_LP_RD_TO_CNT		0x80
162 #define DSI_HS_WR_TO_CNT		0x84
163 #define DSI_LP_WR_TO_CNT		0x88
164 #define DSI_BTA_TO_CNT			0x8c
165 
166 #define DSI_LPCLK_CTRL			0x94
167 #define AUTO_CLKLANE_CTRL		BIT(1)
168 #define PHY_TXREQUESTCLKHS		BIT(0)
169 
170 #define DSI_PHY_TMR_LPCLK_CFG		0x98
171 #define PHY_CLKHS2LP_TIME(lbcc)		(((lbcc) & 0x3ff) << 16)
172 #define PHY_CLKLP2HS_TIME(lbcc)		((lbcc) & 0x3ff)
173 
174 #define DSI_PHY_TMR_CFG			0x9c
175 #define PHY_HS2LP_TIME(lbcc)		(((lbcc) & 0xff) << 24)
176 #define PHY_LP2HS_TIME(lbcc)		(((lbcc) & 0xff) << 16)
177 #define MAX_RD_TIME(lbcc)		((lbcc) & 0x7fff)
178 #define PHY_HS2LP_TIME_V131(lbcc)	(((lbcc) & 0x3ff) << 16)
179 #define PHY_LP2HS_TIME_V131(lbcc)	((lbcc) & 0x3ff)
180 
181 #define DSI_PHY_RSTZ			0xa0
182 #define PHY_DISFORCEPLL			0
183 #define PHY_ENFORCEPLL			BIT(3)
184 #define PHY_DISABLECLK			0
185 #define PHY_ENABLECLK			BIT(2)
186 #define PHY_RSTZ			0
187 #define PHY_UNRSTZ			BIT(1)
188 #define PHY_SHUTDOWNZ			0
189 #define PHY_UNSHUTDOWNZ			BIT(0)
190 
191 #define DSI_PHY_IF_CFG			0xa4
192 #define PHY_STOP_WAIT_TIME(cycle)	(((cycle) & 0xff) << 8)
193 #define N_LANES(n)			(((n) - 1) & 0x3)
194 
195 #define DSI_PHY_ULPS_CTRL		0xa8
196 #define DSI_PHY_TX_TRIGGERS		0xac
197 
198 #define DSI_PHY_STATUS			0xb0
199 #define PHY_STOP_STATE_CLK_LANE		BIT(2)
200 #define PHY_LOCK			BIT(0)
201 
202 #define DSI_PHY_TST_CTRL0		0xb4
203 #define PHY_TESTCLK			BIT(1)
204 #define PHY_UNTESTCLK			0
205 #define PHY_TESTCLR			BIT(0)
206 #define PHY_UNTESTCLR			0
207 
208 #define DSI_PHY_TST_CTRL1		0xb8
209 #define PHY_TESTEN			BIT(16)
210 #define PHY_UNTESTEN			0
211 #define PHY_TESTDOUT(n)			(((n) & 0xff) << 8)
212 #define PHY_TESTDIN(n)			((n) & 0xff)
213 
214 #define DSI_INT_ST0			0xbc
215 #define DSI_INT_ST1			0xc0
216 #define DSI_INT_MSK0			0xc4
217 #define DSI_INT_MSK1			0xc8
218 
219 #define DSI_PHY_TMR_RD_CFG		0xf4
220 #define MAX_RD_TIME_V131(lbcc)		((lbcc) & 0x7fff)
221 
222 #define PHY_STATUS_TIMEOUT_US		10000
223 #define CMD_PKT_STATUS_TIMEOUT_US	20000
224 
225 #ifdef CONFIG_DEBUG_FS
226 #define VPG_DEFS(name, dsi) \
227 	((void __force *)&((*dsi).vpg_defs.name))
228 
229 #define REGISTER(name, mask, dsi) \
230 	{ #name, VPG_DEFS(name, dsi), mask, dsi }
231 
232 struct debugfs_entries {
233 	const char				*name;
234 	bool					*reg;
235 	u32					mask;
236 	struct dw_mipi_dsi			*dsi;
237 };
238 #endif /* CONFIG_DEBUG_FS */
239 
240 struct dw_mipi_dsi {
241 	struct drm_bridge bridge;
242 	struct mipi_dsi_host dsi_host;
243 	struct drm_bridge *panel_bridge;
244 	struct device *dev;
245 	void __iomem *base;
246 
247 	struct clk *pclk;
248 
249 	bool device_found;
250 	unsigned int lane_mbps; /* per lane */
251 	u32 channel;
252 	u32 lanes;
253 	u32 format;
254 	unsigned long mode_flags;
255 
256 #ifdef CONFIG_DEBUG_FS
257 	struct dentry *debugfs;
258 	struct debugfs_entries *debugfs_vpg;
259 	struct {
260 		bool vpg;
261 		bool vpg_horizontal;
262 		bool vpg_ber_pattern;
263 	} vpg_defs;
264 #endif /* CONFIG_DEBUG_FS */
265 
266 	struct dw_mipi_dsi *master; /* dual-dsi master ptr */
267 	struct dw_mipi_dsi *slave; /* dual-dsi slave ptr */
268 
269 	const struct dw_mipi_dsi_plat_data *plat_data;
270 };
271 
272 /*
273  * Check if either a link to a master or slave is present
274  */
275 static inline bool dw_mipi_is_dual_mode(struct dw_mipi_dsi *dsi)
276 {
277 	return dsi->slave || dsi->master;
278 }
279 
280 /*
281  * The controller should generate 2 frames before
282  * preparing the peripheral.
283  */
284 static void dw_mipi_dsi_wait_for_two_frames(const struct drm_display_mode *mode)
285 {
286 	int refresh, two_frames;
287 
288 	refresh = drm_mode_vrefresh(mode);
289 	two_frames = DIV_ROUND_UP(MSEC_PER_SEC, refresh) * 2;
290 	msleep(two_frames);
291 }
292 
293 static inline struct dw_mipi_dsi *host_to_dsi(struct mipi_dsi_host *host)
294 {
295 	return container_of(host, struct dw_mipi_dsi, dsi_host);
296 }
297 
298 static inline struct dw_mipi_dsi *bridge_to_dsi(struct drm_bridge *bridge)
299 {
300 	return container_of(bridge, struct dw_mipi_dsi, bridge);
301 }
302 
303 static inline void dsi_write(struct dw_mipi_dsi *dsi, u32 reg, u32 val)
304 {
305 	writel(val, dsi->base + reg);
306 }
307 
308 static inline u32 dsi_read(struct dw_mipi_dsi *dsi, u32 reg)
309 {
310 	return readl(dsi->base + reg);
311 }
312 
313 static int dw_mipi_dsi_panel_or_bridge(struct dw_mipi_dsi *dsi,
314 				       struct device_node *node)
315 {
316 	struct drm_bridge *bridge;
317 	struct drm_panel *panel;
318 	int ret;
319 
320 	ret = drm_of_find_panel_or_bridge(node, 1, 0, &panel, &bridge);
321 	if (ret)
322 		return ret;
323 
324 	if (panel) {
325 		bridge = drm_panel_bridge_add_typed(panel,
326 						    DRM_MODE_CONNECTOR_DSI);
327 		if (IS_ERR(bridge))
328 			return PTR_ERR(bridge);
329 	}
330 
331 	dsi->panel_bridge = bridge;
332 
333 	if (!dsi->panel_bridge)
334 		return -EPROBE_DEFER;
335 
336 	return 0;
337 }
338 
339 static int dw_mipi_dsi_host_attach(struct mipi_dsi_host *host,
340 				   struct mipi_dsi_device *device)
341 {
342 	struct dw_mipi_dsi *dsi = host_to_dsi(host);
343 	const struct dw_mipi_dsi_plat_data *pdata = dsi->plat_data;
344 	int ret;
345 
346 	if (device->lanes > dsi->plat_data->max_data_lanes) {
347 		dev_err(dsi->dev, "the number of data lanes(%u) is too many\n",
348 			device->lanes);
349 		return -EINVAL;
350 	}
351 
352 	dsi->lanes = device->lanes;
353 	dsi->channel = device->channel;
354 	dsi->format = device->format;
355 	dsi->mode_flags = device->mode_flags;
356 
357 	if (!dsi->device_found) {
358 		ret = dw_mipi_dsi_panel_or_bridge(dsi, host->dev->of_node);
359 		if (ret)
360 			return ret;
361 
362 		dsi->device_found = true;
363 	}
364 
365 	if (pdata->host_ops && pdata->host_ops->attach) {
366 		ret = pdata->host_ops->attach(pdata->priv_data, device);
367 		if (ret < 0)
368 			return ret;
369 	}
370 
371 	return 0;
372 }
373 
374 static int dw_mipi_dsi_host_detach(struct mipi_dsi_host *host,
375 				   struct mipi_dsi_device *device)
376 {
377 	struct dw_mipi_dsi *dsi = host_to_dsi(host);
378 	const struct dw_mipi_dsi_plat_data *pdata = dsi->plat_data;
379 	int ret;
380 
381 	if (pdata->host_ops && pdata->host_ops->detach) {
382 		ret = pdata->host_ops->detach(pdata->priv_data, device);
383 		if (ret < 0)
384 			return ret;
385 	}
386 
387 	drm_of_panel_bridge_remove(host->dev->of_node, 1, 0);
388 
389 	drm_bridge_remove(&dsi->bridge);
390 
391 	return 0;
392 }
393 
394 static void dw_mipi_message_config(struct dw_mipi_dsi *dsi,
395 				   const struct mipi_dsi_msg *msg)
396 {
397 	bool lpm = msg->flags & MIPI_DSI_MSG_USE_LPM;
398 	u32 val = 0;
399 
400 	/*
401 	 * TODO dw drv improvements
402 	 * largest packet sizes during hfp or during vsa/vpb/vfp
403 	 * should be computed according to byte lane, lane number and only
404 	 * if sending lp cmds in high speed is enable (PHY_TXREQUESTCLKHS)
405 	 */
406 	dsi_write(dsi, DSI_DPI_LP_CMD_TIM, OUTVACT_LPCMD_TIME(16)
407 		  | INVACT_LPCMD_TIME(4));
408 
409 	if (msg->flags & MIPI_DSI_MSG_REQ_ACK)
410 		val |= ACK_RQST_EN;
411 	if (lpm)
412 		val |= CMD_MODE_ALL_LP;
413 
414 	dsi_write(dsi, DSI_CMD_MODE_CFG, val);
415 
416 	val = dsi_read(dsi, DSI_VID_MODE_CFG);
417 	if (lpm)
418 		val |= ENABLE_LOW_POWER_CMD;
419 	else
420 		val &= ~ENABLE_LOW_POWER_CMD;
421 	dsi_write(dsi, DSI_VID_MODE_CFG, val);
422 }
423 
424 static int dw_mipi_dsi_gen_pkt_hdr_write(struct dw_mipi_dsi *dsi, u32 hdr_val)
425 {
426 	int ret;
427 	u32 val, mask;
428 
429 	ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
430 				 val, !(val & GEN_CMD_FULL), 1000,
431 				 CMD_PKT_STATUS_TIMEOUT_US);
432 	if (ret) {
433 		dev_err(dsi->dev, "failed to get available command FIFO\n");
434 		return ret;
435 	}
436 
437 	dsi_write(dsi, DSI_GEN_HDR, hdr_val);
438 
439 	mask = GEN_CMD_EMPTY | GEN_PLD_W_EMPTY;
440 	ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
441 				 val, (val & mask) == mask,
442 				 1000, CMD_PKT_STATUS_TIMEOUT_US);
443 	if (ret) {
444 		dev_err(dsi->dev, "failed to write command FIFO\n");
445 		return ret;
446 	}
447 
448 	return 0;
449 }
450 
451 static int dw_mipi_dsi_write(struct dw_mipi_dsi *dsi,
452 			     const struct mipi_dsi_packet *packet)
453 {
454 	const u8 *tx_buf = packet->payload;
455 	int len = packet->payload_length, pld_data_bytes = sizeof(u32), ret;
456 	__le32 word;
457 	u32 val;
458 
459 	while (len) {
460 		if (len < pld_data_bytes) {
461 			word = 0;
462 			memcpy(&word, tx_buf, len);
463 			dsi_write(dsi, DSI_GEN_PLD_DATA, le32_to_cpu(word));
464 			len = 0;
465 		} else {
466 			memcpy(&word, tx_buf, pld_data_bytes);
467 			dsi_write(dsi, DSI_GEN_PLD_DATA, le32_to_cpu(word));
468 			tx_buf += pld_data_bytes;
469 			len -= pld_data_bytes;
470 		}
471 
472 		ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
473 					 val, !(val & GEN_PLD_W_FULL), 1000,
474 					 CMD_PKT_STATUS_TIMEOUT_US);
475 		if (ret) {
476 			dev_err(dsi->dev,
477 				"failed to get available write payload FIFO\n");
478 			return ret;
479 		}
480 	}
481 
482 	word = 0;
483 	memcpy(&word, packet->header, sizeof(packet->header));
484 	return dw_mipi_dsi_gen_pkt_hdr_write(dsi, le32_to_cpu(word));
485 }
486 
487 static int dw_mipi_dsi_read(struct dw_mipi_dsi *dsi,
488 			    const struct mipi_dsi_msg *msg)
489 {
490 	int i, j, ret, len = msg->rx_len;
491 	u8 *buf = msg->rx_buf;
492 	u32 val;
493 
494 	/* Wait end of the read operation */
495 	ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
496 				 val, !(val & GEN_RD_CMD_BUSY),
497 				 1000, CMD_PKT_STATUS_TIMEOUT_US);
498 	if (ret) {
499 		dev_err(dsi->dev, "Timeout during read operation\n");
500 		return ret;
501 	}
502 
503 	for (i = 0; i < len; i += 4) {
504 		/* Read fifo must not be empty before all bytes are read */
505 		ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
506 					 val, !(val & GEN_PLD_R_EMPTY),
507 					 1000, CMD_PKT_STATUS_TIMEOUT_US);
508 		if (ret) {
509 			dev_err(dsi->dev, "Read payload FIFO is empty\n");
510 			return ret;
511 		}
512 
513 		val = dsi_read(dsi, DSI_GEN_PLD_DATA);
514 		for (j = 0; j < 4 && j + i < len; j++)
515 			buf[i + j] = val >> (8 * j);
516 	}
517 
518 	return ret;
519 }
520 
521 static ssize_t dw_mipi_dsi_host_transfer(struct mipi_dsi_host *host,
522 					 const struct mipi_dsi_msg *msg)
523 {
524 	struct dw_mipi_dsi *dsi = host_to_dsi(host);
525 	struct mipi_dsi_packet packet;
526 	int ret, nb_bytes;
527 
528 	ret = mipi_dsi_create_packet(&packet, msg);
529 	if (ret) {
530 		dev_err(dsi->dev, "failed to create packet: %d\n", ret);
531 		return ret;
532 	}
533 
534 	dw_mipi_message_config(dsi, msg);
535 	if (dsi->slave)
536 		dw_mipi_message_config(dsi->slave, msg);
537 
538 	ret = dw_mipi_dsi_write(dsi, &packet);
539 	if (ret)
540 		return ret;
541 	if (dsi->slave) {
542 		ret = dw_mipi_dsi_write(dsi->slave, &packet);
543 		if (ret)
544 			return ret;
545 	}
546 
547 	if (msg->rx_buf && msg->rx_len) {
548 		ret = dw_mipi_dsi_read(dsi, msg);
549 		if (ret)
550 			return ret;
551 		nb_bytes = msg->rx_len;
552 	} else {
553 		nb_bytes = packet.size;
554 	}
555 
556 	return nb_bytes;
557 }
558 
559 static const struct mipi_dsi_host_ops dw_mipi_dsi_host_ops = {
560 	.attach = dw_mipi_dsi_host_attach,
561 	.detach = dw_mipi_dsi_host_detach,
562 	.transfer = dw_mipi_dsi_host_transfer,
563 };
564 
565 static void dw_mipi_dsi_video_mode_config(struct dw_mipi_dsi *dsi)
566 {
567 	u32 val;
568 
569 	/*
570 	 * TODO dw drv improvements
571 	 * enabling low power is panel-dependent, we should use the
572 	 * panel configuration here...
573 	 */
574 	val = ENABLE_LOW_POWER;
575 
576 	if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
577 		val |= VID_MODE_TYPE_BURST;
578 	else if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
579 		val |= VID_MODE_TYPE_NON_BURST_SYNC_PULSES;
580 	else
581 		val |= VID_MODE_TYPE_NON_BURST_SYNC_EVENTS;
582 
583 #ifdef CONFIG_DEBUG_FS
584 	if (dsi->vpg_defs.vpg) {
585 		val |= VID_MODE_VPG_ENABLE;
586 		val |= dsi->vpg_defs.vpg_horizontal ?
587 		       VID_MODE_VPG_HORIZONTAL : 0;
588 		val |= dsi->vpg_defs.vpg_ber_pattern ? VID_MODE_VPG_MODE : 0;
589 	}
590 #endif /* CONFIG_DEBUG_FS */
591 
592 	dsi_write(dsi, DSI_VID_MODE_CFG, val);
593 }
594 
595 static void dw_mipi_dsi_set_mode(struct dw_mipi_dsi *dsi,
596 				 unsigned long mode_flags)
597 {
598 	u32 val;
599 
600 	dsi_write(dsi, DSI_PWR_UP, RESET);
601 
602 	if (mode_flags & MIPI_DSI_MODE_VIDEO) {
603 		dsi_write(dsi, DSI_MODE_CFG, ENABLE_VIDEO_MODE);
604 		dw_mipi_dsi_video_mode_config(dsi);
605 	} else {
606 		dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
607 	}
608 
609 	val = PHY_TXREQUESTCLKHS;
610 	if (dsi->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)
611 		val |= AUTO_CLKLANE_CTRL;
612 	dsi_write(dsi, DSI_LPCLK_CTRL, val);
613 
614 	dsi_write(dsi, DSI_PWR_UP, POWERUP);
615 }
616 
617 static void dw_mipi_dsi_disable(struct dw_mipi_dsi *dsi)
618 {
619 	dsi_write(dsi, DSI_PWR_UP, RESET);
620 	dsi_write(dsi, DSI_PHY_RSTZ, PHY_RSTZ);
621 }
622 
623 static void dw_mipi_dsi_init(struct dw_mipi_dsi *dsi)
624 {
625 	const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
626 	unsigned int esc_rate; /* in MHz */
627 	u32 esc_clk_division;
628 	int ret;
629 
630 	/*
631 	 * The maximum permitted escape clock is 20MHz and it is derived from
632 	 * lanebyteclk, which is running at "lane_mbps / 8".
633 	 */
634 	if (phy_ops->get_esc_clk_rate) {
635 		ret = phy_ops->get_esc_clk_rate(dsi->plat_data->priv_data,
636 						&esc_rate);
637 		if (ret)
638 			DRM_DEBUG_DRIVER("Phy get_esc_clk_rate() failed\n");
639 	} else
640 		esc_rate = 20; /* Default to 20MHz */
641 
642 	/*
643 	 * We want :
644 	 *     (lane_mbps >> 3) / esc_clk_division < X
645 	 * which is:
646 	 *     (lane_mbps >> 3) / X > esc_clk_division
647 	 */
648 	esc_clk_division = (dsi->lane_mbps >> 3) / esc_rate + 1;
649 
650 	dsi_write(dsi, DSI_PWR_UP, RESET);
651 
652 	/*
653 	 * TODO dw drv improvements
654 	 * timeout clock division should be computed with the
655 	 * high speed transmission counter timeout and byte lane...
656 	 */
657 	dsi_write(dsi, DSI_CLKMGR_CFG, TO_CLK_DIVISION(10) |
658 		  TX_ESC_CLK_DIVISION(esc_clk_division));
659 }
660 
661 static void dw_mipi_dsi_dpi_config(struct dw_mipi_dsi *dsi,
662 				   const struct drm_display_mode *mode)
663 {
664 	u32 val = 0, color = 0;
665 
666 	switch (dsi->format) {
667 	case MIPI_DSI_FMT_RGB888:
668 		color = DPI_COLOR_CODING_24BIT;
669 		break;
670 	case MIPI_DSI_FMT_RGB666:
671 		color = DPI_COLOR_CODING_18BIT_2 | LOOSELY18_EN;
672 		break;
673 	case MIPI_DSI_FMT_RGB666_PACKED:
674 		color = DPI_COLOR_CODING_18BIT_1;
675 		break;
676 	case MIPI_DSI_FMT_RGB565:
677 		color = DPI_COLOR_CODING_16BIT_1;
678 		break;
679 	}
680 
681 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
682 		val |= VSYNC_ACTIVE_LOW;
683 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
684 		val |= HSYNC_ACTIVE_LOW;
685 
686 	dsi_write(dsi, DSI_DPI_VCID, DPI_VCID(dsi->channel));
687 	dsi_write(dsi, DSI_DPI_COLOR_CODING, color);
688 	dsi_write(dsi, DSI_DPI_CFG_POL, val);
689 }
690 
691 static void dw_mipi_dsi_packet_handler_config(struct dw_mipi_dsi *dsi)
692 {
693 	dsi_write(dsi, DSI_PCKHDL_CFG, CRC_RX_EN | ECC_RX_EN | BTA_EN);
694 }
695 
696 static void dw_mipi_dsi_video_packet_config(struct dw_mipi_dsi *dsi,
697 					    const struct drm_display_mode *mode)
698 {
699 	/*
700 	 * TODO dw drv improvements
701 	 * only burst mode is supported here. For non-burst video modes,
702 	 * we should compute DSI_VID_PKT_SIZE, DSI_VCCR.NUMC &
703 	 * DSI_VNPCR.NPSIZE... especially because this driver supports
704 	 * non-burst video modes, see dw_mipi_dsi_video_mode_config()...
705 	 */
706 
707 	dsi_write(dsi, DSI_VID_PKT_SIZE,
708 		       dw_mipi_is_dual_mode(dsi) ?
709 				VID_PKT_SIZE(mode->hdisplay / 2) :
710 				VID_PKT_SIZE(mode->hdisplay));
711 }
712 
713 static void dw_mipi_dsi_command_mode_config(struct dw_mipi_dsi *dsi)
714 {
715 	/*
716 	 * TODO dw drv improvements
717 	 * compute high speed transmission counter timeout according
718 	 * to the timeout clock division (TO_CLK_DIVISION) and byte lane...
719 	 */
720 	dsi_write(dsi, DSI_TO_CNT_CFG, HSTX_TO_CNT(1000) | LPRX_TO_CNT(1000));
721 	/*
722 	 * TODO dw drv improvements
723 	 * the Bus-Turn-Around Timeout Counter should be computed
724 	 * according to byte lane...
725 	 */
726 	dsi_write(dsi, DSI_BTA_TO_CNT, 0xd00);
727 	dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
728 }
729 
730 /* Get lane byte clock cycles. */
731 static u32 dw_mipi_dsi_get_hcomponent_lbcc(struct dw_mipi_dsi *dsi,
732 					   const struct drm_display_mode *mode,
733 					   u32 hcomponent)
734 {
735 	u32 frac, lbcc;
736 
737 	lbcc = hcomponent * dsi->lane_mbps * MSEC_PER_SEC / 8;
738 
739 	frac = lbcc % mode->clock;
740 	lbcc = lbcc / mode->clock;
741 	if (frac)
742 		lbcc++;
743 
744 	return lbcc;
745 }
746 
747 static void dw_mipi_dsi_line_timer_config(struct dw_mipi_dsi *dsi,
748 					  const struct drm_display_mode *mode)
749 {
750 	u32 htotal, hsa, hbp, lbcc;
751 
752 	htotal = mode->htotal;
753 	hsa = mode->hsync_end - mode->hsync_start;
754 	hbp = mode->htotal - mode->hsync_end;
755 
756 	/*
757 	 * TODO dw drv improvements
758 	 * computations below may be improved...
759 	 */
760 	lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, htotal);
761 	dsi_write(dsi, DSI_VID_HLINE_TIME, lbcc);
762 
763 	lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hsa);
764 	dsi_write(dsi, DSI_VID_HSA_TIME, lbcc);
765 
766 	lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hbp);
767 	dsi_write(dsi, DSI_VID_HBP_TIME, lbcc);
768 }
769 
770 static void dw_mipi_dsi_vertical_timing_config(struct dw_mipi_dsi *dsi,
771 					const struct drm_display_mode *mode)
772 {
773 	u32 vactive, vsa, vfp, vbp;
774 
775 	vactive = mode->vdisplay;
776 	vsa = mode->vsync_end - mode->vsync_start;
777 	vfp = mode->vsync_start - mode->vdisplay;
778 	vbp = mode->vtotal - mode->vsync_end;
779 
780 	dsi_write(dsi, DSI_VID_VACTIVE_LINES, vactive);
781 	dsi_write(dsi, DSI_VID_VSA_LINES, vsa);
782 	dsi_write(dsi, DSI_VID_VFP_LINES, vfp);
783 	dsi_write(dsi, DSI_VID_VBP_LINES, vbp);
784 }
785 
786 static void dw_mipi_dsi_dphy_timing_config(struct dw_mipi_dsi *dsi)
787 {
788 	const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
789 	struct dw_mipi_dsi_dphy_timing timing;
790 	u32 hw_version;
791 	int ret;
792 
793 	ret = phy_ops->get_timing(dsi->plat_data->priv_data,
794 				  dsi->lane_mbps, &timing);
795 	if (ret)
796 		DRM_DEV_ERROR(dsi->dev, "Retrieving phy timings failed\n");
797 
798 	/*
799 	 * TODO dw drv improvements
800 	 * data & clock lane timers should be computed according to panel
801 	 * blankings and to the automatic clock lane control mode...
802 	 * note: DSI_PHY_TMR_CFG.MAX_RD_TIME should be in line with
803 	 * DSI_CMD_MODE_CFG.MAX_RD_PKT_SIZE_LP (see CMD_MODE_ALL_LP)
804 	 */
805 
806 	hw_version = dsi_read(dsi, DSI_VERSION) & VERSION;
807 
808 	if (hw_version >= HWVER_131) {
809 		dsi_write(dsi, DSI_PHY_TMR_CFG,
810 			  PHY_HS2LP_TIME_V131(timing.data_hs2lp) |
811 			  PHY_LP2HS_TIME_V131(timing.data_lp2hs));
812 		dsi_write(dsi, DSI_PHY_TMR_RD_CFG, MAX_RD_TIME_V131(10000));
813 	} else {
814 		dsi_write(dsi, DSI_PHY_TMR_CFG,
815 			  PHY_HS2LP_TIME(timing.data_hs2lp) |
816 			  PHY_LP2HS_TIME(timing.data_lp2hs) |
817 			  MAX_RD_TIME(10000));
818 	}
819 
820 	dsi_write(dsi, DSI_PHY_TMR_LPCLK_CFG,
821 		  PHY_CLKHS2LP_TIME(timing.clk_hs2lp) |
822 		  PHY_CLKLP2HS_TIME(timing.clk_lp2hs));
823 }
824 
825 static void dw_mipi_dsi_dphy_interface_config(struct dw_mipi_dsi *dsi)
826 {
827 	/*
828 	 * TODO dw drv improvements
829 	 * stop wait time should be the maximum between host dsi
830 	 * and panel stop wait times
831 	 */
832 	dsi_write(dsi, DSI_PHY_IF_CFG, PHY_STOP_WAIT_TIME(0x20) |
833 		  N_LANES(dsi->lanes));
834 }
835 
836 static void dw_mipi_dsi_dphy_init(struct dw_mipi_dsi *dsi)
837 {
838 	/* Clear PHY state */
839 	dsi_write(dsi, DSI_PHY_RSTZ, PHY_DISFORCEPLL | PHY_DISABLECLK
840 		  | PHY_RSTZ | PHY_SHUTDOWNZ);
841 	dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLR);
842 	dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_TESTCLR);
843 	dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLR);
844 }
845 
846 static void dw_mipi_dsi_dphy_enable(struct dw_mipi_dsi *dsi)
847 {
848 	u32 val;
849 	int ret;
850 
851 	dsi_write(dsi, DSI_PHY_RSTZ, PHY_ENFORCEPLL | PHY_ENABLECLK |
852 		  PHY_UNRSTZ | PHY_UNSHUTDOWNZ);
853 
854 	ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS, val,
855 				 val & PHY_LOCK, 1000, PHY_STATUS_TIMEOUT_US);
856 	if (ret)
857 		DRM_DEBUG_DRIVER("failed to wait phy lock state\n");
858 
859 	ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS,
860 				 val, val & PHY_STOP_STATE_CLK_LANE, 1000,
861 				 PHY_STATUS_TIMEOUT_US);
862 	if (ret)
863 		DRM_DEBUG_DRIVER("failed to wait phy clk lane stop state\n");
864 }
865 
866 static void dw_mipi_dsi_clear_err(struct dw_mipi_dsi *dsi)
867 {
868 	dsi_read(dsi, DSI_INT_ST0);
869 	dsi_read(dsi, DSI_INT_ST1);
870 	dsi_write(dsi, DSI_INT_MSK0, 0);
871 	dsi_write(dsi, DSI_INT_MSK1, 0);
872 }
873 
874 static void dw_mipi_dsi_bridge_post_atomic_disable(struct drm_bridge *bridge,
875 						   struct drm_bridge_state *old_bridge_state)
876 {
877 	struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
878 	const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
879 
880 	/*
881 	 * Switch to command mode before panel-bridge post_disable &
882 	 * panel unprepare.
883 	 * Note: panel-bridge disable & panel disable has been called
884 	 * before by the drm framework.
885 	 */
886 	dw_mipi_dsi_set_mode(dsi, 0);
887 
888 	/*
889 	 * TODO Only way found to call panel-bridge post_disable &
890 	 * panel unprepare before the dsi "final" disable...
891 	 * This needs to be fixed in the drm_bridge framework and the API
892 	 * needs to be updated to manage our own call chains...
893 	 */
894 	if (dsi->panel_bridge->funcs->post_disable)
895 		dsi->panel_bridge->funcs->post_disable(dsi->panel_bridge);
896 
897 	if (phy_ops->power_off)
898 		phy_ops->power_off(dsi->plat_data->priv_data);
899 
900 	if (dsi->slave) {
901 		dw_mipi_dsi_disable(dsi->slave);
902 		clk_disable_unprepare(dsi->slave->pclk);
903 		pm_runtime_put(dsi->slave->dev);
904 	}
905 	dw_mipi_dsi_disable(dsi);
906 
907 	clk_disable_unprepare(dsi->pclk);
908 	pm_runtime_put(dsi->dev);
909 }
910 
911 static unsigned int dw_mipi_dsi_get_lanes(struct dw_mipi_dsi *dsi)
912 {
913 	/* this instance is the slave, so add the master's lanes */
914 	if (dsi->master)
915 		return dsi->master->lanes + dsi->lanes;
916 
917 	/* this instance is the master, so add the slave's lanes */
918 	if (dsi->slave)
919 		return dsi->lanes + dsi->slave->lanes;
920 
921 	/* single-dsi, so no other instance to consider */
922 	return dsi->lanes;
923 }
924 
925 static void dw_mipi_dsi_mode_set(struct dw_mipi_dsi *dsi,
926 				 const struct drm_display_mode *adjusted_mode)
927 {
928 	const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
929 	void *priv_data = dsi->plat_data->priv_data;
930 	int ret;
931 	u32 lanes = dw_mipi_dsi_get_lanes(dsi);
932 
933 	clk_prepare_enable(dsi->pclk);
934 
935 	ret = phy_ops->get_lane_mbps(priv_data, adjusted_mode, dsi->mode_flags,
936 				     lanes, dsi->format, &dsi->lane_mbps);
937 	if (ret)
938 		DRM_DEBUG_DRIVER("Phy get_lane_mbps() failed\n");
939 
940 	pm_runtime_get_sync(dsi->dev);
941 	dw_mipi_dsi_init(dsi);
942 	dw_mipi_dsi_dpi_config(dsi, adjusted_mode);
943 	dw_mipi_dsi_packet_handler_config(dsi);
944 	dw_mipi_dsi_video_mode_config(dsi);
945 	dw_mipi_dsi_video_packet_config(dsi, adjusted_mode);
946 	dw_mipi_dsi_command_mode_config(dsi);
947 	dw_mipi_dsi_line_timer_config(dsi, adjusted_mode);
948 	dw_mipi_dsi_vertical_timing_config(dsi, adjusted_mode);
949 
950 	dw_mipi_dsi_dphy_init(dsi);
951 	dw_mipi_dsi_dphy_timing_config(dsi);
952 	dw_mipi_dsi_dphy_interface_config(dsi);
953 
954 	dw_mipi_dsi_clear_err(dsi);
955 
956 	ret = phy_ops->init(priv_data);
957 	if (ret)
958 		DRM_DEBUG_DRIVER("Phy init() failed\n");
959 
960 	dw_mipi_dsi_dphy_enable(dsi);
961 
962 	dw_mipi_dsi_wait_for_two_frames(adjusted_mode);
963 
964 	/* Switch to cmd mode for panel-bridge pre_enable & panel prepare */
965 	dw_mipi_dsi_set_mode(dsi, 0);
966 
967 	if (phy_ops->power_on)
968 		phy_ops->power_on(dsi->plat_data->priv_data);
969 }
970 
971 static void dw_mipi_dsi_bridge_mode_set(struct drm_bridge *bridge,
972 					const struct drm_display_mode *mode,
973 					const struct drm_display_mode *adjusted_mode)
974 {
975 	struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
976 
977 	dw_mipi_dsi_mode_set(dsi, adjusted_mode);
978 	if (dsi->slave)
979 		dw_mipi_dsi_mode_set(dsi->slave, adjusted_mode);
980 }
981 
982 static void dw_mipi_dsi_bridge_atomic_enable(struct drm_bridge *bridge,
983 					     struct drm_bridge_state *old_bridge_state)
984 {
985 	struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
986 
987 	/* Switch to video mode for panel-bridge enable & panel enable */
988 	dw_mipi_dsi_set_mode(dsi, MIPI_DSI_MODE_VIDEO);
989 	if (dsi->slave)
990 		dw_mipi_dsi_set_mode(dsi->slave, MIPI_DSI_MODE_VIDEO);
991 }
992 
993 static enum drm_mode_status
994 dw_mipi_dsi_bridge_mode_valid(struct drm_bridge *bridge,
995 			      const struct drm_display_info *info,
996 			      const struct drm_display_mode *mode)
997 {
998 	struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
999 	const struct dw_mipi_dsi_plat_data *pdata = dsi->plat_data;
1000 	enum drm_mode_status mode_status = MODE_OK;
1001 
1002 	if (pdata->mode_valid)
1003 		mode_status = pdata->mode_valid(pdata->priv_data, mode,
1004 						dsi->mode_flags,
1005 						dw_mipi_dsi_get_lanes(dsi),
1006 						dsi->format);
1007 
1008 	return mode_status;
1009 }
1010 
1011 static int dw_mipi_dsi_bridge_attach(struct drm_bridge *bridge,
1012 				     enum drm_bridge_attach_flags flags)
1013 {
1014 	struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
1015 
1016 	if (!bridge->encoder) {
1017 		DRM_ERROR("Parent encoder object not found\n");
1018 		return -ENODEV;
1019 	}
1020 
1021 	/* Set the encoder type as caller does not know it */
1022 	bridge->encoder->encoder_type = DRM_MODE_ENCODER_DSI;
1023 
1024 	if (!dsi->device_found) {
1025 		int ret;
1026 
1027 		ret = dw_mipi_dsi_panel_or_bridge(dsi, dsi->dev->of_node);
1028 		if (ret)
1029 			return ret;
1030 
1031 		dsi->device_found = true;
1032 	}
1033 
1034 	/* Attach the panel-bridge to the dsi bridge */
1035 	return drm_bridge_attach(bridge->encoder, dsi->panel_bridge, bridge,
1036 				 flags);
1037 }
1038 
1039 static const struct drm_bridge_funcs dw_mipi_dsi_bridge_funcs = {
1040 	.atomic_duplicate_state	= drm_atomic_helper_bridge_duplicate_state,
1041 	.atomic_destroy_state	= drm_atomic_helper_bridge_destroy_state,
1042 	.atomic_reset		= drm_atomic_helper_bridge_reset,
1043 	.atomic_enable		= dw_mipi_dsi_bridge_atomic_enable,
1044 	.atomic_post_disable	= dw_mipi_dsi_bridge_post_atomic_disable,
1045 	.mode_set		= dw_mipi_dsi_bridge_mode_set,
1046 	.mode_valid		= dw_mipi_dsi_bridge_mode_valid,
1047 	.attach			= dw_mipi_dsi_bridge_attach,
1048 };
1049 
1050 #ifdef CONFIG_DEBUG_FS
1051 
1052 static int dw_mipi_dsi_debugfs_write(void *data, u64 val)
1053 {
1054 	struct debugfs_entries *vpg = data;
1055 	struct dw_mipi_dsi *dsi;
1056 	u32 mode_cfg;
1057 
1058 	if (!vpg)
1059 		return -ENODEV;
1060 
1061 	dsi = vpg->dsi;
1062 
1063 	*vpg->reg = (bool)val;
1064 
1065 	mode_cfg = dsi_read(dsi, DSI_VID_MODE_CFG);
1066 
1067 	if (*vpg->reg)
1068 		mode_cfg |= vpg->mask;
1069 	else
1070 		mode_cfg &= ~vpg->mask;
1071 
1072 	dsi_write(dsi, DSI_VID_MODE_CFG, mode_cfg);
1073 
1074 	return 0;
1075 }
1076 
1077 static int dw_mipi_dsi_debugfs_show(void *data, u64 *val)
1078 {
1079 	struct debugfs_entries *vpg = data;
1080 
1081 	if (!vpg)
1082 		return -ENODEV;
1083 
1084 	*val = *vpg->reg;
1085 
1086 	return 0;
1087 }
1088 
1089 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, dw_mipi_dsi_debugfs_show,
1090 			 dw_mipi_dsi_debugfs_write, "%llu\n");
1091 
1092 static void debugfs_create_files(void *data)
1093 {
1094 	struct dw_mipi_dsi *dsi = data;
1095 	struct debugfs_entries debugfs[] = {
1096 		REGISTER(vpg, VID_MODE_VPG_ENABLE, dsi),
1097 		REGISTER(vpg_horizontal, VID_MODE_VPG_HORIZONTAL, dsi),
1098 		REGISTER(vpg_ber_pattern, VID_MODE_VPG_MODE, dsi),
1099 	};
1100 	int i;
1101 
1102 	dsi->debugfs_vpg = kmemdup(debugfs, sizeof(debugfs), GFP_KERNEL);
1103 	if (!dsi->debugfs_vpg)
1104 		return;
1105 
1106 	for (i = 0; i < ARRAY_SIZE(debugfs); i++)
1107 		debugfs_create_file(dsi->debugfs_vpg[i].name, 0644,
1108 				    dsi->debugfs, &dsi->debugfs_vpg[i],
1109 				    &fops_x32);
1110 }
1111 
1112 static void dw_mipi_dsi_debugfs_init(struct dw_mipi_dsi *dsi)
1113 {
1114 	dsi->debugfs = debugfs_create_dir(dev_name(dsi->dev), NULL);
1115 	if (IS_ERR(dsi->debugfs)) {
1116 		dev_err(dsi->dev, "failed to create debugfs root\n");
1117 		return;
1118 	}
1119 
1120 	debugfs_create_files(dsi);
1121 }
1122 
1123 static void dw_mipi_dsi_debugfs_remove(struct dw_mipi_dsi *dsi)
1124 {
1125 	debugfs_remove_recursive(dsi->debugfs);
1126 	kfree(dsi->debugfs_vpg);
1127 }
1128 
1129 #else
1130 
1131 static void dw_mipi_dsi_debugfs_init(struct dw_mipi_dsi *dsi) { }
1132 static void dw_mipi_dsi_debugfs_remove(struct dw_mipi_dsi *dsi) { }
1133 
1134 #endif /* CONFIG_DEBUG_FS */
1135 
1136 static struct dw_mipi_dsi *
1137 __dw_mipi_dsi_probe(struct platform_device *pdev,
1138 		    const struct dw_mipi_dsi_plat_data *plat_data)
1139 {
1140 	struct device *dev = &pdev->dev;
1141 	struct reset_control *apb_rst;
1142 	struct dw_mipi_dsi *dsi;
1143 	int ret;
1144 
1145 	dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
1146 	if (!dsi)
1147 		return ERR_PTR(-ENOMEM);
1148 
1149 	dsi->dev = dev;
1150 	dsi->plat_data = plat_data;
1151 
1152 	if (!plat_data->phy_ops->init || !plat_data->phy_ops->get_lane_mbps ||
1153 	    !plat_data->phy_ops->get_timing) {
1154 		DRM_ERROR("Phy not properly configured\n");
1155 		return ERR_PTR(-ENODEV);
1156 	}
1157 
1158 	if (!plat_data->base) {
1159 		dsi->base = devm_platform_ioremap_resource(pdev, 0);
1160 		if (IS_ERR(dsi->base))
1161 			return ERR_PTR(-ENODEV);
1162 
1163 	} else {
1164 		dsi->base = plat_data->base;
1165 	}
1166 
1167 	dsi->pclk = devm_clk_get(dev, "pclk");
1168 	if (IS_ERR(dsi->pclk)) {
1169 		ret = PTR_ERR(dsi->pclk);
1170 		dev_err(dev, "Unable to get pclk: %d\n", ret);
1171 		return ERR_PTR(ret);
1172 	}
1173 
1174 	/*
1175 	 * Note that the reset was not defined in the initial device tree, so
1176 	 * we have to be prepared for it not being found.
1177 	 */
1178 	apb_rst = devm_reset_control_get_optional_exclusive(dev, "apb");
1179 	if (IS_ERR(apb_rst)) {
1180 		ret = PTR_ERR(apb_rst);
1181 
1182 		if (ret != -EPROBE_DEFER)
1183 			dev_err(dev, "Unable to get reset control: %d\n", ret);
1184 
1185 		return ERR_PTR(ret);
1186 	}
1187 
1188 	if (apb_rst) {
1189 		ret = clk_prepare_enable(dsi->pclk);
1190 		if (ret) {
1191 			dev_err(dev, "%s: Failed to enable pclk\n", __func__);
1192 			return ERR_PTR(ret);
1193 		}
1194 
1195 		reset_control_assert(apb_rst);
1196 		usleep_range(10, 20);
1197 		reset_control_deassert(apb_rst);
1198 
1199 		clk_disable_unprepare(dsi->pclk);
1200 	}
1201 
1202 	dw_mipi_dsi_debugfs_init(dsi);
1203 	pm_runtime_enable(dev);
1204 
1205 	dsi->dsi_host.ops = &dw_mipi_dsi_host_ops;
1206 	dsi->dsi_host.dev = dev;
1207 	ret = mipi_dsi_host_register(&dsi->dsi_host);
1208 	if (ret) {
1209 		dev_err(dev, "Failed to register MIPI host: %d\n", ret);
1210 		pm_runtime_disable(dev);
1211 		dw_mipi_dsi_debugfs_remove(dsi);
1212 		return ERR_PTR(ret);
1213 	}
1214 
1215 	dsi->bridge.driver_private = dsi;
1216 	dsi->bridge.funcs = &dw_mipi_dsi_bridge_funcs;
1217 #ifdef CONFIG_OF
1218 	dsi->bridge.of_node = pdev->dev.of_node;
1219 #endif
1220 	drm_bridge_add(&dsi->bridge);
1221 
1222 	return dsi;
1223 }
1224 
1225 static void __dw_mipi_dsi_remove(struct dw_mipi_dsi *dsi)
1226 {
1227 	mipi_dsi_host_unregister(&dsi->dsi_host);
1228 
1229 	pm_runtime_disable(dsi->dev);
1230 	dw_mipi_dsi_debugfs_remove(dsi);
1231 }
1232 
1233 void dw_mipi_dsi_set_slave(struct dw_mipi_dsi *dsi, struct dw_mipi_dsi *slave)
1234 {
1235 	/* introduce controllers to each other */
1236 	dsi->slave = slave;
1237 	dsi->slave->master = dsi;
1238 
1239 	/* migrate settings for already attached displays */
1240 	dsi->slave->lanes = dsi->lanes;
1241 	dsi->slave->channel = dsi->channel;
1242 	dsi->slave->format = dsi->format;
1243 	dsi->slave->mode_flags = dsi->mode_flags;
1244 }
1245 EXPORT_SYMBOL_GPL(dw_mipi_dsi_set_slave);
1246 
1247 /*
1248  * Probe/remove API, used from platforms based on the DRM bridge API.
1249  */
1250 struct dw_mipi_dsi *
1251 dw_mipi_dsi_probe(struct platform_device *pdev,
1252 		  const struct dw_mipi_dsi_plat_data *plat_data)
1253 {
1254 	return __dw_mipi_dsi_probe(pdev, plat_data);
1255 }
1256 EXPORT_SYMBOL_GPL(dw_mipi_dsi_probe);
1257 
1258 void dw_mipi_dsi_remove(struct dw_mipi_dsi *dsi)
1259 {
1260 	__dw_mipi_dsi_remove(dsi);
1261 }
1262 EXPORT_SYMBOL_GPL(dw_mipi_dsi_remove);
1263 
1264 /*
1265  * Bind/unbind API, used from platforms based on the component framework.
1266  */
1267 int dw_mipi_dsi_bind(struct dw_mipi_dsi *dsi, struct drm_encoder *encoder)
1268 {
1269 	return drm_bridge_attach(encoder, &dsi->bridge, NULL, 0);
1270 }
1271 EXPORT_SYMBOL_GPL(dw_mipi_dsi_bind);
1272 
1273 void dw_mipi_dsi_unbind(struct dw_mipi_dsi *dsi)
1274 {
1275 }
1276 EXPORT_SYMBOL_GPL(dw_mipi_dsi_unbind);
1277 
1278 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
1279 MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
1280 MODULE_DESCRIPTION("DW MIPI DSI host controller driver");
1281 MODULE_LICENSE("GPL");
1282 MODULE_ALIAS("platform:dw-mipi-dsi");
1283