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 2880a58240SChen-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 133*86cf6788SChen-Yu Tsai /* Configure the dot clock */ 134*86cf6788SChen-Yu Tsai clk_set_rate(tcon->dclk, mode->crtc_clock * 1000); 135*86cf6788SChen-Yu Tsai 1369026e0d1SMaxime Ripard /* Adjust clock delay */ 1379026e0d1SMaxime Ripard clk_delay = sun4i_tcon_get_clk_delay(mode, 0); 1389026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 1399026e0d1SMaxime Ripard SUN4I_TCON0_CTL_CLK_DELAY_MASK, 1409026e0d1SMaxime Ripard SUN4I_TCON0_CTL_CLK_DELAY(clk_delay)); 1419026e0d1SMaxime Ripard 1429026e0d1SMaxime Ripard /* Set the resolution */ 1439026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC0_REG, 1449026e0d1SMaxime Ripard SUN4I_TCON0_BASIC0_X(mode->crtc_hdisplay) | 1459026e0d1SMaxime Ripard SUN4I_TCON0_BASIC0_Y(mode->crtc_vdisplay)); 1469026e0d1SMaxime Ripard 1479026e0d1SMaxime Ripard /* 1489026e0d1SMaxime Ripard * This is called a backporch in the register documentation, 14923a1cb11SChen-Yu Tsai * but it really is the back porch + hsync 1509026e0d1SMaxime Ripard */ 1519026e0d1SMaxime Ripard bp = mode->crtc_htotal - mode->crtc_hsync_start; 1529026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n", 1539026e0d1SMaxime Ripard mode->crtc_htotal, bp); 1549026e0d1SMaxime Ripard 1559026e0d1SMaxime Ripard /* Set horizontal display timings */ 1569026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG, 1579026e0d1SMaxime Ripard SUN4I_TCON0_BASIC1_H_TOTAL(mode->crtc_htotal) | 1589026e0d1SMaxime Ripard SUN4I_TCON0_BASIC1_H_BACKPORCH(bp)); 1599026e0d1SMaxime Ripard 1609026e0d1SMaxime Ripard /* 1619026e0d1SMaxime Ripard * This is called a backporch in the register documentation, 16223a1cb11SChen-Yu Tsai * but it really is the back porch + hsync 1639026e0d1SMaxime Ripard */ 1649026e0d1SMaxime Ripard bp = mode->crtc_vtotal - mode->crtc_vsync_start; 1659026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n", 1669026e0d1SMaxime Ripard mode->crtc_vtotal, bp); 1679026e0d1SMaxime Ripard 1689026e0d1SMaxime Ripard /* Set vertical display timings */ 1699026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG, 1709026e0d1SMaxime Ripard SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal) | 1719026e0d1SMaxime Ripard SUN4I_TCON0_BASIC2_V_BACKPORCH(bp)); 1729026e0d1SMaxime Ripard 1739026e0d1SMaxime Ripard /* Set Hsync and Vsync length */ 1749026e0d1SMaxime Ripard hsync = mode->crtc_hsync_end - mode->crtc_hsync_start; 1759026e0d1SMaxime Ripard vsync = mode->crtc_vsync_end - mode->crtc_vsync_start; 1769026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync); 1779026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC3_REG, 1789026e0d1SMaxime Ripard SUN4I_TCON0_BASIC3_V_SYNC(vsync) | 1799026e0d1SMaxime Ripard SUN4I_TCON0_BASIC3_H_SYNC(hsync)); 1809026e0d1SMaxime Ripard 1819026e0d1SMaxime Ripard /* Setup the polarity of the various signals */ 1829026e0d1SMaxime Ripard if (!(mode->flags & DRM_MODE_FLAG_PHSYNC)) 1839026e0d1SMaxime Ripard val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE; 1849026e0d1SMaxime Ripard 1859026e0d1SMaxime Ripard if (!(mode->flags & DRM_MODE_FLAG_PVSYNC)) 1869026e0d1SMaxime Ripard val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE; 1879026e0d1SMaxime Ripard 1889026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_IO_POL_REG, 1899026e0d1SMaxime Ripard SUN4I_TCON0_IO_POL_HSYNC_POSITIVE | SUN4I_TCON0_IO_POL_VSYNC_POSITIVE, 1909026e0d1SMaxime Ripard val); 1919026e0d1SMaxime Ripard 1929026e0d1SMaxime Ripard /* Map output pins to channel 0 */ 1939026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 1949026e0d1SMaxime Ripard SUN4I_TCON_GCTL_IOMAP_MASK, 1959026e0d1SMaxime Ripard SUN4I_TCON_GCTL_IOMAP_TCON0); 1969026e0d1SMaxime Ripard 1979026e0d1SMaxime Ripard /* Enable the output on the pins */ 1989026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0); 1999026e0d1SMaxime Ripard } 2009026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon0_mode_set); 2019026e0d1SMaxime Ripard 2029026e0d1SMaxime Ripard void sun4i_tcon1_mode_set(struct sun4i_tcon *tcon, 2039026e0d1SMaxime Ripard struct drm_display_mode *mode) 2049026e0d1SMaxime Ripard { 2059026e0d1SMaxime Ripard unsigned int bp, hsync, vsync; 2069026e0d1SMaxime Ripard u8 clk_delay; 2079026e0d1SMaxime Ripard u32 val; 2089026e0d1SMaxime Ripard 20991ea2f29SChen-Yu Tsai WARN_ON(!tcon->quirks->has_channel_1); 2108e924047SMaxime Ripard 211*86cf6788SChen-Yu Tsai /* Configure the dot clock */ 212*86cf6788SChen-Yu Tsai clk_set_rate(tcon->sclk1, mode->crtc_clock * 1000); 213*86cf6788SChen-Yu Tsai 2149026e0d1SMaxime Ripard /* Adjust clock delay */ 2159026e0d1SMaxime Ripard clk_delay = sun4i_tcon_get_clk_delay(mode, 1); 2169026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 2179026e0d1SMaxime Ripard SUN4I_TCON1_CTL_CLK_DELAY_MASK, 2189026e0d1SMaxime Ripard SUN4I_TCON1_CTL_CLK_DELAY(clk_delay)); 2199026e0d1SMaxime Ripard 2209026e0d1SMaxime Ripard /* Set interlaced mode */ 2219026e0d1SMaxime Ripard if (mode->flags & DRM_MODE_FLAG_INTERLACE) 2229026e0d1SMaxime Ripard val = SUN4I_TCON1_CTL_INTERLACE_ENABLE; 2239026e0d1SMaxime Ripard else 2249026e0d1SMaxime Ripard val = 0; 2259026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 2269026e0d1SMaxime Ripard SUN4I_TCON1_CTL_INTERLACE_ENABLE, 2279026e0d1SMaxime Ripard val); 2289026e0d1SMaxime Ripard 2299026e0d1SMaxime Ripard /* Set the input resolution */ 2309026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC0_REG, 2319026e0d1SMaxime Ripard SUN4I_TCON1_BASIC0_X(mode->crtc_hdisplay) | 2329026e0d1SMaxime Ripard SUN4I_TCON1_BASIC0_Y(mode->crtc_vdisplay)); 2339026e0d1SMaxime Ripard 2349026e0d1SMaxime Ripard /* Set the upscaling resolution */ 2359026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC1_REG, 2369026e0d1SMaxime Ripard SUN4I_TCON1_BASIC1_X(mode->crtc_hdisplay) | 2379026e0d1SMaxime Ripard SUN4I_TCON1_BASIC1_Y(mode->crtc_vdisplay)); 2389026e0d1SMaxime Ripard 2399026e0d1SMaxime Ripard /* Set the output resolution */ 2409026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC2_REG, 2419026e0d1SMaxime Ripard SUN4I_TCON1_BASIC2_X(mode->crtc_hdisplay) | 2429026e0d1SMaxime Ripard SUN4I_TCON1_BASIC2_Y(mode->crtc_vdisplay)); 2439026e0d1SMaxime Ripard 2449026e0d1SMaxime Ripard /* Set horizontal display timings */ 2459026e0d1SMaxime Ripard bp = mode->crtc_htotal - mode->crtc_hsync_end; 2469026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n", 2479026e0d1SMaxime Ripard mode->htotal, bp); 2489026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC3_REG, 2499026e0d1SMaxime Ripard SUN4I_TCON1_BASIC3_H_TOTAL(mode->crtc_htotal) | 2509026e0d1SMaxime Ripard SUN4I_TCON1_BASIC3_H_BACKPORCH(bp)); 2519026e0d1SMaxime Ripard 2529026e0d1SMaxime Ripard /* Set vertical display timings */ 2539026e0d1SMaxime Ripard bp = mode->crtc_vtotal - mode->crtc_vsync_end; 2549026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n", 2559026e0d1SMaxime Ripard mode->vtotal, bp); 2569026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC4_REG, 2579026e0d1SMaxime Ripard SUN4I_TCON1_BASIC4_V_TOTAL(mode->vtotal) | 2589026e0d1SMaxime Ripard SUN4I_TCON1_BASIC4_V_BACKPORCH(bp)); 2599026e0d1SMaxime Ripard 2609026e0d1SMaxime Ripard /* Set Hsync and Vsync length */ 2619026e0d1SMaxime Ripard hsync = mode->crtc_hsync_end - mode->crtc_hsync_start; 2629026e0d1SMaxime Ripard vsync = mode->crtc_vsync_end - mode->crtc_vsync_start; 2639026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync); 2649026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC5_REG, 2659026e0d1SMaxime Ripard SUN4I_TCON1_BASIC5_V_SYNC(vsync) | 2669026e0d1SMaxime Ripard SUN4I_TCON1_BASIC5_H_SYNC(hsync)); 2679026e0d1SMaxime Ripard 2689026e0d1SMaxime Ripard /* Map output pins to channel 1 */ 2699026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 2709026e0d1SMaxime Ripard SUN4I_TCON_GCTL_IOMAP_MASK, 2719026e0d1SMaxime Ripard SUN4I_TCON_GCTL_IOMAP_TCON1); 2729026e0d1SMaxime Ripard 2739026e0d1SMaxime Ripard /* 2749026e0d1SMaxime Ripard * FIXME: Undocumented bits 2759026e0d1SMaxime Ripard */ 27691ea2f29SChen-Yu Tsai if (tcon->quirks->has_unknown_mux) 2779026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_MUX_CTRL_REG, 1); 2789026e0d1SMaxime Ripard } 2799026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon1_mode_set); 2809026e0d1SMaxime Ripard 2819026e0d1SMaxime Ripard static void sun4i_tcon_finish_page_flip(struct drm_device *dev, 2829026e0d1SMaxime Ripard struct sun4i_crtc *scrtc) 2839026e0d1SMaxime Ripard { 2849026e0d1SMaxime Ripard unsigned long flags; 2859026e0d1SMaxime Ripard 2869026e0d1SMaxime Ripard spin_lock_irqsave(&dev->event_lock, flags); 2879026e0d1SMaxime Ripard if (scrtc->event) { 2889026e0d1SMaxime Ripard drm_crtc_send_vblank_event(&scrtc->crtc, scrtc->event); 2899026e0d1SMaxime Ripard drm_crtc_vblank_put(&scrtc->crtc); 2909026e0d1SMaxime Ripard scrtc->event = NULL; 2919026e0d1SMaxime Ripard } 2929026e0d1SMaxime Ripard spin_unlock_irqrestore(&dev->event_lock, flags); 2939026e0d1SMaxime Ripard } 2949026e0d1SMaxime Ripard 2959026e0d1SMaxime Ripard static irqreturn_t sun4i_tcon_handler(int irq, void *private) 2969026e0d1SMaxime Ripard { 2979026e0d1SMaxime Ripard struct sun4i_tcon *tcon = private; 2989026e0d1SMaxime Ripard struct drm_device *drm = tcon->drm; 29946cce6daSChen-Yu Tsai struct sun4i_crtc *scrtc = tcon->crtc; 3009026e0d1SMaxime Ripard unsigned int status; 3019026e0d1SMaxime Ripard 3029026e0d1SMaxime Ripard regmap_read(tcon->regs, SUN4I_TCON_GINT0_REG, &status); 3039026e0d1SMaxime Ripard 3049026e0d1SMaxime Ripard if (!(status & (SUN4I_TCON_GINT0_VBLANK_INT(0) | 3059026e0d1SMaxime Ripard SUN4I_TCON_GINT0_VBLANK_INT(1)))) 3069026e0d1SMaxime Ripard return IRQ_NONE; 3079026e0d1SMaxime Ripard 3089026e0d1SMaxime Ripard drm_crtc_handle_vblank(&scrtc->crtc); 3099026e0d1SMaxime Ripard sun4i_tcon_finish_page_flip(drm, scrtc); 3109026e0d1SMaxime Ripard 3119026e0d1SMaxime Ripard /* Acknowledge the interrupt */ 3129026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, 3139026e0d1SMaxime Ripard SUN4I_TCON_GINT0_VBLANK_INT(0) | 3149026e0d1SMaxime Ripard SUN4I_TCON_GINT0_VBLANK_INT(1), 3159026e0d1SMaxime Ripard 0); 3169026e0d1SMaxime Ripard 3179026e0d1SMaxime Ripard return IRQ_HANDLED; 3189026e0d1SMaxime Ripard } 3199026e0d1SMaxime Ripard 3209026e0d1SMaxime Ripard static int sun4i_tcon_init_clocks(struct device *dev, 3219026e0d1SMaxime Ripard struct sun4i_tcon *tcon) 3229026e0d1SMaxime Ripard { 3239026e0d1SMaxime Ripard tcon->clk = devm_clk_get(dev, "ahb"); 3249026e0d1SMaxime Ripard if (IS_ERR(tcon->clk)) { 3259026e0d1SMaxime Ripard dev_err(dev, "Couldn't get the TCON bus clock\n"); 3269026e0d1SMaxime Ripard return PTR_ERR(tcon->clk); 3279026e0d1SMaxime Ripard } 3289026e0d1SMaxime Ripard clk_prepare_enable(tcon->clk); 3299026e0d1SMaxime Ripard 3309026e0d1SMaxime Ripard tcon->sclk0 = devm_clk_get(dev, "tcon-ch0"); 3319026e0d1SMaxime Ripard if (IS_ERR(tcon->sclk0)) { 3329026e0d1SMaxime Ripard dev_err(dev, "Couldn't get the TCON channel 0 clock\n"); 3339026e0d1SMaxime Ripard return PTR_ERR(tcon->sclk0); 3349026e0d1SMaxime Ripard } 3359026e0d1SMaxime Ripard 33691ea2f29SChen-Yu Tsai if (tcon->quirks->has_channel_1) { 3379026e0d1SMaxime Ripard tcon->sclk1 = devm_clk_get(dev, "tcon-ch1"); 3389026e0d1SMaxime Ripard if (IS_ERR(tcon->sclk1)) { 3399026e0d1SMaxime Ripard dev_err(dev, "Couldn't get the TCON channel 1 clock\n"); 3409026e0d1SMaxime Ripard return PTR_ERR(tcon->sclk1); 3419026e0d1SMaxime Ripard } 3428e924047SMaxime Ripard } 3439026e0d1SMaxime Ripard 3444c7f16d1SChen-Yu Tsai return 0; 3459026e0d1SMaxime Ripard } 3469026e0d1SMaxime Ripard 3479026e0d1SMaxime Ripard static void sun4i_tcon_free_clocks(struct sun4i_tcon *tcon) 3489026e0d1SMaxime Ripard { 3499026e0d1SMaxime Ripard clk_disable_unprepare(tcon->clk); 3509026e0d1SMaxime Ripard } 3519026e0d1SMaxime Ripard 3529026e0d1SMaxime Ripard static int sun4i_tcon_init_irq(struct device *dev, 3539026e0d1SMaxime Ripard struct sun4i_tcon *tcon) 3549026e0d1SMaxime Ripard { 3559026e0d1SMaxime Ripard struct platform_device *pdev = to_platform_device(dev); 3569026e0d1SMaxime Ripard int irq, ret; 3579026e0d1SMaxime Ripard 3589026e0d1SMaxime Ripard irq = platform_get_irq(pdev, 0); 3599026e0d1SMaxime Ripard if (irq < 0) { 3609026e0d1SMaxime Ripard dev_err(dev, "Couldn't retrieve the TCON interrupt\n"); 3619026e0d1SMaxime Ripard return irq; 3629026e0d1SMaxime Ripard } 3639026e0d1SMaxime Ripard 3649026e0d1SMaxime Ripard ret = devm_request_irq(dev, irq, sun4i_tcon_handler, 0, 3659026e0d1SMaxime Ripard dev_name(dev), tcon); 3669026e0d1SMaxime Ripard if (ret) { 3679026e0d1SMaxime Ripard dev_err(dev, "Couldn't request the IRQ\n"); 3689026e0d1SMaxime Ripard return ret; 3699026e0d1SMaxime Ripard } 3709026e0d1SMaxime Ripard 3719026e0d1SMaxime Ripard return 0; 3729026e0d1SMaxime Ripard } 3739026e0d1SMaxime Ripard 3749026e0d1SMaxime Ripard static struct regmap_config sun4i_tcon_regmap_config = { 3759026e0d1SMaxime Ripard .reg_bits = 32, 3769026e0d1SMaxime Ripard .val_bits = 32, 3779026e0d1SMaxime Ripard .reg_stride = 4, 3789026e0d1SMaxime Ripard .max_register = 0x800, 3799026e0d1SMaxime Ripard }; 3809026e0d1SMaxime Ripard 3819026e0d1SMaxime Ripard static int sun4i_tcon_init_regmap(struct device *dev, 3829026e0d1SMaxime Ripard struct sun4i_tcon *tcon) 3839026e0d1SMaxime Ripard { 3849026e0d1SMaxime Ripard struct platform_device *pdev = to_platform_device(dev); 3859026e0d1SMaxime Ripard struct resource *res; 3869026e0d1SMaxime Ripard void __iomem *regs; 3879026e0d1SMaxime Ripard 3889026e0d1SMaxime Ripard res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 3899026e0d1SMaxime Ripard regs = devm_ioremap_resource(dev, res); 390af346f55SWei Yongjun if (IS_ERR(regs)) 3919026e0d1SMaxime Ripard return PTR_ERR(regs); 3929026e0d1SMaxime Ripard 3939026e0d1SMaxime Ripard tcon->regs = devm_regmap_init_mmio(dev, regs, 3949026e0d1SMaxime Ripard &sun4i_tcon_regmap_config); 3959026e0d1SMaxime Ripard if (IS_ERR(tcon->regs)) { 3969026e0d1SMaxime Ripard dev_err(dev, "Couldn't create the TCON regmap\n"); 3979026e0d1SMaxime Ripard return PTR_ERR(tcon->regs); 3989026e0d1SMaxime Ripard } 3999026e0d1SMaxime Ripard 4009026e0d1SMaxime Ripard /* Make sure the TCON is disabled and all IRQs are off */ 4019026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_GCTL_REG, 0); 4029026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_GINT0_REG, 0); 4039026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_GINT1_REG, 0); 4049026e0d1SMaxime Ripard 4059026e0d1SMaxime Ripard /* Disable IO lines and set them to tristate */ 4069026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, ~0); 4079026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_IO_TRI_REG, ~0); 4089026e0d1SMaxime Ripard 4099026e0d1SMaxime Ripard return 0; 4109026e0d1SMaxime Ripard } 4119026e0d1SMaxime Ripard 412b317fa3bSChen-Yu Tsai /* 413b317fa3bSChen-Yu Tsai * On SoCs with the old display pipeline design (Display Engine 1.0), 414b317fa3bSChen-Yu Tsai * the TCON is always tied to just one backend. Hence we can traverse 415b317fa3bSChen-Yu Tsai * the of_graph upwards to find the backend our tcon is connected to, 416b317fa3bSChen-Yu Tsai * and take its ID as our own. 417b317fa3bSChen-Yu Tsai * 418b317fa3bSChen-Yu Tsai * We can either identify backends from their compatible strings, which 419b317fa3bSChen-Yu Tsai * means maintaining a large list of them. Or, since the backend is 420b317fa3bSChen-Yu Tsai * registered and binded before the TCON, we can just go through the 421b317fa3bSChen-Yu Tsai * list of registered backends and compare the device node. 422b317fa3bSChen-Yu Tsai */ 423b317fa3bSChen-Yu Tsai static struct sun4i_backend *sun4i_tcon_find_backend(struct sun4i_drv *drv, 424b317fa3bSChen-Yu Tsai struct device_node *node) 425b317fa3bSChen-Yu Tsai { 426b317fa3bSChen-Yu Tsai struct device_node *port, *ep, *remote; 427b317fa3bSChen-Yu Tsai struct sun4i_backend *backend; 428b317fa3bSChen-Yu Tsai 429b317fa3bSChen-Yu Tsai port = of_graph_get_port_by_id(node, 0); 430b317fa3bSChen-Yu Tsai if (!port) 431b317fa3bSChen-Yu Tsai return ERR_PTR(-EINVAL); 432b317fa3bSChen-Yu Tsai 433b317fa3bSChen-Yu Tsai for_each_available_child_of_node(port, ep) { 434b317fa3bSChen-Yu Tsai remote = of_graph_get_remote_port_parent(ep); 435b317fa3bSChen-Yu Tsai if (!remote) 436b317fa3bSChen-Yu Tsai continue; 437b317fa3bSChen-Yu Tsai 438b317fa3bSChen-Yu Tsai /* does this node match any registered backends? */ 439b317fa3bSChen-Yu Tsai list_for_each_entry(backend, &drv->backend_list, list) { 440b317fa3bSChen-Yu Tsai if (remote == backend->node) { 441b317fa3bSChen-Yu Tsai of_node_put(remote); 442b317fa3bSChen-Yu Tsai of_node_put(port); 443b317fa3bSChen-Yu Tsai return backend; 444b317fa3bSChen-Yu Tsai } 445b317fa3bSChen-Yu Tsai } 446b317fa3bSChen-Yu Tsai 447b317fa3bSChen-Yu Tsai /* keep looking through upstream ports */ 448b317fa3bSChen-Yu Tsai backend = sun4i_tcon_find_backend(drv, remote); 449b317fa3bSChen-Yu Tsai if (!IS_ERR(backend)) { 450b317fa3bSChen-Yu Tsai of_node_put(remote); 451b317fa3bSChen-Yu Tsai of_node_put(port); 452b317fa3bSChen-Yu Tsai return backend; 453b317fa3bSChen-Yu Tsai } 454b317fa3bSChen-Yu Tsai } 455b317fa3bSChen-Yu Tsai 456b317fa3bSChen-Yu Tsai return ERR_PTR(-EINVAL); 457b317fa3bSChen-Yu Tsai } 458b317fa3bSChen-Yu Tsai 4599026e0d1SMaxime Ripard static int sun4i_tcon_bind(struct device *dev, struct device *master, 4609026e0d1SMaxime Ripard void *data) 4619026e0d1SMaxime Ripard { 4629026e0d1SMaxime Ripard struct drm_device *drm = data; 4639026e0d1SMaxime Ripard struct sun4i_drv *drv = drm->dev_private; 46480a58240SChen-Yu Tsai struct sun4i_backend *backend; 4659026e0d1SMaxime Ripard struct sun4i_tcon *tcon; 4669026e0d1SMaxime Ripard int ret; 4679026e0d1SMaxime Ripard 468b317fa3bSChen-Yu Tsai backend = sun4i_tcon_find_backend(drv, dev->of_node); 469b317fa3bSChen-Yu Tsai if (IS_ERR(backend)) { 470b317fa3bSChen-Yu Tsai dev_err(dev, "Couldn't find matching backend\n"); 47180a58240SChen-Yu Tsai return -EPROBE_DEFER; 472b317fa3bSChen-Yu Tsai } 47380a58240SChen-Yu Tsai 4749026e0d1SMaxime Ripard tcon = devm_kzalloc(dev, sizeof(*tcon), GFP_KERNEL); 4759026e0d1SMaxime Ripard if (!tcon) 4769026e0d1SMaxime Ripard return -ENOMEM; 4779026e0d1SMaxime Ripard dev_set_drvdata(dev, tcon); 4789026e0d1SMaxime Ripard tcon->drm = drm; 479ae558110SMaxime Ripard tcon->dev = dev; 480d281c862SChen-Yu Tsai tcon->id = backend->id; 48191ea2f29SChen-Yu Tsai tcon->quirks = of_device_get_match_data(dev); 4829026e0d1SMaxime Ripard 4839026e0d1SMaxime Ripard tcon->lcd_rst = devm_reset_control_get(dev, "lcd"); 4849026e0d1SMaxime Ripard if (IS_ERR(tcon->lcd_rst)) { 4859026e0d1SMaxime Ripard dev_err(dev, "Couldn't get our reset line\n"); 4869026e0d1SMaxime Ripard return PTR_ERR(tcon->lcd_rst); 4879026e0d1SMaxime Ripard } 4889026e0d1SMaxime Ripard 4899026e0d1SMaxime Ripard /* Make sure our TCON is reset */ 4909026e0d1SMaxime Ripard if (!reset_control_status(tcon->lcd_rst)) 4919026e0d1SMaxime Ripard reset_control_assert(tcon->lcd_rst); 4929026e0d1SMaxime Ripard 4939026e0d1SMaxime Ripard ret = reset_control_deassert(tcon->lcd_rst); 4949026e0d1SMaxime Ripard if (ret) { 4959026e0d1SMaxime Ripard dev_err(dev, "Couldn't deassert our reset line\n"); 4969026e0d1SMaxime Ripard return ret; 4979026e0d1SMaxime Ripard } 4989026e0d1SMaxime Ripard 4999026e0d1SMaxime Ripard ret = sun4i_tcon_init_clocks(dev, tcon); 5009026e0d1SMaxime Ripard if (ret) { 5019026e0d1SMaxime Ripard dev_err(dev, "Couldn't init our TCON clocks\n"); 5029026e0d1SMaxime Ripard goto err_assert_reset; 5039026e0d1SMaxime Ripard } 5049026e0d1SMaxime Ripard 5054c7f16d1SChen-Yu Tsai ret = sun4i_tcon_init_regmap(dev, tcon); 5069026e0d1SMaxime Ripard if (ret) { 5074c7f16d1SChen-Yu Tsai dev_err(dev, "Couldn't init our TCON regmap\n"); 5089026e0d1SMaxime Ripard goto err_free_clocks; 5099026e0d1SMaxime Ripard } 5109026e0d1SMaxime Ripard 5114c7f16d1SChen-Yu Tsai ret = sun4i_dclk_create(dev, tcon); 5124c7f16d1SChen-Yu Tsai if (ret) { 5134c7f16d1SChen-Yu Tsai dev_err(dev, "Couldn't create our TCON dot clock\n"); 5144c7f16d1SChen-Yu Tsai goto err_free_clocks; 5154c7f16d1SChen-Yu Tsai } 5164c7f16d1SChen-Yu Tsai 5179026e0d1SMaxime Ripard ret = sun4i_tcon_init_irq(dev, tcon); 5189026e0d1SMaxime Ripard if (ret) { 5199026e0d1SMaxime Ripard dev_err(dev, "Couldn't init our TCON interrupts\n"); 5204c7f16d1SChen-Yu Tsai goto err_free_dotclock; 5219026e0d1SMaxime Ripard } 5229026e0d1SMaxime Ripard 52380a58240SChen-Yu Tsai tcon->crtc = sun4i_crtc_init(drm, backend, tcon); 52446cce6daSChen-Yu Tsai if (IS_ERR(tcon->crtc)) { 52546cce6daSChen-Yu Tsai dev_err(dev, "Couldn't create our CRTC\n"); 52646cce6daSChen-Yu Tsai ret = PTR_ERR(tcon->crtc); 52746cce6daSChen-Yu Tsai goto err_free_clocks; 52846cce6daSChen-Yu Tsai } 52946cce6daSChen-Yu Tsai 530b9c8506cSChen-Yu Tsai ret = sun4i_rgb_init(drm, tcon); 53113fef095SChen-Yu Tsai if (ret < 0) 53213fef095SChen-Yu Tsai goto err_free_clocks; 53313fef095SChen-Yu Tsai 53480a58240SChen-Yu Tsai list_add_tail(&tcon->list, &drv->tcon_list); 53580a58240SChen-Yu Tsai 53613fef095SChen-Yu Tsai return 0; 5379026e0d1SMaxime Ripard 5384c7f16d1SChen-Yu Tsai err_free_dotclock: 5394c7f16d1SChen-Yu Tsai sun4i_dclk_free(tcon); 5409026e0d1SMaxime Ripard err_free_clocks: 5419026e0d1SMaxime Ripard sun4i_tcon_free_clocks(tcon); 5429026e0d1SMaxime Ripard err_assert_reset: 5439026e0d1SMaxime Ripard reset_control_assert(tcon->lcd_rst); 5449026e0d1SMaxime Ripard return ret; 5459026e0d1SMaxime Ripard } 5469026e0d1SMaxime Ripard 5479026e0d1SMaxime Ripard static void sun4i_tcon_unbind(struct device *dev, struct device *master, 5489026e0d1SMaxime Ripard void *data) 5499026e0d1SMaxime Ripard { 5509026e0d1SMaxime Ripard struct sun4i_tcon *tcon = dev_get_drvdata(dev); 5519026e0d1SMaxime Ripard 55280a58240SChen-Yu Tsai list_del(&tcon->list); 5534c7f16d1SChen-Yu Tsai sun4i_dclk_free(tcon); 5549026e0d1SMaxime Ripard sun4i_tcon_free_clocks(tcon); 5559026e0d1SMaxime Ripard } 5569026e0d1SMaxime Ripard 557dfeb693dSJulia Lawall static const struct component_ops sun4i_tcon_ops = { 5589026e0d1SMaxime Ripard .bind = sun4i_tcon_bind, 5599026e0d1SMaxime Ripard .unbind = sun4i_tcon_unbind, 5609026e0d1SMaxime Ripard }; 5619026e0d1SMaxime Ripard 5629026e0d1SMaxime Ripard static int sun4i_tcon_probe(struct platform_device *pdev) 5639026e0d1SMaxime Ripard { 56429e57fabSMaxime Ripard struct device_node *node = pdev->dev.of_node; 565894f5a9fSMaxime Ripard struct drm_bridge *bridge; 56629e57fabSMaxime Ripard struct drm_panel *panel; 567ebc94461SRob Herring int ret; 56829e57fabSMaxime Ripard 569ebc94461SRob Herring ret = drm_of_find_panel_or_bridge(node, 1, 0, &panel, &bridge); 570ebc94461SRob Herring if (ret == -EPROBE_DEFER) 571ebc94461SRob Herring return ret; 57229e57fabSMaxime Ripard 5739026e0d1SMaxime Ripard return component_add(&pdev->dev, &sun4i_tcon_ops); 5749026e0d1SMaxime Ripard } 5759026e0d1SMaxime Ripard 5769026e0d1SMaxime Ripard static int sun4i_tcon_remove(struct platform_device *pdev) 5779026e0d1SMaxime Ripard { 5789026e0d1SMaxime Ripard component_del(&pdev->dev, &sun4i_tcon_ops); 5799026e0d1SMaxime Ripard 5809026e0d1SMaxime Ripard return 0; 5819026e0d1SMaxime Ripard } 5829026e0d1SMaxime Ripard 58391ea2f29SChen-Yu Tsai static const struct sun4i_tcon_quirks sun5i_a13_quirks = { 58491ea2f29SChen-Yu Tsai .has_unknown_mux = true, 58591ea2f29SChen-Yu Tsai .has_channel_1 = true, 58691ea2f29SChen-Yu Tsai }; 58791ea2f29SChen-Yu Tsai 58893a5ec14SChen-Yu Tsai static const struct sun4i_tcon_quirks sun6i_a31_quirks = { 58993a5ec14SChen-Yu Tsai .has_channel_1 = true, 59093a5ec14SChen-Yu Tsai }; 59193a5ec14SChen-Yu Tsai 59293a5ec14SChen-Yu Tsai static const struct sun4i_tcon_quirks sun6i_a31s_quirks = { 59393a5ec14SChen-Yu Tsai .has_channel_1 = true, 59493a5ec14SChen-Yu Tsai }; 59593a5ec14SChen-Yu Tsai 59691ea2f29SChen-Yu Tsai static const struct sun4i_tcon_quirks sun8i_a33_quirks = { 59791ea2f29SChen-Yu Tsai /* nothing is supported */ 59891ea2f29SChen-Yu Tsai }; 59991ea2f29SChen-Yu Tsai 6009026e0d1SMaxime Ripard static const struct of_device_id sun4i_tcon_of_table[] = { 60191ea2f29SChen-Yu Tsai { .compatible = "allwinner,sun5i-a13-tcon", .data = &sun5i_a13_quirks }, 60293a5ec14SChen-Yu Tsai { .compatible = "allwinner,sun6i-a31-tcon", .data = &sun6i_a31_quirks }, 60393a5ec14SChen-Yu Tsai { .compatible = "allwinner,sun6i-a31s-tcon", .data = &sun6i_a31s_quirks }, 60491ea2f29SChen-Yu Tsai { .compatible = "allwinner,sun8i-a33-tcon", .data = &sun8i_a33_quirks }, 6059026e0d1SMaxime Ripard { } 6069026e0d1SMaxime Ripard }; 6079026e0d1SMaxime Ripard MODULE_DEVICE_TABLE(of, sun4i_tcon_of_table); 6089026e0d1SMaxime Ripard 6099026e0d1SMaxime Ripard static struct platform_driver sun4i_tcon_platform_driver = { 6109026e0d1SMaxime Ripard .probe = sun4i_tcon_probe, 6119026e0d1SMaxime Ripard .remove = sun4i_tcon_remove, 6129026e0d1SMaxime Ripard .driver = { 6139026e0d1SMaxime Ripard .name = "sun4i-tcon", 6149026e0d1SMaxime Ripard .of_match_table = sun4i_tcon_of_table, 6159026e0d1SMaxime Ripard }, 6169026e0d1SMaxime Ripard }; 6179026e0d1SMaxime Ripard module_platform_driver(sun4i_tcon_platform_driver); 6189026e0d1SMaxime Ripard 6199026e0d1SMaxime Ripard MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>"); 6209026e0d1SMaxime Ripard MODULE_DESCRIPTION("Allwinner A10 Timing Controller Driver"); 6219026e0d1SMaxime Ripard MODULE_LICENSE("GPL"); 622