]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/karo/common/fdt.c
board: karo: make vbus-supply property name depend on board type
[karo-tx-uboot.git] / board / karo / common / fdt.c
1 /*
2  * (C) Copyright 2012,2013 Lothar Waßmann <LW@KARO-electronics.de>
3  *
4  * See file CREDITS for list of people who contributed to this
5  * project.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
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
18 #include <common.h>
19 #include <errno.h>
20 #include <libfdt.h>
21 #include <fdt_support.h>
22 #include <nand.h>
23 #include <mxcfb.h>
24 #include <linux/list.h>
25 #include <linux/fb.h>
26 #include <jffs2/load_kernel.h>
27 #include <malloc.h>
28
29 #include "karo.h"
30
31 #ifdef CONFIG_MAX_DTB_SIZE
32 #define MAX_DTB_SIZE    CONFIG_MAX_DTB_SIZE
33 #else
34 #define MAX_DTB_SIZE    SZ_64K
35 #endif
36
37 DECLARE_GLOBAL_DATA_PTR;
38
39 static void karo_set_fdtsize(void *fdt)
40 {
41         size_t fdtsize = getenv_ulong("fdtsize", 16, 0);
42
43         if (fdtsize == fdt_totalsize(fdt)) {
44                 return;
45         }
46         debug("FDT size changed from %u to %u\n",
47                 fdtsize, fdt_totalsize(fdt));
48         setenv_hex("fdtsize", fdt_totalsize(fdt));
49 }
50
51 static void *karo_fdt_load_dtb(void)
52 {
53         int ret;
54         void *fdt = (void *)getenv_ulong("fdtaddr", 16, CONFIG_SYS_FDT_ADDR);
55
56         if (had_ctrlc()) {
57                 printf("aborting DTB load\n");
58                 return NULL;
59         }
60
61         /* clear FDT header in memory */
62         memset(fdt, 0, 4);
63
64         ret = karo_load_part("dtb", fdt, MAX_DTB_SIZE);
65         if (ret) {
66                 printf("Failed to load dtb from flash: %d\n", ret);
67                 return NULL;
68         }
69
70         if (fdt_check_header(fdt)) {
71                 debug("No valid DTB in flash\n");
72                 return NULL;
73         }
74         debug("Using DTB from flash\n");
75         karo_set_fdtsize(fdt);
76         return fdt;
77 }
78
79 void karo_fdt_move_fdt(void)
80 {
81         void *fdt;
82         unsigned long fdt_addr = getenv_ulong("fdtaddr", 16, 0);
83
84         if (working_fdt) {
85                 debug("DTB already loaded\n");
86                 return;
87         }
88
89         setenv("fdtsize", 0);
90
91         if (!fdt_addr) {
92                 fdt_addr = CONFIG_SYS_FDT_ADDR;
93                 printf("fdtaddr is not set; using default: %08lx\n",
94                         fdt_addr);
95         }
96
97         fdt = karo_fdt_load_dtb();
98         if (fdt == NULL) {
99                 fdt = (void *)gd->fdt_blob;
100                 if (fdt == NULL) {
101 #ifdef CONFIG_OF_EMBED
102                         printf("Compiled in FDT not found");
103 #else
104                         printf("No FDT found");
105 #endif
106                         printf("; creating empty DTB\n");
107                         fdt = (void *)fdt_addr;
108                         fdt_create_empty_tree(fdt, 256);
109                 } else {
110                         printf("No DTB in flash; using default DTB\n");
111                 }
112                 debug("Checking FDT header @ %p\n", fdt);
113                 if (fdt_check_header(fdt)) {
114                         printf("ERROR: No valid DTB found at %p\n", fdt);
115                         return;
116                 }
117                 debug("Moving FDT from %p..%p to %08lx..%08lx\n",
118                         fdt, fdt + fdt_totalsize(fdt) - 1,
119                         fdt_addr, fdt_addr + fdt_totalsize(fdt) - 1);
120                 memmove((void *)fdt_addr, fdt, fdt_totalsize(fdt));
121         }
122         set_working_fdt_addr((void *)fdt_addr);
123         gd->fdt_blob = fdt;
124         karo_set_fdtsize(fdt);
125 }
126
127 void karo_fdt_remove_node(void *blob, const char *node)
128 {
129         int off = fdt_path_offset(blob, node);
130         int ret;
131
132         debug("Removing node '%s' from DT\n", node);
133
134         if (off < 0) {
135                 printf("Could not find node '%s': %s\n", node,
136                         fdt_strerror(off));
137         } else {
138                 ret = fdt_del_node(blob, off);
139                 if (ret)
140                         printf("Failed to remove node '%s': %s\n",
141                                 node, fdt_strerror(ret));
142         }
143         karo_set_fdtsize(blob);
144 }
145
146 void karo_fdt_enable_node(void *blob, const char *node, int enable)
147 {
148         int off = fdt_path_offset(blob, node);
149
150         debug("%sabling node '%s'\n", enable ? "En" : "Dis", node);
151         if (off < 0) {
152                 printf("Could not find node '%s': %s\n", node,
153                         fdt_strerror(off));
154                 return;
155         }
156         fdt_set_node_status(blob, off,
157                         enable ? FDT_STATUS_OKAY : FDT_STATUS_DISABLED, 0);
158
159         karo_set_fdtsize(blob);
160 }
161
162 static void fdt_disable_tp_node(void *blob, const char *name)
163 {
164         int offs = fdt_node_offset_by_compatible(blob, -1, name);
165
166         while (offs >= 0) {
167                 debug("Disabling node '%s'\n", name);
168                 fdt_set_node_status(blob, offs, FDT_STATUS_DISABLED, 0);
169                 offs = fdt_node_offset_by_compatible(blob, offs, name);
170         }
171 }
172
173 void karo_fdt_fixup_touchpanel(void *blob, const char *panels[],
174                         size_t num_panels)
175 {
176         int i;
177         const char *model = getenv("touchpanel");
178
179         for (i = 0; i < num_panels; i++) {
180                 const char *tp = panels[i];
181
182                 if (model != NULL) {
183                         if (strcmp(model, tp) == 0)
184                                 continue;
185                         while (tp != NULL) {
186                                 if (*tp != '\0' && strcmp(model, tp + 1) == 0)
187                                         break;
188                                 tp = strpbrk(tp + 1, ",-");
189                         }
190                         if (tp != NULL)
191                                 continue;
192                 }
193                 fdt_disable_tp_node(blob, panels[i]);
194         }
195         karo_set_fdtsize(blob);
196 }
197
198 static int karo_fdt_disable_node_phandle(void *blob, const char *parent,
199                                         const char *name)
200 {
201         const uint32_t *ph;
202         int off;
203
204         off = fdt_path_offset(blob, parent);
205         if (off < 0) {
206                 printf("Failed to find node '%s'\n", parent);
207                 return off;
208         }
209
210         ph = fdt_getprop(blob, off, name, NULL);
211         if (ph == NULL) {
212                 debug("Failed to find '%s' phandle in node '%s'\n", name,
213                         fdt_get_name(blob, off, NULL));
214                 return -FDT_ERR_NOTFOUND;
215         }
216
217         off = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*ph));
218         if (off <= 0) {
219                 printf("Failed to find '%s' node via phandle %04x\n",
220                         name, fdt32_to_cpu(*ph));
221                 return -FDT_ERR_NOTFOUND;
222         }
223         return fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
224 }
225
226 void karo_fdt_fixup_usb_otg(void *blob, const char *node, const char *phy,
227                         const char *phy_supply)
228 {
229         const char *otg_mode = getenv("otg_mode");
230         int off;
231         int ret;
232         int disable_otg = 0;
233         int disable_phy_pins = 1;
234
235         debug("OTG mode is '%s'\n", otg_mode ? otg_mode : "<UNSET>");
236
237         off = fdt_path_offset(blob, node);
238         if (off < 0) {
239                 debug("Failed to find node %s\n", node);
240                 return;
241         }
242
243         if (otg_mode && (strcmp(otg_mode, "device") == 0 ||
244                                 strcmp(otg_mode, "gadget") == 0)) {
245                 debug("Setting dr_mode to 'peripheral'\n");
246                 ret = fdt_setprop_string(blob, off, "dr_mode", "peripheral");
247         } else if (otg_mode && strcmp(otg_mode, "host") == 0) {
248                 debug("Setting dr_mode to 'host'\n");
249                 ret = fdt_setprop_string(blob, off, "dr_mode", "host");
250                 disable_phy_pins = 0;
251         } else if (otg_mode && strcmp(otg_mode, "otg") == 0) {
252                 debug("Setting dr_mode to 'host'\n");
253                 ret = fdt_setprop_string(blob, off, "dr_mode", "otg");
254                 disable_phy_pins = 0;
255         } else {
256                 if (otg_mode && strcmp(otg_mode, "none") != 0)
257                         printf("Invalid 'otg_mode' setting '%s'; disabling usbotg port\n",
258                                 otg_mode);
259                 disable_otg = 1;
260                 ret = 0;
261         }
262
263         if ((!disable_phy_pins && !disable_otg) || ret)
264                 goto out;
265
266         ret = karo_fdt_disable_node_phandle(blob, node, phy_supply);
267         if (ret)
268                 goto out;
269
270         if (disable_otg) {
271                 debug("Disabling usbphy\n");
272                 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
273                 if (ret)
274                         goto out;
275
276                 ret = karo_fdt_disable_node_phandle(blob, node, phy);
277         } else if (disable_phy_pins) {
278                 debug("Removing 'vbus-supply' from usbotg node\n");
279                 fdt_delprop(blob, off, phy_supply);
280         }
281
282 out:
283         if (ret)
284                 printf("Failed to update usbotg: %s\n", fdt_strerror(ret));
285         else
286                 debug("node '%s' updated\n", node);
287         karo_set_fdtsize(blob);
288 }
289
290 static inline int karo_fdt_flexcan_enabled(void *blob)
291 {
292         static const char *can_ifs[] = {
293                 "can0",
294                 "can1",
295         };
296         size_t i;
297
298         for (i = 0; i < ARRAY_SIZE(can_ifs); i++) {
299                 const char *status;
300                 int off = fdt_path_offset(blob, can_ifs[i]);
301
302                 if (off < 0) {
303                         debug("node '%s' not found\n", can_ifs[i]);
304                         continue;
305                 }
306                 status = fdt_getprop(blob, off, "status", NULL);
307                 if (status && strcmp(status, "okay") == 0) {
308                         debug("%s is enabled\n", can_ifs[i]);
309                         return 1;
310                 }
311         }
312         debug("can driver is disabled\n");
313         return 0;
314 }
315
316 static inline void karo_fdt_set_lcd_pins(void *blob, const char *name)
317 {
318         int off = fdt_path_offset(blob, name);
319         u32 ph;
320         const struct fdt_property *pc;
321         int len;
322
323         if (off < 0)
324                 return;
325
326         ph = fdt_create_phandle(blob, off);
327         if (!ph)
328                 return;
329
330         off = fdt_path_offset(blob, "display");
331         if (off < 0)
332                 return;
333
334         pc = fdt_get_property(blob, off, "pinctrl-0", &len);
335         if (!pc || len < sizeof(ph))
336                 return;
337
338         memcpy((void *)pc->data, &ph, sizeof(ph));
339         fdt_setprop_cell(blob, off, "pinctrl-0", ph);
340 }
341
342 void karo_fdt_fixup_flexcan(void *blob, int xcvr_present)
343 {
344         int ret;
345         const char *xcvr_status = "disabled";
346         const char *otg_mode = getenv("otg_mode");
347
348         if (xcvr_present) {
349                 if (karo_fdt_flexcan_enabled(blob)) {
350                         if (!is_lvds()) {
351                                 debug("Changing LCD to use 23bits only\n");
352                                 karo_fdt_set_lcd_pins(blob, "lcdif_23bit_pins_a");
353                                 xcvr_status = NULL;
354                         }
355                 } else if (!is_lvds()) {
356                         debug("Changing LCD to use 24bits\n");
357                         karo_fdt_set_lcd_pins(blob, "lcdif_24bit_pins_a");
358                 }
359         } else {
360                 int off = fdt_path_offset(blob, "can0");
361
362                 if (off >= 0)
363                         fdt_delprop(blob, off, "xceiver-supply");
364                 off = fdt_path_offset(blob, "can1");
365                 if (off >= 0)
366                         fdt_delprop(blob, off, "xceiver-supply");
367                 if (!is_lvds())
368                         karo_fdt_set_lcd_pins(blob, "lcdif_24bit_pins_a");
369         }
370
371         if (otg_mode && strcmp(otg_mode, "host") == 0)
372                 karo_fdt_enable_node(blob, "can1", 0);
373
374         if (xcvr_status) {
375                 debug("Disabling CAN XCVR\n");
376                 ret = fdt_find_and_setprop(blob, "reg_can_xcvr", "status",
377                                         xcvr_status, strlen(xcvr_status) + 1, 1);
378                 if (ret)
379                         printf("Failed to disable CAN transceiver switch: %s\n",
380                                 fdt_strerror(ret));
381         }
382 }
383
384 void karo_fdt_del_prop(void *blob, const char *compat, phys_addr_t offs,
385                         const char *prop)
386 {
387         int ret;
388         int offset;
389         const uint32_t *phandle;
390         uint32_t ph = 0;
391
392         offset = fdt_node_offset_by_compat_reg(blob, compat, offs);
393         if (offset <= 0)
394                 return;
395
396         phandle = fdt_getprop(blob, offset, prop, NULL);
397         if (phandle) {
398                 ph = fdt32_to_cpu(*phandle);
399         }
400
401         debug("Removing property '%s' from node %s@%08lx\n", prop, compat, offs);
402         ret = fdt_delprop(blob, offset, prop);
403         if (ret == 0)
404                 karo_set_fdtsize(blob);
405
406         if (!ph)
407                 return;
408
409         offset = fdt_node_offset_by_phandle(blob, ph);
410         if (offset <= 0)
411                 return;
412
413         debug("Removing node @ %08x\n", offset);
414         fdt_del_node(blob, offset);
415         karo_set_fdtsize(blob);
416 }
417
418 static int fdt_init_fb_mode(const void *blob, int off, struct fb_videomode *fb_mode)
419 {
420         const uint32_t *prop;
421
422         memset(fb_mode, 0, sizeof(*fb_mode));
423
424         prop = fdt_getprop(blob, off, "clock-frequency", NULL);
425         if (prop)
426                 fb_mode->pixclock = KHZ2PICOS(fdt32_to_cpu(*prop) / 1000);
427
428         prop = fdt_getprop(blob, off, "hactive", NULL);
429         if (prop)
430                 fb_mode->xres = fdt32_to_cpu(*prop);
431
432         prop = fdt_getprop(blob, off, "vactive", NULL);
433         if (prop)
434                 fb_mode->yres = fdt32_to_cpu(*prop);
435
436         prop = fdt_getprop(blob, off, "hback-porch", NULL);
437         if (prop)
438                 fb_mode->left_margin = fdt32_to_cpu(*prop);
439
440         prop = fdt_getprop(blob, off, "hsync-len", NULL);
441         if (prop)
442                 fb_mode->hsync_len = fdt32_to_cpu(*prop);
443
444         prop = fdt_getprop(blob, off, "hfront-porch", NULL);
445         if (prop)
446                 fb_mode->right_margin = fdt32_to_cpu(*prop);
447
448         prop = fdt_getprop(blob, off, "vback-porch", NULL);
449         if (prop)
450                 fb_mode->upper_margin = fdt32_to_cpu(*prop);
451
452         prop = fdt_getprop(blob, off, "vsync-len", NULL);
453         if (prop)
454                 fb_mode->vsync_len = fdt32_to_cpu(*prop);
455
456         prop = fdt_getprop(blob, off, "vfront-porch", NULL);
457         if (prop)
458                 fb_mode->lower_margin = fdt32_to_cpu(*prop);
459
460         prop = fdt_getprop(blob, off, "hsync-active", NULL);
461         if (prop)
462                 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
463
464         prop = fdt_getprop(blob, off, "vsync-active", NULL);
465         if (prop)
466                 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
467
468         prop = fdt_getprop(blob, off, "de-active", NULL);
469         if (prop)
470                 fb_mode->sync |= *prop ? 0 : FB_SYNC_OE_LOW_ACT;
471
472         prop = fdt_getprop(blob, off, "pixelclk-active", NULL);
473         if (prop)
474                 fb_mode->sync |= *prop ? 0 : FB_SYNC_CLK_LAT_FALL;
475
476         return 0;
477 }
478
479 static int fdt_update_native_fb_mode(void *blob, int off)
480 {
481         int ret;
482         uint32_t ph;
483
484         ret = fdt_increase_size(blob, 64);
485         if (ret) {
486                 printf("Warning: Failed to increase FDT size: %s\n",
487                         fdt_strerror(ret));
488         }
489         debug("Creating phandle at offset %d\n", off);
490         ph = fdt_create_phandle(blob, off);
491         if (!ph) {
492                 printf("Failed to create phandle for video timing\n");
493                 return -ENOMEM;
494         }
495
496         debug("phandle of %s @ %06x=%04x\n", fdt_get_name(blob, off, NULL),
497                 off, ph);
498         off = fdt_parent_offset(blob, off);
499         if (off < 0)
500                 return off;
501         debug("parent offset=%06x\n", off);
502         ret = fdt_setprop_cell(blob, off, "native-mode", ph);
503         if (ret)
504                 printf("Failed to set property 'native-mode': %s\n",
505                         fdt_strerror(ret));
506         karo_set_fdtsize(blob);
507         return ret;
508 }
509
510 static int karo_fdt_find_video_timings(void *blob)
511 {
512         int off = fdt_path_offset(blob, "display");
513         const char *subnode = "display-timings";
514
515         if (off < 0) {
516                 debug("Could not find node 'display' in FDT: %s\n",
517                         fdt_strerror(off));
518                 return off;
519         }
520
521         off = fdt_subnode_offset(blob, off, subnode);
522         if (off < 0) {
523                 debug("Could not find node '%s' in FDT: %s\n", subnode,
524                         fdt_strerror(off));
525         }
526         return off;
527 }
528
529 int karo_fdt_get_fb_mode(void *blob, const char *name, struct fb_videomode *fb_mode)
530 {
531         int off = karo_fdt_find_video_timings(blob);
532
533         if (off < 0)
534                 return off;
535         while (off > 0) {
536                 const char *n, *endp;
537                 int len, d = 1;
538
539                 off = fdt_next_node(blob, off, &d);
540                 if (off < 0)
541                         return off;
542                 if (d < 1)
543                         return -EINVAL;
544                 if (d > 2) {
545                         debug("Skipping node @ %04x %s depth %d\n", off,
546                                 fdt_get_name(blob, off, NULL), d);
547                         continue;
548                 }
549                 debug("parsing subnode @ %04x %s depth %d\n", off,
550                         fdt_get_name(blob, off, NULL), d);
551
552                 n = fdt_getprop(blob, off, "panel-name", &len);
553                 if (!n) {
554                         n = fdt_get_name(blob, off, NULL);
555                         if (strcasecmp(n, name) == 0) {
556                                 break;
557                         }
558                 } else {
559                         int found = 0;
560
561                         for (endp = n + len; n < endp; n += strlen(n) + 1) {
562                                 debug("Checking panel-name '%s'\n", n);
563                                 if (strcasecmp(n, name) == 0) {
564                                         debug("Using node %s @ %04x\n",
565                                                 fdt_get_name(blob, off, NULL), off);
566                                         found = 1;
567                                         break;
568                                 }
569                         }
570                         if (found)
571                                 break;
572                 }
573         }
574         if (off > 0) {
575                 return fdt_init_fb_mode(blob, off, fb_mode);
576         }
577         return -EINVAL;
578 }
579
580 #define SET_FB_PROP(n, v) ({                                            \
581         int ret;                                                        \
582         ret = fdt_setprop_u32(blob, off, n, v);                         \
583         if (ret) {                                                      \
584                 printf("Failed to set property %s: %s\n", name,         \
585                         fdt_strerror(ret));                             \
586         }                                                               \
587         ret;                                                            \
588 })
589
590
591 int karo_fdt_create_fb_mode(void *blob, const char *name,
592                         struct fb_videomode *fb_mode)
593 {
594         int off = fdt_path_offset(blob, "display");
595         int ret;
596         const char *subnode = "display-timings";
597
598         if (off < 0) {
599                 printf("'display' node not found in FDT\n");
600                 return off;
601         }
602
603         ret = fdt_increase_size(blob, 512);
604         if (ret) {
605                 printf("Failed to increase FDT size: %s\n", fdt_strerror(ret));
606                 return ret;
607         }
608
609         ret = fdt_subnode_offset(blob, off, subnode);
610         if (ret < 0) {
611                 debug("Could not find node '%s' in FDT: %s\n", subnode,
612                         fdt_strerror(ret));
613                 ret = fdt_add_subnode(blob, off, subnode);
614                 if (ret < 0) {
615                         printf("Failed to add %s subnode: %s\n", subnode,
616                                 fdt_strerror(ret));
617                         return ret;
618                 }
619         }
620
621         ret = fdt_add_subnode(blob, ret, name);
622         if (ret < 0) {
623                 printf("Failed to add %s subnode: %s\n", name,
624                         fdt_strerror(ret));
625                 return ret;
626         }
627         off = ret;
628
629         ret = SET_FB_PROP("clock-frequency",
630                         PICOS2KHZ(fb_mode->pixclock) * 1000);
631         if (ret)
632                 goto out;
633         ret = SET_FB_PROP("hactive", fb_mode->xres);
634         if (ret)
635                 goto out;
636         ret = SET_FB_PROP("vactive", fb_mode->yres);
637         if (ret)
638                 goto out;
639         ret = SET_FB_PROP("hback-porch", fb_mode->left_margin);
640         if (ret)
641                 goto out;
642         ret = SET_FB_PROP("hsync-len", fb_mode->hsync_len);
643         if (ret)
644                 goto out;
645         ret = SET_FB_PROP("hfront-porch", fb_mode->right_margin);
646         if (ret)
647                 goto out;
648         ret = SET_FB_PROP("vback-porch", fb_mode->upper_margin);
649         if (ret)
650                 goto out;
651         ret = SET_FB_PROP("vsync-len", fb_mode->vsync_len);
652         if (ret)
653                 goto out;
654         ret = SET_FB_PROP("vfront-porch", fb_mode->lower_margin);
655         if (ret)
656                 goto out;
657         ret = SET_FB_PROP("hsync-active",
658                         fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
659         if (ret)
660                 goto out;
661         ret = SET_FB_PROP("vsync-active",
662                         fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
663         if (ret)
664                 goto out;
665         ret = SET_FB_PROP("de-active",
666                         !(fb_mode->sync & FB_SYNC_OE_LOW_ACT));
667         if (ret)
668                 goto out;
669         ret = SET_FB_PROP("pixelclk-active",
670                         !(fb_mode->sync & FB_SYNC_CLK_LAT_FALL));
671 out:
672         karo_set_fdtsize(blob);
673         return ret;
674 }
675
676 int karo_fdt_update_fb_mode(void *blob, const char *name)
677 {
678         int off = fdt_path_offset(blob, "display");
679         const char *subnode = "display-timings";
680
681         if (off < 0)
682                 return off;
683
684         if (name == NULL) {
685                 int ret;
686
687                 debug("Disabling node '%s' at %03x\n",
688                         fdt_get_name(blob, off, NULL), off);
689                 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
690                 return ret;
691         }
692
693         off = fdt_subnode_offset(blob, off, subnode);
694         if (off < 0) {
695                 debug("Could not find node '%s' in FDT: %s\n", subnode,
696                         fdt_strerror(off));
697                 return off;
698         }
699         while (off > 0) {
700                 const char *n, *endp;
701                 int len, d = 1;
702
703                 off = fdt_next_node(blob, off, &d);
704                 if (off < 0)
705                         return off;
706                 if (d < 1)
707                         return -EINVAL;
708                 if (d > 2) {
709                         debug("Skipping node @ %04x %s depth %d\n", off,
710                                 fdt_get_name(blob, off, NULL), d);
711                         continue;
712                 }
713                 debug("parsing subnode @ %04x %s depth %d\n", off,
714                         fdt_get_name(blob, off, NULL), d);
715
716                 n = fdt_getprop(blob, off, "panel-name", &len);
717                 if (!n) {
718                         n = fdt_get_name(blob, off, NULL);
719                         if (strcasecmp(n, name) == 0) {
720                                 break;
721                         }
722                 } else {
723                         int found = 0;
724
725                         for (endp = n + len; n < endp; n += strlen(n) + 1) {
726                                 debug("Checking panel-name '%s'\n", n);
727                                 if (strcasecmp(n, name) == 0) {
728                                         debug("Using node %s @ %04x\n",
729                                                 fdt_get_name(blob, off, NULL), off);
730                                         found = 1;
731                                         break;
732                                 }
733                         }
734                         if (found)
735                                 break;
736                 }
737         }
738         if (off > 0)
739                 return fdt_update_native_fb_mode(blob, off);
740         return off;
741 }
742
743 #ifdef CONFIG_SYS_LVDS_IF
744 int karo_fdt_get_lcd_bus_width(const void *blob, int default_width)
745 {
746         int off = fdt_path_offset(blob, "display");
747
748         if (off >= 0) {
749                 const uint32_t *prop;
750
751                 prop = fdt_getprop(blob, off, "fsl,data-width", NULL);
752                 if (prop)
753                         return fdt32_to_cpu(*prop);
754         }
755         return default_width;
756 }
757
758 int karo_fdt_get_lvds_mapping(const void *blob, int default_mapping)
759 {
760         int off = fdt_path_offset(blob, "display");
761
762         if (off >= 0) {
763                 const char *prop;
764
765                 prop = fdt_getprop(blob, off, "fsl,data-mapping", NULL);
766                 if (prop)
767                         return strcmp(prop, "jeida") == 0;
768         }
769         return default_mapping;
770 }
771
772 u8 karo_fdt_get_lvds_channels(const void *blob)
773 {
774         static const char *lvds_chans[] = {
775                 "lvds0",
776                 "lvds1",
777         };
778         u8 lvds_chan_mask = 0;
779         int i;
780
781         for (i = 0; i < ARRAY_SIZE(lvds_chans); i++) {
782                 const char *status;
783                 int off = fdt_path_offset(blob, lvds_chans[i]);
784
785                 if (off < 0)
786                         continue;
787
788                 status = fdt_getprop(blob, off, "status", NULL);
789                 if (status && strcmp(status, "okay") == 0) {
790                         debug("%s is enabled\n", lvds_chans[i]);
791                         lvds_chan_mask |= 1 << i;
792                 }
793         }
794         return lvds_chan_mask;
795 }
796 #endif
797
798 int karo_fdt_get_backlight_polarity(const void *blob)
799 {
800 #ifdef CONFIG_SYS_LVDS_IF
801         const char *backlight_node = "/backlight0";
802 #else
803         const char *backlight_node = "/backlight";
804 #endif
805         int off = fdt_path_offset(blob, "backlight"); /* first try alias */
806         const struct fdt_property *prop;
807         int len;
808
809         if (off < 0) {
810                 /*
811                  * if no 'backlight' alias exists try finding '/backlight0'
812                  * or '/backlight' depending on LVDS or not
813                  */
814                 off = fdt_path_offset(blob, backlight_node);
815                 if (off < 0) {
816                         printf("%s node not found in DT\n", backlight_node);
817                         return off;
818                 }
819         }
820
821         prop = fdt_get_property(blob, off, "pwms", &len);
822         if (!prop)
823                 printf("'pwms' property not found\n");
824         else
825                 debug("'pwms' property has len %d\n", len);
826
827         len /= sizeof(u32);
828         if (prop && len > 3) {
829                 const u32 *data = (const u32 *)prop->data;
830                 return fdt32_to_cpu(data[3]) == 0;
831         }
832         return 0;
833 }