video: sunxi: Hook up SSD2828 with the sunxi video driver
[oweals/u-boot.git] / drivers / video / sunxi_display.c
index 5a14785666c662e674bcadeb3b9be3d586837977..0505f3c96398578c91aad5d77a601fb74d840350 100644 (file)
 
 #include <asm/arch/clock.h>
 #include <asm/arch/display.h>
+#include <asm/arch/gpio.h>
 #include <asm/global_data.h>
+#include <asm/gpio.h>
 #include <asm/io.h>
 #include <errno.h>
 #include <fdtdec.h>
 #include <fdt_support.h>
 #include <video_fb.h>
 #include "videomodes.h"
+#include "ssd2828.h"
 
 DECLARE_GLOBAL_DATA_PTR;
 
+enum sunxi_monitor {
+       sunxi_monitor_none,
+       sunxi_monitor_dvi,
+       sunxi_monitor_hdmi,
+       sunxi_monitor_lcd,
+       sunxi_monitor_vga,
+};
+#define SUNXI_MONITOR_LAST sunxi_monitor_vga
+
 struct sunxi_display {
        GraphicDevice graphic_device;
-       bool enabled;
+       enum sunxi_monitor monitor;
+       unsigned int depth;
 } sunxi_display;
 
+#ifdef CONFIG_VIDEO_HDMI
+
 /*
  * Wait up to 200ms for value to be set in given part of reg.
  */
@@ -42,13 +57,13 @@ static int await_completion(u32 *reg, u32 mask, u32 val)
        return 0;
 }
 
-static int sunxi_hdmi_hpd_detect(void)
+static int sunxi_hdmi_hpd_detect(int hpd_delay)
 {
        struct sunxi_ccm_reg * const ccm =
                (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
        struct sunxi_hdmi_reg * const hdmi =
                (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
-       unsigned long tmo = timer_get_us() + 300000;
+       unsigned long tmo = timer_get_us() + hpd_delay * 1000;
 
        /* Set pll3 to 300MHz */
        clock_set_pll3(300000000);
@@ -159,7 +174,7 @@ static int sunxi_hdmi_edid_get_block(int block, u8 *buf)
        return r;
 }
 
-static int sunxi_hdmi_edid_get_mode(struct ctfb_res_modes *mode, char *monitor)
+static int sunxi_hdmi_edid_get_mode(struct ctfb_res_modes *mode)
 {
        struct edid1_info edid1;
        struct edid_cea861_info cea681[4];
@@ -241,19 +256,21 @@ static int sunxi_hdmi_edid_get_mode(struct ctfb_res_modes *mode, char *monitor)
        }
 
        /* Check for basic audio support, if found enable hdmi output */
-       strcpy(monitor, "dvi");
+       sunxi_display.monitor = sunxi_monitor_dvi;
        for (i = 0; i < ext_blocks; i++) {
                if (cea681[i].extension_tag != EDID_CEA861_EXTENSION_TAG ||
                    cea681[i].revision < 2)
                        continue;
 
                if (EDID_CEA861_SUPPORTS_BASIC_AUDIO(cea681[i]))
-                       strcpy(monitor, "hdmi");
+                       sunxi_display.monitor = sunxi_monitor_hdmi;
        }
 
        return 0;
 }
 
+#endif /* CONFIG_VIDEO_HDMI */
+
 /*
  * This is the entity that mixes and matches the different layers and inputs.
  * Allwinner calls it the back-end, but i like composer better.
@@ -266,7 +283,7 @@ static void sunxi_composer_init(void)
                (struct sunxi_de_be_reg *)SUNXI_DE_BE0_BASE;
        int i;
 
-#ifdef CONFIG_MACH_SUN6I
+#if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I
        /* Reset off */
        setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_DE_BE0);
 #endif
@@ -301,23 +318,46 @@ static void sunxi_composer_mode_set(const struct ctfb_res_modes *mode,
        setbits_le32(&de_be->mode, SUNXI_DE_BE_MODE_LAYER0_ENABLE);
 }
 
+static void sunxi_composer_enable(void)
+{
+       struct sunxi_de_be_reg * const de_be =
+               (struct sunxi_de_be_reg *)SUNXI_DE_BE0_BASE;
+
+       setbits_le32(&de_be->reg_ctrl, SUNXI_DE_BE_REG_CTRL_LOAD_REGS);
+       setbits_le32(&de_be->mode, SUNXI_DE_BE_MODE_START);
+}
+
 /*
  * LCDC, what allwinner calls a CRTC, so timing controller and serializer.
  */
