dm: x86: video: Add a driver-model driver for ivybridge graphics
[oweals/u-boot.git] / drivers / video / sunxi_display.c
index d92dfa88635260560c871c92536209f18e27c194..50b16a9129f8e10aa19bc9507ce2ed33d2e41444 100644 (file)
@@ -2,7 +2,7 @@
  * Display driver for Allwinner SoCs.
  *
  * (C) Copyright 2013-2014 Luc Verhaegen <libv@skynet.be>
- * (C) Copyright 2014 Hans de Goede <hdegoede@redhat.com>
+ * (C) Copyright 2014-2015 Hans de Goede <hdegoede@redhat.com>
  *
  * SPDX-License-Identifier:    GPL-2.0+
  */
 #include <asm/arch/clock.h>
 #include <asm/arch/display.h>
 #include <asm/arch/gpio.h>
+#include <asm/arch/pwm.h>
 #include <asm/global_data.h>
 #include <asm/gpio.h>
 #include <asm/io.h>
+#include <axp_pmic.h>
 #include <errno.h>
 #include <fdtdec.h>
 #include <fdt_support.h>
+#include <i2c.h>
+#include <malloc.h>
 #include <video_fb.h>
 #include "videomodes.h"
+#include "anx9804.h"
+#include "hitachi_tx18d42vm_lcd.h"
+#include "ssd2828.h"
+
+#ifdef CONFIG_VIDEO_LCD_BL_PWM_ACTIVE_LOW
+#define PWM_ON 0
+#define PWM_OFF 1
+#else
+#define PWM_ON 1
+#define PWM_OFF 0
+#endif
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -29,15 +44,27 @@ enum sunxi_monitor {
        sunxi_monitor_hdmi,
        sunxi_monitor_lcd,
        sunxi_monitor_vga,
+       sunxi_monitor_composite_pal,
+       sunxi_monitor_composite_ntsc,
+       sunxi_monitor_composite_pal_m,
+       sunxi_monitor_composite_pal_nc,
 };
-#define SUNXI_MONITOR_LAST sunxi_monitor_vga
+#define SUNXI_MONITOR_LAST sunxi_monitor_composite_pal_nc
 
 struct sunxi_display {
        GraphicDevice graphic_device;
        enum sunxi_monitor monitor;
        unsigned int depth;
+       unsigned int fb_addr;
+       unsigned int fb_size;
 } sunxi_display;
 
+const struct ctfb_res_modes composite_video_modes[2] = {
+       /*  x     y  hz  pixclk ps/kHz   le   ri  up  lo   hs vs  s  vmode */
+       { 720,  576, 50, 37037,  27000, 137,   5, 20, 27,   2, 2, 0, FB_VMODE_INTERLACED },
+       { 720,  480, 60, 37037,  27000, 116,  20, 16, 27,   2, 2, 0, FB_VMODE_INTERLACED },
+};
+
 #ifdef CONFIG_VIDEO_HDMI
 
 /*
@@ -72,7 +99,7 @@ static int sunxi_hdmi_hpd_detect(int hpd_delay)
                        CCM_HDMI_CTRL_PLL3);
 
        /* Set ahb gating to pass */
-#ifdef CONFIG_MACH_SUN6I
+#ifdef CONFIG_SUNXI_GEN_SUN6I
        setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_HDMI);
 #endif
        setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_HDMI);
@@ -101,7 +128,7 @@ static void sunxi_hdmi_shutdown(void)
        clrbits_le32(&hdmi->ctrl, SUNXI_HDMI_CTRL_ENABLE);
        clrbits_le32(&ccm->hdmi_clk_cfg, CCM_HDMI_CTRL_GATE);
        clrbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_HDMI);
-#ifdef CONFIG_MACH_SUN6I
+#ifdef CONFIG_SUNXI_GEN_SUN6I
        clrbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_HDMI);
 #endif
        clock_set_pll3(0);
@@ -270,6 +297,133 @@ static int sunxi_hdmi_edid_get_mode(struct ctfb_res_modes *mode)
 
 #endif /* CONFIG_VIDEO_HDMI */
 
