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.
14 #include <drm/drm_crtc_helper.h>
15 #include <drm/drm_mipi_dsi.h>
16 #include <drm/drm_panel.h>
17 #include <drm/drm_atomic_helper.h>
19 #include <linux/clk.h>
20 #include <linux/gpio/consumer.h>
21 #include <linux/irq.h>
22 #include <linux/of_device.h>
23 #include <linux/of_gpio.h>
24 #include <linux/of_graph.h>
25 #include <linux/phy/phy.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/component.h>
29 #include <video/mipi_display.h>
30 #include <video/videomode.h>
32 #include "exynos_drm_crtc.h"
33 #include "exynos_drm_drv.h"
35 /* returns true iff both arguments logically differs */
36 #define NEQV(a, b) (!(a) ^ !(b))
39 #define DSIM_STOP_STATE_DAT(x) (((x) & 0xf) << 0)
40 #define DSIM_STOP_STATE_CLK (1 << 8)
41 #define DSIM_TX_READY_HS_CLK (1 << 10)
42 #define DSIM_PLL_STABLE (1 << 31)
45 #define DSIM_FUNCRST (1 << 16)
46 #define DSIM_SWRST (1 << 0)
49 #define DSIM_LPDR_TIMEOUT(x) ((x) << 0)
50 #define DSIM_BTA_TIMEOUT(x) ((x) << 16)
53 #define DSIM_ESC_PRESCALER(x) (((x) & 0xffff) << 0)
54 #define DSIM_ESC_PRESCALER_MASK (0xffff << 0)
55 #define DSIM_LANE_ESC_CLK_EN_CLK (1 << 19)
56 #define DSIM_LANE_ESC_CLK_EN_DATA(x) (((x) & 0xf) << 20)
57 #define DSIM_LANE_ESC_CLK_EN_DATA_MASK (0xf << 20)
58 #define DSIM_BYTE_CLKEN (1 << 24)
59 #define DSIM_BYTE_CLK_SRC(x) (((x) & 0x3) << 25)
60 #define DSIM_BYTE_CLK_SRC_MASK (0x3 << 25)
61 #define DSIM_PLL_BYPASS (1 << 27)
62 #define DSIM_ESC_CLKEN (1 << 28)
63 #define DSIM_TX_REQUEST_HSCLK (1 << 31)
66 #define DSIM_LANE_EN_CLK (1 << 0)
67 #define DSIM_LANE_EN(x) (((x) & 0xf) << 1)
68 #define DSIM_NUM_OF_DATA_LANE(x) (((x) & 0x3) << 5)
69 #define DSIM_SUB_PIX_FORMAT(x) (((x) & 0x7) << 8)
70 #define DSIM_MAIN_PIX_FORMAT_MASK (0x7 << 12)
71 #define DSIM_MAIN_PIX_FORMAT_RGB888 (0x7 << 12)
72 #define DSIM_MAIN_PIX_FORMAT_RGB666 (0x6 << 12)
73 #define DSIM_MAIN_PIX_FORMAT_RGB666_P (0x5 << 12)
74 #define DSIM_MAIN_PIX_FORMAT_RGB565 (0x4 << 12)
75 #define DSIM_SUB_VC (((x) & 0x3) << 16)
76 #define DSIM_MAIN_VC (((x) & 0x3) << 18)
77 #define DSIM_HSA_MODE (1 << 20)
78 #define DSIM_HBP_MODE (1 << 21)
79 #define DSIM_HFP_MODE (1 << 22)
80 #define DSIM_HSE_MODE (1 << 23)
81 #define DSIM_AUTO_MODE (1 << 24)
82 #define DSIM_VIDEO_MODE (1 << 25)
83 #define DSIM_BURST_MODE (1 << 26)
84 #define DSIM_SYNC_INFORM (1 << 27)
85 #define DSIM_EOT_DISABLE (1 << 28)
86 #define DSIM_MFLUSH_VS (1 << 29)
87 /* This flag is valid only for exynos3250/3472/4415/5260/5430 */
88 #define DSIM_CLKLANE_STOP (1 << 30)
91 #define DSIM_TX_TRIGGER_RST (1 << 4)
92 #define DSIM_TX_LPDT_LP (1 << 6)
93 #define DSIM_CMD_LPDT_LP (1 << 7)
94 #define DSIM_FORCE_BTA (1 << 16)
95 #define DSIM_FORCE_STOP_STATE (1 << 20)
96 #define DSIM_STOP_STATE_CNT(x) (((x) & 0x7ff) << 21)
97 #define DSIM_STOP_STATE_CNT_MASK (0x7ff << 21)
100 #define DSIM_MAIN_STAND_BY (1 << 31)
101 #define DSIM_MAIN_VRESOL(x, num_bits) (((x) & ((1 << (num_bits)) - 1)) << 16)
102 #define DSIM_MAIN_HRESOL(x, num_bits) (((x) & ((1 << (num_bits)) - 1)) << 0)
105 #define DSIM_CMD_ALLOW(x) ((x) << 28)
106 #define DSIM_STABLE_VFP(x) ((x) << 16)
107 #define DSIM_MAIN_VBP(x) ((x) << 0)
108 #define DSIM_CMD_ALLOW_MASK (0xf << 28)
109 #define DSIM_STABLE_VFP_MASK (0x7ff << 16)
110 #define DSIM_MAIN_VBP_MASK (0x7ff << 0)
113 #define DSIM_MAIN_HFP(x) ((x) << 16)
114 #define DSIM_MAIN_HBP(x) ((x) << 0)
115 #define DSIM_MAIN_HFP_MASK ((0xffff) << 16)
116 #define DSIM_MAIN_HBP_MASK ((0xffff) << 0)
119 #define DSIM_MAIN_VSA(x) ((x) << 22)
120 #define DSIM_MAIN_HSA(x) ((x) << 0)
121 #define DSIM_MAIN_VSA_MASK ((0x3ff) << 22)
122 #define DSIM_MAIN_HSA_MASK ((0xffff) << 0)
125 #define DSIM_SUB_STANDY(x) ((x) << 31)
126 #define DSIM_SUB_VRESOL(x) ((x) << 16)
127 #define DSIM_SUB_HRESOL(x) ((x) << 0)
128 #define DSIM_SUB_STANDY_MASK ((0x1) << 31)
129 #define DSIM_SUB_VRESOL_MASK ((0x7ff) << 16)
130 #define DSIM_SUB_HRESOL_MASK ((0x7ff) << 0)
133 #define DSIM_INT_PLL_STABLE (1 << 31)
134 #define DSIM_INT_SW_RST_RELEASE (1 << 30)
135 #define DSIM_INT_SFR_FIFO_EMPTY (1 << 29)
136 #define DSIM_INT_SFR_HDR_FIFO_EMPTY (1 << 28)
137 #define DSIM_INT_BTA (1 << 25)
138 #define DSIM_INT_FRAME_DONE (1 << 24)
139 #define DSIM_INT_RX_TIMEOUT (1 << 21)
140 #define DSIM_INT_BTA_TIMEOUT (1 << 20)
141 #define DSIM_INT_RX_DONE (1 << 18)
142 #define DSIM_INT_RX_TE (1 << 17)
143 #define DSIM_INT_RX_ACK (1 << 16)
144 #define DSIM_INT_RX_ECC_ERR (1 << 15)
145 #define DSIM_INT_RX_CRC_ERR (1 << 14)
148 #define DSIM_RX_DATA_FULL (1 << 25)
149 #define DSIM_RX_DATA_EMPTY (1 << 24)
150 #define DSIM_SFR_HEADER_FULL (1 << 23)
151 #define DSIM_SFR_HEADER_EMPTY (1 << 22)
152 #define DSIM_SFR_PAYLOAD_FULL (1 << 21)
153 #define DSIM_SFR_PAYLOAD_EMPTY (1 << 20)
154 #define DSIM_I80_HEADER_FULL (1 << 19)
155 #define DSIM_I80_HEADER_EMPTY (1 << 18)
156 #define DSIM_I80_PAYLOAD_FULL (1 << 17)
157 #define DSIM_I80_PAYLOAD_EMPTY (1 << 16)
158 #define DSIM_SD_HEADER_FULL (1 << 15)
159 #define DSIM_SD_HEADER_EMPTY (1 << 14)
160 #define DSIM_SD_PAYLOAD_FULL (1 << 13)
161 #define DSIM_SD_PAYLOAD_EMPTY (1 << 12)
162 #define DSIM_MD_HEADER_FULL (1 << 11)
163 #define DSIM_MD_HEADER_EMPTY (1 << 10)
164 #define DSIM_MD_PAYLOAD_FULL (1 << 9)
165 #define DSIM_MD_PAYLOAD_EMPTY (1 << 8)
166 #define DSIM_RX_FIFO (1 << 4)
167 #define DSIM_SFR_FIFO (1 << 3)
168 #define DSIM_I80_FIFO (1 << 2)
169 #define DSIM_SD_FIFO (1 << 1)
170 #define DSIM_MD_FIFO (1 << 0)
173 #define DSIM_AFC_EN (1 << 14)
174 #define DSIM_AFC_CTL(x) (((x) & 0x7) << 5)
177 #define DSIM_FREQ_BAND(x) ((x) << 24)
178 #define DSIM_PLL_EN (1 << 23)
179 #define DSIM_PLL_P(x) ((x) << 13)
180 #define DSIM_PLL_M(x) ((x) << 4)
181 #define DSIM_PLL_S(x) ((x) << 1)
184 #define DSIM_PHYCTRL_ULPS_EXIT(x) (((x) & 0x1ff) << 0)
185 #define DSIM_PHYCTRL_B_DPHYCTL_VREG_LP (1 << 30)
186 #define DSIM_PHYCTRL_B_DPHYCTL_SLEW_UP (1 << 14)
189 #define DSIM_PHYTIMING_LPX(x) ((x) << 8)
190 #define DSIM_PHYTIMING_HS_EXIT(x) ((x) << 0)
192 /* DSIM_PHYTIMING1 */
193 #define DSIM_PHYTIMING1_CLK_PREPARE(x) ((x) << 24)
194 #define DSIM_PHYTIMING1_CLK_ZERO(x) ((x) << 16)
195 #define DSIM_PHYTIMING1_CLK_POST(x) ((x) << 8)
196 #define DSIM_PHYTIMING1_CLK_TRAIL(x) ((x) << 0)
198 /* DSIM_PHYTIMING2 */
199 #define DSIM_PHYTIMING2_HS_PREPARE(x) ((x) << 16)
200 #define DSIM_PHYTIMING2_HS_ZERO(x) ((x) << 8)
201 #define DSIM_PHYTIMING2_HS_TRAIL(x) ((x) << 0)
203 #define DSI_MAX_BUS_WIDTH 4
204 #define DSI_NUM_VIRTUAL_CHANNELS 4
205 #define DSI_TX_FIFO_SIZE 2048
206 #define DSI_RX_FIFO_SIZE 256
207 #define DSI_XFER_TIMEOUT_MS 100
208 #define DSI_RX_FIFO_EMPTY 0x30800002
210 #define OLD_SCLK_MIPI_CLK_NAME "pll_clk"
212 #define REG_ADDR(dsi, reg_idx) ((dsi)->reg_base + \
213 dsi->driver_data->reg_ofs[(reg_idx)])
214 #define DSI_WRITE(dsi, reg_idx, val) writel((val), \
215 REG_ADDR((dsi), (reg_idx)))
216 #define DSI_READ(dsi, reg_idx) readl(REG_ADDR((dsi), (reg_idx)))
218 static char *clk_names[5] = { "bus_clk", "sclk_mipi",
219 "phyclk_mipidphy0_bitclkdiv8", "phyclk_mipidphy0_rxclkesc0",
220 "sclk_rgb_vclk_to_dsim0" };
222 enum exynos_dsi_transfer_type {
227 struct exynos_dsi_transfer {
228 struct list_head list;
229 struct completion completed;
235 const u8 *tx_payload;
244 #define DSIM_STATE_ENABLED BIT(0)
245 #define DSIM_STATE_INITIALIZED BIT(1)
246 #define DSIM_STATE_CMD_LPM BIT(2)
247 #define DSIM_STATE_VIDOUT_AVAILABLE BIT(3)
249 struct exynos_dsi_driver_data {
250 unsigned int *reg_ofs;
251 unsigned int plltmr_reg;
252 unsigned int has_freqband:1;
253 unsigned int has_clklane_stop:1;
254 unsigned int num_clks;
255 unsigned int max_freq;
256 unsigned int wait_for_reset;
257 unsigned int num_bits_resol;
258 unsigned int *reg_values;
262 struct drm_encoder encoder;
263 struct mipi_dsi_host dsi_host;
264 struct drm_connector connector;
265 struct device_node *panel_node;
266 struct drm_panel *panel;
269 void __iomem *reg_base;
272 struct regulator_bulk_data supplies[2];
285 struct drm_property *brightness;
286 struct completion completed;
288 spinlock_t transfer_lock; /* protects transfer_list */
289 struct list_head transfer_list;
291 struct exynos_dsi_driver_data *driver_data;
292 struct device_node *bridge_node;
295 #define host_to_dsi(host) container_of(host, struct exynos_dsi, dsi_host)
296 #define connector_to_dsi(c) container_of(c, struct exynos_dsi, connector)
298 static inline struct exynos_dsi *encoder_to_dsi(struct drm_encoder *e)
300 return container_of(e, struct exynos_dsi, encoder);
304 DSIM_STATUS_REG, /* Status register */
305 DSIM_SWRST_REG, /* Software reset register */
306 DSIM_CLKCTRL_REG, /* Clock control register */
307 DSIM_TIMEOUT_REG, /* Time out register */
308 DSIM_CONFIG_REG, /* Configuration register */
309 DSIM_ESCMODE_REG, /* Escape mode register */
311 DSIM_MVPORCH_REG, /* Main display Vporch register */
312 DSIM_MHPORCH_REG, /* Main display Hporch register */
313 DSIM_MSYNC_REG, /* Main display sync area register */
314 DSIM_INTSRC_REG, /* Interrupt source register */
315 DSIM_INTMSK_REG, /* Interrupt mask register */
316 DSIM_PKTHDR_REG, /* Packet Header FIFO register */
317 DSIM_PAYLOAD_REG, /* Payload FIFO register */
318 DSIM_RXFIFO_REG, /* Read FIFO register */
319 DSIM_FIFOCTRL_REG, /* FIFO status and control register */
320 DSIM_PLLCTRL_REG, /* PLL control register */
327 static unsigned int exynos_reg_ofs[] = {
328 [DSIM_STATUS_REG] = 0x00,
329 [DSIM_SWRST_REG] = 0x04,
330 [DSIM_CLKCTRL_REG] = 0x08,
331 [DSIM_TIMEOUT_REG] = 0x0c,
332 [DSIM_CONFIG_REG] = 0x10,
333 [DSIM_ESCMODE_REG] = 0x14,
334 [DSIM_MDRESOL_REG] = 0x18,
335 [DSIM_MVPORCH_REG] = 0x1c,
336 [DSIM_MHPORCH_REG] = 0x20,
337 [DSIM_MSYNC_REG] = 0x24,
338 [DSIM_INTSRC_REG] = 0x2c,
339 [DSIM_INTMSK_REG] = 0x30,
340 [DSIM_PKTHDR_REG] = 0x34,
341 [DSIM_PAYLOAD_REG] = 0x38,
342 [DSIM_RXFIFO_REG] = 0x3c,
343 [DSIM_FIFOCTRL_REG] = 0x44,
344 [DSIM_PLLCTRL_REG] = 0x4c,
345 [DSIM_PHYCTRL_REG] = 0x5c,
346 [DSIM_PHYTIMING_REG] = 0x64,
347 [DSIM_PHYTIMING1_REG] = 0x68,
348 [DSIM_PHYTIMING2_REG] = 0x6c,
351 static unsigned int exynos5433_reg_ofs[] = {
352 [DSIM_STATUS_REG] = 0x04,
353 [DSIM_SWRST_REG] = 0x0C,
354 [DSIM_CLKCTRL_REG] = 0x10,
355 [DSIM_TIMEOUT_REG] = 0x14,
356 [DSIM_CONFIG_REG] = 0x18,
357 [DSIM_ESCMODE_REG] = 0x1C,
358 [DSIM_MDRESOL_REG] = 0x20,
359 [DSIM_MVPORCH_REG] = 0x24,
360 [DSIM_MHPORCH_REG] = 0x28,
361 [DSIM_MSYNC_REG] = 0x2C,
362 [DSIM_INTSRC_REG] = 0x34,
363 [DSIM_INTMSK_REG] = 0x38,
364 [DSIM_PKTHDR_REG] = 0x3C,
365 [DSIM_PAYLOAD_REG] = 0x40,
366 [DSIM_RXFIFO_REG] = 0x44,
367 [DSIM_FIFOCTRL_REG] = 0x4C,
368 [DSIM_PLLCTRL_REG] = 0x94,
369 [DSIM_PHYCTRL_REG] = 0xA4,
370 [DSIM_PHYTIMING_REG] = 0xB4,
371 [DSIM_PHYTIMING1_REG] = 0xB8,
372 [DSIM_PHYTIMING2_REG] = 0xBC,
384 PHYTIMING_CLK_PREPARE,
388 PHYTIMING_HS_PREPARE,
393 static unsigned int reg_values[] = {
394 [RESET_TYPE] = DSIM_SWRST,
396 [STOP_STATE_CNT] = 0xf,
397 [PHYCTRL_ULPS_EXIT] = DSIM_PHYCTRL_ULPS_EXIT(0x0af),
398 [PHYCTRL_VREG_LP] = 0,
399 [PHYCTRL_SLEW_UP] = 0,
400 [PHYTIMING_LPX] = DSIM_PHYTIMING_LPX(0x06),
401 [PHYTIMING_HS_EXIT] = DSIM_PHYTIMING_HS_EXIT(0x0b),
402 [PHYTIMING_CLK_PREPARE] = DSIM_PHYTIMING1_CLK_PREPARE(0x07),
403 [PHYTIMING_CLK_ZERO] = DSIM_PHYTIMING1_CLK_ZERO(0x27),
404 [PHYTIMING_CLK_POST] = DSIM_PHYTIMING1_CLK_POST(0x0d),
405 [PHYTIMING_CLK_TRAIL] = DSIM_PHYTIMING1_CLK_TRAIL(0x08),
406 [PHYTIMING_HS_PREPARE] = DSIM_PHYTIMING2_HS_PREPARE(0x09),
407 [PHYTIMING_HS_ZERO] = DSIM_PHYTIMING2_HS_ZERO(0x0d),
408 [PHYTIMING_HS_TRAIL] = DSIM_PHYTIMING2_HS_TRAIL(0x0b),
411 static unsigned int exynos5433_reg_values[] = {
412 [RESET_TYPE] = DSIM_FUNCRST,
414 [STOP_STATE_CNT] = 0xa,
415 [PHYCTRL_ULPS_EXIT] = DSIM_PHYCTRL_ULPS_EXIT(0x190),
416 [PHYCTRL_VREG_LP] = DSIM_PHYCTRL_B_DPHYCTL_VREG_LP,
417 [PHYCTRL_SLEW_UP] = DSIM_PHYCTRL_B_DPHYCTL_SLEW_UP,
418 [PHYTIMING_LPX] = DSIM_PHYTIMING_LPX(0x07),
419 [PHYTIMING_HS_EXIT] = DSIM_PHYTIMING_HS_EXIT(0x0c),
420 [PHYTIMING_CLK_PREPARE] = DSIM_PHYTIMING1_CLK_PREPARE(0x09),
421 [PHYTIMING_CLK_ZERO] = DSIM_PHYTIMING1_CLK_ZERO(0x2d),
422 [PHYTIMING_CLK_POST] = DSIM_PHYTIMING1_CLK_POST(0x0e),
423 [PHYTIMING_CLK_TRAIL] = DSIM_PHYTIMING1_CLK_TRAIL(0x09),
424 [PHYTIMING_HS_PREPARE] = DSIM_PHYTIMING2_HS_PREPARE(0x0b),
425 [PHYTIMING_HS_ZERO] = DSIM_PHYTIMING2_HS_ZERO(0x10),
426 [PHYTIMING_HS_TRAIL] = DSIM_PHYTIMING2_HS_TRAIL(0x0c),
429 static struct exynos_dsi_driver_data exynos3_dsi_driver_data = {
430 .reg_ofs = exynos_reg_ofs,
433 .has_clklane_stop = 1,
437 .num_bits_resol = 11,
438 .reg_values = reg_values,
441 static struct exynos_dsi_driver_data exynos4_dsi_driver_data = {
442 .reg_ofs = exynos_reg_ofs,
445 .has_clklane_stop = 1,
449 .num_bits_resol = 11,
450 .reg_values = reg_values,
453 static struct exynos_dsi_driver_data exynos4415_dsi_driver_data = {
454 .reg_ofs = exynos_reg_ofs,
456 .has_clklane_stop = 1,
460 .num_bits_resol = 11,
461 .reg_values = reg_values,
464 static struct exynos_dsi_driver_data exynos5_dsi_driver_data = {
465 .reg_ofs = exynos_reg_ofs,
470 .num_bits_resol = 11,
471 .reg_values = reg_values,
474 static struct exynos_dsi_driver_data exynos5433_dsi_driver_data = {
475 .reg_ofs = exynos5433_reg_ofs,
477 .has_clklane_stop = 1,
481 .num_bits_resol = 12,
482 .reg_values = exynos5433_reg_values,
485 static struct of_device_id exynos_dsi_of_match[] = {
486 { .compatible = "samsung,exynos3250-mipi-dsi",
487 .data = &exynos3_dsi_driver_data },
488 { .compatible = "samsung,exynos4210-mipi-dsi",
489 .data = &exynos4_dsi_driver_data },
490 { .compatible = "samsung,exynos4415-mipi-dsi",
491 .data = &exynos4415_dsi_driver_data },
492 { .compatible = "samsung,exynos5410-mipi-dsi",
493 .data = &exynos5_dsi_driver_data },
494 { .compatible = "samsung,exynos5433-mipi-dsi",
495 .data = &exynos5433_dsi_driver_data },
499 static inline struct exynos_dsi_driver_data *exynos_dsi_get_driver_data(
500 struct platform_device *pdev)
502 const struct of_device_id *of_id =
503 of_match_device(exynos_dsi_of_match, &pdev->dev);
505 return (struct exynos_dsi_driver_data *)of_id->data;
508 static void exynos_dsi_wait_for_reset(struct exynos_dsi *dsi)
510 if (wait_for_completion_timeout(&dsi->completed, msecs_to_jiffies(300)))
513 dev_err(dsi->dev, "timeout waiting for reset\n");
516 static void exynos_dsi_reset(struct exynos_dsi *dsi)
518 struct exynos_dsi_driver_data *driver_data = dsi->driver_data;
520 reinit_completion(&dsi->completed);
521 DSI_WRITE(dsi, DSIM_SWRST_REG, driver_data->reg_values[RESET_TYPE]);
525 #define MHZ (1000*1000)
528 static unsigned long exynos_dsi_pll_find_pms(struct exynos_dsi *dsi,
529 unsigned long fin, unsigned long fout, u8 *p, u16 *m, u8 *s)
531 struct exynos_dsi_driver_data *driver_data = dsi->driver_data;
532 unsigned long best_freq = 0;
533 u32 min_delta = 0xffffffff;
535 u8 _p, uninitialized_var(best_p);
536 u16 _m, uninitialized_var(best_m);
537 u8 _s, uninitialized_var(best_s);
539 p_min = DIV_ROUND_UP(fin, (12 * MHZ));
540 p_max = fin / (6 * MHZ);
542 for (_p = p_min; _p <= p_max; ++_p) {
543 for (_s = 0; _s <= 5; ++_s) {
547 tmp = (u64)fout * (_p << _s);
550 if (_m < 41 || _m > 125)
555 if (tmp < 500 * MHZ ||
556 tmp > driver_data->max_freq * MHZ)
560 do_div(tmp, _p << _s);
562 delta = abs(fout - tmp);
563 if (delta < min_delta) {
582 static unsigned long exynos_dsi_set_pll(struct exynos_dsi *dsi,
585 struct exynos_dsi_driver_data *driver_data = dsi->driver_data;
586 unsigned long fin, fout;
592 fin = dsi->pll_clk_rate;
593 fout = exynos_dsi_pll_find_pms(dsi, fin, freq, &p, &m, &s);
596 "failed to find PLL PMS for requested frequency\n");
599 dev_dbg(dsi->dev, "PLL freq %lu, (p %d, m %d, s %d)\n", fout, p, m, s);
601 writel(driver_data->reg_values[PLL_TIMER],
602 dsi->reg_base + driver_data->plltmr_reg);
604 reg = DSIM_PLL_EN | DSIM_PLL_P(p) | DSIM_PLL_M(m) | DSIM_PLL_S(s);
606 if (driver_data->has_freqband) {
607 static const unsigned long freq_bands[] = {
608 100 * MHZ, 120 * MHZ, 160 * MHZ, 200 * MHZ,
609 270 * MHZ, 320 * MHZ, 390 * MHZ, 450 * MHZ,
610 510 * MHZ, 560 * MHZ, 640 * MHZ, 690 * MHZ,
611 770 * MHZ, 870 * MHZ, 950 * MHZ,
615 for (band = 0; band < ARRAY_SIZE(freq_bands); ++band)
616 if (fout < freq_bands[band])
619 dev_dbg(dsi->dev, "band %d\n", band);
621 reg |= DSIM_FREQ_BAND(band);
624 DSI_WRITE(dsi, DSIM_PLLCTRL_REG, reg);
628 if (timeout-- == 0) {
629 dev_err(dsi->dev, "PLL failed to stabilize\n");
632 reg = DSI_READ(dsi, DSIM_STATUS_REG);
633 } while ((reg & DSIM_PLL_STABLE) == 0);
638 static int exynos_dsi_enable_clock(struct exynos_dsi *dsi)
640 unsigned long hs_clk, byte_clk, esc_clk;
641 unsigned long esc_div;
644 hs_clk = exynos_dsi_set_pll(dsi, dsi->burst_clk_rate);
646 dev_err(dsi->dev, "failed to configure DSI PLL\n");
650 byte_clk = hs_clk / 8;
651 esc_div = DIV_ROUND_UP(byte_clk, dsi->esc_clk_rate);
652 esc_clk = byte_clk / esc_div;
654 if (esc_clk > 20 * MHZ) {
656 esc_clk = byte_clk / esc_div;
659 dev_dbg(dsi->dev, "hs_clk = %lu, byte_clk = %lu, esc_clk = %lu\n",
660 hs_clk, byte_clk, esc_clk);
662 reg = DSI_READ(dsi, DSIM_CLKCTRL_REG);
663 reg &= ~(DSIM_ESC_PRESCALER_MASK | DSIM_LANE_ESC_CLK_EN_CLK
664 | DSIM_LANE_ESC_CLK_EN_DATA_MASK | DSIM_PLL_BYPASS
665 | DSIM_BYTE_CLK_SRC_MASK);
666 reg |= DSIM_ESC_CLKEN | DSIM_BYTE_CLKEN
667 | DSIM_ESC_PRESCALER(esc_div)
668 | DSIM_LANE_ESC_CLK_EN_CLK
669 | DSIM_LANE_ESC_CLK_EN_DATA(BIT(dsi->lanes) - 1)
670 | DSIM_BYTE_CLK_SRC(0)
671 | DSIM_TX_REQUEST_HSCLK;
672 DSI_WRITE(dsi, DSIM_CLKCTRL_REG, reg);
677 static void exynos_dsi_set_phy_ctrl(struct exynos_dsi *dsi)
679 struct exynos_dsi_driver_data *driver_data = dsi->driver_data;
680 unsigned int *reg_values = driver_data->reg_values;
683 if (driver_data->has_freqband)
686 /* B D-PHY: D-PHY Master & Slave Analog Block control */
687 reg = reg_values[PHYCTRL_ULPS_EXIT] | reg_values[PHYCTRL_VREG_LP] |
688 reg_values[PHYCTRL_SLEW_UP];
689 DSI_WRITE(dsi, DSIM_PHYCTRL_REG, reg);
692 * T LPX: Transmitted length of any Low-Power state period
693 * T HS-EXIT: Time that the transmitter drives LP-11 following a HS
696 reg = reg_values[PHYTIMING_LPX] | reg_values[PHYTIMING_HS_EXIT];
697 DSI_WRITE(dsi, DSIM_PHYTIMING_REG, reg);
700 * T CLK-PREPARE: Time that the transmitter drives the Clock Lane LP-00
701 * Line state immediately before the HS-0 Line state starting the
703 * T CLK-ZERO: Time that the transmitter drives the HS-0 state prior to
704 * transmitting the Clock.
705 * T CLK_POST: Time that the transmitter continues to send HS clock
706 * after the last associated Data Lane has transitioned to LP Mode
707 * Interval is defined as the period from the end of T HS-TRAIL to
708 * the beginning of T CLK-TRAIL
709 * T CLK-TRAIL: Time that the transmitter drives the HS-0 state after
710 * the last payload clock bit of a HS transmission burst
712 reg = reg_values[PHYTIMING_CLK_PREPARE] |
713 reg_values[PHYTIMING_CLK_ZERO] |
714 reg_values[PHYTIMING_CLK_POST] |
715 reg_values[PHYTIMING_CLK_TRAIL];
717 DSI_WRITE(dsi, DSIM_PHYTIMING1_REG, reg);
720 * T HS-PREPARE: Time that the transmitter drives the Data Lane LP-00
721 * Line state immediately before the HS-0 Line state starting the
723 * T HS-ZERO: Time that the transmitter drives the HS-0 state prior to
724 * transmitting the Sync sequence.
725 * T HS-TRAIL: Time that the transmitter drives the flipped differential
726 * state after last payload data bit of a HS transmission burst
728 reg = reg_values[PHYTIMING_HS_PREPARE] | reg_values[PHYTIMING_HS_ZERO] |
729 reg_values[PHYTIMING_HS_TRAIL];
730 DSI_WRITE(dsi, DSIM_PHYTIMING2_REG, reg);
733 static void exynos_dsi_disable_clock(struct exynos_dsi *dsi)
737 reg = DSI_READ(dsi, DSIM_CLKCTRL_REG);
738 reg &= ~(DSIM_LANE_ESC_CLK_EN_CLK | DSIM_LANE_ESC_CLK_EN_DATA_MASK
739 | DSIM_ESC_CLKEN | DSIM_BYTE_CLKEN);
740 DSI_WRITE(dsi, DSIM_CLKCTRL_REG, reg);
742 reg = DSI_READ(dsi, DSIM_PLLCTRL_REG);
744 DSI_WRITE(dsi, DSIM_PLLCTRL_REG, reg);
747 static void exynos_dsi_enable_lane(struct exynos_dsi *dsi, u32 lane)
749 u32 reg = DSI_READ(dsi, DSIM_CONFIG_REG);
750 reg |= (DSIM_NUM_OF_DATA_LANE(dsi->lanes - 1) | DSIM_LANE_EN_CLK |
752 DSI_WRITE(dsi, DSIM_CONFIG_REG, reg);
755 static int exynos_dsi_init_link(struct exynos_dsi *dsi)
757 struct exynos_dsi_driver_data *driver_data = dsi->driver_data;
762 /* Initialize FIFO pointers */
763 reg = DSI_READ(dsi, DSIM_FIFOCTRL_REG);
765 DSI_WRITE(dsi, DSIM_FIFOCTRL_REG, reg);
767 usleep_range(9000, 11000);
770 DSI_WRITE(dsi, DSIM_FIFOCTRL_REG, reg);
771 usleep_range(9000, 11000);
773 /* DSI configuration */
777 * The first bit of mode_flags specifies display configuration.
778 * If this bit is set[= MIPI_DSI_MODE_VIDEO], dsi will support video
779 * mode, otherwise it will support command mode.
781 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO) {
782 reg |= DSIM_VIDEO_MODE;
785 * The user manual describes that following bits are ignored in
788 if (!(dsi->mode_flags & MIPI_DSI_MODE_VSYNC_FLUSH))
789 reg |= DSIM_MFLUSH_VS;
790 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
791 reg |= DSIM_SYNC_INFORM;
792 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
793 reg |= DSIM_BURST_MODE;
794 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_AUTO_VERT)
795 reg |= DSIM_AUTO_MODE;
796 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_HSE)
797 reg |= DSIM_HSE_MODE;
798 if (!(dsi->mode_flags & MIPI_DSI_MODE_VIDEO_HFP))
799 reg |= DSIM_HFP_MODE;
800 if (!(dsi->mode_flags & MIPI_DSI_MODE_VIDEO_HBP))
801 reg |= DSIM_HBP_MODE;
802 if (!(dsi->mode_flags & MIPI_DSI_MODE_VIDEO_HSA))
803 reg |= DSIM_HSA_MODE;
806 if (!(dsi->mode_flags & MIPI_DSI_MODE_EOT_PACKET))
807 reg |= DSIM_EOT_DISABLE;
809 switch (dsi->format) {
810 case MIPI_DSI_FMT_RGB888:
811 reg |= DSIM_MAIN_PIX_FORMAT_RGB888;
813 case MIPI_DSI_FMT_RGB666:
814 reg |= DSIM_MAIN_PIX_FORMAT_RGB666;
816 case MIPI_DSI_FMT_RGB666_PACKED:
817 reg |= DSIM_MAIN_PIX_FORMAT_RGB666_P;
819 case MIPI_DSI_FMT_RGB565:
820 reg |= DSIM_MAIN_PIX_FORMAT_RGB565;
823 dev_err(dsi->dev, "invalid pixel format\n");
828 * Use non-continuous clock mode if the periparal wants and
829 * host controller supports
831 * In non-continous clock mode, host controller will turn off
832 * the HS clock between high-speed transmissions to reduce
835 if (driver_data->has_clklane_stop &&
836 dsi->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS) {
837 reg |= DSIM_CLKLANE_STOP;
839 DSI_WRITE(dsi, DSIM_CONFIG_REG, reg);
841 lanes_mask = BIT(dsi->lanes) - 1;
842 exynos_dsi_enable_lane(dsi, lanes_mask);
844 /* Check clock and data lane state are stop state */
847 if (timeout-- == 0) {
848 dev_err(dsi->dev, "waiting for bus lanes timed out\n");
852 reg = DSI_READ(dsi, DSIM_STATUS_REG);
853 if ((reg & DSIM_STOP_STATE_DAT(lanes_mask))
854 != DSIM_STOP_STATE_DAT(lanes_mask))
856 } while (!(reg & (DSIM_STOP_STATE_CLK | DSIM_TX_READY_HS_CLK)));
858 reg = DSI_READ(dsi, DSIM_ESCMODE_REG);
859 reg &= ~DSIM_STOP_STATE_CNT_MASK;
860 reg |= DSIM_STOP_STATE_CNT(driver_data->reg_values[STOP_STATE_CNT]);
861 DSI_WRITE(dsi, DSIM_ESCMODE_REG, reg);
863 reg = DSIM_BTA_TIMEOUT(0xff) | DSIM_LPDR_TIMEOUT(0xffff);
864 DSI_WRITE(dsi, DSIM_TIMEOUT_REG, reg);
869 static void exynos_dsi_set_display_mode(struct exynos_dsi *dsi)
871 struct videomode *vm = &dsi->vm;
872 unsigned int num_bits_resol = dsi->driver_data->num_bits_resol;
875 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO) {
876 reg = DSIM_CMD_ALLOW(0xf)
877 | DSIM_STABLE_VFP(vm->vfront_porch)
878 | DSIM_MAIN_VBP(vm->vback_porch);
879 DSI_WRITE(dsi, DSIM_MVPORCH_REG, reg);
881 reg = DSIM_MAIN_HFP(vm->hfront_porch)
882 | DSIM_MAIN_HBP(vm->hback_porch);
883 DSI_WRITE(dsi, DSIM_MHPORCH_REG, reg);
885 reg = DSIM_MAIN_VSA(vm->vsync_len)
886 | DSIM_MAIN_HSA(vm->hsync_len);
887 DSI_WRITE(dsi, DSIM_MSYNC_REG, reg);
889 reg = DSIM_MAIN_HRESOL(vm->hactive, num_bits_resol) |
890 DSIM_MAIN_VRESOL(vm->vactive, num_bits_resol);
892 DSI_WRITE(dsi, DSIM_MDRESOL_REG, reg);
894 dev_dbg(dsi->dev, "LCD size = %dx%d\n", vm->hactive, vm->vactive);
897 static void exynos_dsi_set_display_enable(struct exynos_dsi *dsi, bool enable)
901 reg = DSI_READ(dsi, DSIM_MDRESOL_REG);
903 reg |= DSIM_MAIN_STAND_BY;
905 reg &= ~DSIM_MAIN_STAND_BY;
906 DSI_WRITE(dsi, DSIM_MDRESOL_REG, reg);
909 static int exynos_dsi_wait_for_hdr_fifo(struct exynos_dsi *dsi)
914 u32 reg = DSI_READ(dsi, DSIM_FIFOCTRL_REG);
916 if (!(reg & DSIM_SFR_HEADER_FULL))
920 usleep_range(950, 1050);
926 static void exynos_dsi_set_cmd_lpm(struct exynos_dsi *dsi, bool lpm)
928 u32 v = DSI_READ(dsi, DSIM_ESCMODE_REG);
931 v |= DSIM_CMD_LPDT_LP;
933 v &= ~DSIM_CMD_LPDT_LP;
935 DSI_WRITE(dsi, DSIM_ESCMODE_REG, v);
938 static void exynos_dsi_force_bta(struct exynos_dsi *dsi)
940 u32 v = DSI_READ(dsi, DSIM_ESCMODE_REG);
942 DSI_WRITE(dsi, DSIM_ESCMODE_REG, v);
945 static void exynos_dsi_send_to_fifo(struct exynos_dsi *dsi,
946 struct exynos_dsi_transfer *xfer)
948 struct device *dev = dsi->dev;
949 const u8 *payload = xfer->tx_payload + xfer->tx_done;
950 u16 length = xfer->tx_len - xfer->tx_done;
951 bool first = !xfer->tx_done;
954 dev_dbg(dev, "< xfer %p: tx len %u, done %u, rx len %u, done %u\n",
955 xfer, xfer->tx_len, xfer->tx_done, xfer->rx_len, xfer->rx_done);
957 if (length > DSI_TX_FIFO_SIZE)
958 length = DSI_TX_FIFO_SIZE;
960 xfer->tx_done += length;
963 while (length >= 4) {
964 reg = (payload[3] << 24) | (payload[2] << 16)
965 | (payload[1] << 8) | payload[0];
966 DSI_WRITE(dsi, DSIM_PAYLOAD_REG, reg);
974 reg |= payload[2] << 16;
977 reg |= payload[1] << 8;
981 DSI_WRITE(dsi, DSIM_PAYLOAD_REG, reg);
988 /* Send packet header */
992 reg = (xfer->data[1] << 16) | (xfer->data[0] << 8) | xfer->data_id;
993 if (exynos_dsi_wait_for_hdr_fifo(dsi)) {
994 dev_err(dev, "waiting for header FIFO timed out\n");
998 if (NEQV(xfer->flags & MIPI_DSI_MSG_USE_LPM,
999 dsi->state & DSIM_STATE_CMD_LPM)) {
1000 exynos_dsi_set_cmd_lpm(dsi, xfer->flags & MIPI_DSI_MSG_USE_LPM);
1001 dsi->state ^= DSIM_STATE_CMD_LPM;
1004 DSI_WRITE(dsi, DSIM_PKTHDR_REG, reg);
1006 if (xfer->flags & MIPI_DSI_MSG_REQ_ACK)
1007 exynos_dsi_force_bta(dsi);
1010 static void exynos_dsi_read_from_fifo(struct exynos_dsi *dsi,
1011 struct exynos_dsi_transfer *xfer)
1013 u8 *payload = xfer->rx_payload + xfer->rx_done;
1014 bool first = !xfer->rx_done;
1015 struct device *dev = dsi->dev;
1020 reg = DSI_READ(dsi, DSIM_RXFIFO_REG);
1022 switch (reg & 0x3f) {
1023 case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
1024 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
1025 if (xfer->rx_len >= 2) {
1026 payload[1] = reg >> 16;
1030 case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
1031 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
1032 payload[0] = reg >> 8;
1034 xfer->rx_len = xfer->rx_done;
1037 case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
1038 dev_err(dev, "DSI Error Report: 0x%04x\n",
1039 (reg >> 8) & 0xffff);
1044 length = (reg >> 8) & 0xffff;
1045 if (length > xfer->rx_len) {
1047 "response too long (%u > %u bytes), stripping\n",
1048 xfer->rx_len, length);
1049 length = xfer->rx_len;
1050 } else if (length < xfer->rx_len)
1051 xfer->rx_len = length;
1054 length = xfer->rx_len - xfer->rx_done;
1055 xfer->rx_done += length;
1057 /* Receive payload */
1058 while (length >= 4) {
1059 reg = DSI_READ(dsi, DSIM_RXFIFO_REG);
1060 payload[0] = (reg >> 0) & 0xff;
1061 payload[1] = (reg >> 8) & 0xff;
1062 payload[2] = (reg >> 16) & 0xff;
1063 payload[3] = (reg >> 24) & 0xff;
1069 reg = DSI_READ(dsi, DSIM_RXFIFO_REG);
1072 payload[2] = (reg >> 16) & 0xff;
1075 payload[1] = (reg >> 8) & 0xff;
1078 payload[0] = reg & 0xff;
1082 if (xfer->rx_done == xfer->rx_len)
1086 length = DSI_RX_FIFO_SIZE / 4;
1088 reg = DSI_READ(dsi, DSIM_RXFIFO_REG);
1089 if (reg == DSI_RX_FIFO_EMPTY)
1094 static void exynos_dsi_transfer_start(struct exynos_dsi *dsi)
1096 unsigned long flags;
1097 struct exynos_dsi_transfer *xfer;
1101 spin_lock_irqsave(&dsi->transfer_lock, flags);
1103 if (list_empty(&dsi->transfer_list)) {
1104 spin_unlock_irqrestore(&dsi->transfer_lock, flags);
1108 xfer = list_first_entry(&dsi->transfer_list,
1109 struct exynos_dsi_transfer, list);
1111 spin_unlock_irqrestore(&dsi->transfer_lock, flags);
1113 if (xfer->tx_len && xfer->tx_done == xfer->tx_len)
1114 /* waiting for RX */
1117 exynos_dsi_send_to_fifo(dsi, xfer);
1119 if (xfer->tx_len || xfer->rx_len)
1123 complete(&xfer->completed);
1125 spin_lock_irqsave(&dsi->transfer_lock, flags);
1127 list_del_init(&xfer->list);
1128 start = !list_empty(&dsi->transfer_list);
1130 spin_unlock_irqrestore(&dsi->transfer_lock, flags);
1136 static bool exynos_dsi_transfer_finish(struct exynos_dsi *dsi)
1138 struct exynos_dsi_transfer *xfer;
1139 unsigned long flags;
1142 spin_lock_irqsave(&dsi->transfer_lock, flags);
1144 if (list_empty(&dsi->transfer_list)) {
1145 spin_unlock_irqrestore(&dsi->transfer_lock, flags);
1149 xfer = list_first_entry(&dsi->transfer_list,
1150 struct exynos_dsi_transfer, list);
1152 spin_unlock_irqrestore(&dsi->transfer_lock, flags);
1155 "> xfer %p, tx_len %u, tx_done %u, rx_len %u, rx_done %u\n",
1156 xfer, xfer->tx_len, xfer->tx_done, xfer->rx_len, xfer->rx_done);
1158 if (xfer->tx_done != xfer->tx_len)
1161 if (xfer->rx_done != xfer->rx_len)
1162 exynos_dsi_read_from_fifo(dsi, xfer);
1164 if (xfer->rx_done != xfer->rx_len)
1167 spin_lock_irqsave(&dsi->transfer_lock, flags);
1169 list_del_init(&xfer->list);
1170 start = !list_empty(&dsi->transfer_list);
1172 spin_unlock_irqrestore(&dsi->transfer_lock, flags);
1176 complete(&xfer->completed);
1181 static void exynos_dsi_remove_transfer(struct exynos_dsi *dsi,
1182 struct exynos_dsi_transfer *xfer)
1184 unsigned long flags;
1187 spin_lock_irqsave(&dsi->transfer_lock, flags);
1189 if (!list_empty(&dsi->transfer_list) &&
1190 xfer == list_first_entry(&dsi->transfer_list,
1191 struct exynos_dsi_transfer, list)) {
1192 list_del_init(&xfer->list);
1193 start = !list_empty(&dsi->transfer_list);
1194 spin_unlock_irqrestore(&dsi->transfer_lock, flags);
1196 exynos_dsi_transfer_start(dsi);
1200 list_del_init(&xfer->list);
1202 spin_unlock_irqrestore(&dsi->transfer_lock, flags);
1205 static int exynos_dsi_transfer(struct exynos_dsi *dsi,
1206 struct exynos_dsi_transfer *xfer)
1208 unsigned long flags;
1213 xfer->result = -ETIMEDOUT;
1214 init_completion(&xfer->completed);
1216 spin_lock_irqsave(&dsi->transfer_lock, flags);
1218 stopped = list_empty(&dsi->transfer_list);
1219 list_add_tail(&xfer->list, &dsi->transfer_list);
1221 spin_unlock_irqrestore(&dsi->transfer_lock, flags);
1224 exynos_dsi_transfer_start(dsi);
1226 wait_for_completion_timeout(&xfer->completed,
1227 msecs_to_jiffies(DSI_XFER_TIMEOUT_MS));
1228 if (xfer->result == -ETIMEDOUT) {
1229 exynos_dsi_remove_transfer(dsi, xfer);
1230 dev_err(dsi->dev, "xfer timed out: %*ph %*ph\n", 2, xfer->data,
1231 xfer->tx_len, xfer->tx_payload);
1235 /* Also covers hardware timeout condition */
1236 return xfer->result;
1239 static irqreturn_t exynos_dsi_irq(int irq, void *dev_id)
1241 struct exynos_dsi *dsi = dev_id;
1244 status = DSI_READ(dsi, DSIM_INTSRC_REG);
1246 static unsigned long int j;
1247 if (printk_timed_ratelimit(&j, 500))
1248 dev_warn(dsi->dev, "spurious interrupt\n");
1251 DSI_WRITE(dsi, DSIM_INTSRC_REG, status);
1253 if (status & DSIM_INT_SW_RST_RELEASE) {
1254 u32 mask = ~(DSIM_INT_RX_DONE | DSIM_INT_SFR_FIFO_EMPTY |
1255 DSIM_INT_SFR_HDR_FIFO_EMPTY | DSIM_INT_FRAME_DONE |
1256 DSIM_INT_RX_ECC_ERR | DSIM_INT_SW_RST_RELEASE);
1257 DSI_WRITE(dsi, DSIM_INTMSK_REG, mask);
1258 complete(&dsi->completed);
1262 if (!(status & (DSIM_INT_RX_DONE | DSIM_INT_SFR_FIFO_EMPTY |
1263 DSIM_INT_FRAME_DONE | DSIM_INT_PLL_STABLE)))
1266 if (exynos_dsi_transfer_finish(dsi))
1267 exynos_dsi_transfer_start(dsi);
1272 static irqreturn_t exynos_dsi_te_irq_handler(int irq, void *dev_id)
1274 struct exynos_dsi *dsi = (struct exynos_dsi *)dev_id;
1275 struct drm_encoder *encoder = &dsi->encoder;
1277 if (dsi->state & DSIM_STATE_VIDOUT_AVAILABLE)
1278 exynos_drm_crtc_te_handler(encoder->crtc);
1283 static void exynos_dsi_enable_irq(struct exynos_dsi *dsi)
1285 enable_irq(dsi->irq);
1287 if (gpio_is_valid(dsi->te_gpio))
1288 enable_irq(gpio_to_irq(dsi->te_gpio));
1291 static void exynos_dsi_disable_irq(struct exynos_dsi *dsi)
1293 if (gpio_is_valid(dsi->te_gpio))
1294 disable_irq(gpio_to_irq(dsi->te_gpio));
1296 disable_irq(dsi->irq);
1299 static int exynos_dsi_init(struct exynos_dsi *dsi)
1301 struct exynos_dsi_driver_data *driver_data = dsi->driver_data;
1303 exynos_dsi_reset(dsi);
1304 exynos_dsi_enable_irq(dsi);
1306 if (driver_data->reg_values[RESET_TYPE] == DSIM_FUNCRST)
1307 exynos_dsi_enable_lane(dsi, BIT(dsi->lanes) - 1);
1309 exynos_dsi_enable_clock(dsi);
1310 if (driver_data->wait_for_reset)
1311 exynos_dsi_wait_for_reset(dsi);
1312 exynos_dsi_set_phy_ctrl(dsi);
1313 exynos_dsi_init_link(dsi);
1318 static int exynos_dsi_register_te_irq(struct exynos_dsi *dsi)
1323 dsi->te_gpio = of_get_named_gpio(dsi->panel_node, "te-gpios", 0);
1324 if (!gpio_is_valid(dsi->te_gpio)) {
1325 dev_err(dsi->dev, "no te-gpios specified\n");
1330 ret = gpio_request(dsi->te_gpio, "te_gpio");
1332 dev_err(dsi->dev, "gpio request failed with %d\n", ret);
1336 te_gpio_irq = gpio_to_irq(dsi->te_gpio);
1337 irq_set_status_flags(te_gpio_irq, IRQ_NOAUTOEN);
1339 ret = request_threaded_irq(te_gpio_irq, exynos_dsi_te_irq_handler, NULL,
1340 IRQF_TRIGGER_RISING, "TE", dsi);
1342 dev_err(dsi->dev, "request interrupt failed with %d\n", ret);
1343 gpio_free(dsi->te_gpio);
1351 static void exynos_dsi_unregister_te_irq(struct exynos_dsi *dsi)
1353 if (gpio_is_valid(dsi->te_gpio)) {
1354 free_irq(gpio_to_irq(dsi->te_gpio), dsi);
1355 gpio_free(dsi->te_gpio);
1356 dsi->te_gpio = -ENOENT;
1360 static int exynos_dsi_host_attach(struct mipi_dsi_host *host,
1361 struct mipi_dsi_device *device)
1363 struct exynos_dsi *dsi = host_to_dsi(host);
1365 dsi->lanes = device->lanes;
1366 dsi->format = device->format;
1367 dsi->mode_flags = device->mode_flags;
1368 dsi->panel_node = device->dev.of_node;
1371 * This is a temporary solution and should be made by more generic way.
1373 * If attached panel device is for command mode one, dsi should register
1374 * TE interrupt handler.
1376 if (!(dsi->mode_flags & MIPI_DSI_MODE_VIDEO)) {
1377 int ret = exynos_dsi_register_te_irq(dsi);
1383 if (dsi->connector.dev)
1384 drm_helper_hpd_irq_event(dsi->connector.dev);
1389 static int exynos_dsi_host_detach(struct mipi_dsi_host *host,
1390 struct mipi_dsi_device *device)
1392 struct exynos_dsi *dsi = host_to_dsi(host);
1394 exynos_dsi_unregister_te_irq(dsi);
1396 dsi->panel_node = NULL;
1398 if (dsi->connector.dev)
1399 drm_helper_hpd_irq_event(dsi->connector.dev);
1404 /* distinguish between short and long DSI packet types */
1405 static bool exynos_dsi_is_short_dsi_type(u8 type)
1407 return (type & 0x0f) <= 8;
1410 static ssize_t exynos_dsi_host_transfer(struct mipi_dsi_host *host,
1411 const struct mipi_dsi_msg *msg)
1413 struct exynos_dsi *dsi = host_to_dsi(host);
1414 struct exynos_dsi_transfer xfer;
1417 if (!(dsi->state & DSIM_STATE_ENABLED))
1420 if (!(dsi->state & DSIM_STATE_INITIALIZED)) {
1421 ret = exynos_dsi_init(dsi);
1424 dsi->state |= DSIM_STATE_INITIALIZED;
1427 if (msg->tx_len == 0)
1430 xfer.data_id = msg->type | (msg->channel << 6);
1432 if (exynos_dsi_is_short_dsi_type(msg->type)) {
1433 const char *tx_buf = msg->tx_buf;
1435 if (msg->tx_len > 2)
1438 xfer.data[0] = tx_buf[0];
1439 xfer.data[1] = (msg->tx_len == 2) ? tx_buf[1] : 0;
1441 xfer.tx_len = msg->tx_len;
1442 xfer.data[0] = msg->tx_len & 0xff;
1443 xfer.data[1] = msg->tx_len >> 8;
1444 xfer.tx_payload = msg->tx_buf;
1447 xfer.rx_len = msg->rx_len;
1448 xfer.rx_payload = msg->rx_buf;
1449 xfer.flags = msg->flags;
1451 ret = exynos_dsi_transfer(dsi, &xfer);
1452 return (ret < 0) ? ret : xfer.rx_done;
1455 static const struct mipi_dsi_host_ops exynos_dsi_ops = {
1456 .attach = exynos_dsi_host_attach,
1457 .detach = exynos_dsi_host_detach,
1458 .transfer = exynos_dsi_host_transfer,
1461 static int exynos_dsi_poweron(struct exynos_dsi *dsi)
1463 struct exynos_dsi_driver_data *driver_data = dsi->driver_data;
1466 ret = regulator_bulk_enable(ARRAY_SIZE(dsi->supplies), dsi->supplies);
1468 dev_err(dsi->dev, "cannot enable regulators %d\n", ret);
1472 for (i = 0; i < driver_data->num_clks; i++) {
1473 ret = clk_prepare_enable(dsi->clks[i]);
1478 ret = phy_power_on(dsi->phy);
1480 dev_err(dsi->dev, "cannot enable phy %d\n", ret);
1488 clk_disable_unprepare(dsi->clks[i]);
1489 regulator_bulk_disable(ARRAY_SIZE(dsi->supplies), dsi->supplies);
1494 static void exynos_dsi_poweroff(struct exynos_dsi *dsi)
1496 struct exynos_dsi_driver_data *driver_data = dsi->driver_data;
1499 usleep_range(10000, 20000);
1501 if (dsi->state & DSIM_STATE_INITIALIZED) {
1502 dsi->state &= ~DSIM_STATE_INITIALIZED;
1504 exynos_dsi_disable_clock(dsi);
1506 exynos_dsi_disable_irq(dsi);
1509 dsi->state &= ~DSIM_STATE_CMD_LPM;
1511 phy_power_off(dsi->phy);
1513 for (i = driver_data->num_clks - 1; i > -1; i--)
1514 clk_disable_unprepare(dsi->clks[i]);
1516 ret = regulator_bulk_disable(ARRAY_SIZE(dsi->supplies), dsi->supplies);
1518 dev_err(dsi->dev, "cannot disable regulators %d\n", ret);
1521 static void exynos_dsi_enable(struct drm_encoder *encoder)
1523 struct exynos_dsi *dsi = encoder_to_dsi(encoder);
1526 if (dsi->state & DSIM_STATE_ENABLED)
1529 ret = exynos_dsi_poweron(dsi);
1533 dsi->state |= DSIM_STATE_ENABLED;
1535 ret = drm_panel_prepare(dsi->panel);
1537 dsi->state &= ~DSIM_STATE_ENABLED;
1538 exynos_dsi_poweroff(dsi);
1542 exynos_dsi_set_display_mode(dsi);
1543 exynos_dsi_set_display_enable(dsi, true);
1545 ret = drm_panel_enable(dsi->panel);
1547 dsi->state &= ~DSIM_STATE_ENABLED;
1548 exynos_dsi_set_display_enable(dsi, false);
1549 drm_panel_unprepare(dsi->panel);
1550 exynos_dsi_poweroff(dsi);
1554 dsi->state |= DSIM_STATE_VIDOUT_AVAILABLE;
1557 static void exynos_dsi_disable(struct drm_encoder *encoder)
1559 struct exynos_dsi *dsi = encoder_to_dsi(encoder);
1561 if (!(dsi->state & DSIM_STATE_ENABLED))
1564 dsi->state &= ~DSIM_STATE_VIDOUT_AVAILABLE;
1566 drm_panel_disable(dsi->panel);
1567 exynos_dsi_set_display_enable(dsi, false);
1568 drm_panel_unprepare(dsi->panel);
1570 dsi->state &= ~DSIM_STATE_ENABLED;
1572 exynos_dsi_poweroff(dsi);
1575 static enum drm_connector_status
1576 exynos_dsi_detect(struct drm_connector *connector, bool force)
1578 struct exynos_dsi *dsi = connector_to_dsi(connector);
1581 dsi->panel = of_drm_find_panel(dsi->panel_node);
1583 drm_panel_attach(dsi->panel, &dsi->connector);
1584 } else if (!dsi->panel_node) {
1585 struct drm_encoder *encoder;
1587 encoder = platform_get_drvdata(to_platform_device(dsi->dev));
1588 exynos_dsi_disable(encoder);
1589 drm_panel_detach(dsi->panel);
1594 return connector_status_connected;
1596 return connector_status_disconnected;
1599 static void exynos_dsi_connector_destroy(struct drm_connector *connector)
1601 drm_connector_unregister(connector);
1602 drm_connector_cleanup(connector);
1603 connector->dev = NULL;
1606 static struct drm_connector_funcs exynos_dsi_connector_funcs = {
1607 .dpms = drm_atomic_helper_connector_dpms,
1608 .detect = exynos_dsi_detect,
1609 .fill_modes = drm_helper_probe_single_connector_modes,
1610 .destroy = exynos_dsi_connector_destroy,
1611 .reset = drm_atomic_helper_connector_reset,
1612 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1613 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1616 static int exynos_dsi_get_modes(struct drm_connector *connector)
1618 struct exynos_dsi *dsi = connector_to_dsi(connector);
1621 return dsi->panel->funcs->get_modes(dsi->panel);
1626 static struct drm_encoder *
1627 exynos_dsi_best_encoder(struct drm_connector *connector)
1629 struct exynos_dsi *dsi = connector_to_dsi(connector);
1631 return &dsi->encoder;
1634 static struct drm_connector_helper_funcs exynos_dsi_connector_helper_funcs = {
1635 .get_modes = exynos_dsi_get_modes,
1636 .best_encoder = exynos_dsi_best_encoder,
1639 static int exynos_dsi_create_connector(struct drm_encoder *encoder)
1641 struct exynos_dsi *dsi = encoder_to_dsi(encoder);
1642 struct drm_connector *connector = &dsi->connector;
1645 connector->polled = DRM_CONNECTOR_POLL_HPD;
1647 ret = drm_connector_init(encoder->dev, connector,
1648 &exynos_dsi_connector_funcs,
1649 DRM_MODE_CONNECTOR_DSI);
1651 DRM_ERROR("Failed to initialize connector with drm\n");
1655 drm_connector_helper_add(connector, &exynos_dsi_connector_helper_funcs);
1656 drm_connector_register(connector);
1657 drm_mode_connector_attach_encoder(connector, encoder);
1662 static bool exynos_dsi_mode_fixup(struct drm_encoder *encoder,
1663 const struct drm_display_mode *mode,
1664 struct drm_display_mode *adjusted_mode)
1669 static void exynos_dsi_mode_set(struct drm_encoder *encoder,
1670 struct drm_display_mode *mode,
1671 struct drm_display_mode *adjusted_mode)
1673 struct exynos_dsi *dsi = encoder_to_dsi(encoder);
1674 struct videomode *vm = &dsi->vm;
1675 struct drm_display_mode *m = adjusted_mode;
1677 vm->hactive = m->hdisplay;
1678 vm->vactive = m->vdisplay;
1679 vm->vfront_porch = m->vsync_start - m->vdisplay;
1680 vm->vback_porch = m->vtotal - m->vsync_end;
1681 vm->vsync_len = m->vsync_end - m->vsync_start;
1682 vm->hfront_porch = m->hsync_start - m->hdisplay;
1683 vm->hback_porch = m->htotal - m->hsync_end;
1684 vm->hsync_len = m->hsync_end - m->hsync_start;
1687 static struct drm_encoder_helper_funcs exynos_dsi_encoder_helper_funcs = {
1688 .mode_fixup = exynos_dsi_mode_fixup,
1689 .mode_set = exynos_dsi_mode_set,
1690 .enable = exynos_dsi_enable,
1691 .disable = exynos_dsi_disable,
1694 static struct drm_encoder_funcs exynos_dsi_encoder_funcs = {
1695 .destroy = drm_encoder_cleanup,
1698 MODULE_DEVICE_TABLE(of, exynos_dsi_of_match);
1700 /* of_* functions will be removed after merge of of_graph patches */
1701 static struct device_node *
1702 of_get_child_by_name_reg(struct device_node *parent, const char *name, u32 reg)
1704 struct device_node *np;
1706 for_each_child_of_node(parent, np) {
1709 if (!np->name || of_node_cmp(np->name, name))
1712 if (of_property_read_u32(np, "reg", &r) < 0)
1722 static struct device_node *of_graph_get_port_by_reg(struct device_node *parent,
1725 struct device_node *ports, *port;
1727 ports = of_get_child_by_name(parent, "ports");
1731 port = of_get_child_by_name_reg(parent, "port", reg);
1738 static struct device_node *
1739 of_graph_get_endpoint_by_reg(struct device_node *port, u32 reg)
1741 return of_get_child_by_name_reg(port, "endpoint", reg);
1744 static int exynos_dsi_of_read_u32(const struct device_node *np,
1745 const char *propname, u32 *out_value)
1747 int ret = of_property_read_u32(np, propname, out_value);
1750 pr_err("%s: failed to get '%s' property\n", np->full_name,
1761 static int exynos_dsi_parse_dt(struct exynos_dsi *dsi)
1763 struct device *dev = dsi->dev;
1764 struct device_node *node = dev->of_node;
1765 struct device_node *port, *ep;
1768 ret = exynos_dsi_of_read_u32(node, "samsung,pll-clock-frequency",
1769 &dsi->pll_clk_rate);
1773 port = of_graph_get_port_by_reg(node, DSI_PORT_OUT);
1775 dev_err(dev, "no output port specified\n");
1779 ep = of_graph_get_endpoint_by_reg(port, 0);
1782 dev_err(dev, "no endpoint specified in output port\n");
1786 ret = exynos_dsi_of_read_u32(ep, "samsung,burst-clock-frequency",
1787 &dsi->burst_clk_rate);
1791 ret = exynos_dsi_of_read_u32(ep, "samsung,esc-clock-frequency",
1792 &dsi->esc_clk_rate);
1798 ep = of_graph_get_next_endpoint(node, NULL);
1804 dsi->bridge_node = of_graph_get_remote_port_parent(ep);
1805 if (!dsi->bridge_node) {
1815 static int exynos_dsi_bind(struct device *dev, struct device *master,
1818 struct drm_encoder *encoder = dev_get_drvdata(dev);
1819 struct exynos_dsi *dsi = encoder_to_dsi(encoder);
1820 struct drm_device *drm_dev = data;
1821 struct drm_bridge *bridge;
1824 ret = exynos_drm_crtc_get_pipe_from_type(drm_dev,
1825 EXYNOS_DISPLAY_TYPE_LCD);
1829 encoder->possible_crtcs = 1 << ret;
1831 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
1833 drm_encoder_init(drm_dev, encoder, &exynos_dsi_encoder_funcs,
1834 DRM_MODE_ENCODER_TMDS);
1836 drm_encoder_helper_add(encoder, &exynos_dsi_encoder_helper_funcs);
1838 ret = exynos_dsi_create_connector(encoder);
1840 DRM_ERROR("failed to create connector ret = %d\n", ret);
1841 drm_encoder_cleanup(encoder);
1845 bridge = of_drm_find_bridge(dsi->bridge_node);
1847 drm_bridge_attach(drm_dev, bridge);
1850 return mipi_dsi_host_register(&dsi->dsi_host);
1853 static void exynos_dsi_unbind(struct device *dev, struct device *master,
1856 struct drm_encoder *encoder = dev_get_drvdata(dev);
1857 struct exynos_dsi *dsi = encoder_to_dsi(encoder);
1859 exynos_dsi_disable(encoder);
1861 mipi_dsi_host_unregister(&dsi->dsi_host);
1864 static const struct component_ops exynos_dsi_component_ops = {
1865 .bind = exynos_dsi_bind,
1866 .unbind = exynos_dsi_unbind,
1869 static int exynos_dsi_probe(struct platform_device *pdev)
1871 struct device *dev = &pdev->dev;
1872 struct resource *res;
1873 struct exynos_dsi *dsi;
1876 dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
1880 /* To be checked as invalid one */
1881 dsi->te_gpio = -ENOENT;
1883 init_completion(&dsi->completed);
1884 spin_lock_init(&dsi->transfer_lock);
1885 INIT_LIST_HEAD(&dsi->transfer_list);
1887 dsi->dsi_host.ops = &exynos_dsi_ops;
1888 dsi->dsi_host.dev = dev;
1891 dsi->driver_data = exynos_dsi_get_driver_data(pdev);
1893 ret = exynos_dsi_parse_dt(dsi);
1897 dsi->supplies[0].supply = "vddcore";
1898 dsi->supplies[1].supply = "vddio";
1899 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(dsi->supplies),
1902 dev_info(dev, "failed to get regulators: %d\n", ret);
1903 return -EPROBE_DEFER;
1906 dsi->clks = devm_kzalloc(dev,
1907 sizeof(*dsi->clks) * dsi->driver_data->num_clks,
1912 for (i = 0; i < dsi->driver_data->num_clks; i++) {
1913 dsi->clks[i] = devm_clk_get(dev, clk_names[i]);
1914 if (IS_ERR(dsi->clks[i])) {
1915 if (strcmp(clk_names[i], "sclk_mipi") == 0) {
1916 strcpy(clk_names[i], OLD_SCLK_MIPI_CLK_NAME);
1921 dev_info(dev, "failed to get the clock: %s\n",
1923 return PTR_ERR(dsi->clks[i]);
1927 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1928 dsi->reg_base = devm_ioremap_resource(dev, res);
1929 if (IS_ERR(dsi->reg_base)) {
1930 dev_err(dev, "failed to remap io region\n");
1931 return PTR_ERR(dsi->reg_base);
1934 dsi->phy = devm_phy_get(dev, "dsim");
1935 if (IS_ERR(dsi->phy)) {
1936 dev_info(dev, "failed to get dsim phy\n");
1937 return PTR_ERR(dsi->phy);
1940 dsi->irq = platform_get_irq(pdev, 0);
1942 dev_err(dev, "failed to request dsi irq resource\n");
1946 irq_set_status_flags(dsi->irq, IRQ_NOAUTOEN);
1947 ret = devm_request_threaded_irq(dev, dsi->irq, NULL,
1948 exynos_dsi_irq, IRQF_ONESHOT,
1949 dev_name(dev), dsi);
1951 dev_err(dev, "failed to request dsi irq\n");
1955 platform_set_drvdata(pdev, &dsi->encoder);
1957 return component_add(dev, &exynos_dsi_component_ops);
1960 static int exynos_dsi_remove(struct platform_device *pdev)
1962 component_del(&pdev->dev, &exynos_dsi_component_ops);
1967 struct platform_driver dsi_driver = {
1968 .probe = exynos_dsi_probe,
1969 .remove = exynos_dsi_remove,
1971 .name = "exynos-dsi",
1972 .owner = THIS_MODULE,
1973 .of_match_table = exynos_dsi_of_match,
1977 MODULE_AUTHOR("Tomasz Figa <t.figa@samsung.com>");
1978 MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
1979 MODULE_DESCRIPTION("Samsung SoC MIPI DSI Master");
1980 MODULE_LICENSE("GPL v2");