e49b1182eca9336c45b3076102efdb1cb15f8687
[firefly-linux-kernel-4.4.55.git] / drivers / video / rockchip / screen / lcd_mipi.c
1 /*
2  * Copyright (C) 2014 ROCKCHIP, Inc.
3  * drivers/video/rockchip/screen/lcd_mipi.c
4  * author: libing@rock-chips.com
5  * create date: 2014-04-10
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #ifndef CONFIG_LCD_MIPI
17 #include <common.h>
18 #endif
19 #ifdef CONFIG_LCD_MIPI
20 #include "../transmitter/mipi_dsi.h"
21 #include <linux/delay.h>
22 #endif
23 #ifdef CONFIG_RK_3288_DSI_UBOOT
24 #include <common.h>
25 #include <asm/io.h>
26 #include <errno.h>
27 #include <malloc.h>
28 #include <fdtdec.h>
29 #include <errno.h>
30 #include <asm/io.h>
31 #include <asm/arch/rkplat.h>
32 #include <lcd.h>
33 #include "../transmitter/mipi_dsi.h"
34 #endif
35
36 #ifdef CONFIG_RK_3288_DSI_UBOOT
37 #define MIPI_SCREEN_DBG(x...)   /* printf(x) */
38 #elif defined CONFIG_LCD_MIPI
39 #define MIPI_SCREEN_DBG(x...)   /* printk(KERN_ERR x) */
40 #else
41 #define MIPI_SCREEN_DBG(x...)
42 #endif
43 #ifdef CONFIG_RK_3288_DSI_UBOOT
44 DECLARE_GLOBAL_DATA_PTR;
45 #define msleep(a) udelay(a * 1000)
46 #define printk(x...)    /* printf(x) */
47 #endif
48 static struct mipi_screen *gmipi_screen;
49
50 static void rk_mipi_screen_pwr_disable(struct mipi_screen *screen)
51 {
52         if (screen->lcd_en_gpio != INVALID_GPIO) {
53                 gpio_direction_output(screen->lcd_en_gpio, !screen->lcd_en_atv_val);
54                 msleep(screen->lcd_en_delay);
55         } else{
56                 MIPI_SCREEN_DBG("lcd_en_gpio is null");
57         }
58
59         if (screen->lcd_rst_gpio != INVALID_GPIO) {
60
61                 gpio_direction_output(screen->lcd_rst_gpio, !screen->lcd_rst_atv_val);
62                 msleep(screen->lcd_rst_delay);
63         } else {
64                 MIPI_SCREEN_DBG("lcd_rst_gpio is null");
65         }
66 }
67
68 static void rk_mipi_screen_pwr_enable(struct mipi_screen *screen)
69 {
70         if (screen->lcd_en_gpio != INVALID_GPIO) {
71                 gpio_direction_output(screen->lcd_en_gpio, !screen->lcd_en_atv_val);
72                 msleep(screen->lcd_en_delay);
73                 gpio_direction_output(screen->lcd_en_gpio, screen->lcd_en_atv_val);
74                 msleep(screen->lcd_en_delay);
75         } else
76                 MIPI_SCREEN_DBG("lcd_en_gpio is null\n");
77
78         if (screen->lcd_rst_gpio != INVALID_GPIO) {
79                 gpio_direction_output(screen->lcd_rst_gpio, !screen->lcd_rst_atv_val);
80                 msleep (screen->lcd_rst_delay);
81                 gpio_direction_output(screen->lcd_rst_gpio, screen->lcd_rst_atv_val);
82                 msleep(screen->lcd_rst_delay);
83         } else
84                 MIPI_SCREEN_DBG("lcd_rst_gpio is null\n");
85 }
86
87 static void rk_mipi_screen_cmd_init(struct mipi_screen *screen)
88 {
89         u8 len, i;
90         u8 *cmds;
91         struct list_head *screen_pos;
92         struct mipi_dcs_cmd_ctr_list  *dcs_cmd;
93 #ifdef CONFIG_RK_3288_DSI_UBOOT
94         cmds = calloc(1, 0x400);
95         if (!cmds) {
96                 printf("request cmds fail!\n");
97                 return;
98         }
99 #endif
100
101 #ifdef CONFIG_LCD_MIPI
102         cmds = kmalloc(0x400, GFP_KERNEL);
103         if (!cmds) {
104                 printk("request cmds fail!\n");
105                 return ;
106         }
107 #endif
108         list_for_each(screen_pos, &screen->cmdlist_head) {
109                 dcs_cmd = list_entry(screen_pos, struct mipi_dcs_cmd_ctr_list, list);
110                 len = dcs_cmd->dcs_cmd.cmd_len + 1;
111                 for (i = 1; i < len ; i++) {
112                         cmds[i] = dcs_cmd->dcs_cmd.cmds[i-1];
113                 }
114                 MIPI_SCREEN_DBG("dcs_cmd.name:%s\n", dcs_cmd->dcs_cmd.name);
115                 if (dcs_cmd->dcs_cmd.type == LPDT) {
116                         cmds[0] = LPDT;
117                         if (dcs_cmd->dcs_cmd.dsi_id == 0) {
118                                 MIPI_SCREEN_DBG("dcs_cmd.dsi_id == 0 line=%d\n", __LINE__);
119                                 dsi_send_packet(0, cmds, len);
120                         } else if (dcs_cmd->dcs_cmd.dsi_id == 1) {
121                                 MIPI_SCREEN_DBG("dcs_cmd.dsi_id == 1 line=%d\n", __LINE__);
122                                 dsi_send_packet(1, cmds, len);
123                         } else if (dcs_cmd->dcs_cmd.dsi_id == 2) {
124                                 MIPI_SCREEN_DBG("dcs_cmd.dsi_id == 2 line=%d\n", __LINE__);
125                                 dsi_send_packet(0, cmds, len);
126                                 dsi_send_packet(1, cmds, len);
127                         } else {
128                                 MIPI_SCREEN_DBG("dsi is err.\n");
129                         }
130                         msleep(dcs_cmd->dcs_cmd.delay);
131                 } else if (dcs_cmd->dcs_cmd.type == HSDT) {
132                         cmds[0] = HSDT;
133                         if (dcs_cmd->dcs_cmd.dsi_id == 0) {
134                                 MIPI_SCREEN_DBG("dcs_cmd.dsi_id == 0 line=%d\n", __LINE__);
135                                 dsi_send_packet(0, cmds, len);
136                         } else if (dcs_cmd->dcs_cmd.dsi_id == 1) {
137                                 MIPI_SCREEN_DBG("dcs_cmd.dsi_id == 1 line=%d\n", __LINE__);
138                                 dsi_send_packet(1, cmds, len);
139                         } else if (dcs_cmd->dcs_cmd.dsi_id == 2) {
140                                 MIPI_SCREEN_DBG("dcs_cmd.dsi_id == 2 line=%d\n", __LINE__);
141                                 dsi_send_packet(0, cmds, len);
142                                 dsi_send_packet(1, cmds, len);
143                         } else {
144                                 MIPI_SCREEN_DBG("dsi is err.");
145                         }
146                         msleep(dcs_cmd->dcs_cmd.delay);
147                 } else
148                         MIPI_SCREEN_DBG("cmd type err.\n");
149         }
150
151 #ifdef CONFIG_RK_3288_DSI_UBOOT
152         free(cmds);
153 #endif
154 #ifdef CONFIG_LCD_MIPI
155         kfree(cmds);
156 #endif
157 }
158
159 int rk_mipi_screen(void)
160 {
161         u8 dcs[16] = {0}, rk_dsi_num;
162         rk_dsi_num = gmipi_screen->mipi_dsi_num;
163         if (gmipi_screen->screen_init == 0) {
164                 rk_mipi_screen_pwr_enable(gmipi_screen);
165                 dsi_enable_hs_clk(0, 1);
166                 if (rk_dsi_num == 2) {
167                         dsi_enable_hs_clk(1, 1);
168                 }
169
170                 dsi_enable_command_mode(0, 1);
171                 if (rk_dsi_num == 2) {
172                         dsi_enable_command_mode(1, 1);
173                 }
174
175                 dcs[0] = LPDT;
176                 dcs[1] = DTYPE_DCS_SWRITE_0P;
177                 dcs[2] = dcs_exit_sleep_mode;
178                 dsi_send_packet(0, dcs, 3);
179                 if (rk_dsi_num == 2)
180                         dsi_send_packet(1, dcs, 3);
181
182                 msleep(20);
183
184                 dcs[0] = LPDT;
185                 dcs[1] = DTYPE_DCS_SWRITE_0P;
186                 dcs[2] = dcs_set_display_on;
187                 dsi_send_packet(0, dcs, 3);
188                 if (rk_dsi_num == 2)
189                         dsi_send_packet(1, dcs, 3);
190
191                 msleep(20);
192         } else {
193                 rk_mipi_screen_pwr_enable(gmipi_screen);
194
195                 dsi_enable_hs_clk(0, 1);
196                 if (rk_dsi_num == 2) {
197                         dsi_enable_hs_clk(1, 1);
198                 }
199
200                 dsi_enable_command_mode(0, 1);
201                 if (rk_dsi_num == 2) {
202                         dsi_enable_command_mode(1, 1);
203                 }
204
205                 rk_mipi_screen_cmd_init(gmipi_screen);
206         }
207
208         MIPI_SCREEN_DBG("++++++++++++++++%s:%d\n", __func__, __LINE__);
209         return 0;
210 }
211
212 int rk_mipi_screen_standby(u8 enable)
213 {
214         u8 dcs[16] = {0}, rk_dsi_num;
215         rk_dsi_num = gmipi_screen->mipi_dsi_num;
216
217         if (dsi_is_active(0) != 1)
218                 return -1;
219
220         if (rk_dsi_num == 2)
221                 if ((dsi_is_active(0) != 1) || (dsi_is_active(1) != 1))
222                         return -1;
223
224         if (enable) {
225                 /* below is changeable */
226                 dcs[0] = LPDT;
227                 dcs[1] = DTYPE_DCS_SWRITE_0P;
228                 dcs[2] = dcs_set_display_off;
229                 dsi_send_packet(0, dcs, 3);
230                 if (rk_dsi_num == 2)
231                         dsi_send_packet(1, dcs, 3);
232
233                 msleep(30);
234
235                 dcs[0] = LPDT;
236                 dcs[1] = DTYPE_DCS_SWRITE_0P;
237                 dcs[2] = dcs_enter_sleep_mode;
238                 dsi_send_packet(0, dcs, 3);
239                 if (rk_dsi_num == 2)
240                         dsi_send_packet(1, dcs, 3);
241
242                 msleep(100);
243                 rk_mipi_screen_pwr_disable(gmipi_screen);
244                 MIPI_SCREEN_DBG("++++enable++++++++++++%s:%d\n", __func__, __LINE__);
245         } else {
246                 rk_mipi_screen();
247         }
248         return 0;
249 }
250 #ifdef CONFIG_LCD_MIPI
251 static int rk_mipi_screen_init_dt(struct device *dev,
252                                   struct mipi_screen *screen)
253 {
254         struct device_node *childnode, *grandchildnode, *root;
255         struct mipi_dcs_cmd_ctr_list *dcs_cmd;
256         struct list_head *pos;
257         struct property *prop;
258         enum of_gpio_flags flags;
259         u32 value, i, debug, gpio, ret, length;
260
261         memset(screen, 0, sizeof(*screen));
262
263         INIT_LIST_HEAD(&screen->cmdlist_head);
264
265         childnode = of_find_node_by_name(NULL, "mipi_dsi_init");
266         if (!childnode) {
267                 MIPI_SCREEN_DBG("%s: Can not get child => mipi_init.\n", __func__);
268         } else {
269                 ret = of_property_read_u32(childnode, "rockchip,screen_init", &value);
270                 if (ret) {
271                         MIPI_SCREEN_DBG("%s: Can not read property: screen_init.\n", __func__);
272                 } else {
273                         if ((value != 0) && (value != 1)) {
274                                 printk("err: rockchip,mipi_dsi_init not match.\n");
275                                 return -1;
276                         } else
277                                 screen->screen_init = value ;
278
279                         MIPI_SCREEN_DBG("%s: lcd->screen_init = %d.\n", __func__, screen->screen_init);
280                 }
281
282                 ret = of_property_read_u32(childnode, "rockchip,dsi_lane", &value);
283                 if (ret) {
284                         MIPI_SCREEN_DBG("%s: Can not read property: dsi_lane.\n", __func__);
285                 } else {
286                         screen->dsi_lane = value;
287                         MIPI_SCREEN_DBG("%s: mipi_lcd->dsi_lane = %d.\n", __func__, screen->dsi_lane);
288                 }
289
290                 ret = of_property_read_u32(childnode, "rockchip,dsi_hs_clk", &value);
291                 if (ret) {
292                         MIPI_SCREEN_DBG("%s: Can not read property: dsi_hs_clk.\n", __func__);
293                 } else {
294                         if ((value <= 90) || (value >= 1500)) {
295                                 printk("err: rockchip,hs_tx_clk not match.");
296                                 return -1;
297                         } else {
298                                 screen->hs_tx_clk = value*MHz;
299                         }
300
301                         MIPI_SCREEN_DBG("%s: lcd->screen->hs_tx_clk = %d.\n", __func__, screen->hs_tx_clk);
302                 }
303
304                 ret = of_property_read_u32(childnode, "rockchip,mipi_dsi_num", &value);
305                 if (ret) {
306                         MIPI_SCREEN_DBG("%s: Can not read property: mipi_dsi_num.\n", __func__);
307                 } else {
308                         if ((value != 1) && (value != 2)) {
309                                 printk("err: rockchip,mipi_dsi_num not match.\n");
310                                 return -1;
311                         } else {
312                                 screen->mipi_dsi_num = value ;
313                         }
314
315                         MIPI_SCREEN_DBG("%s: lcd->screen.mipi_dsi_num = %d.\n", __func__, screen->mipi_dsi_num);
316                 }
317         }
318
319         childnode = of_find_node_by_name(NULL, "mipi_power_ctr");
320         if (!childnode) {
321                 screen->lcd_rst_gpio = INVALID_GPIO;
322                 screen->lcd_en_gpio = INVALID_GPIO;
323                 MIPI_SCREEN_DBG("%s: Can not get child => mipi_power_ctr.\n", __func__);
324         } else {
325                 grandchildnode = of_find_node_by_name(childnode, "mipi_lcd_rst");
326                 if (!grandchildnode) {
327                         screen->lcd_rst_gpio = INVALID_GPIO;
328                         MIPI_SCREEN_DBG("%s: Can not read property: mipi_lcd_rst.\n", __func__);
329                 } else {
330                         ret = of_property_read_u32(grandchildnode, "rockchip,delay", &value);
331                         if (ret) {
332                                 MIPI_SCREEN_DBG("%s: Can not read property: delay.\n", __func__);
333                         } else {
334                                 screen->lcd_rst_delay = value;
335                                 MIPI_SCREEN_DBG("%s: lcd->screen->lcd_rst_delay = %d.\n", __func__, screen->lcd_rst_delay);
336                         }
337
338                         gpio = of_get_named_gpio_flags(grandchildnode, "rockchip,gpios", 0, &flags);
339                         if (!gpio_is_valid(gpio)) {
340                                 MIPI_SCREEN_DBG("rest: Can not read property: %s->gpios.\n", __func__);
341                         }
342
343                         ret = gpio_request(gpio, "mipi_lcd_rst");
344                         if (ret) {
345                                 screen->lcd_rst_gpio = INVALID_GPIO;
346                                 MIPI_SCREEN_DBG("request mipi_lcd_rst gpio fail:%d\n", gpio);
347                                 return -1;
348                         }
349
350                         screen->lcd_rst_gpio = gpio;
351                         screen->lcd_rst_atv_val = (flags == GPIO_ACTIVE_HIGH) ? 1:0;
352
353                         MIPI_SCREEN_DBG("lcd->lcd_rst_gpio=%d,dsi->lcd_rst_atv_val=%d\n", screen->lcd_rst_gpio, screen->lcd_rst_atv_val);
354                 }
355
356                 grandchildnode = of_find_node_by_name(childnode, "mipi_lcd_en");
357                 if (!grandchildnode) {
358                         screen->lcd_en_gpio = INVALID_GPIO;
359                         MIPI_SCREEN_DBG("%s: Can not read property: mipi_lcd_en.\n", __func__);
360                 } else {
361                         ret = of_property_read_u32(grandchildnode, "rockchip,delay", &value);
362                         if (ret) {
363                                 MIPI_SCREEN_DBG("%s: Can not read property: mipi_lcd_en-delay.\n", __func__);
364                         } else {
365                                 screen->lcd_en_delay = value;
366                                 MIPI_SCREEN_DBG("%s: lcd->screen.lcd_en_delay = %d.\n", __func__, screen->lcd_en_delay);
367                         }
368
369                         gpio = of_get_named_gpio_flags(grandchildnode, "rockchip,gpios", 0, &flags);
370                         if (!gpio_is_valid(gpio)) {
371                                 MIPI_SCREEN_DBG("rest: Can not read property: %s->gpios.\n", __func__);
372                         }
373
374                         ret = gpio_request(gpio, "mipi_lcd_en");
375                         if (ret) {
376                                 screen->lcd_en_gpio = INVALID_GPIO;
377                                 MIPI_SCREEN_DBG("request mipi_lcd_en gpio fail:%d\n", gpio);
378                                 return -1;
379                         }
380                         screen->lcd_en_gpio = gpio;
381                         screen->lcd_en_atv_val = (flags == GPIO_ACTIVE_HIGH) ? 1:0;
382                         MIPI_SCREEN_DBG("dsi->lcd_en_gpio=%d, dsi->screen.lcd_en_atv_val=%d\n", screen->lcd_en_gpio, screen->lcd_en_atv_val);
383                 }
384         }
385
386         root = of_find_node_by_name(NULL, "screen-on-cmds");
387         if (!root) {
388                 MIPI_SCREEN_DBG("can't find screen-on-cmds node\n");
389         } else {
390                 for_each_child_of_node(root, childnode) {
391                         dcs_cmd = kmalloc(sizeof(struct mipi_dcs_cmd_ctr_list), GFP_KERNEL);
392                         strcpy(dcs_cmd->dcs_cmd.name, childnode->name);
393
394                         dcs_cmd->dcs_cmd.cmds =
395                                 devm_kzalloc(dev, CMD_LEN_MAX, GFP_KERNEL);
396                         if (!dcs_cmd->dcs_cmd.cmds) {
397                                 pr_err("malloc cmds fail!\n");
398                                 return -ENOMEM;
399                         }
400
401                         prop = of_find_property(childnode, "rockchip,cmd", &length);
402                         if (!prop) {
403                                 MIPI_SCREEN_DBG("Can not read property: cmds\n");
404                                 kfree(dcs_cmd->dcs_cmd.cmds);
405                                 dcs_cmd->dcs_cmd.cmds = NULL;
406                                 return -EINVAL;
407                         }
408
409                         if ((length / sizeof(u32)) > CMD_LEN_MAX) {
410                                 /* the length can not longer than the cmds arrary in struct dcs_cmds */
411                                 MIPI_SCREEN_DBG("error: the dcs cmd length is %d, but the max length supported is %d\n",
412                                                 length / sizeof(u32),
413                                                 CMD_LEN_MAX);
414                         }
415                         MIPI_SCREEN_DBG("\n childnode->name =%s:length=%d\n", childnode->name, (length / sizeof(u32)));
416
417                         ret = of_property_read_u32_array(childnode,
418                                                          "rockchip,cmd",
419                                                          dcs_cmd->dcs_cmd.cmds,
420                                                          length / sizeof(u32));
421                         if (ret < 0) {
422                                 MIPI_SCREEN_DBG("%s: Can not read property: %s--->cmds\n", __func__, childnode->name);
423                                 kfree(dcs_cmd->dcs_cmd.cmds);
424                                 dcs_cmd->dcs_cmd.cmds = NULL;
425                                 return ret;
426                         } else {
427                                 dcs_cmd->dcs_cmd.cmd_len =  length / sizeof(u32);
428                         }
429                         ret = of_property_read_u32(childnode, "rockchip,dsi_id", &value);
430                         if (ret) {
431                                 MIPI_SCREEN_DBG("%s: Can not read property: %s--->cmd_type\n", __func__, childnode->name);
432                         } else {
433                                 if (screen->mipi_dsi_num == 1) {
434                                         if (value != 0) {
435                                                 printk("err: rockchip,dsi_id not match.\n");
436                                         } else {
437                                                 dcs_cmd->dcs_cmd.dsi_id = value;
438                                         }
439                                 } else {
440                                         if ((value < 0) || (value > 2))
441                                                 printk("err: rockchip,dsi_id not match.\n");
442                                         else
443                                                 dcs_cmd->dcs_cmd.dsi_id = value;
444                                 }
445                         }
446
447                         ret = of_property_read_u32(childnode, "rockchip,cmd_type", &value);
448                         if (ret) {
449                                 MIPI_SCREEN_DBG("%s: Can not read property: %s--->cmd_type\n", __func__, childnode->name);
450                         } else {
451                                 if ((value != 0) && (value != 1)) {
452                                         printk("err: rockchip, cmd_type not match.\n");
453                                 } else {
454                                         dcs_cmd->dcs_cmd.type = value;
455                                 }
456                         }
457
458                         ret = of_property_read_u32(childnode, "rockchip,cmd_delay", &value);
459                         if (ret)
460                                 MIPI_SCREEN_DBG("%s: Can not read property: %s--->cmd_delay\n", __func__, childnode->name);
461                         else
462                                 dcs_cmd->dcs_cmd.delay = value;
463
464                         list_add_tail(&dcs_cmd->list, &screen->cmdlist_head);
465                 }
466         }
467         ret = of_property_read_u32(root, "rockchip,cmd_debug", &debug);
468         if (ret) {
469                 MIPI_SCREEN_DBG("%s: Can not read property: rockchip,cmd_debug.\n", __func__);
470         } else {
471                 if (debug) {
472                         list_for_each(pos, &screen->cmdlist_head) {
473                                 dcs_cmd = list_entry(pos, struct mipi_dcs_cmd_ctr_list, list);
474                                 printk("\n dcs_name:%s,dcs_type:%d,side_id:%d,cmd_len:%d,delay:%d\n\n",
475                                         dcs_cmd->dcs_cmd.name,
476                                         dcs_cmd->dcs_cmd.type,
477                                         dcs_cmd->dcs_cmd.dsi_id,
478                                         dcs_cmd->dcs_cmd.cmd_len,
479                                         dcs_cmd->dcs_cmd.delay);
480                                 for (i = 0; i < (dcs_cmd->dcs_cmd.cmd_len); i++) {
481                                         printk("[%d]=%02x,", i+1, dcs_cmd->dcs_cmd.cmds[i]);
482                                 }
483                         }
484                 } else {
485                         MIPI_SCREEN_DBG("---close cmd debug---\n");
486                 }
487         }
488         return 0;
489 }
490 #endif
491 int rk_mipi_get_dsi_num(void)
492 {
493         return gmipi_screen->mipi_dsi_num;
494 }
495 #ifdef CONFIG_LCD_MIPI
496 EXPORT_SYMBOL(rk_mipi_get_dsi_num);
497 #endif
498
499 int rk_mipi_get_dsi_lane(void)
500 {
501         return gmipi_screen->dsi_lane;
502 }
503 #ifdef CONFIG_LCD_MIPI
504 EXPORT_SYMBOL(rk_mipi_get_dsi_lane);
505 #endif
506
507 int rk_mipi_get_dsi_clk(void)
508 {
509         return gmipi_screen->hs_tx_clk;
510 }
511 #ifdef CONFIG_LCD_MIPI
512 EXPORT_SYMBOL(rk_mipi_get_dsi_clk);
513 #endif
514 #ifdef CONFIG_RK_3288_DSI_UBOOT
515 #ifdef CONFIG_OF_LIBFDT
516 static int rk_mipi_screen_init_dt(struct mipi_screen *screen)
517 {
518         struct mipi_dcs_cmd_ctr_list *dcs_cmd;
519         u32 i;
520         int length;
521         int err;
522         int node;
523         const void *blob;
524         struct fdt_gpio_state gpio_val;
525         int noffset;
526
527         INIT_LIST_HEAD(&screen->cmdlist_head);
528
529         blob = gd->fdt_blob; /* getenv_hex("fdtaddr", 0); */
530         node = fdtdec_next_compatible(blob, 0, COMPAT_ROCKCHIP_MIPI_INIT);
531         if (node < 0) {
532                 MIPI_SCREEN_DBG("Can not get node of COMPAT_ROCKCHIP_MIPI_INIT\n");
533         }
534         screen->screen_init = fdtdec_get_int(blob, node, "rockchip,screen_init", -1);
535         if (screen->screen_init < 0) {
536                 MIPI_SCREEN_DBG("Can not get screen_init\n");
537         }
538         screen->dsi_lane = fdtdec_get_int(blob, node, "rockchip,dsi_lane", -1);
539         if (screen->dsi_lane < 0) {
540                 MIPI_SCREEN_DBG("Can not get dsi_lane\n");
541         }
542         screen->hs_tx_clk = fdtdec_get_int(blob, node, "rockchip,dsi_hs_clk", -1);
543         if (screen->hs_tx_clk < 0) {
544                 MIPI_SCREEN_DBG("Can not get dsi_hs_clk\n");
545         } else {
546                 screen->hs_tx_clk = screen->hs_tx_clk*MHZ;
547         }
548         screen->mipi_dsi_num = fdtdec_get_int(blob, node, "rockchip,mipi_dsi_num", -1);
549         if (screen->mipi_dsi_num < 0) {
550                 MIPI_SCREEN_DBG("Can't get mipi_dsi_num\n");
551         }
552 #if 0
553         node = fdtdec_next_compatible(blob, 0, COMPAT_ROCKCHIP_MIPI_PWR);
554         if (node < 0) {
555                 printf("Can not get node of COMPAT_ROCKCHIP_MIPI_PWR\n");
556         }
557 #endif
558
559 #if 0
560 /*get the lcd rst status
561         handle = fdt_getprop_u32_default(blob, "/mipi_power_ctr", "mipi_lcd_rst", -1);
562         node = fdt_node_offset_by_phandle(blob, handle);
563 */
564         node = fdtdec_next_compatible(blob, 0, COMPAT_ROCKCHIP_MIPI_PWR);
565         if (node < 0) {
566                 printf("Can not get node of COMPAT_ROCKCHIP_MIPI_PWR\n");
567         } else {
568                 subnode = fdtdec_next_compatible_subnode(blob, node,
569                                 COMPAT_ROCKCHIP_MIPI_LCD_RST, &depth);
570                 if (subnode <= 0) {
571                         screen->lcd_rst_gpio = INVALID_GPIO;
572                         printf("Can't get pin of mipi_lcd_rst\n");
573                 } else {
574                         err = fdtdec_decode_gpio(blob, subnode, "rockchip,gpios", &gpio_val);
575                         gpio_val.gpio = rk_gpio_base_to_bank(gpio_val.gpio & RK_GPIO_BANK_MASK) | (gpio_val.gpio & RK_GPIO_PIN_MASK);
576                         if (err < 0) {
577                                 screen->lcd_rst_gpio = INVALID_GPIO;
578                                 printf("Can't find GPIO rst\n");
579                         } else {
580                                 screen->lcd_rst_gpio = gpio_val.gpio;
581                                 screen->lcd_rst_atv_val = !(gpio_val.flags & OF_GPIO_ACTIVE_LOW);
582                         }
583                         screen->lcd_rst_delay = fdtdec_get_int(blob, subnode, "rockchip,delay", -1);
584                         if (screen->lcd_rst_delay < 0) {
585                                 printf("Can't get delay of rst delay\n");
586                         }
587                         printf("Get lcd rst gpio and delay successfully!\n");
588                 }
589         }
590 #endif
591         /* get the lcd rst & en status */
592         node = fdtdec_next_compatible(blob, 0, COMPAT_ROCKCHIP_MIPI_PWR);
593         if (node < 0) {
594                 MIPI_SCREEN_DBG("Can not get node of COMPAT_ROCKCHIP_MIPI_PWR\n");
595         } else {
596 #if 0
597                 noffset = fdt_first_subnode(blob, node);
598                 const char *name = fdt_get_name(blob, noffset, NULL);
599                 printf("XJH_DEBUG1:%s\n", name);
600                 noffset = fdt_next_subnode(blob, noffset);
601                 const char *name1 = fdt_get_name(blob, noffset, NULL);
602                 printf("XJH_DEBUG2:%s\n", name1);
603 #endif
604                 for (noffset = fdt_first_subnode(blob, node);
605                 noffset >= 0;
606                 noffset = fdt_next_subnode(blob, noffset)) {
607                         if (0 == fdt_node_check_compatible(blob, noffset, "rockchip,lcd_rst")) {
608                                 err = fdtdec_decode_gpio(blob, noffset, "rockchip,gpios", &gpio_val);
609                                 gpio_val.gpio = rk_gpio_base_to_bank(gpio_val.gpio & RK_GPIO_BANK_MASK) | (gpio_val.gpio & RK_GPIO_PIN_MASK);
610                                 if (err < 0) {
611                                         screen->lcd_rst_gpio = INVALID_GPIO;
612                                         MIPI_SCREEN_DBG("Can't find GPIO rst\n");
613                                 } else {
614                                         screen->lcd_rst_gpio = gpio_val.gpio;
615                                         screen->lcd_rst_atv_val = !(gpio_val.flags & OF_GPIO_ACTIVE_LOW);
616                                 }
617                                 screen->lcd_rst_delay = fdtdec_get_int(blob, noffset, "rockchip,delay", -1);
618                                 if (screen->lcd_rst_delay < 0) {
619                                         MIPI_SCREEN_DBG("Can't get delay of rst delay\n");
620                                 }
621                                 MIPI_SCREEN_DBG("Get lcd rst gpio and delay successfully!\n");
622                         }
623                         if (0 == fdt_node_check_compatible(blob, noffset, "rockchip,lcd_en")) {
624                                 err = fdtdec_decode_gpio(blob, noffset, "rockchip,gpios", &gpio_val);
625                                 gpio_val.gpio = rk_gpio_base_to_bank(gpio_val.gpio & RK_GPIO_BANK_MASK) | (gpio_val.gpio & RK_GPIO_PIN_MASK);
626                                 if (err < 0) {
627                                         screen->lcd_en_gpio = INVALID_GPIO;
628                                         MIPI_SCREEN_DBG("Can't find GPIO en\n");
629                                 } else {
630                                         screen->lcd_en_gpio = gpio_val.gpio;
631                                         screen->lcd_en_atv_val = !(gpio_val.flags & OF_GPIO_ACTIVE_LOW);
632                                 }
633                                 screen->lcd_en_delay = fdtdec_get_int(blob, noffset, "rockchip,delay", -1);
634                                 if (screen->lcd_en_delay < 0) {
635                                         MIPI_SCREEN_DBG("Can't get delay of lcd_en delay\n");
636                                 }
637                                 MIPI_SCREEN_DBG("Get lcd en gpio and delay successfully:delay %d!\n", screen->lcd_en_delay);
638                         }
639                 }
640         }
641
642         /*get the initial command list*/
643         node = fdtdec_next_compatible(blob, 0, COMPAT_ROCKCHIP_MIPI_SONCMDS);
644         if (node < 0) {
645                 MIPI_SCREEN_DBG("Can not get node of COMPAT_ROCKCHIP_MIPI_SONCMDS\n");
646         } else {
647                 for (noffset = fdt_first_subnode(blob, node);
648                 noffset >= 0;
649                 noffset = fdt_next_subnode(blob, noffset)) {
650
651                         MIPI_SCREEN_DBG("build MIPI LCD init cmd tables\n");
652                         /*
653                         subnode = fdtdec_next_compatible_subnode(blob, node,
654                                         COMPAT_ROCKCHIP_MIPI_ONCMDS, &depth);
655                         if (noffset < 0)
656                                 break;
657                         */
658                         dcs_cmd = calloc(1, sizeof(struct mipi_dcs_cmd_ctr_list));
659                         /* node = fdt_node_offset_by_phandle(blob, handle); */
660                         strcpy(dcs_cmd->dcs_cmd.name, fdt_get_name(blob, noffset, NULL));
661                         MIPI_SCREEN_DBG("%s\n", dcs_cmd->dcs_cmd.name);
662                         dcs_cmd->dcs_cmd.type = fdtdec_get_int(blob, noffset, "rockchip,cmd_type", -1);
663                         MIPI_SCREEN_DBG("dcs_cmd.type=%02x\n", dcs_cmd->dcs_cmd.type);
664                         dcs_cmd->dcs_cmd.dsi_id = fdtdec_get_int(blob, noffset, "rockchip,dsi_id", -1);
665                         MIPI_SCREEN_DBG("dcs_cmd.dsi_id=%02x\n", dcs_cmd->dcs_cmd.dsi_id);
666                         fdt_getprop(blob, noffset, "rockchip,cmd", &length);
667                         dcs_cmd->dcs_cmd.cmd_len = length / sizeof(u32) ;
668
669                         dcs_cmd->dcs_cmd.cmds = calloc(1, CMD_LEN_MAX);
670                         if (!dcs_cmd->dcs_cmd.cmds) {
671                                 pr_err("calloc cmds fail!\n");
672                                 return -1;
673                         }
674
675                         err = fdtdec_get_int_array(blob, noffset,
676                                                    "rockchip,cmd",
677                                                    dcs_cmd->dcs_cmd.cmds,
678                                                    dcs_cmd->dcs_cmd.cmd_len);
679                         dcs_cmd->dcs_cmd.delay = fdtdec_get_int(blob, noffset, "rockchip,cmd_delay", -1);
680                         MIPI_SCREEN_DBG("dcs_cmd.delay=%d\n", dcs_cmd->dcs_cmd.delay);
681                         list_add_tail(&dcs_cmd->list, &screen->cmdlist_head);
682                 }
683         }
684         return 0;
685 }
686 #endif /* CONFIG_OF_LIBFDT */
687
688 int rk_mipi_screen_probe(void)
689 {
690         int ret = 0;
691         gmipi_screen = calloc(1, sizeof(struct mipi_screen));
692         if (!gmipi_screen) {
693                 printf("request struct screen fail!\n");
694                 return -ENOMEM;
695         }
696 #ifdef CONFIG_OF_LIBFDT
697         ret = rk_mipi_screen_init_dt(gmipi_screen);
698         if (ret < 0) {
699                 printf(" rk_mipi_screen_init_dt fail!\n");
700                 return -1;
701         }
702 #endif /* CONFIG_OF_LIBFDT */
703
704         MIPI_SCREEN_DBG("---rk_mipi_screen_probe--end\n");
705         return 0;
706 }
707
708 #endif /* CONFIG_RK_3288_DSI_UBOOT */
709 #ifdef CONFIG_LCD_MIPI
710 static int __init rk_mipi_screen_probe(struct platform_device *pdev)
711 {
712         static int ret;
713
714         gmipi_screen = devm_kzalloc(&pdev->dev, sizeof(struct mipi_screen), GFP_KERNEL);
715         if (!gmipi_screen) {
716                 dev_err(&pdev->dev, "request struct screen fail!\n");
717                 return -ENOMEM;
718         }
719
720         ret = rk_mipi_screen_init_dt(&pdev->dev, gmipi_screen);
721         if (ret < 0) {
722                 dev_err(&pdev->dev, " rk_mipi_screen_init_dt fail!\n");
723                 return -1;
724         }
725
726         MIPI_SCREEN_DBG("---rk_mipi_screen_probe--end\n");
727         return 0;
728 }
729
730 static struct platform_driver mipi_screen_platform_driver = {
731         .driver = {
732                 .name = "rk_mipi_screen",
733         },
734 };
735
736 static int __init rk_mipi_screen_init(void)
737 {
738         platform_device_register_simple("rk_mipi_screen", -1, NULL, 0);
739         return platform_driver_probe(&mipi_screen_platform_driver, rk_mipi_screen_probe);
740 }
741
742 static void __exit rk_mipi_screen_exit(void)
743 {
744         platform_driver_unregister(&mipi_screen_platform_driver);
745 }
746
747 subsys_initcall_sync(rk_mipi_screen_init);
748 module_exit(rk_mipi_screen_exit);
749 #endif