+#ifdef CONFIG_MACH_SUN4I
+/*
+ * Testing has shown that on sun4i the display backend engine does not have
+ * deep enough fifo-s causing flickering / tearing in full-hd mode due to
+ * fifo underruns. So on sun4i we use the display frontend engine to do the
+ * dma from memory, as the frontend does have deep enough fifo-s.
+ */
+
+static const u32 sun4i_vert_coef[32] = {
+       0x00004000, 0x000140ff, 0x00033ffe, 0x00043ffd,
+       0x00063efc, 0xff083dfc, 0x000a3bfb, 0xff0d39fb,
+       0xff0f37fb, 0xff1136fa, 0xfe1433fb, 0xfe1631fb,
+       0xfd192ffb, 0xfd1c2cfb, 0xfd1f29fb, 0xfc2127fc,
+       0xfc2424fc, 0xfc2721fc, 0xfb291ffd, 0xfb2c1cfd,
+       0xfb2f19fd, 0xfb3116fe, 0xfb3314fe, 0xfa3611ff,
+       0xfb370fff, 0xfb390dff, 0xfb3b0a00, 0xfc3d08ff,
+       0xfc3e0600, 0xfd3f0400, 0xfe3f0300, 0xff400100,
+};
+
+static const u32 sun4i_horz_coef[64] = {
+       0x40000000, 0x00000000, 0x40fe0000, 0x0000ff03,
+       0x3ffd0000, 0x0000ff05, 0x3ffc0000, 0x0000ff06,
+       0x3efb0000, 0x0000ff08, 0x3dfb0000, 0x0000ff09,
+       0x3bfa0000, 0x0000fe0d, 0x39fa0000, 0x0000fe0f,
+       0x38fa0000, 0x0000fe10, 0x36fa0000, 0x0000fe12,
+       0x33fa0000, 0x0000fd16, 0x31fa0000, 0x0000fd18,
+       0x2ffa0000, 0x0000fd1a, 0x2cfa0000, 0x0000fc1e,
+       0x29fa0000, 0x0000fc21, 0x27fb0000, 0x0000fb23,
+       0x24fb0000, 0x0000fb26, 0x21fb0000, 0x0000fb29,
+       0x1ffc0000, 0x0000fa2b, 0x1cfc0000, 0x0000fa2e,
+       0x19fd0000, 0x0000fa30, 0x16fd0000, 0x0000fa33,
+       0x14fd0000, 0x0000fa35, 0x11fe0000, 0x0000fa37,
+       0x0ffe0000, 0x0000fa39, 0x0dfe0000, 0x0000fa3b,
+       0x0afe0000, 0x0000fa3e, 0x08ff0000, 0x0000fb3e,
+       0x06ff0000, 0x0000fb40, 0x05ff0000, 0x0000fc40,
+       0x03ff0000, 0x0000fd41, 0x01ff0000, 0x0000fe42,
+};
+
+static void sunxi_frontend_init(void)
+{
+       struct sunxi_ccm_reg * const ccm =
+               (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
+       struct sunxi_de_fe_reg * const de_fe =
+               (struct sunxi_de_fe_reg *)SUNXI_DE_FE0_BASE;
+       int i;
+
+       /* Clocks on */
+       setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_DE_FE0);
+       setbits_le32(&ccm->dram_clk_gate, 1 << CCM_DRAM_GATE_OFFSET_DE_FE0);
+       clock_set_de_mod_clock(&ccm->fe0_clk_cfg, 300000000);
+
+       setbits_le32(&de_fe->enable, SUNXI_DE_FE_ENABLE_EN);
+
+       for (i = 0; i < 32; i++) {
+               writel(sun4i_horz_coef[2 * i], &de_fe->ch0_horzcoef0[i]);
+               writel(sun4i_horz_coef[2 * i + 1], &de_fe->ch0_horzcoef1[i]);
+               writel(sun4i_vert_coef[i], &de_fe->ch0_vertcoef[i]);
+               writel(sun4i_horz_coef[2 * i], &de_fe->ch1_horzcoef0[i]);
+               writel(sun4i_horz_coef[2 * i + 1], &de_fe->ch1_horzcoef1[i]);
+               writel(sun4i_vert_coef[i], &de_fe->ch1_vertcoef[i]);
+       }
+
+       setbits_le32(&de_fe->frame_ctrl, SUNXI_DE_FE_FRAME_CTRL_COEF_RDY);
+}
+
+static void sunxi_frontend_mode_set(const struct ctfb_res_modes *mode,
+                                   unsigned int address)
+{
+       struct sunxi_de_fe_reg * const de_fe =
+               (struct sunxi_de_fe_reg *)SUNXI_DE_FE0_BASE;
+
+       setbits_le32(&de_fe->bypass, SUNXI_DE_FE_BYPASS_CSC_BYPASS);
+       writel(CONFIG_SYS_SDRAM_BASE + address, &de_fe->ch0_addr);
+       writel(mode->xres * 4, &de_fe->ch0_stride);
+       writel(SUNXI_DE_FE_INPUT_FMT_ARGB8888, &de_fe->input_fmt);
+       writel(SUNXI_DE_FE_OUTPUT_FMT_ARGB8888, &de_fe->output_fmt);
+
+       writel(SUNXI_DE_FE_HEIGHT(mode->yres) | SUNXI_DE_FE_WIDTH(mode->xres),
+              &de_fe->ch0_insize);
+       writel(SUNXI_DE_FE_HEIGHT(mode->yres) | SUNXI_DE_FE_WIDTH(mode->xres),
+              &de_fe->ch0_outsize);
+       writel(SUNXI_DE_FE_FACTOR_INT(1), &de_fe->ch0_horzfact);
+       writel(SUNXI_DE_FE_FACTOR_INT(1), &de_fe->ch0_vertfact);
+
+       writel(SUNXI_DE_FE_HEIGHT(mode->yres) | SUNXI_DE_FE_WIDTH(mode->xres),
+              &de_fe->ch1_insize);
+       writel(SUNXI_DE_FE_HEIGHT(mode->yres) | SUNXI_DE_FE_WIDTH(mode->xres),
+              &de_fe->ch1_outsize);
+       writel(SUNXI_DE_FE_FACTOR_INT(1), &de_fe->ch1_horzfact);
+       writel(SUNXI_DE_FE_FACTOR_INT(1), &de_fe->ch1_vertfact);
+
+       setbits_le32(&de_fe->frame_ctrl, SUNXI_DE_FE_FRAME_CTRL_REG_RDY);
+}
+
+static void sunxi_frontend_enable(void)
+{
+       struct sunxi_de_fe_reg * const de_fe =
+               (struct sunxi_de_fe_reg *)SUNXI_DE_FE0_BASE;
+
+       setbits_le32(&de_fe->frame_ctrl, SUNXI_DE_FE_FRAME_CTRL_FRM_START);
+}
+#else
+static void sunxi_frontend_init(void) {}
+static void sunxi_frontend_mode_set(const struct ctfb_res_modes *mode,
+                                   unsigned int address) {}
+static void sunxi_frontend_enable(void) {}
+#endif
+
+static bool sunxi_is_composite(void)
+{
+       switch (sunxi_display.monitor) {
+       case sunxi_monitor_none:
+       case sunxi_monitor_dvi:
+       case sunxi_monitor_hdmi:
+       case sunxi_monitor_lcd:
+       case sunxi_monitor_vga:
+               return false;
+       case sunxi_monitor_composite_pal:
+       case sunxi_monitor_composite_ntsc:
+       case sunxi_monitor_composite_pal_m:
+       case sunxi_monitor_composite_pal_nc:
+               return true;
+       }
+
+       return false; /* Never reached */
+}
+
 /*
  * This is the entity that mixes and matches the different layers and inputs.
  * Allwinner calls it the back-end, but i like composer better.
@@ -282,14 +436,18 @@ static void sunxi_composer_init(void)
                (struct sunxi_de_be_reg *)SUNXI_DE_BE0_BASE;
        int i;
 
-#if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I
+       sunxi_frontend_init();
+
+#ifdef CONFIG_SUNXI_GEN_SUN6I
        /* Reset off */
        setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_DE_BE0);
 #endif
 
        /* Clocks on */
        setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_DE_BE0);
+#ifndef CONFIG_MACH_SUN4I /* On sun4i the frontend does the dma */
        setbits_le32(&ccm->dram_clk_gate, 1 << CCM_DRAM_GATE_OFFSET_DE_BE0);
+#endif
        clock_set_de_mod_clock(&ccm->be0_clk_cfg, 300000000);
 
        /* Engine bug, clear registers after reset */
@@ -299,22 +457,49 @@ static void sunxi_composer_init(void)
        setbits_le32(&de_be->mode, SUNXI_DE_BE_MODE_ENABLE);
 }
 
