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