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> 15f11adcecSJonathan Liu #include <drm/drm_connector.h> 169026e0d1SMaxime Ripard #include <drm/drm_crtc.h> 179026e0d1SMaxime Ripard #include <drm/drm_crtc_helper.h> 18ad537fb2SChen-Yu Tsai #include <drm/drm_encoder.h> 199026e0d1SMaxime Ripard #include <drm/drm_modes.h> 20ebc94461SRob Herring #include <drm/drm_of.h> 21490cda5aSGiulio Benetti #include <drm/drm_panel.h> 229026e0d1SMaxime Ripard 23ad537fb2SChen-Yu Tsai #include <uapi/drm/drm_mode.h> 24ad537fb2SChen-Yu Tsai 259026e0d1SMaxime Ripard #include <linux/component.h> 269026e0d1SMaxime Ripard #include <linux/ioport.h> 279026e0d1SMaxime Ripard #include <linux/of_address.h> 2891ea2f29SChen-Yu Tsai #include <linux/of_device.h> 299026e0d1SMaxime Ripard #include <linux/of_irq.h> 309026e0d1SMaxime Ripard #include <linux/regmap.h> 319026e0d1SMaxime Ripard #include <linux/reset.h> 329026e0d1SMaxime Ripard 339026e0d1SMaxime Ripard #include "sun4i_crtc.h" 349026e0d1SMaxime Ripard #include "sun4i_dotclock.h" 359026e0d1SMaxime Ripard #include "sun4i_drv.h" 36a0c1214eSMaxime Ripard #include "sun4i_lvds.h" 3729e57fabSMaxime Ripard #include "sun4i_rgb.h" 389026e0d1SMaxime Ripard #include "sun4i_tcon.h" 39a08fc7c8SMaxime Ripard #include "sun6i_mipi_dsi.h" 40cf77d79bSJernej Skrabec #include "sun8i_tcon_top.h" 4187969338SIcenowy Zheng #include "sunxi_engine.h" 429026e0d1SMaxime Ripard 43a0c1214eSMaxime Ripard static struct drm_connector *sun4i_tcon_get_connector(const struct drm_encoder *encoder) 44a0c1214eSMaxime Ripard { 45a0c1214eSMaxime Ripard struct drm_connector *connector; 46a0c1214eSMaxime Ripard struct drm_connector_list_iter iter; 47a0c1214eSMaxime Ripard 48a0c1214eSMaxime Ripard drm_connector_list_iter_begin(encoder->dev, &iter); 49a0c1214eSMaxime Ripard drm_for_each_connector_iter(connector, &iter) 50a0c1214eSMaxime Ripard if (connector->encoder == encoder) { 51a0c1214eSMaxime Ripard drm_connector_list_iter_end(&iter); 52a0c1214eSMaxime Ripard return connector; 53a0c1214eSMaxime Ripard } 54a0c1214eSMaxime Ripard drm_connector_list_iter_end(&iter); 55a0c1214eSMaxime Ripard 56a0c1214eSMaxime Ripard return NULL; 57a0c1214eSMaxime Ripard } 58a0c1214eSMaxime Ripard 59a0c1214eSMaxime Ripard static int sun4i_tcon_get_pixel_depth(const struct drm_encoder *encoder) 60a0c1214eSMaxime Ripard { 61a0c1214eSMaxime Ripard struct drm_connector *connector; 62a0c1214eSMaxime Ripard struct drm_display_info *info; 63a0c1214eSMaxime Ripard 64a0c1214eSMaxime Ripard connector = sun4i_tcon_get_connector(encoder); 65a0c1214eSMaxime Ripard if (!connector) 66a0c1214eSMaxime Ripard return -EINVAL; 67a0c1214eSMaxime Ripard 68a0c1214eSMaxime Ripard info = &connector->display_info; 69a0c1214eSMaxime Ripard if (info->num_bus_formats != 1) 70a0c1214eSMaxime Ripard return -EINVAL; 71a0c1214eSMaxime Ripard 72a0c1214eSMaxime Ripard switch (info->bus_formats[0]) { 73a0c1214eSMaxime Ripard case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG: 74a0c1214eSMaxime Ripard return 18; 75a0c1214eSMaxime Ripard 76a0c1214eSMaxime Ripard case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA: 77a0c1214eSMaxime Ripard case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG: 78a0c1214eSMaxime Ripard return 24; 79a0c1214eSMaxime Ripard } 80a0c1214eSMaxime Ripard 81a0c1214eSMaxime Ripard return -EINVAL; 82a0c1214eSMaxime Ripard } 83a0c1214eSMaxime Ripard 8445e88f99SMaxime Ripard static void sun4i_tcon_channel_set_status(struct sun4i_tcon *tcon, int channel, 8545e88f99SMaxime Ripard bool enabled) 869026e0d1SMaxime Ripard { 8745e88f99SMaxime Ripard struct clk *clk; 889026e0d1SMaxime Ripard 8945e88f99SMaxime Ripard switch (channel) { 9045e88f99SMaxime Ripard case 0: 9134d698f6SJernej Skrabec WARN_ON(!tcon->quirks->has_channel_0); 929026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 939026e0d1SMaxime Ripard SUN4I_TCON0_CTL_TCON_ENABLE, 9445e88f99SMaxime Ripard enabled ? SUN4I_TCON0_CTL_TCON_ENABLE : 0); 9545e88f99SMaxime Ripard clk = tcon->dclk; 9645e88f99SMaxime Ripard break; 9745e88f99SMaxime Ripard case 1: 9891ea2f29SChen-Yu Tsai WARN_ON(!tcon->quirks->has_channel_1); 999026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 1009026e0d1SMaxime Ripard SUN4I_TCON1_CTL_TCON_ENABLE, 10145e88f99SMaxime Ripard enabled ? SUN4I_TCON1_CTL_TCON_ENABLE : 0); 10245e88f99SMaxime Ripard clk = tcon->sclk1; 10345e88f99SMaxime Ripard break; 10445e88f99SMaxime Ripard default: 10545e88f99SMaxime Ripard DRM_WARN("Unknown channel... doing nothing\n"); 10645e88f99SMaxime Ripard return; 1079026e0d1SMaxime Ripard } 10845e88f99SMaxime Ripard 109f3e5feebSJernej Skrabec if (enabled) { 11045e88f99SMaxime Ripard clk_prepare_enable(clk); 1117035046dSOndrej Jirman clk_rate_exclusive_get(clk); 112f3e5feebSJernej Skrabec } else { 113f3e5feebSJernej Skrabec clk_rate_exclusive_put(clk); 11445e88f99SMaxime Ripard clk_disable_unprepare(clk); 11545e88f99SMaxime Ripard } 116f3e5feebSJernej Skrabec } 11745e88f99SMaxime Ripard 118a0c1214eSMaxime Ripard static void sun4i_tcon_lvds_set_status(struct sun4i_tcon *tcon, 119a0c1214eSMaxime Ripard const struct drm_encoder *encoder, 120a0c1214eSMaxime Ripard bool enabled) 121a0c1214eSMaxime Ripard { 122a0c1214eSMaxime Ripard if (enabled) { 123a0c1214eSMaxime Ripard u8 val; 124a0c1214eSMaxime Ripard 125a0c1214eSMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_IF_REG, 126a0c1214eSMaxime Ripard SUN4I_TCON0_LVDS_IF_EN, 127a0c1214eSMaxime Ripard SUN4I_TCON0_LVDS_IF_EN); 128a0c1214eSMaxime Ripard 129a0c1214eSMaxime Ripard /* 130a0c1214eSMaxime Ripard * As their name suggest, these values only apply to the A31 131a0c1214eSMaxime Ripard * and later SoCs. We'll have to rework this when merging 132a0c1214eSMaxime Ripard * support for the older SoCs. 133a0c1214eSMaxime Ripard */ 134a0c1214eSMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG, 135a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_C(2) | 136a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_V(3) | 137a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_PD(2) | 138a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_EN_LDO); 139a0c1214eSMaxime Ripard udelay(2); 140a0c1214eSMaxime Ripard 141a0c1214eSMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG, 142a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_EN_MB, 143a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_EN_MB); 144a0c1214eSMaxime Ripard udelay(2); 145a0c1214eSMaxime Ripard 146a0c1214eSMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG, 147a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_EN_DRVC, 148a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_EN_DRVC); 149a0c1214eSMaxime Ripard 150a0c1214eSMaxime Ripard if (sun4i_tcon_get_pixel_depth(encoder) == 18) 151a0c1214eSMaxime Ripard val = 7; 152a0c1214eSMaxime Ripard else 153a0c1214eSMaxime Ripard val = 0xf; 154a0c1214eSMaxime Ripard 155a0c1214eSMaxime Ripard regmap_write_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG, 156a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_EN_DRVD(0xf), 157a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_EN_DRVD(val)); 158a0c1214eSMaxime Ripard } else { 159a0c1214eSMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_IF_REG, 160a0c1214eSMaxime Ripard SUN4I_TCON0_LVDS_IF_EN, 0); 161a0c1214eSMaxime Ripard } 162a0c1214eSMaxime Ripard } 163a0c1214eSMaxime Ripard 16445e88f99SMaxime Ripard void sun4i_tcon_set_status(struct sun4i_tcon *tcon, 16545e88f99SMaxime Ripard const struct drm_encoder *encoder, 16645e88f99SMaxime Ripard bool enabled) 16745e88f99SMaxime Ripard { 168a0c1214eSMaxime Ripard bool is_lvds = false; 16945e88f99SMaxime Ripard int channel; 17045e88f99SMaxime Ripard 17145e88f99SMaxime Ripard switch (encoder->encoder_type) { 172a0c1214eSMaxime Ripard case DRM_MODE_ENCODER_LVDS: 173a0c1214eSMaxime Ripard is_lvds = true; 174a0c1214eSMaxime Ripard /* Fallthrough */ 175a08fc7c8SMaxime Ripard case DRM_MODE_ENCODER_DSI: 17645e88f99SMaxime Ripard case DRM_MODE_ENCODER_NONE: 17745e88f99SMaxime Ripard channel = 0; 17845e88f99SMaxime Ripard break; 17945e88f99SMaxime Ripard case DRM_MODE_ENCODER_TMDS: 18045e88f99SMaxime Ripard case DRM_MODE_ENCODER_TVDAC: 18145e88f99SMaxime Ripard channel = 1; 18245e88f99SMaxime Ripard break; 18345e88f99SMaxime Ripard default: 18445e88f99SMaxime Ripard DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n"); 18545e88f99SMaxime Ripard return; 18645e88f99SMaxime Ripard } 18745e88f99SMaxime Ripard 188a0c1214eSMaxime Ripard if (is_lvds && !enabled) 189a0c1214eSMaxime Ripard sun4i_tcon_lvds_set_status(tcon, encoder, false); 190a0c1214eSMaxime Ripard 19145e88f99SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 19245e88f99SMaxime Ripard SUN4I_TCON_GCTL_TCON_ENABLE, 19345e88f99SMaxime Ripard enabled ? SUN4I_TCON_GCTL_TCON_ENABLE : 0); 19445e88f99SMaxime Ripard 195a0c1214eSMaxime Ripard if (is_lvds && enabled) 196a0c1214eSMaxime Ripard sun4i_tcon_lvds_set_status(tcon, encoder, true); 197a0c1214eSMaxime Ripard 19845e88f99SMaxime Ripard sun4i_tcon_channel_set_status(tcon, channel, enabled); 19945e88f99SMaxime Ripard } 2009026e0d1SMaxime Ripard 2019026e0d1SMaxime Ripard void sun4i_tcon_enable_vblank(struct sun4i_tcon *tcon, bool enable) 2029026e0d1SMaxime Ripard { 2039026e0d1SMaxime Ripard u32 mask, val = 0; 2049026e0d1SMaxime Ripard 2059026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("%sabling VBLANK interrupt\n", enable ? "En" : "Dis"); 2069026e0d1SMaxime Ripard 2079026e0d1SMaxime Ripard mask = SUN4I_TCON_GINT0_VBLANK_ENABLE(0) | 208a493ceaeSMaxime Ripard SUN4I_TCON_GINT0_VBLANK_ENABLE(1) | 209a493ceaeSMaxime Ripard SUN4I_TCON_GINT0_TCON0_TRI_FINISH_ENABLE; 2109026e0d1SMaxime Ripard 2119026e0d1SMaxime Ripard if (enable) 2129026e0d1SMaxime Ripard val = mask; 2139026e0d1SMaxime Ripard 2149026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, mask, val); 2159026e0d1SMaxime Ripard } 2169026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_enable_vblank); 2179026e0d1SMaxime Ripard 21867e32645SChen-Yu Tsai /* 21967e32645SChen-Yu Tsai * This function is a helper for TCON output muxing. The TCON output 22067e32645SChen-Yu Tsai * muxing control register in earlier SoCs (without the TCON TOP block) 22167e32645SChen-Yu Tsai * are located in TCON0. This helper returns a pointer to TCON0's 22267e32645SChen-Yu Tsai * sun4i_tcon structure, or NULL if not found. 22367e32645SChen-Yu Tsai */ 22467e32645SChen-Yu Tsai static struct sun4i_tcon *sun4i_get_tcon0(struct drm_device *drm) 22567e32645SChen-Yu Tsai { 22667e32645SChen-Yu Tsai struct sun4i_drv *drv = drm->dev_private; 22767e32645SChen-Yu Tsai struct sun4i_tcon *tcon; 22867e32645SChen-Yu Tsai 22967e32645SChen-Yu Tsai list_for_each_entry(tcon, &drv->tcon_list, list) 23067e32645SChen-Yu Tsai if (tcon->id == 0) 23167e32645SChen-Yu Tsai return tcon; 23267e32645SChen-Yu Tsai 23367e32645SChen-Yu Tsai dev_warn(drm->dev, 23467e32645SChen-Yu Tsai "TCON0 not found, display output muxing may not work\n"); 23567e32645SChen-Yu Tsai 23667e32645SChen-Yu Tsai return NULL; 23767e32645SChen-Yu Tsai } 23867e32645SChen-Yu Tsai 239f8c73f4fSMaxime Ripard void sun4i_tcon_set_mux(struct sun4i_tcon *tcon, int channel, 240abcb8766SMaxime Ripard const struct drm_encoder *encoder) 241f8c73f4fSMaxime Ripard { 242ad537fb2SChen-Yu Tsai int ret = -ENOTSUPP; 243b7cb9b91SMaxime Ripard 244ad537fb2SChen-Yu Tsai if (tcon->quirks->set_mux) 245ad537fb2SChen-Yu Tsai ret = tcon->quirks->set_mux(tcon, encoder); 246f8c73f4fSMaxime Ripard 247ad537fb2SChen-Yu Tsai DRM_DEBUG_DRIVER("Muxing encoder %s to CRTC %s: %d\n", 248ad537fb2SChen-Yu Tsai encoder->name, encoder->crtc->name, ret); 249f8c73f4fSMaxime Ripard } 250f8c73f4fSMaxime Ripard 251961c645cSMaxime Ripard static int sun4i_tcon_get_clk_delay(const struct drm_display_mode *mode, 2529026e0d1SMaxime Ripard int channel) 2539026e0d1SMaxime Ripard { 2549026e0d1SMaxime Ripard int delay = mode->vtotal - mode->vdisplay; 2559026e0d1SMaxime Ripard 2569026e0d1SMaxime Ripard if (mode->flags & DRM_MODE_FLAG_INTERLACE) 2579026e0d1SMaxime Ripard delay /= 2; 2589026e0d1SMaxime Ripard 2599026e0d1SMaxime Ripard if (channel == 1) 2609026e0d1SMaxime Ripard delay -= 2; 2619026e0d1SMaxime Ripard 2629026e0d1SMaxime Ripard delay = min(delay, 30); 2639026e0d1SMaxime Ripard 2649026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("TCON %d clock delay %u\n", channel, delay); 2659026e0d1SMaxime Ripard 2669026e0d1SMaxime Ripard return delay; 2679026e0d1SMaxime Ripard } 2689026e0d1SMaxime Ripard 269ba19c537SMaxime Ripard static void sun4i_tcon0_mode_set_common(struct sun4i_tcon *tcon, 270ba19c537SMaxime Ripard const struct drm_display_mode *mode) 271ba19c537SMaxime Ripard { 272ba19c537SMaxime Ripard /* Configure the dot clock */ 273ba19c537SMaxime Ripard clk_set_rate(tcon->dclk, mode->crtc_clock * 1000); 274ba19c537SMaxime Ripard 275ba19c537SMaxime Ripard /* Set the resolution */ 276ba19c537SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC0_REG, 277ba19c537SMaxime Ripard SUN4I_TCON0_BASIC0_X(mode->crtc_hdisplay) | 278ba19c537SMaxime Ripard SUN4I_TCON0_BASIC0_Y(mode->crtc_vdisplay)); 279ba19c537SMaxime Ripard } 280ba19c537SMaxime Ripard 281f11adcecSJonathan Liu static void sun4i_tcon0_mode_set_dithering(struct sun4i_tcon *tcon, 282f11adcecSJonathan Liu const struct drm_connector *connector) 283f11adcecSJonathan Liu { 284f11adcecSJonathan Liu u32 bus_format = 0; 285f11adcecSJonathan Liu u32 val = 0; 286f11adcecSJonathan Liu 287f11adcecSJonathan Liu /* XXX Would this ever happen? */ 288f11adcecSJonathan Liu if (!connector) 289f11adcecSJonathan Liu return; 290f11adcecSJonathan Liu 291f11adcecSJonathan Liu /* 292f11adcecSJonathan Liu * FIXME: Undocumented bits 293f11adcecSJonathan Liu * 294f11adcecSJonathan Liu * The whole dithering process and these parameters are not 295f11adcecSJonathan Liu * explained in the vendor documents or BSP kernel code. 296f11adcecSJonathan Liu */ 297f11adcecSJonathan Liu regmap_write(tcon->regs, SUN4I_TCON0_FRM_SEED_PR_REG, 0x11111111); 298f11adcecSJonathan Liu regmap_write(tcon->regs, SUN4I_TCON0_FRM_SEED_PG_REG, 0x11111111); 299f11adcecSJonathan Liu regmap_write(tcon->regs, SUN4I_TCON0_FRM_SEED_PB_REG, 0x11111111); 300f11adcecSJonathan Liu regmap_write(tcon->regs, SUN4I_TCON0_FRM_SEED_LR_REG, 0x11111111); 301f11adcecSJonathan Liu regmap_write(tcon->regs, SUN4I_TCON0_FRM_SEED_LG_REG, 0x11111111); 302f11adcecSJonathan Liu regmap_write(tcon->regs, SUN4I_TCON0_FRM_SEED_LB_REG, 0x11111111); 303f11adcecSJonathan Liu regmap_write(tcon->regs, SUN4I_TCON0_FRM_TBL0_REG, 0x01010000); 304f11adcecSJonathan Liu regmap_write(tcon->regs, SUN4I_TCON0_FRM_TBL1_REG, 0x15151111); 305f11adcecSJonathan Liu regmap_write(tcon->regs, SUN4I_TCON0_FRM_TBL2_REG, 0x57575555); 306f11adcecSJonathan Liu regmap_write(tcon->regs, SUN4I_TCON0_FRM_TBL3_REG, 0x7f7f7777); 307f11adcecSJonathan Liu 308f11adcecSJonathan Liu /* Do dithering if panel only supports 6 bits per color */ 309f11adcecSJonathan Liu if (connector->display_info.bpc == 6) 310f11adcecSJonathan Liu val |= SUN4I_TCON0_FRM_CTL_EN; 311f11adcecSJonathan Liu 312f11adcecSJonathan Liu if (connector->display_info.num_bus_formats == 1) 313f11adcecSJonathan Liu bus_format = connector->display_info.bus_formats[0]; 314f11adcecSJonathan Liu 315f11adcecSJonathan Liu /* Check the connection format */ 316f11adcecSJonathan Liu switch (bus_format) { 317f11adcecSJonathan Liu case MEDIA_BUS_FMT_RGB565_1X16: 318f11adcecSJonathan Liu /* R and B components are only 5 bits deep */ 319f11adcecSJonathan Liu val |= SUN4I_TCON0_FRM_CTL_MODE_R; 320f11adcecSJonathan Liu val |= SUN4I_TCON0_FRM_CTL_MODE_B; 321f11adcecSJonathan Liu case MEDIA_BUS_FMT_RGB666_1X18: 322f11adcecSJonathan Liu case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG: 323f11adcecSJonathan Liu /* Fall through: enable dithering */ 324f11adcecSJonathan Liu val |= SUN4I_TCON0_FRM_CTL_EN; 325f11adcecSJonathan Liu break; 326f11adcecSJonathan Liu } 327f11adcecSJonathan Liu 328f11adcecSJonathan Liu /* Write dithering settings */ 329f11adcecSJonathan Liu regmap_write(tcon->regs, SUN4I_TCON_FRM_CTL_REG, val); 330f11adcecSJonathan Liu } 331f11adcecSJonathan Liu 332a08fc7c8SMaxime Ripard static void sun4i_tcon0_mode_set_cpu(struct sun4i_tcon *tcon, 33379891d56SChen-Yu Tsai const struct drm_encoder *encoder, 334a08fc7c8SMaxime Ripard const struct drm_display_mode *mode) 335a08fc7c8SMaxime Ripard { 33679891d56SChen-Yu Tsai /* TODO support normal CPU interface modes */ 33779891d56SChen-Yu Tsai struct sun6i_dsi *dsi = encoder_to_sun6i_dsi(encoder); 33879891d56SChen-Yu Tsai struct mipi_dsi_device *device = dsi->device; 339a08fc7c8SMaxime Ripard u8 bpp = mipi_dsi_pixel_format_to_bpp(device->format); 340a08fc7c8SMaxime Ripard u8 lanes = device->lanes; 341a08fc7c8SMaxime Ripard u32 block_space, start_delay; 342a08fc7c8SMaxime Ripard u32 tcon_div; 343a08fc7c8SMaxime Ripard 344a08fc7c8SMaxime Ripard tcon->dclk_min_div = 4; 345a08fc7c8SMaxime Ripard tcon->dclk_max_div = 127; 346a08fc7c8SMaxime Ripard 347a08fc7c8SMaxime Ripard sun4i_tcon0_mode_set_common(tcon, mode); 348a08fc7c8SMaxime Ripard 349f11adcecSJonathan Liu /* Set dithering if needed */ 350f11adcecSJonathan Liu sun4i_tcon0_mode_set_dithering(tcon, sun4i_tcon_get_connector(encoder)); 351f11adcecSJonathan Liu 352a08fc7c8SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 353a08fc7c8SMaxime Ripard SUN4I_TCON0_CTL_IF_MASK, 354a08fc7c8SMaxime Ripard SUN4I_TCON0_CTL_IF_8080); 355a08fc7c8SMaxime Ripard 356a08fc7c8SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_ECC_FIFO_REG, 357a08fc7c8SMaxime Ripard SUN4I_TCON_ECC_FIFO_EN); 358a08fc7c8SMaxime Ripard 359a08fc7c8SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_CPU_IF_REG, 360a08fc7c8SMaxime Ripard SUN4I_TCON0_CPU_IF_MODE_DSI | 361a08fc7c8SMaxime Ripard SUN4I_TCON0_CPU_IF_TRI_FIFO_FLUSH | 362a08fc7c8SMaxime Ripard SUN4I_TCON0_CPU_IF_TRI_FIFO_EN | 363a08fc7c8SMaxime Ripard SUN4I_TCON0_CPU_IF_TRI_EN); 364a08fc7c8SMaxime Ripard 365a08fc7c8SMaxime Ripard /* 366a08fc7c8SMaxime Ripard * This looks suspicious, but it works... 367a08fc7c8SMaxime Ripard * 368a08fc7c8SMaxime Ripard * The datasheet says that this should be set higher than 20 * 369a08fc7c8SMaxime Ripard * pixel cycle, but it's not clear what a pixel cycle is. 370a08fc7c8SMaxime Ripard */ 371a08fc7c8SMaxime Ripard regmap_read(tcon->regs, SUN4I_TCON0_DCLK_REG, &tcon_div); 372a08fc7c8SMaxime Ripard tcon_div &= GENMASK(6, 0); 373a08fc7c8SMaxime Ripard block_space = mode->htotal * bpp / (tcon_div * lanes); 374a08fc7c8SMaxime Ripard block_space -= mode->hdisplay + 40; 375a08fc7c8SMaxime Ripard 376a08fc7c8SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI0_REG, 377a08fc7c8SMaxime Ripard SUN4I_TCON0_CPU_TRI0_BLOCK_SPACE(block_space) | 378a08fc7c8SMaxime Ripard SUN4I_TCON0_CPU_TRI0_BLOCK_SIZE(mode->hdisplay)); 379a08fc7c8SMaxime Ripard 380a08fc7c8SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI1_REG, 381a08fc7c8SMaxime Ripard SUN4I_TCON0_CPU_TRI1_BLOCK_NUM(mode->vdisplay)); 382a08fc7c8SMaxime Ripard 383a08fc7c8SMaxime Ripard start_delay = (mode->crtc_vtotal - mode->crtc_vdisplay - 10 - 1); 384a08fc7c8SMaxime Ripard start_delay = start_delay * mode->crtc_htotal * 149; 385a08fc7c8SMaxime Ripard start_delay = start_delay / (mode->crtc_clock / 1000) / 8; 386a08fc7c8SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI2_REG, 387a08fc7c8SMaxime Ripard SUN4I_TCON0_CPU_TRI2_TRANS_START_SET(10) | 388a08fc7c8SMaxime Ripard SUN4I_TCON0_CPU_TRI2_START_DELAY(start_delay)); 389a08fc7c8SMaxime Ripard 390a08fc7c8SMaxime Ripard /* 391a08fc7c8SMaxime Ripard * The Allwinner BSP has a comment that the period should be 392a08fc7c8SMaxime Ripard * the display clock * 15, but uses an hardcoded 3000... 393a08fc7c8SMaxime Ripard */ 394a08fc7c8SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_SAFE_PERIOD_REG, 395a08fc7c8SMaxime Ripard SUN4I_TCON_SAFE_PERIOD_NUM(3000) | 396a08fc7c8SMaxime Ripard SUN4I_TCON_SAFE_PERIOD_MODE(3)); 397a08fc7c8SMaxime Ripard 398a08fc7c8SMaxime Ripard /* Enable the output on the pins */ 399a08fc7c8SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 400a08fc7c8SMaxime Ripard 0xe0000000); 401a08fc7c8SMaxime Ripard } 402a08fc7c8SMaxime Ripard 403a0c1214eSMaxime Ripard static void sun4i_tcon0_mode_set_lvds(struct sun4i_tcon *tcon, 404a0c1214eSMaxime Ripard const struct drm_encoder *encoder, 405a0c1214eSMaxime Ripard const struct drm_display_mode *mode) 406a0c1214eSMaxime Ripard { 407a0c1214eSMaxime Ripard unsigned int bp; 408a0c1214eSMaxime Ripard u8 clk_delay; 409a0c1214eSMaxime Ripard u32 reg, val = 0; 410a0c1214eSMaxime Ripard 41134d698f6SJernej Skrabec WARN_ON(!tcon->quirks->has_channel_0); 41234d698f6SJernej Skrabec 413a0c1214eSMaxime Ripard tcon->dclk_min_div = 7; 414a0c1214eSMaxime Ripard tcon->dclk_max_div = 7; 415a0c1214eSMaxime Ripard sun4i_tcon0_mode_set_common(tcon, mode); 416a0c1214eSMaxime Ripard 417f11adcecSJonathan Liu /* Set dithering if needed */ 418f11adcecSJonathan Liu sun4i_tcon0_mode_set_dithering(tcon, sun4i_tcon_get_connector(encoder)); 419f11adcecSJonathan Liu 420a0c1214eSMaxime Ripard /* Adjust clock delay */ 421a0c1214eSMaxime Ripard clk_delay = sun4i_tcon_get_clk_delay(mode, 0); 422a0c1214eSMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 423a0c1214eSMaxime Ripard SUN4I_TCON0_CTL_CLK_DELAY_MASK, 424a0c1214eSMaxime Ripard SUN4I_TCON0_CTL_CLK_DELAY(clk_delay)); 425a0c1214eSMaxime Ripard 426a0c1214eSMaxime Ripard /* 427a0c1214eSMaxime Ripard * This is called a backporch in the register documentation, 428a0c1214eSMaxime Ripard * but it really is the back porch + hsync 429a0c1214eSMaxime Ripard */ 430a0c1214eSMaxime Ripard bp = mode->crtc_htotal - mode->crtc_hsync_start; 431a0c1214eSMaxime Ripard DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n", 432a0c1214eSMaxime Ripard mode->crtc_htotal, bp); 433a0c1214eSMaxime Ripard 434a0c1214eSMaxime Ripard /* Set horizontal display timings */ 435a0c1214eSMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG, 436a0c1214eSMaxime Ripard SUN4I_TCON0_BASIC1_H_TOTAL(mode->htotal) | 437a0c1214eSMaxime Ripard SUN4I_TCON0_BASIC1_H_BACKPORCH(bp)); 438a0c1214eSMaxime Ripard 439a0c1214eSMaxime Ripard /* 440a0c1214eSMaxime Ripard * This is called a backporch in the register documentation, 441a0c1214eSMaxime Ripard * but it really is the back porch + hsync 442a0c1214eSMaxime Ripard */ 443a0c1214eSMaxime Ripard bp = mode->crtc_vtotal - mode->crtc_vsync_start; 444a0c1214eSMaxime Ripard DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n", 445a0c1214eSMaxime Ripard mode->crtc_vtotal, bp); 446a0c1214eSMaxime Ripard 447a0c1214eSMaxime Ripard /* Set vertical display timings */ 448a0c1214eSMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG, 449a0c1214eSMaxime Ripard SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) | 450a0c1214eSMaxime Ripard SUN4I_TCON0_BASIC2_V_BACKPORCH(bp)); 451a0c1214eSMaxime Ripard 452a0c1214eSMaxime Ripard reg = SUN4I_TCON0_LVDS_IF_CLK_SEL_TCON0 | 453a0c1214eSMaxime Ripard SUN4I_TCON0_LVDS_IF_DATA_POL_NORMAL | 454a0c1214eSMaxime Ripard SUN4I_TCON0_LVDS_IF_CLK_POL_NORMAL; 455a0c1214eSMaxime Ripard if (sun4i_tcon_get_pixel_depth(encoder) == 24) 456a0c1214eSMaxime Ripard reg |= SUN4I_TCON0_LVDS_IF_BITWIDTH_24BITS; 457a0c1214eSMaxime Ripard else 458a0c1214eSMaxime Ripard reg |= SUN4I_TCON0_LVDS_IF_BITWIDTH_18BITS; 459a0c1214eSMaxime Ripard 460a0c1214eSMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_LVDS_IF_REG, reg); 461a0c1214eSMaxime Ripard 462a0c1214eSMaxime Ripard /* Setup the polarity of the various signals */ 463a0c1214eSMaxime Ripard if (!(mode->flags & DRM_MODE_FLAG_PHSYNC)) 464a0c1214eSMaxime Ripard val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE; 465a0c1214eSMaxime Ripard 466a0c1214eSMaxime Ripard if (!(mode->flags & DRM_MODE_FLAG_PVSYNC)) 467a0c1214eSMaxime Ripard val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE; 468a0c1214eSMaxime Ripard 469a0c1214eSMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_IO_POL_REG, val); 470a0c1214eSMaxime Ripard 471a0c1214eSMaxime Ripard /* Map output pins to channel 0 */ 472a0c1214eSMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 473a0c1214eSMaxime Ripard SUN4I_TCON_GCTL_IOMAP_MASK, 474a0c1214eSMaxime Ripard SUN4I_TCON_GCTL_IOMAP_TCON0); 47580b79e31SOndrej Jirman 47680b79e31SOndrej Jirman /* Enable the output on the pins */ 47780b79e31SOndrej Jirman regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0xe0000000); 478a0c1214eSMaxime Ripard } 479a0c1214eSMaxime Ripard 480ba19c537SMaxime Ripard static void sun4i_tcon0_mode_set_rgb(struct sun4i_tcon *tcon, 481b842e2c9SPaul Kocialkowski const struct drm_encoder *encoder, 4825b8f0910SMaxime Ripard const struct drm_display_mode *mode) 4839026e0d1SMaxime Ripard { 4844843c9a2SPaul Kocialkowski struct drm_connector *connector = sun4i_tcon_get_connector(encoder); 4854843c9a2SPaul Kocialkowski struct drm_display_info display_info = connector->display_info; 4869026e0d1SMaxime Ripard unsigned int bp, hsync, vsync; 4879026e0d1SMaxime Ripard u8 clk_delay; 4889026e0d1SMaxime Ripard u32 val = 0; 4899026e0d1SMaxime Ripard 49034d698f6SJernej Skrabec WARN_ON(!tcon->quirks->has_channel_0); 49134d698f6SJernej Skrabec 492ec08d596SMaxime Ripard tcon->dclk_min_div = 6; 493ec08d596SMaxime Ripard tcon->dclk_max_div = 127; 494ba19c537SMaxime Ripard sun4i_tcon0_mode_set_common(tcon, mode); 49586cf6788SChen-Yu Tsai 496f11adcecSJonathan Liu /* Set dithering if needed */ 4974843c9a2SPaul Kocialkowski sun4i_tcon0_mode_set_dithering(tcon, connector); 498f11adcecSJonathan Liu 4999026e0d1SMaxime Ripard /* Adjust clock delay */ 5009026e0d1SMaxime Ripard clk_delay = sun4i_tcon_get_clk_delay(mode, 0); 5019026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 5029026e0d1SMaxime Ripard SUN4I_TCON0_CTL_CLK_DELAY_MASK, 5039026e0d1SMaxime Ripard SUN4I_TCON0_CTL_CLK_DELAY(clk_delay)); 5049026e0d1SMaxime Ripard 5059026e0d1SMaxime Ripard /* 5069026e0d1SMaxime Ripard * This is called a backporch in the register documentation, 50723a1cb11SChen-Yu Tsai * but it really is the back porch + hsync 5089026e0d1SMaxime Ripard */ 5099026e0d1SMaxime Ripard bp = mode->crtc_htotal - mode->crtc_hsync_start; 5109026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n", 5119026e0d1SMaxime Ripard mode->crtc_htotal, bp); 5129026e0d1SMaxime Ripard 5139026e0d1SMaxime Ripard /* Set horizontal display timings */ 5149026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG, 5159026e0d1SMaxime Ripard SUN4I_TCON0_BASIC1_H_TOTAL(mode->crtc_htotal) | 5169026e0d1SMaxime Ripard SUN4I_TCON0_BASIC1_H_BACKPORCH(bp)); 5179026e0d1SMaxime Ripard 5189026e0d1SMaxime Ripard /* 5199026e0d1SMaxime Ripard * This is called a backporch in the register documentation, 52023a1cb11SChen-Yu Tsai * but it really is the back porch + hsync 5219026e0d1SMaxime Ripard */ 5229026e0d1SMaxime Ripard bp = mode->crtc_vtotal - mode->crtc_vsync_start; 5239026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n", 5249026e0d1SMaxime Ripard mode->crtc_vtotal, bp); 5259026e0d1SMaxime Ripard 5269026e0d1SMaxime Ripard /* Set vertical display timings */ 5279026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG, 528a88cbbd4SMaxime Ripard SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) | 5299026e0d1SMaxime Ripard SUN4I_TCON0_BASIC2_V_BACKPORCH(bp)); 5309026e0d1SMaxime Ripard 5319026e0d1SMaxime Ripard /* Set Hsync and Vsync length */ 5329026e0d1SMaxime Ripard hsync = mode->crtc_hsync_end - mode->crtc_hsync_start; 5339026e0d1SMaxime Ripard vsync = mode->crtc_vsync_end - mode->crtc_vsync_start; 5349026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync); 5359026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC3_REG, 5369026e0d1SMaxime Ripard SUN4I_TCON0_BASIC3_V_SYNC(vsync) | 5379026e0d1SMaxime Ripard SUN4I_TCON0_BASIC3_H_SYNC(hsync)); 5389026e0d1SMaxime Ripard 5399026e0d1SMaxime Ripard /* Setup the polarity of the various signals */ 540fa4127c5SGiulio Benetti if (mode->flags & DRM_MODE_FLAG_PHSYNC) 5419026e0d1SMaxime Ripard val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE; 5429026e0d1SMaxime Ripard 543fa4127c5SGiulio Benetti if (mode->flags & DRM_MODE_FLAG_PVSYNC) 5449026e0d1SMaxime Ripard val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE; 5459026e0d1SMaxime Ripard 54665bf2d54SPaul Kocialkowski if (display_info.bus_flags & DRM_BUS_FLAG_DE_LOW) 54765bf2d54SPaul Kocialkowski val |= SUN4I_TCON0_IO_POL_DE_NEGATIVE; 54865bf2d54SPaul Kocialkowski 549490cda5aSGiulio Benetti /* 550490cda5aSGiulio Benetti * On A20 and similar SoCs, the only way to achieve Positive Edge 551490cda5aSGiulio Benetti * (Rising Edge), is setting dclk clock phase to 2/3(240°). 552490cda5aSGiulio Benetti * By default TCON works in Negative Edge(Falling Edge), 553490cda5aSGiulio Benetti * this is why phase is set to 0 in that case. 554490cda5aSGiulio Benetti * Unfortunately there's no way to logically invert dclk through 555490cda5aSGiulio Benetti * IO_POL register. 556490cda5aSGiulio Benetti * The only acceptable way to work, triple checked with scope, 557490cda5aSGiulio Benetti * is using clock phase set to 0° for Negative Edge and set to 240° 558490cda5aSGiulio Benetti * for Positive Edge. 559490cda5aSGiulio Benetti * On A33 and similar SoCs there would be a 90° phase option, 560490cda5aSGiulio Benetti * but it divides also dclk by 2. 561490cda5aSGiulio Benetti * Following code is a way to avoid quirks all around TCON 562490cda5aSGiulio Benetti * and DOTCLOCK drivers. 563490cda5aSGiulio Benetti */ 564490cda5aSGiulio Benetti if (display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_POSEDGE) 565490cda5aSGiulio Benetti clk_set_phase(tcon->dclk, 240); 566490cda5aSGiulio Benetti 567490cda5aSGiulio Benetti if (display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_NEGEDGE) 568490cda5aSGiulio Benetti clk_set_phase(tcon->dclk, 0); 569490cda5aSGiulio Benetti 5709026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_IO_POL_REG, 57165bf2d54SPaul Kocialkowski SUN4I_TCON0_IO_POL_HSYNC_POSITIVE | 57265bf2d54SPaul Kocialkowski SUN4I_TCON0_IO_POL_VSYNC_POSITIVE | 57365bf2d54SPaul Kocialkowski SUN4I_TCON0_IO_POL_DE_NEGATIVE, 5749026e0d1SMaxime Ripard val); 5759026e0d1SMaxime Ripard 5769026e0d1SMaxime Ripard /* Map output pins to channel 0 */ 5779026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 5789026e0d1SMaxime Ripard SUN4I_TCON_GCTL_IOMAP_MASK, 5799026e0d1SMaxime Ripard SUN4I_TCON_GCTL_IOMAP_TCON0); 5809026e0d1SMaxime Ripard 5819026e0d1SMaxime Ripard /* Enable the output on the pins */ 5829026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0); 5839026e0d1SMaxime Ripard } 5849026e0d1SMaxime Ripard 5855b8f0910SMaxime Ripard static void sun4i_tcon1_mode_set(struct sun4i_tcon *tcon, 5865b8f0910SMaxime Ripard const struct drm_display_mode *mode) 5879026e0d1SMaxime Ripard { 588b8317a3dSMaxime Ripard unsigned int bp, hsync, vsync, vtotal; 5899026e0d1SMaxime Ripard u8 clk_delay; 5909026e0d1SMaxime Ripard u32 val; 5919026e0d1SMaxime Ripard 59291ea2f29SChen-Yu Tsai WARN_ON(!tcon->quirks->has_channel_1); 5938e924047SMaxime Ripard 59486cf6788SChen-Yu Tsai /* Configure the dot clock */ 59586cf6788SChen-Yu Tsai clk_set_rate(tcon->sclk1, mode->crtc_clock * 1000); 59686cf6788SChen-Yu Tsai 5979026e0d1SMaxime Ripard /* Adjust clock delay */ 5989026e0d1SMaxime Ripard clk_delay = sun4i_tcon_get_clk_delay(mode, 1); 5999026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 6009026e0d1SMaxime Ripard SUN4I_TCON1_CTL_CLK_DELAY_MASK, 6019026e0d1SMaxime Ripard SUN4I_TCON1_CTL_CLK_DELAY(clk_delay)); 6029026e0d1SMaxime Ripard 6039026e0d1SMaxime Ripard /* Set interlaced mode */ 6049026e0d1SMaxime Ripard if (mode->flags & DRM_MODE_FLAG_INTERLACE) 6059026e0d1SMaxime Ripard val = SUN4I_TCON1_CTL_INTERLACE_ENABLE; 6069026e0d1SMaxime Ripard else 6079026e0d1SMaxime Ripard val = 0; 6089026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 6099026e0d1SMaxime Ripard SUN4I_TCON1_CTL_INTERLACE_ENABLE, 6109026e0d1SMaxime Ripard val); 6119026e0d1SMaxime Ripard 6129026e0d1SMaxime Ripard /* Set the input resolution */ 6139026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC0_REG, 6149026e0d1SMaxime Ripard SUN4I_TCON1_BASIC0_X(mode->crtc_hdisplay) | 6159026e0d1SMaxime Ripard SUN4I_TCON1_BASIC0_Y(mode->crtc_vdisplay)); 6169026e0d1SMaxime Ripard 6179026e0d1SMaxime Ripard /* Set the upscaling resolution */ 6189026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC1_REG, 6199026e0d1SMaxime Ripard SUN4I_TCON1_BASIC1_X(mode->crtc_hdisplay) | 6209026e0d1SMaxime Ripard SUN4I_TCON1_BASIC1_Y(mode->crtc_vdisplay)); 6219026e0d1SMaxime Ripard 6229026e0d1SMaxime Ripard /* Set the output resolution */ 6239026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC2_REG, 6249026e0d1SMaxime Ripard SUN4I_TCON1_BASIC2_X(mode->crtc_hdisplay) | 6259026e0d1SMaxime Ripard SUN4I_TCON1_BASIC2_Y(mode->crtc_vdisplay)); 6269026e0d1SMaxime Ripard 6279026e0d1SMaxime Ripard /* Set horizontal display timings */ 6283cb2f46bSMaxime Ripard bp = mode->crtc_htotal - mode->crtc_hsync_start; 6299026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n", 6309026e0d1SMaxime Ripard mode->htotal, bp); 6319026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC3_REG, 6329026e0d1SMaxime Ripard SUN4I_TCON1_BASIC3_H_TOTAL(mode->crtc_htotal) | 6339026e0d1SMaxime Ripard SUN4I_TCON1_BASIC3_H_BACKPORCH(bp)); 6349026e0d1SMaxime Ripard 6353cb2f46bSMaxime Ripard bp = mode->crtc_vtotal - mode->crtc_vsync_start; 6369026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n", 637b8317a3dSMaxime Ripard mode->crtc_vtotal, bp); 638b8317a3dSMaxime Ripard 639b8317a3dSMaxime Ripard /* 640b8317a3dSMaxime Ripard * The vertical resolution needs to be doubled in all 641b8317a3dSMaxime Ripard * cases. We could use crtc_vtotal and always multiply by two, 642b8317a3dSMaxime Ripard * but that leads to a rounding error in interlace when vtotal 643b8317a3dSMaxime Ripard * is odd. 644b8317a3dSMaxime Ripard * 645b8317a3dSMaxime Ripard * This happens with TV's PAL for example, where vtotal will 646b8317a3dSMaxime Ripard * be 625, crtc_vtotal 312, and thus crtc_vtotal * 2 will be 647b8317a3dSMaxime Ripard * 624, which apparently confuses the hardware. 648b8317a3dSMaxime Ripard * 649b8317a3dSMaxime Ripard * To work around this, we will always use vtotal, and 650b8317a3dSMaxime Ripard * multiply by two only if we're not in interlace. 651b8317a3dSMaxime Ripard */ 652b8317a3dSMaxime Ripard vtotal = mode->vtotal; 653b8317a3dSMaxime Ripard if (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) 654b8317a3dSMaxime Ripard vtotal = vtotal * 2; 655b8317a3dSMaxime Ripard 656b8317a3dSMaxime Ripard /* Set vertical display timings */ 6579026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC4_REG, 658b8317a3dSMaxime Ripard SUN4I_TCON1_BASIC4_V_TOTAL(vtotal) | 6599026e0d1SMaxime Ripard SUN4I_TCON1_BASIC4_V_BACKPORCH(bp)); 6609026e0d1SMaxime Ripard 6619026e0d1SMaxime Ripard /* Set Hsync and Vsync length */ 6629026e0d1SMaxime Ripard hsync = mode->crtc_hsync_end - mode->crtc_hsync_start; 6639026e0d1SMaxime Ripard vsync = mode->crtc_vsync_end - mode->crtc_vsync_start; 6649026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync); 6659026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC5_REG, 6669026e0d1SMaxime Ripard SUN4I_TCON1_BASIC5_V_SYNC(vsync) | 6679026e0d1SMaxime Ripard SUN4I_TCON1_BASIC5_H_SYNC(hsync)); 6689026e0d1SMaxime Ripard 6699026e0d1SMaxime Ripard /* Map output pins to channel 1 */ 6709026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 6719026e0d1SMaxime Ripard SUN4I_TCON_GCTL_IOMAP_MASK, 6729026e0d1SMaxime Ripard SUN4I_TCON_GCTL_IOMAP_TCON1); 6739026e0d1SMaxime Ripard } 6745b8f0910SMaxime Ripard 6755b8f0910SMaxime Ripard void sun4i_tcon_mode_set(struct sun4i_tcon *tcon, 6765b8f0910SMaxime Ripard const struct drm_encoder *encoder, 6775b8f0910SMaxime Ripard const struct drm_display_mode *mode) 6785b8f0910SMaxime Ripard { 6795b8f0910SMaxime Ripard switch (encoder->encoder_type) { 680a08fc7c8SMaxime Ripard case DRM_MODE_ENCODER_DSI: 68179891d56SChen-Yu Tsai /* DSI is tied to special case of CPU interface */ 68279891d56SChen-Yu Tsai sun4i_tcon0_mode_set_cpu(tcon, encoder, mode); 683a08fc7c8SMaxime Ripard break; 684a0c1214eSMaxime Ripard case DRM_MODE_ENCODER_LVDS: 685a0c1214eSMaxime Ripard sun4i_tcon0_mode_set_lvds(tcon, encoder, mode); 686a0c1214eSMaxime Ripard break; 6875b8f0910SMaxime Ripard case DRM_MODE_ENCODER_NONE: 688b842e2c9SPaul Kocialkowski sun4i_tcon0_mode_set_rgb(tcon, encoder, mode); 6895b8f0910SMaxime Ripard sun4i_tcon_set_mux(tcon, 0, encoder); 6905b8f0910SMaxime Ripard break; 6915b8f0910SMaxime Ripard case DRM_MODE_ENCODER_TVDAC: 6925b8f0910SMaxime Ripard case DRM_MODE_ENCODER_TMDS: 6935b8f0910SMaxime Ripard sun4i_tcon1_mode_set(tcon, mode); 6945b8f0910SMaxime Ripard sun4i_tcon_set_mux(tcon, 1, encoder); 6955b8f0910SMaxime Ripard break; 6965b8f0910SMaxime Ripard default: 6975b8f0910SMaxime Ripard DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n"); 6985b8f0910SMaxime Ripard } 6995b8f0910SMaxime Ripard } 7005b8f0910SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_mode_set); 7019026e0d1SMaxime Ripard 7029026e0d1SMaxime Ripard static void sun4i_tcon_finish_page_flip(struct drm_device *dev, 7039026e0d1SMaxime Ripard struct sun4i_crtc *scrtc) 7049026e0d1SMaxime Ripard { 7059026e0d1SMaxime Ripard unsigned long flags; 7069026e0d1SMaxime Ripard 7079026e0d1SMaxime Ripard spin_lock_irqsave(&dev->event_lock, flags); 7089026e0d1SMaxime Ripard if (scrtc->event) { 7099026e0d1SMaxime Ripard drm_crtc_send_vblank_event(&scrtc->crtc, scrtc->event); 7109026e0d1SMaxime Ripard drm_crtc_vblank_put(&scrtc->crtc); 7119026e0d1SMaxime Ripard scrtc->event = NULL; 7129026e0d1SMaxime Ripard } 7139026e0d1SMaxime Ripard spin_unlock_irqrestore(&dev->event_lock, flags); 7149026e0d1SMaxime Ripard } 7159026e0d1SMaxime Ripard 7169026e0d1SMaxime Ripard static irqreturn_t sun4i_tcon_handler(int irq, void *private) 7179026e0d1SMaxime Ripard { 7189026e0d1SMaxime Ripard struct sun4i_tcon *tcon = private; 7199026e0d1SMaxime Ripard struct drm_device *drm = tcon->drm; 72046cce6daSChen-Yu Tsai struct sun4i_crtc *scrtc = tcon->crtc; 7213004f75fSMaxime Ripard struct sunxi_engine *engine = scrtc->engine; 7229026e0d1SMaxime Ripard unsigned int status; 7239026e0d1SMaxime Ripard 7249026e0d1SMaxime Ripard regmap_read(tcon->regs, SUN4I_TCON_GINT0_REG, &status); 7259026e0d1SMaxime Ripard 7269026e0d1SMaxime Ripard if (!(status & (SUN4I_TCON_GINT0_VBLANK_INT(0) | 727a493ceaeSMaxime Ripard SUN4I_TCON_GINT0_VBLANK_INT(1) | 728a493ceaeSMaxime Ripard SUN4I_TCON_GINT0_TCON0_TRI_FINISH_INT))) 7299026e0d1SMaxime Ripard return IRQ_NONE; 7309026e0d1SMaxime Ripard 7319026e0d1SMaxime Ripard drm_crtc_handle_vblank(&scrtc->crtc); 7329026e0d1SMaxime Ripard sun4i_tcon_finish_page_flip(drm, scrtc); 7339026e0d1SMaxime Ripard 7349026e0d1SMaxime Ripard /* Acknowledge the interrupt */ 7359026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, 7369026e0d1SMaxime Ripard SUN4I_TCON_GINT0_VBLANK_INT(0) | 737a493ceaeSMaxime Ripard SUN4I_TCON_GINT0_VBLANK_INT(1) | 738a493ceaeSMaxime Ripard SUN4I_TCON_GINT0_TCON0_TRI_FINISH_INT, 7399026e0d1SMaxime Ripard 0); 7409026e0d1SMaxime Ripard 7413004f75fSMaxime Ripard if (engine->ops->vblank_quirk) 7423004f75fSMaxime Ripard engine->ops->vblank_quirk(engine); 7433004f75fSMaxime Ripard 7449026e0d1SMaxime Ripard return IRQ_HANDLED; 7459026e0d1SMaxime Ripard } 7469026e0d1SMaxime Ripard 7479026e0d1SMaxime Ripard static int sun4i_tcon_init_clocks(struct device *dev, 7489026e0d1SMaxime Ripard struct sun4i_tcon *tcon) 7499026e0d1SMaxime Ripard { 7509026e0d1SMaxime Ripard tcon->clk = devm_clk_get(dev, "ahb"); 7519026e0d1SMaxime Ripard if (IS_ERR(tcon->clk)) { 7529026e0d1SMaxime Ripard dev_err(dev, "Couldn't get the TCON bus clock\n"); 7539026e0d1SMaxime Ripard return PTR_ERR(tcon->clk); 7549026e0d1SMaxime Ripard } 7559026e0d1SMaxime Ripard clk_prepare_enable(tcon->clk); 7569026e0d1SMaxime Ripard 75734d698f6SJernej Skrabec if (tcon->quirks->has_channel_0) { 7589026e0d1SMaxime Ripard tcon->sclk0 = devm_clk_get(dev, "tcon-ch0"); 7599026e0d1SMaxime Ripard if (IS_ERR(tcon->sclk0)) { 7609026e0d1SMaxime Ripard dev_err(dev, "Couldn't get the TCON channel 0 clock\n"); 7619026e0d1SMaxime Ripard return PTR_ERR(tcon->sclk0); 7629026e0d1SMaxime Ripard } 76334d698f6SJernej Skrabec } 764*b14e945bSPaul Kocialkowski clk_prepare_enable(tcon->sclk0); 7659026e0d1SMaxime Ripard 76691ea2f29SChen-Yu Tsai if (tcon->quirks->has_channel_1) { 7679026e0d1SMaxime Ripard tcon->sclk1 = devm_clk_get(dev, "tcon-ch1"); 7689026e0d1SMaxime Ripard if (IS_ERR(tcon->sclk1)) { 7699026e0d1SMaxime Ripard dev_err(dev, "Couldn't get the TCON channel 1 clock\n"); 7709026e0d1SMaxime Ripard return PTR_ERR(tcon->sclk1); 7719026e0d1SMaxime Ripard } 7728e924047SMaxime Ripard } 7739026e0d1SMaxime Ripard 7744c7f16d1SChen-Yu Tsai return 0; 7759026e0d1SMaxime Ripard } 7769026e0d1SMaxime Ripard 7779026e0d1SMaxime Ripard static void sun4i_tcon_free_clocks(struct sun4i_tcon *tcon) 7789026e0d1SMaxime Ripard { 779*b14e945bSPaul Kocialkowski clk_disable_unprepare(tcon->sclk0); 7809026e0d1SMaxime Ripard clk_disable_unprepare(tcon->clk); 7819026e0d1SMaxime Ripard } 7829026e0d1SMaxime Ripard 7839026e0d1SMaxime Ripard static int sun4i_tcon_init_irq(struct device *dev, 7849026e0d1SMaxime Ripard struct sun4i_tcon *tcon) 7859026e0d1SMaxime Ripard { 7869026e0d1SMaxime Ripard struct platform_device *pdev = to_platform_device(dev); 7879026e0d1SMaxime Ripard int irq, ret; 7889026e0d1SMaxime Ripard 7899026e0d1SMaxime Ripard irq = platform_get_irq(pdev, 0); 7909026e0d1SMaxime Ripard if (irq < 0) { 7919026e0d1SMaxime Ripard dev_err(dev, "Couldn't retrieve the TCON interrupt\n"); 7929026e0d1SMaxime Ripard return irq; 7939026e0d1SMaxime Ripard } 7949026e0d1SMaxime Ripard 7959026e0d1SMaxime Ripard ret = devm_request_irq(dev, irq, sun4i_tcon_handler, 0, 7969026e0d1SMaxime Ripard dev_name(dev), tcon); 7979026e0d1SMaxime Ripard if (ret) { 7989026e0d1SMaxime Ripard dev_err(dev, "Couldn't request the IRQ\n"); 7999026e0d1SMaxime Ripard return ret; 8009026e0d1SMaxime Ripard } 8019026e0d1SMaxime Ripard 8029026e0d1SMaxime Ripard return 0; 8039026e0d1SMaxime Ripard } 8049026e0d1SMaxime Ripard 8059026e0d1SMaxime Ripard static struct regmap_config sun4i_tcon_regmap_config = { 8069026e0d1SMaxime Ripard .reg_bits = 32, 8079026e0d1SMaxime Ripard .val_bits = 32, 8089026e0d1SMaxime Ripard .reg_stride = 4, 8099026e0d1SMaxime Ripard .max_register = 0x800, 8109026e0d1SMaxime Ripard }; 8119026e0d1SMaxime Ripard 8129026e0d1SMaxime Ripard static int sun4i_tcon_init_regmap(struct device *dev, 8139026e0d1SMaxime Ripard struct sun4i_tcon *tcon) 8149026e0d1SMaxime Ripard { 8159026e0d1SMaxime Ripard struct platform_device *pdev = to_platform_device(dev); 8169026e0d1SMaxime Ripard struct resource *res; 8179026e0d1SMaxime Ripard void __iomem *regs; 8189026e0d1SMaxime Ripard 8199026e0d1SMaxime Ripard res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 8209026e0d1SMaxime Ripard regs = devm_ioremap_resource(dev, res); 821af346f55SWei Yongjun if (IS_ERR(regs)) 8229026e0d1SMaxime Ripard return PTR_ERR(regs); 8239026e0d1SMaxime Ripard 8249026e0d1SMaxime Ripard tcon->regs = devm_regmap_init_mmio(dev, regs, 8259026e0d1SMaxime Ripard &sun4i_tcon_regmap_config); 8269026e0d1SMaxime Ripard if (IS_ERR(tcon->regs)) { 8279026e0d1SMaxime Ripard dev_err(dev, "Couldn't create the TCON regmap\n"); 8289026e0d1SMaxime Ripard return PTR_ERR(tcon->regs); 8299026e0d1SMaxime Ripard } 8309026e0d1SMaxime Ripard 8319026e0d1SMaxime Ripard /* Make sure the TCON is disabled and all IRQs are off */ 8329026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_GCTL_REG, 0); 8339026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_GINT0_REG, 0); 8349026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_GINT1_REG, 0); 8359026e0d1SMaxime Ripard 8369026e0d1SMaxime Ripard /* Disable IO lines and set them to tristate */ 8379026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, ~0); 8389026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_IO_TRI_REG, ~0); 8399026e0d1SMaxime Ripard 8409026e0d1SMaxime Ripard return 0; 8419026e0d1SMaxime Ripard } 8429026e0d1SMaxime Ripard 843b317fa3bSChen-Yu Tsai /* 844b317fa3bSChen-Yu Tsai * On SoCs with the old display pipeline design (Display Engine 1.0), 845b317fa3bSChen-Yu Tsai * the TCON is always tied to just one backend. Hence we can traverse 846b317fa3bSChen-Yu Tsai * the of_graph upwards to find the backend our tcon is connected to, 847b317fa3bSChen-Yu Tsai * and take its ID as our own. 848b317fa3bSChen-Yu Tsai * 849b317fa3bSChen-Yu Tsai * We can either identify backends from their compatible strings, which 850b317fa3bSChen-Yu Tsai * means maintaining a large list of them. Or, since the backend is 851b317fa3bSChen-Yu Tsai * registered and binded before the TCON, we can just go through the 852b317fa3bSChen-Yu Tsai * list of registered backends and compare the device node. 85387969338SIcenowy Zheng * 85487969338SIcenowy Zheng * As the structures now store engines instead of backends, here this 85587969338SIcenowy Zheng * function in fact searches the corresponding engine, and the ID is 85687969338SIcenowy Zheng * requested via the get_id function of the engine. 857b317fa3bSChen-Yu Tsai */ 858e8d5bbf7SChen-Yu Tsai static struct sunxi_engine * 859e8d5bbf7SChen-Yu Tsai sun4i_tcon_find_engine_traverse(struct sun4i_drv *drv, 86049836b11SJernej Skrabec struct device_node *node, 86149836b11SJernej Skrabec u32 port_id) 862b317fa3bSChen-Yu Tsai { 863b317fa3bSChen-Yu Tsai struct device_node *port, *ep, *remote; 864be3fe0f9SChen-Yu Tsai struct sunxi_engine *engine = ERR_PTR(-EINVAL); 86549836b11SJernej Skrabec u32 reg = 0; 866b317fa3bSChen-Yu Tsai 86749836b11SJernej Skrabec port = of_graph_get_port_by_id(node, port_id); 868b317fa3bSChen-Yu Tsai if (!port) 869b317fa3bSChen-Yu Tsai return ERR_PTR(-EINVAL); 870b317fa3bSChen-Yu Tsai 8711469619dSChen-Yu Tsai /* 8721469619dSChen-Yu Tsai * This only works if there is only one path from the TCON 8731469619dSChen-Yu Tsai * to any display engine. Otherwise the probe order of the 8741469619dSChen-Yu Tsai * TCONs and display engines is not guaranteed. They may 8751469619dSChen-Yu Tsai * either bind to the wrong one, or worse, bind to the same 8761469619dSChen-Yu Tsai * one if additional checks are not done. 8771469619dSChen-Yu Tsai * 8781469619dSChen-Yu Tsai * Bail out if there are multiple input connections. 8791469619dSChen-Yu Tsai */ 880be3fe0f9SChen-Yu Tsai if (of_get_available_child_count(port) != 1) 881be3fe0f9SChen-Yu Tsai goto out_put_port; 8821469619dSChen-Yu Tsai 883be3fe0f9SChen-Yu Tsai /* Get the first connection without specifying an ID */ 884be3fe0f9SChen-Yu Tsai ep = of_get_next_available_child(port, NULL); 885be3fe0f9SChen-Yu Tsai if (!ep) 886be3fe0f9SChen-Yu Tsai goto out_put_port; 887be3fe0f9SChen-Yu Tsai 888b317fa3bSChen-Yu Tsai remote = of_graph_get_remote_port_parent(ep); 889b317fa3bSChen-Yu Tsai if (!remote) 890be3fe0f9SChen-Yu Tsai goto out_put_ep; 891b317fa3bSChen-Yu Tsai 89287969338SIcenowy Zheng /* does this node match any registered engines? */ 893be3fe0f9SChen-Yu Tsai list_for_each_entry(engine, &drv->engine_list, list) 894be3fe0f9SChen-Yu Tsai if (remote == engine->node) 895be3fe0f9SChen-Yu Tsai goto out_put_remote; 896b317fa3bSChen-Yu Tsai 89749836b11SJernej Skrabec /* 89849836b11SJernej Skrabec * According to device tree binding input ports have even id 89949836b11SJernej Skrabec * number and output ports have odd id. Since component with 90049836b11SJernej Skrabec * more than one input and one output (TCON TOP) exits, correct 90149836b11SJernej Skrabec * remote input id has to be calculated by subtracting 1 from 90249836b11SJernej Skrabec * remote output id. If this for some reason can't be done, 0 90349836b11SJernej Skrabec * is used as input port id. 90449836b11SJernej Skrabec */ 905da82107eSJernej Skrabec of_node_put(port); 90649836b11SJernej Skrabec port = of_graph_get_remote_port(ep); 90749836b11SJernej Skrabec if (!of_property_read_u32(port, "reg", ®) && reg > 0) 90849836b11SJernej Skrabec reg -= 1; 90949836b11SJernej Skrabec 910b317fa3bSChen-Yu Tsai /* keep looking through upstream ports */ 91149836b11SJernej Skrabec engine = sun4i_tcon_find_engine_traverse(drv, remote, reg); 912b317fa3bSChen-Yu Tsai 913be3fe0f9SChen-Yu Tsai out_put_remote: 914be3fe0f9SChen-Yu Tsai of_node_put(remote); 915be3fe0f9SChen-Yu Tsai out_put_ep: 916be3fe0f9SChen-Yu Tsai of_node_put(ep); 917be3fe0f9SChen-Yu Tsai out_put_port: 918be3fe0f9SChen-Yu Tsai of_node_put(port); 919be3fe0f9SChen-Yu Tsai 920be3fe0f9SChen-Yu Tsai return engine; 921b317fa3bSChen-Yu Tsai } 922b317fa3bSChen-Yu Tsai 923e8d5bbf7SChen-Yu Tsai /* 924e8d5bbf7SChen-Yu Tsai * The device tree binding says that the remote endpoint ID of any 925e8d5bbf7SChen-Yu Tsai * connection between components, up to and including the TCON, of 926e8d5bbf7SChen-Yu Tsai * the display pipeline should be equal to the actual ID of the local 927e8d5bbf7SChen-Yu Tsai * component. Thus we can look at any one of the input connections of 928e8d5bbf7SChen-Yu Tsai * the TCONs, and use that connection's remote endpoint ID as our own. 929e8d5bbf7SChen-Yu Tsai * 930e8d5bbf7SChen-Yu Tsai * Since the user of this function already finds the input port, 931e8d5bbf7SChen-Yu Tsai * the port is passed in directly without further checks. 932e8d5bbf7SChen-Yu Tsai */ 933e8d5bbf7SChen-Yu Tsai static int sun4i_tcon_of_get_id_from_port(struct device_node *port) 934e8d5bbf7SChen-Yu Tsai { 935e8d5bbf7SChen-Yu Tsai struct device_node *ep; 936e8d5bbf7SChen-Yu Tsai int ret = -EINVAL; 937e8d5bbf7SChen-Yu Tsai 938e8d5bbf7SChen-Yu Tsai /* try finding an upstream endpoint */ 939e8d5bbf7SChen-Yu Tsai for_each_available_child_of_node(port, ep) { 940e8d5bbf7SChen-Yu Tsai struct device_node *remote; 941e8d5bbf7SChen-Yu Tsai u32 reg; 942e8d5bbf7SChen-Yu Tsai 943e8d5bbf7SChen-Yu Tsai remote = of_graph_get_remote_endpoint(ep); 944e8d5bbf7SChen-Yu Tsai if (!remote) 945e8d5bbf7SChen-Yu Tsai continue; 946e8d5bbf7SChen-Yu Tsai 947e8d5bbf7SChen-Yu Tsai ret = of_property_read_u32(remote, "reg", ®); 948e8d5bbf7SChen-Yu Tsai if (ret) 949e8d5bbf7SChen-Yu Tsai continue; 950e8d5bbf7SChen-Yu Tsai 951e8d5bbf7SChen-Yu Tsai ret = reg; 952e8d5bbf7SChen-Yu Tsai } 953e8d5bbf7SChen-Yu Tsai 954e8d5bbf7SChen-Yu Tsai return ret; 955e8d5bbf7SChen-Yu Tsai } 956e8d5bbf7SChen-Yu Tsai 957e8d5bbf7SChen-Yu Tsai /* 958e8d5bbf7SChen-Yu Tsai * Once we know the TCON's id, we can look through the list of 959e8d5bbf7SChen-Yu Tsai * engines to find a matching one. We assume all engines have 960e8d5bbf7SChen-Yu Tsai * been probed and added to the list. 961e8d5bbf7SChen-Yu Tsai */ 962e8d5bbf7SChen-Yu Tsai static struct sunxi_engine *sun4i_tcon_get_engine_by_id(struct sun4i_drv *drv, 963e8d5bbf7SChen-Yu Tsai int id) 964e8d5bbf7SChen-Yu Tsai { 965e8d5bbf7SChen-Yu Tsai struct sunxi_engine *engine; 966e8d5bbf7SChen-Yu Tsai 967e8d5bbf7SChen-Yu Tsai list_for_each_entry(engine, &drv->engine_list, list) 968e8d5bbf7SChen-Yu Tsai if (engine->id == id) 969e8d5bbf7SChen-Yu Tsai return engine; 970e8d5bbf7SChen-Yu Tsai 971e8d5bbf7SChen-Yu Tsai return ERR_PTR(-EINVAL); 972e8d5bbf7SChen-Yu Tsai } 973e8d5bbf7SChen-Yu Tsai 974cf77d79bSJernej Skrabec static bool sun4i_tcon_connected_to_tcon_top(struct device_node *node) 975cf77d79bSJernej Skrabec { 976cf77d79bSJernej Skrabec struct device_node *remote; 977cf77d79bSJernej Skrabec bool ret = false; 978cf77d79bSJernej Skrabec 979cf77d79bSJernej Skrabec remote = of_graph_get_remote_node(node, 0, -1); 980cf77d79bSJernej Skrabec if (remote) { 981185e0bebSMaxime Ripard ret = !!(IS_ENABLED(CONFIG_DRM_SUN8I_TCON_TOP) && 982185e0bebSMaxime Ripard of_match_node(sun8i_tcon_top_of_table, remote)); 983cf77d79bSJernej Skrabec of_node_put(remote); 984cf77d79bSJernej Skrabec } 985cf77d79bSJernej Skrabec 986cf77d79bSJernej Skrabec return ret; 987cf77d79bSJernej Skrabec } 988cf77d79bSJernej Skrabec 989cf77d79bSJernej Skrabec static int sun4i_tcon_get_index(struct sun4i_drv *drv) 990cf77d79bSJernej Skrabec { 991cf77d79bSJernej Skrabec struct list_head *pos; 992cf77d79bSJernej Skrabec int size = 0; 993cf77d79bSJernej Skrabec 994cf77d79bSJernej Skrabec /* 995cf77d79bSJernej Skrabec * Because TCON is added to the list at the end of the probe 996cf77d79bSJernej Skrabec * (after this function is called), index of the current TCON 997cf77d79bSJernej Skrabec * will be same as current TCON list size. 998cf77d79bSJernej Skrabec */ 999cf77d79bSJernej Skrabec list_for_each(pos, &drv->tcon_list) 1000cf77d79bSJernej Skrabec ++size; 1001cf77d79bSJernej Skrabec 1002cf77d79bSJernej Skrabec return size; 1003cf77d79bSJernej Skrabec } 1004cf77d79bSJernej Skrabec 1005e8d5bbf7SChen-Yu Tsai /* 1006e8d5bbf7SChen-Yu Tsai * On SoCs with the old display pipeline design (Display Engine 1.0), 1007e8d5bbf7SChen-Yu Tsai * we assumed the TCON was always tied to just one backend. However 1008e8d5bbf7SChen-Yu Tsai * this proved not to be the case. On the A31, the TCON can select 1009e8d5bbf7SChen-Yu Tsai * either backend as its source. On the A20 (and likely on the A10), 1010e8d5bbf7SChen-Yu Tsai * the backend can choose which TCON to output to. 1011e8d5bbf7SChen-Yu Tsai * 1012e8d5bbf7SChen-Yu Tsai * The device tree binding says that the remote endpoint ID of any 1013e8d5bbf7SChen-Yu Tsai * connection between components, up to and including the TCON, of 1014e8d5bbf7SChen-Yu Tsai * the display pipeline should be equal to the actual ID of the local 1015e8d5bbf7SChen-Yu Tsai * component. Thus we should be able to look at any one of the input 1016e8d5bbf7SChen-Yu Tsai * connections of the TCONs, and use that connection's remote endpoint 1017e8d5bbf7SChen-Yu Tsai * ID as our own. 1018e8d5bbf7SChen-Yu Tsai * 1019e8d5bbf7SChen-Yu Tsai * However the connections between the backend and TCON were assumed 1020e8d5bbf7SChen-Yu Tsai * to be always singular, and their endpoit IDs were all incorrectly 1021e8d5bbf7SChen-Yu Tsai * set to 0. This means for these old device trees, we cannot just look 1022e8d5bbf7SChen-Yu Tsai * up the remote endpoint ID of a TCON input endpoint. TCON1 would be 1023e8d5bbf7SChen-Yu Tsai * incorrectly identified as TCON0. 1024e8d5bbf7SChen-Yu Tsai * 1025e8d5bbf7SChen-Yu Tsai * This function first checks if the TCON node has 2 input endpoints. 1026e8d5bbf7SChen-Yu Tsai * If so, then the device tree is a corrected version, and it will use 1027e8d5bbf7SChen-Yu Tsai * sun4i_tcon_of_get_id() and sun4i_tcon_get_engine_by_id() from above 1028e8d5bbf7SChen-Yu Tsai * to fetch the ID and engine directly. If not, then it is likely an 1029e8d5bbf7SChen-Yu Tsai * old device trees, where the endpoint IDs were incorrect, but did not 1030e8d5bbf7SChen-Yu Tsai * have endpoint connections between the backend and TCON across 1031e8d5bbf7SChen-Yu Tsai * different display pipelines. It will fall back to the old method of 1032e8d5bbf7SChen-Yu Tsai * traversing the of_graph to try and find a matching engine by device 1033e8d5bbf7SChen-Yu Tsai * node. 1034e8d5bbf7SChen-Yu Tsai * 1035e8d5bbf7SChen-Yu Tsai * In the case of single display pipeline device trees, either method 1036e8d5bbf7SChen-Yu Tsai * works. 1037e8d5bbf7SChen-Yu Tsai */ 1038e8d5bbf7SChen-Yu Tsai static struct sunxi_engine *sun4i_tcon_find_engine(struct sun4i_drv *drv, 1039e8d5bbf7SChen-Yu Tsai struct device_node *node) 1040e8d5bbf7SChen-Yu Tsai { 1041e8d5bbf7SChen-Yu Tsai struct device_node *port; 1042e8d5bbf7SChen-Yu Tsai struct sunxi_engine *engine; 1043e8d5bbf7SChen-Yu Tsai 1044e8d5bbf7SChen-Yu Tsai port = of_graph_get_port_by_id(node, 0); 1045e8d5bbf7SChen-Yu Tsai if (!port) 1046e8d5bbf7SChen-Yu Tsai return ERR_PTR(-EINVAL); 1047e8d5bbf7SChen-Yu Tsai 1048e8d5bbf7SChen-Yu Tsai /* 1049e8d5bbf7SChen-Yu Tsai * Is this a corrected device tree with cross pipeline 1050e8d5bbf7SChen-Yu Tsai * connections between the backend and TCON? 1051e8d5bbf7SChen-Yu Tsai */ 1052e8d5bbf7SChen-Yu Tsai if (of_get_child_count(port) > 1) { 1053cf77d79bSJernej Skrabec int id; 1054cf77d79bSJernej Skrabec 1055cf77d79bSJernej Skrabec /* 1056cf77d79bSJernej Skrabec * When pipeline has the same number of TCONs and engines which 1057cf77d79bSJernej Skrabec * are represented by frontends/backends (DE1) or mixers (DE2), 1058cf77d79bSJernej Skrabec * we match them by their respective IDs. However, if pipeline 1059cf77d79bSJernej Skrabec * contains TCON TOP, chances are that there are either more 1060cf77d79bSJernej Skrabec * TCONs than engines (R40) or TCONs with non-consecutive ids. 1061cf77d79bSJernej Skrabec * (H6). In that case it's easier just use TCON index in list 1062cf77d79bSJernej Skrabec * as an id. That means that on R40, any 2 TCONs can be enabled 1063cf77d79bSJernej Skrabec * in DT out of 4 (there are 2 mixers). Due to the design of 1064cf77d79bSJernej Skrabec * TCON TOP, remaining 2 TCONs can't be connected to anything 1065cf77d79bSJernej Skrabec * anyway. 1066cf77d79bSJernej Skrabec */ 1067cf77d79bSJernej Skrabec if (sun4i_tcon_connected_to_tcon_top(node)) 1068cf77d79bSJernej Skrabec id = sun4i_tcon_get_index(drv); 1069cf77d79bSJernej Skrabec else 1070cf77d79bSJernej Skrabec id = sun4i_tcon_of_get_id_from_port(port); 1071e8d5bbf7SChen-Yu Tsai 1072e8d5bbf7SChen-Yu Tsai /* Get our engine by matching our ID */ 1073e8d5bbf7SChen-Yu Tsai engine = sun4i_tcon_get_engine_by_id(drv, id); 1074e8d5bbf7SChen-Yu Tsai 1075e8d5bbf7SChen-Yu Tsai of_node_put(port); 1076e8d5bbf7SChen-Yu Tsai return engine; 1077e8d5bbf7SChen-Yu Tsai } 1078e8d5bbf7SChen-Yu Tsai 1079e8d5bbf7SChen-Yu Tsai /* Fallback to old method by traversing input endpoints */ 1080e8d5bbf7SChen-Yu Tsai of_node_put(port); 108149836b11SJernej Skrabec return sun4i_tcon_find_engine_traverse(drv, node, 0); 1082e8d5bbf7SChen-Yu Tsai } 1083e8d5bbf7SChen-Yu Tsai 10849026e0d1SMaxime Ripard static int sun4i_tcon_bind(struct device *dev, struct device *master, 10859026e0d1SMaxime Ripard void *data) 10869026e0d1SMaxime Ripard { 10879026e0d1SMaxime Ripard struct drm_device *drm = data; 10889026e0d1SMaxime Ripard struct sun4i_drv *drv = drm->dev_private; 108987969338SIcenowy Zheng struct sunxi_engine *engine; 1090a0c1214eSMaxime Ripard struct device_node *remote; 10919026e0d1SMaxime Ripard struct sun4i_tcon *tcon; 10926664e9dcSChen-Yu Tsai struct reset_control *edp_rstc; 1093a0c1214eSMaxime Ripard bool has_lvds_rst, has_lvds_alt, can_lvds; 10949026e0d1SMaxime Ripard int ret; 10959026e0d1SMaxime Ripard 109687969338SIcenowy Zheng engine = sun4i_tcon_find_engine(drv, dev->of_node); 109787969338SIcenowy Zheng if (IS_ERR(engine)) { 109887969338SIcenowy Zheng dev_err(dev, "Couldn't find matching engine\n"); 109980a58240SChen-Yu Tsai return -EPROBE_DEFER; 1100b317fa3bSChen-Yu Tsai } 110180a58240SChen-Yu Tsai 11029026e0d1SMaxime Ripard tcon = devm_kzalloc(dev, sizeof(*tcon), GFP_KERNEL); 11039026e0d1SMaxime Ripard if (!tcon) 11049026e0d1SMaxime Ripard return -ENOMEM; 11059026e0d1SMaxime Ripard dev_set_drvdata(dev, tcon); 11069026e0d1SMaxime Ripard tcon->drm = drm; 1107ae558110SMaxime Ripard tcon->dev = dev; 110887969338SIcenowy Zheng tcon->id = engine->id; 110991ea2f29SChen-Yu Tsai tcon->quirks = of_device_get_match_data(dev); 11109026e0d1SMaxime Ripard 11119026e0d1SMaxime Ripard tcon->lcd_rst = devm_reset_control_get(dev, "lcd"); 11129026e0d1SMaxime Ripard if (IS_ERR(tcon->lcd_rst)) { 11139026e0d1SMaxime Ripard dev_err(dev, "Couldn't get our reset line\n"); 11149026e0d1SMaxime Ripard return PTR_ERR(tcon->lcd_rst); 11159026e0d1SMaxime Ripard } 11169026e0d1SMaxime Ripard 11176664e9dcSChen-Yu Tsai if (tcon->quirks->needs_edp_reset) { 11186664e9dcSChen-Yu Tsai edp_rstc = devm_reset_control_get_shared(dev, "edp"); 11196664e9dcSChen-Yu Tsai if (IS_ERR(edp_rstc)) { 11206664e9dcSChen-Yu Tsai dev_err(dev, "Couldn't get edp reset line\n"); 11216664e9dcSChen-Yu Tsai return PTR_ERR(edp_rstc); 11226664e9dcSChen-Yu Tsai } 11236664e9dcSChen-Yu Tsai 11246664e9dcSChen-Yu Tsai ret = reset_control_deassert(edp_rstc); 11256664e9dcSChen-Yu Tsai if (ret) { 11266664e9dcSChen-Yu Tsai dev_err(dev, "Couldn't deassert edp reset line\n"); 11276664e9dcSChen-Yu Tsai return ret; 11286664e9dcSChen-Yu Tsai } 11296664e9dcSChen-Yu Tsai } 11306664e9dcSChen-Yu Tsai 11319026e0d1SMaxime Ripard /* Make sure our TCON is reset */ 1132d57294c1SChen-Yu Tsai ret = reset_control_reset(tcon->lcd_rst); 11339026e0d1SMaxime Ripard if (ret) { 11349026e0d1SMaxime Ripard dev_err(dev, "Couldn't deassert our reset line\n"); 11359026e0d1SMaxime Ripard return ret; 11369026e0d1SMaxime Ripard } 11379026e0d1SMaxime Ripard 1138e742a17cSMaxime Ripard if (tcon->quirks->supports_lvds) { 1139a0c1214eSMaxime Ripard /* 1140e742a17cSMaxime Ripard * This can only be made optional since we've had DT 1141e742a17cSMaxime Ripard * nodes without the LVDS reset properties. 1142a0c1214eSMaxime Ripard * 1143e742a17cSMaxime Ripard * If the property is missing, just disable LVDS, and 1144e742a17cSMaxime Ripard * print a warning. 1145a0c1214eSMaxime Ripard */ 1146a0c1214eSMaxime Ripard tcon->lvds_rst = devm_reset_control_get_optional(dev, "lvds"); 1147a0c1214eSMaxime Ripard if (IS_ERR(tcon->lvds_rst)) { 1148a0c1214eSMaxime Ripard dev_err(dev, "Couldn't get our reset line\n"); 1149a0c1214eSMaxime Ripard return PTR_ERR(tcon->lvds_rst); 1150a0c1214eSMaxime Ripard } else if (tcon->lvds_rst) { 1151a0c1214eSMaxime Ripard has_lvds_rst = true; 1152a0c1214eSMaxime Ripard reset_control_reset(tcon->lvds_rst); 1153a0c1214eSMaxime Ripard } else { 1154a0c1214eSMaxime Ripard has_lvds_rst = false; 1155a0c1214eSMaxime Ripard } 1156a0c1214eSMaxime Ripard 1157a0c1214eSMaxime Ripard /* 1158e742a17cSMaxime Ripard * This can only be made optional since we've had DT 1159e742a17cSMaxime Ripard * nodes without the LVDS reset properties. 1160a0c1214eSMaxime Ripard * 1161e742a17cSMaxime Ripard * If the property is missing, just disable LVDS, and 1162e742a17cSMaxime Ripard * print a warning. 1163a0c1214eSMaxime Ripard */ 1164a0c1214eSMaxime Ripard if (tcon->quirks->has_lvds_alt) { 1165a0c1214eSMaxime Ripard tcon->lvds_pll = devm_clk_get(dev, "lvds-alt"); 1166a0c1214eSMaxime Ripard if (IS_ERR(tcon->lvds_pll)) { 1167a0c1214eSMaxime Ripard if (PTR_ERR(tcon->lvds_pll) == -ENOENT) { 1168a0c1214eSMaxime Ripard has_lvds_alt = false; 1169a0c1214eSMaxime Ripard } else { 1170a0c1214eSMaxime Ripard dev_err(dev, "Couldn't get the LVDS PLL\n"); 117186a3ae58SDan Carpenter return PTR_ERR(tcon->lvds_pll); 1172a0c1214eSMaxime Ripard } 1173a0c1214eSMaxime Ripard } else { 1174a0c1214eSMaxime Ripard has_lvds_alt = true; 1175a0c1214eSMaxime Ripard } 1176a0c1214eSMaxime Ripard } 1177a0c1214eSMaxime Ripard 1178e742a17cSMaxime Ripard if (!has_lvds_rst || 1179e742a17cSMaxime Ripard (tcon->quirks->has_lvds_alt && !has_lvds_alt)) { 1180e742a17cSMaxime Ripard dev_warn(dev, "Missing LVDS properties, Please upgrade your DT\n"); 1181a0c1214eSMaxime Ripard dev_warn(dev, "LVDS output disabled\n"); 1182a0c1214eSMaxime Ripard can_lvds = false; 1183a0c1214eSMaxime Ripard } else { 1184a0c1214eSMaxime Ripard can_lvds = true; 1185a0c1214eSMaxime Ripard } 1186e742a17cSMaxime Ripard } else { 1187e742a17cSMaxime Ripard can_lvds = false; 1188e742a17cSMaxime Ripard } 1189a0c1214eSMaxime Ripard 11909026e0d1SMaxime Ripard ret = sun4i_tcon_init_clocks(dev, tcon); 11919026e0d1SMaxime Ripard if (ret) { 11929026e0d1SMaxime Ripard dev_err(dev, "Couldn't init our TCON clocks\n"); 11939026e0d1SMaxime Ripard goto err_assert_reset; 11949026e0d1SMaxime Ripard } 11959026e0d1SMaxime Ripard 11964c7f16d1SChen-Yu Tsai ret = sun4i_tcon_init_regmap(dev, tcon); 11979026e0d1SMaxime Ripard if (ret) { 11984c7f16d1SChen-Yu Tsai dev_err(dev, "Couldn't init our TCON regmap\n"); 11999026e0d1SMaxime Ripard goto err_free_clocks; 12009026e0d1SMaxime Ripard } 12019026e0d1SMaxime Ripard 120234d698f6SJernej Skrabec if (tcon->quirks->has_channel_0) { 12034c7f16d1SChen-Yu Tsai ret = sun4i_dclk_create(dev, tcon); 12044c7f16d1SChen-Yu Tsai if (ret) { 12054c7f16d1SChen-Yu Tsai dev_err(dev, "Couldn't create our TCON dot clock\n"); 12064c7f16d1SChen-Yu Tsai goto err_free_clocks; 12074c7f16d1SChen-Yu Tsai } 120834d698f6SJernej Skrabec } 12094c7f16d1SChen-Yu Tsai 12109026e0d1SMaxime Ripard ret = sun4i_tcon_init_irq(dev, tcon); 12119026e0d1SMaxime Ripard if (ret) { 12129026e0d1SMaxime Ripard dev_err(dev, "Couldn't init our TCON interrupts\n"); 12134c7f16d1SChen-Yu Tsai goto err_free_dotclock; 12149026e0d1SMaxime Ripard } 12159026e0d1SMaxime Ripard 121687969338SIcenowy Zheng tcon->crtc = sun4i_crtc_init(drm, engine, tcon); 121746cce6daSChen-Yu Tsai if (IS_ERR(tcon->crtc)) { 121846cce6daSChen-Yu Tsai dev_err(dev, "Couldn't create our CRTC\n"); 121946cce6daSChen-Yu Tsai ret = PTR_ERR(tcon->crtc); 122092411f6dSMaxime Ripard goto err_free_dotclock; 122146cce6daSChen-Yu Tsai } 122246cce6daSChen-Yu Tsai 12232a72d0c5SJernej Skrabec if (tcon->quirks->has_channel_0) { 1224a0c1214eSMaxime Ripard /* 1225a0c1214eSMaxime Ripard * If we have an LVDS panel connected to the TCON, we should 1226a0c1214eSMaxime Ripard * just probe the LVDS connector. Otherwise, just probe RGB as 1227a0c1214eSMaxime Ripard * we used to. 1228a0c1214eSMaxime Ripard */ 1229a0c1214eSMaxime Ripard remote = of_graph_get_remote_node(dev->of_node, 1, 0); 1230a0c1214eSMaxime Ripard if (of_device_is_compatible(remote, "panel-lvds")) 1231a0c1214eSMaxime Ripard if (can_lvds) 1232a0c1214eSMaxime Ripard ret = sun4i_lvds_init(drm, tcon); 1233a0c1214eSMaxime Ripard else 1234a0c1214eSMaxime Ripard ret = -EINVAL; 1235a0c1214eSMaxime Ripard else 1236b9c8506cSChen-Yu Tsai ret = sun4i_rgb_init(drm, tcon); 1237a0c1214eSMaxime Ripard of_node_put(remote); 1238a0c1214eSMaxime Ripard 123913fef095SChen-Yu Tsai if (ret < 0) 124092411f6dSMaxime Ripard goto err_free_dotclock; 12412a72d0c5SJernej Skrabec } 124213fef095SChen-Yu Tsai 124327e18de7SChen-Yu Tsai if (tcon->quirks->needs_de_be_mux) { 124427e18de7SChen-Yu Tsai /* 124527e18de7SChen-Yu Tsai * We assume there is no dynamic muxing of backends 124627e18de7SChen-Yu Tsai * and TCONs, so we select the backend with same ID. 124727e18de7SChen-Yu Tsai * 124827e18de7SChen-Yu Tsai * While dynamic selection might be interesting, since 124927e18de7SChen-Yu Tsai * the CRTC is tied to the TCON, while the layers are 125027e18de7SChen-Yu Tsai * tied to the backends, this means, we will need to 125127e18de7SChen-Yu Tsai * switch between groups of layers. There might not be 125227e18de7SChen-Yu Tsai * a way to represent this constraint in DRM. 125327e18de7SChen-Yu Tsai */ 125427e18de7SChen-Yu Tsai regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 125527e18de7SChen-Yu Tsai SUN4I_TCON0_CTL_SRC_SEL_MASK, 125627e18de7SChen-Yu Tsai tcon->id); 125727e18de7SChen-Yu Tsai regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 125827e18de7SChen-Yu Tsai SUN4I_TCON1_CTL_SRC_SEL_MASK, 125927e18de7SChen-Yu Tsai tcon->id); 126027e18de7SChen-Yu Tsai } 126127e18de7SChen-Yu Tsai 126280a58240SChen-Yu Tsai list_add_tail(&tcon->list, &drv->tcon_list); 126380a58240SChen-Yu Tsai 126413fef095SChen-Yu Tsai return 0; 12659026e0d1SMaxime Ripard 12664c7f16d1SChen-Yu Tsai err_free_dotclock: 126734d698f6SJernej Skrabec if (tcon->quirks->has_channel_0) 12684c7f16d1SChen-Yu Tsai sun4i_dclk_free(tcon); 12699026e0d1SMaxime Ripard err_free_clocks: 12709026e0d1SMaxime Ripard sun4i_tcon_free_clocks(tcon); 12719026e0d1SMaxime Ripard err_assert_reset: 12729026e0d1SMaxime Ripard reset_control_assert(tcon->lcd_rst); 12739026e0d1SMaxime Ripard return ret; 12749026e0d1SMaxime Ripard } 12759026e0d1SMaxime Ripard 12769026e0d1SMaxime Ripard static void sun4i_tcon_unbind(struct device *dev, struct device *master, 12779026e0d1SMaxime Ripard void *data) 12789026e0d1SMaxime Ripard { 12799026e0d1SMaxime Ripard struct sun4i_tcon *tcon = dev_get_drvdata(dev); 12809026e0d1SMaxime Ripard 128180a58240SChen-Yu Tsai list_del(&tcon->list); 128234d698f6SJernej Skrabec if (tcon->quirks->has_channel_0) 12834c7f16d1SChen-Yu Tsai sun4i_dclk_free(tcon); 12849026e0d1SMaxime Ripard sun4i_tcon_free_clocks(tcon); 12859026e0d1SMaxime Ripard } 12869026e0d1SMaxime Ripard 1287dfeb693dSJulia Lawall static const struct component_ops sun4i_tcon_ops = { 12889026e0d1SMaxime Ripard .bind = sun4i_tcon_bind, 12899026e0d1SMaxime Ripard .unbind = sun4i_tcon_unbind, 12909026e0d1SMaxime Ripard }; 12919026e0d1SMaxime Ripard 12929026e0d1SMaxime Ripard static int sun4i_tcon_probe(struct platform_device *pdev) 12939026e0d1SMaxime Ripard { 129429e57fabSMaxime Ripard struct device_node *node = pdev->dev.of_node; 129563d6310fSJernej Skrabec const struct sun4i_tcon_quirks *quirks; 1296894f5a9fSMaxime Ripard struct drm_bridge *bridge; 129729e57fabSMaxime Ripard struct drm_panel *panel; 1298ebc94461SRob Herring int ret; 129929e57fabSMaxime Ripard 130063d6310fSJernej Skrabec quirks = of_device_get_match_data(&pdev->dev); 130163d6310fSJernej Skrabec 130263d6310fSJernej Skrabec /* panels and bridges are present only on TCONs with channel 0 */ 130363d6310fSJernej Skrabec if (quirks->has_channel_0) { 1304ebc94461SRob Herring ret = drm_of_find_panel_or_bridge(node, 1, 0, &panel, &bridge); 1305ebc94461SRob Herring if (ret == -EPROBE_DEFER) 1306ebc94461SRob Herring return ret; 130763d6310fSJernej Skrabec } 130829e57fabSMaxime Ripard 13099026e0d1SMaxime Ripard return component_add(&pdev->dev, &sun4i_tcon_ops); 13109026e0d1SMaxime Ripard } 13119026e0d1SMaxime Ripard 13129026e0d1SMaxime Ripard static int sun4i_tcon_remove(struct platform_device *pdev) 13139026e0d1SMaxime Ripard { 13149026e0d1SMaxime Ripard component_del(&pdev->dev, &sun4i_tcon_ops); 13159026e0d1SMaxime Ripard 13169026e0d1SMaxime Ripard return 0; 13179026e0d1SMaxime Ripard } 13189026e0d1SMaxime Ripard 1319ad537fb2SChen-Yu Tsai /* platform specific TCON muxing callbacks */ 13204bb206bfSJonathan Liu static int sun4i_a10_tcon_set_mux(struct sun4i_tcon *tcon, 13214bb206bfSJonathan Liu const struct drm_encoder *encoder) 13224bb206bfSJonathan Liu { 13234bb206bfSJonathan Liu struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev); 13244bb206bfSJonathan Liu u32 shift; 13254bb206bfSJonathan Liu 13264bb206bfSJonathan Liu if (!tcon0) 13274bb206bfSJonathan Liu return -EINVAL; 13284bb206bfSJonathan Liu 13294bb206bfSJonathan Liu switch (encoder->encoder_type) { 13304bb206bfSJonathan Liu case DRM_MODE_ENCODER_TMDS: 13314bb206bfSJonathan Liu /* HDMI */ 13324bb206bfSJonathan Liu shift = 8; 13334bb206bfSJonathan Liu break; 13344bb206bfSJonathan Liu default: 13354bb206bfSJonathan Liu return -EINVAL; 13364bb206bfSJonathan Liu } 13374bb206bfSJonathan Liu 13384bb206bfSJonathan Liu regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG, 13394bb206bfSJonathan Liu 0x3 << shift, tcon->id << shift); 13404bb206bfSJonathan Liu 13414bb206bfSJonathan Liu return 0; 13424bb206bfSJonathan Liu } 13434bb206bfSJonathan Liu 1344ad537fb2SChen-Yu Tsai static int sun5i_a13_tcon_set_mux(struct sun4i_tcon *tcon, 1345abcb8766SMaxime Ripard const struct drm_encoder *encoder) 1346ad537fb2SChen-Yu Tsai { 1347ad537fb2SChen-Yu Tsai u32 val; 1348ad537fb2SChen-Yu Tsai 1349ad537fb2SChen-Yu Tsai if (encoder->encoder_type == DRM_MODE_ENCODER_TVDAC) 1350ad537fb2SChen-Yu Tsai val = 1; 1351ad537fb2SChen-Yu Tsai else 1352ad537fb2SChen-Yu Tsai val = 0; 1353ad537fb2SChen-Yu Tsai 1354ad537fb2SChen-Yu Tsai /* 1355ad537fb2SChen-Yu Tsai * FIXME: Undocumented bits 1356ad537fb2SChen-Yu Tsai */ 1357ad537fb2SChen-Yu Tsai return regmap_write(tcon->regs, SUN4I_TCON_MUX_CTRL_REG, val); 1358ad537fb2SChen-Yu Tsai } 1359ad537fb2SChen-Yu Tsai 136067e32645SChen-Yu Tsai static int sun6i_tcon_set_mux(struct sun4i_tcon *tcon, 1361abcb8766SMaxime Ripard const struct drm_encoder *encoder) 136267e32645SChen-Yu Tsai { 136367e32645SChen-Yu Tsai struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev); 136467e32645SChen-Yu Tsai u32 shift; 136567e32645SChen-Yu Tsai 136667e32645SChen-Yu Tsai if (!tcon0) 136767e32645SChen-Yu Tsai return -EINVAL; 136867e32645SChen-Yu Tsai 136967e32645SChen-Yu Tsai switch (encoder->encoder_type) { 137067e32645SChen-Yu Tsai case DRM_MODE_ENCODER_TMDS: 137167e32645SChen-Yu Tsai /* HDMI */ 137267e32645SChen-Yu Tsai shift = 8; 137367e32645SChen-Yu Tsai break; 137467e32645SChen-Yu Tsai default: 137567e32645SChen-Yu Tsai /* TODO A31 has MIPI DSI but A31s does not */ 137667e32645SChen-Yu Tsai return -EINVAL; 137767e32645SChen-Yu Tsai } 137867e32645SChen-Yu Tsai 137967e32645SChen-Yu Tsai regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG, 138067e32645SChen-Yu Tsai 0x3 << shift, tcon->id << shift); 138167e32645SChen-Yu Tsai 138267e32645SChen-Yu Tsai return 0; 138367e32645SChen-Yu Tsai } 138467e32645SChen-Yu Tsai 13850305189aSJernej Skrabec static int sun8i_r40_tcon_tv_set_mux(struct sun4i_tcon *tcon, 13860305189aSJernej Skrabec const struct drm_encoder *encoder) 13870305189aSJernej Skrabec { 13880305189aSJernej Skrabec struct device_node *port, *remote; 13890305189aSJernej Skrabec struct platform_device *pdev; 13900305189aSJernej Skrabec int id, ret; 13910305189aSJernej Skrabec 13920305189aSJernej Skrabec /* find TCON TOP platform device and TCON id */ 13930305189aSJernej Skrabec 13940305189aSJernej Skrabec port = of_graph_get_port_by_id(tcon->dev->of_node, 0); 13950305189aSJernej Skrabec if (!port) 13960305189aSJernej Skrabec return -EINVAL; 13970305189aSJernej Skrabec 13980305189aSJernej Skrabec id = sun4i_tcon_of_get_id_from_port(port); 13990305189aSJernej Skrabec of_node_put(port); 14000305189aSJernej Skrabec 14010305189aSJernej Skrabec remote = of_graph_get_remote_node(tcon->dev->of_node, 0, -1); 14020305189aSJernej Skrabec if (!remote) 14030305189aSJernej Skrabec return -EINVAL; 14040305189aSJernej Skrabec 14050305189aSJernej Skrabec pdev = of_find_device_by_node(remote); 14060305189aSJernej Skrabec of_node_put(remote); 14070305189aSJernej Skrabec if (!pdev) 14080305189aSJernej Skrabec return -EINVAL; 14090305189aSJernej Skrabec 1410185e0bebSMaxime Ripard if (IS_ENABLED(CONFIG_DRM_SUN8I_TCON_TOP) && 1411185e0bebSMaxime Ripard encoder->encoder_type == DRM_MODE_ENCODER_TMDS) { 14120305189aSJernej Skrabec ret = sun8i_tcon_top_set_hdmi_src(&pdev->dev, id); 14130305189aSJernej Skrabec if (ret) 14140305189aSJernej Skrabec return ret; 14150305189aSJernej Skrabec } 14160305189aSJernej Skrabec 1417185e0bebSMaxime Ripard if (IS_ENABLED(CONFIG_DRM_SUN8I_TCON_TOP)) { 1418185e0bebSMaxime Ripard ret = sun8i_tcon_top_de_config(&pdev->dev, tcon->id, id); 1419185e0bebSMaxime Ripard if (ret) 1420185e0bebSMaxime Ripard return ret; 1421185e0bebSMaxime Ripard } 1422185e0bebSMaxime Ripard 1423185e0bebSMaxime Ripard return 0; 14240305189aSJernej Skrabec } 14250305189aSJernej Skrabec 14264bb206bfSJonathan Liu static const struct sun4i_tcon_quirks sun4i_a10_quirks = { 142734d698f6SJernej Skrabec .has_channel_0 = true, 14284bb206bfSJonathan Liu .has_channel_1 = true, 14294bb206bfSJonathan Liu .set_mux = sun4i_a10_tcon_set_mux, 14304bb206bfSJonathan Liu }; 14314bb206bfSJonathan Liu 143291ea2f29SChen-Yu Tsai static const struct sun4i_tcon_quirks sun5i_a13_quirks = { 143334d698f6SJernej Skrabec .has_channel_0 = true, 143491ea2f29SChen-Yu Tsai .has_channel_1 = true, 1435ad537fb2SChen-Yu Tsai .set_mux = sun5i_a13_tcon_set_mux, 143691ea2f29SChen-Yu Tsai }; 143791ea2f29SChen-Yu Tsai 143893a5ec14SChen-Yu Tsai static const struct sun4i_tcon_quirks sun6i_a31_quirks = { 143934d698f6SJernej Skrabec .has_channel_0 = true, 144093a5ec14SChen-Yu Tsai .has_channel_1 = true, 1441a0c1214eSMaxime Ripard .has_lvds_alt = true, 144227e18de7SChen-Yu Tsai .needs_de_be_mux = true, 144367e32645SChen-Yu Tsai .set_mux = sun6i_tcon_set_mux, 144493a5ec14SChen-Yu Tsai }; 144593a5ec14SChen-Yu Tsai 144693a5ec14SChen-Yu Tsai static const struct sun4i_tcon_quirks sun6i_a31s_quirks = { 144734d698f6SJernej Skrabec .has_channel_0 = true, 144893a5ec14SChen-Yu Tsai .has_channel_1 = true, 144927e18de7SChen-Yu Tsai .needs_de_be_mux = true, 145093a5ec14SChen-Yu Tsai }; 145193a5ec14SChen-Yu Tsai 1452aaddb6d2SJonathan Liu static const struct sun4i_tcon_quirks sun7i_a20_quirks = { 145334d698f6SJernej Skrabec .has_channel_0 = true, 1454aaddb6d2SJonathan Liu .has_channel_1 = true, 1455aaddb6d2SJonathan Liu /* Same display pipeline structure as A10 */ 1456aaddb6d2SJonathan Liu .set_mux = sun4i_a10_tcon_set_mux, 1457aaddb6d2SJonathan Liu }; 1458aaddb6d2SJonathan Liu 145991ea2f29SChen-Yu Tsai static const struct sun4i_tcon_quirks sun8i_a33_quirks = { 146034d698f6SJernej Skrabec .has_channel_0 = true, 1461a0c1214eSMaxime Ripard .has_lvds_alt = true, 146291ea2f29SChen-Yu Tsai }; 146391ea2f29SChen-Yu Tsai 14642f0d7bb1SMaxime Ripard static const struct sun4i_tcon_quirks sun8i_a83t_lcd_quirks = { 1465e742a17cSMaxime Ripard .supports_lvds = true, 146634d698f6SJernej Skrabec .has_channel_0 = true, 14672f0d7bb1SMaxime Ripard }; 14682f0d7bb1SMaxime Ripard 146905adc89bSJernej Skrabec static const struct sun4i_tcon_quirks sun8i_a83t_tv_quirks = { 147005adc89bSJernej Skrabec .has_channel_1 = true, 147105adc89bSJernej Skrabec }; 147205adc89bSJernej Skrabec 14730305189aSJernej Skrabec static const struct sun4i_tcon_quirks sun8i_r40_tv_quirks = { 14740305189aSJernej Skrabec .has_channel_1 = true, 14750305189aSJernej Skrabec .set_mux = sun8i_r40_tcon_tv_set_mux, 14760305189aSJernej Skrabec }; 14770305189aSJernej Skrabec 14781a0edb3fSIcenowy Zheng static const struct sun4i_tcon_quirks sun8i_v3s_quirks = { 147934d698f6SJernej Skrabec .has_channel_0 = true, 14801a0edb3fSIcenowy Zheng }; 14811a0edb3fSIcenowy Zheng 14826664e9dcSChen-Yu Tsai static const struct sun4i_tcon_quirks sun9i_a80_tcon_lcd_quirks = { 14836664e9dcSChen-Yu Tsai .has_channel_0 = true, 14846664e9dcSChen-Yu Tsai .needs_edp_reset = true, 14856664e9dcSChen-Yu Tsai }; 14866664e9dcSChen-Yu Tsai 14876664e9dcSChen-Yu Tsai static const struct sun4i_tcon_quirks sun9i_a80_tcon_tv_quirks = { 14886664e9dcSChen-Yu Tsai .has_channel_1 = true, 14896664e9dcSChen-Yu Tsai .needs_edp_reset = true, 14906664e9dcSChen-Yu Tsai }; 14916664e9dcSChen-Yu Tsai 1492ff71c2cfSChen-Yu Tsai /* sun4i_drv uses this list to check if a device node is a TCON */ 1493ff71c2cfSChen-Yu Tsai const struct of_device_id sun4i_tcon_of_table[] = { 14944bb206bfSJonathan Liu { .compatible = "allwinner,sun4i-a10-tcon", .data = &sun4i_a10_quirks }, 149591ea2f29SChen-Yu Tsai { .compatible = "allwinner,sun5i-a13-tcon", .data = &sun5i_a13_quirks }, 149693a5ec14SChen-Yu Tsai { .compatible = "allwinner,sun6i-a31-tcon", .data = &sun6i_a31_quirks }, 149793a5ec14SChen-Yu Tsai { .compatible = "allwinner,sun6i-a31s-tcon", .data = &sun6i_a31s_quirks }, 1498aaddb6d2SJonathan Liu { .compatible = "allwinner,sun7i-a20-tcon", .data = &sun7i_a20_quirks }, 149991ea2f29SChen-Yu Tsai { .compatible = "allwinner,sun8i-a33-tcon", .data = &sun8i_a33_quirks }, 15002f0d7bb1SMaxime Ripard { .compatible = "allwinner,sun8i-a83t-tcon-lcd", .data = &sun8i_a83t_lcd_quirks }, 150105adc89bSJernej Skrabec { .compatible = "allwinner,sun8i-a83t-tcon-tv", .data = &sun8i_a83t_tv_quirks }, 15020305189aSJernej Skrabec { .compatible = "allwinner,sun8i-r40-tcon-tv", .data = &sun8i_r40_tv_quirks }, 15031a0edb3fSIcenowy Zheng { .compatible = "allwinner,sun8i-v3s-tcon", .data = &sun8i_v3s_quirks }, 15046664e9dcSChen-Yu Tsai { .compatible = "allwinner,sun9i-a80-tcon-lcd", .data = &sun9i_a80_tcon_lcd_quirks }, 15056664e9dcSChen-Yu Tsai { .compatible = "allwinner,sun9i-a80-tcon-tv", .data = &sun9i_a80_tcon_tv_quirks }, 15069026e0d1SMaxime Ripard { } 15079026e0d1SMaxime Ripard }; 15089026e0d1SMaxime Ripard MODULE_DEVICE_TABLE(of, sun4i_tcon_of_table); 1509ff71c2cfSChen-Yu Tsai EXPORT_SYMBOL(sun4i_tcon_of_table); 15109026e0d1SMaxime Ripard 15119026e0d1SMaxime Ripard static struct platform_driver sun4i_tcon_platform_driver = { 15129026e0d1SMaxime Ripard .probe = sun4i_tcon_probe, 15139026e0d1SMaxime Ripard .remove = sun4i_tcon_remove, 15149026e0d1SMaxime Ripard .driver = { 15159026e0d1SMaxime Ripard .name = "sun4i-tcon", 15169026e0d1SMaxime Ripard .of_match_table = sun4i_tcon_of_table, 15179026e0d1SMaxime Ripard }, 15189026e0d1SMaxime Ripard }; 15199026e0d1SMaxime Ripard module_platform_driver(sun4i_tcon_platform_driver); 15209026e0d1SMaxime Ripard 15219026e0d1SMaxime Ripard MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>"); 15229026e0d1SMaxime Ripard MODULE_DESCRIPTION("Allwinner A10 Timing Controller Driver"); 15239026e0d1SMaxime Ripard MODULE_LICENSE("GPL"); 1524