+static u32 sunxi_rgb2yuv_coef[12] = {
+       0x00000107, 0x00000204, 0x00000064, 0x00000108,
+       0x00003f69, 0x00003ed6, 0x000001c1, 0x00000808,
+       0x000001c1, 0x00003e88, 0x00003fb8, 0x00000808
+};
+
 static void sunxi_composer_mode_set(const struct ctfb_res_modes *mode,
                                    unsigned int address)
 {
        struct sunxi_de_be_reg * const de_be =
                (struct sunxi_de_be_reg *)SUNXI_DE_BE0_BASE;
+       int i;
+
+       sunxi_frontend_mode_set(mode, address);
 
        writel(SUNXI_DE_BE_HEIGHT(mode->yres) | SUNXI_DE_BE_WIDTH(mode->xres),
               &de_be->disp_size);
        writel(SUNXI_DE_BE_HEIGHT(mode->yres) | SUNXI_DE_BE_WIDTH(mode->xres),
               &de_be->layer0_size);
+#ifndef CONFIG_MACH_SUN4I /* On sun4i the frontend does the dma */
        writel(SUNXI_DE_BE_LAYER_STRIDE(mode->xres), &de_be->layer0_stride);
        writel(address << 3, &de_be->layer0_addr_low32b);
        writel(address >> 29, &de_be->layer0_addr_high4b);
+#else
+       writel(SUNXI_DE_BE_LAYER_ATTR0_SRC_FE0, &de_be->layer0_attr0_ctrl);
+#endif
        writel(SUNXI_DE_BE_LAYER_ATTR1_FMT_XRGB8888, &de_be->layer0_attr1_ctrl);
 
        setbits_le32(&de_be->mode, SUNXI_DE_BE_MODE_LAYER0_ENABLE);
+       if (mode->vmode == FB_VMODE_INTERLACED)
+               setbits_le32(&de_be->mode,
+#ifndef CONFIG_MACH_SUN5I
+                            SUNXI_DE_BE_MODE_DEFLICKER_ENABLE |
+#endif
+                            SUNXI_DE_BE_MODE_INTERLACE_ENABLE);
+
+       if (sunxi_is_composite()) {
+               writel(SUNXI_DE_BE_OUTPUT_COLOR_CTRL_ENABLE,
+                      &de_be->output_color_ctrl);
+               for (i = 0; i < 12; i++)
+                       writel(sunxi_rgb2yuv_coef[i],
+                              &de_be->output_color_coef[i]);
+       }
 }
 
 static void sunxi_composer_enable(void)
@@ -322,6 +507,8 @@ static void sunxi_composer_enable(void)
        struct sunxi_de_be_reg * const de_be =
                (struct sunxi_de_be_reg *)SUNXI_DE_BE0_BASE;
 
+       sunxi_frontend_enable();
+
        setbits_le32(&de_be->reg_ctrl, SUNXI_DE_BE_REG_CTRL_LOAD_REGS);
        setbits_le32(&de_be->mode, SUNXI_DE_BE_MODE_START);
 }
@@ -337,6 +524,7 @@ static void sunxi_lcdc_pll_set(int tcon, int dotclock,
        int value, n, m, min_m, max_m, diff;
        int best_n = 0, best_m = 0, best_diff = 0x0FFFFFFF;
        int best_double = 0;
+       bool use_mipi_pll = false;
 
        if (tcon == 0) {
 #ifdef CONFIG_VIDEO_LCD_IF_PARALLEL
@@ -387,22 +575,51 @@ static void sunxi_lcdc_pll_set(int tcon, int dotclock,
                }
        }
 
-       debug("dotclock: %dkHz = %dkHz: (%d * 3MHz * %d) / %d\n",
-             dotclock, (best_double + 1) * 3000 * best_n / best_m,
-             best_double + 1, best_n, best_m);
+#ifdef CONFIG_MACH_SUN6I
+       /*
+        * Use the MIPI pll if we've been unable to find any matching setting
+        * for PLL3, this happens with high dotclocks because of min_m = 6.
+        */
+       if (tcon == 0 && best_n == 0) {
+               use_mipi_pll = true;
+               best_m = 6;  /* Minimum m for tcon0 */
+       }
 
-       clock_set_pll3(best_n * 3000000);
+       if (use_mipi_pll) {
+               clock_set_pll3(297000000); /* Fix the video pll at 297 MHz */
+               clock_set_mipi_pll(best_m * dotclock * 1000);
+               debug("dotclock: %dkHz = %dkHz via mipi pll\n",
+                     dotclock, clock_get_mipi_pll() / best_m / 1000);
+       } else
+#endif
+       {
+               clock_set_pll3(best_n * 3000000);
+               debug("dotclock: %dkHz = %dkHz: (%d * 3MHz * %d) / %d\n",
+                     dotclock,
+                     (best_double + 1) * clock_get_pll3() / best_m / 1000,
+                     best_double + 1, best_n, best_m);
+       }
 
        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),
+               u32 pll;
+
+               if (use_mipi_pll)
+                       pll = CCM_LCD_CH0_CTRL_MIPI_PLL;
+               else if (best_double)
+                       pll = CCM_LCD_CH0_CTRL_PLL3_2X;
+               else
+                       pll = CCM_LCD_CH0_CTRL_PLL3;
+
+               writel(CCM_LCD_CH0_CTRL_GATE | CCM_LCD_CH0_CTRL_RST | pll,
                       &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);
+               if (sunxi_is_composite())
+                       setbits_le32(&ccm->lcd0_ch1_clk_cfg,
+                                    CCM_LCD_CH1_CTRL_HALF_SCLK1);
        }
 
        *clk_div = best_m;
@@ -417,7 +634,7 @@ static void sunxi_lcdc_init(void)
                (struct sunxi_lcdc_reg *)SUNXI_LCD0_BASE;
 
        /* Reset off */
-#if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I
+#ifdef CONFIG_SUNXI_GEN_SUN6I
        setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_LCD0);
 #else
        setbits_le32(&ccm->lcd0_ch0_clk_cfg, CCM_LCD_CH0_CTRL_RST);
@@ -426,7 +643,11 @@ static void sunxi_lcdc_init(void)
        /* Clock on */
        setbits_le32(&ccm->ahb_gate1, 1 << AHB_GATE_OFFSET_LCD0);
 #ifdef CONFIG_VIDEO_LCD_IF_LVDS
+#ifdef CONFIG_SUNXI_GEN_SUN6I
+       setbits_le32(&ccm->ahb_reset2_cfg, 1 << AHB_RESET_OFFSET_LVDS);
+#else
        setbits_le32(&ccm->lvds_clk_cfg, CCM_LVDS_CTRL_RST);
+#endif
 #endif
 
        /* Init lcdc */
@@ -450,6 +671,16 @@ static void sunxi_lcdc_enable(void)
 #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);
+#ifdef CONFIG_SUNXI_GEN_SUN6I
+       udelay(2); /* delay at least 1200 ns */
+       setbits_le32(&lcdc->lvds_ana0, SUNXI_LCDC_LVDS_ANA0_EN_MB);
+       udelay(2); /* delay at least 1200 ns */
+       setbits_le32(&lcdc->lvds_ana0, SUNXI_LCDC_LVDS_ANA0_DRVC);
+       if (sunxi_display.depth == 18)
+               setbits_le32(&lcdc->lvds_ana0, SUNXI_LCDC_LVDS_ANA0_DRVD(0x7));
+       else
+               setbits_le32(&lcdc->lvds_ana0, SUNXI_LCDC_LVDS_ANA0_DRVD(0xf));
+#else
        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);
