2 * Samsung SoC MIPI DSI Master driver.
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd
6 * Contacts: Tomasz Figa <t.figa@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <asm/unaligned.h>
16 #include <drm/drm_crtc_helper.h>
17 #include <drm/drm_mipi_dsi.h>
18 #include <drm/drm_panel.h>
19 #include <drm/drm_atomic_helper.h>
21 #include <linux/clk.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/irq.h>
24 #include <linux/of_device.h>
25 #include <linux/of_gpio.h>
26 #include <linux/of_graph.h>
27 #include <linux/phy/phy.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/component.h>
31 #include <video/mipi_display.h>
32 #include <video/videomode.h>
34 #include "exynos_drm_crtc.h"
35 #include "exynos_drm_drv.h"
37 /* returns true iff both arguments logically differs */
38 #define NEQV(a, b) (!(a) ^ !(b))
41 #define DSIM_STOP_STATE_DAT(x) (((x) & 0xf) << 0)
42 #define DSIM_STOP_STATE_CLK (1 << 8)
43 #define DSIM_TX_READY_HS_CLK (1 << 10)
44 #define DSIM_PLL_STABLE (1 << 31)
47 #define DSIM_FUNCRST (1 << 16)
48 #define DSIM_SWRST (1 << 0)
51 #define DSIM_LPDR_TIMEOUT(x) ((x) << 0)
52 #define DSIM_BTA_TIMEOUT(x) ((x) << 16)
55 #define DSIM_ESC_PRESCALER(x) (((x) & 0xffff) << 0)
56 #define DSIM_ESC_PRESCALER_MASK (0xffff << 0)
57 #define DSIM_LANE_ESC_CLK_EN_CLK (1 << 19)
58 #define DSIM_LANE_ESC_CLK_EN_DATA(x) (((x) & 0xf) << 20)
59 #define DSIM_LANE_ESC_CLK_EN_DATA_MASK (0xf << 20)
60 #define DSIM_BYTE_CLKEN (1 << 24)
61 #define DSIM_BYTE_CLK_SRC(x) (((x) & 0x3) << 25)
62 #define DSIM_BYTE_CLK_SRC_MASK (0x3 << 25)
63 #define DSIM_PLL_BYPASS (1 << 27)
64 #define DSIM_ESC_CLKEN (1 << 28)
65 #define DSIM_TX_REQUEST_HSCLK (1 << 31)
68 #define DSIM_LANE_EN_CLK (1 << 0)
69 #define DSIM_LANE_EN(x) (((x) & 0xf) << 1)
70 #define DSIM_NUM_OF_DATA_LANE(x) (((x) & 0x3) << 5)
71 #define DSIM_SUB_PIX_FORMAT(x) (((x) & 0x7) << 8)
72 #define DSIM_MAIN_PIX_FORMAT_MASK (0x7 << 12)
73 #define DSIM_MAIN_PIX_FORMAT_RGB888 (0x7 << 12)
74 #define DSIM_MAIN_PIX_FORMAT_RGB666 (0x6 << 12)
75 #define DSIM_MAIN_PIX_FORMAT_RGB666_P (0x5 << 12)
76 #define DSIM_MAIN_PIX_FORMAT_RGB565 (0x4 << 12)
77 #define DSIM_SUB_VC (((x) & 0x3) << 16)
78 #define DSIM_MAIN_VC (((x) & 0x3) << 18)
79 #define DSIM_HSA_MODE (1 << 20)
80 #define DSIM_HBP_MODE (1 << 21)
81 #define DSIM_HFP_MODE (1 << 22)
82 #define DSIM_HSE_MODE (1 << 23)
83 #define DSIM_AUTO_MODE (1 << 24)
84 #define DSIM_VIDEO_MODE (1 << 25)
85 #define DSIM_BURST_MODE (1 << 26)
86 #define DSIM_SYNC_INFORM (1 << 27)
87 #define DSIM_EOT_DISABLE (1 << 28)
88 #define DSIM_MFLUSH_VS (1 << 29)
89 /* This flag is valid only for exynos3250/3472/5260/5430 */
90 #define DSIM_CLKLANE_STOP (1 << 30)
93 #define DSIM_TX_TRIGGER_RST (1 << 4)
94 #define DSIM_TX_LPDT_LP (1 << 6)
95 #define DSIM_CMD_LPDT_LP (1 << 7)
96 #define DSIM_FORCE_BTA (1 << 16)
97 #define DSIM_FORCE_STOP_STATE (1 << 20)
98 #define DSIM_STOP_STATE_CNT(x) (((x) & 0x7ff) << 21)
99 #define DSIM_STOP_STATE_CNT_MASK (0x7ff << 21)
102 #define DSIM_MAIN_STAND_BY (1 << 31)
103 #define DSIM_MAIN_VRESOL(x, num_bits) (((x) & ((1 << (num_bits)) - 1)) << 16)
104 #define DSIM_MAIN_HRESOL(x, num_bits) (((x) & ((1 << (num_bits)) - 1)) << 0)
107 #define DSIM_CMD_ALLOW(x) ((x) << 28)
108 #define DSIM_STABLE_VFP(x) ((x) << 16)
109 #define DSIM_MAIN_VBP(x) ((x) << 0)
110 #define DSIM_CMD_ALLOW_MASK (0xf << 28)
111 #define DSIM_STABLE_VFP_MASK (0x7ff << 16)
112 #define DSIM_MAIN_VBP_MASK (0x7ff << 0)
115 #define DSIM_MAIN_HFP(x) ((x) << 16)
116 #define DSIM_MAIN_HBP(x) ((x) << 0)
117 #define DSIM_MAIN_HFP_MASK ((0xffff) << 16)
118 #define DSIM_MAIN_HBP_MASK ((0xffff) << 0)
121 #define DSIM_MAIN_VSA(x) ((x) << 22)
122 #define DSIM_MAIN_HSA(x) ((x) << 0)
123 #define DSIM_MAIN_VSA_MASK ((0x3ff) << 22)
124 #define DSIM_MAIN_HSA_MASK ((0xffff) << 0)
127 #define DSIM_SUB_STANDY(x) ((x) << 31)
128 #define DSIM_SUB_VRESOL(x) ((x) << 16)
129 #define DSIM_SUB_HRESOL(x) ((x) << 0)
130 #define DSIM_SUB_STANDY_MASK ((0x1) << 31)
131 #define DSIM_SUB_VRESOL_MASK ((0x7ff) << 16)
132 #define DSIM_SUB_HRESOL_MASK ((0x7ff) << 0)
135 #define DSIM_INT_PLL_STABLE (1 << 31)
136 #define DSIM_INT_SW_RST_RELEASE (1 << 30)
137 #define DSIM_INT_SFR_FIFO_EMPTY (1 << 29)
138 #define DSIM_INT_SFR_HDR_FIFO_EMPTY (1 << 28)
139 #define DSIM_INT_BTA (1 << 25)
140 #define DSIM_INT_FRAME_DONE (1 << 24)
141 #define DSIM_INT_RX_TIMEOUT (1 << 21)
142 #define DSIM_INT_BTA_TIMEOUT (1 << 20)
143 #define DSIM_INT_RX_DONE (1 << 18)
144 #define DSIM_INT_RX_TE (1 << 17)
145 #define DSIM_INT_RX_ACK (1 << 16)
146 #define DSIM_INT_RX_ECC_ERR (1 << 15)
147 #define DSIM_INT_RX_CRC_ERR (1 << 14)
150 #define DSIM_RX_DATA_FULL (1 << 25)
151 #define DSIM_RX_DATA_EMPTY (1 << 24)
152 #define DSIM_SFR_HEADER_FULL (1 << 23)
153 #define DSIM_SFR_HEADER_EMPTY (1 << 22)
154 #define DSIM_SFR_PAYLOAD_FULL (1 << 21)
155 #define DSIM_SFR_PAYLOAD_EMPTY (1 << 20)
156 #define DSIM_I80_HEADER_FULL (1 << 19)
157 #define DSIM_I80_HEADER_EMPTY (1 << 18)
158 #define DSIM_I80_PAYLOAD_FULL (1 << 17)
159 #define DSIM_I80_PAYLOAD_EMPTY (1 << 16)
160 #define DSIM_SD_HEADER_FULL (1 << 15)
161 #define DSIM_SD_HEADER_EMPTY (1 << 14)
162 #define DSIM_SD_PAYLOAD_FULL (1 << 13)
163 #define DSIM_SD_PAYLOAD_EMPTY (1 << 12)
164 #define DSIM_MD_HEADER_FULL (1 << 11)
165 #define DSIM_MD_HEADER_EMPTY (1 << 10)
166 #define DSIM_MD_PAYLOAD_FULL (1 << 9)
167 #define DSIM_MD_PAYLOAD_EMPTY (1 << 8)
168 #define DSIM_RX_FIFO (1 << 4)
169 #define DSIM_SFR_FIFO (1 << 3)
170 #define DSIM_I80_FIFO (1 << 2)
171 #define DSIM_SD_FIFO (1 << 1)
172 #define DSIM_MD_FIFO (1 << 0)
175 #define DSIM_AFC_EN (1 << 14)
176 #define DSIM_AFC_CTL(x) (((x) & 0x7) << 5)
179 #define DSIM_FREQ_BAND(x) ((x) << 24)
180 #define DSIM_PLL_EN (1 << 23)
181 #define DSIM_PLL_P(x) ((x) << 13)
182 #define DSIM_PLL_M(x) ((x) << 4)
183 #define DSIM_PLL_S(x) ((x) << 1)
186 #define DSIM_PHYCTRL_ULPS_EXIT(x) (((x) & 0x1ff) << 0)
187 #define DSIM_PHYCTRL_B_DPHYCTL_VREG_LP (1 << 30)
188 #define DSIM_PHYCTRL_B_DPHYCTL_SLEW_UP (1 << 14)
191 #define DSIM_PHYTIMING_LPX(x) ((x) << 8)
192 #define DSIM_PHYTIMING_HS_EXIT(x) ((x) << 0)
194 /* DSIM_PHYTIMING1 */
195 #define DSIM_PHYTIMING1_CLK_PREPARE(x) ((x) << 24)
196 #define DSIM_PHYTIMING1_CLK_ZERO(x) ((x) << 16)
197 #define DSIM_PHYTIMING1_CLK_POST(x) ((x) << 8)
198 #define DSIM_PHYTIMING1_CLK_TRAIL(x) ((x) << 0)
200 /* DSIM_PHYTIMING2 */
201 #define DSIM_PHYTIMING2_HS_PREPARE(x) ((x) << 16)
202 #define DSIM_PHYTIMING2_HS_ZERO(x) ((x) << 8)
203 #define DSIM_PHYTIMING2_HS_TRAIL(x) ((x) << 0)
205 #define DSI_MAX_BUS_WIDTH 4
206 #define DSI_NUM_VIRTUAL_CHANNELS 4
207 #define DSI_TX_FIFO_SIZE 2048
208 #define DSI_RX_FIFO_SIZE 256
209 #define DSI_XFER_TIMEOUT_MS 100
210 #define DSI_RX_FIFO_EMPTY 0x30800002
212 #define OLD_SCLK_MIPI_CLK_NAME "pll_clk"
214 static char *clk_names
[5] = { "bus_clk", "sclk_mipi",
215 "phyclk_mipidphy0_bitclkdiv8", "phyclk_mipidphy0_rxclkesc0",
216 "sclk_rgb_vclk_to_dsim0" };
218 enum exynos_dsi_transfer_type
{
223 struct exynos_dsi_transfer
{
224 struct list_head list
;
225 struct completion completed
;
227 struct mipi_dsi_packet packet
;
236 #define DSIM_STATE_ENABLED BIT(0)
237 #define DSIM_STATE_INITIALIZED BIT(1)
238 #define DSIM_STATE_CMD_LPM BIT(2)
239 #define DSIM_STATE_VIDOUT_AVAILABLE BIT(3)
241 struct exynos_dsi_driver_data
{
242 const unsigned int *reg_ofs
;
243 unsigned int plltmr_reg
;
244 unsigned int has_freqband
:1;
245 unsigned int has_clklane_stop
:1;
246 unsigned int num_clks
;
247 unsigned int max_freq
;
248 unsigned int wait_for_reset
;
249 unsigned int num_bits_resol
;
250 const unsigned int *reg_values
;
254 struct drm_encoder encoder
;
255 struct mipi_dsi_host dsi_host
;
256 struct drm_connector connector
;
257 struct drm_panel
*panel
;
260 void __iomem
*reg_base
;
263 struct regulator_bulk_data supplies
[2];
275 struct drm_property
*brightness
;
276 struct completion completed
;
278 spinlock_t transfer_lock
; /* protects transfer_list */
279 struct list_head transfer_list
;
281 const struct exynos_dsi_driver_data
*driver_data
;
282 struct device_node
*bridge_node
;
285 #define host_to_dsi(host) container_of(host, struct exynos_dsi, dsi_host)
286 #define connector_to_dsi(c) container_of(c, struct exynos_dsi, connector)
288 static inline struct exynos_dsi
*encoder_to_dsi(struct drm_encoder
*e
)
290 return container_of(e
, struct exynos_dsi
, encoder
);
294 DSIM_STATUS_REG
, /* Status register */
295 DSIM_SWRST_REG
, /* Software reset register */
296 DSIM_CLKCTRL_REG
, /* Clock control register */
297 DSIM_TIMEOUT_REG
, /* Time out register */
298 DSIM_CONFIG_REG
, /* Configuration register */
299 DSIM_ESCMODE_REG
, /* Escape mode register */
301 DSIM_MVPORCH_REG
, /* Main display Vporch register */
302 DSIM_MHPORCH_REG
, /* Main display Hporch register */
303 DSIM_MSYNC_REG
, /* Main display sync area register */
304 DSIM_INTSRC_REG
, /* Interrupt source register */
305 DSIM_INTMSK_REG
, /* Interrupt mask register */
306 DSIM_PKTHDR_REG
, /* Packet Header FIFO register */
307 DSIM_PAYLOAD_REG
, /* Payload FIFO register */
308 DSIM_RXFIFO_REG
, /* Read FIFO register */
309 DSIM_FIFOCTRL_REG
, /* FIFO status and control register */
310 DSIM_PLLCTRL_REG
, /* PLL control register */
318 static inline void exynos_dsi_write(struct exynos_dsi
*dsi
, enum reg_idx idx
,
322 writel(val
, dsi
->reg_base
+ dsi
->driver_data
->reg_ofs
[idx
]);
325 static inline u32
exynos_dsi_read(struct exynos_dsi
*dsi
, enum reg_idx idx
)
327 return readl(dsi
->reg_base
+ dsi
->driver_data
->reg_ofs
[idx
]);
330 static const unsigned int exynos_reg_ofs
[] = {
331 [DSIM_STATUS_REG
] = 0x00,
332 [DSIM_SWRST_REG
] = 0x04,
333 [DSIM_CLKCTRL_REG
] = 0x08,
334 [DSIM_TIMEOUT_REG
] = 0x0c,
335 [DSIM_CONFIG_REG
] = 0x10,
336 [DSIM_ESCMODE_REG
] = 0x14,
337 [DSIM_MDRESOL_REG
] = 0x18,
338 [DSIM_MVPORCH_REG
] = 0x1c,
339 [DSIM_MHPORCH_REG
] = 0x20,
340 [DSIM_MSYNC_REG
] = 0x24,
341 [DSIM_INTSRC_REG
] = 0x2c,
342 [DSIM_INTMSK_REG
] = 0x30,
343 [DSIM_PKTHDR_REG
] = 0x34,
344 [DSIM_PAYLOAD_REG
] = 0x38,
345 [DSIM_RXFIFO_REG
] = 0x3c,
346 [DSIM_FIFOCTRL_REG
] = 0x44,
347 [DSIM_PLLCTRL_REG
] = 0x4c,
348 [DSIM_PHYCTRL_REG
] = 0x5c,
349 [DSIM_PHYTIMING_REG
] = 0x64,
350 [DSIM_PHYTIMING1_REG
] = 0x68,
351 [DSIM_PHYTIMING2_REG
] = 0x6c,
354 static const unsigned int exynos5433_reg_ofs
[] = {
355 [DSIM_STATUS_REG
] = 0x04,
356 [DSIM_SWRST_REG
] = 0x0C,
357 [DSIM_CLKCTRL_REG
] = 0x10,
358 [DSIM_TIMEOUT_REG
] = 0x14,
359 [DSIM_CONFIG_REG
] = 0x18,
360 [DSIM_ESCMODE_REG
] = 0x1C,
361 [DSIM_MDRESOL_REG
] = 0x20,
362 [DSIM_MVPORCH_REG
] = 0x24,
363 [DSIM_MHPORCH_REG
] = 0x28,
364 [DSIM_MSYNC_REG
] = 0x2C,
365 [DSIM_INTSRC_REG
] = 0x34,
366 [DSIM_INTMSK_REG
] = 0x38,
367 [DSIM_PKTHDR_REG
] = 0x3C,
368 [DSIM_PAYLOAD_REG
] = 0x40,
369 [DSIM_RXFIFO_REG
] = 0x44,
370 [DSIM_FIFOCTRL_REG
] = 0x4C,
371 [DSIM_PLLCTRL_REG
] = 0x94,
372 [DSIM_PHYCTRL_REG
] = 0xA4,
373 [DSIM_PHYTIMING_REG
] = 0xB4,
374 [DSIM_PHYTIMING1_REG
] = 0xB8,
375 [DSIM_PHYTIMING2_REG
] = 0xBC,
387 PHYTIMING_CLK_PREPARE
,
391 PHYTIMING_HS_PREPARE
,
396 static const unsigned int reg_values
[] = {
397 [RESET_TYPE
] = DSIM_SWRST
,
399 [STOP_STATE_CNT
] = 0xf,
400 [PHYCTRL_ULPS_EXIT
] = DSIM_PHYCTRL_ULPS_EXIT(0x0af),
401 [PHYCTRL_VREG_LP
] = 0,
402 [PHYCTRL_SLEW_UP
] = 0,
403 [PHYTIMING_LPX
] = DSIM_PHYTIMING_LPX(0x06),
404 [PHYTIMING_HS_EXIT
] = DSIM_PHYTIMING_HS_EXIT(0x0b),
405 [PHYTIMING_CLK_PREPARE
] = DSIM_PHYTIMING1_CLK_PREPARE(0x07),
406 [PHYTIMING_CLK_ZERO
] = DSIM_PHYTIMING1_CLK_ZERO(0x27),
407 [PHYTIMING_CLK_POST
] = DSIM_PHYTIMING1_CLK_POST(0x0d),
408 [PHYTIMING_CLK_TRAIL
] = DSIM_PHYTIMING1_CLK_TRAIL(0x08),
409 [PHYTIMING_HS_PREPARE
] = DSIM_PHYTIMING2_HS_PREPARE(0x09),
410 [PHYTIMING_HS_ZERO
] = DSIM_PHYTIMING2_HS_ZERO(0x0d),
411 [PHYTIMING_HS_TRAIL
] = DSIM_PHYTIMING2_HS_TRAIL(0x0b),
414 static const unsigned int exynos5422_reg_values
[] = {
415 [RESET_TYPE
] = DSIM_SWRST
,
417 [STOP_STATE_CNT
] = 0xf,
418 [PHYCTRL_ULPS_EXIT
] = DSIM_PHYCTRL_ULPS_EXIT(0xaf),
419 [PHYCTRL_VREG_LP
] = 0,
420 [PHYCTRL_SLEW_UP
] = 0,
421 [PHYTIMING_LPX
] = DSIM_PHYTIMING_LPX(0x08),
422 [PHYTIMING_HS_EXIT
] = DSIM_PHYTIMING_HS_EXIT(0x0d),
423 [PHYTIMING_CLK_PREPARE
] = DSIM_PHYTIMING1_CLK_PREPARE(0x09),
424 [PHYTIMING_CLK_ZERO
] = DSIM_PHYTIMING1_CLK_ZERO(0x30),
425 [PHYTIMING_CLK_POST
] = DSIM_PHYTIMING1_CLK_POST(0x0e),
426 [PHYTIMING_CLK_TRAIL
] = DSIM_PHYTIMING1_CLK_TRAIL(0x0a),
427 [PHYTIMING_HS_PREPARE
] = DSIM_PHYTIMING2_HS_PREPARE(0x0c),
428 [PHYTIMING_HS_ZERO
] = DSIM_PHYTIMING2_HS_ZERO(0x11),
429 [PHYTIMING_HS_TRAIL
] = DSIM_PHYTIMING2_HS_TRAIL(0x0d),
432 static const unsigned int exynos5433_reg_values
[] = {
433 [RESET_TYPE
] = DSIM_FUNCRST
,
435 [STOP_STATE_CNT
] = 0xa,
436 [PHYCTRL_ULPS_EXIT
] = DSIM_PHYCTRL_ULPS_EXIT(0x190),
437 [PHYCTRL_VREG_LP
] = DSIM_PHYCTRL_B_DPHYCTL_VREG_LP
,
438 [PHYCTRL_SLEW_UP
] = DSIM_PHYCTRL_B_DPHYCTL_SLEW_UP
,
439 [PHYTIMING_LPX
] = DSIM_PHYTIMING_LPX(0x07),
440 [PHYTIMING_HS_EXIT
] = DSIM_PHYTIMING_HS_EXIT(0x0c),
441 [PHYTIMING_CLK_PREPARE
] = DSIM_PHYTIMING1_CLK_PREPARE(0x09),
442 [PHYTIMING_CLK_ZERO
] = DSIM_PHYTIMING1_CLK_ZERO(0x2d),
443 [PHYTIMING_CLK_POST
] = DSIM_PHYTIMING1_CLK_POST(0x0e),
444 [PHYTIMING_CLK_TRAIL
] = DSIM_PHYTIMING1_CLK_TRAIL(0x09),
445 [PHYTIMING_HS_PREPARE
] = DSIM_PHYTIMING2_HS_PREPARE(0x0b),
446 [PHYTIMING_HS_ZERO
] = DSIM_PHYTIMING2_HS_ZERO(0x10),
447 [PHYTIMING_HS_TRAIL
] = DSIM_PHYTIMING2_HS_TRAIL(0x0c),
450 static const struct exynos_dsi_driver_data exynos3_dsi_driver_data
= {
451 .reg_ofs
= exynos_reg_ofs
,
454 .has_clklane_stop
= 1,
458 .num_bits_resol
= 11,
459 .reg_values
= reg_values
,
462 static const struct exynos_dsi_driver_data exynos4_dsi_driver_data
= {
463 .reg_ofs
= exynos_reg_ofs
,
466 .has_clklane_stop
= 1,
470 .num_bits_resol
= 11,
471 .reg_values
= reg_values
,
474 static const struct exynos_dsi_driver_data exynos5_dsi_driver_data
= {
475 .reg_ofs
= exynos_reg_ofs
,
480 .num_bits_resol
= 11,
481 .reg_values
= reg_values
,
484 static const struct exynos_dsi_driver_data exynos5433_dsi_driver_data
= {
485 .reg_ofs
= exynos5433_reg_ofs
,
487 .has_clklane_stop
= 1,
491 .num_bits_resol
= 12,
492 .reg_values
= exynos5433_reg_values
,
495 static const struct exynos_dsi_driver_data exynos5422_dsi_driver_data
= {
496 .reg_ofs
= exynos5433_reg_ofs
,
498 .has_clklane_stop
= 1,
502 .num_bits_resol
= 12,
503 .reg_values
= exynos5422_reg_values
,
506 static const struct of_device_id exynos_dsi_of_match
[] = {
507 { .compatible
= "samsung,exynos3250-mipi-dsi",
508 .data
= &exynos3_dsi_driver_data
},
509 { .compatible
= "samsung,exynos4210-mipi-dsi",
510 .data
= &exynos4_dsi_driver_data
},
511 { .compatible
= "samsung,exynos5410-mipi-dsi",
512 .data
= &exynos5_dsi_driver_data
},
513 { .compatible
= "samsung,exynos5422-mipi-dsi",
514 .data
= &exynos5422_dsi_driver_data
},
515 { .compatible
= "samsung,exynos5433-mipi-dsi",
516 .data
= &exynos5433_dsi_driver_data
},
520 static void exynos_dsi_wait_for_reset(struct exynos_dsi
*dsi
)
522 if (wait_for_completion_timeout(&dsi
->completed
, msecs_to_jiffies(300)))
525 dev_err(dsi
->dev
, "timeout waiting for reset\n");
528 static void exynos_dsi_reset(struct exynos_dsi
*dsi
)
530 u32 reset_val
= dsi
->driver_data
->reg_values
[RESET_TYPE
];
532 reinit_completion(&dsi
->completed
);
533 exynos_dsi_write(dsi
, DSIM_SWRST_REG
, reset_val
);
537 #define MHZ (1000*1000)
540 static unsigned long exynos_dsi_pll_find_pms(struct exynos_dsi
*dsi
,
541 unsigned long fin
, unsigned long fout
, u8
*p
, u16
*m
, u8
*s
)
543 const struct exynos_dsi_driver_data
*driver_data
= dsi
->driver_data
;
544 unsigned long best_freq
= 0;
545 u32 min_delta
= 0xffffffff;
547 u8 _p
, uninitialized_var(best_p
);
548 u16 _m
, uninitialized_var(best_m
);
549 u8 _s
, uninitialized_var(best_s
);
551 p_min
= DIV_ROUND_UP(fin
, (12 * MHZ
));
552 p_max
= fin
/ (6 * MHZ
);
554 for (_p
= p_min
; _p
<= p_max
; ++_p
) {
555 for (_s
= 0; _s
<= 5; ++_s
) {
559 tmp
= (u64
)fout
* (_p
<< _s
);
562 if (_m
< 41 || _m
> 125)
567 if (tmp
< 500 * MHZ
||
568 tmp
> driver_data
->max_freq
* MHZ
)
572 do_div(tmp
, _p
<< _s
);
574 delta
= abs(fout
- tmp
);
575 if (delta
< min_delta
) {
594 static unsigned long exynos_dsi_set_pll(struct exynos_dsi
*dsi
,
597 const struct exynos_dsi_driver_data
*driver_data
= dsi
->driver_data
;
598 unsigned long fin
, fout
;
604 fin
= dsi
->pll_clk_rate
;
605 fout
= exynos_dsi_pll_find_pms(dsi
, fin
, freq
, &p
, &m
, &s
);
608 "failed to find PLL PMS for requested frequency\n");
611 dev_dbg(dsi
->dev
, "PLL freq %lu, (p %d, m %d, s %d)\n", fout
, p
, m
, s
);
613 writel(driver_data
->reg_values
[PLL_TIMER
],
614 dsi
->reg_base
+ driver_data
->plltmr_reg
);
616 reg
= DSIM_PLL_EN
| DSIM_PLL_P(p
) | DSIM_PLL_M(m
) | DSIM_PLL_S(s
);
618 if (driver_data
->has_freqband
) {
619 static const unsigned long freq_bands
[] = {
620 100 * MHZ
, 120 * MHZ
, 160 * MHZ
, 200 * MHZ
,
621 270 * MHZ
, 320 * MHZ
, 390 * MHZ
, 450 * MHZ
,
622 510 * MHZ
, 560 * MHZ
, 640 * MHZ
, 690 * MHZ
,
623 770 * MHZ
, 870 * MHZ
, 950 * MHZ
,
627 for (band
= 0; band
< ARRAY_SIZE(freq_bands
); ++band
)
628 if (fout
< freq_bands
[band
])
631 dev_dbg(dsi
->dev
, "band %d\n", band
);
633 reg
|= DSIM_FREQ_BAND(band
);
636 exynos_dsi_write(dsi
, DSIM_PLLCTRL_REG
, reg
);
640 if (timeout
-- == 0) {
641 dev_err(dsi
->dev
, "PLL failed to stabilize\n");
644 reg
= exynos_dsi_read(dsi
, DSIM_STATUS_REG
);
645 } while ((reg
& DSIM_PLL_STABLE
) == 0);
650 static int exynos_dsi_enable_clock(struct exynos_dsi
*dsi
)
652 unsigned long hs_clk
, byte_clk
, esc_clk
;
653 unsigned long esc_div
;
656 hs_clk
= exynos_dsi_set_pll(dsi
, dsi
->burst_clk_rate
);
658 dev_err(dsi
->dev
, "failed to configure DSI PLL\n");
662 byte_clk
= hs_clk
/ 8;
663 esc_div
= DIV_ROUND_UP(byte_clk
, dsi
->esc_clk_rate
);
664 esc_clk
= byte_clk
/ esc_div
;
666 if (esc_clk
> 20 * MHZ
) {
668 esc_clk
= byte_clk
/ esc_div
;
671 dev_dbg(dsi
->dev
, "hs_clk = %lu, byte_clk = %lu, esc_clk = %lu\n",
672 hs_clk
, byte_clk
, esc_clk
);
674 reg
= exynos_dsi_read(dsi
, DSIM_CLKCTRL_REG
);
675 reg
&= ~(DSIM_ESC_PRESCALER_MASK
| DSIM_LANE_ESC_CLK_EN_CLK
676 | DSIM_LANE_ESC_CLK_EN_DATA_MASK
| DSIM_PLL_BYPASS
677 | DSIM_BYTE_CLK_SRC_MASK
);
678 reg
|= DSIM_ESC_CLKEN
| DSIM_BYTE_CLKEN
679 | DSIM_ESC_PRESCALER(esc_div
)
680 | DSIM_LANE_ESC_CLK_EN_CLK
681 | DSIM_LANE_ESC_CLK_EN_DATA(BIT(dsi
->lanes
) - 1)
682 | DSIM_BYTE_CLK_SRC(0)
683 | DSIM_TX_REQUEST_HSCLK
;
684 exynos_dsi_write(dsi
, DSIM_CLKCTRL_REG
, reg
);
689 static void exynos_dsi_set_phy_ctrl(struct exynos_dsi
*dsi
)
691 const struct exynos_dsi_driver_data
*driver_data
= dsi
->driver_data
;
692 const unsigned int *reg_values
= driver_data
->reg_values
;
695 if (driver_data
->has_freqband
)
698 /* B D-PHY: D-PHY Master & Slave Analog Block control */
699 reg
= reg_values
[PHYCTRL_ULPS_EXIT
] | reg_values
[PHYCTRL_VREG_LP
] |
700 reg_values
[PHYCTRL_SLEW_UP
];
701 exynos_dsi_write(dsi
, DSIM_PHYCTRL_REG
, reg
);
704 * T LPX: Transmitted length of any Low-Power state period
705 * T HS-EXIT: Time that the transmitter drives LP-11 following a HS
708 reg
= reg_values
[PHYTIMING_LPX
] | reg_values
[PHYTIMING_HS_EXIT
];
709 exynos_dsi_write(dsi
, DSIM_PHYTIMING_REG
, reg
);
712 * T CLK-PREPARE: Time that the transmitter drives the Clock Lane LP-00
713 * Line state immediately before the HS-0 Line state starting the
715 * T CLK-ZERO: Time that the transmitter drives the HS-0 state prior to
716 * transmitting the Clock.
717 * T CLK_POST: Time that the transmitter continues to send HS clock
718 * after the last associated Data Lane has transitioned to LP Mode
719 * Interval is defined as the period from the end of T HS-TRAIL to
720 * the beginning of T CLK-TRAIL
721 * T CLK-TRAIL: Time that the transmitter drives the HS-0 state after
722 * the last payload clock bit of a HS transmission burst
724 reg
= reg_values
[PHYTIMING_CLK_PREPARE
] |
725 reg_values
[PHYTIMING_CLK_ZERO
] |
726 reg_values
[PHYTIMING_CLK_POST
] |
727 reg_values
[PHYTIMING_CLK_TRAIL
];
729 exynos_dsi_write(dsi
, DSIM_PHYTIMING1_REG
, reg
);
732 * T HS-PREPARE: Time that the transmitter drives the Data Lane LP-00
733 * Line state immediately before the HS-0 Line state starting the
735 * T HS-ZERO: Time that the transmitter drives the HS-0 state prior to
736 * transmitting the Sync sequence.
737 * T HS-TRAIL: Time that the transmitter drives the flipped differential
738 * state after last payload data bit of a HS transmission burst
740 reg
= reg_values
[PHYTIMING_HS_PREPARE
] | reg_values
[PHYTIMING_HS_ZERO
] |
741 reg_values
[PHYTIMING_HS_TRAIL
];
742 exynos_dsi_write(dsi
, DSIM_PHYTIMING2_REG
, reg
);
745 static void exynos_dsi_disable_clock(struct exynos_dsi
*dsi
)
749 reg
= exynos_dsi_read(dsi
, DSIM_CLKCTRL_REG
);
750 reg
&= ~(DSIM_LANE_ESC_CLK_EN_CLK
| DSIM_LANE_ESC_CLK_EN_DATA_MASK
751 | DSIM_ESC_CLKEN
| DSIM_BYTE_CLKEN
);
752 exynos_dsi_write(dsi
, DSIM_CLKCTRL_REG
, reg
);
754 reg
= exynos_dsi_read(dsi
, DSIM_PLLCTRL_REG
);
756 exynos_dsi_write(dsi
, DSIM_PLLCTRL_REG
, reg
);
759 static void exynos_dsi_enable_lane(struct exynos_dsi
*dsi
, u32 lane
)
761 u32 reg
= exynos_dsi_read(dsi
, DSIM_CONFIG_REG
);
762 reg
|= (DSIM_NUM_OF_DATA_LANE(dsi
->lanes
- 1) | DSIM_LANE_EN_CLK
|
764 exynos_dsi_write(dsi
, DSIM_CONFIG_REG
, reg
);
767 static int exynos_dsi_init_link(struct exynos_dsi
*dsi
)
769 const struct exynos_dsi_driver_data
*driver_data
= dsi
->driver_data
;
774 /* Initialize FIFO pointers */
775 reg
= exynos_dsi_read(dsi
, DSIM_FIFOCTRL_REG
);
777 exynos_dsi_write(dsi
, DSIM_FIFOCTRL_REG
, reg
);
779 usleep_range(9000, 11000);
782 exynos_dsi_write(dsi
, DSIM_FIFOCTRL_REG
, reg
);
783 usleep_range(9000, 11000);
785 /* DSI configuration */
789 * The first bit of mode_flags specifies display configuration.
790 * If this bit is set[= MIPI_DSI_MODE_VIDEO], dsi will support video
791 * mode, otherwise it will support command mode.
793 if (dsi
->mode_flags
& MIPI_DSI_MODE_VIDEO
) {
794 reg
|= DSIM_VIDEO_MODE
;
797 * The user manual describes that following bits are ignored in
800 if (!(dsi
->mode_flags
& MIPI_DSI_MODE_VSYNC_FLUSH
))
801 reg
|= DSIM_MFLUSH_VS
;
802 if (dsi
->mode_flags
& MIPI_DSI_MODE_VIDEO_SYNC_PULSE
)
803 reg
|= DSIM_SYNC_INFORM
;
804 if (dsi
->mode_flags
& MIPI_DSI_MODE_VIDEO_BURST
)
805 reg
|= DSIM_BURST_MODE
;
806 if (dsi
->mode_flags
& MIPI_DSI_MODE_VIDEO_AUTO_VERT
)
807 reg
|= DSIM_AUTO_MODE
;
808 if (dsi
->mode_flags
& MIPI_DSI_MODE_VIDEO_HSE
)
809 reg
|= DSIM_HSE_MODE
;
810 if (!(dsi
->mode_flags
& MIPI_DSI_MODE_VIDEO_HFP
))
811 reg
|= DSIM_HFP_MODE
;
812 if (!(dsi
->mode_flags
& MIPI_DSI_MODE_VIDEO_HBP
))
813 reg
|= DSIM_HBP_MODE
;
814 if (!(dsi
->mode_flags
& MIPI_DSI_MODE_VIDEO_HSA
))
815 reg
|= DSIM_HSA_MODE
;
818 if (!(dsi
->mode_flags
& MIPI_DSI_MODE_EOT_PACKET
))
819 reg
|= DSIM_EOT_DISABLE
;
821 switch (dsi
->format
) {
822 case MIPI_DSI_FMT_RGB888
:
823 reg
|= DSIM_MAIN_PIX_FORMAT_RGB888
;
825 case MIPI_DSI_FMT_RGB666
:
826 reg
|= DSIM_MAIN_PIX_FORMAT_RGB666
;
828 case MIPI_DSI_FMT_RGB666_PACKED
:
829 reg
|= DSIM_MAIN_PIX_FORMAT_RGB666_P
;
831 case MIPI_DSI_FMT_RGB565
:
832 reg
|= DSIM_MAIN_PIX_FORMAT_RGB565
;
835 dev_err(dsi
->dev
, "invalid pixel format\n");
840 * Use non-continuous clock mode if the periparal wants and
841 * host controller supports
843 * In non-continous clock mode, host controller will turn off
844 * the HS clock between high-speed transmissions to reduce
847 if (driver_data
->has_clklane_stop
&&
848 dsi
->mode_flags
& MIPI_DSI_CLOCK_NON_CONTINUOUS
) {
849 reg
|= DSIM_CLKLANE_STOP
;
851 exynos_dsi_write(dsi
, DSIM_CONFIG_REG
, reg
);
853 lanes_mask
= BIT(dsi
->lanes
) - 1;
854 exynos_dsi_enable_lane(dsi
, lanes_mask
);
856 /* Check clock and data lane state are stop state */
859 if (timeout
-- == 0) {
860 dev_err(dsi
->dev
, "waiting for bus lanes timed out\n");
864 reg
= exynos_dsi_read(dsi
, DSIM_STATUS_REG
);
865 if ((reg
& DSIM_STOP_STATE_DAT(lanes_mask
))
866 != DSIM_STOP_STATE_DAT(lanes_mask
))
868 } while (!(reg
& (DSIM_STOP_STATE_CLK
| DSIM_TX_READY_HS_CLK
)));
870 reg
= exynos_dsi_read(dsi
, DSIM_ESCMODE_REG
);
871 reg
&= ~DSIM_STOP_STATE_CNT_MASK
;
872 reg
|= DSIM_STOP_STATE_CNT(driver_data
->reg_values
[STOP_STATE_CNT
]);
873 exynos_dsi_write(dsi
, DSIM_ESCMODE_REG
, reg
);
875 reg
= DSIM_BTA_TIMEOUT(0xff) | DSIM_LPDR_TIMEOUT(0xffff);
876 exynos_dsi_write(dsi
, DSIM_TIMEOUT_REG
, reg
);
881 static void exynos_dsi_set_display_mode(struct exynos_dsi
*dsi
)
883 struct drm_display_mode
*m
= &dsi
->encoder
.crtc
->state
->adjusted_mode
;
884 unsigned int num_bits_resol
= dsi
->driver_data
->num_bits_resol
;
887 if (dsi
->mode_flags
& MIPI_DSI_MODE_VIDEO
) {
888 reg
= DSIM_CMD_ALLOW(0xf)
889 | DSIM_STABLE_VFP(m
->vsync_start
- m
->vdisplay
)
890 | DSIM_MAIN_VBP(m
->vtotal
- m
->vsync_end
);
891 exynos_dsi_write(dsi
, DSIM_MVPORCH_REG
, reg
);
893 reg
= DSIM_MAIN_HFP(m
->hsync_start
- m
->hdisplay
)
894 | DSIM_MAIN_HBP(m
->htotal
- m
->hsync_end
);
895 exynos_dsi_write(dsi
, DSIM_MHPORCH_REG
, reg
);
897 reg
= DSIM_MAIN_VSA(m
->vsync_end
- m
->vsync_start
)
898 | DSIM_MAIN_HSA(m
->hsync_end
- m
->hsync_start
);
899 exynos_dsi_write(dsi
, DSIM_MSYNC_REG
, reg
);
901 reg
= DSIM_MAIN_HRESOL(m
->hdisplay
, num_bits_resol
) |
902 DSIM_MAIN_VRESOL(m
->vdisplay
, num_bits_resol
);
904 exynos_dsi_write(dsi
, DSIM_MDRESOL_REG
, reg
);
906 dev_dbg(dsi
->dev
, "LCD size = %dx%d\n", m
->hdisplay
, m
->vdisplay
);
909 static void exynos_dsi_set_display_enable(struct exynos_dsi
*dsi
, bool enable
)
913 reg
= exynos_dsi_read(dsi
, DSIM_MDRESOL_REG
);
915 reg
|= DSIM_MAIN_STAND_BY
;
917 reg
&= ~DSIM_MAIN_STAND_BY
;
918 exynos_dsi_write(dsi
, DSIM_MDRESOL_REG
, reg
);
921 static int exynos_dsi_wait_for_hdr_fifo(struct exynos_dsi
*dsi
)
926 u32 reg
= exynos_dsi_read(dsi
, DSIM_FIFOCTRL_REG
);
928 if (!(reg
& DSIM_SFR_HEADER_FULL
))
932 usleep_range(950, 1050);
938 static void exynos_dsi_set_cmd_lpm(struct exynos_dsi
*dsi
, bool lpm
)
940 u32 v
= exynos_dsi_read(dsi
, DSIM_ESCMODE_REG
);
943 v
|= DSIM_CMD_LPDT_LP
;
945 v
&= ~DSIM_CMD_LPDT_LP
;
947 exynos_dsi_write(dsi
, DSIM_ESCMODE_REG
, v
);
950 static void exynos_dsi_force_bta(struct exynos_dsi
*dsi
)
952 u32 v
= exynos_dsi_read(dsi
, DSIM_ESCMODE_REG
);
954 exynos_dsi_write(dsi
, DSIM_ESCMODE_REG
, v
);
957 static void exynos_dsi_send_to_fifo(struct exynos_dsi
*dsi
,
958 struct exynos_dsi_transfer
*xfer
)
960 struct device
*dev
= dsi
->dev
;
961 struct mipi_dsi_packet
*pkt
= &xfer
->packet
;
962 const u8
*payload
= pkt
->payload
+ xfer
->tx_done
;
963 u16 length
= pkt
->payload_length
- xfer
->tx_done
;
964 bool first
= !xfer
->tx_done
;
967 dev_dbg(dev
, "< xfer %pK: tx len %u, done %u, rx len %u, done %u\n",
968 xfer
, length
, xfer
->tx_done
, xfer
->rx_len
, xfer
->rx_done
);
970 if (length
> DSI_TX_FIFO_SIZE
)
971 length
= DSI_TX_FIFO_SIZE
;
973 xfer
->tx_done
+= length
;
976 while (length
>= 4) {
977 reg
= get_unaligned_le32(payload
);
978 exynos_dsi_write(dsi
, DSIM_PAYLOAD_REG
, reg
);
986 reg
|= payload
[2] << 16;
989 reg
|= payload
[1] << 8;
993 exynos_dsi_write(dsi
, DSIM_PAYLOAD_REG
, reg
);
997 /* Send packet header */
1001 reg
= get_unaligned_le32(pkt
->header
);
1002 if (exynos_dsi_wait_for_hdr_fifo(dsi
)) {
1003 dev_err(dev
, "waiting for header FIFO timed out\n");
1007 if (NEQV(xfer
->flags
& MIPI_DSI_MSG_USE_LPM
,
1008 dsi
->state
& DSIM_STATE_CMD_LPM
)) {
1009 exynos_dsi_set_cmd_lpm(dsi
, xfer
->flags
& MIPI_DSI_MSG_USE_LPM
);
1010 dsi
->state
^= DSIM_STATE_CMD_LPM
;
1013 exynos_dsi_write(dsi
, DSIM_PKTHDR_REG
, reg
);
1015 if (xfer
->flags
& MIPI_DSI_MSG_REQ_ACK
)
1016 exynos_dsi_force_bta(dsi
);
1019 static void exynos_dsi_read_from_fifo(struct exynos_dsi
*dsi
,
1020 struct exynos_dsi_transfer
*xfer
)
1022 u8
*payload
= xfer
->rx_payload
+ xfer
->rx_done
;
1023 bool first
= !xfer
->rx_done
;
1024 struct device
*dev
= dsi
->dev
;
1029 reg
= exynos_dsi_read(dsi
, DSIM_RXFIFO_REG
);
1031 switch (reg
& 0x3f) {
1032 case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE
:
1033 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE
:
1034 if (xfer
->rx_len
>= 2) {
1035 payload
[1] = reg
>> 16;
1039 case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE
:
1040 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE
:
1041 payload
[0] = reg
>> 8;
1043 xfer
->rx_len
= xfer
->rx_done
;
1046 case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT
:
1047 dev_err(dev
, "DSI Error Report: 0x%04x\n",
1048 (reg
>> 8) & 0xffff);
1053 length
= (reg
>> 8) & 0xffff;
1054 if (length
> xfer
->rx_len
) {
1056 "response too long (%u > %u bytes), stripping\n",
1057 xfer
->rx_len
, length
);
1058 length
= xfer
->rx_len
;
1059 } else if (length
< xfer
->rx_len
)
1060 xfer
->rx_len
= length
;
1063 length
= xfer
->rx_len
- xfer
->rx_done
;
1064 xfer
->rx_done
+= length
;
1066 /* Receive payload */
1067 while (length
>= 4) {
1068 reg
= exynos_dsi_read(dsi
, DSIM_RXFIFO_REG
);
1069 payload
[0] = (reg
>> 0) & 0xff;
1070 payload
[1] = (reg
>> 8) & 0xff;
1071 payload
[2] = (reg
>> 16) & 0xff;
1072 payload
[3] = (reg
>> 24) & 0xff;
1078 reg
= exynos_dsi_read(dsi
, DSIM_RXFIFO_REG
);
1081 payload
[2] = (reg
>> 16) & 0xff;
1084 payload
[1] = (reg
>> 8) & 0xff;
1087 payload
[0] = reg
& 0xff;
1091 if (xfer
->rx_done
== xfer
->rx_len
)
1095 length
= DSI_RX_FIFO_SIZE
/ 4;
1097 reg
= exynos_dsi_read(dsi
, DSIM_RXFIFO_REG
);
1098 if (reg
== DSI_RX_FIFO_EMPTY
)
1103 static void exynos_dsi_transfer_start(struct exynos_dsi
*dsi
)
1105 unsigned long flags
;
1106 struct exynos_dsi_transfer
*xfer
;
1110 spin_lock_irqsave(&dsi
->transfer_lock
, flags
);
1112 if (list_empty(&dsi
->transfer_list
)) {
1113 spin_unlock_irqrestore(&dsi
->transfer_lock
, flags
);
1117 xfer
= list_first_entry(&dsi
->transfer_list
,
1118 struct exynos_dsi_transfer
, list
);
1120 spin_unlock_irqrestore(&dsi
->transfer_lock
, flags
);
1122 if (xfer
->packet
.payload_length
&&
1123 xfer
->tx_done
== xfer
->packet
.payload_length
)
1124 /* waiting for RX */
1127 exynos_dsi_send_to_fifo(dsi
, xfer
);
1129 if (xfer
->packet
.payload_length
|| xfer
->rx_len
)
1133 complete(&xfer
->completed
);
1135 spin_lock_irqsave(&dsi
->transfer_lock
, flags
);
1137 list_del_init(&xfer
->list
);
1138 start
= !list_empty(&dsi
->transfer_list
);
1140 spin_unlock_irqrestore(&dsi
->transfer_lock
, flags
);
1146 static bool exynos_dsi_transfer_finish(struct exynos_dsi
*dsi
)
1148 struct exynos_dsi_transfer
*xfer
;
1149 unsigned long flags
;
1152 spin_lock_irqsave(&dsi
->transfer_lock
, flags
);
1154 if (list_empty(&dsi
->transfer_list
)) {
1155 spin_unlock_irqrestore(&dsi
->transfer_lock
, flags
);
1159 xfer
= list_first_entry(&dsi
->transfer_list
,
1160 struct exynos_dsi_transfer
, list
);
1162 spin_unlock_irqrestore(&dsi
->transfer_lock
, flags
);
1165 "> xfer %pK, tx_len %zu, tx_done %u, rx_len %u, rx_done %u\n",
1166 xfer
, xfer
->packet
.payload_length
, xfer
->tx_done
, xfer
->rx_len
,
1169 if (xfer
->tx_done
!= xfer
->packet
.payload_length
)
1172 if (xfer
->rx_done
!= xfer
->rx_len
)
1173 exynos_dsi_read_from_fifo(dsi
, xfer
);
1175 if (xfer
->rx_done
!= xfer
->rx_len
)
1178 spin_lock_irqsave(&dsi
->transfer_lock
, flags
);
1180 list_del_init(&xfer
->list
);
1181 start
= !list_empty(&dsi
->transfer_list
);
1183 spin_unlock_irqrestore(&dsi
->transfer_lock
, flags
);
1187 complete(&xfer
->completed
);
1192 static void exynos_dsi_remove_transfer(struct exynos_dsi
*dsi
,
1193 struct exynos_dsi_transfer
*xfer
)
1195 unsigned long flags
;
1198 spin_lock_irqsave(&dsi
->transfer_lock
, flags
);
1200 if (!list_empty(&dsi
->transfer_list
) &&
1201 xfer
== list_first_entry(&dsi
->transfer_list
,
1202 struct exynos_dsi_transfer
, list
)) {
1203 list_del_init(&xfer
->list
);
1204 start
= !list_empty(&dsi
->transfer_list
);
1205 spin_unlock_irqrestore(&dsi
->transfer_lock
, flags
);
1207 exynos_dsi_transfer_start(dsi
);
1211 list_del_init(&xfer
->list
);
1213 spin_unlock_irqrestore(&dsi
->transfer_lock
, flags
);
1216 static int exynos_dsi_transfer(struct exynos_dsi
*dsi
,
1217 struct exynos_dsi_transfer
*xfer
)
1219 unsigned long flags
;
1224 xfer
->result
= -ETIMEDOUT
;
1225 init_completion(&xfer
->completed
);
1227 spin_lock_irqsave(&dsi
->transfer_lock
, flags
);
1229 stopped
= list_empty(&dsi
->transfer_list
);
1230 list_add_tail(&xfer
->list
, &dsi
->transfer_list
);
1232 spin_unlock_irqrestore(&dsi
->transfer_lock
, flags
);
1235 exynos_dsi_transfer_start(dsi
);
1237 wait_for_completion_timeout(&xfer
->completed
,
1238 msecs_to_jiffies(DSI_XFER_TIMEOUT_MS
));
1239 if (xfer
->result
== -ETIMEDOUT
) {
1240 struct mipi_dsi_packet
*pkt
= &xfer
->packet
;
1241 exynos_dsi_remove_transfer(dsi
, xfer
);
1242 dev_err(dsi
->dev
, "xfer timed out: %*ph %*ph\n", 4, pkt
->header
,
1243 (int)pkt
->payload_length
, pkt
->payload
);
1247 /* Also covers hardware timeout condition */
1248 return xfer
->result
;
1251 static irqreturn_t
exynos_dsi_irq(int irq
, void *dev_id
)
1253 struct exynos_dsi
*dsi
= dev_id
;
1256 status
= exynos_dsi_read(dsi
, DSIM_INTSRC_REG
);
1258 static unsigned long int j
;
1259 if (printk_timed_ratelimit(&j
, 500))
1260 dev_warn(dsi
->dev
, "spurious interrupt\n");
1263 exynos_dsi_write(dsi
, DSIM_INTSRC_REG
, status
);
1265 if (status
& DSIM_INT_SW_RST_RELEASE
) {
1266 u32 mask
= ~(DSIM_INT_RX_DONE
| DSIM_INT_SFR_FIFO_EMPTY
|
1267 DSIM_INT_SFR_HDR_FIFO_EMPTY
| DSIM_INT_RX_ECC_ERR
|
1268 DSIM_INT_SW_RST_RELEASE
);
1269 exynos_dsi_write(dsi
, DSIM_INTMSK_REG
, mask
);
1270 complete(&dsi
->completed
);
1274 if (!(status
& (DSIM_INT_RX_DONE
| DSIM_INT_SFR_FIFO_EMPTY
|
1275 DSIM_INT_PLL_STABLE
)))
1278 if (exynos_dsi_transfer_finish(dsi
))
1279 exynos_dsi_transfer_start(dsi
);
1284 static irqreturn_t
exynos_dsi_te_irq_handler(int irq
, void *dev_id
)
1286 struct exynos_dsi
*dsi
= (struct exynos_dsi
*)dev_id
;
1287 struct drm_encoder
*encoder
= &dsi
->encoder
;
1289 if (dsi
->state
& DSIM_STATE_VIDOUT_AVAILABLE
)
1290 exynos_drm_crtc_te_handler(encoder
->crtc
);
1295 static void exynos_dsi_enable_irq(struct exynos_dsi
*dsi
)
1297 enable_irq(dsi
->irq
);
1299 if (gpio_is_valid(dsi
->te_gpio
))
1300 enable_irq(gpio_to_irq(dsi
->te_gpio
));
1303 static void exynos_dsi_disable_irq(struct exynos_dsi
*dsi
)
1305 if (gpio_is_valid(dsi
->te_gpio
))
1306 disable_irq(gpio_to_irq(dsi
->te_gpio
));
1308 disable_irq(dsi
->irq
);
1311 static int exynos_dsi_init(struct exynos_dsi
*dsi
)
1313 const struct exynos_dsi_driver_data
*driver_data
= dsi
->driver_data
;
1315 exynos_dsi_reset(dsi
);
1316 exynos_dsi_enable_irq(dsi
);
1318 if (driver_data
->reg_values
[RESET_TYPE
] == DSIM_FUNCRST
)
1319 exynos_dsi_enable_lane(dsi
, BIT(dsi
->lanes
) - 1);
1321 exynos_dsi_enable_clock(dsi
);
1322 if (driver_data
->wait_for_reset
)
1323 exynos_dsi_wait_for_reset(dsi
);
1324 exynos_dsi_set_phy_ctrl(dsi
);
1325 exynos_dsi_init_link(dsi
);
1330 static int exynos_dsi_register_te_irq(struct exynos_dsi
*dsi
,
1331 struct device
*panel
)
1336 dsi
->te_gpio
= of_get_named_gpio(panel
->of_node
, "te-gpios", 0);
1337 if (dsi
->te_gpio
== -ENOENT
)
1340 if (!gpio_is_valid(dsi
->te_gpio
)) {
1342 dev_err(dsi
->dev
, "cannot get te-gpios, %d\n", ret
);
1346 ret
= gpio_request(dsi
->te_gpio
, "te_gpio");
1348 dev_err(dsi
->dev
, "gpio request failed with %d\n", ret
);
1352 te_gpio_irq
= gpio_to_irq(dsi
->te_gpio
);
1353 irq_set_status_flags(te_gpio_irq
, IRQ_NOAUTOEN
);
1355 ret
= request_threaded_irq(te_gpio_irq
, exynos_dsi_te_irq_handler
, NULL
,
1356 IRQF_TRIGGER_RISING
, "TE", dsi
);
1358 dev_err(dsi
->dev
, "request interrupt failed with %d\n", ret
);
1359 gpio_free(dsi
->te_gpio
);
1367 static void exynos_dsi_unregister_te_irq(struct exynos_dsi
*dsi
)
1369 if (gpio_is_valid(dsi
->te_gpio
)) {
1370 free_irq(gpio_to_irq(dsi
->te_gpio
), dsi
);
1371 gpio_free(dsi
->te_gpio
);
1372 dsi
->te_gpio
= -ENOENT
;
1376 static void exynos_dsi_enable(struct drm_encoder
*encoder
)
1378 struct exynos_dsi
*dsi
= encoder_to_dsi(encoder
);
1381 if (dsi
->state
& DSIM_STATE_ENABLED
)
1384 pm_runtime_get_sync(dsi
->dev
);
1386 dsi
->state
|= DSIM_STATE_ENABLED
;
1388 ret
= drm_panel_prepare(dsi
->panel
);
1390 dsi
->state
&= ~DSIM_STATE_ENABLED
;
1391 pm_runtime_put_sync(dsi
->dev
);
1395 exynos_dsi_set_display_mode(dsi
);
1396 exynos_dsi_set_display_enable(dsi
, true);
1398 ret
= drm_panel_enable(dsi
->panel
);
1400 dsi
->state
&= ~DSIM_STATE_ENABLED
;
1401 exynos_dsi_set_display_enable(dsi
, false);
1402 drm_panel_unprepare(dsi
->panel
);
1403 pm_runtime_put_sync(dsi
->dev
);
1407 dsi
->state
|= DSIM_STATE_VIDOUT_AVAILABLE
;
1410 static void exynos_dsi_disable(struct drm_encoder
*encoder
)
1412 struct exynos_dsi
*dsi
= encoder_to_dsi(encoder
);
1414 if (!(dsi
->state
& DSIM_STATE_ENABLED
))
1417 dsi
->state
&= ~DSIM_STATE_VIDOUT_AVAILABLE
;
1419 drm_panel_disable(dsi
->panel
);
1420 exynos_dsi_set_display_enable(dsi
, false);
1421 drm_panel_unprepare(dsi
->panel
);
1423 dsi
->state
&= ~DSIM_STATE_ENABLED
;
1425 pm_runtime_put_sync(dsi
->dev
);
1428 static enum drm_connector_status
1429 exynos_dsi_detect(struct drm_connector
*connector
, bool force
)
1431 return connector
->status
;
1434 static void exynos_dsi_connector_destroy(struct drm_connector
*connector
)
1436 drm_connector_unregister(connector
);
1437 drm_connector_cleanup(connector
);
1438 connector
->dev
= NULL
;
1441 static const struct drm_connector_funcs exynos_dsi_connector_funcs
= {
1442 .detect
= exynos_dsi_detect
,
1443 .fill_modes
= drm_helper_probe_single_connector_modes
,
1444 .destroy
= exynos_dsi_connector_destroy
,
1445 .reset
= drm_atomic_helper_connector_reset
,
1446 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
1447 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
1450 static int exynos_dsi_get_modes(struct drm_connector
*connector
)
1452 struct exynos_dsi
*dsi
= connector_to_dsi(connector
);
1455 return dsi
->panel
->funcs
->get_modes(dsi
->panel
);
1460 static const struct drm_connector_helper_funcs exynos_dsi_connector_helper_funcs
= {
1461 .get_modes
= exynos_dsi_get_modes
,
1464 static int exynos_dsi_create_connector(struct drm_encoder
*encoder
)
1466 struct exynos_dsi
*dsi
= encoder_to_dsi(encoder
);
1467 struct drm_connector
*connector
= &dsi
->connector
;
1470 connector
->polled
= DRM_CONNECTOR_POLL_HPD
;
1472 ret
= drm_connector_init(encoder
->dev
, connector
,
1473 &exynos_dsi_connector_funcs
,
1474 DRM_MODE_CONNECTOR_DSI
);
1476 DRM_ERROR("Failed to initialize connector with drm\n");
1480 connector
->status
= connector_status_disconnected
;
1481 drm_connector_helper_add(connector
, &exynos_dsi_connector_helper_funcs
);
1482 drm_connector_attach_encoder(connector
, encoder
);
1487 static const struct drm_encoder_helper_funcs exynos_dsi_encoder_helper_funcs
= {
1488 .enable
= exynos_dsi_enable
,
1489 .disable
= exynos_dsi_disable
,
1492 static const struct drm_encoder_funcs exynos_dsi_encoder_funcs
= {
1493 .destroy
= drm_encoder_cleanup
,
1496 MODULE_DEVICE_TABLE(of
, exynos_dsi_of_match
);
1498 static int exynos_dsi_host_attach(struct mipi_dsi_host
*host
,
1499 struct mipi_dsi_device
*device
)
1501 struct exynos_dsi
*dsi
= host_to_dsi(host
);
1502 struct drm_device
*drm
= dsi
->connector
.dev
;
1505 * This is a temporary solution and should be made by more generic way.
1507 * If attached panel device is for command mode one, dsi should register
1508 * TE interrupt handler.
1510 if (!(device
->mode_flags
& MIPI_DSI_MODE_VIDEO
)) {
1511 int ret
= exynos_dsi_register_te_irq(dsi
, &device
->dev
);
1516 mutex_lock(&drm
->mode_config
.mutex
);
1518 dsi
->lanes
= device
->lanes
;
1519 dsi
->format
= device
->format
;
1520 dsi
->mode_flags
= device
->mode_flags
;
1521 dsi
->panel
= of_drm_find_panel(device
->dev
.of_node
);
1522 if (IS_ERR(dsi
->panel
))
1526 drm_panel_attach(dsi
->panel
, &dsi
->connector
);
1527 dsi
->connector
.status
= connector_status_connected
;
1529 exynos_drm_crtc_get_by_type(drm
, EXYNOS_DISPLAY_TYPE_LCD
)->i80_mode
=
1530 !(dsi
->mode_flags
& MIPI_DSI_MODE_VIDEO
);
1532 mutex_unlock(&drm
->mode_config
.mutex
);
1534 if (drm
->mode_config
.poll_enabled
)
1535 drm_kms_helper_hotplug_event(drm
);
1540 static int exynos_dsi_host_detach(struct mipi_dsi_host
*host
,
1541 struct mipi_dsi_device
*device
)
1543 struct exynos_dsi
*dsi
= host_to_dsi(host
);
1544 struct drm_device
*drm
= dsi
->connector
.dev
;
1546 mutex_lock(&drm
->mode_config
.mutex
);
1549 exynos_dsi_disable(&dsi
->encoder
);
1550 drm_panel_detach(dsi
->panel
);
1552 dsi
->connector
.status
= connector_status_disconnected
;
1555 mutex_unlock(&drm
->mode_config
.mutex
);
1557 if (drm
->mode_config
.poll_enabled
)
1558 drm_kms_helper_hotplug_event(drm
);
1560 exynos_dsi_unregister_te_irq(dsi
);
1565 static ssize_t
exynos_dsi_host_transfer(struct mipi_dsi_host
*host
,
1566 const struct mipi_dsi_msg
*msg
)
1568 struct exynos_dsi
*dsi
= host_to_dsi(host
);
1569 struct exynos_dsi_transfer xfer
;
1572 if (!(dsi
->state
& DSIM_STATE_ENABLED
))
1575 if (!(dsi
->state
& DSIM_STATE_INITIALIZED
)) {
1576 ret
= exynos_dsi_init(dsi
);
1579 dsi
->state
|= DSIM_STATE_INITIALIZED
;
1582 ret
= mipi_dsi_create_packet(&xfer
.packet
, msg
);
1586 xfer
.rx_len
= msg
->rx_len
;
1587 xfer
.rx_payload
= msg
->rx_buf
;
1588 xfer
.flags
= msg
->flags
;
1590 ret
= exynos_dsi_transfer(dsi
, &xfer
);
1591 return (ret
< 0) ? ret
: xfer
.rx_done
;
1594 static const struct mipi_dsi_host_ops exynos_dsi_ops
= {
1595 .attach
= exynos_dsi_host_attach
,
1596 .detach
= exynos_dsi_host_detach
,
1597 .transfer
= exynos_dsi_host_transfer
,
1600 static int exynos_dsi_of_read_u32(const struct device_node
*np
,
1601 const char *propname
, u32
*out_value
)
1603 int ret
= of_property_read_u32(np
, propname
, out_value
);
1606 pr_err("%pOF: failed to get '%s' property\n", np
, propname
);
1616 static int exynos_dsi_parse_dt(struct exynos_dsi
*dsi
)
1618 struct device
*dev
= dsi
->dev
;
1619 struct device_node
*node
= dev
->of_node
;
1622 ret
= exynos_dsi_of_read_u32(node
, "samsung,pll-clock-frequency",
1623 &dsi
->pll_clk_rate
);
1627 ret
= exynos_dsi_of_read_u32(node
, "samsung,burst-clock-frequency",
1628 &dsi
->burst_clk_rate
);
1632 ret
= exynos_dsi_of_read_u32(node
, "samsung,esc-clock-frequency",
1633 &dsi
->esc_clk_rate
);
1637 dsi
->bridge_node
= of_graph_get_remote_node(node
, DSI_PORT_IN
, 0);
1642 static int exynos_dsi_bind(struct device
*dev
, struct device
*master
,
1645 struct drm_encoder
*encoder
= dev_get_drvdata(dev
);
1646 struct exynos_dsi
*dsi
= encoder_to_dsi(encoder
);
1647 struct drm_device
*drm_dev
= data
;
1648 struct drm_bridge
*bridge
;
1651 drm_encoder_init(drm_dev
, encoder
, &exynos_dsi_encoder_funcs
,
1652 DRM_MODE_ENCODER_TMDS
, NULL
);
1654 drm_encoder_helper_add(encoder
, &exynos_dsi_encoder_helper_funcs
);
1656 ret
= exynos_drm_set_possible_crtcs(encoder
, EXYNOS_DISPLAY_TYPE_LCD
);
1660 ret
= exynos_dsi_create_connector(encoder
);
1662 DRM_ERROR("failed to create connector ret = %d\n", ret
);
1663 drm_encoder_cleanup(encoder
);
1667 if (dsi
->bridge_node
) {
1668 bridge
= of_drm_find_bridge(dsi
->bridge_node
);
1670 drm_bridge_attach(encoder
, bridge
, NULL
);
1673 return mipi_dsi_host_register(&dsi
->dsi_host
);
1676 static void exynos_dsi_unbind(struct device
*dev
, struct device
*master
,
1679 struct drm_encoder
*encoder
= dev_get_drvdata(dev
);
1680 struct exynos_dsi
*dsi
= encoder_to_dsi(encoder
);
1682 exynos_dsi_disable(encoder
);
1684 mipi_dsi_host_unregister(&dsi
->dsi_host
);
1687 static const struct component_ops exynos_dsi_component_ops
= {
1688 .bind
= exynos_dsi_bind
,
1689 .unbind
= exynos_dsi_unbind
,
1692 static int exynos_dsi_probe(struct platform_device
*pdev
)
1694 struct device
*dev
= &pdev
->dev
;
1695 struct resource
*res
;
1696 struct exynos_dsi
*dsi
;
1699 dsi
= devm_kzalloc(dev
, sizeof(*dsi
), GFP_KERNEL
);
1703 /* To be checked as invalid one */
1704 dsi
->te_gpio
= -ENOENT
;
1706 init_completion(&dsi
->completed
);
1707 spin_lock_init(&dsi
->transfer_lock
);
1708 INIT_LIST_HEAD(&dsi
->transfer_list
);
1710 dsi
->dsi_host
.ops
= &exynos_dsi_ops
;
1711 dsi
->dsi_host
.dev
= dev
;
1714 dsi
->driver_data
= of_device_get_match_data(dev
);
1716 ret
= exynos_dsi_parse_dt(dsi
);
1720 dsi
->supplies
[0].supply
= "vddcore";
1721 dsi
->supplies
[1].supply
= "vddio";
1722 ret
= devm_regulator_bulk_get(dev
, ARRAY_SIZE(dsi
->supplies
),
1725 if (ret
!= -EPROBE_DEFER
)
1726 dev_info(dev
, "failed to get regulators: %d\n", ret
);
1730 dsi
->clks
= devm_kcalloc(dev
,
1731 dsi
->driver_data
->num_clks
, sizeof(*dsi
->clks
),
1736 for (i
= 0; i
< dsi
->driver_data
->num_clks
; i
++) {
1737 dsi
->clks
[i
] = devm_clk_get(dev
, clk_names
[i
]);
1738 if (IS_ERR(dsi
->clks
[i
])) {
1739 if (strcmp(clk_names
[i
], "sclk_mipi") == 0) {
1740 dsi
->clks
[i
] = devm_clk_get(dev
,
1741 OLD_SCLK_MIPI_CLK_NAME
);
1742 if (!IS_ERR(dsi
->clks
[i
]))
1746 dev_info(dev
, "failed to get the clock: %s\n",
1748 return PTR_ERR(dsi
->clks
[i
]);
1752 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1753 dsi
->reg_base
= devm_ioremap_resource(dev
, res
);
1754 if (IS_ERR(dsi
->reg_base
)) {
1755 dev_err(dev
, "failed to remap io region\n");
1756 return PTR_ERR(dsi
->reg_base
);
1759 dsi
->phy
= devm_phy_get(dev
, "dsim");
1760 if (IS_ERR(dsi
->phy
)) {
1761 dev_info(dev
, "failed to get dsim phy\n");
1762 return PTR_ERR(dsi
->phy
);
1765 dsi
->irq
= platform_get_irq(pdev
, 0);
1767 dev_err(dev
, "failed to request dsi irq resource\n");
1771 irq_set_status_flags(dsi
->irq
, IRQ_NOAUTOEN
);
1772 ret
= devm_request_threaded_irq(dev
, dsi
->irq
, NULL
,
1773 exynos_dsi_irq
, IRQF_ONESHOT
,
1774 dev_name(dev
), dsi
);
1776 dev_err(dev
, "failed to request dsi irq\n");
1780 platform_set_drvdata(pdev
, &dsi
->encoder
);
1782 pm_runtime_enable(dev
);
1784 return component_add(dev
, &exynos_dsi_component_ops
);
1787 static int exynos_dsi_remove(struct platform_device
*pdev
)
1789 struct exynos_dsi
*dsi
= platform_get_drvdata(pdev
);
1791 of_node_put(dsi
->bridge_node
);
1793 pm_runtime_disable(&pdev
->dev
);
1795 component_del(&pdev
->dev
, &exynos_dsi_component_ops
);
1800 static int __maybe_unused
exynos_dsi_suspend(struct device
*dev
)
1802 struct drm_encoder
*encoder
= dev_get_drvdata(dev
);
1803 struct exynos_dsi
*dsi
= encoder_to_dsi(encoder
);
1804 const struct exynos_dsi_driver_data
*driver_data
= dsi
->driver_data
;
1807 usleep_range(10000, 20000);
1809 if (dsi
->state
& DSIM_STATE_INITIALIZED
) {
1810 dsi
->state
&= ~DSIM_STATE_INITIALIZED
;
1812 exynos_dsi_disable_clock(dsi
);
1814 exynos_dsi_disable_irq(dsi
);
1817 dsi
->state
&= ~DSIM_STATE_CMD_LPM
;
1819 phy_power_off(dsi
->phy
);
1821 for (i
= driver_data
->num_clks
- 1; i
> -1; i
--)
1822 clk_disable_unprepare(dsi
->clks
[i
]);
1824 ret
= regulator_bulk_disable(ARRAY_SIZE(dsi
->supplies
), dsi
->supplies
);
1826 dev_err(dsi
->dev
, "cannot disable regulators %d\n", ret
);
1831 static int __maybe_unused
exynos_dsi_resume(struct device
*dev
)
1833 struct drm_encoder
*encoder
= dev_get_drvdata(dev
);
1834 struct exynos_dsi
*dsi
= encoder_to_dsi(encoder
);
1835 const struct exynos_dsi_driver_data
*driver_data
= dsi
->driver_data
;
1838 ret
= regulator_bulk_enable(ARRAY_SIZE(dsi
->supplies
), dsi
->supplies
);
1840 dev_err(dsi
->dev
, "cannot enable regulators %d\n", ret
);
1844 for (i
= 0; i
< driver_data
->num_clks
; i
++) {
1845 ret
= clk_prepare_enable(dsi
->clks
[i
]);
1850 ret
= phy_power_on(dsi
->phy
);
1852 dev_err(dsi
->dev
, "cannot enable phy %d\n", ret
);
1860 clk_disable_unprepare(dsi
->clks
[i
]);
1861 regulator_bulk_disable(ARRAY_SIZE(dsi
->supplies
), dsi
->supplies
);
1866 static const struct dev_pm_ops exynos_dsi_pm_ops
= {
1867 SET_RUNTIME_PM_OPS(exynos_dsi_suspend
, exynos_dsi_resume
, NULL
)
1868 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
1869 pm_runtime_force_resume
)
1872 struct platform_driver dsi_driver
= {
1873 .probe
= exynos_dsi_probe
,
1874 .remove
= exynos_dsi_remove
,
1876 .name
= "exynos-dsi",
1877 .owner
= THIS_MODULE
,
1878 .pm
= &exynos_dsi_pm_ops
,
1879 .of_match_table
= exynos_dsi_of_match
,
1883 MODULE_AUTHOR("Tomasz Figa <t.figa@samsung.com>");
1884 MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
1885 MODULE_DESCRIPTION("Samsung SoC MIPI DSI Master");
1886 MODULE_LICENSE("GPL v2");