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