xref: /linux/drivers/gpu/drm/mcde/mcde_dsi.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: GPL-2.0+
2 #include <linux/clk.h>
3 #include <linux/component.h>
4 #include <linux/delay.h>
5 #include <linux/io.h>
6 #include <linux/mfd/syscon.h>
7 #include <linux/module.h>
8 #include <linux/of.h>
9 #include <linux/platform_device.h>
10 #include <linux/regmap.h>
11 #include <linux/regulator/consumer.h>
12 #include <video/mipi_display.h>
13 
14 #include <drm/drm_atomic_state_helper.h>
15 #include <drm/drm_bridge.h>
16 #include <drm/drm_device.h>
17 #include <drm/drm_drv.h>
18 #include <drm/drm_encoder.h>
19 #include <drm/drm_mipi_dsi.h>
20 #include <drm/drm_of.h>
21 #include <drm/drm_panel.h>
22 #include <drm/drm_print.h>
23 
24 #include "mcde_drm.h"
25 #include "mcde_dsi_regs.h"
26 
27 #define DSI_DEFAULT_LP_FREQ_HZ	19200000
28 #define DSI_DEFAULT_HS_FREQ_HZ	420160000
29 
30 /* PRCMU DSI reset registers */
31 #define PRCM_DSI_SW_RESET 0x324
32 #define PRCM_DSI_SW_RESET_DSI0_SW_RESETN BIT(0)
33 #define PRCM_DSI_SW_RESET_DSI1_SW_RESETN BIT(1)
34 #define PRCM_DSI_SW_RESET_DSI2_SW_RESETN BIT(2)
35 
36 struct mcde_dsi {
37 	struct device *dev;
38 	struct mcde *mcde;
39 	struct drm_bridge bridge;
40 	struct mipi_dsi_host dsi_host;
41 	struct mipi_dsi_device *mdsi;
42 	const struct drm_display_mode *mode;
43 	struct clk *hs_clk;
44 	struct clk *lp_clk;
45 	unsigned long hs_freq;
46 	unsigned long lp_freq;
47 	bool unused;
48 
49 	void __iomem *regs;
50 	struct regmap *prcmu;
51 };
52 
53 static inline struct mcde_dsi *bridge_to_mcde_dsi(struct drm_bridge *bridge)
54 {
55 	return container_of(bridge, struct mcde_dsi, bridge);
56 }
57 
58 static inline struct mcde_dsi *host_to_mcde_dsi(struct mipi_dsi_host *h)
59 {
60 	return container_of(h, struct mcde_dsi, dsi_host);
61 }
62 
63 bool mcde_dsi_irq(struct mipi_dsi_device *mdsi)
64 {
65 	struct mcde_dsi *d;
66 	u32 val;
67 	bool te_received = false;
68 
69 	d = host_to_mcde_dsi(mdsi->host);
70 
71 	dev_dbg(d->dev, "%s called\n", __func__);
72 
73 	val = readl(d->regs + DSI_DIRECT_CMD_STS_FLAG);
74 	if (val)
75 		dev_dbg(d->dev, "DSI_DIRECT_CMD_STS_FLAG = %08x\n", val);
76 	if (val & DSI_DIRECT_CMD_STS_WRITE_COMPLETED)
77 		dev_dbg(d->dev, "direct command write completed\n");
78 	if (val & DSI_DIRECT_CMD_STS_TE_RECEIVED) {
79 		te_received = true;
80 		dev_dbg(d->dev, "direct command TE received\n");
81 	}
82 	if (val & DSI_DIRECT_CMD_STS_ACKNOWLEDGE_WITH_ERR_RECEIVED)
83 		dev_err(d->dev, "direct command ACK ERR received\n");
84 	if (val & DSI_DIRECT_CMD_STS_READ_COMPLETED_WITH_ERR)
85 		dev_err(d->dev, "direct command read ERR received\n");
86 	/* Mask off the ACK value and clear status */
87 	writel(val, d->regs + DSI_DIRECT_CMD_STS_CLR);
88 
89 	val = readl(d->regs + DSI_CMD_MODE_STS_FLAG);
90 	if (val)
91 		dev_dbg(d->dev, "DSI_CMD_MODE_STS_FLAG = %08x\n", val);
92 	if (val & DSI_CMD_MODE_STS_ERR_NO_TE)
93 		/* This happens all the time (safe to ignore) */
94 		dev_dbg(d->dev, "CMD mode no TE\n");
95 	if (val & DSI_CMD_MODE_STS_ERR_TE_MISS)
96 		/* This happens all the time (safe to ignore) */
97 		dev_dbg(d->dev, "CMD mode TE miss\n");
98 	if (val & DSI_CMD_MODE_STS_ERR_SDI1_UNDERRUN)
99 		dev_err(d->dev, "CMD mode SD1 underrun\n");
100 	if (val & DSI_CMD_MODE_STS_ERR_SDI2_UNDERRUN)
101 		dev_err(d->dev, "CMD mode SD2 underrun\n");
102 	if (val & DSI_CMD_MODE_STS_ERR_UNWANTED_RD)
103 		dev_err(d->dev, "CMD mode unwanted RD\n");
104 	writel(val, d->regs + DSI_CMD_MODE_STS_CLR);
105 
106 	val = readl(d->regs + DSI_DIRECT_CMD_RD_STS_FLAG);
107 	if (val)
108 		dev_dbg(d->dev, "DSI_DIRECT_CMD_RD_STS_FLAG = %08x\n", val);
109 	writel(val, d->regs + DSI_DIRECT_CMD_RD_STS_CLR);
110 
111 	val = readl(d->regs + DSI_TG_STS_FLAG);
112 	if (val)
113 		dev_dbg(d->dev, "DSI_TG_STS_FLAG = %08x\n", val);
114 	writel(val, d->regs + DSI_TG_STS_CLR);
115 
116 	val = readl(d->regs + DSI_VID_MODE_STS_FLAG);
117 	if (val)
118 		dev_dbg(d->dev, "DSI_VID_MODE_STS_FLAG = %08x\n", val);
119 	if (val & DSI_VID_MODE_STS_VSG_RUNNING)
120 		dev_dbg(d->dev, "VID mode VSG running\n");
121 	if (val & DSI_VID_MODE_STS_ERR_MISSING_DATA)
122 		dev_err(d->dev, "VID mode missing data\n");
123 	if (val & DSI_VID_MODE_STS_ERR_MISSING_HSYNC)
124 		dev_err(d->dev, "VID mode missing HSYNC\n");
125 	if (val & DSI_VID_MODE_STS_ERR_MISSING_VSYNC)
126 		dev_err(d->dev, "VID mode missing VSYNC\n");
127 	if (val & DSI_VID_MODE_STS_REG_ERR_SMALL_LENGTH)
128 		dev_err(d->dev, "VID mode less bytes than expected between two HSYNC\n");
129 	if (val & DSI_VID_MODE_STS_REG_ERR_SMALL_HEIGHT)
130 		dev_err(d->dev, "VID mode less lines than expected between two VSYNC\n");
131 	if (val & (DSI_VID_MODE_STS_ERR_BURSTWRITE |
132 		   DSI_VID_MODE_STS_ERR_LINEWRITE |
133 		   DSI_VID_MODE_STS_ERR_LONGREAD))
134 		dev_err(d->dev, "VID mode read/write error\n");
135 	if (val & DSI_VID_MODE_STS_ERR_VRS_WRONG_LENGTH)
136 		dev_err(d->dev, "VID mode received packets differ from expected size\n");
137 	if (val & DSI_VID_MODE_STS_VSG_RECOVERY)
138 		dev_err(d->dev, "VID mode VSG in recovery mode\n");
139 	writel(val, d->regs + DSI_VID_MODE_STS_CLR);
140 
141 	return te_received;
142 }
143 
144 static void mcde_dsi_attach_to_mcde(struct mcde_dsi *d)
145 {
146 	d->mcde->mdsi = d->mdsi;
147 
148 	/*
149 	 * Select the way the DSI data flow is pushing to the display:
150 	 * currently we just support video or command mode depending
151 	 * on the type of display. Video mode defaults to using the
152 	 * formatter itself for synchronization (stateless video panel).
153 	 *
154 	 * FIXME: add flags to struct mipi_dsi_device .flags to indicate
155 	 * displays that require BTA (bus turn around) so we can handle
156 	 * such displays as well. Figure out how to properly handle
157 	 * single frame on-demand updates with DRM for command mode
158 	 * displays (MCDE_COMMAND_ONESHOT_FLOW).
159 	 */
160 	if (d->mdsi->mode_flags & MIPI_DSI_MODE_VIDEO)
161 		d->mcde->flow_mode = MCDE_VIDEO_FORMATTER_FLOW;
162 	else
163 		d->mcde->flow_mode = MCDE_COMMAND_TE_FLOW;
164 }
165 
166 static int mcde_dsi_host_attach(struct mipi_dsi_host *host,
167 				struct mipi_dsi_device *mdsi)
168 {
169 	struct mcde_dsi *d = host_to_mcde_dsi(host);
170 
171 	if (mdsi->lanes < 1 || mdsi->lanes > 2) {
172 		DRM_ERROR("dsi device params invalid, 1 or 2 lanes supported\n");
173 		return -EINVAL;
174 	}
175 
176 	dev_info(d->dev, "attached DSI device with %d lanes\n", mdsi->lanes);
177 	/* MIPI_DSI_FMT_RGB88 etc */
178 	dev_info(d->dev, "format %08x, %dbpp\n", mdsi->format,
179 		 mipi_dsi_pixel_format_to_bpp(mdsi->format));
180 	dev_info(d->dev, "mode flags: %08lx\n", mdsi->mode_flags);
181 
182 	d->mdsi = mdsi;
183 	if (d->mcde)
184 		mcde_dsi_attach_to_mcde(d);
185 
186 	return 0;
187 }
188 
189 static int mcde_dsi_host_detach(struct mipi_dsi_host *host,
190 				struct mipi_dsi_device *mdsi)
191 {
192 	struct mcde_dsi *d = host_to_mcde_dsi(host);
193 
194 	d->mdsi = NULL;
195 	if (d->mcde)
196 		d->mcde->mdsi = NULL;
197 
198 	return 0;
199 }
200 
201 #define MCDE_DSI_HOST_IS_READ(type)			    \
202 	((type == MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM) || \
203 	 (type == MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM) || \
204 	 (type == MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM) || \
205 	 (type == MIPI_DSI_DCS_READ))
206 
207 static int mcde_dsi_execute_transfer(struct mcde_dsi *d,
208 				     const struct mipi_dsi_msg *msg)
209 {
210 	const u32 loop_delay_us = 10; /* us */
211 	u32 loop_counter;
212 	size_t txlen = msg->tx_len;
213 	size_t rxlen = msg->rx_len;
214 	int i;
215 	u32 val;
216 	int ret;
217 
218 	writel(~0, d->regs + DSI_DIRECT_CMD_STS_CLR);
219 	writel(~0, d->regs + DSI_CMD_MODE_STS_CLR);
220 	/* Send command */
221 	writel(1, d->regs + DSI_DIRECT_CMD_SEND);
222 
223 	loop_counter = 1000 * 1000 / loop_delay_us;
224 	if (MCDE_DSI_HOST_IS_READ(msg->type)) {
225 		/* Read command */
226 		while (!(readl(d->regs + DSI_DIRECT_CMD_STS) &
227 			 (DSI_DIRECT_CMD_STS_READ_COMPLETED |
228 			  DSI_DIRECT_CMD_STS_READ_COMPLETED_WITH_ERR))
229 		       && --loop_counter)
230 			usleep_range(loop_delay_us, (loop_delay_us * 3) / 2);
231 		if (!loop_counter) {
232 			dev_err(d->dev, "DSI read timeout!\n");
233 			/* Set exit code and retry */
234 			return -ETIME;
235 		}
236 	} else {
237 		/* Writing only */
238 		while (!(readl(d->regs + DSI_DIRECT_CMD_STS) &
239 			 DSI_DIRECT_CMD_STS_WRITE_COMPLETED)
240 		       && --loop_counter)
241 			usleep_range(loop_delay_us, (loop_delay_us * 3) / 2);
242 
243 		if (!loop_counter) {
244 			/* Set exit code and retry */
245 			dev_err(d->dev, "DSI write timeout!\n");
246 			return -ETIME;
247 		}
248 	}
249 
250 	val = readl(d->regs + DSI_DIRECT_CMD_STS);
251 	if (val & DSI_DIRECT_CMD_STS_READ_COMPLETED_WITH_ERR) {
252 		dev_err(d->dev, "read completed with error\n");
253 		writel(1, d->regs + DSI_DIRECT_CMD_RD_INIT);
254 		return -EIO;
255 	}
256 	if (val & DSI_DIRECT_CMD_STS_ACKNOWLEDGE_WITH_ERR_RECEIVED) {
257 		val >>= DSI_DIRECT_CMD_STS_ACK_VAL_SHIFT;
258 		dev_err(d->dev, "error during transmission: %04x\n",
259 			val);
260 		return -EIO;
261 	}
262 
263 	if (!MCDE_DSI_HOST_IS_READ(msg->type)) {
264 		/* Return number of bytes written */
265 		ret = txlen;
266 	} else {
267 		/* OK this is a read command, get the response */
268 		u32 rdsz;
269 		u32 rddat;
270 		u8 *rx = msg->rx_buf;
271 
272 		rdsz = readl(d->regs + DSI_DIRECT_CMD_RD_PROPERTY);
273 		rdsz &= DSI_DIRECT_CMD_RD_PROPERTY_RD_SIZE_MASK;
274 		rddat = readl(d->regs + DSI_DIRECT_CMD_RDDAT);
275 		if (rdsz < rxlen) {
276 			dev_err(d->dev, "read error, requested %zd got %d\n",
277 				rxlen, rdsz);
278 			return -EIO;
279 		}
280 		/* FIXME: read more than 4 bytes */
281 		for (i = 0; i < 4 && i < rxlen; i++)
282 			rx[i] = (rddat >> (i * 8)) & 0xff;
283 		ret = rdsz;
284 	}
285 
286 	/* Successful transmission */
287 	return ret;
288 }
289 
290 static ssize_t mcde_dsi_host_transfer(struct mipi_dsi_host *host,
291 				      const struct mipi_dsi_msg *msg)
292 {
293 	struct mcde_dsi *d = host_to_mcde_dsi(host);
294 	const u8 *tx = msg->tx_buf;
295 	size_t txlen = msg->tx_len;
296 	size_t rxlen = msg->rx_len;
297 	unsigned int retries = 0;
298 	u32 val;
299 	int ret;
300 	int i;
301 
302 	if (txlen > 16) {
303 		dev_err(d->dev,
304 			"dunno how to write more than 16 bytes yet\n");
305 		return -EIO;
306 	}
307 	if (rxlen > 4) {
308 		dev_err(d->dev,
309 			"dunno how to read more than 4 bytes yet\n");
310 		return -EIO;
311 	}
312 
313 	dev_dbg(d->dev,
314 		"message to channel %d, write %zd bytes read %zd bytes\n",
315 		msg->channel, txlen, rxlen);
316 
317 	/* Command "nature" */
318 	if (MCDE_DSI_HOST_IS_READ(msg->type))
319 		/* MCTL_MAIN_DATA_CTL already set up */
320 		val = DSI_DIRECT_CMD_MAIN_SETTINGS_CMD_NAT_READ;
321 	else
322 		val = DSI_DIRECT_CMD_MAIN_SETTINGS_CMD_NAT_WRITE;
323 	/*
324 	 * More than 2 bytes will not fit in a single packet, so it's
325 	 * time to set the "long not short" bit. One byte is used by
326 	 * the MIPI DCS command leaving just one byte for the payload
327 	 * in a short package.
328 	 */
329 	if (mipi_dsi_packet_format_is_long(msg->type))
330 		val |= DSI_DIRECT_CMD_MAIN_SETTINGS_CMD_LONGNOTSHORT;
331 	val |= 0 << DSI_DIRECT_CMD_MAIN_SETTINGS_CMD_ID_SHIFT;
332 	val |= txlen << DSI_DIRECT_CMD_MAIN_SETTINGS_CMD_SIZE_SHIFT;
333 	val |= DSI_DIRECT_CMD_MAIN_SETTINGS_CMD_LP_EN;
334 	val |= msg->type << DSI_DIRECT_CMD_MAIN_SETTINGS_CMD_HEAD_SHIFT;
335 	writel(val, d->regs + DSI_DIRECT_CMD_MAIN_SETTINGS);
336 
337 	/* MIPI DCS command is part of the data */
338 	if (txlen > 0) {
339 		val = 0;
340 		for (i = 0; i < 4 && i < txlen; i++)
341 			val |= tx[i] << (i * 8);
342 	}
343 	writel(val, d->regs + DSI_DIRECT_CMD_WRDAT0);
344 	if (txlen > 4) {
345 		val = 0;
346 		for (i = 0; i < 4 && (i + 4) < txlen; i++)
347 			val |= tx[i + 4] << (i * 8);
348 		writel(val, d->regs + DSI_DIRECT_CMD_WRDAT1);
349 	}
350 	if (txlen > 8) {
351 		val = 0;
352 		for (i = 0; i < 4 && (i + 8) < txlen; i++)
353 			val |= tx[i + 8] << (i * 8);
354 		writel(val, d->regs + DSI_DIRECT_CMD_WRDAT2);
355 	}
356 	if (txlen > 12) {
357 		val = 0;
358 		for (i = 0; i < 4 && (i + 12) < txlen; i++)
359 			val |= tx[i + 12] << (i * 8);
360 		writel(val, d->regs + DSI_DIRECT_CMD_WRDAT3);
361 	}
362 
363 	while (retries < 3) {
364 		ret = mcde_dsi_execute_transfer(d, msg);
365 		if (ret >= 0)
366 			break;
367 		retries++;
368 	}
369 	if (ret < 0 && retries)
370 		dev_err(d->dev, "gave up after %d retries\n", retries);
371 
372 	/* Clear any errors */
373 	writel(~0, d->regs + DSI_DIRECT_CMD_STS_CLR);
374 	writel(~0, d->regs + DSI_CMD_MODE_STS_CLR);
375 
376 	return ret;
377 }
378 
379 static const struct mipi_dsi_host_ops mcde_dsi_host_ops = {
380 	.attach = mcde_dsi_host_attach,
381 	.detach = mcde_dsi_host_detach,
382 	.transfer = mcde_dsi_host_transfer,
383 };
384 
385 /* This sends a direct (short) command to request TE */
386 void mcde_dsi_te_request(struct mipi_dsi_device *mdsi)
387 {
388 	struct mcde_dsi *d;
389 	u32 val;
390 
391 	d = host_to_mcde_dsi(mdsi->host);
392 
393 	/* Command "nature" TE request */
394 	val = DSI_DIRECT_CMD_MAIN_SETTINGS_CMD_NAT_TE_REQ;
395 	val |= 0 << DSI_DIRECT_CMD_MAIN_SETTINGS_CMD_ID_SHIFT;
396 	val |= 2 << DSI_DIRECT_CMD_MAIN_SETTINGS_CMD_SIZE_SHIFT;
397 	val |= DSI_DIRECT_CMD_MAIN_SETTINGS_CMD_LP_EN;
398 	val |= MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM <<
399 		DSI_DIRECT_CMD_MAIN_SETTINGS_CMD_HEAD_SHIFT;
400 	writel(val, d->regs + DSI_DIRECT_CMD_MAIN_SETTINGS);
401 
402 	/* Clear TE reveived and error status bits and enables them */
403 	writel(DSI_DIRECT_CMD_STS_CLR_TE_RECEIVED_CLR |
404 	       DSI_DIRECT_CMD_STS_CLR_ACKNOWLEDGE_WITH_ERR_RECEIVED_CLR,
405 	       d->regs + DSI_DIRECT_CMD_STS_CLR);
406 	val = readl(d->regs + DSI_DIRECT_CMD_STS_CTL);
407 	val |= DSI_DIRECT_CMD_STS_CTL_TE_RECEIVED_EN;
408 	val |= DSI_DIRECT_CMD_STS_CTL_ACKNOWLEDGE_WITH_ERR_EN;
409 	writel(val, d->regs + DSI_DIRECT_CMD_STS_CTL);
410 
411 	/* Clear and enable no TE or TE missing status */
412 	writel(DSI_CMD_MODE_STS_CLR_ERR_NO_TE_CLR |
413 	       DSI_CMD_MODE_STS_CLR_ERR_TE_MISS_CLR,
414 	       d->regs + DSI_CMD_MODE_STS_CLR);
415 	val = readl(d->regs + DSI_CMD_MODE_STS_CTL);
416 	val |= DSI_CMD_MODE_STS_CTL_ERR_NO_TE_EN;
417 	val |= DSI_CMD_MODE_STS_CTL_ERR_TE_MISS_EN;
418 	writel(val, d->regs + DSI_CMD_MODE_STS_CTL);
419 
420 	/* Send this TE request command */
421 	writel(1, d->regs + DSI_DIRECT_CMD_SEND);
422 }
423 
424 static void mcde_dsi_setup_video_mode(struct mcde_dsi *d,
425 				      const struct drm_display_mode *mode)
426 {
427 	/* cpp, characters per pixel, number of bytes per pixel */
428 	u8 cpp = mipi_dsi_pixel_format_to_bpp(d->mdsi->format) / 8;
429 	u64 pclk;
430 	u64 bpl;
431 	int hfp;
432 	int hbp;
433 	int hsa;
434 	u32 blkline_pck, line_duration;
435 	u32 val;
436 
437 	val = 0;
438 	if (d->mdsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
439 		val |= DSI_VID_MAIN_CTL_BURST_MODE;
440 	if (d->mdsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
441 		val |= DSI_VID_MAIN_CTL_SYNC_PULSE_ACTIVE;
442 		val |= DSI_VID_MAIN_CTL_SYNC_PULSE_HORIZONTAL;
443 	}
444 	/* RGB header and pixel mode */
445 	switch (d->mdsi->format) {
446 	case MIPI_DSI_FMT_RGB565:
447 		val |= MIPI_DSI_PACKED_PIXEL_STREAM_16 <<
448 			DSI_VID_MAIN_CTL_HEADER_SHIFT;
449 		val |= DSI_VID_MAIN_CTL_VID_PIXEL_MODE_16BITS;
450 		break;
451 	case MIPI_DSI_FMT_RGB666_PACKED:
452 		val |= MIPI_DSI_PACKED_PIXEL_STREAM_18 <<
453 			DSI_VID_MAIN_CTL_HEADER_SHIFT;
454 		val |= DSI_VID_MAIN_CTL_VID_PIXEL_MODE_18BITS;
455 		break;
456 	case MIPI_DSI_FMT_RGB666:
457 		val |= MIPI_DSI_PIXEL_STREAM_3BYTE_18
458 			<< DSI_VID_MAIN_CTL_HEADER_SHIFT;
459 		val |= DSI_VID_MAIN_CTL_VID_PIXEL_MODE_18BITS_LOOSE;
460 		break;
461 	case MIPI_DSI_FMT_RGB888:
462 		val |= MIPI_DSI_PACKED_PIXEL_STREAM_24 <<
463 			DSI_VID_MAIN_CTL_HEADER_SHIFT;
464 		val |= DSI_VID_MAIN_CTL_VID_PIXEL_MODE_24BITS;
465 		break;
466 	default:
467 		dev_err(d->dev, "unknown pixel mode\n");
468 		return;
469 	}
470 
471 	/* TODO: TVG (test video generator) could be enabled here */
472 
473 	/*
474 	 * During vertical blanking: go to LP mode
475 	 * Like with the EOL setting, if this is not set, the EOL area will be
476 	 * filled with NULL or blanking packets in the vblank area.
477 	 * FIXME: some Samsung phones and display panels such as s6e63m0 use
478 	 * DSI_VID_MAIN_CTL_REG_BLKLINE_MODE_BLANKING here instead,
479 	 * figure out how to properly configure that from the panel.
480 	 */
481 	val |= DSI_VID_MAIN_CTL_REG_BLKLINE_MODE_LP_0;
482 	/*
483 	 * During EOL: go to LP mode. If this is not set, the EOL area will be
484 	 * filled with NULL or blanking packets.
485 	 */
486 	val |= DSI_VID_MAIN_CTL_REG_BLKEOL_MODE_LP_0;
487 	/* Recovery mode 1 */
488 	val |= 1 << DSI_VID_MAIN_CTL_RECOVERY_MODE_SHIFT;
489 	/* All other fields zero */
490 	writel(val, d->regs + DSI_VID_MAIN_CTL);
491 
492 	/* Vertical frame parameters are pretty straight-forward */
493 	val = mode->vdisplay << DSI_VID_VSIZE_VACT_LENGTH_SHIFT;
494 	/* vertical front porch */
495 	val |= (mode->vsync_start - mode->vdisplay)
496 		<< DSI_VID_VSIZE_VFP_LENGTH_SHIFT;
497 	/* vertical sync active */
498 	val |= (mode->vsync_end - mode->vsync_start)
499 		<< DSI_VID_VSIZE_VSA_LENGTH_SHIFT;
500 	/* vertical back porch */
501 	val |= (mode->vtotal - mode->vsync_end)
502 		<< DSI_VID_VSIZE_VBP_LENGTH_SHIFT;
503 	writel(val, d->regs + DSI_VID_VSIZE);
504 
505 	/*
506 	 * Horizontal frame parameters:
507 	 * horizontal resolution is given in pixels but must be re-calculated
508 	 * into bytes since this is what the hardware expects, these registers
509 	 * define the payload size of the packet.
510 	 *
511 	 * hfp = horizontal front porch in bytes
512 	 * hbp = horizontal back porch in bytes
513 	 * hsa = horizontal sync active in bytes
514 	 *
515 	 * 6 + 2 is HFP header + checksum
516 	 */
517 	hfp = (mode->hsync_start - mode->hdisplay) * cpp - 6 - 2;
518 	if (d->mdsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
519 		/*
520 		 * Use sync pulse for sync: explicit HSA time
521 		 * 6 is HBP header + checksum
522 		 * 4 is RGB header + checksum
523 		 */
524 		hbp = (mode->htotal - mode->hsync_end) * cpp - 4 - 6;
525 		/*
526 		 * 6 is HBP header + checksum
527 		 * 4 is HSW packet bytes
528 		 * 4 is RGB header + checksum
529 		 */
530 		hsa = (mode->hsync_end - mode->hsync_start) * cpp - 4 - 4 - 6;
531 	} else {
532 		/*
533 		 * Use event for sync: HBP includes both back porch and sync
534 		 * 6 is HBP header + checksum
535 		 * 4 is HSW packet bytes
536 		 * 4 is RGB header + checksum
537 		 */
538 		hbp = (mode->htotal - mode->hsync_start) * cpp - 4 - 4 - 6;
539 		/* HSA is not present in this mode and set to 0 */
540 		hsa = 0;
541 	}
542 	if (hfp < 0) {
543 		dev_info(d->dev, "hfp negative, set to 0\n");
544 		hfp = 0;
545 	}
546 	if (hbp < 0) {
547 		dev_info(d->dev, "hbp negative, set to 0\n");
548 		hbp = 0;
549 	}
550 	if (hsa < 0) {
551 		dev_info(d->dev, "hsa negative, set to 0\n");
552 		hsa = 0;
553 	}
554 	dev_dbg(d->dev, "hfp: %u, hbp: %u, hsa: %u bytes\n",
555 		hfp, hbp, hsa);
556 
557 	/* Frame parameters: horizontal sync active */
558 	val = hsa << DSI_VID_HSIZE1_HSA_LENGTH_SHIFT;
559 	/* horizontal back porch */
560 	val |= hbp << DSI_VID_HSIZE1_HBP_LENGTH_SHIFT;
561 	/* horizontal front porch */
562 	val |= hfp << DSI_VID_HSIZE1_HFP_LENGTH_SHIFT;
563 	writel(val, d->regs + DSI_VID_HSIZE1);
564 
565 	/* RGB data length (visible bytes on one scanline) */
566 	val = mode->hdisplay * cpp;
567 	writel(val, d->regs + DSI_VID_HSIZE2);
568 	dev_dbg(d->dev, "RGB length, visible area on a line: %u bytes\n", val);
569 
570 	/*
571 	 * Calculate the time between two pixels in picoseconds using
572 	 * the supplied refresh rate and total resolution including
573 	 * porches and sync.
574 	 */
575 	/* (ps/s) / (pixels/s) = ps/pixels */
576 	pclk = DIV_ROUND_UP_ULL(1000000000000, (mode->clock * 1000));
577 	dev_dbg(d->dev, "picoseconds between two pixels: %llu\n",
578 		pclk);
579 
580 	/*
581 	 * How many bytes per line will this update frequency yield?
582 	 *
583 	 * Calculate the number of picoseconds for one scanline (1), then
584 	 * divide by 1000000000000 (2) to get in pixels per second we
585 	 * want to output.
586 	 *
587 	 * Multiply with number of bytes per second at this video display
588 	 * frequency (3) to get number of bytes transferred during this
589 	 * time. Notice that we use the frequency the display wants,
590 	 * not what we actually get from the DSI PLL, which is hs_freq.
591 	 *
592 	 * These arithmetics are done in a different order to avoid
593 	 * overflow.
594 	 */
595 	bpl = pclk * mode->htotal; /* (1) picoseconds per line */
596 	dev_dbg(d->dev, "picoseconds per line: %llu\n", bpl);
597 	/* Multiply with bytes per second (3) */
598 	bpl *= (d->mdsi->hs_rate / 8);
599 	/* Pixels per second (2) */
600 	bpl = DIV_ROUND_DOWN_ULL(bpl, 1000000); /* microseconds */
601 	bpl = DIV_ROUND_DOWN_ULL(bpl, 1000000); /* seconds */
602 	/* parallel transactions in all lanes */
603 	bpl *= d->mdsi->lanes;
604 	dev_dbg(d->dev,
605 		"calculated bytes per line: %llu @ %d Hz with HS %lu Hz\n",
606 		bpl, drm_mode_vrefresh(mode), d->mdsi->hs_rate);
607 
608 	/*
609 	 * 6 is header + checksum, header = 4 bytes, checksum = 2 bytes
610 	 * 4 is short packet for vsync/hsync
611 	 */
612 	if (d->mdsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
613 		/* Set the event packet size to 0 (not used) */
614 		writel(0, d->regs + DSI_VID_BLKSIZE1);
615 		/*
616 		 * FIXME: isn't the hsync width in pixels? The porch and
617 		 * sync area size is in pixels here, but this -6
618 		 * seems to be for bytes. It looks like this in the vendor
619 		 * code though. Is it completely untested?
620 		 */
621 		blkline_pck = bpl - (mode->hsync_end - mode->hsync_start) - 6;
622 		val = blkline_pck << DSI_VID_BLKSIZE2_BLKLINE_PULSE_PCK_SHIFT;
623 		writel(val, d->regs + DSI_VID_BLKSIZE2);
624 	} else {
625 		/* Set the sync pulse packet size to 0 (not used) */
626 		writel(0, d->regs + DSI_VID_BLKSIZE2);
627 		/* Specifying payload size in bytes (-4-6 from manual) */
628 		blkline_pck = bpl - 4 - 6;
629 		if (blkline_pck > 0x1FFF)
630 			dev_err(d->dev, "blkline_pck too big %d bytes\n",
631 				blkline_pck);
632 		val = blkline_pck << DSI_VID_BLKSIZE1_BLKLINE_EVENT_PCK_SHIFT;
633 		val &= DSI_VID_BLKSIZE1_BLKLINE_EVENT_PCK_MASK;
634 		writel(val, d->regs + DSI_VID_BLKSIZE1);
635 	}
636 
637 	/*
638 	 * The line duration is used to scale back the frequency from
639 	 * the max frequency supported by the HS clock to the desired
640 	 * update frequency in vrefresh.
641 	 */
642 	line_duration = blkline_pck + 6;
643 	/*
644 	 * The datasheet contains this complex condition to decreasing
645 	 * the line duration by 1 under very specific circumstances.
646 	 * Here we also imply that LP is used during burst EOL.
647 	 */
648 	if (d->mdsi->lanes == 2 && (hsa & 0x01) && (hfp & 0x01)
649 	    && (d->mdsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST))
650 		line_duration--;
651 	line_duration = DIV_ROUND_CLOSEST(line_duration, d->mdsi->lanes);
652 	dev_dbg(d->dev, "line duration %u bytes\n", line_duration);
653 	val = line_duration << DSI_VID_DPHY_TIME_REG_LINE_DURATION_SHIFT;
654 	/*
655 	 * This is the time to perform LP->HS on D-PHY
656 	 * FIXME: nowhere to get this from: DT property on the DSI?
657 	 * The manual says this is "system dependent".
658 	 * values like 48 and 72 seen in the vendor code.
659 	 */
660 	val |= 48 << DSI_VID_DPHY_TIME_REG_WAKEUP_TIME_SHIFT;
661 	writel(val, d->regs + DSI_VID_DPHY_TIME);
662 
663 	/*
664 	 * See the manual figure 657 page 2203 for understanding the impact
665 	 * of the different burst mode settings.
666 	 */
667 	if (d->mdsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST) {
668 		int blkeol_pck, blkeol_duration;
669 		/*
670 		 * Packet size at EOL for burst mode, this is only used
671 		 * if DSI_VID_MAIN_CTL_REG_BLKEOL_MODE_LP_0 is NOT set,
672 		 * but we instead send NULL or blanking packets at EOL.
673 		 * This is given in number of bytes.
674 		 *
675 		 * See the manual page 2198 for the 13 reg_blkeol_pck bits.
676 		 */
677 		blkeol_pck = bpl - (mode->htotal * cpp) - 6;
678 		if (blkeol_pck < 0) {
679 			dev_err(d->dev, "video block does not fit on line!\n");
680 			dev_err(d->dev,
681 				"calculated bytes per line: %llu @ %d Hz\n",
682 				bpl, drm_mode_vrefresh(mode));
683 			dev_err(d->dev,
684 				"bytes per line (blkline_pck) %u bytes\n",
685 				blkline_pck);
686 			dev_err(d->dev,
687 				"blkeol_pck becomes %d bytes\n", blkeol_pck);
688 			return;
689 		}
690 		dev_dbg(d->dev, "BLKEOL packet: %d bytes\n", blkeol_pck);
691 
692 		val = readl(d->regs + DSI_VID_BLKSIZE1);
693 		val &= ~DSI_VID_BLKSIZE1_BLKEOL_PCK_MASK;
694 		val |= blkeol_pck << DSI_VID_BLKSIZE1_BLKEOL_PCK_SHIFT;
695 		writel(val, d->regs + DSI_VID_BLKSIZE1);
696 		/* Use the same value for exact burst limit */
697 		val = blkeol_pck <<
698 			DSI_VID_VCA_SETTING2_EXACT_BURST_LIMIT_SHIFT;
699 		val &= DSI_VID_VCA_SETTING2_EXACT_BURST_LIMIT_MASK;
700 		writel(val, d->regs + DSI_VID_VCA_SETTING2);
701 		/*
702 		 * This BLKEOL duration is claimed to be the duration in clock
703 		 * cycles of the BLLP end-of-line (EOL) period for each line if
704 		 * DSI_VID_MAIN_CTL_REG_BLKEOL_MODE_LP_0 is set.
705 		 *
706 		 * It is hard to trust the manuals' claim that this is in clock
707 		 * cycles as we mimic the behaviour of the vendor code, which
708 		 * appears to write a number of bytes that would have been
709 		 * transferred on a single lane.
710 		 *
711 		 * See the manual figure 657 page 2203 and page 2198 for the 13
712 		 * reg_blkeol_duration bits.
713 		 *
714 		 * FIXME: should this also be set up also for non-burst mode
715 		 * according to figure 565 page 2202?
716 		 */
717 		blkeol_duration = DIV_ROUND_CLOSEST(blkeol_pck + 6,
718 						    d->mdsi->lanes);
719 		dev_dbg(d->dev, "BLKEOL duration: %d clock cycles\n",
720 			blkeol_duration);
721 
722 		val = readl(d->regs + DSI_VID_PCK_TIME);
723 		val &= ~DSI_VID_PCK_TIME_BLKEOL_DURATION_MASK;
724 		val |= blkeol_duration <<
725 			DSI_VID_PCK_TIME_BLKEOL_DURATION_SHIFT;
726 		writel(val, d->regs + DSI_VID_PCK_TIME);
727 
728 		/* Max burst limit, this is given in bytes */
729 		val = readl(d->regs + DSI_VID_VCA_SETTING1);
730 		val &= ~DSI_VID_VCA_SETTING1_MAX_BURST_LIMIT_MASK;
731 		val |= (blkeol_pck - 6) <<
732 			DSI_VID_VCA_SETTING1_MAX_BURST_LIMIT_SHIFT;
733 		writel(val, d->regs + DSI_VID_VCA_SETTING1);
734 	}
735 
736 	/* Maximum line limit */
737 	val = readl(d->regs + DSI_VID_VCA_SETTING2);
738 	val &= ~DSI_VID_VCA_SETTING2_MAX_LINE_LIMIT_MASK;
739 	val |= (blkline_pck - 6) <<
740 		DSI_VID_VCA_SETTING2_MAX_LINE_LIMIT_SHIFT;
741 	writel(val, d->regs + DSI_VID_VCA_SETTING2);
742 	dev_dbg(d->dev, "blkline pck: %d bytes\n", blkline_pck - 6);
743 }
744 
745 static void mcde_dsi_start(struct mcde_dsi *d)
746 {
747 	unsigned long hs_freq;
748 	u32 val;
749 	int i;
750 
751 	/* No integration mode */
752 	writel(0, d->regs + DSI_MCTL_INTEGRATION_MODE);
753 
754 	/* Enable the DSI port, from drivers/video/mcde/dsilink_v2.c */
755 	val = DSI_MCTL_MAIN_DATA_CTL_LINK_EN |
756 		DSI_MCTL_MAIN_DATA_CTL_BTA_EN |
757 		DSI_MCTL_MAIN_DATA_CTL_READ_EN |
758 		DSI_MCTL_MAIN_DATA_CTL_REG_TE_EN;
759 	if (!(d->mdsi->mode_flags & MIPI_DSI_MODE_NO_EOT_PACKET))
760 		val |= DSI_MCTL_MAIN_DATA_CTL_HOST_EOT_GEN;
761 	writel(val, d->regs + DSI_MCTL_MAIN_DATA_CTL);
762 
763 	/* Set a high command timeout, clear other fields */
764 	val = 0x3ff << DSI_CMD_MODE_CTL_TE_TIMEOUT_SHIFT;
765 	writel(val, d->regs + DSI_CMD_MODE_CTL);
766 
767 	/*
768 	 * UI_X4 is described as "unit interval times four"
769 	 * I guess since DSI packets are 4 bytes wide, one unit
770 	 * is one byte.
771 	 */
772 	hs_freq = clk_get_rate(d->hs_clk);
773 	hs_freq /= 1000000; /* MHz */
774 	val = 4000 / hs_freq;
775 	dev_dbg(d->dev, "UI value: %d\n", val);
776 	val <<= DSI_MCTL_DPHY_STATIC_UI_X4_SHIFT;
777 	val &= DSI_MCTL_DPHY_STATIC_UI_X4_MASK;
778 	writel(val, d->regs + DSI_MCTL_DPHY_STATIC);
779 
780 	/*
781 	 * Enable clocking: 0x0f (something?) between each burst,
782 	 * enable the second lane if needed, enable continuous clock if
783 	 * needed, enable switch into ULPM (ultra-low power mode) on
784 	 * all the lines.
785 	 */
786 	val = 0x0f << DSI_MCTL_MAIN_PHY_CTL_WAIT_BURST_TIME_SHIFT;
787 	if (d->mdsi->lanes == 2)
788 		val |= DSI_MCTL_MAIN_PHY_CTL_LANE2_EN;
789 	if (!(d->mdsi->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS))
790 		val |= DSI_MCTL_MAIN_PHY_CTL_CLK_CONTINUOUS;
791 	val |= DSI_MCTL_MAIN_PHY_CTL_CLK_ULPM_EN |
792 		DSI_MCTL_MAIN_PHY_CTL_DAT1_ULPM_EN |
793 		DSI_MCTL_MAIN_PHY_CTL_DAT2_ULPM_EN;
794 	writel(val, d->regs + DSI_MCTL_MAIN_PHY_CTL);
795 
796 	val = (1 << DSI_MCTL_ULPOUT_TIME_CKLANE_ULPOUT_TIME_SHIFT) |
797 		(1 << DSI_MCTL_ULPOUT_TIME_DATA_ULPOUT_TIME_SHIFT);
798 	writel(val, d->regs + DSI_MCTL_ULPOUT_TIME);
799 
800 	writel(DSI_DPHY_LANES_TRIM_DPHY_SPECS_90_81B_0_90,
801 	       d->regs + DSI_DPHY_LANES_TRIM);
802 
803 	/* High PHY timeout */
804 	val = (0x0f << DSI_MCTL_DPHY_TIMEOUT_CLK_DIV_SHIFT) |
805 		(0x3fff << DSI_MCTL_DPHY_TIMEOUT_HSTX_TO_VAL_SHIFT) |
806 		(0x3fff << DSI_MCTL_DPHY_TIMEOUT_LPRX_TO_VAL_SHIFT);
807 	writel(val, d->regs + DSI_MCTL_DPHY_TIMEOUT);
808 
809 	val = DSI_MCTL_MAIN_EN_PLL_START |
810 		DSI_MCTL_MAIN_EN_CKLANE_EN |
811 		DSI_MCTL_MAIN_EN_DAT1_EN |
812 		DSI_MCTL_MAIN_EN_IF1_EN;
813 	if (d->mdsi->lanes == 2)
814 		val |= DSI_MCTL_MAIN_EN_DAT2_EN;
815 	writel(val, d->regs + DSI_MCTL_MAIN_EN);
816 
817 	/* Wait for the PLL to lock and the clock and data lines to come up */
818 	i = 0;
819 	val = DSI_MCTL_MAIN_STS_PLL_LOCK |
820 		DSI_MCTL_MAIN_STS_CLKLANE_READY |
821 		DSI_MCTL_MAIN_STS_DAT1_READY;
822 	if (d->mdsi->lanes == 2)
823 		val |= DSI_MCTL_MAIN_STS_DAT2_READY;
824 	while ((readl(d->regs + DSI_MCTL_MAIN_STS) & val) != val) {
825 		/* Sleep for a millisecond */
826 		usleep_range(1000, 1500);
827 		if (i++ == 100) {
828 			dev_warn(d->dev, "DSI lanes did not start up\n");
829 			return;
830 		}
831 	}
832 
833 	/* TODO needed? */
834 
835 	/* Command mode, clear IF1 ID */
836 	val = readl(d->regs + DSI_CMD_MODE_CTL);
837 	/*
838 	 * If we enable low-power mode here,
839 	 * then display updates become really slow.
840 	 */
841 	if (d->mdsi->mode_flags & MIPI_DSI_MODE_LPM)
842 		val |= DSI_CMD_MODE_CTL_IF1_LP_EN;
843 	val &= ~DSI_CMD_MODE_CTL_IF1_ID_MASK;
844 	writel(val, d->regs + DSI_CMD_MODE_CTL);
845 
846 	/* Wait for DSI PHY to initialize */
847 	usleep_range(100, 200);
848 	dev_info(d->dev, "DSI link enabled\n");
849 }
850 
851 /*
852  * Notice that this is called from inside the display controller
853  * and not from the bridge callbacks.
854  */
855 void mcde_dsi_enable(struct drm_bridge *bridge)
856 {
857 	struct mcde_dsi *d = bridge_to_mcde_dsi(bridge);
858 	unsigned long hs_freq, lp_freq;
859 	u32 val;
860 	int ret;
861 
862 	/* Copy maximum clock frequencies */
863 	if (d->mdsi->lp_rate)
864 		lp_freq = d->mdsi->lp_rate;
865 	else
866 		lp_freq = DSI_DEFAULT_LP_FREQ_HZ;
867 	if (d->mdsi->hs_rate)
868 		hs_freq = d->mdsi->hs_rate;
869 	else
870 		hs_freq = DSI_DEFAULT_HS_FREQ_HZ;
871 
872 	/* Enable LP (Low Power, Energy Save, ES) and HS (High Speed) clocks */
873 	d->lp_freq = clk_round_rate(d->lp_clk, lp_freq);
874 	ret = clk_set_rate(d->lp_clk, d->lp_freq);
875 	if (ret)
876 		dev_err(d->dev, "failed to set LP clock rate %lu Hz\n",
877 			d->lp_freq);
878 
879 	d->hs_freq = clk_round_rate(d->hs_clk, hs_freq);
880 	ret = clk_set_rate(d->hs_clk, d->hs_freq);
881 	if (ret)
882 		dev_err(d->dev, "failed to set HS clock rate %lu Hz\n",
883 			d->hs_freq);
884 
885 	/* Start clocks */
886 	ret = clk_prepare_enable(d->lp_clk);
887 	if (ret)
888 		dev_err(d->dev, "failed to enable LP clock\n");
889 	else
890 		dev_info(d->dev, "DSI LP clock rate %lu Hz\n",
891 			 d->lp_freq);
892 	ret = clk_prepare_enable(d->hs_clk);
893 	if (ret)
894 		dev_err(d->dev, "failed to enable HS clock\n");
895 	else
896 		dev_info(d->dev, "DSI HS clock rate %lu Hz\n",
897 			 d->hs_freq);
898 
899 	/* Assert RESET through the PRCMU, active low */
900 	/* FIXME: which DSI block? */
901 	regmap_update_bits(d->prcmu, PRCM_DSI_SW_RESET,
902 			   PRCM_DSI_SW_RESET_DSI0_SW_RESETN, 0);
903 
904 	usleep_range(100, 200);
905 
906 	/* De-assert RESET again */
907 	regmap_update_bits(d->prcmu, PRCM_DSI_SW_RESET,
908 			   PRCM_DSI_SW_RESET_DSI0_SW_RESETN,
909 			   PRCM_DSI_SW_RESET_DSI0_SW_RESETN);
910 
911 	/* Start up the hardware */
912 	mcde_dsi_start(d);
913 
914 	if (d->mdsi->mode_flags & MIPI_DSI_MODE_VIDEO) {
915 		/* Set up the video mode from the DRM mode */
916 		mcde_dsi_setup_video_mode(d, d->mode);
917 
918 		/* Put IF1 into video mode */
919 		val = readl(d->regs + DSI_MCTL_MAIN_DATA_CTL);
920 		val |= DSI_MCTL_MAIN_DATA_CTL_IF1_MODE;
921 		writel(val, d->regs + DSI_MCTL_MAIN_DATA_CTL);
922 
923 		/* Disable command mode on IF1 */
924 		val = readl(d->regs + DSI_CMD_MODE_CTL);
925 		val &= ~DSI_CMD_MODE_CTL_IF1_LP_EN;
926 		writel(val, d->regs + DSI_CMD_MODE_CTL);
927 
928 		/* Enable some error interrupts */
929 		val = readl(d->regs + DSI_VID_MODE_STS_CTL);
930 		val |= DSI_VID_MODE_STS_CTL_ERR_MISSING_VSYNC;
931 		val |= DSI_VID_MODE_STS_CTL_ERR_MISSING_DATA;
932 		writel(val, d->regs + DSI_VID_MODE_STS_CTL);
933 
934 		/* Enable video mode */
935 		val = readl(d->regs + DSI_MCTL_MAIN_DATA_CTL);
936 		val |= DSI_MCTL_MAIN_DATA_CTL_VID_EN;
937 		writel(val, d->regs + DSI_MCTL_MAIN_DATA_CTL);
938 	} else {
939 		/* Command mode, clear IF1 ID */
940 		val = readl(d->regs + DSI_CMD_MODE_CTL);
941 		/*
942 		 * If we enable low-power mode here
943 		 * the display updates become really slow.
944 		 */
945 		if (d->mdsi->mode_flags & MIPI_DSI_MODE_LPM)
946 			val |= DSI_CMD_MODE_CTL_IF1_LP_EN;
947 		val &= ~DSI_CMD_MODE_CTL_IF1_ID_MASK;
948 		writel(val, d->regs + DSI_CMD_MODE_CTL);
949 	}
950 
951 	dev_info(d->dev, "enabled MCDE DSI master\n");
952 }
953 
954 static void mcde_dsi_bridge_mode_set(struct drm_bridge *bridge,
955 				     const struct drm_display_mode *mode,
956 				     const struct drm_display_mode *adj)
957 {
958 	struct mcde_dsi *d = bridge_to_mcde_dsi(bridge);
959 
960 	if (!d->mdsi) {
961 		dev_err(d->dev, "no DSI device attached to encoder!\n");
962 		return;
963 	}
964 
965 	d->mode = mode;
966 
967 	dev_info(d->dev, "set DSI master to %dx%d %u Hz %s mode\n",
968 		 mode->hdisplay, mode->vdisplay, mode->clock * 1000,
969 		 (d->mdsi->mode_flags & MIPI_DSI_MODE_VIDEO) ? "VIDEO" : "CMD"
970 		);
971 }
972 
973 static void mcde_dsi_wait_for_command_mode_stop(struct mcde_dsi *d)
974 {
975 	u32 val;
976 	int i;
977 
978 	/*
979 	 * Wait until we get out of command mode
980 	 * CSM = Command State Machine
981 	 */
982 	i = 0;
983 	val = DSI_CMD_MODE_STS_CSM_RUNNING;
984 	while ((readl(d->regs + DSI_CMD_MODE_STS) & val) == val) {
985 		/* Sleep for a millisecond */
986 		usleep_range(1000, 2000);
987 		if (i++ == 100) {
988 			dev_warn(d->dev,
989 				 "could not get out of command mode\n");
990 			return;
991 		}
992 	}
993 }
994 
995 static void mcde_dsi_wait_for_video_mode_stop(struct mcde_dsi *d)
996 {
997 	u32 val;
998 	int i;
999 
1000 	/* Wait until we get out og video mode */
1001 	i = 0;
1002 	val = DSI_VID_MODE_STS_VSG_RUNNING;
1003 	while ((readl(d->regs + DSI_VID_MODE_STS) & val) == val) {
1004 		/* Sleep for a millisecond */
1005 		usleep_range(1000, 2000);
1006 		if (i++ == 100) {
1007 			dev_warn(d->dev,
1008 				 "could not get out of video mode\n");
1009 			return;
1010 		}
1011 	}
1012 }
1013 
1014 /*
1015  * Notice that this is called from inside the display controller
1016  * and not from the bridge callbacks.
1017  */
1018 void mcde_dsi_disable(struct drm_bridge *bridge)
1019 {
1020 	struct mcde_dsi *d = bridge_to_mcde_dsi(bridge);
1021 	u32 val;
1022 
1023 	if (d->mdsi->mode_flags & MIPI_DSI_MODE_VIDEO) {
1024 		/* Stop video mode */
1025 		val = readl(d->regs + DSI_MCTL_MAIN_DATA_CTL);
1026 		val &= ~DSI_MCTL_MAIN_DATA_CTL_VID_EN;
1027 		writel(val, d->regs + DSI_MCTL_MAIN_DATA_CTL);
1028 		mcde_dsi_wait_for_video_mode_stop(d);
1029 	} else {
1030 		/* Stop command mode */
1031 		mcde_dsi_wait_for_command_mode_stop(d);
1032 	}
1033 
1034 	/*
1035 	 * Stop clocks and terminate any DSI traffic here so the panel can
1036 	 * send commands to shut down the display using DSI direct write until
1037 	 * this point.
1038 	 */
1039 
1040 	/* Disable all error interrupts */
1041 	writel(0, d->regs + DSI_VID_MODE_STS_CTL);
1042 	clk_disable_unprepare(d->hs_clk);
1043 	clk_disable_unprepare(d->lp_clk);
1044 }
1045 
1046 static int mcde_dsi_bridge_attach(struct drm_bridge *bridge,
1047 				  struct drm_encoder *encoder,
1048 				  enum drm_bridge_attach_flags flags)
1049 {
1050 	struct mcde_dsi *d = bridge_to_mcde_dsi(bridge);
1051 	struct drm_device *drm = bridge->dev;
1052 
1053 	if (!drm_core_check_feature(drm, DRIVER_ATOMIC)) {
1054 		dev_err(d->dev, "we need atomic updates\n");
1055 		return -ENOTSUPP;
1056 	}
1057 
1058 	/* Attach the DSI bridge to the output (panel etc) bridge */
1059 	return drm_bridge_attach(encoder, d->bridge.next_bridge, bridge, flags);
1060 }
1061 
1062 static const struct drm_bridge_funcs mcde_dsi_bridge_funcs = {
1063 	.atomic_create_state = drm_atomic_helper_bridge_create_state,
1064 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
1065 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
1066 	.attach = mcde_dsi_bridge_attach,
1067 	.mode_set = mcde_dsi_bridge_mode_set,
1068 };
1069 
1070 static int mcde_dsi_bind(struct device *dev, struct device *master,
1071 			 void *data)
1072 {
1073 	struct drm_device *drm = data;
1074 	struct mcde *mcde = to_mcde(drm);
1075 	struct mcde_dsi *d = dev_get_drvdata(dev);
1076 	struct drm_bridge *bridge __free(drm_bridge_put) = NULL;
1077 
1078 	if (!of_get_available_child_count(dev->of_node)) {
1079 		dev_info(dev, "unused DSI interface\n");
1080 		d->unused = true;
1081 		return 0;
1082 	}
1083 	d->mcde = mcde;
1084 	/* If the display attached before binding, set this up */
1085 	if (d->mdsi)
1086 		mcde_dsi_attach_to_mcde(d);
1087 
1088 	/* Obtain the clocks */
1089 	d->hs_clk = devm_clk_get(dev, "hs");
1090 	if (IS_ERR(d->hs_clk)) {
1091 		dev_err(dev, "unable to get HS clock\n");
1092 		return PTR_ERR(d->hs_clk);
1093 	}
1094 
1095 	d->lp_clk = devm_clk_get(dev, "lp");
1096 	if (IS_ERR(d->lp_clk)) {
1097 		dev_err(dev, "unable to get LP clock\n");
1098 		return PTR_ERR(d->lp_clk);
1099 	}
1100 
1101 	/* Look for a panel as a child to this node */
1102 	for_each_available_child_of_node_scoped(dev->of_node, child) {
1103 		bridge = of_drm_find_and_get_bridge(child);
1104 		if (!bridge) {
1105 			dev_err(dev, "failed to find bridge\n");
1106 			return -EINVAL;
1107 		}
1108 
1109 		break;
1110 	}
1111 	if (!bridge) {
1112 		dev_err(dev, "no bridge\n");
1113 		return -ENODEV;
1114 	} else if (!drm_bridge_is_panel(bridge)) {
1115 		/* TODO: AV8100 HDMI encoder goes here for example */
1116 		dev_info(dev, "connected to non-panel bridge (unsupported)\n");
1117 		return -ENODEV;
1118 	}
1119 
1120 	d->bridge.next_bridge = drm_bridge_get(bridge);
1121 
1122 	/* Create a bridge for this DSI channel */
1123 	d->bridge.of_node = dev->of_node;
1124 	drm_bridge_add(&d->bridge);
1125 
1126 	/* TODO: first come first serve, use a list */
1127 	mcde->bridge = &d->bridge;
1128 
1129 	dev_info(dev, "initialized MCDE DSI bridge\n");
1130 
1131 	return 0;
1132 }
1133 
1134 static void mcde_dsi_unbind(struct device *dev, struct device *master,
1135 			    void *data)
1136 {
1137 	struct mcde_dsi *d = dev_get_drvdata(dev);
1138 
1139 	regmap_update_bits(d->prcmu, PRCM_DSI_SW_RESET,
1140 			   PRCM_DSI_SW_RESET_DSI0_SW_RESETN, 0);
1141 }
1142 
1143 static const struct component_ops mcde_dsi_component_ops = {
1144 	.bind   = mcde_dsi_bind,
1145 	.unbind = mcde_dsi_unbind,
1146 };
1147 
1148 static int mcde_dsi_probe(struct platform_device *pdev)
1149 {
1150 	struct device *dev = &pdev->dev;
1151 	struct mcde_dsi *d;
1152 	struct mipi_dsi_host *host;
1153 	u32 dsi_id;
1154 	int ret;
1155 
1156 	d = devm_drm_bridge_alloc(dev, struct mcde_dsi, bridge, &mcde_dsi_bridge_funcs);
1157 	if (IS_ERR(d))
1158 		return PTR_ERR(d);
1159 	d->dev = dev;
1160 	platform_set_drvdata(pdev, d);
1161 
1162 	/* Get a handle on the PRCMU so we can do reset */
1163 	d->prcmu =
1164 		syscon_regmap_lookup_by_compatible("stericsson,db8500-prcmu");
1165 	if (IS_ERR(d->prcmu)) {
1166 		dev_err(dev, "no PRCMU regmap\n");
1167 		return PTR_ERR(d->prcmu);
1168 	}
1169 
1170 	d->regs = devm_platform_ioremap_resource(pdev, 0);
1171 	if (IS_ERR(d->regs))
1172 		return PTR_ERR(d->regs);
1173 
1174 	dsi_id = readl(d->regs + DSI_ID_REG);
1175 	dev_info(dev, "HW revision 0x%08x\n", dsi_id);
1176 
1177 	host = &d->dsi_host;
1178 	host->dev = dev;
1179 	host->ops = &mcde_dsi_host_ops;
1180 	ret = mipi_dsi_host_register(host);
1181 	if (ret < 0) {
1182 		dev_err(dev, "failed to register DSI host: %d\n", ret);
1183 		return ret;
1184 	}
1185 	dev_info(dev, "registered DSI host\n");
1186 
1187 	platform_set_drvdata(pdev, d);
1188 	return component_add(dev, &mcde_dsi_component_ops);
1189 }
1190 
1191 static void mcde_dsi_remove(struct platform_device *pdev)
1192 {
1193 	struct mcde_dsi *d = platform_get_drvdata(pdev);
1194 
1195 	component_del(&pdev->dev, &mcde_dsi_component_ops);
1196 	mipi_dsi_host_unregister(&d->dsi_host);
1197 }
1198 
1199 static const struct of_device_id mcde_dsi_of_match[] = {
1200 	{
1201 		.compatible = "ste,mcde-dsi",
1202 	},
1203 	{},
1204 };
1205 
1206 struct platform_driver mcde_dsi_driver = {
1207 	.driver = {
1208 		.name           = "mcde-dsi",
1209 		.of_match_table = mcde_dsi_of_match,
1210 	},
1211 	.probe = mcde_dsi_probe,
1212 	.remove = mcde_dsi_remove,
1213 };
1214