@@ -457,36 +688,45 @@ static void sunxi_lcdc_enable(void)
        setbits_le32(&lcdc->lvds_ana1, SUNXI_LCDC_LVDS_ANA1_INIT2);
        setbits_le32(&lcdc->lvds_ana0, SUNXI_LCDC_LVDS_ANA0_UPDATE);
 #endif
+#endif
 }
 
 static void sunxi_lcdc_panel_enable(void)
 {
-       int pin;
+       int pin, reset_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) {
+       if (pin >= 0) {
                gpio_request(pin, "lcd_backlight_enable");
                gpio_direction_output(pin, 0);
        }
 
        pin = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_BL_PWM);
-       if (pin != -1) {
+       if (pin >= 0) {
                gpio_request(pin, "lcd_backlight_pwm");
-               /* backlight pwm is inverted, set to 1 to disable backlight */
-               gpio_direction_output(pin, 1);
+               gpio_direction_output(pin, PWM_OFF);
+       }
+
+       reset_pin = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_RESET);
+       if (reset_pin >= 0) {
+               gpio_request(reset_pin, "lcd_reset");
+               gpio_direction_output(reset_pin, 0); /* Assert reset */
        }
 
        /* 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) {
+       if (pin >= 0) {
                gpio_request(pin, "lcd_power");
                gpio_direction_output(pin, 1);
        }
+
+       if (reset_pin >= 0)
+               gpio_direction_output(reset_pin, 1); /* De-assert reset */
 }
 
 static void sunxi_lcdc_backlight_enable(void)
@@ -500,37 +740,59 @@ static void sunxi_lcdc_backlight_enable(void)
        mdelay(40);
 
        pin = sunxi_name_to_gpio(CONFIG_VIDEO_LCD_BL_EN);
-       if (pin != -1)
+       if (pin >= 0)
                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);
+#ifdef SUNXI_PWM_PIN0
+       if (pin == SUNXI_PWM_PIN0) {
+               writel(SUNXI_PWM_CTRL_POLARITY0(PWM_ON) |
+                      SUNXI_PWM_CTRL_ENABLE0 |
+                      SUNXI_PWM_CTRL_PRESCALE0(0xf), SUNXI_PWM_CTRL_REG);
+               writel(SUNXI_PWM_PERIOD_80PCT, SUNXI_PWM_CH0_PERIOD);
+               sunxi_gpio_set_cfgpin(pin, SUNXI_PWM_MUX);
+               return;
        }
+#endif
+       if (pin >= 0)
+               gpio_direction_output(pin, PWM_ON);
 }
 
-static int sunxi_lcdc_get_clk_delay(const struct ctfb_res_modes *mode)
+static int sunxi_lcdc_get_clk_delay(const struct ctfb_res_modes *mode, int tcon)
 {
        int delay;
 
-       delay = mode->lower_margin + mode->vsync_len + mode->upper_margin - 2;
+       delay = mode->lower_margin + mode->vsync_len + mode->upper_margin;
+       if (mode->vmode == FB_VMODE_INTERLACED)
+               delay /= 2;
+       if (tcon == 1)
+               delay -= 2;
+
        return (delay > 30) ? 30 : delay;
 }
 
-static void sunxi_lcdc_tcon0_mode_set(const struct ctfb_res_modes *mode)
+static void sunxi_lcdc_tcon0_mode_set(const struct ctfb_res_modes *mode,
+                                     bool for_ext_vga_dac)
 {
        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++)
+#if defined CONFIG_MACH_SUN8I && defined CONFIG_VIDEO_LCD_IF_LVDS
+       for (pin = SUNXI_GPD(18); pin <= SUNXI_GPD(27); pin++) {
+#else
+       for (pin = SUNXI_GPD(0); pin <= SUNXI_GPD(27); pin++) {
+#endif
 #ifdef CONFIG_VIDEO_LCD_IF_PARALLEL
-               sunxi_gpio_set_cfgpin(pin, SUNXI_GPD0_LCD0);
+               sunxi_gpio_set_cfgpin(pin, SUNXI_GPD_LCD0);
 #endif
 #ifdef CONFIG_VIDEO_LCD_IF_LVDS
-               sunxi_gpio_set_cfgpin(pin, SUNXI_GPD0_LVDS0);
+               sunxi_gpio_set_cfgpin(pin, SUNXI_GPD_LVDS0);
+#endif
+#ifdef CONFIG_VIDEO_LCD_PANEL_EDP_4_LANE_1620M_VIA_ANX9804
+               sunxi_gpio_set_drv(pin, 3);
 #endif
+       }
 
        sunxi_lcdc_pll_set(0, mode->pixclock_khz, &clk_div, &clk_double);
 
@@ -538,7 +800,7 @@ static void sunxi_lcdc_tcon0_mode_set(const struct ctfb_res_modes *mode)
        clrsetbits_le32(&lcdc->ctrl, SUNXI_LCDC_CTRL_IO_MAP_MASK,
                        SUNXI_LCDC_CTRL_IO_MAP_TCON0);
 
-       clk_delay = sunxi_lcdc_get_clk_delay(mode);
+       clk_delay = sunxi_lcdc_get_clk_delay(mode, 0);
        writel(SUNXI_LCDC_TCON0_CTRL_ENABLE |
               SUNXI_LCDC_TCON0_CTRL_CLK_DELAY(clk_delay), &lcdc->tcon0_ctrl);
 
@@ -567,7 +829,8 @@ static void sunxi_lcdc_tcon0_mode_set(const struct ctfb_res_modes *mode)
 #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);
+       writel(SUNXI_LCDC_TCON0_LVDS_INTF_BITWIDTH(val) |
+              SUNXI_LCDC_TCON0_LVDS_CLK_SEL_TCON0, &lcdc->tcon0_lvds_intf);
 #endif
 
        if (sunxi_display.depth == 18 || sunxi_display.depth == 16) {
@@ -587,43 +850,48 @@ static void sunxi_lcdc_tcon0_mode_set(const struct ctfb_res_modes *mode)
                       &lcdc->tcon0_frm_ctrl);
        }
 
