12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 29026e0d1SMaxime Ripard /* 39026e0d1SMaxime Ripard * Copyright (C) 2015 Free Electrons 49026e0d1SMaxime Ripard * Copyright (C) 2015 NextThing Co 59026e0d1SMaxime Ripard * 69026e0d1SMaxime Ripard * Maxime Ripard <maxime.ripard@free-electrons.com> 79026e0d1SMaxime Ripard */ 89026e0d1SMaxime Ripard 99c25a297SSam Ravnborg #include <linux/component.h> 109c25a297SSam Ravnborg #include <linux/ioport.h> 119c25a297SSam Ravnborg #include <linux/module.h> 129c25a297SSam Ravnborg #include <linux/of_address.h> 139c25a297SSam Ravnborg #include <linux/of_device.h> 149c25a297SSam Ravnborg #include <linux/of_irq.h> 159c25a297SSam Ravnborg #include <linux/regmap.h> 169c25a297SSam Ravnborg #include <linux/reset.h> 179c25a297SSam Ravnborg 189026e0d1SMaxime Ripard #include <drm/drm_atomic_helper.h> 19ee68c743SBoris Brezillon #include <drm/drm_bridge.h> 20f11adcecSJonathan Liu #include <drm/drm_connector.h> 219026e0d1SMaxime Ripard #include <drm/drm_crtc.h> 22ad537fb2SChen-Yu Tsai #include <drm/drm_encoder.h> 239026e0d1SMaxime Ripard #include <drm/drm_modes.h> 24ebc94461SRob Herring #include <drm/drm_of.h> 25490cda5aSGiulio Benetti #include <drm/drm_panel.h> 269c25a297SSam Ravnborg #include <drm/drm_print.h> 27fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h> 289c25a297SSam Ravnborg #include <drm/drm_vblank.h> 299026e0d1SMaxime Ripard 30ad537fb2SChen-Yu Tsai #include <uapi/drm/drm_mode.h> 31ad537fb2SChen-Yu Tsai 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" 38a08fc7c8SMaxime Ripard #include "sun6i_mipi_dsi.h" 39cf77d79bSJernej Skrabec #include "sun8i_tcon_top.h" 4087969338SIcenowy Zheng #include "sunxi_engine.h" 419026e0d1SMaxime Ripard 42a0c1214eSMaxime Ripard static struct drm_connector *sun4i_tcon_get_connector(const struct drm_encoder *encoder) 43a0c1214eSMaxime Ripard { 44a0c1214eSMaxime Ripard struct drm_connector *connector; 45a0c1214eSMaxime Ripard struct drm_connector_list_iter iter; 46a0c1214eSMaxime Ripard 47a0c1214eSMaxime Ripard drm_connector_list_iter_begin(encoder->dev, &iter); 48a0c1214eSMaxime Ripard drm_for_each_connector_iter(connector, &iter) 49a0c1214eSMaxime Ripard if (connector->encoder == encoder) { 50a0c1214eSMaxime Ripard drm_connector_list_iter_end(&iter); 51a0c1214eSMaxime Ripard return connector; 52a0c1214eSMaxime Ripard } 53a0c1214eSMaxime Ripard drm_connector_list_iter_end(&iter); 54a0c1214eSMaxime Ripard 55a0c1214eSMaxime Ripard return NULL; 56a0c1214eSMaxime Ripard } 57a0c1214eSMaxime Ripard 58a0c1214eSMaxime Ripard static int sun4i_tcon_get_pixel_depth(const struct drm_encoder *encoder) 59a0c1214eSMaxime Ripard { 60a0c1214eSMaxime Ripard struct drm_connector *connector; 61a0c1214eSMaxime Ripard struct drm_display_info *info; 62a0c1214eSMaxime Ripard 63a0c1214eSMaxime Ripard connector = sun4i_tcon_get_connector(encoder); 64a0c1214eSMaxime Ripard if (!connector) 65a0c1214eSMaxime Ripard return -EINVAL; 66a0c1214eSMaxime Ripard 67a0c1214eSMaxime Ripard info = &connector->display_info; 68a0c1214eSMaxime Ripard if (info->num_bus_formats != 1) 69a0c1214eSMaxime Ripard return -EINVAL; 70a0c1214eSMaxime Ripard 71a0c1214eSMaxime Ripard switch (info->bus_formats[0]) { 72a0c1214eSMaxime Ripard case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG: 73a0c1214eSMaxime Ripard return 18; 74a0c1214eSMaxime Ripard 75a0c1214eSMaxime Ripard case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA: 76a0c1214eSMaxime Ripard case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG: 77a0c1214eSMaxime Ripard return 24; 78a0c1214eSMaxime Ripard } 79a0c1214eSMaxime Ripard 80a0c1214eSMaxime Ripard return -EINVAL; 81a0c1214eSMaxime Ripard } 82a0c1214eSMaxime Ripard 8345e88f99SMaxime Ripard static void sun4i_tcon_channel_set_status(struct sun4i_tcon *tcon, int channel, 8445e88f99SMaxime Ripard bool enabled) 859026e0d1SMaxime Ripard { 8645e88f99SMaxime Ripard struct clk *clk; 879026e0d1SMaxime Ripard 8845e88f99SMaxime Ripard switch (channel) { 8945e88f99SMaxime Ripard case 0: 9034d698f6SJernej Skrabec WARN_ON(!tcon->quirks->has_channel_0); 919026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 929026e0d1SMaxime Ripard SUN4I_TCON0_CTL_TCON_ENABLE, 9345e88f99SMaxime Ripard enabled ? SUN4I_TCON0_CTL_TCON_ENABLE : 0); 9445e88f99SMaxime Ripard clk = tcon->dclk; 9545e88f99SMaxime Ripard break; 9645e88f99SMaxime Ripard case 1: 9791ea2f29SChen-Yu Tsai WARN_ON(!tcon->quirks->has_channel_1); 989026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 999026e0d1SMaxime Ripard SUN4I_TCON1_CTL_TCON_ENABLE, 10045e88f99SMaxime Ripard enabled ? SUN4I_TCON1_CTL_TCON_ENABLE : 0); 10145e88f99SMaxime Ripard clk = tcon->sclk1; 10245e88f99SMaxime Ripard break; 10345e88f99SMaxime Ripard default: 10445e88f99SMaxime Ripard DRM_WARN("Unknown channel... doing nothing\n"); 10545e88f99SMaxime Ripard return; 1069026e0d1SMaxime Ripard } 10745e88f99SMaxime Ripard 108f3e5feebSJernej Skrabec if (enabled) { 10945e88f99SMaxime Ripard clk_prepare_enable(clk); 1107035046dSOndrej Jirman clk_rate_exclusive_get(clk); 111f3e5feebSJernej Skrabec } else { 112f3e5feebSJernej Skrabec clk_rate_exclusive_put(clk); 11345e88f99SMaxime Ripard clk_disable_unprepare(clk); 11445e88f99SMaxime Ripard } 115f3e5feebSJernej Skrabec } 11645e88f99SMaxime Ripard 117d718e53aSAndrey Lebedev static void sun4i_tcon_setup_lvds_phy(struct sun4i_tcon *tcon, 118d718e53aSAndrey Lebedev const struct drm_encoder *encoder) 119d718e53aSAndrey Lebedev { 120d718e53aSAndrey Lebedev regmap_write(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG, 121d718e53aSAndrey Lebedev SUN4I_TCON0_LVDS_ANA0_CK_EN | 122d718e53aSAndrey Lebedev SUN4I_TCON0_LVDS_ANA0_REG_V | 123d718e53aSAndrey Lebedev SUN4I_TCON0_LVDS_ANA0_REG_C | 124d718e53aSAndrey Lebedev SUN4I_TCON0_LVDS_ANA0_EN_MB | 125d718e53aSAndrey Lebedev SUN4I_TCON0_LVDS_ANA0_PD | 126d718e53aSAndrey Lebedev SUN4I_TCON0_LVDS_ANA0_DCHS); 127d718e53aSAndrey Lebedev 128d718e53aSAndrey Lebedev udelay(2); /* delay at least 1200 ns */ 129d718e53aSAndrey Lebedev regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA1_REG, 130d718e53aSAndrey Lebedev SUN4I_TCON0_LVDS_ANA1_INIT, 131d718e53aSAndrey Lebedev SUN4I_TCON0_LVDS_ANA1_INIT); 132d718e53aSAndrey Lebedev udelay(1); /* delay at least 120 ns */ 133d718e53aSAndrey Lebedev regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA1_REG, 134d718e53aSAndrey Lebedev SUN4I_TCON0_LVDS_ANA1_UPDATE, 135d718e53aSAndrey Lebedev SUN4I_TCON0_LVDS_ANA1_UPDATE); 136d718e53aSAndrey Lebedev regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG, 137d718e53aSAndrey Lebedev SUN4I_TCON0_LVDS_ANA0_EN_MB, 138d718e53aSAndrey Lebedev SUN4I_TCON0_LVDS_ANA0_EN_MB); 139d718e53aSAndrey Lebedev } 140d718e53aSAndrey Lebedev 1415627c9d8SAndrey Lebedev static void sun6i_tcon_setup_lvds_phy(struct sun4i_tcon *tcon, 1425627c9d8SAndrey Lebedev const struct drm_encoder *encoder) 143a0c1214eSMaxime Ripard { 144a0c1214eSMaxime Ripard u8 val; 145a0c1214eSMaxime Ripard 146a0c1214eSMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG, 147a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_C(2) | 148a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_V(3) | 149a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_PD(2) | 150a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_EN_LDO); 151a0c1214eSMaxime Ripard udelay(2); 152a0c1214eSMaxime Ripard 153a0c1214eSMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG, 154a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_EN_MB, 155a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_EN_MB); 156a0c1214eSMaxime Ripard udelay(2); 157a0c1214eSMaxime Ripard 158a0c1214eSMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG, 159a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_EN_DRVC, 160a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_EN_DRVC); 161a0c1214eSMaxime Ripard 162a0c1214eSMaxime Ripard if (sun4i_tcon_get_pixel_depth(encoder) == 18) 163a0c1214eSMaxime Ripard val = 7; 164a0c1214eSMaxime Ripard else 165a0c1214eSMaxime Ripard val = 0xf; 166a0c1214eSMaxime Ripard 167a0c1214eSMaxime Ripard regmap_write_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG, 168a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_EN_DRVD(0xf), 169a0c1214eSMaxime Ripard SUN6I_TCON0_LVDS_ANA0_EN_DRVD(val)); 1705627c9d8SAndrey Lebedev } 1715627c9d8SAndrey Lebedev 1725627c9d8SAndrey Lebedev static void sun4i_tcon_lvds_set_status(struct sun4i_tcon *tcon, 1735627c9d8SAndrey Lebedev const struct drm_encoder *encoder, 1745627c9d8SAndrey Lebedev bool enabled) 1755627c9d8SAndrey Lebedev { 1765627c9d8SAndrey Lebedev if (enabled) { 1775627c9d8SAndrey Lebedev regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_IF_REG, 1785627c9d8SAndrey Lebedev SUN4I_TCON0_LVDS_IF_EN, 1795627c9d8SAndrey Lebedev SUN4I_TCON0_LVDS_IF_EN); 1805627c9d8SAndrey Lebedev if (tcon->quirks->setup_lvds_phy) 1815627c9d8SAndrey Lebedev tcon->quirks->setup_lvds_phy(tcon, encoder); 182a0c1214eSMaxime Ripard } else { 183a0c1214eSMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_IF_REG, 184a0c1214eSMaxime Ripard SUN4I_TCON0_LVDS_IF_EN, 0); 185a0c1214eSMaxime Ripard } 186a0c1214eSMaxime Ripard } 187a0c1214eSMaxime Ripard 18845e88f99SMaxime Ripard void sun4i_tcon_set_status(struct sun4i_tcon *tcon, 18945e88f99SMaxime Ripard const struct drm_encoder *encoder, 19045e88f99SMaxime Ripard bool enabled) 19145e88f99SMaxime Ripard { 192a0c1214eSMaxime Ripard bool is_lvds = false; 19345e88f99SMaxime Ripard int channel; 19445e88f99SMaxime Ripard 19545e88f99SMaxime Ripard switch (encoder->encoder_type) { 196a0c1214eSMaxime Ripard case DRM_MODE_ENCODER_LVDS: 197a0c1214eSMaxime Ripard is_lvds = true; 198df561f66SGustavo A. R. Silva fallthrough; 199a08fc7c8SMaxime Ripard case DRM_MODE_ENCODER_DSI: 20045e88f99SMaxime Ripard case DRM_MODE_ENCODER_NONE: 20145e88f99SMaxime Ripard channel = 0; 20245e88f99SMaxime Ripard break; 20345e88f99SMaxime Ripard case DRM_MODE_ENCODER_TMDS: 20445e88f99SMaxime Ripard case DRM_MODE_ENCODER_TVDAC: 20545e88f99SMaxime Ripard channel = 1; 20645e88f99SMaxime Ripard break; 20745e88f99SMaxime Ripard default: 20845e88f99SMaxime Ripard DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n"); 20945e88f99SMaxime Ripard return; 21045e88f99SMaxime Ripard } 21145e88f99SMaxime Ripard 212a0c1214eSMaxime Ripard if (is_lvds && !enabled) 213a0c1214eSMaxime Ripard sun4i_tcon_lvds_set_status(tcon, encoder, false); 214a0c1214eSMaxime Ripard 21545e88f99SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 21645e88f99SMaxime Ripard SUN4I_TCON_GCTL_TCON_ENABLE, 21745e88f99SMaxime Ripard enabled ? SUN4I_TCON_GCTL_TCON_ENABLE : 0); 21845e88f99SMaxime Ripard 219a0c1214eSMaxime Ripard if (is_lvds && enabled) 220a0c1214eSMaxime Ripard sun4i_tcon_lvds_set_status(tcon, encoder, true); 221a0c1214eSMaxime Ripard 22245e88f99SMaxime Ripard sun4i_tcon_channel_set_status(tcon, channel, enabled); 22345e88f99SMaxime Ripard } 2249026e0d1SMaxime Ripard 2259026e0d1SMaxime Ripard void sun4i_tcon_enable_vblank(struct sun4i_tcon *tcon, bool enable) 2269026e0d1SMaxime Ripard { 2279026e0d1SMaxime Ripard u32 mask, val = 0; 2289026e0d1SMaxime Ripard 2299026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("%sabling VBLANK interrupt\n", enable ? "En" : "Dis"); 2309026e0d1SMaxime Ripard 2319026e0d1SMaxime Ripard mask = SUN4I_TCON_GINT0_VBLANK_ENABLE(0) | 232a493ceaeSMaxime Ripard SUN4I_TCON_GINT0_VBLANK_ENABLE(1) | 233a493ceaeSMaxime Ripard SUN4I_TCON_GINT0_TCON0_TRI_FINISH_ENABLE; 2349026e0d1SMaxime Ripard 2359026e0d1SMaxime Ripard if (enable) 2369026e0d1SMaxime Ripard val = mask; 2379026e0d1SMaxime Ripard 2389026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, mask, val); 2399026e0d1SMaxime Ripard } 2409026e0d1SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_enable_vblank); 2419026e0d1SMaxime Ripard 24267e32645SChen-Yu Tsai /* 24367e32645SChen-Yu Tsai * This function is a helper for TCON output muxing. The TCON output 24467e32645SChen-Yu Tsai * muxing control register in earlier SoCs (without the TCON TOP block) 24567e32645SChen-Yu Tsai * are located in TCON0. This helper returns a pointer to TCON0's 24667e32645SChen-Yu Tsai * sun4i_tcon structure, or NULL if not found. 24767e32645SChen-Yu Tsai */ 24867e32645SChen-Yu Tsai static struct sun4i_tcon *sun4i_get_tcon0(struct drm_device *drm) 24967e32645SChen-Yu Tsai { 25067e32645SChen-Yu Tsai struct sun4i_drv *drv = drm->dev_private; 25167e32645SChen-Yu Tsai struct sun4i_tcon *tcon; 25267e32645SChen-Yu Tsai 25367e32645SChen-Yu Tsai list_for_each_entry(tcon, &drv->tcon_list, list) 25467e32645SChen-Yu Tsai if (tcon->id == 0) 25567e32645SChen-Yu Tsai return tcon; 25667e32645SChen-Yu Tsai 25767e32645SChen-Yu Tsai dev_warn(drm->dev, 25867e32645SChen-Yu Tsai "TCON0 not found, display output muxing may not work\n"); 25967e32645SChen-Yu Tsai 26067e32645SChen-Yu Tsai return NULL; 26167e32645SChen-Yu Tsai } 26267e32645SChen-Yu Tsai 2631f2f0599SYueHaibing static void sun4i_tcon_set_mux(struct sun4i_tcon *tcon, int channel, 264abcb8766SMaxime Ripard const struct drm_encoder *encoder) 265f8c73f4fSMaxime Ripard { 266ad537fb2SChen-Yu Tsai int ret = -ENOTSUPP; 267b7cb9b91SMaxime Ripard 268ad537fb2SChen-Yu Tsai if (tcon->quirks->set_mux) 269ad537fb2SChen-Yu Tsai ret = tcon->quirks->set_mux(tcon, encoder); 270f8c73f4fSMaxime Ripard 271ad537fb2SChen-Yu Tsai DRM_DEBUG_DRIVER("Muxing encoder %s to CRTC %s: %d\n", 272ad537fb2SChen-Yu Tsai encoder->name, encoder->crtc->name, ret); 273f8c73f4fSMaxime Ripard } 274f8c73f4fSMaxime Ripard 275961c645cSMaxime Ripard static int sun4i_tcon_get_clk_delay(const struct drm_display_mode *mode, 2769026e0d1SMaxime Ripard int channel) 2779026e0d1SMaxime Ripard { 2789026e0d1SMaxime Ripard int delay = mode->vtotal - mode->vdisplay; 2799026e0d1SMaxime Ripard 2809026e0d1SMaxime Ripard if (mode->flags & DRM_MODE_FLAG_INTERLACE) 2819026e0d1SMaxime Ripard delay /= 2; 2829026e0d1SMaxime Ripard 2839026e0d1SMaxime Ripard if (channel == 1) 2849026e0d1SMaxime Ripard delay -= 2; 2859026e0d1SMaxime Ripard 2869026e0d1SMaxime Ripard delay = min(delay, 30); 2879026e0d1SMaxime Ripard 2889026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("TCON %d clock delay %u\n", channel, delay); 2899026e0d1SMaxime Ripard 2909026e0d1SMaxime Ripard return delay; 2919026e0d1SMaxime Ripard } 2929026e0d1SMaxime Ripard 293ba19c537SMaxime Ripard static void sun4i_tcon0_mode_set_common(struct sun4i_tcon *tcon, 294ba19c537SMaxime Ripard const struct drm_display_mode *mode) 295ba19c537SMaxime Ripard { 296ba19c537SMaxime Ripard /* Configure the dot clock */ 297ba19c537SMaxime Ripard clk_set_rate(tcon->dclk, mode->crtc_clock * 1000); 298ba19c537SMaxime Ripard 299ba19c537SMaxime Ripard /* Set the resolution */ 300ba19c537SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC0_REG, 301ba19c537SMaxime Ripard SUN4I_TCON0_BASIC0_X(mode->crtc_hdisplay) | 302ba19c537SMaxime Ripard SUN4I_TCON0_BASIC0_Y(mode->crtc_vdisplay)); 303ba19c537SMaxime Ripard } 304ba19c537SMaxime Ripard 305f11adcecSJonathan Liu static void sun4i_tcon0_mode_set_dithering(struct sun4i_tcon *tcon, 306f11adcecSJonathan Liu const struct drm_connector *connector) 307f11adcecSJonathan Liu { 308f11adcecSJonathan Liu u32 bus_format = 0; 309f11adcecSJonathan Liu u32 val = 0; 310f11adcecSJonathan Liu 311f11adcecSJonathan Liu /* XXX Would this ever happen? */ 312f11adcecSJonathan Liu if (!connector) 313f11adcecSJonathan Liu return; 314f11adcecSJonathan Liu 315f11adcecSJonathan Liu /* 316f11adcecSJonathan Liu * FIXME: Undocumented bits 317f11adcecSJonathan Liu * 318f11adcecSJonathan Liu * The whole dithering process and these parameters are not 319f11adcecSJonathan Liu * explained in the vendor documents or BSP kernel code. 320f11adcecSJonathan Liu */ 321f11adcecSJonathan Liu regmap_write(tcon->regs, SUN4I_TCON0_FRM_SEED_PR_REG, 0x11111111); 322f11adcecSJonathan Liu regmap_write(tcon->regs, SUN4I_TCON0_FRM_SEED_PG_REG, 0x11111111); 323f11adcecSJonathan Liu regmap_write(tcon->regs, SUN4I_TCON0_FRM_SEED_PB_REG, 0x11111111); 324f11adcecSJonathan Liu regmap_write(tcon->regs, SUN4I_TCON0_FRM_SEED_LR_REG, 0x11111111); 325f11adcecSJonathan Liu regmap_write(tcon->regs, SUN4I_TCON0_FRM_SEED_LG_REG, 0x11111111); 326f11adcecSJonathan Liu regmap_write(tcon->regs, SUN4I_TCON0_FRM_SEED_LB_REG, 0x11111111); 327f11adcecSJonathan Liu regmap_write(tcon->regs, SUN4I_TCON0_FRM_TBL0_REG, 0x01010000); 328f11adcecSJonathan Liu regmap_write(tcon->regs, SUN4I_TCON0_FRM_TBL1_REG, 0x15151111); 329f11adcecSJonathan Liu regmap_write(tcon->regs, SUN4I_TCON0_FRM_TBL2_REG, 0x57575555); 330f11adcecSJonathan Liu regmap_write(tcon->regs, SUN4I_TCON0_FRM_TBL3_REG, 0x7f7f7777); 331f11adcecSJonathan Liu 332f11adcecSJonathan Liu /* Do dithering if panel only supports 6 bits per color */ 333f11adcecSJonathan Liu if (connector->display_info.bpc == 6) 334f11adcecSJonathan Liu val |= SUN4I_TCON0_FRM_CTL_EN; 335f11adcecSJonathan Liu 336f11adcecSJonathan Liu if (connector->display_info.num_bus_formats == 1) 337f11adcecSJonathan Liu bus_format = connector->display_info.bus_formats[0]; 338f11adcecSJonathan Liu 339f11adcecSJonathan Liu /* Check the connection format */ 340f11adcecSJonathan Liu switch (bus_format) { 341f11adcecSJonathan Liu case MEDIA_BUS_FMT_RGB565_1X16: 342f11adcecSJonathan Liu /* R and B components are only 5 bits deep */ 343f11adcecSJonathan Liu val |= SUN4I_TCON0_FRM_CTL_MODE_R; 344f11adcecSJonathan Liu val |= SUN4I_TCON0_FRM_CTL_MODE_B; 345df561f66SGustavo A. R. Silva fallthrough; 346f11adcecSJonathan Liu case MEDIA_BUS_FMT_RGB666_1X18: 347f11adcecSJonathan Liu case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG: 348f11adcecSJonathan Liu /* Fall through: enable dithering */ 349f11adcecSJonathan Liu val |= SUN4I_TCON0_FRM_CTL_EN; 350f11adcecSJonathan Liu break; 351f11adcecSJonathan Liu } 352f11adcecSJonathan Liu 353f11adcecSJonathan Liu /* Write dithering settings */ 354f11adcecSJonathan Liu regmap_write(tcon->regs, SUN4I_TCON_FRM_CTL_REG, val); 355f11adcecSJonathan Liu } 356f11adcecSJonathan Liu 357a08fc7c8SMaxime Ripard static void sun4i_tcon0_mode_set_cpu(struct sun4i_tcon *tcon, 35879891d56SChen-Yu Tsai const struct drm_encoder *encoder, 359a08fc7c8SMaxime Ripard const struct drm_display_mode *mode) 360a08fc7c8SMaxime Ripard { 36179891d56SChen-Yu Tsai /* TODO support normal CPU interface modes */ 36279891d56SChen-Yu Tsai struct sun6i_dsi *dsi = encoder_to_sun6i_dsi(encoder); 36379891d56SChen-Yu Tsai struct mipi_dsi_device *device = dsi->device; 364a08fc7c8SMaxime Ripard u8 bpp = mipi_dsi_pixel_format_to_bpp(device->format); 365a08fc7c8SMaxime Ripard u8 lanes = device->lanes; 366a08fc7c8SMaxime Ripard u32 block_space, start_delay; 367a08fc7c8SMaxime Ripard u32 tcon_div; 368a08fc7c8SMaxime Ripard 36985fb3526SMaxime Ripard tcon->dclk_min_div = SUN6I_DSI_TCON_DIV; 37085fb3526SMaxime Ripard tcon->dclk_max_div = SUN6I_DSI_TCON_DIV; 371a08fc7c8SMaxime Ripard 372a08fc7c8SMaxime Ripard sun4i_tcon0_mode_set_common(tcon, mode); 373a08fc7c8SMaxime Ripard 374f11adcecSJonathan Liu /* Set dithering if needed */ 375f11adcecSJonathan Liu sun4i_tcon0_mode_set_dithering(tcon, sun4i_tcon_get_connector(encoder)); 376f11adcecSJonathan Liu 377a08fc7c8SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 378a08fc7c8SMaxime Ripard SUN4I_TCON0_CTL_IF_MASK, 379a08fc7c8SMaxime Ripard SUN4I_TCON0_CTL_IF_8080); 380a08fc7c8SMaxime Ripard 381a08fc7c8SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_ECC_FIFO_REG, 382a08fc7c8SMaxime Ripard SUN4I_TCON_ECC_FIFO_EN); 383a08fc7c8SMaxime Ripard 384a08fc7c8SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_CPU_IF_REG, 385a08fc7c8SMaxime Ripard SUN4I_TCON0_CPU_IF_MODE_DSI | 386a08fc7c8SMaxime Ripard SUN4I_TCON0_CPU_IF_TRI_FIFO_FLUSH | 387a08fc7c8SMaxime Ripard SUN4I_TCON0_CPU_IF_TRI_FIFO_EN | 388a08fc7c8SMaxime Ripard SUN4I_TCON0_CPU_IF_TRI_EN); 389a08fc7c8SMaxime Ripard 390a08fc7c8SMaxime Ripard /* 391a08fc7c8SMaxime Ripard * This looks suspicious, but it works... 392a08fc7c8SMaxime Ripard * 393a08fc7c8SMaxime Ripard * The datasheet says that this should be set higher than 20 * 394a08fc7c8SMaxime Ripard * pixel cycle, but it's not clear what a pixel cycle is. 395a08fc7c8SMaxime Ripard */ 396a08fc7c8SMaxime Ripard regmap_read(tcon->regs, SUN4I_TCON0_DCLK_REG, &tcon_div); 397a08fc7c8SMaxime Ripard tcon_div &= GENMASK(6, 0); 398a08fc7c8SMaxime Ripard block_space = mode->htotal * bpp / (tcon_div * lanes); 399a08fc7c8SMaxime Ripard block_space -= mode->hdisplay + 40; 400a08fc7c8SMaxime Ripard 401a08fc7c8SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI0_REG, 402a08fc7c8SMaxime Ripard SUN4I_TCON0_CPU_TRI0_BLOCK_SPACE(block_space) | 403a08fc7c8SMaxime Ripard SUN4I_TCON0_CPU_TRI0_BLOCK_SIZE(mode->hdisplay)); 404a08fc7c8SMaxime Ripard 405a08fc7c8SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI1_REG, 406a08fc7c8SMaxime Ripard SUN4I_TCON0_CPU_TRI1_BLOCK_NUM(mode->vdisplay)); 407a08fc7c8SMaxime Ripard 408a08fc7c8SMaxime Ripard start_delay = (mode->crtc_vtotal - mode->crtc_vdisplay - 10 - 1); 409a08fc7c8SMaxime Ripard start_delay = start_delay * mode->crtc_htotal * 149; 410a08fc7c8SMaxime Ripard start_delay = start_delay / (mode->crtc_clock / 1000) / 8; 411a08fc7c8SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI2_REG, 412a08fc7c8SMaxime Ripard SUN4I_TCON0_CPU_TRI2_TRANS_START_SET(10) | 413a08fc7c8SMaxime Ripard SUN4I_TCON0_CPU_TRI2_START_DELAY(start_delay)); 414a08fc7c8SMaxime Ripard 415a08fc7c8SMaxime Ripard /* 416a08fc7c8SMaxime Ripard * The Allwinner BSP has a comment that the period should be 417a08fc7c8SMaxime Ripard * the display clock * 15, but uses an hardcoded 3000... 418a08fc7c8SMaxime Ripard */ 419a08fc7c8SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_SAFE_PERIOD_REG, 420a08fc7c8SMaxime Ripard SUN4I_TCON_SAFE_PERIOD_NUM(3000) | 421a08fc7c8SMaxime Ripard SUN4I_TCON_SAFE_PERIOD_MODE(3)); 422a08fc7c8SMaxime Ripard 423a08fc7c8SMaxime Ripard /* Enable the output on the pins */ 424a08fc7c8SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 425a08fc7c8SMaxime Ripard 0xe0000000); 426a08fc7c8SMaxime Ripard } 427a08fc7c8SMaxime Ripard 428a0c1214eSMaxime Ripard static void sun4i_tcon0_mode_set_lvds(struct sun4i_tcon *tcon, 429a0c1214eSMaxime Ripard const struct drm_encoder *encoder, 430a0c1214eSMaxime Ripard const struct drm_display_mode *mode) 431a0c1214eSMaxime Ripard { 432a0c1214eSMaxime Ripard unsigned int bp; 433a0c1214eSMaxime Ripard u8 clk_delay; 434a0c1214eSMaxime Ripard u32 reg, val = 0; 435a0c1214eSMaxime Ripard 43634d698f6SJernej Skrabec WARN_ON(!tcon->quirks->has_channel_0); 43734d698f6SJernej Skrabec 438a0c1214eSMaxime Ripard tcon->dclk_min_div = 7; 439a0c1214eSMaxime Ripard tcon->dclk_max_div = 7; 440a0c1214eSMaxime Ripard sun4i_tcon0_mode_set_common(tcon, mode); 441a0c1214eSMaxime Ripard 442f11adcecSJonathan Liu /* Set dithering if needed */ 443f11adcecSJonathan Liu sun4i_tcon0_mode_set_dithering(tcon, sun4i_tcon_get_connector(encoder)); 444f11adcecSJonathan Liu 445a0c1214eSMaxime Ripard /* Adjust clock delay */ 446a0c1214eSMaxime Ripard clk_delay = sun4i_tcon_get_clk_delay(mode, 0); 447a0c1214eSMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 448a0c1214eSMaxime Ripard SUN4I_TCON0_CTL_CLK_DELAY_MASK, 449a0c1214eSMaxime Ripard SUN4I_TCON0_CTL_CLK_DELAY(clk_delay)); 450a0c1214eSMaxime Ripard 451a0c1214eSMaxime Ripard /* 452a0c1214eSMaxime Ripard * This is called a backporch in the register documentation, 453a0c1214eSMaxime Ripard * but it really is the back porch + hsync 454a0c1214eSMaxime Ripard */ 455a0c1214eSMaxime Ripard bp = mode->crtc_htotal - mode->crtc_hsync_start; 456a0c1214eSMaxime Ripard DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n", 457a0c1214eSMaxime Ripard mode->crtc_htotal, bp); 458a0c1214eSMaxime Ripard 459a0c1214eSMaxime Ripard /* Set horizontal display timings */ 460a0c1214eSMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG, 461a0c1214eSMaxime Ripard SUN4I_TCON0_BASIC1_H_TOTAL(mode->htotal) | 462a0c1214eSMaxime Ripard SUN4I_TCON0_BASIC1_H_BACKPORCH(bp)); 463a0c1214eSMaxime Ripard 464a0c1214eSMaxime Ripard /* 465a0c1214eSMaxime Ripard * This is called a backporch in the register documentation, 466a0c1214eSMaxime Ripard * but it really is the back porch + hsync 467a0c1214eSMaxime Ripard */ 468a0c1214eSMaxime Ripard bp = mode->crtc_vtotal - mode->crtc_vsync_start; 469a0c1214eSMaxime Ripard DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n", 470a0c1214eSMaxime Ripard mode->crtc_vtotal, bp); 471a0c1214eSMaxime Ripard 472a0c1214eSMaxime Ripard /* Set vertical display timings */ 473a0c1214eSMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG, 474a0c1214eSMaxime Ripard SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) | 475a0c1214eSMaxime Ripard SUN4I_TCON0_BASIC2_V_BACKPORCH(bp)); 476a0c1214eSMaxime Ripard 4773bc46a08SMaxime Ripard reg = SUN4I_TCON0_LVDS_IF_CLK_SEL_TCON0; 478a0c1214eSMaxime Ripard if (sun4i_tcon_get_pixel_depth(encoder) == 24) 479a0c1214eSMaxime Ripard reg |= SUN4I_TCON0_LVDS_IF_BITWIDTH_24BITS; 480a0c1214eSMaxime Ripard else 481a0c1214eSMaxime Ripard reg |= SUN4I_TCON0_LVDS_IF_BITWIDTH_18BITS; 482a0c1214eSMaxime Ripard 483a0c1214eSMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_LVDS_IF_REG, reg); 484a0c1214eSMaxime Ripard 485a0c1214eSMaxime Ripard /* Setup the polarity of the various signals */ 486a0c1214eSMaxime Ripard if (!(mode->flags & DRM_MODE_FLAG_PHSYNC)) 487a0c1214eSMaxime Ripard val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE; 488a0c1214eSMaxime Ripard 489a0c1214eSMaxime Ripard if (!(mode->flags & DRM_MODE_FLAG_PVSYNC)) 490a0c1214eSMaxime Ripard val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE; 491a0c1214eSMaxime Ripard 492a0c1214eSMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_IO_POL_REG, val); 493a0c1214eSMaxime Ripard 494a0c1214eSMaxime Ripard /* Map output pins to channel 0 */ 495a0c1214eSMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 496a0c1214eSMaxime Ripard SUN4I_TCON_GCTL_IOMAP_MASK, 497a0c1214eSMaxime Ripard SUN4I_TCON_GCTL_IOMAP_TCON0); 49880b79e31SOndrej Jirman 49980b79e31SOndrej Jirman /* Enable the output on the pins */ 50080b79e31SOndrej Jirman regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0xe0000000); 501a0c1214eSMaxime Ripard } 502a0c1214eSMaxime Ripard 503ba19c537SMaxime Ripard static void sun4i_tcon0_mode_set_rgb(struct sun4i_tcon *tcon, 504b842e2c9SPaul Kocialkowski const struct drm_encoder *encoder, 5055b8f0910SMaxime Ripard const struct drm_display_mode *mode) 5069026e0d1SMaxime Ripard { 5074843c9a2SPaul Kocialkowski struct drm_connector *connector = sun4i_tcon_get_connector(encoder); 5081e612a0fSVille Syrjälä const struct drm_display_info *info = &connector->display_info; 5099026e0d1SMaxime Ripard unsigned int bp, hsync, vsync; 5109026e0d1SMaxime Ripard u8 clk_delay; 5119026e0d1SMaxime Ripard u32 val = 0; 5129026e0d1SMaxime Ripard 51334d698f6SJernej Skrabec WARN_ON(!tcon->quirks->has_channel_0); 51434d698f6SJernej Skrabec 5154396393fSChen-Yu Tsai tcon->dclk_min_div = tcon->quirks->dclk_min_div; 516ec08d596SMaxime Ripard tcon->dclk_max_div = 127; 517ba19c537SMaxime Ripard sun4i_tcon0_mode_set_common(tcon, mode); 51886cf6788SChen-Yu Tsai 519f11adcecSJonathan Liu /* Set dithering if needed */ 5204843c9a2SPaul Kocialkowski sun4i_tcon0_mode_set_dithering(tcon, connector); 521f11adcecSJonathan Liu 5229026e0d1SMaxime Ripard /* Adjust clock delay */ 5239026e0d1SMaxime Ripard clk_delay = sun4i_tcon_get_clk_delay(mode, 0); 5249026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 5259026e0d1SMaxime Ripard SUN4I_TCON0_CTL_CLK_DELAY_MASK, 5269026e0d1SMaxime Ripard SUN4I_TCON0_CTL_CLK_DELAY(clk_delay)); 5279026e0d1SMaxime Ripard 5289026e0d1SMaxime Ripard /* 5299026e0d1SMaxime Ripard * This is called a backporch in the register documentation, 53023a1cb11SChen-Yu Tsai * but it really is the back porch + hsync 5319026e0d1SMaxime Ripard */ 5329026e0d1SMaxime Ripard bp = mode->crtc_htotal - mode->crtc_hsync_start; 5339026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n", 5349026e0d1SMaxime Ripard mode->crtc_htotal, bp); 5359026e0d1SMaxime Ripard 5369026e0d1SMaxime Ripard /* Set horizontal display timings */ 5379026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG, 5389026e0d1SMaxime Ripard SUN4I_TCON0_BASIC1_H_TOTAL(mode->crtc_htotal) | 5399026e0d1SMaxime Ripard SUN4I_TCON0_BASIC1_H_BACKPORCH(bp)); 5409026e0d1SMaxime Ripard 5419026e0d1SMaxime Ripard /* 5429026e0d1SMaxime Ripard * This is called a backporch in the register documentation, 54323a1cb11SChen-Yu Tsai * but it really is the back porch + hsync 5449026e0d1SMaxime Ripard */ 5459026e0d1SMaxime Ripard bp = mode->crtc_vtotal - mode->crtc_vsync_start; 5469026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n", 5479026e0d1SMaxime Ripard mode->crtc_vtotal, bp); 5489026e0d1SMaxime Ripard 5499026e0d1SMaxime Ripard /* Set vertical display timings */ 5509026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG, 551a88cbbd4SMaxime Ripard SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) | 5529026e0d1SMaxime Ripard SUN4I_TCON0_BASIC2_V_BACKPORCH(bp)); 5539026e0d1SMaxime Ripard 5549026e0d1SMaxime Ripard /* Set Hsync and Vsync length */ 5559026e0d1SMaxime Ripard hsync = mode->crtc_hsync_end - mode->crtc_hsync_start; 5569026e0d1SMaxime Ripard vsync = mode->crtc_vsync_end - mode->crtc_vsync_start; 5579026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync); 5589026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_BASIC3_REG, 5599026e0d1SMaxime Ripard SUN4I_TCON0_BASIC3_V_SYNC(vsync) | 5609026e0d1SMaxime Ripard SUN4I_TCON0_BASIC3_H_SYNC(hsync)); 5619026e0d1SMaxime Ripard 5629026e0d1SMaxime Ripard /* Setup the polarity of the various signals */ 563fa4127c5SGiulio Benetti if (mode->flags & DRM_MODE_FLAG_PHSYNC) 5649026e0d1SMaxime Ripard val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE; 5659026e0d1SMaxime Ripard 566fa4127c5SGiulio Benetti if (mode->flags & DRM_MODE_FLAG_PVSYNC) 5679026e0d1SMaxime Ripard val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE; 5689026e0d1SMaxime Ripard 5691e612a0fSVille Syrjälä if (info->bus_flags & DRM_BUS_FLAG_DE_LOW) 57065bf2d54SPaul Kocialkowski val |= SUN4I_TCON0_IO_POL_DE_NEGATIVE; 57165bf2d54SPaul Kocialkowski 572490cda5aSGiulio Benetti /* 573490cda5aSGiulio Benetti * On A20 and similar SoCs, the only way to achieve Positive Edge 574490cda5aSGiulio Benetti * (Rising Edge), is setting dclk clock phase to 2/3(240°). 575490cda5aSGiulio Benetti * By default TCON works in Negative Edge(Falling Edge), 576490cda5aSGiulio Benetti * this is why phase is set to 0 in that case. 577490cda5aSGiulio Benetti * Unfortunately there's no way to logically invert dclk through 578490cda5aSGiulio Benetti * IO_POL register. 579490cda5aSGiulio Benetti * The only acceptable way to work, triple checked with scope, 580490cda5aSGiulio Benetti * is using clock phase set to 0° for Negative Edge and set to 240° 581490cda5aSGiulio Benetti * for Positive Edge. 582490cda5aSGiulio Benetti * On A33 and similar SoCs there would be a 90° phase option, 583490cda5aSGiulio Benetti * but it divides also dclk by 2. 584490cda5aSGiulio Benetti * Following code is a way to avoid quirks all around TCON 585490cda5aSGiulio Benetti * and DOTCLOCK drivers. 586490cda5aSGiulio Benetti */ 5871e612a0fSVille Syrjälä if (info->bus_flags & DRM_BUS_FLAG_PIXDATA_DRIVE_POSEDGE) 588490cda5aSGiulio Benetti clk_set_phase(tcon->dclk, 240); 589490cda5aSGiulio Benetti 5901e612a0fSVille Syrjälä if (info->bus_flags & DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE) 591490cda5aSGiulio Benetti clk_set_phase(tcon->dclk, 0); 592490cda5aSGiulio Benetti 5939026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON0_IO_POL_REG, 59465bf2d54SPaul Kocialkowski SUN4I_TCON0_IO_POL_HSYNC_POSITIVE | 59565bf2d54SPaul Kocialkowski SUN4I_TCON0_IO_POL_VSYNC_POSITIVE | 59665bf2d54SPaul Kocialkowski SUN4I_TCON0_IO_POL_DE_NEGATIVE, 5979026e0d1SMaxime Ripard val); 5989026e0d1SMaxime Ripard 5999026e0d1SMaxime Ripard /* Map output pins to channel 0 */ 6009026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 6019026e0d1SMaxime Ripard SUN4I_TCON_GCTL_IOMAP_MASK, 6029026e0d1SMaxime Ripard SUN4I_TCON_GCTL_IOMAP_TCON0); 6039026e0d1SMaxime Ripard 6049026e0d1SMaxime Ripard /* Enable the output on the pins */ 6059026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0); 6069026e0d1SMaxime Ripard } 6079026e0d1SMaxime Ripard 6085b8f0910SMaxime Ripard static void sun4i_tcon1_mode_set(struct sun4i_tcon *tcon, 6095b8f0910SMaxime Ripard const struct drm_display_mode *mode) 6109026e0d1SMaxime Ripard { 611b8317a3dSMaxime Ripard unsigned int bp, hsync, vsync, vtotal; 6129026e0d1SMaxime Ripard u8 clk_delay; 6139026e0d1SMaxime Ripard u32 val; 6149026e0d1SMaxime Ripard 61591ea2f29SChen-Yu Tsai WARN_ON(!tcon->quirks->has_channel_1); 6168e924047SMaxime Ripard 61786cf6788SChen-Yu Tsai /* Configure the dot clock */ 61886cf6788SChen-Yu Tsai clk_set_rate(tcon->sclk1, mode->crtc_clock * 1000); 61986cf6788SChen-Yu Tsai 6209026e0d1SMaxime Ripard /* Adjust clock delay */ 6219026e0d1SMaxime Ripard clk_delay = sun4i_tcon_get_clk_delay(mode, 1); 6229026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 6239026e0d1SMaxime Ripard SUN4I_TCON1_CTL_CLK_DELAY_MASK, 6249026e0d1SMaxime Ripard SUN4I_TCON1_CTL_CLK_DELAY(clk_delay)); 6259026e0d1SMaxime Ripard 6269026e0d1SMaxime Ripard /* Set interlaced mode */ 6279026e0d1SMaxime Ripard if (mode->flags & DRM_MODE_FLAG_INTERLACE) 6289026e0d1SMaxime Ripard val = SUN4I_TCON1_CTL_INTERLACE_ENABLE; 6299026e0d1SMaxime Ripard else 6309026e0d1SMaxime Ripard val = 0; 6319026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 6329026e0d1SMaxime Ripard SUN4I_TCON1_CTL_INTERLACE_ENABLE, 6339026e0d1SMaxime Ripard val); 6349026e0d1SMaxime Ripard 6359026e0d1SMaxime Ripard /* Set the input resolution */ 6369026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC0_REG, 6379026e0d1SMaxime Ripard SUN4I_TCON1_BASIC0_X(mode->crtc_hdisplay) | 6389026e0d1SMaxime Ripard SUN4I_TCON1_BASIC0_Y(mode->crtc_vdisplay)); 6399026e0d1SMaxime Ripard 6409026e0d1SMaxime Ripard /* Set the upscaling resolution */ 6419026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC1_REG, 6429026e0d1SMaxime Ripard SUN4I_TCON1_BASIC1_X(mode->crtc_hdisplay) | 6439026e0d1SMaxime Ripard SUN4I_TCON1_BASIC1_Y(mode->crtc_vdisplay)); 6449026e0d1SMaxime Ripard 6459026e0d1SMaxime Ripard /* Set the output resolution */ 6469026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC2_REG, 6479026e0d1SMaxime Ripard SUN4I_TCON1_BASIC2_X(mode->crtc_hdisplay) | 6489026e0d1SMaxime Ripard SUN4I_TCON1_BASIC2_Y(mode->crtc_vdisplay)); 6499026e0d1SMaxime Ripard 6509026e0d1SMaxime Ripard /* Set horizontal display timings */ 6513cb2f46bSMaxime Ripard bp = mode->crtc_htotal - mode->crtc_hsync_start; 6529026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n", 6539026e0d1SMaxime Ripard mode->htotal, bp); 6549026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC3_REG, 6559026e0d1SMaxime Ripard SUN4I_TCON1_BASIC3_H_TOTAL(mode->crtc_htotal) | 6569026e0d1SMaxime Ripard SUN4I_TCON1_BASIC3_H_BACKPORCH(bp)); 6579026e0d1SMaxime Ripard 6583cb2f46bSMaxime Ripard bp = mode->crtc_vtotal - mode->crtc_vsync_start; 6599026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n", 660b8317a3dSMaxime Ripard mode->crtc_vtotal, bp); 661b8317a3dSMaxime Ripard 662b8317a3dSMaxime Ripard /* 663b8317a3dSMaxime Ripard * The vertical resolution needs to be doubled in all 664b8317a3dSMaxime Ripard * cases. We could use crtc_vtotal and always multiply by two, 665b8317a3dSMaxime Ripard * but that leads to a rounding error in interlace when vtotal 666b8317a3dSMaxime Ripard * is odd. 667b8317a3dSMaxime Ripard * 668b8317a3dSMaxime Ripard * This happens with TV's PAL for example, where vtotal will 669b8317a3dSMaxime Ripard * be 625, crtc_vtotal 312, and thus crtc_vtotal * 2 will be 670b8317a3dSMaxime Ripard * 624, which apparently confuses the hardware. 671b8317a3dSMaxime Ripard * 672b8317a3dSMaxime Ripard * To work around this, we will always use vtotal, and 673b8317a3dSMaxime Ripard * multiply by two only if we're not in interlace. 674b8317a3dSMaxime Ripard */ 675b8317a3dSMaxime Ripard vtotal = mode->vtotal; 676b8317a3dSMaxime Ripard if (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) 677b8317a3dSMaxime Ripard vtotal = vtotal * 2; 678b8317a3dSMaxime Ripard 679b8317a3dSMaxime Ripard /* Set vertical display timings */ 6809026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC4_REG, 681b8317a3dSMaxime Ripard SUN4I_TCON1_BASIC4_V_TOTAL(vtotal) | 6829026e0d1SMaxime Ripard SUN4I_TCON1_BASIC4_V_BACKPORCH(bp)); 6839026e0d1SMaxime Ripard 6849026e0d1SMaxime Ripard /* Set Hsync and Vsync length */ 6859026e0d1SMaxime Ripard hsync = mode->crtc_hsync_end - mode->crtc_hsync_start; 6869026e0d1SMaxime Ripard vsync = mode->crtc_vsync_end - mode->crtc_vsync_start; 6879026e0d1SMaxime Ripard DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync); 6889026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_BASIC5_REG, 6899026e0d1SMaxime Ripard SUN4I_TCON1_BASIC5_V_SYNC(vsync) | 6909026e0d1SMaxime Ripard SUN4I_TCON1_BASIC5_H_SYNC(hsync)); 6919026e0d1SMaxime Ripard 692*50791f5dSJernej Skrabec /* Setup the polarity of multiple signals */ 693*50791f5dSJernej Skrabec if (tcon->quirks->polarity_in_ch0) { 694*50791f5dSJernej Skrabec val = 0; 695*50791f5dSJernej Skrabec 696*50791f5dSJernej Skrabec if (mode->flags & DRM_MODE_FLAG_PHSYNC) 697*50791f5dSJernej Skrabec val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE; 698*50791f5dSJernej Skrabec 699*50791f5dSJernej Skrabec if (mode->flags & DRM_MODE_FLAG_PVSYNC) 700*50791f5dSJernej Skrabec val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE; 701*50791f5dSJernej Skrabec 702*50791f5dSJernej Skrabec regmap_write(tcon->regs, SUN4I_TCON0_IO_POL_REG, val); 703*50791f5dSJernej Skrabec } else { 704*50791f5dSJernej Skrabec /* according to vendor driver, this bit must be always set */ 705*50791f5dSJernej Skrabec val = SUN4I_TCON1_IO_POL_UNKNOWN; 706*50791f5dSJernej Skrabec 707*50791f5dSJernej Skrabec if (mode->flags & DRM_MODE_FLAG_PHSYNC) 708*50791f5dSJernej Skrabec val |= SUN4I_TCON1_IO_POL_HSYNC_POSITIVE; 709*50791f5dSJernej Skrabec 710*50791f5dSJernej Skrabec if (mode->flags & DRM_MODE_FLAG_PVSYNC) 711*50791f5dSJernej Skrabec val |= SUN4I_TCON1_IO_POL_VSYNC_POSITIVE; 712*50791f5dSJernej Skrabec 713*50791f5dSJernej Skrabec regmap_write(tcon->regs, SUN4I_TCON1_IO_POL_REG, val); 714*50791f5dSJernej Skrabec } 715*50791f5dSJernej Skrabec 7169026e0d1SMaxime Ripard /* Map output pins to channel 1 */ 7179026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG, 7189026e0d1SMaxime Ripard SUN4I_TCON_GCTL_IOMAP_MASK, 7199026e0d1SMaxime Ripard SUN4I_TCON_GCTL_IOMAP_TCON1); 7209026e0d1SMaxime Ripard } 7215b8f0910SMaxime Ripard 7225b8f0910SMaxime Ripard void sun4i_tcon_mode_set(struct sun4i_tcon *tcon, 7235b8f0910SMaxime Ripard const struct drm_encoder *encoder, 7245b8f0910SMaxime Ripard const struct drm_display_mode *mode) 7255b8f0910SMaxime Ripard { 7265b8f0910SMaxime Ripard switch (encoder->encoder_type) { 727a08fc7c8SMaxime Ripard case DRM_MODE_ENCODER_DSI: 72879891d56SChen-Yu Tsai /* DSI is tied to special case of CPU interface */ 72979891d56SChen-Yu Tsai sun4i_tcon0_mode_set_cpu(tcon, encoder, mode); 730a08fc7c8SMaxime Ripard break; 731a0c1214eSMaxime Ripard case DRM_MODE_ENCODER_LVDS: 732a0c1214eSMaxime Ripard sun4i_tcon0_mode_set_lvds(tcon, encoder, mode); 733a0c1214eSMaxime Ripard break; 7345b8f0910SMaxime Ripard case DRM_MODE_ENCODER_NONE: 735b842e2c9SPaul Kocialkowski sun4i_tcon0_mode_set_rgb(tcon, encoder, mode); 7365b8f0910SMaxime Ripard sun4i_tcon_set_mux(tcon, 0, encoder); 7375b8f0910SMaxime Ripard break; 7385b8f0910SMaxime Ripard case DRM_MODE_ENCODER_TVDAC: 7395b8f0910SMaxime Ripard case DRM_MODE_ENCODER_TMDS: 7405b8f0910SMaxime Ripard sun4i_tcon1_mode_set(tcon, mode); 7415b8f0910SMaxime Ripard sun4i_tcon_set_mux(tcon, 1, encoder); 7425b8f0910SMaxime Ripard break; 7435b8f0910SMaxime Ripard default: 7445b8f0910SMaxime Ripard DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n"); 7455b8f0910SMaxime Ripard } 7465b8f0910SMaxime Ripard } 7475b8f0910SMaxime Ripard EXPORT_SYMBOL(sun4i_tcon_mode_set); 7489026e0d1SMaxime Ripard 7499026e0d1SMaxime Ripard static void sun4i_tcon_finish_page_flip(struct drm_device *dev, 7509026e0d1SMaxime Ripard struct sun4i_crtc *scrtc) 7519026e0d1SMaxime Ripard { 7529026e0d1SMaxime Ripard unsigned long flags; 7539026e0d1SMaxime Ripard 7549026e0d1SMaxime Ripard spin_lock_irqsave(&dev->event_lock, flags); 7559026e0d1SMaxime Ripard if (scrtc->event) { 7569026e0d1SMaxime Ripard drm_crtc_send_vblank_event(&scrtc->crtc, scrtc->event); 7579026e0d1SMaxime Ripard drm_crtc_vblank_put(&scrtc->crtc); 7589026e0d1SMaxime Ripard scrtc->event = NULL; 7599026e0d1SMaxime Ripard } 7609026e0d1SMaxime Ripard spin_unlock_irqrestore(&dev->event_lock, flags); 7619026e0d1SMaxime Ripard } 7629026e0d1SMaxime Ripard 7639026e0d1SMaxime Ripard static irqreturn_t sun4i_tcon_handler(int irq, void *private) 7649026e0d1SMaxime Ripard { 7659026e0d1SMaxime Ripard struct sun4i_tcon *tcon = private; 7669026e0d1SMaxime Ripard struct drm_device *drm = tcon->drm; 76746cce6daSChen-Yu Tsai struct sun4i_crtc *scrtc = tcon->crtc; 7683004f75fSMaxime Ripard struct sunxi_engine *engine = scrtc->engine; 7699026e0d1SMaxime Ripard unsigned int status; 7709026e0d1SMaxime Ripard 7719026e0d1SMaxime Ripard regmap_read(tcon->regs, SUN4I_TCON_GINT0_REG, &status); 7729026e0d1SMaxime Ripard 7739026e0d1SMaxime Ripard if (!(status & (SUN4I_TCON_GINT0_VBLANK_INT(0) | 774a493ceaeSMaxime Ripard SUN4I_TCON_GINT0_VBLANK_INT(1) | 775a493ceaeSMaxime Ripard SUN4I_TCON_GINT0_TCON0_TRI_FINISH_INT))) 7769026e0d1SMaxime Ripard return IRQ_NONE; 7779026e0d1SMaxime Ripard 7789026e0d1SMaxime Ripard drm_crtc_handle_vblank(&scrtc->crtc); 7799026e0d1SMaxime Ripard sun4i_tcon_finish_page_flip(drm, scrtc); 7809026e0d1SMaxime Ripard 7819026e0d1SMaxime Ripard /* Acknowledge the interrupt */ 7829026e0d1SMaxime Ripard regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, 7839026e0d1SMaxime Ripard SUN4I_TCON_GINT0_VBLANK_INT(0) | 784a493ceaeSMaxime Ripard SUN4I_TCON_GINT0_VBLANK_INT(1) | 785a493ceaeSMaxime Ripard SUN4I_TCON_GINT0_TCON0_TRI_FINISH_INT, 7869026e0d1SMaxime Ripard 0); 7879026e0d1SMaxime Ripard 7883004f75fSMaxime Ripard if (engine->ops->vblank_quirk) 7893004f75fSMaxime Ripard engine->ops->vblank_quirk(engine); 7903004f75fSMaxime Ripard 7919026e0d1SMaxime Ripard return IRQ_HANDLED; 7929026e0d1SMaxime Ripard } 7939026e0d1SMaxime Ripard 7949026e0d1SMaxime Ripard static int sun4i_tcon_init_clocks(struct device *dev, 7959026e0d1SMaxime Ripard struct sun4i_tcon *tcon) 7969026e0d1SMaxime Ripard { 7979026e0d1SMaxime Ripard tcon->clk = devm_clk_get(dev, "ahb"); 7989026e0d1SMaxime Ripard if (IS_ERR(tcon->clk)) { 7999026e0d1SMaxime Ripard dev_err(dev, "Couldn't get the TCON bus clock\n"); 8009026e0d1SMaxime Ripard return PTR_ERR(tcon->clk); 8019026e0d1SMaxime Ripard } 8029026e0d1SMaxime Ripard clk_prepare_enable(tcon->clk); 8039026e0d1SMaxime Ripard 80434d698f6SJernej Skrabec if (tcon->quirks->has_channel_0) { 8059026e0d1SMaxime Ripard tcon->sclk0 = devm_clk_get(dev, "tcon-ch0"); 8069026e0d1SMaxime Ripard if (IS_ERR(tcon->sclk0)) { 8079026e0d1SMaxime Ripard dev_err(dev, "Couldn't get the TCON channel 0 clock\n"); 8089026e0d1SMaxime Ripard return PTR_ERR(tcon->sclk0); 8099026e0d1SMaxime Ripard } 81034d698f6SJernej Skrabec } 811b14e945bSPaul Kocialkowski clk_prepare_enable(tcon->sclk0); 8129026e0d1SMaxime Ripard 81391ea2f29SChen-Yu Tsai if (tcon->quirks->has_channel_1) { 8149026e0d1SMaxime Ripard tcon->sclk1 = devm_clk_get(dev, "tcon-ch1"); 8159026e0d1SMaxime Ripard if (IS_ERR(tcon->sclk1)) { 8169026e0d1SMaxime Ripard dev_err(dev, "Couldn't get the TCON channel 1 clock\n"); 8179026e0d1SMaxime Ripard return PTR_ERR(tcon->sclk1); 8189026e0d1SMaxime Ripard } 8198e924047SMaxime Ripard } 8209026e0d1SMaxime Ripard 8214c7f16d1SChen-Yu Tsai return 0; 8229026e0d1SMaxime Ripard } 8239026e0d1SMaxime Ripard 8249026e0d1SMaxime Ripard static void sun4i_tcon_free_clocks(struct sun4i_tcon *tcon) 8259026e0d1SMaxime Ripard { 826b14e945bSPaul Kocialkowski clk_disable_unprepare(tcon->sclk0); 8279026e0d1SMaxime Ripard clk_disable_unprepare(tcon->clk); 8289026e0d1SMaxime Ripard } 8299026e0d1SMaxime Ripard 8309026e0d1SMaxime Ripard static int sun4i_tcon_init_irq(struct device *dev, 8319026e0d1SMaxime Ripard struct sun4i_tcon *tcon) 8329026e0d1SMaxime Ripard { 8339026e0d1SMaxime Ripard struct platform_device *pdev = to_platform_device(dev); 8349026e0d1SMaxime Ripard int irq, ret; 8359026e0d1SMaxime Ripard 8369026e0d1SMaxime Ripard irq = platform_get_irq(pdev, 0); 837ed7cca1fSMarkus Elfring if (irq < 0) 8389026e0d1SMaxime Ripard return irq; 8399026e0d1SMaxime Ripard 8409026e0d1SMaxime Ripard ret = devm_request_irq(dev, irq, sun4i_tcon_handler, 0, 8419026e0d1SMaxime Ripard dev_name(dev), tcon); 8429026e0d1SMaxime Ripard if (ret) { 8439026e0d1SMaxime Ripard dev_err(dev, "Couldn't request the IRQ\n"); 8449026e0d1SMaxime Ripard return ret; 8459026e0d1SMaxime Ripard } 8469026e0d1SMaxime Ripard 8479026e0d1SMaxime Ripard return 0; 8489026e0d1SMaxime Ripard } 8499026e0d1SMaxime Ripard 850f13478c9SRikard Falkeborn static const struct regmap_config sun4i_tcon_regmap_config = { 8519026e0d1SMaxime Ripard .reg_bits = 32, 8529026e0d1SMaxime Ripard .val_bits = 32, 8539026e0d1SMaxime Ripard .reg_stride = 4, 8549026e0d1SMaxime Ripard .max_register = 0x800, 8559026e0d1SMaxime Ripard }; 8569026e0d1SMaxime Ripard 8579026e0d1SMaxime Ripard static int sun4i_tcon_init_regmap(struct device *dev, 8589026e0d1SMaxime Ripard struct sun4i_tcon *tcon) 8599026e0d1SMaxime Ripard { 8609026e0d1SMaxime Ripard struct platform_device *pdev = to_platform_device(dev); 8619026e0d1SMaxime Ripard struct resource *res; 8629026e0d1SMaxime Ripard void __iomem *regs; 8639026e0d1SMaxime Ripard 8649026e0d1SMaxime Ripard res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 8659026e0d1SMaxime Ripard regs = devm_ioremap_resource(dev, res); 866af346f55SWei Yongjun if (IS_ERR(regs)) 8679026e0d1SMaxime Ripard return PTR_ERR(regs); 8689026e0d1SMaxime Ripard 8699026e0d1SMaxime Ripard tcon->regs = devm_regmap_init_mmio(dev, regs, 8709026e0d1SMaxime Ripard &sun4i_tcon_regmap_config); 8719026e0d1SMaxime Ripard if (IS_ERR(tcon->regs)) { 8729026e0d1SMaxime Ripard dev_err(dev, "Couldn't create the TCON regmap\n"); 8739026e0d1SMaxime Ripard return PTR_ERR(tcon->regs); 8749026e0d1SMaxime Ripard } 8759026e0d1SMaxime Ripard 8769026e0d1SMaxime Ripard /* Make sure the TCON is disabled and all IRQs are off */ 8779026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_GCTL_REG, 0); 8789026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_GINT0_REG, 0); 8799026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON_GINT1_REG, 0); 8809026e0d1SMaxime Ripard 8819026e0d1SMaxime Ripard /* Disable IO lines and set them to tristate */ 8829026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, ~0); 8839026e0d1SMaxime Ripard regmap_write(tcon->regs, SUN4I_TCON1_IO_TRI_REG, ~0); 8849026e0d1SMaxime Ripard 8859026e0d1SMaxime Ripard return 0; 8869026e0d1SMaxime Ripard } 8879026e0d1SMaxime Ripard 888b317fa3bSChen-Yu Tsai /* 889b317fa3bSChen-Yu Tsai * On SoCs with the old display pipeline design (Display Engine 1.0), 890b317fa3bSChen-Yu Tsai * the TCON is always tied to just one backend. Hence we can traverse 891b317fa3bSChen-Yu Tsai * the of_graph upwards to find the backend our tcon is connected to, 892b317fa3bSChen-Yu Tsai * and take its ID as our own. 893b317fa3bSChen-Yu Tsai * 894b317fa3bSChen-Yu Tsai * We can either identify backends from their compatible strings, which 895b317fa3bSChen-Yu Tsai * means maintaining a large list of them. Or, since the backend is 896b317fa3bSChen-Yu Tsai * registered and binded before the TCON, we can just go through the 897b317fa3bSChen-Yu Tsai * list of registered backends and compare the device node. 89887969338SIcenowy Zheng * 89987969338SIcenowy Zheng * As the structures now store engines instead of backends, here this 90087969338SIcenowy Zheng * function in fact searches the corresponding engine, and the ID is 90187969338SIcenowy Zheng * requested via the get_id function of the engine. 902b317fa3bSChen-Yu Tsai */ 903e8d5bbf7SChen-Yu Tsai static struct sunxi_engine * 904e8d5bbf7SChen-Yu Tsai sun4i_tcon_find_engine_traverse(struct sun4i_drv *drv, 90549836b11SJernej Skrabec struct device_node *node, 90649836b11SJernej Skrabec u32 port_id) 907b317fa3bSChen-Yu Tsai { 908b317fa3bSChen-Yu Tsai struct device_node *port, *ep, *remote; 909be3fe0f9SChen-Yu Tsai struct sunxi_engine *engine = ERR_PTR(-EINVAL); 91049836b11SJernej Skrabec u32 reg = 0; 911b317fa3bSChen-Yu Tsai 91249836b11SJernej Skrabec port = of_graph_get_port_by_id(node, port_id); 913b317fa3bSChen-Yu Tsai if (!port) 914b317fa3bSChen-Yu Tsai return ERR_PTR(-EINVAL); 915b317fa3bSChen-Yu Tsai 9161469619dSChen-Yu Tsai /* 9171469619dSChen-Yu Tsai * This only works if there is only one path from the TCON 9181469619dSChen-Yu Tsai * to any display engine. Otherwise the probe order of the 9191469619dSChen-Yu Tsai * TCONs and display engines is not guaranteed. They may 9201469619dSChen-Yu Tsai * either bind to the wrong one, or worse, bind to the same 9211469619dSChen-Yu Tsai * one if additional checks are not done. 9221469619dSChen-Yu Tsai * 9231469619dSChen-Yu Tsai * Bail out if there are multiple input connections. 9241469619dSChen-Yu Tsai */ 925be3fe0f9SChen-Yu Tsai if (of_get_available_child_count(port) != 1) 926be3fe0f9SChen-Yu Tsai goto out_put_port; 9271469619dSChen-Yu Tsai 928be3fe0f9SChen-Yu Tsai /* Get the first connection without specifying an ID */ 929be3fe0f9SChen-Yu Tsai ep = of_get_next_available_child(port, NULL); 930be3fe0f9SChen-Yu Tsai if (!ep) 931be3fe0f9SChen-Yu Tsai goto out_put_port; 932be3fe0f9SChen-Yu Tsai 933b317fa3bSChen-Yu Tsai remote = of_graph_get_remote_port_parent(ep); 934b317fa3bSChen-Yu Tsai if (!remote) 935be3fe0f9SChen-Yu Tsai goto out_put_ep; 936b317fa3bSChen-Yu Tsai 93787969338SIcenowy Zheng /* does this node match any registered engines? */ 938be3fe0f9SChen-Yu Tsai list_for_each_entry(engine, &drv->engine_list, list) 939be3fe0f9SChen-Yu Tsai if (remote == engine->node) 940be3fe0f9SChen-Yu Tsai goto out_put_remote; 941b317fa3bSChen-Yu Tsai 94249836b11SJernej Skrabec /* 94349836b11SJernej Skrabec * According to device tree binding input ports have even id 94449836b11SJernej Skrabec * number and output ports have odd id. Since component with 94549836b11SJernej Skrabec * more than one input and one output (TCON TOP) exits, correct 94649836b11SJernej Skrabec * remote input id has to be calculated by subtracting 1 from 94749836b11SJernej Skrabec * remote output id. If this for some reason can't be done, 0 94849836b11SJernej Skrabec * is used as input port id. 94949836b11SJernej Skrabec */ 950da82107eSJernej Skrabec of_node_put(port); 95149836b11SJernej Skrabec port = of_graph_get_remote_port(ep); 95249836b11SJernej Skrabec if (!of_property_read_u32(port, "reg", ®) && reg > 0) 95349836b11SJernej Skrabec reg -= 1; 95449836b11SJernej Skrabec 955b317fa3bSChen-Yu Tsai /* keep looking through upstream ports */ 95649836b11SJernej Skrabec engine = sun4i_tcon_find_engine_traverse(drv, remote, reg); 957b317fa3bSChen-Yu Tsai 958be3fe0f9SChen-Yu Tsai out_put_remote: 959be3fe0f9SChen-Yu Tsai of_node_put(remote); 960be3fe0f9SChen-Yu Tsai out_put_ep: 961be3fe0f9SChen-Yu Tsai of_node_put(ep); 962be3fe0f9SChen-Yu Tsai out_put_port: 963be3fe0f9SChen-Yu Tsai of_node_put(port); 964be3fe0f9SChen-Yu Tsai 965be3fe0f9SChen-Yu Tsai return engine; 966b317fa3bSChen-Yu Tsai } 967b317fa3bSChen-Yu Tsai 968e8d5bbf7SChen-Yu Tsai /* 969e8d5bbf7SChen-Yu Tsai * The device tree binding says that the remote endpoint ID of any 970e8d5bbf7SChen-Yu Tsai * connection between components, up to and including the TCON, of 971e8d5bbf7SChen-Yu Tsai * the display pipeline should be equal to the actual ID of the local 972e8d5bbf7SChen-Yu Tsai * component. Thus we can look at any one of the input connections of 973e8d5bbf7SChen-Yu Tsai * the TCONs, and use that connection's remote endpoint ID as our own. 974e8d5bbf7SChen-Yu Tsai * 975e8d5bbf7SChen-Yu Tsai * Since the user of this function already finds the input port, 976e8d5bbf7SChen-Yu Tsai * the port is passed in directly without further checks. 977e8d5bbf7SChen-Yu Tsai */ 978e8d5bbf7SChen-Yu Tsai static int sun4i_tcon_of_get_id_from_port(struct device_node *port) 979e8d5bbf7SChen-Yu Tsai { 980e8d5bbf7SChen-Yu Tsai struct device_node *ep; 981e8d5bbf7SChen-Yu Tsai int ret = -EINVAL; 982e8d5bbf7SChen-Yu Tsai 983e8d5bbf7SChen-Yu Tsai /* try finding an upstream endpoint */ 984e8d5bbf7SChen-Yu Tsai for_each_available_child_of_node(port, ep) { 985e8d5bbf7SChen-Yu Tsai struct device_node *remote; 986e8d5bbf7SChen-Yu Tsai u32 reg; 987e8d5bbf7SChen-Yu Tsai 988e8d5bbf7SChen-Yu Tsai remote = of_graph_get_remote_endpoint(ep); 989e8d5bbf7SChen-Yu Tsai if (!remote) 990e8d5bbf7SChen-Yu Tsai continue; 991e8d5bbf7SChen-Yu Tsai 992e8d5bbf7SChen-Yu Tsai ret = of_property_read_u32(remote, "reg", ®); 993e8d5bbf7SChen-Yu Tsai if (ret) 994e8d5bbf7SChen-Yu Tsai continue; 995e8d5bbf7SChen-Yu Tsai 996e8d5bbf7SChen-Yu Tsai ret = reg; 997e8d5bbf7SChen-Yu Tsai } 998e8d5bbf7SChen-Yu Tsai 999e8d5bbf7SChen-Yu Tsai return ret; 1000e8d5bbf7SChen-Yu Tsai } 1001e8d5bbf7SChen-Yu Tsai 1002e8d5bbf7SChen-Yu Tsai /* 1003e8d5bbf7SChen-Yu Tsai * Once we know the TCON's id, we can look through the list of 1004e8d5bbf7SChen-Yu Tsai * engines to find a matching one. We assume all engines have 1005e8d5bbf7SChen-Yu Tsai * been probed and added to the list. 1006e8d5bbf7SChen-Yu Tsai */ 1007e8d5bbf7SChen-Yu Tsai static struct sunxi_engine *sun4i_tcon_get_engine_by_id(struct sun4i_drv *drv, 1008e8d5bbf7SChen-Yu Tsai int id) 1009e8d5bbf7SChen-Yu Tsai { 1010e8d5bbf7SChen-Yu Tsai struct sunxi_engine *engine; 1011e8d5bbf7SChen-Yu Tsai 1012e8d5bbf7SChen-Yu Tsai list_for_each_entry(engine, &drv->engine_list, list) 1013e8d5bbf7SChen-Yu Tsai if (engine->id == id) 1014e8d5bbf7SChen-Yu Tsai return engine; 1015e8d5bbf7SChen-Yu Tsai 1016e8d5bbf7SChen-Yu Tsai return ERR_PTR(-EINVAL); 1017e8d5bbf7SChen-Yu Tsai } 1018e8d5bbf7SChen-Yu Tsai 1019cf77d79bSJernej Skrabec static bool sun4i_tcon_connected_to_tcon_top(struct device_node *node) 1020cf77d79bSJernej Skrabec { 1021cf77d79bSJernej Skrabec struct device_node *remote; 1022cf77d79bSJernej Skrabec bool ret = false; 1023cf77d79bSJernej Skrabec 1024cf77d79bSJernej Skrabec remote = of_graph_get_remote_node(node, 0, -1); 1025cf77d79bSJernej Skrabec if (remote) { 1026185e0bebSMaxime Ripard ret = !!(IS_ENABLED(CONFIG_DRM_SUN8I_TCON_TOP) && 1027185e0bebSMaxime Ripard of_match_node(sun8i_tcon_top_of_table, remote)); 1028cf77d79bSJernej Skrabec of_node_put(remote); 1029cf77d79bSJernej Skrabec } 1030cf77d79bSJernej Skrabec 1031cf77d79bSJernej Skrabec return ret; 1032cf77d79bSJernej Skrabec } 1033cf77d79bSJernej Skrabec 1034cf77d79bSJernej Skrabec static int sun4i_tcon_get_index(struct sun4i_drv *drv) 1035cf77d79bSJernej Skrabec { 1036cf77d79bSJernej Skrabec struct list_head *pos; 1037cf77d79bSJernej Skrabec int size = 0; 1038cf77d79bSJernej Skrabec 1039cf77d79bSJernej Skrabec /* 1040cf77d79bSJernej Skrabec * Because TCON is added to the list at the end of the probe 1041cf77d79bSJernej Skrabec * (after this function is called), index of the current TCON 1042cf77d79bSJernej Skrabec * will be same as current TCON list size. 1043cf77d79bSJernej Skrabec */ 1044cf77d79bSJernej Skrabec list_for_each(pos, &drv->tcon_list) 1045cf77d79bSJernej Skrabec ++size; 1046cf77d79bSJernej Skrabec 1047cf77d79bSJernej Skrabec return size; 1048cf77d79bSJernej Skrabec } 1049cf77d79bSJernej Skrabec 1050e8d5bbf7SChen-Yu Tsai /* 1051e8d5bbf7SChen-Yu Tsai * On SoCs with the old display pipeline design (Display Engine 1.0), 1052e8d5bbf7SChen-Yu Tsai * we assumed the TCON was always tied to just one backend. However 1053e8d5bbf7SChen-Yu Tsai * this proved not to be the case. On the A31, the TCON can select 1054e8d5bbf7SChen-Yu Tsai * either backend as its source. On the A20 (and likely on the A10), 1055e8d5bbf7SChen-Yu Tsai * the backend can choose which TCON to output to. 1056e8d5bbf7SChen-Yu Tsai * 1057e8d5bbf7SChen-Yu Tsai * The device tree binding says that the remote endpoint ID of any 1058e8d5bbf7SChen-Yu Tsai * connection between components, up to and including the TCON, of 1059e8d5bbf7SChen-Yu Tsai * the display pipeline should be equal to the actual ID of the local 1060e8d5bbf7SChen-Yu Tsai * component. Thus we should be able to look at any one of the input 1061e8d5bbf7SChen-Yu Tsai * connections of the TCONs, and use that connection's remote endpoint 1062e8d5bbf7SChen-Yu Tsai * ID as our own. 1063e8d5bbf7SChen-Yu Tsai * 1064e8d5bbf7SChen-Yu Tsai * However the connections between the backend and TCON were assumed 1065e8d5bbf7SChen-Yu Tsai * to be always singular, and their endpoit IDs were all incorrectly 1066e8d5bbf7SChen-Yu Tsai * set to 0. This means for these old device trees, we cannot just look 1067e8d5bbf7SChen-Yu Tsai * up the remote endpoint ID of a TCON input endpoint. TCON1 would be 1068e8d5bbf7SChen-Yu Tsai * incorrectly identified as TCON0. 1069e8d5bbf7SChen-Yu Tsai * 1070e8d5bbf7SChen-Yu Tsai * This function first checks if the TCON node has 2 input endpoints. 1071e8d5bbf7SChen-Yu Tsai * If so, then the device tree is a corrected version, and it will use 1072e8d5bbf7SChen-Yu Tsai * sun4i_tcon_of_get_id() and sun4i_tcon_get_engine_by_id() from above 1073e8d5bbf7SChen-Yu Tsai * to fetch the ID and engine directly. If not, then it is likely an 1074e8d5bbf7SChen-Yu Tsai * old device trees, where the endpoint IDs were incorrect, but did not 1075e8d5bbf7SChen-Yu Tsai * have endpoint connections between the backend and TCON across 1076e8d5bbf7SChen-Yu Tsai * different display pipelines. It will fall back to the old method of 1077e8d5bbf7SChen-Yu Tsai * traversing the of_graph to try and find a matching engine by device 1078e8d5bbf7SChen-Yu Tsai * node. 1079e8d5bbf7SChen-Yu Tsai * 1080e8d5bbf7SChen-Yu Tsai * In the case of single display pipeline device trees, either method 1081e8d5bbf7SChen-Yu Tsai * works. 1082e8d5bbf7SChen-Yu Tsai */ 1083e8d5bbf7SChen-Yu Tsai static struct sunxi_engine *sun4i_tcon_find_engine(struct sun4i_drv *drv, 1084e8d5bbf7SChen-Yu Tsai struct device_node *node) 1085e8d5bbf7SChen-Yu Tsai { 1086e8d5bbf7SChen-Yu Tsai struct device_node *port; 1087e8d5bbf7SChen-Yu Tsai struct sunxi_engine *engine; 1088e8d5bbf7SChen-Yu Tsai 1089e8d5bbf7SChen-Yu Tsai port = of_graph_get_port_by_id(node, 0); 1090e8d5bbf7SChen-Yu Tsai if (!port) 1091e8d5bbf7SChen-Yu Tsai return ERR_PTR(-EINVAL); 1092e8d5bbf7SChen-Yu Tsai 1093e8d5bbf7SChen-Yu Tsai /* 1094e8d5bbf7SChen-Yu Tsai * Is this a corrected device tree with cross pipeline 1095e8d5bbf7SChen-Yu Tsai * connections between the backend and TCON? 1096e8d5bbf7SChen-Yu Tsai */ 1097e8d5bbf7SChen-Yu Tsai if (of_get_child_count(port) > 1) { 1098cf77d79bSJernej Skrabec int id; 1099cf77d79bSJernej Skrabec 1100cf77d79bSJernej Skrabec /* 1101cf77d79bSJernej Skrabec * When pipeline has the same number of TCONs and engines which 1102cf77d79bSJernej Skrabec * are represented by frontends/backends (DE1) or mixers (DE2), 1103cf77d79bSJernej Skrabec * we match them by their respective IDs. However, if pipeline 1104cf77d79bSJernej Skrabec * contains TCON TOP, chances are that there are either more 1105cf77d79bSJernej Skrabec * TCONs than engines (R40) or TCONs with non-consecutive ids. 1106cf77d79bSJernej Skrabec * (H6). In that case it's easier just use TCON index in list 1107cf77d79bSJernej Skrabec * as an id. That means that on R40, any 2 TCONs can be enabled 1108cf77d79bSJernej Skrabec * in DT out of 4 (there are 2 mixers). Due to the design of 1109cf77d79bSJernej Skrabec * TCON TOP, remaining 2 TCONs can't be connected to anything 1110cf77d79bSJernej Skrabec * anyway. 1111cf77d79bSJernej Skrabec */ 1112cf77d79bSJernej Skrabec if (sun4i_tcon_connected_to_tcon_top(node)) 1113cf77d79bSJernej Skrabec id = sun4i_tcon_get_index(drv); 1114cf77d79bSJernej Skrabec else 1115cf77d79bSJernej Skrabec id = sun4i_tcon_of_get_id_from_port(port); 1116e8d5bbf7SChen-Yu Tsai 1117e8d5bbf7SChen-Yu Tsai /* Get our engine by matching our ID */ 1118e8d5bbf7SChen-Yu Tsai engine = sun4i_tcon_get_engine_by_id(drv, id); 1119e8d5bbf7SChen-Yu Tsai 1120e8d5bbf7SChen-Yu Tsai of_node_put(port); 1121e8d5bbf7SChen-Yu Tsai return engine; 1122e8d5bbf7SChen-Yu Tsai } 1123e8d5bbf7SChen-Yu Tsai 1124e8d5bbf7SChen-Yu Tsai /* Fallback to old method by traversing input endpoints */ 1125e8d5bbf7SChen-Yu Tsai of_node_put(port); 112649836b11SJernej Skrabec return sun4i_tcon_find_engine_traverse(drv, node, 0); 1127e8d5bbf7SChen-Yu Tsai } 1128e8d5bbf7SChen-Yu Tsai 11299026e0d1SMaxime Ripard static int sun4i_tcon_bind(struct device *dev, struct device *master, 11309026e0d1SMaxime Ripard void *data) 11319026e0d1SMaxime Ripard { 11329026e0d1SMaxime Ripard struct drm_device *drm = data; 11339026e0d1SMaxime Ripard struct sun4i_drv *drv = drm->dev_private; 113487969338SIcenowy Zheng struct sunxi_engine *engine; 1135a0c1214eSMaxime Ripard struct device_node *remote; 11369026e0d1SMaxime Ripard struct sun4i_tcon *tcon; 11376664e9dcSChen-Yu Tsai struct reset_control *edp_rstc; 1138a0c1214eSMaxime Ripard bool has_lvds_rst, has_lvds_alt, can_lvds; 11399026e0d1SMaxime Ripard int ret; 11409026e0d1SMaxime Ripard 114187969338SIcenowy Zheng engine = sun4i_tcon_find_engine(drv, dev->of_node); 114287969338SIcenowy Zheng if (IS_ERR(engine)) { 114387969338SIcenowy Zheng dev_err(dev, "Couldn't find matching engine\n"); 114480a58240SChen-Yu Tsai return -EPROBE_DEFER; 1145b317fa3bSChen-Yu Tsai } 114680a58240SChen-Yu Tsai 11479026e0d1SMaxime Ripard tcon = devm_kzalloc(dev, sizeof(*tcon), GFP_KERNEL); 11489026e0d1SMaxime Ripard if (!tcon) 11499026e0d1SMaxime Ripard return -ENOMEM; 11509026e0d1SMaxime Ripard dev_set_drvdata(dev, tcon); 11519026e0d1SMaxime Ripard tcon->drm = drm; 1152ae558110SMaxime Ripard tcon->dev = dev; 115387969338SIcenowy Zheng tcon->id = engine->id; 115491ea2f29SChen-Yu Tsai tcon->quirks = of_device_get_match_data(dev); 11559026e0d1SMaxime Ripard 11569026e0d1SMaxime Ripard tcon->lcd_rst = devm_reset_control_get(dev, "lcd"); 11579026e0d1SMaxime Ripard if (IS_ERR(tcon->lcd_rst)) { 11589026e0d1SMaxime Ripard dev_err(dev, "Couldn't get our reset line\n"); 11599026e0d1SMaxime Ripard return PTR_ERR(tcon->lcd_rst); 11609026e0d1SMaxime Ripard } 11619026e0d1SMaxime Ripard 11626664e9dcSChen-Yu Tsai if (tcon->quirks->needs_edp_reset) { 11636664e9dcSChen-Yu Tsai edp_rstc = devm_reset_control_get_shared(dev, "edp"); 11646664e9dcSChen-Yu Tsai if (IS_ERR(edp_rstc)) { 11656664e9dcSChen-Yu Tsai dev_err(dev, "Couldn't get edp reset line\n"); 11666664e9dcSChen-Yu Tsai return PTR_ERR(edp_rstc); 11676664e9dcSChen-Yu Tsai } 11686664e9dcSChen-Yu Tsai 11696664e9dcSChen-Yu Tsai ret = reset_control_deassert(edp_rstc); 11706664e9dcSChen-Yu Tsai if (ret) { 11716664e9dcSChen-Yu Tsai dev_err(dev, "Couldn't deassert edp reset line\n"); 11726664e9dcSChen-Yu Tsai return ret; 11736664e9dcSChen-Yu Tsai } 11746664e9dcSChen-Yu Tsai } 11756664e9dcSChen-Yu Tsai 11769026e0d1SMaxime Ripard /* Make sure our TCON is reset */ 1177d57294c1SChen-Yu Tsai ret = reset_control_reset(tcon->lcd_rst); 11789026e0d1SMaxime Ripard if (ret) { 11799026e0d1SMaxime Ripard dev_err(dev, "Couldn't deassert our reset line\n"); 11809026e0d1SMaxime Ripard return ret; 11819026e0d1SMaxime Ripard } 11829026e0d1SMaxime Ripard 1183e742a17cSMaxime Ripard if (tcon->quirks->supports_lvds) { 1184a0c1214eSMaxime Ripard /* 1185e742a17cSMaxime Ripard * This can only be made optional since we've had DT 1186e742a17cSMaxime Ripard * nodes without the LVDS reset properties. 1187a0c1214eSMaxime Ripard * 1188e742a17cSMaxime Ripard * If the property is missing, just disable LVDS, and 1189e742a17cSMaxime Ripard * print a warning. 1190a0c1214eSMaxime Ripard */ 1191a0c1214eSMaxime Ripard tcon->lvds_rst = devm_reset_control_get_optional(dev, "lvds"); 1192a0c1214eSMaxime Ripard if (IS_ERR(tcon->lvds_rst)) { 1193a0c1214eSMaxime Ripard dev_err(dev, "Couldn't get our reset line\n"); 1194a0c1214eSMaxime Ripard return PTR_ERR(tcon->lvds_rst); 1195a0c1214eSMaxime Ripard } else if (tcon->lvds_rst) { 1196a0c1214eSMaxime Ripard has_lvds_rst = true; 1197a0c1214eSMaxime Ripard reset_control_reset(tcon->lvds_rst); 1198a0c1214eSMaxime Ripard } else { 1199a0c1214eSMaxime Ripard has_lvds_rst = false; 1200a0c1214eSMaxime Ripard } 1201a0c1214eSMaxime Ripard 1202a0c1214eSMaxime Ripard /* 1203e742a17cSMaxime Ripard * This can only be made optional since we've had DT 1204e742a17cSMaxime Ripard * nodes without the LVDS reset properties. 1205a0c1214eSMaxime Ripard * 1206e742a17cSMaxime Ripard * If the property is missing, just disable LVDS, and 1207e742a17cSMaxime Ripard * print a warning. 1208a0c1214eSMaxime Ripard */ 1209a0c1214eSMaxime Ripard if (tcon->quirks->has_lvds_alt) { 1210a0c1214eSMaxime Ripard tcon->lvds_pll = devm_clk_get(dev, "lvds-alt"); 1211a0c1214eSMaxime Ripard if (IS_ERR(tcon->lvds_pll)) { 1212a0c1214eSMaxime Ripard if (PTR_ERR(tcon->lvds_pll) == -ENOENT) { 1213a0c1214eSMaxime Ripard has_lvds_alt = false; 1214a0c1214eSMaxime Ripard } else { 1215a0c1214eSMaxime Ripard dev_err(dev, "Couldn't get the LVDS PLL\n"); 121686a3ae58SDan Carpenter return PTR_ERR(tcon->lvds_pll); 1217a0c1214eSMaxime Ripard } 1218a0c1214eSMaxime Ripard } else { 1219a0c1214eSMaxime Ripard has_lvds_alt = true; 1220a0c1214eSMaxime Ripard } 1221a0c1214eSMaxime Ripard } 1222a0c1214eSMaxime Ripard 1223e742a17cSMaxime Ripard if (!has_lvds_rst || 1224e742a17cSMaxime Ripard (tcon->quirks->has_lvds_alt && !has_lvds_alt)) { 1225e742a17cSMaxime Ripard dev_warn(dev, "Missing LVDS properties, Please upgrade your DT\n"); 1226a0c1214eSMaxime Ripard dev_warn(dev, "LVDS output disabled\n"); 1227a0c1214eSMaxime Ripard can_lvds = false; 1228a0c1214eSMaxime Ripard } else { 1229a0c1214eSMaxime Ripard can_lvds = true; 1230a0c1214eSMaxime Ripard } 1231e742a17cSMaxime Ripard } else { 1232e742a17cSMaxime Ripard can_lvds = false; 1233e742a17cSMaxime Ripard } 1234a0c1214eSMaxime Ripard 12359026e0d1SMaxime Ripard ret = sun4i_tcon_init_clocks(dev, tcon); 12369026e0d1SMaxime Ripard if (ret) { 12379026e0d1SMaxime Ripard dev_err(dev, "Couldn't init our TCON clocks\n"); 12389026e0d1SMaxime Ripard goto err_assert_reset; 12399026e0d1SMaxime Ripard } 12409026e0d1SMaxime Ripard 12414c7f16d1SChen-Yu Tsai ret = sun4i_tcon_init_regmap(dev, tcon); 12429026e0d1SMaxime Ripard if (ret) { 12434c7f16d1SChen-Yu Tsai dev_err(dev, "Couldn't init our TCON regmap\n"); 12449026e0d1SMaxime Ripard goto err_free_clocks; 12459026e0d1SMaxime Ripard } 12469026e0d1SMaxime Ripard 124734d698f6SJernej Skrabec if (tcon->quirks->has_channel_0) { 12484c7f16d1SChen-Yu Tsai ret = sun4i_dclk_create(dev, tcon); 12494c7f16d1SChen-Yu Tsai if (ret) { 12504c7f16d1SChen-Yu Tsai dev_err(dev, "Couldn't create our TCON dot clock\n"); 12514c7f16d1SChen-Yu Tsai goto err_free_clocks; 12524c7f16d1SChen-Yu Tsai } 125334d698f6SJernej Skrabec } 12544c7f16d1SChen-Yu Tsai 12559026e0d1SMaxime Ripard ret = sun4i_tcon_init_irq(dev, tcon); 12569026e0d1SMaxime Ripard if (ret) { 12579026e0d1SMaxime Ripard dev_err(dev, "Couldn't init our TCON interrupts\n"); 12584c7f16d1SChen-Yu Tsai goto err_free_dotclock; 12599026e0d1SMaxime Ripard } 12609026e0d1SMaxime Ripard 126187969338SIcenowy Zheng tcon->crtc = sun4i_crtc_init(drm, engine, tcon); 126246cce6daSChen-Yu Tsai if (IS_ERR(tcon->crtc)) { 126346cce6daSChen-Yu Tsai dev_err(dev, "Couldn't create our CRTC\n"); 126446cce6daSChen-Yu Tsai ret = PTR_ERR(tcon->crtc); 126592411f6dSMaxime Ripard goto err_free_dotclock; 126646cce6daSChen-Yu Tsai } 126746cce6daSChen-Yu Tsai 12682a72d0c5SJernej Skrabec if (tcon->quirks->has_channel_0) { 1269a0c1214eSMaxime Ripard /* 1270a0c1214eSMaxime Ripard * If we have an LVDS panel connected to the TCON, we should 1271a0c1214eSMaxime Ripard * just probe the LVDS connector. Otherwise, just probe RGB as 1272a0c1214eSMaxime Ripard * we used to. 1273a0c1214eSMaxime Ripard */ 1274a0c1214eSMaxime Ripard remote = of_graph_get_remote_node(dev->of_node, 1, 0); 1275a0c1214eSMaxime Ripard if (of_device_is_compatible(remote, "panel-lvds")) 1276a0c1214eSMaxime Ripard if (can_lvds) 1277a0c1214eSMaxime Ripard ret = sun4i_lvds_init(drm, tcon); 1278a0c1214eSMaxime Ripard else 1279a0c1214eSMaxime Ripard ret = -EINVAL; 1280a0c1214eSMaxime Ripard else 1281b9c8506cSChen-Yu Tsai ret = sun4i_rgb_init(drm, tcon); 1282a0c1214eSMaxime Ripard of_node_put(remote); 1283a0c1214eSMaxime Ripard 128413fef095SChen-Yu Tsai if (ret < 0) 128592411f6dSMaxime Ripard goto err_free_dotclock; 12862a72d0c5SJernej Skrabec } 128713fef095SChen-Yu Tsai 128827e18de7SChen-Yu Tsai if (tcon->quirks->needs_de_be_mux) { 128927e18de7SChen-Yu Tsai /* 129027e18de7SChen-Yu Tsai * We assume there is no dynamic muxing of backends 129127e18de7SChen-Yu Tsai * and TCONs, so we select the backend with same ID. 129227e18de7SChen-Yu Tsai * 129327e18de7SChen-Yu Tsai * While dynamic selection might be interesting, since 129427e18de7SChen-Yu Tsai * the CRTC is tied to the TCON, while the layers are 129527e18de7SChen-Yu Tsai * tied to the backends, this means, we will need to 129627e18de7SChen-Yu Tsai * switch between groups of layers. There might not be 129727e18de7SChen-Yu Tsai * a way to represent this constraint in DRM. 129827e18de7SChen-Yu Tsai */ 129927e18de7SChen-Yu Tsai regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG, 130027e18de7SChen-Yu Tsai SUN4I_TCON0_CTL_SRC_SEL_MASK, 130127e18de7SChen-Yu Tsai tcon->id); 130227e18de7SChen-Yu Tsai regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG, 130327e18de7SChen-Yu Tsai SUN4I_TCON1_CTL_SRC_SEL_MASK, 130427e18de7SChen-Yu Tsai tcon->id); 130527e18de7SChen-Yu Tsai } 130627e18de7SChen-Yu Tsai 130780a58240SChen-Yu Tsai list_add_tail(&tcon->list, &drv->tcon_list); 130880a58240SChen-Yu Tsai 130913fef095SChen-Yu Tsai return 0; 13109026e0d1SMaxime Ripard 13114c7f16d1SChen-Yu Tsai err_free_dotclock: 131234d698f6SJernej Skrabec if (tcon->quirks->has_channel_0) 13134c7f16d1SChen-Yu Tsai sun4i_dclk_free(tcon); 13149026e0d1SMaxime Ripard err_free_clocks: 13159026e0d1SMaxime Ripard sun4i_tcon_free_clocks(tcon); 13169026e0d1SMaxime Ripard err_assert_reset: 13179026e0d1SMaxime Ripard reset_control_assert(tcon->lcd_rst); 13189026e0d1SMaxime Ripard return ret; 13199026e0d1SMaxime Ripard } 13209026e0d1SMaxime Ripard 13219026e0d1SMaxime Ripard static void sun4i_tcon_unbind(struct device *dev, struct device *master, 13229026e0d1SMaxime Ripard void *data) 13239026e0d1SMaxime Ripard { 13249026e0d1SMaxime Ripard struct sun4i_tcon *tcon = dev_get_drvdata(dev); 13259026e0d1SMaxime Ripard 132680a58240SChen-Yu Tsai list_del(&tcon->list); 132734d698f6SJernej Skrabec if (tcon->quirks->has_channel_0) 13284c7f16d1SChen-Yu Tsai sun4i_dclk_free(tcon); 13299026e0d1SMaxime Ripard sun4i_tcon_free_clocks(tcon); 13309026e0d1SMaxime Ripard } 13319026e0d1SMaxime Ripard 1332dfeb693dSJulia Lawall static const struct component_ops sun4i_tcon_ops = { 13339026e0d1SMaxime Ripard .bind = sun4i_tcon_bind, 13349026e0d1SMaxime Ripard .unbind = sun4i_tcon_unbind, 13359026e0d1SMaxime Ripard }; 13369026e0d1SMaxime Ripard 13379026e0d1SMaxime Ripard static int sun4i_tcon_probe(struct platform_device *pdev) 13389026e0d1SMaxime Ripard { 133929e57fabSMaxime Ripard struct device_node *node = pdev->dev.of_node; 134063d6310fSJernej Skrabec const struct sun4i_tcon_quirks *quirks; 1341894f5a9fSMaxime Ripard struct drm_bridge *bridge; 134229e57fabSMaxime Ripard struct drm_panel *panel; 1343ebc94461SRob Herring int ret; 134429e57fabSMaxime Ripard 134563d6310fSJernej Skrabec quirks = of_device_get_match_data(&pdev->dev); 134663d6310fSJernej Skrabec 134763d6310fSJernej Skrabec /* panels and bridges are present only on TCONs with channel 0 */ 134863d6310fSJernej Skrabec if (quirks->has_channel_0) { 1349ebc94461SRob Herring ret = drm_of_find_panel_or_bridge(node, 1, 0, &panel, &bridge); 1350ebc94461SRob Herring if (ret == -EPROBE_DEFER) 1351ebc94461SRob Herring return ret; 135263d6310fSJernej Skrabec } 135329e57fabSMaxime Ripard 13549026e0d1SMaxime Ripard return component_add(&pdev->dev, &sun4i_tcon_ops); 13559026e0d1SMaxime Ripard } 13569026e0d1SMaxime Ripard 13579026e0d1SMaxime Ripard static int sun4i_tcon_remove(struct platform_device *pdev) 13589026e0d1SMaxime Ripard { 13599026e0d1SMaxime Ripard component_del(&pdev->dev, &sun4i_tcon_ops); 13609026e0d1SMaxime Ripard 13619026e0d1SMaxime Ripard return 0; 13629026e0d1SMaxime Ripard } 13639026e0d1SMaxime Ripard 1364ad537fb2SChen-Yu Tsai /* platform specific TCON muxing callbacks */ 13654bb206bfSJonathan Liu static int sun4i_a10_tcon_set_mux(struct sun4i_tcon *tcon, 13664bb206bfSJonathan Liu const struct drm_encoder *encoder) 13674bb206bfSJonathan Liu { 13684bb206bfSJonathan Liu struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev); 13694bb206bfSJonathan Liu u32 shift; 13704bb206bfSJonathan Liu 13714bb206bfSJonathan Liu if (!tcon0) 13724bb206bfSJonathan Liu return -EINVAL; 13734bb206bfSJonathan Liu 13744bb206bfSJonathan Liu switch (encoder->encoder_type) { 13754bb206bfSJonathan Liu case DRM_MODE_ENCODER_TMDS: 13764bb206bfSJonathan Liu /* HDMI */ 13774bb206bfSJonathan Liu shift = 8; 13784bb206bfSJonathan Liu break; 13794bb206bfSJonathan Liu default: 13804bb206bfSJonathan Liu return -EINVAL; 13814bb206bfSJonathan Liu } 13824bb206bfSJonathan Liu 13834bb206bfSJonathan Liu regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG, 13844bb206bfSJonathan Liu 0x3 << shift, tcon->id << shift); 13854bb206bfSJonathan Liu 13864bb206bfSJonathan Liu return 0; 13874bb206bfSJonathan Liu } 13884bb206bfSJonathan Liu 1389ad537fb2SChen-Yu Tsai static int sun5i_a13_tcon_set_mux(struct sun4i_tcon *tcon, 1390abcb8766SMaxime Ripard const struct drm_encoder *encoder) 1391ad537fb2SChen-Yu Tsai { 1392ad537fb2SChen-Yu Tsai u32 val; 1393ad537fb2SChen-Yu Tsai 1394ad537fb2SChen-Yu Tsai if (encoder->encoder_type == DRM_MODE_ENCODER_TVDAC) 1395ad537fb2SChen-Yu Tsai val = 1; 1396ad537fb2SChen-Yu Tsai else 1397ad537fb2SChen-Yu Tsai val = 0; 1398ad537fb2SChen-Yu Tsai 1399ad537fb2SChen-Yu Tsai /* 1400ad537fb2SChen-Yu Tsai * FIXME: Undocumented bits 1401ad537fb2SChen-Yu Tsai */ 1402ad537fb2SChen-Yu Tsai return regmap_write(tcon->regs, SUN4I_TCON_MUX_CTRL_REG, val); 1403ad537fb2SChen-Yu Tsai } 1404ad537fb2SChen-Yu Tsai 140567e32645SChen-Yu Tsai static int sun6i_tcon_set_mux(struct sun4i_tcon *tcon, 1406abcb8766SMaxime Ripard const struct drm_encoder *encoder) 140767e32645SChen-Yu Tsai { 140867e32645SChen-Yu Tsai struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev); 140967e32645SChen-Yu Tsai u32 shift; 141067e32645SChen-Yu Tsai 141167e32645SChen-Yu Tsai if (!tcon0) 141267e32645SChen-Yu Tsai return -EINVAL; 141367e32645SChen-Yu Tsai 141467e32645SChen-Yu Tsai switch (encoder->encoder_type) { 141567e32645SChen-Yu Tsai case DRM_MODE_ENCODER_TMDS: 141667e32645SChen-Yu Tsai /* HDMI */ 141767e32645SChen-Yu Tsai shift = 8; 141867e32645SChen-Yu Tsai break; 141967e32645SChen-Yu Tsai default: 142067e32645SChen-Yu Tsai /* TODO A31 has MIPI DSI but A31s does not */ 142167e32645SChen-Yu Tsai return -EINVAL; 142267e32645SChen-Yu Tsai } 142367e32645SChen-Yu Tsai 142467e32645SChen-Yu Tsai regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG, 142567e32645SChen-Yu Tsai 0x3 << shift, tcon->id << shift); 142667e32645SChen-Yu Tsai 142767e32645SChen-Yu Tsai return 0; 142867e32645SChen-Yu Tsai } 142967e32645SChen-Yu Tsai 14300305189aSJernej Skrabec static int sun8i_r40_tcon_tv_set_mux(struct sun4i_tcon *tcon, 14310305189aSJernej Skrabec const struct drm_encoder *encoder) 14320305189aSJernej Skrabec { 14330305189aSJernej Skrabec struct device_node *port, *remote; 14340305189aSJernej Skrabec struct platform_device *pdev; 14350305189aSJernej Skrabec int id, ret; 14360305189aSJernej Skrabec 14370305189aSJernej Skrabec /* find TCON TOP platform device and TCON id */ 14380305189aSJernej Skrabec 14390305189aSJernej Skrabec port = of_graph_get_port_by_id(tcon->dev->of_node, 0); 14400305189aSJernej Skrabec if (!port) 14410305189aSJernej Skrabec return -EINVAL; 14420305189aSJernej Skrabec 14430305189aSJernej Skrabec id = sun4i_tcon_of_get_id_from_port(port); 14440305189aSJernej Skrabec of_node_put(port); 14450305189aSJernej Skrabec 14460305189aSJernej Skrabec remote = of_graph_get_remote_node(tcon->dev->of_node, 0, -1); 14470305189aSJernej Skrabec if (!remote) 14480305189aSJernej Skrabec return -EINVAL; 14490305189aSJernej Skrabec 14500305189aSJernej Skrabec pdev = of_find_device_by_node(remote); 14510305189aSJernej Skrabec of_node_put(remote); 14520305189aSJernej Skrabec if (!pdev) 14530305189aSJernej Skrabec return -EINVAL; 14540305189aSJernej Skrabec 1455185e0bebSMaxime Ripard if (IS_ENABLED(CONFIG_DRM_SUN8I_TCON_TOP) && 1456185e0bebSMaxime Ripard encoder->encoder_type == DRM_MODE_ENCODER_TMDS) { 14570305189aSJernej Skrabec ret = sun8i_tcon_top_set_hdmi_src(&pdev->dev, id); 145807b5b12dSYu Kuai if (ret) { 145907b5b12dSYu Kuai put_device(&pdev->dev); 14600305189aSJernej Skrabec return ret; 14610305189aSJernej Skrabec } 146207b5b12dSYu Kuai } 14630305189aSJernej Skrabec 1464185e0bebSMaxime Ripard if (IS_ENABLED(CONFIG_DRM_SUN8I_TCON_TOP)) { 1465185e0bebSMaxime Ripard ret = sun8i_tcon_top_de_config(&pdev->dev, tcon->id, id); 146607b5b12dSYu Kuai if (ret) { 146707b5b12dSYu Kuai put_device(&pdev->dev); 1468185e0bebSMaxime Ripard return ret; 1469185e0bebSMaxime Ripard } 147007b5b12dSYu Kuai } 1471185e0bebSMaxime Ripard 1472185e0bebSMaxime Ripard return 0; 14730305189aSJernej Skrabec } 14740305189aSJernej Skrabec 14754bb206bfSJonathan Liu static const struct sun4i_tcon_quirks sun4i_a10_quirks = { 147634d698f6SJernej Skrabec .has_channel_0 = true, 14774bb206bfSJonathan Liu .has_channel_1 = true, 14784396393fSChen-Yu Tsai .dclk_min_div = 4, 14794bb206bfSJonathan Liu .set_mux = sun4i_a10_tcon_set_mux, 14804bb206bfSJonathan Liu }; 14814bb206bfSJonathan Liu 148291ea2f29SChen-Yu Tsai static const struct sun4i_tcon_quirks sun5i_a13_quirks = { 148334d698f6SJernej Skrabec .has_channel_0 = true, 148491ea2f29SChen-Yu Tsai .has_channel_1 = true, 14854396393fSChen-Yu Tsai .dclk_min_div = 4, 1486ad537fb2SChen-Yu Tsai .set_mux = sun5i_a13_tcon_set_mux, 148791ea2f29SChen-Yu Tsai }; 148891ea2f29SChen-Yu Tsai 148993a5ec14SChen-Yu Tsai static const struct sun4i_tcon_quirks sun6i_a31_quirks = { 149034d698f6SJernej Skrabec .has_channel_0 = true, 149193a5ec14SChen-Yu Tsai .has_channel_1 = true, 1492a0c1214eSMaxime Ripard .has_lvds_alt = true, 149327e18de7SChen-Yu Tsai .needs_de_be_mux = true, 14944396393fSChen-Yu Tsai .dclk_min_div = 1, 149567e32645SChen-Yu Tsai .set_mux = sun6i_tcon_set_mux, 149693a5ec14SChen-Yu Tsai }; 149793a5ec14SChen-Yu Tsai 149893a5ec14SChen-Yu Tsai static const struct sun4i_tcon_quirks sun6i_a31s_quirks = { 149934d698f6SJernej Skrabec .has_channel_0 = true, 150093a5ec14SChen-Yu Tsai .has_channel_1 = true, 150127e18de7SChen-Yu Tsai .needs_de_be_mux = true, 15024396393fSChen-Yu Tsai .dclk_min_div = 1, 150393a5ec14SChen-Yu Tsai }; 150493a5ec14SChen-Yu Tsai 1505d718e53aSAndrey Lebedev static const struct sun4i_tcon_quirks sun7i_a20_tcon0_quirks = { 1506d718e53aSAndrey Lebedev .supports_lvds = true, 1507d718e53aSAndrey Lebedev .has_channel_0 = true, 1508d718e53aSAndrey Lebedev .has_channel_1 = true, 1509d718e53aSAndrey Lebedev .dclk_min_div = 4, 1510d718e53aSAndrey Lebedev /* Same display pipeline structure as A10 */ 1511d718e53aSAndrey Lebedev .set_mux = sun4i_a10_tcon_set_mux, 1512d718e53aSAndrey Lebedev .setup_lvds_phy = sun4i_tcon_setup_lvds_phy, 1513d718e53aSAndrey Lebedev }; 1514d718e53aSAndrey Lebedev 1515aaddb6d2SJonathan Liu static const struct sun4i_tcon_quirks sun7i_a20_quirks = { 151634d698f6SJernej Skrabec .has_channel_0 = true, 1517aaddb6d2SJonathan Liu .has_channel_1 = true, 15184396393fSChen-Yu Tsai .dclk_min_div = 4, 1519aaddb6d2SJonathan Liu /* Same display pipeline structure as A10 */ 1520aaddb6d2SJonathan Liu .set_mux = sun4i_a10_tcon_set_mux, 1521aaddb6d2SJonathan Liu }; 1522aaddb6d2SJonathan Liu 152391ea2f29SChen-Yu Tsai static const struct sun4i_tcon_quirks sun8i_a33_quirks = { 152434d698f6SJernej Skrabec .has_channel_0 = true, 1525a0c1214eSMaxime Ripard .has_lvds_alt = true, 15264396393fSChen-Yu Tsai .dclk_min_div = 1, 15275627c9d8SAndrey Lebedev .setup_lvds_phy = sun6i_tcon_setup_lvds_phy, 1528cf650f2cSMaxime Ripard .supports_lvds = true, 152991ea2f29SChen-Yu Tsai }; 153091ea2f29SChen-Yu Tsai 15312f0d7bb1SMaxime Ripard static const struct sun4i_tcon_quirks sun8i_a83t_lcd_quirks = { 1532e742a17cSMaxime Ripard .supports_lvds = true, 153334d698f6SJernej Skrabec .has_channel_0 = true, 15344396393fSChen-Yu Tsai .dclk_min_div = 1, 15355627c9d8SAndrey Lebedev .setup_lvds_phy = sun6i_tcon_setup_lvds_phy, 15362f0d7bb1SMaxime Ripard }; 15372f0d7bb1SMaxime Ripard 153805adc89bSJernej Skrabec static const struct sun4i_tcon_quirks sun8i_a83t_tv_quirks = { 153905adc89bSJernej Skrabec .has_channel_1 = true, 154005adc89bSJernej Skrabec }; 154105adc89bSJernej Skrabec 15420305189aSJernej Skrabec static const struct sun4i_tcon_quirks sun8i_r40_tv_quirks = { 15430305189aSJernej Skrabec .has_channel_1 = true, 1544*50791f5dSJernej Skrabec .polarity_in_ch0 = true, 15450305189aSJernej Skrabec .set_mux = sun8i_r40_tcon_tv_set_mux, 15460305189aSJernej Skrabec }; 15470305189aSJernej Skrabec 15481a0edb3fSIcenowy Zheng static const struct sun4i_tcon_quirks sun8i_v3s_quirks = { 154934d698f6SJernej Skrabec .has_channel_0 = true, 15504396393fSChen-Yu Tsai .dclk_min_div = 1, 15511a0edb3fSIcenowy Zheng }; 15521a0edb3fSIcenowy Zheng 15536664e9dcSChen-Yu Tsai static const struct sun4i_tcon_quirks sun9i_a80_tcon_lcd_quirks = { 15546664e9dcSChen-Yu Tsai .has_channel_0 = true, 15556664e9dcSChen-Yu Tsai .needs_edp_reset = true, 15564396393fSChen-Yu Tsai .dclk_min_div = 1, 15576664e9dcSChen-Yu Tsai }; 15586664e9dcSChen-Yu Tsai 15596664e9dcSChen-Yu Tsai static const struct sun4i_tcon_quirks sun9i_a80_tcon_tv_quirks = { 15606664e9dcSChen-Yu Tsai .has_channel_1 = true, 15616664e9dcSChen-Yu Tsai .needs_edp_reset = true, 15626664e9dcSChen-Yu Tsai }; 15636664e9dcSChen-Yu Tsai 1564ff71c2cfSChen-Yu Tsai /* sun4i_drv uses this list to check if a device node is a TCON */ 1565ff71c2cfSChen-Yu Tsai const struct of_device_id sun4i_tcon_of_table[] = { 15664bb206bfSJonathan Liu { .compatible = "allwinner,sun4i-a10-tcon", .data = &sun4i_a10_quirks }, 156791ea2f29SChen-Yu Tsai { .compatible = "allwinner,sun5i-a13-tcon", .data = &sun5i_a13_quirks }, 156893a5ec14SChen-Yu Tsai { .compatible = "allwinner,sun6i-a31-tcon", .data = &sun6i_a31_quirks }, 156993a5ec14SChen-Yu Tsai { .compatible = "allwinner,sun6i-a31s-tcon", .data = &sun6i_a31s_quirks }, 1570aaddb6d2SJonathan Liu { .compatible = "allwinner,sun7i-a20-tcon", .data = &sun7i_a20_quirks }, 1571d718e53aSAndrey Lebedev { .compatible = "allwinner,sun7i-a20-tcon0", .data = &sun7i_a20_tcon0_quirks }, 1572cd0ecabdSAndrey Lebedev { .compatible = "allwinner,sun7i-a20-tcon1", .data = &sun7i_a20_quirks }, 1573d0ec0a3eSChen-Yu Tsai { .compatible = "allwinner,sun8i-a23-tcon", .data = &sun8i_a33_quirks }, 157491ea2f29SChen-Yu Tsai { .compatible = "allwinner,sun8i-a33-tcon", .data = &sun8i_a33_quirks }, 15752f0d7bb1SMaxime Ripard { .compatible = "allwinner,sun8i-a83t-tcon-lcd", .data = &sun8i_a83t_lcd_quirks }, 157605adc89bSJernej Skrabec { .compatible = "allwinner,sun8i-a83t-tcon-tv", .data = &sun8i_a83t_tv_quirks }, 15770305189aSJernej Skrabec { .compatible = "allwinner,sun8i-r40-tcon-tv", .data = &sun8i_r40_tv_quirks }, 15781a0edb3fSIcenowy Zheng { .compatible = "allwinner,sun8i-v3s-tcon", .data = &sun8i_v3s_quirks }, 15796664e9dcSChen-Yu Tsai { .compatible = "allwinner,sun9i-a80-tcon-lcd", .data = &sun9i_a80_tcon_lcd_quirks }, 15806664e9dcSChen-Yu Tsai { .compatible = "allwinner,sun9i-a80-tcon-tv", .data = &sun9i_a80_tcon_tv_quirks }, 15819026e0d1SMaxime Ripard { } 15829026e0d1SMaxime Ripard }; 15839026e0d1SMaxime Ripard MODULE_DEVICE_TABLE(of, sun4i_tcon_of_table); 1584ff71c2cfSChen-Yu Tsai EXPORT_SYMBOL(sun4i_tcon_of_table); 15859026e0d1SMaxime Ripard 15869026e0d1SMaxime Ripard static struct platform_driver sun4i_tcon_platform_driver = { 15879026e0d1SMaxime Ripard .probe = sun4i_tcon_probe, 15889026e0d1SMaxime Ripard .remove = sun4i_tcon_remove, 15899026e0d1SMaxime Ripard .driver = { 15909026e0d1SMaxime Ripard .name = "sun4i-tcon", 15919026e0d1SMaxime Ripard .of_match_table = sun4i_tcon_of_table, 15929026e0d1SMaxime Ripard }, 15939026e0d1SMaxime Ripard }; 15949026e0d1SMaxime Ripard module_platform_driver(sun4i_tcon_platform_driver); 15959026e0d1SMaxime Ripard 15969026e0d1SMaxime Ripard MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>"); 15979026e0d1SMaxime Ripard MODULE_DESCRIPTION("Allwinner A10 Timing Controller Driver"); 15989026e0d1SMaxime Ripard MODULE_LICENSE("GPL"); 1599