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