xref: /openbmc/linux/drivers/gpu/drm/vc4/vc4_dsi.c (revision 9221b289)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2016 Broadcom
4  */
5 
6 /**
7  * DOC: VC4 DSI0/DSI1 module
8  *
9  * BCM2835 contains two DSI modules, DSI0 and DSI1.  DSI0 is a
10  * single-lane DSI controller, while DSI1 is a more modern 4-lane DSI
11  * controller.
12  *
13  * Most Raspberry Pi boards expose DSI1 as their "DISPLAY" connector,
14  * while the compute module brings both DSI0 and DSI1 out.
15  *
16  * This driver has been tested for DSI1 video-mode display only
17  * currently, with most of the information necessary for DSI0
18  * hopefully present.
19  */
20 
21 #include <linux/clk-provider.h>
22 #include <linux/clk.h>
23 #include <linux/completion.h>
24 #include <linux/component.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/dmaengine.h>
27 #include <linux/i2c.h>
28 #include <linux/io.h>
29 #include <linux/of_address.h>
30 #include <linux/of_platform.h>
31 #include <linux/pm_runtime.h>
32 
33 #include <drm/drm_atomic_helper.h>
34 #include <drm/drm_bridge.h>
35 #include <drm/drm_edid.h>
36 #include <drm/drm_mipi_dsi.h>
37 #include <drm/drm_of.h>
38 #include <drm/drm_panel.h>
39 #include <drm/drm_probe_helper.h>
40 #include <drm/drm_simple_kms_helper.h>
41 
42 #include "vc4_drv.h"
43 #include "vc4_regs.h"
44 
45 #define DSI_CMD_FIFO_DEPTH  16
46 #define DSI_PIX_FIFO_DEPTH 256
47 #define DSI_PIX_FIFO_WIDTH   4
48 
49 #define DSI0_CTRL		0x00
50 
51 /* Command packet control. */
52 #define DSI0_TXPKT1C		0x04 /* AKA PKTC */
53 #define DSI1_TXPKT1C		0x04
54 # define DSI_TXPKT1C_TRIG_CMD_MASK	VC4_MASK(31, 24)
55 # define DSI_TXPKT1C_TRIG_CMD_SHIFT	24
56 # define DSI_TXPKT1C_CMD_REPEAT_MASK	VC4_MASK(23, 10)
57 # define DSI_TXPKT1C_CMD_REPEAT_SHIFT	10
58 
59 # define DSI_TXPKT1C_DISPLAY_NO_MASK	VC4_MASK(9, 8)
60 # define DSI_TXPKT1C_DISPLAY_NO_SHIFT	8
61 /* Short, trigger, BTA, or a long packet that fits all in CMDFIFO. */
62 # define DSI_TXPKT1C_DISPLAY_NO_SHORT		0
63 /* Primary display where cmdfifo provides part of the payload and
64  * pixelvalve the rest.
65  */
66 # define DSI_TXPKT1C_DISPLAY_NO_PRIMARY		1
67 /* Secondary display where cmdfifo provides part of the payload and
68  * pixfifo the rest.
69  */
70 # define DSI_TXPKT1C_DISPLAY_NO_SECONDARY	2
71 
72 # define DSI_TXPKT1C_CMD_TX_TIME_MASK	VC4_MASK(7, 6)
73 # define DSI_TXPKT1C_CMD_TX_TIME_SHIFT	6
74 
75 # define DSI_TXPKT1C_CMD_CTRL_MASK	VC4_MASK(5, 4)
76 # define DSI_TXPKT1C_CMD_CTRL_SHIFT	4
77 /* Command only.  Uses TXPKT1H and DISPLAY_NO */
78 # define DSI_TXPKT1C_CMD_CTRL_TX	0
79 /* Command with BTA for either ack or read data. */
80 # define DSI_TXPKT1C_CMD_CTRL_RX	1
81 /* Trigger according to TRIG_CMD */
82 # define DSI_TXPKT1C_CMD_CTRL_TRIG	2
83 /* BTA alone for getting error status after a command, or a TE trigger
84  * without a previous command.
85  */
86 # define DSI_TXPKT1C_CMD_CTRL_BTA	3
87 
88 # define DSI_TXPKT1C_CMD_MODE_LP	BIT(3)
89 # define DSI_TXPKT1C_CMD_TYPE_LONG	BIT(2)
90 # define DSI_TXPKT1C_CMD_TE_EN		BIT(1)
91 # define DSI_TXPKT1C_CMD_EN		BIT(0)
92 
93 /* Command packet header. */
94 #define DSI0_TXPKT1H		0x08 /* AKA PKTH */
95 #define DSI1_TXPKT1H		0x08
96 # define DSI_TXPKT1H_BC_CMDFIFO_MASK	VC4_MASK(31, 24)
97 # define DSI_TXPKT1H_BC_CMDFIFO_SHIFT	24
98 # define DSI_TXPKT1H_BC_PARAM_MASK	VC4_MASK(23, 8)
99 # define DSI_TXPKT1H_BC_PARAM_SHIFT	8
100 # define DSI_TXPKT1H_BC_DT_MASK		VC4_MASK(7, 0)
101 # define DSI_TXPKT1H_BC_DT_SHIFT	0
102 
103 #define DSI0_RXPKT1H		0x0c /* AKA RX1_PKTH */
104 #define DSI1_RXPKT1H		0x14
105 # define DSI_RXPKT1H_CRC_ERR		BIT(31)
106 # define DSI_RXPKT1H_DET_ERR		BIT(30)
107 # define DSI_RXPKT1H_ECC_ERR		BIT(29)
108 # define DSI_RXPKT1H_COR_ERR		BIT(28)
109 # define DSI_RXPKT1H_INCOMP_PKT		BIT(25)
110 # define DSI_RXPKT1H_PKT_TYPE_LONG	BIT(24)
111 /* Byte count if DSI_RXPKT1H_PKT_TYPE_LONG */
112 # define DSI_RXPKT1H_BC_PARAM_MASK	VC4_MASK(23, 8)
113 # define DSI_RXPKT1H_BC_PARAM_SHIFT	8
114 /* Short return bytes if !DSI_RXPKT1H_PKT_TYPE_LONG */
115 # define DSI_RXPKT1H_SHORT_1_MASK	VC4_MASK(23, 16)
116 # define DSI_RXPKT1H_SHORT_1_SHIFT	16
117 # define DSI_RXPKT1H_SHORT_0_MASK	VC4_MASK(15, 8)
118 # define DSI_RXPKT1H_SHORT_0_SHIFT	8
119 # define DSI_RXPKT1H_DT_LP_CMD_MASK	VC4_MASK(7, 0)
120 # define DSI_RXPKT1H_DT_LP_CMD_SHIFT	0
121 
122 #define DSI0_RXPKT2H		0x10 /* AKA RX2_PKTH */
123 #define DSI1_RXPKT2H		0x18
124 # define DSI_RXPKT1H_DET_ERR		BIT(30)
125 # define DSI_RXPKT1H_ECC_ERR		BIT(29)
126 # define DSI_RXPKT1H_COR_ERR		BIT(28)
127 # define DSI_RXPKT1H_INCOMP_PKT		BIT(25)
128 # define DSI_RXPKT1H_BC_PARAM_MASK	VC4_MASK(23, 8)
129 # define DSI_RXPKT1H_BC_PARAM_SHIFT	8
130 # define DSI_RXPKT1H_DT_MASK		VC4_MASK(7, 0)
131 # define DSI_RXPKT1H_DT_SHIFT		0
132 
133 #define DSI0_TXPKT_CMD_FIFO	0x14 /* AKA CMD_DATAF */
134 #define DSI1_TXPKT_CMD_FIFO	0x1c
135 
136 #define DSI0_DISP0_CTRL		0x18
137 # define DSI_DISP0_PIX_CLK_DIV_MASK	VC4_MASK(21, 13)
138 # define DSI_DISP0_PIX_CLK_DIV_SHIFT	13
139 # define DSI_DISP0_LP_STOP_CTRL_MASK	VC4_MASK(12, 11)
140 # define DSI_DISP0_LP_STOP_CTRL_SHIFT	11
141 # define DSI_DISP0_LP_STOP_DISABLE	0
142 # define DSI_DISP0_LP_STOP_PERLINE	1
143 # define DSI_DISP0_LP_STOP_PERFRAME	2
144 
145 /* Transmit RGB pixels and null packets only during HACTIVE, instead
146  * of going to LP-STOP.
147  */
148 # define DSI_DISP_HACTIVE_NULL		BIT(10)
149 /* Transmit blanking packet only during vblank, instead of allowing LP-STOP. */
150 # define DSI_DISP_VBLP_CTRL		BIT(9)
151 /* Transmit blanking packet only during HFP, instead of allowing LP-STOP. */
152 # define DSI_DISP_HFP_CTRL		BIT(8)
153 /* Transmit blanking packet only during HBP, instead of allowing LP-STOP. */
154 # define DSI_DISP_HBP_CTRL		BIT(7)
155 # define DSI_DISP0_CHANNEL_MASK		VC4_MASK(6, 5)
156 # define DSI_DISP0_CHANNEL_SHIFT	5
157 /* Enables end events for HSYNC/VSYNC, not just start events. */
158 # define DSI_DISP0_ST_END		BIT(4)
159 # define DSI_DISP0_PFORMAT_MASK		VC4_MASK(3, 2)
160 # define DSI_DISP0_PFORMAT_SHIFT	2
161 # define DSI_PFORMAT_RGB565		0
162 # define DSI_PFORMAT_RGB666_PACKED	1
163 # define DSI_PFORMAT_RGB666		2
164 # define DSI_PFORMAT_RGB888		3
165 /* Default is VIDEO mode. */
166 # define DSI_DISP0_COMMAND_MODE		BIT(1)
167 # define DSI_DISP0_ENABLE		BIT(0)
168 
169 #define DSI0_DISP1_CTRL		0x1c
170 #define DSI1_DISP1_CTRL		0x2c
171 /* Format of the data written to TXPKT_PIX_FIFO. */
172 # define DSI_DISP1_PFORMAT_MASK		VC4_MASK(2, 1)
173 # define DSI_DISP1_PFORMAT_SHIFT	1
174 # define DSI_DISP1_PFORMAT_16BIT	0
175 # define DSI_DISP1_PFORMAT_24BIT	1
176 # define DSI_DISP1_PFORMAT_32BIT_LE	2
177 # define DSI_DISP1_PFORMAT_32BIT_BE	3
178 
179 /* DISP1 is always command mode. */
180 # define DSI_DISP1_ENABLE		BIT(0)
181 
182 #define DSI0_TXPKT_PIX_FIFO		0x20 /* AKA PIX_FIFO */
183 
184 #define DSI0_INT_STAT			0x24
185 #define DSI0_INT_EN			0x28
186 # define DSI0_INT_FIFO_ERR		BIT(25)
187 # define DSI0_INT_CMDC_DONE_MASK	VC4_MASK(24, 23)
188 # define DSI0_INT_CMDC_DONE_SHIFT	23
189 #  define DSI0_INT_CMDC_DONE_NO_REPEAT		1
190 #  define DSI0_INT_CMDC_DONE_REPEAT		3
191 # define DSI0_INT_PHY_DIR_RTF		BIT(22)
192 # define DSI0_INT_PHY_D1_ULPS		BIT(21)
193 # define DSI0_INT_PHY_D1_STOP		BIT(20)
194 # define DSI0_INT_PHY_RXLPDT		BIT(19)
195 # define DSI0_INT_PHY_RXTRIG		BIT(18)
196 # define DSI0_INT_PHY_D0_ULPS		BIT(17)
197 # define DSI0_INT_PHY_D0_LPDT		BIT(16)
198 # define DSI0_INT_PHY_D0_FTR		BIT(15)
199 # define DSI0_INT_PHY_D0_STOP		BIT(14)
200 /* Signaled when the clock lane enters the given state. */
201 # define DSI0_INT_PHY_CLK_ULPS		BIT(13)
202 # define DSI0_INT_PHY_CLK_HS		BIT(12)
203 # define DSI0_INT_PHY_CLK_FTR		BIT(11)
204 /* Signaled on timeouts */
205 # define DSI0_INT_PR_TO			BIT(10)
206 # define DSI0_INT_TA_TO			BIT(9)
207 # define DSI0_INT_LPRX_TO		BIT(8)
208 # define DSI0_INT_HSTX_TO		BIT(7)
209 /* Contention on a line when trying to drive the line low */
210 # define DSI0_INT_ERR_CONT_LP1		BIT(6)
211 # define DSI0_INT_ERR_CONT_LP0		BIT(5)
212 /* Control error: incorrect line state sequence on data lane 0. */
213 # define DSI0_INT_ERR_CONTROL		BIT(4)
214 # define DSI0_INT_ERR_SYNC_ESC		BIT(3)
215 # define DSI0_INT_RX2_PKT		BIT(2)
216 # define DSI0_INT_RX1_PKT		BIT(1)
217 # define DSI0_INT_CMD_PKT		BIT(0)
218 
219 #define DSI0_INTERRUPTS_ALWAYS_ENABLED	(DSI0_INT_ERR_SYNC_ESC | \
220 					 DSI0_INT_ERR_CONTROL |	 \
221 					 DSI0_INT_ERR_CONT_LP0 | \
222 					 DSI0_INT_ERR_CONT_LP1 | \
223 					 DSI0_INT_HSTX_TO |	 \
224 					 DSI0_INT_LPRX_TO |	 \
225 					 DSI0_INT_TA_TO |	 \
226 					 DSI0_INT_PR_TO)
227 
228 # define DSI1_INT_PHY_D3_ULPS		BIT(30)
229 # define DSI1_INT_PHY_D3_STOP		BIT(29)
230 # define DSI1_INT_PHY_D2_ULPS		BIT(28)
231 # define DSI1_INT_PHY_D2_STOP		BIT(27)
232 # define DSI1_INT_PHY_D1_ULPS		BIT(26)
233 # define DSI1_INT_PHY_D1_STOP		BIT(25)
234 # define DSI1_INT_PHY_D0_ULPS		BIT(24)
235 # define DSI1_INT_PHY_D0_STOP		BIT(23)
236 # define DSI1_INT_FIFO_ERR		BIT(22)
237 # define DSI1_INT_PHY_DIR_RTF		BIT(21)
238 # define DSI1_INT_PHY_RXLPDT		BIT(20)
239 # define DSI1_INT_PHY_RXTRIG		BIT(19)
240 # define DSI1_INT_PHY_D0_LPDT		BIT(18)
241 # define DSI1_INT_PHY_DIR_FTR		BIT(17)
242 
243 /* Signaled when the clock lane enters the given state. */
244 # define DSI1_INT_PHY_CLOCK_ULPS	BIT(16)
245 # define DSI1_INT_PHY_CLOCK_HS		BIT(15)
246 # define DSI1_INT_PHY_CLOCK_STOP	BIT(14)
247 
248 /* Signaled on timeouts */
249 # define DSI1_INT_PR_TO			BIT(13)
250 # define DSI1_INT_TA_TO			BIT(12)
251 # define DSI1_INT_LPRX_TO		BIT(11)
252 # define DSI1_INT_HSTX_TO		BIT(10)
253 
254 /* Contention on a line when trying to drive the line low */
255 # define DSI1_INT_ERR_CONT_LP1		BIT(9)
256 # define DSI1_INT_ERR_CONT_LP0		BIT(8)
257 
258 /* Control error: incorrect line state sequence on data lane 0. */
259 # define DSI1_INT_ERR_CONTROL		BIT(7)
260 /* LPDT synchronization error (bits received not a multiple of 8. */
261 
262 # define DSI1_INT_ERR_SYNC_ESC		BIT(6)
263 /* Signaled after receiving an error packet from the display in
264  * response to a read.
265  */
266 # define DSI1_INT_RXPKT2		BIT(5)
267 /* Signaled after receiving a packet.  The header and optional short
268  * response will be in RXPKT1H, and a long response will be in the
269  * RXPKT_FIFO.
270  */
271 # define DSI1_INT_RXPKT1		BIT(4)
272 # define DSI1_INT_TXPKT2_DONE		BIT(3)
273 # define DSI1_INT_TXPKT2_END		BIT(2)
274 /* Signaled after all repeats of TXPKT1 are transferred. */
275 # define DSI1_INT_TXPKT1_DONE		BIT(1)
276 /* Signaled after each TXPKT1 repeat is scheduled. */
277 # define DSI1_INT_TXPKT1_END		BIT(0)
278 
279 #define DSI1_INTERRUPTS_ALWAYS_ENABLED	(DSI1_INT_ERR_SYNC_ESC | \
280 					 DSI1_INT_ERR_CONTROL |	 \
281 					 DSI1_INT_ERR_CONT_LP0 | \
282 					 DSI1_INT_ERR_CONT_LP1 | \
283 					 DSI1_INT_HSTX_TO |	 \
284 					 DSI1_INT_LPRX_TO |	 \
285 					 DSI1_INT_TA_TO |	 \
286 					 DSI1_INT_PR_TO)
287 
288 #define DSI0_STAT		0x2c
289 #define DSI0_HSTX_TO_CNT	0x30
290 #define DSI0_LPRX_TO_CNT	0x34
291 #define DSI0_TA_TO_CNT		0x38
292 #define DSI0_PR_TO_CNT		0x3c
293 #define DSI0_PHYC		0x40
294 # define DSI1_PHYC_ESC_CLK_LPDT_MASK	VC4_MASK(25, 20)
295 # define DSI1_PHYC_ESC_CLK_LPDT_SHIFT	20
296 # define DSI1_PHYC_HS_CLK_CONTINUOUS	BIT(18)
297 # define DSI0_PHYC_ESC_CLK_LPDT_MASK	VC4_MASK(17, 12)
298 # define DSI0_PHYC_ESC_CLK_LPDT_SHIFT	12
299 # define DSI1_PHYC_CLANE_ULPS		BIT(17)
300 # define DSI1_PHYC_CLANE_ENABLE		BIT(16)
301 # define DSI_PHYC_DLANE3_ULPS		BIT(13)
302 # define DSI_PHYC_DLANE3_ENABLE		BIT(12)
303 # define DSI0_PHYC_HS_CLK_CONTINUOUS	BIT(10)
304 # define DSI0_PHYC_CLANE_ULPS		BIT(9)
305 # define DSI_PHYC_DLANE2_ULPS		BIT(9)
306 # define DSI0_PHYC_CLANE_ENABLE		BIT(8)
307 # define DSI_PHYC_DLANE2_ENABLE		BIT(8)
308 # define DSI_PHYC_DLANE1_ULPS		BIT(5)
309 # define DSI_PHYC_DLANE1_ENABLE		BIT(4)
310 # define DSI_PHYC_DLANE0_FORCE_STOP	BIT(2)
311 # define DSI_PHYC_DLANE0_ULPS		BIT(1)
312 # define DSI_PHYC_DLANE0_ENABLE		BIT(0)
313 
314 #define DSI0_HS_CLT0		0x44
315 #define DSI0_HS_CLT1		0x48
316 #define DSI0_HS_CLT2		0x4c
317 #define DSI0_HS_DLT3		0x50
318 #define DSI0_HS_DLT4		0x54
319 #define DSI0_HS_DLT5		0x58
320 #define DSI0_HS_DLT6		0x5c
321 #define DSI0_HS_DLT7		0x60
322 
323 #define DSI0_PHY_AFEC0		0x64
324 # define DSI0_PHY_AFEC0_DDR2CLK_EN		BIT(26)
325 # define DSI0_PHY_AFEC0_DDRCLK_EN		BIT(25)
326 # define DSI0_PHY_AFEC0_LATCH_ULPS		BIT(24)
327 # define DSI1_PHY_AFEC0_IDR_DLANE3_MASK		VC4_MASK(31, 29)
328 # define DSI1_PHY_AFEC0_IDR_DLANE3_SHIFT	29
329 # define DSI1_PHY_AFEC0_IDR_DLANE2_MASK		VC4_MASK(28, 26)
330 # define DSI1_PHY_AFEC0_IDR_DLANE2_SHIFT	26
331 # define DSI1_PHY_AFEC0_IDR_DLANE1_MASK		VC4_MASK(27, 23)
332 # define DSI1_PHY_AFEC0_IDR_DLANE1_SHIFT	23
333 # define DSI1_PHY_AFEC0_IDR_DLANE0_MASK		VC4_MASK(22, 20)
334 # define DSI1_PHY_AFEC0_IDR_DLANE0_SHIFT	20
335 # define DSI1_PHY_AFEC0_IDR_CLANE_MASK		VC4_MASK(19, 17)
336 # define DSI1_PHY_AFEC0_IDR_CLANE_SHIFT		17
337 # define DSI0_PHY_AFEC0_ACTRL_DLANE1_MASK	VC4_MASK(23, 20)
338 # define DSI0_PHY_AFEC0_ACTRL_DLANE1_SHIFT	20
339 # define DSI0_PHY_AFEC0_ACTRL_DLANE0_MASK	VC4_MASK(19, 16)
340 # define DSI0_PHY_AFEC0_ACTRL_DLANE0_SHIFT	16
341 # define DSI0_PHY_AFEC0_ACTRL_CLANE_MASK	VC4_MASK(15, 12)
342 # define DSI0_PHY_AFEC0_ACTRL_CLANE_SHIFT	12
343 # define DSI1_PHY_AFEC0_DDR2CLK_EN		BIT(16)
344 # define DSI1_PHY_AFEC0_DDRCLK_EN		BIT(15)
345 # define DSI1_PHY_AFEC0_LATCH_ULPS		BIT(14)
346 # define DSI1_PHY_AFEC0_RESET			BIT(13)
347 # define DSI1_PHY_AFEC0_PD			BIT(12)
348 # define DSI0_PHY_AFEC0_RESET			BIT(11)
349 # define DSI1_PHY_AFEC0_PD_BG			BIT(11)
350 # define DSI0_PHY_AFEC0_PD			BIT(10)
351 # define DSI1_PHY_AFEC0_PD_DLANE1		BIT(10)
352 # define DSI0_PHY_AFEC0_PD_BG			BIT(9)
353 # define DSI1_PHY_AFEC0_PD_DLANE2		BIT(9)
354 # define DSI0_PHY_AFEC0_PD_DLANE1		BIT(8)
355 # define DSI1_PHY_AFEC0_PD_DLANE3		BIT(8)
356 # define DSI_PHY_AFEC0_PTATADJ_MASK		VC4_MASK(7, 4)
357 # define DSI_PHY_AFEC0_PTATADJ_SHIFT		4
358 # define DSI_PHY_AFEC0_CTATADJ_MASK		VC4_MASK(3, 0)
359 # define DSI_PHY_AFEC0_CTATADJ_SHIFT		0
360 
361 #define DSI0_PHY_AFEC1		0x68
362 # define DSI0_PHY_AFEC1_IDR_DLANE1_MASK		VC4_MASK(10, 8)
363 # define DSI0_PHY_AFEC1_IDR_DLANE1_SHIFT	8
364 # define DSI0_PHY_AFEC1_IDR_DLANE0_MASK		VC4_MASK(6, 4)
365 # define DSI0_PHY_AFEC1_IDR_DLANE0_SHIFT	4
366 # define DSI0_PHY_AFEC1_IDR_CLANE_MASK		VC4_MASK(2, 0)
367 # define DSI0_PHY_AFEC1_IDR_CLANE_SHIFT		0
368 
369 #define DSI0_TST_SEL		0x6c
370 #define DSI0_TST_MON		0x70
371 #define DSI0_ID			0x74
372 # define DSI_ID_VALUE		0x00647369
373 
374 #define DSI1_CTRL		0x00
375 # define DSI_CTRL_HS_CLKC_MASK		VC4_MASK(15, 14)
376 # define DSI_CTRL_HS_CLKC_SHIFT		14
377 # define DSI_CTRL_HS_CLKC_BYTE		0
378 # define DSI_CTRL_HS_CLKC_DDR2		1
379 # define DSI_CTRL_HS_CLKC_DDR		2
380 
381 # define DSI_CTRL_RX_LPDT_EOT_DISABLE	BIT(13)
382 # define DSI_CTRL_LPDT_EOT_DISABLE	BIT(12)
383 # define DSI_CTRL_HSDT_EOT_DISABLE	BIT(11)
384 # define DSI_CTRL_SOFT_RESET_CFG	BIT(10)
385 # define DSI_CTRL_CAL_BYTE		BIT(9)
386 # define DSI_CTRL_INV_BYTE		BIT(8)
387 # define DSI_CTRL_CLR_LDF		BIT(7)
388 # define DSI0_CTRL_CLR_PBCF		BIT(6)
389 # define DSI1_CTRL_CLR_RXF		BIT(6)
390 # define DSI0_CTRL_CLR_CPBCF		BIT(5)
391 # define DSI1_CTRL_CLR_PDF		BIT(5)
392 # define DSI0_CTRL_CLR_PDF		BIT(4)
393 # define DSI1_CTRL_CLR_CDF		BIT(4)
394 # define DSI0_CTRL_CLR_CDF		BIT(3)
395 # define DSI0_CTRL_CTRL2		BIT(2)
396 # define DSI1_CTRL_DISABLE_DISP_CRCC	BIT(2)
397 # define DSI0_CTRL_CTRL1		BIT(1)
398 # define DSI1_CTRL_DISABLE_DISP_ECCC	BIT(1)
399 # define DSI0_CTRL_CTRL0		BIT(0)
400 # define DSI1_CTRL_EN			BIT(0)
401 # define DSI0_CTRL_RESET_FIFOS		(DSI_CTRL_CLR_LDF | \
402 					 DSI0_CTRL_CLR_PBCF | \
403 					 DSI0_CTRL_CLR_CPBCF |	\
404 					 DSI0_CTRL_CLR_PDF | \
405 					 DSI0_CTRL_CLR_CDF)
406 # define DSI1_CTRL_RESET_FIFOS		(DSI_CTRL_CLR_LDF | \
407 					 DSI1_CTRL_CLR_RXF | \
408 					 DSI1_CTRL_CLR_PDF | \
409 					 DSI1_CTRL_CLR_CDF)
410 
411 #define DSI1_TXPKT2C		0x0c
412 #define DSI1_TXPKT2H		0x10
413 #define DSI1_TXPKT_PIX_FIFO	0x20
414 #define DSI1_RXPKT_FIFO		0x24
415 #define DSI1_DISP0_CTRL		0x28
416 #define DSI1_INT_STAT		0x30
417 #define DSI1_INT_EN		0x34
418 /* State reporting bits.  These mostly behave like INT_STAT, where
419  * writing a 1 clears the bit.
420  */
421 #define DSI1_STAT		0x38
422 # define DSI1_STAT_PHY_D3_ULPS		BIT(31)
423 # define DSI1_STAT_PHY_D3_STOP		BIT(30)
424 # define DSI1_STAT_PHY_D2_ULPS		BIT(29)
425 # define DSI1_STAT_PHY_D2_STOP		BIT(28)
426 # define DSI1_STAT_PHY_D1_ULPS		BIT(27)
427 # define DSI1_STAT_PHY_D1_STOP		BIT(26)
428 # define DSI1_STAT_PHY_D0_ULPS		BIT(25)
429 # define DSI1_STAT_PHY_D0_STOP		BIT(24)
430 # define DSI1_STAT_FIFO_ERR		BIT(23)
431 # define DSI1_STAT_PHY_RXLPDT		BIT(22)
432 # define DSI1_STAT_PHY_RXTRIG		BIT(21)
433 # define DSI1_STAT_PHY_D0_LPDT		BIT(20)
434 /* Set when in forward direction */
435 # define DSI1_STAT_PHY_DIR		BIT(19)
436 # define DSI1_STAT_PHY_CLOCK_ULPS	BIT(18)
437 # define DSI1_STAT_PHY_CLOCK_HS		BIT(17)
438 # define DSI1_STAT_PHY_CLOCK_STOP	BIT(16)
439 # define DSI1_STAT_PR_TO		BIT(15)
440 # define DSI1_STAT_TA_TO		BIT(14)
441 # define DSI1_STAT_LPRX_TO		BIT(13)
442 # define DSI1_STAT_HSTX_TO		BIT(12)
443 # define DSI1_STAT_ERR_CONT_LP1		BIT(11)
444 # define DSI1_STAT_ERR_CONT_LP0		BIT(10)
445 # define DSI1_STAT_ERR_CONTROL		BIT(9)
446 # define DSI1_STAT_ERR_SYNC_ESC		BIT(8)
447 # define DSI1_STAT_RXPKT2		BIT(7)
448 # define DSI1_STAT_RXPKT1		BIT(6)
449 # define DSI1_STAT_TXPKT2_BUSY		BIT(5)
450 # define DSI1_STAT_TXPKT2_DONE		BIT(4)
451 # define DSI1_STAT_TXPKT2_END		BIT(3)
452 # define DSI1_STAT_TXPKT1_BUSY		BIT(2)
453 # define DSI1_STAT_TXPKT1_DONE		BIT(1)
454 # define DSI1_STAT_TXPKT1_END		BIT(0)
455 
456 #define DSI1_HSTX_TO_CNT	0x3c
457 #define DSI1_LPRX_TO_CNT	0x40
458 #define DSI1_TA_TO_CNT		0x44
459 #define DSI1_PR_TO_CNT		0x48
460 #define DSI1_PHYC		0x4c
461 
462 #define DSI1_HS_CLT0		0x50
463 # define DSI_HS_CLT0_CZERO_MASK		VC4_MASK(26, 18)
464 # define DSI_HS_CLT0_CZERO_SHIFT	18
465 # define DSI_HS_CLT0_CPRE_MASK		VC4_MASK(17, 9)
466 # define DSI_HS_CLT0_CPRE_SHIFT		9
467 # define DSI_HS_CLT0_CPREP_MASK		VC4_MASK(8, 0)
468 # define DSI_HS_CLT0_CPREP_SHIFT	0
469 
470 #define DSI1_HS_CLT1		0x54
471 # define DSI_HS_CLT1_CTRAIL_MASK	VC4_MASK(17, 9)
472 # define DSI_HS_CLT1_CTRAIL_SHIFT	9
473 # define DSI_HS_CLT1_CPOST_MASK		VC4_MASK(8, 0)
474 # define DSI_HS_CLT1_CPOST_SHIFT	0
475 
476 #define DSI1_HS_CLT2		0x58
477 # define DSI_HS_CLT2_WUP_MASK		VC4_MASK(23, 0)
478 # define DSI_HS_CLT2_WUP_SHIFT		0
479 
480 #define DSI1_HS_DLT3		0x5c
481 # define DSI_HS_DLT3_EXIT_MASK		VC4_MASK(26, 18)
482 # define DSI_HS_DLT3_EXIT_SHIFT		18
483 # define DSI_HS_DLT3_ZERO_MASK		VC4_MASK(17, 9)
484 # define DSI_HS_DLT3_ZERO_SHIFT		9
485 # define DSI_HS_DLT3_PRE_MASK		VC4_MASK(8, 0)
486 # define DSI_HS_DLT3_PRE_SHIFT		0
487 
488 #define DSI1_HS_DLT4		0x60
489 # define DSI_HS_DLT4_ANLAT_MASK		VC4_MASK(22, 18)
490 # define DSI_HS_DLT4_ANLAT_SHIFT	18
491 # define DSI_HS_DLT4_TRAIL_MASK		VC4_MASK(17, 9)
492 # define DSI_HS_DLT4_TRAIL_SHIFT	9
493 # define DSI_HS_DLT4_LPX_MASK		VC4_MASK(8, 0)
494 # define DSI_HS_DLT4_LPX_SHIFT		0
495 
496 #define DSI1_HS_DLT5		0x64
497 # define DSI_HS_DLT5_INIT_MASK		VC4_MASK(23, 0)
498 # define DSI_HS_DLT5_INIT_SHIFT		0
499 
500 #define DSI1_HS_DLT6		0x68
501 # define DSI_HS_DLT6_TA_GET_MASK	VC4_MASK(31, 24)
502 # define DSI_HS_DLT6_TA_GET_SHIFT	24
503 # define DSI_HS_DLT6_TA_SURE_MASK	VC4_MASK(23, 16)
504 # define DSI_HS_DLT6_TA_SURE_SHIFT	16
505 # define DSI_HS_DLT6_TA_GO_MASK		VC4_MASK(15, 8)
506 # define DSI_HS_DLT6_TA_GO_SHIFT	8
507 # define DSI_HS_DLT6_LP_LPX_MASK	VC4_MASK(7, 0)
508 # define DSI_HS_DLT6_LP_LPX_SHIFT	0
509 
510 #define DSI1_HS_DLT7		0x6c
511 # define DSI_HS_DLT7_LP_WUP_MASK	VC4_MASK(23, 0)
512 # define DSI_HS_DLT7_LP_WUP_SHIFT	0
513 
514 #define DSI1_PHY_AFEC0		0x70
515 
516 #define DSI1_PHY_AFEC1		0x74
517 # define DSI1_PHY_AFEC1_ACTRL_DLANE3_MASK	VC4_MASK(19, 16)
518 # define DSI1_PHY_AFEC1_ACTRL_DLANE3_SHIFT	16
519 # define DSI1_PHY_AFEC1_ACTRL_DLANE2_MASK	VC4_MASK(15, 12)
520 # define DSI1_PHY_AFEC1_ACTRL_DLANE2_SHIFT	12
521 # define DSI1_PHY_AFEC1_ACTRL_DLANE1_MASK	VC4_MASK(11, 8)
522 # define DSI1_PHY_AFEC1_ACTRL_DLANE1_SHIFT	8
523 # define DSI1_PHY_AFEC1_ACTRL_DLANE0_MASK	VC4_MASK(7, 4)
524 # define DSI1_PHY_AFEC1_ACTRL_DLANE0_SHIFT	4
525 # define DSI1_PHY_AFEC1_ACTRL_CLANE_MASK	VC4_MASK(3, 0)
526 # define DSI1_PHY_AFEC1_ACTRL_CLANE_SHIFT	0
527 
528 #define DSI1_TST_SEL		0x78
529 #define DSI1_TST_MON		0x7c
530 #define DSI1_PHY_TST1		0x80
531 #define DSI1_PHY_TST2		0x84
532 #define DSI1_PHY_FIFO_STAT	0x88
533 /* Actually, all registers in the range that aren't otherwise claimed
534  * will return the ID.
535  */
536 #define DSI1_ID			0x8c
537 
538 struct vc4_dsi_variant {
539 	/* Whether we're on bcm2835's DSI0 or DSI1. */
540 	unsigned int port;
541 
542 	bool broken_axi_workaround;
543 
544 	const char *debugfs_name;
545 	const struct debugfs_reg32 *regs;
546 	size_t nregs;
547 
548 };
549 
550 /* General DSI hardware state. */
551 struct vc4_dsi {
552 	struct platform_device *pdev;
553 
554 	struct mipi_dsi_host dsi_host;
555 	struct drm_encoder *encoder;
556 	struct drm_bridge *bridge;
557 	struct list_head bridge_chain;
558 
559 	void __iomem *regs;
560 
561 	struct dma_chan *reg_dma_chan;
562 	dma_addr_t reg_dma_paddr;
563 	u32 *reg_dma_mem;
564 	dma_addr_t reg_paddr;
565 
566 	const struct vc4_dsi_variant *variant;
567 
568 	/* DSI channel for the panel we're connected to. */
569 	u32 channel;
570 	u32 lanes;
571 	u32 format;
572 	u32 divider;
573 	u32 mode_flags;
574 
575 	/* Input clock from CPRMAN to the digital PHY, for the DSI
576 	 * escape clock.
577 	 */
578 	struct clk *escape_clock;
579 
580 	/* Input clock to the analog PHY, used to generate the DSI bit
581 	 * clock.
582 	 */
583 	struct clk *pll_phy_clock;
584 
585 	/* HS Clocks generated within the DSI analog PHY. */
586 	struct clk_fixed_factor phy_clocks[3];
587 
588 	struct clk_hw_onecell_data *clk_onecell;
589 
590 	/* Pixel clock output to the pixelvalve, generated from the HS
591 	 * clock.
592 	 */
593 	struct clk *pixel_clock;
594 
595 	struct completion xfer_completion;
596 	int xfer_result;
597 
598 	struct debugfs_regset32 regset;
599 };
600 
601 #define host_to_dsi(host) container_of(host, struct vc4_dsi, dsi_host)
602 
603 static inline void
604 dsi_dma_workaround_write(struct vc4_dsi *dsi, u32 offset, u32 val)
605 {
606 	struct dma_chan *chan = dsi->reg_dma_chan;
607 	struct dma_async_tx_descriptor *tx;
608 	dma_cookie_t cookie;
609 	int ret;
610 
611 	/* DSI0 should be able to write normally. */
612 	if (!chan) {
613 		writel(val, dsi->regs + offset);
614 		return;
615 	}
616 
617 	*dsi->reg_dma_mem = val;
618 
619 	tx = chan->device->device_prep_dma_memcpy(chan,
620 						  dsi->reg_paddr + offset,
621 						  dsi->reg_dma_paddr,
622 						  4, 0);
623 	if (!tx) {
624 		DRM_ERROR("Failed to set up DMA register write\n");
625 		return;
626 	}
627 
628 	cookie = tx->tx_submit(tx);
629 	ret = dma_submit_error(cookie);
630 	if (ret) {
631 		DRM_ERROR("Failed to submit DMA: %d\n", ret);
632 		return;
633 	}
634 	ret = dma_sync_wait(chan, cookie);
635 	if (ret)
636 		DRM_ERROR("Failed to wait for DMA: %d\n", ret);
637 }
638 
639 #define DSI_READ(offset) readl(dsi->regs + (offset))
640 #define DSI_WRITE(offset, val) dsi_dma_workaround_write(dsi, offset, val)
641 #define DSI_PORT_READ(offset) \
642 	DSI_READ(dsi->variant->port ? DSI1_##offset : DSI0_##offset)
643 #define DSI_PORT_WRITE(offset, val) \
644 	DSI_WRITE(dsi->variant->port ? DSI1_##offset : DSI0_##offset, val)
645 #define DSI_PORT_BIT(bit) (dsi->variant->port ? DSI1_##bit : DSI0_##bit)
646 
647 /* VC4 DSI encoder KMS struct */
648 struct vc4_dsi_encoder {
649 	struct vc4_encoder base;
650 	struct vc4_dsi *dsi;
651 };
652 
653 static inline struct vc4_dsi_encoder *
654 to_vc4_dsi_encoder(struct drm_encoder *encoder)
655 {
656 	return container_of(encoder, struct vc4_dsi_encoder, base.base);
657 }
658 
659 static const struct debugfs_reg32 dsi0_regs[] = {
660 	VC4_REG32(DSI0_CTRL),
661 	VC4_REG32(DSI0_STAT),
662 	VC4_REG32(DSI0_HSTX_TO_CNT),
663 	VC4_REG32(DSI0_LPRX_TO_CNT),
664 	VC4_REG32(DSI0_TA_TO_CNT),
665 	VC4_REG32(DSI0_PR_TO_CNT),
666 	VC4_REG32(DSI0_DISP0_CTRL),
667 	VC4_REG32(DSI0_DISP1_CTRL),
668 	VC4_REG32(DSI0_INT_STAT),
669 	VC4_REG32(DSI0_INT_EN),
670 	VC4_REG32(DSI0_PHYC),
671 	VC4_REG32(DSI0_HS_CLT0),
672 	VC4_REG32(DSI0_HS_CLT1),
673 	VC4_REG32(DSI0_HS_CLT2),
674 	VC4_REG32(DSI0_HS_DLT3),
675 	VC4_REG32(DSI0_HS_DLT4),
676 	VC4_REG32(DSI0_HS_DLT5),
677 	VC4_REG32(DSI0_HS_DLT6),
678 	VC4_REG32(DSI0_HS_DLT7),
679 	VC4_REG32(DSI0_PHY_AFEC0),
680 	VC4_REG32(DSI0_PHY_AFEC1),
681 	VC4_REG32(DSI0_ID),
682 };
683 
684 static const struct debugfs_reg32 dsi1_regs[] = {
685 	VC4_REG32(DSI1_CTRL),
686 	VC4_REG32(DSI1_STAT),
687 	VC4_REG32(DSI1_HSTX_TO_CNT),
688 	VC4_REG32(DSI1_LPRX_TO_CNT),
689 	VC4_REG32(DSI1_TA_TO_CNT),
690 	VC4_REG32(DSI1_PR_TO_CNT),
691 	VC4_REG32(DSI1_DISP0_CTRL),
692 	VC4_REG32(DSI1_DISP1_CTRL),
693 	VC4_REG32(DSI1_INT_STAT),
694 	VC4_REG32(DSI1_INT_EN),
695 	VC4_REG32(DSI1_PHYC),
696 	VC4_REG32(DSI1_HS_CLT0),
697 	VC4_REG32(DSI1_HS_CLT1),
698 	VC4_REG32(DSI1_HS_CLT2),
699 	VC4_REG32(DSI1_HS_DLT3),
700 	VC4_REG32(DSI1_HS_DLT4),
701 	VC4_REG32(DSI1_HS_DLT5),
702 	VC4_REG32(DSI1_HS_DLT6),
703 	VC4_REG32(DSI1_HS_DLT7),
704 	VC4_REG32(DSI1_PHY_AFEC0),
705 	VC4_REG32(DSI1_PHY_AFEC1),
706 	VC4_REG32(DSI1_ID),
707 };
708 
709 static void vc4_dsi_latch_ulps(struct vc4_dsi *dsi, bool latch)
710 {
711 	u32 afec0 = DSI_PORT_READ(PHY_AFEC0);
712 
713 	if (latch)
714 		afec0 |= DSI_PORT_BIT(PHY_AFEC0_LATCH_ULPS);
715 	else
716 		afec0 &= ~DSI_PORT_BIT(PHY_AFEC0_LATCH_ULPS);
717 
718 	DSI_PORT_WRITE(PHY_AFEC0, afec0);
719 }
720 
721 /* Enters or exits Ultra Low Power State. */
722 static void vc4_dsi_ulps(struct vc4_dsi *dsi, bool ulps)
723 {
724 	bool non_continuous = dsi->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS;
725 	u32 phyc_ulps = ((non_continuous ? DSI_PORT_BIT(PHYC_CLANE_ULPS) : 0) |
726 			 DSI_PHYC_DLANE0_ULPS |
727 			 (dsi->lanes > 1 ? DSI_PHYC_DLANE1_ULPS : 0) |
728 			 (dsi->lanes > 2 ? DSI_PHYC_DLANE2_ULPS : 0) |
729 			 (dsi->lanes > 3 ? DSI_PHYC_DLANE3_ULPS : 0));
730 	u32 stat_ulps = ((non_continuous ? DSI1_STAT_PHY_CLOCK_ULPS : 0) |
731 			 DSI1_STAT_PHY_D0_ULPS |
732 			 (dsi->lanes > 1 ? DSI1_STAT_PHY_D1_ULPS : 0) |
733 			 (dsi->lanes > 2 ? DSI1_STAT_PHY_D2_ULPS : 0) |
734 			 (dsi->lanes > 3 ? DSI1_STAT_PHY_D3_ULPS : 0));
735 	u32 stat_stop = ((non_continuous ? DSI1_STAT_PHY_CLOCK_STOP : 0) |
736 			 DSI1_STAT_PHY_D0_STOP |
737 			 (dsi->lanes > 1 ? DSI1_STAT_PHY_D1_STOP : 0) |
738 			 (dsi->lanes > 2 ? DSI1_STAT_PHY_D2_STOP : 0) |
739 			 (dsi->lanes > 3 ? DSI1_STAT_PHY_D3_STOP : 0));
740 	int ret;
741 	bool ulps_currently_enabled = (DSI_PORT_READ(PHY_AFEC0) &
742 				       DSI_PORT_BIT(PHY_AFEC0_LATCH_ULPS));
743 
744 	if (ulps == ulps_currently_enabled)
745 		return;
746 
747 	DSI_PORT_WRITE(STAT, stat_ulps);
748 	DSI_PORT_WRITE(PHYC, DSI_PORT_READ(PHYC) | phyc_ulps);
749 	ret = wait_for((DSI_PORT_READ(STAT) & stat_ulps) == stat_ulps, 200);
750 	if (ret) {
751 		dev_warn(&dsi->pdev->dev,
752 			 "Timeout waiting for DSI ULPS entry: STAT 0x%08x",
753 			 DSI_PORT_READ(STAT));
754 		DSI_PORT_WRITE(PHYC, DSI_PORT_READ(PHYC) & ~phyc_ulps);
755 		vc4_dsi_latch_ulps(dsi, false);
756 		return;
757 	}
758 
759 	/* The DSI module can't be disabled while the module is
760 	 * generating ULPS state.  So, to be able to disable the
761 	 * module, we have the AFE latch the ULPS state and continue
762 	 * on to having the module enter STOP.
763 	 */
764 	vc4_dsi_latch_ulps(dsi, ulps);
765 
766 	DSI_PORT_WRITE(STAT, stat_stop);
767 	DSI_PORT_WRITE(PHYC, DSI_PORT_READ(PHYC) & ~phyc_ulps);
768 	ret = wait_for((DSI_PORT_READ(STAT) & stat_stop) == stat_stop, 200);
769 	if (ret) {
770 		dev_warn(&dsi->pdev->dev,
771 			 "Timeout waiting for DSI STOP entry: STAT 0x%08x",
772 			 DSI_PORT_READ(STAT));
773 		DSI_PORT_WRITE(PHYC, DSI_PORT_READ(PHYC) & ~phyc_ulps);
774 		return;
775 	}
776 }
777 
778 static u32
779 dsi_hs_timing(u32 ui_ns, u32 ns, u32 ui)
780 {
781 	/* The HS timings have to be rounded up to a multiple of 8
782 	 * because we're using the byte clock.
783 	 */
784 	return roundup(ui + DIV_ROUND_UP(ns, ui_ns), 8);
785 }
786 
787 /* ESC always runs at 100Mhz. */
788 #define ESC_TIME_NS 10
789 
790 static u32
791 dsi_esc_timing(u32 ns)
792 {
793 	return DIV_ROUND_UP(ns, ESC_TIME_NS);
794 }
795 
796 static void vc4_dsi_encoder_disable(struct drm_encoder *encoder)
797 {
798 	struct vc4_dsi_encoder *vc4_encoder = to_vc4_dsi_encoder(encoder);
799 	struct vc4_dsi *dsi = vc4_encoder->dsi;
800 	struct device *dev = &dsi->pdev->dev;
801 	struct drm_bridge *iter;
802 
803 	list_for_each_entry_reverse(iter, &dsi->bridge_chain, chain_node) {
804 		if (iter->funcs->disable)
805 			iter->funcs->disable(iter);
806 
807 		if (iter == dsi->bridge)
808 			break;
809 	}
810 
811 	vc4_dsi_ulps(dsi, true);
812 
813 	list_for_each_entry_from(iter, &dsi->bridge_chain, chain_node) {
814 		if (iter->funcs->post_disable)
815 			iter->funcs->post_disable(iter);
816 	}
817 
818 	clk_disable_unprepare(dsi->pll_phy_clock);
819 	clk_disable_unprepare(dsi->escape_clock);
820 	clk_disable_unprepare(dsi->pixel_clock);
821 
822 	pm_runtime_put(dev);
823 }
824 
825 /* Extends the mode's blank intervals to handle BCM2835's integer-only
826  * DSI PLL divider.
827  *
828  * On 2835, PLLD is set to 2Ghz, and may not be changed by the display
829  * driver since most peripherals are hanging off of the PLLD_PER
830  * divider.  PLLD_DSI1, which drives our DSI bit clock (and therefore
831  * the pixel clock), only has an integer divider off of DSI.
832  *
833  * To get our panel mode to refresh at the expected 60Hz, we need to
834  * extend the horizontal blank time.  This means we drive a
835  * higher-than-expected clock rate to the panel, but that's what the
836  * firmware does too.
837  */
838 static bool vc4_dsi_encoder_mode_fixup(struct drm_encoder *encoder,
839 				       const struct drm_display_mode *mode,
840 				       struct drm_display_mode *adjusted_mode)
841 {
842 	struct vc4_dsi_encoder *vc4_encoder = to_vc4_dsi_encoder(encoder);
843 	struct vc4_dsi *dsi = vc4_encoder->dsi;
844 	struct clk *phy_parent = clk_get_parent(dsi->pll_phy_clock);
845 	unsigned long parent_rate = clk_get_rate(phy_parent);
846 	unsigned long pixel_clock_hz = mode->clock * 1000;
847 	unsigned long pll_clock = pixel_clock_hz * dsi->divider;
848 	int divider;
849 
850 	/* Find what divider gets us a faster clock than the requested
851 	 * pixel clock.
852 	 */
853 	for (divider = 1; divider < 255; divider++) {
854 		if (parent_rate / (divider + 1) < pll_clock)
855 			break;
856 	}
857 
858 	/* Now that we've picked a PLL divider, calculate back to its
859 	 * pixel clock.
860 	 */
861 	pll_clock = parent_rate / divider;
862 	pixel_clock_hz = pll_clock / dsi->divider;
863 
864 	adjusted_mode->clock = pixel_clock_hz / 1000;
865 
866 	/* Given the new pixel clock, adjust HFP to keep vrefresh the same. */
867 	adjusted_mode->htotal = adjusted_mode->clock * mode->htotal /
868 				mode->clock;
869 	adjusted_mode->hsync_end += adjusted_mode->htotal - mode->htotal;
870 	adjusted_mode->hsync_start += adjusted_mode->htotal - mode->htotal;
871 
872 	return true;
873 }
874 
875 static void vc4_dsi_encoder_enable(struct drm_encoder *encoder)
876 {
877 	struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
878 	struct vc4_dsi_encoder *vc4_encoder = to_vc4_dsi_encoder(encoder);
879 	struct vc4_dsi *dsi = vc4_encoder->dsi;
880 	struct device *dev = &dsi->pdev->dev;
881 	bool debug_dump_regs = false;
882 	struct drm_bridge *iter;
883 	unsigned long hs_clock;
884 	u32 ui_ns;
885 	/* Minimum LP state duration in escape clock cycles. */
886 	u32 lpx = dsi_esc_timing(60);
887 	unsigned long pixel_clock_hz = mode->clock * 1000;
888 	unsigned long dsip_clock;
889 	unsigned long phy_clock;
890 	int ret;
891 
892 	ret = pm_runtime_resume_and_get(dev);
893 	if (ret) {
894 		DRM_ERROR("Failed to runtime PM enable on DSI%d\n", dsi->variant->port);
895 		return;
896 	}
897 
898 	if (debug_dump_regs) {
899 		struct drm_printer p = drm_info_printer(&dsi->pdev->dev);
900 		dev_info(&dsi->pdev->dev, "DSI regs before:\n");
901 		drm_print_regset32(&p, &dsi->regset);
902 	}
903 
904 	/* Round up the clk_set_rate() request slightly, since
905 	 * PLLD_DSI1 is an integer divider and its rate selection will
906 	 * never round up.
907 	 */
908 	phy_clock = (pixel_clock_hz + 1000) * dsi->divider;
909 	ret = clk_set_rate(dsi->pll_phy_clock, phy_clock);
910 	if (ret) {
911 		dev_err(&dsi->pdev->dev,
912 			"Failed to set phy clock to %ld: %d\n", phy_clock, ret);
913 	}
914 
915 	/* Reset the DSI and all its fifos. */
916 	DSI_PORT_WRITE(CTRL,
917 		       DSI_CTRL_SOFT_RESET_CFG |
918 		       DSI_PORT_BIT(CTRL_RESET_FIFOS));
919 
920 	DSI_PORT_WRITE(CTRL,
921 		       DSI_CTRL_HSDT_EOT_DISABLE |
922 		       DSI_CTRL_RX_LPDT_EOT_DISABLE);
923 
924 	/* Clear all stat bits so we see what has happened during enable. */
925 	DSI_PORT_WRITE(STAT, DSI_PORT_READ(STAT));
926 
927 	/* Set AFE CTR00/CTR1 to release powerdown of analog. */
928 	if (dsi->variant->port == 0) {
929 		u32 afec0 = (VC4_SET_FIELD(7, DSI_PHY_AFEC0_PTATADJ) |
930 			     VC4_SET_FIELD(7, DSI_PHY_AFEC0_CTATADJ));
931 
932 		if (dsi->lanes < 2)
933 			afec0 |= DSI0_PHY_AFEC0_PD_DLANE1;
934 
935 		if (!(dsi->mode_flags & MIPI_DSI_MODE_VIDEO))
936 			afec0 |= DSI0_PHY_AFEC0_RESET;
937 
938 		DSI_PORT_WRITE(PHY_AFEC0, afec0);
939 
940 		/* AFEC reset hold time */
941 		mdelay(1);
942 
943 		DSI_PORT_WRITE(PHY_AFEC1,
944 			       VC4_SET_FIELD(6,  DSI0_PHY_AFEC1_IDR_DLANE1) |
945 			       VC4_SET_FIELD(6,  DSI0_PHY_AFEC1_IDR_DLANE0) |
946 			       VC4_SET_FIELD(6,  DSI0_PHY_AFEC1_IDR_CLANE));
947 	} else {
948 		u32 afec0 = (VC4_SET_FIELD(7, DSI_PHY_AFEC0_PTATADJ) |
949 			     VC4_SET_FIELD(7, DSI_PHY_AFEC0_CTATADJ) |
950 			     VC4_SET_FIELD(6, DSI1_PHY_AFEC0_IDR_CLANE) |
951 			     VC4_SET_FIELD(6, DSI1_PHY_AFEC0_IDR_DLANE0) |
952 			     VC4_SET_FIELD(6, DSI1_PHY_AFEC0_IDR_DLANE1) |
953 			     VC4_SET_FIELD(6, DSI1_PHY_AFEC0_IDR_DLANE2) |
954 			     VC4_SET_FIELD(6, DSI1_PHY_AFEC0_IDR_DLANE3));
955 
956 		if (dsi->lanes < 4)
957 			afec0 |= DSI1_PHY_AFEC0_PD_DLANE3;
958 		if (dsi->lanes < 3)
959 			afec0 |= DSI1_PHY_AFEC0_PD_DLANE2;
960 		if (dsi->lanes < 2)
961 			afec0 |= DSI1_PHY_AFEC0_PD_DLANE1;
962 
963 		afec0 |= DSI1_PHY_AFEC0_RESET;
964 
965 		DSI_PORT_WRITE(PHY_AFEC0, afec0);
966 
967 		DSI_PORT_WRITE(PHY_AFEC1, 0);
968 
969 		/* AFEC reset hold time */
970 		mdelay(1);
971 	}
972 
973 	ret = clk_prepare_enable(dsi->escape_clock);
974 	if (ret) {
975 		DRM_ERROR("Failed to turn on DSI escape clock: %d\n", ret);
976 		return;
977 	}
978 
979 	ret = clk_prepare_enable(dsi->pll_phy_clock);
980 	if (ret) {
981 		DRM_ERROR("Failed to turn on DSI PLL: %d\n", ret);
982 		return;
983 	}
984 
985 	hs_clock = clk_get_rate(dsi->pll_phy_clock);
986 
987 	/* Yes, we set the DSI0P/DSI1P pixel clock to the byte rate,
988 	 * not the pixel clock rate.  DSIxP take from the APHY's byte,
989 	 * DDR2, or DDR4 clock (we use byte) and feed into the PV at
990 	 * that rate.  Separately, a value derived from PIX_CLK_DIV
991 	 * and HS_CLKC is fed into the PV to divide down to the actual
992 	 * pixel clock for pushing pixels into DSI.
993 	 */
994 	dsip_clock = phy_clock / 8;
995 	ret = clk_set_rate(dsi->pixel_clock, dsip_clock);
996 	if (ret) {
997 		dev_err(dev, "Failed to set pixel clock to %ldHz: %d\n",
998 			dsip_clock, ret);
999 	}
1000 
1001 	ret = clk_prepare_enable(dsi->pixel_clock);
1002 	if (ret) {
1003 		DRM_ERROR("Failed to turn on DSI pixel clock: %d\n", ret);
1004 		return;
1005 	}
1006 
1007 	/* How many ns one DSI unit interval is.  Note that the clock
1008 	 * is DDR, so there's an extra divide by 2.
1009 	 */
1010 	ui_ns = DIV_ROUND_UP(500000000, hs_clock);
1011 
1012 	DSI_PORT_WRITE(HS_CLT0,
1013 		       VC4_SET_FIELD(dsi_hs_timing(ui_ns, 262, 0),
1014 				     DSI_HS_CLT0_CZERO) |
1015 		       VC4_SET_FIELD(dsi_hs_timing(ui_ns, 0, 8),
1016 				     DSI_HS_CLT0_CPRE) |
1017 		       VC4_SET_FIELD(dsi_hs_timing(ui_ns, 38, 0),
1018 				     DSI_HS_CLT0_CPREP));
1019 
1020 	DSI_PORT_WRITE(HS_CLT1,
1021 		       VC4_SET_FIELD(dsi_hs_timing(ui_ns, 60, 0),
1022 				     DSI_HS_CLT1_CTRAIL) |
1023 		       VC4_SET_FIELD(dsi_hs_timing(ui_ns, 60, 52),
1024 				     DSI_HS_CLT1_CPOST));
1025 
1026 	DSI_PORT_WRITE(HS_CLT2,
1027 		       VC4_SET_FIELD(dsi_hs_timing(ui_ns, 1000000, 0),
1028 				     DSI_HS_CLT2_WUP));
1029 
1030 	DSI_PORT_WRITE(HS_DLT3,
1031 		       VC4_SET_FIELD(dsi_hs_timing(ui_ns, 100, 0),
1032 				     DSI_HS_DLT3_EXIT) |
1033 		       VC4_SET_FIELD(dsi_hs_timing(ui_ns, 105, 6),
1034 				     DSI_HS_DLT3_ZERO) |
1035 		       VC4_SET_FIELD(dsi_hs_timing(ui_ns, 40, 4),
1036 				     DSI_HS_DLT3_PRE));
1037 
1038 	DSI_PORT_WRITE(HS_DLT4,
1039 		       VC4_SET_FIELD(dsi_hs_timing(ui_ns, lpx * ESC_TIME_NS, 0),
1040 				     DSI_HS_DLT4_LPX) |
1041 		       VC4_SET_FIELD(max(dsi_hs_timing(ui_ns, 0, 8),
1042 					 dsi_hs_timing(ui_ns, 60, 4)),
1043 				     DSI_HS_DLT4_TRAIL) |
1044 		       VC4_SET_FIELD(0, DSI_HS_DLT4_ANLAT));
1045 
1046 	/* T_INIT is how long STOP is driven after power-up to
1047 	 * indicate to the slave (also coming out of power-up) that
1048 	 * master init is complete, and should be greater than the
1049 	 * maximum of two value: T_INIT,MASTER and T_INIT,SLAVE.  The
1050 	 * D-PHY spec gives a minimum 100us for T_INIT,MASTER and
1051 	 * T_INIT,SLAVE, while allowing protocols on top of it to give
1052 	 * greater minimums.  The vc4 firmware uses an extremely
1053 	 * conservative 5ms, and we maintain that here.
1054 	 */
1055 	DSI_PORT_WRITE(HS_DLT5, VC4_SET_FIELD(dsi_hs_timing(ui_ns,
1056 							    5 * 1000 * 1000, 0),
1057 					      DSI_HS_DLT5_INIT));
1058 
1059 	DSI_PORT_WRITE(HS_DLT6,
1060 		       VC4_SET_FIELD(lpx * 5, DSI_HS_DLT6_TA_GET) |
1061 		       VC4_SET_FIELD(lpx, DSI_HS_DLT6_TA_SURE) |
1062 		       VC4_SET_FIELD(lpx * 4, DSI_HS_DLT6_TA_GO) |
1063 		       VC4_SET_FIELD(lpx, DSI_HS_DLT6_LP_LPX));
1064 
1065 	DSI_PORT_WRITE(HS_DLT7,
1066 		       VC4_SET_FIELD(dsi_esc_timing(1000000),
1067 				     DSI_HS_DLT7_LP_WUP));
1068 
1069 	DSI_PORT_WRITE(PHYC,
1070 		       DSI_PHYC_DLANE0_ENABLE |
1071 		       (dsi->lanes >= 2 ? DSI_PHYC_DLANE1_ENABLE : 0) |
1072 		       (dsi->lanes >= 3 ? DSI_PHYC_DLANE2_ENABLE : 0) |
1073 		       (dsi->lanes >= 4 ? DSI_PHYC_DLANE3_ENABLE : 0) |
1074 		       DSI_PORT_BIT(PHYC_CLANE_ENABLE) |
1075 		       ((dsi->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS) ?
1076 			0 : DSI_PORT_BIT(PHYC_HS_CLK_CONTINUOUS)) |
1077 		       (dsi->variant->port == 0 ?
1078 			VC4_SET_FIELD(lpx - 1, DSI0_PHYC_ESC_CLK_LPDT) :
1079 			VC4_SET_FIELD(lpx - 1, DSI1_PHYC_ESC_CLK_LPDT)));
1080 
1081 	DSI_PORT_WRITE(CTRL,
1082 		       DSI_PORT_READ(CTRL) |
1083 		       DSI_CTRL_CAL_BYTE);
1084 
1085 	/* HS timeout in HS clock cycles: disabled. */
1086 	DSI_PORT_WRITE(HSTX_TO_CNT, 0);
1087 	/* LP receive timeout in HS clocks. */
1088 	DSI_PORT_WRITE(LPRX_TO_CNT, 0xffffff);
1089 	/* Bus turnaround timeout */
1090 	DSI_PORT_WRITE(TA_TO_CNT, 100000);
1091 	/* Display reset sequence timeout */
1092 	DSI_PORT_WRITE(PR_TO_CNT, 100000);
1093 
1094 	/* Set up DISP1 for transferring long command payloads through
1095 	 * the pixfifo.
1096 	 */
1097 	DSI_PORT_WRITE(DISP1_CTRL,
1098 		       VC4_SET_FIELD(DSI_DISP1_PFORMAT_32BIT_LE,
1099 				     DSI_DISP1_PFORMAT) |
1100 		       DSI_DISP1_ENABLE);
1101 
1102 	/* Ungate the block. */
1103 	if (dsi->variant->port == 0)
1104 		DSI_PORT_WRITE(CTRL, DSI_PORT_READ(CTRL) | DSI0_CTRL_CTRL0);
1105 	else
1106 		DSI_PORT_WRITE(CTRL, DSI_PORT_READ(CTRL) | DSI1_CTRL_EN);
1107 
1108 	/* Bring AFE out of reset. */
1109 	DSI_PORT_WRITE(PHY_AFEC0,
1110 		       DSI_PORT_READ(PHY_AFEC0) &
1111 		       ~DSI_PORT_BIT(PHY_AFEC0_RESET));
1112 
1113 	vc4_dsi_ulps(dsi, false);
1114 
1115 	list_for_each_entry_reverse(iter, &dsi->bridge_chain, chain_node) {
1116 		if (iter->funcs->pre_enable)
1117 			iter->funcs->pre_enable(iter);
1118 	}
1119 
1120 	if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO) {
1121 		DSI_PORT_WRITE(DISP0_CTRL,
1122 			       VC4_SET_FIELD(dsi->divider,
1123 					     DSI_DISP0_PIX_CLK_DIV) |
1124 			       VC4_SET_FIELD(dsi->format, DSI_DISP0_PFORMAT) |
1125 			       VC4_SET_FIELD(DSI_DISP0_LP_STOP_PERFRAME,
1126 					     DSI_DISP0_LP_STOP_CTRL) |
1127 			       DSI_DISP0_ST_END |
1128 			       DSI_DISP0_ENABLE);
1129 	} else {
1130 		DSI_PORT_WRITE(DISP0_CTRL,
1131 			       DSI_DISP0_COMMAND_MODE |
1132 			       DSI_DISP0_ENABLE);
1133 	}
1134 
1135 	list_for_each_entry(iter, &dsi->bridge_chain, chain_node) {
1136 		if (iter->funcs->enable)
1137 			iter->funcs->enable(iter);
1138 	}
1139 
1140 	if (debug_dump_regs) {
1141 		struct drm_printer p = drm_info_printer(&dsi->pdev->dev);
1142 		dev_info(&dsi->pdev->dev, "DSI regs after:\n");
1143 		drm_print_regset32(&p, &dsi->regset);
1144 	}
1145 }
1146 
1147 static ssize_t vc4_dsi_host_transfer(struct mipi_dsi_host *host,
1148 				     const struct mipi_dsi_msg *msg)
1149 {
1150 	struct vc4_dsi *dsi = host_to_dsi(host);
1151 	struct mipi_dsi_packet packet;
1152 	u32 pkth = 0, pktc = 0;
1153 	int i, ret;
1154 	bool is_long = mipi_dsi_packet_format_is_long(msg->type);
1155 	u32 cmd_fifo_len = 0, pix_fifo_len = 0;
1156 
1157 	mipi_dsi_create_packet(&packet, msg);
1158 
1159 	pkth |= VC4_SET_FIELD(packet.header[0], DSI_TXPKT1H_BC_DT);
1160 	pkth |= VC4_SET_FIELD(packet.header[1] |
1161 			      (packet.header[2] << 8),
1162 			      DSI_TXPKT1H_BC_PARAM);
1163 	if (is_long) {
1164 		/* Divide data across the various FIFOs we have available.
1165 		 * The command FIFO takes byte-oriented data, but is of
1166 		 * limited size. The pixel FIFO (never actually used for
1167 		 * pixel data in reality) is word oriented, and substantially
1168 		 * larger. So, we use the pixel FIFO for most of the data,
1169 		 * sending the residual bytes in the command FIFO at the start.
1170 		 *
1171 		 * With this arrangement, the command FIFO will never get full.
1172 		 */
1173 		if (packet.payload_length <= 16) {
1174 			cmd_fifo_len = packet.payload_length;
1175 			pix_fifo_len = 0;
1176 		} else {
1177 			cmd_fifo_len = (packet.payload_length %
1178 					DSI_PIX_FIFO_WIDTH);
1179 			pix_fifo_len = ((packet.payload_length - cmd_fifo_len) /
1180 					DSI_PIX_FIFO_WIDTH);
1181 		}
1182 
1183 		WARN_ON_ONCE(pix_fifo_len >= DSI_PIX_FIFO_DEPTH);
1184 
1185 		pkth |= VC4_SET_FIELD(cmd_fifo_len, DSI_TXPKT1H_BC_CMDFIFO);
1186 	}
1187 
1188 	if (msg->rx_len) {
1189 		pktc |= VC4_SET_FIELD(DSI_TXPKT1C_CMD_CTRL_RX,
1190 				      DSI_TXPKT1C_CMD_CTRL);
1191 	} else {
1192 		pktc |= VC4_SET_FIELD(DSI_TXPKT1C_CMD_CTRL_TX,
1193 				      DSI_TXPKT1C_CMD_CTRL);
1194 	}
1195 
1196 	for (i = 0; i < cmd_fifo_len; i++)
1197 		DSI_PORT_WRITE(TXPKT_CMD_FIFO, packet.payload[i]);
1198 	for (i = 0; i < pix_fifo_len; i++) {
1199 		const u8 *pix = packet.payload + cmd_fifo_len + i * 4;
1200 
1201 		DSI_PORT_WRITE(TXPKT_PIX_FIFO,
1202 			       pix[0] |
1203 			       pix[1] << 8 |
1204 			       pix[2] << 16 |
1205 			       pix[3] << 24);
1206 	}
1207 
1208 	if (msg->flags & MIPI_DSI_MSG_USE_LPM)
1209 		pktc |= DSI_TXPKT1C_CMD_MODE_LP;
1210 	if (is_long)
1211 		pktc |= DSI_TXPKT1C_CMD_TYPE_LONG;
1212 
1213 	/* Send one copy of the packet.  Larger repeats are used for pixel
1214 	 * data in command mode.
1215 	 */
1216 	pktc |= VC4_SET_FIELD(1, DSI_TXPKT1C_CMD_REPEAT);
1217 
1218 	pktc |= DSI_TXPKT1C_CMD_EN;
1219 	if (pix_fifo_len) {
1220 		pktc |= VC4_SET_FIELD(DSI_TXPKT1C_DISPLAY_NO_SECONDARY,
1221 				      DSI_TXPKT1C_DISPLAY_NO);
1222 	} else {
1223 		pktc |= VC4_SET_FIELD(DSI_TXPKT1C_DISPLAY_NO_SHORT,
1224 				      DSI_TXPKT1C_DISPLAY_NO);
1225 	}
1226 
1227 	/* Enable the appropriate interrupt for the transfer completion. */
1228 	dsi->xfer_result = 0;
1229 	reinit_completion(&dsi->xfer_completion);
1230 	if (dsi->variant->port == 0) {
1231 		DSI_PORT_WRITE(INT_STAT,
1232 			       DSI0_INT_CMDC_DONE_MASK | DSI1_INT_PHY_DIR_RTF);
1233 		if (msg->rx_len) {
1234 			DSI_PORT_WRITE(INT_EN, (DSI0_INTERRUPTS_ALWAYS_ENABLED |
1235 						DSI0_INT_PHY_DIR_RTF));
1236 		} else {
1237 			DSI_PORT_WRITE(INT_EN,
1238 				       (DSI0_INTERRUPTS_ALWAYS_ENABLED |
1239 					VC4_SET_FIELD(DSI0_INT_CMDC_DONE_NO_REPEAT,
1240 						      DSI0_INT_CMDC_DONE)));
1241 		}
1242 	} else {
1243 		DSI_PORT_WRITE(INT_STAT,
1244 			       DSI1_INT_TXPKT1_DONE | DSI1_INT_PHY_DIR_RTF);
1245 		if (msg->rx_len) {
1246 			DSI_PORT_WRITE(INT_EN, (DSI1_INTERRUPTS_ALWAYS_ENABLED |
1247 						DSI1_INT_PHY_DIR_RTF));
1248 		} else {
1249 			DSI_PORT_WRITE(INT_EN, (DSI1_INTERRUPTS_ALWAYS_ENABLED |
1250 						DSI1_INT_TXPKT1_DONE));
1251 		}
1252 	}
1253 
1254 	/* Send the packet. */
1255 	DSI_PORT_WRITE(TXPKT1H, pkth);
1256 	DSI_PORT_WRITE(TXPKT1C, pktc);
1257 
1258 	if (!wait_for_completion_timeout(&dsi->xfer_completion,
1259 					 msecs_to_jiffies(1000))) {
1260 		dev_err(&dsi->pdev->dev, "transfer interrupt wait timeout");
1261 		dev_err(&dsi->pdev->dev, "instat: 0x%08x\n",
1262 			DSI_PORT_READ(INT_STAT));
1263 		ret = -ETIMEDOUT;
1264 	} else {
1265 		ret = dsi->xfer_result;
1266 	}
1267 
1268 	DSI_PORT_WRITE(INT_EN, DSI_PORT_BIT(INTERRUPTS_ALWAYS_ENABLED));
1269 
1270 	if (ret)
1271 		goto reset_fifo_and_return;
1272 
1273 	if (ret == 0 && msg->rx_len) {
1274 		u32 rxpkt1h = DSI_PORT_READ(RXPKT1H);
1275 		u8 *msg_rx = msg->rx_buf;
1276 
1277 		if (rxpkt1h & DSI_RXPKT1H_PKT_TYPE_LONG) {
1278 			u32 rxlen = VC4_GET_FIELD(rxpkt1h,
1279 						  DSI_RXPKT1H_BC_PARAM);
1280 
1281 			if (rxlen != msg->rx_len) {
1282 				DRM_ERROR("DSI returned %db, expecting %db\n",
1283 					  rxlen, (int)msg->rx_len);
1284 				ret = -ENXIO;
1285 				goto reset_fifo_and_return;
1286 			}
1287 
1288 			for (i = 0; i < msg->rx_len; i++)
1289 				msg_rx[i] = DSI_READ(DSI1_RXPKT_FIFO);
1290 		} else {
1291 			/* FINISHME: Handle AWER */
1292 
1293 			msg_rx[0] = VC4_GET_FIELD(rxpkt1h,
1294 						  DSI_RXPKT1H_SHORT_0);
1295 			if (msg->rx_len > 1) {
1296 				msg_rx[1] = VC4_GET_FIELD(rxpkt1h,
1297 							  DSI_RXPKT1H_SHORT_1);
1298 			}
1299 		}
1300 	}
1301 
1302 	return ret;
1303 
1304 reset_fifo_and_return:
1305 	DRM_ERROR("DSI transfer failed, resetting: %d\n", ret);
1306 
1307 	DSI_PORT_WRITE(TXPKT1C, DSI_PORT_READ(TXPKT1C) & ~DSI_TXPKT1C_CMD_EN);
1308 	udelay(1);
1309 	DSI_PORT_WRITE(CTRL,
1310 		       DSI_PORT_READ(CTRL) |
1311 		       DSI_PORT_BIT(CTRL_RESET_FIFOS));
1312 
1313 	DSI_PORT_WRITE(TXPKT1C, 0);
1314 	DSI_PORT_WRITE(INT_EN, DSI_PORT_BIT(INTERRUPTS_ALWAYS_ENABLED));
1315 	return ret;
1316 }
1317 
1318 static const struct component_ops vc4_dsi_ops;
1319 static int vc4_dsi_host_attach(struct mipi_dsi_host *host,
1320 			       struct mipi_dsi_device *device)
1321 {
1322 	struct vc4_dsi *dsi = host_to_dsi(host);
1323 
1324 	dsi->lanes = device->lanes;
1325 	dsi->channel = device->channel;
1326 	dsi->mode_flags = device->mode_flags;
1327 
1328 	switch (device->format) {
1329 	case MIPI_DSI_FMT_RGB888:
1330 		dsi->format = DSI_PFORMAT_RGB888;
1331 		dsi->divider = 24 / dsi->lanes;
1332 		break;
1333 	case MIPI_DSI_FMT_RGB666:
1334 		dsi->format = DSI_PFORMAT_RGB666;
1335 		dsi->divider = 24 / dsi->lanes;
1336 		break;
1337 	case MIPI_DSI_FMT_RGB666_PACKED:
1338 		dsi->format = DSI_PFORMAT_RGB666_PACKED;
1339 		dsi->divider = 18 / dsi->lanes;
1340 		break;
1341 	case MIPI_DSI_FMT_RGB565:
1342 		dsi->format = DSI_PFORMAT_RGB565;
1343 		dsi->divider = 16 / dsi->lanes;
1344 		break;
1345 	default:
1346 		dev_err(&dsi->pdev->dev, "Unknown DSI format: %d.\n",
1347 			dsi->format);
1348 		return 0;
1349 	}
1350 
1351 	if (!(dsi->mode_flags & MIPI_DSI_MODE_VIDEO)) {
1352 		dev_err(&dsi->pdev->dev,
1353 			"Only VIDEO mode panels supported currently.\n");
1354 		return 0;
1355 	}
1356 
1357 	return component_add(&dsi->pdev->dev, &vc4_dsi_ops);
1358 }
1359 
1360 static int vc4_dsi_host_detach(struct mipi_dsi_host *host,
1361 			       struct mipi_dsi_device *device)
1362 {
1363 	struct vc4_dsi *dsi = host_to_dsi(host);
1364 
1365 	component_del(&dsi->pdev->dev, &vc4_dsi_ops);
1366 	return 0;
1367 }
1368 
1369 static const struct mipi_dsi_host_ops vc4_dsi_host_ops = {
1370 	.attach = vc4_dsi_host_attach,
1371 	.detach = vc4_dsi_host_detach,
1372 	.transfer = vc4_dsi_host_transfer,
1373 };
1374 
1375 static const struct drm_encoder_helper_funcs vc4_dsi_encoder_helper_funcs = {
1376 	.disable = vc4_dsi_encoder_disable,
1377 	.enable = vc4_dsi_encoder_enable,
1378 	.mode_fixup = vc4_dsi_encoder_mode_fixup,
1379 };
1380 
1381 static const struct vc4_dsi_variant bcm2711_dsi1_variant = {
1382 	.port			= 1,
1383 	.debugfs_name		= "dsi1_regs",
1384 	.regs			= dsi1_regs,
1385 	.nregs			= ARRAY_SIZE(dsi1_regs),
1386 };
1387 
1388 static const struct vc4_dsi_variant bcm2835_dsi0_variant = {
1389 	.port			= 0,
1390 	.debugfs_name		= "dsi0_regs",
1391 	.regs			= dsi0_regs,
1392 	.nregs			= ARRAY_SIZE(dsi0_regs),
1393 };
1394 
1395 static const struct vc4_dsi_variant bcm2835_dsi1_variant = {
1396 	.port			= 1,
1397 	.broken_axi_workaround	= true,
1398 	.debugfs_name		= "dsi1_regs",
1399 	.regs			= dsi1_regs,
1400 	.nregs			= ARRAY_SIZE(dsi1_regs),
1401 };
1402 
1403 static const struct of_device_id vc4_dsi_dt_match[] = {
1404 	{ .compatible = "brcm,bcm2711-dsi1", &bcm2711_dsi1_variant },
1405 	{ .compatible = "brcm,bcm2835-dsi0", &bcm2835_dsi0_variant },
1406 	{ .compatible = "brcm,bcm2835-dsi1", &bcm2835_dsi1_variant },
1407 	{}
1408 };
1409 
1410 static void dsi_handle_error(struct vc4_dsi *dsi,
1411 			     irqreturn_t *ret, u32 stat, u32 bit,
1412 			     const char *type)
1413 {
1414 	if (!(stat & bit))
1415 		return;
1416 
1417 	DRM_ERROR("DSI%d: %s error\n", dsi->variant->port, type);
1418 	*ret = IRQ_HANDLED;
1419 }
1420 
1421 /*
1422  * Initial handler for port 1 where we need the reg_dma workaround.
1423  * The register DMA writes sleep, so we can't do it in the top half.
1424  * Instead we use IRQF_ONESHOT so that the IRQ gets disabled in the
1425  * parent interrupt contrller until our interrupt thread is done.
1426  */
1427 static irqreturn_t vc4_dsi_irq_defer_to_thread_handler(int irq, void *data)
1428 {
1429 	struct vc4_dsi *dsi = data;
1430 	u32 stat = DSI_PORT_READ(INT_STAT);
1431 
1432 	if (!stat)
1433 		return IRQ_NONE;
1434 
1435 	return IRQ_WAKE_THREAD;
1436 }
1437 
1438 /*
1439  * Normal IRQ handler for port 0, or the threaded IRQ handler for port
1440  * 1 where we need the reg_dma workaround.
1441  */
1442 static irqreturn_t vc4_dsi_irq_handler(int irq, void *data)
1443 {
1444 	struct vc4_dsi *dsi = data;
1445 	u32 stat = DSI_PORT_READ(INT_STAT);
1446 	irqreturn_t ret = IRQ_NONE;
1447 
1448 	DSI_PORT_WRITE(INT_STAT, stat);
1449 
1450 	dsi_handle_error(dsi, &ret, stat,
1451 			 DSI_PORT_BIT(INT_ERR_SYNC_ESC), "LPDT sync");
1452 	dsi_handle_error(dsi, &ret, stat,
1453 			 DSI_PORT_BIT(INT_ERR_CONTROL), "data lane 0 sequence");
1454 	dsi_handle_error(dsi, &ret, stat,
1455 			 DSI_PORT_BIT(INT_ERR_CONT_LP0), "LP0 contention");
1456 	dsi_handle_error(dsi, &ret, stat,
1457 			 DSI_PORT_BIT(INT_ERR_CONT_LP1), "LP1 contention");
1458 	dsi_handle_error(dsi, &ret, stat,
1459 			 DSI_PORT_BIT(INT_HSTX_TO), "HSTX timeout");
1460 	dsi_handle_error(dsi, &ret, stat,
1461 			 DSI_PORT_BIT(INT_LPRX_TO), "LPRX timeout");
1462 	dsi_handle_error(dsi, &ret, stat,
1463 			 DSI_PORT_BIT(INT_TA_TO), "turnaround timeout");
1464 	dsi_handle_error(dsi, &ret, stat,
1465 			 DSI_PORT_BIT(INT_PR_TO), "peripheral reset timeout");
1466 
1467 	if (stat & ((dsi->variant->port ? DSI1_INT_TXPKT1_DONE :
1468 					  DSI0_INT_CMDC_DONE_MASK) |
1469 		    DSI_PORT_BIT(INT_PHY_DIR_RTF))) {
1470 		complete(&dsi->xfer_completion);
1471 		ret = IRQ_HANDLED;
1472 	} else if (stat & DSI_PORT_BIT(INT_HSTX_TO)) {
1473 		complete(&dsi->xfer_completion);
1474 		dsi->xfer_result = -ETIMEDOUT;
1475 		ret = IRQ_HANDLED;
1476 	}
1477 
1478 	return ret;
1479 }
1480 
1481 /**
1482  * vc4_dsi_init_phy_clocks - Exposes clocks generated by the analog
1483  * PHY that are consumed by CPRMAN (clk-bcm2835.c).
1484  * @dsi: DSI encoder
1485  */
1486 static int
1487 vc4_dsi_init_phy_clocks(struct vc4_dsi *dsi)
1488 {
1489 	struct device *dev = &dsi->pdev->dev;
1490 	const char *parent_name = __clk_get_name(dsi->pll_phy_clock);
1491 	static const struct {
1492 		const char *name;
1493 		int div;
1494 	} phy_clocks[] = {
1495 		{ "byte", 8 },
1496 		{ "ddr2", 4 },
1497 		{ "ddr", 2 },
1498 	};
1499 	int i;
1500 
1501 	dsi->clk_onecell = devm_kzalloc(dev,
1502 					sizeof(*dsi->clk_onecell) +
1503 					ARRAY_SIZE(phy_clocks) *
1504 					sizeof(struct clk_hw *),
1505 					GFP_KERNEL);
1506 	if (!dsi->clk_onecell)
1507 		return -ENOMEM;
1508 	dsi->clk_onecell->num = ARRAY_SIZE(phy_clocks);
1509 
1510 	for (i = 0; i < ARRAY_SIZE(phy_clocks); i++) {
1511 		struct clk_fixed_factor *fix = &dsi->phy_clocks[i];
1512 		struct clk_init_data init;
1513 		char clk_name[16];
1514 		int ret;
1515 
1516 		snprintf(clk_name, sizeof(clk_name),
1517 			 "dsi%u_%s", dsi->variant->port, phy_clocks[i].name);
1518 
1519 		/* We just use core fixed factor clock ops for the PHY
1520 		 * clocks.  The clocks are actually gated by the
1521 		 * PHY_AFEC0_DDRCLK_EN bits, which we should be
1522 		 * setting if we use the DDR/DDR2 clocks.  However,
1523 		 * vc4_dsi_encoder_enable() is setting up both AFEC0,
1524 		 * setting both our parent DSI PLL's rate and this
1525 		 * clock's rate, so it knows if DDR/DDR2 are going to
1526 		 * be used and could enable the gates itself.
1527 		 */
1528 		fix->mult = 1;
1529 		fix->div = phy_clocks[i].div;
1530 		fix->hw.init = &init;
1531 
1532 		memset(&init, 0, sizeof(init));
1533 		init.parent_names = &parent_name;
1534 		init.num_parents = 1;
1535 		init.name = clk_name;
1536 		init.ops = &clk_fixed_factor_ops;
1537 
1538 		ret = devm_clk_hw_register(dev, &fix->hw);
1539 		if (ret)
1540 			return ret;
1541 
1542 		dsi->clk_onecell->hws[i] = &fix->hw;
1543 	}
1544 
1545 	return of_clk_add_hw_provider(dev->of_node,
1546 				      of_clk_hw_onecell_get,
1547 				      dsi->clk_onecell);
1548 }
1549 
1550 static void vc4_dsi_dma_mem_release(void *ptr)
1551 {
1552 	struct vc4_dsi *dsi = ptr;
1553 	struct device *dev = &dsi->pdev->dev;
1554 
1555 	dma_free_coherent(dev, 4, dsi->reg_dma_mem, dsi->reg_dma_paddr);
1556 	dsi->reg_dma_mem = NULL;
1557 }
1558 
1559 static void vc4_dsi_dma_chan_release(void *ptr)
1560 {
1561 	struct vc4_dsi *dsi = ptr;
1562 
1563 	dma_release_channel(dsi->reg_dma_chan);
1564 	dsi->reg_dma_chan = NULL;
1565 }
1566 
1567 static int vc4_dsi_bind(struct device *dev, struct device *master, void *data)
1568 {
1569 	struct platform_device *pdev = to_platform_device(dev);
1570 	struct drm_device *drm = dev_get_drvdata(master);
1571 	struct vc4_dsi *dsi = dev_get_drvdata(dev);
1572 	struct vc4_dsi_encoder *vc4_dsi_encoder;
1573 	int ret;
1574 
1575 	dsi->variant = of_device_get_match_data(dev);
1576 
1577 	vc4_dsi_encoder = devm_kzalloc(dev, sizeof(*vc4_dsi_encoder),
1578 				       GFP_KERNEL);
1579 	if (!vc4_dsi_encoder)
1580 		return -ENOMEM;
1581 
1582 	INIT_LIST_HEAD(&dsi->bridge_chain);
1583 	vc4_dsi_encoder->base.type = dsi->variant->port ?
1584 			VC4_ENCODER_TYPE_DSI1 : VC4_ENCODER_TYPE_DSI0;
1585 	vc4_dsi_encoder->dsi = dsi;
1586 	dsi->encoder = &vc4_dsi_encoder->base.base;
1587 
1588 	dsi->regs = vc4_ioremap_regs(pdev, 0);
1589 	if (IS_ERR(dsi->regs))
1590 		return PTR_ERR(dsi->regs);
1591 
1592 	dsi->regset.base = dsi->regs;
1593 	dsi->regset.regs = dsi->variant->regs;
1594 	dsi->regset.nregs = dsi->variant->nregs;
1595 
1596 	if (DSI_PORT_READ(ID) != DSI_ID_VALUE) {
1597 		dev_err(dev, "Port returned 0x%08x for ID instead of 0x%08x\n",
1598 			DSI_PORT_READ(ID), DSI_ID_VALUE);
1599 		return -ENODEV;
1600 	}
1601 
1602 	/* DSI1 on BCM2835/6/7 has a broken AXI slave that doesn't respond to
1603 	 * writes from the ARM.  It does handle writes from the DMA engine,
1604 	 * so set up a channel for talking to it.
1605 	 */
1606 	if (dsi->variant->broken_axi_workaround) {
1607 		dma_cap_mask_t dma_mask;
1608 
1609 		dsi->reg_dma_mem = dma_alloc_coherent(dev, 4,
1610 						      &dsi->reg_dma_paddr,
1611 						      GFP_KERNEL);
1612 		if (!dsi->reg_dma_mem) {
1613 			DRM_ERROR("Failed to get DMA memory\n");
1614 			return -ENOMEM;
1615 		}
1616 
1617 		ret = devm_add_action_or_reset(dev, vc4_dsi_dma_mem_release, dsi);
1618 		if (ret)
1619 			return ret;
1620 
1621 		dma_cap_zero(dma_mask);
1622 		dma_cap_set(DMA_MEMCPY, dma_mask);
1623 
1624 		dsi->reg_dma_chan = dma_request_chan_by_mask(&dma_mask);
1625 		if (IS_ERR(dsi->reg_dma_chan)) {
1626 			ret = PTR_ERR(dsi->reg_dma_chan);
1627 			if (ret != -EPROBE_DEFER)
1628 				DRM_ERROR("Failed to get DMA channel: %d\n",
1629 					  ret);
1630 			return ret;
1631 		}
1632 
1633 		ret = devm_add_action_or_reset(dev, vc4_dsi_dma_chan_release, dsi);
1634 		if (ret)
1635 			return ret;
1636 
1637 		/* Get the physical address of the device's registers.  The
1638 		 * struct resource for the regs gives us the bus address
1639 		 * instead.
1640 		 */
1641 		dsi->reg_paddr = be32_to_cpup(of_get_address(dev->of_node,
1642 							     0, NULL, NULL));
1643 	}
1644 
1645 	init_completion(&dsi->xfer_completion);
1646 	/* At startup enable error-reporting interrupts and nothing else. */
1647 	DSI_PORT_WRITE(INT_EN, DSI1_INTERRUPTS_ALWAYS_ENABLED);
1648 	/* Clear any existing interrupt state. */
1649 	DSI_PORT_WRITE(INT_STAT, DSI_PORT_READ(INT_STAT));
1650 
1651 	if (dsi->reg_dma_mem)
1652 		ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0),
1653 						vc4_dsi_irq_defer_to_thread_handler,
1654 						vc4_dsi_irq_handler,
1655 						IRQF_ONESHOT,
1656 						"vc4 dsi", dsi);
1657 	else
1658 		ret = devm_request_irq(dev, platform_get_irq(pdev, 0),
1659 				       vc4_dsi_irq_handler, 0, "vc4 dsi", dsi);
1660 	if (ret) {
1661 		if (ret != -EPROBE_DEFER)
1662 			dev_err(dev, "Failed to get interrupt: %d\n", ret);
1663 		return ret;
1664 	}
1665 
1666 	dsi->escape_clock = devm_clk_get(dev, "escape");
1667 	if (IS_ERR(dsi->escape_clock)) {
1668 		ret = PTR_ERR(dsi->escape_clock);
1669 		if (ret != -EPROBE_DEFER)
1670 			dev_err(dev, "Failed to get escape clock: %d\n", ret);
1671 		return ret;
1672 	}
1673 
1674 	dsi->pll_phy_clock = devm_clk_get(dev, "phy");
1675 	if (IS_ERR(dsi->pll_phy_clock)) {
1676 		ret = PTR_ERR(dsi->pll_phy_clock);
1677 		if (ret != -EPROBE_DEFER)
1678 			dev_err(dev, "Failed to get phy clock: %d\n", ret);
1679 		return ret;
1680 	}
1681 
1682 	dsi->pixel_clock = devm_clk_get(dev, "pixel");
1683 	if (IS_ERR(dsi->pixel_clock)) {
1684 		ret = PTR_ERR(dsi->pixel_clock);
1685 		if (ret != -EPROBE_DEFER)
1686 			dev_err(dev, "Failed to get pixel clock: %d\n", ret);
1687 		return ret;
1688 	}
1689 
1690 	dsi->bridge = devm_drm_of_get_bridge(dev, dev->of_node, 0, 0);
1691 	if (IS_ERR(dsi->bridge))
1692 		return PTR_ERR(dsi->bridge);
1693 
1694 	/* The esc clock rate is supposed to always be 100Mhz. */
1695 	ret = clk_set_rate(dsi->escape_clock, 100 * 1000000);
1696 	if (ret) {
1697 		dev_err(dev, "Failed to set esc clock: %d\n", ret);
1698 		return ret;
1699 	}
1700 
1701 	ret = vc4_dsi_init_phy_clocks(dsi);
1702 	if (ret)
1703 		return ret;
1704 
1705 	drm_simple_encoder_init(drm, dsi->encoder, DRM_MODE_ENCODER_DSI);
1706 	drm_encoder_helper_add(dsi->encoder, &vc4_dsi_encoder_helper_funcs);
1707 
1708 	ret = drm_bridge_attach(dsi->encoder, dsi->bridge, NULL, 0);
1709 	if (ret)
1710 		return ret;
1711 	/* Disable the atomic helper calls into the bridge.  We
1712 	 * manually call the bridge pre_enable / enable / etc. calls
1713 	 * from our driver, since we need to sequence them within the
1714 	 * encoder's enable/disable paths.
1715 	 */
1716 	list_splice_init(&dsi->encoder->bridge_chain, &dsi->bridge_chain);
1717 
1718 	vc4_debugfs_add_regset32(drm, dsi->variant->debugfs_name, &dsi->regset);
1719 
1720 	pm_runtime_enable(dev);
1721 
1722 	return 0;
1723 }
1724 
1725 static void vc4_dsi_unbind(struct device *dev, struct device *master,
1726 			   void *data)
1727 {
1728 	struct vc4_dsi *dsi = dev_get_drvdata(dev);
1729 
1730 	pm_runtime_disable(dev);
1731 
1732 	/*
1733 	 * Restore the bridge_chain so the bridge detach procedure can happen
1734 	 * normally.
1735 	 */
1736 	list_splice_init(&dsi->bridge_chain, &dsi->encoder->bridge_chain);
1737 	drm_encoder_cleanup(dsi->encoder);
1738 }
1739 
1740 static const struct component_ops vc4_dsi_ops = {
1741 	.bind   = vc4_dsi_bind,
1742 	.unbind = vc4_dsi_unbind,
1743 };
1744 
1745 static int vc4_dsi_dev_probe(struct platform_device *pdev)
1746 {
1747 	struct device *dev = &pdev->dev;
1748 	struct vc4_dsi *dsi;
1749 
1750 	dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
1751 	if (!dsi)
1752 		return -ENOMEM;
1753 	dev_set_drvdata(dev, dsi);
1754 
1755 	dsi->pdev = pdev;
1756 	dsi->dsi_host.ops = &vc4_dsi_host_ops;
1757 	dsi->dsi_host.dev = dev;
1758 	mipi_dsi_host_register(&dsi->dsi_host);
1759 
1760 	return 0;
1761 }
1762 
1763 static int vc4_dsi_dev_remove(struct platform_device *pdev)
1764 {
1765 	struct device *dev = &pdev->dev;
1766 	struct vc4_dsi *dsi = dev_get_drvdata(dev);
1767 
1768 	mipi_dsi_host_unregister(&dsi->dsi_host);
1769 	return 0;
1770 }
1771 
1772 struct platform_driver vc4_dsi_driver = {
1773 	.probe = vc4_dsi_dev_probe,
1774 	.remove = vc4_dsi_dev_remove,
1775 	.driver = {
1776 		.name = "vc4_dsi",
1777 		.of_match_table = vc4_dsi_dt_match,
1778 	},
1779 };
1780