19026e0d1SMaxime Ripard /* 29026e0d1SMaxime Ripard * Copyright (C) 2015 Free Electrons 39026e0d1SMaxime Ripard * Copyright (C) 2015 NextThing Co 49026e0d1SMaxime Ripard * 59026e0d1SMaxime Ripard * Maxime Ripard <maxime.ripard@free-electrons.com> 69026e0d1SMaxime Ripard * 79026e0d1SMaxime Ripard * This program is free software; you can redistribute it and/or 89026e0d1SMaxime Ripard * modify it under the terms of the GNU General Public License as 99026e0d1SMaxime Ripard * published by the Free Software Foundation; either version 2 of 109026e0d1SMaxime Ripard * the License, or (at your option) any later version. 119026e0d1SMaxime Ripard */ 129026e0d1SMaxime Ripard 139026e0d1SMaxime Ripard #include <drm/drmP.h> 149026e0d1SMaxime Ripard #include <drm/drm_atomic_helper.h> 159026e0d1SMaxime Ripard #include <drm/drm_crtc.h> 169026e0d1SMaxime Ripard #include <drm/drm_crtc_helper.h> 17ad537fb2SChen-Yu Tsai #include <drm/drm_encoder.h> 189026e0d1SMaxime Ripard #include <drm/drm_modes.h> 19ebc94461SRob Herring #include <drm/drm_of.h> 202c17a436SGiulio Benetti #include <drm/drm_panel.h> 219026e0d1SMaxime Ripard 22ad537fb2SChen-Yu Tsai #include <uapi/drm/drm_mode.h> 23ad537fb2SChen-Yu Tsai 249026e0d1SMaxime Ripard #include <linux/component.h> 259026e0d1SMaxime Ripard #include <linux/ioport.h> 269026e0d1SMaxime Ripard #include <linux/of_address.h> 2791ea2f29SChen-Yu Tsai #include <linux/of_device.h> 289026e0d1SMaxime Ripard #include <linux/of_irq.h> 299026e0d1SMaxime Ripard #include <linux/regmap.h> 309026e0d1SMaxime Ripard #include <linux/reset.h> 319026e0d1SMaxime Ripard 329026e0d1SMaxime Ripard #include "sun4i_crtc.h" 339026e0d1SMaxime Ripard #include "sun4i_dotclock.h" 349026e0d1SMaxime Ripard #include "sun4i_drv.h" 35a0c1214eSMaxime Ripard #include "sun4i_lvds.h" 3629e57fabSMaxime Ripard #include "sun4i_rgb.h" 379026e0d1SMaxime Ripard #include "sun4i_tcon.h" 3887969338SIcenowy Zheng #include "sunxi_engine.h" 399026e0d1SMaxime Ripard 40a0c1214eSMaxime Ripard static struct drm_connector *sun4i_tcon_get_connector(const struct drm_encoder *encoder) 41a0c1214eSMaxime Ripard { 42a0c1214eSMaxime Ripard struct drm_connector *connector; 43a0c1214eSMaxime Ripard struct drm_connector_list_iter iter; 44a0c1214eSMaxime Ripard 45a0c1214eSMaxime Ripard drm_connector_list_iter_begin(encoder->dev, &iter); 46a0c1214eSMaxime Ripard drm_for_each_connector_iter(connector, &iter) 47a0c1214eSMaxime Ripard if (connector->encoder == encoder) { 48a0c1214eSMaxime Ripard drm_connector_list_iter_end(&iter); 49a0c1214eSMaxime Ripard return connector; 50a0c1214eSMaxime Ripard } 51a0c1214eSMaxime Ripard drm_connector_list_iter_end(&iter); 52a0c1214eSMaxime Ripard 53a0c1214eSMaxime Ripard return NULL; 54a0c1214eSMaxime Ripard } 55a0c1214eSMaxime Ripard 56a0c1214eSMaxime Ripard static int sun4i_tcon_get_pixel_depth(const struct drm_encoder *encoder) 57a0c1214eSMaxime Ripard { 58a0c1214eSMaxime Ripard struct drm_connector *connector; 59a0c1214eSMaxime Ripard struct drm_display_info *info; 60a0c1214eSMaxime Ripard 61a0c1214eSMaxime Ripard connector = sun4i_tcon_get_connector(encoder); 62a0c1214eSMaxime Ripard if (!connector) 63a0c1214eSMaxime Ripard return -EINVAL; 64a0c1214eSMaxime Ripard 65a0c1214eSMaxime Ripard info = &connector->display_info; 66a0c1214eSMaxime Ripard if (info->num_bus_formats != 1) 67a0c1214eSMaxime Ripard return -EINVAL; 68a0c1214eSMaxime Ripard 69a0c1214eSMaxime Ripard switch (info->bus_formats[0]) { 70a0c1214eSMaxime Ripard case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG: 71a0c1214eSMaxime Ripard return 18; 72a0c1214eSMaxime Ripard 73a0c1214eSMaxime Ripard case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA: 74a0c1214eSMaxime Ripard case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG: 75a0c1214eSMaxime Ripard return 24; 76a0c1214eSMaxime Ripard } 77a0c1214eSMaxime Ripard 78a0c1214eSMaxime Ripard return -EINVAL; 79a0c1214eSMaxime Ripard } 80a0c1214eSMaxime Ripard 8145e88f99SMaxime Ripard static void sun4i_tcon_channel_set_status(struct sun4i_tcon *tcon, int channel, 8245e88f99SMaxime Ripard bool enabled) 839026e0d1SMaxime Ripard { 8445e88f99SMaxime Ripard struct clk *clk; 859026e0d1SMaxime Ripard 8645e88f99SMaxime Ripard switch (channel) { 8745e88f99SMaxime Ripard case 0: 8834d698f6SJernej Skrabec WARN_ON(!tcon->quirks->has_channel_0); 899026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 909026e0d1SMaxime Ripard SUN4I_TCON0_CTL_TCON_ENABLE, 9145e88f99SMaxime Ripard enabled ? SUN4I_TCON0_CTL_TCON_ENABLE : 0); 9245e88f99SMaxime Ripard clk = tcon->dclk; 9345e88f99SMaxime Ripard break; 9445e88f99SMaxime Ripard case 1: 9591ea2f29SChen-Yu Tsai WARN_ON(!tcon->quirks->has_channel_1); 969026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 979026e0d1SMaxime Ripard SUN4I_TCON1_CTL_TCON_ENABLE, 9845e88f99SMaxime Ripard enabled ? SUN4I_TCON1_CTL_TCON_ENABLE : 0); 9945e88f99SMaxime Ripard clk = tcon->sclk1; 10045e88f99SMaxime Ripard break; 10145e88f99SMaxime Ripard default: 10245e88f99SMaxime Ripard DRM_WARN("Unknown channel... doing nothing\n"); 10345e88f99SMaxime Ripard return; 1049026e0d1SMaxime Ripard } 10545e88f99SMaxime Ripard 106f3e5feebSJernej Skrabec if (enabled) { 10745e88f99SMaxime Ripard clk_prepare_enable(clk); 1087035046dSOndrej Jirman clk_rate_exclusive_get(clk); 109f3e5feebSJernej Skrabec } else { 110f3e5feebSJernej Skrabec clk_rate_exclusive_put(clk); 11145e88f99SMaxime Ripard clk_disable_unprepare(clk); 11245e88f99SMaxime Ripard } 113f3e5feebSJernej Skrabec } 11445e88f99SMaxime Ripard 115a0c1214eSMaxime Ripard static void sun4i_tcon_lvds_set_status(struct sun4i_tcon *tcon, 116a0c1214eSMaxime Ripard const struct drm_encoder *encoder, 117a0c1214eSMaxime Ripard bool enabled) 118a0c1214eSMaxime Ripard { 119a0c1214eSMaxime Ripard if (enabled) { 120a0c1214eSMaxime Ripard u8 val; 121a0c1214eSMaxime Ripard 122a0c1214eSMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_IF_REG, 123a0c1214eSMaxime Ripard SUN4I_TCON0_LVDS_IF_EN, 124a0c1214eSMaxime Ripard SUN4I_TCON0_LVDS_IF_EN); 125a0c1214eSMaxime Ripard 126a0c1214eSMaxime Ripard /* 127a0c1214eSMaxime Ripard * As their name suggest, these values only apply to the A31 128a0c1214eSMaxime Ripard * and later SoCs. We'll have to rework this when merging 129a0c1214eSMaxime Ripard * support for the older SoCs. 130a0c1214eSMaxime Ripard */ 131a0c1214eSMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG, 132a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_C(2) | 133a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_V(3) | 134a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_PD(2) | 135a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_EN_LDO); 136a0c1214eSMaxime Ripard udelay(2); 137a0c1214eSMaxime Ripard 138a0c1214eSMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG, 139a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_EN_MB, 140a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_EN_MB); 141a0c1214eSMaxime Ripard udelay(2); 142a0c1214eSMaxime Ripard 143a0c1214eSMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG, 144a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_EN_DRVC, 145a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_EN_DRVC); 146a0c1214eSMaxime Ripard 147a0c1214eSMaxime Ripard if (sun4i_tcon_get_pixel_depth(encoder) == 18) 148a0c1214eSMaxime Ripard val = 7; 149a0c1214eSMaxime Ripard else 150a0c1214eSMaxime Ripard val = 0xf; 151a0c1214eSMaxime Ripard 152a0c1214eSMaxime Ripard regmap_write_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG, 153a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_EN_DRVD(0xf), 154a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_EN_DRVD(val)); 155a0c1214eSMaxime Ripard } else { 156a0c1214eSMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_IF_REG, 157a0c1214eSMaxime Ripard SUN4I_TCON0_LVDS_IF_EN, 0); 158a0c1214eSMaxime Ripard } 159a0c1214eSMaxime Ripard } 160a0c1214eSMaxime Ripard 16145e88f99SMaxime Ripard void sun4i_tcon_set_status(struct sun4i_tcon *tcon, 16245e88f99SMaxime Ripard const struct drm_encoder *encoder, 16345e88f99SMaxime Ripard bool enabled) 16445e88f99SMaxime Ripard { 165a0c1214eSMaxime Ripard bool is_lvds = false; 16645e88f99SMaxime Ripard int channel; 16745e88f99SMaxime Ripard 16845e88f99SMaxime Ripard switch (encoder->encoder_type) { 169a0c1214eSMaxime Ripard case DRM_MODE_ENCODER_LVDS: 170a0c1214eSMaxime Ripard is_lvds = true; 171a0c1214eSMaxime Ripard /* Fallthrough */ 17245e88f99SMaxime Ripard case DRM_MODE_ENCODER_NONE: 17345e88f99SMaxime Ripard channel = 0; 17445e88f99SMaxime Ripard break; 17545e88f99SMaxime Ripard case DRM_MODE_ENCODER_TMDS: 17645e88f99SMaxime Ripard case DRM_MODE_ENCODER_TVDAC: 17745e88f99SMaxime Ripard channel = 1; 17845e88f99SMaxime Ripard break; 17945e88f99SMaxime Ripard default: 18045e88f99SMaxime Ripard DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n"); 18145e88f99SMaxime Ripard return; 18245e88f99SMaxime Ripard } 18345e88f99SMaxime Ripard 184a0c1214eSMaxime Ripard if (is_lvds && !enabled) 185a0c1214eSMaxime Ripard sun4i_tcon_lvds_set_status(tcon, encoder, false); 186a0c1214eSMaxime Ripard 18745e88f99SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 18845e88f99SMaxime Ripard SUN4I_TCON_GCTL_TCON_ENABLE, 18945e88f99SMaxime Ripard enabled ? SUN4I_TCON_GCTL_TCON_ENABLE : 0); 19045e88f99SMaxime Ripard 191a0c1214eSMaxime Ripard if (is_lvds && enabled) 192a0c1214eSMaxime Ripard sun4i_tcon_lvds_set_status(tcon, encoder, true); 193a0c1214eSMaxime Ripard 19445e88f99SMaxime Ripard sun4i_tcon_channel_set_status(tcon, channel, enabled); 19545e88f99SMaxime Ripard } 1969026e0d1SMaxime Ripard 1979026e0d1SMaxime Ripard void sun4i_tcon_enable_vblank(struct sun4i_tcon *tcon, bool enable) 1989026e0d1SMaxime Ripard { 1999026e0d1SMaxime Ripard u32 mask, val = 0; 2009026e0d1SMaxime Ripard 2019026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("%sabling VBLANK interrupt\n", enable ? "En" : "Dis"); 2029026e0d1SMaxime Ripard 2039026e0d1SMaxime Ripard mask = SUN4I_TCON_GINT0_VBLANK_ENABLE(0) | 204*a493ceaeSMaxime Ripard SUN4I_TCON_GINT0_VBLANK_ENABLE(1) | 205*a493ceaeSMaxime Ripard SUN4I_TCON_GINT0_TCON0_TRI_FINISH_ENABLE; 2069026e0d1SMaxime Ripard 2079026e0d1SMaxime Ripard if (enable) 2089026e0d1SMaxime Ripard val = mask; 2099026e0d1SMaxime Ripard 2109026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, mask, val); 2119026e0d1SMaxime Ripard } 2129026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_enable_vblank); 2139026e0d1SMaxime Ripard 21467e32645SChen-Yu Tsai /* 21567e32645SChen-Yu Tsai * This function is a helper for TCON output muxing. The TCON output 21667e32645SChen-Yu Tsai * muxing control register in earlier SoCs (without the TCON TOP block) 21767e32645SChen-Yu Tsai * are located in TCON0. This helper returns a pointer to TCON0's 21867e32645SChen-Yu Tsai * sun4i_tcon structure, or NULL if not found. 21967e32645SChen-Yu Tsai */ 22067e32645SChen-Yu Tsai static struct sun4i_tcon *sun4i_get_tcon0(struct drm_device *drm) 22167e32645SChen-Yu Tsai { 22267e32645SChen-Yu Tsai struct sun4i_drv *drv = drm->dev_private; 22367e32645SChen-Yu Tsai struct sun4i_tcon *tcon; 22467e32645SChen-Yu Tsai 22567e32645SChen-Yu Tsai list_for_each_entry(tcon, &drv->tcon_list, list) 22667e32645SChen-Yu Tsai if (tcon->id == 0) 22767e32645SChen-Yu Tsai return tcon; 22867e32645SChen-Yu Tsai 22967e32645SChen-Yu Tsai dev_warn(drm->dev, 23067e32645SChen-Yu Tsai "TCON0 not found, display output muxing may not work\n"); 23167e32645SChen-Yu Tsai 23267e32645SChen-Yu Tsai return NULL; 23367e32645SChen-Yu Tsai } 23467e32645SChen-Yu Tsai 235f8c73f4fSMaxime Ripard void sun4i_tcon_set_mux(struct sun4i_tcon *tcon, int channel, 236abcb8766SMaxime Ripard const struct drm_encoder *encoder) 237f8c73f4fSMaxime Ripard { 238ad537fb2SChen-Yu Tsai int ret = -ENOTSUPP; 239b7cb9b91SMaxime Ripard 240ad537fb2SChen-Yu Tsai if (tcon->quirks->set_mux) 241ad537fb2SChen-Yu Tsai ret = tcon->quirks->set_mux(tcon, encoder); 242f8c73f4fSMaxime Ripard 243ad537fb2SChen-Yu Tsai DRM_DEBUG_DRIVER("Muxing encoder %s to CRTC %s: %d\n", 244ad537fb2SChen-Yu Tsai encoder->name, encoder->crtc->name, ret); 245f8c73f4fSMaxime Ripard } 246f8c73f4fSMaxime Ripard 247961c645cSMaxime Ripard static int sun4i_tcon_get_clk_delay(const struct drm_display_mode *mode, 2489026e0d1SMaxime Ripard int channel) 2499026e0d1SMaxime Ripard { 2509026e0d1SMaxime Ripard int delay = mode->vtotal - mode->vdisplay; 2519026e0d1SMaxime Ripard 2529026e0d1SMaxime Ripard if (mode->flags & DRM_MODE_FLAG_INTERLACE) 2539026e0d1SMaxime Ripard delay /= 2; 2549026e0d1SMaxime Ripard 2559026e0d1SMaxime Ripard if (channel == 1) 2569026e0d1SMaxime Ripard delay -= 2; 2579026e0d1SMaxime Ripard 2589026e0d1SMaxime Ripard delay = min(delay, 30); 2599026e0d1SMaxime Ripard 2609026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("TCON %d clock delay %u\n", channel, delay); 2619026e0d1SMaxime Ripard 2629026e0d1SMaxime Ripard return delay; 2639026e0d1SMaxime Ripard } 2649026e0d1SMaxime Ripard 265ba19c537SMaxime Ripard static void sun4i_tcon0_mode_set_common(struct sun4i_tcon *tcon, 266ba19c537SMaxime Ripard const struct drm_display_mode *mode) 267ba19c537SMaxime Ripard { 268ba19c537SMaxime Ripard /* Configure the dot clock */ 269ba19c537SMaxime Ripard clk_set_rate(tcon->dclk, mode->crtc_clock * 1000); 270ba19c537SMaxime Ripard 271ba19c537SMaxime Ripard /* Set the resolution */ 272ba19c537SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC0_REG, 273ba19c537SMaxime Ripard SUN4I_TCON0_BASIC0_X(mode->crtc_hdisplay) | 274ba19c537SMaxime Ripard SUN4I_TCON0_BASIC0_Y(mode->crtc_vdisplay)); 275ba19c537SMaxime Ripard } 276ba19c537SMaxime Ripard 277a0c1214eSMaxime Ripard static void sun4i_tcon0_mode_set_lvds(struct sun4i_tcon *tcon, 278a0c1214eSMaxime Ripard const struct drm_encoder *encoder, 279a0c1214eSMaxime Ripard const struct drm_display_mode *mode) 280a0c1214eSMaxime Ripard { 281a0c1214eSMaxime Ripard unsigned int bp; 282a0c1214eSMaxime Ripard u8 clk_delay; 283a0c1214eSMaxime Ripard u32 reg, val = 0; 284a0c1214eSMaxime Ripard 28534d698f6SJernej Skrabec WARN_ON(!tcon->quirks->has_channel_0); 28634d698f6SJernej Skrabec 287a0c1214eSMaxime Ripard tcon->dclk_min_div = 7; 288a0c1214eSMaxime Ripard tcon->dclk_max_div = 7; 289a0c1214eSMaxime Ripard sun4i_tcon0_mode_set_common(tcon, mode); 290a0c1214eSMaxime Ripard 291a0c1214eSMaxime Ripard /* Adjust clock delay */ 292a0c1214eSMaxime Ripard clk_delay = sun4i_tcon_get_clk_delay(mode, 0); 293a0c1214eSMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 294a0c1214eSMaxime Ripard SUN4I_TCON0_CTL_CLK_DELAY_MASK, 295a0c1214eSMaxime Ripard SUN4I_TCON0_CTL_CLK_DELAY(clk_delay)); 296a0c1214eSMaxime Ripard 297a0c1214eSMaxime Ripard /* 298a0c1214eSMaxime Ripard * This is called a backporch in the register documentation, 299a0c1214eSMaxime Ripard * but it really is the back porch + hsync 300a0c1214eSMaxime Ripard */ 301a0c1214eSMaxime Ripard bp = mode->crtc_htotal - mode->crtc_hsync_start; 302a0c1214eSMaxime Ripard DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n", 303a0c1214eSMaxime Ripard mode->crtc_htotal, bp); 304a0c1214eSMaxime Ripard 305a0c1214eSMaxime Ripard /* Set horizontal display timings */ 306a0c1214eSMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG, 307a0c1214eSMaxime Ripard SUN4I_TCON0_BASIC1_H_TOTAL(mode->htotal) | 308a0c1214eSMaxime Ripard SUN4I_TCON0_BASIC1_H_BACKPORCH(bp)); 309a0c1214eSMaxime Ripard 310a0c1214eSMaxime Ripard /* 311a0c1214eSMaxime Ripard * This is called a backporch in the register documentation, 312a0c1214eSMaxime Ripard * but it really is the back porch + hsync 313a0c1214eSMaxime Ripard */ 314a0c1214eSMaxime Ripard bp = mode->crtc_vtotal - mode->crtc_vsync_start; 315a0c1214eSMaxime Ripard DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n", 316a0c1214eSMaxime Ripard mode->crtc_vtotal, bp); 317a0c1214eSMaxime Ripard 318a0c1214eSMaxime Ripard /* Set vertical display timings */ 319a0c1214eSMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG, 320a0c1214eSMaxime Ripard SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) | 321a0c1214eSMaxime Ripard SUN4I_TCON0_BASIC2_V_BACKPORCH(bp)); 322a0c1214eSMaxime Ripard 323a0c1214eSMaxime Ripard reg = SUN4I_TCON0_LVDS_IF_CLK_SEL_TCON0 | 324a0c1214eSMaxime Ripard SUN4I_TCON0_LVDS_IF_DATA_POL_NORMAL | 325a0c1214eSMaxime Ripard SUN4I_TCON0_LVDS_IF_CLK_POL_NORMAL; 326a0c1214eSMaxime Ripard if (sun4i_tcon_get_pixel_depth(encoder) == 24) 327a0c1214eSMaxime Ripard reg |= SUN4I_TCON0_LVDS_IF_BITWIDTH_24BITS; 328a0c1214eSMaxime Ripard else 329a0c1214eSMaxime Ripard reg |= SUN4I_TCON0_LVDS_IF_BITWIDTH_18BITS; 330a0c1214eSMaxime Ripard 331a0c1214eSMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_LVDS_IF_REG, reg); 332a0c1214eSMaxime Ripard 333a0c1214eSMaxime Ripard /* Setup the polarity of the various signals */ 334a0c1214eSMaxime Ripard if (!(mode->flags & DRM_MODE_FLAG_PHSYNC)) 335a0c1214eSMaxime Ripard val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE; 336a0c1214eSMaxime Ripard 337a0c1214eSMaxime Ripard if (!(mode->flags & DRM_MODE_FLAG_PVSYNC)) 338a0c1214eSMaxime Ripard val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE; 339a0c1214eSMaxime Ripard 340a0c1214eSMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_IO_POL_REG, val); 341a0c1214eSMaxime Ripard 342a0c1214eSMaxime Ripard /* Map output pins to channel 0 */ 343a0c1214eSMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 344a0c1214eSMaxime Ripard SUN4I_TCON_GCTL_IOMAP_MASK, 345a0c1214eSMaxime Ripard SUN4I_TCON_GCTL_IOMAP_TCON0); 34680b79e31SOndrej Jirman 34780b79e31SOndrej Jirman /* Enable the output on the pins */ 34880b79e31SOndrej Jirman regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0xe0000000); 349a0c1214eSMaxime Ripard } 350a0c1214eSMaxime Ripard 351ba19c537SMaxime Ripard static void sun4i_tcon0_mode_set_rgb(struct sun4i_tcon *tcon, 3525b8f0910SMaxime Ripard const struct drm_display_mode *mode) 3539026e0d1SMaxime Ripard { 3542c17a436SGiulio Benetti struct drm_panel *panel = tcon->panel; 3552c17a436SGiulio Benetti struct drm_connector *connector = panel->connector; 3562c17a436SGiulio Benetti struct drm_display_info display_info = connector->display_info; 3579026e0d1SMaxime Ripard unsigned int bp, hsync, vsync; 3589026e0d1SMaxime Ripard u8 clk_delay; 3599026e0d1SMaxime Ripard u32 val = 0; 3609026e0d1SMaxime Ripard 36134d698f6SJernej Skrabec WARN_ON(!tcon->quirks->has_channel_0); 36234d698f6SJernej Skrabec 363ec08d596SMaxime Ripard tcon->dclk_min_div = 6; 364ec08d596SMaxime Ripard tcon->dclk_max_div = 127; 365ba19c537SMaxime Ripard sun4i_tcon0_mode_set_common(tcon, mode); 36686cf6788SChen-Yu Tsai 3679026e0d1SMaxime Ripard /* Adjust clock delay */ 3689026e0d1SMaxime Ripard clk_delay = sun4i_tcon_get_clk_delay(mode, 0); 3699026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 3709026e0d1SMaxime Ripard SUN4I_TCON0_CTL_CLK_DELAY_MASK, 3719026e0d1SMaxime Ripard SUN4I_TCON0_CTL_CLK_DELAY(clk_delay)); 3729026e0d1SMaxime Ripard 3739026e0d1SMaxime Ripard /* 3749026e0d1SMaxime Ripard * This is called a backporch in the register documentation, 37523a1cb11SChen-Yu Tsai * but it really is the back porch + hsync 3769026e0d1SMaxime Ripard */ 3779026e0d1SMaxime Ripard bp = mode->crtc_htotal - mode->crtc_hsync_start; 3789026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n", 3799026e0d1SMaxime Ripard mode->crtc_htotal, bp); 3809026e0d1SMaxime Ripard 3819026e0d1SMaxime Ripard /* Set horizontal display timings */ 3829026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG, 3839026e0d1SMaxime Ripard SUN4I_TCON0_BASIC1_H_TOTAL(mode->crtc_htotal) | 3849026e0d1SMaxime Ripard SUN4I_TCON0_BASIC1_H_BACKPORCH(bp)); 3859026e0d1SMaxime Ripard 3869026e0d1SMaxime Ripard /* 3879026e0d1SMaxime Ripard * This is called a backporch in the register documentation, 38823a1cb11SChen-Yu Tsai * but it really is the back porch + hsync 3899026e0d1SMaxime Ripard */ 3909026e0d1SMaxime Ripard bp = mode->crtc_vtotal - mode->crtc_vsync_start; 3919026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n", 3929026e0d1SMaxime Ripard mode->crtc_vtotal, bp); 3939026e0d1SMaxime Ripard 3949026e0d1SMaxime Ripard /* Set vertical display timings */ 3959026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG, 396a88cbbd4SMaxime Ripard SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) | 3979026e0d1SMaxime Ripard SUN4I_TCON0_BASIC2_V_BACKPORCH(bp)); 3989026e0d1SMaxime Ripard 3999026e0d1SMaxime Ripard /* Set Hsync and Vsync length */ 4009026e0d1SMaxime Ripard hsync = mode->crtc_hsync_end - mode->crtc_hsync_start; 4019026e0d1SMaxime Ripard vsync = mode->crtc_vsync_end - mode->crtc_vsync_start; 4029026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync); 4039026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC3_REG, 4049026e0d1SMaxime Ripard SUN4I_TCON0_BASIC3_V_SYNC(vsync) | 4059026e0d1SMaxime Ripard SUN4I_TCON0_BASIC3_H_SYNC(hsync)); 4069026e0d1SMaxime Ripard 4079026e0d1SMaxime Ripard /* Setup the polarity of the various signals */ 408fa4127c5SGiulio Benetti if (mode->flags & DRM_MODE_FLAG_PHSYNC) 4099026e0d1SMaxime Ripard val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE; 4109026e0d1SMaxime Ripard 411fa4127c5SGiulio Benetti if (mode->flags & DRM_MODE_FLAG_PVSYNC) 4129026e0d1SMaxime Ripard val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE; 4139026e0d1SMaxime Ripard 4142c17a436SGiulio Benetti /* 4152c17a436SGiulio Benetti * On A20 and similar SoCs, the only way to achieve Positive Edge 4162c17a436SGiulio Benetti * (Rising Edge), is setting dclk clock phase to 2/3(240°). 4172c17a436SGiulio Benetti * By default TCON works in Negative Edge(Falling Edge), 4182c17a436SGiulio Benetti * this is why phase is set to 0 in that case. 4192c17a436SGiulio Benetti * Unfortunately there's no way to logically invert dclk through 4202c17a436SGiulio Benetti * IO_POL register. 4212c17a436SGiulio Benetti * The only acceptable way to work, triple checked with scope, 4222c17a436SGiulio Benetti * is using clock phase set to 0° for Negative Edge and set to 240° 4232c17a436SGiulio Benetti * for Positive Edge. 4242c17a436SGiulio Benetti * On A33 and similar SoCs there would be a 90° phase option, 4252c17a436SGiulio Benetti * but it divides also dclk by 2. 4262c17a436SGiulio Benetti * Following code is a way to avoid quirks all around TCON 4272c17a436SGiulio Benetti * and DOTCLOCK drivers. 4282c17a436SGiulio Benetti */ 4292c17a436SGiulio Benetti if (display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_POSEDGE) 4302c17a436SGiulio Benetti clk_set_phase(tcon->dclk, 240); 4312c17a436SGiulio Benetti 4322c17a436SGiulio Benetti if (display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_NEGEDGE) 4332c17a436SGiulio Benetti clk_set_phase(tcon->dclk, 0); 4342c17a436SGiulio Benetti 4359026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_IO_POL_REG, 4369026e0d1SMaxime Ripard SUN4I_TCON0_IO_POL_HSYNC_POSITIVE | SUN4I_TCON0_IO_POL_VSYNC_POSITIVE, 4379026e0d1SMaxime Ripard val); 4389026e0d1SMaxime Ripard 4399026e0d1SMaxime Ripard /* Map output pins to channel 0 */ 4409026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 4419026e0d1SMaxime Ripard SUN4I_TCON_GCTL_IOMAP_MASK, 4429026e0d1SMaxime Ripard SUN4I_TCON_GCTL_IOMAP_TCON0); 4439026e0d1SMaxime Ripard 4449026e0d1SMaxime Ripard /* Enable the output on the pins */ 4459026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0); 4469026e0d1SMaxime Ripard } 4479026e0d1SMaxime Ripard 4485b8f0910SMaxime Ripard static void sun4i_tcon1_mode_set(struct sun4i_tcon *tcon, 4495b8f0910SMaxime Ripard const struct drm_display_mode *mode) 4509026e0d1SMaxime Ripard { 451b8317a3dSMaxime Ripard unsigned int bp, hsync, vsync, vtotal; 4529026e0d1SMaxime Ripard u8 clk_delay; 4539026e0d1SMaxime Ripard u32 val; 4549026e0d1SMaxime Ripard 45591ea2f29SChen-Yu Tsai WARN_ON(!tcon->quirks->has_channel_1); 4568e924047SMaxime Ripard 45786cf6788SChen-Yu Tsai /* Configure the dot clock */ 45886cf6788SChen-Yu Tsai clk_set_rate(tcon->sclk1, mode->crtc_clock * 1000); 45986cf6788SChen-Yu Tsai 4609026e0d1SMaxime Ripard /* Adjust clock delay */ 4619026e0d1SMaxime Ripard clk_delay = sun4i_tcon_get_clk_delay(mode, 1); 4629026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 4639026e0d1SMaxime Ripard SUN4I_TCON1_CTL_CLK_DELAY_MASK, 4649026e0d1SMaxime Ripard SUN4I_TCON1_CTL_CLK_DELAY(clk_delay)); 4659026e0d1SMaxime Ripard 4669026e0d1SMaxime Ripard /* Set interlaced mode */ 4679026e0d1SMaxime Ripard if (mode->flags & DRM_MODE_FLAG_INTERLACE) 4689026e0d1SMaxime Ripard val = SUN4I_TCON1_CTL_INTERLACE_ENABLE; 4699026e0d1SMaxime Ripard else 4709026e0d1SMaxime Ripard val = 0; 4719026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 4729026e0d1SMaxime Ripard SUN4I_TCON1_CTL_INTERLACE_ENABLE, 4739026e0d1SMaxime Ripard val); 4749026e0d1SMaxime Ripard 4759026e0d1SMaxime Ripard /* Set the input resolution */ 4769026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC0_REG, 4779026e0d1SMaxime Ripard SUN4I_TCON1_BASIC0_X(mode->crtc_hdisplay) | 4789026e0d1SMaxime Ripard SUN4I_TCON1_BASIC0_Y(mode->crtc_vdisplay)); 4799026e0d1SMaxime Ripard 4809026e0d1SMaxime Ripard /* Set the upscaling resolution */ 4819026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC1_REG, 4829026e0d1SMaxime Ripard SUN4I_TCON1_BASIC1_X(mode->crtc_hdisplay) | 4839026e0d1SMaxime Ripard SUN4I_TCON1_BASIC1_Y(mode->crtc_vdisplay)); 4849026e0d1SMaxime Ripard 4859026e0d1SMaxime Ripard /* Set the output resolution */ 4869026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC2_REG, 4879026e0d1SMaxime Ripard SUN4I_TCON1_BASIC2_X(mode->crtc_hdisplay) | 4889026e0d1SMaxime Ripard SUN4I_TCON1_BASIC2_Y(mode->crtc_vdisplay)); 4899026e0d1SMaxime Ripard 4909026e0d1SMaxime Ripard /* Set horizontal display timings */ 4913cb2f46bSMaxime Ripard bp = mode->crtc_htotal - mode->crtc_hsync_start; 4929026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n", 4939026e0d1SMaxime Ripard mode->htotal, bp); 4949026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC3_REG, 4959026e0d1SMaxime Ripard SUN4I_TCON1_BASIC3_H_TOTAL(mode->crtc_htotal) | 4969026e0d1SMaxime Ripard SUN4I_TCON1_BASIC3_H_BACKPORCH(bp)); 4979026e0d1SMaxime Ripard 4983cb2f46bSMaxime Ripard bp = mode->crtc_vtotal - mode->crtc_vsync_start; 4999026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n", 500b8317a3dSMaxime Ripard mode->crtc_vtotal, bp); 501b8317a3dSMaxime Ripard 502b8317a3dSMaxime Ripard /* 503b8317a3dSMaxime Ripard * The vertical resolution needs to be doubled in all 504b8317a3dSMaxime Ripard * cases. We could use crtc_vtotal and always multiply by two, 505b8317a3dSMaxime Ripard * but that leads to a rounding error in interlace when vtotal 506b8317a3dSMaxime Ripard * is odd. 507b8317a3dSMaxime Ripard * 508b8317a3dSMaxime Ripard * This happens with TV's PAL for example, where vtotal will 509b8317a3dSMaxime Ripard * be 625, crtc_vtotal 312, and thus crtc_vtotal * 2 will be 510b8317a3dSMaxime Ripard * 624, which apparently confuses the hardware. 511b8317a3dSMaxime Ripard * 512b8317a3dSMaxime Ripard * To work around this, we will always use vtotal, and 513b8317a3dSMaxime Ripard * multiply by two only if we're not in interlace. 514b8317a3dSMaxime Ripard */ 515b8317a3dSMaxime Ripard vtotal = mode->vtotal; 516b8317a3dSMaxime Ripard if (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) 517b8317a3dSMaxime Ripard vtotal = vtotal * 2; 518b8317a3dSMaxime Ripard 519b8317a3dSMaxime Ripard /* Set vertical display timings */ 5209026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC4_REG, 521b8317a3dSMaxime Ripard SUN4I_TCON1_BASIC4_V_TOTAL(vtotal) | 5229026e0d1SMaxime Ripard SUN4I_TCON1_BASIC4_V_BACKPORCH(bp)); 5239026e0d1SMaxime Ripard 5249026e0d1SMaxime Ripard /* Set Hsync and Vsync length */ 5259026e0d1SMaxime Ripard hsync = mode->crtc_hsync_end - mode->crtc_hsync_start; 5269026e0d1SMaxime Ripard vsync = mode->crtc_vsync_end - mode->crtc_vsync_start; 5279026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync); 5289026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC5_REG, 5299026e0d1SMaxime Ripard SUN4I_TCON1_BASIC5_V_SYNC(vsync) | 5309026e0d1SMaxime Ripard SUN4I_TCON1_BASIC5_H_SYNC(hsync)); 5319026e0d1SMaxime Ripard 5329026e0d1SMaxime Ripard /* Map output pins to channel 1 */ 5339026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 5349026e0d1SMaxime Ripard SUN4I_TCON_GCTL_IOMAP_MASK, 5359026e0d1SMaxime Ripard SUN4I_TCON_GCTL_IOMAP_TCON1); 5369026e0d1SMaxime Ripard } 5375b8f0910SMaxime Ripard 5385b8f0910SMaxime Ripard void sun4i_tcon_mode_set(struct sun4i_tcon *tcon, 5395b8f0910SMaxime Ripard const struct drm_encoder *encoder, 5405b8f0910SMaxime Ripard const struct drm_display_mode *mode) 5415b8f0910SMaxime Ripard { 5425b8f0910SMaxime Ripard switch (encoder->encoder_type) { 543a0c1214eSMaxime Ripard case DRM_MODE_ENCODER_LVDS: 544a0c1214eSMaxime Ripard sun4i_tcon0_mode_set_lvds(tcon, encoder, mode); 545a0c1214eSMaxime Ripard break; 5465b8f0910SMaxime Ripard case DRM_MODE_ENCODER_NONE: 547ba19c537SMaxime Ripard sun4i_tcon0_mode_set_rgb(tcon, mode); 5485b8f0910SMaxime Ripard sun4i_tcon_set_mux(tcon, 0, encoder); 5495b8f0910SMaxime Ripard break; 5505b8f0910SMaxime Ripard case DRM_MODE_ENCODER_TVDAC: 5515b8f0910SMaxime Ripard case DRM_MODE_ENCODER_TMDS: 5525b8f0910SMaxime Ripard sun4i_tcon1_mode_set(tcon, mode); 5535b8f0910SMaxime Ripard sun4i_tcon_set_mux(tcon, 1, encoder); 5545b8f0910SMaxime Ripard break; 5555b8f0910SMaxime Ripard default: 5565b8f0910SMaxime Ripard DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n"); 5575b8f0910SMaxime Ripard } 5585b8f0910SMaxime Ripard } 5595b8f0910SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_mode_set); 5609026e0d1SMaxime Ripard 5619026e0d1SMaxime Ripard static void sun4i_tcon_finish_page_flip(struct drm_device *dev, 5629026e0d1SMaxime Ripard struct sun4i_crtc *scrtc) 5639026e0d1SMaxime Ripard { 5649026e0d1SMaxime Ripard unsigned long flags; 5659026e0d1SMaxime Ripard 5669026e0d1SMaxime Ripard spin_lock_irqsave(&dev->event_lock, flags); 5679026e0d1SMaxime Ripard if (scrtc->event) { 5689026e0d1SMaxime Ripard drm_crtc_send_vblank_event(&scrtc->crtc, scrtc->event); 5699026e0d1SMaxime Ripard drm_crtc_vblank_put(&scrtc->crtc); 5709026e0d1SMaxime Ripard scrtc->event = NULL; 5719026e0d1SMaxime Ripard } 5729026e0d1SMaxime Ripard spin_unlock_irqrestore(&dev->event_lock, flags); 5739026e0d1SMaxime Ripard } 5749026e0d1SMaxime Ripard 5759026e0d1SMaxime Ripard static irqreturn_t sun4i_tcon_handler(int irq, void *private) 5769026e0d1SMaxime Ripard { 5779026e0d1SMaxime Ripard struct sun4i_tcon *tcon = private; 5789026e0d1SMaxime Ripard struct drm_device *drm = tcon->drm; 57946cce6daSChen-Yu Tsai struct sun4i_crtc *scrtc = tcon->crtc; 5803004f75fSMaxime Ripard struct sunxi_engine *engine = scrtc->engine; 5819026e0d1SMaxime Ripard unsigned int status; 5829026e0d1SMaxime Ripard 5839026e0d1SMaxime Ripard regmap_read(tcon->regs, SUN4I_TCON_GINT0_REG, &status); 5849026e0d1SMaxime Ripard 5859026e0d1SMaxime Ripard if (!(status & (SUN4I_TCON_GINT0_VBLANK_INT(0) | 586*a493ceaeSMaxime Ripard SUN4I_TCON_GINT0_VBLANK_INT(1) | 587*a493ceaeSMaxime Ripard SUN4I_TCON_GINT0_TCON0_TRI_FINISH_INT))) 5889026e0d1SMaxime Ripard return IRQ_NONE; 5899026e0d1SMaxime Ripard 5909026e0d1SMaxime Ripard drm_crtc_handle_vblank(&scrtc->crtc); 5919026e0d1SMaxime Ripard sun4i_tcon_finish_page_flip(drm, scrtc); 5929026e0d1SMaxime Ripard 5939026e0d1SMaxime Ripard /* Acknowledge the interrupt */ 5949026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, 5959026e0d1SMaxime Ripard SUN4I_TCON_GINT0_VBLANK_INT(0) | 596*a493ceaeSMaxime Ripard SUN4I_TCON_GINT0_VBLANK_INT(1) | 597*a493ceaeSMaxime Ripard SUN4I_TCON_GINT0_TCON0_TRI_FINISH_INT, 5989026e0d1SMaxime Ripard 0); 5999026e0d1SMaxime Ripard 6003004f75fSMaxime Ripard if (engine->ops->vblank_quirk) 6013004f75fSMaxime Ripard engine->ops->vblank_quirk(engine); 6023004f75fSMaxime Ripard 6039026e0d1SMaxime Ripard return IRQ_HANDLED; 6049026e0d1SMaxime Ripard } 6059026e0d1SMaxime Ripard 6069026e0d1SMaxime Ripard static int sun4i_tcon_init_clocks(struct device *dev, 6079026e0d1SMaxime Ripard struct sun4i_tcon *tcon) 6089026e0d1SMaxime Ripard { 6099026e0d1SMaxime Ripard tcon->clk = devm_clk_get(dev, "ahb"); 6109026e0d1SMaxime Ripard if (IS_ERR(tcon->clk)) { 6119026e0d1SMaxime Ripard dev_err(dev, "Couldn't get the TCON bus clock\n"); 6129026e0d1SMaxime Ripard return PTR_ERR(tcon->clk); 6139026e0d1SMaxime Ripard } 6149026e0d1SMaxime Ripard clk_prepare_enable(tcon->clk); 6159026e0d1SMaxime Ripard 61634d698f6SJernej Skrabec if (tcon->quirks->has_channel_0) { 6179026e0d1SMaxime Ripard tcon->sclk0 = devm_clk_get(dev, "tcon-ch0"); 6189026e0d1SMaxime Ripard if (IS_ERR(tcon->sclk0)) { 6199026e0d1SMaxime Ripard dev_err(dev, "Couldn't get the TCON channel 0 clock\n"); 6209026e0d1SMaxime Ripard return PTR_ERR(tcon->sclk0); 6219026e0d1SMaxime Ripard } 62234d698f6SJernej Skrabec } 6239026e0d1SMaxime Ripard 62491ea2f29SChen-Yu Tsai if (tcon->quirks->has_channel_1) { 6259026e0d1SMaxime Ripard tcon->sclk1 = devm_clk_get(dev, "tcon-ch1"); 6269026e0d1SMaxime Ripard if (IS_ERR(tcon->sclk1)) { 6279026e0d1SMaxime Ripard dev_err(dev, "Couldn't get the TCON channel 1 clock\n"); 6289026e0d1SMaxime Ripard return PTR_ERR(tcon->sclk1); 6299026e0d1SMaxime Ripard } 6308e924047SMaxime Ripard } 6319026e0d1SMaxime Ripard 6324c7f16d1SChen-Yu Tsai return 0; 6339026e0d1SMaxime Ripard } 6349026e0d1SMaxime Ripard 6359026e0d1SMaxime Ripard static void sun4i_tcon_free_clocks(struct sun4i_tcon *tcon) 6369026e0d1SMaxime Ripard { 6379026e0d1SMaxime Ripard clk_disable_unprepare(tcon->clk); 6389026e0d1SMaxime Ripard } 6399026e0d1SMaxime Ripard 6409026e0d1SMaxime Ripard static int sun4i_tcon_init_irq(struct device *dev, 6419026e0d1SMaxime Ripard struct sun4i_tcon *tcon) 6429026e0d1SMaxime Ripard { 6439026e0d1SMaxime Ripard struct platform_device *pdev = to_platform_device(dev); 6449026e0d1SMaxime Ripard int irq, ret; 6459026e0d1SMaxime Ripard 6469026e0d1SMaxime Ripard irq = platform_get_irq(pdev, 0); 6479026e0d1SMaxime Ripard if (irq < 0) { 6489026e0d1SMaxime Ripard dev_err(dev, "Couldn't retrieve the TCON interrupt\n"); 6499026e0d1SMaxime Ripard return irq; 6509026e0d1SMaxime Ripard } 6519026e0d1SMaxime Ripard 6529026e0d1SMaxime Ripard ret = devm_request_irq(dev, irq, sun4i_tcon_handler, 0, 6539026e0d1SMaxime Ripard dev_name(dev), tcon); 6549026e0d1SMaxime Ripard if (ret) { 6559026e0d1SMaxime Ripard dev_err(dev, "Couldn't request the IRQ\n"); 6569026e0d1SMaxime Ripard return ret; 6579026e0d1SMaxime Ripard } 6589026e0d1SMaxime Ripard 6599026e0d1SMaxime Ripard return 0; 6609026e0d1SMaxime Ripard } 6619026e0d1SMaxime Ripard 6629026e0d1SMaxime Ripard static struct regmap_config sun4i_tcon_regmap_config = { 6639026e0d1SMaxime Ripard .reg_bits = 32, 6649026e0d1SMaxime Ripard .val_bits = 32, 6659026e0d1SMaxime Ripard .reg_stride = 4, 6669026e0d1SMaxime Ripard .max_register = 0x800, 6679026e0d1SMaxime Ripard }; 6689026e0d1SMaxime Ripard 6699026e0d1SMaxime Ripard static int sun4i_tcon_init_regmap(struct device *dev, 6709026e0d1SMaxime Ripard struct sun4i_tcon *tcon) 6719026e0d1SMaxime Ripard { 6729026e0d1SMaxime Ripard struct platform_device *pdev = to_platform_device(dev); 6739026e0d1SMaxime Ripard struct resource *res; 6749026e0d1SMaxime Ripard void __iomem *regs; 6759026e0d1SMaxime Ripard 6769026e0d1SMaxime Ripard res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 6779026e0d1SMaxime Ripard regs = devm_ioremap_resource(dev, res); 678af346f55SWei Yongjun if (IS_ERR(regs)) 6799026e0d1SMaxime Ripard return PTR_ERR(regs); 6809026e0d1SMaxime Ripard 6819026e0d1SMaxime Ripard tcon->regs = devm_regmap_init_mmio(dev, regs, 6829026e0d1SMaxime Ripard &sun4i_tcon_regmap_config); 6839026e0d1SMaxime Ripard if (IS_ERR(tcon->regs)) { 6849026e0d1SMaxime Ripard dev_err(dev, "Couldn't create the TCON regmap\n"); 6859026e0d1SMaxime Ripard return PTR_ERR(tcon->regs); 6869026e0d1SMaxime Ripard } 6879026e0d1SMaxime Ripard 6889026e0d1SMaxime Ripard /* Make sure the TCON is disabled and all IRQs are off */ 6899026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_GCTL_REG, 0); 6909026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_GINT0_REG, 0); 6919026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_GINT1_REG, 0); 6929026e0d1SMaxime Ripard 6939026e0d1SMaxime Ripard /* Disable IO lines and set them to tristate */ 6949026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, ~0); 6959026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_IO_TRI_REG, ~0); 6969026e0d1SMaxime Ripard 6979026e0d1SMaxime Ripard return 0; 6989026e0d1SMaxime Ripard } 6999026e0d1SMaxime Ripard 700b317fa3bSChen-Yu Tsai /* 701b317fa3bSChen-Yu Tsai * On SoCs with the old display pipeline design (Display Engine 1.0), 702b317fa3bSChen-Yu Tsai * the TCON is always tied to just one backend. Hence we can traverse 703b317fa3bSChen-Yu Tsai * the of_graph upwards to find the backend our tcon is connected to, 704b317fa3bSChen-Yu Tsai * and take its ID as our own. 705b317fa3bSChen-Yu Tsai * 706b317fa3bSChen-Yu Tsai * We can either identify backends from their compatible strings, which 707b317fa3bSChen-Yu Tsai * means maintaining a large list of them. Or, since the backend is 708b317fa3bSChen-Yu Tsai * registered and binded before the TCON, we can just go through the 709b317fa3bSChen-Yu Tsai * list of registered backends and compare the device node. 71087969338SIcenowy Zheng * 71187969338SIcenowy Zheng * As the structures now store engines instead of backends, here this 71287969338SIcenowy Zheng * function in fact searches the corresponding engine, and the ID is 71387969338SIcenowy Zheng * requested via the get_id function of the engine. 714b317fa3bSChen-Yu Tsai */ 715e8d5bbf7SChen-Yu Tsai static struct sunxi_engine * 716e8d5bbf7SChen-Yu Tsai sun4i_tcon_find_engine_traverse(struct sun4i_drv *drv, 717b317fa3bSChen-Yu Tsai struct device_node *node) 718b317fa3bSChen-Yu Tsai { 719b317fa3bSChen-Yu Tsai struct device_node *port, *ep, *remote; 720be3fe0f9SChen-Yu Tsai struct sunxi_engine *engine = ERR_PTR(-EINVAL); 721b317fa3bSChen-Yu Tsai 722b317fa3bSChen-Yu Tsai port = of_graph_get_port_by_id(node, 0); 723b317fa3bSChen-Yu Tsai if (!port) 724b317fa3bSChen-Yu Tsai return ERR_PTR(-EINVAL); 725b317fa3bSChen-Yu Tsai 7261469619dSChen-Yu Tsai /* 7271469619dSChen-Yu Tsai * This only works if there is only one path from the TCON 7281469619dSChen-Yu Tsai * to any display engine. Otherwise the probe order of the 7291469619dSChen-Yu Tsai * TCONs and display engines is not guaranteed. They may 7301469619dSChen-Yu Tsai * either bind to the wrong one, or worse, bind to the same 7311469619dSChen-Yu Tsai * one if additional checks are not done. 7321469619dSChen-Yu Tsai * 7331469619dSChen-Yu Tsai * Bail out if there are multiple input connections. 7341469619dSChen-Yu Tsai */ 735be3fe0f9SChen-Yu Tsai if (of_get_available_child_count(port) != 1) 736be3fe0f9SChen-Yu Tsai goto out_put_port; 7371469619dSChen-Yu Tsai 738be3fe0f9SChen-Yu Tsai /* Get the first connection without specifying an ID */ 739be3fe0f9SChen-Yu Tsai ep = of_get_next_available_child(port, NULL); 740be3fe0f9SChen-Yu Tsai if (!ep) 741be3fe0f9SChen-Yu Tsai goto out_put_port; 742be3fe0f9SChen-Yu Tsai 743b317fa3bSChen-Yu Tsai remote = of_graph_get_remote_port_parent(ep); 744b317fa3bSChen-Yu Tsai if (!remote) 745be3fe0f9SChen-Yu Tsai goto out_put_ep; 746b317fa3bSChen-Yu Tsai 74787969338SIcenowy Zheng /* does this node match any registered engines? */ 748be3fe0f9SChen-Yu Tsai list_for_each_entry(engine, &drv->engine_list, list) 749be3fe0f9SChen-Yu Tsai if (remote == engine->node) 750be3fe0f9SChen-Yu Tsai goto out_put_remote; 751b317fa3bSChen-Yu Tsai 752b317fa3bSChen-Yu Tsai /* keep looking through upstream ports */ 753e8d5bbf7SChen-Yu Tsai engine = sun4i_tcon_find_engine_traverse(drv, remote); 754b317fa3bSChen-Yu Tsai 755be3fe0f9SChen-Yu Tsai out_put_remote: 756be3fe0f9SChen-Yu Tsai of_node_put(remote); 757be3fe0f9SChen-Yu Tsai out_put_ep: 758be3fe0f9SChen-Yu Tsai of_node_put(ep); 759be3fe0f9SChen-Yu Tsai out_put_port: 760be3fe0f9SChen-Yu Tsai of_node_put(port); 761be3fe0f9SChen-Yu Tsai 762be3fe0f9SChen-Yu Tsai return engine; 763b317fa3bSChen-Yu Tsai } 764b317fa3bSChen-Yu Tsai 765e8d5bbf7SChen-Yu Tsai /* 766e8d5bbf7SChen-Yu Tsai * The device tree binding says that the remote endpoint ID of any 767e8d5bbf7SChen-Yu Tsai * connection between components, up to and including the TCON, of 768e8d5bbf7SChen-Yu Tsai * the display pipeline should be equal to the actual ID of the local 769e8d5bbf7SChen-Yu Tsai * component. Thus we can look at any one of the input connections of 770e8d5bbf7SChen-Yu Tsai * the TCONs, and use that connection's remote endpoint ID as our own. 771e8d5bbf7SChen-Yu Tsai * 772e8d5bbf7SChen-Yu Tsai * Since the user of this function already finds the input port, 773e8d5bbf7SChen-Yu Tsai * the port is passed in directly without further checks. 774e8d5bbf7SChen-Yu Tsai */ 775e8d5bbf7SChen-Yu Tsai static int sun4i_tcon_of_get_id_from_port(struct device_node *port) 776e8d5bbf7SChen-Yu Tsai { 777e8d5bbf7SChen-Yu Tsai struct device_node *ep; 778e8d5bbf7SChen-Yu Tsai int ret = -EINVAL; 779e8d5bbf7SChen-Yu Tsai 780e8d5bbf7SChen-Yu Tsai /* try finding an upstream endpoint */ 781e8d5bbf7SChen-Yu Tsai for_each_available_child_of_node(port, ep) { 782e8d5bbf7SChen-Yu Tsai struct device_node *remote; 783e8d5bbf7SChen-Yu Tsai u32 reg; 784e8d5bbf7SChen-Yu Tsai 785e8d5bbf7SChen-Yu Tsai remote = of_graph_get_remote_endpoint(ep); 786e8d5bbf7SChen-Yu Tsai if (!remote) 787e8d5bbf7SChen-Yu Tsai continue; 788e8d5bbf7SChen-Yu Tsai 789e8d5bbf7SChen-Yu Tsai ret = of_property_read_u32(remote, "reg", ®); 790e8d5bbf7SChen-Yu Tsai if (ret) 791e8d5bbf7SChen-Yu Tsai continue; 792e8d5bbf7SChen-Yu Tsai 793e8d5bbf7SChen-Yu Tsai ret = reg; 794e8d5bbf7SChen-Yu Tsai } 795e8d5bbf7SChen-Yu Tsai 796e8d5bbf7SChen-Yu Tsai return ret; 797e8d5bbf7SChen-Yu Tsai } 798e8d5bbf7SChen-Yu Tsai 799e8d5bbf7SChen-Yu Tsai /* 800e8d5bbf7SChen-Yu Tsai * Once we know the TCON's id, we can look through the list of 801e8d5bbf7SChen-Yu Tsai * engines to find a matching one. We assume all engines have 802e8d5bbf7SChen-Yu Tsai * been probed and added to the list. 803e8d5bbf7SChen-Yu Tsai */ 804e8d5bbf7SChen-Yu Tsai static struct sunxi_engine *sun4i_tcon_get_engine_by_id(struct sun4i_drv *drv, 805e8d5bbf7SChen-Yu Tsai int id) 806e8d5bbf7SChen-Yu Tsai { 807e8d5bbf7SChen-Yu Tsai struct sunxi_engine *engine; 808e8d5bbf7SChen-Yu Tsai 809e8d5bbf7SChen-Yu Tsai list_for_each_entry(engine, &drv->engine_list, list) 810e8d5bbf7SChen-Yu Tsai if (engine->id == id) 811e8d5bbf7SChen-Yu Tsai return engine; 812e8d5bbf7SChen-Yu Tsai 813e8d5bbf7SChen-Yu Tsai return ERR_PTR(-EINVAL); 814e8d5bbf7SChen-Yu Tsai } 815e8d5bbf7SChen-Yu Tsai 816e8d5bbf7SChen-Yu Tsai /* 817e8d5bbf7SChen-Yu Tsai * On SoCs with the old display pipeline design (Display Engine 1.0), 818e8d5bbf7SChen-Yu Tsai * we assumed the TCON was always tied to just one backend. However 819e8d5bbf7SChen-Yu Tsai * this proved not to be the case. On the A31, the TCON can select 820e8d5bbf7SChen-Yu Tsai * either backend as its source. On the A20 (and likely on the A10), 821e8d5bbf7SChen-Yu Tsai * the backend can choose which TCON to output to. 822e8d5bbf7SChen-Yu Tsai * 823e8d5bbf7SChen-Yu Tsai * The device tree binding says that the remote endpoint ID of any 824e8d5bbf7SChen-Yu Tsai * connection between components, up to and including the TCON, of 825e8d5bbf7SChen-Yu Tsai * the display pipeline should be equal to the actual ID of the local 826e8d5bbf7SChen-Yu Tsai * component. Thus we should be able to look at any one of the input 827e8d5bbf7SChen-Yu Tsai * connections of the TCONs, and use that connection's remote endpoint 828e8d5bbf7SChen-Yu Tsai * ID as our own. 829e8d5bbf7SChen-Yu Tsai * 830e8d5bbf7SChen-Yu Tsai * However the connections between the backend and TCON were assumed 831e8d5bbf7SChen-Yu Tsai * to be always singular, and their endpoit IDs were all incorrectly 832e8d5bbf7SChen-Yu Tsai * set to 0. This means for these old device trees, we cannot just look 833e8d5bbf7SChen-Yu Tsai * up the remote endpoint ID of a TCON input endpoint. TCON1 would be 834e8d5bbf7SChen-Yu Tsai * incorrectly identified as TCON0. 835e8d5bbf7SChen-Yu Tsai * 836e8d5bbf7SChen-Yu Tsai * This function first checks if the TCON node has 2 input endpoints. 837e8d5bbf7SChen-Yu Tsai * If so, then the device tree is a corrected version, and it will use 838e8d5bbf7SChen-Yu Tsai * sun4i_tcon_of_get_id() and sun4i_tcon_get_engine_by_id() from above 839e8d5bbf7SChen-Yu Tsai * to fetch the ID and engine directly. If not, then it is likely an 840e8d5bbf7SChen-Yu Tsai * old device trees, where the endpoint IDs were incorrect, but did not 841e8d5bbf7SChen-Yu Tsai * have endpoint connections between the backend and TCON across 842e8d5bbf7SChen-Yu Tsai * different display pipelines. It will fall back to the old method of 843e8d5bbf7SChen-Yu Tsai * traversing the of_graph to try and find a matching engine by device 844e8d5bbf7SChen-Yu Tsai * node. 845e8d5bbf7SChen-Yu Tsai * 846e8d5bbf7SChen-Yu Tsai * In the case of single display pipeline device trees, either method 847e8d5bbf7SChen-Yu Tsai * works. 848e8d5bbf7SChen-Yu Tsai */ 849e8d5bbf7SChen-Yu Tsai static struct sunxi_engine *sun4i_tcon_find_engine(struct sun4i_drv *drv, 850e8d5bbf7SChen-Yu Tsai struct device_node *node) 851e8d5bbf7SChen-Yu Tsai { 852e8d5bbf7SChen-Yu Tsai struct device_node *port; 853e8d5bbf7SChen-Yu Tsai struct sunxi_engine *engine; 854e8d5bbf7SChen-Yu Tsai 855e8d5bbf7SChen-Yu Tsai port = of_graph_get_port_by_id(node, 0); 856e8d5bbf7SChen-Yu Tsai if (!port) 857e8d5bbf7SChen-Yu Tsai return ERR_PTR(-EINVAL); 858e8d5bbf7SChen-Yu Tsai 859e8d5bbf7SChen-Yu Tsai /* 860e8d5bbf7SChen-Yu Tsai * Is this a corrected device tree with cross pipeline 861e8d5bbf7SChen-Yu Tsai * connections between the backend and TCON? 862e8d5bbf7SChen-Yu Tsai */ 863e8d5bbf7SChen-Yu Tsai if (of_get_child_count(port) > 1) { 864e8d5bbf7SChen-Yu Tsai /* Get our ID directly from an upstream endpoint */ 865e8d5bbf7SChen-Yu Tsai int id = sun4i_tcon_of_get_id_from_port(port); 866e8d5bbf7SChen-Yu Tsai 867e8d5bbf7SChen-Yu Tsai /* Get our engine by matching our ID */ 868e8d5bbf7SChen-Yu Tsai engine = sun4i_tcon_get_engine_by_id(drv, id); 869e8d5bbf7SChen-Yu Tsai 870e8d5bbf7SChen-Yu Tsai of_node_put(port); 871e8d5bbf7SChen-Yu Tsai return engine; 872e8d5bbf7SChen-Yu Tsai } 873e8d5bbf7SChen-Yu Tsai 874e8d5bbf7SChen-Yu Tsai /* Fallback to old method by traversing input endpoints */ 875e8d5bbf7SChen-Yu Tsai of_node_put(port); 876e8d5bbf7SChen-Yu Tsai return sun4i_tcon_find_engine_traverse(drv, node); 877e8d5bbf7SChen-Yu Tsai } 878e8d5bbf7SChen-Yu Tsai 8799026e0d1SMaxime Ripard static int sun4i_tcon_bind(struct device *dev, struct device *master, 8809026e0d1SMaxime Ripard void *data) 8819026e0d1SMaxime Ripard { 8829026e0d1SMaxime Ripard struct drm_device *drm = data; 8839026e0d1SMaxime Ripard struct sun4i_drv *drv = drm->dev_private; 88487969338SIcenowy Zheng struct sunxi_engine *engine; 885a0c1214eSMaxime Ripard struct device_node *remote; 8869026e0d1SMaxime Ripard struct sun4i_tcon *tcon; 8876664e9dcSChen-Yu Tsai struct reset_control *edp_rstc; 888a0c1214eSMaxime Ripard bool has_lvds_rst, has_lvds_alt, can_lvds; 8899026e0d1SMaxime Ripard int ret; 8909026e0d1SMaxime Ripard 89187969338SIcenowy Zheng engine = sun4i_tcon_find_engine(drv, dev->of_node); 89287969338SIcenowy Zheng if (IS_ERR(engine)) { 89387969338SIcenowy Zheng dev_err(dev, "Couldn't find matching engine\n"); 89480a58240SChen-Yu Tsai return -EPROBE_DEFER; 895b317fa3bSChen-Yu Tsai } 89680a58240SChen-Yu Tsai 8979026e0d1SMaxime Ripard tcon = devm_kzalloc(dev, sizeof(*tcon), GFP_KERNEL); 8989026e0d1SMaxime Ripard if (!tcon) 8999026e0d1SMaxime Ripard return -ENOMEM; 9009026e0d1SMaxime Ripard dev_set_drvdata(dev, tcon); 9019026e0d1SMaxime Ripard tcon->drm = drm; 902ae558110SMaxime Ripard tcon->dev = dev; 90387969338SIcenowy Zheng tcon->id = engine->id; 90491ea2f29SChen-Yu Tsai tcon->quirks = of_device_get_match_data(dev); 9059026e0d1SMaxime Ripard 9069026e0d1SMaxime Ripard tcon->lcd_rst = devm_reset_control_get(dev, "lcd"); 9079026e0d1SMaxime Ripard if (IS_ERR(tcon->lcd_rst)) { 9089026e0d1SMaxime Ripard dev_err(dev, "Couldn't get our reset line\n"); 9099026e0d1SMaxime Ripard return PTR_ERR(tcon->lcd_rst); 9109026e0d1SMaxime Ripard } 9119026e0d1SMaxime Ripard 9126664e9dcSChen-Yu Tsai if (tcon->quirks->needs_edp_reset) { 9136664e9dcSChen-Yu Tsai edp_rstc = devm_reset_control_get_shared(dev, "edp"); 9146664e9dcSChen-Yu Tsai if (IS_ERR(edp_rstc)) { 9156664e9dcSChen-Yu Tsai dev_err(dev, "Couldn't get edp reset line\n"); 9166664e9dcSChen-Yu Tsai return PTR_ERR(edp_rstc); 9176664e9dcSChen-Yu Tsai } 9186664e9dcSChen-Yu Tsai 9196664e9dcSChen-Yu Tsai ret = reset_control_deassert(edp_rstc); 9206664e9dcSChen-Yu Tsai if (ret) { 9216664e9dcSChen-Yu Tsai dev_err(dev, "Couldn't deassert edp reset line\n"); 9226664e9dcSChen-Yu Tsai return ret; 9236664e9dcSChen-Yu Tsai } 9246664e9dcSChen-Yu Tsai } 9256664e9dcSChen-Yu Tsai 9269026e0d1SMaxime Ripard /* Make sure our TCON is reset */ 927d57294c1SChen-Yu Tsai ret = reset_control_reset(tcon->lcd_rst); 9289026e0d1SMaxime Ripard if (ret) { 9299026e0d1SMaxime Ripard dev_err(dev, "Couldn't deassert our reset line\n"); 9309026e0d1SMaxime Ripard return ret; 9319026e0d1SMaxime Ripard } 9329026e0d1SMaxime Ripard 933e742a17cSMaxime Ripard if (tcon->quirks->supports_lvds) { 934a0c1214eSMaxime Ripard /* 935e742a17cSMaxime Ripard * This can only be made optional since we've had DT 936e742a17cSMaxime Ripard * nodes without the LVDS reset properties. 937a0c1214eSMaxime Ripard * 938e742a17cSMaxime Ripard * If the property is missing, just disable LVDS, and 939e742a17cSMaxime Ripard * print a warning. 940a0c1214eSMaxime Ripard */ 941a0c1214eSMaxime Ripard tcon->lvds_rst = devm_reset_control_get_optional(dev, "lvds"); 942a0c1214eSMaxime Ripard if (IS_ERR(tcon->lvds_rst)) { 943a0c1214eSMaxime Ripard dev_err(dev, "Couldn't get our reset line\n"); 944a0c1214eSMaxime Ripard return PTR_ERR(tcon->lvds_rst); 945a0c1214eSMaxime Ripard } else if (tcon->lvds_rst) { 946a0c1214eSMaxime Ripard has_lvds_rst = true; 947a0c1214eSMaxime Ripard reset_control_reset(tcon->lvds_rst); 948a0c1214eSMaxime Ripard } else { 949a0c1214eSMaxime Ripard has_lvds_rst = false; 950a0c1214eSMaxime Ripard } 951a0c1214eSMaxime Ripard 952a0c1214eSMaxime Ripard /* 953e742a17cSMaxime Ripard * This can only be made optional since we've had DT 954e742a17cSMaxime Ripard * nodes without the LVDS reset properties. 955a0c1214eSMaxime Ripard * 956e742a17cSMaxime Ripard * If the property is missing, just disable LVDS, and 957e742a17cSMaxime Ripard * print a warning. 958a0c1214eSMaxime Ripard */ 959a0c1214eSMaxime Ripard if (tcon->quirks->has_lvds_alt) { 960a0c1214eSMaxime Ripard tcon->lvds_pll = devm_clk_get(dev, "lvds-alt"); 961a0c1214eSMaxime Ripard if (IS_ERR(tcon->lvds_pll)) { 962a0c1214eSMaxime Ripard if (PTR_ERR(tcon->lvds_pll) == -ENOENT) { 963a0c1214eSMaxime Ripard has_lvds_alt = false; 964a0c1214eSMaxime Ripard } else { 965a0c1214eSMaxime Ripard dev_err(dev, "Couldn't get the LVDS PLL\n"); 96686a3ae58SDan Carpenter return PTR_ERR(tcon->lvds_pll); 967a0c1214eSMaxime Ripard } 968a0c1214eSMaxime Ripard } else { 969a0c1214eSMaxime Ripard has_lvds_alt = true; 970a0c1214eSMaxime Ripard } 971a0c1214eSMaxime Ripard } 972a0c1214eSMaxime Ripard 973e742a17cSMaxime Ripard if (!has_lvds_rst || 974e742a17cSMaxime Ripard (tcon->quirks->has_lvds_alt && !has_lvds_alt)) { 975e742a17cSMaxime Ripard dev_warn(dev, "Missing LVDS properties, Please upgrade your DT\n"); 976a0c1214eSMaxime Ripard dev_warn(dev, "LVDS output disabled\n"); 977a0c1214eSMaxime Ripard can_lvds = false; 978a0c1214eSMaxime Ripard } else { 979a0c1214eSMaxime Ripard can_lvds = true; 980a0c1214eSMaxime Ripard } 981e742a17cSMaxime Ripard } else { 982e742a17cSMaxime Ripard can_lvds = false; 983e742a17cSMaxime Ripard } 984a0c1214eSMaxime Ripard 9859026e0d1SMaxime Ripard ret = sun4i_tcon_init_clocks(dev, tcon); 9869026e0d1SMaxime Ripard if (ret) { 9879026e0d1SMaxime Ripard dev_err(dev, "Couldn't init our TCON clocks\n"); 9889026e0d1SMaxime Ripard goto err_assert_reset; 9899026e0d1SMaxime Ripard } 9909026e0d1SMaxime Ripard 9914c7f16d1SChen-Yu Tsai ret = sun4i_tcon_init_regmap(dev, tcon); 9929026e0d1SMaxime Ripard if (ret) { 9934c7f16d1SChen-Yu Tsai dev_err(dev, "Couldn't init our TCON regmap\n"); 9949026e0d1SMaxime Ripard goto err_free_clocks; 9959026e0d1SMaxime Ripard } 9969026e0d1SMaxime Ripard 99734d698f6SJernej Skrabec if (tcon->quirks->has_channel_0) { 9984c7f16d1SChen-Yu Tsai ret = sun4i_dclk_create(dev, tcon); 9994c7f16d1SChen-Yu Tsai if (ret) { 10004c7f16d1SChen-Yu Tsai dev_err(dev, "Couldn't create our TCON dot clock\n"); 10014c7f16d1SChen-Yu Tsai goto err_free_clocks; 10024c7f16d1SChen-Yu Tsai } 100334d698f6SJernej Skrabec } 10044c7f16d1SChen-Yu Tsai 10059026e0d1SMaxime Ripard ret = sun4i_tcon_init_irq(dev, tcon); 10069026e0d1SMaxime Ripard if (ret) { 10079026e0d1SMaxime Ripard dev_err(dev, "Couldn't init our TCON interrupts\n"); 10084c7f16d1SChen-Yu Tsai goto err_free_dotclock; 10099026e0d1SMaxime Ripard } 10109026e0d1SMaxime Ripard 101187969338SIcenowy Zheng tcon->crtc = sun4i_crtc_init(drm, engine, tcon); 101246cce6daSChen-Yu Tsai if (IS_ERR(tcon->crtc)) { 101346cce6daSChen-Yu Tsai dev_err(dev, "Couldn't create our CRTC\n"); 101446cce6daSChen-Yu Tsai ret = PTR_ERR(tcon->crtc); 101592411f6dSMaxime Ripard goto err_free_dotclock; 101646cce6daSChen-Yu Tsai } 101746cce6daSChen-Yu Tsai 1018a0c1214eSMaxime Ripard /* 1019a0c1214eSMaxime Ripard * If we have an LVDS panel connected to the TCON, we should 1020a0c1214eSMaxime Ripard * just probe the LVDS connector. Otherwise, just probe RGB as 1021a0c1214eSMaxime Ripard * we used to. 1022a0c1214eSMaxime Ripard */ 1023a0c1214eSMaxime Ripard remote = of_graph_get_remote_node(dev->of_node, 1, 0); 1024a0c1214eSMaxime Ripard if (of_device_is_compatible(remote, "panel-lvds")) 1025a0c1214eSMaxime Ripard if (can_lvds) 1026a0c1214eSMaxime Ripard ret = sun4i_lvds_init(drm, tcon); 1027a0c1214eSMaxime Ripard else 1028a0c1214eSMaxime Ripard ret = -EINVAL; 1029a0c1214eSMaxime Ripard else 1030b9c8506cSChen-Yu Tsai ret = sun4i_rgb_init(drm, tcon); 1031a0c1214eSMaxime Ripard of_node_put(remote); 1032a0c1214eSMaxime Ripard 103313fef095SChen-Yu Tsai if (ret < 0) 103492411f6dSMaxime Ripard goto err_free_dotclock; 103513fef095SChen-Yu Tsai 103627e18de7SChen-Yu Tsai if (tcon->quirks->needs_de_be_mux) { 103727e18de7SChen-Yu Tsai /* 103827e18de7SChen-Yu Tsai * We assume there is no dynamic muxing of backends 103927e18de7SChen-Yu Tsai * and TCONs, so we select the backend with same ID. 104027e18de7SChen-Yu Tsai * 104127e18de7SChen-Yu Tsai * While dynamic selection might be interesting, since 104227e18de7SChen-Yu Tsai * the CRTC is tied to the TCON, while the layers are 104327e18de7SChen-Yu Tsai * tied to the backends, this means, we will need to 104427e18de7SChen-Yu Tsai * switch between groups of layers. There might not be 104527e18de7SChen-Yu Tsai * a way to represent this constraint in DRM. 104627e18de7SChen-Yu Tsai */ 104727e18de7SChen-Yu Tsai regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 104827e18de7SChen-Yu Tsai SUN4I_TCON0_CTL_SRC_SEL_MASK, 104927e18de7SChen-Yu Tsai tcon->id); 105027e18de7SChen-Yu Tsai regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 105127e18de7SChen-Yu Tsai SUN4I_TCON1_CTL_SRC_SEL_MASK, 105227e18de7SChen-Yu Tsai tcon->id); 105327e18de7SChen-Yu Tsai } 105427e18de7SChen-Yu Tsai 105580a58240SChen-Yu Tsai list_add_tail(&tcon->list, &drv->tcon_list); 105680a58240SChen-Yu Tsai 105713fef095SChen-Yu Tsai return 0; 10589026e0d1SMaxime Ripard 10594c7f16d1SChen-Yu Tsai err_free_dotclock: 106034d698f6SJernej Skrabec if (tcon->quirks->has_channel_0) 10614c7f16d1SChen-Yu Tsai sun4i_dclk_free(tcon); 10629026e0d1SMaxime Ripard err_free_clocks: 10639026e0d1SMaxime Ripard sun4i_tcon_free_clocks(tcon); 10649026e0d1SMaxime Ripard err_assert_reset: 10659026e0d1SMaxime Ripard reset_control_assert(tcon->lcd_rst); 10669026e0d1SMaxime Ripard return ret; 10679026e0d1SMaxime Ripard } 10689026e0d1SMaxime Ripard 10699026e0d1SMaxime Ripard static void sun4i_tcon_unbind(struct device *dev, struct device *master, 10709026e0d1SMaxime Ripard void *data) 10719026e0d1SMaxime Ripard { 10729026e0d1SMaxime Ripard struct sun4i_tcon *tcon = dev_get_drvdata(dev); 10739026e0d1SMaxime Ripard 107480a58240SChen-Yu Tsai list_del(&tcon->list); 107534d698f6SJernej Skrabec if (tcon->quirks->has_channel_0) 10764c7f16d1SChen-Yu Tsai sun4i_dclk_free(tcon); 10779026e0d1SMaxime Ripard sun4i_tcon_free_clocks(tcon); 10789026e0d1SMaxime Ripard } 10799026e0d1SMaxime Ripard 1080dfeb693dSJulia Lawall static const struct component_ops sun4i_tcon_ops = { 10819026e0d1SMaxime Ripard .bind = sun4i_tcon_bind, 10829026e0d1SMaxime Ripard .unbind = sun4i_tcon_unbind, 10839026e0d1SMaxime Ripard }; 10849026e0d1SMaxime Ripard 10859026e0d1SMaxime Ripard static int sun4i_tcon_probe(struct platform_device *pdev) 10869026e0d1SMaxime Ripard { 108729e57fabSMaxime Ripard struct device_node *node = pdev->dev.of_node; 1088894f5a9fSMaxime Ripard struct drm_bridge *bridge; 108929e57fabSMaxime Ripard struct drm_panel *panel; 1090ebc94461SRob Herring int ret; 109129e57fabSMaxime Ripard 1092ebc94461SRob Herring ret = drm_of_find_panel_or_bridge(node, 1, 0, &panel, &bridge); 1093ebc94461SRob Herring if (ret == -EPROBE_DEFER) 1094ebc94461SRob Herring return ret; 109529e57fabSMaxime Ripard 10969026e0d1SMaxime Ripard return component_add(&pdev->dev, &sun4i_tcon_ops); 10979026e0d1SMaxime Ripard } 10989026e0d1SMaxime Ripard 10999026e0d1SMaxime Ripard static int sun4i_tcon_remove(struct platform_device *pdev) 11009026e0d1SMaxime Ripard { 11019026e0d1SMaxime Ripard component_del(&pdev->dev, &sun4i_tcon_ops); 11029026e0d1SMaxime Ripard 11039026e0d1SMaxime Ripard return 0; 11049026e0d1SMaxime Ripard } 11059026e0d1SMaxime Ripard 1106ad537fb2SChen-Yu Tsai /* platform specific TCON muxing callbacks */ 11074bb206bfSJonathan Liu static int sun4i_a10_tcon_set_mux(struct sun4i_tcon *tcon, 11084bb206bfSJonathan Liu const struct drm_encoder *encoder) 11094bb206bfSJonathan Liu { 11104bb206bfSJonathan Liu struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev); 11114bb206bfSJonathan Liu u32 shift; 11124bb206bfSJonathan Liu 11134bb206bfSJonathan Liu if (!tcon0) 11144bb206bfSJonathan Liu return -EINVAL; 11154bb206bfSJonathan Liu 11164bb206bfSJonathan Liu switch (encoder->encoder_type) { 11174bb206bfSJonathan Liu case DRM_MODE_ENCODER_TMDS: 11184bb206bfSJonathan Liu /* HDMI */ 11194bb206bfSJonathan Liu shift = 8; 11204bb206bfSJonathan Liu break; 11214bb206bfSJonathan Liu default: 11224bb206bfSJonathan Liu return -EINVAL; 11234bb206bfSJonathan Liu } 11244bb206bfSJonathan Liu 11254bb206bfSJonathan Liu regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG, 11264bb206bfSJonathan Liu 0x3 << shift, tcon->id << shift); 11274bb206bfSJonathan Liu 11284bb206bfSJonathan Liu return 0; 11294bb206bfSJonathan Liu } 11304bb206bfSJonathan Liu 1131ad537fb2SChen-Yu Tsai static int sun5i_a13_tcon_set_mux(struct sun4i_tcon *tcon, 1132abcb8766SMaxime Ripard const struct drm_encoder *encoder) 1133ad537fb2SChen-Yu Tsai { 1134ad537fb2SChen-Yu Tsai u32 val; 1135ad537fb2SChen-Yu Tsai 1136ad537fb2SChen-Yu Tsai if (encoder->encoder_type == DRM_MODE_ENCODER_TVDAC) 1137ad537fb2SChen-Yu Tsai val = 1; 1138ad537fb2SChen-Yu Tsai else 1139ad537fb2SChen-Yu Tsai val = 0; 1140ad537fb2SChen-Yu Tsai 1141ad537fb2SChen-Yu Tsai /* 1142ad537fb2SChen-Yu Tsai * FIXME: Undocumented bits 1143ad537fb2SChen-Yu Tsai */ 1144ad537fb2SChen-Yu Tsai return regmap_write(tcon->regs, SUN4I_TCON_MUX_CTRL_REG, val); 1145ad537fb2SChen-Yu Tsai } 1146ad537fb2SChen-Yu Tsai 114767e32645SChen-Yu Tsai static int sun6i_tcon_set_mux(struct sun4i_tcon *tcon, 1148abcb8766SMaxime Ripard const struct drm_encoder *encoder) 114967e32645SChen-Yu Tsai { 115067e32645SChen-Yu Tsai struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev); 115167e32645SChen-Yu Tsai u32 shift; 115267e32645SChen-Yu Tsai 115367e32645SChen-Yu Tsai if (!tcon0) 115467e32645SChen-Yu Tsai return -EINVAL; 115567e32645SChen-Yu Tsai 115667e32645SChen-Yu Tsai switch (encoder->encoder_type) { 115767e32645SChen-Yu Tsai case DRM_MODE_ENCODER_TMDS: 115867e32645SChen-Yu Tsai /* HDMI */ 115967e32645SChen-Yu Tsai shift = 8; 116067e32645SChen-Yu Tsai break; 116167e32645SChen-Yu Tsai default: 116267e32645SChen-Yu Tsai /* TODO A31 has MIPI DSI but A31s does not */ 116367e32645SChen-Yu Tsai return -EINVAL; 116467e32645SChen-Yu Tsai } 116567e32645SChen-Yu Tsai 116667e32645SChen-Yu Tsai regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG, 116767e32645SChen-Yu Tsai 0x3 << shift, tcon->id << shift); 116867e32645SChen-Yu Tsai 116967e32645SChen-Yu Tsai return 0; 117067e32645SChen-Yu Tsai } 117167e32645SChen-Yu Tsai 11724bb206bfSJonathan Liu static const struct sun4i_tcon_quirks sun4i_a10_quirks = { 117334d698f6SJernej Skrabec .has_channel_0 = true, 11744bb206bfSJonathan Liu .has_channel_1 = true, 11754bb206bfSJonathan Liu .set_mux = sun4i_a10_tcon_set_mux, 11764bb206bfSJonathan Liu }; 11774bb206bfSJonathan Liu 117891ea2f29SChen-Yu Tsai static const struct sun4i_tcon_quirks sun5i_a13_quirks = { 117934d698f6SJernej Skrabec .has_channel_0 = true, 118091ea2f29SChen-Yu Tsai .has_channel_1 = true, 1181ad537fb2SChen-Yu Tsai .set_mux = sun5i_a13_tcon_set_mux, 118291ea2f29SChen-Yu Tsai }; 118391ea2f29SChen-Yu Tsai 118493a5ec14SChen-Yu Tsai static const struct sun4i_tcon_quirks sun6i_a31_quirks = { 118534d698f6SJernej Skrabec .has_channel_0 = true, 118693a5ec14SChen-Yu Tsai .has_channel_1 = true, 1187a0c1214eSMaxime Ripard .has_lvds_alt = true, 118827e18de7SChen-Yu Tsai .needs_de_be_mux = true, 118967e32645SChen-Yu Tsai .set_mux = sun6i_tcon_set_mux, 119093a5ec14SChen-Yu Tsai }; 119193a5ec14SChen-Yu Tsai 119293a5ec14SChen-Yu Tsai static const struct sun4i_tcon_quirks sun6i_a31s_quirks = { 119334d698f6SJernej Skrabec .has_channel_0 = true, 119493a5ec14SChen-Yu Tsai .has_channel_1 = true, 119527e18de7SChen-Yu Tsai .needs_de_be_mux = true, 119693a5ec14SChen-Yu Tsai }; 119793a5ec14SChen-Yu Tsai 1198aaddb6d2SJonathan Liu static const struct sun4i_tcon_quirks sun7i_a20_quirks = { 119934d698f6SJernej Skrabec .has_channel_0 = true, 1200aaddb6d2SJonathan Liu .has_channel_1 = true, 1201aaddb6d2SJonathan Liu /* Same display pipeline structure as A10 */ 1202aaddb6d2SJonathan Liu .set_mux = sun4i_a10_tcon_set_mux, 1203aaddb6d2SJonathan Liu }; 1204aaddb6d2SJonathan Liu 120591ea2f29SChen-Yu Tsai static const struct sun4i_tcon_quirks sun8i_a33_quirks = { 120634d698f6SJernej Skrabec .has_channel_0 = true, 1207a0c1214eSMaxime Ripard .has_lvds_alt = true, 120891ea2f29SChen-Yu Tsai }; 120991ea2f29SChen-Yu Tsai 12102f0d7bb1SMaxime Ripard static const struct sun4i_tcon_quirks sun8i_a83t_lcd_quirks = { 1211e742a17cSMaxime Ripard .supports_lvds = true, 121234d698f6SJernej Skrabec .has_channel_0 = true, 12132f0d7bb1SMaxime Ripard }; 12142f0d7bb1SMaxime Ripard 121505adc89bSJernej Skrabec static const struct sun4i_tcon_quirks sun8i_a83t_tv_quirks = { 121605adc89bSJernej Skrabec .has_channel_1 = true, 121705adc89bSJernej Skrabec }; 121805adc89bSJernej Skrabec 12191a0edb3fSIcenowy Zheng static const struct sun4i_tcon_quirks sun8i_v3s_quirks = { 122034d698f6SJernej Skrabec .has_channel_0 = true, 12211a0edb3fSIcenowy Zheng }; 12221a0edb3fSIcenowy Zheng 12236664e9dcSChen-Yu Tsai static const struct sun4i_tcon_quirks sun9i_a80_tcon_lcd_quirks = { 12246664e9dcSChen-Yu Tsai .has_channel_0 = true, 12256664e9dcSChen-Yu Tsai .needs_edp_reset = true, 12266664e9dcSChen-Yu Tsai }; 12276664e9dcSChen-Yu Tsai 12286664e9dcSChen-Yu Tsai static const struct sun4i_tcon_quirks sun9i_a80_tcon_tv_quirks = { 12296664e9dcSChen-Yu Tsai .has_channel_1 = true, 12306664e9dcSChen-Yu Tsai .needs_edp_reset = true, 12316664e9dcSChen-Yu Tsai }; 12326664e9dcSChen-Yu Tsai 1233ff71c2cfSChen-Yu Tsai /* sun4i_drv uses this list to check if a device node is a TCON */ 1234ff71c2cfSChen-Yu Tsai const struct of_device_id sun4i_tcon_of_table[] = { 12354bb206bfSJonathan Liu { .compatible = "allwinner,sun4i-a10-tcon", .data = &sun4i_a10_quirks }, 123691ea2f29SChen-Yu Tsai { .compatible = "allwinner,sun5i-a13-tcon", .data = &sun5i_a13_quirks }, 123793a5ec14SChen-Yu Tsai { .compatible = "allwinner,sun6i-a31-tcon", .data = &sun6i_a31_quirks }, 123893a5ec14SChen-Yu Tsai { .compatible = "allwinner,sun6i-a31s-tcon", .data = &sun6i_a31s_quirks }, 1239aaddb6d2SJonathan Liu { .compatible = "allwinner,sun7i-a20-tcon", .data = &sun7i_a20_quirks }, 124091ea2f29SChen-Yu Tsai { .compatible = "allwinner,sun8i-a33-tcon", .data = &sun8i_a33_quirks }, 12412f0d7bb1SMaxime Ripard { .compatible = "allwinner,sun8i-a83t-tcon-lcd", .data = &sun8i_a83t_lcd_quirks }, 124205adc89bSJernej Skrabec { .compatible = "allwinner,sun8i-a83t-tcon-tv", .data = &sun8i_a83t_tv_quirks }, 12431a0edb3fSIcenowy Zheng { .compatible = "allwinner,sun8i-v3s-tcon", .data = &sun8i_v3s_quirks }, 12446664e9dcSChen-Yu Tsai { .compatible = "allwinner,sun9i-a80-tcon-lcd", .data = &sun9i_a80_tcon_lcd_quirks }, 12456664e9dcSChen-Yu Tsai { .compatible = "allwinner,sun9i-a80-tcon-tv", .data = &sun9i_a80_tcon_tv_quirks }, 12469026e0d1SMaxime Ripard { } 12479026e0d1SMaxime Ripard }; 12489026e0d1SMaxime Ripard MODULE_DEVICE_TABLE(of, sun4i_tcon_of_table); 1249ff71c2cfSChen-Yu Tsai EXPORT_SYMBOL(sun4i_tcon_of_table); 12509026e0d1SMaxime Ripard 12519026e0d1SMaxime Ripard static struct platform_driver sun4i_tcon_platform_driver = { 12529026e0d1SMaxime Ripard .probe = sun4i_tcon_probe, 12539026e0d1SMaxime Ripard .remove = sun4i_tcon_remove, 12549026e0d1SMaxime Ripard .driver = { 12559026e0d1SMaxime Ripard .name = "sun4i-tcon", 12569026e0d1SMaxime Ripard .of_match_table = sun4i_tcon_of_table, 12579026e0d1SMaxime Ripard }, 12589026e0d1SMaxime Ripard }; 12599026e0d1SMaxime Ripard module_platform_driver(sun4i_tcon_platform_driver); 12609026e0d1SMaxime Ripard 12619026e0d1SMaxime Ripard MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>"); 12629026e0d1SMaxime Ripard MODULE_DESCRIPTION("Allwinner A10 Timing Controller Driver"); 12639026e0d1SMaxime Ripard MODULE_LICENSE("GPL"); 1264