-#ifdef CONFIG_VIDEO_LCD_IF_PARALLEL
-       val = SUNXI_LCDC_TCON0_IO_POL_DCLK_PHASE0;
-#endif
-#ifdef CONFIG_VIDEO_LCD_IF_LVDS
-       val = SUNXI_LCDC_TCON0_IO_POL_DCLK_PHASE60;
-#endif
+       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;
+
+#ifdef CONFIG_VIDEO_VGA_VIA_LCD_FORCE_SYNC_ACTIVE_HIGH
+       if (for_ext_vga_dac)
+               val = 0;
+#endif
        writel(val, &lcdc->tcon0_io_polarity);
 
        writel(0, &lcdc->tcon0_io_tristate);
 }
 
-#if defined CONFIG_VIDEO_HDMI || defined CONFIG_VIDEO_VGA
+#if defined CONFIG_VIDEO_HDMI || defined CONFIG_VIDEO_VGA || defined CONFIG_VIDEO_COMPOSITE
 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;
+       int bp, clk_delay, total, val, yres;
 
        /* Use tcon1 */
        clrsetbits_le32(&lcdc->ctrl, SUNXI_LCDC_CTRL_IO_MAP_MASK,
                        SUNXI_LCDC_CTRL_IO_MAP_TCON1);
 
-       clk_delay = sunxi_lcdc_get_clk_delay(mode);
+       clk_delay = sunxi_lcdc_get_clk_delay(mode, 1);
        writel(SUNXI_LCDC_TCON1_CTRL_ENABLE |
+              ((mode->vmode == FB_VMODE_INTERLACED) ?
+                       SUNXI_LCDC_TCON1_CTRL_INTERLACE_ENABLE : 0) |
               SUNXI_LCDC_TCON1_CTRL_CLK_DELAY(clk_delay), &lcdc->tcon1_ctrl);
 
-       writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(mode->yres),
+       yres = mode->yres;
+       if (mode->vmode == FB_VMODE_INTERLACED)
+               yres /= 2;
+       writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(yres),
               &lcdc->tcon1_timing_source);
-       writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(mode->yres),
+       writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(yres),
               &lcdc->tcon1_timing_scale);
-       writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(mode->yres),
+       writel(SUNXI_LCDC_X(mode->xres) | SUNXI_LCDC_Y(yres),
               &lcdc->tcon1_timing_out);
 
        bp = mode->hsync_len + mode->left_margin;
@@ -633,6 +901,8 @@ static void sunxi_lcdc_tcon1_mode_set(const struct ctfb_res_modes *mode,
 
        bp = mode->vsync_len + mode->upper_margin;
        total = mode->yres + mode->lower_margin + bp;
+       if (mode->vmode == FB_VMODE_NONINTERLACED)
+               total *= 2;
        writel(SUNXI_LCDC_TCON1_TIMING_V_TOTAL(total) |
               SUNXI_LCDC_TCON1_TIMING_V_BP(bp), &lcdc->tcon1_timing_v);
 
@@ -640,8 +910,8 @@ static void sunxi_lcdc_tcon1_mode_set(const struct ctfb_res_modes *mode,
               &lcdc->tcon1_timing_sync);
 
        if (use_portd_hvsync) {
-               sunxi_gpio_set_cfgpin(SUNXI_GPD(26), SUNXI_GPD0_LCD0);
-               sunxi_gpio_set_cfgpin(SUNXI_GPD(27), SUNXI_GPD0_LCD0);
+               sunxi_gpio_set_cfgpin(SUNXI_GPD(26), SUNXI_GPD_LCD0);
+               sunxi_gpio_set_cfgpin(SUNXI_GPD(27), SUNXI_GPD_LCD0);
 
                val = 0;
                if (mode->sync & FB_SYNC_HOR_HIGH_ACT)
@@ -654,9 +924,16 @@ static void sunxi_lcdc_tcon1_mode_set(const struct ctfb_res_modes *mode,
                             SUNXI_LCDC_TCON_VSYNC_MASK |
                             SUNXI_LCDC_TCON_HSYNC_MASK);
        }
+
+#ifdef CONFIG_MACH_SUN5I
+       if (sunxi_is_composite())
+               clrsetbits_le32(&lcdc->mux_ctrl, SUNXI_LCDC_MUX_CTRL_SRC0_MASK,
+                               SUNXI_LCDC_MUX_CTRL_SRC0(1));
+#endif
+
        sunxi_lcdc_pll_set(1, mode->pixclock_khz, clk_div, clk_double);
 }
-#endif /* CONFIG_VIDEO_HDMI || defined CONFIG_VIDEO_VGA */
+#endif /* CONFIG_VIDEO_HDMI || defined CONFIG_VIDEO_VGA || CONFIG_VIDEO_COMPOSITE */
 
 #ifdef CONFIG_VIDEO_HDMI
 
@@ -770,28 +1047,89 @@ static void sunxi_hdmi_enable(void)
 
 #endif /* CONFIG_VIDEO_HDMI */
 
-#ifdef CONFIG_VIDEO_VGA
+#if defined CONFIG_VIDEO_VGA || defined CONFIG_VIDEO_COMPOSITE
 
