xref: /linux/drivers/gpu/drm/bridge/samsung-dsim.c (revision 4e445729dc103ff7780ed03da9f8310759ea5dae)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Samsung MIPI DSIM bridge driver.
4  *
5  * Copyright (C) 2021 Amarula Solutions(India)
6  * Copyright (c) 2014 Samsung Electronics Co., Ltd
7  * Author: Jagan Teki <jagan@amarulasolutions.com>
8  *
9  * Based on exynos_drm_dsi from
10  * Tomasz Figa <t.figa@samsung.com>
11  */
12 
13 #include <linux/unaligned.h>
14 
15 #include <linux/clk.h>
16 #include <linux/delay.h>
17 #include <linux/export.h>
18 #include <linux/irq.h>
19 #include <linux/media-bus-format.h>
20 #include <linux/of.h>
21 #include <linux/phy/phy.h>
22 #include <linux/platform_device.h>
23 #include <linux/units.h>
24 
25 #include <video/mipi_display.h>
26 
27 #include <drm/bridge/samsung-dsim.h>
28 #include <drm/drm_panel.h>
29 #include <drm/drm_print.h>
30 
31 /* returns true iff both arguments logically differs */
32 #define NEQV(a, b) (!(a) ^ !(b))
33 
34 /* DSIM_STATUS or DSIM_DPHY_STATUS */
35 #define DSIM_STOP_STATE_DAT(x)		(((x) & 0xf) << 0)
36 #define DSIM_STOP_STATE_CLK		BIT(8)
37 #define DSIM_TX_READY_HS_CLK		BIT(10)
38 #define DSIM_PLL_STABLE			BIT(31)
39 
40 /* DSIM_SWRST */
41 #define DSIM_FUNCRST			BIT(16)
42 #define DSIM_SWRST			BIT(0)
43 
44 /* DSIM_TIMEOUT */
45 #define DSIM_LPDR_TIMEOUT(x)		((x) << 0)
46 #define DSIM_BTA_TIMEOUT(x)		((x) << 16)
47 
48 /* DSIM_CLKCTRL */
49 #define DSIM_ESC_PRESCALER(x)		(((x) & 0xffff) << 0)
50 #define DSIM_ESC_PRESCALER_MASK		(0xffff << 0)
51 #define DSIM_LANE_ESC_CLK_EN_CLK	BIT(19)
52 #define DSIM_LANE_ESC_CLK_EN_DATA(x)	(((x) & 0xf) << 20)
53 #define DSIM_LANE_ESC_CLK_EN_DATA_MASK	(0xf << 20)
54 #define DSIM_BYTE_CLKEN			BIT(24)
55 #define DSIM_BYTE_CLK_SRC(x)		(((x) & 0x3) << 25)
56 #define DSIM_BYTE_CLK_SRC_MASK		(0x3 << 25)
57 #define DSIM_PLL_BYPASS			BIT(27)
58 #define DSIM_ESC_CLKEN			BIT(28)
59 #define DSIM_TX_REQUEST_HSCLK		BIT(31)
60 
61 /* DSIM_CONFIG */
62 #define DSIM_LANE_EN_CLK		BIT(0)
63 #define DSIM_LANE_EN(x)			(((x) & 0xf) << 1)
64 #define DSIM_NUM_OF_DATA_LANE(x)	(((x) & 0x3) << 5)
65 #define DSIM_SUB_PIX_FORMAT(x)		(((x) & 0x7) << 8)
66 #define DSIM_MAIN_PIX_FORMAT_MASK	(0x7 << 12)
67 #define DSIM_MAIN_PIX_FORMAT_RGB888	(0x7 << 12)
68 #define DSIM_MAIN_PIX_FORMAT_RGB666	(0x6 << 12)
69 #define DSIM_MAIN_PIX_FORMAT_RGB666_P	(0x5 << 12)
70 #define DSIM_MAIN_PIX_FORMAT_RGB565	(0x4 << 12)
71 #define DSIM_SUB_VC			(((x) & 0x3) << 16)
72 #define DSIM_MAIN_VC			(((x) & 0x3) << 18)
73 #define DSIM_HSA_DISABLE_MODE		BIT(20)
74 #define DSIM_HBP_DISABLE_MODE		BIT(21)
75 #define DSIM_HFP_DISABLE_MODE		BIT(22)
76 /*
77  * The i.MX 8M Mini Applications Processor Reference Manual,
78  * Rev. 3, 11/2020 Page 4091
79  * The i.MX 8M Nano Applications Processor Reference Manual,
80  * Rev. 2, 07/2022 Page 3058
81  * The i.MX 8M Plus Applications Processor Reference Manual,
82  * Rev. 1, 06/2021 Page 5436
83  * all claims this bit is 'HseDisableMode' with the definition
84  * 0 = Disables transfer
85  * 1 = Enables transfer
86  *
87  * This clearly states that HSE is not a disabled bit.
88  *
89  * The naming convention follows as per the manual and the
90  * driver logic is based on the MIPI_DSI_MODE_VIDEO_HSE flag.
91  */
92 #define DSIM_HSE_DISABLE_MODE		BIT(23)
93 #define DSIM_AUTO_MODE			BIT(24)
94 #define DSIM_VIDEO_MODE			BIT(25)
95 #define DSIM_BURST_MODE			BIT(26)
96 #define DSIM_SYNC_INFORM		BIT(27)
97 #define DSIM_EOT_DISABLE		BIT(28)
98 #define DSIM_MFLUSH_VS			BIT(29)
99 /* This flag is valid only for exynos3250/3472/5260/5430 */
100 #define DSIM_CLKLANE_STOP		BIT(30)
101 #define DSIM_NON_CONTINUOUS_CLKLANE	BIT(31)
102 
103 /* DSIM_ESCMODE */
104 #define DSIM_TX_TRIGGER_RST		BIT(4)
105 #define DSIM_TX_LPDT_LP			BIT(6)
106 #define DSIM_CMD_LPDT_LP		BIT(7)
107 #define DSIM_FORCE_BTA			BIT(16)
108 #define DSIM_FORCE_STOP_STATE		BIT(20)
109 #define DSIM_STOP_STATE_CNT(x)		(((x) & 0x7ff) << 21)
110 #define DSIM_STOP_STATE_CNT_MASK	(0x7ff << 21)
111 
112 /* DSIM_MDRESOL */
113 #define DSIM_MAIN_STAND_BY		BIT(31)
114 #define DSIM_MAIN_VRESOL(x, num_bits)	(((x) & ((1 << (num_bits)) - 1)) << 16)
115 #define DSIM_MAIN_HRESOL(x, num_bits)	(((x) & ((1 << (num_bits)) - 1)) << 0)
116 
117 /* DSIM_MVPORCH */
118 #define DSIM_CMD_ALLOW(x)		((x) << 28)
119 #define DSIM_STABLE_VFP(x)		((x) << 16)
120 #define DSIM_MAIN_VBP(x)		((x) << 0)
121 #define DSIM_CMD_ALLOW_MASK		(0xf << 28)
122 #define DSIM_STABLE_VFP_MASK		(0x7ff << 16)
123 #define DSIM_MAIN_VBP_MASK		(0x7ff << 0)
124 
125 /* DSIM_MHPORCH */
126 #define DSIM_MAIN_HFP(x)		((x) << 16)
127 #define DSIM_MAIN_HBP(x)		((x) << 0)
128 #define DSIM_MAIN_HFP_MASK		((0xffff) << 16)
129 #define DSIM_MAIN_HBP_MASK		((0xffff) << 0)
130 
131 /* DSIM_MSYNC */
132 #define DSIM_MAIN_VSA(x)		((x) << 22)
133 #define DSIM_MAIN_HSA(x)		((x) << 0)
134 #define DSIM_MAIN_VSA_MASK		((0x3ff) << 22)
135 #define DSIM_MAIN_HSA_MASK		((0xffff) << 0)
136 
137 /* DSIM_SDRESOL */
138 #define DSIM_SUB_STANDY(x)		((x) << 31)
139 #define DSIM_SUB_VRESOL(x)		((x) << 16)
140 #define DSIM_SUB_HRESOL(x)		((x) << 0)
141 #define DSIM_SUB_STANDY_MASK		((0x1) << 31)
142 #define DSIM_SUB_VRESOL_MASK		((0x7ff) << 16)
143 #define DSIM_SUB_HRESOL_MASK		((0x7ff) << 0)
144 
145 /* DSIM_INTSRC */
146 #define DSIM_INT_PLL_STABLE		BIT(31)
147 #define DSIM_INT_SW_RST_RELEASE		BIT(30)
148 #define DSIM_INT_SFR_FIFO_EMPTY		BIT(29)
149 #define DSIM_INT_SFR_HDR_FIFO_EMPTY	BIT(28)
150 #define DSIM_INT_BTA			BIT(25)
151 #define DSIM_INT_FRAME_DONE		BIT(24)
152 #define DSIM_INT_RX_TIMEOUT		BIT(21)
153 #define DSIM_INT_BTA_TIMEOUT		BIT(20)
154 #define DSIM_INT_RX_DONE		BIT(18)
155 #define DSIM_INT_RX_TE			BIT(17)
156 #define DSIM_INT_RX_ACK			BIT(16)
157 #define DSIM_INT_RX_ECC_ERR		BIT(15)
158 #define DSIM_INT_RX_CRC_ERR		BIT(14)
159 
160 /* DSIM_FIFOCTRL */
161 #define DSIM_RX_DATA_FULL		BIT(25)
162 #define DSIM_RX_DATA_EMPTY		BIT(24)
163 #define DSIM_SFR_HEADER_FULL		BIT(23)
164 #define DSIM_SFR_HEADER_EMPTY		BIT(22)
165 #define DSIM_SFR_PAYLOAD_FULL		BIT(21)
166 #define DSIM_SFR_PAYLOAD_EMPTY		BIT(20)
167 #define DSIM_I80_HEADER_FULL		BIT(19)
168 #define DSIM_I80_HEADER_EMPTY		BIT(18)
169 #define DSIM_I80_PAYLOAD_FULL		BIT(17)
170 #define DSIM_I80_PAYLOAD_EMPTY		BIT(16)
171 #define DSIM_SD_HEADER_FULL		BIT(15)
172 #define DSIM_SD_HEADER_EMPTY		BIT(14)
173 #define DSIM_SD_PAYLOAD_FULL		BIT(13)
174 #define DSIM_SD_PAYLOAD_EMPTY		BIT(12)
175 #define DSIM_MD_HEADER_FULL		BIT(11)
176 #define DSIM_MD_HEADER_EMPTY		BIT(10)
177 #define DSIM_MD_PAYLOAD_FULL		BIT(9)
178 #define DSIM_MD_PAYLOAD_EMPTY		BIT(8)
179 #define DSIM_RX_FIFO			BIT(4)
180 #define DSIM_SFR_FIFO			BIT(3)
181 #define DSIM_I80_FIFO			BIT(2)
182 #define DSIM_SD_FIFO			BIT(1)
183 #define DSIM_MD_FIFO			BIT(0)
184 
185 /* DSIM_PHYACCHR */
186 #define DSIM_AFC_EN			BIT(14)
187 #define DSIM_AFC_CTL(x)			(((x) & 0x7) << 5)
188 
189 /* DSIM_PLLCTRL */
190 #define DSIM_PLL_DPDNSWAP_CLK		(1 << 25)
191 #define DSIM_PLL_DPDNSWAP_DAT		(1 << 24)
192 #define DSIM_FREQ_BAND(x)		((x) << 24)
193 #define DSIM_PLL_EN			BIT(23)
194 #define DSIM_PLL_P(x, offset)		((x) << (offset))
195 #define DSIM_PLL_M(x)			((x) << 4)
196 #define DSIM_PLL_S(x)			((x) << 1)
197 
198 /* DSIM_PHYCTRL */
199 #define DSIM_PHYCTRL_ULPS_EXIT(x)	(((x) & 0x1ff) << 0)
200 #define DSIM_PHYCTRL_B_DPHYCTL_VREG_LP	BIT(30)
201 #define DSIM_PHYCTRL_B_DPHYCTL_SLEW_UP	BIT(14)
202 
203 /* DSIM_PHYTIMING */
204 #define DSIM_PHYTIMING_LPX(x)		((x) << 8)
205 #define DSIM_PHYTIMING_HS_EXIT(x)	((x) << 0)
206 
207 /* DSIM_PHYTIMING1 */
208 #define DSIM_PHYTIMING1_CLK_PREPARE(x)	((x) << 24)
209 #define DSIM_PHYTIMING1_CLK_ZERO(x)	((x) << 16)
210 #define DSIM_PHYTIMING1_CLK_POST(x)	((x) << 8)
211 #define DSIM_PHYTIMING1_CLK_TRAIL(x)	((x) << 0)
212 
213 /* DSIM_PHYTIMING2 */
214 #define DSIM_PHYTIMING2_HS_PREPARE(x)	((x) << 16)
215 #define DSIM_PHYTIMING2_HS_ZERO(x)	((x) << 8)
216 #define DSIM_PHYTIMING2_HS_TRAIL(x)	((x) << 0)
217 
218 #define DSI_MAX_BUS_WIDTH		4
219 #define DSI_NUM_VIRTUAL_CHANNELS	4
220 #define DSI_TX_FIFO_SIZE		2048
221 #define DSI_RX_FIFO_SIZE		256
222 #define DSI_XFER_TIMEOUT_MS		100
223 #define DSI_RX_FIFO_EMPTY		0x30800002
224 
225 #define OLD_SCLK_MIPI_CLK_NAME		"pll_clk"
226 
227 #define PS_TO_CYCLE(ps, hz) DIV64_U64_ROUND_CLOSEST(((ps) * (hz)), 1000000000000ULL)
228 
229 static const char *const clk_names[5] = {
230 	"bus_clk",
231 	"sclk_mipi",
232 	"phyclk_mipidphy0_bitclkdiv8",
233 	"phyclk_mipidphy0_rxclkesc0",
234 	"sclk_rgb_vclk_to_dsim0"
235 };
236 
237 enum samsung_dsim_transfer_type {
238 	EXYNOS_DSI_TX,
239 	EXYNOS_DSI_RX,
240 };
241 
242 enum reg_idx {
243 	DSIM_STATUS_REG,	/* Status register (legacy) */
244 	DSIM_LINK_STATUS_REG,	/* Link status register */
245 	DSIM_DPHY_STATUS_REG,	/* D-PHY status register */
246 	DSIM_SWRST_REG,		/* Software reset register */
247 	DSIM_CLKCTRL_REG,	/* Clock control register */
248 	DSIM_TIMEOUT_REG,	/* Time out register */
249 	DSIM_CONFIG_REG,	/* Configuration register */
250 	DSIM_ESCMODE_REG,	/* Escape mode register */
251 	DSIM_MDRESOL_REG,
252 	DSIM_MVPORCH_REG,	/* Main display Vporch register */
253 	DSIM_MHPORCH_REG,	/* Main display Hporch register */
254 	DSIM_MSYNC_REG,		/* Main display sync area register */
255 	DSIM_INTSRC_REG,	/* Interrupt source register */
256 	DSIM_INTMSK_REG,	/* Interrupt mask register */
257 	DSIM_PKTHDR_REG,	/* Packet Header FIFO register */
258 	DSIM_PAYLOAD_REG,	/* Payload FIFO register */
259 	DSIM_RXFIFO_REG,	/* Read FIFO register */
260 	DSIM_FIFOCTRL_REG,	/* FIFO status and control register */
261 	DSIM_PLLCTRL_REG,	/* PLL control register */
262 	DSIM_PHYCTRL_REG,
263 	DSIM_PHYTIMING_REG,
264 	DSIM_PHYTIMING1_REG,
265 	DSIM_PHYTIMING2_REG,
266 	NUM_REGS
267 };
268 
269 static const unsigned int exynos_reg_ofs[] = {
270 	[DSIM_STATUS_REG] =  0x00,
271 	[DSIM_SWRST_REG] =  0x04,
272 	[DSIM_CLKCTRL_REG] =  0x08,
273 	[DSIM_TIMEOUT_REG] =  0x0c,
274 	[DSIM_CONFIG_REG] =  0x10,
275 	[DSIM_ESCMODE_REG] =  0x14,
276 	[DSIM_MDRESOL_REG] =  0x18,
277 	[DSIM_MVPORCH_REG] =  0x1c,
278 	[DSIM_MHPORCH_REG] =  0x20,
279 	[DSIM_MSYNC_REG] =  0x24,
280 	[DSIM_INTSRC_REG] =  0x2c,
281 	[DSIM_INTMSK_REG] =  0x30,
282 	[DSIM_PKTHDR_REG] =  0x34,
283 	[DSIM_PAYLOAD_REG] =  0x38,
284 	[DSIM_RXFIFO_REG] =  0x3c,
285 	[DSIM_FIFOCTRL_REG] =  0x44,
286 	[DSIM_PLLCTRL_REG] =  0x4c,
287 	[DSIM_PHYCTRL_REG] =  0x5c,
288 	[DSIM_PHYTIMING_REG] =  0x64,
289 	[DSIM_PHYTIMING1_REG] =  0x68,
290 	[DSIM_PHYTIMING2_REG] =  0x6c,
291 };
292 
293 static const unsigned int exynos5433_reg_ofs[] = {
294 	[DSIM_STATUS_REG] = 0x04,
295 	[DSIM_SWRST_REG] = 0x0C,
296 	[DSIM_CLKCTRL_REG] = 0x10,
297 	[DSIM_TIMEOUT_REG] = 0x14,
298 	[DSIM_CONFIG_REG] = 0x18,
299 	[DSIM_ESCMODE_REG] = 0x1C,
300 	[DSIM_MDRESOL_REG] = 0x20,
301 	[DSIM_MVPORCH_REG] = 0x24,
302 	[DSIM_MHPORCH_REG] = 0x28,
303 	[DSIM_MSYNC_REG] = 0x2C,
304 	[DSIM_INTSRC_REG] = 0x34,
305 	[DSIM_INTMSK_REG] = 0x38,
306 	[DSIM_PKTHDR_REG] = 0x3C,
307 	[DSIM_PAYLOAD_REG] = 0x40,
308 	[DSIM_RXFIFO_REG] = 0x44,
309 	[DSIM_FIFOCTRL_REG] = 0x4C,
310 	[DSIM_PLLCTRL_REG] = 0x94,
311 	[DSIM_PHYCTRL_REG] = 0xA4,
312 	[DSIM_PHYTIMING_REG] = 0xB4,
313 	[DSIM_PHYTIMING1_REG] = 0xB8,
314 	[DSIM_PHYTIMING2_REG] = 0xBC,
315 };
316 
317 enum reg_value_idx {
318 	RESET_TYPE,
319 	PLL_TIMER,
320 	STOP_STATE_CNT,
321 	PHYCTRL_ULPS_EXIT,
322 	PHYCTRL_VREG_LP,
323 	PHYCTRL_SLEW_UP,
324 	PHYTIMING_LPX,
325 	PHYTIMING_HS_EXIT,
326 	PHYTIMING_CLK_PREPARE,
327 	PHYTIMING_CLK_ZERO,
328 	PHYTIMING_CLK_POST,
329 	PHYTIMING_CLK_TRAIL,
330 	PHYTIMING_HS_PREPARE,
331 	PHYTIMING_HS_ZERO,
332 	PHYTIMING_HS_TRAIL
333 };
334 
335 static const unsigned int reg_values[] = {
336 	[RESET_TYPE] = DSIM_SWRST,
337 	[PLL_TIMER] = 500,
338 	[STOP_STATE_CNT] = 0xf,
339 	[PHYCTRL_ULPS_EXIT] = DSIM_PHYCTRL_ULPS_EXIT(0x0af),
340 	[PHYCTRL_VREG_LP] = 0,
341 	[PHYCTRL_SLEW_UP] = 0,
342 	[PHYTIMING_LPX] = DSIM_PHYTIMING_LPX(0x06),
343 	[PHYTIMING_HS_EXIT] = DSIM_PHYTIMING_HS_EXIT(0x0b),
344 	[PHYTIMING_CLK_PREPARE] = DSIM_PHYTIMING1_CLK_PREPARE(0x07),
345 	[PHYTIMING_CLK_ZERO] = DSIM_PHYTIMING1_CLK_ZERO(0x27),
346 	[PHYTIMING_CLK_POST] = DSIM_PHYTIMING1_CLK_POST(0x0d),
347 	[PHYTIMING_CLK_TRAIL] = DSIM_PHYTIMING1_CLK_TRAIL(0x08),
348 	[PHYTIMING_HS_PREPARE] = DSIM_PHYTIMING2_HS_PREPARE(0x09),
349 	[PHYTIMING_HS_ZERO] = DSIM_PHYTIMING2_HS_ZERO(0x0d),
350 	[PHYTIMING_HS_TRAIL] = DSIM_PHYTIMING2_HS_TRAIL(0x0b),
351 };
352 
353 static const unsigned int exynos5422_reg_values[] = {
354 	[RESET_TYPE] = DSIM_SWRST,
355 	[PLL_TIMER] = 500,
356 	[STOP_STATE_CNT] = 0xf,
357 	[PHYCTRL_ULPS_EXIT] = DSIM_PHYCTRL_ULPS_EXIT(0xaf),
358 	[PHYCTRL_VREG_LP] = 0,
359 	[PHYCTRL_SLEW_UP] = 0,
360 	[PHYTIMING_LPX] = DSIM_PHYTIMING_LPX(0x08),
361 	[PHYTIMING_HS_EXIT] = DSIM_PHYTIMING_HS_EXIT(0x0d),
362 	[PHYTIMING_CLK_PREPARE] = DSIM_PHYTIMING1_CLK_PREPARE(0x09),
363 	[PHYTIMING_CLK_ZERO] = DSIM_PHYTIMING1_CLK_ZERO(0x30),
364 	[PHYTIMING_CLK_POST] = DSIM_PHYTIMING1_CLK_POST(0x0e),
365 	[PHYTIMING_CLK_TRAIL] = DSIM_PHYTIMING1_CLK_TRAIL(0x0a),
366 	[PHYTIMING_HS_PREPARE] = DSIM_PHYTIMING2_HS_PREPARE(0x0c),
367 	[PHYTIMING_HS_ZERO] = DSIM_PHYTIMING2_HS_ZERO(0x11),
368 	[PHYTIMING_HS_TRAIL] = DSIM_PHYTIMING2_HS_TRAIL(0x0d),
369 };
370 
371 static const unsigned int exynos5433_reg_values[] = {
372 	[RESET_TYPE] = DSIM_FUNCRST,
373 	[PLL_TIMER] = 22200,
374 	[STOP_STATE_CNT] = 0xa,
375 	[PHYCTRL_ULPS_EXIT] = DSIM_PHYCTRL_ULPS_EXIT(0x190),
376 	[PHYCTRL_VREG_LP] = DSIM_PHYCTRL_B_DPHYCTL_VREG_LP,
377 	[PHYCTRL_SLEW_UP] = DSIM_PHYCTRL_B_DPHYCTL_SLEW_UP,
378 	[PHYTIMING_LPX] = DSIM_PHYTIMING_LPX(0x07),
379 	[PHYTIMING_HS_EXIT] = DSIM_PHYTIMING_HS_EXIT(0x0c),
380 	[PHYTIMING_CLK_PREPARE] = DSIM_PHYTIMING1_CLK_PREPARE(0x09),
381 	[PHYTIMING_CLK_ZERO] = DSIM_PHYTIMING1_CLK_ZERO(0x2d),
382 	[PHYTIMING_CLK_POST] = DSIM_PHYTIMING1_CLK_POST(0x0e),
383 	[PHYTIMING_CLK_TRAIL] = DSIM_PHYTIMING1_CLK_TRAIL(0x09),
384 	[PHYTIMING_HS_PREPARE] = DSIM_PHYTIMING2_HS_PREPARE(0x0b),
385 	[PHYTIMING_HS_ZERO] = DSIM_PHYTIMING2_HS_ZERO(0x10),
386 	[PHYTIMING_HS_TRAIL] = DSIM_PHYTIMING2_HS_TRAIL(0x0c),
387 };
388 
389 static const unsigned int imx8mm_dsim_reg_values[] = {
390 	[RESET_TYPE] = DSIM_SWRST,
391 	[PLL_TIMER] = 500,
392 	[STOP_STATE_CNT] = 0xf,
393 	[PHYCTRL_ULPS_EXIT] = DSIM_PHYCTRL_ULPS_EXIT(0xaf),
394 	[PHYCTRL_VREG_LP] = 0,
395 	[PHYCTRL_SLEW_UP] = 0,
396 	[PHYTIMING_LPX] = DSIM_PHYTIMING_LPX(0x06),
397 	[PHYTIMING_HS_EXIT] = DSIM_PHYTIMING_HS_EXIT(0x0b),
398 	[PHYTIMING_CLK_PREPARE] = DSIM_PHYTIMING1_CLK_PREPARE(0x07),
399 	[PHYTIMING_CLK_ZERO] = DSIM_PHYTIMING1_CLK_ZERO(0x26),
400 	[PHYTIMING_CLK_POST] = DSIM_PHYTIMING1_CLK_POST(0x0d),
401 	[PHYTIMING_CLK_TRAIL] = DSIM_PHYTIMING1_CLK_TRAIL(0x08),
402 	[PHYTIMING_HS_PREPARE] = DSIM_PHYTIMING2_HS_PREPARE(0x08),
403 	[PHYTIMING_HS_ZERO] = DSIM_PHYTIMING2_HS_ZERO(0x0d),
404 	[PHYTIMING_HS_TRAIL] = DSIM_PHYTIMING2_HS_TRAIL(0x0b),
405 };
406 
407 static const struct samsung_dsim_driver_data exynos3_dsi_driver_data = {
408 	.reg_ofs = exynos_reg_ofs,
409 	.plltmr_reg = 0x50,
410 	.has_legacy_status_reg = 1,
411 	.has_freqband = 1,
412 	.has_clklane_stop = 1,
413 	.num_clks = 2,
414 	.max_freq = 1000,
415 	.wait_for_reset = 1,
416 	.num_bits_resol = 11,
417 	.pll_p_offset = 13,
418 	.reg_values = reg_values,
419 	.pll_fin_min = 6,
420 	.pll_fin_max = 12,
421 	.m_min = 41,
422 	.m_max = 125,
423 	.min_freq = 500,
424 	.has_broken_fifoctrl_emptyhdr = 1,
425 };
426 
427 static const struct samsung_dsim_driver_data exynos4_dsi_driver_data = {
428 	.reg_ofs = exynos_reg_ofs,
429 	.plltmr_reg = 0x50,
430 	.has_legacy_status_reg = 1,
431 	.has_freqband = 1,
432 	.has_clklane_stop = 1,
433 	.num_clks = 2,
434 	.max_freq = 1000,
435 	.wait_for_reset = 1,
436 	.num_bits_resol = 11,
437 	.pll_p_offset = 13,
438 	.reg_values = reg_values,
439 	.pll_fin_min = 6,
440 	.pll_fin_max = 12,
441 	.m_min = 41,
442 	.m_max = 125,
443 	.min_freq = 500,
444 	.has_broken_fifoctrl_emptyhdr = 1,
445 };
446 
447 static const struct samsung_dsim_driver_data exynos5_dsi_driver_data = {
448 	.reg_ofs = exynos_reg_ofs,
449 	.plltmr_reg = 0x58,
450 	.has_legacy_status_reg = 1,
451 	.num_clks = 2,
452 	.max_freq = 1000,
453 	.wait_for_reset = 1,
454 	.num_bits_resol = 11,
455 	.pll_p_offset = 13,
456 	.reg_values = reg_values,
457 	.pll_fin_min = 6,
458 	.pll_fin_max = 12,
459 	.m_min = 41,
460 	.m_max = 125,
461 	.min_freq = 500,
462 };
463 
464 static const struct samsung_dsim_driver_data exynos5433_dsi_driver_data = {
465 	.reg_ofs = exynos5433_reg_ofs,
466 	.plltmr_reg = 0xa0,
467 	.has_legacy_status_reg = 1,
468 	.has_clklane_stop = 1,
469 	.num_clks = 5,
470 	.max_freq = 1500,
471 	.wait_for_reset = 0,
472 	.num_bits_resol = 12,
473 	.pll_p_offset = 13,
474 	.reg_values = exynos5433_reg_values,
475 	.pll_fin_min = 6,
476 	.pll_fin_max = 12,
477 	.m_min = 41,
478 	.m_max = 125,
479 	.min_freq = 500,
480 };
481 
482 static const struct samsung_dsim_driver_data exynos5422_dsi_driver_data = {
483 	.reg_ofs = exynos5433_reg_ofs,
484 	.plltmr_reg = 0xa0,
485 	.has_legacy_status_reg = 1,
486 	.has_clklane_stop = 1,
487 	.num_clks = 2,
488 	.max_freq = 1500,
489 	.wait_for_reset = 1,
490 	.num_bits_resol = 12,
491 	.pll_p_offset = 13,
492 	.reg_values = exynos5422_reg_values,
493 	.pll_fin_min = 6,
494 	.pll_fin_max = 12,
495 	.m_min = 41,
496 	.m_max = 125,
497 	.min_freq = 500,
498 };
499 
500 static const struct samsung_dsim_driver_data imx8mm_dsi_driver_data = {
501 	.reg_ofs = exynos5433_reg_ofs,
502 	.plltmr_reg = 0xa0,
503 	.has_legacy_status_reg = 1,
504 	.has_clklane_stop = 1,
505 	.num_clks = 2,
506 	.max_freq = 2100,
507 	.wait_for_reset = 0,
508 	.num_bits_resol = 12,
509 	/*
510 	 * Unlike Exynos, PLL_P(PMS_P) offset 14 is used in i.MX8M Mini/Nano/Plus
511 	 * downstream driver - drivers/gpu/drm/bridge/sec-dsim.c
512 	 */
513 	.pll_p_offset = 14,
514 	.reg_values = imx8mm_dsim_reg_values,
515 	.pll_fin_min = 2,
516 	.pll_fin_max = 30,
517 	.m_min = 64,
518 	.m_max = 1023,
519 	.min_freq = 1050,
520 };
521 
522 static const struct samsung_dsim_driver_data *
523 samsung_dsim_types[DSIM_TYPE_COUNT] = {
524 	[DSIM_TYPE_EXYNOS3250] = &exynos3_dsi_driver_data,
525 	[DSIM_TYPE_EXYNOS4210] = &exynos4_dsi_driver_data,
526 	[DSIM_TYPE_EXYNOS5410] = &exynos5_dsi_driver_data,
527 	[DSIM_TYPE_EXYNOS5422] = &exynos5422_dsi_driver_data,
528 	[DSIM_TYPE_EXYNOS5433] = &exynos5433_dsi_driver_data,
529 	[DSIM_TYPE_IMX8MM] = &imx8mm_dsi_driver_data,
530 	[DSIM_TYPE_IMX8MP] = &imx8mm_dsi_driver_data,
531 };
532 
533 static inline struct samsung_dsim *host_to_dsi(struct mipi_dsi_host *h)
534 {
535 	return container_of(h, struct samsung_dsim, dsi_host);
536 }
537 
538 static inline struct samsung_dsim *bridge_to_dsi(struct drm_bridge *b)
539 {
540 	return container_of(b, struct samsung_dsim, bridge);
541 }
542 
543 static inline void samsung_dsim_write(struct samsung_dsim *dsi,
544 				      enum reg_idx idx, u32 val)
545 {
546 	writel(val, dsi->reg_base + dsi->driver_data->reg_ofs[idx]);
547 }
548 
549 static inline u32 samsung_dsim_read(struct samsung_dsim *dsi, enum reg_idx idx)
550 {
551 	return readl(dsi->reg_base + dsi->driver_data->reg_ofs[idx]);
552 }
553 
554 static void samsung_dsim_wait_for_reset(struct samsung_dsim *dsi)
555 {
556 	if (wait_for_completion_timeout(&dsi->completed, msecs_to_jiffies(300)))
557 		return;
558 
559 	dev_err(dsi->dev, "timeout waiting for reset\n");
560 }
561 
562 static void samsung_dsim_reset(struct samsung_dsim *dsi)
563 {
564 	u32 reset_val = dsi->driver_data->reg_values[RESET_TYPE];
565 
566 	reinit_completion(&dsi->completed);
567 	samsung_dsim_write(dsi, DSIM_SWRST_REG, reset_val);
568 }
569 
570 static unsigned long samsung_dsim_pll_find_pms(struct samsung_dsim *dsi,
571 					       unsigned long fin,
572 					       unsigned long fout,
573 					       u8 *p, u16 *m, u8 *s)
574 {
575 	const struct samsung_dsim_driver_data *driver_data = dsi->driver_data;
576 	unsigned long best_freq = 0;
577 	u32 min_delta = 0xffffffff;
578 	u8 p_min, p_max;
579 	u8 _p, best_p;
580 	u16 _m, best_m;
581 	u8 _s, best_s;
582 
583 	p_min = DIV_ROUND_UP(fin, (driver_data->pll_fin_max * HZ_PER_MHZ));
584 	p_max = fin / (driver_data->pll_fin_min * HZ_PER_MHZ);
585 
586 	for (_p = p_min; _p <= p_max; ++_p) {
587 		for (_s = 0; _s <= 5; ++_s) {
588 			u64 tmp;
589 			u32 delta;
590 
591 			tmp = (u64)fout * (_p << _s);
592 			do_div(tmp, fin);
593 			_m = tmp;
594 			if (_m < driver_data->m_min || _m > driver_data->m_max)
595 				continue;
596 
597 			tmp = (u64)_m * fin;
598 			do_div(tmp, _p);
599 			if (tmp < driver_data->min_freq  * HZ_PER_MHZ ||
600 			    tmp > driver_data->max_freq * HZ_PER_MHZ)
601 				continue;
602 
603 			tmp = (u64)_m * fin;
604 			do_div(tmp, _p << _s);
605 
606 			delta = abs(fout - tmp);
607 			if (delta < min_delta) {
608 				best_p = _p;
609 				best_m = _m;
610 				best_s = _s;
611 				min_delta = delta;
612 				best_freq = tmp;
613 			}
614 		}
615 	}
616 
617 	if (best_freq) {
618 		*p = best_p;
619 		*m = best_m;
620 		*s = best_s;
621 	}
622 
623 	return best_freq;
624 }
625 
626 static unsigned long samsung_dsim_set_pll(struct samsung_dsim *dsi,
627 					  unsigned long freq)
628 {
629 	const struct samsung_dsim_driver_data *driver_data = dsi->driver_data;
630 	unsigned long fin, fout;
631 	int timeout;
632 	u8 p, s;
633 	u16 m;
634 	u32 reg;
635 
636 	if (dsi->pll_clk) {
637 		/*
638 		 * Ensure that the reference clock is generated with a power of
639 		 * two divider from its parent, but close to the PLLs upper
640 		 * limit.
641 		 */
642 		fin = clk_get_rate(clk_get_parent(dsi->pll_clk));
643 		while (fin > driver_data->pll_fin_max * HZ_PER_MHZ)
644 			fin /= 2;
645 		clk_set_rate(dsi->pll_clk, fin);
646 
647 		fin = clk_get_rate(dsi->pll_clk);
648 	} else {
649 		fin = dsi->pll_clk_rate;
650 	}
651 	dev_dbg(dsi->dev, "PLL ref clock freq %lu\n", fin);
652 
653 	fout = samsung_dsim_pll_find_pms(dsi, fin, freq, &p, &m, &s);
654 	if (!fout) {
655 		dev_err(dsi->dev,
656 			"failed to find PLL PMS for requested frequency\n");
657 		return 0;
658 	}
659 	dev_dbg(dsi->dev, "PLL freq %lu, (p %d, m %d, s %d)\n", fout, p, m, s);
660 
661 	writel(driver_data->reg_values[PLL_TIMER],
662 	       dsi->reg_base + driver_data->plltmr_reg);
663 
664 	reg = DSIM_PLL_EN | DSIM_PLL_P(p, driver_data->pll_p_offset) |
665 	      DSIM_PLL_M(m) | DSIM_PLL_S(s);
666 
667 	if (driver_data->has_freqband) {
668 		static const unsigned long freq_bands[] = {
669 			100 * HZ_PER_MHZ, 120 * HZ_PER_MHZ, 160 * HZ_PER_MHZ,
670 			200 * HZ_PER_MHZ, 270 * HZ_PER_MHZ, 320 * HZ_PER_MHZ,
671 			390 * HZ_PER_MHZ, 450 * HZ_PER_MHZ, 510 * HZ_PER_MHZ,
672 			560 * HZ_PER_MHZ, 640 * HZ_PER_MHZ, 690 * HZ_PER_MHZ,
673 			770 * HZ_PER_MHZ, 870 * HZ_PER_MHZ, 950 * HZ_PER_MHZ,
674 		};
675 		int band;
676 
677 		for (band = 0; band < ARRAY_SIZE(freq_bands); ++band)
678 			if (fout < freq_bands[band])
679 				break;
680 
681 		dev_dbg(dsi->dev, "band %d\n", band);
682 
683 		reg |= DSIM_FREQ_BAND(band);
684 	}
685 
686 	if (dsi->swap_dn_dp_clk)
687 		reg |= DSIM_PLL_DPDNSWAP_CLK;
688 	if (dsi->swap_dn_dp_data)
689 		reg |= DSIM_PLL_DPDNSWAP_DAT;
690 
691 	samsung_dsim_write(dsi, DSIM_PLLCTRL_REG, reg);
692 
693 	timeout = 1000;
694 	do {
695 		if (timeout-- == 0) {
696 			dev_err(dsi->dev, "PLL failed to stabilize\n");
697 			return 0;
698 		}
699 		if (driver_data->has_legacy_status_reg)
700 			reg = samsung_dsim_read(dsi, DSIM_STATUS_REG);
701 		else
702 			reg = samsung_dsim_read(dsi, DSIM_LINK_STATUS_REG);
703 	} while ((reg & DSIM_PLL_STABLE) == 0);
704 
705 	dsi->hs_clock = fout;
706 
707 	return fout;
708 }
709 
710 static int samsung_dsim_enable_clock(struct samsung_dsim *dsi)
711 {
712 	unsigned long hs_clk, byte_clk, esc_clk, pix_clk;
713 	unsigned long esc_div;
714 	u32 reg;
715 	struct drm_display_mode *m = &dsi->mode;
716 	int bpp = mipi_dsi_pixel_format_to_bpp(dsi->format);
717 
718 	/* m->clock is in KHz */
719 	pix_clk = m->clock * 1000;
720 
721 	/* Use burst_clk_rate if available, otherwise use the pix_clk */
722 	if (dsi->burst_clk_rate)
723 		hs_clk = samsung_dsim_set_pll(dsi, dsi->burst_clk_rate);
724 	else
725 		hs_clk = samsung_dsim_set_pll(dsi, DIV_ROUND_UP(pix_clk * bpp, dsi->lanes));
726 
727 	if (!hs_clk) {
728 		dev_err(dsi->dev, "failed to configure DSI PLL\n");
729 		return -EFAULT;
730 	}
731 
732 	byte_clk = hs_clk / 8;
733 	esc_div = DIV_ROUND_UP(byte_clk, dsi->esc_clk_rate);
734 	esc_clk = byte_clk / esc_div;
735 
736 	if (esc_clk > 20 * HZ_PER_MHZ) {
737 		++esc_div;
738 		esc_clk = byte_clk / esc_div;
739 	}
740 
741 	dev_dbg(dsi->dev, "hs_clk = %lu, byte_clk = %lu, esc_clk = %lu\n",
742 		hs_clk, byte_clk, esc_clk);
743 
744 	reg = samsung_dsim_read(dsi, DSIM_CLKCTRL_REG);
745 	reg &= ~(DSIM_ESC_PRESCALER_MASK | DSIM_LANE_ESC_CLK_EN_CLK
746 			| DSIM_LANE_ESC_CLK_EN_DATA_MASK | DSIM_PLL_BYPASS
747 			| DSIM_BYTE_CLK_SRC_MASK);
748 	reg |= DSIM_ESC_CLKEN | DSIM_BYTE_CLKEN
749 			| DSIM_ESC_PRESCALER(esc_div)
750 			| DSIM_LANE_ESC_CLK_EN_CLK
751 			| DSIM_LANE_ESC_CLK_EN_DATA(BIT(dsi->lanes) - 1)
752 			| DSIM_BYTE_CLK_SRC(0)
753 			| DSIM_TX_REQUEST_HSCLK;
754 	samsung_dsim_write(dsi, DSIM_CLKCTRL_REG, reg);
755 
756 	return 0;
757 }
758 
759 static void samsung_dsim_set_phy_ctrl(struct samsung_dsim *dsi)
760 {
761 	const struct samsung_dsim_driver_data *driver_data = dsi->driver_data;
762 	const unsigned int *reg_values = driver_data->reg_values;
763 	u32 reg;
764 	struct phy_configure_opts_mipi_dphy cfg;
765 	int clk_prepare, lpx, clk_zero, clk_post, clk_trail;
766 	int hs_exit, hs_prepare, hs_zero, hs_trail;
767 	unsigned long long byte_clock = dsi->hs_clock / 8;
768 
769 	if (driver_data->has_freqband)
770 		return;
771 
772 	phy_mipi_dphy_get_default_config_for_hsclk(dsi->hs_clock,
773 						   dsi->lanes, &cfg);
774 
775 	/*
776 	 * TODO:
777 	 * The tech Applications Processor manuals for i.MX8M Mini, Nano,
778 	 * and Plus don't state what the definition of the PHYTIMING
779 	 * bits are beyond their address and bit position.
780 	 * After reviewing NXP's downstream code, it appears
781 	 * that the various PHYTIMING registers take the number
782 	 * of cycles and use various dividers on them.  This
783 	 * calculation does not result in an exact match to the
784 	 * downstream code, but it is very close to the values
785 	 * generated by their lookup table, and it appears
786 	 * to sync at a variety of resolutions. If someone
787 	 * can get a more accurate mathematical equation needed
788 	 * for these registers, this should be updated.
789 	 */
790 
791 	lpx = PS_TO_CYCLE(cfg.lpx, byte_clock);
792 	hs_exit = PS_TO_CYCLE(cfg.hs_exit, byte_clock);
793 	clk_prepare = PS_TO_CYCLE(cfg.clk_prepare, byte_clock);
794 	clk_zero = PS_TO_CYCLE(cfg.clk_zero, byte_clock);
795 	clk_post = PS_TO_CYCLE(cfg.clk_post, byte_clock);
796 	clk_trail = PS_TO_CYCLE(cfg.clk_trail, byte_clock);
797 	hs_prepare = PS_TO_CYCLE(cfg.hs_prepare, byte_clock);
798 	hs_zero = PS_TO_CYCLE(cfg.hs_zero, byte_clock);
799 	hs_trail = PS_TO_CYCLE(cfg.hs_trail, byte_clock);
800 
801 	/* B D-PHY: D-PHY Master & Slave Analog Block control */
802 	reg = reg_values[PHYCTRL_ULPS_EXIT] | reg_values[PHYCTRL_VREG_LP] |
803 		reg_values[PHYCTRL_SLEW_UP];
804 
805 	samsung_dsim_write(dsi, DSIM_PHYCTRL_REG, reg);
806 
807 	/*
808 	 * T LPX: Transmitted length of any Low-Power state period
809 	 * T HS-EXIT: Time that the transmitter drives LP-11 following a HS
810 	 *	burst
811 	 */
812 
813 	reg  = DSIM_PHYTIMING_LPX(lpx) | DSIM_PHYTIMING_HS_EXIT(hs_exit);
814 
815 	samsung_dsim_write(dsi, DSIM_PHYTIMING_REG, reg);
816 
817 	/*
818 	 * T CLK-PREPARE: Time that the transmitter drives the Clock Lane LP-00
819 	 *	Line state immediately before the HS-0 Line state starting the
820 	 *	HS transmission
821 	 * T CLK-ZERO: Time that the transmitter drives the HS-0 state prior to
822 	 *	transmitting the Clock.
823 	 * T CLK_POST: Time that the transmitter continues to send HS clock
824 	 *	after the last associated Data Lane has transitioned to LP Mode
825 	 *	Interval is defined as the period from the end of T HS-TRAIL to
826 	 *	the beginning of T CLK-TRAIL
827 	 * T CLK-TRAIL: Time that the transmitter drives the HS-0 state after
828 	 *	the last payload clock bit of a HS transmission burst
829 	 */
830 
831 	reg = DSIM_PHYTIMING1_CLK_PREPARE(clk_prepare)	|
832 	      DSIM_PHYTIMING1_CLK_ZERO(clk_zero)	|
833 	      DSIM_PHYTIMING1_CLK_POST(clk_post)	|
834 	      DSIM_PHYTIMING1_CLK_TRAIL(clk_trail);
835 
836 	samsung_dsim_write(dsi, DSIM_PHYTIMING1_REG, reg);
837 
838 	/*
839 	 * T HS-PREPARE: Time that the transmitter drives the Data Lane LP-00
840 	 *	Line state immediately before the HS-0 Line state starting the
841 	 *	HS transmission
842 	 * T HS-ZERO: Time that the transmitter drives the HS-0 state prior to
843 	 *	transmitting the Sync sequence.
844 	 * T HS-TRAIL: Time that the transmitter drives the flipped differential
845 	 *	state after last payload data bit of a HS transmission burst
846 	 */
847 
848 	reg = DSIM_PHYTIMING2_HS_PREPARE(hs_prepare) |
849 	      DSIM_PHYTIMING2_HS_ZERO(hs_zero) |
850 	      DSIM_PHYTIMING2_HS_TRAIL(hs_trail);
851 
852 	samsung_dsim_write(dsi, DSIM_PHYTIMING2_REG, reg);
853 }
854 
855 static void samsung_dsim_disable_clock(struct samsung_dsim *dsi)
856 {
857 	u32 reg;
858 
859 	reg = samsung_dsim_read(dsi, DSIM_CLKCTRL_REG);
860 	reg &= ~(DSIM_LANE_ESC_CLK_EN_CLK | DSIM_LANE_ESC_CLK_EN_DATA_MASK
861 			| DSIM_ESC_CLKEN | DSIM_BYTE_CLKEN);
862 	samsung_dsim_write(dsi, DSIM_CLKCTRL_REG, reg);
863 
864 	reg = samsung_dsim_read(dsi, DSIM_PLLCTRL_REG);
865 	reg &= ~DSIM_PLL_EN;
866 	samsung_dsim_write(dsi, DSIM_PLLCTRL_REG, reg);
867 }
868 
869 static void samsung_dsim_enable_lane(struct samsung_dsim *dsi, u32 lane)
870 {
871 	u32 reg = samsung_dsim_read(dsi, DSIM_CONFIG_REG);
872 
873 	reg |= (DSIM_NUM_OF_DATA_LANE(dsi->lanes - 1) | DSIM_LANE_EN_CLK |
874 			DSIM_LANE_EN(lane));
875 	samsung_dsim_write(dsi, DSIM_CONFIG_REG, reg);
876 }
877 
878 static int samsung_dsim_init_link(struct samsung_dsim *dsi)
879 {
880 	const struct samsung_dsim_driver_data *driver_data = dsi->driver_data;
881 	int timeout;
882 	u32 reg;
883 	u32 lanes_mask;
884 
885 	/* Initialize FIFO pointers */
886 	reg = samsung_dsim_read(dsi, DSIM_FIFOCTRL_REG);
887 	reg &= ~0x1f;
888 	samsung_dsim_write(dsi, DSIM_FIFOCTRL_REG, reg);
889 
890 	usleep_range(9000, 11000);
891 
892 	reg |= 0x1f;
893 	samsung_dsim_write(dsi, DSIM_FIFOCTRL_REG, reg);
894 	usleep_range(9000, 11000);
895 
896 	/* DSI configuration */
897 	reg = 0;
898 
899 	/*
900 	 * The first bit of mode_flags specifies display configuration.
901 	 * If this bit is set[= MIPI_DSI_MODE_VIDEO], dsi will support video
902 	 * mode, otherwise it will support command mode.
903 	 */
904 	if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO) {
905 		reg |= DSIM_VIDEO_MODE;
906 
907 		/*
908 		 * The user manual describes that following bits are ignored in
909 		 * command mode.
910 		 */
911 		if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
912 			reg |= DSIM_SYNC_INFORM;
913 		if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
914 			reg |= DSIM_BURST_MODE;
915 		if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_AUTO_VERT)
916 			reg |= DSIM_AUTO_MODE;
917 		if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_HSE)
918 			reg |= DSIM_HSE_DISABLE_MODE;
919 		if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_NO_HFP)
920 			reg |= DSIM_HFP_DISABLE_MODE;
921 		if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_NO_HBP)
922 			reg |= DSIM_HBP_DISABLE_MODE;
923 		if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_NO_HSA)
924 			reg |= DSIM_HSA_DISABLE_MODE;
925 	}
926 
927 	if (dsi->mode_flags & MIPI_DSI_MODE_NO_EOT_PACKET)
928 		reg |= DSIM_EOT_DISABLE;
929 
930 	switch (dsi->format) {
931 	case MIPI_DSI_FMT_RGB888:
932 		reg |= DSIM_MAIN_PIX_FORMAT_RGB888;
933 		break;
934 	case MIPI_DSI_FMT_RGB666:
935 		reg |= DSIM_MAIN_PIX_FORMAT_RGB666;
936 		break;
937 	case MIPI_DSI_FMT_RGB666_PACKED:
938 		reg |= DSIM_MAIN_PIX_FORMAT_RGB666_P;
939 		break;
940 	case MIPI_DSI_FMT_RGB565:
941 		reg |= DSIM_MAIN_PIX_FORMAT_RGB565;
942 		break;
943 	default:
944 		dev_err(dsi->dev, "invalid pixel format\n");
945 		return -EINVAL;
946 	}
947 
948 	/*
949 	 * Use non-continuous clock mode if the periparal wants and
950 	 * host controller supports
951 	 *
952 	 * In non-continous clock mode, host controller will turn off
953 	 * the HS clock between high-speed transmissions to reduce
954 	 * power consumption.
955 	 */
956 	if (driver_data->has_clklane_stop &&
957 	    dsi->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS) {
958 		if (!samsung_dsim_hw_is_exynos(dsi->plat_data->hw_type))
959 			reg |= DSIM_NON_CONTINUOUS_CLKLANE;
960 
961 		reg |= DSIM_CLKLANE_STOP;
962 	}
963 	samsung_dsim_write(dsi, DSIM_CONFIG_REG, reg);
964 
965 	lanes_mask = BIT(dsi->lanes) - 1;
966 	samsung_dsim_enable_lane(dsi, lanes_mask);
967 
968 	/* Check clock and data lane state are stop state */
969 	timeout = 100;
970 	do {
971 		if (timeout-- == 0) {
972 			dev_err(dsi->dev, "waiting for bus lanes timed out\n");
973 			return -EFAULT;
974 		}
975 
976 		if (driver_data->has_legacy_status_reg)
977 			reg = samsung_dsim_read(dsi, DSIM_STATUS_REG);
978 		else
979 			reg = samsung_dsim_read(dsi, DSIM_DPHY_STATUS_REG);
980 		if ((reg & DSIM_STOP_STATE_DAT(lanes_mask))
981 		    != DSIM_STOP_STATE_DAT(lanes_mask))
982 			continue;
983 	} while (!(reg & (DSIM_STOP_STATE_CLK | DSIM_TX_READY_HS_CLK)));
984 
985 	reg = samsung_dsim_read(dsi, DSIM_ESCMODE_REG);
986 	reg &= ~DSIM_STOP_STATE_CNT_MASK;
987 	reg |= DSIM_STOP_STATE_CNT(driver_data->reg_values[STOP_STATE_CNT]);
988 	samsung_dsim_write(dsi, DSIM_ESCMODE_REG, reg);
989 
990 	reg = DSIM_BTA_TIMEOUT(0xff) | DSIM_LPDR_TIMEOUT(0xffff);
991 	samsung_dsim_write(dsi, DSIM_TIMEOUT_REG, reg);
992 
993 	return 0;
994 }
995 
996 static void samsung_dsim_set_display_mode(struct samsung_dsim *dsi)
997 {
998 	struct drm_display_mode *m = &dsi->mode;
999 	unsigned int num_bits_resol = dsi->driver_data->num_bits_resol;
1000 	u32 reg;
1001 
1002 	if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO) {
1003 		u64 byte_clk = dsi->hs_clock / 8;
1004 		u64 pix_clk = m->clock * 1000;
1005 
1006 		int hfp = DIV64_U64_ROUND_UP((m->hsync_start - m->hdisplay) * byte_clk, pix_clk);
1007 		int hbp = DIV64_U64_ROUND_UP((m->htotal - m->hsync_end) * byte_clk, pix_clk);
1008 		int hsa = DIV64_U64_ROUND_UP((m->hsync_end - m->hsync_start) * byte_clk, pix_clk);
1009 
1010 		/* remove packet overhead when possible */
1011 		hfp = max(hfp - 6, 0);
1012 		hbp = max(hbp - 6, 0);
1013 		hsa = max(hsa - 6, 0);
1014 
1015 		dev_dbg(dsi->dev, "calculated hfp: %u, hbp: %u, hsa: %u",
1016 			hfp, hbp, hsa);
1017 
1018 		reg = DSIM_CMD_ALLOW(0xf)
1019 			| DSIM_STABLE_VFP(m->vsync_start - m->vdisplay)
1020 			| DSIM_MAIN_VBP(m->vtotal - m->vsync_end);
1021 		samsung_dsim_write(dsi, DSIM_MVPORCH_REG, reg);
1022 
1023 		reg = DSIM_MAIN_HFP(hfp) | DSIM_MAIN_HBP(hbp);
1024 		samsung_dsim_write(dsi, DSIM_MHPORCH_REG, reg);
1025 
1026 		reg = DSIM_MAIN_VSA(m->vsync_end - m->vsync_start)
1027 			| DSIM_MAIN_HSA(hsa);
1028 		samsung_dsim_write(dsi, DSIM_MSYNC_REG, reg);
1029 	}
1030 	reg =  DSIM_MAIN_HRESOL(m->hdisplay, num_bits_resol) |
1031 		DSIM_MAIN_VRESOL(m->vdisplay, num_bits_resol);
1032 
1033 	samsung_dsim_write(dsi, DSIM_MDRESOL_REG, reg);
1034 
1035 	dev_dbg(dsi->dev, "LCD size = %dx%d\n", m->hdisplay, m->vdisplay);
1036 }
1037 
1038 static void samsung_dsim_set_display_enable(struct samsung_dsim *dsi, bool enable)
1039 {
1040 	u32 reg;
1041 
1042 	reg = samsung_dsim_read(dsi, DSIM_MDRESOL_REG);
1043 	if (enable)
1044 		reg |= DSIM_MAIN_STAND_BY;
1045 	else
1046 		reg &= ~DSIM_MAIN_STAND_BY;
1047 	samsung_dsim_write(dsi, DSIM_MDRESOL_REG, reg);
1048 }
1049 
1050 static int samsung_dsim_wait_for_hdr_fifo(struct samsung_dsim *dsi)
1051 {
1052 	int timeout = 2000;
1053 
1054 	do {
1055 		u32 reg = samsung_dsim_read(dsi, DSIM_FIFOCTRL_REG);
1056 
1057 		if (!dsi->driver_data->has_broken_fifoctrl_emptyhdr) {
1058 			if (reg & DSIM_SFR_HEADER_EMPTY)
1059 				return 0;
1060 		} else {
1061 			if (!(reg & DSIM_SFR_HEADER_FULL)) {
1062 				/*
1063 				 * Wait a little bit, so the pending data can
1064 				 * actually leave the FIFO to avoid overflow.
1065 				 */
1066 				if (!cond_resched())
1067 					usleep_range(950, 1050);
1068 				return 0;
1069 			}
1070 		}
1071 
1072 		if (!cond_resched())
1073 			usleep_range(950, 1050);
1074 	} while (--timeout);
1075 
1076 	return -ETIMEDOUT;
1077 }
1078 
1079 static void samsung_dsim_set_cmd_lpm(struct samsung_dsim *dsi, bool lpm)
1080 {
1081 	u32 v = samsung_dsim_read(dsi, DSIM_ESCMODE_REG);
1082 
1083 	if (lpm)
1084 		v |= DSIM_CMD_LPDT_LP;
1085 	else
1086 		v &= ~DSIM_CMD_LPDT_LP;
1087 
1088 	samsung_dsim_write(dsi, DSIM_ESCMODE_REG, v);
1089 }
1090 
1091 static void samsung_dsim_force_bta(struct samsung_dsim *dsi)
1092 {
1093 	u32 v = samsung_dsim_read(dsi, DSIM_ESCMODE_REG);
1094 
1095 	v |= DSIM_FORCE_BTA;
1096 	samsung_dsim_write(dsi, DSIM_ESCMODE_REG, v);
1097 }
1098 
1099 static void samsung_dsim_send_to_fifo(struct samsung_dsim *dsi,
1100 				      struct samsung_dsim_transfer *xfer)
1101 {
1102 	struct device *dev = dsi->dev;
1103 	struct mipi_dsi_packet *pkt = &xfer->packet;
1104 	const u8 *payload = pkt->payload + xfer->tx_done;
1105 	u16 length = pkt->payload_length - xfer->tx_done;
1106 	bool first = !xfer->tx_done;
1107 	u32 reg;
1108 
1109 	dev_dbg(dev, "< xfer %p: tx len %u, done %u, rx len %u, done %u\n",
1110 		xfer, length, xfer->tx_done, xfer->rx_len, xfer->rx_done);
1111 
1112 	if (length > DSI_TX_FIFO_SIZE)
1113 		length = DSI_TX_FIFO_SIZE;
1114 
1115 	xfer->tx_done += length;
1116 
1117 	/* Send payload */
1118 	while (length >= 4) {
1119 		reg = get_unaligned_le32(payload);
1120 		samsung_dsim_write(dsi, DSIM_PAYLOAD_REG, reg);
1121 		payload += 4;
1122 		length -= 4;
1123 	}
1124 
1125 	reg = 0;
1126 	switch (length) {
1127 	case 3:
1128 		reg |= payload[2] << 16;
1129 		fallthrough;
1130 	case 2:
1131 		reg |= payload[1] << 8;
1132 		fallthrough;
1133 	case 1:
1134 		reg |= payload[0];
1135 		samsung_dsim_write(dsi, DSIM_PAYLOAD_REG, reg);
1136 		break;
1137 	}
1138 
1139 	/* Send packet header */
1140 	if (!first)
1141 		return;
1142 
1143 	reg = get_unaligned_le32(pkt->header);
1144 	if (samsung_dsim_wait_for_hdr_fifo(dsi)) {
1145 		dev_err(dev, "waiting for header FIFO timed out\n");
1146 		return;
1147 	}
1148 
1149 	if (NEQV(xfer->flags & MIPI_DSI_MSG_USE_LPM,
1150 		 dsi->state & DSIM_STATE_CMD_LPM)) {
1151 		samsung_dsim_set_cmd_lpm(dsi, xfer->flags & MIPI_DSI_MSG_USE_LPM);
1152 		dsi->state ^= DSIM_STATE_CMD_LPM;
1153 	}
1154 
1155 	samsung_dsim_write(dsi, DSIM_PKTHDR_REG, reg);
1156 
1157 	if (xfer->flags & MIPI_DSI_MSG_REQ_ACK)
1158 		samsung_dsim_force_bta(dsi);
1159 }
1160 
1161 static void samsung_dsim_read_from_fifo(struct samsung_dsim *dsi,
1162 					struct samsung_dsim_transfer *xfer)
1163 {
1164 	u8 *payload = xfer->rx_payload + xfer->rx_done;
1165 	bool first = !xfer->rx_done;
1166 	struct device *dev = dsi->dev;
1167 	u16 length;
1168 	u32 reg;
1169 
1170 	if (first) {
1171 		reg = samsung_dsim_read(dsi, DSIM_RXFIFO_REG);
1172 
1173 		switch (reg & 0x3f) {
1174 		case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
1175 		case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
1176 			if (xfer->rx_len >= 2) {
1177 				payload[1] = reg >> 16;
1178 				++xfer->rx_done;
1179 			}
1180 			fallthrough;
1181 		case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
1182 		case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
1183 			payload[0] = reg >> 8;
1184 			++xfer->rx_done;
1185 			xfer->rx_len = xfer->rx_done;
1186 			xfer->result = 0;
1187 			goto clear_fifo;
1188 		case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
1189 			dev_err(dev, "DSI Error Report: 0x%04x\n", (reg >> 8) & 0xffff);
1190 			xfer->result = 0;
1191 			goto clear_fifo;
1192 		}
1193 
1194 		length = (reg >> 8) & 0xffff;
1195 		if (length > xfer->rx_len) {
1196 			dev_err(dev,
1197 				"response too long (%u > %u bytes), stripping\n",
1198 				xfer->rx_len, length);
1199 			length = xfer->rx_len;
1200 		} else if (length < xfer->rx_len) {
1201 			xfer->rx_len = length;
1202 		}
1203 	}
1204 
1205 	length = xfer->rx_len - xfer->rx_done;
1206 	xfer->rx_done += length;
1207 
1208 	/* Receive payload */
1209 	while (length >= 4) {
1210 		reg = samsung_dsim_read(dsi, DSIM_RXFIFO_REG);
1211 		payload[0] = (reg >>  0) & 0xff;
1212 		payload[1] = (reg >>  8) & 0xff;
1213 		payload[2] = (reg >> 16) & 0xff;
1214 		payload[3] = (reg >> 24) & 0xff;
1215 		payload += 4;
1216 		length -= 4;
1217 	}
1218 
1219 	if (length) {
1220 		reg = samsung_dsim_read(dsi, DSIM_RXFIFO_REG);
1221 		switch (length) {
1222 		case 3:
1223 			payload[2] = (reg >> 16) & 0xff;
1224 			fallthrough;
1225 		case 2:
1226 			payload[1] = (reg >> 8) & 0xff;
1227 			fallthrough;
1228 		case 1:
1229 			payload[0] = reg & 0xff;
1230 		}
1231 	}
1232 
1233 	if (xfer->rx_done == xfer->rx_len)
1234 		xfer->result = 0;
1235 
1236 clear_fifo:
1237 	length = DSI_RX_FIFO_SIZE / 4;
1238 	do {
1239 		reg = samsung_dsim_read(dsi, DSIM_RXFIFO_REG);
1240 		if (reg == DSI_RX_FIFO_EMPTY)
1241 			break;
1242 	} while (--length);
1243 }
1244 
1245 static void samsung_dsim_transfer_start(struct samsung_dsim *dsi)
1246 {
1247 	unsigned long flags;
1248 	struct samsung_dsim_transfer *xfer;
1249 
1250 	spin_lock_irqsave(&dsi->transfer_lock, flags);
1251 
1252 	while (!list_empty(&dsi->transfer_list)) {
1253 		xfer = list_first_entry(&dsi->transfer_list,
1254 					struct samsung_dsim_transfer, list);
1255 
1256 		spin_unlock_irqrestore(&dsi->transfer_lock, flags);
1257 
1258 		if (xfer->packet.payload_length &&
1259 		    xfer->tx_done == xfer->packet.payload_length)
1260 			/* waiting for RX */
1261 			return;
1262 
1263 		samsung_dsim_send_to_fifo(dsi, xfer);
1264 
1265 		if (xfer->packet.payload_length || xfer->rx_len)
1266 			return;
1267 
1268 		xfer->result = 0;
1269 		complete(&xfer->completed);
1270 
1271 		spin_lock_irqsave(&dsi->transfer_lock, flags);
1272 
1273 		list_del_init(&xfer->list);
1274 	}
1275 
1276 	spin_unlock_irqrestore(&dsi->transfer_lock, flags);
1277 }
1278 
1279 static bool samsung_dsim_transfer_finish(struct samsung_dsim *dsi)
1280 {
1281 	struct samsung_dsim_transfer *xfer;
1282 	unsigned long flags;
1283 	bool start = true;
1284 
1285 	spin_lock_irqsave(&dsi->transfer_lock, flags);
1286 
1287 	if (list_empty(&dsi->transfer_list)) {
1288 		spin_unlock_irqrestore(&dsi->transfer_lock, flags);
1289 		return false;
1290 	}
1291 
1292 	xfer = list_first_entry(&dsi->transfer_list,
1293 				struct samsung_dsim_transfer, list);
1294 
1295 	spin_unlock_irqrestore(&dsi->transfer_lock, flags);
1296 
1297 	dev_dbg(dsi->dev,
1298 		"> xfer %p, tx_len %zu, tx_done %u, rx_len %u, rx_done %u\n",
1299 		xfer, xfer->packet.payload_length, xfer->tx_done, xfer->rx_len,
1300 		xfer->rx_done);
1301 
1302 	if (xfer->tx_done != xfer->packet.payload_length)
1303 		return true;
1304 
1305 	if (xfer->rx_done != xfer->rx_len)
1306 		samsung_dsim_read_from_fifo(dsi, xfer);
1307 
1308 	if (xfer->rx_done != xfer->rx_len)
1309 		return true;
1310 
1311 	spin_lock_irqsave(&dsi->transfer_lock, flags);
1312 
1313 	list_del_init(&xfer->list);
1314 	start = !list_empty(&dsi->transfer_list);
1315 
1316 	spin_unlock_irqrestore(&dsi->transfer_lock, flags);
1317 
1318 	if (!xfer->rx_len)
1319 		xfer->result = 0;
1320 	complete(&xfer->completed);
1321 
1322 	return start;
1323 }
1324 
1325 static void samsung_dsim_remove_transfer(struct samsung_dsim *dsi,
1326 					 struct samsung_dsim_transfer *xfer)
1327 {
1328 	unsigned long flags;
1329 	bool start;
1330 
1331 	spin_lock_irqsave(&dsi->transfer_lock, flags);
1332 
1333 	if (!list_empty(&dsi->transfer_list) &&
1334 	    xfer == list_first_entry(&dsi->transfer_list,
1335 				     struct samsung_dsim_transfer, list)) {
1336 		list_del_init(&xfer->list);
1337 		start = !list_empty(&dsi->transfer_list);
1338 		spin_unlock_irqrestore(&dsi->transfer_lock, flags);
1339 		if (start)
1340 			samsung_dsim_transfer_start(dsi);
1341 		return;
1342 	}
1343 
1344 	list_del_init(&xfer->list);
1345 
1346 	spin_unlock_irqrestore(&dsi->transfer_lock, flags);
1347 }
1348 
1349 static int samsung_dsim_transfer(struct samsung_dsim *dsi,
1350 				 struct samsung_dsim_transfer *xfer)
1351 {
1352 	unsigned long flags;
1353 	bool stopped;
1354 
1355 	xfer->tx_done = 0;
1356 	xfer->rx_done = 0;
1357 	xfer->result = -ETIMEDOUT;
1358 	init_completion(&xfer->completed);
1359 
1360 	spin_lock_irqsave(&dsi->transfer_lock, flags);
1361 
1362 	stopped = list_empty(&dsi->transfer_list);
1363 	list_add_tail(&xfer->list, &dsi->transfer_list);
1364 
1365 	spin_unlock_irqrestore(&dsi->transfer_lock, flags);
1366 
1367 	if (stopped)
1368 		samsung_dsim_transfer_start(dsi);
1369 
1370 	wait_for_completion_timeout(&xfer->completed,
1371 				    msecs_to_jiffies(DSI_XFER_TIMEOUT_MS));
1372 	if (xfer->result == -ETIMEDOUT) {
1373 		struct mipi_dsi_packet *pkt = &xfer->packet;
1374 
1375 		samsung_dsim_remove_transfer(dsi, xfer);
1376 		dev_err(dsi->dev, "xfer timed out: %*ph %*ph\n", 4, pkt->header,
1377 			(int)pkt->payload_length, pkt->payload);
1378 		return -ETIMEDOUT;
1379 	}
1380 
1381 	/* Also covers hardware timeout condition */
1382 	return xfer->result;
1383 }
1384 
1385 static irqreturn_t samsung_dsim_irq(int irq, void *dev_id)
1386 {
1387 	struct samsung_dsim *dsi = dev_id;
1388 	u32 status;
1389 
1390 	status = samsung_dsim_read(dsi, DSIM_INTSRC_REG);
1391 	if (!status) {
1392 		static unsigned long j;
1393 
1394 		if (printk_timed_ratelimit(&j, 500))
1395 			dev_warn(dsi->dev, "spurious interrupt\n");
1396 		return IRQ_HANDLED;
1397 	}
1398 	samsung_dsim_write(dsi, DSIM_INTSRC_REG, status);
1399 
1400 	if (status & DSIM_INT_SW_RST_RELEASE) {
1401 		unsigned long mask = ~(DSIM_INT_RX_DONE |
1402 				       DSIM_INT_SFR_FIFO_EMPTY |
1403 				       DSIM_INT_SFR_HDR_FIFO_EMPTY |
1404 				       DSIM_INT_RX_ECC_ERR |
1405 				       DSIM_INT_SW_RST_RELEASE);
1406 		samsung_dsim_write(dsi, DSIM_INTMSK_REG, mask);
1407 		complete(&dsi->completed);
1408 		return IRQ_HANDLED;
1409 	}
1410 
1411 	if (!(status & (DSIM_INT_RX_DONE | DSIM_INT_SFR_FIFO_EMPTY |
1412 			DSIM_INT_PLL_STABLE)))
1413 		return IRQ_HANDLED;
1414 
1415 	if (samsung_dsim_transfer_finish(dsi))
1416 		samsung_dsim_transfer_start(dsi);
1417 
1418 	return IRQ_HANDLED;
1419 }
1420 
1421 static void samsung_dsim_enable_irq(struct samsung_dsim *dsi)
1422 {
1423 	enable_irq(dsi->irq);
1424 
1425 	if (dsi->te_gpio)
1426 		enable_irq(gpiod_to_irq(dsi->te_gpio));
1427 }
1428 
1429 static void samsung_dsim_disable_irq(struct samsung_dsim *dsi)
1430 {
1431 	if (dsi->te_gpio)
1432 		disable_irq(gpiod_to_irq(dsi->te_gpio));
1433 
1434 	disable_irq(dsi->irq);
1435 }
1436 
1437 static int samsung_dsim_init(struct samsung_dsim *dsi)
1438 {
1439 	const struct samsung_dsim_driver_data *driver_data = dsi->driver_data;
1440 
1441 	if (dsi->state & DSIM_STATE_INITIALIZED)
1442 		return 0;
1443 
1444 	samsung_dsim_reset(dsi);
1445 	samsung_dsim_enable_irq(dsi);
1446 
1447 	if (driver_data->reg_values[RESET_TYPE] == DSIM_FUNCRST)
1448 		samsung_dsim_enable_lane(dsi, BIT(dsi->lanes) - 1);
1449 
1450 	samsung_dsim_enable_clock(dsi);
1451 	if (driver_data->wait_for_reset)
1452 		samsung_dsim_wait_for_reset(dsi);
1453 	samsung_dsim_set_phy_ctrl(dsi);
1454 	samsung_dsim_init_link(dsi);
1455 
1456 	dsi->state |= DSIM_STATE_INITIALIZED;
1457 
1458 	return 0;
1459 }
1460 
1461 static void samsung_dsim_atomic_pre_enable(struct drm_bridge *bridge,
1462 					   struct drm_atomic_state *state)
1463 {
1464 	struct samsung_dsim *dsi = bridge_to_dsi(bridge);
1465 	int ret;
1466 
1467 	if (dsi->state & DSIM_STATE_ENABLED)
1468 		return;
1469 
1470 	ret = pm_runtime_resume_and_get(dsi->dev);
1471 	if (ret < 0) {
1472 		dev_err(dsi->dev, "failed to enable DSI device.\n");
1473 		return;
1474 	}
1475 
1476 	dsi->state |= DSIM_STATE_ENABLED;
1477 
1478 	/*
1479 	 * For Exynos-DSIM the downstream bridge, or panel are expecting
1480 	 * the host initialization during DSI transfer.
1481 	 */
1482 	if (!samsung_dsim_hw_is_exynos(dsi->plat_data->hw_type)) {
1483 		ret = samsung_dsim_init(dsi);
1484 		if (ret)
1485 			return;
1486 	}
1487 }
1488 
1489 static void samsung_dsim_atomic_enable(struct drm_bridge *bridge,
1490 				       struct drm_atomic_state *state)
1491 {
1492 	struct samsung_dsim *dsi = bridge_to_dsi(bridge);
1493 
1494 	samsung_dsim_set_display_mode(dsi);
1495 	samsung_dsim_set_display_enable(dsi, true);
1496 
1497 	dsi->state |= DSIM_STATE_VIDOUT_AVAILABLE;
1498 }
1499 
1500 static void samsung_dsim_atomic_disable(struct drm_bridge *bridge,
1501 					struct drm_atomic_state *state)
1502 {
1503 	struct samsung_dsim *dsi = bridge_to_dsi(bridge);
1504 
1505 	if (!(dsi->state & DSIM_STATE_ENABLED))
1506 		return;
1507 
1508 	samsung_dsim_set_display_enable(dsi, false);
1509 	dsi->state &= ~DSIM_STATE_VIDOUT_AVAILABLE;
1510 }
1511 
1512 static void samsung_dsim_atomic_post_disable(struct drm_bridge *bridge,
1513 					     struct drm_atomic_state *state)
1514 {
1515 	struct samsung_dsim *dsi = bridge_to_dsi(bridge);
1516 
1517 	dsi->state &= ~DSIM_STATE_ENABLED;
1518 	pm_runtime_put_sync(dsi->dev);
1519 }
1520 
1521 /*
1522  * This pixel output formats list referenced from,
1523  * AN13573 i.MX 8/RT MIPI DSI/CSI-2, Rev. 0, 21 March 2022
1524  * 3.7.4 Pixel formats
1525  * Table 14. DSI pixel packing formats
1526  */
1527 static const u32 samsung_dsim_pixel_output_fmts[] = {
1528 	MEDIA_BUS_FMT_YUYV10_1X20,
1529 	MEDIA_BUS_FMT_YUYV12_1X24,
1530 	MEDIA_BUS_FMT_UYVY8_1X16,
1531 	MEDIA_BUS_FMT_RGB101010_1X30,
1532 	MEDIA_BUS_FMT_RGB121212_1X36,
1533 	MEDIA_BUS_FMT_RGB565_1X16,
1534 	MEDIA_BUS_FMT_RGB666_1X18,
1535 	MEDIA_BUS_FMT_RGB888_1X24,
1536 };
1537 
1538 static bool samsung_dsim_pixel_output_fmt_supported(u32 fmt)
1539 {
1540 	int i;
1541 
1542 	if (fmt == MEDIA_BUS_FMT_FIXED)
1543 		return false;
1544 
1545 	for (i = 0; i < ARRAY_SIZE(samsung_dsim_pixel_output_fmts); i++) {
1546 		if (samsung_dsim_pixel_output_fmts[i] == fmt)
1547 			return true;
1548 	}
1549 
1550 	return false;
1551 }
1552 
1553 static u32 *
1554 samsung_dsim_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
1555 				       struct drm_bridge_state *bridge_state,
1556 				       struct drm_crtc_state *crtc_state,
1557 				       struct drm_connector_state *conn_state,
1558 				       u32 output_fmt,
1559 				       unsigned int *num_input_fmts)
1560 {
1561 	u32 *input_fmts;
1562 
1563 	input_fmts = kmalloc(sizeof(*input_fmts), GFP_KERNEL);
1564 	if (!input_fmts)
1565 		return NULL;
1566 
1567 	if (!samsung_dsim_pixel_output_fmt_supported(output_fmt))
1568 		/*
1569 		 * Some bridge/display drivers are still not able to pass the
1570 		 * correct format, so handle those pipelines by falling back
1571 		 * to the default format till the supported formats finalized.
1572 		 */
1573 		output_fmt = MEDIA_BUS_FMT_RGB888_1X24;
1574 
1575 	input_fmts[0] = output_fmt;
1576 	*num_input_fmts = 1;
1577 
1578 	return input_fmts;
1579 }
1580 
1581 static int samsung_dsim_atomic_check(struct drm_bridge *bridge,
1582 				     struct drm_bridge_state *bridge_state,
1583 				     struct drm_crtc_state *crtc_state,
1584 				     struct drm_connector_state *conn_state)
1585 {
1586 	struct samsung_dsim *dsi = bridge_to_dsi(bridge);
1587 	struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
1588 
1589 	/*
1590 	 * The i.MX8M Mini/Nano glue logic between LCDIF and DSIM
1591 	 * inverts HS/VS/DE sync signals polarity, therefore, while
1592 	 * i.MX 8M Mini Applications Processor Reference Manual Rev. 3, 11/2020
1593 	 * 13.6.3.5.2 RGB interface
1594 	 * i.MX 8M Nano Applications Processor Reference Manual Rev. 2, 07/2022
1595 	 * 13.6.2.7.2 RGB interface
1596 	 * both claim "Vsync, Hsync, and VDEN are active high signals.", the
1597 	 * LCDIF must generate inverted HS/VS/DE signals, i.e. active LOW.
1598 	 *
1599 	 * The i.MX8M Plus glue logic between LCDIFv3 and DSIM does not
1600 	 * implement the same behavior, therefore LCDIFv3 must generate
1601 	 * HS/VS/DE signals active HIGH.
1602 	 */
1603 	if (dsi->plat_data->hw_type == DSIM_TYPE_IMX8MM) {
1604 		adjusted_mode->flags |= (DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC);
1605 		adjusted_mode->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
1606 	} else if (dsi->plat_data->hw_type == DSIM_TYPE_IMX8MP) {
1607 		adjusted_mode->flags &= ~(DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC);
1608 		adjusted_mode->flags |= (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
1609 	}
1610 
1611 	/*
1612 	 * When using video sync pulses, the HFP, HBP, and HSA are divided between
1613 	 * the available lanes if there is more than one lane.  For certain
1614 	 * timings and lane configurations, the HFP may not be evenly divisible.
1615 	 * If the HFP is rounded down, it ends up being too small which can cause
1616 	 * some monitors to not sync properly. In these instances, adjust htotal
1617 	 * and hsync to round the HFP up, and recalculate the htotal. Through trial
1618 	 * and error, it appears that the HBP and HSA do not appearto need the same
1619 	 * correction that HFP does.
1620 	 */
1621 	if (dsi->lanes > 1) {
1622 		int hfp = adjusted_mode->hsync_start - adjusted_mode->hdisplay;
1623 		int remainder = hfp % dsi->lanes;
1624 
1625 		if (remainder) {
1626 			adjusted_mode->hsync_start += remainder;
1627 			adjusted_mode->hsync_end   += remainder;
1628 			adjusted_mode->htotal      += remainder;
1629 		}
1630 	}
1631 
1632 	return 0;
1633 }
1634 
1635 static void samsung_dsim_mode_set(struct drm_bridge *bridge,
1636 				  const struct drm_display_mode *mode,
1637 				  const struct drm_display_mode *adjusted_mode)
1638 {
1639 	struct samsung_dsim *dsi = bridge_to_dsi(bridge);
1640 
1641 	drm_mode_copy(&dsi->mode, adjusted_mode);
1642 }
1643 
1644 static int samsung_dsim_attach(struct drm_bridge *bridge,
1645 			       struct drm_encoder *encoder,
1646 			       enum drm_bridge_attach_flags flags)
1647 {
1648 	struct samsung_dsim *dsi = bridge_to_dsi(bridge);
1649 
1650 	return drm_bridge_attach(encoder, dsi->out_bridge, bridge,
1651 				 flags);
1652 }
1653 
1654 static const struct drm_bridge_funcs samsung_dsim_bridge_funcs = {
1655 	.atomic_duplicate_state		= drm_atomic_helper_bridge_duplicate_state,
1656 	.atomic_destroy_state		= drm_atomic_helper_bridge_destroy_state,
1657 	.atomic_reset			= drm_atomic_helper_bridge_reset,
1658 	.atomic_get_input_bus_fmts	= samsung_dsim_atomic_get_input_bus_fmts,
1659 	.atomic_check			= samsung_dsim_atomic_check,
1660 	.atomic_pre_enable		= samsung_dsim_atomic_pre_enable,
1661 	.atomic_enable			= samsung_dsim_atomic_enable,
1662 	.atomic_disable			= samsung_dsim_atomic_disable,
1663 	.atomic_post_disable		= samsung_dsim_atomic_post_disable,
1664 	.mode_set			= samsung_dsim_mode_set,
1665 	.attach				= samsung_dsim_attach,
1666 };
1667 
1668 static irqreturn_t samsung_dsim_te_irq_handler(int irq, void *dev_id)
1669 {
1670 	struct samsung_dsim *dsi = (struct samsung_dsim *)dev_id;
1671 	const struct samsung_dsim_plat_data *pdata = dsi->plat_data;
1672 
1673 	if (pdata->host_ops && pdata->host_ops->te_irq_handler)
1674 		return pdata->host_ops->te_irq_handler(dsi);
1675 
1676 	return IRQ_HANDLED;
1677 }
1678 
1679 static int samsung_dsim_register_te_irq(struct samsung_dsim *dsi, struct device *dev)
1680 {
1681 	int te_gpio_irq;
1682 	int ret;
1683 
1684 	dsi->te_gpio = devm_gpiod_get_optional(dev, "te", GPIOD_IN);
1685 	if (!dsi->te_gpio)
1686 		return 0;
1687 	else if (IS_ERR(dsi->te_gpio))
1688 		return dev_err_probe(dev, PTR_ERR(dsi->te_gpio), "failed to get te GPIO\n");
1689 
1690 	te_gpio_irq = gpiod_to_irq(dsi->te_gpio);
1691 
1692 	ret = request_threaded_irq(te_gpio_irq, samsung_dsim_te_irq_handler, NULL,
1693 				   IRQF_TRIGGER_RISING | IRQF_NO_AUTOEN, "TE", dsi);
1694 	if (ret) {
1695 		dev_err(dsi->dev, "request interrupt failed with %d\n", ret);
1696 		gpiod_put(dsi->te_gpio);
1697 		return ret;
1698 	}
1699 
1700 	return 0;
1701 }
1702 
1703 static int samsung_dsim_host_attach(struct mipi_dsi_host *host,
1704 				    struct mipi_dsi_device *device)
1705 {
1706 	struct samsung_dsim *dsi = host_to_dsi(host);
1707 	const struct samsung_dsim_plat_data *pdata = dsi->plat_data;
1708 	struct device *dev = dsi->dev;
1709 	struct device_node *np = dev->of_node;
1710 	struct device_node *remote;
1711 	struct drm_panel *panel;
1712 	int ret;
1713 
1714 	/*
1715 	 * Devices can also be child nodes when we also control that device
1716 	 * through the upstream device (ie, MIPI-DCS for a MIPI-DSI device).
1717 	 *
1718 	 * Lookup for a child node of the given parent that isn't either port
1719 	 * or ports.
1720 	 */
1721 	for_each_available_child_of_node(np, remote) {
1722 		if (of_node_name_eq(remote, "port") ||
1723 		    of_node_name_eq(remote, "ports"))
1724 			continue;
1725 
1726 		goto of_find_panel_or_bridge;
1727 	}
1728 
1729 	/*
1730 	 * of_graph_get_remote_node() produces a noisy error message if port
1731 	 * node isn't found and the absence of the port is a legit case here,
1732 	 * so at first we silently check whether graph presents in the
1733 	 * device-tree node.
1734 	 */
1735 	if (!of_graph_is_present(np))
1736 		return -ENODEV;
1737 
1738 	remote = of_graph_get_remote_node(np, 1, 0);
1739 
1740 of_find_panel_or_bridge:
1741 	if (!remote)
1742 		return -ENODEV;
1743 
1744 	panel = of_drm_find_panel(remote);
1745 	if (!IS_ERR(panel)) {
1746 		dsi->out_bridge = devm_drm_panel_bridge_add(dev, panel);
1747 	} else {
1748 		dsi->out_bridge = of_drm_find_bridge(remote);
1749 		if (!dsi->out_bridge)
1750 			dsi->out_bridge = ERR_PTR(-EINVAL);
1751 	}
1752 
1753 	of_node_put(remote);
1754 
1755 	if (IS_ERR(dsi->out_bridge)) {
1756 		ret = PTR_ERR(dsi->out_bridge);
1757 		DRM_DEV_ERROR(dev, "failed to find the bridge: %d\n", ret);
1758 		return ret;
1759 	}
1760 
1761 	DRM_DEV_INFO(dev, "Attached %s device (lanes:%d bpp:%d mode-flags:0x%lx)\n",
1762 		     device->name, device->lanes,
1763 		     mipi_dsi_pixel_format_to_bpp(device->format),
1764 		     device->mode_flags);
1765 
1766 	drm_bridge_add(&dsi->bridge);
1767 
1768 	/*
1769 	 * This is a temporary solution and should be made by more generic way.
1770 	 *
1771 	 * If attached panel device is for command mode one, dsi should register
1772 	 * TE interrupt handler.
1773 	 */
1774 	if (!(device->mode_flags & MIPI_DSI_MODE_VIDEO)) {
1775 		ret = samsung_dsim_register_te_irq(dsi, &device->dev);
1776 		if (ret)
1777 			return ret;
1778 	}
1779 
1780 	if (pdata->host_ops && pdata->host_ops->attach) {
1781 		ret = pdata->host_ops->attach(dsi, device);
1782 		if (ret)
1783 			return ret;
1784 	}
1785 
1786 	dsi->lanes = device->lanes;
1787 	dsi->format = device->format;
1788 	dsi->mode_flags = device->mode_flags;
1789 
1790 	return 0;
1791 }
1792 
1793 static void samsung_dsim_unregister_te_irq(struct samsung_dsim *dsi)
1794 {
1795 	if (dsi->te_gpio) {
1796 		free_irq(gpiod_to_irq(dsi->te_gpio), dsi);
1797 		gpiod_put(dsi->te_gpio);
1798 	}
1799 }
1800 
1801 static int samsung_dsim_host_detach(struct mipi_dsi_host *host,
1802 				    struct mipi_dsi_device *device)
1803 {
1804 	struct samsung_dsim *dsi = host_to_dsi(host);
1805 	const struct samsung_dsim_plat_data *pdata = dsi->plat_data;
1806 
1807 	dsi->out_bridge = NULL;
1808 
1809 	if (pdata->host_ops && pdata->host_ops->detach)
1810 		pdata->host_ops->detach(dsi, device);
1811 
1812 	samsung_dsim_unregister_te_irq(dsi);
1813 
1814 	drm_bridge_remove(&dsi->bridge);
1815 
1816 	return 0;
1817 }
1818 
1819 static ssize_t samsung_dsim_host_transfer(struct mipi_dsi_host *host,
1820 					  const struct mipi_dsi_msg *msg)
1821 {
1822 	struct samsung_dsim *dsi = host_to_dsi(host);
1823 	struct samsung_dsim_transfer xfer;
1824 	int ret;
1825 
1826 	if (!(dsi->state & DSIM_STATE_ENABLED))
1827 		return -EINVAL;
1828 
1829 	ret = samsung_dsim_init(dsi);
1830 	if (ret)
1831 		return ret;
1832 
1833 	ret = mipi_dsi_create_packet(&xfer.packet, msg);
1834 	if (ret < 0)
1835 		return ret;
1836 
1837 	xfer.rx_len = msg->rx_len;
1838 	xfer.rx_payload = msg->rx_buf;
1839 	xfer.flags = msg->flags;
1840 
1841 	ret = samsung_dsim_transfer(dsi, &xfer);
1842 	return (ret < 0) ? ret : xfer.rx_done;
1843 }
1844 
1845 static const struct mipi_dsi_host_ops samsung_dsim_ops = {
1846 	.attach = samsung_dsim_host_attach,
1847 	.detach = samsung_dsim_host_detach,
1848 	.transfer = samsung_dsim_host_transfer,
1849 };
1850 
1851 static int samsung_dsim_of_read_u32(const struct device_node *np,
1852 				    const char *propname, u32 *out_value, bool optional)
1853 {
1854 	int ret = of_property_read_u32(np, propname, out_value);
1855 
1856 	if (ret < 0 && !optional)
1857 		pr_err("%pOF: failed to get '%s' property\n", np, propname);
1858 
1859 	return ret;
1860 }
1861 
1862 static int samsung_dsim_parse_dt(struct samsung_dsim *dsi)
1863 {
1864 	struct device *dev = dsi->dev;
1865 	struct device_node *node = dev->of_node;
1866 	u32 lane_polarities[5] = { 0 };
1867 	struct device_node *endpoint;
1868 	int i, nr_lanes, ret;
1869 
1870 	ret = samsung_dsim_of_read_u32(node, "samsung,pll-clock-frequency",
1871 				       &dsi->pll_clk_rate, 1);
1872 	/* If it doesn't exist, read it from the clock instead of failing */
1873 	if (ret < 0) {
1874 		dev_dbg(dev, "Using sclk_mipi for pll clock frequency\n");
1875 		dsi->pll_clk = devm_clk_get(dev, "sclk_mipi");
1876 		if (IS_ERR(dsi->pll_clk))
1877 			return PTR_ERR(dsi->pll_clk);
1878 	}
1879 
1880 	/* If it doesn't exist, use pixel clock instead of failing */
1881 	ret = samsung_dsim_of_read_u32(node, "samsung,burst-clock-frequency",
1882 				       &dsi->burst_clk_rate, 1);
1883 	if (ret < 0) {
1884 		dev_dbg(dev, "Using pixel clock for HS clock frequency\n");
1885 		dsi->burst_clk_rate = 0;
1886 	}
1887 
1888 	ret = samsung_dsim_of_read_u32(node, "samsung,esc-clock-frequency",
1889 				       &dsi->esc_clk_rate, 0);
1890 	if (ret < 0)
1891 		return ret;
1892 
1893 	endpoint = of_graph_get_endpoint_by_regs(node, 1, -1);
1894 	nr_lanes = of_property_count_u32_elems(endpoint, "data-lanes");
1895 	if (nr_lanes > 0 && nr_lanes <= 4) {
1896 		/* Polarity 0 is clock lane, 1..4 are data lanes. */
1897 		of_property_read_u32_array(endpoint, "lane-polarities",
1898 					   lane_polarities, nr_lanes + 1);
1899 		for (i = 1; i <= nr_lanes; i++) {
1900 			if (lane_polarities[1] != lane_polarities[i])
1901 				DRM_DEV_ERROR(dsi->dev, "Data lanes polarities do not match");
1902 		}
1903 		if (lane_polarities[0])
1904 			dsi->swap_dn_dp_clk = true;
1905 		if (lane_polarities[1])
1906 			dsi->swap_dn_dp_data = true;
1907 	}
1908 
1909 	return 0;
1910 }
1911 
1912 static int generic_dsim_register_host(struct samsung_dsim *dsi)
1913 {
1914 	return mipi_dsi_host_register(&dsi->dsi_host);
1915 }
1916 
1917 static void generic_dsim_unregister_host(struct samsung_dsim *dsi)
1918 {
1919 	mipi_dsi_host_unregister(&dsi->dsi_host);
1920 }
1921 
1922 static const struct samsung_dsim_host_ops generic_dsim_host_ops = {
1923 	.register_host = generic_dsim_register_host,
1924 	.unregister_host = generic_dsim_unregister_host,
1925 };
1926 
1927 static const struct drm_bridge_timings samsung_dsim_bridge_timings_de_high = {
1928 	.input_bus_flags = DRM_BUS_FLAG_DE_HIGH,
1929 };
1930 
1931 static const struct drm_bridge_timings samsung_dsim_bridge_timings_de_low = {
1932 	.input_bus_flags = DRM_BUS_FLAG_DE_LOW,
1933 };
1934 
1935 int samsung_dsim_probe(struct platform_device *pdev)
1936 {
1937 	struct device *dev = &pdev->dev;
1938 	struct samsung_dsim *dsi;
1939 	int ret, i;
1940 
1941 	dsi = devm_drm_bridge_alloc(dev, struct samsung_dsim, bridge, &samsung_dsim_bridge_funcs);
1942 	if (IS_ERR(dsi))
1943 		return PTR_ERR(dsi);
1944 
1945 	init_completion(&dsi->completed);
1946 	spin_lock_init(&dsi->transfer_lock);
1947 	INIT_LIST_HEAD(&dsi->transfer_list);
1948 
1949 	dsi->dsi_host.ops = &samsung_dsim_ops;
1950 	dsi->dsi_host.dev = dev;
1951 
1952 	dsi->dev = dev;
1953 	dsi->plat_data = of_device_get_match_data(dev);
1954 	dsi->driver_data = samsung_dsim_types[dsi->plat_data->hw_type];
1955 
1956 	dsi->supplies[0].supply = "vddcore";
1957 	dsi->supplies[1].supply = "vddio";
1958 	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(dsi->supplies),
1959 				      dsi->supplies);
1960 	if (ret)
1961 		return dev_err_probe(dev, ret, "failed to get regulators\n");
1962 
1963 	dsi->clks = devm_kcalloc(dev, dsi->driver_data->num_clks,
1964 				 sizeof(*dsi->clks), GFP_KERNEL);
1965 	if (!dsi->clks)
1966 		return -ENOMEM;
1967 
1968 	for (i = 0; i < dsi->driver_data->num_clks; i++) {
1969 		dsi->clks[i] = devm_clk_get(dev, clk_names[i]);
1970 		if (IS_ERR(dsi->clks[i])) {
1971 			if (strcmp(clk_names[i], "sclk_mipi") == 0) {
1972 				dsi->clks[i] = devm_clk_get(dev, OLD_SCLK_MIPI_CLK_NAME);
1973 				if (!IS_ERR(dsi->clks[i]))
1974 					continue;
1975 			}
1976 
1977 			dev_info(dev, "failed to get the clock: %s\n", clk_names[i]);
1978 			return PTR_ERR(dsi->clks[i]);
1979 		}
1980 	}
1981 
1982 	dsi->reg_base = devm_platform_ioremap_resource(pdev, 0);
1983 	if (IS_ERR(dsi->reg_base))
1984 		return PTR_ERR(dsi->reg_base);
1985 
1986 	dsi->phy = devm_phy_optional_get(dev, "dsim");
1987 	if (IS_ERR(dsi->phy)) {
1988 		dev_info(dev, "failed to get dsim phy\n");
1989 		return PTR_ERR(dsi->phy);
1990 	}
1991 
1992 	dsi->irq = platform_get_irq(pdev, 0);
1993 	if (dsi->irq < 0)
1994 		return dsi->irq;
1995 
1996 	ret = devm_request_threaded_irq(dev, dsi->irq, NULL,
1997 					samsung_dsim_irq,
1998 					IRQF_ONESHOT | IRQF_NO_AUTOEN,
1999 					dev_name(dev), dsi);
2000 	if (ret) {
2001 		dev_err(dev, "failed to request dsi irq\n");
2002 		return ret;
2003 	}
2004 
2005 	ret = samsung_dsim_parse_dt(dsi);
2006 	if (ret)
2007 		return ret;
2008 
2009 	platform_set_drvdata(pdev, dsi);
2010 
2011 	pm_runtime_enable(dev);
2012 
2013 	dsi->bridge.of_node = dev->of_node;
2014 	dsi->bridge.type = DRM_MODE_CONNECTOR_DSI;
2015 
2016 	/* DE_LOW: i.MX8M Mini/Nano LCDIF-DSIM glue logic inverts HS/VS/DE */
2017 	if (dsi->plat_data->hw_type == DSIM_TYPE_IMX8MM)
2018 		dsi->bridge.timings = &samsung_dsim_bridge_timings_de_low;
2019 	else
2020 		dsi->bridge.timings = &samsung_dsim_bridge_timings_de_high;
2021 
2022 	if (dsi->plat_data->host_ops && dsi->plat_data->host_ops->register_host) {
2023 		ret = dsi->plat_data->host_ops->register_host(dsi);
2024 		if (ret)
2025 			goto err_disable_runtime;
2026 	}
2027 
2028 	return 0;
2029 
2030 err_disable_runtime:
2031 	pm_runtime_disable(dev);
2032 
2033 	return ret;
2034 }
2035 EXPORT_SYMBOL_GPL(samsung_dsim_probe);
2036 
2037 void samsung_dsim_remove(struct platform_device *pdev)
2038 {
2039 	struct samsung_dsim *dsi = platform_get_drvdata(pdev);
2040 
2041 	pm_runtime_disable(&pdev->dev);
2042 
2043 	if (dsi->plat_data->host_ops && dsi->plat_data->host_ops->unregister_host)
2044 		dsi->plat_data->host_ops->unregister_host(dsi);
2045 }
2046 EXPORT_SYMBOL_GPL(samsung_dsim_remove);
2047 
2048 static int samsung_dsim_suspend(struct device *dev)
2049 {
2050 	struct samsung_dsim *dsi = dev_get_drvdata(dev);
2051 	const struct samsung_dsim_driver_data *driver_data = dsi->driver_data;
2052 	int ret, i;
2053 
2054 	usleep_range(10000, 20000);
2055 
2056 	if (dsi->state & DSIM_STATE_INITIALIZED) {
2057 		dsi->state &= ~DSIM_STATE_INITIALIZED;
2058 
2059 		samsung_dsim_disable_clock(dsi);
2060 
2061 		samsung_dsim_disable_irq(dsi);
2062 	}
2063 
2064 	dsi->state &= ~DSIM_STATE_CMD_LPM;
2065 
2066 	phy_power_off(dsi->phy);
2067 
2068 	for (i = driver_data->num_clks - 1; i > -1; i--)
2069 		clk_disable_unprepare(dsi->clks[i]);
2070 
2071 	ret = regulator_bulk_disable(ARRAY_SIZE(dsi->supplies), dsi->supplies);
2072 	if (ret < 0)
2073 		dev_err(dsi->dev, "cannot disable regulators %d\n", ret);
2074 
2075 	return 0;
2076 }
2077 
2078 static int samsung_dsim_resume(struct device *dev)
2079 {
2080 	struct samsung_dsim *dsi = dev_get_drvdata(dev);
2081 	const struct samsung_dsim_driver_data *driver_data = dsi->driver_data;
2082 	int ret, i;
2083 
2084 	ret = regulator_bulk_enable(ARRAY_SIZE(dsi->supplies), dsi->supplies);
2085 	if (ret < 0) {
2086 		dev_err(dsi->dev, "cannot enable regulators %d\n", ret);
2087 		return ret;
2088 	}
2089 
2090 	for (i = 0; i < driver_data->num_clks; i++) {
2091 		ret = clk_prepare_enable(dsi->clks[i]);
2092 		if (ret < 0)
2093 			goto err_clk;
2094 	}
2095 
2096 	ret = phy_power_on(dsi->phy);
2097 	if (ret < 0) {
2098 		dev_err(dsi->dev, "cannot enable phy %d\n", ret);
2099 		goto err_clk;
2100 	}
2101 
2102 	return 0;
2103 
2104 err_clk:
2105 	while (--i > -1)
2106 		clk_disable_unprepare(dsi->clks[i]);
2107 	regulator_bulk_disable(ARRAY_SIZE(dsi->supplies), dsi->supplies);
2108 
2109 	return ret;
2110 }
2111 
2112 const struct dev_pm_ops samsung_dsim_pm_ops = {
2113 	RUNTIME_PM_OPS(samsung_dsim_suspend, samsung_dsim_resume, NULL)
2114 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
2115 				pm_runtime_force_resume)
2116 };
2117 EXPORT_SYMBOL_GPL(samsung_dsim_pm_ops);
2118 
2119 static const struct samsung_dsim_plat_data samsung_dsim_imx8mm_pdata = {
2120 	.hw_type = DSIM_TYPE_IMX8MM,
2121 	.host_ops = &generic_dsim_host_ops,
2122 };
2123 
2124 static const struct samsung_dsim_plat_data samsung_dsim_imx8mp_pdata = {
2125 	.hw_type = DSIM_TYPE_IMX8MP,
2126 	.host_ops = &generic_dsim_host_ops,
2127 };
2128 
2129 static const struct of_device_id samsung_dsim_of_match[] = {
2130 	{
2131 		.compatible = "fsl,imx8mm-mipi-dsim",
2132 		.data = &samsung_dsim_imx8mm_pdata,
2133 	},
2134 	{
2135 		.compatible = "fsl,imx8mp-mipi-dsim",
2136 		.data = &samsung_dsim_imx8mp_pdata,
2137 	},
2138 	{ /* sentinel. */ }
2139 };
2140 MODULE_DEVICE_TABLE(of, samsung_dsim_of_match);
2141 
2142 static struct platform_driver samsung_dsim_driver = {
2143 	.probe = samsung_dsim_probe,
2144 	.remove = samsung_dsim_remove,
2145 	.driver = {
2146 		   .name = "samsung-dsim",
2147 		   .pm = pm_ptr(&samsung_dsim_pm_ops),
2148 		   .of_match_table = samsung_dsim_of_match,
2149 	},
2150 };
2151 
2152 module_platform_driver(samsung_dsim_driver);
2153 
2154 MODULE_AUTHOR("Jagan Teki <jagan@amarulasolutions.com>");
2155 MODULE_DESCRIPTION("Samsung MIPI DSIM controller bridge");
2156 MODULE_LICENSE("GPL");
2157