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> 179026e0d1SMaxime Ripard #include <drm/drm_modes.h> 18ebc94461SRob Herring #include <drm/drm_of.h> 199026e0d1SMaxime Ripard 209026e0d1SMaxime Ripard #include <linux/component.h> 219026e0d1SMaxime Ripard #include <linux/ioport.h> 229026e0d1SMaxime Ripard #include <linux/of_address.h> 2391ea2f29SChen-Yu Tsai #include <linux/of_device.h> 249026e0d1SMaxime Ripard #include <linux/of_irq.h> 259026e0d1SMaxime Ripard #include <linux/regmap.h> 269026e0d1SMaxime Ripard #include <linux/reset.h> 279026e0d1SMaxime Ripard 28*80a58240SChen-Yu Tsai #include "sun4i_backend.h" 299026e0d1SMaxime Ripard #include "sun4i_crtc.h" 309026e0d1SMaxime Ripard #include "sun4i_dotclock.h" 319026e0d1SMaxime Ripard #include "sun4i_drv.h" 3229e57fabSMaxime Ripard #include "sun4i_rgb.h" 339026e0d1SMaxime Ripard #include "sun4i_tcon.h" 349026e0d1SMaxime Ripard 359026e0d1SMaxime Ripard void sun4i_tcon_disable(struct sun4i_tcon *tcon) 369026e0d1SMaxime Ripard { 379026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Disabling TCON\n"); 389026e0d1SMaxime Ripard 399026e0d1SMaxime Ripard /* Disable the TCON */ 409026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 419026e0d1SMaxime Ripard SUN4I_TCON_GCTL_TCON_ENABLE, 0); 429026e0d1SMaxime Ripard } 439026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_disable); 449026e0d1SMaxime Ripard 459026e0d1SMaxime Ripard void sun4i_tcon_enable(struct sun4i_tcon *tcon) 469026e0d1SMaxime Ripard { 479026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Enabling TCON\n"); 489026e0d1SMaxime Ripard 499026e0d1SMaxime Ripard /* Enable the TCON */ 509026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 519026e0d1SMaxime Ripard SUN4I_TCON_GCTL_TCON_ENABLE, 529026e0d1SMaxime Ripard SUN4I_TCON_GCTL_TCON_ENABLE); 539026e0d1SMaxime Ripard } 549026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_enable); 559026e0d1SMaxime Ripard 569026e0d1SMaxime Ripard void sun4i_tcon_channel_disable(struct sun4i_tcon *tcon, int channel) 579026e0d1SMaxime Ripard { 589026e0d1SMaxime Ripard /* Disable the TCON's channel */ 599026e0d1SMaxime Ripard if (channel == 0) { 609026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 619026e0d1SMaxime Ripard SUN4I_TCON0_CTL_TCON_ENABLE, 0); 629026e0d1SMaxime Ripard clk_disable_unprepare(tcon->dclk); 638e924047SMaxime Ripard return; 648e924047SMaxime Ripard } 658e924047SMaxime Ripard 6691ea2f29SChen-Yu Tsai WARN_ON(!tcon->quirks->has_channel_1); 679026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 689026e0d1SMaxime Ripard SUN4I_TCON1_CTL_TCON_ENABLE, 0); 699026e0d1SMaxime Ripard clk_disable_unprepare(tcon->sclk1); 709026e0d1SMaxime Ripard } 719026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_channel_disable); 729026e0d1SMaxime Ripard 739026e0d1SMaxime Ripard void sun4i_tcon_channel_enable(struct sun4i_tcon *tcon, int channel) 749026e0d1SMaxime Ripard { 759026e0d1SMaxime Ripard /* Enable the TCON's channel */ 769026e0d1SMaxime Ripard if (channel == 0) { 779026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 789026e0d1SMaxime Ripard SUN4I_TCON0_CTL_TCON_ENABLE, 799026e0d1SMaxime Ripard SUN4I_TCON0_CTL_TCON_ENABLE); 809026e0d1SMaxime Ripard clk_prepare_enable(tcon->dclk); 818e924047SMaxime Ripard return; 828e924047SMaxime Ripard } 838e924047SMaxime Ripard 8491ea2f29SChen-Yu Tsai WARN_ON(!tcon->quirks->has_channel_1); 859026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 869026e0d1SMaxime Ripard SUN4I_TCON1_CTL_TCON_ENABLE, 879026e0d1SMaxime Ripard SUN4I_TCON1_CTL_TCON_ENABLE); 889026e0d1SMaxime Ripard clk_prepare_enable(tcon->sclk1); 899026e0d1SMaxime Ripard } 909026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_channel_enable); 919026e0d1SMaxime Ripard 929026e0d1SMaxime Ripard void sun4i_tcon_enable_vblank(struct sun4i_tcon *tcon, bool enable) 939026e0d1SMaxime Ripard { 949026e0d1SMaxime Ripard u32 mask, val = 0; 959026e0d1SMaxime Ripard 969026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("%sabling VBLANK interrupt\n", enable ? "En" : "Dis"); 979026e0d1SMaxime Ripard 989026e0d1SMaxime Ripard mask = SUN4I_TCON_GINT0_VBLANK_ENABLE(0) | 999026e0d1SMaxime Ripard SUN4I_TCON_GINT0_VBLANK_ENABLE(1); 1009026e0d1SMaxime Ripard 1019026e0d1SMaxime Ripard if (enable) 1029026e0d1SMaxime Ripard val = mask; 1039026e0d1SMaxime Ripard 1049026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, mask, val); 1059026e0d1SMaxime Ripard } 1069026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_enable_vblank); 1079026e0d1SMaxime Ripard 1089026e0d1SMaxime Ripard static int sun4i_tcon_get_clk_delay(struct drm_display_mode *mode, 1099026e0d1SMaxime Ripard int channel) 1109026e0d1SMaxime Ripard { 1119026e0d1SMaxime Ripard int delay = mode->vtotal - mode->vdisplay; 1129026e0d1SMaxime Ripard 1139026e0d1SMaxime Ripard if (mode->flags & DRM_MODE_FLAG_INTERLACE) 1149026e0d1SMaxime Ripard delay /= 2; 1159026e0d1SMaxime Ripard 1169026e0d1SMaxime Ripard if (channel == 1) 1179026e0d1SMaxime Ripard delay -= 2; 1189026e0d1SMaxime Ripard 1199026e0d1SMaxime Ripard delay = min(delay, 30); 1209026e0d1SMaxime Ripard 1219026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("TCON %d clock delay %u\n", channel, delay); 1229026e0d1SMaxime Ripard 1239026e0d1SMaxime Ripard return delay; 1249026e0d1SMaxime Ripard } 1259026e0d1SMaxime Ripard 1269026e0d1SMaxime Ripard void sun4i_tcon0_mode_set(struct sun4i_tcon *tcon, 1279026e0d1SMaxime Ripard struct drm_display_mode *mode) 1289026e0d1SMaxime Ripard { 1299026e0d1SMaxime Ripard unsigned int bp, hsync, vsync; 1309026e0d1SMaxime Ripard u8 clk_delay; 1319026e0d1SMaxime Ripard u32 val = 0; 1329026e0d1SMaxime Ripard 1339026e0d1SMaxime Ripard /* Adjust clock delay */ 1349026e0d1SMaxime Ripard clk_delay = sun4i_tcon_get_clk_delay(mode, 0); 1359026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 1369026e0d1SMaxime Ripard SUN4I_TCON0_CTL_CLK_DELAY_MASK, 1379026e0d1SMaxime Ripard SUN4I_TCON0_CTL_CLK_DELAY(clk_delay)); 1389026e0d1SMaxime Ripard 1399026e0d1SMaxime Ripard /* Set the resolution */ 1409026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC0_REG, 1419026e0d1SMaxime Ripard SUN4I_TCON0_BASIC0_X(mode->crtc_hdisplay) | 1429026e0d1SMaxime Ripard SUN4I_TCON0_BASIC0_Y(mode->crtc_vdisplay)); 1439026e0d1SMaxime Ripard 1449026e0d1SMaxime Ripard /* 1459026e0d1SMaxime Ripard * This is called a backporch in the register documentation, 14623a1cb11SChen-Yu Tsai * but it really is the back porch + hsync 1479026e0d1SMaxime Ripard */ 1489026e0d1SMaxime Ripard bp = mode->crtc_htotal - mode->crtc_hsync_start; 1499026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n", 1509026e0d1SMaxime Ripard mode->crtc_htotal, bp); 1519026e0d1SMaxime Ripard 1529026e0d1SMaxime Ripard /* Set horizontal display timings */ 1539026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG, 1549026e0d1SMaxime Ripard SUN4I_TCON0_BASIC1_H_TOTAL(mode->crtc_htotal) | 1559026e0d1SMaxime Ripard SUN4I_TCON0_BASIC1_H_BACKPORCH(bp)); 1569026e0d1SMaxime Ripard 1579026e0d1SMaxime Ripard /* 1589026e0d1SMaxime Ripard * This is called a backporch in the register documentation, 15923a1cb11SChen-Yu Tsai * but it really is the back porch + hsync 1609026e0d1SMaxime Ripard */ 1619026e0d1SMaxime Ripard bp = mode->crtc_vtotal - mode->crtc_vsync_start; 1629026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n", 1639026e0d1SMaxime Ripard mode->crtc_vtotal, bp); 1649026e0d1SMaxime Ripard 1659026e0d1SMaxime Ripard /* Set vertical display timings */ 1669026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG, 1679026e0d1SMaxime Ripard SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal) | 1689026e0d1SMaxime Ripard SUN4I_TCON0_BASIC2_V_BACKPORCH(bp)); 1699026e0d1SMaxime Ripard 1709026e0d1SMaxime Ripard /* Set Hsync and Vsync length */ 1719026e0d1SMaxime Ripard hsync = mode->crtc_hsync_end - mode->crtc_hsync_start; 1729026e0d1SMaxime Ripard vsync = mode->crtc_vsync_end - mode->crtc_vsync_start; 1739026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync); 1749026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC3_REG, 1759026e0d1SMaxime Ripard SUN4I_TCON0_BASIC3_V_SYNC(vsync) | 1769026e0d1SMaxime Ripard SUN4I_TCON0_BASIC3_H_SYNC(hsync)); 1779026e0d1SMaxime Ripard 1789026e0d1SMaxime Ripard /* Setup the polarity of the various signals */ 1799026e0d1SMaxime Ripard if (!(mode->flags & DRM_MODE_FLAG_PHSYNC)) 1809026e0d1SMaxime Ripard val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE; 1819026e0d1SMaxime Ripard 1829026e0d1SMaxime Ripard if (!(mode->flags & DRM_MODE_FLAG_PVSYNC)) 1839026e0d1SMaxime Ripard val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE; 1849026e0d1SMaxime Ripard 1859026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_IO_POL_REG, 1869026e0d1SMaxime Ripard SUN4I_TCON0_IO_POL_HSYNC_POSITIVE | SUN4I_TCON0_IO_POL_VSYNC_POSITIVE, 1879026e0d1SMaxime Ripard val); 1889026e0d1SMaxime Ripard 1899026e0d1SMaxime Ripard /* Map output pins to channel 0 */ 1909026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 1919026e0d1SMaxime Ripard SUN4I_TCON_GCTL_IOMAP_MASK, 1929026e0d1SMaxime Ripard SUN4I_TCON_GCTL_IOMAP_TCON0); 1939026e0d1SMaxime Ripard 1949026e0d1SMaxime Ripard /* Enable the output on the pins */ 1959026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0); 1969026e0d1SMaxime Ripard } 1979026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon0_mode_set); 1989026e0d1SMaxime Ripard 1999026e0d1SMaxime Ripard void sun4i_tcon1_mode_set(struct sun4i_tcon *tcon, 2009026e0d1SMaxime Ripard struct drm_display_mode *mode) 2019026e0d1SMaxime Ripard { 2029026e0d1SMaxime Ripard unsigned int bp, hsync, vsync; 2039026e0d1SMaxime Ripard u8 clk_delay; 2049026e0d1SMaxime Ripard u32 val; 2059026e0d1SMaxime Ripard 20691ea2f29SChen-Yu Tsai WARN_ON(!tcon->quirks->has_channel_1); 2078e924047SMaxime Ripard 2089026e0d1SMaxime Ripard /* Adjust clock delay */ 2099026e0d1SMaxime Ripard clk_delay = sun4i_tcon_get_clk_delay(mode, 1); 2109026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 2119026e0d1SMaxime Ripard SUN4I_TCON1_CTL_CLK_DELAY_MASK, 2129026e0d1SMaxime Ripard SUN4I_TCON1_CTL_CLK_DELAY(clk_delay)); 2139026e0d1SMaxime Ripard 2149026e0d1SMaxime Ripard /* Set interlaced mode */ 2159026e0d1SMaxime Ripard if (mode->flags & DRM_MODE_FLAG_INTERLACE) 2169026e0d1SMaxime Ripard val = SUN4I_TCON1_CTL_INTERLACE_ENABLE; 2179026e0d1SMaxime Ripard else 2189026e0d1SMaxime Ripard val = 0; 2199026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 2209026e0d1SMaxime Ripard SUN4I_TCON1_CTL_INTERLACE_ENABLE, 2219026e0d1SMaxime Ripard val); 2229026e0d1SMaxime Ripard 2239026e0d1SMaxime Ripard /* Set the input resolution */ 2249026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC0_REG, 2259026e0d1SMaxime Ripard SUN4I_TCON1_BASIC0_X(mode->crtc_hdisplay) | 2269026e0d1SMaxime Ripard SUN4I_TCON1_BASIC0_Y(mode->crtc_vdisplay)); 2279026e0d1SMaxime Ripard 2289026e0d1SMaxime Ripard /* Set the upscaling resolution */ 2299026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC1_REG, 2309026e0d1SMaxime Ripard SUN4I_TCON1_BASIC1_X(mode->crtc_hdisplay) | 2319026e0d1SMaxime Ripard SUN4I_TCON1_BASIC1_Y(mode->crtc_vdisplay)); 2329026e0d1SMaxime Ripard 2339026e0d1SMaxime Ripard /* Set the output resolution */ 2349026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC2_REG, 2359026e0d1SMaxime Ripard SUN4I_TCON1_BASIC2_X(mode->crtc_hdisplay) | 2369026e0d1SMaxime Ripard SUN4I_TCON1_BASIC2_Y(mode->crtc_vdisplay)); 2379026e0d1SMaxime Ripard 2389026e0d1SMaxime Ripard /* Set horizontal display timings */ 2399026e0d1SMaxime Ripard bp = mode->crtc_htotal - mode->crtc_hsync_end; 2409026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n", 2419026e0d1SMaxime Ripard mode->htotal, bp); 2429026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC3_REG, 2439026e0d1SMaxime Ripard SUN4I_TCON1_BASIC3_H_TOTAL(mode->crtc_htotal) | 2449026e0d1SMaxime Ripard SUN4I_TCON1_BASIC3_H_BACKPORCH(bp)); 2459026e0d1SMaxime Ripard 2469026e0d1SMaxime Ripard /* Set vertical display timings */ 2479026e0d1SMaxime Ripard bp = mode->crtc_vtotal - mode->crtc_vsync_end; 2489026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n", 2499026e0d1SMaxime Ripard mode->vtotal, bp); 2509026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC4_REG, 2519026e0d1SMaxime Ripard SUN4I_TCON1_BASIC4_V_TOTAL(mode->vtotal) | 2529026e0d1SMaxime Ripard SUN4I_TCON1_BASIC4_V_BACKPORCH(bp)); 2539026e0d1SMaxime Ripard 2549026e0d1SMaxime Ripard /* Set Hsync and Vsync length */ 2559026e0d1SMaxime Ripard hsync = mode->crtc_hsync_end - mode->crtc_hsync_start; 2569026e0d1SMaxime Ripard vsync = mode->crtc_vsync_end - mode->crtc_vsync_start; 2579026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync); 2589026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC5_REG, 2599026e0d1SMaxime Ripard SUN4I_TCON1_BASIC5_V_SYNC(vsync) | 2609026e0d1SMaxime Ripard SUN4I_TCON1_BASIC5_H_SYNC(hsync)); 2619026e0d1SMaxime Ripard 2629026e0d1SMaxime Ripard /* Map output pins to channel 1 */ 2639026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 2649026e0d1SMaxime Ripard SUN4I_TCON_GCTL_IOMAP_MASK, 2659026e0d1SMaxime Ripard SUN4I_TCON_GCTL_IOMAP_TCON1); 2669026e0d1SMaxime Ripard 2679026e0d1SMaxime Ripard /* 2689026e0d1SMaxime Ripard * FIXME: Undocumented bits 2699026e0d1SMaxime Ripard */ 27091ea2f29SChen-Yu Tsai if (tcon->quirks->has_unknown_mux) 2719026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_MUX_CTRL_REG, 1); 2729026e0d1SMaxime Ripard } 2739026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon1_mode_set); 2749026e0d1SMaxime Ripard 2759026e0d1SMaxime Ripard static void sun4i_tcon_finish_page_flip(struct drm_device *dev, 2769026e0d1SMaxime Ripard struct sun4i_crtc *scrtc) 2779026e0d1SMaxime Ripard { 2789026e0d1SMaxime Ripard unsigned long flags; 2799026e0d1SMaxime Ripard 2809026e0d1SMaxime Ripard spin_lock_irqsave(&dev->event_lock, flags); 2819026e0d1SMaxime Ripard if (scrtc->event) { 2829026e0d1SMaxime Ripard drm_crtc_send_vblank_event(&scrtc->crtc, scrtc->event); 2839026e0d1SMaxime Ripard drm_crtc_vblank_put(&scrtc->crtc); 2849026e0d1SMaxime Ripard scrtc->event = NULL; 2859026e0d1SMaxime Ripard } 2869026e0d1SMaxime Ripard spin_unlock_irqrestore(&dev->event_lock, flags); 2879026e0d1SMaxime Ripard } 2889026e0d1SMaxime Ripard 2899026e0d1SMaxime Ripard static irqreturn_t sun4i_tcon_handler(int irq, void *private) 2909026e0d1SMaxime Ripard { 2919026e0d1SMaxime Ripard struct sun4i_tcon *tcon = private; 2929026e0d1SMaxime Ripard struct drm_device *drm = tcon->drm; 29346cce6daSChen-Yu Tsai struct sun4i_crtc *scrtc = tcon->crtc; 2949026e0d1SMaxime Ripard unsigned int status; 2959026e0d1SMaxime Ripard 2969026e0d1SMaxime Ripard regmap_read(tcon->regs, SUN4I_TCON_GINT0_REG, &status); 2979026e0d1SMaxime Ripard 2989026e0d1SMaxime Ripard if (!(status & (SUN4I_TCON_GINT0_VBLANK_INT(0) | 2999026e0d1SMaxime Ripard SUN4I_TCON_GINT0_VBLANK_INT(1)))) 3009026e0d1SMaxime Ripard return IRQ_NONE; 3019026e0d1SMaxime Ripard 3029026e0d1SMaxime Ripard drm_crtc_handle_vblank(&scrtc->crtc); 3039026e0d1SMaxime Ripard sun4i_tcon_finish_page_flip(drm, scrtc); 3049026e0d1SMaxime Ripard 3059026e0d1SMaxime Ripard /* Acknowledge the interrupt */ 3069026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, 3079026e0d1SMaxime Ripard SUN4I_TCON_GINT0_VBLANK_INT(0) | 3089026e0d1SMaxime Ripard SUN4I_TCON_GINT0_VBLANK_INT(1), 3099026e0d1SMaxime Ripard 0); 3109026e0d1SMaxime Ripard 3119026e0d1SMaxime Ripard return IRQ_HANDLED; 3129026e0d1SMaxime Ripard } 3139026e0d1SMaxime Ripard 3149026e0d1SMaxime Ripard static int sun4i_tcon_init_clocks(struct device *dev, 3159026e0d1SMaxime Ripard struct sun4i_tcon *tcon) 3169026e0d1SMaxime Ripard { 3179026e0d1SMaxime Ripard tcon->clk = devm_clk_get(dev, "ahb"); 3189026e0d1SMaxime Ripard if (IS_ERR(tcon->clk)) { 3199026e0d1SMaxime Ripard dev_err(dev, "Couldn't get the TCON bus clock\n"); 3209026e0d1SMaxime Ripard return PTR_ERR(tcon->clk); 3219026e0d1SMaxime Ripard } 3229026e0d1SMaxime Ripard clk_prepare_enable(tcon->clk); 3239026e0d1SMaxime Ripard 3249026e0d1SMaxime Ripard tcon->sclk0 = devm_clk_get(dev, "tcon-ch0"); 3259026e0d1SMaxime Ripard if (IS_ERR(tcon->sclk0)) { 3269026e0d1SMaxime Ripard dev_err(dev, "Couldn't get the TCON channel 0 clock\n"); 3279026e0d1SMaxime Ripard return PTR_ERR(tcon->sclk0); 3289026e0d1SMaxime Ripard } 3299026e0d1SMaxime Ripard 33091ea2f29SChen-Yu Tsai if (tcon->quirks->has_channel_1) { 3319026e0d1SMaxime Ripard tcon->sclk1 = devm_clk_get(dev, "tcon-ch1"); 3329026e0d1SMaxime Ripard if (IS_ERR(tcon->sclk1)) { 3339026e0d1SMaxime Ripard dev_err(dev, "Couldn't get the TCON channel 1 clock\n"); 3349026e0d1SMaxime Ripard return PTR_ERR(tcon->sclk1); 3359026e0d1SMaxime Ripard } 3368e924047SMaxime Ripard } 3379026e0d1SMaxime Ripard 3384c7f16d1SChen-Yu Tsai return 0; 3399026e0d1SMaxime Ripard } 3409026e0d1SMaxime Ripard 3419026e0d1SMaxime Ripard static void sun4i_tcon_free_clocks(struct sun4i_tcon *tcon) 3429026e0d1SMaxime Ripard { 3439026e0d1SMaxime Ripard clk_disable_unprepare(tcon->clk); 3449026e0d1SMaxime Ripard } 3459026e0d1SMaxime Ripard 3469026e0d1SMaxime Ripard static int sun4i_tcon_init_irq(struct device *dev, 3479026e0d1SMaxime Ripard struct sun4i_tcon *tcon) 3489026e0d1SMaxime Ripard { 3499026e0d1SMaxime Ripard struct platform_device *pdev = to_platform_device(dev); 3509026e0d1SMaxime Ripard int irq, ret; 3519026e0d1SMaxime Ripard 3529026e0d1SMaxime Ripard irq = platform_get_irq(pdev, 0); 3539026e0d1SMaxime Ripard if (irq < 0) { 3549026e0d1SMaxime Ripard dev_err(dev, "Couldn't retrieve the TCON interrupt\n"); 3559026e0d1SMaxime Ripard return irq; 3569026e0d1SMaxime Ripard } 3579026e0d1SMaxime Ripard 3589026e0d1SMaxime Ripard ret = devm_request_irq(dev, irq, sun4i_tcon_handler, 0, 3599026e0d1SMaxime Ripard dev_name(dev), tcon); 3609026e0d1SMaxime Ripard if (ret) { 3619026e0d1SMaxime Ripard dev_err(dev, "Couldn't request the IRQ\n"); 3629026e0d1SMaxime Ripard return ret; 3639026e0d1SMaxime Ripard } 3649026e0d1SMaxime Ripard 3659026e0d1SMaxime Ripard return 0; 3669026e0d1SMaxime Ripard } 3679026e0d1SMaxime Ripard 3689026e0d1SMaxime Ripard static struct regmap_config sun4i_tcon_regmap_config = { 3699026e0d1SMaxime Ripard .reg_bits = 32, 3709026e0d1SMaxime Ripard .val_bits = 32, 3719026e0d1SMaxime Ripard .reg_stride = 4, 3729026e0d1SMaxime Ripard .max_register = 0x800, 3739026e0d1SMaxime Ripard }; 3749026e0d1SMaxime Ripard 3759026e0d1SMaxime Ripard static int sun4i_tcon_init_regmap(struct device *dev, 3769026e0d1SMaxime Ripard struct sun4i_tcon *tcon) 3779026e0d1SMaxime Ripard { 3789026e0d1SMaxime Ripard struct platform_device *pdev = to_platform_device(dev); 3799026e0d1SMaxime Ripard struct resource *res; 3809026e0d1SMaxime Ripard void __iomem *regs; 3819026e0d1SMaxime Ripard 3829026e0d1SMaxime Ripard res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 3839026e0d1SMaxime Ripard regs = devm_ioremap_resource(dev, res); 384af346f55SWei Yongjun if (IS_ERR(regs)) 3859026e0d1SMaxime Ripard return PTR_ERR(regs); 3869026e0d1SMaxime Ripard 3879026e0d1SMaxime Ripard tcon->regs = devm_regmap_init_mmio(dev, regs, 3889026e0d1SMaxime Ripard &sun4i_tcon_regmap_config); 3899026e0d1SMaxime Ripard if (IS_ERR(tcon->regs)) { 3909026e0d1SMaxime Ripard dev_err(dev, "Couldn't create the TCON regmap\n"); 3919026e0d1SMaxime Ripard return PTR_ERR(tcon->regs); 3929026e0d1SMaxime Ripard } 3939026e0d1SMaxime Ripard 3949026e0d1SMaxime Ripard /* Make sure the TCON is disabled and all IRQs are off */ 3959026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_GCTL_REG, 0); 3969026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_GINT0_REG, 0); 3979026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_GINT1_REG, 0); 3989026e0d1SMaxime Ripard 3999026e0d1SMaxime Ripard /* Disable IO lines and set them to tristate */ 4009026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, ~0); 4019026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_IO_TRI_REG, ~0); 4029026e0d1SMaxime Ripard 4039026e0d1SMaxime Ripard return 0; 4049026e0d1SMaxime Ripard } 4059026e0d1SMaxime Ripard 4069026e0d1SMaxime Ripard static int sun4i_tcon_bind(struct device *dev, struct device *master, 4079026e0d1SMaxime Ripard void *data) 4089026e0d1SMaxime Ripard { 4099026e0d1SMaxime Ripard struct drm_device *drm = data; 4109026e0d1SMaxime Ripard struct sun4i_drv *drv = drm->dev_private; 411*80a58240SChen-Yu Tsai struct sun4i_backend *backend; 4129026e0d1SMaxime Ripard struct sun4i_tcon *tcon; 4139026e0d1SMaxime Ripard int ret; 4149026e0d1SMaxime Ripard 415*80a58240SChen-Yu Tsai /* Wait for a backend to be registered */ 416*80a58240SChen-Yu Tsai if (list_empty(&drv->backend_list)) 417*80a58240SChen-Yu Tsai return -EPROBE_DEFER; 418*80a58240SChen-Yu Tsai 4199026e0d1SMaxime Ripard tcon = devm_kzalloc(dev, sizeof(*tcon), GFP_KERNEL); 4209026e0d1SMaxime Ripard if (!tcon) 4219026e0d1SMaxime Ripard return -ENOMEM; 4229026e0d1SMaxime Ripard dev_set_drvdata(dev, tcon); 4239026e0d1SMaxime Ripard tcon->drm = drm; 424ae558110SMaxime Ripard tcon->dev = dev; 42591ea2f29SChen-Yu Tsai tcon->quirks = of_device_get_match_data(dev); 4269026e0d1SMaxime Ripard 4279026e0d1SMaxime Ripard tcon->lcd_rst = devm_reset_control_get(dev, "lcd"); 4289026e0d1SMaxime Ripard if (IS_ERR(tcon->lcd_rst)) { 4299026e0d1SMaxime Ripard dev_err(dev, "Couldn't get our reset line\n"); 4309026e0d1SMaxime Ripard return PTR_ERR(tcon->lcd_rst); 4319026e0d1SMaxime Ripard } 4329026e0d1SMaxime Ripard 4339026e0d1SMaxime Ripard /* Make sure our TCON is reset */ 4349026e0d1SMaxime Ripard if (!reset_control_status(tcon->lcd_rst)) 4359026e0d1SMaxime Ripard reset_control_assert(tcon->lcd_rst); 4369026e0d1SMaxime Ripard 4379026e0d1SMaxime Ripard ret = reset_control_deassert(tcon->lcd_rst); 4389026e0d1SMaxime Ripard if (ret) { 4399026e0d1SMaxime Ripard dev_err(dev, "Couldn't deassert our reset line\n"); 4409026e0d1SMaxime Ripard return ret; 4419026e0d1SMaxime Ripard } 4429026e0d1SMaxime Ripard 4439026e0d1SMaxime Ripard ret = sun4i_tcon_init_clocks(dev, tcon); 4449026e0d1SMaxime Ripard if (ret) { 4459026e0d1SMaxime Ripard dev_err(dev, "Couldn't init our TCON clocks\n"); 4469026e0d1SMaxime Ripard goto err_assert_reset; 4479026e0d1SMaxime Ripard } 4489026e0d1SMaxime Ripard 4494c7f16d1SChen-Yu Tsai ret = sun4i_tcon_init_regmap(dev, tcon); 4509026e0d1SMaxime Ripard if (ret) { 4514c7f16d1SChen-Yu Tsai dev_err(dev, "Couldn't init our TCON regmap\n"); 4529026e0d1SMaxime Ripard goto err_free_clocks; 4539026e0d1SMaxime Ripard } 4549026e0d1SMaxime Ripard 4554c7f16d1SChen-Yu Tsai ret = sun4i_dclk_create(dev, tcon); 4564c7f16d1SChen-Yu Tsai if (ret) { 4574c7f16d1SChen-Yu Tsai dev_err(dev, "Couldn't create our TCON dot clock\n"); 4584c7f16d1SChen-Yu Tsai goto err_free_clocks; 4594c7f16d1SChen-Yu Tsai } 4604c7f16d1SChen-Yu Tsai 4619026e0d1SMaxime Ripard ret = sun4i_tcon_init_irq(dev, tcon); 4629026e0d1SMaxime Ripard if (ret) { 4639026e0d1SMaxime Ripard dev_err(dev, "Couldn't init our TCON interrupts\n"); 4644c7f16d1SChen-Yu Tsai goto err_free_dotclock; 4659026e0d1SMaxime Ripard } 4669026e0d1SMaxime Ripard 467*80a58240SChen-Yu Tsai backend = list_first_entry(&drv->backend_list, 468*80a58240SChen-Yu Tsai struct sun4i_backend, list); 469*80a58240SChen-Yu Tsai tcon->crtc = sun4i_crtc_init(drm, backend, tcon); 47046cce6daSChen-Yu Tsai if (IS_ERR(tcon->crtc)) { 47146cce6daSChen-Yu Tsai dev_err(dev, "Couldn't create our CRTC\n"); 47246cce6daSChen-Yu Tsai ret = PTR_ERR(tcon->crtc); 47346cce6daSChen-Yu Tsai goto err_free_clocks; 47446cce6daSChen-Yu Tsai } 47546cce6daSChen-Yu Tsai 476b9c8506cSChen-Yu Tsai ret = sun4i_rgb_init(drm, tcon); 47713fef095SChen-Yu Tsai if (ret < 0) 47813fef095SChen-Yu Tsai goto err_free_clocks; 47913fef095SChen-Yu Tsai 480*80a58240SChen-Yu Tsai list_add_tail(&tcon->list, &drv->tcon_list); 481*80a58240SChen-Yu Tsai 48213fef095SChen-Yu Tsai return 0; 4839026e0d1SMaxime Ripard 4844c7f16d1SChen-Yu Tsai err_free_dotclock: 4854c7f16d1SChen-Yu Tsai sun4i_dclk_free(tcon); 4869026e0d1SMaxime Ripard err_free_clocks: 4879026e0d1SMaxime Ripard sun4i_tcon_free_clocks(tcon); 4889026e0d1SMaxime Ripard err_assert_reset: 4899026e0d1SMaxime Ripard reset_control_assert(tcon->lcd_rst); 4909026e0d1SMaxime Ripard return ret; 4919026e0d1SMaxime Ripard } 4929026e0d1SMaxime Ripard 4939026e0d1SMaxime Ripard static void sun4i_tcon_unbind(struct device *dev, struct device *master, 4949026e0d1SMaxime Ripard void *data) 4959026e0d1SMaxime Ripard { 4969026e0d1SMaxime Ripard struct sun4i_tcon *tcon = dev_get_drvdata(dev); 4979026e0d1SMaxime Ripard 498*80a58240SChen-Yu Tsai list_del(&tcon->list); 4994c7f16d1SChen-Yu Tsai sun4i_dclk_free(tcon); 5009026e0d1SMaxime Ripard sun4i_tcon_free_clocks(tcon); 5019026e0d1SMaxime Ripard } 5029026e0d1SMaxime Ripard 503dfeb693dSJulia Lawall static const struct component_ops sun4i_tcon_ops = { 5049026e0d1SMaxime Ripard .bind = sun4i_tcon_bind, 5059026e0d1SMaxime Ripard .unbind = sun4i_tcon_unbind, 5069026e0d1SMaxime Ripard }; 5079026e0d1SMaxime Ripard 5089026e0d1SMaxime Ripard static int sun4i_tcon_probe(struct platform_device *pdev) 5099026e0d1SMaxime Ripard { 51029e57fabSMaxime Ripard struct device_node *node = pdev->dev.of_node; 511894f5a9fSMaxime Ripard struct drm_bridge *bridge; 51229e57fabSMaxime Ripard struct drm_panel *panel; 513ebc94461SRob Herring int ret; 51429e57fabSMaxime Ripard 515ebc94461SRob Herring ret = drm_of_find_panel_or_bridge(node, 1, 0, &panel, &bridge); 516ebc94461SRob Herring if (ret == -EPROBE_DEFER) 517ebc94461SRob Herring return ret; 51829e57fabSMaxime Ripard 5199026e0d1SMaxime Ripard return component_add(&pdev->dev, &sun4i_tcon_ops); 5209026e0d1SMaxime Ripard } 5219026e0d1SMaxime Ripard 5229026e0d1SMaxime Ripard static int sun4i_tcon_remove(struct platform_device *pdev) 5239026e0d1SMaxime Ripard { 5249026e0d1SMaxime Ripard component_del(&pdev->dev, &sun4i_tcon_ops); 5259026e0d1SMaxime Ripard 5269026e0d1SMaxime Ripard return 0; 5279026e0d1SMaxime Ripard } 5289026e0d1SMaxime Ripard 52991ea2f29SChen-Yu Tsai static const struct sun4i_tcon_quirks sun5i_a13_quirks = { 53091ea2f29SChen-Yu Tsai .has_unknown_mux = true, 53191ea2f29SChen-Yu Tsai .has_channel_1 = true, 53291ea2f29SChen-Yu Tsai }; 53391ea2f29SChen-Yu Tsai 53493a5ec14SChen-Yu Tsai static const struct sun4i_tcon_quirks sun6i_a31_quirks = { 53593a5ec14SChen-Yu Tsai .has_channel_1 = true, 53693a5ec14SChen-Yu Tsai }; 53793a5ec14SChen-Yu Tsai 53893a5ec14SChen-Yu Tsai static const struct sun4i_tcon_quirks sun6i_a31s_quirks = { 53993a5ec14SChen-Yu Tsai .has_channel_1 = true, 54093a5ec14SChen-Yu Tsai }; 54193a5ec14SChen-Yu Tsai 54291ea2f29SChen-Yu Tsai static const struct sun4i_tcon_quirks sun8i_a33_quirks = { 54391ea2f29SChen-Yu Tsai /* nothing is supported */ 54491ea2f29SChen-Yu Tsai }; 54591ea2f29SChen-Yu Tsai 5469026e0d1SMaxime Ripard static const struct of_device_id sun4i_tcon_of_table[] = { 54791ea2f29SChen-Yu Tsai { .compatible = "allwinner,sun5i-a13-tcon", .data = &sun5i_a13_quirks }, 54893a5ec14SChen-Yu Tsai { .compatible = "allwinner,sun6i-a31-tcon", .data = &sun6i_a31_quirks }, 54993a5ec14SChen-Yu Tsai { .compatible = "allwinner,sun6i-a31s-tcon", .data = &sun6i_a31s_quirks }, 55091ea2f29SChen-Yu Tsai { .compatible = "allwinner,sun8i-a33-tcon", .data = &sun8i_a33_quirks }, 5519026e0d1SMaxime Ripard { } 5529026e0d1SMaxime Ripard }; 5539026e0d1SMaxime Ripard MODULE_DEVICE_TABLE(of, sun4i_tcon_of_table); 5549026e0d1SMaxime Ripard 5559026e0d1SMaxime Ripard static struct platform_driver sun4i_tcon_platform_driver = { 5569026e0d1SMaxime Ripard .probe = sun4i_tcon_probe, 5579026e0d1SMaxime Ripard .remove = sun4i_tcon_remove, 5589026e0d1SMaxime Ripard .driver = { 5599026e0d1SMaxime Ripard .name = "sun4i-tcon", 5609026e0d1SMaxime Ripard .of_match_table = sun4i_tcon_of_table, 5619026e0d1SMaxime Ripard }, 5629026e0d1SMaxime Ripard }; 5639026e0d1SMaxime Ripard module_platform_driver(sun4i_tcon_platform_driver); 5649026e0d1SMaxime Ripard 5659026e0d1SMaxime Ripard MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>"); 5669026e0d1SMaxime Ripard MODULE_DESCRIPTION("Allwinner A10 Timing Controller Driver"); 5679026e0d1SMaxime Ripard MODULE_LICENSE("GPL"); 568