-static void sunxi_vga_mode_set(void)
+static void sunxi_tvencoder_mode_set(void)
 {
        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;
 
+       /* Reset off */
+       setbits_le32(&ccm->lcd0_ch0_clk_cfg, CCM_LCD_CH0_CTRL_TVE_RST);
        /* 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);
+       switch (sunxi_display.monitor) {
+       case sunxi_monitor_vga:
+               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_CFG0_VGA, &tve->cfg0);
+               writel(SUNXI_TVE_DAC_CFG0_VGA, &tve->dac_cfg0);
+               writel(SUNXI_TVE_UNKNOWN1_VGA, &tve->unknown1);
+               break;
+       case sunxi_monitor_composite_pal_nc:
+               writel(SUNXI_TVE_CHROMA_FREQ_PAL_NC, &tve->chroma_freq);
+               /* Fall through */
+       case sunxi_monitor_composite_pal:
+               writel(SUNXI_TVE_GCTRL_DAC_INPUT(0, 1) |
+                      SUNXI_TVE_GCTRL_DAC_INPUT(1, 2) |
+                      SUNXI_TVE_GCTRL_DAC_INPUT(2, 3) |
+                      SUNXI_TVE_GCTRL_DAC_INPUT(3, 4), &tve->gctrl);
+               writel(SUNXI_TVE_CFG0_PAL, &tve->cfg0);
+               writel(SUNXI_TVE_DAC_CFG0_COMPOSITE, &tve->dac_cfg0);
+               writel(SUNXI_TVE_FILTER_COMPOSITE, &tve->filter);
+               writel(SUNXI_TVE_PORCH_NUM_PAL, &tve->porch_num);
+               writel(SUNXI_TVE_LINE_NUM_PAL, &tve->line_num);
+               writel(SUNXI_TVE_BLANK_BLACK_LEVEL_PAL, &tve->blank_black_level);
+               writel(SUNXI_TVE_UNKNOWN1_COMPOSITE, &tve->unknown1);
+               writel(SUNXI_TVE_CBR_LEVEL_PAL, &tve->cbr_level);
+               writel(SUNXI_TVE_BURST_WIDTH_COMPOSITE, &tve->burst_width);
+               writel(SUNXI_TVE_UNKNOWN2_PAL, &tve->unknown2);
+               writel(SUNXI_TVE_ACTIVE_NUM_COMPOSITE, &tve->active_num);
+               writel(SUNXI_TVE_CHROMA_BW_GAIN_COMP, &tve->chroma_bw_gain);
+               writel(SUNXI_TVE_NOTCH_WIDTH_COMPOSITE, &tve->notch_width);
+               writel(SUNXI_TVE_RESYNC_NUM_PAL, &tve->resync_num);
+               writel(SUNXI_TVE_SLAVE_PARA_COMPOSITE, &tve->slave_para);
+               break;
+       case sunxi_monitor_composite_pal_m:
+               writel(SUNXI_TVE_CHROMA_FREQ_PAL_M, &tve->chroma_freq);
+               writel(SUNXI_TVE_COLOR_BURST_PAL_M, &tve->color_burst);
+               /* Fall through */
+       case sunxi_monitor_composite_ntsc:
+               writel(SUNXI_TVE_GCTRL_DAC_INPUT(0, 1) |
+                      SUNXI_TVE_GCTRL_DAC_INPUT(1, 2) |
+                      SUNXI_TVE_GCTRL_DAC_INPUT(2, 3) |
+                      SUNXI_TVE_GCTRL_DAC_INPUT(3, 4), &tve->gctrl);
+               writel(SUNXI_TVE_CFG0_NTSC, &tve->cfg0);
+               writel(SUNXI_TVE_DAC_CFG0_COMPOSITE, &tve->dac_cfg0);
+               writel(SUNXI_TVE_FILTER_COMPOSITE, &tve->filter);
+               writel(SUNXI_TVE_PORCH_NUM_NTSC, &tve->porch_num);
+               writel(SUNXI_TVE_LINE_NUM_NTSC, &tve->line_num);
+               writel(SUNXI_TVE_BLANK_BLACK_LEVEL_NTSC, &tve->blank_black_level);
+               writel(SUNXI_TVE_UNKNOWN1_COMPOSITE, &tve->unknown1);
+               writel(SUNXI_TVE_CBR_LEVEL_NTSC, &tve->cbr_level);
+               writel(SUNXI_TVE_BURST_PHASE_NTSC, &tve->burst_phase);
+               writel(SUNXI_TVE_BURST_WIDTH_COMPOSITE, &tve->burst_width);
+               writel(SUNXI_TVE_UNKNOWN2_NTSC, &tve->unknown2);
+               writel(SUNXI_TVE_SYNC_VBI_LEVEL_NTSC, &tve->sync_vbi_level);
+               writel(SUNXI_TVE_ACTIVE_NUM_COMPOSITE, &tve->active_num);
+               writel(SUNXI_TVE_CHROMA_BW_GAIN_COMP, &tve->chroma_bw_gain);
+               writel(SUNXI_TVE_NOTCH_WIDTH_COMPOSITE, &tve->notch_width);
+               writel(SUNXI_TVE_RESYNC_NUM_NTSC, &tve->resync_num);
+               writel(SUNXI_TVE_SLAVE_PARA_COMPOSITE, &tve->slave_para);
+               break;
+       case sunxi_monitor_none:
+       case sunxi_monitor_dvi:
+       case sunxi_monitor_hdmi:
+       case sunxi_monitor_lcd:
+               break;
+       }
 }
 
-static void sunxi_vga_enable(void)
+static void sunxi_tvencoder_enable(void)
 {
        struct sunxi_tve_reg * const tve =
                (struct sunxi_tve_reg *)SUNXI_TVE0_BASE;
@@ -799,15 +1137,18 @@ static void sunxi_vga_enable(void)
        setbits_le32(&tve->gctrl, SUNXI_TVE_GCTRL_ENABLE);
 }
 
-#endif /* CONFIG_VIDEO_VGA */
+#endif /* CONFIG_VIDEO_VGA || defined CONFIG_VIDEO_COMPOSITE */
 
 static void sunxi_drc_init(void)
 {
-#if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I
+#ifdef CONFIG_SUNXI_GEN_SUN6I
        struct sunxi_ccm_reg * const ccm =
                (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
 
        /* On sun6i the drc must be clocked even when in pass-through mode */
+#ifdef CONFIG_MACH_SUN8I_A33
+       setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_SAT);
+#endif
        setbits_le32(&ccm->ahb_reset1_cfg, 1 << AHB_RESET_OFFSET_DRC0);
        clock_set_de_mod_clock(&ccm->iep_drc0_clk_cfg, 300000000);
 #endif
@@ -819,13 +1160,47 @@ static void sunxi_vga_external_dac_enable(void)
        int pin;
 
        pin = sunxi_name_to_gpio(CONFIG_VIDEO_VGA_EXTERNAL_DAC_EN);
-       if (pin != -1) {
+       if (pin >= 0) {
                gpio_request(pin, "vga_enable");
                gpio_direction_output(pin, 1);
        }
 }
 #endif /* CONFIG_VIDEO_VGA_VIA_LCD */
 
+#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
+       };
+
+       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();
@@ -854,26 +1229,63 @@ static void sunxi_mode_set(const struct ctfb_res_modes *mode,
                break;
        case sunxi_monitor_lcd:
                sunxi_lcdc_panel_enable();
+               if (IS_ENABLED(CONFIG_VIDEO_LCD_PANEL_EDP_4_LANE_1620M_VIA_ANX9804)) {
+                       /*
+                        * The anx9804 needs 1.8V from eldo3, we do this here
+                        * and not via CONFIG_AXP_ELDO3_VOLT from board_init()
+                        * to avoid turning this on when using hdmi output.
+                        */
+                       axp_set_eldo(3, 1800);
+                       anx9804_init(CONFIG_VIDEO_LCD_I2C_BUS, 4,
+                                    ANX9804_DATA_RATE_1620M,
+                                    sunxi_display.depth);
+               }
+               if (IS_ENABLED(CONFIG_VIDEO_LCD_HITACHI_TX18D42VM)) {
+                       mdelay(50); /* Wait for lcd controller power on */
+                       hitachi_tx18d42vm_init();
+               }
+               if (IS_ENABLED(CONFIG_VIDEO_LCD_TL059WV5C0)) {
+                       unsigned int orig_i2c_bus = i2c_get_bus_num();
+                       i2c_set_bus_num(CONFIG_VIDEO_LCD_I2C_BUS);
+                       i2c_reg_write(0x5c, 0x04, 0x42); /* Turn on the LCD */
+                       i2c_set_bus_num(orig_i2c_bus);
+               }
                sunxi_composer_mode_set(mode, address);
-               sunxi_lcdc_tcon0_mode_set(mode);
+               sunxi_lcdc_tcon0_mode_set(mode, false);
                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_tvencoder_mode_set();
                sunxi_composer_enable();
                sunxi_lcdc_enable();
-               sunxi_vga_enable();
+               sunxi_tvencoder_enable();
 #elif defined CONFIG_VIDEO_VGA_VIA_LCD
                sunxi_composer_mode_set(mode, address);
-               sunxi_lcdc_tcon0_mode_set(mode);
+               sunxi_lcdc_tcon0_mode_set(mode, true);
                sunxi_composer_enable();
                sunxi_lcdc_enable();
                sunxi_vga_external_dac_enable();
+#endif
+               break;
+       case sunxi_monitor_composite_pal:
+       case sunxi_monitor_composite_ntsc:
+       case sunxi_monitor_composite_pal_m:
+       case sunxi_monitor_composite_pal_nc:
+#ifdef CONFIG_VIDEO_COMPOSITE
+               sunxi_composer_mode_set(mode, address);
+               sunxi_lcdc_tcon1_mode_set(mode, &clk_div, &clk_double, 0);
+               sunxi_tvencoder_mode_set();
+               sunxi_composer_enable();
+               sunxi_lcdc_enable();
+               sunxi_tvencoder_enable();
 #endif
                break;
        }
