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