1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright: 2017 Cadence Design Systems, Inc.
4  *
5  * Author: Boris Brezillon <boris.brezillon@bootlin.com>
6  */
7 
8 #include <drm/drm_atomic_helper.h>
9 #include <drm/drm_bridge.h>
10 #include <drm/drm_crtc_helper.h>
11 #include <drm/drm_mipi_dsi.h>
12 #include <drm/drm_panel.h>
13 #include <video/mipi_display.h>
14 
15 #include <linux/clk.h>
16 #include <linux/iopoll.h>
17 #include <linux/module.h>
18 #include <linux/of_address.h>
19 #include <linux/of_graph.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/reset.h>
23 
24 #define IP_CONF				0x0
25 #define SP_HS_FIFO_DEPTH(x)		(((x) & GENMASK(30, 26)) >> 26)
26 #define SP_LP_FIFO_DEPTH(x)		(((x) & GENMASK(25, 21)) >> 21)
27 #define VRS_FIFO_DEPTH(x)		(((x) & GENMASK(20, 16)) >> 16)
28 #define DIRCMD_FIFO_DEPTH(x)		(((x) & GENMASK(15, 13)) >> 13)
29 #define SDI_IFACE_32			BIT(12)
30 #define INTERNAL_DATAPATH_32		(0 << 10)
31 #define INTERNAL_DATAPATH_16		(1 << 10)
32 #define INTERNAL_DATAPATH_8		(3 << 10)
33 #define INTERNAL_DATAPATH_SIZE		((x) & GENMASK(11, 10))
34 #define NUM_IFACE(x)			((((x) & GENMASK(9, 8)) >> 8) + 1)
35 #define MAX_LANE_NB(x)			(((x) & GENMASK(7, 6)) >> 6)
36 #define RX_FIFO_DEPTH(x)		((x) & GENMASK(5, 0))
37 
38 #define MCTL_MAIN_DATA_CTL		0x4
39 #define TE_MIPI_POLLING_EN		BIT(25)
40 #define TE_HW_POLLING_EN		BIT(24)
41 #define DISP_EOT_GEN			BIT(18)
42 #define HOST_EOT_GEN			BIT(17)
43 #define DISP_GEN_CHECKSUM		BIT(16)
44 #define DISP_GEN_ECC			BIT(15)
45 #define BTA_EN				BIT(14)
46 #define READ_EN				BIT(13)
47 #define REG_TE_EN			BIT(12)
48 #define IF_TE_EN(x)			BIT(8 + (x))
49 #define TVG_SEL				BIT(6)
50 #define VID_EN				BIT(5)
51 #define IF_VID_SELECT(x)		((x) << 2)
52 #define IF_VID_SELECT_MASK		GENMASK(3, 2)
53 #define IF_VID_MODE			BIT(1)
54 #define LINK_EN				BIT(0)
55 
56 #define MCTL_MAIN_PHY_CTL		0x8
57 #define HS_INVERT_DAT(x)		BIT(19 + ((x) * 2))
58 #define SWAP_PINS_DAT(x)		BIT(18 + ((x) * 2))
59 #define HS_INVERT_CLK			BIT(17)
60 #define SWAP_PINS_CLK			BIT(16)
61 #define HS_SKEWCAL_EN			BIT(15)
62 #define WAIT_BURST_TIME(x)		((x) << 10)
63 #define DATA_ULPM_EN(x)			BIT(6 + (x))
64 #define CLK_ULPM_EN			BIT(5)
65 #define CLK_CONTINUOUS			BIT(4)
66 #define DATA_LANE_EN(x)			BIT((x) - 1)
67 
68 #define MCTL_MAIN_EN			0xc
69 #define DATA_FORCE_STOP			BIT(17)
70 #define CLK_FORCE_STOP			BIT(16)
71 #define IF_EN(x)			BIT(13 + (x))
72 #define DATA_LANE_ULPM_REQ(l)		BIT(9 + (l))
73 #define CLK_LANE_ULPM_REQ		BIT(8)
74 #define DATA_LANE_START(x)		BIT(4 + (x))
75 #define CLK_LANE_EN			BIT(3)
76 #define PLL_START			BIT(0)
77 
78 #define MCTL_DPHY_CFG0			0x10
79 #define DPHY_C_RSTB			BIT(20)
80 #define DPHY_D_RSTB(x)			GENMASK(15 + (x), 16)
81 #define DPHY_PLL_PDN			BIT(10)
82 #define DPHY_CMN_PDN			BIT(9)
83 #define DPHY_C_PDN			BIT(8)
84 #define DPHY_D_PDN(x)			GENMASK(3 + (x), 4)
85 #define DPHY_ALL_D_PDN			GENMASK(7, 4)
86 #define DPHY_PLL_PSO			BIT(1)
87 #define DPHY_CMN_PSO			BIT(0)
88 
89 #define MCTL_DPHY_TIMEOUT1		0x14
90 #define HSTX_TIMEOUT(x)			((x) << 4)
91 #define HSTX_TIMEOUT_MAX		GENMASK(17, 0)
92 #define CLK_DIV(x)			(x)
93 #define CLK_DIV_MAX			GENMASK(3, 0)
94 
95 #define MCTL_DPHY_TIMEOUT2		0x18
96 #define LPRX_TIMEOUT(x)			(x)
97 
98 #define MCTL_ULPOUT_TIME		0x1c
99 #define DATA_LANE_ULPOUT_TIME(x)	((x) << 9)
100 #define CLK_LANE_ULPOUT_TIME(x)		(x)
101 
102 #define MCTL_3DVIDEO_CTL		0x20
103 #define VID_VSYNC_3D_EN			BIT(7)
104 #define VID_VSYNC_3D_LR			BIT(5)
105 #define VID_VSYNC_3D_SECOND_EN		BIT(4)
106 #define VID_VSYNC_3DFORMAT_LINE		(0 << 2)
107 #define VID_VSYNC_3DFORMAT_FRAME	(1 << 2)
108 #define VID_VSYNC_3DFORMAT_PIXEL	(2 << 2)
109 #define VID_VSYNC_3DMODE_OFF		0
110 #define VID_VSYNC_3DMODE_PORTRAIT	1
111 #define VID_VSYNC_3DMODE_LANDSCAPE	2
112 
113 #define MCTL_MAIN_STS			0x24
114 #define MCTL_MAIN_STS_CTL		0x130
115 #define MCTL_MAIN_STS_CLR		0x150
116 #define MCTL_MAIN_STS_FLAG		0x170
117 #define HS_SKEWCAL_DONE			BIT(11)
118 #define IF_UNTERM_PKT_ERR(x)		BIT(8 + (x))
119 #define LPRX_TIMEOUT_ERR		BIT(7)
120 #define HSTX_TIMEOUT_ERR		BIT(6)
121 #define DATA_LANE_RDY(l)		BIT(2 + (l))
122 #define CLK_LANE_RDY			BIT(1)
123 #define PLL_LOCKED			BIT(0)
124 
125 #define MCTL_DPHY_ERR			0x28
126 #define MCTL_DPHY_ERR_CTL1		0x148
127 #define MCTL_DPHY_ERR_CLR		0x168
128 #define MCTL_DPHY_ERR_FLAG		0x188
129 #define ERR_CONT_LP(x, l)		BIT(18 + ((x) * 4) + (l))
130 #define ERR_CONTROL(l)			BIT(14 + (l))
131 #define ERR_SYNESC(l)			BIT(10 + (l))
132 #define ERR_ESC(l)			BIT(6 + (l))
133 
134 #define MCTL_DPHY_ERR_CTL2		0x14c
135 #define ERR_CONT_LP_EDGE(x, l)		BIT(12 + ((x) * 4) + (l))
136 #define ERR_CONTROL_EDGE(l)		BIT(8 + (l))
137 #define ERR_SYN_ESC_EDGE(l)		BIT(4 + (l))
138 #define ERR_ESC_EDGE(l)			BIT(0 + (l))
139 
140 #define MCTL_LANE_STS			0x2c
141 #define PPI_C_TX_READY_HS		BIT(18)
142 #define DPHY_PLL_LOCK			BIT(17)
143 #define PPI_D_RX_ULPS_ESC(x)		(((x) & GENMASK(15, 12)) >> 12)
144 #define LANE_STATE_START		0
145 #define LANE_STATE_IDLE			1
146 #define LANE_STATE_WRITE		2
147 #define LANE_STATE_ULPM			3
148 #define LANE_STATE_READ			4
149 #define DATA_LANE_STATE(l, val)		\
150 	(((val) >> (2 + 2 * (l) + ((l) ? 1 : 0))) & GENMASK((l) ? 1 : 2, 0))
151 #define CLK_LANE_STATE_HS		2
152 #define CLK_LANE_STATE(val)		((val) & GENMASK(1, 0))
153 
154 #define DSC_MODE_CTL			0x30
155 #define DSC_MODE_EN			BIT(0)
156 
157 #define DSC_CMD_SEND			0x34
158 #define DSC_SEND_PPS			BIT(0)
159 #define DSC_EXECUTE_QUEUE		BIT(1)
160 
161 #define DSC_PPS_WRDAT			0x38
162 
163 #define DSC_MODE_STS			0x3c
164 #define DSC_PPS_DONE			BIT(1)
165 #define DSC_EXEC_DONE			BIT(2)
166 
167 #define CMD_MODE_CTL			0x70
168 #define IF_LP_EN(x)			BIT(9 + (x))
169 #define IF_VCHAN_ID(x, c)		((c) << ((x) * 2))
170 
171 #define CMD_MODE_CTL2			0x74
172 #define TE_TIMEOUT(x)			((x) << 11)
173 #define FILL_VALUE(x)			((x) << 3)
174 #define ARB_IF_WITH_HIGHEST_PRIORITY(x)	((x) << 1)
175 #define ARB_ROUND_ROBIN_MODE		BIT(0)
176 
177 #define CMD_MODE_STS			0x78
178 #define CMD_MODE_STS_CTL		0x134
179 #define CMD_MODE_STS_CLR		0x154
180 #define CMD_MODE_STS_FLAG		0x174
181 #define ERR_IF_UNDERRUN(x)		BIT(4 + (x))
182 #define ERR_UNWANTED_READ		BIT(3)
183 #define ERR_TE_MISS			BIT(2)
184 #define ERR_NO_TE			BIT(1)
185 #define CSM_RUNNING			BIT(0)
186 
187 #define DIRECT_CMD_SEND			0x80
188 
189 #define DIRECT_CMD_MAIN_SETTINGS	0x84
190 #define TRIGGER_VAL(x)			((x) << 25)
191 #define CMD_LP_EN			BIT(24)
192 #define CMD_SIZE(x)			((x) << 16)
193 #define CMD_VCHAN_ID(x)			((x) << 14)
194 #define CMD_DATATYPE(x)			((x) << 8)
195 #define CMD_LONG			BIT(3)
196 #define WRITE_CMD			0
197 #define READ_CMD			1
198 #define TE_REQ				4
199 #define TRIGGER_REQ			5
200 #define BTA_REQ				6
201 
202 #define DIRECT_CMD_STS			0x88
203 #define DIRECT_CMD_STS_CTL		0x138
204 #define DIRECT_CMD_STS_CLR		0x158
205 #define DIRECT_CMD_STS_FLAG		0x178
206 #define RCVD_ACK_VAL(val)		((val) >> 16)
207 #define RCVD_TRIGGER_VAL(val)		(((val) & GENMASK(14, 11)) >> 11)
208 #define READ_COMPLETED_WITH_ERR		BIT(10)
209 #define BTA_FINISHED			BIT(9)
210 #define BTA_COMPLETED			BIT(8)
211 #define TE_RCVD				BIT(7)
212 #define TRIGGER_RCVD			BIT(6)
213 #define ACK_WITH_ERR_RCVD		BIT(5)
214 #define ACK_RCVD			BIT(4)
215 #define READ_COMPLETED			BIT(3)
216 #define TRIGGER_COMPLETED		BIT(2)
217 #define WRITE_COMPLETED			BIT(1)
218 #define SENDING_CMD			BIT(0)
219 
220 #define DIRECT_CMD_STOP_READ		0x8c
221 
222 #define DIRECT_CMD_WRDATA		0x90
223 
224 #define DIRECT_CMD_FIFO_RST		0x94
225 
226 #define DIRECT_CMD_RDDATA		0xa0
227 
228 #define DIRECT_CMD_RD_PROPS		0xa4
229 #define RD_DCS				BIT(18)
230 #define RD_VCHAN_ID(val)		(((val) >> 16) & GENMASK(1, 0))
231 #define RD_SIZE(val)			((val) & GENMASK(15, 0))
232 
233 #define DIRECT_CMD_RD_STS		0xa8
234 #define DIRECT_CMD_RD_STS_CTL		0x13c
235 #define DIRECT_CMD_RD_STS_CLR		0x15c
236 #define DIRECT_CMD_RD_STS_FLAG		0x17c
237 #define ERR_EOT_WITH_ERR		BIT(8)
238 #define ERR_MISSING_EOT			BIT(7)
239 #define ERR_WRONG_LENGTH		BIT(6)
240 #define ERR_OVERSIZE			BIT(5)
241 #define ERR_RECEIVE			BIT(4)
242 #define ERR_UNDECODABLE			BIT(3)
243 #define ERR_CHECKSUM			BIT(2)
244 #define ERR_UNCORRECTABLE		BIT(1)
245 #define ERR_FIXED			BIT(0)
246 
247 #define VID_MAIN_CTL			0xb0
248 #define VID_IGNORE_MISS_VSYNC		BIT(31)
249 #define VID_FIELD_SW			BIT(28)
250 #define VID_INTERLACED_EN		BIT(27)
251 #define RECOVERY_MODE(x)		((x) << 25)
252 #define RECOVERY_MODE_NEXT_HSYNC	0
253 #define RECOVERY_MODE_NEXT_STOP_POINT	2
254 #define RECOVERY_MODE_NEXT_VSYNC	3
255 #define REG_BLKEOL_MODE(x)		((x) << 23)
256 #define REG_BLKLINE_MODE(x)		((x) << 21)
257 #define REG_BLK_MODE_NULL_PKT		0
258 #define REG_BLK_MODE_BLANKING_PKT	1
259 #define REG_BLK_MODE_LP			2
260 #define SYNC_PULSE_HORIZONTAL		BIT(20)
261 #define SYNC_PULSE_ACTIVE		BIT(19)
262 #define BURST_MODE			BIT(18)
263 #define VID_PIXEL_MODE_MASK		GENMASK(17, 14)
264 #define VID_PIXEL_MODE_RGB565		(0 << 14)
265 #define VID_PIXEL_MODE_RGB666_PACKED	(1 << 14)
266 #define VID_PIXEL_MODE_RGB666		(2 << 14)
267 #define VID_PIXEL_MODE_RGB888		(3 << 14)
268 #define VID_PIXEL_MODE_RGB101010	(4 << 14)
269 #define VID_PIXEL_MODE_RGB121212	(5 << 14)
270 #define VID_PIXEL_MODE_YUV420		(8 << 14)
271 #define VID_PIXEL_MODE_YUV422_PACKED	(9 << 14)
272 #define VID_PIXEL_MODE_YUV422		(10 << 14)
273 #define VID_PIXEL_MODE_YUV422_24B	(11 << 14)
274 #define VID_PIXEL_MODE_DSC_COMP		(12 << 14)
275 #define VID_DATATYPE(x)			((x) << 8)
276 #define VID_VIRTCHAN_ID(iface, x)	((x) << (4 + (iface) * 2))
277 #define STOP_MODE(x)			((x) << 2)
278 #define START_MODE(x)			(x)
279 
280 #define VID_VSIZE1			0xb4
281 #define VFP_LEN(x)			((x) << 12)
282 #define VBP_LEN(x)			((x) << 6)
283 #define VSA_LEN(x)			(x)
284 
285 #define VID_VSIZE2			0xb8
286 #define VACT_LEN(x)			(x)
287 
288 #define VID_HSIZE1			0xc0
289 #define HBP_LEN(x)			((x) << 16)
290 #define HSA_LEN(x)			(x)
291 
292 #define VID_HSIZE2			0xc4
293 #define HFP_LEN(x)			((x) << 16)
294 #define HACT_LEN(x)			(x)
295 
296 #define VID_BLKSIZE1			0xcc
297 #define BLK_EOL_PKT_LEN(x)		((x) << 15)
298 #define BLK_LINE_EVENT_PKT_LEN(x)	(x)
299 
300 #define VID_BLKSIZE2			0xd0
301 #define BLK_LINE_PULSE_PKT_LEN(x)	(x)
302 
303 #define VID_PKT_TIME			0xd8
304 #define BLK_EOL_DURATION(x)		(x)
305 
306 #define VID_DPHY_TIME			0xdc
307 #define REG_WAKEUP_TIME(x)		((x) << 17)
308 #define REG_LINE_DURATION(x)		(x)
309 
310 #define VID_ERR_COLOR1			0xe0
311 #define COL_GREEN(x)			((x) << 12)
312 #define COL_RED(x)			(x)
313 
314 #define VID_ERR_COLOR2			0xe4
315 #define PAD_VAL(x)			((x) << 12)
316 #define COL_BLUE(x)			(x)
317 
318 #define VID_VPOS			0xe8
319 #define LINE_VAL(val)			(((val) & GENMASK(14, 2)) >> 2)
320 #define LINE_POS(val)			((val) & GENMASK(1, 0))
321 
322 #define VID_HPOS			0xec
323 #define HORIZ_VAL(val)			(((val) & GENMASK(17, 3)) >> 3)
324 #define HORIZ_POS(val)			((val) & GENMASK(2, 0))
325 
326 #define VID_MODE_STS			0xf0
327 #define VID_MODE_STS_CTL		0x140
328 #define VID_MODE_STS_CLR		0x160
329 #define VID_MODE_STS_FLAG		0x180
330 #define VSG_RECOVERY			BIT(10)
331 #define ERR_VRS_WRONG_LEN		BIT(9)
332 #define ERR_LONG_READ			BIT(8)
333 #define ERR_LINE_WRITE			BIT(7)
334 #define ERR_BURST_WRITE			BIT(6)
335 #define ERR_SMALL_HEIGHT		BIT(5)
336 #define ERR_SMALL_LEN			BIT(4)
337 #define ERR_MISSING_VSYNC		BIT(3)
338 #define ERR_MISSING_HSYNC		BIT(2)
339 #define ERR_MISSING_DATA		BIT(1)
340 #define VSG_RUNNING			BIT(0)
341 
342 #define VID_VCA_SETTING1		0xf4
343 #define BURST_LP			BIT(16)
344 #define MAX_BURST_LIMIT(x)		(x)
345 
346 #define VID_VCA_SETTING2		0xf8
347 #define MAX_LINE_LIMIT(x)		((x) << 16)
348 #define EXACT_BURST_LIMIT(x)		(x)
349 
350 #define TVG_CTL				0xfc
351 #define TVG_STRIPE_SIZE(x)		((x) << 5)
352 #define TVG_MODE_MASK			GENMASK(4, 3)
353 #define TVG_MODE_SINGLE_COLOR		(0 << 3)
354 #define TVG_MODE_VSTRIPES		(2 << 3)
355 #define TVG_MODE_HSTRIPES		(3 << 3)
356 #define TVG_STOPMODE_MASK		GENMASK(2, 1)
357 #define TVG_STOPMODE_EOF		(0 << 1)
358 #define TVG_STOPMODE_EOL		(1 << 1)
359 #define TVG_STOPMODE_NOW		(2 << 1)
360 #define TVG_RUN				BIT(0)
361 
362 #define TVG_IMG_SIZE			0x100
363 #define TVG_NBLINES(x)			((x) << 16)
364 #define TVG_LINE_SIZE(x)		(x)
365 
366 #define TVG_COLOR1			0x104
367 #define TVG_COL1_GREEN(x)		((x) << 12)
368 #define TVG_COL1_RED(x)			(x)
369 
370 #define TVG_COLOR1_BIS			0x108
371 #define TVG_COL1_BLUE(x)		(x)
372 
373 #define TVG_COLOR2			0x10c
374 #define TVG_COL2_GREEN(x)		((x) << 12)
375 #define TVG_COL2_RED(x)			(x)
376 
377 #define TVG_COLOR2_BIS			0x110
378 #define TVG_COL2_BLUE(x)		(x)
379 
380 #define TVG_STS				0x114
381 #define TVG_STS_CTL			0x144
382 #define TVG_STS_CLR			0x164
383 #define TVG_STS_FLAG			0x184
384 #define TVG_STS_RUNNING			BIT(0)
385 
386 #define STS_CTL_EDGE(e)			((e) << 16)
387 
388 #define DPHY_LANES_MAP			0x198
389 #define DAT_REMAP_CFG(b, l)		((l) << ((b) * 8))
390 
391 #define DPI_IRQ_EN			0x1a0
392 #define DPI_IRQ_CLR			0x1a4
393 #define DPI_IRQ_STS			0x1a8
394 #define PIXEL_BUF_OVERFLOW		BIT(0)
395 
396 #define DPI_CFG				0x1ac
397 #define DPI_CFG_FIFO_DEPTH(x)		((x) >> 16)
398 #define DPI_CFG_FIFO_LEVEL(x)		((x) & GENMASK(15, 0))
399 
400 #define TEST_GENERIC			0x1f0
401 #define TEST_STATUS(x)			((x) >> 16)
402 #define TEST_CTRL(x)			(x)
403 
404 #define ID_REG				0x1fc
405 #define REV_VENDOR_ID(x)		(((x) & GENMASK(31, 20)) >> 20)
406 #define REV_PRODUCT_ID(x)		(((x) & GENMASK(19, 12)) >> 12)
407 #define REV_HW(x)			(((x) & GENMASK(11, 8)) >> 8)
408 #define REV_MAJOR(x)			(((x) & GENMASK(7, 4)) >> 4)
409 #define REV_MINOR(x)			((x) & GENMASK(3, 0))
410 
411 #define DSI_OUTPUT_PORT			0
412 #define DSI_INPUT_PORT(inputid)		(1 + (inputid))
413 
414 #define DSI_HBP_FRAME_OVERHEAD		12
415 #define DSI_HSA_FRAME_OVERHEAD		14
416 #define DSI_HFP_FRAME_OVERHEAD		6
417 #define DSI_HSS_VSS_VSE_FRAME_OVERHEAD	4
418 #define DSI_BLANKING_FRAME_OVERHEAD	6
419 #define DSI_NULL_FRAME_OVERHEAD		6
420 #define DSI_EOT_PKT_SIZE		4
421 
422 #define REG_WAKEUP_TIME_NS		800
423 #define DPHY_PLL_RATE_HZ		108000000
424 
425 /* DPHY registers */
426 #define DPHY_PMA_CMN(reg)		(reg)
427 #define DPHY_PMA_LCLK(reg)		(0x100 + (reg))
428 #define DPHY_PMA_LDATA(lane, reg)	(0x200 + ((lane) * 0x100) + (reg))
429 #define DPHY_PMA_RCLK(reg)		(0x600 + (reg))
430 #define DPHY_PMA_RDATA(lane, reg)	(0x700 + ((lane) * 0x100) + (reg))
431 #define DPHY_PCS(reg)			(0xb00 + (reg))
432 
433 #define DPHY_CMN_SSM			DPHY_PMA_CMN(0x20)
434 #define DPHY_CMN_SSM_EN			BIT(0)
435 #define DPHY_CMN_TX_MODE_EN		BIT(9)
436 
437 #define DPHY_CMN_PWM			DPHY_PMA_CMN(0x40)
438 #define DPHY_CMN_PWM_DIV(x)		((x) << 20)
439 #define DPHY_CMN_PWM_LOW(x)		((x) << 10)
440 #define DPHY_CMN_PWM_HIGH(x)		(x)
441 
442 #define DPHY_CMN_FBDIV			DPHY_PMA_CMN(0x4c)
443 #define DPHY_CMN_FBDIV_VAL(low, high)	(((high) << 11) | ((low) << 22))
444 #define DPHY_CMN_FBDIV_FROM_REG		(BIT(10) | BIT(21))
445 
446 #define DPHY_CMN_OPIPDIV		DPHY_PMA_CMN(0x50)
447 #define DPHY_CMN_IPDIV_FROM_REG		BIT(0)
448 #define DPHY_CMN_IPDIV(x)		((x) << 1)
449 #define DPHY_CMN_OPDIV_FROM_REG		BIT(6)
450 #define DPHY_CMN_OPDIV(x)		((x) << 7)
451 
452 #define DPHY_PSM_CFG			DPHY_PCS(0x4)
453 #define DPHY_PSM_CFG_FROM_REG		BIT(0)
454 #define DPHY_PSM_CLK_DIV(x)		((x) << 1)
455 
456 struct cdns_dsi_output {
457 	struct mipi_dsi_device *dev;
458 	struct drm_panel *panel;
459 	struct drm_bridge *bridge;
460 };
461 
462 enum cdns_dsi_input_id {
463 	CDNS_SDI_INPUT,
464 	CDNS_DPI_INPUT,
465 	CDNS_DSC_INPUT,
466 };
467 
468 struct cdns_dphy_cfg {
469 	u8 pll_ipdiv;
470 	u8 pll_opdiv;
471 	u16 pll_fbdiv;
472 	unsigned long lane_bps;
473 	unsigned int nlanes;
474 };
475 
476 struct cdns_dsi_cfg {
477 	unsigned int hfp;
478 	unsigned int hsa;
479 	unsigned int hbp;
480 	unsigned int hact;
481 	unsigned int htotal;
482 };
483 
484 struct cdns_dphy;
485 
486 enum cdns_dphy_clk_lane_cfg {
487 	DPHY_CLK_CFG_LEFT_DRIVES_ALL = 0,
488 	DPHY_CLK_CFG_LEFT_DRIVES_RIGHT = 1,
489 	DPHY_CLK_CFG_LEFT_DRIVES_LEFT = 2,
490 	DPHY_CLK_CFG_RIGHT_DRIVES_ALL = 3,
491 };
492 
493 struct cdns_dphy_ops {
494 	int (*probe)(struct cdns_dphy *dphy);
495 	void (*remove)(struct cdns_dphy *dphy);
496 	void (*set_psm_div)(struct cdns_dphy *dphy, u8 div);
497 	void (*set_clk_lane_cfg)(struct cdns_dphy *dphy,
498 				 enum cdns_dphy_clk_lane_cfg cfg);
499 	void (*set_pll_cfg)(struct cdns_dphy *dphy,
500 			    const struct cdns_dphy_cfg *cfg);
501 	unsigned long (*get_wakeup_time_ns)(struct cdns_dphy *dphy);
502 };
503 
504 struct cdns_dphy {
505 	struct cdns_dphy_cfg cfg;
506 	void __iomem *regs;
507 	struct clk *psm_clk;
508 	struct clk *pll_ref_clk;
509 	const struct cdns_dphy_ops *ops;
510 };
511 
512 struct cdns_dsi_input {
513 	enum cdns_dsi_input_id id;
514 	struct drm_bridge bridge;
515 };
516 
517 struct cdns_dsi {
518 	struct mipi_dsi_host base;
519 	void __iomem *regs;
520 	struct cdns_dsi_input input;
521 	struct cdns_dsi_output output;
522 	unsigned int direct_cmd_fifo_depth;
523 	unsigned int rx_fifo_depth;
524 	struct completion direct_cmd_comp;
525 	struct clk *dsi_p_clk;
526 	struct reset_control *dsi_p_rst;
527 	struct clk *dsi_sys_clk;
528 	bool link_initialized;
529 	struct cdns_dphy *dphy;
530 };
531 
input_to_dsi(struct cdns_dsi_input * input)532 static inline struct cdns_dsi *input_to_dsi(struct cdns_dsi_input *input)
533 {
534 	return container_of(input, struct cdns_dsi, input);
535 }
536 
to_cdns_dsi(struct mipi_dsi_host * host)537 static inline struct cdns_dsi *to_cdns_dsi(struct mipi_dsi_host *host)
538 {
539 	return container_of(host, struct cdns_dsi, base);
540 }
541 
542 static inline struct cdns_dsi_input *
bridge_to_cdns_dsi_input(struct drm_bridge * bridge)543 bridge_to_cdns_dsi_input(struct drm_bridge *bridge)
544 {
545 	return container_of(bridge, struct cdns_dsi_input, bridge);
546 }
547 
cdns_dsi_get_dphy_pll_cfg(struct cdns_dphy * dphy,struct cdns_dphy_cfg * cfg,unsigned int dpi_htotal,unsigned int dpi_bpp,unsigned int dpi_hz,unsigned int dsi_htotal,unsigned int dsi_nlanes,unsigned int * dsi_hfp_ext)548 static int cdns_dsi_get_dphy_pll_cfg(struct cdns_dphy *dphy,
549 				     struct cdns_dphy_cfg *cfg,
550 				     unsigned int dpi_htotal,
551 				     unsigned int dpi_bpp,
552 				     unsigned int dpi_hz,
553 				     unsigned int dsi_htotal,
554 				     unsigned int dsi_nlanes,
555 				     unsigned int *dsi_hfp_ext)
556 {
557 	u64 dlane_bps, dlane_bps_max, fbdiv, fbdiv_max, adj_dsi_htotal;
558 	unsigned long pll_ref_hz = clk_get_rate(dphy->pll_ref_clk);
559 
560 	memset(cfg, 0, sizeof(*cfg));
561 
562 	cfg->nlanes = dsi_nlanes;
563 
564 	if (pll_ref_hz < 9600000 || pll_ref_hz >= 150000000)
565 		return -EINVAL;
566 	else if (pll_ref_hz < 19200000)
567 		cfg->pll_ipdiv = 1;
568 	else if (pll_ref_hz < 38400000)
569 		cfg->pll_ipdiv = 2;
570 	else if (pll_ref_hz < 76800000)
571 		cfg->pll_ipdiv = 4;
572 	else
573 		cfg->pll_ipdiv = 8;
574 
575 	/*
576 	 * Make sure DSI htotal is aligned on a lane boundary when calculating
577 	 * the expected data rate. This is done by extending HFP in case of
578 	 * misalignment.
579 	 */
580 	adj_dsi_htotal = dsi_htotal;
581 	if (dsi_htotal % dsi_nlanes)
582 		adj_dsi_htotal += dsi_nlanes - (dsi_htotal % dsi_nlanes);
583 
584 	dlane_bps = (u64)dpi_hz * adj_dsi_htotal;
585 
586 	/* data rate in bytes/sec is not an integer, refuse the mode. */
587 	if (do_div(dlane_bps, dsi_nlanes * dpi_htotal))
588 		return -EINVAL;
589 
590 	/* data rate was in bytes/sec, convert to bits/sec. */
591 	dlane_bps *= 8;
592 
593 	if (dlane_bps > 2500000000UL || dlane_bps < 160000000UL)
594 		return -EINVAL;
595 	else if (dlane_bps >= 1250000000)
596 		cfg->pll_opdiv = 1;
597 	else if (dlane_bps >= 630000000)
598 		cfg->pll_opdiv = 2;
599 	else if (dlane_bps >= 320000000)
600 		cfg->pll_opdiv = 4;
601 	else if (dlane_bps >= 160000000)
602 		cfg->pll_opdiv = 8;
603 
604 	/*
605 	 * Allow a deviation of 0.2% on the per-lane data rate to try to
606 	 * recover a potential mismatch between DPI and PPI clks.
607 	 */
608 	dlane_bps_max = dlane_bps + DIV_ROUND_DOWN_ULL(dlane_bps, 500);
609 	fbdiv_max = DIV_ROUND_DOWN_ULL(dlane_bps_max * 2 *
610 				       cfg->pll_opdiv * cfg->pll_ipdiv,
611 				       pll_ref_hz);
612 	fbdiv = DIV_ROUND_UP_ULL(dlane_bps * 2 * cfg->pll_opdiv *
613 				 cfg->pll_ipdiv,
614 				 pll_ref_hz);
615 
616 	/*
617 	 * Iterate over all acceptable fbdiv and try to find an adjusted DSI
618 	 * htotal length providing an exact match.
619 	 *
620 	 * Note that we could do something even trickier by relying on the fact
621 	 * that a new line is not necessarily aligned on a lane boundary, so,
622 	 * by making adj_dsi_htotal non aligned on a dsi_lanes we can improve a
623 	 * bit the precision. With this, the step would be
624 	 *
625 	 *	pll_ref_hz / (2 * opdiv * ipdiv * nlanes)
626 	 *
627 	 * instead of
628 	 *
629 	 *	pll_ref_hz / (2 * opdiv * ipdiv)
630 	 *
631 	 * The drawback of this approach is that we would need to make sure the
632 	 * number or lines is a multiple of the realignment periodicity which is
633 	 * a function of the number of lanes and the original misalignment. For
634 	 * example, for NLANES = 4 and HTOTAL % NLANES = 3, it takes 4 lines
635 	 * to realign on a lane:
636 	 * LINE 0: expected number of bytes, starts emitting first byte of
637 	 *	   LINE 1 on LANE 3
638 	 * LINE 1: expected number of bytes, starts emitting first 2 bytes of
639 	 *	   LINE 2 on LANES 2 and 3
640 	 * LINE 2: expected number of bytes, starts emitting first 3 bytes of
641 	 *	   of LINE 3 on LANES 1, 2 and 3
642 	 * LINE 3: one byte less, now things are realigned on LANE 0 for LINE 4
643 	 *
644 	 * I figured this extra complexity was not worth the benefit, but if
645 	 * someone really has unfixable mismatch, that would be something to
646 	 * investigate.
647 	 */
648 	for (; fbdiv <= fbdiv_max; fbdiv++) {
649 		u32 rem;
650 
651 		adj_dsi_htotal = (u64)fbdiv * pll_ref_hz * dsi_nlanes *
652 				 dpi_htotal;
653 
654 		/*
655 		 * Do the division in 2 steps to avoid an overflow on the
656 		 * divider.
657 		 */
658 		rem = do_div(adj_dsi_htotal, dpi_hz);
659 		if (rem)
660 			continue;
661 
662 		rem = do_div(adj_dsi_htotal,
663 			     cfg->pll_opdiv * cfg->pll_ipdiv * 2 * 8);
664 		if (rem)
665 			continue;
666 
667 		cfg->pll_fbdiv = fbdiv;
668 		*dsi_hfp_ext = adj_dsi_htotal - dsi_htotal;
669 		break;
670 	}
671 
672 	/* No match, let's just reject the display mode. */
673 	if (!cfg->pll_fbdiv)
674 		return -EINVAL;
675 
676 	dlane_bps = DIV_ROUND_DOWN_ULL((u64)dpi_hz * adj_dsi_htotal * 8,
677 				       dsi_nlanes * dpi_htotal);
678 	cfg->lane_bps = dlane_bps;
679 
680 	return 0;
681 }
682 
cdns_dphy_setup_psm(struct cdns_dphy * dphy)683 static int cdns_dphy_setup_psm(struct cdns_dphy *dphy)
684 {
685 	unsigned long psm_clk_hz = clk_get_rate(dphy->psm_clk);
686 	unsigned long psm_div;
687 
688 	if (!psm_clk_hz || psm_clk_hz > 100000000)
689 		return -EINVAL;
690 
691 	psm_div = DIV_ROUND_CLOSEST(psm_clk_hz, 1000000);
692 	if (dphy->ops->set_psm_div)
693 		dphy->ops->set_psm_div(dphy, psm_div);
694 
695 	return 0;
696 }
697 
cdns_dphy_set_clk_lane_cfg(struct cdns_dphy * dphy,enum cdns_dphy_clk_lane_cfg cfg)698 static void cdns_dphy_set_clk_lane_cfg(struct cdns_dphy *dphy,
699 				       enum cdns_dphy_clk_lane_cfg cfg)
700 {
701 	if (dphy->ops->set_clk_lane_cfg)
702 		dphy->ops->set_clk_lane_cfg(dphy, cfg);
703 }
704 
cdns_dphy_set_pll_cfg(struct cdns_dphy * dphy,const struct cdns_dphy_cfg * cfg)705 static void cdns_dphy_set_pll_cfg(struct cdns_dphy *dphy,
706 				  const struct cdns_dphy_cfg *cfg)
707 {
708 	if (dphy->ops->set_pll_cfg)
709 		dphy->ops->set_pll_cfg(dphy, cfg);
710 }
711 
cdns_dphy_get_wakeup_time_ns(struct cdns_dphy * dphy)712 static unsigned long cdns_dphy_get_wakeup_time_ns(struct cdns_dphy *dphy)
713 {
714 	return dphy->ops->get_wakeup_time_ns(dphy);
715 }
716 
dpi_to_dsi_timing(unsigned int dpi_timing,unsigned int dpi_bpp,unsigned int dsi_pkt_overhead)717 static unsigned int dpi_to_dsi_timing(unsigned int dpi_timing,
718 				      unsigned int dpi_bpp,
719 				      unsigned int dsi_pkt_overhead)
720 {
721 	unsigned int dsi_timing = DIV_ROUND_UP(dpi_timing * dpi_bpp, 8);
722 
723 	if (dsi_timing < dsi_pkt_overhead)
724 		dsi_timing = 0;
725 	else
726 		dsi_timing -= dsi_pkt_overhead;
727 
728 	return dsi_timing;
729 }
730 
cdns_dsi_mode2cfg(struct cdns_dsi * dsi,const struct drm_display_mode * mode,struct cdns_dsi_cfg * dsi_cfg,struct cdns_dphy_cfg * dphy_cfg,bool mode_valid_check)731 static int cdns_dsi_mode2cfg(struct cdns_dsi *dsi,
732 			     const struct drm_display_mode *mode,
733 			     struct cdns_dsi_cfg *dsi_cfg,
734 			     struct cdns_dphy_cfg *dphy_cfg,
735 			     bool mode_valid_check)
736 {
737 	unsigned long dsi_htotal = 0, dsi_hss_hsa_hse_hbp = 0;
738 	struct cdns_dsi_output *output = &dsi->output;
739 	unsigned int dsi_hfp_ext = 0, dpi_hfp, tmp;
740 	bool sync_pulse = false;
741 	int bpp, nlanes, ret;
742 
743 	memset(dsi_cfg, 0, sizeof(*dsi_cfg));
744 
745 	if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
746 		sync_pulse = true;
747 
748 	bpp = mipi_dsi_pixel_format_to_bpp(output->dev->format);
749 	nlanes = output->dev->lanes;
750 
751 	if (mode_valid_check)
752 		tmp = mode->htotal -
753 		      (sync_pulse ? mode->hsync_end : mode->hsync_start);
754 	else
755 		tmp = mode->crtc_htotal -
756 		      (sync_pulse ?
757 		       mode->crtc_hsync_end : mode->crtc_hsync_start);
758 
759 	dsi_cfg->hbp = dpi_to_dsi_timing(tmp, bpp, DSI_HBP_FRAME_OVERHEAD);
760 	dsi_htotal += dsi_cfg->hbp + DSI_HBP_FRAME_OVERHEAD;
761 	dsi_hss_hsa_hse_hbp += dsi_cfg->hbp + DSI_HBP_FRAME_OVERHEAD;
762 
763 	if (sync_pulse) {
764 		if (mode_valid_check)
765 			tmp = mode->hsync_end - mode->hsync_start;
766 		else
767 			tmp = mode->crtc_hsync_end - mode->crtc_hsync_start;
768 
769 		dsi_cfg->hsa = dpi_to_dsi_timing(tmp, bpp,
770 						 DSI_HSA_FRAME_OVERHEAD);
771 		dsi_htotal += dsi_cfg->hsa + DSI_HSA_FRAME_OVERHEAD;
772 		dsi_hss_hsa_hse_hbp += dsi_cfg->hsa + DSI_HSA_FRAME_OVERHEAD;
773 	}
774 
775 	dsi_cfg->hact = dpi_to_dsi_timing(mode_valid_check ?
776 					  mode->hdisplay : mode->crtc_hdisplay,
777 					  bpp, 0);
778 	dsi_htotal += dsi_cfg->hact;
779 
780 	if (mode_valid_check)
781 		dpi_hfp = mode->hsync_start - mode->hdisplay;
782 	else
783 		dpi_hfp = mode->crtc_hsync_start - mode->crtc_hdisplay;
784 
785 	dsi_cfg->hfp = dpi_to_dsi_timing(dpi_hfp, bpp, DSI_HFP_FRAME_OVERHEAD);
786 	dsi_htotal += dsi_cfg->hfp + DSI_HFP_FRAME_OVERHEAD;
787 
788 	if (mode_valid_check)
789 		ret = cdns_dsi_get_dphy_pll_cfg(dsi->dphy, dphy_cfg,
790 						mode->htotal, bpp,
791 						mode->clock * 1000,
792 						dsi_htotal, nlanes,
793 						&dsi_hfp_ext);
794 	else
795 		ret = cdns_dsi_get_dphy_pll_cfg(dsi->dphy, dphy_cfg,
796 						mode->crtc_htotal, bpp,
797 						mode->crtc_clock * 1000,
798 						dsi_htotal, nlanes,
799 						&dsi_hfp_ext);
800 
801 	if (ret)
802 		return ret;
803 
804 	dsi_cfg->hfp += dsi_hfp_ext;
805 	dsi_htotal += dsi_hfp_ext;
806 	dsi_cfg->htotal = dsi_htotal;
807 
808 	/*
809 	 * Make sure DPI(HFP) > DSI(HSS+HSA+HSE+HBP) to guarantee that the FIFO
810 	 * is empty before we start a receiving a new line on the DPI
811 	 * interface.
812 	 */
813 	if ((u64)dphy_cfg->lane_bps * dpi_hfp * nlanes <
814 	    (u64)dsi_hss_hsa_hse_hbp *
815 	    (mode_valid_check ? mode->clock : mode->crtc_clock) * 1000)
816 		return -EINVAL;
817 
818 	return 0;
819 }
820 
cdns_dsi_bridge_attach(struct drm_bridge * bridge)821 static int cdns_dsi_bridge_attach(struct drm_bridge *bridge)
822 {
823 	struct cdns_dsi_input *input = bridge_to_cdns_dsi_input(bridge);
824 	struct cdns_dsi *dsi = input_to_dsi(input);
825 	struct cdns_dsi_output *output = &dsi->output;
826 
827 	if (!drm_core_check_feature(bridge->dev, DRIVER_ATOMIC)) {
828 		dev_err(dsi->base.dev,
829 			"cdns-dsi driver is only compatible with DRM devices supporting atomic updates");
830 		return -ENOTSUPP;
831 	}
832 
833 	return drm_bridge_attach(bridge->encoder, output->bridge, bridge);
834 }
835 
836 static enum drm_mode_status
cdns_dsi_bridge_mode_valid(struct drm_bridge * bridge,const struct drm_display_mode * mode)837 cdns_dsi_bridge_mode_valid(struct drm_bridge *bridge,
838 			   const struct drm_display_mode *mode)
839 {
840 	struct cdns_dsi_input *input = bridge_to_cdns_dsi_input(bridge);
841 	struct cdns_dsi *dsi = input_to_dsi(input);
842 	struct cdns_dsi_output *output = &dsi->output;
843 	struct cdns_dphy_cfg dphy_cfg;
844 	struct cdns_dsi_cfg dsi_cfg;
845 	int bpp, nlanes, ret;
846 
847 	/*
848 	 * VFP_DSI should be less than VFP_DPI and VFP_DSI should be at
849 	 * least 1.
850 	 */
851 	if (mode->vtotal - mode->vsync_end < 2)
852 		return MODE_V_ILLEGAL;
853 
854 	/* VSA_DSI = VSA_DPI and must be at least 2. */
855 	if (mode->vsync_end - mode->vsync_start < 2)
856 		return MODE_V_ILLEGAL;
857 
858 	/* HACT must be 32-bits aligned. */
859 	bpp = mipi_dsi_pixel_format_to_bpp(output->dev->format);
860 	if ((mode->hdisplay * bpp) % 32)
861 		return MODE_H_ILLEGAL;
862 
863 	nlanes = output->dev->lanes;
864 
865 	ret = cdns_dsi_mode2cfg(dsi, mode, &dsi_cfg, &dphy_cfg, true);
866 	if (ret)
867 		return MODE_CLOCK_RANGE;
868 
869 	return MODE_OK;
870 }
871 
cdns_dsi_bridge_disable(struct drm_bridge * bridge)872 static void cdns_dsi_bridge_disable(struct drm_bridge *bridge)
873 {
874 	struct cdns_dsi_input *input = bridge_to_cdns_dsi_input(bridge);
875 	struct cdns_dsi *dsi = input_to_dsi(input);
876 	u32 val;
877 
878 	val = readl(dsi->regs + MCTL_MAIN_DATA_CTL);
879 	val &= ~(IF_VID_SELECT_MASK | IF_VID_MODE | VID_EN | HOST_EOT_GEN |
880 		 DISP_EOT_GEN);
881 	writel(val, dsi->regs + MCTL_MAIN_DATA_CTL);
882 
883 	val = readl(dsi->regs + MCTL_MAIN_EN) & ~IF_EN(input->id);
884 	writel(val, dsi->regs + MCTL_MAIN_EN);
885 	pm_runtime_put(dsi->base.dev);
886 }
887 
cdns_dsi_hs_init(struct cdns_dsi * dsi,const struct cdns_dphy_cfg * dphy_cfg)888 static void cdns_dsi_hs_init(struct cdns_dsi *dsi,
889 			     const struct cdns_dphy_cfg *dphy_cfg)
890 {
891 	u32 status;
892 
893 	/*
894 	 * Power all internal DPHY blocks down and maintain their reset line
895 	 * asserted before changing the DPHY config.
896 	 */
897 	writel(DPHY_CMN_PSO | DPHY_PLL_PSO | DPHY_ALL_D_PDN | DPHY_C_PDN |
898 	       DPHY_CMN_PDN | DPHY_PLL_PDN,
899 	       dsi->regs + MCTL_DPHY_CFG0);
900 
901 	/*
902 	 * Configure the internal PSM clk divider so that the DPHY has a
903 	 * 1MHz clk (or something close).
904 	 */
905 	WARN_ON_ONCE(cdns_dphy_setup_psm(dsi->dphy));
906 
907 	/*
908 	 * Configure attach clk lanes to data lanes: the DPHY has 2 clk lanes
909 	 * and 8 data lanes, each clk lane can be attache different set of
910 	 * data lanes. The 2 groups are named 'left' and 'right', so here we
911 	 * just say that we want the 'left' clk lane to drive the 'left' data
912 	 * lanes.
913 	 */
914 	cdns_dphy_set_clk_lane_cfg(dsi->dphy, DPHY_CLK_CFG_LEFT_DRIVES_LEFT);
915 
916 	/*
917 	 * Configure the DPHY PLL that will be used to generate the TX byte
918 	 * clk.
919 	 */
920 	cdns_dphy_set_pll_cfg(dsi->dphy, dphy_cfg);
921 
922 	/* Start TX state machine. */
923 	writel(DPHY_CMN_SSM_EN | DPHY_CMN_TX_MODE_EN,
924 	       dsi->dphy->regs + DPHY_CMN_SSM);
925 
926 	/* Activate the PLL and wait until it's locked. */
927 	writel(PLL_LOCKED, dsi->regs + MCTL_MAIN_STS_CLR);
928 	writel(DPHY_CMN_PSO | DPHY_ALL_D_PDN | DPHY_C_PDN | DPHY_CMN_PDN,
929 	       dsi->regs + MCTL_DPHY_CFG0);
930 	WARN_ON_ONCE(readl_poll_timeout(dsi->regs + MCTL_MAIN_STS, status,
931 					status & PLL_LOCKED, 100, 100));
932 	/* De-assert data and clock reset lines. */
933 	writel(DPHY_CMN_PSO | DPHY_ALL_D_PDN | DPHY_C_PDN | DPHY_CMN_PDN |
934 	       DPHY_D_RSTB(dphy_cfg->nlanes) | DPHY_C_RSTB,
935 	       dsi->regs + MCTL_DPHY_CFG0);
936 }
937 
cdns_dsi_init_link(struct cdns_dsi * dsi)938 static void cdns_dsi_init_link(struct cdns_dsi *dsi)
939 {
940 	struct cdns_dsi_output *output = &dsi->output;
941 	unsigned long sysclk_period, ulpout;
942 	u32 val;
943 	int i;
944 
945 	if (dsi->link_initialized)
946 		return;
947 
948 	val = 0;
949 	for (i = 1; i < output->dev->lanes; i++)
950 		val |= DATA_LANE_EN(i);
951 
952 	if (!(output->dev->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS))
953 		val |= CLK_CONTINUOUS;
954 
955 	writel(val, dsi->regs + MCTL_MAIN_PHY_CTL);
956 
957 	/* ULPOUT should be set to 1ms and is expressed in sysclk cycles. */
958 	sysclk_period = NSEC_PER_SEC / clk_get_rate(dsi->dsi_sys_clk);
959 	ulpout = DIV_ROUND_UP(NSEC_PER_MSEC, sysclk_period);
960 	writel(CLK_LANE_ULPOUT_TIME(ulpout) | DATA_LANE_ULPOUT_TIME(ulpout),
961 	       dsi->regs + MCTL_ULPOUT_TIME);
962 
963 	writel(LINK_EN, dsi->regs + MCTL_MAIN_DATA_CTL);
964 
965 	val = CLK_LANE_EN | PLL_START;
966 	for (i = 0; i < output->dev->lanes; i++)
967 		val |= DATA_LANE_START(i);
968 
969 	writel(val, dsi->regs + MCTL_MAIN_EN);
970 
971 	dsi->link_initialized = true;
972 }
973 
cdns_dsi_bridge_enable(struct drm_bridge * bridge)974 static void cdns_dsi_bridge_enable(struct drm_bridge *bridge)
975 {
976 	struct cdns_dsi_input *input = bridge_to_cdns_dsi_input(bridge);
977 	struct cdns_dsi *dsi = input_to_dsi(input);
978 	struct cdns_dsi_output *output = &dsi->output;
979 	struct drm_display_mode *mode;
980 	struct cdns_dphy_cfg dphy_cfg;
981 	unsigned long tx_byte_period;
982 	struct cdns_dsi_cfg dsi_cfg;
983 	u32 tmp, reg_wakeup, div;
984 	int bpp, nlanes;
985 
986 	if (WARN_ON(pm_runtime_get_sync(dsi->base.dev) < 0))
987 		return;
988 
989 	mode = &bridge->encoder->crtc->state->adjusted_mode;
990 	bpp = mipi_dsi_pixel_format_to_bpp(output->dev->format);
991 	nlanes = output->dev->lanes;
992 
993 	WARN_ON_ONCE(cdns_dsi_mode2cfg(dsi, mode, &dsi_cfg, &dphy_cfg, false));
994 
995 	cdns_dsi_hs_init(dsi, &dphy_cfg);
996 	cdns_dsi_init_link(dsi);
997 
998 	writel(HBP_LEN(dsi_cfg.hbp) | HSA_LEN(dsi_cfg.hsa),
999 	       dsi->regs + VID_HSIZE1);
1000 	writel(HFP_LEN(dsi_cfg.hfp) | HACT_LEN(dsi_cfg.hact),
1001 	       dsi->regs + VID_HSIZE2);
1002 
1003 	writel(VBP_LEN(mode->crtc_vtotal - mode->crtc_vsync_end - 1) |
1004 	       VFP_LEN(mode->crtc_vsync_start - mode->crtc_vdisplay) |
1005 	       VSA_LEN(mode->crtc_vsync_end - mode->crtc_vsync_start + 1),
1006 	       dsi->regs + VID_VSIZE1);
1007 	writel(mode->crtc_vdisplay, dsi->regs + VID_VSIZE2);
1008 
1009 	tmp = dsi_cfg.htotal -
1010 	      (dsi_cfg.hsa + DSI_BLANKING_FRAME_OVERHEAD +
1011 	       DSI_HSA_FRAME_OVERHEAD);
1012 	writel(BLK_LINE_PULSE_PKT_LEN(tmp), dsi->regs + VID_BLKSIZE2);
1013 	if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
1014 		writel(MAX_LINE_LIMIT(tmp - DSI_NULL_FRAME_OVERHEAD),
1015 		       dsi->regs + VID_VCA_SETTING2);
1016 
1017 	tmp = dsi_cfg.htotal -
1018 	      (DSI_HSS_VSS_VSE_FRAME_OVERHEAD + DSI_BLANKING_FRAME_OVERHEAD);
1019 	writel(BLK_LINE_EVENT_PKT_LEN(tmp), dsi->regs + VID_BLKSIZE1);
1020 	if (!(output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE))
1021 		writel(MAX_LINE_LIMIT(tmp - DSI_NULL_FRAME_OVERHEAD),
1022 		       dsi->regs + VID_VCA_SETTING2);
1023 
1024 	tmp = DIV_ROUND_UP(dsi_cfg.htotal, nlanes) -
1025 	      DIV_ROUND_UP(dsi_cfg.hsa, nlanes);
1026 
1027 	if (!(output->dev->mode_flags & MIPI_DSI_MODE_EOT_PACKET))
1028 		tmp -= DIV_ROUND_UP(DSI_EOT_PKT_SIZE, nlanes);
1029 
1030 	tx_byte_period = DIV_ROUND_DOWN_ULL((u64)NSEC_PER_SEC * 8,
1031 					    dphy_cfg.lane_bps);
1032 	reg_wakeup = cdns_dphy_get_wakeup_time_ns(dsi->dphy) /
1033 		     tx_byte_period;
1034 	writel(REG_WAKEUP_TIME(reg_wakeup) | REG_LINE_DURATION(tmp),
1035 	       dsi->regs + VID_DPHY_TIME);
1036 
1037 	/*
1038 	 * HSTX and LPRX timeouts are both expressed in TX byte clk cycles and
1039 	 * both should be set to at least the time it takes to transmit a
1040 	 * frame.
1041 	 */
1042 	tmp = NSEC_PER_SEC / drm_mode_vrefresh(mode);
1043 	tmp /= tx_byte_period;
1044 
1045 	for (div = 0; div <= CLK_DIV_MAX; div++) {
1046 		if (tmp <= HSTX_TIMEOUT_MAX)
1047 			break;
1048 
1049 		tmp >>= 1;
1050 	}
1051 
1052 	if (tmp > HSTX_TIMEOUT_MAX)
1053 		tmp = HSTX_TIMEOUT_MAX;
1054 
1055 	writel(CLK_DIV(div) | HSTX_TIMEOUT(tmp),
1056 	       dsi->regs + MCTL_DPHY_TIMEOUT1);
1057 
1058 	writel(LPRX_TIMEOUT(tmp), dsi->regs + MCTL_DPHY_TIMEOUT2);
1059 
1060 	if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO) {
1061 		switch (output->dev->format) {
1062 		case MIPI_DSI_FMT_RGB888:
1063 			tmp = VID_PIXEL_MODE_RGB888 |
1064 			      VID_DATATYPE(MIPI_DSI_PACKED_PIXEL_STREAM_24);
1065 			break;
1066 
1067 		case MIPI_DSI_FMT_RGB666:
1068 			tmp = VID_PIXEL_MODE_RGB666 |
1069 			      VID_DATATYPE(MIPI_DSI_PIXEL_STREAM_3BYTE_18);
1070 			break;
1071 
1072 		case MIPI_DSI_FMT_RGB666_PACKED:
1073 			tmp = VID_PIXEL_MODE_RGB666_PACKED |
1074 			      VID_DATATYPE(MIPI_DSI_PACKED_PIXEL_STREAM_18);
1075 			break;
1076 
1077 		case MIPI_DSI_FMT_RGB565:
1078 			tmp = VID_PIXEL_MODE_RGB565 |
1079 			      VID_DATATYPE(MIPI_DSI_PACKED_PIXEL_STREAM_16);
1080 			break;
1081 
1082 		default:
1083 			dev_err(dsi->base.dev, "Unsupported DSI format\n");
1084 			return;
1085 		}
1086 
1087 		if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
1088 			tmp |= SYNC_PULSE_ACTIVE | SYNC_PULSE_HORIZONTAL;
1089 
1090 		tmp |= REG_BLKLINE_MODE(REG_BLK_MODE_BLANKING_PKT) |
1091 		       REG_BLKEOL_MODE(REG_BLK_MODE_BLANKING_PKT) |
1092 		       RECOVERY_MODE(RECOVERY_MODE_NEXT_HSYNC) |
1093 		       VID_IGNORE_MISS_VSYNC;
1094 
1095 		writel(tmp, dsi->regs + VID_MAIN_CTL);
1096 	}
1097 
1098 	tmp = readl(dsi->regs + MCTL_MAIN_DATA_CTL);
1099 	tmp &= ~(IF_VID_SELECT_MASK | HOST_EOT_GEN | IF_VID_MODE);
1100 
1101 	if (!(output->dev->mode_flags & MIPI_DSI_MODE_EOT_PACKET))
1102 		tmp |= HOST_EOT_GEN;
1103 
1104 	if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO)
1105 		tmp |= IF_VID_MODE | IF_VID_SELECT(input->id) | VID_EN;
1106 
1107 	writel(tmp, dsi->regs + MCTL_MAIN_DATA_CTL);
1108 
1109 	tmp = readl(dsi->regs + MCTL_MAIN_EN) | IF_EN(input->id);
1110 	writel(tmp, dsi->regs + MCTL_MAIN_EN);
1111 }
1112 
1113 static const struct drm_bridge_funcs cdns_dsi_bridge_funcs = {
1114 	.attach = cdns_dsi_bridge_attach,
1115 	.mode_valid = cdns_dsi_bridge_mode_valid,
1116 	.disable = cdns_dsi_bridge_disable,
1117 	.enable = cdns_dsi_bridge_enable,
1118 };
1119 
cdns_dsi_attach(struct mipi_dsi_host * host,struct mipi_dsi_device * dev)1120 static int cdns_dsi_attach(struct mipi_dsi_host *host,
1121 			   struct mipi_dsi_device *dev)
1122 {
1123 	struct cdns_dsi *dsi = to_cdns_dsi(host);
1124 	struct cdns_dsi_output *output = &dsi->output;
1125 	struct cdns_dsi_input *input = &dsi->input;
1126 	struct drm_bridge *bridge;
1127 	struct drm_panel *panel;
1128 	struct device_node *np;
1129 	int ret;
1130 
1131 	/*
1132 	 * We currently do not support connecting several DSI devices to the
1133 	 * same host. In order to support that we'd need the DRM bridge
1134 	 * framework to allow dynamic reconfiguration of the bridge chain.
1135 	 */
1136 	if (output->dev)
1137 		return -EBUSY;
1138 
1139 	/* We do not support burst mode yet. */
1140 	if (dev->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
1141 		return -ENOTSUPP;
1142 
1143 	/*
1144 	 * The host <-> device link might be described using an OF-graph
1145 	 * representation, in this case we extract the device of_node from
1146 	 * this representation, otherwise we use dsidev->dev.of_node which
1147 	 * should have been filled by the core.
1148 	 */
1149 	np = of_graph_get_remote_node(dsi->base.dev->of_node, DSI_OUTPUT_PORT,
1150 				      dev->channel);
1151 	if (!np)
1152 		np = of_node_get(dev->dev.of_node);
1153 
1154 	panel = of_drm_find_panel(np);
1155 	if (!IS_ERR(panel)) {
1156 		bridge = drm_panel_bridge_add(panel, DRM_MODE_CONNECTOR_DSI);
1157 	} else {
1158 		bridge = of_drm_find_bridge(dev->dev.of_node);
1159 		if (!bridge)
1160 			bridge = ERR_PTR(-EINVAL);
1161 	}
1162 
1163 	of_node_put(np);
1164 
1165 	if (IS_ERR(bridge)) {
1166 		ret = PTR_ERR(bridge);
1167 		dev_err(host->dev, "failed to add DSI device %s (err = %d)",
1168 			dev->name, ret);
1169 		return ret;
1170 	}
1171 
1172 	output->dev = dev;
1173 	output->bridge = bridge;
1174 	output->panel = panel;
1175 
1176 	/*
1177 	 * The DSI output has been properly configured, we can now safely
1178 	 * register the input to the bridge framework so that it can take place
1179 	 * in a display pipeline.
1180 	 */
1181 	drm_bridge_add(&input->bridge);
1182 
1183 	return 0;
1184 }
1185 
cdns_dsi_detach(struct mipi_dsi_host * host,struct mipi_dsi_device * dev)1186 static int cdns_dsi_detach(struct mipi_dsi_host *host,
1187 			   struct mipi_dsi_device *dev)
1188 {
1189 	struct cdns_dsi *dsi = to_cdns_dsi(host);
1190 	struct cdns_dsi_output *output = &dsi->output;
1191 	struct cdns_dsi_input *input = &dsi->input;
1192 
1193 	drm_bridge_remove(&input->bridge);
1194 	if (output->panel)
1195 		drm_panel_bridge_remove(output->bridge);
1196 
1197 	return 0;
1198 }
1199 
cdns_dsi_interrupt(int irq,void * data)1200 static irqreturn_t cdns_dsi_interrupt(int irq, void *data)
1201 {
1202 	struct cdns_dsi *dsi = data;
1203 	irqreturn_t ret = IRQ_NONE;
1204 	u32 flag, ctl;
1205 
1206 	flag = readl(dsi->regs + DIRECT_CMD_STS_FLAG);
1207 	if (flag) {
1208 		ctl = readl(dsi->regs + DIRECT_CMD_STS_CTL);
1209 		ctl &= ~flag;
1210 		writel(ctl, dsi->regs + DIRECT_CMD_STS_CTL);
1211 		complete(&dsi->direct_cmd_comp);
1212 		ret = IRQ_HANDLED;
1213 	}
1214 
1215 	return ret;
1216 }
1217 
cdns_dsi_transfer(struct mipi_dsi_host * host,const struct mipi_dsi_msg * msg)1218 static ssize_t cdns_dsi_transfer(struct mipi_dsi_host *host,
1219 				 const struct mipi_dsi_msg *msg)
1220 {
1221 	struct cdns_dsi *dsi = to_cdns_dsi(host);
1222 	u32 cmd, sts, val, wait = WRITE_COMPLETED, ctl = 0;
1223 	struct mipi_dsi_packet packet;
1224 	int ret, i, tx_len, rx_len;
1225 
1226 	ret = pm_runtime_get_sync(host->dev);
1227 	if (ret < 0)
1228 		return ret;
1229 
1230 	cdns_dsi_init_link(dsi);
1231 
1232 	ret = mipi_dsi_create_packet(&packet, msg);
1233 	if (ret)
1234 		goto out;
1235 
1236 	tx_len = msg->tx_buf ? msg->tx_len : 0;
1237 	rx_len = msg->rx_buf ? msg->rx_len : 0;
1238 
1239 	/* For read operations, the maximum TX len is 2. */
1240 	if (rx_len && tx_len > 2) {
1241 		ret = -ENOTSUPP;
1242 		goto out;
1243 	}
1244 
1245 	/* TX len is limited by the CMD FIFO depth. */
1246 	if (tx_len > dsi->direct_cmd_fifo_depth) {
1247 		ret = -ENOTSUPP;
1248 		goto out;
1249 	}
1250 
1251 	/* RX len is limited by the RX FIFO depth. */
1252 	if (rx_len > dsi->rx_fifo_depth) {
1253 		ret = -ENOTSUPP;
1254 		goto out;
1255 	}
1256 
1257 	cmd = CMD_SIZE(tx_len) | CMD_VCHAN_ID(msg->channel) |
1258 	      CMD_DATATYPE(msg->type);
1259 
1260 	if (msg->flags & MIPI_DSI_MSG_USE_LPM)
1261 		cmd |= CMD_LP_EN;
1262 
1263 	if (mipi_dsi_packet_format_is_long(msg->type))
1264 		cmd |= CMD_LONG;
1265 
1266 	if (rx_len) {
1267 		cmd |= READ_CMD;
1268 		wait = READ_COMPLETED_WITH_ERR | READ_COMPLETED;
1269 		ctl = READ_EN | BTA_EN;
1270 	} else if (msg->flags & MIPI_DSI_MSG_REQ_ACK) {
1271 		cmd |= BTA_REQ;
1272 		wait = ACK_WITH_ERR_RCVD | ACK_RCVD;
1273 		ctl = BTA_EN;
1274 	}
1275 
1276 	writel(readl(dsi->regs + MCTL_MAIN_DATA_CTL) | ctl,
1277 	       dsi->regs + MCTL_MAIN_DATA_CTL);
1278 
1279 	writel(cmd, dsi->regs + DIRECT_CMD_MAIN_SETTINGS);
1280 
1281 	for (i = 0; i < tx_len; i += 4) {
1282 		const u8 *buf = msg->tx_buf;
1283 		int j;
1284 
1285 		val = 0;
1286 		for (j = 0; j < 4 && j + i < tx_len; j++)
1287 			val |= (u32)buf[i + j] << (8 * j);
1288 
1289 		writel(val, dsi->regs + DIRECT_CMD_WRDATA);
1290 	}
1291 
1292 	/* Clear status flags before sending the command. */
1293 	writel(wait, dsi->regs + DIRECT_CMD_STS_CLR);
1294 	writel(wait, dsi->regs + DIRECT_CMD_STS_CTL);
1295 	reinit_completion(&dsi->direct_cmd_comp);
1296 	writel(0, dsi->regs + DIRECT_CMD_SEND);
1297 
1298 	wait_for_completion_timeout(&dsi->direct_cmd_comp,
1299 				    msecs_to_jiffies(1000));
1300 
1301 	sts = readl(dsi->regs + DIRECT_CMD_STS);
1302 	writel(wait, dsi->regs + DIRECT_CMD_STS_CLR);
1303 	writel(0, dsi->regs + DIRECT_CMD_STS_CTL);
1304 
1305 	writel(readl(dsi->regs + MCTL_MAIN_DATA_CTL) & ~ctl,
1306 	       dsi->regs + MCTL_MAIN_DATA_CTL);
1307 
1308 	/* We did not receive the events we were waiting for. */
1309 	if (!(sts & wait)) {
1310 		ret = -ETIMEDOUT;
1311 		goto out;
1312 	}
1313 
1314 	/* 'READ' or 'WRITE with ACK' failed. */
1315 	if (sts & (READ_COMPLETED_WITH_ERR | ACK_WITH_ERR_RCVD)) {
1316 		ret = -EIO;
1317 		goto out;
1318 	}
1319 
1320 	for (i = 0; i < rx_len; i += 4) {
1321 		u8 *buf = msg->rx_buf;
1322 		int j;
1323 
1324 		val = readl(dsi->regs + DIRECT_CMD_RDDATA);
1325 		for (j = 0; j < 4 && j + i < rx_len; j++)
1326 			buf[i + j] = val >> (8 * j);
1327 	}
1328 
1329 out:
1330 	pm_runtime_put(host->dev);
1331 	return ret;
1332 }
1333 
1334 static const struct mipi_dsi_host_ops cdns_dsi_ops = {
1335 	.attach = cdns_dsi_attach,
1336 	.detach = cdns_dsi_detach,
1337 	.transfer = cdns_dsi_transfer,
1338 };
1339 
cdns_dsi_resume(struct device * dev)1340 static int __maybe_unused cdns_dsi_resume(struct device *dev)
1341 {
1342 	struct cdns_dsi *dsi = dev_get_drvdata(dev);
1343 
1344 	reset_control_deassert(dsi->dsi_p_rst);
1345 	clk_prepare_enable(dsi->dsi_p_clk);
1346 	clk_prepare_enable(dsi->dsi_sys_clk);
1347 	clk_prepare_enable(dsi->dphy->psm_clk);
1348 	clk_prepare_enable(dsi->dphy->pll_ref_clk);
1349 
1350 	return 0;
1351 }
1352 
cdns_dsi_suspend(struct device * dev)1353 static int __maybe_unused cdns_dsi_suspend(struct device *dev)
1354 {
1355 	struct cdns_dsi *dsi = dev_get_drvdata(dev);
1356 
1357 	clk_disable_unprepare(dsi->dphy->pll_ref_clk);
1358 	clk_disable_unprepare(dsi->dphy->psm_clk);
1359 	clk_disable_unprepare(dsi->dsi_sys_clk);
1360 	clk_disable_unprepare(dsi->dsi_p_clk);
1361 	reset_control_assert(dsi->dsi_p_rst);
1362 	dsi->link_initialized = false;
1363 	return 0;
1364 }
1365 
1366 static UNIVERSAL_DEV_PM_OPS(cdns_dsi_pm_ops, cdns_dsi_suspend, cdns_dsi_resume,
1367 			    NULL);
1368 
cdns_dphy_ref_get_wakeup_time_ns(struct cdns_dphy * dphy)1369 static unsigned long cdns_dphy_ref_get_wakeup_time_ns(struct cdns_dphy *dphy)
1370 {
1371 	/* Default wakeup time is 800 ns (in a simulated environment). */
1372 	return 800;
1373 }
1374 
cdns_dphy_ref_set_pll_cfg(struct cdns_dphy * dphy,const struct cdns_dphy_cfg * cfg)1375 static void cdns_dphy_ref_set_pll_cfg(struct cdns_dphy *dphy,
1376 				      const struct cdns_dphy_cfg *cfg)
1377 {
1378 	u32 fbdiv_low, fbdiv_high;
1379 
1380 	fbdiv_low = (cfg->pll_fbdiv / 4) - 2;
1381 	fbdiv_high = cfg->pll_fbdiv - fbdiv_low - 2;
1382 
1383 	writel(DPHY_CMN_IPDIV_FROM_REG | DPHY_CMN_OPDIV_FROM_REG |
1384 	       DPHY_CMN_IPDIV(cfg->pll_ipdiv) |
1385 	       DPHY_CMN_OPDIV(cfg->pll_opdiv),
1386 	       dphy->regs + DPHY_CMN_OPIPDIV);
1387 	writel(DPHY_CMN_FBDIV_FROM_REG |
1388 	       DPHY_CMN_FBDIV_VAL(fbdiv_low, fbdiv_high),
1389 	       dphy->regs + DPHY_CMN_FBDIV);
1390 	writel(DPHY_CMN_PWM_HIGH(6) | DPHY_CMN_PWM_LOW(0x101) |
1391 	       DPHY_CMN_PWM_DIV(0x8),
1392 	       dphy->regs + DPHY_CMN_PWM);
1393 }
1394 
cdns_dphy_ref_set_psm_div(struct cdns_dphy * dphy,u8 div)1395 static void cdns_dphy_ref_set_psm_div(struct cdns_dphy *dphy, u8 div)
1396 {
1397 	writel(DPHY_PSM_CFG_FROM_REG | DPHY_PSM_CLK_DIV(div),
1398 	       dphy->regs + DPHY_PSM_CFG);
1399 }
1400 
1401 /*
1402  * This is the reference implementation of DPHY hooks. Specific integration of
1403  * this IP may have to re-implement some of them depending on how they decided
1404  * to wire things in the SoC.
1405  */
1406 static const struct cdns_dphy_ops ref_dphy_ops = {
1407 	.get_wakeup_time_ns = cdns_dphy_ref_get_wakeup_time_ns,
1408 	.set_pll_cfg = cdns_dphy_ref_set_pll_cfg,
1409 	.set_psm_div = cdns_dphy_ref_set_psm_div,
1410 };
1411 
1412 static const struct of_device_id cdns_dphy_of_match[] = {
1413 	{ .compatible = "cdns,dphy", .data = &ref_dphy_ops },
1414 	{ /* sentinel */ },
1415 };
1416 
cdns_dphy_probe(struct platform_device * pdev)1417 static struct cdns_dphy *cdns_dphy_probe(struct platform_device *pdev)
1418 {
1419 	const struct of_device_id *match;
1420 	struct cdns_dphy *dphy;
1421 	struct of_phandle_args args;
1422 	struct resource res;
1423 	int ret;
1424 
1425 	ret = of_parse_phandle_with_args(pdev->dev.of_node, "phys",
1426 					 "#phy-cells", 0, &args);
1427 	if (ret)
1428 		return ERR_PTR(-ENOENT);
1429 
1430 	match = of_match_node(cdns_dphy_of_match, args.np);
1431 	if (!match || !match->data)
1432 		return ERR_PTR(-EINVAL);
1433 
1434 	dphy = devm_kzalloc(&pdev->dev, sizeof(*dphy), GFP_KERNEL);
1435 	if (!dphy)
1436 		return ERR_PTR(-ENOMEM);
1437 
1438 	dphy->ops = match->data;
1439 
1440 	ret = of_address_to_resource(args.np, 0, &res);
1441 	if (ret)
1442 		return ERR_PTR(ret);
1443 
1444 	dphy->regs = devm_ioremap_resource(&pdev->dev, &res);
1445 	if (IS_ERR(dphy->regs))
1446 		return ERR_CAST(dphy->regs);
1447 
1448 	dphy->psm_clk = of_clk_get_by_name(args.np, "psm");
1449 	if (IS_ERR(dphy->psm_clk))
1450 		return ERR_CAST(dphy->psm_clk);
1451 
1452 	dphy->pll_ref_clk = of_clk_get_by_name(args.np, "pll_ref");
1453 	if (IS_ERR(dphy->pll_ref_clk)) {
1454 		ret = PTR_ERR(dphy->pll_ref_clk);
1455 		goto err_put_psm_clk;
1456 	}
1457 
1458 	if (dphy->ops->probe) {
1459 		ret = dphy->ops->probe(dphy);
1460 		if (ret)
1461 			goto err_put_pll_ref_clk;
1462 	}
1463 
1464 	return dphy;
1465 
1466 err_put_pll_ref_clk:
1467 	clk_put(dphy->pll_ref_clk);
1468 
1469 err_put_psm_clk:
1470 	clk_put(dphy->psm_clk);
1471 
1472 	return ERR_PTR(ret);
1473 }
1474 
cdns_dphy_remove(struct cdns_dphy * dphy)1475 static void cdns_dphy_remove(struct cdns_dphy *dphy)
1476 {
1477 	if (dphy->ops->remove)
1478 		dphy->ops->remove(dphy);
1479 
1480 	clk_put(dphy->pll_ref_clk);
1481 	clk_put(dphy->psm_clk);
1482 }
1483 
cdns_dsi_drm_probe(struct platform_device * pdev)1484 static int cdns_dsi_drm_probe(struct platform_device *pdev)
1485 {
1486 	struct cdns_dsi *dsi;
1487 	struct cdns_dsi_input *input;
1488 	struct resource *res;
1489 	int ret, irq;
1490 	u32 val;
1491 
1492 	dsi = devm_kzalloc(&pdev->dev, sizeof(*dsi), GFP_KERNEL);
1493 	if (!dsi)
1494 		return -ENOMEM;
1495 
1496 	platform_set_drvdata(pdev, dsi);
1497 
1498 	input = &dsi->input;
1499 
1500 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1501 	dsi->regs = devm_ioremap_resource(&pdev->dev, res);
1502 	if (IS_ERR(dsi->regs))
1503 		return PTR_ERR(dsi->regs);
1504 
1505 	dsi->dsi_p_clk = devm_clk_get(&pdev->dev, "dsi_p_clk");
1506 	if (IS_ERR(dsi->dsi_p_clk))
1507 		return PTR_ERR(dsi->dsi_p_clk);
1508 
1509 	dsi->dsi_p_rst = devm_reset_control_get_optional_exclusive(&pdev->dev,
1510 								"dsi_p_rst");
1511 	if (IS_ERR(dsi->dsi_p_rst))
1512 		return PTR_ERR(dsi->dsi_p_rst);
1513 
1514 	dsi->dsi_sys_clk = devm_clk_get(&pdev->dev, "dsi_sys_clk");
1515 	if (IS_ERR(dsi->dsi_sys_clk))
1516 		return PTR_ERR(dsi->dsi_sys_clk);
1517 
1518 	irq = platform_get_irq(pdev, 0);
1519 	if (irq < 0)
1520 		return irq;
1521 
1522 	dsi->dphy = cdns_dphy_probe(pdev);
1523 	if (IS_ERR(dsi->dphy))
1524 		return PTR_ERR(dsi->dphy);
1525 
1526 	ret = clk_prepare_enable(dsi->dsi_p_clk);
1527 	if (ret)
1528 		goto err_remove_dphy;
1529 
1530 	val = readl(dsi->regs + ID_REG);
1531 	if (REV_VENDOR_ID(val) != 0xcad) {
1532 		dev_err(&pdev->dev, "invalid vendor id\n");
1533 		ret = -EINVAL;
1534 		goto err_disable_pclk;
1535 	}
1536 
1537 	val = readl(dsi->regs + IP_CONF);
1538 	dsi->direct_cmd_fifo_depth = 1 << (DIRCMD_FIFO_DEPTH(val) + 2);
1539 	dsi->rx_fifo_depth = RX_FIFO_DEPTH(val);
1540 	init_completion(&dsi->direct_cmd_comp);
1541 
1542 	writel(0, dsi->regs + MCTL_MAIN_DATA_CTL);
1543 	writel(0, dsi->regs + MCTL_MAIN_EN);
1544 	writel(0, dsi->regs + MCTL_MAIN_PHY_CTL);
1545 
1546 	/*
1547 	 * We only support the DPI input, so force input->id to
1548 	 * CDNS_DPI_INPUT.
1549 	 */
1550 	input->id = CDNS_DPI_INPUT;
1551 	input->bridge.funcs = &cdns_dsi_bridge_funcs;
1552 	input->bridge.of_node = pdev->dev.of_node;
1553 
1554 	/* Mask all interrupts before registering the IRQ handler. */
1555 	writel(0, dsi->regs + MCTL_MAIN_STS_CTL);
1556 	writel(0, dsi->regs + MCTL_DPHY_ERR_CTL1);
1557 	writel(0, dsi->regs + CMD_MODE_STS_CTL);
1558 	writel(0, dsi->regs + DIRECT_CMD_STS_CTL);
1559 	writel(0, dsi->regs + DIRECT_CMD_RD_STS_CTL);
1560 	writel(0, dsi->regs + VID_MODE_STS_CTL);
1561 	writel(0, dsi->regs + TVG_STS_CTL);
1562 	writel(0, dsi->regs + DPI_IRQ_EN);
1563 	ret = devm_request_irq(&pdev->dev, irq, cdns_dsi_interrupt, 0,
1564 			       dev_name(&pdev->dev), dsi);
1565 	if (ret)
1566 		goto err_disable_pclk;
1567 
1568 	pm_runtime_enable(&pdev->dev);
1569 	dsi->base.dev = &pdev->dev;
1570 	dsi->base.ops = &cdns_dsi_ops;
1571 
1572 	ret = mipi_dsi_host_register(&dsi->base);
1573 	if (ret)
1574 		goto err_disable_runtime_pm;
1575 
1576 	clk_disable_unprepare(dsi->dsi_p_clk);
1577 
1578 	return 0;
1579 
1580 err_disable_runtime_pm:
1581 	pm_runtime_disable(&pdev->dev);
1582 
1583 err_disable_pclk:
1584 	clk_disable_unprepare(dsi->dsi_p_clk);
1585 
1586 err_remove_dphy:
1587 	cdns_dphy_remove(dsi->dphy);
1588 
1589 	return ret;
1590 }
1591 
cdns_dsi_drm_remove(struct platform_device * pdev)1592 static int cdns_dsi_drm_remove(struct platform_device *pdev)
1593 {
1594 	struct cdns_dsi *dsi = platform_get_drvdata(pdev);
1595 
1596 	mipi_dsi_host_unregister(&dsi->base);
1597 	pm_runtime_disable(&pdev->dev);
1598 	cdns_dphy_remove(dsi->dphy);
1599 
1600 	return 0;
1601 }
1602 
1603 static const struct of_device_id cdns_dsi_of_match[] = {
1604 	{ .compatible = "cdns,dsi" },
1605 	{ },
1606 };
1607 
1608 static struct platform_driver cdns_dsi_platform_driver = {
1609 	.probe  = cdns_dsi_drm_probe,
1610 	.remove = cdns_dsi_drm_remove,
1611 	.driver = {
1612 		.name   = "cdns-dsi",
1613 		.of_match_table = cdns_dsi_of_match,
1614 		.pm = &cdns_dsi_pm_ops,
1615 	},
1616 };
1617 module_platform_driver(cdns_dsi_platform_driver);
1618 
1619 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@bootlin.com>");
1620 MODULE_DESCRIPTION("Cadence DSI driver");
1621 MODULE_LICENSE("GPL");
1622 MODULE_ALIAS("platform:cdns-dsi");
1623 
1624