@@ -882,15 +1294,72 @@ static void sunxi_mode_set(const struct ctfb_res_modes *mode,
 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";
+       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";
+       case sunxi_monitor_composite_pal:       return "composite-pal";
+       case sunxi_monitor_composite_ntsc:      return "composite-ntsc";
+       case sunxi_monitor_composite_pal_m:     return "composite-pal-m";
+       case sunxi_monitor_composite_pal_nc:    return "composite-pal-nc";
        }
        return NULL; /* never reached */
 }
 
+ulong board_get_usable_ram_top(ulong total_size)
+{
+       return gd->ram_top - CONFIG_SUNXI_MAX_FB_SIZE;
+}
+
+static bool sunxi_has_hdmi(void)
+{
+#ifdef CONFIG_VIDEO_HDMI
+       return true;
+#else
+       return false;
+#endif
+}
+
+static bool sunxi_has_lcd(void)
+{
+       char *lcd_mode = CONFIG_VIDEO_LCD_MODE;
+
+       return lcd_mode[0] != 0;
+}
+
+static bool sunxi_has_vga(void)
+{
+#if defined CONFIG_VIDEO_VGA || defined CONFIG_VIDEO_VGA_VIA_LCD
+       return true;
+#else
+       return false;
+#endif
+}
+
+static bool sunxi_has_composite(void)
+{
+#ifdef CONFIG_VIDEO_COMPOSITE
+       return true;
+#else
+       return false;
+#endif
+}
+
+static enum sunxi_monitor sunxi_get_default_mon(bool allow_hdmi)
+{
+       if (allow_hdmi && sunxi_has_hdmi())
+               return sunxi_monitor_dvi;
+       else if (sunxi_has_lcd())
+               return sunxi_monitor_lcd;
+       else if (sunxi_has_vga())
+               return sunxi_monitor_vga;
+       else if (sunxi_has_composite())
+               return sunxi_monitor_composite_pal;
+       else
+               return sunxi_monitor_none;
+}
+
 void *video_hw_init(void)
 {
        static GraphicDevice *graphic_device = &sunxi_display.graphic_device;
@@ -900,28 +1369,23 @@ void *video_hw_init(void)
 #ifdef CONFIG_VIDEO_HDMI
        int ret, hpd, hpd_delay, edid;
 #endif
+       int i, overscan_offset, overscan_x, overscan_y;
+       unsigned int fb_dma_addr;
        char mon[16];
        char *lcd_mode = CONFIG_VIDEO_LCD_MODE;
-       int i;
 
        memset(&sunxi_display, 0, sizeof(struct sunxi_display));
 
-       printf("Reserved %dkB of RAM for Framebuffer.\n",
-              CONFIG_SUNXI_FB_SIZE >> 10);
-       gd->fb_base = gd->ram_top;
-
        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);
-       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
+       overscan_x = video_get_option_int(options, "overscan_x", -1);
+       overscan_y = video_get_option_int(options, "overscan_y", -1);
+       sunxi_display.monitor = sunxi_get_default_mon(true);
        video_get_option_string(options, "monitor", mon, sizeof(mon),
                                sunxi_get_mon_desc(sunxi_display.monitor));
        for (i = 0; i <= SUNXI_MONITOR_LAST; i++) {
@@ -946,16 +1410,7 @@ void *video_hw_init(void)
                                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
-                       }
+                       sunxi_display.monitor = sunxi_get_default_mon(false);
                } /* else continue with hdmi/dvi without a cable connected */
        }
 #endif
@@ -965,54 +1420,98 @@ void *video_hw_init(void)
                return NULL;
        case sunxi_monitor_dvi:
        case sunxi_monitor_hdmi:
-#ifdef CONFIG_VIDEO_HDMI
+               if (!sunxi_has_hdmi()) {
+                       printf("HDMI/DVI not supported on this board\n");
+                       sunxi_display.monitor = sunxi_monitor_none;
+                       return NULL;
+               }
                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;
+               if (!sunxi_has_lcd()) {
+                       printf("LCD not supported on this board\n");
+                       sunxi_display.monitor = sunxi_monitor_none;
+                       return NULL;
                }
-               printf("LCD not supported on this board\n");
-               sunxi_display.monitor = sunxi_monitor_none;
-               return NULL;
+               sunxi_display.depth = video_get_params(&custom, lcd_mode);
+               mode = &custom;
+               break;
        case sunxi_monitor_vga:
-#if defined CONFIG_VIDEO_VGA_VIA_LCD || defined CONFIG_VIDEO_VGA
+               if (!sunxi_has_vga()) {
+                       printf("VGA not supported on this board\n");
+                       sunxi_display.monitor = sunxi_monitor_none;
+                       return NULL;
+               }
                sunxi_display.depth = 18;
                break;
-#else
-               printf("VGA not supported on this board\n");
-               sunxi_display.monitor = sunxi_monitor_none;
-               return NULL;
-#endif
+       case sunxi_monitor_composite_pal:
+       case sunxi_monitor_composite_ntsc:
+       case sunxi_monitor_composite_pal_m:
+       case sunxi_monitor_composite_pal_nc:
+               if (!sunxi_has_composite()) {
+                       printf("Composite video not supported on this board\n");
+                       sunxi_display.monitor = sunxi_monitor_none;
+                       return NULL;
+               }
+               if (sunxi_display.monitor == sunxi_monitor_composite_pal ||
+                   sunxi_display.monitor == sunxi_monitor_composite_pal_nc)
+                       mode = &composite_video_modes[0];
+               else
+                       mode = &composite_video_modes[1];
+               sunxi_display.depth = 24;
+               break;
        }
 
