19026e0d1SMaxime Ripard /* 29026e0d1SMaxime Ripard * Copyright (C) 2015 Free Electrons 39026e0d1SMaxime Ripard * Copyright (C) 2015 NextThing Co 49026e0d1SMaxime Ripard * 59026e0d1SMaxime Ripard * Maxime Ripard <maxime.ripard@free-electrons.com> 69026e0d1SMaxime Ripard * 79026e0d1SMaxime Ripard * This program is free software; you can redistribute it and/or 89026e0d1SMaxime Ripard * modify it under the terms of the GNU General Public License as 99026e0d1SMaxime Ripard * published by the Free Software Foundation; either version 2 of 109026e0d1SMaxime Ripard * the License, or (at your option) any later version. 119026e0d1SMaxime Ripard */ 129026e0d1SMaxime Ripard 139026e0d1SMaxime Ripard #include <drm/drmP.h> 149026e0d1SMaxime Ripard #include <drm/drm_atomic_helper.h> 159026e0d1SMaxime Ripard #include <drm/drm_crtc.h> 169026e0d1SMaxime Ripard #include <drm/drm_crtc_helper.h> 17ad537fb2SChen-Yu Tsai #include <drm/drm_encoder.h> 189026e0d1SMaxime Ripard #include <drm/drm_modes.h> 19ebc94461SRob Herring #include <drm/drm_of.h> 209026e0d1SMaxime Ripard 21ad537fb2SChen-Yu Tsai #include <uapi/drm/drm_mode.h> 22ad537fb2SChen-Yu Tsai 239026e0d1SMaxime Ripard #include <linux/component.h> 249026e0d1SMaxime Ripard #include <linux/ioport.h> 259026e0d1SMaxime Ripard #include <linux/of_address.h> 2691ea2f29SChen-Yu Tsai #include <linux/of_device.h> 279026e0d1SMaxime Ripard #include <linux/of_irq.h> 289026e0d1SMaxime Ripard #include <linux/regmap.h> 299026e0d1SMaxime Ripard #include <linux/reset.h> 309026e0d1SMaxime Ripard 319026e0d1SMaxime Ripard #include "sun4i_crtc.h" 329026e0d1SMaxime Ripard #include "sun4i_dotclock.h" 339026e0d1SMaxime Ripard #include "sun4i_drv.h" 3429e57fabSMaxime Ripard #include "sun4i_rgb.h" 359026e0d1SMaxime Ripard #include "sun4i_tcon.h" 3687969338SIcenowy Zheng #include "sunxi_engine.h" 379026e0d1SMaxime Ripard 3845e88f99SMaxime Ripard static void sun4i_tcon_channel_set_status(struct sun4i_tcon *tcon, int channel, 3945e88f99SMaxime Ripard bool enabled) 409026e0d1SMaxime Ripard { 4145e88f99SMaxime Ripard struct clk *clk; 429026e0d1SMaxime Ripard 4345e88f99SMaxime Ripard switch (channel) { 4445e88f99SMaxime Ripard case 0: 459026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 469026e0d1SMaxime Ripard SUN4I_TCON0_CTL_TCON_ENABLE, 4745e88f99SMaxime Ripard enabled ? SUN4I_TCON0_CTL_TCON_ENABLE : 0); 4845e88f99SMaxime Ripard clk = tcon->dclk; 4945e88f99SMaxime Ripard break; 5045e88f99SMaxime Ripard case 1: 5191ea2f29SChen-Yu Tsai WARN_ON(!tcon->quirks->has_channel_1); 529026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 539026e0d1SMaxime Ripard SUN4I_TCON1_CTL_TCON_ENABLE, 5445e88f99SMaxime Ripard enabled ? SUN4I_TCON1_CTL_TCON_ENABLE : 0); 5545e88f99SMaxime Ripard clk = tcon->sclk1; 5645e88f99SMaxime Ripard break; 5745e88f99SMaxime Ripard default: 5845e88f99SMaxime Ripard DRM_WARN("Unknown channel... doing nothing\n"); 5945e88f99SMaxime Ripard return; 609026e0d1SMaxime Ripard } 6145e88f99SMaxime Ripard 6245e88f99SMaxime Ripard if (enabled) 6345e88f99SMaxime Ripard clk_prepare_enable(clk); 6445e88f99SMaxime Ripard else 6545e88f99SMaxime Ripard clk_disable_unprepare(clk); 6645e88f99SMaxime Ripard } 6745e88f99SMaxime Ripard 6845e88f99SMaxime Ripard void sun4i_tcon_set_status(struct sun4i_tcon *tcon, 6945e88f99SMaxime Ripard const struct drm_encoder *encoder, 7045e88f99SMaxime Ripard bool enabled) 7145e88f99SMaxime Ripard { 7245e88f99SMaxime Ripard int channel; 7345e88f99SMaxime Ripard 7445e88f99SMaxime Ripard switch (encoder->encoder_type) { 7545e88f99SMaxime Ripard case DRM_MODE_ENCODER_NONE: 7645e88f99SMaxime Ripard channel = 0; 7745e88f99SMaxime Ripard break; 7845e88f99SMaxime Ripard case DRM_MODE_ENCODER_TMDS: 7945e88f99SMaxime Ripard case DRM_MODE_ENCODER_TVDAC: 8045e88f99SMaxime Ripard channel = 1; 8145e88f99SMaxime Ripard break; 8245e88f99SMaxime Ripard default: 8345e88f99SMaxime Ripard DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n"); 8445e88f99SMaxime Ripard return; 8545e88f99SMaxime Ripard } 8645e88f99SMaxime Ripard 8745e88f99SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 8845e88f99SMaxime Ripard SUN4I_TCON_GCTL_TCON_ENABLE, 8945e88f99SMaxime Ripard enabled ? SUN4I_TCON_GCTL_TCON_ENABLE : 0); 9045e88f99SMaxime Ripard 9145e88f99SMaxime Ripard sun4i_tcon_channel_set_status(tcon, channel, enabled); 9245e88f99SMaxime Ripard } 939026e0d1SMaxime Ripard 949026e0d1SMaxime Ripard void sun4i_tcon_enable_vblank(struct sun4i_tcon *tcon, bool enable) 959026e0d1SMaxime Ripard { 969026e0d1SMaxime Ripard u32 mask, val = 0; 979026e0d1SMaxime Ripard 989026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("%sabling VBLANK interrupt\n", enable ? "En" : "Dis"); 999026e0d1SMaxime Ripard 1009026e0d1SMaxime Ripard mask = SUN4I_TCON_GINT0_VBLANK_ENABLE(0) | 1019026e0d1SMaxime Ripard SUN4I_TCON_GINT0_VBLANK_ENABLE(1); 1029026e0d1SMaxime Ripard 1039026e0d1SMaxime Ripard if (enable) 1049026e0d1SMaxime Ripard val = mask; 1059026e0d1SMaxime Ripard 1069026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, mask, val); 1079026e0d1SMaxime Ripard } 1089026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_enable_vblank); 1099026e0d1SMaxime Ripard 11067e32645SChen-Yu Tsai /* 11167e32645SChen-Yu Tsai * This function is a helper for TCON output muxing. The TCON output 11267e32645SChen-Yu Tsai * muxing control register in earlier SoCs (without the TCON TOP block) 11367e32645SChen-Yu Tsai * are located in TCON0. This helper returns a pointer to TCON0's 11467e32645SChen-Yu Tsai * sun4i_tcon structure, or NULL if not found. 11567e32645SChen-Yu Tsai */ 11667e32645SChen-Yu Tsai static struct sun4i_tcon *sun4i_get_tcon0(struct drm_device *drm) 11767e32645SChen-Yu Tsai { 11867e32645SChen-Yu Tsai struct sun4i_drv *drv = drm->dev_private; 11967e32645SChen-Yu Tsai struct sun4i_tcon *tcon; 12067e32645SChen-Yu Tsai 12167e32645SChen-Yu Tsai list_for_each_entry(tcon, &drv->tcon_list, list) 12267e32645SChen-Yu Tsai if (tcon->id == 0) 12367e32645SChen-Yu Tsai return tcon; 12467e32645SChen-Yu Tsai 12567e32645SChen-Yu Tsai dev_warn(drm->dev, 12667e32645SChen-Yu Tsai "TCON0 not found, display output muxing may not work\n"); 12767e32645SChen-Yu Tsai 12867e32645SChen-Yu Tsai return NULL; 12967e32645SChen-Yu Tsai } 13067e32645SChen-Yu Tsai 131f8c73f4fSMaxime Ripard void sun4i_tcon_set_mux(struct sun4i_tcon *tcon, int channel, 132abcb8766SMaxime Ripard const struct drm_encoder *encoder) 133f8c73f4fSMaxime Ripard { 134ad537fb2SChen-Yu Tsai int ret = -ENOTSUPP; 135b7cb9b91SMaxime Ripard 136ad537fb2SChen-Yu Tsai if (tcon->quirks->set_mux) 137ad537fb2SChen-Yu Tsai ret = tcon->quirks->set_mux(tcon, encoder); 138f8c73f4fSMaxime Ripard 139ad537fb2SChen-Yu Tsai DRM_DEBUG_DRIVER("Muxing encoder %s to CRTC %s: %d\n", 140ad537fb2SChen-Yu Tsai encoder->name, encoder->crtc->name, ret); 141f8c73f4fSMaxime Ripard } 142f8c73f4fSMaxime Ripard 143961c645cSMaxime Ripard static int sun4i_tcon_get_clk_delay(const struct drm_display_mode *mode, 1449026e0d1SMaxime Ripard int channel) 1459026e0d1SMaxime Ripard { 1469026e0d1SMaxime Ripard int delay = mode->vtotal - mode->vdisplay; 1479026e0d1SMaxime Ripard 1489026e0d1SMaxime Ripard if (mode->flags & DRM_MODE_FLAG_INTERLACE) 1499026e0d1SMaxime Ripard delay /= 2; 1509026e0d1SMaxime Ripard 1519026e0d1SMaxime Ripard if (channel == 1) 1529026e0d1SMaxime Ripard delay -= 2; 1539026e0d1SMaxime Ripard 1549026e0d1SMaxime Ripard delay = min(delay, 30); 1559026e0d1SMaxime Ripard 1569026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("TCON %d clock delay %u\n", channel, delay); 1579026e0d1SMaxime Ripard 1589026e0d1SMaxime Ripard return delay; 1599026e0d1SMaxime Ripard } 1609026e0d1SMaxime Ripard 161ba19c537SMaxime Ripard static void sun4i_tcon0_mode_set_common(struct sun4i_tcon *tcon, 162ba19c537SMaxime Ripard const struct drm_display_mode *mode) 163ba19c537SMaxime Ripard { 164ba19c537SMaxime Ripard /* Configure the dot clock */ 165ba19c537SMaxime Ripard clk_set_rate(tcon->dclk, mode->crtc_clock * 1000); 166ba19c537SMaxime Ripard 167ba19c537SMaxime Ripard /* Set the resolution */ 168ba19c537SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC0_REG, 169ba19c537SMaxime Ripard SUN4I_TCON0_BASIC0_X(mode->crtc_hdisplay) | 170ba19c537SMaxime Ripard SUN4I_TCON0_BASIC0_Y(mode->crtc_vdisplay)); 171ba19c537SMaxime Ripard } 172ba19c537SMaxime Ripard 173ba19c537SMaxime Ripard static void sun4i_tcon0_mode_set_rgb(struct sun4i_tcon *tcon, 1745b8f0910SMaxime Ripard const struct drm_display_mode *mode) 1759026e0d1SMaxime Ripard { 1769026e0d1SMaxime Ripard unsigned int bp, hsync, vsync; 1779026e0d1SMaxime Ripard u8 clk_delay; 1789026e0d1SMaxime Ripard u32 val = 0; 1799026e0d1SMaxime Ripard 180ba19c537SMaxime Ripard sun4i_tcon0_mode_set_common(tcon, mode); 18186cf6788SChen-Yu Tsai 1829026e0d1SMaxime Ripard /* Adjust clock delay */ 1839026e0d1SMaxime Ripard clk_delay = sun4i_tcon_get_clk_delay(mode, 0); 1849026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 1859026e0d1SMaxime Ripard SUN4I_TCON0_CTL_CLK_DELAY_MASK, 1869026e0d1SMaxime Ripard SUN4I_TCON0_CTL_CLK_DELAY(clk_delay)); 1879026e0d1SMaxime Ripard 1889026e0d1SMaxime Ripard /* 1899026e0d1SMaxime Ripard * This is called a backporch in the register documentation, 19023a1cb11SChen-Yu Tsai * but it really is the back porch + hsync 1919026e0d1SMaxime Ripard */ 1929026e0d1SMaxime Ripard bp = mode->crtc_htotal - mode->crtc_hsync_start; 1939026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n", 1949026e0d1SMaxime Ripard mode->crtc_htotal, bp); 1959026e0d1SMaxime Ripard 1969026e0d1SMaxime Ripard /* Set horizontal display timings */ 1979026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG, 1989026e0d1SMaxime Ripard SUN4I_TCON0_BASIC1_H_TOTAL(mode->crtc_htotal) | 1999026e0d1SMaxime Ripard SUN4I_TCON0_BASIC1_H_BACKPORCH(bp)); 2009026e0d1SMaxime Ripard 2019026e0d1SMaxime Ripard /* 2029026e0d1SMaxime Ripard * This is called a backporch in the register documentation, 20323a1cb11SChen-Yu Tsai * but it really is the back porch + hsync 2049026e0d1SMaxime Ripard */ 2059026e0d1SMaxime Ripard bp = mode->crtc_vtotal - mode->crtc_vsync_start; 2069026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n", 2079026e0d1SMaxime Ripard mode->crtc_vtotal, bp); 2089026e0d1SMaxime Ripard 2099026e0d1SMaxime Ripard /* Set vertical display timings */ 2109026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG, 211a88cbbd4SMaxime Ripard SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) | 2129026e0d1SMaxime Ripard SUN4I_TCON0_BASIC2_V_BACKPORCH(bp)); 2139026e0d1SMaxime Ripard 2149026e0d1SMaxime Ripard /* Set Hsync and Vsync length */ 2159026e0d1SMaxime Ripard hsync = mode->crtc_hsync_end - mode->crtc_hsync_start; 2169026e0d1SMaxime Ripard vsync = mode->crtc_vsync_end - mode->crtc_vsync_start; 2179026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync); 2189026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC3_REG, 2199026e0d1SMaxime Ripard SUN4I_TCON0_BASIC3_V_SYNC(vsync) | 2209026e0d1SMaxime Ripard SUN4I_TCON0_BASIC3_H_SYNC(hsync)); 2219026e0d1SMaxime Ripard 2229026e0d1SMaxime Ripard /* Setup the polarity of the various signals */ 2239026e0d1SMaxime Ripard if (!(mode->flags & DRM_MODE_FLAG_PHSYNC)) 2249026e0d1SMaxime Ripard val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE; 2259026e0d1SMaxime Ripard 2269026e0d1SMaxime Ripard if (!(mode->flags & DRM_MODE_FLAG_PVSYNC)) 2279026e0d1SMaxime Ripard val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE; 2289026e0d1SMaxime Ripard 2299026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_IO_POL_REG, 2309026e0d1SMaxime Ripard SUN4I_TCON0_IO_POL_HSYNC_POSITIVE | SUN4I_TCON0_IO_POL_VSYNC_POSITIVE, 2319026e0d1SMaxime Ripard val); 2329026e0d1SMaxime Ripard 2339026e0d1SMaxime Ripard /* Map output pins to channel 0 */ 2349026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 2359026e0d1SMaxime Ripard SUN4I_TCON_GCTL_IOMAP_MASK, 2369026e0d1SMaxime Ripard SUN4I_TCON_GCTL_IOMAP_TCON0); 2379026e0d1SMaxime Ripard 2389026e0d1SMaxime Ripard /* Enable the output on the pins */ 2399026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0); 2409026e0d1SMaxime Ripard } 2419026e0d1SMaxime Ripard 2425b8f0910SMaxime Ripard static void sun4i_tcon1_mode_set(struct sun4i_tcon *tcon, 2435b8f0910SMaxime Ripard const struct drm_display_mode *mode) 2449026e0d1SMaxime Ripard { 245b8317a3dSMaxime Ripard unsigned int bp, hsync, vsync, vtotal; 2469026e0d1SMaxime Ripard u8 clk_delay; 2479026e0d1SMaxime Ripard u32 val; 2489026e0d1SMaxime Ripard 24991ea2f29SChen-Yu Tsai WARN_ON(!tcon->quirks->has_channel_1); 2508e924047SMaxime Ripard 25186cf6788SChen-Yu Tsai /* Configure the dot clock */ 25286cf6788SChen-Yu Tsai clk_set_rate(tcon->sclk1, mode->crtc_clock * 1000); 25386cf6788SChen-Yu Tsai 2549026e0d1SMaxime Ripard /* Adjust clock delay */ 2559026e0d1SMaxime Ripard clk_delay = sun4i_tcon_get_clk_delay(mode, 1); 2569026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 2579026e0d1SMaxime Ripard SUN4I_TCON1_CTL_CLK_DELAY_MASK, 2589026e0d1SMaxime Ripard SUN4I_TCON1_CTL_CLK_DELAY(clk_delay)); 2599026e0d1SMaxime Ripard 2609026e0d1SMaxime Ripard /* Set interlaced mode */ 2619026e0d1SMaxime Ripard if (mode->flags & DRM_MODE_FLAG_INTERLACE) 2629026e0d1SMaxime Ripard val = SUN4I_TCON1_CTL_INTERLACE_ENABLE; 2639026e0d1SMaxime Ripard else 2649026e0d1SMaxime Ripard val = 0; 2659026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 2669026e0d1SMaxime Ripard SUN4I_TCON1_CTL_INTERLACE_ENABLE, 2679026e0d1SMaxime Ripard val); 2689026e0d1SMaxime Ripard 2699026e0d1SMaxime Ripard /* Set the input resolution */ 2709026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC0_REG, 2719026e0d1SMaxime Ripard SUN4I_TCON1_BASIC0_X(mode->crtc_hdisplay) | 2729026e0d1SMaxime Ripard SUN4I_TCON1_BASIC0_Y(mode->crtc_vdisplay)); 2739026e0d1SMaxime Ripard 2749026e0d1SMaxime Ripard /* Set the upscaling resolution */ 2759026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC1_REG, 2769026e0d1SMaxime Ripard SUN4I_TCON1_BASIC1_X(mode->crtc_hdisplay) | 2779026e0d1SMaxime Ripard SUN4I_TCON1_BASIC1_Y(mode->crtc_vdisplay)); 2789026e0d1SMaxime Ripard 2799026e0d1SMaxime Ripard /* Set the output resolution */ 2809026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC2_REG, 2819026e0d1SMaxime Ripard SUN4I_TCON1_BASIC2_X(mode->crtc_hdisplay) | 2829026e0d1SMaxime Ripard SUN4I_TCON1_BASIC2_Y(mode->crtc_vdisplay)); 2839026e0d1SMaxime Ripard 2849026e0d1SMaxime Ripard /* Set horizontal display timings */ 2853cb2f46bSMaxime Ripard bp = mode->crtc_htotal - mode->crtc_hsync_start; 2869026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n", 2879026e0d1SMaxime Ripard mode->htotal, bp); 2889026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC3_REG, 2899026e0d1SMaxime Ripard SUN4I_TCON1_BASIC3_H_TOTAL(mode->crtc_htotal) | 2909026e0d1SMaxime Ripard SUN4I_TCON1_BASIC3_H_BACKPORCH(bp)); 2919026e0d1SMaxime Ripard 2923cb2f46bSMaxime Ripard bp = mode->crtc_vtotal - mode->crtc_vsync_start; 2939026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n", 294b8317a3dSMaxime Ripard mode->crtc_vtotal, bp); 295b8317a3dSMaxime Ripard 296b8317a3dSMaxime Ripard /* 297b8317a3dSMaxime Ripard * The vertical resolution needs to be doubled in all 298b8317a3dSMaxime Ripard * cases. We could use crtc_vtotal and always multiply by two, 299b8317a3dSMaxime Ripard * but that leads to a rounding error in interlace when vtotal 300b8317a3dSMaxime Ripard * is odd. 301b8317a3dSMaxime Ripard * 302b8317a3dSMaxime Ripard * This happens with TV's PAL for example, where vtotal will 303b8317a3dSMaxime Ripard * be 625, crtc_vtotal 312, and thus crtc_vtotal * 2 will be 304b8317a3dSMaxime Ripard * 624, which apparently confuses the hardware. 305b8317a3dSMaxime Ripard * 306b8317a3dSMaxime Ripard * To work around this, we will always use vtotal, and 307b8317a3dSMaxime Ripard * multiply by two only if we're not in interlace. 308b8317a3dSMaxime Ripard */ 309b8317a3dSMaxime Ripard vtotal = mode->vtotal; 310b8317a3dSMaxime Ripard if (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) 311b8317a3dSMaxime Ripard vtotal = vtotal * 2; 312b8317a3dSMaxime Ripard 313b8317a3dSMaxime Ripard /* Set vertical display timings */ 3149026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC4_REG, 315b8317a3dSMaxime Ripard SUN4I_TCON1_BASIC4_V_TOTAL(vtotal) | 3169026e0d1SMaxime Ripard SUN4I_TCON1_BASIC4_V_BACKPORCH(bp)); 3179026e0d1SMaxime Ripard 3189026e0d1SMaxime Ripard /* Set Hsync and Vsync length */ 3199026e0d1SMaxime Ripard hsync = mode->crtc_hsync_end - mode->crtc_hsync_start; 3209026e0d1SMaxime Ripard vsync = mode->crtc_vsync_end - mode->crtc_vsync_start; 3219026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync); 3229026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC5_REG, 3239026e0d1SMaxime Ripard SUN4I_TCON1_BASIC5_V_SYNC(vsync) | 3249026e0d1SMaxime Ripard SUN4I_TCON1_BASIC5_H_SYNC(hsync)); 3259026e0d1SMaxime Ripard 3269026e0d1SMaxime Ripard /* Map output pins to channel 1 */ 3279026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 3289026e0d1SMaxime Ripard SUN4I_TCON_GCTL_IOMAP_MASK, 3299026e0d1SMaxime Ripard SUN4I_TCON_GCTL_IOMAP_TCON1); 3309026e0d1SMaxime Ripard } 3315b8f0910SMaxime Ripard 3325b8f0910SMaxime Ripard void sun4i_tcon_mode_set(struct sun4i_tcon *tcon, 3335b8f0910SMaxime Ripard const struct drm_encoder *encoder, 3345b8f0910SMaxime Ripard const struct drm_display_mode *mode) 3355b8f0910SMaxime Ripard { 3365b8f0910SMaxime Ripard switch (encoder->encoder_type) { 3375b8f0910SMaxime Ripard case DRM_MODE_ENCODER_NONE: 338ba19c537SMaxime Ripard sun4i_tcon0_mode_set_rgb(tcon, mode); 3395b8f0910SMaxime Ripard sun4i_tcon_set_mux(tcon, 0, encoder); 3405b8f0910SMaxime Ripard break; 3415b8f0910SMaxime Ripard case DRM_MODE_ENCODER_TVDAC: 3425b8f0910SMaxime Ripard case DRM_MODE_ENCODER_TMDS: 3435b8f0910SMaxime Ripard sun4i_tcon1_mode_set(tcon, mode); 3445b8f0910SMaxime Ripard sun4i_tcon_set_mux(tcon, 1, encoder); 3455b8f0910SMaxime Ripard break; 3465b8f0910SMaxime Ripard default: 3475b8f0910SMaxime Ripard DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n"); 3485b8f0910SMaxime Ripard } 3495b8f0910SMaxime Ripard } 3505b8f0910SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_mode_set); 3519026e0d1SMaxime Ripard 3529026e0d1SMaxime Ripard static void sun4i_tcon_finish_page_flip(struct drm_device *dev, 3539026e0d1SMaxime Ripard struct sun4i_crtc *scrtc) 3549026e0d1SMaxime Ripard { 3559026e0d1SMaxime Ripard unsigned long flags; 3569026e0d1SMaxime Ripard 3579026e0d1SMaxime Ripard spin_lock_irqsave(&dev->event_lock, flags); 3589026e0d1SMaxime Ripard if (scrtc->event) { 3599026e0d1SMaxime Ripard drm_crtc_send_vblank_event(&scrtc->crtc, scrtc->event); 3609026e0d1SMaxime Ripard drm_crtc_vblank_put(&scrtc->crtc); 3619026e0d1SMaxime Ripard scrtc->event = NULL; 3629026e0d1SMaxime Ripard } 3639026e0d1SMaxime Ripard spin_unlock_irqrestore(&dev->event_lock, flags); 3649026e0d1SMaxime Ripard } 3659026e0d1SMaxime Ripard 3669026e0d1SMaxime Ripard static irqreturn_t sun4i_tcon_handler(int irq, void *private) 3679026e0d1SMaxime Ripard { 3689026e0d1SMaxime Ripard struct sun4i_tcon *tcon = private; 3699026e0d1SMaxime Ripard struct drm_device *drm = tcon->drm; 37046cce6daSChen-Yu Tsai struct sun4i_crtc *scrtc = tcon->crtc; 3719026e0d1SMaxime Ripard unsigned int status; 3729026e0d1SMaxime Ripard 3739026e0d1SMaxime Ripard regmap_read(tcon->regs, SUN4I_TCON_GINT0_REG, &status); 3749026e0d1SMaxime Ripard 3759026e0d1SMaxime Ripard if (!(status & (SUN4I_TCON_GINT0_VBLANK_INT(0) | 3769026e0d1SMaxime Ripard SUN4I_TCON_GINT0_VBLANK_INT(1)))) 3779026e0d1SMaxime Ripard return IRQ_NONE; 3789026e0d1SMaxime Ripard 3799026e0d1SMaxime Ripard drm_crtc_handle_vblank(&scrtc->crtc); 3809026e0d1SMaxime Ripard sun4i_tcon_finish_page_flip(drm, scrtc); 3819026e0d1SMaxime Ripard 3829026e0d1SMaxime Ripard /* Acknowledge the interrupt */ 3839026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, 3849026e0d1SMaxime Ripard SUN4I_TCON_GINT0_VBLANK_INT(0) | 3859026e0d1SMaxime Ripard SUN4I_TCON_GINT0_VBLANK_INT(1), 3869026e0d1SMaxime Ripard 0); 3879026e0d1SMaxime Ripard 3889026e0d1SMaxime Ripard return IRQ_HANDLED; 3899026e0d1SMaxime Ripard } 3909026e0d1SMaxime Ripard 3919026e0d1SMaxime Ripard static int sun4i_tcon_init_clocks(struct device *dev, 3929026e0d1SMaxime Ripard struct sun4i_tcon *tcon) 3939026e0d1SMaxime Ripard { 3949026e0d1SMaxime Ripard tcon->clk = devm_clk_get(dev, "ahb"); 3959026e0d1SMaxime Ripard if (IS_ERR(tcon->clk)) { 3969026e0d1SMaxime Ripard dev_err(dev, "Couldn't get the TCON bus clock\n"); 3979026e0d1SMaxime Ripard return PTR_ERR(tcon->clk); 3989026e0d1SMaxime Ripard } 3999026e0d1SMaxime Ripard clk_prepare_enable(tcon->clk); 4009026e0d1SMaxime Ripard 4019026e0d1SMaxime Ripard tcon->sclk0 = devm_clk_get(dev, "tcon-ch0"); 4029026e0d1SMaxime Ripard if (IS_ERR(tcon->sclk0)) { 4039026e0d1SMaxime Ripard dev_err(dev, "Couldn't get the TCON channel 0 clock\n"); 4049026e0d1SMaxime Ripard return PTR_ERR(tcon->sclk0); 4059026e0d1SMaxime Ripard } 4069026e0d1SMaxime Ripard 40791ea2f29SChen-Yu Tsai if (tcon->quirks->has_channel_1) { 4089026e0d1SMaxime Ripard tcon->sclk1 = devm_clk_get(dev, "tcon-ch1"); 4099026e0d1SMaxime Ripard if (IS_ERR(tcon->sclk1)) { 4109026e0d1SMaxime Ripard dev_err(dev, "Couldn't get the TCON channel 1 clock\n"); 4119026e0d1SMaxime Ripard return PTR_ERR(tcon->sclk1); 4129026e0d1SMaxime Ripard } 4138e924047SMaxime Ripard } 4149026e0d1SMaxime Ripard 4154c7f16d1SChen-Yu Tsai return 0; 4169026e0d1SMaxime Ripard } 4179026e0d1SMaxime Ripard 4189026e0d1SMaxime Ripard static void sun4i_tcon_free_clocks(struct sun4i_tcon *tcon) 4199026e0d1SMaxime Ripard { 4209026e0d1SMaxime Ripard clk_disable_unprepare(tcon->clk); 4219026e0d1SMaxime Ripard } 4229026e0d1SMaxime Ripard 4239026e0d1SMaxime Ripard static int sun4i_tcon_init_irq(struct device *dev, 4249026e0d1SMaxime Ripard struct sun4i_tcon *tcon) 4259026e0d1SMaxime Ripard { 4269026e0d1SMaxime Ripard struct platform_device *pdev = to_platform_device(dev); 4279026e0d1SMaxime Ripard int irq, ret; 4289026e0d1SMaxime Ripard 4299026e0d1SMaxime Ripard irq = platform_get_irq(pdev, 0); 4309026e0d1SMaxime Ripard if (irq < 0) { 4319026e0d1SMaxime Ripard dev_err(dev, "Couldn't retrieve the TCON interrupt\n"); 4329026e0d1SMaxime Ripard return irq; 4339026e0d1SMaxime Ripard } 4349026e0d1SMaxime Ripard 4359026e0d1SMaxime Ripard ret = devm_request_irq(dev, irq, sun4i_tcon_handler, 0, 4369026e0d1SMaxime Ripard dev_name(dev), tcon); 4379026e0d1SMaxime Ripard if (ret) { 4389026e0d1SMaxime Ripard dev_err(dev, "Couldn't request the IRQ\n"); 4399026e0d1SMaxime Ripard return ret; 4409026e0d1SMaxime Ripard } 4419026e0d1SMaxime Ripard 4429026e0d1SMaxime Ripard return 0; 4439026e0d1SMaxime Ripard } 4449026e0d1SMaxime Ripard 4459026e0d1SMaxime Ripard static struct regmap_config sun4i_tcon_regmap_config = { 4469026e0d1SMaxime Ripard .reg_bits = 32, 4479026e0d1SMaxime Ripard .val_bits = 32, 4489026e0d1SMaxime Ripard .reg_stride = 4, 4499026e0d1SMaxime Ripard .max_register = 0x800, 4509026e0d1SMaxime Ripard }; 4519026e0d1SMaxime Ripard 4529026e0d1SMaxime Ripard static int sun4i_tcon_init_regmap(struct device *dev, 4539026e0d1SMaxime Ripard struct sun4i_tcon *tcon) 4549026e0d1SMaxime Ripard { 4559026e0d1SMaxime Ripard struct platform_device *pdev = to_platform_device(dev); 4569026e0d1SMaxime Ripard struct resource *res; 4579026e0d1SMaxime Ripard void __iomem *regs; 4589026e0d1SMaxime Ripard 4599026e0d1SMaxime Ripard res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 4609026e0d1SMaxime Ripard regs = devm_ioremap_resource(dev, res); 461af346f55SWei Yongjun if (IS_ERR(regs)) 4629026e0d1SMaxime Ripard return PTR_ERR(regs); 4639026e0d1SMaxime Ripard 4649026e0d1SMaxime Ripard tcon->regs = devm_regmap_init_mmio(dev, regs, 4659026e0d1SMaxime Ripard &sun4i_tcon_regmap_config); 4669026e0d1SMaxime Ripard if (IS_ERR(tcon->regs)) { 4679026e0d1SMaxime Ripard dev_err(dev, "Couldn't create the TCON regmap\n"); 4689026e0d1SMaxime Ripard return PTR_ERR(tcon->regs); 4699026e0d1SMaxime Ripard } 4709026e0d1SMaxime Ripard 4719026e0d1SMaxime Ripard /* Make sure the TCON is disabled and all IRQs are off */ 4729026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_GCTL_REG, 0); 4739026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_GINT0_REG, 0); 4749026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_GINT1_REG, 0); 4759026e0d1SMaxime Ripard 4769026e0d1SMaxime Ripard /* Disable IO lines and set them to tristate */ 4779026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, ~0); 4789026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_IO_TRI_REG, ~0); 4799026e0d1SMaxime Ripard 4809026e0d1SMaxime Ripard return 0; 4819026e0d1SMaxime Ripard } 4829026e0d1SMaxime Ripard 483b317fa3bSChen-Yu Tsai /* 484b317fa3bSChen-Yu Tsai * On SoCs with the old display pipeline design (Display Engine 1.0), 485b317fa3bSChen-Yu Tsai * the TCON is always tied to just one backend. Hence we can traverse 486b317fa3bSChen-Yu Tsai * the of_graph upwards to find the backend our tcon is connected to, 487b317fa3bSChen-Yu Tsai * and take its ID as our own. 488b317fa3bSChen-Yu Tsai * 489b317fa3bSChen-Yu Tsai * We can either identify backends from their compatible strings, which 490b317fa3bSChen-Yu Tsai * means maintaining a large list of them. Or, since the backend is 491b317fa3bSChen-Yu Tsai * registered and binded before the TCON, we can just go through the 492b317fa3bSChen-Yu Tsai * list of registered backends and compare the device node. 49387969338SIcenowy Zheng * 49487969338SIcenowy Zheng * As the structures now store engines instead of backends, here this 49587969338SIcenowy Zheng * function in fact searches the corresponding engine, and the ID is 49687969338SIcenowy Zheng * requested via the get_id function of the engine. 497b317fa3bSChen-Yu Tsai */ 498e8d5bbf7SChen-Yu Tsai static struct sunxi_engine * 499e8d5bbf7SChen-Yu Tsai sun4i_tcon_find_engine_traverse(struct sun4i_drv *drv, 500b317fa3bSChen-Yu Tsai struct device_node *node) 501b317fa3bSChen-Yu Tsai { 502b317fa3bSChen-Yu Tsai struct device_node *port, *ep, *remote; 503be3fe0f9SChen-Yu Tsai struct sunxi_engine *engine = ERR_PTR(-EINVAL); 504b317fa3bSChen-Yu Tsai 505b317fa3bSChen-Yu Tsai port = of_graph_get_port_by_id(node, 0); 506b317fa3bSChen-Yu Tsai if (!port) 507b317fa3bSChen-Yu Tsai return ERR_PTR(-EINVAL); 508b317fa3bSChen-Yu Tsai 5091469619dSChen-Yu Tsai /* 5101469619dSChen-Yu Tsai * This only works if there is only one path from the TCON 5111469619dSChen-Yu Tsai * to any display engine. Otherwise the probe order of the 5121469619dSChen-Yu Tsai * TCONs and display engines is not guaranteed. They may 5131469619dSChen-Yu Tsai * either bind to the wrong one, or worse, bind to the same 5141469619dSChen-Yu Tsai * one if additional checks are not done. 5151469619dSChen-Yu Tsai * 5161469619dSChen-Yu Tsai * Bail out if there are multiple input connections. 5171469619dSChen-Yu Tsai */ 518be3fe0f9SChen-Yu Tsai if (of_get_available_child_count(port) != 1) 519be3fe0f9SChen-Yu Tsai goto out_put_port; 5201469619dSChen-Yu Tsai 521be3fe0f9SChen-Yu Tsai /* Get the first connection without specifying an ID */ 522be3fe0f9SChen-Yu Tsai ep = of_get_next_available_child(port, NULL); 523be3fe0f9SChen-Yu Tsai if (!ep) 524be3fe0f9SChen-Yu Tsai goto out_put_port; 525be3fe0f9SChen-Yu Tsai 526b317fa3bSChen-Yu Tsai remote = of_graph_get_remote_port_parent(ep); 527b317fa3bSChen-Yu Tsai if (!remote) 528be3fe0f9SChen-Yu Tsai goto out_put_ep; 529b317fa3bSChen-Yu Tsai 53087969338SIcenowy Zheng /* does this node match any registered engines? */ 531be3fe0f9SChen-Yu Tsai list_for_each_entry(engine, &drv->engine_list, list) 532be3fe0f9SChen-Yu Tsai if (remote == engine->node) 533be3fe0f9SChen-Yu Tsai goto out_put_remote; 534b317fa3bSChen-Yu Tsai 535b317fa3bSChen-Yu Tsai /* keep looking through upstream ports */ 536e8d5bbf7SChen-Yu Tsai engine = sun4i_tcon_find_engine_traverse(drv, remote); 537b317fa3bSChen-Yu Tsai 538be3fe0f9SChen-Yu Tsai out_put_remote: 539be3fe0f9SChen-Yu Tsai of_node_put(remote); 540be3fe0f9SChen-Yu Tsai out_put_ep: 541be3fe0f9SChen-Yu Tsai of_node_put(ep); 542be3fe0f9SChen-Yu Tsai out_put_port: 543be3fe0f9SChen-Yu Tsai of_node_put(port); 544be3fe0f9SChen-Yu Tsai 545be3fe0f9SChen-Yu Tsai return engine; 546b317fa3bSChen-Yu Tsai } 547b317fa3bSChen-Yu Tsai 548e8d5bbf7SChen-Yu Tsai /* 549e8d5bbf7SChen-Yu Tsai * The device tree binding says that the remote endpoint ID of any 550e8d5bbf7SChen-Yu Tsai * connection between components, up to and including the TCON, of 551e8d5bbf7SChen-Yu Tsai * the display pipeline should be equal to the actual ID of the local 552e8d5bbf7SChen-Yu Tsai * component. Thus we can look at any one of the input connections of 553e8d5bbf7SChen-Yu Tsai * the TCONs, and use that connection's remote endpoint ID as our own. 554e8d5bbf7SChen-Yu Tsai * 555e8d5bbf7SChen-Yu Tsai * Since the user of this function already finds the input port, 556e8d5bbf7SChen-Yu Tsai * the port is passed in directly without further checks. 557e8d5bbf7SChen-Yu Tsai */ 558e8d5bbf7SChen-Yu Tsai static int sun4i_tcon_of_get_id_from_port(struct device_node *port) 559e8d5bbf7SChen-Yu Tsai { 560e8d5bbf7SChen-Yu Tsai struct device_node *ep; 561e8d5bbf7SChen-Yu Tsai int ret = -EINVAL; 562e8d5bbf7SChen-Yu Tsai 563e8d5bbf7SChen-Yu Tsai /* try finding an upstream endpoint */ 564e8d5bbf7SChen-Yu Tsai for_each_available_child_of_node(port, ep) { 565e8d5bbf7SChen-Yu Tsai struct device_node *remote; 566e8d5bbf7SChen-Yu Tsai u32 reg; 567e8d5bbf7SChen-Yu Tsai 568e8d5bbf7SChen-Yu Tsai remote = of_graph_get_remote_endpoint(ep); 569e8d5bbf7SChen-Yu Tsai if (!remote) 570e8d5bbf7SChen-Yu Tsai continue; 571e8d5bbf7SChen-Yu Tsai 572e8d5bbf7SChen-Yu Tsai ret = of_property_read_u32(remote, "reg", ®); 573e8d5bbf7SChen-Yu Tsai if (ret) 574e8d5bbf7SChen-Yu Tsai continue; 575e8d5bbf7SChen-Yu Tsai 576e8d5bbf7SChen-Yu Tsai ret = reg; 577e8d5bbf7SChen-Yu Tsai } 578e8d5bbf7SChen-Yu Tsai 579e8d5bbf7SChen-Yu Tsai return ret; 580e8d5bbf7SChen-Yu Tsai } 581e8d5bbf7SChen-Yu Tsai 582e8d5bbf7SChen-Yu Tsai /* 583e8d5bbf7SChen-Yu Tsai * Once we know the TCON's id, we can look through the list of 584e8d5bbf7SChen-Yu Tsai * engines to find a matching one. We assume all engines have 585e8d5bbf7SChen-Yu Tsai * been probed and added to the list. 586e8d5bbf7SChen-Yu Tsai */ 587e8d5bbf7SChen-Yu Tsai static struct sunxi_engine *sun4i_tcon_get_engine_by_id(struct sun4i_drv *drv, 588e8d5bbf7SChen-Yu Tsai int id) 589e8d5bbf7SChen-Yu Tsai { 590e8d5bbf7SChen-Yu Tsai struct sunxi_engine *engine; 591e8d5bbf7SChen-Yu Tsai 592e8d5bbf7SChen-Yu Tsai list_for_each_entry(engine, &drv->engine_list, list) 593e8d5bbf7SChen-Yu Tsai if (engine->id == id) 594e8d5bbf7SChen-Yu Tsai return engine; 595e8d5bbf7SChen-Yu Tsai 596e8d5bbf7SChen-Yu Tsai return ERR_PTR(-EINVAL); 597e8d5bbf7SChen-Yu Tsai } 598e8d5bbf7SChen-Yu Tsai 599e8d5bbf7SChen-Yu Tsai /* 600e8d5bbf7SChen-Yu Tsai * On SoCs with the old display pipeline design (Display Engine 1.0), 601e8d5bbf7SChen-Yu Tsai * we assumed the TCON was always tied to just one backend. However 602e8d5bbf7SChen-Yu Tsai * this proved not to be the case. On the A31, the TCON can select 603e8d5bbf7SChen-Yu Tsai * either backend as its source. On the A20 (and likely on the A10), 604e8d5bbf7SChen-Yu Tsai * the backend can choose which TCON to output to. 605e8d5bbf7SChen-Yu Tsai * 606e8d5bbf7SChen-Yu Tsai * The device tree binding says that the remote endpoint ID of any 607e8d5bbf7SChen-Yu Tsai * connection between components, up to and including the TCON, of 608e8d5bbf7SChen-Yu Tsai * the display pipeline should be equal to the actual ID of the local 609e8d5bbf7SChen-Yu Tsai * component. Thus we should be able to look at any one of the input 610e8d5bbf7SChen-Yu Tsai * connections of the TCONs, and use that connection's remote endpoint 611e8d5bbf7SChen-Yu Tsai * ID as our own. 612e8d5bbf7SChen-Yu Tsai * 613e8d5bbf7SChen-Yu Tsai * However the connections between the backend and TCON were assumed 614e8d5bbf7SChen-Yu Tsai * to be always singular, and their endpoit IDs were all incorrectly 615e8d5bbf7SChen-Yu Tsai * set to 0. This means for these old device trees, we cannot just look 616e8d5bbf7SChen-Yu Tsai * up the remote endpoint ID of a TCON input endpoint. TCON1 would be 617e8d5bbf7SChen-Yu Tsai * incorrectly identified as TCON0. 618e8d5bbf7SChen-Yu Tsai * 619e8d5bbf7SChen-Yu Tsai * This function first checks if the TCON node has 2 input endpoints. 620e8d5bbf7SChen-Yu Tsai * If so, then the device tree is a corrected version, and it will use 621e8d5bbf7SChen-Yu Tsai * sun4i_tcon_of_get_id() and sun4i_tcon_get_engine_by_id() from above 622e8d5bbf7SChen-Yu Tsai * to fetch the ID and engine directly. If not, then it is likely an 623e8d5bbf7SChen-Yu Tsai * old device trees, where the endpoint IDs were incorrect, but did not 624e8d5bbf7SChen-Yu Tsai * have endpoint connections between the backend and TCON across 625e8d5bbf7SChen-Yu Tsai * different display pipelines. It will fall back to the old method of 626e8d5bbf7SChen-Yu Tsai * traversing the of_graph to try and find a matching engine by device 627e8d5bbf7SChen-Yu Tsai * node. 628e8d5bbf7SChen-Yu Tsai * 629e8d5bbf7SChen-Yu Tsai * In the case of single display pipeline device trees, either method 630e8d5bbf7SChen-Yu Tsai * works. 631e8d5bbf7SChen-Yu Tsai */ 632e8d5bbf7SChen-Yu Tsai static struct sunxi_engine *sun4i_tcon_find_engine(struct sun4i_drv *drv, 633e8d5bbf7SChen-Yu Tsai struct device_node *node) 634e8d5bbf7SChen-Yu Tsai { 635e8d5bbf7SChen-Yu Tsai struct device_node *port; 636e8d5bbf7SChen-Yu Tsai struct sunxi_engine *engine; 637e8d5bbf7SChen-Yu Tsai 638e8d5bbf7SChen-Yu Tsai port = of_graph_get_port_by_id(node, 0); 639e8d5bbf7SChen-Yu Tsai if (!port) 640e8d5bbf7SChen-Yu Tsai return ERR_PTR(-EINVAL); 641e8d5bbf7SChen-Yu Tsai 642e8d5bbf7SChen-Yu Tsai /* 643e8d5bbf7SChen-Yu Tsai * Is this a corrected device tree with cross pipeline 644e8d5bbf7SChen-Yu Tsai * connections between the backend and TCON? 645e8d5bbf7SChen-Yu Tsai */ 646e8d5bbf7SChen-Yu Tsai if (of_get_child_count(port) > 1) { 647e8d5bbf7SChen-Yu Tsai /* Get our ID directly from an upstream endpoint */ 648e8d5bbf7SChen-Yu Tsai int id = sun4i_tcon_of_get_id_from_port(port); 649e8d5bbf7SChen-Yu Tsai 650e8d5bbf7SChen-Yu Tsai /* Get our engine by matching our ID */ 651e8d5bbf7SChen-Yu Tsai engine = sun4i_tcon_get_engine_by_id(drv, id); 652e8d5bbf7SChen-Yu Tsai 653e8d5bbf7SChen-Yu Tsai of_node_put(port); 654e8d5bbf7SChen-Yu Tsai return engine; 655e8d5bbf7SChen-Yu Tsai } 656e8d5bbf7SChen-Yu Tsai 657e8d5bbf7SChen-Yu Tsai /* Fallback to old method by traversing input endpoints */ 658e8d5bbf7SChen-Yu Tsai of_node_put(port); 659e8d5bbf7SChen-Yu Tsai return sun4i_tcon_find_engine_traverse(drv, node); 660e8d5bbf7SChen-Yu Tsai } 661e8d5bbf7SChen-Yu Tsai 6629026e0d1SMaxime Ripard static int sun4i_tcon_bind(struct device *dev, struct device *master, 6639026e0d1SMaxime Ripard void *data) 6649026e0d1SMaxime Ripard { 6659026e0d1SMaxime Ripard struct drm_device *drm = data; 6669026e0d1SMaxime Ripard struct sun4i_drv *drv = drm->dev_private; 66787969338SIcenowy Zheng struct sunxi_engine *engine; 6689026e0d1SMaxime Ripard struct sun4i_tcon *tcon; 6699026e0d1SMaxime Ripard int ret; 6709026e0d1SMaxime Ripard 67187969338SIcenowy Zheng engine = sun4i_tcon_find_engine(drv, dev->of_node); 67287969338SIcenowy Zheng if (IS_ERR(engine)) { 67387969338SIcenowy Zheng dev_err(dev, "Couldn't find matching engine\n"); 67480a58240SChen-Yu Tsai return -EPROBE_DEFER; 675b317fa3bSChen-Yu Tsai } 67680a58240SChen-Yu Tsai 6779026e0d1SMaxime Ripard tcon = devm_kzalloc(dev, sizeof(*tcon), GFP_KERNEL); 6789026e0d1SMaxime Ripard if (!tcon) 6799026e0d1SMaxime Ripard return -ENOMEM; 6809026e0d1SMaxime Ripard dev_set_drvdata(dev, tcon); 6819026e0d1SMaxime Ripard tcon->drm = drm; 682ae558110SMaxime Ripard tcon->dev = dev; 68387969338SIcenowy Zheng tcon->id = engine->id; 68491ea2f29SChen-Yu Tsai tcon->quirks = of_device_get_match_data(dev); 6859026e0d1SMaxime Ripard 6869026e0d1SMaxime Ripard tcon->lcd_rst = devm_reset_control_get(dev, "lcd"); 6879026e0d1SMaxime Ripard if (IS_ERR(tcon->lcd_rst)) { 6889026e0d1SMaxime Ripard dev_err(dev, "Couldn't get our reset line\n"); 6899026e0d1SMaxime Ripard return PTR_ERR(tcon->lcd_rst); 6909026e0d1SMaxime Ripard } 6919026e0d1SMaxime Ripard 6929026e0d1SMaxime Ripard /* Make sure our TCON is reset */ 693d57294c1SChen-Yu Tsai ret = reset_control_reset(tcon->lcd_rst); 6949026e0d1SMaxime Ripard if (ret) { 6959026e0d1SMaxime Ripard dev_err(dev, "Couldn't deassert our reset line\n"); 6969026e0d1SMaxime Ripard return ret; 6979026e0d1SMaxime Ripard } 6989026e0d1SMaxime Ripard 6999026e0d1SMaxime Ripard ret = sun4i_tcon_init_clocks(dev, tcon); 7009026e0d1SMaxime Ripard if (ret) { 7019026e0d1SMaxime Ripard dev_err(dev, "Couldn't init our TCON clocks\n"); 7029026e0d1SMaxime Ripard goto err_assert_reset; 7039026e0d1SMaxime Ripard } 7049026e0d1SMaxime Ripard 7054c7f16d1SChen-Yu Tsai ret = sun4i_tcon_init_regmap(dev, tcon); 7069026e0d1SMaxime Ripard if (ret) { 7074c7f16d1SChen-Yu Tsai dev_err(dev, "Couldn't init our TCON regmap\n"); 7089026e0d1SMaxime Ripard goto err_free_clocks; 7099026e0d1SMaxime Ripard } 7109026e0d1SMaxime Ripard 7114c7f16d1SChen-Yu Tsai ret = sun4i_dclk_create(dev, tcon); 7124c7f16d1SChen-Yu Tsai if (ret) { 7134c7f16d1SChen-Yu Tsai dev_err(dev, "Couldn't create our TCON dot clock\n"); 7144c7f16d1SChen-Yu Tsai goto err_free_clocks; 7154c7f16d1SChen-Yu Tsai } 7164c7f16d1SChen-Yu Tsai 7179026e0d1SMaxime Ripard ret = sun4i_tcon_init_irq(dev, tcon); 7189026e0d1SMaxime Ripard if (ret) { 7199026e0d1SMaxime Ripard dev_err(dev, "Couldn't init our TCON interrupts\n"); 7204c7f16d1SChen-Yu Tsai goto err_free_dotclock; 7219026e0d1SMaxime Ripard } 7229026e0d1SMaxime Ripard 72387969338SIcenowy Zheng tcon->crtc = sun4i_crtc_init(drm, engine, tcon); 72446cce6daSChen-Yu Tsai if (IS_ERR(tcon->crtc)) { 72546cce6daSChen-Yu Tsai dev_err(dev, "Couldn't create our CRTC\n"); 72646cce6daSChen-Yu Tsai ret = PTR_ERR(tcon->crtc); 72746cce6daSChen-Yu Tsai goto err_free_clocks; 72846cce6daSChen-Yu Tsai } 72946cce6daSChen-Yu Tsai 730b9c8506cSChen-Yu Tsai ret = sun4i_rgb_init(drm, tcon); 73113fef095SChen-Yu Tsai if (ret < 0) 73213fef095SChen-Yu Tsai goto err_free_clocks; 73313fef095SChen-Yu Tsai 73427e18de7SChen-Yu Tsai if (tcon->quirks->needs_de_be_mux) { 73527e18de7SChen-Yu Tsai /* 73627e18de7SChen-Yu Tsai * We assume there is no dynamic muxing of backends 73727e18de7SChen-Yu Tsai * and TCONs, so we select the backend with same ID. 73827e18de7SChen-Yu Tsai * 73927e18de7SChen-Yu Tsai * While dynamic selection might be interesting, since 74027e18de7SChen-Yu Tsai * the CRTC is tied to the TCON, while the layers are 74127e18de7SChen-Yu Tsai * tied to the backends, this means, we will need to 74227e18de7SChen-Yu Tsai * switch between groups of layers. There might not be 74327e18de7SChen-Yu Tsai * a way to represent this constraint in DRM. 74427e18de7SChen-Yu Tsai */ 74527e18de7SChen-Yu Tsai regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 74627e18de7SChen-Yu Tsai SUN4I_TCON0_CTL_SRC_SEL_MASK, 74727e18de7SChen-Yu Tsai tcon->id); 74827e18de7SChen-Yu Tsai regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 74927e18de7SChen-Yu Tsai SUN4I_TCON1_CTL_SRC_SEL_MASK, 75027e18de7SChen-Yu Tsai tcon->id); 75127e18de7SChen-Yu Tsai } 75227e18de7SChen-Yu Tsai 75380a58240SChen-Yu Tsai list_add_tail(&tcon->list, &drv->tcon_list); 75480a58240SChen-Yu Tsai 75513fef095SChen-Yu Tsai return 0; 7569026e0d1SMaxime Ripard 7574c7f16d1SChen-Yu Tsai err_free_dotclock: 7584c7f16d1SChen-Yu Tsai sun4i_dclk_free(tcon); 7599026e0d1SMaxime Ripard err_free_clocks: 7609026e0d1SMaxime Ripard sun4i_tcon_free_clocks(tcon); 7619026e0d1SMaxime Ripard err_assert_reset: 7629026e0d1SMaxime Ripard reset_control_assert(tcon->lcd_rst); 7639026e0d1SMaxime Ripard return ret; 7649026e0d1SMaxime Ripard } 7659026e0d1SMaxime Ripard 7669026e0d1SMaxime Ripard static void sun4i_tcon_unbind(struct device *dev, struct device *master, 7679026e0d1SMaxime Ripard void *data) 7689026e0d1SMaxime Ripard { 7699026e0d1SMaxime Ripard struct sun4i_tcon *tcon = dev_get_drvdata(dev); 7709026e0d1SMaxime Ripard 77180a58240SChen-Yu Tsai list_del(&tcon->list); 7724c7f16d1SChen-Yu Tsai sun4i_dclk_free(tcon); 7739026e0d1SMaxime Ripard sun4i_tcon_free_clocks(tcon); 7749026e0d1SMaxime Ripard } 7759026e0d1SMaxime Ripard 776dfeb693dSJulia Lawall static const struct component_ops sun4i_tcon_ops = { 7779026e0d1SMaxime Ripard .bind = sun4i_tcon_bind, 7789026e0d1SMaxime Ripard .unbind = sun4i_tcon_unbind, 7799026e0d1SMaxime Ripard }; 7809026e0d1SMaxime Ripard 7819026e0d1SMaxime Ripard static int sun4i_tcon_probe(struct platform_device *pdev) 7829026e0d1SMaxime Ripard { 78329e57fabSMaxime Ripard struct device_node *node = pdev->dev.of_node; 784894f5a9fSMaxime Ripard struct drm_bridge *bridge; 78529e57fabSMaxime Ripard struct drm_panel *panel; 786ebc94461SRob Herring int ret; 78729e57fabSMaxime Ripard 788ebc94461SRob Herring ret = drm_of_find_panel_or_bridge(node, 1, 0, &panel, &bridge); 789ebc94461SRob Herring if (ret == -EPROBE_DEFER) 790ebc94461SRob Herring return ret; 79129e57fabSMaxime Ripard 7929026e0d1SMaxime Ripard return component_add(&pdev->dev, &sun4i_tcon_ops); 7939026e0d1SMaxime Ripard } 7949026e0d1SMaxime Ripard 7959026e0d1SMaxime Ripard static int sun4i_tcon_remove(struct platform_device *pdev) 7969026e0d1SMaxime Ripard { 7979026e0d1SMaxime Ripard component_del(&pdev->dev, &sun4i_tcon_ops); 7989026e0d1SMaxime Ripard 7999026e0d1SMaxime Ripard return 0; 8009026e0d1SMaxime Ripard } 8019026e0d1SMaxime Ripard 802ad537fb2SChen-Yu Tsai /* platform specific TCON muxing callbacks */ 8034bb206bfSJonathan Liu static int sun4i_a10_tcon_set_mux(struct sun4i_tcon *tcon, 8044bb206bfSJonathan Liu const struct drm_encoder *encoder) 8054bb206bfSJonathan Liu { 8064bb206bfSJonathan Liu struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev); 8074bb206bfSJonathan Liu u32 shift; 8084bb206bfSJonathan Liu 8094bb206bfSJonathan Liu if (!tcon0) 8104bb206bfSJonathan Liu return -EINVAL; 8114bb206bfSJonathan Liu 8124bb206bfSJonathan Liu switch (encoder->encoder_type) { 8134bb206bfSJonathan Liu case DRM_MODE_ENCODER_TMDS: 8144bb206bfSJonathan Liu /* HDMI */ 8154bb206bfSJonathan Liu shift = 8; 8164bb206bfSJonathan Liu break; 8174bb206bfSJonathan Liu default: 8184bb206bfSJonathan Liu return -EINVAL; 8194bb206bfSJonathan Liu } 8204bb206bfSJonathan Liu 8214bb206bfSJonathan Liu regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG, 8224bb206bfSJonathan Liu 0x3 << shift, tcon->id << shift); 8234bb206bfSJonathan Liu 8244bb206bfSJonathan Liu return 0; 8254bb206bfSJonathan Liu } 8264bb206bfSJonathan Liu 827ad537fb2SChen-Yu Tsai static int sun5i_a13_tcon_set_mux(struct sun4i_tcon *tcon, 828abcb8766SMaxime Ripard const struct drm_encoder *encoder) 829ad537fb2SChen-Yu Tsai { 830ad537fb2SChen-Yu Tsai u32 val; 831ad537fb2SChen-Yu Tsai 832ad537fb2SChen-Yu Tsai if (encoder->encoder_type == DRM_MODE_ENCODER_TVDAC) 833ad537fb2SChen-Yu Tsai val = 1; 834ad537fb2SChen-Yu Tsai else 835ad537fb2SChen-Yu Tsai val = 0; 836ad537fb2SChen-Yu Tsai 837ad537fb2SChen-Yu Tsai /* 838ad537fb2SChen-Yu Tsai * FIXME: Undocumented bits 839ad537fb2SChen-Yu Tsai */ 840ad537fb2SChen-Yu Tsai return regmap_write(tcon->regs, SUN4I_TCON_MUX_CTRL_REG, val); 841ad537fb2SChen-Yu Tsai } 842ad537fb2SChen-Yu Tsai 84367e32645SChen-Yu Tsai static int sun6i_tcon_set_mux(struct sun4i_tcon *tcon, 844abcb8766SMaxime Ripard const struct drm_encoder *encoder) 84567e32645SChen-Yu Tsai { 84667e32645SChen-Yu Tsai struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev); 84767e32645SChen-Yu Tsai u32 shift; 84867e32645SChen-Yu Tsai 84967e32645SChen-Yu Tsai if (!tcon0) 85067e32645SChen-Yu Tsai return -EINVAL; 85167e32645SChen-Yu Tsai 85267e32645SChen-Yu Tsai switch (encoder->encoder_type) { 85367e32645SChen-Yu Tsai case DRM_MODE_ENCODER_TMDS: 85467e32645SChen-Yu Tsai /* HDMI */ 85567e32645SChen-Yu Tsai shift = 8; 85667e32645SChen-Yu Tsai break; 85767e32645SChen-Yu Tsai default: 85867e32645SChen-Yu Tsai /* TODO A31 has MIPI DSI but A31s does not */ 85967e32645SChen-Yu Tsai return -EINVAL; 86067e32645SChen-Yu Tsai } 86167e32645SChen-Yu Tsai 86267e32645SChen-Yu Tsai regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG, 86367e32645SChen-Yu Tsai 0x3 << shift, tcon->id << shift); 86467e32645SChen-Yu Tsai 86567e32645SChen-Yu Tsai return 0; 86667e32645SChen-Yu Tsai } 86767e32645SChen-Yu Tsai 8684bb206bfSJonathan Liu static const struct sun4i_tcon_quirks sun4i_a10_quirks = { 8694bb206bfSJonathan Liu .has_channel_1 = true, 8704bb206bfSJonathan Liu .set_mux = sun4i_a10_tcon_set_mux, 8714bb206bfSJonathan Liu }; 8724bb206bfSJonathan Liu 87391ea2f29SChen-Yu Tsai static const struct sun4i_tcon_quirks sun5i_a13_quirks = { 87491ea2f29SChen-Yu Tsai .has_channel_1 = true, 875ad537fb2SChen-Yu Tsai .set_mux = sun5i_a13_tcon_set_mux, 87691ea2f29SChen-Yu Tsai }; 87791ea2f29SChen-Yu Tsai 87893a5ec14SChen-Yu Tsai static const struct sun4i_tcon_quirks sun6i_a31_quirks = { 87993a5ec14SChen-Yu Tsai .has_channel_1 = true, 88027e18de7SChen-Yu Tsai .needs_de_be_mux = true, 88167e32645SChen-Yu Tsai .set_mux = sun6i_tcon_set_mux, 88293a5ec14SChen-Yu Tsai }; 88393a5ec14SChen-Yu Tsai 88493a5ec14SChen-Yu Tsai static const struct sun4i_tcon_quirks sun6i_a31s_quirks = { 88593a5ec14SChen-Yu Tsai .has_channel_1 = true, 88627e18de7SChen-Yu Tsai .needs_de_be_mux = true, 88793a5ec14SChen-Yu Tsai }; 88893a5ec14SChen-Yu Tsai 889aaddb6d2SJonathan Liu static const struct sun4i_tcon_quirks sun7i_a20_quirks = { 890aaddb6d2SJonathan Liu .has_channel_1 = true, 891aaddb6d2SJonathan Liu /* Same display pipeline structure as A10 */ 892aaddb6d2SJonathan Liu .set_mux = sun4i_a10_tcon_set_mux, 893aaddb6d2SJonathan Liu }; 894aaddb6d2SJonathan Liu 89591ea2f29SChen-Yu Tsai static const struct sun4i_tcon_quirks sun8i_a33_quirks = { 89691ea2f29SChen-Yu Tsai /* nothing is supported */ 89791ea2f29SChen-Yu Tsai }; 89891ea2f29SChen-Yu Tsai 8991a0edb3fSIcenowy Zheng static const struct sun4i_tcon_quirks sun8i_v3s_quirks = { 9001a0edb3fSIcenowy Zheng /* nothing is supported */ 9011a0edb3fSIcenowy Zheng }; 9021a0edb3fSIcenowy Zheng 903*ff71c2cfSChen-Yu Tsai /* sun4i_drv uses this list to check if a device node is a TCON */ 904*ff71c2cfSChen-Yu Tsai const struct of_device_id sun4i_tcon_of_table[] = { 9054bb206bfSJonathan Liu { .compatible = "allwinner,sun4i-a10-tcon", .data = &sun4i_a10_quirks }, 90691ea2f29SChen-Yu Tsai { .compatible = "allwinner,sun5i-a13-tcon", .data = &sun5i_a13_quirks }, 90793a5ec14SChen-Yu Tsai { .compatible = "allwinner,sun6i-a31-tcon", .data = &sun6i_a31_quirks }, 90893a5ec14SChen-Yu Tsai { .compatible = "allwinner,sun6i-a31s-tcon", .data = &sun6i_a31s_quirks }, 909aaddb6d2SJonathan Liu { .compatible = "allwinner,sun7i-a20-tcon", .data = &sun7i_a20_quirks }, 91091ea2f29SChen-Yu Tsai { .compatible = "allwinner,sun8i-a33-tcon", .data = &sun8i_a33_quirks }, 9111a0edb3fSIcenowy Zheng { .compatible = "allwinner,sun8i-v3s-tcon", .data = &sun8i_v3s_quirks }, 9129026e0d1SMaxime Ripard { } 9139026e0d1SMaxime Ripard }; 9149026e0d1SMaxime Ripard MODULE_DEVICE_TABLE(of, sun4i_tcon_of_table); 915*ff71c2cfSChen-Yu Tsai EXPORT_SYMBOL(sun4i_tcon_of_table); 9169026e0d1SMaxime Ripard 9179026e0d1SMaxime Ripard static struct platform_driver sun4i_tcon_platform_driver = { 9189026e0d1SMaxime Ripard .probe = sun4i_tcon_probe, 9199026e0d1SMaxime Ripard .remove = sun4i_tcon_remove, 9209026e0d1SMaxime Ripard .driver = { 9219026e0d1SMaxime Ripard .name = "sun4i-tcon", 9229026e0d1SMaxime Ripard .of_match_table = sun4i_tcon_of_table, 9239026e0d1SMaxime Ripard }, 9249026e0d1SMaxime Ripard }; 9259026e0d1SMaxime Ripard module_platform_driver(sun4i_tcon_platform_driver); 9269026e0d1SMaxime Ripard 9279026e0d1SMaxime Ripard MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>"); 9289026e0d1SMaxime Ripard MODULE_DESCRIPTION("Allwinner A10 Timing Controller Driver"); 9299026e0d1SMaxime Ripard MODULE_LICENSE("GPL"); 930