clk: rockchip: support setting ddr clock via SCPI APIs
[firefly-linux-kernel-4.4.55.git] / drivers / video / rockchip / transmitter / rk32_mipi_dsi.c
1 /*
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.
10  *
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.
15  */
16
17 /* config */
18 #ifndef CONFIG_RK32_MIPI_DSI
19 #include <common.h>
20 #endif
21
22 #ifdef CONFIG_RK32_MIPI_DSI
23 #define MIPI_DSI_REGISTER_IO    0
24 #define CONFIG_MIPI_DSI_LINUX   0
25 #endif
26 #define DSI_RK3288              0x3288
27 #define DSI_RK312x              0x3128
28 #define DSI_RK3368              0x3368
29 #define DSI_RK3366              0x3366
30 #define DSI_RK3399              0x3399
31 #define DSI_ERR                 -1
32
33 #include <linux/kernel.h>
34 #include <linux/init.h>
35 #include <linux/platform_device.h>
36 #include <linux/slab.h>
37 #include <linux/rk_fb.h>
38 #include <linux/rk_screen.h>
39 #include <linux/delay.h>
40 #include <linux/clk.h>
41 #include <linux/interrupt.h>
42 #include <asm/div64.h>
43
44 #include <linux/fs.h>
45 #include <linux/debugfs.h>
46 #include <linux/seq_file.h>
47 #include <linux/regulator/machine.h>
48
49 #include <linux/dma-mapping.h>
50 #include "mipi_dsi.h"
51 #include "rk32_mipi_dsi.h"
52 #include <linux/rockchip/iomap.h>
53 #include <linux/rockchip/cpu.h>
54 #include<linux/mfd/syscon.h>
55 #include<linux/regmap.h>
56
57 #define MIPI_DBG(x...)  /* printk(KERN_INFO x) */
58
59 #ifdef CONFIG_MIPI_DSI_LINUX
60 #define MIPI_TRACE(x...)        /* printk(KERN_INFO x) */
61 #else
62 #define MIPI_TRACE(...)    \
63         do {\
64                 printf(__VA_ARGS__);\
65                 printf("\n");\
66         } while (0);
67
68 #endif
69
70 /*
71 *                        Driver Version Note
72 *
73 *v1.0 : this driver is rk32 mipi dsi driver of rockchip;
74 *v1.1 : add test eye pattern;
75 *
76 */
77
78 #define RK_MIPI_DSI_VERSION_AND_TIME  "rockchip mipi_dsi v1.1 2014-06-17"
79
80 static struct dsi *dsi0;
81 static struct dsi *dsi1;
82
83 static int rk32_mipi_dsi_is_active(void *arg);
84 static int rk32_mipi_dsi_enable_hs_clk(void *arg, u32 enable);
85 static int rk32_mipi_dsi_enable_video_mode(void *arg, u32 enable);
86 static int rk32_mipi_dsi_enable_command_mode(void *arg, u32 enable);
87 static int rk32_mipi_dsi_is_enable(void *arg, u32 enable);
88 static int rk32_mipi_power_down_DDR(void);
89 static int rk32_mipi_power_up_DDR(void);
90 int rk_mipi_screen_standby(u8 enable);
91
92 int rockchip_get_screen_type(void)
93 {
94         struct device_node *type_node;
95         struct device_node *childnode;
96         u32 val = 0;
97
98         type_node = of_find_node_by_name(NULL, "display-timings");
99         if (!type_node) {
100                 pr_err("could not find display-timings node\n");
101                 return -1;
102         }
103
104         for_each_child_of_node(type_node, childnode) {
105                 if (!of_property_read_u32(childnode, "screen-type", &val))
106                         return val;
107         }
108
109         return 0;
110 }
111
112 static int rk32_dsi_read_reg(struct dsi *dsi, u16 reg, u32 *pval)
113 {
114         if (dsi->ops.id == DSI_RK3288 ||
115             dsi->ops.id == DSI_RK3399)
116                 *pval = __raw_readl(dsi->host.membase + (reg - MIPI_DSI_HOST_OFFSET));
117         else if (dsi->ops.id == DSI_RK312x ||
118                  dsi->ops.id == DSI_RK3368 ||
119                  dsi->ops.id == DSI_RK3366) {
120                 if (reg >= MIPI_DSI_HOST_OFFSET)
121                         *pval = __raw_readl(dsi->host.membase + (reg - MIPI_DSI_HOST_OFFSET));
122                 else if (reg >= MIPI_DSI_PHY_OFFSET)
123                         *pval = __raw_readl(dsi->phy.membase + (reg - MIPI_DSI_PHY_OFFSET));
124         }
125         return 0;
126 }
127
128 static int rk32_dsi_write_reg(struct dsi *dsi, u16 reg, u32 *pval)
129 {
130         if (dsi->ops.id == DSI_RK3288 ||
131             dsi->ops.id == DSI_RK3399)
132                 __raw_writel(*pval, dsi->host.membase + (reg - MIPI_DSI_HOST_OFFSET));
133         else if (dsi->ops.id == DSI_RK312x ||
134                  dsi->ops.id == DSI_RK3368 ||
135                  dsi->ops.id == DSI_RK3366) {
136                 if (reg >= MIPI_DSI_HOST_OFFSET)
137                         __raw_writel(*pval, dsi->host.membase + (reg - MIPI_DSI_HOST_OFFSET));
138                 else if (reg >= MIPI_DSI_PHY_OFFSET)
139                         __raw_writel(*pval, dsi->phy.membase + (reg - MIPI_DSI_PHY_OFFSET));
140         }
141         return 0;
142 }
143
144 static int rk32_dsi_get_bits(struct dsi *dsi, u32 reg)
145 {
146         u32 val = 0;
147         u32 bits = (reg >> 8) & 0xff;
148         u16 reg_addr = (reg >> 16) & 0xffff;
149         u8 offset = reg & 0xff;
150
151         if (bits < 32)
152                 bits = (1 << bits) - 1;
153         else
154                 bits = 0xffffffff;
155
156         rk32_dsi_read_reg(dsi, reg_addr, &val);
157         val >>= offset;
158         val &= bits;
159         return val;
160 }
161
162 static int rk32_dsi_set_bits(struct dsi *dsi, u32 data, u32 reg)
163 {
164         static u32 val;
165         u32 bits = (reg >> 8) & 0xff;
166         u16 reg_addr = (reg >> 16) & 0xffff;
167         u8 offset = reg & 0xff;
168
169         if (bits < 32)
170                 bits = (1 << bits) - 1;
171         else
172                 bits = 0xffffffff;
173
174         if (bits != 0xffffffff)
175                 rk32_dsi_read_reg(dsi, reg_addr, &val);
176
177         val &= ~(bits << offset);
178         val |= (data & bits) << offset;
179         rk32_dsi_write_reg(dsi, reg_addr, &val);
180
181         if (data > bits) {
182                 MIPI_TRACE("%s error reg_addr:0x%04x, offset:%d, bits:0x%04x, value:0x%04x\n",
183                                 __func__, reg_addr, offset, bits, data);
184         }
185         return 0;
186 }
187 #if 0
188 static int rk32_dwc_phy_test_rd(struct dsi *dsi, unsigned char test_code)
189 {
190         int val = 0;
191         rk32_dsi_set_bits(dsi, 1, phy_testclk);
192         rk32_dsi_set_bits(dsi, test_code, phy_testdin);
193         rk32_dsi_set_bits(dsi, 1, phy_testen);
194         rk32_dsi_set_bits(dsi, 0, phy_testclk);
195         rk32_dsi_set_bits(dsi, 0, phy_testen);;
196
197         rk32_dsi_set_bits(dsi, 0, phy_testen);
198         val = rk32_dsi_get_bits(dsi, phy_testdout);
199         rk32_dsi_set_bits(dsi, 1, phy_testclk);
200         rk32_dsi_set_bits(dsi, 0, phy_testclk);
201
202         return val;
203 }
204 #endif
205 static int rk32_dwc_phy_test_wr(struct dsi *dsi, unsigned char test_code, unsigned char *test_data, unsigned char size)
206 {
207         int i = 0;
208
209         MIPI_DBG("test_code=0x%x,test_data=0x%x\n", test_code, test_data[0]);
210         rk32_dsi_set_bits(dsi, 0x10000 | test_code, PHY_TEST_CTRL1);
211         rk32_dsi_set_bits(dsi, 0x2, PHY_TEST_CTRL0);
212         rk32_dsi_set_bits(dsi, 0x0, PHY_TEST_CTRL0);
213
214         for (i = 0; i < size; i++) {
215                 rk32_dsi_set_bits(dsi, test_data[i], PHY_TEST_CTRL1);
216                 rk32_dsi_set_bits(dsi, 0x2, PHY_TEST_CTRL0);
217                 rk32_dsi_set_bits(dsi, 0x0, PHY_TEST_CTRL0);
218                 MIPI_DBG("rk32_dwc_phy_test_wr:%08x\n", rk32_dsi_get_bits(dsi, PHY_TEST_CTRL1));
219         }
220         return 0;
221 }
222
223 static int rk32_phy_power_up(struct dsi *dsi)
224 {
225         /* enable ref clock */
226         clk_prepare_enable(dsi->phy.refclk);
227         clk_prepare_enable(dsi->dsi_pclk);
228         if (dsi->ops.id == DSI_RK3399)
229                 clk_prepare_enable(dsi->dsi_host_pclk);
230         udelay(10);
231
232         switch (dsi->host.lane) {
233         case 4:
234                 rk32_dsi_set_bits(dsi, 3, n_lanes);
235                 break;
236         case 3:
237                 rk32_dsi_set_bits(dsi, 2, n_lanes);
238                 break;
239         case 2:
240                 rk32_dsi_set_bits(dsi, 1, n_lanes);
241                 break;
242         case 1:
243                 rk32_dsi_set_bits(dsi, 0, n_lanes);
244                 break;
245         default:
246                 break;
247         }
248         rk32_dsi_set_bits(dsi, 1, phy_shutdownz);
249         rk32_dsi_set_bits(dsi, 1, phy_rstz);
250         rk32_dsi_set_bits(dsi, 1, phy_enableclk);
251         rk32_dsi_set_bits(dsi, 1, phy_forcepll);
252
253         return 0;
254 }
255
256 static int rk312x_mipi_dsi_phy_set_gotp(struct dsi *dsi, u32 offset, int n)
257 {
258         u32 val = 0, temp = 0, Tlpx = 0;
259         u32 ddr_clk = dsi->phy.ddr_clk;
260         u32 Ttxbyte_clk = dsi->phy.Ttxbyte_clk;
261         u32 Tsys_clk = dsi->phy.Tsys_clk;
262         u32 Ttxclkesc = dsi->phy.Ttxclkesc;
263         printk("%s : ddr_clk %d\n", __func__, ddr_clk);
264         switch (offset) {
265         case DPHY_CLOCK_OFFSET:
266                 MIPI_DBG("******set DPHY_CLOCK_OFFSET gotp******\n");
267                 break;
268         case DPHY_LANE0_OFFSET:
269                 MIPI_DBG("******set DPHY_LANE0_OFFSET gotp******\n");
270                 break;
271         case DPHY_LANE1_OFFSET:
272                 MIPI_DBG("******set DPHY_LANE1_OFFSET gotp******\n");
273                 break;
274         case DPHY_LANE2_OFFSET:
275                 MIPI_DBG("******set DPHY_LANE2_OFFSET gotp******\n");
276                 break;
277         case DPHY_LANE3_OFFSET:
278                 MIPI_DBG("******set DPHY_LANE3_OFFSET gotp******\n");
279                 break;
280         default:
281                 break;
282         }
283
284         if (ddr_clk < 110 * MHz)
285                 val = 0;
286         else if (ddr_clk < 150 * MHz)
287                 val = 1;
288         else if (ddr_clk < 200 * MHz)
289                 val = 2;
290         else if (ddr_clk < 250 * MHz)
291                 val = 3;
292         else if (ddr_clk < 300 * MHz)
293                 val = 4;
294         else if (ddr_clk < 400 * MHz)
295                 val = 5;
296         else if (ddr_clk < 500 * MHz)
297                 val = 6;
298         else if (ddr_clk < 600 * MHz)
299                 val = 7;
300         else if (ddr_clk < 700 * MHz)
301                 val = 8;
302         else if (ddr_clk < 800 * MHz)
303                 val = 9;
304         else if (ddr_clk <= 1000 * MHz)
305                 val = 10;
306         printk("%s reg_ths_settle = 0x%x\n", __func__, val);
307         rk32_dsi_set_bits(dsi, val, reg_ths_settle + offset);
308
309         if (ddr_clk < 110 * MHz)
310                 val = 0x20;
311         else if (ddr_clk < 150 * MHz)
312                 val = 0x06;
313         else if (ddr_clk < 200 * MHz)
314                 val = 0x18;
315         else if (ddr_clk < 250 * MHz)
316                 val = 0x05;
317         else if (ddr_clk < 300 * MHz)
318                 val = 0x51;
319         else if (ddr_clk < 400 * MHz)
320                 val = 0x64;
321         else if (ddr_clk < 500 * MHz)
322                 val = 0x59;
323         else if (ddr_clk < 600 * MHz)
324                 val = 0x6a;
325         else if (ddr_clk < 700 * MHz)
326                 val = 0x3e;
327         else if (ddr_clk < 800 * MHz)
328                 val = 0x21;
329         else if (ddr_clk <= 1000 * MHz)
330                 val = 0x09;
331         printk("%s reg_hs_ths_prepare = 0x%x\n", __func__, val);
332         rk32_dsi_set_bits(dsi, val, reg_hs_ths_prepare + offset);
333
334         if (offset != DPHY_CLOCK_OFFSET) {
335                 if (ddr_clk < 110 * MHz)
336                         val = 2;
337                 else if (ddr_clk < 150 * MHz)
338                         val = 3;
339                 else if (ddr_clk < 200 * MHz)
340                         val = 4;
341                 else if (ddr_clk < 250 * MHz)
342                         val = 5;
343                 else if (ddr_clk < 300 * MHz)
344                         val = 6;
345                 else if (ddr_clk < 400 * MHz)
346                         val = 7;
347                 else if (ddr_clk < 500 * MHz)
348                         val = 7;
349                 else if (ddr_clk < 600 * MHz)
350                         val = 8;
351                 else if (ddr_clk < 700 * MHz)
352                         val = 8;
353                 else if (ddr_clk < 800 * MHz)
354                         val = 9;
355                 else if (ddr_clk <= 1000 * MHz)
356                         val = 9;
357         } else {
358                 if (ddr_clk < 110 * MHz)
359                         val = 0x16;
360                 else if (ddr_clk < 150 * MHz)
361                         val = 0x16;
362                 else if (ddr_clk < 200 * MHz)
363                         val = 0x17;
364                 else if (ddr_clk < 250 * MHz)
365                         val = 0x17;
366                 else if (ddr_clk < 300 * MHz)
367                         val = 0x18;
368                 else if (ddr_clk < 400 * MHz)
369                         val = 0x19;
370                 else if (ddr_clk < 500 * MHz)
371                         val = 0x1b;
372                 else if (ddr_clk < 600 * MHz)
373                         val = 0x1d;
374                 else if (ddr_clk < 700 * MHz)
375                         val = 0x1e;
376                 else if (ddr_clk < 800 * MHz)
377                         val = 0x1f;
378                 else if (ddr_clk <= 1000 * MHz)
379                         val = 0x20;
380         }
381         printk("%s reg_hs_the_zero = 0x%x\n", __func__, val);
382         rk32_dsi_set_bits(dsi, val, reg_hs_the_zero + offset);
383
384         if (ddr_clk < 110 * MHz)
385                 val = 0x22;
386         else if (ddr_clk < 150 * MHz)
387                 val = 0x45;
388         else if (ddr_clk < 200 * MHz)
389                 val = 0x0b;
390         else if (ddr_clk < 250 * MHz)
391                 val = 0x16;
392         else if (ddr_clk < 300 * MHz)
393                 val = 0x2c;
394         else if (ddr_clk < 400 * MHz)
395                 val = 0x33;
396         else if (ddr_clk < 500 * MHz)
397                 val = 0x4e;
398         else if (ddr_clk < 600 * MHz)
399                 val = 0x3a;
400         else if (ddr_clk < 700 * MHz)
401                 val = 0x6a;
402         else if (ddr_clk < 800 * MHz)
403                 val = 0x29;
404         else if (ddr_clk <= 1000 * MHz)
405                 val = 0x21;     /* 0x27 */
406
407         printk("%s reg_hs_ths_trail = 0x%x\n", __func__, val);
408
409         rk32_dsi_set_bits(dsi, val, reg_hs_ths_trail + offset);
410         val = 120000 / Ttxbyte_clk + 1;
411         MIPI_DBG("reg_hs_ths_exit: %d, %d\n", val, val*Ttxbyte_clk/1000);
412         rk32_dsi_set_bits(dsi, val, reg_hs_ths_exit + offset);
413
414         if (offset == DPHY_CLOCK_OFFSET) {
415                 val = (60000 + 52*dsi->phy.UI) / Ttxbyte_clk + 1;
416                 MIPI_DBG("reg_hs_tclk_post: %d, %d\n", val, val*Ttxbyte_clk/1000);
417                 rk32_dsi_set_bits(dsi, val, reg_hs_tclk_post + offset);
418                 val = 10*dsi->phy.UI / Ttxbyte_clk + 1;
419                 MIPI_DBG("reg_hs_tclk_pre: %d, %d\n", val, val*Ttxbyte_clk/1000);
420                 rk32_dsi_set_bits(dsi, val, reg_hs_tclk_pre + offset);
421         }
422
423         val = 1010000000 / Tsys_clk + 1;
424         MIPI_DBG("reg_hs_twakup: %d, %d\n", val, val*Tsys_clk/1000);
425         if (val > 0x3ff) {
426                 val = 0x2ff;
427                 MIPI_DBG("val is too large, 0x3ff is the largest\n");
428         }
429         temp = (val >> 8) & 0x03;
430         val &= 0xff;
431         rk32_dsi_set_bits(dsi, temp, reg_hs_twakup_h + offset);
432         rk32_dsi_set_bits(dsi, val, reg_hs_twakup_l + offset);
433
434         if (Ttxclkesc > 50000) {
435                 val = 2*Ttxclkesc;
436                 MIPI_DBG("Ttxclkesc:%d\n", Ttxclkesc);
437         }
438         val = val / Ttxbyte_clk;
439         Tlpx = val*Ttxbyte_clk;
440         MIPI_DBG("reg_hs_tlpx: %d, %d\n", val, Tlpx);
441         val -= 2;
442         rk32_dsi_set_bits(dsi, val, reg_hs_tlpx + offset);
443
444         Tlpx = 2*Ttxclkesc;
445         val = 4*Tlpx / Ttxclkesc;
446         MIPI_DBG("reg_hs_tta_go: %d, %d\n", val, val*Ttxclkesc);
447         rk32_dsi_set_bits(dsi, val, reg_hs_tta_go + offset);
448         val = 3 * Tlpx / 2 / Ttxclkesc;
449         MIPI_DBG("reg_hs_tta_sure: %d, %d\n", val, val*Ttxclkesc);
450         rk32_dsi_set_bits(dsi, val, reg_hs_tta_sure + offset);
451         val = 5 * Tlpx / Ttxclkesc;
452         MIPI_DBG("reg_hs_tta_wait: %d, %d\n", val, val*Ttxclkesc);
453         rk32_dsi_set_bits(dsi, val, reg_hs_tta_wait + offset);
454         return 0;
455 }
456
457 static void rk312x_mipi_dsi_set_hs_clk(struct dsi *dsi)
458 {
459         rk32_dsi_set_bits(dsi, dsi->phy.prediv, reg_prediv);
460         rk32_dsi_set_bits(dsi, dsi->phy.fbdiv & 0xff, reg_fbdiv);
461         rk32_dsi_set_bits(dsi, (dsi->phy.fbdiv >> 8) & 0x01, reg_fbdiv_8);
462 }
463
464 static int rk312x_phy_power_up(struct dsi *dsi)
465 {
466         /* enable ref clock */
467         clk_prepare_enable(dsi->phy.refclk);
468         clk_prepare_enable(dsi->dsi_pclk);
469         clk_prepare_enable(dsi->dsi_host_pclk);
470         if (dsi->ops.id == DSI_RK312x)
471                 clk_prepare_enable(dsi->h2p_hclk);
472
473         udelay(10);
474
475         rk312x_mipi_dsi_set_hs_clk(dsi);
476         rk32_dsi_set_bits(dsi, 0xe4, DPHY_REGISTER1);
477         switch (dsi->host.lane) {
478         case 4:
479                 rk32_dsi_set_bits(dsi, 1, lane_en_3);
480         case 3:
481                 rk32_dsi_set_bits(dsi, 1, lane_en_2);
482         case 2:
483                 rk32_dsi_set_bits(dsi, 1, lane_en_1);
484         case 1:
485                 rk32_dsi_set_bits(dsi, 1, lane_en_0);
486                 rk32_dsi_set_bits(dsi, 1, lane_en_ck);
487                 break;
488         default:
489                 break;
490         }
491
492         rk32_dsi_set_bits(dsi, 0xe0, DPHY_REGISTER1);
493         udelay(10);
494
495         rk32_dsi_set_bits(dsi, 0x1e, DPHY_REGISTER20);
496         rk32_dsi_set_bits(dsi, 0x1f, DPHY_REGISTER20);
497
498         rk32_dsi_set_bits(dsi, 1, phy_enableclk);
499
500         return 0;
501 }
502
503 static int rk_phy_power_up(struct dsi *dsi)
504 {
505         if (dsi->ops.id == DSI_RK3288 ||
506             dsi->ops.id == DSI_RK3399)
507                 rk32_phy_power_up(dsi);
508         else if (dsi->ops.id == DSI_RK312x ||
509                  dsi->ops.id == DSI_RK3368 ||
510                  dsi->ops.id == DSI_RK3366)
511                 rk312x_phy_power_up(dsi);
512         return 0;
513 }
514
515 static int rk32_phy_power_down(struct dsi *dsi)
516 {
517         rk32_dsi_set_bits(dsi, 0, phy_shutdownz);
518         clk_disable_unprepare(dsi->phy.refclk);
519         clk_disable_unprepare(dsi->dsi_pclk);
520         if (dsi->ops.id == DSI_RK3399)
521                 clk_disable_unprepare(dsi->dsi_host_pclk);
522         return 0;
523 }
524
525 static int rk312x_phy_power_down(struct dsi *dsi)
526 {
527         rk32_dsi_set_bits(dsi, 0x01, DPHY_REGISTER0);
528         rk32_dsi_set_bits(dsi, 0xe3, DPHY_REGISTER1);
529
530         clk_disable_unprepare(dsi->phy.refclk);
531         clk_disable_unprepare(dsi->dsi_pclk);
532         clk_disable_unprepare(dsi->dsi_host_pclk);
533
534         if (dsi->ops.id == DSI_RK312x)
535                 clk_disable_unprepare(dsi->h2p_hclk);
536
537         return 0;
538 }
539
540 static int rk_phy_power_down(struct dsi *dsi)
541 {
542         if (dsi->ops.id == DSI_RK3288 ||
543             dsi->ops.id == DSI_RK3399)
544                 rk32_phy_power_down(dsi);
545         else if (dsi->ops.id == DSI_RK312x ||
546                  dsi->ops.id == DSI_RK3368 ||
547                  dsi->ops.id == DSI_RK3366)
548                 rk312x_phy_power_down(dsi);
549         return 0;
550 }
551
552 static int rk32_phy_init(struct dsi *dsi)
553 {
554         u32 val = 0 , ddr_clk = 0, fbdiv = 0, prediv = 0;
555         unsigned char test_data[2] = {0};
556
557         ddr_clk = dsi->phy.ddr_clk;
558         prediv  = dsi->phy.prediv;
559         fbdiv   = dsi->phy.fbdiv;
560
561         if (ddr_clk < 200 * MHz)
562                 val = 0x0;
563         else if (ddr_clk < 300 * MHz)
564                 val = 0x1;
565         else if (ddr_clk < 500 * MHz)
566                 val = 0x2;
567         else if (ddr_clk < 700 * MHz)
568                 val = 0x3;
569         else if (ddr_clk < 900 * MHz)
570                 val = 0x4;
571         else if (ddr_clk < 1100 * MHz)
572                 val = 0x5;
573         else if (ddr_clk < 1300 * MHz)
574                 val = 0x6;
575         else if (ddr_clk <= 1500 * MHz)
576                 val = 0x7;
577
578         test_data[0] = 0x80 | val << 3 | 0x3;
579         rk32_dwc_phy_test_wr(dsi, 0x10, test_data, 1);
580
581         test_data[0] = 0x8;
582         rk32_dwc_phy_test_wr(dsi, 0x11, test_data, 1);
583
584         test_data[0] = 0x80 | 0x40;
585         rk32_dwc_phy_test_wr(dsi, 0x12, test_data, 1);
586
587         if (ddr_clk < 90 * MHz)
588                 val = 0x01;
589         else if (ddr_clk < 100 * MHz)
590                 val = 0x10;
591         else if (ddr_clk < 110 * MHz)
592                 val = 0x20;
593         else if (ddr_clk < 130 * MHz)
594                 val = 0x01;
595         else if (ddr_clk < 140 * MHz)
596                 val = 0x11;
597         else if (ddr_clk < 150 * MHz)
598                 val = 0x21;
599         else if (ddr_clk < 170 * MHz)
600                 val = 0x02;
601         else if (ddr_clk < 180 * MHz)
602                 val = 0x12;
603         else if (ddr_clk < 200 * MHz)
604                 val = 0x22;
605         else if (ddr_clk < 220 * MHz)
606                 val = 0x03;
607         else if (ddr_clk < 240 * MHz)
608                 val = 0x13;
609         else if (ddr_clk < 250 * MHz)
610                 val = 0x23;
611         else if (ddr_clk < 270 * MHz)
612                 val = 0x04;
613         else if (ddr_clk < 300 * MHz)
614                 val = 0x14;
615         else if (ddr_clk < 330 * MHz)
616                 val = 0x05;
617         else if (ddr_clk < 360 * MHz)
618                 val = 0x15;
619         else if (ddr_clk < 400 * MHz)
620                 val = 0x25;
621         else if (ddr_clk < 450 * MHz)
622                 val = 0x06;
623         else if (ddr_clk < 500 * MHz)
624                 val = 0x16;
625         else if (ddr_clk < 550 * MHz)
626                 val = 0x07;
627         else if (ddr_clk < 600 * MHz)
628                 val = 0x17;
629         else if (ddr_clk < 650 * MHz)
630                 val = 0x08;
631         else if (ddr_clk < 700 * MHz)
632                 val = 0x18;
633         else if (ddr_clk < 750 * MHz)
634                 val = 0x09;
635         else if (ddr_clk < 800 * MHz)
636                 val = 0x19;
637         else if (ddr_clk < 850 * MHz)
638                 val = 0x29;
639         else if (ddr_clk < 900 * MHz)
640                 val = 0x39;
641         else if (ddr_clk < 950 * MHz)
642                 val = 0x0a;
643         else if (ddr_clk < 1000 * MHz)
644                 val = 0x1a;
645         else if (ddr_clk < 1050 * MHz)
646                 val = 0x2a;
647         else if (ddr_clk < 1100 * MHz)
648                 val = 0x3a;
649         else if (ddr_clk < 1150 * MHz)
650                 val = 0x0b;
651         else if (ddr_clk < 1200 * MHz)
652                 val = 0x1b;
653         else if (ddr_clk < 1250 * MHz)
654                 val = 0x2b;
655         else if (ddr_clk < 1300 * MHz)
656                 val = 0x3b;
657         else if (ddr_clk < 1350 * MHz)
658                 val = 0x0c;
659         else if (ddr_clk < 1400 * MHz)
660                 val = 0x1c;
661         else if (ddr_clk < 1450 * MHz)
662                 val = 0x2c;
663         else if (ddr_clk <= 1500 * MHz)
664                 val = 0x3c;
665
666         test_data[0] = val << 1;
667         rk32_dwc_phy_test_wr(dsi, code_hs_rx_lane0, test_data, 1);
668
669         test_data[0] = prediv - 1;
670         rk32_dwc_phy_test_wr(dsi, code_pll_input_div_rat, test_data, 1);
671         mdelay(2);
672         test_data[0] = (fbdiv - 1) & 0x1f;
673         rk32_dwc_phy_test_wr(dsi, code_pll_loop_div_rat, test_data, 1);
674         mdelay(2);
675         test_data[0] = (fbdiv - 1) >> 5 | 0x80;
676         rk32_dwc_phy_test_wr(dsi, code_pll_loop_div_rat, test_data, 1);
677         mdelay(2);
678         test_data[0] = 0x30;
679         rk32_dwc_phy_test_wr(dsi, code_pll_input_loop_div_rat, test_data, 1);
680         mdelay(2);
681
682         test_data[0] = 0x4d;
683         rk32_dwc_phy_test_wr(dsi, 0x20, test_data, 1);
684
685         test_data[0] = 0x3d;
686         rk32_dwc_phy_test_wr(dsi, 0x21, test_data, 1);
687
688         test_data[0] = 0xdf;
689         rk32_dwc_phy_test_wr(dsi, 0x21, test_data, 1);
690
691         test_data[0] =  0x7;
692         rk32_dwc_phy_test_wr(dsi, 0x22, test_data, 1);
693
694         test_data[0] = 0x80 | 0x7;
695         rk32_dwc_phy_test_wr(dsi, 0x22, test_data, 1);
696
697         test_data[0] = 0x80 | 15;
698         rk32_dwc_phy_test_wr(dsi, code_hstxdatalanerequsetstatetime, test_data, 1);
699
700         test_data[0] = 0x80 | 85;
701         rk32_dwc_phy_test_wr(dsi, code_hstxdatalanepreparestatetime, test_data, 1);
702
703         test_data[0] = 0x40 | 10;
704         rk32_dwc_phy_test_wr(dsi, code_hstxdatalanehszerostatetime, test_data, 1);
705
706         return 0;
707 }
708
709 static int rk312x_phy_init(struct dsi *dsi, int n)
710 {
711         /* DPHY init */
712         if (dsi->ops.id == DSI_RK3366) {
713                 rk32_dsi_set_bits(dsi, 0x00, DSI_DPHY_BITS(0x06 << 2, 32, 0));
714                 rk32_dsi_set_bits(dsi, 0x00, DSI_DPHY_BITS(0x07 << 2, 32, 0));
715         } else {
716                 rk32_dsi_set_bits(dsi, 0x11, DSI_DPHY_BITS(0x06 << 2, 32, 0));
717                 rk32_dsi_set_bits(dsi, 0x11, DSI_DPHY_BITS(0x07 << 2, 32, 0));
718         }
719         rk32_dsi_set_bits(dsi, 0xcc, DSI_DPHY_BITS(0x09<<2, 32, 0));
720 #if 0
721         dsi_set_bits(0x4e, DSI_DPHY_BITS(0x08<<2, 32, 0));
722         dsi_set_bits(0x84, DSI_DPHY_BITS(0x0a<<2, 32, 0));
723 #endif
724
725         /*reg1[4] 0: enable a function of "pll phase for serial data being captured
726                                  inside analog part"
727                   1: disable it
728         we disable it here because reg5[6:4] is not compatible with the HS speed.
729         */
730
731         if (dsi->phy.ddr_clk >= 800*MHz) {
732                 if (dsi->ops.id == DSI_RK3366) {
733                         rk32_dsi_set_bits(dsi, 0x00,
734                                           DSI_DPHY_BITS(0x05 << 2, 32, 0));
735                 } else if (dsi->ops.id == DSI_RK3368) {
736                         rk32_dsi_set_bits(dsi, 0x10, DSI_DPHY_BITS(0x05<<2, 32, 0));
737                 } else {
738                         rk32_dsi_set_bits(dsi, 0x30, DSI_DPHY_BITS(0x05<<2, 32, 0));
739                 }
740         } else {
741                 rk32_dsi_set_bits(dsi, 1, reg_da_ppfc);
742         }
743
744         switch  (dsi->host.lane) {
745         case 4:
746                 rk312x_mipi_dsi_phy_set_gotp(dsi, DPHY_LANE3_OFFSET, n);
747         case 3:
748                 rk312x_mipi_dsi_phy_set_gotp(dsi, DPHY_LANE2_OFFSET, n);
749         case 2:
750                 rk312x_mipi_dsi_phy_set_gotp(dsi, DPHY_LANE1_OFFSET, n);
751         case 1:
752                 rk312x_mipi_dsi_phy_set_gotp(dsi, DPHY_LANE0_OFFSET, n);
753                 rk312x_mipi_dsi_phy_set_gotp(dsi, DPHY_CLOCK_OFFSET, n);
754                 break;
755         default:
756                 break;
757         }
758 /*
759         rk32_dsi_set_bits(dsi, 0x00e4, reg1_phy);
760         rk32_dsi_set_bits(dsi, 0x007d, reg0_phy);
761         udelay(1000);
762         rk32_dsi_set_bits(dsi, 0x00e0, reg1_phy);
763         rk32_dsi_set_bits(dsi, 0x001e, reg20_phy);
764         udelay(1000);
765         rk32_dsi_set_bits(dsi, 0x001f, reg20_phy);
766
767         rk32_dsi_set_bits(dsi, 0x0063, reg10_phy);
768         */
769         if (dsi->ops.id == DSI_RK3366) {
770                 /* increasing the driver strength */
771                 rk32_dsi_set_bits(dsi, 0x4f, reg8_phy);
772                 rk32_dsi_set_bits(dsi, 0x5f, regb_phy);
773                 /* increasing the slew rate */
774                 rk32_dsi_set_bits(dsi, 0xc6, rega_phy);
775         } else if (dsi->ops.id == DSI_RK3368) {
776                 rk32_dsi_set_bits(dsi, 0x1, reg5_phy);
777                 rk32_dsi_set_bits(dsi, 0x9, regb_0_3_phy);
778         } else {
779                 rk32_dsi_set_bits(dsi, 0x6, reg5_phy);
780                 rk32_dsi_set_bits(dsi, 0x9, regb_0_3_phy);
781         }
782         rk32_dsi_set_bits(dsi, 0x6, reg10_4_6_phy);
783
784         return 0;
785 }
786
787 static int rk_phy_init(struct dsi *dsi)
788 {
789         if (dsi->ops.id == DSI_RK3288 ||
790             dsi->ops.id == DSI_RK3399)
791                 rk32_phy_init(dsi);
792         else if (dsi->ops.id == DSI_RK312x ||
793                  dsi->ops.id == DSI_RK3368 ||
794                  dsi->ops.id == DSI_RK3366)
795                 rk312x_phy_init(dsi, 4);
796         return 0;
797 }
798
799 static int rk32_mipi_dsi_host_power_up(struct dsi *dsi)
800 {
801         int ret = 0;
802         u32 val;
803
804         /* disable all interrupt */
805         rk32_dsi_set_bits(dsi, 0x1fffff, INT_MKS0);
806         rk32_dsi_set_bits(dsi, 0x3ffff, INT_MKS1);
807
808         rk32_mipi_dsi_is_enable(dsi, 1);
809
810         val = 10;
811         while (!rk32_dsi_get_bits(dsi, phylock) && val--) {
812                 udelay(10);
813         };
814
815         if (val == 0) {
816                 ret = -1;
817                 MIPI_TRACE("%s:phylock fail.\n", __func__);
818         }
819
820         val = 10;
821         while (!rk32_dsi_get_bits(dsi, phystopstateclklane) && val--) {
822                 udelay(10);
823         };
824
825         return ret;
826 }
827
828 static int rk32_mipi_dsi_host_power_down(struct dsi *dsi)
829 {
830         rk32_mipi_dsi_enable_video_mode(dsi, 0);
831         rk32_mipi_dsi_enable_hs_clk(dsi, 0);
832         rk32_mipi_dsi_is_enable(dsi, 0);
833         return 0;
834 }
835
836 static int rk32_mipi_dsi_host_init(struct dsi *dsi)
837 {
838         u32 val = 0, bytes_px = 0;
839         struct mipi_dsi_screen *screen = &dsi->screen;
840         u32 decimals = dsi->phy.Ttxbyte_clk, temp = 0, i = 0;
841         u32 m = 1, lane = dsi->host.lane, Tpclk = dsi->phy.Tpclk,
842                         Ttxbyte_clk = dsi->phy.Ttxbyte_clk;
843
844         rk32_dsi_set_bits(dsi, dsi->host.lane - 1, n_lanes);
845         rk32_dsi_set_bits(dsi, dsi->vid, dpi_vcid);
846
847         switch (screen->face) {
848         case OUT_P888:
849                 rk32_dsi_set_bits(dsi, 5, dpi_color_coding);
850                 bytes_px = 3;
851                 break;
852         case OUT_D888_P666:
853         case OUT_P666:
854                 rk32_dsi_set_bits(dsi, 3, dpi_color_coding);
855                 rk32_dsi_set_bits(dsi, 1, en18_loosely);
856                 bytes_px = 3;
857                 break;
858         case OUT_P565:
859                 rk32_dsi_set_bits(dsi, 0, dpi_color_coding);
860                 bytes_px = 2;
861         default:
862                 break;
863         }
864         if (dsi->ops.id == DSI_RK3288 ||
865             dsi->ops.id == DSI_RK3368 ||
866             dsi->ops.id == DSI_RK3366 ||
867             dsi->ops.id == DSI_RK3399) {
868                 rk32_dsi_set_bits(dsi, 1, hsync_active_low);
869                 rk32_dsi_set_bits(dsi, 1, vsync_active_low);
870
871                 rk32_dsi_set_bits(dsi, 0, dataen_active_low);
872                 rk32_dsi_set_bits(dsi, 0, colorm_active_low);
873                 rk32_dsi_set_bits(dsi, 0, shutd_active_low);
874         } else if (dsi->ops.id == DSI_RK312x) {
875                 rk32_dsi_set_bits(dsi, !screen->pin_hsync, hsync_active_low);
876                 rk32_dsi_set_bits(dsi, !screen->pin_vsync, vsync_active_low);
877
878                 rk32_dsi_set_bits(dsi, screen->pin_den, dataen_active_low);
879                 rk32_dsi_set_bits(dsi, 0, colorm_active_low);
880                 rk32_dsi_set_bits(dsi, 0, shutd_active_low);
881         }
882
883         rk32_dsi_set_bits(dsi, dsi->host.video_mode, vid_mode_type); /* burst mode */
884
885         switch (dsi->host.video_mode) {
886         case VM_BM:
887                 if (screen->type == SCREEN_DUAL_MIPI)
888                         rk32_dsi_set_bits(dsi, screen->x_res / 2 + 4, vid_pkt_size);
889                 else
890                         rk32_dsi_set_bits(dsi, screen->x_res, vid_pkt_size);
891                 break;
892         case VM_NBMWSE:
893         case VM_NBMWSP:
894                 for (i = 8; i < 32; i++) {
895                         temp = i * lane * Tpclk % Ttxbyte_clk;
896                         if (decimals > temp) {
897                                 decimals = temp;
898                                 m = i;
899                         }
900                         if (decimals == 0)
901                                 break;
902                 }
903
904                 rk32_dsi_set_bits(dsi, screen->x_res / m + 1, num_chunks);
905                 rk32_dsi_set_bits(dsi, m, vid_pkt_size);
906                 temp = m * lane * Tpclk / Ttxbyte_clk - m * bytes_px;
907                 MIPI_DBG("%s:%d, %d\n", __func__, m, temp);
908
909                 if (temp >= 12)
910                         rk32_dsi_set_bits(dsi, temp - 12, null_pkt_size);
911                 break;
912         default:
913                 break;
914         }
915
916         /* rk32_dsi_set_bits(dsi, 0, CMD_MODE_CFG << 16); */
917         if (screen->type == SCREEN_MIPI) {
918                 rk32_dsi_set_bits(dsi, dsi->phy.Tpclk * (screen->x_res + screen->left_margin +
919                                         screen->hsync_len + screen->right_margin) \
920                                                 / dsi->phy.Ttxbyte_clk, vid_hline_time);
921         } else { /* used for dual mipi screen */
922                 rk32_dsi_set_bits(dsi, dsi->phy.Tpclk * (screen->x_res + 8 + screen->left_margin +
923                                         screen->hsync_len + screen->right_margin) \
924                                                 / dsi->phy.Ttxbyte_clk, vid_hline_time);
925         }
926         MIPI_DBG("dsi->phy.Tpclk = %d\n", dsi->phy.Tpclk);
927         MIPI_DBG("screen->left_margin = %d\n", screen->left_margin);
928         MIPI_DBG("dsi->phy.Ttxbyte_clk = %d\n", dsi->phy.Ttxbyte_clk);
929         MIPI_DBG("screen->hsync_len = %d\n", screen->hsync_len);
930         rk32_dsi_set_bits(dsi, dsi->phy.Tpclk * (screen->left_margin) / dsi->phy.Ttxbyte_clk,
931                                         vid_hbp_time);
932         rk32_dsi_set_bits(dsi, dsi->phy.Tpclk * (screen->hsync_len) / dsi->phy.Ttxbyte_clk,
933                                         vid_hsa_time);
934
935         rk32_dsi_set_bits(dsi, screen->y_res , vid_active_lines);
936         rk32_dsi_set_bits(dsi, screen->lower_margin, vid_vfp_lines);
937         rk32_dsi_set_bits(dsi, screen->upper_margin, vid_vbp_lines);
938         rk32_dsi_set_bits(dsi, screen->vsync_len, vid_vsa_lines);
939
940         dsi->phy.txclkesc = 20 * MHz;
941         val = dsi->phy.txbyte_clk / dsi->phy.txclkesc + 1;
942         dsi->phy.txclkesc = dsi->phy.txbyte_clk / val;
943         rk32_dsi_set_bits(dsi, val, TX_ESC_CLK_DIVISION);
944
945         rk32_dsi_set_bits(dsi, 10, TO_CLK_DIVISION);
946         rk32_dsi_set_bits(dsi, 1000, hstx_to_cnt); /* no sure */
947         rk32_dsi_set_bits(dsi, 1000, lprx_to_cnt);
948         if (dsi->ops.id == DSI_RK3399)
949                 rk32_dsi_set_bits(dsi, 32, phy_stop_wait_time);
950         else
951                 rk32_dsi_set_bits(dsi, 100, phy_stop_wait_time);
952
953         /* enable send command in low power mode */
954         rk32_dsi_set_bits(dsi, 4, outvact_lpcmd_time);
955         rk32_dsi_set_bits(dsi, 4, invact_lpcmd_time);
956         rk32_dsi_set_bits(dsi, 1, lp_cmd_en);
957
958         rk32_dsi_set_bits(dsi, 20, phy_hs2lp_time);
959         rk32_dsi_set_bits(dsi, 16, phy_lp2hs_time);
960         /*
961         rk32_dsi_set_bits(dsi, 87, phy_hs2lp_time_clk_lane);
962         rk32_dsi_set_bits(dsi, 25, phy_hs2hs_time_clk_lane);
963         */
964         rk32_dsi_set_bits(dsi, 10000, max_rd_time);
965
966         rk32_dsi_set_bits(dsi, 1, lp_hfp_en);
967         /* rk32_dsi_set_bits(dsi, 1, lp_hbp_en); */
968         rk32_dsi_set_bits(dsi, 1, lp_vact_en);
969         rk32_dsi_set_bits(dsi, 1, lp_vfp_en);
970         rk32_dsi_set_bits(dsi, 1, lp_vbp_en);
971         rk32_dsi_set_bits(dsi, 1, lp_vsa_en);
972
973         /* rk32_dsi_set_bits(dsi, 1, frame_bta_ack_en); */
974         rk32_dsi_set_bits(dsi, 1, phy_enableclk);
975         rk32_dsi_set_bits(dsi, 0, phy_tx_triggers);
976
977         if (screen->refresh_mode == COMMAND_MODE) {
978                 rk32_dsi_set_bits(dsi, screen->x_res, edpi_cmd_size);
979                 rk32_dsi_set_bits(dsi, 1, tear_fx_en);
980         }
981
982         /* enable non-continued function */
983         /* rk32_dsi_set_bits(dsi, 1, auto_clklane_ctrl); */
984         /*
985         rk32_dsi_set_bits(dsi, 1, phy_txexitulpslan);
986         rk32_dsi_set_bits(dsi, 1, phy_txexitulpsclk);
987         */
988         return 0;
989 }
990
991 /*
992  *      mipi protocol layer definition
993  */
994 static int rk_mipi_dsi_init(void *arg, u32 n)
995 {
996         u32 decimals = 1000, i = 0, pre = 0;
997         struct dsi *dsi = arg;
998         struct mipi_dsi_screen *screen = &dsi->screen;
999
1000         if (!screen)
1001                 return -1;
1002
1003         if ((screen->type != SCREEN_MIPI) && (screen->type != SCREEN_DUAL_MIPI)) {
1004                 MIPI_TRACE("only mipi dsi lcd is supported!\n");
1005                 return -1;
1006         }
1007
1008         if (((screen->type == SCREEN_DUAL_MIPI) && (rk_mipi_get_dsi_num() == 1)) || ((screen->type == SCREEN_MIPI) && (rk_mipi_get_dsi_num() == 2))) {
1009                 MIPI_TRACE("dsi number and mipi type not match!\n");
1010                 return -1;
1011         }
1012
1013         dsi->phy.Tpclk = rk_fb_get_prmry_screen_pixclock();
1014
1015         if (dsi->phy.refclk)
1016                 dsi->phy.ref_clk = clk_get_rate(dsi->phy.refclk) ;
1017         if (dsi->ops.id == DSI_RK312x ||
1018             dsi->ops.id == DSI_RK3368 ||
1019             dsi->ops.id == DSI_RK3366)
1020                 dsi->phy.ref_clk = dsi->phy.ref_clk / 2; /* 1/2 of input refclk */
1021
1022         dsi->phy.sys_clk = dsi->phy.ref_clk;
1023
1024         printk("dsi->phy.sys_clk =%d\n", dsi->phy.sys_clk);
1025         if (dsi->ops.id == DSI_RK3288 ||
1026             dsi->ops.id == DSI_RK3399) {
1027                 if ((screen->hs_tx_clk <= 90 * MHz) || (screen->hs_tx_clk >= 1500 * MHz))
1028                         dsi->phy.ddr_clk = 1500 * MHz; /* default is 1.5HGz */
1029                 else
1030                         dsi->phy.ddr_clk = screen->hs_tx_clk;
1031         } else if (dsi->ops.id == DSI_RK312x ||
1032                 dsi->ops.id == DSI_RK3368 ||
1033                 dsi->ops.id == DSI_RK3366) {
1034                 if ((screen->hs_tx_clk <= 80 * MHz) || (screen->hs_tx_clk >= 1000 * MHz))
1035                         dsi->phy.ddr_clk = 1000 * MHz; /* default is 1GHz */
1036                 else
1037                         dsi->phy.ddr_clk = screen->hs_tx_clk;
1038         }
1039
1040         if (n != 0)
1041                 dsi->phy.ddr_clk = n;
1042
1043         decimals = dsi->phy.ref_clk;
1044         for (i = 1; i < 6; i++) {
1045                 pre = dsi->phy.ref_clk / i;
1046                 if ((decimals > (dsi->phy.ddr_clk % pre)) && (dsi->phy.ddr_clk / pre < 512)) {
1047                         decimals = dsi->phy.ddr_clk % pre;
1048                         dsi->phy.prediv = i;
1049                         dsi->phy.fbdiv = dsi->phy.ddr_clk / pre;
1050                 }
1051                 if (decimals == 0)
1052                         break;
1053         }
1054
1055         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);
1056
1057         dsi->phy.ddr_clk = dsi->phy.ref_clk / dsi->phy.prediv * dsi->phy.fbdiv;
1058
1059         MIPI_DBG("dsi->phy.ddr_clk =%d\n", dsi->phy.ddr_clk);
1060         dsi->phy.txbyte_clk = dsi->phy.ddr_clk / 8;
1061
1062         dsi->phy.txclkesc = 20 * MHz; /* < 20MHz */
1063         dsi->phy.txclkesc = dsi->phy.txbyte_clk / (dsi->phy.txbyte_clk / dsi->phy.txclkesc + 1);
1064
1065         dsi->phy.pclk = div_u64(1000000000000llu, dsi->phy.Tpclk);
1066         dsi->phy.Ttxclkesc = div_u64(1000000000000llu, dsi->phy.txclkesc);
1067         dsi->phy.Tsys_clk = div_u64(1000000000000llu, dsi->phy.sys_clk);
1068         dsi->phy.Tddr_clk = div_u64(1000000000000llu, dsi->phy.ddr_clk);
1069         dsi->phy.Ttxbyte_clk = div_u64(1000000000000llu, dsi->phy.txbyte_clk);
1070
1071         dsi->phy.UI = dsi->phy.Tddr_clk;
1072         dsi->vid = 0;
1073
1074         if (screen->dsi_lane > 0 && screen->dsi_lane <= 4)
1075                 dsi->host.lane = screen->dsi_lane;
1076         else
1077                 dsi->host.lane = 4;
1078
1079         dsi->host.video_mode = VM_BM;
1080
1081         MIPI_DBG("UI:%d\n", dsi->phy.UI);
1082         MIPI_DBG("ref_clk:%d\n", dsi->phy.ref_clk);
1083         MIPI_DBG("pclk:%d, Tpclk:%d\n", dsi->phy.pclk, dsi->phy.Tpclk);
1084         MIPI_DBG("sys_clk:%d, Tsys_clk:%d\n", dsi->phy.sys_clk, dsi->phy.Tsys_clk);
1085         MIPI_DBG("ddr_clk:%d, Tddr_clk:%d\n", dsi->phy.ddr_clk, dsi->phy.Tddr_clk);
1086         MIPI_DBG("txbyte_clk:%d, Ttxbyte_clk:%d\n", dsi->phy.txbyte_clk,
1087                                 dsi->phy.Ttxbyte_clk);
1088         MIPI_DBG("txclkesc:%d, Ttxclkesc:%d\n", dsi->phy.txclkesc, dsi->phy.Ttxclkesc);
1089
1090         mdelay(10);
1091         rk_phy_power_up(dsi);
1092         rk32_mipi_dsi_host_power_up(dsi);
1093         rk_phy_init(dsi);
1094         rk32_mipi_dsi_host_init(dsi);
1095
1096         return 0;
1097 }
1098
1099 static int rk32_mipi_dsi_is_enable(void *arg, u32 enable)
1100 {
1101         struct dsi *dsi = arg;
1102
1103         rk32_dsi_set_bits(dsi, enable, shutdownz);
1104         return 0;
1105 }
1106
1107 static int rk32_mipi_dsi_enable_video_mode(void *arg, u32 enable)
1108 {
1109         struct dsi *dsi = arg;
1110
1111         rk32_dsi_set_bits(dsi, !enable, cmd_video_mode);
1112         return 0;
1113 }
1114
1115 static int rk32_mipi_dsi_enable_command_mode(void *arg, u32 enable)
1116 {
1117         struct dsi *dsi = arg;
1118
1119         rk32_dsi_set_bits(dsi, enable, cmd_video_mode);
1120         return 0;
1121 }
1122
1123 static int rk32_mipi_dsi_enable_hs_clk(void *arg, u32 enable)
1124 {
1125         struct dsi *dsi = arg;
1126
1127         rk32_dsi_set_bits(dsi, enable, phy_txrequestclkhs);
1128         return 0;
1129 }
1130
1131 static int rk32_mipi_dsi_is_active(void *arg)
1132 {
1133         struct dsi *dsi = arg;
1134
1135         return rk32_dsi_get_bits(dsi, shutdownz);
1136 }
1137
1138 static int rk32_mipi_dsi_send_packet(void *arg, unsigned char cmds[], u32 length)
1139 {
1140         struct dsi *dsi = arg;
1141         unsigned char *regs;
1142         u32 type, liTmp = 0, i = 0, j = 0, data = 0;
1143
1144         if (rk32_dsi_get_bits(dsi, gen_cmd_full) == 1) {
1145                 MIPI_TRACE("gen_cmd_full\n");
1146                 return -1;
1147         }
1148         regs = kmalloc(0x400, GFP_KERNEL);
1149         if (!regs) {
1150                 printk("request regs fail!\n");
1151                 return -ENOMEM;
1152         }
1153         memcpy(regs, cmds, length);
1154
1155         liTmp = length - 2;
1156         type = regs[1];
1157
1158         switch (type) {
1159         case DTYPE_DCS_SWRITE_0P:
1160                 rk32_dsi_set_bits(dsi, regs[0], dcs_sw_0p_tx);
1161                 data = regs[2] << 8 | type;
1162                 break;
1163         case DTYPE_DCS_SWRITE_1P:
1164                 rk32_dsi_set_bits(dsi, regs[0], dcs_sw_1p_tx);
1165                 data = regs[2] << 8 | type;
1166                 data |= regs[3] << 16;
1167                 break;
1168         case DTYPE_DCS_LWRITE:
1169                 rk32_dsi_set_bits(dsi, regs[0], dcs_lw_tx);
1170                 for (i = 0; i < liTmp; i++) {
1171                         regs[i] = regs[i+2];
1172                 }
1173                 for (i = 0; i < liTmp; i++) {
1174                         j = i % 4;
1175                         data |= regs[i] << (j * 8);
1176                         if (j == 3 || ((i + 1) == liTmp)) {
1177                                 if (rk32_dsi_get_bits(dsi, gen_pld_w_full) == 1) {
1178                                         MIPI_TRACE("gen_pld_w_full :%d\n", i);
1179                                         break;
1180                                 }
1181                                 rk32_dsi_set_bits(dsi, data, GEN_PLD_DATA);
1182                                 MIPI_DBG("write GEN_PLD_DATA:%d, %08x\n", i, data);
1183                                 data = 0;
1184                         }
1185                 }
1186                 data = type;
1187                 data |= (liTmp & 0xffff) << 8;
1188                 break;
1189         case DTYPE_GEN_LWRITE:
1190                 rk32_dsi_set_bits(dsi, regs[0], gen_lw_tx);
1191                 for (i = 0; i < liTmp; i++) {
1192                         regs[i] = regs[i+2];
1193                 }
1194                 for (i = 0; i < liTmp; i++) {
1195                         j = i % 4;
1196                         data |= regs[i] << (j * 8);
1197                         if (j == 3 || ((i + 1) == liTmp)) {
1198                                 if (rk32_dsi_get_bits(dsi, gen_pld_w_full) == 1) {
1199                                         MIPI_TRACE("gen_pld_w_full :%d\n", i);
1200                                         break;
1201                                 }
1202                                 rk32_dsi_set_bits(dsi, data, GEN_PLD_DATA);
1203                                 MIPI_DBG("write GEN_PLD_DATA:%d, %08x\n", i, data);
1204                                 data = 0;
1205                         }
1206                 }
1207                 data = (dsi->vid << 6) | type;
1208                 data |= (liTmp & 0xffff) << 8;
1209                 break;
1210         case DTYPE_GEN_SWRITE_2P: /* one command and one parameter */
1211                 rk32_dsi_set_bits(dsi, regs[0], gen_sw_2p_tx);
1212                 if (liTmp <= 2) {
1213                         /* It is used for normal Generic Short WRITE Packet with 2 parameters. */
1214                         data = type;
1215                         data |= regs[2] << 8;   /* dcs command */
1216                         data |= regs[3] << 16;  /* parameter of command */
1217                         break;
1218                 }
1219
1220                 /* The below is used for Generic Short WRITE Packet with 2 parameters
1221                  * that more than 2 parameters. Though it is illegal dcs command, we can't
1222                  * make sure the users do not send that command.
1223                  */
1224                 for (i = 0; i < liTmp; i++) {
1225                         regs[i] = regs[i+2];
1226                 }
1227                 for (i = 0; i < liTmp; i++) {
1228                         j = i % 4;
1229                         data |= regs[i] << (j * 8);
1230                         if (j == 3 || ((i + 1) == liTmp)) {
1231                                 if (rk32_dsi_get_bits(dsi, gen_pld_w_full) == 1) {
1232                                         MIPI_TRACE("gen_pld_w_full :%d\n", i);
1233                                         break;
1234                                 }
1235                                 rk32_dsi_set_bits(dsi, data, GEN_PLD_DATA);
1236                                 MIPI_DBG("write GEN_PLD_DATA:%d, %08x\n", i, data);
1237                                 data = 0;
1238                         }
1239                 }
1240                 data = type;
1241                 data |= (liTmp & 0xffff) << 8;
1242                 break;
1243         case DTYPE_GEN_SWRITE_1P: /* one command without parameter */
1244                 rk32_dsi_set_bits(dsi, regs[0], gen_sw_1p_tx);
1245                 data = type;
1246                 data |= regs[2] << 8;
1247                 break;
1248         case DTYPE_GEN_SWRITE_0P: /* nop packet without command and parameter */
1249                 rk32_dsi_set_bits(dsi, regs[0], gen_sw_0p_tx);
1250                 data =  type;
1251                 break;
1252         default:
1253                 printk("0x%x:this type not suppport!\n", type);
1254         }
1255
1256         MIPI_DBG("%d command sent in %s size:%d\n", __LINE__, regs[0] ? "LP mode" : "HS mode", liTmp);
1257
1258         MIPI_DBG("write GEN_HDR:%08x\n", data);
1259         rk32_dsi_set_bits(dsi, data, GEN_HDR);
1260
1261         i = 10;
1262         while (!rk32_dsi_get_bits(dsi, gen_cmd_empty) && i--) {
1263                 MIPI_DBG(".");
1264                 udelay(10);
1265         }
1266         udelay(10);
1267         kfree(regs);
1268         return 0;
1269 }
1270
1271 static int rk32_mipi_dsi_read_dcs_packet(void *arg, unsigned char *data1, u32 n)
1272 {
1273         struct dsi *dsi = arg;
1274         unsigned char regs[2];
1275         u32 data = 0;
1276         int type = 0x06;
1277         regs[0] = LPDT;
1278         regs[1] = 0x0a;
1279         n = n - 1;
1280
1281         rk32_dsi_set_bits(dsi, regs[0], dcs_sr_0p_tx);
1282         /*
1283         if(type == DTYPE_GEN_SWRITE_0P)
1284                 data = (dsi->vid << 6) | (n << 4) | type;
1285         else
1286                 data = (dsi->vid << 6) | ((n-1) << 4) | type;
1287         */
1288
1289         data |= regs[1] << 8 | type;
1290
1291         printk("write GEN_HDR:%08x\n", data);
1292
1293         rk32_dsi_set_bits(dsi, 0xFFFF, bta_to_cnt);
1294         rk32_dsi_set_bits(dsi, 1, bta_en);
1295         rk32_dsi_set_bits(dsi, data, GEN_HDR);
1296         udelay(20);
1297
1298         printk("rk32_mipi_dsi_read_dcs_packet==0x%x\n", rk32_dsi_get_bits(dsi, GEN_PLD_DATA));
1299         rk32_dsi_set_bits(dsi, 0, bta_en);
1300
1301         return 0;
1302 }
1303
1304 static int rk32_mipi_dsi_power_up(void *arg)
1305 {
1306         struct dsi *dsi = arg;
1307
1308         rk32_phy_power_up(dsi);
1309         rk32_mipi_dsi_host_power_up(dsi);
1310         return 0;
1311 }
1312
1313 static int rk32_mipi_dsi_power_down(void *arg)
1314 {
1315         struct dsi *dsi = arg;
1316         struct mipi_dsi_screen *screen = &dsi->screen;
1317
1318         if (!screen)
1319                 return -1;
1320
1321         rk32_mipi_dsi_host_power_down(dsi);
1322         rk_phy_power_down(dsi);
1323
1324         MIPI_TRACE("%s:%d\n", __func__, __LINE__);
1325         return 0;
1326 }
1327
1328 static int rk32_mipi_dsi_get_id(void *arg)
1329 {
1330         u32 id = 0;
1331         struct dsi *dsi = arg;
1332
1333         id = rk32_dsi_get_bits(dsi, VERSION);
1334         return id;
1335 }
1336
1337 #ifdef MIPI_DSI_REGISTER_IO
1338 #include <linux/proc_fs.h>
1339 #include <asm/uaccess.h>
1340 #include <linux/slab.h>
1341
1342 ssize_t reg_proc_write(struct file *file, const char __user *buff, size_t count, loff_t *offp)
1343 {
1344         int ret = -1, i = 0;
1345         u32 read_val = 0;
1346         char *buf = kmalloc(count, GFP_KERNEL);
1347         char *data = buf;
1348         char str[32];
1349         char command = 0;
1350         u64 regs_val = 0;
1351         memset(buf, 0, count);
1352         ret = copy_from_user((void *)buf, buff, count);
1353         data = strstr(data, "-");
1354         if (data == NULL)
1355                 goto reg_proc_write_exit;
1356         command = *(++data);
1357         switch (command) {
1358         case 'w':
1359                 while (1) {
1360                         data = strstr(data, "0x");
1361                         if (data == NULL)
1362                                 goto reg_proc_write_exit;
1363                         sscanf(data, "0x%llx", &regs_val);
1364                         if ((regs_val & 0xffff00000000ULL) == 0)
1365                                 goto reg_proc_write_exit;
1366                         read_val = regs_val & 0xffffffff;
1367                         printk("regs_val=0x%llx\n", regs_val);
1368                         rk32_dsi_write_reg(dsi0, regs_val >> 32, &read_val);
1369                         rk32_dsi_read_reg(dsi0, regs_val >> 32, &read_val);
1370                         regs_val &= 0xffffffff;
1371                         if (read_val != regs_val)
1372                                 MIPI_TRACE("%s fail:0x%08x\n", __func__, read_val);
1373                         data += 3;
1374                         msleep(1);
1375                 }
1376                 break;
1377         case 'r':
1378                 data = strstr(data, "0x");
1379                 if (data == NULL) {
1380                         goto reg_proc_write_exit;
1381                 }
1382                 sscanf(data, "0x%llx", &regs_val);
1383                 rk32_dsi_read_reg(dsi0, (u16)regs_val, &read_val);
1384                 MIPI_TRACE("*%04x : %08x\n", (u16)regs_val, read_val);
1385                 msleep(1);
1386                 break;
1387         case 's':
1388                 while (*(++data) == ' ')
1389                         ;
1390                 sscanf(data, "%d", &read_val);
1391                 if (read_val == 11)
1392                         read_val = 11289600;
1393                 else
1394                         read_val *= MHz;
1395                 break;
1396         case 'd':
1397         case 'g':
1398         case 'c':
1399                 while (*(++data) == ' ')
1400                         ;
1401                 i = 0;
1402
1403                 do {
1404                         if (i > 31) {
1405                                 MIPI_TRACE("payload entry is larger than 32\n");
1406                                 break;
1407                         }
1408                         sscanf(data, "%x,", (unsigned int *)(str + i)); /* -c 1,29,02,03,05,06,> pro */
1409                         data = strstr(data, ",");
1410                         if (data == NULL)
1411                                 break;
1412                         data++;
1413                         i++;
1414                 } while (1);
1415                 read_val = i;
1416                 i = 2;
1417                 while (i--) {
1418                         msleep(10);
1419                         if (command == 'd')
1420                                 rk32_mipi_dsi_send_packet(dsi0, str, read_val);
1421                         else
1422                                 rk32_mipi_dsi_send_packet(dsi0, str, read_val);
1423                 }
1424                 i = 1;
1425                 while (i--) {
1426                         msleep(1000);
1427                 }
1428                 break;
1429         default:
1430                 break;
1431         }
1432
1433 reg_proc_write_exit:
1434         kfree(buf);
1435         msleep(20);
1436         return count;
1437 }
1438
1439 int reg_proc_read(struct seq_file *s, void *v)
1440 {
1441         int i = 0;
1442         u32 val = 0;
1443         struct dsi *dsi = s->private;
1444
1445         for (i = VERSION; i < (VERSION + (0xdc << 16)); i += 4<<16) {
1446                 val = rk32_dsi_get_bits(dsi, i);
1447                 seq_printf(s, "%04x: %08x\n", i>>16, val);
1448         }
1449         return 0;
1450 }
1451 static int reg_proc_open(struct inode *inode, struct file *file)
1452 {
1453         struct dsi *dsi = inode->i_private;
1454
1455         return single_open(file, reg_proc_read, dsi);
1456 }
1457
1458 int reg_proc_close(struct inode *inode, struct file *file)
1459 {
1460         return 0;
1461 }
1462
1463 struct file_operations reg_proc_fops = {
1464         .owner = THIS_MODULE,
1465         .open = reg_proc_open,
1466         .release = reg_proc_close,
1467         .write = reg_proc_write,
1468         .read = seq_read,
1469 };
1470
1471 ssize_t reg_proc_write1(struct file *file, const char __user *buff, size_t count, loff_t *offp)
1472 {
1473         int ret = -1, i = 0;
1474         u32 read_val = 0;
1475         char *buf = kmalloc(count, GFP_KERNEL);
1476         char *data = buf;
1477         char str[32];
1478         char command = 0;
1479         u64 regs_val = 0;
1480         memset(buf, 0, count);
1481         ret = copy_from_user((void *)buf, buff, count);
1482
1483         data = strstr(data, "-");
1484         if (data == NULL)
1485                 goto reg_proc_write_exit;
1486         command = *(++data);
1487
1488         switch (command) {
1489         case 'w':
1490                 while (1) {
1491                         data = strstr(data, "0x");
1492                         if (data == NULL)
1493                                 goto reg_proc_write_exit;
1494                         sscanf(data, "0x%llx", &regs_val);
1495                         if ((regs_val & 0xffff00000000ULL) == 0)
1496                                 goto reg_proc_write_exit;
1497                         read_val = regs_val & 0xffffffff;
1498                         rk32_dsi_write_reg(dsi1, regs_val >> 32, &read_val);
1499                         rk32_dsi_read_reg(dsi1, regs_val >> 32, &read_val);
1500                         regs_val &= 0xffffffff;
1501                         if (read_val != regs_val)
1502                                 MIPI_TRACE("%s fail:0x%08x\n", __func__, read_val);
1503                         data += 3;
1504                         msleep(1);
1505                 }
1506                 break;
1507         case 'r':
1508                 data = strstr(data, "0x");
1509                 if (data == NULL)
1510                         goto reg_proc_write_exit;
1511                 sscanf(data, "0x%llx", &regs_val);
1512                 rk32_dsi_read_reg(dsi1, (u16)regs_val, &read_val);
1513                 MIPI_TRACE("*%04x : %08x\n", (u16)regs_val, read_val);
1514                 msleep(1);
1515                 break;
1516         case 's':
1517                 while (*(++data) == ' ')
1518                         ;
1519                 sscanf(data, "%d", &read_val);
1520                 if (read_val == 11)
1521                         read_val = 11289600;
1522                 else
1523                         read_val *= MHz;
1524                 break;
1525         case 'd':
1526         case 'g':
1527         case 'c':
1528                 while (*(++data) == ' ')
1529                         ;
1530                 i = 0;
1531
1532                 do {
1533                         if (i > 31) {
1534                                 MIPI_TRACE("payload entry is larger than 32\n");
1535                                 break;
1536                         }
1537                         sscanf(data, "%x,", (unsigned int *)(str + i)); /* -c 1,29,02,03,05,06,> pro */
1538                         data = strstr(data, ",");
1539                         if (data == NULL)
1540                                 break;
1541                         data++;
1542                         i++;
1543                 } while (1);
1544                 read_val = i;
1545                 i = 2;
1546                 while (i--) {
1547                         msleep(10);
1548                         if (command == 'd')
1549                                 rk32_mipi_dsi_send_packet(dsi1, str, read_val);
1550                         else
1551                                 rk32_mipi_dsi_send_packet(dsi1, str, read_val);
1552                 }
1553                 i = 1;
1554                 while (i--) {
1555                         msleep(1000);
1556                 }
1557                 break;
1558         default:
1559                 break;
1560         }
1561
1562 reg_proc_write_exit:
1563         kfree(buf);
1564         msleep(20);
1565         return count;
1566 }
1567
1568 int reg_proc_close1(struct inode *inode, struct file *file)
1569 {
1570         return 0;
1571 }
1572
1573 struct file_operations reg_proc_fops1 = {
1574         .owner = THIS_MODULE,
1575         .open = reg_proc_open,
1576         .release = reg_proc_close1,
1577         .write = reg_proc_write1,
1578         .read = seq_read,
1579 };
1580 #endif
1581 #if 0/* def CONFIG_MIPI_DSI_LINUX */
1582 static irqreturn_t rk32_mipi_dsi_irq_handler(int irq, void *data)
1583 {
1584         printk("-------rk32_mipi_dsi_irq_handler-------\n");
1585         return IRQ_HANDLED;
1586 }
1587 #endif
1588
1589 #if 0
1590 static int dwc_phy_test_rd(struct dsi *dsi, unsigned char test_code)
1591 {
1592         int val = 0;
1593
1594         rk32_dsi_set_bits(dsi, 0x10000 | test_code, PHY_TEST_CTRL1);
1595         rk32_dsi_set_bits(dsi, 0x2, PHY_TEST_CTRL0);
1596         rk32_dsi_set_bits(dsi, 0x0, PHY_TEST_CTRL0);
1597
1598         val = rk32_dsi_get_bits(dsi, PHY_TEST_CTRL1);
1599         return val;
1600 }
1601 #endif
1602 static int rk32_dsi_enable(void)
1603 {
1604         u16 opt_mode = 0;
1605         MIPI_DBG("rk32_dsi_enable-------\n");
1606         if (!dsi0->clk_on) {
1607                 dsi0->clk_on = 1;
1608 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
1609                 pm_runtime_get_sync(&dsi0->pdev->dev);
1610 #endif
1611                 opt_mode = dsi0->screen.refresh_mode;
1612                 rk_fb_get_prmry_screen(dsi0->screen.screen);
1613                 dsi0->screen.lcdc_id = dsi0->screen.screen->lcdc_id;
1614                 rk32_init_phy_mode(dsi0->screen.lcdc_id);
1615
1616                 dsi_init(0, 0);
1617                 if (rk_mipi_get_dsi_num() == 2)
1618                         dsi_init(1, 0);
1619
1620                 rk_mipi_screen_standby(0);
1621
1622         /* After the core reset, DPI waits for the first VSYNC
1623         active transition to start signal sampling, including pixel data,
1624         and preventing image transmission in the middle of a frame.
1625         */
1626                 dsi_is_enable(0, 0);
1627                 if (rk_mipi_get_dsi_num() == 2)
1628                         dsi_is_enable(1, 0);
1629
1630                 if (opt_mode != COMMAND_MODE) {
1631                         dsi_enable_video_mode(0, 1);
1632                         if (rk_mipi_get_dsi_num() == 2)
1633                                 dsi_enable_video_mode(1, 1);
1634                 }
1635                 dsi_is_enable(0, 1);
1636                 if (rk_mipi_get_dsi_num() == 2)
1637                         dsi_is_enable(1, 1);
1638         }
1639         return 0;
1640 }
1641
1642 static void rockchip_mipi_cmd_mode_refresh(unsigned int xpos,
1643                                            unsigned int ypos,
1644                                            unsigned int xsize,
1645                                            unsigned int ysize)
1646 {
1647         u32 x0 = xpos;
1648         u32 y0 = ypos;
1649         u32 x1 = x0 + xsize - 1;
1650         u32 y1 = y0 + ysize - 1;
1651
1652         unsigned char x0_MSB = ((x0 >> 8) & 0xff);
1653         unsigned char x0_LSB = (x0 & 0xff);
1654         unsigned char x1_MSB = ((x1 >> 8) & 0xff);
1655         unsigned char x1_LSB = (x1 & 0xff);
1656         unsigned char y0_MSB = ((y0 >> 8) & 0xff);
1657         unsigned char y0_LSB = (y0 & 0xff);
1658         unsigned char y1_MSB = ((y1 >> 8) & 0xff);
1659         unsigned char y1_LSB = (y1 & 0xff);
1660
1661         unsigned char set_col_cmd[7] = {0};
1662         unsigned char set_page_cmd[7] = {0};
1663         unsigned char wms[3] = {0};
1664         u32 len;
1665
1666         set_col_cmd[0] = HSDT;
1667         set_col_cmd[1] = 0x39;
1668         set_col_cmd[2] = 0x2a;
1669         set_col_cmd[3] = x0_MSB;
1670         set_col_cmd[4] = x0_LSB;
1671         set_col_cmd[5] = x1_MSB;
1672         set_col_cmd[6] = x1_LSB;
1673
1674         len = ARRAY_SIZE(set_col_cmd);
1675         rk32_mipi_dsi_send_packet(dsi0, set_col_cmd, len);
1676         if (rk_mipi_get_dsi_num() == 2)
1677                 rk32_mipi_dsi_send_packet(dsi1, set_col_cmd, len);
1678
1679         set_page_cmd[0] = HSDT;
1680         set_page_cmd[1] = 0x39;
1681         set_page_cmd[2] = 0x2b;
1682         set_page_cmd[3] = y0_MSB;
1683         set_page_cmd[4] = y0_LSB;
1684         set_page_cmd[5] = y1_MSB;
1685         set_page_cmd[6] = y1_LSB;
1686
1687         len = ARRAY_SIZE(set_page_cmd);
1688         rk32_mipi_dsi_send_packet(dsi0, set_page_cmd, len);
1689         if (rk_mipi_get_dsi_num() == 2)
1690                 rk32_mipi_dsi_send_packet(dsi1, set_page_cmd, len);
1691
1692         wms[0] = HSDT;
1693         wms[1] = 0x39;
1694         wms[2] = 0x2C;
1695
1696         len = ARRAY_SIZE(wms);
1697         rk32_mipi_dsi_send_packet(dsi0, wms, len);
1698         if (rk_mipi_get_dsi_num() == 2)
1699                 rk32_mipi_dsi_send_packet(dsi1, wms, len);
1700 }
1701
1702 #ifdef CONFIG_MIPI_DSI_LINUX
1703 static int rk32_dsi_disable(void)
1704 {
1705         MIPI_DBG("rk32_dsi_disable-------\n");
1706         if (dsi0->clk_on) {
1707                 dsi0->clk_on = 0;
1708                 rk_mipi_screen_standby(1);
1709                 dsi_power_off(0);
1710                 if (rk_mipi_get_dsi_num() == 2)
1711                         dsi_power_off(1);
1712 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
1713                 pm_runtime_put(&dsi0->pdev->dev);
1714 #endif
1715         }
1716         return 0;
1717 }
1718
1719 static struct rk_fb_trsm_ops trsm_dsi_ops = {
1720         .enable = rk32_dsi_enable,
1721         .disable = rk32_dsi_disable,
1722         .dsp_pwr_on = rk32_mipi_power_up_DDR,
1723         .dsp_pwr_off = rk32_mipi_power_down_DDR,
1724         .refresh = rockchip_mipi_cmd_mode_refresh,
1725 };
1726 #endif
1727
1728 static void rockchip_3399_grf_config(int lcdc_id)
1729 {
1730         int dsi_num;
1731         int val;
1732
1733         dsi_num = rk_mipi_get_dsi_num();
1734         if (dsi_num == 1) {
1735                 if (lcdc_id == 1) {
1736                         val = 0x1 << 16 | 0x1;
1737                         regmap_write(dsi0->grf_base, RK3399_GRF_CON20, val);
1738                 } else {
1739                         val = 0x1 << 16 | 0x0;
1740                         regmap_write(dsi0->grf_base, RK3399_GRF_CON20, val);
1741                 }
1742
1743                 val = 0x1 << 28 | 0x0 << 12;
1744                 val |= 0xf << 20 | 0x0 << 4;
1745                 val |= 0xf << 16 | 0x0;
1746                 regmap_write(dsi0->grf_base, RK3399_GRF_CON22, val);
1747         } else {
1748                 if (lcdc_id == 1) {
1749                         val = 0x1 << 16 | 0x1;
1750                         val |= 0x1 << 20 | 0x1 << 4;
1751                         regmap_write(dsi0->grf_base, RK3399_GRF_CON20, val);
1752                 } else {
1753                         val = 0x1 << 16 | 0x0;
1754                         val |= 0x1 << 20 | 0x0 << 4;
1755                         regmap_write(dsi0->grf_base, RK3399_GRF_CON20, val);
1756                 }
1757
1758                 val = 0x1 << 28 | 0x0 << 12;
1759                 val |= 0xf << 20 | 0x0 << 4;
1760                 val |= 0xf << 16 | 0x0;
1761                 regmap_write(dsi0->grf_base, RK3399_GRF_CON22, val);
1762
1763                 val = 0x1 << 20 | 0x0 << 0x4;
1764                 regmap_write(dsi0->grf_base, RK3399_GRF_CON7, val);
1765
1766                 val = 0xf << 24 | 0x0 << 8;
1767                 val |= 0xf << 20 | 0x0 << 4;
1768                 val |= 0xf << 16 | 0x0;
1769                 regmap_write(dsi0->grf_base, RK3399_GRF_CON23, val);
1770
1771                 val = 0x1 << 22 | 0x1 << 6;
1772                 val |= 0x1 << 21 | 0x0 << 5;
1773                 regmap_write(dsi0->grf_base, RK3399_GRF_CON24, val);
1774
1775                 val = 0xf << 24 | 0x0 << 8;
1776                 val |= 0xf << 20 | 0x0 << 4;
1777                 val |= 0xf << 16 | 0xf;
1778                 regmap_write(dsi0->grf_base, RK3399_GRF_CON23, val);
1779         }
1780 }
1781
1782 static void rk32_init_phy_mode(int lcdc_id)
1783 {
1784         int val0 = 0, val1 = 0;
1785
1786         MIPI_DBG("rk32_init_phy_mode----------lcdc_id=%d\n", lcdc_id);
1787
1788         /* Only the rk3288 VOP need setting the VOP output. */
1789         if (dsi0->ops.id == DSI_RK3288) {
1790                 /* D-PHY mode select */
1791                 if (rk_mipi_get_dsi_num() == 1) {
1792                         if (lcdc_id == 1)
1793                                 /* 1'b1: VOP LIT output to DSI host0;1'b0: VOP BIG output to DSI host0 */
1794                                 val0 = 0x1 << 22 | 0x1 << 6;
1795                         else
1796                                 val0 = 0x1 << 22 | 0x0 << 6;
1797                         writel_relaxed(val0, RK_GRF_VIRT + RK3288_GRF_SOC_CON6);
1798                 } else {
1799                         if (lcdc_id == 1) {
1800                                 val0 = 0x1 << 25 | 0x1 <<  9 | 0x1 << 22 | 0x1 <<  6;
1801                                 val1 = 0x1 << 31 | 0x1 << 30 | 0x0 << 15 | 0x1 << 14;
1802                         } else {
1803                                 val0 = 0x1 << 25 | 0x0 <<  9 | 0x1 << 22 | 0x0 << 14;
1804                                 val1 = 0x1 << 31 | 0x1 << 30 | 0x0 << 15 | 0x1 << 14;
1805                         }
1806                         writel_relaxed(val0, RK_GRF_VIRT + RK3288_GRF_SOC_CON6);
1807                         writel_relaxed(val1, RK_GRF_VIRT + RK3288_GRF_SOC_CON14);
1808                 }
1809         } else if (dsi0->ops.id == DSI_RK3399) {
1810                 rockchip_3399_grf_config(lcdc_id);
1811         }
1812 }
1813
1814 #ifdef CONFIG_MIPI_DSI_LINUX
1815 static int rk32_mipi_power_down_DDR(void)
1816 {
1817         dsi_is_enable(0, 0);
1818         if (rk_mipi_get_dsi_num() == 2)
1819                 dsi_is_enable(1, 0);
1820         return 0;
1821 }
1822
1823 static int rk32_mipi_power_up_DDR(void)
1824 {
1825         dsi_is_enable(0, 0);
1826         if (rk_mipi_get_dsi_num() == 2)
1827                 dsi_is_enable(1, 0);
1828         dsi_enable_video_mode(0, 1);
1829         dsi_enable_video_mode(1, 1);
1830         dsi_is_enable(0, 1);
1831         if (rk_mipi_get_dsi_num() == 2)
1832                 dsi_is_enable(1, 1);
1833         return 0;
1834 }
1835
1836 struct dsi_type {
1837         char *label;
1838         u32 dsi_id;
1839 };
1840
1841 static struct dsi_type dsi_rk312x = {
1842         .label = "rk312-dsi",
1843         .dsi_id = DSI_RK312x,
1844 };
1845
1846 static struct dsi_type dsi_rk32 = {
1847         .label = "rk32-dsi",
1848         .dsi_id = DSI_RK3288,
1849 };
1850
1851 static struct dsi_type dsi_rk3368 = {
1852         .label = "rk3368-dsi",
1853         .dsi_id = DSI_RK3368,
1854 };
1855
1856 static struct dsi_type dsi_rk3366 = {
1857         .label = "rk3366-dsi",
1858         .dsi_id = DSI_RK3366,
1859 };
1860
1861 static struct dsi_type dsi_rk3399 = {
1862         .label = "rk3399-dsi",
1863         .dsi_id = DSI_RK3399,
1864 };
1865
1866 static const struct of_device_id of_rk_mipi_dsi_match[] = {
1867         { .compatible = "rockchip,rk32-dsi", .data = &dsi_rk32},
1868         { .compatible = "rockchip,rk312x-dsi", .data = &dsi_rk312x},
1869         { .compatible = "rockchip,rk3368-dsi", .data = &dsi_rk3368},
1870         { .compatible = "rockchip,rk3366-dsi", .data = &dsi_rk3366},
1871         { .compatible = "rockchip,rk3399-dsi", .data = &dsi_rk3399},
1872         { /* Sentinel */ }
1873 };
1874
1875 static int rk32_mipi_dsi_probe(struct platform_device *pdev)
1876 {
1877         int ret = 0;
1878         static int id;
1879         struct dsi *dsi;
1880         struct mipi_dsi_ops *ops;
1881         struct rk_screen *screen;
1882         struct mipi_dsi_screen *dsi_screen;
1883         struct resource *res_host, *res_phy;
1884         struct device_node *np = pdev->dev.of_node;
1885         const struct dsi_type *data;
1886         const struct of_device_id *of_id =
1887                         of_match_device(of_rk_mipi_dsi_match, &pdev->dev);
1888         if (!of_id) {
1889                 dev_err(&pdev->dev, "failed to match device\n");
1890                 return -ENODEV;
1891         }
1892         data = of_id->data;
1893
1894         dsi = devm_kzalloc(&pdev->dev, sizeof(struct dsi), GFP_KERNEL);
1895         if (!dsi) {
1896                 dev_err(&pdev->dev, "request struct dsi fail!\n");
1897                 return -ENOMEM;
1898         }
1899         dsi->ops.id = data->dsi_id;
1900         printk(KERN_INFO "%s\n", data->label);
1901         if (dsi->ops.id == DSI_RK3288 ||
1902             dsi->ops.id == DSI_RK3399) {
1903                 res_host = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1904                 dsi->host.membase = devm_ioremap_resource(&pdev->dev, res_host);
1905                 if (IS_ERR(dsi->host.membase)) {
1906                         dev_err(&pdev->dev, "get resource mipi host membase fail!\n");
1907                         return PTR_ERR(dsi->host.membase);
1908                 }
1909         } else if (dsi->ops.id == DSI_RK312x ||
1910                         dsi->ops.id == DSI_RK3368 ||
1911                         dsi->ops.id == DSI_RK3366) {
1912                 res_host = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mipi_dsi_host");
1913                 dsi->host.membase = devm_ioremap_resource(&pdev->dev, res_host);
1914                 if (IS_ERR(dsi->host.membase)) {
1915                         dev_err(&pdev->dev, "get resource mipi host membase fail!\n");
1916                         return PTR_ERR(dsi->host.membase);
1917                 }
1918                 res_phy = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mipi_dsi_phy");
1919                 dsi->phy.membase = devm_ioremap_resource(&pdev->dev, res_phy);
1920                 if (IS_ERR(dsi->phy.membase)) {
1921                         dev_err(&pdev->dev, "get resource mipi phy membase fail!\n");
1922                         return PTR_ERR(dsi->phy.membase);
1923                 }
1924         }
1925
1926         dsi->phy.refclk  = devm_clk_get(&pdev->dev, "clk_mipi_24m");
1927         if (unlikely(IS_ERR(dsi->phy.refclk))) {
1928                 dev_err(&pdev->dev, "get clk_mipi_24m clock fail\n");
1929                 return PTR_ERR(dsi->phy.refclk);
1930         }
1931
1932         /* Get the APB bus clk access mipi phy */
1933         dsi->dsi_pclk = devm_clk_get(&pdev->dev, "pclk_mipi_dsi");
1934         if (unlikely(IS_ERR(dsi->dsi_pclk))) {
1935                 dev_err(&pdev->dev, "get pclk_mipi_dsi clock fail\n");
1936                 return PTR_ERR(dsi->dsi_pclk);
1937         }
1938
1939         if (dsi->ops.id == DSI_RK3368 ||
1940             dsi->ops.id == DSI_RK3366) {
1941                 /* Get the APB bus clk access mipi host */
1942                 dsi->dsi_host_pclk = devm_clk_get(&pdev->dev, "pclk_mipi_dsi_host");
1943                 if (unlikely(IS_ERR(dsi->dsi_host_pclk))) {
1944                         dev_err(&pdev->dev, "get pclk_mipi_dsi_host clock fail\n");
1945                         return PTR_ERR(dsi->dsi_host_pclk);
1946                 }
1947         }
1948
1949         if (dsi->ops.id == DSI_RK312x) {
1950                 /* Get the APB bus clk access mipi host */
1951                 dsi->dsi_host_pclk = devm_clk_get(&pdev->dev, "pclk_mipi_dsi_host");
1952                 if (unlikely(IS_ERR(dsi->dsi_host_pclk))) {
1953                         dev_err(&pdev->dev, "get pclk_mipi_dsi_host clock fail\n");
1954                         return PTR_ERR(dsi->dsi_host_pclk);
1955                 }
1956                 /* Get the pd_vio AHB h2p bridge clock */
1957                 dsi->h2p_hclk = devm_clk_get(&pdev->dev, "hclk_vio_h2p");
1958                 if (unlikely(IS_ERR(dsi->h2p_hclk))) {
1959                         dev_err(&pdev->dev, "get hclk_vio_h2p clock fail\n");
1960                         return PTR_ERR(dsi->h2p_hclk);
1961                 }
1962         }
1963
1964         if (dsi->ops.id == DSI_RK3399) {
1965                 /* Get mipi phy cfg clk */
1966                 dsi->dsi_host_pclk = devm_clk_get(&pdev->dev, "mipi_dphy_cfg");
1967                 if (unlikely(IS_ERR(dsi->dsi_host_pclk))) {
1968                         dev_err(&pdev->dev, "get mipi_dphy_cfg clock fail\n");
1969                         return PTR_ERR(dsi->dsi_host_pclk);
1970                 }
1971
1972                 dsi->grf_base = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
1973                 if (IS_ERR(dsi->grf_base)) {
1974                         dev_err(&pdev->dev, "can't find mipi grf property\n");
1975                         dsi->grf_base = NULL;
1976                 }
1977         }
1978
1979         dsi->host.irq = platform_get_irq(pdev, 0);
1980         if (dsi->host.irq < 0) {
1981                 dev_err(&pdev->dev, "no irq resource?\n");
1982                 return dsi->host.irq;
1983         }
1984         /*
1985         ret = request_irq(dsi->host.irq, rk32_mipi_dsi_irq_handler, 0,dev_name(&pdev->dev), dsi);
1986         if (ret) {
1987                 dev_err(&pdev->dev, "request mipi_dsi irq fail\n");
1988                 return -EINVAL;
1989         }
1990         */
1991         printk("dsi->host.irq =%d\n", dsi->host.irq);
1992
1993         disable_irq(dsi->host.irq);
1994
1995         screen = devm_kzalloc(&pdev->dev, sizeof(struct rk_screen), GFP_KERNEL);
1996         if (!screen) {
1997                 dev_err(&pdev->dev, "request struct rk_screen fail!\n");
1998                 return -1;
1999         }
2000         rk_fb_get_prmry_screen(screen);
2001
2002         dsi->pdev = pdev;
2003         ops = &dsi->ops;
2004         ops->dsi = dsi;
2005
2006         ops->get_id = rk32_mipi_dsi_get_id,
2007         ops->dsi_send_packet = rk32_mipi_dsi_send_packet;
2008         ops->dsi_read_dcs_packet = rk32_mipi_dsi_read_dcs_packet,
2009         ops->dsi_enable_video_mode = rk32_mipi_dsi_enable_video_mode,
2010         ops->dsi_enable_command_mode = rk32_mipi_dsi_enable_command_mode,
2011         ops->dsi_enable_hs_clk = rk32_mipi_dsi_enable_hs_clk,
2012         ops->dsi_is_active = rk32_mipi_dsi_is_active,
2013         ops->dsi_is_enable = rk32_mipi_dsi_is_enable,
2014         ops->power_up = rk32_mipi_dsi_power_up,
2015         ops->power_down = rk32_mipi_dsi_power_down,
2016         ops->dsi_init = rk_mipi_dsi_init,
2017
2018         dsi_screen = &dsi->screen;
2019         dsi_screen->screen = screen;
2020         dsi_screen->type = screen->type;
2021         dsi_screen->face = screen->face;
2022         dsi_screen->lcdc_id = screen->lcdc_id;
2023         dsi_screen->screen_id = screen->screen_id;
2024         dsi_screen->pixclock = screen->mode.pixclock;
2025         dsi_screen->left_margin = screen->mode.left_margin;
2026         dsi_screen->right_margin = screen->mode.right_margin;
2027         dsi_screen->hsync_len = screen->mode.hsync_len;
2028         dsi_screen->upper_margin = screen->mode.upper_margin;
2029         dsi_screen->lower_margin = screen->mode.lower_margin;
2030         dsi_screen->vsync_len = screen->mode.vsync_len;
2031         dsi_screen->x_res = screen->mode.xres;
2032         dsi_screen->y_res = screen->mode.yres;
2033         dsi_screen->pin_hsync = screen->pin_hsync;
2034         dsi_screen->pin_vsync = screen->pin_vsync;
2035         dsi_screen->pin_den = screen->pin_den;
2036         dsi_screen->pin_dclk = screen->pin_dclk;
2037         dsi_screen->dsi_lane = rk_mipi_get_dsi_lane();
2038         dsi_screen->dsi_lane = rk_mipi_get_dsi_lane();
2039         dsi_screen->hs_tx_clk = rk_mipi_get_dsi_clk();
2040         /* dsi_screen->lcdc_id = 1; */
2041         dsi_screen->refresh_mode = screen->refresh_mode;
2042
2043         dsi->dsi_id = id++;
2044
2045         sprintf(ops->name, "rk_mipi_dsi.%d", dsi->dsi_id);
2046         platform_set_drvdata(pdev, dsi);
2047 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
2048         /* enable power domain */
2049         pm_runtime_enable(&pdev->dev);
2050 #endif
2051
2052         register_dsi_ops(dsi->dsi_id, &dsi->ops);
2053
2054         if (id == 1) {
2055                 /*
2056                 if(!support_uboot_display())
2057                         rk32_init_phy_mode(dsi_screen->lcdc_id);
2058                 */
2059                 rk_fb_trsm_ops_register(&trsm_dsi_ops, SCREEN_MIPI);
2060 #ifdef MIPI_DSI_REGISTER_IO
2061                 debugfs_create_file("mipidsi0", S_IFREG | S_IRUGO, dsi->debugfs_dir, dsi,
2062                                                         &reg_proc_fops);
2063 #endif
2064                 dsi0 = dsi;
2065         } else {
2066                 dsi1 = dsi;
2067 #ifdef MIPI_DSI_REGISTER_IO
2068                 debugfs_create_file("mipidsi1", S_IFREG | S_IRUGO, dsi->debugfs_dir, dsi,
2069                                                         &reg_proc_fops1);
2070 #endif
2071         }
2072
2073         if (support_uboot_display()) {
2074                 clk_prepare_enable(dsi->phy.refclk);
2075                 clk_prepare_enable(dsi->dsi_pclk);
2076                 if (dsi->ops.id == DSI_RK312x) {
2077                         clk_prepare_enable(dsi->dsi_host_pclk);
2078                         clk_prepare_enable(dsi->h2p_hclk);
2079                 } else if (dsi->ops.id == DSI_RK3368 ||
2080                         dsi->ops.id == DSI_RK3366)
2081                         clk_prepare_enable(dsi->dsi_host_pclk);
2082                 else if (dsi->ops.id == DSI_RK3399)
2083                         clk_prepare_enable(dsi->dsi_host_pclk);
2084
2085                 dsi->clk_on = 1;
2086                 udelay(10);
2087         }
2088
2089         dev_info(&pdev->dev, "rk mipi_dsi probe success!\n");
2090         dev_info(&pdev->dev, "%s\n", RK_MIPI_DSI_VERSION_AND_TIME);
2091
2092         return ret;
2093 }
2094
2095 static int rockchip_mipi_remove(struct platform_device *pdev)
2096 {
2097 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
2098         pm_runtime_disable(&pdev->dev);
2099 #endif
2100         return 0;
2101 }
2102
2103 static struct platform_driver rk32_mipi_dsi_driver = {
2104         .probe = rk32_mipi_dsi_probe,
2105         .remove = rockchip_mipi_remove,
2106         .driver = {
2107                 .name = "rk32-mipi",
2108                 .owner = THIS_MODULE,
2109 #ifdef CONFIG_OF
2110                 .of_match_table = of_rk_mipi_dsi_match,
2111 #endif
2112         },
2113 };
2114
2115 static int __init rk32_mipi_dsi_init(void)
2116 {
2117         return platform_driver_register(&rk32_mipi_dsi_driver);
2118 }
2119 fs_initcall(rk32_mipi_dsi_init);
2120
2121 static void __exit rk32_mipi_dsi_exit(void)
2122 {
2123         platform_driver_unregister(&rk32_mipi_dsi_driver);
2124 }
2125 module_exit(rk32_mipi_dsi_exit);
2126 #endif