-       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, sunxi_get_mon_desc(sunxi_display.monitor));
+       /* Yes these defaults are quite high, overscan on composite sucks... */
+       if (overscan_x == -1)
+               overscan_x = sunxi_is_composite() ? 32 : 0;
+       if (overscan_y == -1)
+               overscan_y = sunxi_is_composite() ? 20 : 0;
+
+       sunxi_display.fb_size =
+               (mode->xres * mode->yres * 4 + 0xfff) & ~0xfff;
+       overscan_offset = (overscan_y * mode->xres + overscan_x) * 4;
+       /* We want to keep the fb_base for simplefb page aligned, where as
+        * the sunxi dma engines will happily accept an unaligned address. */
+       if (overscan_offset)
+               sunxi_display.fb_size += 0x1000;
+
+       if (sunxi_display.fb_size > CONFIG_SUNXI_MAX_FB_SIZE) {
+               printf("Error need %dkB for fb, but only %dkB is reserved\n",
+                      sunxi_display.fb_size >> 10,
+                      CONFIG_SUNXI_MAX_FB_SIZE >> 10);
+               return NULL;
        }
 
+       printf("Setting up a %dx%d%s %s console (overscan %dx%d)\n",
+              mode->xres, mode->yres,
+              (mode->vmode == FB_VMODE_INTERLACED) ? "i" : "",
+              sunxi_get_mon_desc(sunxi_display.monitor),
+              overscan_x, overscan_y);
+
+       gd->fb_base = gd->bd->bi_dram[0].start +
+                     gd->bd->bi_dram[0].size - sunxi_display.fb_size;
        sunxi_engines_init();
-       sunxi_mode_set(mode, gd->fb_base - CONFIG_SYS_SDRAM_BASE);
+
+       fb_dma_addr = gd->fb_base - CONFIG_SYS_SDRAM_BASE;
+       sunxi_display.fb_addr = gd->fb_base;
+       if (overscan_offset) {
+               fb_dma_addr += 0x1000 - (overscan_offset & 0xfff);
+               sunxi_display.fb_addr += (overscan_offset + 0xfff) & ~0xfff;
+               memset((void *)gd->fb_base, 0, sunxi_display.fb_size);
+               flush_cache(gd->fb_base, sunxi_display.fb_size);
+       }
+       sunxi_mode_set(mode, fb_dma_addr);
 
        /*
         * These are the only members of this structure that are used. All the
-        * others are driver specific. There is nothing to decribe pitch or
-        * stride, but we are lucky with our hw.
+        * others are driver specific. The pitch is stored in plnSizeX.
         */
-       graphic_device->frameAdrs = gd->fb_base;
+       graphic_device->frameAdrs = sunxi_display.fb_addr;
        graphic_device->gdfIndex = GDF_32BIT_X888RGB;
        graphic_device->gdfBytesPP = 4;
-       graphic_device->winSizeX = mode->xres;
-       graphic_device->winSizeY = mode->yres;
+       graphic_device->winSizeX = mode->xres - 2 * overscan_x;
+       graphic_device->winSizeY = mode->yres - 2 * overscan_y;
+       graphic_device->plnSizeX = mode->xres * graphic_device->gdfBytesPP;
 
        return graphic_device;
 }
@@ -1025,25 +1524,38 @@ int sunxi_simplefb_setup(void *blob)
 {
        static GraphicDevice *graphic_device = &sunxi_display.graphic_device;
        int offset, ret;
+       u64 start, size;
        const char *pipeline = NULL;
 
+#ifdef CONFIG_MACH_SUN4I
+#define PIPELINE_PREFIX "de_fe0-"
+#else
+#define PIPELINE_PREFIX
+#endif
+
        switch (sunxi_display.monitor) {
        case sunxi_monitor_none:
                return 0;
        case sunxi_monitor_dvi:
        case sunxi_monitor_hdmi:
-               pipeline = "de_be0-lcd0-hdmi";
+               pipeline = PIPELINE_PREFIX "de_be0-lcd0-hdmi";
                break;
        case sunxi_monitor_lcd:
-               pipeline = "de_be0-lcd0";
+               pipeline = PIPELINE_PREFIX "de_be0-lcd0";
                break;
        case sunxi_monitor_vga:
 #ifdef CONFIG_VIDEO_VGA
-               pipeline = "de_be0-lcd0-tve0";
+               pipeline = PIPELINE_PREFIX "de_be0-lcd0-tve0";
 #elif defined CONFIG_VIDEO_VGA_VIA_LCD
-               pipeline = "de_be0-lcd0";
+               pipeline = PIPELINE_PREFIX "de_be0-lcd0";
 #endif
                break;
+       case sunxi_monitor_composite_pal:
+       case sunxi_monitor_composite_ntsc:
+       case sunxi_monitor_composite_pal_m:
+       case sunxi_monitor_composite_pal_nc:
+               pipeline = PIPELINE_PREFIX "de_be0-lcd0-tve0";
+               break;
        }
 
        /* Find a prefilled simpefb node, matching out pipeline config */
@@ -1062,10 +1574,23 @@ int sunxi_simplefb_setup(void *blob)
                return 0; /* Keep older kernels working */
        }
 
-       ret = fdt_setup_simplefb_node(blob, offset, gd->fb_base,
+       /*
+        * Do not report the framebuffer as free RAM to the OS, note we cannot
+        * use fdt_add_mem_rsv() here, because then it is still seen as RAM,
+        * and e.g. Linux refuses to iomap RAM on ARM, see:
+        * linux/arch/arm/mm/ioremap.c around line 301.
+        */
+       start = gd->bd->bi_dram[0].start;
+       size = gd->bd->bi_dram[0].size - sunxi_display.fb_size;
+       ret = fdt_fixup_memory_banks(blob, &start, &size, 1);
+       if (ret) {
+               eprintf("Cannot setup simplefb: Error reserving memory\n");
+               return ret;
+       }
+
+       ret = fdt_setup_simplefb_node(blob, offset, sunxi_display.fb_addr,
                        graphic_device->winSizeX, graphic_device->winSizeY,
-                       graphic_device->winSizeX * graphic_device->gdfBytesPP,
-                       "x8r8g8b8");
+                       graphic_device->plnSizeX, "x8r8g8b8");
        if (ret)
                eprintf("Cannot setup simplefb: Error setting properties\n");