2 * Copyright (C) 2014 ROCKCHIP, Inc.
3 * drivers/video/display/transmitter/rk32_mipi_dsi.c
4 * author: libing@rock-chips.com
5 * create date: 2014-04-10
6 * debug /sys/kernel/debug/mipidsi*
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #ifndef CONFIG_RK32_MIPI_DSI
22 #ifdef CONFIG_RK32_MIPI_DSI
23 #define MIPI_DSI_REGISTER_IO 0
24 #define CONFIG_MIPI_DSI_LINUX 0
26 #define DWC_DSI_VERSION 0x3133302A
27 #define DWC_DSI_VERSION_RK312x 0x3132312A
28 #define DWC_DSI_VERSION_RK3368 0x3133302B
29 #define DWC_DSI_VERSION_ERR -1
31 #include <linux/kernel.h>
32 #include <linux/init.h>
33 #include <linux/platform_device.h>
34 #include <linux/slab.h>
35 #include <linux/rk_fb.h>
36 #include <linux/rk_screen.h>
37 #include <linux/delay.h>
38 #include <linux/clk.h>
39 #include <linux/interrupt.h>
40 #include <asm/div64.h>
43 #include <linux/debugfs.h>
44 #include <linux/seq_file.h>
45 #include <linux/regulator/machine.h>
47 #include <linux/dma-mapping.h>
49 #include "rk32_mipi_dsi.h"
50 #include <linux/rockchip/iomap.h>
51 #include <linux/rockchip/cpu.h>
53 #define MIPI_DBG(x...) /* printk(KERN_INFO x) */
55 #ifdef CONFIG_MIPI_DSI_LINUX
56 #define MIPI_TRACE(x...) /* printk(KERN_INFO x) */
58 #define MIPI_TRACE(...) \
69 *v1.0 : this driver is rk32 mipi dsi driver of rockchip;
70 *v1.1 : add test eye pattern;
74 #define RK_MIPI_DSI_VERSION_AND_TIME "rockchip mipi_dsi v1.1 2014-06-17"
76 static struct dsi *dsi0;
77 static struct dsi *dsi1;
79 static int rk32_mipi_dsi_is_active(void *arg);
80 static int rk32_mipi_dsi_enable_hs_clk(void *arg, u32 enable);
81 static int rk32_mipi_dsi_enable_video_mode(void *arg, u32 enable);
82 static int rk32_mipi_dsi_enable_command_mode(void *arg, u32 enable);
83 static int rk32_mipi_dsi_is_enable(void *arg, u32 enable);
84 static int rk32_mipi_power_down_DDR(void);
85 static int rk32_mipi_power_up_DDR(void);
86 int rk_mipi_screen_standby(u8 enable);
88 int rockchip_get_screen_type(void)
90 struct device_node *type_node;
91 struct device_node *childnode;
94 type_node = of_find_node_by_name(NULL, "display-timings");
96 pr_err("could not find display-timings node\n");
100 for_each_child_of_node(type_node, childnode) {
101 if (!of_property_read_u32(childnode, "screen-type", &val))
108 static int rk32_dsi_read_reg(struct dsi *dsi, u16 reg, u32 *pval)
110 if (dsi->ops.id == DWC_DSI_VERSION)
111 *pval = __raw_readl(dsi->host.membase + (reg - MIPI_DSI_HOST_OFFSET));
112 else if (dsi->ops.id == DWC_DSI_VERSION_RK312x ||
113 dsi->ops.id == DWC_DSI_VERSION_RK3368) {
114 if (reg >= MIPI_DSI_HOST_OFFSET)
115 *pval = __raw_readl(dsi->host.membase + (reg - MIPI_DSI_HOST_OFFSET));
116 else if (reg >= MIPI_DSI_PHY_OFFSET)
117 *pval = __raw_readl(dsi->phy.membase + (reg - MIPI_DSI_PHY_OFFSET));
122 static int rk32_dsi_write_reg(struct dsi *dsi, u16 reg, u32 *pval)
124 if (dsi->ops.id == DWC_DSI_VERSION)
125 __raw_writel(*pval, dsi->host.membase + (reg - MIPI_DSI_HOST_OFFSET));
126 else if (dsi->ops.id == DWC_DSI_VERSION_RK312x ||
127 dsi->ops.id == DWC_DSI_VERSION_RK3368) {
128 if (reg >= MIPI_DSI_HOST_OFFSET)
129 __raw_writel(*pval, dsi->host.membase + (reg - MIPI_DSI_HOST_OFFSET));
130 else if (reg >= MIPI_DSI_PHY_OFFSET)
131 __raw_writel(*pval, dsi->phy.membase + (reg - MIPI_DSI_PHY_OFFSET));
136 static int rk32_dsi_get_bits(struct dsi *dsi, u32 reg)
139 u32 bits = (reg >> 8) & 0xff;
140 u16 reg_addr = (reg >> 16) & 0xffff;
141 u8 offset = reg & 0xff;
144 bits = (1 << bits) - 1;
148 rk32_dsi_read_reg(dsi, reg_addr, &val);
154 static int rk32_dsi_set_bits(struct dsi *dsi, u32 data, u32 reg)
157 u32 bits = (reg >> 8) & 0xff;
158 u16 reg_addr = (reg >> 16) & 0xffff;
159 u8 offset = reg & 0xff;
162 bits = (1 << bits) - 1;
166 if (bits != 0xffffffff)
167 rk32_dsi_read_reg(dsi, reg_addr, &val);
169 val &= ~(bits << offset);
170 val |= (data & bits) << offset;
171 rk32_dsi_write_reg(dsi, reg_addr, &val);
173 if (data > ((1 << (bits+1)) - 1)) {
174 MIPI_TRACE("%s error reg_addr:0x%04x, offset:%d, bits:0x%04x, value:0x%04x\n",
175 __func__, reg_addr, offset, bits, data);
180 static int rk32_dwc_phy_test_rd(struct dsi *dsi, unsigned char test_code)
183 rk32_dsi_set_bits(dsi, 1, phy_testclk);
184 rk32_dsi_set_bits(dsi, test_code, phy_testdin);
185 rk32_dsi_set_bits(dsi, 1, phy_testen);
186 rk32_dsi_set_bits(dsi, 0, phy_testclk);
187 rk32_dsi_set_bits(dsi, 0, phy_testen);;
189 rk32_dsi_set_bits(dsi, 0, phy_testen);
190 val = rk32_dsi_get_bits(dsi, phy_testdout);
191 rk32_dsi_set_bits(dsi, 1, phy_testclk);
192 rk32_dsi_set_bits(dsi, 0, phy_testclk);
197 static int rk32_dwc_phy_test_wr(struct dsi *dsi, unsigned char test_code, unsigned char *test_data, unsigned char size)
201 MIPI_DBG("test_code=0x%x,test_data=0x%x\n", test_code, test_data[0]);
202 rk32_dsi_set_bits(dsi, 0x10000 | test_code, PHY_TEST_CTRL1);
203 rk32_dsi_set_bits(dsi, 0x2, PHY_TEST_CTRL0);
204 rk32_dsi_set_bits(dsi, 0x0, PHY_TEST_CTRL0);
206 for (i = 0; i < size; i++) {
207 rk32_dsi_set_bits(dsi, test_data[i], PHY_TEST_CTRL1);
208 rk32_dsi_set_bits(dsi, 0x2, PHY_TEST_CTRL0);
209 rk32_dsi_set_bits(dsi, 0x0, PHY_TEST_CTRL0);
210 MIPI_DBG("rk32_dwc_phy_test_wr:%08x\n", rk32_dsi_get_bits(dsi, PHY_TEST_CTRL1));
215 static int rk32_phy_power_up(struct dsi *dsi)
217 /* enable ref clock */
218 clk_prepare_enable(dsi->phy.refclk);
219 clk_prepare_enable(dsi->dsi_pclk);
220 clk_prepare_enable(dsi->dsi_pd);
223 switch (dsi->host.lane) {
225 rk32_dsi_set_bits(dsi, 3, n_lanes);
228 rk32_dsi_set_bits(dsi, 2, n_lanes);
231 rk32_dsi_set_bits(dsi, 1, n_lanes);
234 rk32_dsi_set_bits(dsi, 0, n_lanes);
239 rk32_dsi_set_bits(dsi, 1, phy_shutdownz);
240 rk32_dsi_set_bits(dsi, 1, phy_rstz);
241 rk32_dsi_set_bits(dsi, 1, phy_enableclk);
242 rk32_dsi_set_bits(dsi, 1, phy_forcepll);
247 static int rk312x_mipi_dsi_phy_set_gotp(struct dsi *dsi, u32 offset, int n)
249 u32 val = 0, temp = 0, Tlpx = 0;
250 u32 ddr_clk = dsi->phy.ddr_clk;
251 u32 Ttxbyte_clk = dsi->phy.Ttxbyte_clk;
252 u32 Tsys_clk = dsi->phy.Tsys_clk;
253 u32 Ttxclkesc = dsi->phy.Ttxclkesc;
254 printk("%s : ddr_clk %d\n", __func__, ddr_clk);
256 case DPHY_CLOCK_OFFSET:
257 MIPI_DBG("******set DPHY_CLOCK_OFFSET gotp******\n");
259 case DPHY_LANE0_OFFSET:
260 MIPI_DBG("******set DPHY_LANE0_OFFSET gotp******\n");
262 case DPHY_LANE1_OFFSET:
263 MIPI_DBG("******set DPHY_LANE1_OFFSET gotp******\n");
265 case DPHY_LANE2_OFFSET:
266 MIPI_DBG("******set DPHY_LANE2_OFFSET gotp******\n");
268 case DPHY_LANE3_OFFSET:
269 MIPI_DBG("******set DPHY_LANE3_OFFSET gotp******\n");
275 if (ddr_clk < 110 * MHz)
277 else if (ddr_clk < 150 * MHz)
279 else if (ddr_clk < 200 * MHz)
281 else if (ddr_clk < 250 * MHz)
283 else if (ddr_clk < 300 * MHz)
285 else if (ddr_clk < 400 * MHz)
287 else if (ddr_clk < 500 * MHz)
289 else if (ddr_clk < 600 * MHz)
291 else if (ddr_clk < 700 * MHz)
293 else if (ddr_clk < 800 * MHz)
295 else if (ddr_clk <= 1000 * MHz)
297 printk("%s reg_ths_settle = 0x%x\n", __func__, val);
298 rk32_dsi_set_bits(dsi, val, reg_ths_settle + offset);
300 if (ddr_clk < 110 * MHz)
302 else if (ddr_clk < 150 * MHz)
304 else if (ddr_clk < 200 * MHz)
306 else if (ddr_clk < 250 * MHz)
308 else if (ddr_clk < 300 * MHz)
310 else if (ddr_clk < 400 * MHz)
312 else if (ddr_clk < 500 * MHz)
314 else if (ddr_clk < 600 * MHz)
316 else if (ddr_clk < 700 * MHz)
318 else if (ddr_clk < 800 * MHz)
320 else if (ddr_clk <= 1000 * MHz)
322 printk("%s reg_hs_ths_prepare = 0x%x\n", __func__, val);
323 rk32_dsi_set_bits(dsi, val, reg_hs_ths_prepare + offset);
325 if (offset != DPHY_CLOCK_OFFSET) {
326 if (ddr_clk < 110 * MHz)
328 else if (ddr_clk < 150 * MHz)
330 else if (ddr_clk < 200 * MHz)
332 else if (ddr_clk < 250 * MHz)
334 else if (ddr_clk < 300 * MHz)
336 else if (ddr_clk < 400 * MHz)
338 else if (ddr_clk < 500 * MHz)
340 else if (ddr_clk < 600 * MHz)
342 else if (ddr_clk < 700 * MHz)
344 else if (ddr_clk < 800 * MHz)
346 else if (ddr_clk <= 1000 * MHz)
349 if (ddr_clk < 110 * MHz)
351 else if (ddr_clk < 150 * MHz)
353 else if (ddr_clk < 200 * MHz)
355 else if (ddr_clk < 250 * MHz)
357 else if (ddr_clk < 300 * MHz)
359 else if (ddr_clk < 400 * MHz)
361 else if (ddr_clk < 500 * MHz)
363 else if (ddr_clk < 600 * MHz)
365 else if (ddr_clk < 700 * MHz)
367 else if (ddr_clk < 800 * MHz)
369 else if (ddr_clk <= 1000 * MHz)
372 printk("%s reg_hs_the_zero = 0x%x\n", __func__, val);
373 rk32_dsi_set_bits(dsi, val, reg_hs_the_zero + offset);
375 if (ddr_clk < 110 * MHz)
377 else if (ddr_clk < 150 * MHz)
379 else if (ddr_clk < 200 * MHz)
381 else if (ddr_clk < 250 * MHz)
383 else if (ddr_clk < 300 * MHz)
385 else if (ddr_clk < 400 * MHz)
387 else if (ddr_clk < 500 * MHz)
389 else if (ddr_clk < 600 * MHz)
391 else if (ddr_clk < 700 * MHz)
393 else if (ddr_clk < 800 * MHz)
395 else if (ddr_clk <= 1000 * MHz)
396 val = 0x21; /* 0x27 */
398 printk("%s reg_hs_ths_trail = 0x%x\n", __func__, val);
400 rk32_dsi_set_bits(dsi, val, reg_hs_ths_trail + offset);
401 val = 120000 / Ttxbyte_clk + 1;
402 MIPI_DBG("reg_hs_ths_exit: %d, %d\n", val, val*Ttxbyte_clk/1000);
403 rk32_dsi_set_bits(dsi, val, reg_hs_ths_exit + offset);
405 if (offset == DPHY_CLOCK_OFFSET) {
406 val = (60000 + 52*dsi->phy.UI) / Ttxbyte_clk + 1;
407 MIPI_DBG("reg_hs_tclk_post: %d, %d\n", val, val*Ttxbyte_clk/1000);
408 rk32_dsi_set_bits(dsi, val, reg_hs_tclk_post + offset);
409 val = 10*dsi->phy.UI / Ttxbyte_clk + 1;
410 MIPI_DBG("reg_hs_tclk_pre: %d, %d\n", val, val*Ttxbyte_clk/1000);
411 rk32_dsi_set_bits(dsi, val, reg_hs_tclk_pre + offset);
414 val = 1010000000 / Tsys_clk + 1;
415 MIPI_DBG("reg_hs_twakup: %d, %d\n", val, val*Tsys_clk/1000);
418 MIPI_DBG("val is too large, 0x3ff is the largest\n");
420 temp = (val >> 8) & 0x03;
422 rk32_dsi_set_bits(dsi, temp, reg_hs_twakup_h + offset);
423 rk32_dsi_set_bits(dsi, val, reg_hs_twakup_l + offset);
425 if (Ttxclkesc > 50000) {
427 MIPI_DBG("Ttxclkesc:%d\n", Ttxclkesc);
429 val = val / Ttxbyte_clk;
430 Tlpx = val*Ttxbyte_clk;
431 MIPI_DBG("reg_hs_tlpx: %d, %d\n", val, Tlpx);
433 rk32_dsi_set_bits(dsi, val, reg_hs_tlpx + offset);
436 val = 4*Tlpx / Ttxclkesc;
437 MIPI_DBG("reg_hs_tta_go: %d, %d\n", val, val*Ttxclkesc);
438 rk32_dsi_set_bits(dsi, val, reg_hs_tta_go + offset);
439 val = 3 * Tlpx / 2 / Ttxclkesc;
440 MIPI_DBG("reg_hs_tta_sure: %d, %d\n", val, val*Ttxclkesc);
441 rk32_dsi_set_bits(dsi, val, reg_hs_tta_sure + offset);
442 val = 5 * Tlpx / Ttxclkesc;
443 MIPI_DBG("reg_hs_tta_wait: %d, %d\n", val, val*Ttxclkesc);
444 rk32_dsi_set_bits(dsi, val, reg_hs_tta_wait + offset);
448 static void rk312x_mipi_dsi_set_hs_clk(struct dsi *dsi)
450 rk32_dsi_set_bits(dsi, dsi->phy.prediv, reg_prediv);
451 rk32_dsi_set_bits(dsi, dsi->phy.fbdiv & 0xff, reg_fbdiv);
452 rk32_dsi_set_bits(dsi, (dsi->phy.fbdiv >> 8) & 0x01, reg_fbdiv_8);
455 static int rk312x_phy_power_up(struct dsi *dsi)
457 /* enable ref clock */
458 rk312x_mipi_dsi_set_hs_clk(dsi);
459 clk_prepare_enable(dsi->phy.refclk);
460 clk_prepare_enable(dsi->dsi_pclk);
461 clk_prepare_enable(dsi->dsi_host_pclk);
462 if (dsi->ops.id == DWC_DSI_VERSION_RK312x) {
463 clk_prepare_enable(dsi->h2p_hclk);
464 clk_prepare_enable(dsi->dsi_pd);
466 clk_prepare_enable(dsi->dsi_pd);
470 rk32_dsi_set_bits(dsi, 0xe4, DPHY_REGISTER1);
471 switch (dsi->host.lane) {
473 rk32_dsi_set_bits(dsi, 1, lane_en_3);
475 rk32_dsi_set_bits(dsi, 1, lane_en_2);
477 rk32_dsi_set_bits(dsi, 1, lane_en_1);
479 rk32_dsi_set_bits(dsi, 1, lane_en_0);
480 rk32_dsi_set_bits(dsi, 1, lane_en_ck);
486 rk32_dsi_set_bits(dsi, 0xe0, DPHY_REGISTER1);
489 rk32_dsi_set_bits(dsi, 0x1e, DPHY_REGISTER20);
490 rk32_dsi_set_bits(dsi, 0x1f, DPHY_REGISTER20);
492 rk32_dsi_set_bits(dsi, 1, phy_enableclk);
497 static int rk_phy_power_up(struct dsi *dsi)
499 if (dsi->ops.id == DWC_DSI_VERSION)
500 rk32_phy_power_up(dsi);
501 else if (dsi->ops.id == DWC_DSI_VERSION_RK312x ||
502 dsi->ops.id == DWC_DSI_VERSION_RK3368)
503 rk312x_phy_power_up(dsi);
507 static int rk32_phy_power_down(struct dsi *dsi)
509 rk32_dsi_set_bits(dsi, 0, phy_shutdownz);
510 clk_disable_unprepare(dsi->phy.refclk);
511 clk_disable_unprepare(dsi->dsi_pclk);
512 clk_disable_unprepare(dsi->dsi_pd);
516 static int rk312x_phy_power_down(struct dsi *dsi)
518 rk32_dsi_set_bits(dsi, 0x01, DPHY_REGISTER0);
519 rk32_dsi_set_bits(dsi, 0xe3, DPHY_REGISTER1);
521 clk_disable_unprepare(dsi->phy.refclk);
522 clk_disable_unprepare(dsi->dsi_pclk);
523 clk_disable_unprepare(dsi->dsi_host_pclk);
525 if (dsi->ops.id == DWC_DSI_VERSION_RK312x) {
526 clk_disable_unprepare(dsi->h2p_hclk);
527 clk_disable_unprepare(dsi->dsi_pd);
529 clk_disable_unprepare(dsi->dsi_pd);
533 static int rk_phy_power_down(struct dsi *dsi)
535 if (dsi->ops.id == DWC_DSI_VERSION)
536 rk32_phy_power_down(dsi);
537 else if (dsi->ops.id == DWC_DSI_VERSION_RK312x ||
538 dsi->ops.id == DWC_DSI_VERSION_RK3368)
539 rk312x_phy_power_down(dsi);
543 static int rk32_phy_init(struct dsi *dsi)
545 u32 val = 0 , ddr_clk = 0, fbdiv = 0, prediv = 0;
546 unsigned char test_data[2] = {0};
548 ddr_clk = dsi->phy.ddr_clk;
549 prediv = dsi->phy.prediv;
550 fbdiv = dsi->phy.fbdiv;
552 if (ddr_clk < 200 * MHz)
554 else if (ddr_clk < 300 * MHz)
556 else if (ddr_clk < 500 * MHz)
558 else if (ddr_clk < 700 * MHz)
560 else if (ddr_clk < 900 * MHz)
562 else if (ddr_clk < 1100 * MHz)
564 else if (ddr_clk < 1300 * MHz)
566 else if (ddr_clk <= 1500 * MHz)
569 test_data[0] = 0x80 | val << 3 | 0x3;
570 rk32_dwc_phy_test_wr(dsi, 0x10, test_data, 1);
573 rk32_dwc_phy_test_wr(dsi, 0x11, test_data, 1);
575 test_data[0] = 0x80 | 0x40;
576 rk32_dwc_phy_test_wr(dsi, 0x12, test_data, 1);
578 if (ddr_clk < 90 * MHz)
580 else if (ddr_clk < 100 * MHz)
582 else if (ddr_clk < 110 * MHz)
584 else if (ddr_clk < 130 * MHz)
586 else if (ddr_clk < 140 * MHz)
588 else if (ddr_clk < 150 * MHz)
590 else if (ddr_clk < 170 * MHz)
592 else if (ddr_clk < 180 * MHz)
594 else if (ddr_clk < 200 * MHz)
596 else if (ddr_clk < 220 * MHz)
598 else if (ddr_clk < 240 * MHz)
600 else if (ddr_clk < 250 * MHz)
602 else if (ddr_clk < 270 * MHz)
604 else if (ddr_clk < 300 * MHz)
606 else if (ddr_clk < 330 * MHz)
608 else if (ddr_clk < 360 * MHz)
610 else if (ddr_clk < 400 * MHz)
612 else if (ddr_clk < 450 * MHz)
614 else if (ddr_clk < 500 * MHz)
616 else if (ddr_clk < 550 * MHz)
618 else if (ddr_clk < 600 * MHz)
620 else if (ddr_clk < 650 * MHz)
622 else if (ddr_clk < 700 * MHz)
624 else if (ddr_clk < 750 * MHz)
626 else if (ddr_clk < 800 * MHz)
628 else if (ddr_clk < 850 * MHz)
630 else if (ddr_clk < 900 * MHz)
632 else if (ddr_clk < 950 * MHz)
634 else if (ddr_clk < 1000 * MHz)
636 else if (ddr_clk < 1050 * MHz)
638 else if (ddr_clk < 1100 * MHz)
640 else if (ddr_clk < 1150 * MHz)
642 else if (ddr_clk < 1200 * MHz)
644 else if (ddr_clk < 1250 * MHz)
646 else if (ddr_clk < 1300 * MHz)
648 else if (ddr_clk < 1350 * MHz)
650 else if (ddr_clk < 1400 * MHz)
652 else if (ddr_clk < 1450 * MHz)
654 else if (ddr_clk <= 1500 * MHz)
657 test_data[0] = val << 1;
658 rk32_dwc_phy_test_wr(dsi, code_hs_rx_lane0, test_data, 1);
660 test_data[0] = prediv - 1;
661 rk32_dwc_phy_test_wr(dsi, code_pll_input_div_rat, test_data, 1);
663 test_data[0] = (fbdiv - 1) & 0x1f;
664 rk32_dwc_phy_test_wr(dsi, code_pll_loop_div_rat, test_data, 1);
666 test_data[0] = (fbdiv - 1) >> 5 | 0x80;
667 rk32_dwc_phy_test_wr(dsi, code_pll_loop_div_rat, test_data, 1);
670 rk32_dwc_phy_test_wr(dsi, code_pll_input_loop_div_rat, test_data, 1);
674 rk32_dwc_phy_test_wr(dsi, 0x20, test_data, 1);
677 rk32_dwc_phy_test_wr(dsi, 0x21, test_data, 1);
680 rk32_dwc_phy_test_wr(dsi, 0x21, test_data, 1);
683 rk32_dwc_phy_test_wr(dsi, 0x22, test_data, 1);
685 test_data[0] = 0x80 | 0x7;
686 rk32_dwc_phy_test_wr(dsi, 0x22, test_data, 1);
688 test_data[0] = 0x80 | 15;
689 rk32_dwc_phy_test_wr(dsi, code_hstxdatalanerequsetstatetime, test_data, 1);
691 test_data[0] = 0x80 | 85;
692 rk32_dwc_phy_test_wr(dsi, code_hstxdatalanepreparestatetime, test_data, 1);
694 test_data[0] = 0x40 | 10;
695 rk32_dwc_phy_test_wr(dsi, code_hstxdatalanehszerostatetime, test_data, 1);
700 static int rk312x_phy_init(struct dsi *dsi, int n)
703 rk32_dsi_set_bits(dsi, 0x11, DSI_DPHY_BITS(0x06<<2, 32, 0));
704 rk32_dsi_set_bits(dsi, 0x11, DSI_DPHY_BITS(0x07<<2, 32, 0));
705 rk32_dsi_set_bits(dsi, 0xcc, DSI_DPHY_BITS(0x09<<2, 32, 0));
707 dsi_set_bits(0x4e, DSI_DPHY_BITS(0x08<<2, 32, 0));
708 dsi_set_bits(0x84, DSI_DPHY_BITS(0x0a<<2, 32, 0));
711 /*reg1[4] 0: enable a function of "pll phase for serial data being captured
714 we disable it here because reg5[6:4] is not compatible with the HS speed.
717 if (dsi->phy.ddr_clk >= 800*MHz) {
718 if (dsi->ops.id == DWC_DSI_VERSION_RK3368) {
719 rk32_dsi_set_bits(dsi, 0x10, DSI_DPHY_BITS(0x05<<2, 32, 0));
721 rk32_dsi_set_bits(dsi, 0x30, DSI_DPHY_BITS(0x05<<2, 32, 0));
724 rk32_dsi_set_bits(dsi, 1, reg_da_ppfc);
727 switch (dsi->host.lane) {
729 rk312x_mipi_dsi_phy_set_gotp(dsi, DPHY_LANE3_OFFSET, n);
731 rk312x_mipi_dsi_phy_set_gotp(dsi, DPHY_LANE2_OFFSET, n);
733 rk312x_mipi_dsi_phy_set_gotp(dsi, DPHY_LANE1_OFFSET, n);
735 rk312x_mipi_dsi_phy_set_gotp(dsi, DPHY_LANE0_OFFSET, n);
736 rk312x_mipi_dsi_phy_set_gotp(dsi, DPHY_CLOCK_OFFSET, n);
742 rk32_dsi_set_bits(dsi, 0x00e4, reg1_phy);
743 rk32_dsi_set_bits(dsi, 0x007d, reg0_phy);
745 rk32_dsi_set_bits(dsi, 0x00e0, reg1_phy);
746 rk32_dsi_set_bits(dsi, 0x001e, reg20_phy);
748 rk32_dsi_set_bits(dsi, 0x001f, reg20_phy);
750 rk32_dsi_set_bits(dsi, 0x0063, reg10_phy);
752 if (dsi->ops.id == DWC_DSI_VERSION_RK3368) {
753 rk32_dsi_set_bits(dsi, 0x1, reg5_phy);
755 rk32_dsi_set_bits(dsi, 0x6, reg5_phy);
757 rk32_dsi_set_bits(dsi, 0x6, reg10_4_6_phy);
758 rk32_dsi_set_bits(dsi, 0x9, regb_phy);
763 static int rk_phy_init(struct dsi *dsi)
765 if (dsi->ops.id == DWC_DSI_VERSION)
767 else if (dsi->ops.id == DWC_DSI_VERSION_RK312x ||
768 dsi->ops.id == DWC_DSI_VERSION_RK3368)
769 rk312x_phy_init(dsi, 4);
773 static int rk32_mipi_dsi_host_power_up(struct dsi *dsi)
778 /* disable all interrupt */
779 rk32_dsi_set_bits(dsi, 0x1fffff, INT_MKS0);
780 rk32_dsi_set_bits(dsi, 0x3ffff, INT_MKS1);
782 rk32_mipi_dsi_is_enable(dsi, 1);
785 while (!rk32_dsi_get_bits(dsi, phylock) && val--) {
791 MIPI_TRACE("%s:phylock fail.\n", __func__);
795 while (!rk32_dsi_get_bits(dsi, phystopstateclklane) && val--) {
802 static int rk32_mipi_dsi_host_power_down(struct dsi *dsi)
804 rk32_mipi_dsi_enable_video_mode(dsi, 0);
805 rk32_mipi_dsi_enable_hs_clk(dsi, 0);
806 rk32_mipi_dsi_is_enable(dsi, 0);
810 static int rk32_mipi_dsi_host_init(struct dsi *dsi)
812 u32 val = 0, bytes_px = 0;
813 struct mipi_dsi_screen *screen = &dsi->screen;
814 u32 decimals = dsi->phy.Ttxbyte_clk, temp = 0, i = 0;
815 u32 m = 1, lane = dsi->host.lane, Tpclk = dsi->phy.Tpclk,
816 Ttxbyte_clk = dsi->phy.Ttxbyte_clk;
818 rk32_dsi_set_bits(dsi, dsi->host.lane - 1, n_lanes);
819 rk32_dsi_set_bits(dsi, dsi->vid, dpi_vcid);
821 switch (screen->face) {
823 rk32_dsi_set_bits(dsi, 5, dpi_color_coding);
828 rk32_dsi_set_bits(dsi, 3, dpi_color_coding);
829 rk32_dsi_set_bits(dsi, 1, en18_loosely);
833 rk32_dsi_set_bits(dsi, 0, dpi_color_coding);
838 if (dsi->ops.id == DWC_DSI_VERSION ||
839 dsi->ops.id == DWC_DSI_VERSION_RK3368) {
840 rk32_dsi_set_bits(dsi, 1, hsync_active_low);
841 rk32_dsi_set_bits(dsi, 1, vsync_active_low);
843 rk32_dsi_set_bits(dsi, 0, dataen_active_low);
844 rk32_dsi_set_bits(dsi, 0, colorm_active_low);
845 rk32_dsi_set_bits(dsi, 0, shutd_active_low);
846 } else if (dsi->ops.id == DWC_DSI_VERSION_RK312x) {
847 rk32_dsi_set_bits(dsi, !screen->pin_hsync, hsync_active_low);
848 rk32_dsi_set_bits(dsi, !screen->pin_vsync, vsync_active_low);
850 rk32_dsi_set_bits(dsi, screen->pin_den, dataen_active_low);
851 rk32_dsi_set_bits(dsi, 0, colorm_active_low);
852 rk32_dsi_set_bits(dsi, 0, shutd_active_low);
855 rk32_dsi_set_bits(dsi, dsi->host.video_mode, vid_mode_type); /* burst mode */
857 switch (dsi->host.video_mode) {
859 if (screen->type == SCREEN_DUAL_MIPI)
860 rk32_dsi_set_bits(dsi, screen->x_res / 2 + 4, vid_pkt_size);
862 rk32_dsi_set_bits(dsi, screen->x_res, vid_pkt_size);
866 for (i = 8; i < 32; i++) {
867 temp = i * lane * Tpclk % Ttxbyte_clk;
868 if (decimals > temp) {
876 rk32_dsi_set_bits(dsi, screen->x_res / m + 1, num_chunks);
877 rk32_dsi_set_bits(dsi, m, vid_pkt_size);
878 temp = m * lane * Tpclk / Ttxbyte_clk - m * bytes_px;
879 MIPI_DBG("%s:%d, %d\n", __func__, m, temp);
882 rk32_dsi_set_bits(dsi, temp - 12, null_pkt_size);
888 /* rk32_dsi_set_bits(dsi, 0, CMD_MODE_CFG << 16); */
889 if (screen->type == SCREEN_MIPI) {
890 rk32_dsi_set_bits(dsi, dsi->phy.Tpclk * (screen->x_res + screen->left_margin +
891 screen->hsync_len + screen->right_margin) \
892 / dsi->phy.Ttxbyte_clk, vid_hline_time);
893 } else { /* used for dual mipi screen */
894 rk32_dsi_set_bits(dsi, dsi->phy.Tpclk * (screen->x_res + 8 + screen->left_margin +
895 screen->hsync_len + screen->right_margin) \
896 / dsi->phy.Ttxbyte_clk, vid_hline_time);
898 MIPI_DBG("dsi->phy.Tpclk = %d\n", dsi->phy.Tpclk);
899 MIPI_DBG("screen->left_margin = %d\n", screen->left_margin);
900 MIPI_DBG("dsi->phy.Ttxbyte_clk = %d\n", dsi->phy.Ttxbyte_clk);
901 MIPI_DBG("screen->hsync_len = %d\n", screen->hsync_len);
902 rk32_dsi_set_bits(dsi, dsi->phy.Tpclk * (screen->left_margin) / dsi->phy.Ttxbyte_clk,
904 rk32_dsi_set_bits(dsi, dsi->phy.Tpclk * (screen->hsync_len) / dsi->phy.Ttxbyte_clk,
907 rk32_dsi_set_bits(dsi, screen->y_res , vid_active_lines);
908 rk32_dsi_set_bits(dsi, screen->lower_margin, vid_vfp_lines);
909 rk32_dsi_set_bits(dsi, screen->upper_margin, vid_vbp_lines);
910 rk32_dsi_set_bits(dsi, screen->vsync_len, vid_vsa_lines);
912 dsi->phy.txclkesc = 20 * MHz;
913 val = dsi->phy.txbyte_clk / dsi->phy.txclkesc + 1;
914 dsi->phy.txclkesc = dsi->phy.txbyte_clk / val;
915 rk32_dsi_set_bits(dsi, val, TX_ESC_CLK_DIVISION);
917 rk32_dsi_set_bits(dsi, 10, TO_CLK_DIVISION);
918 rk32_dsi_set_bits(dsi, 1000, hstx_to_cnt); /* no sure */
919 rk32_dsi_set_bits(dsi, 1000, lprx_to_cnt);
920 rk32_dsi_set_bits(dsi, 100, phy_stop_wait_time);
922 /* enable send command in low power mode */
923 rk32_dsi_set_bits(dsi, 4, outvact_lpcmd_time);
924 rk32_dsi_set_bits(dsi, 4, invact_lpcmd_time);
925 rk32_dsi_set_bits(dsi, 1, lp_cmd_en);
927 rk32_dsi_set_bits(dsi, 20, phy_hs2lp_time);
928 rk32_dsi_set_bits(dsi, 16, phy_lp2hs_time);
930 rk32_dsi_set_bits(dsi, 87, phy_hs2lp_time_clk_lane);
931 rk32_dsi_set_bits(dsi, 25, phy_hs2hs_time_clk_lane);
933 rk32_dsi_set_bits(dsi, 10000, max_rd_time);
935 rk32_dsi_set_bits(dsi, 1, lp_hfp_en);
936 /* rk32_dsi_set_bits(dsi, 1, lp_hbp_en); */
937 rk32_dsi_set_bits(dsi, 1, lp_vact_en);
938 rk32_dsi_set_bits(dsi, 1, lp_vfp_en);
939 rk32_dsi_set_bits(dsi, 1, lp_vbp_en);
940 rk32_dsi_set_bits(dsi, 1, lp_vsa_en);
942 /* rk32_dsi_set_bits(dsi, 1, frame_bta_ack_en); */
943 rk32_dsi_set_bits(dsi, 1, phy_enableclk);
944 rk32_dsi_set_bits(dsi, 0, phy_tx_triggers);
946 /* enable non-continued function */
947 /* rk32_dsi_set_bits(dsi, 1, auto_clklane_ctrl); */
949 rk32_dsi_set_bits(dsi, 1, phy_txexitulpslan);
950 rk32_dsi_set_bits(dsi, 1, phy_txexitulpsclk);
956 * mipi protocol layer definition
958 static int rk_mipi_dsi_init(void *arg, u32 n)
960 u32 decimals = 1000, i = 0, pre = 0;
961 struct dsi *dsi = arg;
962 struct mipi_dsi_screen *screen = &dsi->screen;
967 if ((screen->type != SCREEN_MIPI) && (screen->type != SCREEN_DUAL_MIPI)) {
968 MIPI_TRACE("only mipi dsi lcd is supported!\n");
972 if (((screen->type == SCREEN_DUAL_MIPI) && (rk_mipi_get_dsi_num() == 1)) || ((screen->type == SCREEN_MIPI) && (rk_mipi_get_dsi_num() == 2))) {
973 MIPI_TRACE("dsi number and mipi type not match!\n");
977 dsi->phy.Tpclk = rk_fb_get_prmry_screen_pixclock();
980 dsi->phy.ref_clk = clk_get_rate(dsi->phy.refclk) ;
981 if (dsi->ops.id == DWC_DSI_VERSION_RK312x ||
982 dsi->ops.id == DWC_DSI_VERSION_RK3368)
983 dsi->phy.ref_clk = dsi->phy.ref_clk / 2; /* 1/2 of input refclk */
985 dsi->phy.sys_clk = dsi->phy.ref_clk;
987 printk("dsi->phy.sys_clk =%d\n", dsi->phy.sys_clk);
988 if (dsi->ops.id == DWC_DSI_VERSION) {
989 if ((screen->hs_tx_clk <= 90 * MHz) || (screen->hs_tx_clk >= 1500 * MHz))
990 dsi->phy.ddr_clk = 1500 * MHz; /* default is 1.5HGz */
992 dsi->phy.ddr_clk = screen->hs_tx_clk;
993 } else if (dsi->ops.id == DWC_DSI_VERSION_RK312x ||
994 dsi->ops.id == DWC_DSI_VERSION_RK3368) {
995 if ((screen->hs_tx_clk <= 80 * MHz) || (screen->hs_tx_clk >= 1000 * MHz))
996 dsi->phy.ddr_clk = 1000 * MHz; /* default is 1GHz */
998 dsi->phy.ddr_clk = screen->hs_tx_clk;
1002 dsi->phy.ddr_clk = n;
1004 decimals = dsi->phy.ref_clk;
1005 for (i = 1; i < 6; i++) {
1006 pre = dsi->phy.ref_clk / i;
1007 if ((decimals > (dsi->phy.ddr_clk % pre)) && (dsi->phy.ddr_clk / pre < 512)) {
1008 decimals = dsi->phy.ddr_clk % pre;
1009 dsi->phy.prediv = i;
1010 dsi->phy.fbdiv = dsi->phy.ddr_clk / pre;
1016 MIPI_DBG("prediv:%d, fbdiv:%d,dsi->phy.ddr_clk:%d\n", dsi->phy.prediv, dsi->phy.fbdiv, dsi->phy.ref_clk / dsi->phy.prediv * dsi->phy.fbdiv);
1018 dsi->phy.ddr_clk = dsi->phy.ref_clk / dsi->phy.prediv * dsi->phy.fbdiv;
1020 MIPI_DBG("dsi->phy.ddr_clk =%d\n", dsi->phy.ddr_clk);
1021 dsi->phy.txbyte_clk = dsi->phy.ddr_clk / 8;
1023 dsi->phy.txclkesc = 20 * MHz; /* < 20MHz */
1024 dsi->phy.txclkesc = dsi->phy.txbyte_clk / (dsi->phy.txbyte_clk / dsi->phy.txclkesc + 1);
1026 dsi->phy.pclk = div_u64(1000000000000llu, dsi->phy.Tpclk);
1027 dsi->phy.Ttxclkesc = div_u64(1000000000000llu, dsi->phy.txclkesc);
1028 dsi->phy.Tsys_clk = div_u64(1000000000000llu, dsi->phy.sys_clk);
1029 dsi->phy.Tddr_clk = div_u64(1000000000000llu, dsi->phy.ddr_clk);
1030 dsi->phy.Ttxbyte_clk = div_u64(1000000000000llu, dsi->phy.txbyte_clk);
1032 dsi->phy.UI = dsi->phy.Tddr_clk;
1035 if (screen->dsi_lane > 0 && screen->dsi_lane <= 4)
1036 dsi->host.lane = screen->dsi_lane;
1040 dsi->host.video_mode = VM_BM;
1042 MIPI_DBG("UI:%d\n", dsi->phy.UI);
1043 MIPI_DBG("ref_clk:%d\n", dsi->phy.ref_clk);
1044 MIPI_DBG("pclk:%d, Tpclk:%d\n", dsi->phy.pclk, dsi->phy.Tpclk);
1045 MIPI_DBG("sys_clk:%d, Tsys_clk:%d\n", dsi->phy.sys_clk, dsi->phy.Tsys_clk);
1046 MIPI_DBG("ddr_clk:%d, Tddr_clk:%d\n", dsi->phy.ddr_clk, dsi->phy.Tddr_clk);
1047 MIPI_DBG("txbyte_clk:%d, Ttxbyte_clk:%d\n", dsi->phy.txbyte_clk,
1048 dsi->phy.Ttxbyte_clk);
1049 MIPI_DBG("txclkesc:%d, Ttxclkesc:%d\n", dsi->phy.txclkesc, dsi->phy.Ttxclkesc);
1052 rk_phy_power_up(dsi);
1053 rk32_mipi_dsi_host_power_up(dsi);
1055 rk32_mipi_dsi_host_init(dsi);
1060 static int rk32_mipi_dsi_is_enable(void *arg, u32 enable)
1062 struct dsi *dsi = arg;
1064 rk32_dsi_set_bits(dsi, enable, shutdownz);
1068 static int rk32_mipi_dsi_enable_video_mode(void *arg, u32 enable)
1070 struct dsi *dsi = arg;
1072 rk32_dsi_set_bits(dsi, !enable, cmd_video_mode);
1076 static int rk32_mipi_dsi_enable_command_mode(void *arg, u32 enable)
1078 struct dsi *dsi = arg;
1080 rk32_dsi_set_bits(dsi, enable, cmd_video_mode);
1084 static int rk32_mipi_dsi_enable_hs_clk(void *arg, u32 enable)
1086 struct dsi *dsi = arg;
1088 rk32_dsi_set_bits(dsi, enable, phy_txrequestclkhs);
1092 static int rk32_mipi_dsi_is_active(void *arg)
1094 struct dsi *dsi = arg;
1096 return rk32_dsi_get_bits(dsi, shutdownz);
1099 static int rk32_mipi_dsi_send_packet(void *arg, unsigned char cmds[], u32 length)
1101 struct dsi *dsi = arg;
1102 unsigned char *regs;
1103 u32 type, liTmp = 0, i = 0, j = 0, data = 0;
1105 if (rk32_dsi_get_bits(dsi, gen_cmd_full) == 1) {
1106 MIPI_TRACE("gen_cmd_full\n");
1109 regs = kmalloc(0x400, GFP_KERNEL);
1111 printk("request regs fail!\n");
1114 memcpy(regs, cmds, length);
1120 case DTYPE_DCS_SWRITE_0P:
1121 rk32_dsi_set_bits(dsi, regs[0], dcs_sw_0p_tx);
1122 data = regs[2] << 8 | type;
1124 case DTYPE_DCS_SWRITE_1P:
1125 rk32_dsi_set_bits(dsi, regs[0], dcs_sw_1p_tx);
1126 data = regs[2] << 8 | type;
1127 data |= regs[3] << 16;
1129 case DTYPE_DCS_LWRITE:
1130 rk32_dsi_set_bits(dsi, regs[0], dcs_lw_tx);
1131 for (i = 0; i < liTmp; i++) {
1132 regs[i] = regs[i+2];
1134 for (i = 0; i < liTmp; i++) {
1136 data |= regs[i] << (j * 8);
1137 if (j == 3 || ((i + 1) == liTmp)) {
1138 if (rk32_dsi_get_bits(dsi, gen_pld_w_full) == 1) {
1139 MIPI_TRACE("gen_pld_w_full :%d\n", i);
1142 rk32_dsi_set_bits(dsi, data, GEN_PLD_DATA);
1143 MIPI_DBG("write GEN_PLD_DATA:%d, %08x\n", i, data);
1148 data |= (liTmp & 0xffff) << 8;
1150 case DTYPE_GEN_LWRITE:
1151 rk32_dsi_set_bits(dsi, regs[0], gen_lw_tx);
1152 for (i = 0; i < liTmp; i++) {
1153 regs[i] = regs[i+2];
1155 for (i = 0; i < liTmp; i++) {
1157 data |= regs[i] << (j * 8);
1158 if (j == 3 || ((i + 1) == liTmp)) {
1159 if (rk32_dsi_get_bits(dsi, gen_pld_w_full) == 1) {
1160 MIPI_TRACE("gen_pld_w_full :%d\n", i);
1163 rk32_dsi_set_bits(dsi, data, GEN_PLD_DATA);
1164 MIPI_DBG("write GEN_PLD_DATA:%d, %08x\n", i, data);
1168 data = (dsi->vid << 6) | type;
1169 data |= (liTmp & 0xffff) << 8;
1171 case DTYPE_GEN_SWRITE_2P: /* one command and one parameter */
1172 rk32_dsi_set_bits(dsi, regs[0], gen_sw_2p_tx);
1174 /* It is used for normal Generic Short WRITE Packet with 2 parameters. */
1176 data |= regs[2] << 8; /* dcs command */
1177 data |= regs[3] << 16; /* parameter of command */
1181 /* The below is used for Generic Short WRITE Packet with 2 parameters
1182 * that more than 2 parameters. Though it is illegal dcs command, we can't
1183 * make sure the users do not send that command.
1185 for (i = 0; i < liTmp; i++) {
1186 regs[i] = regs[i+2];
1188 for (i = 0; i < liTmp; i++) {
1190 data |= regs[i] << (j * 8);
1191 if (j == 3 || ((i + 1) == liTmp)) {
1192 if (rk32_dsi_get_bits(dsi, gen_pld_w_full) == 1) {
1193 MIPI_TRACE("gen_pld_w_full :%d\n", i);
1196 rk32_dsi_set_bits(dsi, data, GEN_PLD_DATA);
1197 MIPI_DBG("write GEN_PLD_DATA:%d, %08x\n", i, data);
1202 data |= (liTmp & 0xffff) << 8;
1204 case DTYPE_GEN_SWRITE_1P: /* one command without parameter */
1205 rk32_dsi_set_bits(dsi, regs[0], gen_sw_1p_tx);
1207 data |= regs[2] << 8;
1209 case DTYPE_GEN_SWRITE_0P: /* nop packet without command and parameter */
1210 rk32_dsi_set_bits(dsi, regs[0], gen_sw_0p_tx);
1214 printk("0x%x:this type not suppport!\n", type);
1217 MIPI_DBG("%d command sent in %s size:%d\n", __LINE__, regs[0] ? "LP mode" : "HS mode", liTmp);
1219 MIPI_DBG("write GEN_HDR:%08x\n", data);
1220 rk32_dsi_set_bits(dsi, data, GEN_HDR);
1223 while (!rk32_dsi_get_bits(dsi, gen_cmd_empty) && i--) {
1232 static int rk32_mipi_dsi_read_dcs_packet(void *arg, unsigned char *data1, u32 n)
1234 struct dsi *dsi = arg;
1235 unsigned char regs[2];
1242 rk32_dsi_set_bits(dsi, regs[0], dcs_sr_0p_tx);
1244 if(type == DTYPE_GEN_SWRITE_0P)
1245 data = (dsi->vid << 6) | (n << 4) | type;
1247 data = (dsi->vid << 6) | ((n-1) << 4) | type;
1250 data |= regs[1] << 8 | type;
1252 printk("write GEN_HDR:%08x\n", data);
1254 rk32_dsi_set_bits(dsi, 0xFFFF, bta_to_cnt);
1255 rk32_dsi_set_bits(dsi, 1, bta_en);
1256 rk32_dsi_set_bits(dsi, data, GEN_HDR);
1259 printk("rk32_mipi_dsi_read_dcs_packet==0x%x\n", rk32_dsi_get_bits(dsi, GEN_PLD_DATA));
1260 rk32_dsi_set_bits(dsi, 0, bta_en);
1265 static int rk32_mipi_dsi_power_up(void *arg)
1267 struct dsi *dsi = arg;
1269 rk32_phy_power_up(dsi);
1270 rk32_mipi_dsi_host_power_up(dsi);
1274 static int rk32_mipi_dsi_power_down(void *arg)
1276 struct dsi *dsi = arg;
1277 struct mipi_dsi_screen *screen = &dsi->screen;
1282 rk32_mipi_dsi_host_power_down(dsi);
1283 rk_phy_power_down(dsi);
1285 MIPI_TRACE("%s:%d\n", __func__, __LINE__);
1289 static int rk32_mipi_dsi_get_id(void *arg)
1292 struct dsi *dsi = arg;
1294 id = rk32_dsi_get_bits(dsi, VERSION);
1298 /* the most top level of mipi dsi init */
1299 static int rk_mipi_dsi_probe(struct dsi *dsi)
1303 register_dsi_ops(dsi->dsi_id, &dsi->ops);
1304 ret = dsi_probe_current_chip(dsi->dsi_id);
1306 MIPI_TRACE("mipi dsi probe fail\n");
1312 #ifdef MIPI_DSI_REGISTER_IO
1313 #include <linux/proc_fs.h>
1314 #include <asm/uaccess.h>
1315 #include <linux/slab.h>
1317 ssize_t reg_proc_write(struct file *file, const char __user *buff, size_t count, loff_t *offp)
1319 int ret = -1, i = 0;
1321 char *buf = kmalloc(count, GFP_KERNEL);
1326 memset(buf, 0, count);
1327 ret = copy_from_user((void *)buf, buff, count);
1328 data = strstr(data, "-");
1330 goto reg_proc_write_exit;
1331 command = *(++data);
1335 data = strstr(data, "0x");
1337 goto reg_proc_write_exit;
1338 sscanf(data, "0x%llx", ®s_val);
1339 if ((regs_val & 0xffff00000000ULL) == 0)
1340 goto reg_proc_write_exit;
1341 read_val = regs_val & 0xffffffff;
1342 printk("regs_val=0x%llx\n", regs_val);
1343 rk32_dsi_write_reg(dsi0, regs_val >> 32, &read_val);
1344 rk32_dsi_read_reg(dsi0, regs_val >> 32, &read_val);
1345 regs_val &= 0xffffffff;
1346 if (read_val != regs_val)
1347 MIPI_TRACE("%s fail:0x%08x\n", __func__, read_val);
1353 data = strstr(data, "0x");
1355 goto reg_proc_write_exit;
1357 sscanf(data, "0x%llx", ®s_val);
1358 rk32_dsi_read_reg(dsi0, (u16)regs_val, &read_val);
1359 MIPI_TRACE("*%04x : %08x\n", (u16)regs_val, read_val);
1363 while (*(++data) == ' ')
1365 sscanf(data, "%d", &read_val);
1367 read_val = 11289600;
1374 while (*(++data) == ' ')
1377 MIPI_TRACE("****%d:%d\n", data-buf, count);
1380 MIPI_TRACE("payload entry is larger than 32\n");
1383 sscanf(data, "%x,", (unsigned int *)(str + i)); /* -c 1,29,02,03,05,06,> pro */
1384 data = strstr(data, ",");
1395 rk32_mipi_dsi_send_packet(dsi0, str, read_val);
1397 rk32_mipi_dsi_send_packet(dsi0, str, read_val);
1408 reg_proc_write_exit:
1414 int reg_proc_read(struct seq_file *s, void *v)
1418 struct dsi *dsi = s->private;
1420 for (i = VERSION; i < (VERSION + (0xdc << 16)); i += 4<<16) {
1421 val = rk32_dsi_get_bits(dsi, i);
1422 seq_printf(s, "%04x: %08x\n", i>>16, val);
1426 static int reg_proc_open(struct inode *inode, struct file *file)
1428 struct dsi *dsi = inode->i_private;
1430 return single_open(file, reg_proc_read, dsi);
1433 int reg_proc_close(struct inode *inode, struct file *file)
1438 struct file_operations reg_proc_fops = {
1439 .owner = THIS_MODULE,
1440 .open = reg_proc_open,
1441 .release = reg_proc_close,
1442 .write = reg_proc_write,
1446 ssize_t reg_proc_write1(struct file *file, const char __user *buff, size_t count, loff_t *offp)
1448 int ret = -1, i = 0;
1450 char *buf = kmalloc(count, GFP_KERNEL);
1455 memset(buf, 0, count);
1456 ret = copy_from_user((void *)buf, buff, count);
1458 data = strstr(data, "-");
1460 goto reg_proc_write_exit;
1461 command = *(++data);
1466 data = strstr(data, "0x");
1468 goto reg_proc_write_exit;
1469 sscanf(data, "0x%llx", ®s_val);
1470 if ((regs_val & 0xffff00000000ULL) == 0)
1471 goto reg_proc_write_exit;
1472 read_val = regs_val & 0xffffffff;
1473 rk32_dsi_write_reg(dsi1, regs_val >> 32, &read_val);
1474 rk32_dsi_read_reg(dsi1, regs_val >> 32, &read_val);
1475 regs_val &= 0xffffffff;
1476 if (read_val != regs_val)
1477 MIPI_TRACE("%s fail:0x%08x\n", __func__, read_val);
1483 data = strstr(data, "0x");
1485 goto reg_proc_write_exit;
1486 sscanf(data, "0x%llx", ®s_val);
1487 rk32_dsi_read_reg(dsi1, (u16)regs_val, &read_val);
1488 MIPI_TRACE("*%04x : %08x\n", (u16)regs_val, read_val);
1492 while (*(++data) == ' ')
1494 sscanf(data, "%d", &read_val);
1496 read_val = 11289600;
1503 while (*(++data) == ' ')
1506 MIPI_TRACE("****%d:%d\n", data-buf, count);
1509 MIPI_TRACE("payload entry is larger than 32\n");
1512 sscanf(data, "%x,", (unsigned int *)(str + i)); /* -c 1,29,02,03,05,06,> pro */
1513 data = strstr(data, ",");
1524 rk32_mipi_dsi_send_packet(dsi1, str, read_val);
1526 rk32_mipi_dsi_send_packet(dsi1, str, read_val);
1537 reg_proc_write_exit:
1543 int reg_proc_close1(struct inode *inode, struct file *file)
1548 struct file_operations reg_proc_fops1 = {
1549 .owner = THIS_MODULE,
1550 .open = reg_proc_open,
1551 .release = reg_proc_close1,
1552 .write = reg_proc_write1,
1556 #if 0/* def CONFIG_MIPI_DSI_LINUX */
1557 static irqreturn_t rk32_mipi_dsi_irq_handler(int irq, void *data)
1559 printk("-------rk32_mipi_dsi_irq_handler-------\n");
1565 static int dwc_phy_test_rd(struct dsi *dsi, unsigned char test_code)
1569 rk32_dsi_set_bits(dsi, 0x10000 | test_code, PHY_TEST_CTRL1);
1570 rk32_dsi_set_bits(dsi, 0x2, PHY_TEST_CTRL0);
1571 rk32_dsi_set_bits(dsi, 0x0, PHY_TEST_CTRL0);
1573 val = rk32_dsi_get_bits(dsi, PHY_TEST_CTRL1);
1577 static int rk32_dsi_enable(void)
1579 MIPI_DBG("rk32_dsi_enable-------\n");
1580 if (!dsi0->clk_on) {
1582 rk_fb_get_prmry_screen(dsi0->screen.screen);
1583 dsi0->screen.lcdc_id = dsi0->screen.screen->lcdc_id;
1584 rk32_init_phy_mode(dsi0->screen.lcdc_id);
1587 if (rk_mipi_get_dsi_num() == 2)
1590 rk_mipi_screen_standby(0);
1592 /* After the core reset, DPI waits for the first VSYNC
1593 active transition to start signal sampling, including pixel data,
1594 and preventing image transmission in the middle of a frame.
1596 dsi_is_enable(0, 0);
1597 if (rk_mipi_get_dsi_num() == 2)
1598 dsi_is_enable(1, 0);
1600 dsi_enable_video_mode(0, 1);
1601 if (rk_mipi_get_dsi_num() == 2)
1602 dsi_enable_video_mode(1, 1);
1604 dsi_is_enable(0, 1);
1605 if (rk_mipi_get_dsi_num() == 2)
1606 dsi_is_enable(1, 1);
1611 #ifdef CONFIG_MIPI_DSI_LINUX
1612 static int rk32_dsi_disable(void)
1614 MIPI_DBG("rk32_dsi_disable-------\n");
1617 rk_mipi_screen_standby(1);
1619 if (rk_mipi_get_dsi_num() == 2)
1625 static struct rk_fb_trsm_ops trsm_dsi_ops = {
1626 .enable = rk32_dsi_enable,
1627 .disable = rk32_dsi_disable,
1628 .dsp_pwr_on = rk32_mipi_power_up_DDR,
1629 .dsp_pwr_off = rk32_mipi_power_down_DDR,
1632 static void rk32_init_phy_mode(int lcdc_id)
1634 int val0 = 0, val1 = 0;
1636 MIPI_DBG("rk32_init_phy_mode----------lcdc_id=%d\n", lcdc_id);
1638 /* Only the rk3288 VOP need setting the VOP output. */
1639 if (dsi0->ops.id != DWC_DSI_VERSION)
1642 /* D-PHY mode select */
1643 if (rk_mipi_get_dsi_num() == 1) {
1645 /* 1'b1: VOP LIT output to DSI host0;1'b0: VOP BIG output to DSI host0 */
1646 val0 = 0x1 << 22 | 0x1 << 6;
1648 val0 = 0x1 << 22 | 0x0 << 6;
1649 writel_relaxed(val0, RK_GRF_VIRT + RK3288_GRF_SOC_CON6);
1652 val0 = 0x1 << 25 | 0x1 << 9 | 0x1 << 22 | 0x1 << 6;
1653 val1 = 0x1 << 31 | 0x1 << 30 | 0x0 << 15 | 0x1 << 14;
1655 val0 = 0x1 << 25 | 0x0 << 9 | 0x1 << 22 | 0x0 << 14;
1656 val1 = 0x1 << 31 | 0x1 << 30 | 0x0 << 15 | 0x1 << 14;
1658 writel_relaxed(val0, RK_GRF_VIRT + RK3288_GRF_SOC_CON6);
1659 writel_relaxed(val1, RK_GRF_VIRT + RK3288_GRF_SOC_CON14);
1663 #ifdef CONFIG_MIPI_DSI_LINUX
1664 static int rk32_mipi_power_down_DDR(void)
1666 dsi_is_enable(0, 0);
1667 if (rk_mipi_get_dsi_num() == 2)
1668 dsi_is_enable(1, 0);
1672 static int rk32_mipi_power_up_DDR(void)
1674 dsi_is_enable(0, 0);
1675 if (rk_mipi_get_dsi_num() == 2)
1676 dsi_is_enable(1, 0);
1677 dsi_enable_video_mode(0, 1);
1678 dsi_enable_video_mode(1, 1);
1679 dsi_is_enable(0, 1);
1680 if (rk_mipi_get_dsi_num() == 2)
1681 dsi_is_enable(1, 1);
1690 static struct dsi_type dsi_rk312x = {
1691 .label = "rk312-dsi",
1692 .dsi_id = DWC_DSI_VERSION_RK312x,
1695 static struct dsi_type dsi_rk32 = {
1696 .label = "rk32-dsi",
1697 .dsi_id = DWC_DSI_VERSION,
1700 static struct dsi_type dsi_rk3368 = {
1701 .label = "rk3368-dsi",
1702 .dsi_id = DWC_DSI_VERSION_RK3368,
1705 static const struct of_device_id of_rk_mipi_dsi_match[] = {
1706 { .compatible = "rockchip,rk32-dsi", .data = &dsi_rk32},
1707 { .compatible = "rockchip,rk312x-dsi", .data = &dsi_rk312x},
1708 { .compatible = "rockchip,rk3368-dsi", .data = &dsi_rk3368},
1712 static int rk32_mipi_dsi_probe(struct platform_device *pdev)
1717 struct mipi_dsi_ops *ops;
1718 struct rk_screen *screen;
1719 struct mipi_dsi_screen *dsi_screen;
1720 struct resource *res_host, *res_phy;
1721 const struct dsi_type *data;
1722 const struct of_device_id *of_id =
1723 of_match_device(of_rk_mipi_dsi_match, &pdev->dev);
1725 dev_err(&pdev->dev, "failed to match device\n");
1730 dsi = devm_kzalloc(&pdev->dev, sizeof(struct dsi), GFP_KERNEL);
1732 dev_err(&pdev->dev, "request struct dsi fail!\n");
1735 dsi->ops.id = data->dsi_id;
1736 printk(KERN_INFO "%s\n", data->label);
1737 if (dsi->ops.id == DWC_DSI_VERSION) {
1738 res_host = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1739 dsi->host.membase = devm_request_and_ioremap(&pdev->dev, res_host);
1740 if (!dsi->host.membase) {
1741 dev_err(&pdev->dev, "get resource mipi host membase fail!\n");
1744 } else if (dsi->ops.id == DWC_DSI_VERSION_RK312x ||
1745 dsi->ops.id == DWC_DSI_VERSION_RK3368) {
1746 res_host = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mipi_dsi_host");
1747 dsi->host.membase = devm_request_and_ioremap(&pdev->dev, res_host);
1748 if (!dsi->host.membase) {
1749 dev_err(&pdev->dev, "get resource mipi host membase fail!\n");
1752 res_phy = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mipi_dsi_phy");
1753 dsi->phy.membase = devm_request_and_ioremap(&pdev->dev, res_phy);
1754 if (!dsi->phy.membase) {
1755 dev_err(&pdev->dev, "get resource mipi phy membase fail!\n");
1760 dsi->phy.refclk = devm_clk_get(&pdev->dev, "clk_mipi_24m");
1761 if (unlikely(IS_ERR(dsi->phy.refclk))) {
1762 dev_err(&pdev->dev, "get clk_mipi_24m clock fail\n");
1763 return PTR_ERR(dsi->phy.refclk);
1766 /* Get the APB bus clk access mipi phy */
1767 dsi->dsi_pclk = devm_clk_get(&pdev->dev, "pclk_mipi_dsi");
1768 if (unlikely(IS_ERR(dsi->dsi_pclk))) {
1769 dev_err(&pdev->dev, "get pclk_mipi_dsi clock fail\n");
1770 return PTR_ERR(dsi->dsi_pclk);
1773 if (dsi->ops.id == DWC_DSI_VERSION_RK3368) {
1774 /* Get the APB bus clk access mipi host */
1775 dsi->dsi_host_pclk = devm_clk_get(&pdev->dev, "pclk_mipi_dsi_host");
1776 if (unlikely(IS_ERR(dsi->dsi_host_pclk))) {
1777 dev_err(&pdev->dev, "get pclk_mipi_dsi_host clock fail\n");
1778 return PTR_ERR(dsi->dsi_host_pclk);
1781 dsi->dsi_pd = devm_clk_get(&pdev->dev, "pd_mipi_dsi");
1782 if (unlikely(IS_ERR(dsi->dsi_pd))) {
1783 dev_err(&pdev->dev, "get pd_mipi_dsi clock fail\n");
1784 return PTR_ERR(dsi->dsi_pd);
1788 if (dsi->ops.id == DWC_DSI_VERSION_RK312x) {
1789 /* Get the APB bus clk access mipi host */
1790 dsi->dsi_host_pclk = devm_clk_get(&pdev->dev, "pclk_mipi_dsi_host");
1791 if (unlikely(IS_ERR(dsi->dsi_host_pclk))) {
1792 dev_err(&pdev->dev, "get pclk_mipi_dsi_host clock fail\n");
1793 return PTR_ERR(dsi->dsi_host_pclk);
1795 /* Get the pd_vio AHB h2p bridge clock */
1796 dsi->h2p_hclk = devm_clk_get(&pdev->dev, "hclk_vio_h2p");
1797 if (unlikely(IS_ERR(dsi->h2p_hclk))) {
1798 dev_err(&pdev->dev, "get hclk_vio_h2p clock fail\n");
1799 return PTR_ERR(dsi->h2p_hclk);
1802 dsi->dsi_pd = devm_clk_get(&pdev->dev, "pd_mipi_dsi");
1803 if (unlikely(IS_ERR(dsi->dsi_pd))) {
1804 dev_err(&pdev->dev, "get pd_mipi_dsi clock fail\n");
1805 return PTR_ERR(dsi->dsi_pd);
1809 if (dsi->ops.id == DWC_DSI_VERSION) {
1810 dsi->dsi_pd = devm_clk_get(&pdev->dev, "pd_mipi_dsi");
1811 if (unlikely(IS_ERR(dsi->dsi_pd))) {
1812 dev_err(&pdev->dev, "get pd_mipi_dsi clock fail\n");
1813 return PTR_ERR(dsi->dsi_pd);
1816 dsi->host.irq = platform_get_irq(pdev, 0);
1817 if (dsi->host.irq < 0) {
1818 dev_err(&pdev->dev, "no irq resource?\n");
1819 return dsi->host.irq;
1822 ret = request_irq(dsi->host.irq, rk32_mipi_dsi_irq_handler, 0,dev_name(&pdev->dev), dsi);
1824 dev_err(&pdev->dev, "request mipi_dsi irq fail\n");
1828 printk("dsi->host.irq =%d\n", dsi->host.irq);
1830 disable_irq(dsi->host.irq);
1832 screen = devm_kzalloc(&pdev->dev, sizeof(struct rk_screen), GFP_KERNEL);
1834 dev_err(&pdev->dev, "request struct rk_screen fail!\n");
1837 rk_fb_get_prmry_screen(screen);
1843 ops->get_id = rk32_mipi_dsi_get_id,
1844 ops->dsi_send_packet = rk32_mipi_dsi_send_packet;
1845 ops->dsi_read_dcs_packet = rk32_mipi_dsi_read_dcs_packet,
1846 ops->dsi_enable_video_mode = rk32_mipi_dsi_enable_video_mode,
1847 ops->dsi_enable_command_mode = rk32_mipi_dsi_enable_command_mode,
1848 ops->dsi_enable_hs_clk = rk32_mipi_dsi_enable_hs_clk,
1849 ops->dsi_is_active = rk32_mipi_dsi_is_active,
1850 ops->dsi_is_enable = rk32_mipi_dsi_is_enable,
1851 ops->power_up = rk32_mipi_dsi_power_up,
1852 ops->power_down = rk32_mipi_dsi_power_down,
1853 ops->dsi_init = rk_mipi_dsi_init,
1855 dsi_screen = &dsi->screen;
1856 dsi_screen->screen = screen;
1857 dsi_screen->type = screen->type;
1858 dsi_screen->face = screen->face;
1859 dsi_screen->lcdc_id = screen->lcdc_id;
1860 dsi_screen->screen_id = screen->screen_id;
1861 dsi_screen->pixclock = screen->mode.pixclock;
1862 dsi_screen->left_margin = screen->mode.left_margin;
1863 dsi_screen->right_margin = screen->mode.right_margin;
1864 dsi_screen->hsync_len = screen->mode.hsync_len;
1865 dsi_screen->upper_margin = screen->mode.upper_margin;
1866 dsi_screen->lower_margin = screen->mode.lower_margin;
1867 dsi_screen->vsync_len = screen->mode.vsync_len;
1868 dsi_screen->x_res = screen->mode.xres;
1869 dsi_screen->y_res = screen->mode.yres;
1870 dsi_screen->pin_hsync = screen->pin_hsync;
1871 dsi_screen->pin_vsync = screen->pin_vsync;
1872 dsi_screen->pin_den = screen->pin_den;
1873 dsi_screen->pin_dclk = screen->pin_dclk;
1874 dsi_screen->dsi_lane = rk_mipi_get_dsi_lane();
1875 /* dsi_screen->dsi_video_mode = screen->dsi_video_mode; */
1876 dsi_screen->dsi_lane = rk_mipi_get_dsi_lane();
1877 dsi_screen->hs_tx_clk = rk_mipi_get_dsi_clk();
1878 /* dsi_screen->lcdc_id = 1; */
1882 sprintf(ops->name, "rk_mipi_dsi.%d", dsi->dsi_id);
1883 platform_set_drvdata(pdev, dsi);
1885 ret = rk_mipi_dsi_probe(dsi);
1887 dev_err(&pdev->dev, "rk mipi_dsi probe fail!\n");
1888 dev_err(&pdev->dev, "%s\n", RK_MIPI_DSI_VERSION_AND_TIME);
1894 if(!support_uboot_display())
1895 rk32_init_phy_mode(dsi_screen->lcdc_id);
1897 rk_fb_trsm_ops_register(&trsm_dsi_ops, SCREEN_MIPI);
1898 #ifdef MIPI_DSI_REGISTER_IO
1899 debugfs_create_file("mipidsi0", S_IFREG | S_IRUGO, dsi->debugfs_dir, dsi,
1905 #ifdef MIPI_DSI_REGISTER_IO
1906 debugfs_create_file("mipidsi1", S_IFREG | S_IRUGO, dsi->debugfs_dir, dsi,
1911 if (support_uboot_display()) {
1912 clk_prepare_enable(dsi->phy.refclk);
1913 clk_prepare_enable(dsi->dsi_pclk);
1914 if (dsi->ops.id == DWC_DSI_VERSION_RK312x) {
1915 clk_prepare_enable(dsi->dsi_host_pclk);
1916 clk_prepare_enable(dsi->h2p_hclk);
1917 } else if (dsi->ops.id == DWC_DSI_VERSION_RK3368)
1918 clk_prepare_enable(dsi->dsi_host_pclk);
1920 clk_prepare_enable(dsi->dsi_pd);
1925 dev_info(&pdev->dev, "rk mipi_dsi probe success!\n");
1926 dev_info(&pdev->dev, "%s\n", RK_MIPI_DSI_VERSION_AND_TIME);
1931 static struct platform_driver rk32_mipi_dsi_driver = {
1932 .probe = rk32_mipi_dsi_probe,
1934 .name = "rk32-mipi",
1935 .owner = THIS_MODULE,
1937 .of_match_table = of_rk_mipi_dsi_match,
1942 static int __init rk32_mipi_dsi_init(void)
1944 return platform_driver_register(&rk32_mipi_dsi_driver);
1946 fs_initcall(rk32_mipi_dsi_init);
1948 static void __exit rk32_mipi_dsi_exit(void)
1950 platform_driver_unregister(&rk32_mipi_dsi_driver);
1952 module_exit(rk32_mipi_dsi_exit);