xref: /openbmc/linux/drivers/gpu/drm/bridge/cadence/cdns-dsi-core.c (revision 7df45f35313c1ae083dac72c066b3aebfc7fc0cd)
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_drv.h>
10 #include <drm/drm_probe_helper.h>
11 #include <video/mipi_display.h>
12 
13 #include <linux/clk.h>
14 #include <linux/interrupt.h>
15 #include <linux/iopoll.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <linux/of_graph.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/reset.h>
22 
23 #include <linux/phy/phy-mipi-dphy.h>
24 
25 #include "cdns-dsi-core.h"
26 #ifdef CONFIG_DRM_CDNS_DSI_J721E
27 #include "cdns-dsi-j721e.h"
28 #endif
29 
30 #define IP_CONF				0x0
31 #define SP_HS_FIFO_DEPTH(x)		(((x) & GENMASK(30, 26)) >> 26)
32 #define SP_LP_FIFO_DEPTH(x)		(((x) & GENMASK(25, 21)) >> 21)
33 #define VRS_FIFO_DEPTH(x)		(((x) & GENMASK(20, 16)) >> 16)
34 #define DIRCMD_FIFO_DEPTH(x)		(((x) & GENMASK(15, 13)) >> 13)
35 #define SDI_IFACE_32			BIT(12)
36 #define INTERNAL_DATAPATH_32		(0 << 10)
37 #define INTERNAL_DATAPATH_16		(1 << 10)
38 #define INTERNAL_DATAPATH_8		(3 << 10)
39 #define INTERNAL_DATAPATH_SIZE		((x) & GENMASK(11, 10))
40 #define NUM_IFACE(x)			((((x) & GENMASK(9, 8)) >> 8) + 1)
41 #define MAX_LANE_NB(x)			(((x) & GENMASK(7, 6)) >> 6)
42 #define RX_FIFO_DEPTH(x)		((x) & GENMASK(5, 0))
43 
44 #define MCTL_MAIN_DATA_CTL		0x4
45 #define TE_MIPI_POLLING_EN		BIT(25)
46 #define TE_HW_POLLING_EN		BIT(24)
47 #define DISP_EOT_GEN			BIT(18)
48 #define HOST_EOT_GEN			BIT(17)
49 #define DISP_GEN_CHECKSUM		BIT(16)
50 #define DISP_GEN_ECC			BIT(15)
51 #define BTA_EN				BIT(14)
52 #define READ_EN				BIT(13)
53 #define REG_TE_EN			BIT(12)
54 #define IF_TE_EN(x)			BIT(8 + (x))
55 #define TVG_SEL				BIT(6)
56 #define VID_EN				BIT(5)
57 #define IF_VID_SELECT(x)		((x) << 2)
58 #define IF_VID_SELECT_MASK		GENMASK(3, 2)
59 #define IF_VID_MODE			BIT(1)
60 #define LINK_EN				BIT(0)
61 
62 #define MCTL_MAIN_PHY_CTL		0x8
63 #define HS_INVERT_DAT(x)		BIT(19 + ((x) * 2))
64 #define SWAP_PINS_DAT(x)		BIT(18 + ((x) * 2))
65 #define HS_INVERT_CLK			BIT(17)
66 #define SWAP_PINS_CLK			BIT(16)
67 #define HS_SKEWCAL_EN			BIT(15)
68 #define WAIT_BURST_TIME(x)		((x) << 10)
69 #define DATA_ULPM_EN(x)			BIT(6 + (x))
70 #define CLK_ULPM_EN			BIT(5)
71 #define CLK_CONTINUOUS			BIT(4)
72 #define DATA_LANE_EN(x)			BIT((x) - 1)
73 
74 #define MCTL_MAIN_EN			0xc
75 #define DATA_FORCE_STOP			BIT(17)
76 #define CLK_FORCE_STOP			BIT(16)
77 #define IF_EN(x)			BIT(13 + (x))
78 #define DATA_LANE_ULPM_REQ(l)		BIT(9 + (l))
79 #define CLK_LANE_ULPM_REQ		BIT(8)
80 #define DATA_LANE_START(x)		BIT(4 + (x))
81 #define CLK_LANE_EN			BIT(3)
82 #define PLL_START			BIT(0)
83 
84 #define MCTL_DPHY_CFG0			0x10
85 #define DPHY_C_RSTB			BIT(20)
86 #define DPHY_D_RSTB(x)			GENMASK(15 + (x), 16)
87 #define DPHY_PLL_PDN			BIT(10)
88 #define DPHY_CMN_PDN			BIT(9)
89 #define DPHY_C_PDN			BIT(8)
90 #define DPHY_D_PDN(x)			GENMASK(3 + (x), 4)
91 #define DPHY_ALL_D_PDN			GENMASK(7, 4)
92 #define DPHY_PLL_PSO			BIT(1)
93 #define DPHY_CMN_PSO			BIT(0)
94 
95 #define MCTL_DPHY_TIMEOUT1		0x14
96 #define HSTX_TIMEOUT(x)			((x) << 4)
97 #define HSTX_TIMEOUT_MAX		GENMASK(17, 0)
98 #define CLK_DIV(x)			(x)
99 #define CLK_DIV_MAX			GENMASK(3, 0)
100 
101 #define MCTL_DPHY_TIMEOUT2		0x18
102 #define LPRX_TIMEOUT(x)			(x)
103 
104 #define MCTL_ULPOUT_TIME		0x1c
105 #define DATA_LANE_ULPOUT_TIME(x)	((x) << 9)
106 #define CLK_LANE_ULPOUT_TIME(x)		(x)
107 
108 #define MCTL_3DVIDEO_CTL		0x20
109 #define VID_VSYNC_3D_EN			BIT(7)
110 #define VID_VSYNC_3D_LR			BIT(5)
111 #define VID_VSYNC_3D_SECOND_EN		BIT(4)
112 #define VID_VSYNC_3DFORMAT_LINE		(0 << 2)
113 #define VID_VSYNC_3DFORMAT_FRAME	(1 << 2)
114 #define VID_VSYNC_3DFORMAT_PIXEL	(2 << 2)
115 #define VID_VSYNC_3DMODE_OFF		0
116 #define VID_VSYNC_3DMODE_PORTRAIT	1
117 #define VID_VSYNC_3DMODE_LANDSCAPE	2
118 
119 #define MCTL_MAIN_STS			0x24
120 #define MCTL_MAIN_STS_CTL		0x130
121 #define MCTL_MAIN_STS_CLR		0x150
122 #define MCTL_MAIN_STS_FLAG		0x170
123 #define HS_SKEWCAL_DONE			BIT(11)
124 #define IF_UNTERM_PKT_ERR(x)		BIT(8 + (x))
125 #define LPRX_TIMEOUT_ERR		BIT(7)
126 #define HSTX_TIMEOUT_ERR		BIT(6)
127 #define DATA_LANE_RDY(l)		BIT(2 + (l))
128 #define CLK_LANE_RDY			BIT(1)
129 #define PLL_LOCKED			BIT(0)
130 
131 #define MCTL_DPHY_ERR			0x28
132 #define MCTL_DPHY_ERR_CTL1		0x148
133 #define MCTL_DPHY_ERR_CLR		0x168
134 #define MCTL_DPHY_ERR_FLAG		0x188
135 #define ERR_CONT_LP(x, l)		BIT(18 + ((x) * 4) + (l))
136 #define ERR_CONTROL(l)			BIT(14 + (l))
137 #define ERR_SYNESC(l)			BIT(10 + (l))
138 #define ERR_ESC(l)			BIT(6 + (l))
139 
140 #define MCTL_DPHY_ERR_CTL2		0x14c
141 #define ERR_CONT_LP_EDGE(x, l)		BIT(12 + ((x) * 4) + (l))
142 #define ERR_CONTROL_EDGE(l)		BIT(8 + (l))
143 #define ERR_SYN_ESC_EDGE(l)		BIT(4 + (l))
144 #define ERR_ESC_EDGE(l)			BIT(0 + (l))
145 
146 #define MCTL_LANE_STS			0x2c
147 #define PPI_C_TX_READY_HS		BIT(18)
148 #define DPHY_PLL_LOCK			BIT(17)
149 #define PPI_D_RX_ULPS_ESC(x)		(((x) & GENMASK(15, 12)) >> 12)
150 #define LANE_STATE_START		0
151 #define LANE_STATE_IDLE			1
152 #define LANE_STATE_WRITE		2
153 #define LANE_STATE_ULPM			3
154 #define LANE_STATE_READ			4
155 #define DATA_LANE_STATE(l, val)		\
156 	(((val) >> (2 + 2 * (l) + ((l) ? 1 : 0))) & GENMASK((l) ? 1 : 2, 0))
157 #define CLK_LANE_STATE_HS		2
158 #define CLK_LANE_STATE(val)		((val) & GENMASK(1, 0))
159 
160 #define DSC_MODE_CTL			0x30
161 #define DSC_MODE_EN			BIT(0)
162 
163 #define DSC_CMD_SEND			0x34
164 #define DSC_SEND_PPS			BIT(0)
165 #define DSC_EXECUTE_QUEUE		BIT(1)
166 
167 #define DSC_PPS_WRDAT			0x38
168 
169 #define DSC_MODE_STS			0x3c
170 #define DSC_PPS_DONE			BIT(1)
171 #define DSC_EXEC_DONE			BIT(2)
172 
173 #define CMD_MODE_CTL			0x70
174 #define IF_LP_EN(x)			BIT(9 + (x))
175 #define IF_VCHAN_ID(x, c)		((c) << ((x) * 2))
176 
177 #define CMD_MODE_CTL2			0x74
178 #define TE_TIMEOUT(x)			((x) << 11)
179 #define FILL_VALUE(x)			((x) << 3)
180 #define ARB_IF_WITH_HIGHEST_PRIORITY(x)	((x) << 1)
181 #define ARB_ROUND_ROBIN_MODE		BIT(0)
182 
183 #define CMD_MODE_STS			0x78
184 #define CMD_MODE_STS_CTL		0x134
185 #define CMD_MODE_STS_CLR		0x154
186 #define CMD_MODE_STS_FLAG		0x174
187 #define ERR_IF_UNDERRUN(x)		BIT(4 + (x))
188 #define ERR_UNWANTED_READ		BIT(3)
189 #define ERR_TE_MISS			BIT(2)
190 #define ERR_NO_TE			BIT(1)
191 #define CSM_RUNNING			BIT(0)
192 
193 #define DIRECT_CMD_SEND			0x80
194 
195 #define DIRECT_CMD_MAIN_SETTINGS	0x84
196 #define TRIGGER_VAL(x)			((x) << 25)
197 #define CMD_LP_EN			BIT(24)
198 #define CMD_SIZE(x)			((x) << 16)
199 #define CMD_VCHAN_ID(x)			((x) << 14)
200 #define CMD_DATATYPE(x)			((x) << 8)
201 #define CMD_LONG			BIT(3)
202 #define WRITE_CMD			0
203 #define READ_CMD			1
204 #define TE_REQ				4
205 #define TRIGGER_REQ			5
206 #define BTA_REQ				6
207 
208 #define DIRECT_CMD_STS			0x88
209 #define DIRECT_CMD_STS_CTL		0x138
210 #define DIRECT_CMD_STS_CLR		0x158
211 #define DIRECT_CMD_STS_FLAG		0x178
212 #define RCVD_ACK_VAL(val)		((val) >> 16)
213 #define RCVD_TRIGGER_VAL(val)		(((val) & GENMASK(14, 11)) >> 11)
214 #define READ_COMPLETED_WITH_ERR		BIT(10)
215 #define BTA_FINISHED			BIT(9)
216 #define BTA_COMPLETED			BIT(8)
217 #define TE_RCVD				BIT(7)
218 #define TRIGGER_RCVD			BIT(6)
219 #define ACK_WITH_ERR_RCVD		BIT(5)
220 #define ACK_RCVD			BIT(4)
221 #define READ_COMPLETED			BIT(3)
222 #define TRIGGER_COMPLETED		BIT(2)
223 #define WRITE_COMPLETED			BIT(1)
224 #define SENDING_CMD			BIT(0)
225 
226 #define DIRECT_CMD_STOP_READ		0x8c
227 
228 #define DIRECT_CMD_WRDATA		0x90
229 
230 #define DIRECT_CMD_FIFO_RST		0x94
231 
232 #define DIRECT_CMD_RDDATA		0xa0
233 
234 #define DIRECT_CMD_RD_PROPS		0xa4
235 #define RD_DCS				BIT(18)
236 #define RD_VCHAN_ID(val)		(((val) >> 16) & GENMASK(1, 0))
237 #define RD_SIZE(val)			((val) & GENMASK(15, 0))
238 
239 #define DIRECT_CMD_RD_STS		0xa8
240 #define DIRECT_CMD_RD_STS_CTL		0x13c
241 #define DIRECT_CMD_RD_STS_CLR		0x15c
242 #define DIRECT_CMD_RD_STS_FLAG		0x17c
243 #define ERR_EOT_WITH_ERR		BIT(8)
244 #define ERR_MISSING_EOT			BIT(7)
245 #define ERR_WRONG_LENGTH		BIT(6)
246 #define ERR_OVERSIZE			BIT(5)
247 #define ERR_RECEIVE			BIT(4)
248 #define ERR_UNDECODABLE			BIT(3)
249 #define ERR_CHECKSUM			BIT(2)
250 #define ERR_UNCORRECTABLE		BIT(1)
251 #define ERR_FIXED			BIT(0)
252 
253 #define VID_MAIN_CTL			0xb0
254 #define VID_IGNORE_MISS_VSYNC		BIT(31)
255 #define VID_FIELD_SW			BIT(28)
256 #define VID_INTERLACED_EN		BIT(27)
257 #define RECOVERY_MODE(x)		((x) << 25)
258 #define RECOVERY_MODE_NEXT_HSYNC	0
259 #define RECOVERY_MODE_NEXT_STOP_POINT	2
260 #define RECOVERY_MODE_NEXT_VSYNC	3
261 #define REG_BLKEOL_MODE(x)		((x) << 23)
262 #define REG_BLKLINE_MODE(x)		((x) << 21)
263 #define REG_BLK_MODE_NULL_PKT		0
264 #define REG_BLK_MODE_BLANKING_PKT	1
265 #define REG_BLK_MODE_LP			2
266 #define SYNC_PULSE_HORIZONTAL		BIT(20)
267 #define SYNC_PULSE_ACTIVE		BIT(19)
268 #define BURST_MODE			BIT(18)
269 #define VID_PIXEL_MODE_MASK		GENMASK(17, 14)
270 #define VID_PIXEL_MODE_RGB565		(0 << 14)
271 #define VID_PIXEL_MODE_RGB666_PACKED	(1 << 14)
272 #define VID_PIXEL_MODE_RGB666		(2 << 14)
273 #define VID_PIXEL_MODE_RGB888		(3 << 14)
274 #define VID_PIXEL_MODE_RGB101010	(4 << 14)
275 #define VID_PIXEL_MODE_RGB121212	(5 << 14)
276 #define VID_PIXEL_MODE_YUV420		(8 << 14)
277 #define VID_PIXEL_MODE_YUV422_PACKED	(9 << 14)
278 #define VID_PIXEL_MODE_YUV422		(10 << 14)
279 #define VID_PIXEL_MODE_YUV422_24B	(11 << 14)
280 #define VID_PIXEL_MODE_DSC_COMP		(12 << 14)
281 #define VID_DATATYPE(x)			((x) << 8)
282 #define VID_VIRTCHAN_ID(iface, x)	((x) << (4 + (iface) * 2))
283 #define STOP_MODE(x)			((x) << 2)
284 #define START_MODE(x)			(x)
285 
286 #define VID_VSIZE1			0xb4
287 #define VFP_LEN(x)			((x) << 12)
288 #define VBP_LEN(x)			((x) << 6)
289 #define VSA_LEN(x)			(x)
290 
291 #define VID_VSIZE2			0xb8
292 #define VACT_LEN(x)			(x)
293 
294 #define VID_HSIZE1			0xc0
295 #define HBP_LEN(x)			((x) << 16)
296 #define HSA_LEN(x)			(x)
297 
298 #define VID_HSIZE2			0xc4
299 #define HFP_LEN(x)			((x) << 16)
300 #define HACT_LEN(x)			(x)
301 
302 #define VID_BLKSIZE1			0xcc
303 #define BLK_EOL_PKT_LEN(x)		((x) << 15)
304 #define BLK_LINE_EVENT_PKT_LEN(x)	(x)
305 
306 #define VID_BLKSIZE2			0xd0
307 #define BLK_LINE_PULSE_PKT_LEN(x)	(x)
308 
309 #define VID_PKT_TIME			0xd8
310 #define BLK_EOL_DURATION(x)		(x)
311 
312 #define VID_DPHY_TIME			0xdc
313 #define REG_WAKEUP_TIME(x)		((x) << 17)
314 #define REG_LINE_DURATION(x)		(x)
315 
316 #define VID_ERR_COLOR1			0xe0
317 #define COL_GREEN(x)			((x) << 12)
318 #define COL_RED(x)			(x)
319 
320 #define VID_ERR_COLOR2			0xe4
321 #define PAD_VAL(x)			((x) << 12)
322 #define COL_BLUE(x)			(x)
323 
324 #define VID_VPOS			0xe8
325 #define LINE_VAL(val)			(((val) & GENMASK(14, 2)) >> 2)
326 #define LINE_POS(val)			((val) & GENMASK(1, 0))
327 
328 #define VID_HPOS			0xec
329 #define HORIZ_VAL(val)			(((val) & GENMASK(17, 3)) >> 3)
330 #define HORIZ_POS(val)			((val) & GENMASK(2, 0))
331 
332 #define VID_MODE_STS			0xf0
333 #define VID_MODE_STS_CTL		0x140
334 #define VID_MODE_STS_CLR		0x160
335 #define VID_MODE_STS_FLAG		0x180
336 #define VSG_RECOVERY			BIT(10)
337 #define ERR_VRS_WRONG_LEN		BIT(9)
338 #define ERR_LONG_READ			BIT(8)
339 #define ERR_LINE_WRITE			BIT(7)
340 #define ERR_BURST_WRITE			BIT(6)
341 #define ERR_SMALL_HEIGHT		BIT(5)
342 #define ERR_SMALL_LEN			BIT(4)
343 #define ERR_MISSING_VSYNC		BIT(3)
344 #define ERR_MISSING_HSYNC		BIT(2)
345 #define ERR_MISSING_DATA		BIT(1)
346 #define VSG_RUNNING			BIT(0)
347 
348 #define VID_VCA_SETTING1		0xf4
349 #define BURST_LP			BIT(16)
350 #define MAX_BURST_LIMIT(x)		(x)
351 
352 #define VID_VCA_SETTING2		0xf8
353 #define MAX_LINE_LIMIT(x)		((x) << 16)
354 #define EXACT_BURST_LIMIT(x)		(x)
355 
356 #define TVG_CTL				0xfc
357 #define TVG_STRIPE_SIZE(x)		((x) << 5)
358 #define TVG_MODE_MASK			GENMASK(4, 3)
359 #define TVG_MODE_SINGLE_COLOR		(0 << 3)
360 #define TVG_MODE_VSTRIPES		(2 << 3)
361 #define TVG_MODE_HSTRIPES		(3 << 3)
362 #define TVG_STOPMODE_MASK		GENMASK(2, 1)
363 #define TVG_STOPMODE_EOF		(0 << 1)
364 #define TVG_STOPMODE_EOL		(1 << 1)
365 #define TVG_STOPMODE_NOW		(2 << 1)
366 #define TVG_RUN				BIT(0)
367 
368 #define TVG_IMG_SIZE			0x100
369 #define TVG_NBLINES(x)			((x) << 16)
370 #define TVG_LINE_SIZE(x)		(x)
371 
372 #define TVG_COLOR1			0x104
373 #define TVG_COL1_GREEN(x)		((x) << 12)
374 #define TVG_COL1_RED(x)			(x)
375 
376 #define TVG_COLOR1_BIS			0x108
377 #define TVG_COL1_BLUE(x)		(x)
378 
379 #define TVG_COLOR2			0x10c
380 #define TVG_COL2_GREEN(x)		((x) << 12)
381 #define TVG_COL2_RED(x)			(x)
382 
383 #define TVG_COLOR2_BIS			0x110
384 #define TVG_COL2_BLUE(x)		(x)
385 
386 #define TVG_STS				0x114
387 #define TVG_STS_CTL			0x144
388 #define TVG_STS_CLR			0x164
389 #define TVG_STS_FLAG			0x184
390 #define TVG_STS_RUNNING			BIT(0)
391 
392 #define STS_CTL_EDGE(e)			((e) << 16)
393 
394 #define DPHY_LANES_MAP			0x198
395 #define DAT_REMAP_CFG(b, l)		((l) << ((b) * 8))
396 
397 #define DPI_IRQ_EN			0x1a0
398 #define DPI_IRQ_CLR			0x1a4
399 #define DPI_IRQ_STS			0x1a8
400 #define PIXEL_BUF_OVERFLOW		BIT(0)
401 
402 #define DPI_CFG				0x1ac
403 #define DPI_CFG_FIFO_DEPTH(x)		((x) >> 16)
404 #define DPI_CFG_FIFO_LEVEL(x)		((x) & GENMASK(15, 0))
405 
406 #define TEST_GENERIC			0x1f0
407 #define TEST_STATUS(x)			((x) >> 16)
408 #define TEST_CTRL(x)			(x)
409 
410 #define ID_REG				0x1fc
411 #define REV_VENDOR_ID(x)		(((x) & GENMASK(31, 20)) >> 20)
412 #define REV_PRODUCT_ID(x)		(((x) & GENMASK(19, 12)) >> 12)
413 #define REV_HW(x)			(((x) & GENMASK(11, 8)) >> 8)
414 #define REV_MAJOR(x)			(((x) & GENMASK(7, 4)) >> 4)
415 #define REV_MINOR(x)			((x) & GENMASK(3, 0))
416 
417 #define DSI_OUTPUT_PORT			0
418 #define DSI_INPUT_PORT(inputid)		(1 + (inputid))
419 
420 #define DSI_HBP_FRAME_OVERHEAD		12
421 #define DSI_HSA_FRAME_OVERHEAD		14
422 #define DSI_HFP_FRAME_OVERHEAD		6
423 #define DSI_HSS_VSS_VSE_FRAME_OVERHEAD	4
424 #define DSI_BLANKING_FRAME_OVERHEAD	6
425 #define DSI_NULL_FRAME_OVERHEAD		6
426 #define DSI_EOT_PKT_SIZE		4
427 
input_to_dsi(struct cdns_dsi_input * input)428 static inline struct cdns_dsi *input_to_dsi(struct cdns_dsi_input *input)
429 {
430 	return container_of(input, struct cdns_dsi, input);
431 }
432 
to_cdns_dsi(struct mipi_dsi_host * host)433 static inline struct cdns_dsi *to_cdns_dsi(struct mipi_dsi_host *host)
434 {
435 	return container_of(host, struct cdns_dsi, base);
436 }
437 
438 static inline struct cdns_dsi_input *
bridge_to_cdns_dsi_input(struct drm_bridge * bridge)439 bridge_to_cdns_dsi_input(struct drm_bridge *bridge)
440 {
441 	return container_of(bridge, struct cdns_dsi_input, bridge);
442 }
443 
mode_to_dpi_hfp(const struct drm_display_mode * mode,bool mode_valid_check)444 static unsigned int mode_to_dpi_hfp(const struct drm_display_mode *mode,
445 				    bool mode_valid_check)
446 {
447 	if (mode_valid_check)
448 		return mode->hsync_start - mode->hdisplay;
449 
450 	return mode->crtc_hsync_start - mode->crtc_hdisplay;
451 }
452 
dpi_to_dsi_timing(unsigned int dpi_timing,unsigned int dpi_bpp,unsigned int dsi_pkt_overhead)453 static unsigned int dpi_to_dsi_timing(unsigned int dpi_timing,
454 				      unsigned int dpi_bpp,
455 				      unsigned int dsi_pkt_overhead)
456 {
457 	unsigned int dsi_timing = DIV_ROUND_UP(dpi_timing * dpi_bpp, 8);
458 
459 	if (dsi_timing < dsi_pkt_overhead)
460 		dsi_timing = 0;
461 	else
462 		dsi_timing -= dsi_pkt_overhead;
463 
464 	return dsi_timing;
465 }
466 
cdns_dsi_mode2cfg(struct cdns_dsi * dsi,const struct drm_display_mode * mode,struct cdns_dsi_cfg * dsi_cfg,bool mode_valid_check)467 static int cdns_dsi_mode2cfg(struct cdns_dsi *dsi,
468 			     const struct drm_display_mode *mode,
469 			     struct cdns_dsi_cfg *dsi_cfg,
470 			     bool mode_valid_check)
471 {
472 	struct cdns_dsi_output *output = &dsi->output;
473 	unsigned int tmp;
474 	bool sync_pulse = false;
475 	int bpp;
476 
477 	memset(dsi_cfg, 0, sizeof(*dsi_cfg));
478 
479 	if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
480 		sync_pulse = true;
481 
482 	bpp = mipi_dsi_pixel_format_to_bpp(output->dev->format);
483 
484 	if (mode_valid_check)
485 		tmp = mode->htotal -
486 		      (sync_pulse ? mode->hsync_end : mode->hsync_start);
487 	else
488 		tmp = mode->crtc_htotal -
489 		      (sync_pulse ?
490 		       mode->crtc_hsync_end : mode->crtc_hsync_start);
491 
492 	dsi_cfg->hbp = dpi_to_dsi_timing(tmp, bpp, DSI_HBP_FRAME_OVERHEAD);
493 
494 	if (sync_pulse) {
495 		if (mode_valid_check)
496 			tmp = mode->hsync_end - mode->hsync_start;
497 		else
498 			tmp = mode->crtc_hsync_end - mode->crtc_hsync_start;
499 
500 		dsi_cfg->hsa = dpi_to_dsi_timing(tmp, bpp,
501 						 DSI_HSA_FRAME_OVERHEAD);
502 	}
503 
504 	dsi_cfg->hact = dpi_to_dsi_timing(mode_valid_check ?
505 					  mode->hdisplay : mode->crtc_hdisplay,
506 					  bpp, 0);
507 	dsi_cfg->hfp = dpi_to_dsi_timing(mode_to_dpi_hfp(mode, mode_valid_check),
508 					 bpp, DSI_HFP_FRAME_OVERHEAD);
509 
510 	return 0;
511 }
512 
cdns_dsi_adjust_phy_config(struct cdns_dsi * dsi,struct cdns_dsi_cfg * dsi_cfg,struct phy_configure_opts_mipi_dphy * phy_cfg,const struct drm_display_mode * mode,bool mode_valid_check)513 static int cdns_dsi_adjust_phy_config(struct cdns_dsi *dsi,
514 			      struct cdns_dsi_cfg *dsi_cfg,
515 			      struct phy_configure_opts_mipi_dphy *phy_cfg,
516 			      const struct drm_display_mode *mode,
517 			      bool mode_valid_check)
518 {
519 	struct cdns_dsi_output *output = &dsi->output;
520 	unsigned long long dlane_bps;
521 	unsigned long adj_dsi_htotal;
522 	unsigned long dsi_htotal;
523 	unsigned long dpi_htotal;
524 	unsigned long dpi_hz;
525 	unsigned int dsi_hfp_ext;
526 	unsigned int lanes = output->dev->lanes;
527 
528 	dsi_htotal = dsi_cfg->hbp + DSI_HBP_FRAME_OVERHEAD;
529 	if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
530 		dsi_htotal += dsi_cfg->hsa + DSI_HSA_FRAME_OVERHEAD;
531 
532 	dsi_htotal += dsi_cfg->hact;
533 	dsi_htotal += dsi_cfg->hfp + DSI_HFP_FRAME_OVERHEAD;
534 
535 	/*
536 	 * Make sure DSI htotal is aligned on a lane boundary when calculating
537 	 * the expected data rate. This is done by extending HFP in case of
538 	 * misalignment.
539 	 */
540 	adj_dsi_htotal = dsi_htotal;
541 	if (dsi_htotal % lanes)
542 		adj_dsi_htotal += lanes - (dsi_htotal % lanes);
543 
544 	dpi_hz = (mode_valid_check ? mode->clock : mode->crtc_clock) * 1000;
545 	dlane_bps = (unsigned long long)dpi_hz * adj_dsi_htotal;
546 
547 	/* data rate in bytes/sec is not an integer, refuse the mode. */
548 	dpi_htotal = mode_valid_check ? mode->htotal : mode->crtc_htotal;
549 	if (do_div(dlane_bps, lanes * dpi_htotal))
550 		return -EINVAL;
551 
552 	/* data rate was in bytes/sec, convert to bits/sec. */
553 	phy_cfg->hs_clk_rate = dlane_bps * 8;
554 
555 	dsi_hfp_ext = adj_dsi_htotal - dsi_htotal;
556 	dsi_cfg->hfp += dsi_hfp_ext;
557 	dsi_cfg->htotal = dsi_htotal + dsi_hfp_ext;
558 
559 	return 0;
560 }
561 
cdns_dsi_check_conf(struct cdns_dsi * dsi,const struct drm_display_mode * mode,struct cdns_dsi_cfg * dsi_cfg,bool mode_valid_check)562 static int cdns_dsi_check_conf(struct cdns_dsi *dsi,
563 			       const struct drm_display_mode *mode,
564 			       struct cdns_dsi_cfg *dsi_cfg,
565 			       bool mode_valid_check)
566 {
567 	struct cdns_dsi_output *output = &dsi->output;
568 	struct phy_configure_opts_mipi_dphy *phy_cfg = &output->phy_opts.mipi_dphy;
569 	unsigned long dsi_hss_hsa_hse_hbp;
570 	unsigned int nlanes = output->dev->lanes;
571 	int mode_clock = (mode_valid_check ? mode->clock : mode->crtc_clock);
572 	int ret;
573 
574 	ret = cdns_dsi_mode2cfg(dsi, mode, dsi_cfg, mode_valid_check);
575 	if (ret)
576 		return ret;
577 
578 	ret = phy_mipi_dphy_get_default_config(mode_clock * 1000,
579 					       mipi_dsi_pixel_format_to_bpp(output->dev->format),
580 					       nlanes, phy_cfg);
581 	if (ret)
582 		return ret;
583 
584 	ret = cdns_dsi_adjust_phy_config(dsi, dsi_cfg, phy_cfg, mode, mode_valid_check);
585 	if (ret)
586 		return ret;
587 
588 	ret = phy_validate(dsi->dphy, PHY_MODE_MIPI_DPHY, 0, &output->phy_opts);
589 	if (ret)
590 		return ret;
591 
592 	dsi_hss_hsa_hse_hbp = dsi_cfg->hbp + DSI_HBP_FRAME_OVERHEAD;
593 	if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
594 		dsi_hss_hsa_hse_hbp += dsi_cfg->hsa + DSI_HSA_FRAME_OVERHEAD;
595 
596 	/*
597 	 * Make sure DPI(HFP) > DSI(HSS+HSA+HSE+HBP) to guarantee that the FIFO
598 	 * is empty before we start a receiving a new line on the DPI
599 	 * interface.
600 	 */
601 	if ((u64)phy_cfg->hs_clk_rate *
602 	    mode_to_dpi_hfp(mode, mode_valid_check) * nlanes <
603 	    (u64)dsi_hss_hsa_hse_hbp *
604 	    (mode_valid_check ? mode->clock : mode->crtc_clock) * 1000)
605 		return -EINVAL;
606 
607 	return 0;
608 }
609 
cdns_dsi_bridge_attach(struct drm_bridge * bridge,enum drm_bridge_attach_flags flags)610 static int cdns_dsi_bridge_attach(struct drm_bridge *bridge,
611 				  enum drm_bridge_attach_flags flags)
612 {
613 	struct cdns_dsi_input *input = bridge_to_cdns_dsi_input(bridge);
614 	struct cdns_dsi *dsi = input_to_dsi(input);
615 	struct cdns_dsi_output *output = &dsi->output;
616 
617 	if (!drm_core_check_feature(bridge->dev, DRIVER_ATOMIC)) {
618 		dev_err(dsi->base.dev,
619 			"cdns-dsi driver is only compatible with DRM devices supporting atomic updates");
620 		return -ENOTSUPP;
621 	}
622 
623 	return drm_bridge_attach(bridge->encoder, output->bridge, bridge,
624 				 flags);
625 }
626 
627 static enum drm_mode_status
cdns_dsi_bridge_mode_valid(struct drm_bridge * bridge,const struct drm_display_info * info,const struct drm_display_mode * mode)628 cdns_dsi_bridge_mode_valid(struct drm_bridge *bridge,
629 			   const struct drm_display_info *info,
630 			   const struct drm_display_mode *mode)
631 {
632 	struct cdns_dsi_input *input = bridge_to_cdns_dsi_input(bridge);
633 	struct cdns_dsi *dsi = input_to_dsi(input);
634 	struct cdns_dsi_output *output = &dsi->output;
635 	struct cdns_dsi_cfg dsi_cfg;
636 	int bpp, ret;
637 
638 	/*
639 	 * VFP_DSI should be less than VFP_DPI and VFP_DSI should be at
640 	 * least 1.
641 	 */
642 	if (mode->vtotal - mode->vsync_end < 2)
643 		return MODE_V_ILLEGAL;
644 
645 	/* VSA_DSI = VSA_DPI and must be at least 2. */
646 	if (mode->vsync_end - mode->vsync_start < 2)
647 		return MODE_V_ILLEGAL;
648 
649 	/* HACT must be 32-bits aligned. */
650 	bpp = mipi_dsi_pixel_format_to_bpp(output->dev->format);
651 	if ((mode->hdisplay * bpp) % 32)
652 		return MODE_H_ILLEGAL;
653 
654 	ret = cdns_dsi_check_conf(dsi, mode, &dsi_cfg, true);
655 	if (ret)
656 		return MODE_BAD;
657 
658 	return MODE_OK;
659 }
660 
cdns_dsi_bridge_disable(struct drm_bridge * bridge)661 static void cdns_dsi_bridge_disable(struct drm_bridge *bridge)
662 {
663 	struct cdns_dsi_input *input = bridge_to_cdns_dsi_input(bridge);
664 	struct cdns_dsi *dsi = input_to_dsi(input);
665 	u32 val;
666 
667 	val = readl(dsi->regs + MCTL_MAIN_DATA_CTL);
668 	val &= ~(IF_VID_SELECT_MASK | IF_VID_MODE | VID_EN | HOST_EOT_GEN |
669 		 DISP_EOT_GEN);
670 	writel(val, dsi->regs + MCTL_MAIN_DATA_CTL);
671 
672 	val = readl(dsi->regs + MCTL_MAIN_EN) & ~IF_EN(input->id);
673 	writel(val, dsi->regs + MCTL_MAIN_EN);
674 
675 	if (dsi->platform_ops && dsi->platform_ops->disable)
676 		dsi->platform_ops->disable(dsi);
677 
678 	pm_runtime_put(dsi->base.dev);
679 }
680 
cdns_dsi_bridge_post_disable(struct drm_bridge * bridge)681 static void cdns_dsi_bridge_post_disable(struct drm_bridge *bridge)
682 {
683 	struct cdns_dsi_input *input = bridge_to_cdns_dsi_input(bridge);
684 	struct cdns_dsi *dsi = input_to_dsi(input);
685 
686 	dsi->phy_initialized = false;
687 	dsi->link_initialized = false;
688 	phy_power_off(dsi->dphy);
689 	phy_exit(dsi->dphy);
690 
691 	pm_runtime_put(dsi->base.dev);
692 }
693 
cdns_dsi_hs_init(struct cdns_dsi * dsi)694 static void cdns_dsi_hs_init(struct cdns_dsi *dsi)
695 {
696 	struct cdns_dsi_output *output = &dsi->output;
697 	u32 status;
698 
699 	if (dsi->phy_initialized)
700 		return;
701 	/*
702 	 * Power all internal DPHY blocks down and maintain their reset line
703 	 * asserted before changing the DPHY config.
704 	 */
705 	writel(DPHY_CMN_PSO | DPHY_PLL_PSO | DPHY_ALL_D_PDN | DPHY_C_PDN |
706 	       DPHY_CMN_PDN | DPHY_PLL_PDN,
707 	       dsi->regs + MCTL_DPHY_CFG0);
708 
709 	phy_init(dsi->dphy);
710 	phy_set_mode(dsi->dphy, PHY_MODE_MIPI_DPHY);
711 	phy_configure(dsi->dphy, &output->phy_opts);
712 	phy_power_on(dsi->dphy);
713 
714 	/* Activate the PLL and wait until it's locked. */
715 	writel(PLL_LOCKED, dsi->regs + MCTL_MAIN_STS_CLR);
716 	writel(DPHY_CMN_PSO | DPHY_ALL_D_PDN | DPHY_C_PDN | DPHY_CMN_PDN,
717 	       dsi->regs + MCTL_DPHY_CFG0);
718 	WARN_ON_ONCE(readl_poll_timeout(dsi->regs + MCTL_MAIN_STS, status,
719 					status & PLL_LOCKED, 100, 100));
720 	/* De-assert data and clock reset lines. */
721 	writel(DPHY_CMN_PSO | DPHY_ALL_D_PDN | DPHY_C_PDN | DPHY_CMN_PDN |
722 	       DPHY_D_RSTB(output->dev->lanes) | DPHY_C_RSTB,
723 	       dsi->regs + MCTL_DPHY_CFG0);
724 	dsi->phy_initialized = true;
725 }
726 
cdns_dsi_init_link(struct cdns_dsi * dsi)727 static void cdns_dsi_init_link(struct cdns_dsi *dsi)
728 {
729 	struct cdns_dsi_output *output = &dsi->output;
730 	unsigned long sysclk_period, ulpout;
731 	u32 val;
732 	int i;
733 
734 	if (dsi->link_initialized)
735 		return;
736 
737 	val = 0;
738 	for (i = 1; i < output->dev->lanes; i++)
739 		val |= DATA_LANE_EN(i);
740 
741 	if (!(output->dev->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS))
742 		val |= CLK_CONTINUOUS;
743 
744 	writel(val, dsi->regs + MCTL_MAIN_PHY_CTL);
745 
746 	/* ULPOUT should be set to 1ms and is expressed in sysclk cycles. */
747 	sysclk_period = NSEC_PER_SEC / clk_get_rate(dsi->dsi_sys_clk);
748 	ulpout = DIV_ROUND_UP(NSEC_PER_MSEC, sysclk_period);
749 	writel(CLK_LANE_ULPOUT_TIME(ulpout) | DATA_LANE_ULPOUT_TIME(ulpout),
750 	       dsi->regs + MCTL_ULPOUT_TIME);
751 
752 	writel(LINK_EN, dsi->regs + MCTL_MAIN_DATA_CTL);
753 
754 	val = CLK_LANE_EN | PLL_START;
755 	for (i = 0; i < output->dev->lanes; i++)
756 		val |= DATA_LANE_START(i);
757 
758 	writel(val, dsi->regs + MCTL_MAIN_EN);
759 
760 	dsi->link_initialized = true;
761 }
762 
cdns_dsi_bridge_enable(struct drm_bridge * bridge)763 static void cdns_dsi_bridge_enable(struct drm_bridge *bridge)
764 {
765 	struct cdns_dsi_input *input = bridge_to_cdns_dsi_input(bridge);
766 	struct cdns_dsi *dsi = input_to_dsi(input);
767 	struct cdns_dsi_output *output = &dsi->output;
768 	struct drm_display_mode *mode;
769 	struct phy_configure_opts_mipi_dphy *phy_cfg = &output->phy_opts.mipi_dphy;
770 	unsigned long tx_byte_period;
771 	struct cdns_dsi_cfg dsi_cfg;
772 	u32 tmp, reg_wakeup, div, status;
773 	int nlanes;
774 
775 	if (WARN_ON(pm_runtime_get_sync(dsi->base.dev) < 0))
776 		return;
777 
778 	if (dsi->platform_ops && dsi->platform_ops->enable)
779 		dsi->platform_ops->enable(dsi);
780 
781 	mode = &bridge->encoder->crtc->state->adjusted_mode;
782 	nlanes = output->dev->lanes;
783 
784 	WARN_ON_ONCE(cdns_dsi_check_conf(dsi, mode, &dsi_cfg, false));
785 
786 	cdns_dsi_hs_init(dsi);
787 	cdns_dsi_init_link(dsi);
788 
789 	/*
790 	 * Now that the DSI Link and DSI Phy are initialized,
791 	 * wait for the CLK and Data Lanes to be ready.
792 	 */
793 	tmp = CLK_LANE_RDY;
794 	for (int i = 0; i < nlanes; i++)
795 		tmp |= DATA_LANE_RDY(i);
796 
797 	if (readl_poll_timeout(dsi->regs + MCTL_MAIN_STS, status,
798 			       (tmp == (status & tmp)), 100, 500000))
799 		dev_err(dsi->base.dev,
800 			"Timed Out: DSI-DPhy Clock and Data Lanes not ready.\n");
801 
802 	writel(HBP_LEN(dsi_cfg.hbp) | HSA_LEN(dsi_cfg.hsa),
803 	       dsi->regs + VID_HSIZE1);
804 	writel(HFP_LEN(dsi_cfg.hfp) | HACT_LEN(dsi_cfg.hact),
805 	       dsi->regs + VID_HSIZE2);
806 
807 	writel(VBP_LEN(mode->crtc_vtotal - mode->crtc_vsync_end - 1) |
808 	       VFP_LEN(mode->crtc_vsync_start - mode->crtc_vdisplay) |
809 	       VSA_LEN(mode->crtc_vsync_end - mode->crtc_vsync_start + 1),
810 	       dsi->regs + VID_VSIZE1);
811 	writel(mode->crtc_vdisplay, dsi->regs + VID_VSIZE2);
812 
813 	tmp = dsi_cfg.htotal -
814 	      (dsi_cfg.hsa + DSI_BLANKING_FRAME_OVERHEAD +
815 	       DSI_HSA_FRAME_OVERHEAD);
816 	writel(BLK_LINE_PULSE_PKT_LEN(tmp), dsi->regs + VID_BLKSIZE2);
817 	if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
818 		writel(MAX_LINE_LIMIT(tmp - DSI_NULL_FRAME_OVERHEAD),
819 		       dsi->regs + VID_VCA_SETTING2);
820 
821 	tmp = dsi_cfg.htotal -
822 	      (DSI_HSS_VSS_VSE_FRAME_OVERHEAD + DSI_BLANKING_FRAME_OVERHEAD);
823 	writel(BLK_LINE_EVENT_PKT_LEN(tmp), dsi->regs + VID_BLKSIZE1);
824 	if (!(output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE))
825 		writel(MAX_LINE_LIMIT(tmp - DSI_NULL_FRAME_OVERHEAD),
826 		       dsi->regs + VID_VCA_SETTING2);
827 
828 	tmp = DIV_ROUND_UP(dsi_cfg.htotal, nlanes) -
829 	      DIV_ROUND_UP(dsi_cfg.hsa, nlanes);
830 
831 	if (!(output->dev->mode_flags & MIPI_DSI_MODE_NO_EOT_PACKET))
832 		tmp -= DIV_ROUND_UP(DSI_EOT_PKT_SIZE, nlanes);
833 
834 	tx_byte_period = DIV_ROUND_DOWN_ULL((u64)NSEC_PER_SEC * 8,
835 					    phy_cfg->hs_clk_rate);
836 	reg_wakeup = (phy_cfg->hs_prepare + phy_cfg->hs_zero) / tx_byte_period;
837 	writel(REG_WAKEUP_TIME(reg_wakeup) | REG_LINE_DURATION(tmp),
838 	       dsi->regs + VID_DPHY_TIME);
839 
840 	/*
841 	 * HSTX and LPRX timeouts are both expressed in TX byte clk cycles and
842 	 * both should be set to at least the time it takes to transmit a
843 	 * frame.
844 	 */
845 	tmp = NSEC_PER_SEC / drm_mode_vrefresh(mode);
846 	tmp /= tx_byte_period;
847 
848 	for (div = 0; div <= CLK_DIV_MAX; div++) {
849 		if (tmp <= HSTX_TIMEOUT_MAX)
850 			break;
851 
852 		tmp >>= 1;
853 	}
854 
855 	if (tmp > HSTX_TIMEOUT_MAX)
856 		tmp = HSTX_TIMEOUT_MAX;
857 
858 	writel(CLK_DIV(div) | HSTX_TIMEOUT(tmp),
859 	       dsi->regs + MCTL_DPHY_TIMEOUT1);
860 
861 	writel(LPRX_TIMEOUT(tmp), dsi->regs + MCTL_DPHY_TIMEOUT2);
862 
863 	if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO) {
864 		switch (output->dev->format) {
865 		case MIPI_DSI_FMT_RGB888:
866 			tmp = VID_PIXEL_MODE_RGB888 |
867 			      VID_DATATYPE(MIPI_DSI_PACKED_PIXEL_STREAM_24);
868 			break;
869 
870 		case MIPI_DSI_FMT_RGB666:
871 			tmp = VID_PIXEL_MODE_RGB666 |
872 			      VID_DATATYPE(MIPI_DSI_PIXEL_STREAM_3BYTE_18);
873 			break;
874 
875 		case MIPI_DSI_FMT_RGB666_PACKED:
876 			tmp = VID_PIXEL_MODE_RGB666_PACKED |
877 			      VID_DATATYPE(MIPI_DSI_PACKED_PIXEL_STREAM_18);
878 			break;
879 
880 		case MIPI_DSI_FMT_RGB565:
881 			tmp = VID_PIXEL_MODE_RGB565 |
882 			      VID_DATATYPE(MIPI_DSI_PACKED_PIXEL_STREAM_16);
883 			break;
884 
885 		default:
886 			dev_err(dsi->base.dev, "Unsupported DSI format\n");
887 			return;
888 		}
889 
890 		if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
891 			tmp |= SYNC_PULSE_ACTIVE | SYNC_PULSE_HORIZONTAL;
892 
893 		tmp |= REG_BLKLINE_MODE(REG_BLK_MODE_BLANKING_PKT) |
894 		       REG_BLKEOL_MODE(REG_BLK_MODE_BLANKING_PKT) |
895 		       RECOVERY_MODE(RECOVERY_MODE_NEXT_HSYNC) |
896 		       VID_IGNORE_MISS_VSYNC;
897 
898 		writel(tmp, dsi->regs + VID_MAIN_CTL);
899 	}
900 
901 	tmp = readl(dsi->regs + MCTL_MAIN_DATA_CTL);
902 	tmp &= ~(IF_VID_SELECT_MASK | HOST_EOT_GEN | IF_VID_MODE);
903 
904 	if (!(output->dev->mode_flags & MIPI_DSI_MODE_NO_EOT_PACKET))
905 		tmp |= HOST_EOT_GEN;
906 
907 	if (output->dev->mode_flags & MIPI_DSI_MODE_VIDEO)
908 		tmp |= IF_VID_MODE | IF_VID_SELECT(input->id) | VID_EN;
909 
910 	writel(tmp, dsi->regs + MCTL_MAIN_DATA_CTL);
911 
912 	tmp = readl(dsi->regs + MCTL_MAIN_EN) | IF_EN(input->id);
913 	writel(tmp, dsi->regs + MCTL_MAIN_EN);
914 }
915 
cdns_dsi_bridge_pre_enable(struct drm_bridge * bridge)916 static void cdns_dsi_bridge_pre_enable(struct drm_bridge *bridge)
917 {
918 	struct cdns_dsi_input *input = bridge_to_cdns_dsi_input(bridge);
919 	struct cdns_dsi *dsi = input_to_dsi(input);
920 
921 	if (WARN_ON(pm_runtime_get_sync(dsi->base.dev) < 0))
922 		return;
923 
924 	cdns_dsi_init_link(dsi);
925 	cdns_dsi_hs_init(dsi);
926 }
927 
928 static const struct drm_bridge_funcs cdns_dsi_bridge_funcs = {
929 	.attach = cdns_dsi_bridge_attach,
930 	.mode_valid = cdns_dsi_bridge_mode_valid,
931 	.disable = cdns_dsi_bridge_disable,
932 	.pre_enable = cdns_dsi_bridge_pre_enable,
933 	.enable = cdns_dsi_bridge_enable,
934 	.post_disable = cdns_dsi_bridge_post_disable,
935 };
936 
cdns_dsi_attach(struct mipi_dsi_host * host,struct mipi_dsi_device * dev)937 static int cdns_dsi_attach(struct mipi_dsi_host *host,
938 			   struct mipi_dsi_device *dev)
939 {
940 	struct cdns_dsi *dsi = to_cdns_dsi(host);
941 	struct cdns_dsi_output *output = &dsi->output;
942 	struct cdns_dsi_input *input = &dsi->input;
943 	struct drm_bridge *bridge;
944 	struct drm_panel *panel;
945 	struct device_node *np;
946 	int ret;
947 
948 	/*
949 	 * We currently do not support connecting several DSI devices to the
950 	 * same host. In order to support that we'd need the DRM bridge
951 	 * framework to allow dynamic reconfiguration of the bridge chain.
952 	 */
953 	if (output->dev)
954 		return -EBUSY;
955 
956 	/* We do not support burst mode yet. */
957 	if (dev->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
958 		return -ENOTSUPP;
959 
960 	/*
961 	 * The host <-> device link might be described using an OF-graph
962 	 * representation, in this case we extract the device of_node from
963 	 * this representation, otherwise we use dsidev->dev.of_node which
964 	 * should have been filled by the core.
965 	 */
966 	np = of_graph_get_remote_node(dsi->base.dev->of_node, DSI_OUTPUT_PORT,
967 				      dev->channel);
968 	if (!np)
969 		np = of_node_get(dev->dev.of_node);
970 
971 	panel = of_drm_find_panel(np);
972 	if (!IS_ERR(panel)) {
973 		bridge = drm_panel_bridge_add_typed(panel,
974 						    DRM_MODE_CONNECTOR_DSI);
975 	} else {
976 		bridge = of_drm_find_bridge(np);
977 		if (!bridge)
978 			bridge = ERR_PTR(-EINVAL);
979 	}
980 
981 	of_node_put(np);
982 
983 	if (IS_ERR(bridge)) {
984 		ret = PTR_ERR(bridge);
985 		dev_err(host->dev, "failed to add DSI device %s (err = %d)",
986 			dev->name, ret);
987 		return ret;
988 	}
989 
990 	output->dev = dev;
991 	output->bridge = bridge;
992 	output->panel = panel;
993 
994 	/*
995 	 * The DSI output has been properly configured, we can now safely
996 	 * register the input to the bridge framework so that it can take place
997 	 * in a display pipeline.
998 	 */
999 	drm_bridge_add(&input->bridge);
1000 
1001 	return 0;
1002 }
1003 
cdns_dsi_detach(struct mipi_dsi_host * host,struct mipi_dsi_device * dev)1004 static int cdns_dsi_detach(struct mipi_dsi_host *host,
1005 			   struct mipi_dsi_device *dev)
1006 {
1007 	struct cdns_dsi *dsi = to_cdns_dsi(host);
1008 	struct cdns_dsi_output *output = &dsi->output;
1009 	struct cdns_dsi_input *input = &dsi->input;
1010 
1011 	drm_bridge_remove(&input->bridge);
1012 	if (output->panel)
1013 		drm_panel_bridge_remove(output->bridge);
1014 
1015 	return 0;
1016 }
1017 
cdns_dsi_interrupt(int irq,void * data)1018 static irqreturn_t cdns_dsi_interrupt(int irq, void *data)
1019 {
1020 	struct cdns_dsi *dsi = data;
1021 	irqreturn_t ret = IRQ_NONE;
1022 	u32 flag, ctl;
1023 
1024 	flag = readl(dsi->regs + DIRECT_CMD_STS_FLAG);
1025 	if (flag) {
1026 		ctl = readl(dsi->regs + DIRECT_CMD_STS_CTL);
1027 		ctl &= ~flag;
1028 		writel(ctl, dsi->regs + DIRECT_CMD_STS_CTL);
1029 		complete(&dsi->direct_cmd_comp);
1030 		ret = IRQ_HANDLED;
1031 	}
1032 
1033 	return ret;
1034 }
1035 
cdns_dsi_transfer(struct mipi_dsi_host * host,const struct mipi_dsi_msg * msg)1036 static ssize_t cdns_dsi_transfer(struct mipi_dsi_host *host,
1037 				 const struct mipi_dsi_msg *msg)
1038 {
1039 	struct cdns_dsi *dsi = to_cdns_dsi(host);
1040 	u32 cmd, sts, val, wait = WRITE_COMPLETED, ctl = 0;
1041 	struct mipi_dsi_packet packet;
1042 	int ret, i, tx_len, rx_len;
1043 
1044 	ret = pm_runtime_resume_and_get(host->dev);
1045 	if (ret < 0)
1046 		return ret;
1047 
1048 	cdns_dsi_init_link(dsi);
1049 
1050 	ret = mipi_dsi_create_packet(&packet, msg);
1051 	if (ret)
1052 		goto out;
1053 
1054 	tx_len = msg->tx_buf ? msg->tx_len : 0;
1055 	rx_len = msg->rx_buf ? msg->rx_len : 0;
1056 
1057 	/* For read operations, the maximum TX len is 2. */
1058 	if (rx_len && tx_len > 2) {
1059 		ret = -ENOTSUPP;
1060 		goto out;
1061 	}
1062 
1063 	/* TX len is limited by the CMD FIFO depth. */
1064 	if (tx_len > dsi->direct_cmd_fifo_depth) {
1065 		ret = -ENOTSUPP;
1066 		goto out;
1067 	}
1068 
1069 	/* RX len is limited by the RX FIFO depth. */
1070 	if (rx_len > dsi->rx_fifo_depth) {
1071 		ret = -ENOTSUPP;
1072 		goto out;
1073 	}
1074 
1075 	cmd = CMD_SIZE(tx_len) | CMD_VCHAN_ID(msg->channel) |
1076 	      CMD_DATATYPE(msg->type);
1077 
1078 	if (msg->flags & MIPI_DSI_MSG_USE_LPM)
1079 		cmd |= CMD_LP_EN;
1080 
1081 	if (mipi_dsi_packet_format_is_long(msg->type))
1082 		cmd |= CMD_LONG;
1083 
1084 	if (rx_len) {
1085 		cmd |= READ_CMD;
1086 		wait = READ_COMPLETED_WITH_ERR | READ_COMPLETED;
1087 		ctl = READ_EN | BTA_EN;
1088 	} else if (msg->flags & MIPI_DSI_MSG_REQ_ACK) {
1089 		cmd |= BTA_REQ;
1090 		wait = ACK_WITH_ERR_RCVD | ACK_RCVD;
1091 		ctl = BTA_EN;
1092 	}
1093 
1094 	writel(readl(dsi->regs + MCTL_MAIN_DATA_CTL) | ctl,
1095 	       dsi->regs + MCTL_MAIN_DATA_CTL);
1096 
1097 	writel(cmd, dsi->regs + DIRECT_CMD_MAIN_SETTINGS);
1098 
1099 	for (i = 0; i < tx_len; i += 4) {
1100 		const u8 *buf = msg->tx_buf;
1101 		int j;
1102 
1103 		val = 0;
1104 		for (j = 0; j < 4 && j + i < tx_len; j++)
1105 			val |= (u32)buf[i + j] << (8 * j);
1106 
1107 		writel(val, dsi->regs + DIRECT_CMD_WRDATA);
1108 	}
1109 
1110 	/* Clear status flags before sending the command. */
1111 	writel(wait, dsi->regs + DIRECT_CMD_STS_CLR);
1112 	writel(wait, dsi->regs + DIRECT_CMD_STS_CTL);
1113 	reinit_completion(&dsi->direct_cmd_comp);
1114 	writel(0, dsi->regs + DIRECT_CMD_SEND);
1115 
1116 	wait_for_completion_timeout(&dsi->direct_cmd_comp,
1117 				    msecs_to_jiffies(1000));
1118 
1119 	sts = readl(dsi->regs + DIRECT_CMD_STS);
1120 	writel(wait, dsi->regs + DIRECT_CMD_STS_CLR);
1121 	writel(0, dsi->regs + DIRECT_CMD_STS_CTL);
1122 
1123 	writel(readl(dsi->regs + MCTL_MAIN_DATA_CTL) & ~ctl,
1124 	       dsi->regs + MCTL_MAIN_DATA_CTL);
1125 
1126 	/* We did not receive the events we were waiting for. */
1127 	if (!(sts & wait)) {
1128 		ret = -ETIMEDOUT;
1129 		goto out;
1130 	}
1131 
1132 	/* 'READ' or 'WRITE with ACK' failed. */
1133 	if (sts & (READ_COMPLETED_WITH_ERR | ACK_WITH_ERR_RCVD)) {
1134 		ret = -EIO;
1135 		goto out;
1136 	}
1137 
1138 	for (i = 0; i < rx_len; i += 4) {
1139 		u8 *buf = msg->rx_buf;
1140 		int j;
1141 
1142 		val = readl(dsi->regs + DIRECT_CMD_RDDATA);
1143 		for (j = 0; j < 4 && j + i < rx_len; j++)
1144 			buf[i + j] = val >> (8 * j);
1145 	}
1146 
1147 out:
1148 	pm_runtime_put(host->dev);
1149 	return ret;
1150 }
1151 
1152 static const struct mipi_dsi_host_ops cdns_dsi_ops = {
1153 	.attach = cdns_dsi_attach,
1154 	.detach = cdns_dsi_detach,
1155 	.transfer = cdns_dsi_transfer,
1156 };
1157 
cdns_dsi_resume(struct device * dev)1158 static int __maybe_unused cdns_dsi_resume(struct device *dev)
1159 {
1160 	struct cdns_dsi *dsi = dev_get_drvdata(dev);
1161 
1162 	reset_control_deassert(dsi->dsi_p_rst);
1163 	clk_prepare_enable(dsi->dsi_p_clk);
1164 	clk_prepare_enable(dsi->dsi_sys_clk);
1165 
1166 	return 0;
1167 }
1168 
cdns_dsi_suspend(struct device * dev)1169 static int __maybe_unused cdns_dsi_suspend(struct device *dev)
1170 {
1171 	struct cdns_dsi *dsi = dev_get_drvdata(dev);
1172 
1173 	clk_disable_unprepare(dsi->dsi_sys_clk);
1174 	clk_disable_unprepare(dsi->dsi_p_clk);
1175 	reset_control_assert(dsi->dsi_p_rst);
1176 	return 0;
1177 }
1178 
1179 static UNIVERSAL_DEV_PM_OPS(cdns_dsi_pm_ops, cdns_dsi_suspend, cdns_dsi_resume,
1180 			    NULL);
1181 
cdns_dsi_drm_probe(struct platform_device * pdev)1182 static int cdns_dsi_drm_probe(struct platform_device *pdev)
1183 {
1184 	struct cdns_dsi *dsi;
1185 	struct cdns_dsi_input *input;
1186 	int ret, irq;
1187 	u32 val;
1188 
1189 	dsi = devm_kzalloc(&pdev->dev, sizeof(*dsi), GFP_KERNEL);
1190 	if (!dsi)
1191 		return -ENOMEM;
1192 
1193 	platform_set_drvdata(pdev, dsi);
1194 
1195 	input = &dsi->input;
1196 
1197 	dsi->regs = devm_platform_ioremap_resource(pdev, 0);
1198 	if (IS_ERR(dsi->regs))
1199 		return PTR_ERR(dsi->regs);
1200 
1201 	dsi->dsi_p_clk = devm_clk_get(&pdev->dev, "dsi_p_clk");
1202 	if (IS_ERR(dsi->dsi_p_clk))
1203 		return PTR_ERR(dsi->dsi_p_clk);
1204 
1205 	dsi->dsi_p_rst = devm_reset_control_get_optional_exclusive(&pdev->dev,
1206 								"dsi_p_rst");
1207 	if (IS_ERR(dsi->dsi_p_rst))
1208 		return PTR_ERR(dsi->dsi_p_rst);
1209 
1210 	dsi->dsi_sys_clk = devm_clk_get(&pdev->dev, "dsi_sys_clk");
1211 	if (IS_ERR(dsi->dsi_sys_clk))
1212 		return PTR_ERR(dsi->dsi_sys_clk);
1213 
1214 	irq = platform_get_irq(pdev, 0);
1215 	if (irq < 0)
1216 		return irq;
1217 
1218 	dsi->dphy = devm_phy_get(&pdev->dev, "dphy");
1219 	if (IS_ERR(dsi->dphy))
1220 		return PTR_ERR(dsi->dphy);
1221 
1222 	ret = clk_prepare_enable(dsi->dsi_p_clk);
1223 	if (ret)
1224 		return ret;
1225 
1226 	val = readl(dsi->regs + ID_REG);
1227 	if (REV_VENDOR_ID(val) != 0xcad) {
1228 		dev_err(&pdev->dev, "invalid vendor id\n");
1229 		ret = -EINVAL;
1230 		goto err_disable_pclk;
1231 	}
1232 
1233 	dsi->platform_ops = of_device_get_match_data(&pdev->dev);
1234 
1235 	val = readl(dsi->regs + IP_CONF);
1236 	dsi->direct_cmd_fifo_depth = 1 << (DIRCMD_FIFO_DEPTH(val) + 2);
1237 	dsi->rx_fifo_depth = RX_FIFO_DEPTH(val);
1238 	init_completion(&dsi->direct_cmd_comp);
1239 
1240 	writel(0, dsi->regs + MCTL_MAIN_DATA_CTL);
1241 	writel(0, dsi->regs + MCTL_MAIN_EN);
1242 	writel(0, dsi->regs + MCTL_MAIN_PHY_CTL);
1243 
1244 	/*
1245 	 * We only support the DPI input, so force input->id to
1246 	 * CDNS_DPI_INPUT.
1247 	 */
1248 	input->id = CDNS_DPI_INPUT;
1249 	input->bridge.funcs = &cdns_dsi_bridge_funcs;
1250 	input->bridge.of_node = pdev->dev.of_node;
1251 
1252 	/* Mask all interrupts before registering the IRQ handler. */
1253 	writel(0, dsi->regs + MCTL_MAIN_STS_CTL);
1254 	writel(0, dsi->regs + MCTL_DPHY_ERR_CTL1);
1255 	writel(0, dsi->regs + CMD_MODE_STS_CTL);
1256 	writel(0, dsi->regs + DIRECT_CMD_STS_CTL);
1257 	writel(0, dsi->regs + DIRECT_CMD_RD_STS_CTL);
1258 	writel(0, dsi->regs + VID_MODE_STS_CTL);
1259 	writel(0, dsi->regs + TVG_STS_CTL);
1260 	writel(0, dsi->regs + DPI_IRQ_EN);
1261 	ret = devm_request_irq(&pdev->dev, irq, cdns_dsi_interrupt, 0,
1262 			       dev_name(&pdev->dev), dsi);
1263 	if (ret)
1264 		goto err_disable_pclk;
1265 
1266 	pm_runtime_enable(&pdev->dev);
1267 	dsi->base.dev = &pdev->dev;
1268 	dsi->base.ops = &cdns_dsi_ops;
1269 
1270 	if (dsi->platform_ops && dsi->platform_ops->init) {
1271 		ret = dsi->platform_ops->init(dsi);
1272 		if (ret != 0) {
1273 			dev_err(&pdev->dev, "platform initialization failed: %d\n",
1274 				ret);
1275 			goto err_disable_runtime_pm;
1276 		}
1277 	}
1278 
1279 	ret = mipi_dsi_host_register(&dsi->base);
1280 	if (ret)
1281 		goto err_deinit_platform;
1282 
1283 	clk_disable_unprepare(dsi->dsi_p_clk);
1284 
1285 	return 0;
1286 
1287 err_deinit_platform:
1288 	if (dsi->platform_ops && dsi->platform_ops->deinit)
1289 		dsi->platform_ops->deinit(dsi);
1290 
1291 err_disable_runtime_pm:
1292 	pm_runtime_disable(&pdev->dev);
1293 
1294 err_disable_pclk:
1295 	clk_disable_unprepare(dsi->dsi_p_clk);
1296 
1297 	return ret;
1298 }
1299 
cdns_dsi_drm_remove(struct platform_device * pdev)1300 static void cdns_dsi_drm_remove(struct platform_device *pdev)
1301 {
1302 	struct cdns_dsi *dsi = platform_get_drvdata(pdev);
1303 
1304 	mipi_dsi_host_unregister(&dsi->base);
1305 
1306 	if (dsi->platform_ops && dsi->platform_ops->deinit)
1307 		dsi->platform_ops->deinit(dsi);
1308 
1309 	pm_runtime_disable(&pdev->dev);
1310 }
1311 
1312 static const struct of_device_id cdns_dsi_of_match[] = {
1313 	{ .compatible = "cdns,dsi" },
1314 #ifdef CONFIG_DRM_CDNS_DSI_J721E
1315 	{ .compatible = "ti,j721e-dsi", .data = &dsi_ti_j721e_ops, },
1316 #endif
1317 	{ },
1318 };
1319 MODULE_DEVICE_TABLE(of, cdns_dsi_of_match);
1320 
1321 static struct platform_driver cdns_dsi_platform_driver = {
1322 	.probe  = cdns_dsi_drm_probe,
1323 	.remove_new = cdns_dsi_drm_remove,
1324 	.driver = {
1325 		.name   = "cdns-dsi",
1326 		.of_match_table = cdns_dsi_of_match,
1327 		.pm = &cdns_dsi_pm_ops,
1328 	},
1329 };
1330 module_platform_driver(cdns_dsi_platform_driver);
1331 
1332 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@bootlin.com>");
1333 MODULE_DESCRIPTION("Cadence DSI driver");
1334 MODULE_LICENSE("GPL");
1335 MODULE_ALIAS("platform:cdns-dsi");
1336 
1337