-static void sunxi_lcdc_pll_set(int dotclock, int *clk_div, int *clk_double)
+static void sunxi_lcdc_pll_set(int tcon, int dotclock,
+                              int *clk_div, int *clk_double)
 {
        struct sunxi_ccm_reg * const ccm =
                (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
-       int value, n, m, diff;
+       int value, n, m, min_m, max_m, diff;
        int best_n = 0, best_m = 0, best_diff = 0x0FFFFFFF;
        int best_double = 0;
 
+       if (tcon == 0) {
+#ifdef CONFIG_VIDEO_LCD_IF_PARALLEL
+               min_m = 6;
+               max_m = 127;
+#endif
+#ifdef CONFIG_VIDEO_LCD_IF_LVDS
+               min_m = max_m = 7;
+#endif
+       } else {
+               min_m = 1;
+               max_m = 15;
+       }
+
        /*
         * Find the lowest divider resulting in a matching clock, if there
         * is no match, pick the closest lower clock, as monitors tend to
         * not sync to higher frequencies.
         */
-       for (m = 15; m > 0; m--) {
+       for (m = min_m; m <= max_m; m++) {
                n = (m * dotclock) / 3000;
 
                if ((n >= 9) && (n <= 127)) {
@@ -354,9 +394,17 @@ static void sunxi_lcdc_pll_set(int dotclock, int *clk_div, int *clk_double)
 
        clock_set_pll3(best_n * 3000000);
 
-       writel(CCM_LCD_CH1_CTRL_GATE |
-           (best_double ? CCM_LCD_CH1_CTRL_PLL3_2X : CCM_LCD_CH1_CTRL_PLL3) |
-           CCM_LCD_CH1_CTRL_M(best_m), &ccm->lcd0_ch1_clk_cfg);
+       if (tcon == 0) {
+               writel(CCM_LCD_CH0_CTRL_GATE | CCM_LCD_CH0_CTRL_RST |
+                      (best_double ? CCM_LCD_CH0_CTRL_PLL3_2X :
+                                     CCM_LCD_CH0_CTRL_PLL3),
+                      &ccm->lcd0_ch0_clk_cfg);
+       } else {
+               writel(CCM_LCD_CH1_CTRL_GATE |
+                      (best_double ? CCM_LCD_CH1_CTRL_PLL3_2X :
+                                     CCM_LCD_CH1_CTRL_PLL3) |
+                      CCM_LCD_CH1_CTRL_M(best_m), &ccm->lcd0_ch1_clk_cfg);
+       }
 
        *clk_div = best_m;
        *clk_double = best_double;
@@ -370,7 +418,7 @@ static void sunxi_lcdc_init(void)
                (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
 
        /* Reset off */
-#ifdef CONFIG_MACH_SUN6I
+#if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I
        setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_LCD0);
 #else
        setbits_le32(&ccm->lcd0_ch0_clk_cfg, CCM_LCD_CH0_CTRL_RST);
@@ -378,6 +426,9 @@ static void sunxi_lcdc_init(void)
 
        /* Clock on */
        setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_LCD0);
+#ifdef CONFIG_VIDEO_LCD_IF_LVDS
+       setbits_le32(&ccm->lvds_clk_cfg, CCM_LVDS_CTRL_RST);
+#endif
 
        /* Init lcdc */
        writel(0, &lcdc->ctrl); /* Disable tcon */
@@ -391,20 +442,178 @@ static void sunxi_lcdc_init(void)
        writel(0xffffffff, &lcdc->tcon1_io_tristate);
 }
 
-static void sunxi_lcdc_mode_set(const struct ctfb_res_modes *mode,
-                               int *clk_div, int *clk_double)
+static void sunxi_lcdc_enable(void)
 {
        struct sunxi_lcdc_reg * const lcdc =
                (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
-       int bp, total;
+
+       setbits_le32(&lcdc->ctrl, SUNXI_LCDC_CTRL_TCON_ENABLE);
+#ifdef CONFIG_VIDEO_LCD_IF_LVDS
+       setbits_le32(&lcdc->tcon0_lvds_intf, SUNXI_LCDC_TCON0_LVDS_INTF_ENABLE);
+       setbits_le32(&lcdc->lvds_ana0, SUNXI_LCDC_LVDS_ANA0);
+       setbits_le32(&lcdc->lvds_ana0, SUNXI_LCDC_LVDS_ANA0_UPDATE);
+       udelay(2); /* delay at least 1200 ns */
+       setbits_le32(&lcdc->lvds_ana1, SUNXI_LCDC_LVDS_ANA1_INIT1);
+       udelay(1); /* delay at least 120 ns */
+       setbits_le32(&lcdc->lvds_ana1, SUNXI_LCDC_LVDS_ANA1_INIT2);
+       setbits_le32(&lcdc->lvds_ana0, SUNXI_LCDC_LVDS_ANA0_UPDATE);
+#endif
+}
+
+static void sunxi_lcdc_panel_enable(void)
+{
+       int pin;
+
+       /*
+        * Start with backlight disabled to avoid the screen flashing to
+        * white while the lcd inits.
+        */
+       pin = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_BL_EN);
+       if (pin != -1) {
+               gpio_request(pin, "lcd_backlight_enable");
+               gpio_direction_output(pin, 0);
+       }
+
+       pin = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_BL_PWM);
+       if (pin != -1) {
+               gpio_request(pin, "lcd_backlight_pwm");
+               /* backlight pwm is inverted, set to 1 to disable backlight */
+               gpio_direction_output(pin, 1);
+       }
+
+       /* Give the backlight some time to turn off and power up the panel. */
+       mdelay(40);
+       pin = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_POWER);
+       if (pin != -1) {
+               gpio_request(pin, "lcd_power");
+               gpio_direction_output(pin, 1);
+       }
+}
+
+static void sunxi_lcdc_backlight_enable(void)
+{
+       int pin;
+
+       /*
+        * We want to have scanned out at least one frame before enabling the
+        * backlight to avoid the screen flashing to white when we enable it.
+        */
+       mdelay(40);
+
+       pin = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_BL_EN);
+       if (pin != -1)
+               gpio_direction_output(pin, 1);
+
+       pin = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_BL_PWM);
+       if (pin != -1) {
+               /* backlight pwm is inverted, set to 0 to enable backlight */
+               gpio_direction_output(pin, 0);
+       }
+}
+
+static int sunxi_lcdc_get_clk_delay(const struct ctfb_res_modes *mode)
+{
+       int delay;
+
+       delay = mode->lower_margin + mode->vsync_len + mode->upper_margin - 2;
+       return (delay > 30) ? 30 : delay;
+}
+
+static void sunxi_lcdc_tcon0_mode_set(const struct ctfb_res_modes *mode)
+{
+       struct sunxi_lcdc_reg * const lcdc =
+               (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
+       int bp, clk_delay, clk_div, clk_double, pin, total, val;
+
+       for (pin = SUNXI_GPD(0); pin <= SUNXI_GPD(27); pin++)
+#ifdef CONFIG_VIDEO_LCD_IF_PARALLEL
+               sunxi_gpio_set_cfgpin(pin, SUNXI_GPD0_LCD0);
+#endif
+#ifdef CONFIG_VIDEO_LCD_IF_LVDS
+               sunxi_gpio_set_cfgpin(pin, SUNXI_GPD0_LVDS0);
+#endif
+
+       sunxi_lcdc_pll_set(0, mode->pixclock_khz, &clk_div, &clk_double);
+
+       /* Use tcon0 */
+       clrsetbits_le32(&lcdc->ctrl, SUNXI_LCDC_CTRL_IO_MAP_MASK,
+                       SUNXI_LCDC_CTRL_IO_MAP_TCON0);
+
+       clk_delay = sunxi_lcdc_get_clk_delay(mode);
+       writel(SUNXI_LCDC_TCON0_CTRL_ENABLE |
+              SUNXI_LCDC_TCON0_CTRL_CLK_DELAY(clk_delay), &lcdc->tcon0_ctrl);
+
+       writel(SUNXI_LCDC_TCON0_DCLK_ENABLE |
+              SUNXI_LCDC_TCON0_DCLK_DIV(clk_div), &lcdc->tcon0_dclk);
+
+       writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(mode->yres),
+              &lcdc->tcon0_timing_active);
+
+       bp = mode->hsync_len + mode->left_margin;
+       total = mode->xres + mode->right_margin + bp;
+       writel(SUNXI_LCDC_TCON0_TIMING_H_TOTAL(total) |
+              SUNXI_LCDC_TCON0_TIMING_H_BP(bp), &lcdc->tcon0_timing_h);
+
+       bp = mode->vsync_len + mode->upper_margin;
+       total = mode->yres + mode->lower_margin + bp;
+       writel(SUNXI_LCDC_TCON0_TIMING_V_TOTAL(total) |
+              SUNXI_LCDC_TCON0_TIMING_V_BP(bp), &lcdc->tcon0_timing_v);
+
+#ifdef CONFIG_VIDEO_LCD_IF_PARALLEL
+       writel(SUNXI_LCDC_X(mode->hsync_len) | SUNXI_LCDC_Y(mode->vsync_len),
+              &lcdc->tcon0_timing_sync);
+
+       writel(0, &lcdc->tcon0_hv_intf);
+       writel(0, &lcdc->tcon0_cpu_intf);
+#endif
+#ifdef CONFIG_VIDEO_LCD_IF_LVDS
+       val = (sunxi_display.depth == 18) ? 1 : 0;
+       writel(SUNXI_LCDC_TCON0_LVDS_INTF_BITWIDTH(val), &lcdc->tcon0_lvds_intf);
+#endif
+
+       if (sunxi_display.depth == 18 || sunxi_display.depth == 16) {
+               writel(SUNXI_LCDC_TCON0_FRM_SEED, &lcdc->tcon0_frm_seed[0]);
+               writel(SUNXI_LCDC_TCON0_FRM_SEED, &lcdc->tcon0_frm_seed[1]);
+               writel(SUNXI_LCDC_TCON0_FRM_SEED, &lcdc->tcon0_frm_seed[2]);
+               writel(SUNXI_LCDC_TCON0_FRM_SEED, &lcdc->tcon0_frm_seed[3]);
+               writel(SUNXI_LCDC_TCON0_FRM_SEED, &lcdc->tcon0_frm_seed[4]);
+               writel(SUNXI_LCDC_TCON0_FRM_SEED, &lcdc->tcon0_frm_seed[5]);
+               writel(SUNXI_LCDC_TCON0_FRM_TAB0, &lcdc->tcon0_frm_table[0]);
+               writel(SUNXI_LCDC_TCON0_FRM_TAB1, &lcdc->tcon0_frm_table[1]);
+               writel(SUNXI_LCDC_TCON0_FRM_TAB2, &lcdc->tcon0_frm_table[2]);
+               writel(SUNXI_LCDC_TCON0_FRM_TAB3, &lcdc->tcon0_frm_table[3]);
+               writel(((sunxi_display.depth == 18) ?
+                       SUNXI_LCDC_TCON0_FRM_CTRL_RGB666 :
+                       SUNXI_LCDC_TCON0_FRM_CTRL_RGB565),
+                      &lcdc->tcon0_frm_ctrl);
+       }
+
+       val = SUNXI_LCDC_TCON0_IO_POL_DCLK_PHASE(CONFIG_VIDEO_LCD_DCLK_PHASE);
+       if (!(mode->sync & FB_SYNC_HOR_HIGH_ACT))
+               val |= SUNXI_LCDC_TCON_HSYNC_MASK;
+       if (!(mode->sync & FB_SYNC_VERT_HIGH_ACT))
+               val |= SUNXI_LCDC_TCON_VSYNC_MASK;
+       writel(val, &lcdc->tcon0_io_polarity);
+
+       writel(0, &lcdc->tcon0_io_tristate);
+}
+
+#if defined CONFIG_VIDEO_HDMI || defined CONFIG_VIDEO_VGA
+static void sunxi_lcdc_tcon1_mode_set(const struct ctfb_res_modes *mode,
+                                     int *clk_div, int *clk_double,
+                                     bool use_portd_hvsync)
+{
+       struct sunxi_lcdc_reg * const lcdc =
+               (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
+       int bp, clk_delay, total, val;
 
        /* Use tcon1 */
        clrsetbits_le32(&lcdc->ctrl, SUNXI_LCDC_CTRL_IO_MAP_MASK,
                        SUNXI_LCDC_CTRL_IO_MAP_TCON1);
 
-       /* Enabled, 0x1e start delay */
+       clk_delay = sunxi_lcdc_get_clk_delay(mode);
        writel(SUNXI_LCDC_TCON1_CTRL_ENABLE |
-              SUNXI_LCDC_TCON1_CTRL_CLK_DELAY(0x1e), &lcdc->tcon1_ctrl);
+              SUNXI_LCDC_TCON1_CTRL_CLK_DELAY(clk_delay), &lcdc->tcon1_ctrl);
 
        writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(mode->yres),
               &lcdc->tcon1_timing_source);
