xref: /linux/drivers/gpu/drm/bridge/nwl-dsi.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * i.MX8 NWL MIPI DSI host driver
4  *
5  * Copyright (C) 2017 NXP
6  * Copyright (C) 2020 Purism SPC
7  */
8 
9 #include <linux/bitfield.h>
10 #include <linux/bits.h>
11 #include <linux/clk.h>
12 #include <linux/irq.h>
13 #include <linux/math64.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/media-bus-format.h>
16 #include <linux/module.h>
17 #include <linux/mux/consumer.h>
18 #include <linux/of.h>
19 #include <linux/phy/phy.h>
20 #include <linux/platform_device.h>
21 #include <linux/regmap.h>
22 #include <linux/reset.h>
23 #include <linux/sys_soc.h>
24 #include <linux/time64.h>
25 
26 #include <drm/drm_atomic_state_helper.h>
27 #include <drm/drm_bridge.h>
28 #include <drm/drm_mipi_dsi.h>
29 #include <drm/drm_of.h>
30 #include <drm/drm_print.h>
31 
32 #include <video/mipi_display.h>
33 
34 #include "nwl-dsi.h"
35 
36 #define DRV_NAME "nwl-dsi"
37 
38 /* i.MX8 NWL quirks */
39 /* i.MX8MQ errata E11418 */
40 #define E11418_HS_MODE_QUIRK	BIT(0)
41 
42 #define NWL_DSI_MIPI_FIFO_TIMEOUT msecs_to_jiffies(500)
43 
44 enum transfer_direction {
45 	DSI_PACKET_SEND,
46 	DSI_PACKET_RECEIVE,
47 };
48 
49 #define NWL_DSI_ENDPOINT_LCDIF 0
50 #define NWL_DSI_ENDPOINT_DCSS 1
51 
52 struct nwl_dsi_transfer {
53 	const struct mipi_dsi_msg *msg;
54 	struct mipi_dsi_packet packet;
55 	struct completion completed;
56 
57 	int status; /* status of transmission */
58 	enum transfer_direction direction;
59 	bool need_bta;
60 	u8 cmd;
61 	u16 rx_word_count;
62 	size_t tx_len; /* in bytes */
63 	size_t rx_len; /* in bytes */
64 };
65 
66 struct nwl_dsi {
67 	struct drm_bridge bridge;
68 	struct mipi_dsi_host dsi_host;
69 	struct device *dev;
70 	struct phy *phy;
71 	union phy_configure_opts phy_cfg;
72 	unsigned int quirks;
73 
74 	struct regmap *regmap;
75 	int irq;
76 	/*
77 	 * The DSI host controller needs this reset sequence according to NWL:
78 	 * 1. Deassert pclk reset to get access to DSI regs
79 	 * 2. Configure DSI Host and DPHY and enable DPHY
80 	 * 3. Deassert ESC and BYTE resets to allow host TX operations)
81 	 * 4. Send DSI cmds to configure peripheral (handled by panel drv)
82 	 * 5. Deassert DPI reset so DPI receives pixels and starts sending
83 	 *    DSI data
84 	 *
85 	 * TODO: Since panel_bridges do their DSI setup in enable we
86 	 * currently have 4. and 5. swapped.
87 	 */
88 	struct reset_control *rst_byte;
89 	struct reset_control *rst_esc;
90 	struct reset_control *rst_dpi;
91 	struct reset_control *rst_pclk;
92 	struct mux_control *mux;
93 
94 	/* DSI clocks */
95 	struct clk *phy_ref_clk;
96 	struct clk *rx_esc_clk;
97 	struct clk *tx_esc_clk;
98 	struct clk *core_clk;
99 	/*
100 	 * hardware bug: the i.MX8MQ needs this clock on during reset
101 	 * even when not using LCDIF.
102 	 */
103 	struct clk *lcdif_clk;
104 
105 	/* dsi lanes */
106 	u32 lanes;
107 	enum mipi_dsi_pixel_format format;
108 	struct drm_display_mode mode;
109 	unsigned long dsi_mode_flags;
110 	int error;
111 
112 	struct nwl_dsi_transfer *xfer;
113 
114 	unsigned int endpoint;
115 };
116 
117 static const struct regmap_config nwl_dsi_regmap_config = {
118 	.reg_bits = 16,
119 	.val_bits = 32,
120 	.reg_stride = 4,
121 	.max_register = NWL_DSI_IRQ_MASK2,
122 	.name = DRV_NAME,
123 };
124 
125 static inline struct nwl_dsi *bridge_to_dsi(struct drm_bridge *bridge)
126 {
127 	return container_of(bridge, struct nwl_dsi, bridge);
128 }
129 
130 static int nwl_dsi_clear_error(struct nwl_dsi *dsi)
131 {
132 	int ret = dsi->error;
133 
134 	dsi->error = 0;
135 	return ret;
136 }
137 
138 static void nwl_dsi_write(struct nwl_dsi *dsi, unsigned int reg, u32 val)
139 {
140 	int ret;
141 
142 	if (dsi->error)
143 		return;
144 
145 	ret = regmap_write(dsi->regmap, reg, val);
146 	if (ret < 0) {
147 		DRM_DEV_ERROR(dsi->dev,
148 			      "Failed to write NWL DSI reg 0x%x: %d\n", reg,
149 			      ret);
150 		dsi->error = ret;
151 	}
152 }
153 
154 static u32 nwl_dsi_read(struct nwl_dsi *dsi, u32 reg)
155 {
156 	unsigned int val;
157 	int ret;
158 
159 	if (dsi->error)
160 		return 0;
161 
162 	ret = regmap_read(dsi->regmap, reg, &val);
163 	if (ret < 0) {
164 		DRM_DEV_ERROR(dsi->dev, "Failed to read NWL DSI reg 0x%x: %d\n",
165 			      reg, ret);
166 		dsi->error = ret;
167 	}
168 	return val;
169 }
170 
171 static int nwl_dsi_get_dpi_pixel_format(enum mipi_dsi_pixel_format format)
172 {
173 	switch (format) {
174 	case MIPI_DSI_FMT_RGB565:
175 		return NWL_DSI_PIXEL_FORMAT_16;
176 	case MIPI_DSI_FMT_RGB666:
177 		return NWL_DSI_PIXEL_FORMAT_18L;
178 	case MIPI_DSI_FMT_RGB666_PACKED:
179 		return NWL_DSI_PIXEL_FORMAT_18;
180 	case MIPI_DSI_FMT_RGB888:
181 		return NWL_DSI_PIXEL_FORMAT_24;
182 	default:
183 		return -EINVAL;
184 	}
185 }
186 
187 /*
188  * ps2bc - Picoseconds to byte clock cycles
189  */
190 static u32 ps2bc(struct nwl_dsi *dsi, unsigned long long ps)
191 {
192 	u32 bpp = mipi_dsi_pixel_format_to_bpp(dsi->format);
193 
194 	return DIV64_U64_ROUND_UP(ps * dsi->mode.clock * bpp,
195 				  dsi->lanes * 8ULL * NSEC_PER_SEC);
196 }
197 
198 /*
199  * ui2bc - UI time periods to byte clock cycles
200  */
201 static u32 ui2bc(unsigned int ui)
202 {
203 	return DIV_ROUND_UP(ui, BITS_PER_BYTE);
204 }
205 
206 /*
207  * us2bc - micro seconds to lp clock cycles
208  */
209 static u32 us2lp(u32 lp_clk_rate, unsigned long us)
210 {
211 	return DIV_ROUND_UP(us * lp_clk_rate, USEC_PER_SEC);
212 }
213 
214 static int nwl_dsi_config_host(struct nwl_dsi *dsi)
215 {
216 	u32 cycles;
217 	struct phy_configure_opts_mipi_dphy *cfg = &dsi->phy_cfg.mipi_dphy;
218 
219 	if (dsi->lanes < 1 || dsi->lanes > 4)
220 		return -EINVAL;
221 
222 	DRM_DEV_DEBUG_DRIVER(dsi->dev, "DSI Lanes %d\n", dsi->lanes);
223 	nwl_dsi_write(dsi, NWL_DSI_CFG_NUM_LANES, dsi->lanes - 1);
224 
225 	if (dsi->dsi_mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS) {
226 		nwl_dsi_write(dsi, NWL_DSI_CFG_NONCONTINUOUS_CLK, 0x01);
227 		nwl_dsi_write(dsi, NWL_DSI_CFG_AUTOINSERT_EOTP, 0x01);
228 	} else {
229 		nwl_dsi_write(dsi, NWL_DSI_CFG_NONCONTINUOUS_CLK, 0x00);
230 		nwl_dsi_write(dsi, NWL_DSI_CFG_AUTOINSERT_EOTP, 0x00);
231 	}
232 
233 	/* values in byte clock cycles */
234 	cycles = ui2bc(cfg->clk_pre);
235 	DRM_DEV_DEBUG_DRIVER(dsi->dev, "cfg_t_pre: 0x%x\n", cycles);
236 	nwl_dsi_write(dsi, NWL_DSI_CFG_T_PRE, cycles);
237 	cycles = ps2bc(dsi, cfg->lpx + cfg->clk_prepare + cfg->clk_zero);
238 	DRM_DEV_DEBUG_DRIVER(dsi->dev, "cfg_tx_gap (pre): 0x%x\n", cycles);
239 	cycles += ui2bc(cfg->clk_pre);
240 	DRM_DEV_DEBUG_DRIVER(dsi->dev, "cfg_t_post: 0x%x\n", cycles);
241 	nwl_dsi_write(dsi, NWL_DSI_CFG_T_POST, cycles);
242 	cycles = ps2bc(dsi, cfg->hs_exit);
243 	DRM_DEV_DEBUG_DRIVER(dsi->dev, "cfg_tx_gap: 0x%x\n", cycles);
244 	nwl_dsi_write(dsi, NWL_DSI_CFG_TX_GAP, cycles);
245 
246 	nwl_dsi_write(dsi, NWL_DSI_CFG_EXTRA_CMDS_AFTER_EOTP, 0x01);
247 	nwl_dsi_write(dsi, NWL_DSI_CFG_HTX_TO_COUNT, 0x00);
248 	nwl_dsi_write(dsi, NWL_DSI_CFG_LRX_H_TO_COUNT, 0x00);
249 	nwl_dsi_write(dsi, NWL_DSI_CFG_BTA_H_TO_COUNT, 0x00);
250 	/* In LP clock cycles */
251 	cycles = us2lp(cfg->lp_clk_rate, cfg->wakeup);
252 	DRM_DEV_DEBUG_DRIVER(dsi->dev, "cfg_twakeup: 0x%x\n", cycles);
253 	nwl_dsi_write(dsi, NWL_DSI_CFG_TWAKEUP, cycles);
254 
255 	return nwl_dsi_clear_error(dsi);
256 }
257 
258 static int nwl_dsi_config_dpi(struct nwl_dsi *dsi)
259 {
260 	u32 mode;
261 	int color_format;
262 	bool burst_mode;
263 	int hfront_porch, hback_porch, vfront_porch, vback_porch;
264 	int hsync_len, vsync_len;
265 
266 	hfront_porch = dsi->mode.hsync_start - dsi->mode.hdisplay;
267 	hsync_len = dsi->mode.hsync_end - dsi->mode.hsync_start;
268 	hback_porch = dsi->mode.htotal - dsi->mode.hsync_end;
269 
270 	vfront_porch = dsi->mode.vsync_start - dsi->mode.vdisplay;
271 	vsync_len = dsi->mode.vsync_end - dsi->mode.vsync_start;
272 	vback_porch = dsi->mode.vtotal - dsi->mode.vsync_end;
273 
274 	DRM_DEV_DEBUG_DRIVER(dsi->dev, "hfront_porch = %d\n", hfront_porch);
275 	DRM_DEV_DEBUG_DRIVER(dsi->dev, "hback_porch = %d\n", hback_porch);
276 	DRM_DEV_DEBUG_DRIVER(dsi->dev, "hsync_len = %d\n", hsync_len);
277 	DRM_DEV_DEBUG_DRIVER(dsi->dev, "hdisplay = %d\n", dsi->mode.hdisplay);
278 	DRM_DEV_DEBUG_DRIVER(dsi->dev, "vfront_porch = %d\n", vfront_porch);
279 	DRM_DEV_DEBUG_DRIVER(dsi->dev, "vback_porch = %d\n", vback_porch);
280 	DRM_DEV_DEBUG_DRIVER(dsi->dev, "vsync_len = %d\n", vsync_len);
281 	DRM_DEV_DEBUG_DRIVER(dsi->dev, "vactive = %d\n", dsi->mode.vdisplay);
282 	DRM_DEV_DEBUG_DRIVER(dsi->dev, "clock = %d kHz\n", dsi->mode.clock);
283 
284 	color_format = nwl_dsi_get_dpi_pixel_format(dsi->format);
285 	if (color_format < 0) {
286 		DRM_DEV_ERROR(dsi->dev, "Invalid color format 0x%x\n",
287 			      dsi->format);
288 		return color_format;
289 	}
290 	DRM_DEV_DEBUG_DRIVER(dsi->dev, "pixel fmt = %d\n", dsi->format);
291 
292 	nwl_dsi_write(dsi, NWL_DSI_INTERFACE_COLOR_CODING, NWL_DSI_DPI_24_BIT);
293 	nwl_dsi_write(dsi, NWL_DSI_PIXEL_FORMAT, color_format);
294 	nwl_dsi_write(dsi, NWL_DSI_VSYNC_POLARITY,
295 		      dsi->mode.flags & DRM_MODE_FLAG_PVSYNC ?
296 		      NWL_DSI_VSYNC_POLARITY_ACTIVE_HIGH :
297 		      NWL_DSI_VSYNC_POLARITY_ACTIVE_LOW);
298 	nwl_dsi_write(dsi, NWL_DSI_HSYNC_POLARITY,
299 		      dsi->mode.flags & DRM_MODE_FLAG_PHSYNC ?
300 		      NWL_DSI_HSYNC_POLARITY_ACTIVE_HIGH :
301 		      NWL_DSI_HSYNC_POLARITY_ACTIVE_LOW);
302 
303 	burst_mode = (dsi->dsi_mode_flags & MIPI_DSI_MODE_VIDEO_BURST) &&
304 		     !(dsi->dsi_mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE);
305 
306 	if (burst_mode) {
307 		nwl_dsi_write(dsi, NWL_DSI_VIDEO_MODE, NWL_DSI_VM_BURST_MODE);
308 		nwl_dsi_write(dsi, NWL_DSI_PIXEL_FIFO_SEND_LEVEL, 256);
309 	} else {
310 		mode = ((dsi->dsi_mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) ?
311 				NWL_DSI_VM_BURST_MODE_WITH_SYNC_PULSES :
312 				NWL_DSI_VM_NON_BURST_MODE_WITH_SYNC_EVENTS);
313 		nwl_dsi_write(dsi, NWL_DSI_VIDEO_MODE, mode);
314 		nwl_dsi_write(dsi, NWL_DSI_PIXEL_FIFO_SEND_LEVEL,
315 			      dsi->mode.hdisplay);
316 	}
317 
318 	nwl_dsi_write(dsi, NWL_DSI_HFP, hfront_porch);
319 	nwl_dsi_write(dsi, NWL_DSI_HBP, hback_porch);
320 	nwl_dsi_write(dsi, NWL_DSI_HSA, hsync_len);
321 
322 	nwl_dsi_write(dsi, NWL_DSI_ENABLE_MULT_PKTS, 0x0);
323 	nwl_dsi_write(dsi, NWL_DSI_BLLP_MODE, 0x1);
324 	nwl_dsi_write(dsi, NWL_DSI_USE_NULL_PKT_BLLP, 0x0);
325 	nwl_dsi_write(dsi, NWL_DSI_VC, 0x0);
326 
327 	nwl_dsi_write(dsi, NWL_DSI_PIXEL_PAYLOAD_SIZE, dsi->mode.hdisplay);
328 	nwl_dsi_write(dsi, NWL_DSI_VACTIVE, dsi->mode.vdisplay - 1);
329 	nwl_dsi_write(dsi, NWL_DSI_VBP, vback_porch);
330 	nwl_dsi_write(dsi, NWL_DSI_VFP, vfront_porch);
331 
332 	return nwl_dsi_clear_error(dsi);
333 }
334 
335 static int nwl_dsi_init_interrupts(struct nwl_dsi *dsi)
336 {
337 	u32 irq_enable = ~(u32)(NWL_DSI_TX_PKT_DONE_MASK |
338 				NWL_DSI_RX_PKT_HDR_RCVD_MASK |
339 				NWL_DSI_TX_FIFO_OVFLW_MASK |
340 				NWL_DSI_HS_TX_TIMEOUT_MASK);
341 
342 	nwl_dsi_write(dsi, NWL_DSI_IRQ_MASK, irq_enable);
343 	nwl_dsi_write(dsi, NWL_DSI_IRQ_MASK2, 0x7);
344 
345 	return nwl_dsi_clear_error(dsi);
346 }
347 
348 static int nwl_dsi_host_attach(struct mipi_dsi_host *dsi_host,
349 			       struct mipi_dsi_device *device)
350 {
351 	struct nwl_dsi *dsi = container_of(dsi_host, struct nwl_dsi, dsi_host);
352 	struct device *dev = dsi->dev;
353 
354 	DRM_DEV_INFO(dev, "lanes=%u, format=0x%x flags=0x%lx\n", device->lanes,
355 		     device->format, device->mode_flags);
356 
357 	if (device->lanes < 1 || device->lanes > 4)
358 		return -EINVAL;
359 
360 	dsi->lanes = device->lanes;
361 	dsi->format = device->format;
362 	dsi->dsi_mode_flags = device->mode_flags;
363 
364 	return 0;
365 }
366 
367 static bool nwl_dsi_read_packet(struct nwl_dsi *dsi, u32 status)
368 {
369 	struct device *dev = dsi->dev;
370 	struct nwl_dsi_transfer *xfer = dsi->xfer;
371 	int err;
372 	u8 *payload = xfer->msg->rx_buf;
373 	u32 val;
374 	u16 word_count;
375 	u8 channel;
376 	u8 data_type;
377 
378 	xfer->status = 0;
379 
380 	if (xfer->rx_word_count == 0) {
381 		if (!(status & NWL_DSI_RX_PKT_HDR_RCVD))
382 			return false;
383 		/* Get the RX header and parse it */
384 		val = nwl_dsi_read(dsi, NWL_DSI_RX_PKT_HEADER);
385 		err = nwl_dsi_clear_error(dsi);
386 		if (err)
387 			xfer->status = err;
388 		word_count = NWL_DSI_WC(val);
389 		channel = NWL_DSI_RX_VC(val);
390 		data_type = NWL_DSI_RX_DT(val);
391 
392 		if (channel != xfer->msg->channel) {
393 			DRM_DEV_ERROR(dev,
394 				      "[%02X] Channel mismatch (%u != %u)\n",
395 				      xfer->cmd, channel, xfer->msg->channel);
396 			xfer->status = -EINVAL;
397 			return true;
398 		}
399 
400 		switch (data_type) {
401 		case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
402 		case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
403 			if (xfer->msg->rx_len > 1) {
404 				/* read second byte */
405 				payload[1] = word_count >> 8;
406 				++xfer->rx_len;
407 			}
408 			fallthrough;
409 		case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
410 		case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
411 			if (xfer->msg->rx_len > 0) {
412 				/* read first byte */
413 				payload[0] = word_count & 0xff;
414 				++xfer->rx_len;
415 			}
416 			xfer->status = xfer->rx_len;
417 			return true;
418 		case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
419 			word_count &= 0xff;
420 			DRM_DEV_ERROR(dev, "[%02X] DSI error report: 0x%02x\n",
421 				      xfer->cmd, word_count);
422 			xfer->status = -EPROTO;
423 			return true;
424 		}
425 
426 		if (word_count > xfer->msg->rx_len) {
427 			DRM_DEV_ERROR(dev,
428 				"[%02X] Receive buffer too small: %zu (< %u)\n",
429 				xfer->cmd, xfer->msg->rx_len, word_count);
430 			xfer->status = -EINVAL;
431 			return true;
432 		}
433 
434 		xfer->rx_word_count = word_count;
435 	} else {
436 		/* Set word_count from previous header read */
437 		word_count = xfer->rx_word_count;
438 	}
439 
440 	/* If RX payload is not yet received, wait for it */
441 	if (!(status & NWL_DSI_RX_PKT_PAYLOAD_DATA_RCVD))
442 		return false;
443 
444 	/* Read the RX payload */
445 	while (word_count >= 4) {
446 		val = nwl_dsi_read(dsi, NWL_DSI_RX_PAYLOAD);
447 		payload[0] = (val >> 0) & 0xff;
448 		payload[1] = (val >> 8) & 0xff;
449 		payload[2] = (val >> 16) & 0xff;
450 		payload[3] = (val >> 24) & 0xff;
451 		payload += 4;
452 		xfer->rx_len += 4;
453 		word_count -= 4;
454 	}
455 
456 	if (word_count > 0) {
457 		val = nwl_dsi_read(dsi, NWL_DSI_RX_PAYLOAD);
458 		switch (word_count) {
459 		case 3:
460 			payload[2] = (val >> 16) & 0xff;
461 			++xfer->rx_len;
462 			fallthrough;
463 		case 2:
464 			payload[1] = (val >> 8) & 0xff;
465 			++xfer->rx_len;
466 			fallthrough;
467 		case 1:
468 			payload[0] = (val >> 0) & 0xff;
469 			++xfer->rx_len;
470 			break;
471 		}
472 	}
473 
474 	xfer->status = xfer->rx_len;
475 	err = nwl_dsi_clear_error(dsi);
476 	if (err)
477 		xfer->status = err;
478 
479 	return true;
480 }
481 
482 static void nwl_dsi_finish_transmission(struct nwl_dsi *dsi, u32 status)
483 {
484 	struct nwl_dsi_transfer *xfer = dsi->xfer;
485 	bool end_packet = false;
486 
487 	if (!xfer)
488 		return;
489 
490 	if (xfer->direction == DSI_PACKET_SEND &&
491 	    status & NWL_DSI_TX_PKT_DONE) {
492 		xfer->status = xfer->tx_len;
493 		end_packet = true;
494 	} else if (status & NWL_DSI_DPHY_DIRECTION &&
495 		   ((status & (NWL_DSI_RX_PKT_HDR_RCVD |
496 			       NWL_DSI_RX_PKT_PAYLOAD_DATA_RCVD)))) {
497 		end_packet = nwl_dsi_read_packet(dsi, status);
498 	}
499 
500 	if (end_packet)
501 		complete(&xfer->completed);
502 }
503 
504 static void nwl_dsi_begin_transmission(struct nwl_dsi *dsi)
505 {
506 	struct nwl_dsi_transfer *xfer = dsi->xfer;
507 	struct mipi_dsi_packet *pkt = &xfer->packet;
508 	const u8 *payload;
509 	size_t length;
510 	u16 word_count;
511 	u8 hs_mode;
512 	u32 val;
513 	u32 hs_workaround = 0;
514 
515 	/* Send the payload, if any */
516 	length = pkt->payload_length;
517 	payload = pkt->payload;
518 
519 	while (length >= 4) {
520 		val = *(u32 *)payload;
521 		hs_workaround |= !(val & 0xFFFF00);
522 		nwl_dsi_write(dsi, NWL_DSI_TX_PAYLOAD, val);
523 		payload += 4;
524 		length -= 4;
525 	}
526 	/* Send the rest of the payload */
527 	val = 0;
528 	switch (length) {
529 	case 3:
530 		val |= payload[2] << 16;
531 		fallthrough;
532 	case 2:
533 		val |= payload[1] << 8;
534 		hs_workaround |= !(val & 0xFFFF00);
535 		fallthrough;
536 	case 1:
537 		val |= payload[0];
538 		nwl_dsi_write(dsi, NWL_DSI_TX_PAYLOAD, val);
539 		break;
540 	}
541 	xfer->tx_len = pkt->payload_length;
542 
543 	/*
544 	 * Send the header
545 	 * header[0] = Virtual Channel + Data Type
546 	 * header[1] = Word Count LSB (LP) or first param (SP)
547 	 * header[2] = Word Count MSB (LP) or second param (SP)
548 	 */
549 	word_count = pkt->header[1] | (pkt->header[2] << 8);
550 	if (hs_workaround && (dsi->quirks & E11418_HS_MODE_QUIRK)) {
551 		DRM_DEV_DEBUG_DRIVER(dsi->dev,
552 				     "Using hs mode workaround for cmd 0x%x\n",
553 				     xfer->cmd);
554 		hs_mode = 1;
555 	} else {
556 		hs_mode = (xfer->msg->flags & MIPI_DSI_MSG_USE_LPM) ? 0 : 1;
557 	}
558 	val = NWL_DSI_WC(word_count) | NWL_DSI_TX_VC(xfer->msg->channel) |
559 	      NWL_DSI_TX_DT(xfer->msg->type) | NWL_DSI_HS_SEL(hs_mode) |
560 	      NWL_DSI_BTA_TX(xfer->need_bta);
561 	nwl_dsi_write(dsi, NWL_DSI_PKT_CONTROL, val);
562 
563 	/* Send packet command */
564 	nwl_dsi_write(dsi, NWL_DSI_SEND_PACKET, 0x1);
565 }
566 
567 static ssize_t nwl_dsi_host_transfer(struct mipi_dsi_host *dsi_host,
568 				     const struct mipi_dsi_msg *msg)
569 {
570 	struct nwl_dsi *dsi = container_of(dsi_host, struct nwl_dsi, dsi_host);
571 	struct nwl_dsi_transfer xfer;
572 	ssize_t ret = 0;
573 
574 	/* Create packet to be sent */
575 	dsi->xfer = &xfer;
576 	ret = mipi_dsi_create_packet(&xfer.packet, msg);
577 	if (ret < 0) {
578 		dsi->xfer = NULL;
579 		return ret;
580 	}
581 
582 	if ((msg->type & MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM ||
583 	     msg->type & MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM ||
584 	     msg->type & MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM ||
585 	     msg->type & MIPI_DSI_DCS_READ) &&
586 	    msg->rx_len > 0 && msg->rx_buf)
587 		xfer.direction = DSI_PACKET_RECEIVE;
588 	else
589 		xfer.direction = DSI_PACKET_SEND;
590 
591 	xfer.need_bta = (xfer.direction == DSI_PACKET_RECEIVE);
592 	xfer.need_bta |= (msg->flags & MIPI_DSI_MSG_REQ_ACK) ? 1 : 0;
593 	xfer.msg = msg;
594 	xfer.status = -ETIMEDOUT;
595 	xfer.rx_word_count = 0;
596 	xfer.rx_len = 0;
597 	xfer.cmd = 0x00;
598 	if (msg->tx_len > 0)
599 		xfer.cmd = ((u8 *)(msg->tx_buf))[0];
600 	init_completion(&xfer.completed);
601 
602 	ret = clk_prepare_enable(dsi->rx_esc_clk);
603 	if (ret < 0) {
604 		DRM_DEV_ERROR(dsi->dev, "Failed to enable rx_esc clk: %zd\n",
605 			      ret);
606 		return ret;
607 	}
608 	DRM_DEV_DEBUG_DRIVER(dsi->dev, "Enabled rx_esc clk @%lu Hz\n",
609 			     clk_get_rate(dsi->rx_esc_clk));
610 
611 	/* Initiate the DSI packet transmision */
612 	nwl_dsi_begin_transmission(dsi);
613 
614 	if (!wait_for_completion_timeout(&xfer.completed,
615 					 NWL_DSI_MIPI_FIFO_TIMEOUT)) {
616 		DRM_DEV_ERROR(dsi_host->dev, "[%02X] DSI transfer timed out\n",
617 			      xfer.cmd);
618 		ret = -ETIMEDOUT;
619 	} else {
620 		ret = xfer.status;
621 	}
622 
623 	clk_disable_unprepare(dsi->rx_esc_clk);
624 
625 	return ret;
626 }
627 
628 static const struct mipi_dsi_host_ops nwl_dsi_host_ops = {
629 	.attach = nwl_dsi_host_attach,
630 	.transfer = nwl_dsi_host_transfer,
631 };
632 
633 static irqreturn_t nwl_dsi_irq_handler(int irq, void *data)
634 {
635 	u32 irq_status;
636 	struct nwl_dsi *dsi = data;
637 
638 	irq_status = nwl_dsi_read(dsi, NWL_DSI_IRQ_STATUS);
639 
640 	if (irq_status & NWL_DSI_TX_FIFO_OVFLW)
641 		DRM_DEV_ERROR_RATELIMITED(dsi->dev, "tx fifo overflow\n");
642 
643 	if (irq_status & NWL_DSI_HS_TX_TIMEOUT)
644 		DRM_DEV_ERROR_RATELIMITED(dsi->dev, "HS tx timeout\n");
645 
646 	if (irq_status & NWL_DSI_TX_PKT_DONE ||
647 	    irq_status & NWL_DSI_RX_PKT_HDR_RCVD ||
648 	    irq_status & NWL_DSI_RX_PKT_PAYLOAD_DATA_RCVD)
649 		nwl_dsi_finish_transmission(dsi, irq_status);
650 
651 	return IRQ_HANDLED;
652 }
653 
654 static int nwl_dsi_mode_set(struct nwl_dsi *dsi)
655 {
656 	struct device *dev = dsi->dev;
657 	union phy_configure_opts *phy_cfg = &dsi->phy_cfg;
658 	int ret;
659 
660 	if (!dsi->lanes) {
661 		DRM_DEV_ERROR(dev, "Need DSI lanes: %d\n", dsi->lanes);
662 		return -EINVAL;
663 	}
664 
665 	ret = phy_init(dsi->phy);
666 	if (ret < 0) {
667 		DRM_DEV_ERROR(dev, "Failed to init DSI phy: %d\n", ret);
668 		return ret;
669 	}
670 
671 	ret = phy_set_mode(dsi->phy, PHY_MODE_MIPI_DPHY);
672 	if (ret < 0) {
673 		DRM_DEV_ERROR(dev, "Failed to set DSI phy mode: %d\n", ret);
674 		goto uninit_phy;
675 	}
676 
677 	ret = phy_configure(dsi->phy, phy_cfg);
678 	if (ret < 0) {
679 		DRM_DEV_ERROR(dev, "Failed to configure DSI phy: %d\n", ret);
680 		goto uninit_phy;
681 	}
682 
683 	ret = clk_prepare_enable(dsi->tx_esc_clk);
684 	if (ret < 0) {
685 		DRM_DEV_ERROR(dsi->dev, "Failed to enable tx_esc clk: %d\n",
686 			      ret);
687 		goto uninit_phy;
688 	}
689 	DRM_DEV_DEBUG_DRIVER(dsi->dev, "Enabled tx_esc clk @%lu Hz\n",
690 			     clk_get_rate(dsi->tx_esc_clk));
691 
692 	ret = nwl_dsi_config_host(dsi);
693 	if (ret < 0) {
694 		DRM_DEV_ERROR(dev, "Failed to set up DSI: %d", ret);
695 		goto disable_clock;
696 	}
697 
698 	ret = nwl_dsi_config_dpi(dsi);
699 	if (ret < 0) {
700 		DRM_DEV_ERROR(dev, "Failed to set up DPI: %d", ret);
701 		goto disable_clock;
702 	}
703 
704 	ret = phy_power_on(dsi->phy);
705 	if (ret < 0) {
706 		DRM_DEV_ERROR(dev, "Failed to power on DPHY (%d)\n", ret);
707 		goto disable_clock;
708 	}
709 
710 	ret = nwl_dsi_init_interrupts(dsi);
711 	if (ret < 0)
712 		goto power_off_phy;
713 
714 	return ret;
715 
716 power_off_phy:
717 	phy_power_off(dsi->phy);
718 disable_clock:
719 	clk_disable_unprepare(dsi->tx_esc_clk);
720 uninit_phy:
721 	phy_exit(dsi->phy);
722 
723 	return ret;
724 }
725 
726 static int nwl_dsi_disable(struct nwl_dsi *dsi)
727 {
728 	struct device *dev = dsi->dev;
729 
730 	DRM_DEV_DEBUG_DRIVER(dev, "Disabling clocks and phy\n");
731 
732 	phy_power_off(dsi->phy);
733 	phy_exit(dsi->phy);
734 
735 	/* Disabling the clock before the phy breaks enabling dsi again */
736 	clk_disable_unprepare(dsi->tx_esc_clk);
737 
738 	return 0;
739 }
740 
741 static void nwl_dsi_bridge_atomic_disable(struct drm_bridge *bridge,
742 					  struct drm_atomic_commit *state)
743 {
744 	struct nwl_dsi *dsi = bridge_to_dsi(bridge);
745 	int ret;
746 
747 	nwl_dsi_disable(dsi);
748 
749 	ret = reset_control_assert(dsi->rst_dpi);
750 	if (ret < 0) {
751 		DRM_DEV_ERROR(dsi->dev, "Failed to assert DPI: %d\n", ret);
752 		return;
753 	}
754 	ret = reset_control_assert(dsi->rst_byte);
755 	if (ret < 0) {
756 		DRM_DEV_ERROR(dsi->dev, "Failed to assert ESC: %d\n", ret);
757 		return;
758 	}
759 	ret = reset_control_assert(dsi->rst_esc);
760 	if (ret < 0) {
761 		DRM_DEV_ERROR(dsi->dev, "Failed to assert BYTE: %d\n", ret);
762 		return;
763 	}
764 	ret = reset_control_assert(dsi->rst_pclk);
765 	if (ret < 0) {
766 		DRM_DEV_ERROR(dsi->dev, "Failed to assert PCLK: %d\n", ret);
767 		return;
768 	}
769 
770 	clk_disable_unprepare(dsi->core_clk);
771 	clk_disable_unprepare(dsi->lcdif_clk);
772 
773 	pm_runtime_put(dsi->dev);
774 }
775 
776 static int nwl_dsi_get_dphy_params(struct nwl_dsi *dsi,
777 				   const struct drm_display_mode *mode,
778 				   union phy_configure_opts *phy_opts)
779 {
780 	unsigned long rate;
781 	int ret;
782 
783 	if (dsi->lanes < 1 || dsi->lanes > 4)
784 		return -EINVAL;
785 
786 	/*
787 	 * So far the DPHY spec minimal timings work for both mixel
788 	 * dphy and nwl dsi host
789 	 */
790 	ret = phy_mipi_dphy_get_default_config(mode->clock * 1000,
791 		mipi_dsi_pixel_format_to_bpp(dsi->format), dsi->lanes,
792 		&phy_opts->mipi_dphy);
793 	if (ret < 0)
794 		return ret;
795 
796 	rate = clk_get_rate(dsi->tx_esc_clk);
797 	DRM_DEV_DEBUG_DRIVER(dsi->dev, "LP clk is @%lu Hz\n", rate);
798 	phy_opts->mipi_dphy.lp_clk_rate = rate;
799 
800 	return 0;
801 }
802 
803 static enum drm_mode_status
804 nwl_dsi_bridge_mode_valid(struct drm_bridge *bridge,
805 			  const struct drm_display_info *info,
806 			  const struct drm_display_mode *mode)
807 {
808 	struct nwl_dsi *dsi = bridge_to_dsi(bridge);
809 	int bpp = mipi_dsi_pixel_format_to_bpp(dsi->format);
810 
811 	if (mode->clock * bpp > 15000000 * dsi->lanes)
812 		return MODE_CLOCK_HIGH;
813 
814 	if (mode->clock * bpp < 80000 * dsi->lanes)
815 		return MODE_CLOCK_LOW;
816 
817 	return MODE_OK;
818 }
819 
820 static int nwl_dsi_bridge_atomic_check(struct drm_bridge *bridge,
821 				       struct drm_bridge_state *bridge_state,
822 				       struct drm_crtc_state *crtc_state,
823 				       struct drm_connector_state *conn_state)
824 {
825 	struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
826 	struct nwl_dsi *dsi = bridge_to_dsi(bridge);
827 
828 	/* At least LCDIF + NWL needs active high sync */
829 	if (dsi->endpoint == NWL_DSI_ENDPOINT_LCDIF) {
830 		adjusted_mode->flags |= (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
831 		adjusted_mode->flags &= ~(DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC);
832 	}
833 
834 	/*
835 	 * Do a full modeset if crtc_state->active is changed to be true.
836 	 * This ensures our ->mode_set() is called to get the DSI controller
837 	 * and the PHY ready to send DCS commands, when only the connector's
838 	 * DPMS is brought out of "Off" status.
839 	 */
840 	if (crtc_state->active_changed && crtc_state->active)
841 		crtc_state->mode_changed = true;
842 
843 	return 0;
844 }
845 
846 static void
847 nwl_dsi_bridge_mode_set(struct drm_bridge *bridge,
848 			const struct drm_display_mode *mode,
849 			const struct drm_display_mode *adjusted_mode)
850 {
851 	struct nwl_dsi *dsi = bridge_to_dsi(bridge);
852 	struct device *dev = dsi->dev;
853 	union phy_configure_opts new_cfg;
854 	unsigned long phy_ref_rate;
855 	int ret;
856 
857 	ret = nwl_dsi_get_dphy_params(dsi, adjusted_mode, &new_cfg);
858 	if (ret < 0)
859 		return;
860 
861 	phy_ref_rate = clk_get_rate(dsi->phy_ref_clk);
862 	DRM_DEV_DEBUG_DRIVER(dev, "PHY at ref rate: %lu\n", phy_ref_rate);
863 	/* Save the new desired phy config */
864 	memcpy(&dsi->phy_cfg, &new_cfg, sizeof(new_cfg));
865 
866 	drm_mode_copy(&dsi->mode, adjusted_mode);
867 	drm_mode_debug_printmodeline(adjusted_mode);
868 
869 	if (pm_runtime_resume_and_get(dev) < 0)
870 		return;
871 
872 	if (clk_prepare_enable(dsi->lcdif_clk) < 0)
873 		goto runtime_put;
874 	if (clk_prepare_enable(dsi->core_clk) < 0)
875 		goto runtime_put;
876 
877 	/* Step 1 from DSI reset-out instructions */
878 	ret = reset_control_deassert(dsi->rst_pclk);
879 	if (ret < 0) {
880 		DRM_DEV_ERROR(dev, "Failed to deassert PCLK: %d\n", ret);
881 		goto runtime_put;
882 	}
883 
884 	/* Step 2 from DSI reset-out instructions */
885 	nwl_dsi_mode_set(dsi);
886 
887 	/* Step 3 from DSI reset-out instructions */
888 	ret = reset_control_deassert(dsi->rst_esc);
889 	if (ret < 0) {
890 		DRM_DEV_ERROR(dev, "Failed to deassert ESC: %d\n", ret);
891 		goto runtime_put;
892 	}
893 	ret = reset_control_deassert(dsi->rst_byte);
894 	if (ret < 0) {
895 		DRM_DEV_ERROR(dev, "Failed to deassert BYTE: %d\n", ret);
896 		goto runtime_put;
897 	}
898 
899 	return;
900 
901 runtime_put:
902 	pm_runtime_put_sync(dev);
903 }
904 
905 static void nwl_dsi_bridge_atomic_enable(struct drm_bridge *bridge,
906 					 struct drm_atomic_commit *state)
907 {
908 	struct nwl_dsi *dsi = bridge_to_dsi(bridge);
909 	int ret;
910 
911 	/* Step 5 from DSI reset-out instructions */
912 	ret = reset_control_deassert(dsi->rst_dpi);
913 	if (ret < 0)
914 		DRM_DEV_ERROR(dsi->dev, "Failed to deassert DPI: %d\n", ret);
915 }
916 
917 static int nwl_dsi_bridge_attach(struct drm_bridge *bridge,
918 				 struct drm_encoder *encoder,
919 				 enum drm_bridge_attach_flags flags)
920 {
921 	struct nwl_dsi *dsi = bridge_to_dsi(bridge);
922 	struct drm_bridge *panel_bridge;
923 
924 	panel_bridge = devm_drm_of_get_bridge(dsi->dev, dsi->dev->of_node, 1, 0);
925 	if (IS_ERR(panel_bridge))
926 		return PTR_ERR(panel_bridge);
927 
928 	return drm_bridge_attach(encoder, panel_bridge, bridge, flags);
929 }
930 
931 static u32 *nwl_bridge_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
932 						 struct drm_bridge_state *bridge_state,
933 						 struct drm_crtc_state *crtc_state,
934 						 struct drm_connector_state *conn_state,
935 						 u32 output_fmt,
936 						 unsigned int *num_input_fmts)
937 {
938 	u32 *input_fmts, input_fmt;
939 
940 	*num_input_fmts = 0;
941 
942 	switch (output_fmt) {
943 	/* If MEDIA_BUS_FMT_FIXED is tested, return default bus format */
944 	case MEDIA_BUS_FMT_FIXED:
945 		input_fmt = MEDIA_BUS_FMT_RGB888_1X24;
946 		break;
947 	case MEDIA_BUS_FMT_RGB888_1X24:
948 	case MEDIA_BUS_FMT_RGB666_1X18:
949 	case MEDIA_BUS_FMT_RGB565_1X16:
950 		input_fmt = output_fmt;
951 		break;
952 	default:
953 		return NULL;
954 	}
955 
956 	input_fmts = kcalloc(1, sizeof(*input_fmts), GFP_KERNEL);
957 	if (!input_fmts)
958 		return NULL;
959 	input_fmts[0] = input_fmt;
960 	*num_input_fmts = 1;
961 
962 	return input_fmts;
963 }
964 
965 static const struct drm_bridge_funcs nwl_dsi_bridge_funcs = {
966 	.atomic_duplicate_state	= drm_atomic_helper_bridge_duplicate_state,
967 	.atomic_destroy_state	= drm_atomic_helper_bridge_destroy_state,
968 	.atomic_create_state		= drm_atomic_helper_bridge_create_state,
969 	.atomic_check		= nwl_dsi_bridge_atomic_check,
970 	.atomic_enable		= nwl_dsi_bridge_atomic_enable,
971 	.atomic_disable		= nwl_dsi_bridge_atomic_disable,
972 	.atomic_get_input_bus_fmts = nwl_bridge_atomic_get_input_bus_fmts,
973 	.mode_set		= nwl_dsi_bridge_mode_set,
974 	.mode_valid		= nwl_dsi_bridge_mode_valid,
975 	.attach			= nwl_dsi_bridge_attach,
976 };
977 
978 static int nwl_dsi_parse_dt(struct nwl_dsi *dsi)
979 {
980 	struct platform_device *pdev = to_platform_device(dsi->dev);
981 	struct clk *clk;
982 	void __iomem *base;
983 	int ret;
984 
985 	dsi->phy = devm_phy_get(dsi->dev, "dphy");
986 	if (IS_ERR(dsi->phy)) {
987 		ret = PTR_ERR(dsi->phy);
988 		if (ret != -EPROBE_DEFER)
989 			DRM_DEV_ERROR(dsi->dev, "Could not get PHY: %d\n", ret);
990 		return ret;
991 	}
992 
993 	clk = devm_clk_get(dsi->dev, "lcdif");
994 	if (IS_ERR(clk)) {
995 		ret = PTR_ERR(clk);
996 		DRM_DEV_ERROR(dsi->dev, "Failed to get lcdif clock: %d\n",
997 			      ret);
998 		return ret;
999 	}
1000 	dsi->lcdif_clk = clk;
1001 
1002 	clk = devm_clk_get(dsi->dev, "core");
1003 	if (IS_ERR(clk)) {
1004 		ret = PTR_ERR(clk);
1005 		DRM_DEV_ERROR(dsi->dev, "Failed to get core clock: %d\n",
1006 			      ret);
1007 		return ret;
1008 	}
1009 	dsi->core_clk = clk;
1010 
1011 	clk = devm_clk_get(dsi->dev, "phy_ref");
1012 	if (IS_ERR(clk)) {
1013 		ret = PTR_ERR(clk);
1014 		DRM_DEV_ERROR(dsi->dev, "Failed to get phy_ref clock: %d\n",
1015 			      ret);
1016 		return ret;
1017 	}
1018 	dsi->phy_ref_clk = clk;
1019 
1020 	clk = devm_clk_get(dsi->dev, "rx_esc");
1021 	if (IS_ERR(clk)) {
1022 		ret = PTR_ERR(clk);
1023 		DRM_DEV_ERROR(dsi->dev, "Failed to get rx_esc clock: %d\n",
1024 			      ret);
1025 		return ret;
1026 	}
1027 	dsi->rx_esc_clk = clk;
1028 
1029 	clk = devm_clk_get(dsi->dev, "tx_esc");
1030 	if (IS_ERR(clk)) {
1031 		ret = PTR_ERR(clk);
1032 		DRM_DEV_ERROR(dsi->dev, "Failed to get tx_esc clock: %d\n",
1033 			      ret);
1034 		return ret;
1035 	}
1036 	dsi->tx_esc_clk = clk;
1037 
1038 	dsi->mux = devm_mux_control_get(dsi->dev, NULL);
1039 	if (IS_ERR(dsi->mux)) {
1040 		ret = PTR_ERR(dsi->mux);
1041 		if (ret != -EPROBE_DEFER)
1042 			DRM_DEV_ERROR(dsi->dev, "Failed to get mux: %d\n", ret);
1043 		return ret;
1044 	}
1045 
1046 	base = devm_platform_ioremap_resource(pdev, 0);
1047 	if (IS_ERR(base))
1048 		return PTR_ERR(base);
1049 
1050 	dsi->regmap =
1051 		devm_regmap_init_mmio(dsi->dev, base, &nwl_dsi_regmap_config);
1052 	if (IS_ERR(dsi->regmap)) {
1053 		ret = PTR_ERR(dsi->regmap);
1054 		DRM_DEV_ERROR(dsi->dev, "Failed to create NWL DSI regmap: %d\n",
1055 			      ret);
1056 		return ret;
1057 	}
1058 
1059 	dsi->irq = platform_get_irq(pdev, 0);
1060 	if (dsi->irq < 0) {
1061 		DRM_DEV_ERROR(dsi->dev, "Failed to get device IRQ: %d\n",
1062 			      dsi->irq);
1063 		return dsi->irq;
1064 	}
1065 
1066 	dsi->rst_pclk = devm_reset_control_get_exclusive(dsi->dev, "pclk");
1067 	if (IS_ERR(dsi->rst_pclk)) {
1068 		DRM_DEV_ERROR(dsi->dev, "Failed to get pclk reset: %ld\n",
1069 			      PTR_ERR(dsi->rst_pclk));
1070 		return PTR_ERR(dsi->rst_pclk);
1071 	}
1072 	dsi->rst_byte = devm_reset_control_get_exclusive(dsi->dev, "byte");
1073 	if (IS_ERR(dsi->rst_byte)) {
1074 		DRM_DEV_ERROR(dsi->dev, "Failed to get byte reset: %ld\n",
1075 			      PTR_ERR(dsi->rst_byte));
1076 		return PTR_ERR(dsi->rst_byte);
1077 	}
1078 	dsi->rst_esc = devm_reset_control_get_exclusive(dsi->dev, "esc");
1079 	if (IS_ERR(dsi->rst_esc)) {
1080 		DRM_DEV_ERROR(dsi->dev, "Failed to get esc reset: %ld\n",
1081 			      PTR_ERR(dsi->rst_esc));
1082 		return PTR_ERR(dsi->rst_esc);
1083 	}
1084 	dsi->rst_dpi = devm_reset_control_get_exclusive(dsi->dev, "dpi");
1085 	if (IS_ERR(dsi->rst_dpi)) {
1086 		DRM_DEV_ERROR(dsi->dev, "Failed to get dpi reset: %ld\n",
1087 			      PTR_ERR(dsi->rst_dpi));
1088 		return PTR_ERR(dsi->rst_dpi);
1089 	}
1090 	return 0;
1091 }
1092 
1093 static int nwl_dsi_select_input(struct nwl_dsi *dsi)
1094 {
1095 	struct device_node *remote;
1096 	int ret;
1097 
1098 	remote = of_graph_get_remote_node(dsi->dev->of_node, 0,
1099 					  NWL_DSI_ENDPOINT_LCDIF);
1100 	if (remote) {
1101 		dsi->endpoint = NWL_DSI_ENDPOINT_LCDIF;
1102 	} else {
1103 		remote = of_graph_get_remote_node(dsi->dev->of_node, 0,
1104 						  NWL_DSI_ENDPOINT_DCSS);
1105 		if (!remote) {
1106 			DRM_DEV_ERROR(dsi->dev,
1107 				      "No valid input endpoint found\n");
1108 			return -EINVAL;
1109 		}
1110 		dsi->endpoint = NWL_DSI_ENDPOINT_DCSS;
1111 	}
1112 
1113 	DRM_DEV_INFO(dsi->dev, "Using %s as input source\n",
1114 		     (dsi->endpoint == NWL_DSI_ENDPOINT_DCSS) ? "DCSS" : "LCDIF");
1115 	ret = mux_control_try_select(dsi->mux, dsi->endpoint);
1116 	if (ret < 0)
1117 		DRM_DEV_ERROR(dsi->dev, "Failed to select input: %d\n", ret);
1118 
1119 	of_node_put(remote);
1120 	return ret;
1121 }
1122 
1123 static int nwl_dsi_deselect_input(struct nwl_dsi *dsi)
1124 {
1125 	int ret;
1126 
1127 	ret = mux_control_deselect(dsi->mux);
1128 	if (ret < 0)
1129 		DRM_DEV_ERROR(dsi->dev, "Failed to deselect input: %d\n", ret);
1130 
1131 	return ret;
1132 }
1133 
1134 static const struct drm_bridge_timings nwl_dsi_timings = {
1135 	.input_bus_flags = DRM_BUS_FLAG_DE_LOW,
1136 };
1137 
1138 static const struct of_device_id nwl_dsi_dt_ids[] = {
1139 	{ .compatible = "fsl,imx8mq-nwl-dsi", },
1140 	{ /* sentinel */ }
1141 };
1142 MODULE_DEVICE_TABLE(of, nwl_dsi_dt_ids);
1143 
1144 static const struct soc_device_attribute nwl_dsi_quirks_match[] = {
1145 	{ .soc_id = "i.MX8MQ", .revision = "2.0",
1146 	  .data = (void *)E11418_HS_MODE_QUIRK },
1147 	{ /* sentinel. */ }
1148 };
1149 
1150 static int nwl_dsi_probe(struct platform_device *pdev)
1151 {
1152 	struct device *dev = &pdev->dev;
1153 	const struct soc_device_attribute *attr;
1154 	struct nwl_dsi *dsi;
1155 	int ret;
1156 
1157 	dsi = devm_drm_bridge_alloc(dev, struct nwl_dsi, bridge,
1158 				    &nwl_dsi_bridge_funcs);
1159 	if (IS_ERR(dsi))
1160 		return PTR_ERR(dsi);
1161 
1162 	dsi->dev = dev;
1163 
1164 	ret = nwl_dsi_parse_dt(dsi);
1165 	if (ret)
1166 		return ret;
1167 
1168 	ret = devm_request_irq(dev, dsi->irq, nwl_dsi_irq_handler, 0,
1169 			       dev_name(dev), dsi);
1170 	if (ret < 0) {
1171 		DRM_DEV_ERROR(dev, "Failed to request IRQ %d: %d\n", dsi->irq,
1172 			      ret);
1173 		return ret;
1174 	}
1175 
1176 	dsi->dsi_host.ops = &nwl_dsi_host_ops;
1177 	dsi->dsi_host.dev = dev;
1178 	ret = mipi_dsi_host_register(&dsi->dsi_host);
1179 	if (ret) {
1180 		DRM_DEV_ERROR(dev, "Failed to register MIPI host: %d\n", ret);
1181 		return ret;
1182 	}
1183 
1184 	attr = soc_device_match(nwl_dsi_quirks_match);
1185 	if (attr)
1186 		dsi->quirks = (uintptr_t)attr->data;
1187 
1188 	dsi->bridge.driver_private = dsi;
1189 	dsi->bridge.of_node = dev->of_node;
1190 	dsi->bridge.timings = &nwl_dsi_timings;
1191 	dsi->bridge.type = DRM_MODE_CONNECTOR_DSI;
1192 
1193 	dev_set_drvdata(dev, dsi);
1194 	pm_runtime_enable(dev);
1195 
1196 	ret = nwl_dsi_select_input(dsi);
1197 	if (ret < 0) {
1198 		pm_runtime_disable(dev);
1199 		mipi_dsi_host_unregister(&dsi->dsi_host);
1200 		return ret;
1201 	}
1202 
1203 	drm_bridge_add(&dsi->bridge);
1204 	return 0;
1205 }
1206 
1207 static void nwl_dsi_remove(struct platform_device *pdev)
1208 {
1209 	struct nwl_dsi *dsi = platform_get_drvdata(pdev);
1210 
1211 	nwl_dsi_deselect_input(dsi);
1212 	mipi_dsi_host_unregister(&dsi->dsi_host);
1213 	drm_bridge_remove(&dsi->bridge);
1214 	pm_runtime_disable(&pdev->dev);
1215 }
1216 
1217 static struct platform_driver nwl_dsi_driver = {
1218 	.probe		= nwl_dsi_probe,
1219 	.remove		= nwl_dsi_remove,
1220 	.driver		= {
1221 		.of_match_table = nwl_dsi_dt_ids,
1222 		.name	= DRV_NAME,
1223 	},
1224 };
1225 
1226 module_platform_driver(nwl_dsi_driver);
1227 
1228 MODULE_AUTHOR("NXP Semiconductor");
1229 MODULE_AUTHOR("Purism SPC");
1230 MODULE_DESCRIPTION("Northwest Logic MIPI-DSI driver");
1231 MODULE_LICENSE("GPL"); /* GPLv2 or later */
1232