+static inline int karo_fdt_flexcan_enabled(void *blob)
+{
+ static const char *can_ifs[] = {
+ "can0",
+ "can1",
+ };
+ size_t i;
+
+ for (i = 0; i < ARRAY_SIZE(can_ifs); i++) {
+ const char *status;
+ int off = fdt_path_offset(blob, can_ifs[i]);
+
+ if (off < 0) {
+ debug("node '%s' not found\n", can_ifs[i]);
+ continue;
+ }
+ status = fdt_getprop(blob, off, "status", NULL);
+ if (status && strcmp(status, "okay") == 0) {
+ debug("%s is enabled\n", can_ifs[i]);
+ return 1;
+ }
+ }
+ debug("can driver is disabled\n");
+ return 0;
+}
+
+static inline void karo_fdt_set_lcd_pins(void *blob, const char *name)
+{
+ int off = fdt_path_offset(blob, name);
+ u32 ph;
+ const struct fdt_property *pc;
+ int len;
+
+ if (off < 0)
+ return;
+
+ ph = fdt_create_phandle(blob, off);
+ if (!ph)
+ return;
+
+ off = fdt_path_offset(blob, "display");
+ if (off < 0)
+ return;
+
+ pc = fdt_get_property(blob, off, "pinctrl-0", &len);
+ if (!pc || len < sizeof(ph))
+ return;
+
+ memcpy((void *)pc->data, &ph, sizeof(ph));
+ fdt_setprop_cell(blob, off, "pinctrl-0", ph);
+}
+
+void karo_fdt_fixup_flexcan(void *blob, int xcvr_present)
+{
+ int ret;
+ const char *xcvr_status = "disabled";
+ const char *otg_mode = getenv("otg_mode");
+
+ if (xcvr_present) {
+ if (karo_fdt_flexcan_enabled(blob)) {
+ if (!is_lvds()) {
+ debug("Changing LCD to use 23bits only\n");
+ karo_fdt_set_lcd_pins(blob, "lcdif_23bit_pins_a");
+ xcvr_status = NULL;
+ }
+ } else if (!is_lvds()) {
+ debug("Changing LCD to use 24bits\n");
+ karo_fdt_set_lcd_pins(blob, "lcdif_24bit_pins_a");
+ }
+ } else {
+ int off = fdt_path_offset(blob, "can0");
+
+ if (off >= 0)
+ fdt_delprop(blob, off, "xceiver-supply");
+ off = fdt_path_offset(blob, "can1");
+ if (off >= 0)
+ fdt_delprop(blob, off, "xceiver-supply");
+ if (!is_lvds())
+ karo_fdt_set_lcd_pins(blob, "lcdif_24bit_pins_a");
+ }
+
+ if (otg_mode && strcmp(otg_mode, "host") == 0)
+ karo_fdt_enable_node(blob, "can1", 0);
+
+ if (xcvr_status) {
+ debug("Disabling CAN XCVR\n");
+ ret = fdt_find_and_setprop(blob, "reg_can_xcvr", "status",
+ xcvr_status, strlen(xcvr_status) + 1, 1);
+ if (ret)
+ printf("Failed to disable CAN transceiver switch: %s\n",
+ fdt_strerror(ret));
+ }
+}
+