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> 1829e57fabSMaxime Ripard #include <drm/drm_panel.h> 199026e0d1SMaxime Ripard 209026e0d1SMaxime Ripard #include <linux/component.h> 219026e0d1SMaxime Ripard #include <linux/ioport.h> 229026e0d1SMaxime Ripard #include <linux/of_address.h> 23*91ea2f29SChen-Yu Tsai #include <linux/of_device.h> 2429e57fabSMaxime Ripard #include <linux/of_graph.h> 259026e0d1SMaxime Ripard #include <linux/of_irq.h> 269026e0d1SMaxime Ripard #include <linux/regmap.h> 279026e0d1SMaxime Ripard #include <linux/reset.h> 289026e0d1SMaxime Ripard 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 66*91ea2f29SChen-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 84*91ea2f29SChen-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, 1469026e0d1SMaxime Ripard * but it really is the front 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, 1599026e0d1SMaxime Ripard * but it really is the front 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 206*91ea2f29SChen-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 */ 270*91ea2f29SChen-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; 2939026e0d1SMaxime Ripard struct sun4i_drv *drv = drm->dev_private; 2949026e0d1SMaxime Ripard struct sun4i_crtc *scrtc = drv->crtc; 2959026e0d1SMaxime Ripard unsigned int status; 2969026e0d1SMaxime Ripard 2979026e0d1SMaxime Ripard regmap_read(tcon->regs, SUN4I_TCON_GINT0_REG, &status); 2989026e0d1SMaxime Ripard 2999026e0d1SMaxime Ripard if (!(status & (SUN4I_TCON_GINT0_VBLANK_INT(0) | 3009026e0d1SMaxime Ripard SUN4I_TCON_GINT0_VBLANK_INT(1)))) 3019026e0d1SMaxime Ripard return IRQ_NONE; 3029026e0d1SMaxime Ripard 3039026e0d1SMaxime Ripard drm_crtc_handle_vblank(&scrtc->crtc); 3049026e0d1SMaxime Ripard sun4i_tcon_finish_page_flip(drm, scrtc); 3059026e0d1SMaxime Ripard 3069026e0d1SMaxime Ripard /* Acknowledge the interrupt */ 3079026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, 3089026e0d1SMaxime Ripard SUN4I_TCON_GINT0_VBLANK_INT(0) | 3099026e0d1SMaxime Ripard SUN4I_TCON_GINT0_VBLANK_INT(1), 3109026e0d1SMaxime Ripard 0); 3119026e0d1SMaxime Ripard 3129026e0d1SMaxime Ripard return IRQ_HANDLED; 3139026e0d1SMaxime Ripard } 3149026e0d1SMaxime Ripard 3159026e0d1SMaxime Ripard static int sun4i_tcon_init_clocks(struct device *dev, 3169026e0d1SMaxime Ripard struct sun4i_tcon *tcon) 3179026e0d1SMaxime Ripard { 3189026e0d1SMaxime Ripard tcon->clk = devm_clk_get(dev, "ahb"); 3199026e0d1SMaxime Ripard if (IS_ERR(tcon->clk)) { 3209026e0d1SMaxime Ripard dev_err(dev, "Couldn't get the TCON bus clock\n"); 3219026e0d1SMaxime Ripard return PTR_ERR(tcon->clk); 3229026e0d1SMaxime Ripard } 3239026e0d1SMaxime Ripard clk_prepare_enable(tcon->clk); 3249026e0d1SMaxime Ripard 3259026e0d1SMaxime Ripard tcon->sclk0 = devm_clk_get(dev, "tcon-ch0"); 3269026e0d1SMaxime Ripard if (IS_ERR(tcon->sclk0)) { 3279026e0d1SMaxime Ripard dev_err(dev, "Couldn't get the TCON channel 0 clock\n"); 3289026e0d1SMaxime Ripard return PTR_ERR(tcon->sclk0); 3299026e0d1SMaxime Ripard } 3309026e0d1SMaxime Ripard 331*91ea2f29SChen-Yu Tsai if (tcon->quirks->has_channel_1) { 3329026e0d1SMaxime Ripard tcon->sclk1 = devm_clk_get(dev, "tcon-ch1"); 3339026e0d1SMaxime Ripard if (IS_ERR(tcon->sclk1)) { 3349026e0d1SMaxime Ripard dev_err(dev, "Couldn't get the TCON channel 1 clock\n"); 3359026e0d1SMaxime Ripard return PTR_ERR(tcon->sclk1); 3369026e0d1SMaxime Ripard } 3378e924047SMaxime Ripard } 3389026e0d1SMaxime Ripard 3399026e0d1SMaxime Ripard return sun4i_dclk_create(dev, tcon); 3409026e0d1SMaxime Ripard } 3419026e0d1SMaxime Ripard 3429026e0d1SMaxime Ripard static void sun4i_tcon_free_clocks(struct sun4i_tcon *tcon) 3439026e0d1SMaxime Ripard { 3449026e0d1SMaxime Ripard sun4i_dclk_free(tcon); 3459026e0d1SMaxime Ripard clk_disable_unprepare(tcon->clk); 3469026e0d1SMaxime Ripard } 3479026e0d1SMaxime Ripard 3489026e0d1SMaxime Ripard static int sun4i_tcon_init_irq(struct device *dev, 3499026e0d1SMaxime Ripard struct sun4i_tcon *tcon) 3509026e0d1SMaxime Ripard { 3519026e0d1SMaxime Ripard struct platform_device *pdev = to_platform_device(dev); 3529026e0d1SMaxime Ripard int irq, ret; 3539026e0d1SMaxime Ripard 3549026e0d1SMaxime Ripard irq = platform_get_irq(pdev, 0); 3559026e0d1SMaxime Ripard if (irq < 0) { 3569026e0d1SMaxime Ripard dev_err(dev, "Couldn't retrieve the TCON interrupt\n"); 3579026e0d1SMaxime Ripard return irq; 3589026e0d1SMaxime Ripard } 3599026e0d1SMaxime Ripard 3609026e0d1SMaxime Ripard ret = devm_request_irq(dev, irq, sun4i_tcon_handler, 0, 3619026e0d1SMaxime Ripard dev_name(dev), tcon); 3629026e0d1SMaxime Ripard if (ret) { 3639026e0d1SMaxime Ripard dev_err(dev, "Couldn't request the IRQ\n"); 3649026e0d1SMaxime Ripard return ret; 3659026e0d1SMaxime Ripard } 3669026e0d1SMaxime Ripard 3679026e0d1SMaxime Ripard return 0; 3689026e0d1SMaxime Ripard } 3699026e0d1SMaxime Ripard 3709026e0d1SMaxime Ripard static struct regmap_config sun4i_tcon_regmap_config = { 3719026e0d1SMaxime Ripard .reg_bits = 32, 3729026e0d1SMaxime Ripard .val_bits = 32, 3739026e0d1SMaxime Ripard .reg_stride = 4, 3749026e0d1SMaxime Ripard .max_register = 0x800, 3759026e0d1SMaxime Ripard }; 3769026e0d1SMaxime Ripard 3779026e0d1SMaxime Ripard static int sun4i_tcon_init_regmap(struct device *dev, 3789026e0d1SMaxime Ripard struct sun4i_tcon *tcon) 3799026e0d1SMaxime Ripard { 3809026e0d1SMaxime Ripard struct platform_device *pdev = to_platform_device(dev); 3819026e0d1SMaxime Ripard struct resource *res; 3829026e0d1SMaxime Ripard void __iomem *regs; 3839026e0d1SMaxime Ripard 3849026e0d1SMaxime Ripard res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 3859026e0d1SMaxime Ripard regs = devm_ioremap_resource(dev, res); 386af346f55SWei Yongjun if (IS_ERR(regs)) 3879026e0d1SMaxime Ripard return PTR_ERR(regs); 3889026e0d1SMaxime Ripard 3899026e0d1SMaxime Ripard tcon->regs = devm_regmap_init_mmio(dev, regs, 3909026e0d1SMaxime Ripard &sun4i_tcon_regmap_config); 3919026e0d1SMaxime Ripard if (IS_ERR(tcon->regs)) { 3929026e0d1SMaxime Ripard dev_err(dev, "Couldn't create the TCON regmap\n"); 3939026e0d1SMaxime Ripard return PTR_ERR(tcon->regs); 3949026e0d1SMaxime Ripard } 3959026e0d1SMaxime Ripard 3969026e0d1SMaxime Ripard /* Make sure the TCON is disabled and all IRQs are off */ 3979026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_GCTL_REG, 0); 3989026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_GINT0_REG, 0); 3999026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_GINT1_REG, 0); 4009026e0d1SMaxime Ripard 4019026e0d1SMaxime Ripard /* Disable IO lines and set them to tristate */ 4029026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, ~0); 4039026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_IO_TRI_REG, ~0); 4049026e0d1SMaxime Ripard 4059026e0d1SMaxime Ripard return 0; 4069026e0d1SMaxime Ripard } 4079026e0d1SMaxime Ripard 408a8444c7eSMaxime Ripard struct drm_panel *sun4i_tcon_find_panel(struct device_node *node) 40929e57fabSMaxime Ripard { 41029e57fabSMaxime Ripard struct device_node *port, *remote, *child; 41129e57fabSMaxime Ripard struct device_node *end_node = NULL; 41229e57fabSMaxime Ripard 41329e57fabSMaxime Ripard /* Inputs are listed first, then outputs */ 41429e57fabSMaxime Ripard port = of_graph_get_port_by_id(node, 1); 41529e57fabSMaxime Ripard 41629e57fabSMaxime Ripard /* 41729e57fabSMaxime Ripard * Our first output is the RGB interface where the panel will 41829e57fabSMaxime Ripard * be connected. 41929e57fabSMaxime Ripard */ 42029e57fabSMaxime Ripard for_each_child_of_node(port, child) { 42129e57fabSMaxime Ripard u32 reg; 42229e57fabSMaxime Ripard 42329e57fabSMaxime Ripard of_property_read_u32(child, "reg", ®); 42429e57fabSMaxime Ripard if (reg == 0) 42529e57fabSMaxime Ripard end_node = child; 42629e57fabSMaxime Ripard } 42729e57fabSMaxime Ripard 42829e57fabSMaxime Ripard if (!end_node) { 42929e57fabSMaxime Ripard DRM_DEBUG_DRIVER("Missing panel endpoint\n"); 43029e57fabSMaxime Ripard return ERR_PTR(-ENODEV); 43129e57fabSMaxime Ripard } 43229e57fabSMaxime Ripard 43329e57fabSMaxime Ripard remote = of_graph_get_remote_port_parent(end_node); 43429e57fabSMaxime Ripard if (!remote) { 4350bbbb00bSMaxime Ripard DRM_DEBUG_DRIVER("Unable to parse remote node\n"); 43629e57fabSMaxime Ripard return ERR_PTR(-EINVAL); 43729e57fabSMaxime Ripard } 43829e57fabSMaxime Ripard 4390bbbb00bSMaxime Ripard return of_drm_find_panel(remote) ?: ERR_PTR(-EPROBE_DEFER); 44029e57fabSMaxime Ripard } 44129e57fabSMaxime Ripard 442894f5a9fSMaxime Ripard struct drm_bridge *sun4i_tcon_find_bridge(struct device_node *node) 443894f5a9fSMaxime Ripard { 444894f5a9fSMaxime Ripard struct device_node *port, *remote, *child; 445894f5a9fSMaxime Ripard struct device_node *end_node = NULL; 446894f5a9fSMaxime Ripard 447894f5a9fSMaxime Ripard /* Inputs are listed first, then outputs */ 448894f5a9fSMaxime Ripard port = of_graph_get_port_by_id(node, 1); 449894f5a9fSMaxime Ripard 450894f5a9fSMaxime Ripard /* 451894f5a9fSMaxime Ripard * Our first output is the RGB interface where the panel will 452894f5a9fSMaxime Ripard * be connected. 453894f5a9fSMaxime Ripard */ 454894f5a9fSMaxime Ripard for_each_child_of_node(port, child) { 455894f5a9fSMaxime Ripard u32 reg; 456894f5a9fSMaxime Ripard 457894f5a9fSMaxime Ripard of_property_read_u32(child, "reg", ®); 458894f5a9fSMaxime Ripard if (reg == 0) 459894f5a9fSMaxime Ripard end_node = child; 460894f5a9fSMaxime Ripard } 461894f5a9fSMaxime Ripard 462894f5a9fSMaxime Ripard if (!end_node) { 463894f5a9fSMaxime Ripard DRM_DEBUG_DRIVER("Missing bridge endpoint\n"); 464894f5a9fSMaxime Ripard return ERR_PTR(-ENODEV); 465894f5a9fSMaxime Ripard } 466894f5a9fSMaxime Ripard 467894f5a9fSMaxime Ripard remote = of_graph_get_remote_port_parent(end_node); 468894f5a9fSMaxime Ripard if (!remote) { 469894f5a9fSMaxime Ripard DRM_DEBUG_DRIVER("Enable to parse remote node\n"); 470894f5a9fSMaxime Ripard return ERR_PTR(-EINVAL); 471894f5a9fSMaxime Ripard } 472894f5a9fSMaxime Ripard 473894f5a9fSMaxime Ripard return of_drm_find_bridge(remote) ?: ERR_PTR(-EPROBE_DEFER); 474894f5a9fSMaxime Ripard } 475894f5a9fSMaxime Ripard 4769026e0d1SMaxime Ripard static int sun4i_tcon_bind(struct device *dev, struct device *master, 4779026e0d1SMaxime Ripard void *data) 4789026e0d1SMaxime Ripard { 4799026e0d1SMaxime Ripard struct drm_device *drm = data; 4809026e0d1SMaxime Ripard struct sun4i_drv *drv = drm->dev_private; 4819026e0d1SMaxime Ripard struct sun4i_tcon *tcon; 4829026e0d1SMaxime Ripard int ret; 4839026e0d1SMaxime Ripard 4849026e0d1SMaxime Ripard tcon = devm_kzalloc(dev, sizeof(*tcon), GFP_KERNEL); 4859026e0d1SMaxime Ripard if (!tcon) 4869026e0d1SMaxime Ripard return -ENOMEM; 4879026e0d1SMaxime Ripard dev_set_drvdata(dev, tcon); 4889026e0d1SMaxime Ripard drv->tcon = tcon; 4899026e0d1SMaxime Ripard tcon->drm = drm; 490ae558110SMaxime Ripard tcon->dev = dev; 491*91ea2f29SChen-Yu Tsai tcon->quirks = of_device_get_match_data(dev); 4929026e0d1SMaxime Ripard 4939026e0d1SMaxime Ripard tcon->lcd_rst = devm_reset_control_get(dev, "lcd"); 4949026e0d1SMaxime Ripard if (IS_ERR(tcon->lcd_rst)) { 4959026e0d1SMaxime Ripard dev_err(dev, "Couldn't get our reset line\n"); 4969026e0d1SMaxime Ripard return PTR_ERR(tcon->lcd_rst); 4979026e0d1SMaxime Ripard } 4989026e0d1SMaxime Ripard 4999026e0d1SMaxime Ripard /* Make sure our TCON is reset */ 5009026e0d1SMaxime Ripard if (!reset_control_status(tcon->lcd_rst)) 5019026e0d1SMaxime Ripard reset_control_assert(tcon->lcd_rst); 5029026e0d1SMaxime Ripard 5039026e0d1SMaxime Ripard ret = reset_control_deassert(tcon->lcd_rst); 5049026e0d1SMaxime Ripard if (ret) { 5059026e0d1SMaxime Ripard dev_err(dev, "Couldn't deassert our reset line\n"); 5069026e0d1SMaxime Ripard return ret; 5079026e0d1SMaxime Ripard } 5089026e0d1SMaxime Ripard 5099026e0d1SMaxime Ripard ret = sun4i_tcon_init_regmap(dev, tcon); 5109026e0d1SMaxime Ripard if (ret) { 5119026e0d1SMaxime Ripard dev_err(dev, "Couldn't init our TCON regmap\n"); 5129026e0d1SMaxime Ripard goto err_assert_reset; 5139026e0d1SMaxime Ripard } 5149026e0d1SMaxime Ripard 5159026e0d1SMaxime Ripard ret = sun4i_tcon_init_clocks(dev, tcon); 5169026e0d1SMaxime Ripard if (ret) { 5179026e0d1SMaxime Ripard dev_err(dev, "Couldn't init our TCON clocks\n"); 5189026e0d1SMaxime Ripard goto err_assert_reset; 5199026e0d1SMaxime Ripard } 5209026e0d1SMaxime Ripard 5219026e0d1SMaxime Ripard ret = sun4i_tcon_init_irq(dev, tcon); 5229026e0d1SMaxime Ripard if (ret) { 5239026e0d1SMaxime Ripard dev_err(dev, "Couldn't init our TCON interrupts\n"); 5249026e0d1SMaxime Ripard goto err_free_clocks; 5259026e0d1SMaxime Ripard } 5269026e0d1SMaxime Ripard 52713fef095SChen-Yu Tsai ret = sun4i_rgb_init(drm); 52813fef095SChen-Yu Tsai if (ret < 0) 52913fef095SChen-Yu Tsai goto err_free_clocks; 53013fef095SChen-Yu Tsai 53113fef095SChen-Yu Tsai return 0; 5329026e0d1SMaxime Ripard 5339026e0d1SMaxime Ripard err_free_clocks: 5349026e0d1SMaxime Ripard sun4i_tcon_free_clocks(tcon); 5359026e0d1SMaxime Ripard err_assert_reset: 5369026e0d1SMaxime Ripard reset_control_assert(tcon->lcd_rst); 5379026e0d1SMaxime Ripard return ret; 5389026e0d1SMaxime Ripard } 5399026e0d1SMaxime Ripard 5409026e0d1SMaxime Ripard static void sun4i_tcon_unbind(struct device *dev, struct device *master, 5419026e0d1SMaxime Ripard void *data) 5429026e0d1SMaxime Ripard { 5439026e0d1SMaxime Ripard struct sun4i_tcon *tcon = dev_get_drvdata(dev); 5449026e0d1SMaxime Ripard 5459026e0d1SMaxime Ripard sun4i_tcon_free_clocks(tcon); 5469026e0d1SMaxime Ripard } 5479026e0d1SMaxime Ripard 5489026e0d1SMaxime Ripard static struct component_ops sun4i_tcon_ops = { 5499026e0d1SMaxime Ripard .bind = sun4i_tcon_bind, 5509026e0d1SMaxime Ripard .unbind = sun4i_tcon_unbind, 5519026e0d1SMaxime Ripard }; 5529026e0d1SMaxime Ripard 5539026e0d1SMaxime Ripard static int sun4i_tcon_probe(struct platform_device *pdev) 5549026e0d1SMaxime Ripard { 55529e57fabSMaxime Ripard struct device_node *node = pdev->dev.of_node; 556894f5a9fSMaxime Ripard struct drm_bridge *bridge; 55729e57fabSMaxime Ripard struct drm_panel *panel; 55829e57fabSMaxime Ripard 55929e57fabSMaxime Ripard /* 560894f5a9fSMaxime Ripard * Neither the bridge or the panel is ready. 56129e57fabSMaxime Ripard * Defer the probe. 56229e57fabSMaxime Ripard */ 56329e57fabSMaxime Ripard panel = sun4i_tcon_find_panel(node); 564894f5a9fSMaxime Ripard bridge = sun4i_tcon_find_bridge(node); 5650bbbb00bSMaxime Ripard 56629e57fabSMaxime Ripard /* 56729e57fabSMaxime Ripard * If we don't have a panel endpoint, just go on 56829e57fabSMaxime Ripard */ 569894f5a9fSMaxime Ripard if ((PTR_ERR(panel) == -EPROBE_DEFER) && 570894f5a9fSMaxime Ripard (PTR_ERR(bridge) == -EPROBE_DEFER)) { 571894f5a9fSMaxime Ripard DRM_DEBUG_DRIVER("Still waiting for our panel/bridge. Deferring...\n"); 57229e57fabSMaxime Ripard return -EPROBE_DEFER; 57329e57fabSMaxime Ripard } 57429e57fabSMaxime Ripard 5759026e0d1SMaxime Ripard return component_add(&pdev->dev, &sun4i_tcon_ops); 5769026e0d1SMaxime Ripard } 5779026e0d1SMaxime Ripard 5789026e0d1SMaxime Ripard static int sun4i_tcon_remove(struct platform_device *pdev) 5799026e0d1SMaxime Ripard { 5809026e0d1SMaxime Ripard component_del(&pdev->dev, &sun4i_tcon_ops); 5819026e0d1SMaxime Ripard 5829026e0d1SMaxime Ripard return 0; 5839026e0d1SMaxime Ripard } 5849026e0d1SMaxime Ripard 585*91ea2f29SChen-Yu Tsai static const struct sun4i_tcon_quirks sun5i_a13_quirks = { 586*91ea2f29SChen-Yu Tsai .has_unknown_mux = true, 587*91ea2f29SChen-Yu Tsai .has_channel_1 = true, 588*91ea2f29SChen-Yu Tsai }; 589*91ea2f29SChen-Yu Tsai 590*91ea2f29SChen-Yu Tsai static const struct sun4i_tcon_quirks sun8i_a33_quirks = { 591*91ea2f29SChen-Yu Tsai /* nothing is supported */ 592*91ea2f29SChen-Yu Tsai }; 593*91ea2f29SChen-Yu Tsai 5949026e0d1SMaxime Ripard static const struct of_device_id sun4i_tcon_of_table[] = { 595*91ea2f29SChen-Yu Tsai { .compatible = "allwinner,sun5i-a13-tcon", .data = &sun5i_a13_quirks }, 596*91ea2f29SChen-Yu Tsai { .compatible = "allwinner,sun8i-a33-tcon", .data = &sun8i_a33_quirks }, 5979026e0d1SMaxime Ripard { } 5989026e0d1SMaxime Ripard }; 5999026e0d1SMaxime Ripard MODULE_DEVICE_TABLE(of, sun4i_tcon_of_table); 6009026e0d1SMaxime Ripard 6019026e0d1SMaxime Ripard static struct platform_driver sun4i_tcon_platform_driver = { 6029026e0d1SMaxime Ripard .probe = sun4i_tcon_probe, 6039026e0d1SMaxime Ripard .remove = sun4i_tcon_remove, 6049026e0d1SMaxime Ripard .driver = { 6059026e0d1SMaxime Ripard .name = "sun4i-tcon", 6069026e0d1SMaxime Ripard .of_match_table = sun4i_tcon_of_table, 6079026e0d1SMaxime Ripard }, 6089026e0d1SMaxime Ripard }; 6099026e0d1SMaxime Ripard module_platform_driver(sun4i_tcon_platform_driver); 6109026e0d1SMaxime Ripard 6119026e0d1SMaxime Ripard MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>"); 6129026e0d1SMaxime Ripard MODULE_DESCRIPTION("Allwinner A10 Timing Controller Driver"); 6139026e0d1SMaxime Ripard MODULE_LICENSE("GPL"); 614