@@ -426,20 +635,26 @@ static void sunxi_lcdc_mode_set(const struct ctfb_res_modes *mode,
        writel(SUNXI_LCDC_X(mode->hsync_len) | SUNXI_LCDC_Y(mode->vsync_len),
               &lcdc->tcon1_timing_sync);
 
-       sunxi_lcdc_pll_set(mode->pixclock_khz, clk_div, clk_double);
-}
+       if (use_portd_hvsync) {
+               sunxi_gpio_set_cfgpin(SUNXI_GPD(26), SUNXI_GPD0_LCD0);
+               sunxi_gpio_set_cfgpin(SUNXI_GPD(27), SUNXI_GPD0_LCD0);
 
-#ifdef CONFIG_MACH_SUN6I
-static void sunxi_drc_init(void)
-{
-       struct sunxi_ccm_reg * const ccm =
-               (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
+               val = 0;
+               if (mode->sync & FB_SYNC_HOR_HIGH_ACT)
+                       val |= SUNXI_LCDC_TCON_HSYNC_MASK;
+               if (mode->sync & FB_SYNC_VERT_HIGH_ACT)
+                       val |= SUNXI_LCDC_TCON_VSYNC_MASK;
+               writel(val, &lcdc->tcon1_io_polarity);
 
-       /* On sun6i the drc must be clocked even when in pass-through mode */
-       setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_DRC0);
-       clock_set_de_mod_clock(&ccm->iep_drc0_clk_cfg, 300000000);
+               clrbits_le32(&lcdc->tcon1_io_tristate,
+                            SUNXI_LCDC_TCON_VSYNC_MASK |
+                            SUNXI_LCDC_TCON_HSYNC_MASK);
+       }
+       sunxi_lcdc_pll_set(1, mode->pixclock_khz, clk_div, clk_double);
 }
-#endif
+#endif /* CONFIG_VIDEO_HDMI || defined CONFIG_VIDEO_VGA */
+
+#ifdef CONFIG_VIDEO_HDMI
 
 static void sunxi_hdmi_setup_info_frames(const struct ctfb_res_modes *mode)
 {
@@ -489,7 +704,7 @@ static void sunxi_hdmi_setup_info_frames(const struct ctfb_res_modes *mode)
 }
 
 static void sunxi_hdmi_mode_set(const struct ctfb_res_modes *mode,
-                               bool hdmi_mode, int clk_div, int clk_double)
+                               int clk_div, int clk_double)
 {
        struct sunxi_hdmi_reg * const hdmi =
                (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
@@ -498,9 +713,12 @@ static void sunxi_hdmi_mode_set(const struct ctfb_res_modes *mode,
        /* Write clear interrupt status bits */
        writel(SUNXI_HDMI_IRQ_STATUS_BITS, &hdmi->irq);
 
-       if (hdmi_mode)
+       if (sunxi_display.monitor == sunxi_monitor_hdmi)
                sunxi_hdmi_setup_info_frames(mode);
 
+       /* Set input sync enable */
+       writel(SUNXI_HDMI_UNKNOWN_INPUT_SYNC, &hdmi->unknown);
+
        /* Init various registers, select pll3 as clock source */
        writel(SUNXI_HDMI_VIDEO_POL_TX_CLK, &hdmi->video_polarity);
        writel(SUNXI_HDMI_PAD_CTRL0_RUN, &hdmi->pad_ctrl0);
@@ -537,71 +755,187 @@ static void sunxi_hdmi_mode_set(const struct ctfb_res_modes *mode,
                setbits_le32(&hdmi->video_polarity, SUNXI_HDMI_VIDEO_POL_VER);
 }
 
-static void sunxi_engines_init(void)
+static void sunxi_hdmi_enable(void)
 {
-       sunxi_composer_init();
-       sunxi_lcdc_init();
-#ifdef CONFIG_MACH_SUN6I
-       sunxi_drc_init();
-#endif
+       struct sunxi_hdmi_reg * const hdmi =
+               (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
+
+       udelay(100);
+       setbits_le32(&hdmi->video_ctrl, SUNXI_HDMI_VIDEO_CTRL_ENABLE);
 }
 
-static void sunxi_mode_set(const struct ctfb_res_modes *mode, char *monitor,
-                          unsigned int address)
+#endif /* CONFIG_VIDEO_HDMI */
+
+#ifdef CONFIG_VIDEO_VGA
+
+static void sunxi_vga_mode_set(void)
 {
-       struct sunxi_de_be_reg * const de_be =
-               (struct sunxi_de_be_reg *)SUNXI_DE_BE0_BASE;
-       struct sunxi_lcdc_reg * const lcdc =
-               (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
-       struct sunxi_hdmi_reg * const hdmi =
-               (struct sunxi_hdmi_reg *)SUNXI_HDMI_BASE;
-       int clk_div, clk_double;
-       int retries = 3;
-       bool hdmi_mode = strcmp(monitor, "hdmi") == 0;
+       struct sunxi_ccm_reg * const ccm =
+               (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
+       struct sunxi_tve_reg * const tve =
+               (struct sunxi_tve_reg *)SUNXI_TVE0_BASE;
+
+       /* Clock on */
+       setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_TVE0);
+
+       /* Set TVE in VGA mode */
+       writel(SUNXI_TVE_GCTRL_DAC_INPUT(0, 1) |
+              SUNXI_TVE_GCTRL_DAC_INPUT(1, 2) |
+              SUNXI_TVE_GCTRL_DAC_INPUT(2, 3), &tve->gctrl);
+       writel(SUNXI_TVE_GCTRL_CFG0_VGA, &tve->cfg0);
+       writel(SUNXI_TVE_GCTRL_DAC_CFG0_VGA, &tve->dac_cfg0);
+       writel(SUNXI_TVE_GCTRL_UNKNOWN1_VGA, &tve->unknown1);
+}
 
-retry:
-       clrbits_le32(&hdmi->video_ctrl, SUNXI_HDMI_VIDEO_CTRL_ENABLE);
-       clrbits_le32(&lcdc->ctrl, SUNXI_LCDC_CTRL_TCON_ENABLE);
-       clrbits_le32(&de_be->mode, SUNXI_DE_BE_MODE_START);
+static void sunxi_vga_enable(void)
+{
+       struct sunxi_tve_reg * const tve =
+               (struct sunxi_tve_reg *)SUNXI_TVE0_BASE;
 
-       sunxi_composer_mode_set(mode, address);
-       sunxi_lcdc_mode_set(mode, &clk_div, &clk_double);
-       sunxi_hdmi_mode_set(mode, hdmi_mode, clk_div, clk_double);
+       setbits_le32(&tve->gctrl, SUNXI_TVE_GCTRL_ENABLE);
+}
 
-       setbits_le32(&de_be->reg_ctrl, SUNXI_DE_BE_REG_CTRL_LOAD_REGS);
-       setbits_le32(&de_be->mode, SUNXI_DE_BE_MODE_START);
+#endif /* CONFIG_VIDEO_VGA */
 
-       udelay(1000000 / mode->refresh + 500);
+static void sunxi_drc_init(void)
+{
+#if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I
+       struct sunxi_ccm_reg * const ccm =
+               (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
 
-       setbits_le32(&lcdc->ctrl, SUNXI_LCDC_CTRL_TCON_ENABLE);
+       /* On sun6i the drc must be clocked even when in pass-through mode */
+       setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_DRC0);
+       clock_set_de_mod_clock(&ccm->iep_drc0_clk_cfg, 300000000);
+#endif
+}
 
-       udelay(1000000 / mode->refresh + 500);
+#ifdef CONFIG_VIDEO_VGA_VIA_LCD
+static void sunxi_vga_external_dac_enable(void)
+{
+       int pin;
 
-       setbits_le32(&hdmi->video_ctrl, SUNXI_HDMI_VIDEO_CTRL_ENABLE);
+       pin = sunxi_name_to_gpio(CONFIG_VIDEO_VGA_EXTERNAL_DAC_EN);
+       if (pin != -1) {
+               gpio_request(pin, "vga_enable");
+               gpio_direction_output(pin, 1);
+       }
+}
+#endif /* CONFIG_VIDEO_VGA_VIA_LCD */
 
-       udelay(1000000 / mode->refresh + 500);
+#ifdef CONFIG_VIDEO_LCD_SSD2828
+static int sunxi_ssd2828_init(const struct ctfb_res_modes *mode)
+{
+       struct ssd2828_config cfg = {
+               .csx_pin = name_to_gpio(CONFIG_VIDEO_LCD_SPI_CS),
+               .sck_pin = name_to_gpio(CONFIG_VIDEO_LCD_SPI_SCLK),
+               .sdi_pin = name_to_gpio(CONFIG_VIDEO_LCD_SPI_MOSI),
+               .sdo_pin = name_to_gpio(CONFIG_VIDEO_LCD_SPI_MISO),
+               .reset_pin = name_to_gpio(CONFIG_VIDEO_LCD_SSD2828_RESET),
+               .ssd2828_tx_clk_khz  = CONFIG_VIDEO_LCD_SSD2828_TX_CLK * 1000,
+               .ssd2828_color_depth = 24,
+#ifdef CONFIG_VIDEO_LCD_PANEL_MIPI_4_LANE_513_MBPS_VIA_SSD2828
+               .mipi_dsi_number_of_data_lanes           = 4,
+               .mipi_dsi_bitrate_per_data_lane_mbps     = 513,
+               .mipi_dsi_delay_after_exit_sleep_mode_ms = 100,
+               .mipi_dsi_delay_after_set_display_on_ms  = 200
+#else
+#error MIPI LCD panel needs configuration parameters
+#endif
+       };
 
-       /*
-        * Sometimes the display pipeline does not sync up properly, if
-        * this happens the hdmi fifo underrun or overrun bits are set.
-        */
-       if (readl(&hdmi->irq) &
-           (SUNXI_HDMI_IRQ_STATUS_FIFO_UF | SUNXI_HDMI_IRQ_STATUS_FIFO_OF)) {
-               if (retries--)
-                       goto retry;
-               printf("HDMI fifo under or overrun\n");
+       if (cfg.csx_pin == -1 || cfg.sck_pin == -1 || cfg.sdi_pin == -1) {
+               printf("SSD2828: SPI pins are not properly configured\n");
+               return 1;
        }
+       if (cfg.reset_pin == -1) {
+               printf("SSD2828: Reset pin is not properly configured\n");
+               return 1;
+       }
+
+       return ssd2828_init(&cfg, mode);
+}
+#endif /* CONFIG_VIDEO_LCD_SSD2828 */
+
+static void sunxi_engines_init(void)
+{
+       sunxi_composer_init();
+       sunxi_lcdc_init();
+       sunxi_drc_init();
+}
+
+static void sunxi_mode_set(const struct ctfb_res_modes *mode,
+                          unsigned int address)
+{
+       int __maybe_unused clk_div, clk_double;
+
+       switch (sunxi_display.monitor) {
+       case sunxi_monitor_none:
+               break;
+       case sunxi_monitor_dvi:
+       case sunxi_monitor_hdmi:
+#ifdef CONFIG_VIDEO_HDMI
+               sunxi_composer_mode_set(mode, address);
+               sunxi_lcdc_tcon1_mode_set(mode, &clk_div, &clk_double, 0);
+               sunxi_hdmi_mode_set(mode, clk_div, clk_double);
+               sunxi_composer_enable();
+               sunxi_lcdc_enable();
+               sunxi_hdmi_enable();
+#endif
+               break;
+       case sunxi_monitor_lcd:
+               sunxi_lcdc_panel_enable();
+               sunxi_composer_mode_set(mode, address);
+               sunxi_lcdc_tcon0_mode_set(mode);
+               sunxi_composer_enable();
+               sunxi_lcdc_enable();
+#ifdef CONFIG_VIDEO_LCD_SSD2828
+               sunxi_ssd2828_init(mode);
+#endif
+               sunxi_lcdc_backlight_enable();
+               break;
+       case sunxi_monitor_vga:
+#ifdef CONFIG_VIDEO_VGA
+               sunxi_composer_mode_set(mode, address);
+               sunxi_lcdc_tcon1_mode_set(mode, &clk_div, &clk_double, 1);
+               sunxi_vga_mode_set();
+               sunxi_composer_enable();
+               sunxi_lcdc_enable();
+               sunxi_vga_enable();
+#elif defined CONFIG_VIDEO_VGA_VIA_LCD
+               sunxi_composer_mode_set(mode, address);
+               sunxi_lcdc_tcon0_mode_set(mode);
+               sunxi_composer_enable();
+               sunxi_lcdc_enable();
+               sunxi_vga_external_dac_enable();
+#endif
+               break;
+       }
+}
+
+static const char *sunxi_get_mon_desc(enum sunxi_monitor monitor)
+{
+       switch (monitor) {
+       case sunxi_monitor_none:        return "none";
+       case sunxi_monitor_dvi:         return "dvi";
+       case sunxi_monitor_hdmi:        return "hdmi";
+       case sunxi_monitor_lcd:         return "lcd";
+       case sunxi_monitor_vga:         return "vga";
+       }
+       return NULL; /* never reached */
 }
 
 void *video_hw_init(void)
 {
        static GraphicDevice *graphic_device = &sunxi_display.graphic_device;
        const struct ctfb_res_modes *mode;
-       struct ctfb_res_modes edid_mode;
+       struct ctfb_res_modes custom;
        const char *options;
-       unsigned int depth;
-       int ret, hpd, edid;
-       char monitor[16];
+#ifdef CONFIG_VIDEO_HDMI
+       int ret, hpd, hpd_delay, edid;
+#endif
+       char mon[16];
+       char *lcd_mode = CONFIG_VIDEO_LCD_MODE;
+       int i;
 
        memset(&sunxi_display, 0, sizeof(struct sunxi_display));
 
@@ -609,38 +943,98 @@ void *video_hw_init(void)
               CONFIG_SUNXI_FB_SIZE >> 10);
        gd->fb_base = gd->ram_top;
 
-       video_get_ctfb_res_modes(RES_MODE_1024x768, 24, &mode, &depth, &options);
+       video_get_ctfb_res_modes(RES_MODE_1024x768, 24, &mode,
+                                &sunxi_display.depth, &options);
+#ifdef CONFIG_VIDEO_HDMI
        hpd = video_get_option_int(options, "hpd", 1);
+       hpd_delay = video_get_option_int(options, "hpd_delay", 500);
        edid = video_get_option_int(options, "edid", 1);
-       video_get_option_string(options, "monitor", monitor, sizeof(monitor),
-                               "dvi");
-
-       /* Always call hdp_detect, as it also enables various clocks, etc. */
-       ret = sunxi_hdmi_hpd_detect();
-       if (hpd && !ret) {
-               sunxi_hdmi_shutdown();
-               return NULL;
+       sunxi_display.monitor = sunxi_monitor_dvi;
+#elif defined CONFIG_VIDEO_VGA_VIA_LCD
+       sunxi_display.monitor = sunxi_monitor_vga;
+#else
+       sunxi_display.monitor = sunxi_monitor_lcd;
+#endif
+       video_get_option_string(options, "monitor", mon, sizeof(mon),
+                               sunxi_get_mon_desc(sunxi_display.monitor));
+       for (i = 0; i <= SUNXI_MONITOR_LAST; i++) {
+               if (strcmp(mon, sunxi_get_mon_desc(i)) == 0) {
+                       sunxi_display.monitor = i;
+                       break;
+               }
        }
-       if (ret)
-               printf("HDMI connected: ");
+       if (i > SUNXI_MONITOR_LAST)
+               printf("Unknown monitor: '%s', falling back to '%s'\n",
+                      mon, sunxi_get_mon_desc(sunxi_display.monitor));
+
+#ifdef CONFIG_VIDEO_HDMI
+       /* If HDMI/DVI is selected do HPD & EDID, and handle fallback */
+       if (sunxi_display.monitor == sunxi_monitor_dvi ||
+           sunxi_display.monitor == sunxi_monitor_hdmi) {
+               /* Always call hdp_detect, as it also enables clocks, etc. */
+               ret = sunxi_hdmi_hpd_detect(hpd_delay);
+               if (ret) {
+                       printf("HDMI connected: ");
+                       if (edid && sunxi_hdmi_edid_get_mode(&custom) == 0)
+                               mode = &custom;
+               } else if (hpd) {
+                       sunxi_hdmi_shutdown();
+                       /* Fallback to lcd / vga / none */
+                       if (lcd_mode[0]) {
+                               sunxi_display.monitor = sunxi_monitor_lcd;
+                       } else {
+#if defined CONFIG_VIDEO_VGA_VIA_LCD || defined CONFIG_VIDEO_VGA
+                               sunxi_display.monitor = sunxi_monitor_vga;
+#else
+                               sunxi_display.monitor = sunxi_monitor_none;
+#endif
+                       }
+               } /* else continue with hdmi/dvi without a cable connected */
+       }
+#endif
 
-       /* Check edid if requested and we've a cable plugged in */
-       if (edid && ret) {
-               if (sunxi_hdmi_edid_get_mode(&edid_mode, monitor) == 0)
-                       mode = &edid_mode;
+       switch (sunxi_display.monitor) {
+       case sunxi_monitor_none:
+               return NULL;
+       case sunxi_monitor_dvi:
+       case sunxi_monitor_hdmi:
+#ifdef CONFIG_VIDEO_HDMI
+               break;
+#else
+               printf("HDMI/DVI not supported on this board\n");
+               sunxi_display.monitor = sunxi_monitor_none;
+               return NULL;
+#endif
+       case sunxi_monitor_lcd:
+               if (lcd_mode[0]) {
+                       sunxi_display.depth = video_get_params(&custom, lcd_mode);
+                       mode = &custom;
+                       break;
+               }
+               printf("LCD not supported on this board\n");
+               sunxi_display.monitor = sunxi_monitor_none;
+               return NULL;
+       case sunxi_monitor_vga:
+#if defined CONFIG_VIDEO_VGA_VIA_LCD || defined CONFIG_VIDEO_VGA
+               sunxi_display.depth = 18;
+               break;
+#else
+               printf("VGA not supported on this board\n");
+               sunxi_display.monitor = sunxi_monitor_none;
+               return NULL;
+#endif
        }
 
        if (mode->vmode != FB_VMODE_NONINTERLACED) {
                printf("Only non-interlaced modes supported, falling back to 1024x768\n");
                mode = &res_mode_init[RES_MODE_1024x768];
        } else {
-               printf("Setting up a %dx%d %s console\n",
-                      mode->xres, mode->yres, monitor);
+               printf("Setting up a %dx%d %s console\n", mode->xres,
+                      mode->yres, sunxi_get_mon_desc(sunxi_display.monitor));
        }
 
-       sunxi_display.enabled = true;
        sunxi_engines_init();
-       sunxi_mode_set(mode, monitor, gd->fb_base - CONFIG_SYS_SDRAM_BASE);
+       sunxi_mode_set(mode, gd->fb_base - CONFIG_SYS_SDRAM_BASE);
 
        /*
         * These are the only members of this structure that are used. All the
@@ -664,16 +1058,33 @@ int sunxi_simplefb_setup(void *blob)
 {
        static GraphicDevice *graphic_device = &sunxi_display.graphic_device;
        int offset, ret;
+       const char *pipeline = NULL;
 
-       if (!sunxi_display.enabled)
+       switch (sunxi_display.monitor) {
+       case sunxi_monitor_none:
                return 0;
+       case sunxi_monitor_dvi:
+       case sunxi_monitor_hdmi:
+               pipeline = "de_be0-lcd0-hdmi";
+               break;
+       case sunxi_monitor_lcd:
+               pipeline = "de_be0-lcd0";
+               break;
+       case sunxi_monitor_vga:
+#ifdef CONFIG_VIDEO_VGA
+               pipeline = "de_be0-lcd0-tve0";
+#elif defined CONFIG_VIDEO_VGA_VIA_LCD
+               pipeline = "de_be0-lcd0";
+#endif
+               break;
+       }
 
-       /* Find a framebuffer node, with pipeline == "de_be0-lcd0-hdmi" */
+       /* Find a prefilled simpefb node, matching out pipeline config */
        offset = fdt_node_offset_by_compatible(blob, -1,
                                               "allwinner,simple-framebuffer");
        while (offset >= 0) {
                ret = fdt_find_string(blob, offset, "allwinner,pipeline",
-                                     "de_be0-lcd0-hdmi");
+                                     pipeline);
                if (ret == 0)
                        break;
                offset = fdt_node_offset_by_compatible(blob, offset,