1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 27caff0fcSAndrey Gusakov /* 3bbfd3190SMarek Vasut * TC358767/TC358867/TC9595 DSI/DPI-to-DPI/(e)DP bridge driver 4bbfd3190SMarek Vasut * 5bbfd3190SMarek Vasut * The TC358767/TC358867/TC9595 can operate in multiple modes. 6c1de02bbSMarek Vasut * All modes are supported -- DPI->(e)DP / DSI->DPI / DSI->(e)DP . 77caff0fcSAndrey Gusakov * 87caff0fcSAndrey Gusakov * Copyright (C) 2016 CogentEmbedded Inc 97caff0fcSAndrey Gusakov * Author: Andrey Gusakov <andrey.gusakov@cogentembedded.com> 107caff0fcSAndrey Gusakov * 117caff0fcSAndrey Gusakov * Copyright (C) 2016 Pengutronix, Philipp Zabel <p.zabel@pengutronix.de> 127caff0fcSAndrey Gusakov * 132f51be09SAndrey Gusakov * Copyright (C) 2016 Zodiac Inflight Innovations 142f51be09SAndrey Gusakov * 157caff0fcSAndrey Gusakov * Initially based on: drivers/gpu/drm/i2c/tda998x_drv.c 167caff0fcSAndrey Gusakov * 177caff0fcSAndrey Gusakov * Copyright (C) 2012 Texas Instruments 187caff0fcSAndrey Gusakov * Author: Rob Clark <robdclark@gmail.com> 197caff0fcSAndrey Gusakov */ 207caff0fcSAndrey Gusakov 213f072c30SAndrey Smirnov #include <linux/bitfield.h> 227caff0fcSAndrey Gusakov #include <linux/clk.h> 237caff0fcSAndrey Gusakov #include <linux/device.h> 247caff0fcSAndrey Gusakov #include <linux/gpio/consumer.h> 257caff0fcSAndrey Gusakov #include <linux/i2c.h> 267caff0fcSAndrey Gusakov #include <linux/kernel.h> 2772bd9ea3SVille Syrjälä #include <linux/media-bus-format.h> 287caff0fcSAndrey Gusakov #include <linux/module.h> 297caff0fcSAndrey Gusakov #include <linux/regmap.h> 307caff0fcSAndrey Gusakov #include <linux/slab.h> 317caff0fcSAndrey Gusakov 32da68386dSThomas Zimmermann #include <drm/display/drm_dp_helper.h> 337caff0fcSAndrey Gusakov #include <drm/drm_atomic_helper.h> 34ee68c743SBoris Brezillon #include <drm/drm_bridge.h> 357caff0fcSAndrey Gusakov #include <drm/drm_edid.h> 36bbfd3190SMarek Vasut #include <drm/drm_mipi_dsi.h> 377caff0fcSAndrey Gusakov #include <drm/drm_of.h> 387caff0fcSAndrey Gusakov #include <drm/drm_panel.h> 39a25b988fSLaurent Pinchart #include <drm/drm_print.h> 40fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h> 417caff0fcSAndrey Gusakov 427caff0fcSAndrey Gusakov /* Registers */ 437caff0fcSAndrey Gusakov 44bbfd3190SMarek Vasut /* PPI layer registers */ 45bbfd3190SMarek Vasut #define PPI_STARTPPI 0x0104 /* START control bit */ 46bbfd3190SMarek Vasut #define PPI_LPTXTIMECNT 0x0114 /* LPTX timing signal */ 47bbfd3190SMarek Vasut #define LPX_PERIOD 3 48bbfd3190SMarek Vasut #define PPI_LANEENABLE 0x0134 49bbfd3190SMarek Vasut #define PPI_TX_RX_TA 0x013c 50bbfd3190SMarek Vasut #define TTA_GET 0x40000 51bbfd3190SMarek Vasut #define TTA_SURE 6 52bbfd3190SMarek Vasut #define PPI_D0S_ATMR 0x0144 53bbfd3190SMarek Vasut #define PPI_D1S_ATMR 0x0148 54bbfd3190SMarek Vasut #define PPI_D0S_CLRSIPOCOUNT 0x0164 /* Assertion timer for Lane 0 */ 55bbfd3190SMarek Vasut #define PPI_D1S_CLRSIPOCOUNT 0x0168 /* Assertion timer for Lane 1 */ 56bbfd3190SMarek Vasut #define PPI_D2S_CLRSIPOCOUNT 0x016c /* Assertion timer for Lane 2 */ 57bbfd3190SMarek Vasut #define PPI_D3S_CLRSIPOCOUNT 0x0170 /* Assertion timer for Lane 3 */ 58bbfd3190SMarek Vasut #define PPI_START_FUNCTION BIT(0) 59bbfd3190SMarek Vasut 60bbfd3190SMarek Vasut /* DSI layer registers */ 61bbfd3190SMarek Vasut #define DSI_STARTDSI 0x0204 /* START control bit of DSI-TX */ 62bbfd3190SMarek Vasut #define DSI_LANEENABLE 0x0210 /* Enables each lane */ 63bbfd3190SMarek Vasut #define DSI_RX_START BIT(0) 64bbfd3190SMarek Vasut 65bbfd3190SMarek Vasut /* Lane enable PPI and DSI register bits */ 66bbfd3190SMarek Vasut #define LANEENABLE_CLEN BIT(0) 67bbfd3190SMarek Vasut #define LANEENABLE_L0EN BIT(1) 68bbfd3190SMarek Vasut #define LANEENABLE_L1EN BIT(2) 69bbfd3190SMarek Vasut #define LANEENABLE_L2EN BIT(1) 70bbfd3190SMarek Vasut #define LANEENABLE_L3EN BIT(2) 71bbfd3190SMarek Vasut 72bbfd3190SMarek Vasut /* Display Parallel Input Interface */ 737caff0fcSAndrey Gusakov #define DPIPXLFMT 0x0440 747caff0fcSAndrey Gusakov #define VS_POL_ACTIVE_LOW (1 << 10) 757caff0fcSAndrey Gusakov #define HS_POL_ACTIVE_LOW (1 << 9) 767caff0fcSAndrey Gusakov #define DE_POL_ACTIVE_HIGH (0 << 8) 777caff0fcSAndrey Gusakov #define SUB_CFG_TYPE_CONFIG1 (0 << 2) /* LSB aligned */ 787caff0fcSAndrey Gusakov #define SUB_CFG_TYPE_CONFIG2 (1 << 2) /* Loosely Packed */ 797caff0fcSAndrey Gusakov #define SUB_CFG_TYPE_CONFIG3 (2 << 2) /* LSB aligned 8-bit */ 807caff0fcSAndrey Gusakov #define DPI_BPP_RGB888 (0 << 0) 817caff0fcSAndrey Gusakov #define DPI_BPP_RGB666 (1 << 0) 827caff0fcSAndrey Gusakov #define DPI_BPP_RGB565 (2 << 0) 837caff0fcSAndrey Gusakov 84bbfd3190SMarek Vasut /* Display Parallel Output Interface */ 85bbfd3190SMarek Vasut #define POCTRL 0x0448 86bbfd3190SMarek Vasut #define POCTRL_S2P BIT(7) 87bbfd3190SMarek Vasut #define POCTRL_PCLK_POL BIT(3) 88bbfd3190SMarek Vasut #define POCTRL_VS_POL BIT(2) 89bbfd3190SMarek Vasut #define POCTRL_HS_POL BIT(1) 90bbfd3190SMarek Vasut #define POCTRL_DE_POL BIT(0) 91bbfd3190SMarek Vasut 927caff0fcSAndrey Gusakov /* Video Path */ 937caff0fcSAndrey Gusakov #define VPCTRL0 0x0450 943f072c30SAndrey Smirnov #define VSDELAY GENMASK(31, 20) 957caff0fcSAndrey Gusakov #define OPXLFMT_RGB666 (0 << 8) 967caff0fcSAndrey Gusakov #define OPXLFMT_RGB888 (1 << 8) 977caff0fcSAndrey Gusakov #define FRMSYNC_DISABLED (0 << 4) /* Video Timing Gen Disabled */ 987caff0fcSAndrey Gusakov #define FRMSYNC_ENABLED (1 << 4) /* Video Timing Gen Enabled */ 997caff0fcSAndrey Gusakov #define MSF_DISABLED (0 << 0) /* Magic Square FRC disabled */ 1007caff0fcSAndrey Gusakov #define MSF_ENABLED (1 << 0) /* Magic Square FRC enabled */ 1017caff0fcSAndrey Gusakov #define HTIM01 0x0454 1023f072c30SAndrey Smirnov #define HPW GENMASK(8, 0) 1033f072c30SAndrey Smirnov #define HBPR GENMASK(24, 16) 1047caff0fcSAndrey Gusakov #define HTIM02 0x0458 1053f072c30SAndrey Smirnov #define HDISPR GENMASK(10, 0) 1063f072c30SAndrey Smirnov #define HFPR GENMASK(24, 16) 1077caff0fcSAndrey Gusakov #define VTIM01 0x045c 1083f072c30SAndrey Smirnov #define VSPR GENMASK(7, 0) 1093f072c30SAndrey Smirnov #define VBPR GENMASK(23, 16) 1107caff0fcSAndrey Gusakov #define VTIM02 0x0460 1113f072c30SAndrey Smirnov #define VFPR GENMASK(23, 16) 1123f072c30SAndrey Smirnov #define VDISPR GENMASK(10, 0) 1137caff0fcSAndrey Gusakov #define VFUEN0 0x0464 1147caff0fcSAndrey Gusakov #define VFUEN BIT(0) /* Video Frame Timing Upload */ 1157caff0fcSAndrey Gusakov 1167caff0fcSAndrey Gusakov /* System */ 1177caff0fcSAndrey Gusakov #define TC_IDREG 0x0500 118f25ee501STomi Valkeinen #define SYSSTAT 0x0508 1197caff0fcSAndrey Gusakov #define SYSCTRL 0x0510 1207caff0fcSAndrey Gusakov #define DP0_AUDSRC_NO_INPUT (0 << 3) 1217caff0fcSAndrey Gusakov #define DP0_AUDSRC_I2S_RX (1 << 3) 1227caff0fcSAndrey Gusakov #define DP0_VIDSRC_NO_INPUT (0 << 0) 1237caff0fcSAndrey Gusakov #define DP0_VIDSRC_DSI_RX (1 << 0) 1247caff0fcSAndrey Gusakov #define DP0_VIDSRC_DPI_RX (2 << 0) 1257caff0fcSAndrey Gusakov #define DP0_VIDSRC_COLOR_BAR (3 << 0) 12652c2197aSLucas Stach #define SYSRSTENB 0x050c 12752c2197aSLucas Stach #define ENBI2C (1 << 0) 12852c2197aSLucas Stach #define ENBLCD0 (1 << 2) 12952c2197aSLucas Stach #define ENBBM (1 << 3) 13052c2197aSLucas Stach #define ENBDSIRX (1 << 4) 13152c2197aSLucas Stach #define ENBREG (1 << 5) 13252c2197aSLucas Stach #define ENBHDCP (1 << 8) 133af9526f2STomi Valkeinen #define GPIOM 0x0540 134f25ee501STomi Valkeinen #define GPIOC 0x0544 135f25ee501STomi Valkeinen #define GPIOO 0x0548 136af9526f2STomi Valkeinen #define GPIOI 0x054c 137af9526f2STomi Valkeinen #define INTCTL_G 0x0560 138af9526f2STomi Valkeinen #define INTSTS_G 0x0564 139f25ee501STomi Valkeinen 140f25ee501STomi Valkeinen #define INT_SYSERR BIT(16) 141f25ee501STomi Valkeinen #define INT_GPIO_H(x) (1 << (x == 0 ? 2 : 10)) 142f25ee501STomi Valkeinen #define INT_GPIO_LC(x) (1 << (x == 0 ? 3 : 11)) 143f25ee501STomi Valkeinen 144af9526f2STomi Valkeinen #define INT_GP0_LCNT 0x0584 145af9526f2STomi Valkeinen #define INT_GP1_LCNT 0x0588 1467caff0fcSAndrey Gusakov 1477caff0fcSAndrey Gusakov /* Control */ 1487caff0fcSAndrey Gusakov #define DP0CTL 0x0600 1497caff0fcSAndrey Gusakov #define VID_MN_GEN BIT(6) /* Auto-generate M/N values */ 1507caff0fcSAndrey Gusakov #define EF_EN BIT(5) /* Enable Enhanced Framing */ 1517caff0fcSAndrey Gusakov #define VID_EN BIT(1) /* Video transmission enable */ 1527caff0fcSAndrey Gusakov #define DP_EN BIT(0) /* Enable DPTX function */ 1537caff0fcSAndrey Gusakov 1547caff0fcSAndrey Gusakov /* Clocks */ 1557caff0fcSAndrey Gusakov #define DP0_VIDMNGEN0 0x0610 1567caff0fcSAndrey Gusakov #define DP0_VIDMNGEN1 0x0614 1577caff0fcSAndrey Gusakov #define DP0_VMNGENSTATUS 0x0618 1587caff0fcSAndrey Gusakov 1597caff0fcSAndrey Gusakov /* Main Channel */ 1607caff0fcSAndrey Gusakov #define DP0_SECSAMPLE 0x0640 1617caff0fcSAndrey Gusakov #define DP0_VIDSYNCDELAY 0x0644 1623f072c30SAndrey Smirnov #define VID_SYNC_DLY GENMASK(15, 0) 1633f072c30SAndrey Smirnov #define THRESH_DLY GENMASK(31, 16) 1643f072c30SAndrey Smirnov 1657caff0fcSAndrey Gusakov #define DP0_TOTALVAL 0x0648 1663f072c30SAndrey Smirnov #define H_TOTAL GENMASK(15, 0) 1673f072c30SAndrey Smirnov #define V_TOTAL GENMASK(31, 16) 1687caff0fcSAndrey Gusakov #define DP0_STARTVAL 0x064c 1693f072c30SAndrey Smirnov #define H_START GENMASK(15, 0) 1703f072c30SAndrey Smirnov #define V_START GENMASK(31, 16) 1717caff0fcSAndrey Gusakov #define DP0_ACTIVEVAL 0x0650 1723f072c30SAndrey Smirnov #define H_ACT GENMASK(15, 0) 1733f072c30SAndrey Smirnov #define V_ACT GENMASK(31, 16) 1743f072c30SAndrey Smirnov 1757caff0fcSAndrey Gusakov #define DP0_SYNCVAL 0x0654 1763f072c30SAndrey Smirnov #define VS_WIDTH GENMASK(30, 16) 1773f072c30SAndrey Smirnov #define HS_WIDTH GENMASK(14, 0) 1787923e09cSTomi Valkeinen #define SYNCVAL_HS_POL_ACTIVE_LOW (1 << 15) 1797923e09cSTomi Valkeinen #define SYNCVAL_VS_POL_ACTIVE_LOW (1 << 31) 1807caff0fcSAndrey Gusakov #define DP0_MISC 0x0658 181f3b8adbeSAndrey Gusakov #define TU_SIZE_RECOMMENDED (63) /* LSCLK cycles per TU */ 1823f072c30SAndrey Smirnov #define MAX_TU_SYMBOL GENMASK(28, 23) 1833f072c30SAndrey Smirnov #define TU_SIZE GENMASK(21, 16) 1847caff0fcSAndrey Gusakov #define BPC_6 (0 << 5) 1857caff0fcSAndrey Gusakov #define BPC_8 (1 << 5) 1867caff0fcSAndrey Gusakov 1877caff0fcSAndrey Gusakov /* AUX channel */ 1887caff0fcSAndrey Gusakov #define DP0_AUXCFG0 0x0660 189fdb29b73SAndrey Smirnov #define DP0_AUXCFG0_BSIZE GENMASK(11, 8) 190fdb29b73SAndrey Smirnov #define DP0_AUXCFG0_ADDR_ONLY BIT(4) 1917caff0fcSAndrey Gusakov #define DP0_AUXCFG1 0x0664 1927caff0fcSAndrey Gusakov #define AUX_RX_FILTER_EN BIT(16) 1937caff0fcSAndrey Gusakov 1947caff0fcSAndrey Gusakov #define DP0_AUXADDR 0x0668 1957caff0fcSAndrey Gusakov #define DP0_AUXWDATA(i) (0x066c + (i) * 4) 1967caff0fcSAndrey Gusakov #define DP0_AUXRDATA(i) (0x067c + (i) * 4) 1977caff0fcSAndrey Gusakov #define DP0_AUXSTATUS 0x068c 19812dfe7c4SAndrey Smirnov #define AUX_BYTES GENMASK(15, 8) 19912dfe7c4SAndrey Smirnov #define AUX_STATUS GENMASK(7, 4) 2007caff0fcSAndrey Gusakov #define AUX_TIMEOUT BIT(1) 2017caff0fcSAndrey Gusakov #define AUX_BUSY BIT(0) 2027caff0fcSAndrey Gusakov #define DP0_AUXI2CADR 0x0698 2037caff0fcSAndrey Gusakov 2047caff0fcSAndrey Gusakov /* Link Training */ 2057caff0fcSAndrey Gusakov #define DP0_SRCCTRL 0x06a0 2067caff0fcSAndrey Gusakov #define DP0_SRCCTRL_SCRMBLDIS BIT(13) 2077caff0fcSAndrey Gusakov #define DP0_SRCCTRL_EN810B BIT(12) 2087caff0fcSAndrey Gusakov #define DP0_SRCCTRL_NOTP (0 << 8) 2097caff0fcSAndrey Gusakov #define DP0_SRCCTRL_TP1 (1 << 8) 2107caff0fcSAndrey Gusakov #define DP0_SRCCTRL_TP2 (2 << 8) 2117caff0fcSAndrey Gusakov #define DP0_SRCCTRL_LANESKEW BIT(7) 2127caff0fcSAndrey Gusakov #define DP0_SRCCTRL_SSCG BIT(3) 2137caff0fcSAndrey Gusakov #define DP0_SRCCTRL_LANES_1 (0 << 2) 2147caff0fcSAndrey Gusakov #define DP0_SRCCTRL_LANES_2 (1 << 2) 2157caff0fcSAndrey Gusakov #define DP0_SRCCTRL_BW27 (1 << 1) 2167caff0fcSAndrey Gusakov #define DP0_SRCCTRL_BW162 (0 << 1) 2177caff0fcSAndrey Gusakov #define DP0_SRCCTRL_AUTOCORRECT BIT(0) 2187caff0fcSAndrey Gusakov #define DP0_LTSTAT 0x06d0 2197caff0fcSAndrey Gusakov #define LT_LOOPDONE BIT(13) 2207caff0fcSAndrey Gusakov #define LT_STATUS_MASK (0x1f << 8) 2217caff0fcSAndrey Gusakov #define LT_CHANNEL1_EQ_BITS (DP_CHANNEL_EQ_BITS << 4) 2227caff0fcSAndrey Gusakov #define LT_INTERLANE_ALIGN_DONE BIT(3) 2237caff0fcSAndrey Gusakov #define LT_CHANNEL0_EQ_BITS (DP_CHANNEL_EQ_BITS) 2247caff0fcSAndrey Gusakov #define DP0_SNKLTCHGREQ 0x06d4 2257caff0fcSAndrey Gusakov #define DP0_LTLOOPCTRL 0x06d8 2267caff0fcSAndrey Gusakov #define DP0_SNKLTCTRL 0x06e4 2277caff0fcSAndrey Gusakov 228adf41098STomi Valkeinen #define DP1_SRCCTRL 0x07a0 229adf41098STomi Valkeinen 2307caff0fcSAndrey Gusakov /* PHY */ 2317caff0fcSAndrey Gusakov #define DP_PHY_CTRL 0x0800 2327caff0fcSAndrey Gusakov #define DP_PHY_RST BIT(28) /* DP PHY Global Soft Reset */ 2337caff0fcSAndrey Gusakov #define BGREN BIT(25) /* AUX PHY BGR Enable */ 2347caff0fcSAndrey Gusakov #define PWR_SW_EN BIT(24) /* PHY Power Switch Enable */ 2357caff0fcSAndrey Gusakov #define PHY_M1_RST BIT(12) /* Reset PHY1 Main Channel */ 2367caff0fcSAndrey Gusakov #define PHY_RDY BIT(16) /* PHY Main Channels Ready */ 2377caff0fcSAndrey Gusakov #define PHY_M0_RST BIT(8) /* Reset PHY0 Main Channel */ 238adf41098STomi Valkeinen #define PHY_2LANE BIT(2) /* PHY Enable 2 lanes */ 2397caff0fcSAndrey Gusakov #define PHY_A0_EN BIT(1) /* PHY Aux Channel0 Enable */ 2407caff0fcSAndrey Gusakov #define PHY_M0_EN BIT(0) /* PHY Main Channel0 Enable */ 2417caff0fcSAndrey Gusakov 2427caff0fcSAndrey Gusakov /* PLL */ 2437caff0fcSAndrey Gusakov #define DP0_PLLCTRL 0x0900 2447caff0fcSAndrey Gusakov #define DP1_PLLCTRL 0x0904 /* not defined in DS */ 2457caff0fcSAndrey Gusakov #define PXL_PLLCTRL 0x0908 2467caff0fcSAndrey Gusakov #define PLLUPDATE BIT(2) 2477caff0fcSAndrey Gusakov #define PLLBYP BIT(1) 2487caff0fcSAndrey Gusakov #define PLLEN BIT(0) 2497caff0fcSAndrey Gusakov #define PXL_PLLPARAM 0x0914 2507caff0fcSAndrey Gusakov #define IN_SEL_REFCLK (0 << 14) 2517caff0fcSAndrey Gusakov #define SYS_PLLPARAM 0x0918 2527caff0fcSAndrey Gusakov #define REF_FREQ_38M4 (0 << 8) /* 38.4 MHz */ 2537caff0fcSAndrey Gusakov #define REF_FREQ_19M2 (1 << 8) /* 19.2 MHz */ 2547caff0fcSAndrey Gusakov #define REF_FREQ_26M (2 << 8) /* 26 MHz */ 2557caff0fcSAndrey Gusakov #define REF_FREQ_13M (3 << 8) /* 13 MHz */ 2567caff0fcSAndrey Gusakov #define SYSCLK_SEL_LSCLK (0 << 4) 2577caff0fcSAndrey Gusakov #define LSCLK_DIV_1 (0 << 0) 2587caff0fcSAndrey Gusakov #define LSCLK_DIV_2 (1 << 0) 2597caff0fcSAndrey Gusakov 2607caff0fcSAndrey Gusakov /* Test & Debug */ 2617caff0fcSAndrey Gusakov #define TSTCTL 0x0a00 2623f072c30SAndrey Smirnov #define COLOR_R GENMASK(31, 24) 2633f072c30SAndrey Smirnov #define COLOR_G GENMASK(23, 16) 2643f072c30SAndrey Smirnov #define COLOR_B GENMASK(15, 8) 2653f072c30SAndrey Smirnov #define ENI2CFILTER BIT(4) 2663f072c30SAndrey Smirnov #define COLOR_BAR_MODE GENMASK(1, 0) 2673f072c30SAndrey Smirnov #define COLOR_BAR_MODE_BARS 2 2687caff0fcSAndrey Gusakov #define PLL_DBG 0x0a04 2697caff0fcSAndrey Gusakov 2707caff0fcSAndrey Gusakov static bool tc_test_pattern; 2717caff0fcSAndrey Gusakov module_param_named(test, tc_test_pattern, bool, 0644); 2727caff0fcSAndrey Gusakov 2737caff0fcSAndrey Gusakov struct tc_edp_link { 274e7dc8d40SThierry Reding u8 dpcd[DP_RECEIVER_CAP_SIZE]; 275e7dc8d40SThierry Reding unsigned int rate; 276e7dc8d40SThierry Reding u8 num_lanes; 2777caff0fcSAndrey Gusakov u8 assr; 278e5607637STomi Valkeinen bool scrambler_dis; 279e5607637STomi Valkeinen bool spread; 2807caff0fcSAndrey Gusakov }; 2817caff0fcSAndrey Gusakov 2827caff0fcSAndrey Gusakov struct tc_data { 2837caff0fcSAndrey Gusakov struct device *dev; 2847caff0fcSAndrey Gusakov struct regmap *regmap; 2857caff0fcSAndrey Gusakov struct drm_dp_aux aux; 2867caff0fcSAndrey Gusakov 2877caff0fcSAndrey Gusakov struct drm_bridge bridge; 288de5e6c02SSam Ravnborg struct drm_bridge *panel_bridge; 2897caff0fcSAndrey Gusakov struct drm_connector connector; 2907caff0fcSAndrey Gusakov 291bbfd3190SMarek Vasut struct mipi_dsi_device *dsi; 292bbfd3190SMarek Vasut 2937caff0fcSAndrey Gusakov /* link settings */ 2947caff0fcSAndrey Gusakov struct tc_edp_link link; 2957caff0fcSAndrey Gusakov 2967caff0fcSAndrey Gusakov /* current mode */ 29746648a3cSTomi Valkeinen struct drm_display_mode mode; 2987caff0fcSAndrey Gusakov 2997caff0fcSAndrey Gusakov u32 rev; 3007caff0fcSAndrey Gusakov u8 assr; 3017caff0fcSAndrey Gusakov 3027caff0fcSAndrey Gusakov struct gpio_desc *sd_gpio; 3037caff0fcSAndrey Gusakov struct gpio_desc *reset_gpio; 3047caff0fcSAndrey Gusakov struct clk *refclk; 305f25ee501STomi Valkeinen 306f25ee501STomi Valkeinen /* do we have IRQ */ 307f25ee501STomi Valkeinen bool have_irq; 308f25ee501STomi Valkeinen 3093080c21aSMarek Vasut /* Input connector type, DSI and not DPI. */ 3103080c21aSMarek Vasut bool input_connector_dsi; 3113080c21aSMarek Vasut 312f25ee501STomi Valkeinen /* HPD pin number (0 or 1) or -ENODEV */ 313f25ee501STomi Valkeinen int hpd_pin; 3147caff0fcSAndrey Gusakov }; 3157caff0fcSAndrey Gusakov 3167caff0fcSAndrey Gusakov static inline struct tc_data *aux_to_tc(struct drm_dp_aux *a) 3177caff0fcSAndrey Gusakov { 3187caff0fcSAndrey Gusakov return container_of(a, struct tc_data, aux); 3197caff0fcSAndrey Gusakov } 3207caff0fcSAndrey Gusakov 3217caff0fcSAndrey Gusakov static inline struct tc_data *bridge_to_tc(struct drm_bridge *b) 3227caff0fcSAndrey Gusakov { 3237caff0fcSAndrey Gusakov return container_of(b, struct tc_data, bridge); 3247caff0fcSAndrey Gusakov } 3257caff0fcSAndrey Gusakov 3267caff0fcSAndrey Gusakov static inline struct tc_data *connector_to_tc(struct drm_connector *c) 3277caff0fcSAndrey Gusakov { 3287caff0fcSAndrey Gusakov return container_of(c, struct tc_data, connector); 3297caff0fcSAndrey Gusakov } 3307caff0fcSAndrey Gusakov 33193a10569SAndrey Smirnov static inline int tc_poll_timeout(struct tc_data *tc, unsigned int addr, 3327caff0fcSAndrey Gusakov unsigned int cond_mask, 3337caff0fcSAndrey Gusakov unsigned int cond_value, 3347caff0fcSAndrey Gusakov unsigned long sleep_us, u64 timeout_us) 3357caff0fcSAndrey Gusakov { 3367caff0fcSAndrey Gusakov unsigned int val; 3377caff0fcSAndrey Gusakov 33893a10569SAndrey Smirnov return regmap_read_poll_timeout(tc->regmap, addr, val, 33993a10569SAndrey Smirnov (val & cond_mask) == cond_value, 34093a10569SAndrey Smirnov sleep_us, timeout_us); 3417caff0fcSAndrey Gusakov } 3427caff0fcSAndrey Gusakov 34372648926SAndrey Smirnov static int tc_aux_wait_busy(struct tc_data *tc) 3447caff0fcSAndrey Gusakov { 3458a6483acSTomi Valkeinen return tc_poll_timeout(tc, DP0_AUXSTATUS, AUX_BUSY, 0, 100, 100000); 3467caff0fcSAndrey Gusakov } 3477caff0fcSAndrey Gusakov 348792a081aSAndrey Smirnov static int tc_aux_write_data(struct tc_data *tc, const void *data, 349792a081aSAndrey Smirnov size_t size) 350792a081aSAndrey Smirnov { 351792a081aSAndrey Smirnov u32 auxwdata[DP_AUX_MAX_PAYLOAD_BYTES / sizeof(u32)] = { 0 }; 352792a081aSAndrey Smirnov int ret, count = ALIGN(size, sizeof(u32)); 353792a081aSAndrey Smirnov 354792a081aSAndrey Smirnov memcpy(auxwdata, data, size); 355792a081aSAndrey Smirnov 356792a081aSAndrey Smirnov ret = regmap_raw_write(tc->regmap, DP0_AUXWDATA(0), auxwdata, count); 357792a081aSAndrey Smirnov if (ret) 358792a081aSAndrey Smirnov return ret; 359792a081aSAndrey Smirnov 360792a081aSAndrey Smirnov return size; 361792a081aSAndrey Smirnov } 362792a081aSAndrey Smirnov 36353b166dcSAndrey Smirnov static int tc_aux_read_data(struct tc_data *tc, void *data, size_t size) 36453b166dcSAndrey Smirnov { 36553b166dcSAndrey Smirnov u32 auxrdata[DP_AUX_MAX_PAYLOAD_BYTES / sizeof(u32)]; 36653b166dcSAndrey Smirnov int ret, count = ALIGN(size, sizeof(u32)); 36753b166dcSAndrey Smirnov 36853b166dcSAndrey Smirnov ret = regmap_raw_read(tc->regmap, DP0_AUXRDATA(0), auxrdata, count); 36953b166dcSAndrey Smirnov if (ret) 37053b166dcSAndrey Smirnov return ret; 37153b166dcSAndrey Smirnov 37253b166dcSAndrey Smirnov memcpy(data, auxrdata, size); 37353b166dcSAndrey Smirnov 37453b166dcSAndrey Smirnov return size; 37553b166dcSAndrey Smirnov } 37653b166dcSAndrey Smirnov 377fdb29b73SAndrey Smirnov static u32 tc_auxcfg0(struct drm_dp_aux_msg *msg, size_t size) 378fdb29b73SAndrey Smirnov { 379fdb29b73SAndrey Smirnov u32 auxcfg0 = msg->request; 380fdb29b73SAndrey Smirnov 381fdb29b73SAndrey Smirnov if (size) 382fdb29b73SAndrey Smirnov auxcfg0 |= FIELD_PREP(DP0_AUXCFG0_BSIZE, size - 1); 383fdb29b73SAndrey Smirnov else 384fdb29b73SAndrey Smirnov auxcfg0 |= DP0_AUXCFG0_ADDR_ONLY; 385fdb29b73SAndrey Smirnov 386fdb29b73SAndrey Smirnov return auxcfg0; 387fdb29b73SAndrey Smirnov } 388fdb29b73SAndrey Smirnov 3897caff0fcSAndrey Gusakov static ssize_t tc_aux_transfer(struct drm_dp_aux *aux, 3907caff0fcSAndrey Gusakov struct drm_dp_aux_msg *msg) 3917caff0fcSAndrey Gusakov { 3927caff0fcSAndrey Gusakov struct tc_data *tc = aux_to_tc(aux); 393e0655feaSAndrey Smirnov size_t size = min_t(size_t, DP_AUX_MAX_PAYLOAD_BYTES - 1, msg->size); 3947caff0fcSAndrey Gusakov u8 request = msg->request & ~DP_AUX_I2C_MOT; 39512dfe7c4SAndrey Smirnov u32 auxstatus; 3967caff0fcSAndrey Gusakov int ret; 3977caff0fcSAndrey Gusakov 39872648926SAndrey Smirnov ret = tc_aux_wait_busy(tc); 3997caff0fcSAndrey Gusakov if (ret) 4006d0c3831SAndrey Smirnov return ret; 4017caff0fcSAndrey Gusakov 402792a081aSAndrey Smirnov switch (request) { 403792a081aSAndrey Smirnov case DP_AUX_NATIVE_READ: 404792a081aSAndrey Smirnov case DP_AUX_I2C_READ: 405792a081aSAndrey Smirnov break; 406792a081aSAndrey Smirnov case DP_AUX_NATIVE_WRITE: 407792a081aSAndrey Smirnov case DP_AUX_I2C_WRITE: 408fdb29b73SAndrey Smirnov if (size) { 409792a081aSAndrey Smirnov ret = tc_aux_write_data(tc, msg->buffer, size); 410792a081aSAndrey Smirnov if (ret < 0) 4116d0c3831SAndrey Smirnov return ret; 412fdb29b73SAndrey Smirnov } 413792a081aSAndrey Smirnov break; 414792a081aSAndrey Smirnov default: 4157caff0fcSAndrey Gusakov return -EINVAL; 4167caff0fcSAndrey Gusakov } 4177caff0fcSAndrey Gusakov 4187caff0fcSAndrey Gusakov /* Store address */ 4196d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP0_AUXADDR, msg->address); 4206d0c3831SAndrey Smirnov if (ret) 4216d0c3831SAndrey Smirnov return ret; 4227caff0fcSAndrey Gusakov /* Start transfer */ 423fdb29b73SAndrey Smirnov ret = regmap_write(tc->regmap, DP0_AUXCFG0, tc_auxcfg0(msg, size)); 4246d0c3831SAndrey Smirnov if (ret) 4256d0c3831SAndrey Smirnov return ret; 4267caff0fcSAndrey Gusakov 42772648926SAndrey Smirnov ret = tc_aux_wait_busy(tc); 4287caff0fcSAndrey Gusakov if (ret) 4296d0c3831SAndrey Smirnov return ret; 4307caff0fcSAndrey Gusakov 43112dfe7c4SAndrey Smirnov ret = regmap_read(tc->regmap, DP0_AUXSTATUS, &auxstatus); 4327caff0fcSAndrey Gusakov if (ret) 4336d0c3831SAndrey Smirnov return ret; 4347caff0fcSAndrey Gusakov 43512dfe7c4SAndrey Smirnov if (auxstatus & AUX_TIMEOUT) 43612dfe7c4SAndrey Smirnov return -ETIMEDOUT; 437fdb29b73SAndrey Smirnov /* 438fdb29b73SAndrey Smirnov * For some reason address-only DP_AUX_I2C_WRITE (MOT), still 439fdb29b73SAndrey Smirnov * reports 1 byte transferred in its status. To deal we that 440fdb29b73SAndrey Smirnov * we ignore aux_bytes field if we know that this was an 441fdb29b73SAndrey Smirnov * address-only transfer 442fdb29b73SAndrey Smirnov */ 443fdb29b73SAndrey Smirnov if (size) 44412dfe7c4SAndrey Smirnov size = FIELD_GET(AUX_BYTES, auxstatus); 44512dfe7c4SAndrey Smirnov msg->reply = FIELD_GET(AUX_STATUS, auxstatus); 44612dfe7c4SAndrey Smirnov 44753b166dcSAndrey Smirnov switch (request) { 44853b166dcSAndrey Smirnov case DP_AUX_NATIVE_READ: 44953b166dcSAndrey Smirnov case DP_AUX_I2C_READ: 450fdb29b73SAndrey Smirnov if (size) 45153b166dcSAndrey Smirnov return tc_aux_read_data(tc, msg->buffer, size); 452fdb29b73SAndrey Smirnov break; 4537caff0fcSAndrey Gusakov } 4547caff0fcSAndrey Gusakov 4557caff0fcSAndrey Gusakov return size; 4567caff0fcSAndrey Gusakov } 4577caff0fcSAndrey Gusakov 4587caff0fcSAndrey Gusakov static const char * const training_pattern1_errors[] = { 4597caff0fcSAndrey Gusakov "No errors", 4607caff0fcSAndrey Gusakov "Aux write error", 4617caff0fcSAndrey Gusakov "Aux read error", 4627caff0fcSAndrey Gusakov "Max voltage reached error", 4637caff0fcSAndrey Gusakov "Loop counter expired error", 4647caff0fcSAndrey Gusakov "res", "res", "res" 4657caff0fcSAndrey Gusakov }; 4667caff0fcSAndrey Gusakov 4677caff0fcSAndrey Gusakov static const char * const training_pattern2_errors[] = { 4687caff0fcSAndrey Gusakov "No errors", 4697caff0fcSAndrey Gusakov "Aux write error", 4707caff0fcSAndrey Gusakov "Aux read error", 4717caff0fcSAndrey Gusakov "Clock recovery failed error", 4727caff0fcSAndrey Gusakov "Loop counter expired error", 4737caff0fcSAndrey Gusakov "res", "res", "res" 4747caff0fcSAndrey Gusakov }; 4757caff0fcSAndrey Gusakov 4767caff0fcSAndrey Gusakov static u32 tc_srcctrl(struct tc_data *tc) 4777caff0fcSAndrey Gusakov { 4787caff0fcSAndrey Gusakov /* 4797caff0fcSAndrey Gusakov * No training pattern, skew lane 1 data by two LSCLK cycles with 4807caff0fcSAndrey Gusakov * respect to lane 0 data, AutoCorrect Mode = 0 4817caff0fcSAndrey Gusakov */ 4824b30bf41STomi Valkeinen u32 reg = DP0_SRCCTRL_NOTP | DP0_SRCCTRL_LANESKEW | DP0_SRCCTRL_EN810B; 4837caff0fcSAndrey Gusakov 4847caff0fcSAndrey Gusakov if (tc->link.scrambler_dis) 4857caff0fcSAndrey Gusakov reg |= DP0_SRCCTRL_SCRMBLDIS; /* Scrambler Disabled */ 4867caff0fcSAndrey Gusakov if (tc->link.spread) 4877caff0fcSAndrey Gusakov reg |= DP0_SRCCTRL_SSCG; /* Spread Spectrum Enable */ 488e7dc8d40SThierry Reding if (tc->link.num_lanes == 2) 4897caff0fcSAndrey Gusakov reg |= DP0_SRCCTRL_LANES_2; /* Two Main Channel Lanes */ 490e7dc8d40SThierry Reding if (tc->link.rate != 162000) 4917caff0fcSAndrey Gusakov reg |= DP0_SRCCTRL_BW27; /* 2.7 Gbps link */ 4927caff0fcSAndrey Gusakov return reg; 4937caff0fcSAndrey Gusakov } 4947caff0fcSAndrey Gusakov 495134fb306SAndrey Smirnov static int tc_pllupdate(struct tc_data *tc, unsigned int pllctrl) 4967caff0fcSAndrey Gusakov { 497134fb306SAndrey Smirnov int ret; 498134fb306SAndrey Smirnov 499134fb306SAndrey Smirnov ret = regmap_write(tc->regmap, pllctrl, PLLUPDATE | PLLEN); 500134fb306SAndrey Smirnov if (ret) 501134fb306SAndrey Smirnov return ret; 502134fb306SAndrey Smirnov 5037caff0fcSAndrey Gusakov /* Wait for PLL to lock: up to 2.09 ms, depending on refclk */ 5047caff0fcSAndrey Gusakov usleep_range(3000, 6000); 505134fb306SAndrey Smirnov 506134fb306SAndrey Smirnov return 0; 5077caff0fcSAndrey Gusakov } 5087caff0fcSAndrey Gusakov 5097caff0fcSAndrey Gusakov static int tc_pxl_pll_en(struct tc_data *tc, u32 refclk, u32 pixelclock) 5107caff0fcSAndrey Gusakov { 5117caff0fcSAndrey Gusakov int ret; 5127caff0fcSAndrey Gusakov int i_pre, best_pre = 1; 5137caff0fcSAndrey Gusakov int i_post, best_post = 1; 5147caff0fcSAndrey Gusakov int div, best_div = 1; 5157caff0fcSAndrey Gusakov int mul, best_mul = 1; 5167caff0fcSAndrey Gusakov int delta, best_delta; 5177caff0fcSAndrey Gusakov int ext_div[] = {1, 2, 3, 5, 7}; 518bbfd3190SMarek Vasut int clk_min, clk_max; 5197caff0fcSAndrey Gusakov int best_pixelclock = 0; 5207caff0fcSAndrey Gusakov int vco_hi = 0; 5216d0c3831SAndrey Smirnov u32 pxl_pllparam; 5227caff0fcSAndrey Gusakov 523bbfd3190SMarek Vasut /* 524bbfd3190SMarek Vasut * refclk * mul / (ext_pre_div * pre_div) should be in range: 525bbfd3190SMarek Vasut * - DPI ..... 0 to 100 MHz 526bbfd3190SMarek Vasut * - (e)DP ... 150 to 650 MHz 527bbfd3190SMarek Vasut */ 528bbfd3190SMarek Vasut if (tc->bridge.type == DRM_MODE_CONNECTOR_DPI) { 529bbfd3190SMarek Vasut clk_min = 0; 530bbfd3190SMarek Vasut clk_max = 100000000; 531bbfd3190SMarek Vasut } else { 532bbfd3190SMarek Vasut clk_min = 150000000; 533bbfd3190SMarek Vasut clk_max = 650000000; 534bbfd3190SMarek Vasut } 535bbfd3190SMarek Vasut 5367caff0fcSAndrey Gusakov dev_dbg(tc->dev, "PLL: requested %d pixelclock, ref %d\n", pixelclock, 5377caff0fcSAndrey Gusakov refclk); 5387caff0fcSAndrey Gusakov best_delta = pixelclock; 5397caff0fcSAndrey Gusakov /* Loop over all possible ext_divs, skipping invalid configurations */ 5407caff0fcSAndrey Gusakov for (i_pre = 0; i_pre < ARRAY_SIZE(ext_div); i_pre++) { 5417caff0fcSAndrey Gusakov /* 5427caff0fcSAndrey Gusakov * refclk / ext_pre_div should be in the 1 to 200 MHz range. 5437caff0fcSAndrey Gusakov * We don't allow any refclk > 200 MHz, only check lower bounds. 5447caff0fcSAndrey Gusakov */ 5457caff0fcSAndrey Gusakov if (refclk / ext_div[i_pre] < 1000000) 5467caff0fcSAndrey Gusakov continue; 5477caff0fcSAndrey Gusakov for (i_post = 0; i_post < ARRAY_SIZE(ext_div); i_post++) { 5487caff0fcSAndrey Gusakov for (div = 1; div <= 16; div++) { 5497caff0fcSAndrey Gusakov u32 clk; 5507caff0fcSAndrey Gusakov u64 tmp; 5517caff0fcSAndrey Gusakov 5527caff0fcSAndrey Gusakov tmp = pixelclock * ext_div[i_pre] * 5537caff0fcSAndrey Gusakov ext_div[i_post] * div; 5547caff0fcSAndrey Gusakov do_div(tmp, refclk); 5557caff0fcSAndrey Gusakov mul = tmp; 5567caff0fcSAndrey Gusakov 5577caff0fcSAndrey Gusakov /* Check limits */ 5587caff0fcSAndrey Gusakov if ((mul < 1) || (mul > 128)) 5597caff0fcSAndrey Gusakov continue; 5607caff0fcSAndrey Gusakov 5617caff0fcSAndrey Gusakov clk = (refclk / ext_div[i_pre] / div) * mul; 562bbfd3190SMarek Vasut if ((clk > clk_max) || (clk < clk_min)) 5637caff0fcSAndrey Gusakov continue; 5647caff0fcSAndrey Gusakov 5657caff0fcSAndrey Gusakov clk = clk / ext_div[i_post]; 5667caff0fcSAndrey Gusakov delta = clk - pixelclock; 5677caff0fcSAndrey Gusakov 5687caff0fcSAndrey Gusakov if (abs(delta) < abs(best_delta)) { 5697caff0fcSAndrey Gusakov best_pre = i_pre; 5707caff0fcSAndrey Gusakov best_post = i_post; 5717caff0fcSAndrey Gusakov best_div = div; 5727caff0fcSAndrey Gusakov best_mul = mul; 5737caff0fcSAndrey Gusakov best_delta = delta; 5747caff0fcSAndrey Gusakov best_pixelclock = clk; 5757caff0fcSAndrey Gusakov } 5767caff0fcSAndrey Gusakov } 5777caff0fcSAndrey Gusakov } 5787caff0fcSAndrey Gusakov } 5797caff0fcSAndrey Gusakov if (best_pixelclock == 0) { 5807caff0fcSAndrey Gusakov dev_err(tc->dev, "Failed to calc clock for %d pixelclock\n", 5817caff0fcSAndrey Gusakov pixelclock); 5827caff0fcSAndrey Gusakov return -EINVAL; 5837caff0fcSAndrey Gusakov } 5847caff0fcSAndrey Gusakov 5857caff0fcSAndrey Gusakov dev_dbg(tc->dev, "PLL: got %d, delta %d\n", best_pixelclock, 5867caff0fcSAndrey Gusakov best_delta); 5877caff0fcSAndrey Gusakov dev_dbg(tc->dev, "PLL: %d / %d / %d * %d / %d\n", refclk, 5887caff0fcSAndrey Gusakov ext_div[best_pre], best_div, best_mul, ext_div[best_post]); 5897caff0fcSAndrey Gusakov 5907caff0fcSAndrey Gusakov /* if VCO >= 300 MHz */ 5917caff0fcSAndrey Gusakov if (refclk / ext_div[best_pre] / best_div * best_mul >= 300000000) 5927caff0fcSAndrey Gusakov vco_hi = 1; 5937caff0fcSAndrey Gusakov /* see DS */ 5947caff0fcSAndrey Gusakov if (best_div == 16) 5957caff0fcSAndrey Gusakov best_div = 0; 5967caff0fcSAndrey Gusakov if (best_mul == 128) 5977caff0fcSAndrey Gusakov best_mul = 0; 5987caff0fcSAndrey Gusakov 5997caff0fcSAndrey Gusakov /* Power up PLL and switch to bypass */ 6006d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, PXL_PLLCTRL, PLLBYP | PLLEN); 6016d0c3831SAndrey Smirnov if (ret) 6026d0c3831SAndrey Smirnov return ret; 6037caff0fcSAndrey Gusakov 6046d0c3831SAndrey Smirnov pxl_pllparam = vco_hi << 24; /* For PLL VCO >= 300 MHz = 1 */ 6056d0c3831SAndrey Smirnov pxl_pllparam |= ext_div[best_pre] << 20; /* External Pre-divider */ 6066d0c3831SAndrey Smirnov pxl_pllparam |= ext_div[best_post] << 16; /* External Post-divider */ 6076d0c3831SAndrey Smirnov pxl_pllparam |= IN_SEL_REFCLK; /* Use RefClk as PLL input */ 6086d0c3831SAndrey Smirnov pxl_pllparam |= best_div << 8; /* Divider for PLL RefClk */ 6096d0c3831SAndrey Smirnov pxl_pllparam |= best_mul; /* Multiplier for PLL */ 6106d0c3831SAndrey Smirnov 6116d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, PXL_PLLPARAM, pxl_pllparam); 6126d0c3831SAndrey Smirnov if (ret) 6136d0c3831SAndrey Smirnov return ret; 6147caff0fcSAndrey Gusakov 6157caff0fcSAndrey Gusakov /* Force PLL parameter update and disable bypass */ 616134fb306SAndrey Smirnov return tc_pllupdate(tc, PXL_PLLCTRL); 6177caff0fcSAndrey Gusakov } 6187caff0fcSAndrey Gusakov 6197caff0fcSAndrey Gusakov static int tc_pxl_pll_dis(struct tc_data *tc) 6207caff0fcSAndrey Gusakov { 6217caff0fcSAndrey Gusakov /* Enable PLL bypass, power down PLL */ 6227caff0fcSAndrey Gusakov return regmap_write(tc->regmap, PXL_PLLCTRL, PLLBYP); 6237caff0fcSAndrey Gusakov } 6247caff0fcSAndrey Gusakov 6257caff0fcSAndrey Gusakov static int tc_stream_clock_calc(struct tc_data *tc) 6267caff0fcSAndrey Gusakov { 6277caff0fcSAndrey Gusakov /* 6287caff0fcSAndrey Gusakov * If the Stream clock and Link Symbol clock are 6297caff0fcSAndrey Gusakov * asynchronous with each other, the value of M changes over 6307caff0fcSAndrey Gusakov * time. This way of generating link clock and stream 6317caff0fcSAndrey Gusakov * clock is called Asynchronous Clock mode. The value M 6327caff0fcSAndrey Gusakov * must change while the value N stays constant. The 6337caff0fcSAndrey Gusakov * value of N in this Asynchronous Clock mode must be set 6347caff0fcSAndrey Gusakov * to 2^15 or 32,768. 6357caff0fcSAndrey Gusakov * 6367caff0fcSAndrey Gusakov * LSCLK = 1/10 of high speed link clock 6377caff0fcSAndrey Gusakov * 6387caff0fcSAndrey Gusakov * f_STRMCLK = M/N * f_LSCLK 6397caff0fcSAndrey Gusakov * M/N = f_STRMCLK / f_LSCLK 6407caff0fcSAndrey Gusakov * 6417caff0fcSAndrey Gusakov */ 6426d0c3831SAndrey Smirnov return regmap_write(tc->regmap, DP0_VIDMNGEN1, 32768); 6437caff0fcSAndrey Gusakov } 6447caff0fcSAndrey Gusakov 645c49f60dfSAndrey Smirnov static int tc_set_syspllparam(struct tc_data *tc) 6467caff0fcSAndrey Gusakov { 6477caff0fcSAndrey Gusakov unsigned long rate; 648c49f60dfSAndrey Smirnov u32 pllparam = SYSCLK_SEL_LSCLK | LSCLK_DIV_2; 6497caff0fcSAndrey Gusakov 6507caff0fcSAndrey Gusakov rate = clk_get_rate(tc->refclk); 6517caff0fcSAndrey Gusakov switch (rate) { 6527caff0fcSAndrey Gusakov case 38400000: 653c49f60dfSAndrey Smirnov pllparam |= REF_FREQ_38M4; 6547caff0fcSAndrey Gusakov break; 6557caff0fcSAndrey Gusakov case 26000000: 656c49f60dfSAndrey Smirnov pllparam |= REF_FREQ_26M; 6577caff0fcSAndrey Gusakov break; 6587caff0fcSAndrey Gusakov case 19200000: 659c49f60dfSAndrey Smirnov pllparam |= REF_FREQ_19M2; 6607caff0fcSAndrey Gusakov break; 6617caff0fcSAndrey Gusakov case 13000000: 662c49f60dfSAndrey Smirnov pllparam |= REF_FREQ_13M; 6637caff0fcSAndrey Gusakov break; 6647caff0fcSAndrey Gusakov default: 6657caff0fcSAndrey Gusakov dev_err(tc->dev, "Invalid refclk rate: %lu Hz\n", rate); 6667caff0fcSAndrey Gusakov return -EINVAL; 6677caff0fcSAndrey Gusakov } 6687caff0fcSAndrey Gusakov 669c49f60dfSAndrey Smirnov return regmap_write(tc->regmap, SYS_PLLPARAM, pllparam); 670c49f60dfSAndrey Smirnov } 671c49f60dfSAndrey Smirnov 672c49f60dfSAndrey Smirnov static int tc_aux_link_setup(struct tc_data *tc) 673c49f60dfSAndrey Smirnov { 674c49f60dfSAndrey Smirnov int ret; 675c49f60dfSAndrey Smirnov u32 dp0_auxcfg1; 676c49f60dfSAndrey Smirnov 6777caff0fcSAndrey Gusakov /* Setup DP-PHY / PLL */ 678c49f60dfSAndrey Smirnov ret = tc_set_syspllparam(tc); 6796d0c3831SAndrey Smirnov if (ret) 6806d0c3831SAndrey Smirnov goto err; 6817caff0fcSAndrey Gusakov 6826d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP_PHY_CTRL, 6836d0c3831SAndrey Smirnov BGREN | PWR_SW_EN | PHY_A0_EN); 6846d0c3831SAndrey Smirnov if (ret) 6856d0c3831SAndrey Smirnov goto err; 6867caff0fcSAndrey Gusakov /* 6877caff0fcSAndrey Gusakov * Initially PLLs are in bypass. Force PLL parameter update, 6887caff0fcSAndrey Gusakov * disable PLL bypass, enable PLL 6897caff0fcSAndrey Gusakov */ 690134fb306SAndrey Smirnov ret = tc_pllupdate(tc, DP0_PLLCTRL); 6916d0c3831SAndrey Smirnov if (ret) 6926d0c3831SAndrey Smirnov goto err; 6937caff0fcSAndrey Gusakov 694134fb306SAndrey Smirnov ret = tc_pllupdate(tc, DP1_PLLCTRL); 6956d0c3831SAndrey Smirnov if (ret) 6966d0c3831SAndrey Smirnov goto err; 6977caff0fcSAndrey Gusakov 6988a6483acSTomi Valkeinen ret = tc_poll_timeout(tc, DP_PHY_CTRL, PHY_RDY, PHY_RDY, 100, 100000); 6997caff0fcSAndrey Gusakov if (ret == -ETIMEDOUT) { 7007caff0fcSAndrey Gusakov dev_err(tc->dev, "Timeout waiting for PHY to become ready"); 7017caff0fcSAndrey Gusakov return ret; 702ca342386STomi Valkeinen } else if (ret) { 7037caff0fcSAndrey Gusakov goto err; 704ca342386STomi Valkeinen } 7057caff0fcSAndrey Gusakov 7067caff0fcSAndrey Gusakov /* Setup AUX link */ 7076d0c3831SAndrey Smirnov dp0_auxcfg1 = AUX_RX_FILTER_EN; 7086d0c3831SAndrey Smirnov dp0_auxcfg1 |= 0x06 << 8; /* Aux Bit Period Calculator Threshold */ 7096d0c3831SAndrey Smirnov dp0_auxcfg1 |= 0x3f << 0; /* Aux Response Timeout Timer */ 7106d0c3831SAndrey Smirnov 7116d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP0_AUXCFG1, dp0_auxcfg1); 7126d0c3831SAndrey Smirnov if (ret) 7136d0c3831SAndrey Smirnov goto err; 7147caff0fcSAndrey Gusakov 715824c7bb4SMarek Vasut /* Register DP AUX channel */ 716824c7bb4SMarek Vasut tc->aux.name = "TC358767 AUX i2c adapter"; 717824c7bb4SMarek Vasut tc->aux.dev = tc->dev; 718824c7bb4SMarek Vasut tc->aux.transfer = tc_aux_transfer; 719824c7bb4SMarek Vasut drm_dp_aux_init(&tc->aux); 720824c7bb4SMarek Vasut 7217caff0fcSAndrey Gusakov return 0; 7227caff0fcSAndrey Gusakov err: 7237caff0fcSAndrey Gusakov dev_err(tc->dev, "tc_aux_link_setup failed: %d\n", ret); 7247caff0fcSAndrey Gusakov return ret; 7257caff0fcSAndrey Gusakov } 7267caff0fcSAndrey Gusakov 7277caff0fcSAndrey Gusakov static int tc_get_display_props(struct tc_data *tc) 7287caff0fcSAndrey Gusakov { 729e7dc8d40SThierry Reding u8 revision, num_lanes; 730e7dc8d40SThierry Reding unsigned int rate; 7317caff0fcSAndrey Gusakov int ret; 732d174db07SAndrey Smirnov u8 reg; 7337caff0fcSAndrey Gusakov 7347caff0fcSAndrey Gusakov /* Read DP Rx Link Capability */ 735e7dc8d40SThierry Reding ret = drm_dp_dpcd_read(&tc->aux, DP_DPCD_REV, tc->link.dpcd, 736e7dc8d40SThierry Reding DP_RECEIVER_CAP_SIZE); 7377caff0fcSAndrey Gusakov if (ret < 0) 7387caff0fcSAndrey Gusakov goto err_dpcd_read; 739e7dc8d40SThierry Reding 740e7dc8d40SThierry Reding revision = tc->link.dpcd[DP_DPCD_REV]; 741e7dc8d40SThierry Reding rate = drm_dp_max_link_rate(tc->link.dpcd); 742e7dc8d40SThierry Reding num_lanes = drm_dp_max_lane_count(tc->link.dpcd); 743e7dc8d40SThierry Reding 744e7dc8d40SThierry Reding if (rate != 162000 && rate != 270000) { 745cffd2b16SAndrey Gusakov dev_dbg(tc->dev, "Falling to 2.7 Gbps rate\n"); 746e7dc8d40SThierry Reding rate = 270000; 747cffd2b16SAndrey Gusakov } 748cffd2b16SAndrey Gusakov 749e7dc8d40SThierry Reding tc->link.rate = rate; 750e7dc8d40SThierry Reding 751e7dc8d40SThierry Reding if (num_lanes > 2) { 752cffd2b16SAndrey Gusakov dev_dbg(tc->dev, "Falling to 2 lanes\n"); 753e7dc8d40SThierry Reding num_lanes = 2; 754cffd2b16SAndrey Gusakov } 7557caff0fcSAndrey Gusakov 756e7dc8d40SThierry Reding tc->link.num_lanes = num_lanes; 757e7dc8d40SThierry Reding 758d174db07SAndrey Smirnov ret = drm_dp_dpcd_readb(&tc->aux, DP_MAX_DOWNSPREAD, ®); 7597caff0fcSAndrey Gusakov if (ret < 0) 7607caff0fcSAndrey Gusakov goto err_dpcd_read; 761d174db07SAndrey Smirnov tc->link.spread = reg & DP_MAX_DOWNSPREAD_0_5; 7627caff0fcSAndrey Gusakov 763d174db07SAndrey Smirnov ret = drm_dp_dpcd_readb(&tc->aux, DP_MAIN_LINK_CHANNEL_CODING, ®); 7647caff0fcSAndrey Gusakov if (ret < 0) 7657caff0fcSAndrey Gusakov goto err_dpcd_read; 7664b30bf41STomi Valkeinen 767e5607637STomi Valkeinen tc->link.scrambler_dis = false; 7687caff0fcSAndrey Gusakov /* read assr */ 769d174db07SAndrey Smirnov ret = drm_dp_dpcd_readb(&tc->aux, DP_EDP_CONFIGURATION_SET, ®); 7707caff0fcSAndrey Gusakov if (ret < 0) 7717caff0fcSAndrey Gusakov goto err_dpcd_read; 772d174db07SAndrey Smirnov tc->link.assr = reg & DP_ALTERNATE_SCRAMBLER_RESET_ENABLE; 7737caff0fcSAndrey Gusakov 7747caff0fcSAndrey Gusakov dev_dbg(tc->dev, "DPCD rev: %d.%d, rate: %s, lanes: %d, framing: %s\n", 775e7dc8d40SThierry Reding revision >> 4, revision & 0x0f, 776e7dc8d40SThierry Reding (tc->link.rate == 162000) ? "1.62Gbps" : "2.7Gbps", 777e7dc8d40SThierry Reding tc->link.num_lanes, 778e7dc8d40SThierry Reding drm_dp_enhanced_frame_cap(tc->link.dpcd) ? 77998bca69bSThierry Reding "enhanced" : "default"); 780e5607637STomi Valkeinen dev_dbg(tc->dev, "Downspread: %s, scrambler: %s\n", 781e5607637STomi Valkeinen tc->link.spread ? "0.5%" : "0.0%", 782e5607637STomi Valkeinen tc->link.scrambler_dis ? "disabled" : "enabled"); 7837caff0fcSAndrey Gusakov dev_dbg(tc->dev, "Display ASSR: %d, TC358767 ASSR: %d\n", 7847caff0fcSAndrey Gusakov tc->link.assr, tc->assr); 7857caff0fcSAndrey Gusakov 7867caff0fcSAndrey Gusakov return 0; 7877caff0fcSAndrey Gusakov 7887caff0fcSAndrey Gusakov err_dpcd_read: 7897caff0fcSAndrey Gusakov dev_err(tc->dev, "failed to read DPCD: %d\n", ret); 7907caff0fcSAndrey Gusakov return ret; 7917caff0fcSAndrey Gusakov } 7927caff0fcSAndrey Gusakov 793aebe58a7SMarek Vasut static int tc_set_common_video_mode(struct tc_data *tc, 79463f8f3baSLaurent Pinchart const struct drm_display_mode *mode) 7957caff0fcSAndrey Gusakov { 7967caff0fcSAndrey Gusakov int left_margin = mode->htotal - mode->hsync_end; 7977caff0fcSAndrey Gusakov int right_margin = mode->hsync_start - mode->hdisplay; 7987caff0fcSAndrey Gusakov int hsync_len = mode->hsync_end - mode->hsync_start; 7997caff0fcSAndrey Gusakov int upper_margin = mode->vtotal - mode->vsync_end; 8007caff0fcSAndrey Gusakov int lower_margin = mode->vsync_start - mode->vdisplay; 8017caff0fcSAndrey Gusakov int vsync_len = mode->vsync_end - mode->vsync_start; 802aebe58a7SMarek Vasut int ret; 80366d1c3b9SAndrey Gusakov 8047caff0fcSAndrey Gusakov dev_dbg(tc->dev, "set mode %dx%d\n", 8057caff0fcSAndrey Gusakov mode->hdisplay, mode->vdisplay); 8067caff0fcSAndrey Gusakov dev_dbg(tc->dev, "H margin %d,%d sync %d\n", 8077caff0fcSAndrey Gusakov left_margin, right_margin, hsync_len); 8087caff0fcSAndrey Gusakov dev_dbg(tc->dev, "V margin %d,%d sync %d\n", 8097caff0fcSAndrey Gusakov upper_margin, lower_margin, vsync_len); 8107caff0fcSAndrey Gusakov dev_dbg(tc->dev, "total: %dx%d\n", mode->htotal, mode->vtotal); 8117caff0fcSAndrey Gusakov 8127caff0fcSAndrey Gusakov 81366d1c3b9SAndrey Gusakov /* 81466d1c3b9SAndrey Gusakov * LCD Ctl Frame Size 81566d1c3b9SAndrey Gusakov * datasheet is not clear of vsdelay in case of DPI 81666d1c3b9SAndrey Gusakov * assume we do not need any delay when DPI is a source of 81766d1c3b9SAndrey Gusakov * sync signals 81866d1c3b9SAndrey Gusakov */ 8196d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, VPCTRL0, 8203f072c30SAndrey Smirnov FIELD_PREP(VSDELAY, 0) | 8217caff0fcSAndrey Gusakov OPXLFMT_RGB888 | FRMSYNC_DISABLED | MSF_DISABLED); 8226d0c3831SAndrey Smirnov if (ret) 8236d0c3831SAndrey Smirnov return ret; 8246d0c3831SAndrey Smirnov 8256d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, HTIM01, 8263f072c30SAndrey Smirnov FIELD_PREP(HBPR, ALIGN(left_margin, 2)) | 8273f072c30SAndrey Smirnov FIELD_PREP(HPW, ALIGN(hsync_len, 2))); 8286d0c3831SAndrey Smirnov if (ret) 8296d0c3831SAndrey Smirnov return ret; 8306d0c3831SAndrey Smirnov 8316d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, HTIM02, 8323f072c30SAndrey Smirnov FIELD_PREP(HDISPR, ALIGN(mode->hdisplay, 2)) | 8333f072c30SAndrey Smirnov FIELD_PREP(HFPR, ALIGN(right_margin, 2))); 8346d0c3831SAndrey Smirnov if (ret) 8356d0c3831SAndrey Smirnov return ret; 8366d0c3831SAndrey Smirnov 8376d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, VTIM01, 8383f072c30SAndrey Smirnov FIELD_PREP(VBPR, upper_margin) | 8393f072c30SAndrey Smirnov FIELD_PREP(VSPR, vsync_len)); 8406d0c3831SAndrey Smirnov if (ret) 8416d0c3831SAndrey Smirnov return ret; 8426d0c3831SAndrey Smirnov 8436d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, VTIM02, 8443f072c30SAndrey Smirnov FIELD_PREP(VFPR, lower_margin) | 8453f072c30SAndrey Smirnov FIELD_PREP(VDISPR, mode->vdisplay)); 8466d0c3831SAndrey Smirnov if (ret) 8476d0c3831SAndrey Smirnov return ret; 8486d0c3831SAndrey Smirnov 8496d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, VFUEN0, VFUEN); /* update settings */ 8506d0c3831SAndrey Smirnov if (ret) 8516d0c3831SAndrey Smirnov return ret; 8527caff0fcSAndrey Gusakov 8537caff0fcSAndrey Gusakov /* Test pattern settings */ 8546d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, TSTCTL, 8553f072c30SAndrey Smirnov FIELD_PREP(COLOR_R, 120) | 8563f072c30SAndrey Smirnov FIELD_PREP(COLOR_G, 20) | 8573f072c30SAndrey Smirnov FIELD_PREP(COLOR_B, 99) | 8583f072c30SAndrey Smirnov ENI2CFILTER | 8593f072c30SAndrey Smirnov FIELD_PREP(COLOR_BAR_MODE, COLOR_BAR_MODE_BARS)); 860aebe58a7SMarek Vasut 8616d0c3831SAndrey Smirnov return ret; 862aebe58a7SMarek Vasut } 863aebe58a7SMarek Vasut 864bbfd3190SMarek Vasut static int tc_set_dpi_video_mode(struct tc_data *tc, 865bbfd3190SMarek Vasut const struct drm_display_mode *mode) 866bbfd3190SMarek Vasut { 867bbfd3190SMarek Vasut u32 value = POCTRL_S2P; 868bbfd3190SMarek Vasut 869bbfd3190SMarek Vasut if (tc->mode.flags & DRM_MODE_FLAG_NHSYNC) 870bbfd3190SMarek Vasut value |= POCTRL_HS_POL; 871bbfd3190SMarek Vasut 872bbfd3190SMarek Vasut if (tc->mode.flags & DRM_MODE_FLAG_NVSYNC) 873bbfd3190SMarek Vasut value |= POCTRL_VS_POL; 874bbfd3190SMarek Vasut 875bbfd3190SMarek Vasut return regmap_write(tc->regmap, POCTRL, value); 876bbfd3190SMarek Vasut } 877bbfd3190SMarek Vasut 878aebe58a7SMarek Vasut static int tc_set_edp_video_mode(struct tc_data *tc, 879aebe58a7SMarek Vasut const struct drm_display_mode *mode) 880aebe58a7SMarek Vasut { 881aebe58a7SMarek Vasut int ret; 882aebe58a7SMarek Vasut int vid_sync_dly; 883aebe58a7SMarek Vasut int max_tu_symbol; 884aebe58a7SMarek Vasut 885aebe58a7SMarek Vasut int left_margin = mode->htotal - mode->hsync_end; 886aebe58a7SMarek Vasut int hsync_len = mode->hsync_end - mode->hsync_start; 887aebe58a7SMarek Vasut int upper_margin = mode->vtotal - mode->vsync_end; 888aebe58a7SMarek Vasut int vsync_len = mode->vsync_end - mode->vsync_start; 889aebe58a7SMarek Vasut u32 dp0_syncval; 890aebe58a7SMarek Vasut u32 bits_per_pixel = 24; 891aebe58a7SMarek Vasut u32 in_bw, out_bw; 892*5fa9e161SLucas Stach u32 dpipxlfmt; 893aebe58a7SMarek Vasut 894aebe58a7SMarek Vasut /* 895aebe58a7SMarek Vasut * Recommended maximum number of symbols transferred in a transfer unit: 896aebe58a7SMarek Vasut * DIV_ROUND_UP((input active video bandwidth in bytes) * tu_size, 897aebe58a7SMarek Vasut * (output active video bandwidth in bytes)) 898aebe58a7SMarek Vasut * Must be less than tu_size. 899aebe58a7SMarek Vasut */ 900aebe58a7SMarek Vasut 901aebe58a7SMarek Vasut in_bw = mode->clock * bits_per_pixel / 8; 902aebe58a7SMarek Vasut out_bw = tc->link.num_lanes * tc->link.rate; 903aebe58a7SMarek Vasut max_tu_symbol = DIV_ROUND_UP(in_bw * TU_SIZE_RECOMMENDED, out_bw); 9047caff0fcSAndrey Gusakov 9057caff0fcSAndrey Gusakov /* DP Main Stream Attributes */ 9067caff0fcSAndrey Gusakov vid_sync_dly = hsync_len + left_margin + mode->hdisplay; 9076d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP0_VIDSYNCDELAY, 9083f072c30SAndrey Smirnov FIELD_PREP(THRESH_DLY, max_tu_symbol) | 9093f072c30SAndrey Smirnov FIELD_PREP(VID_SYNC_DLY, vid_sync_dly)); 9107caff0fcSAndrey Gusakov 9116d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP0_TOTALVAL, 9123f072c30SAndrey Smirnov FIELD_PREP(H_TOTAL, mode->htotal) | 9133f072c30SAndrey Smirnov FIELD_PREP(V_TOTAL, mode->vtotal)); 9146d0c3831SAndrey Smirnov if (ret) 9156d0c3831SAndrey Smirnov return ret; 9167caff0fcSAndrey Gusakov 9176d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP0_STARTVAL, 9183f072c30SAndrey Smirnov FIELD_PREP(H_START, left_margin + hsync_len) | 9193f072c30SAndrey Smirnov FIELD_PREP(V_START, upper_margin + vsync_len)); 9206d0c3831SAndrey Smirnov if (ret) 9216d0c3831SAndrey Smirnov return ret; 9227caff0fcSAndrey Gusakov 9236d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP0_ACTIVEVAL, 9243f072c30SAndrey Smirnov FIELD_PREP(V_ACT, mode->vdisplay) | 9253f072c30SAndrey Smirnov FIELD_PREP(H_ACT, mode->hdisplay)); 9266d0c3831SAndrey Smirnov if (ret) 9276d0c3831SAndrey Smirnov return ret; 9287caff0fcSAndrey Gusakov 9293f072c30SAndrey Smirnov dp0_syncval = FIELD_PREP(VS_WIDTH, vsync_len) | 9303f072c30SAndrey Smirnov FIELD_PREP(HS_WIDTH, hsync_len); 9317caff0fcSAndrey Gusakov 9323f072c30SAndrey Smirnov if (mode->flags & DRM_MODE_FLAG_NVSYNC) 9333f072c30SAndrey Smirnov dp0_syncval |= SYNCVAL_VS_POL_ACTIVE_LOW; 9347caff0fcSAndrey Gusakov 9353f072c30SAndrey Smirnov if (mode->flags & DRM_MODE_FLAG_NHSYNC) 9363f072c30SAndrey Smirnov dp0_syncval |= SYNCVAL_HS_POL_ACTIVE_LOW; 9373f072c30SAndrey Smirnov 9386d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP0_SYNCVAL, dp0_syncval); 9396d0c3831SAndrey Smirnov if (ret) 9406d0c3831SAndrey Smirnov return ret; 9413f072c30SAndrey Smirnov 942*5fa9e161SLucas Stach dpipxlfmt = DE_POL_ACTIVE_HIGH | SUB_CFG_TYPE_CONFIG1 | DPI_BPP_RGB888; 943*5fa9e161SLucas Stach 944*5fa9e161SLucas Stach if (mode->flags & DRM_MODE_FLAG_NVSYNC) 945*5fa9e161SLucas Stach dpipxlfmt |= VS_POL_ACTIVE_LOW; 946*5fa9e161SLucas Stach 947*5fa9e161SLucas Stach if (mode->flags & DRM_MODE_FLAG_NHSYNC) 948*5fa9e161SLucas Stach dpipxlfmt |= HS_POL_ACTIVE_LOW; 949*5fa9e161SLucas Stach 950*5fa9e161SLucas Stach ret = regmap_write(tc->regmap, DPIPXLFMT, dpipxlfmt); 9516d0c3831SAndrey Smirnov if (ret) 9526d0c3831SAndrey Smirnov return ret; 9533f072c30SAndrey Smirnov 9546d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP0_MISC, 9553f072c30SAndrey Smirnov FIELD_PREP(MAX_TU_SYMBOL, max_tu_symbol) | 9563f072c30SAndrey Smirnov FIELD_PREP(TU_SIZE, TU_SIZE_RECOMMENDED) | 957f3b8adbeSAndrey Gusakov BPC_8); 9586d0c3831SAndrey Smirnov return ret; 9597caff0fcSAndrey Gusakov } 9607caff0fcSAndrey Gusakov 961f9538357STomi Valkeinen static int tc_wait_link_training(struct tc_data *tc) 9627caff0fcSAndrey Gusakov { 9637caff0fcSAndrey Gusakov u32 value; 9647caff0fcSAndrey Gusakov int ret; 9657caff0fcSAndrey Gusakov 966aa92213fSAndrey Smirnov ret = tc_poll_timeout(tc, DP0_LTSTAT, LT_LOOPDONE, 9678a6483acSTomi Valkeinen LT_LOOPDONE, 500, 100000); 968aa92213fSAndrey Smirnov if (ret) { 969f9538357STomi Valkeinen dev_err(tc->dev, "Link training timeout waiting for LT_LOOPDONE!\n"); 970aa92213fSAndrey Smirnov return ret; 9717caff0fcSAndrey Gusakov } 9727caff0fcSAndrey Gusakov 9736d0c3831SAndrey Smirnov ret = regmap_read(tc->regmap, DP0_LTSTAT, &value); 9746d0c3831SAndrey Smirnov if (ret) 9756d0c3831SAndrey Smirnov return ret; 976f9538357STomi Valkeinen 977aa92213fSAndrey Smirnov return (value >> 8) & 0x7; 9787caff0fcSAndrey Gusakov } 9797caff0fcSAndrey Gusakov 980cb3263b2STomi Valkeinen static int tc_main_link_enable(struct tc_data *tc) 9817caff0fcSAndrey Gusakov { 9827caff0fcSAndrey Gusakov struct drm_dp_aux *aux = &tc->aux; 9837caff0fcSAndrey Gusakov struct device *dev = tc->dev; 9847caff0fcSAndrey Gusakov u32 dp_phy_ctrl; 9857caff0fcSAndrey Gusakov u32 value; 9867caff0fcSAndrey Gusakov int ret; 98732d36219SAndrey Smirnov u8 tmp[DP_LINK_STATUS_SIZE]; 9887caff0fcSAndrey Gusakov 989cb3263b2STomi Valkeinen dev_dbg(tc->dev, "link enable\n"); 990cb3263b2STomi Valkeinen 9916d0c3831SAndrey Smirnov ret = regmap_read(tc->regmap, DP0CTL, &value); 9926d0c3831SAndrey Smirnov if (ret) 9936d0c3831SAndrey Smirnov return ret; 99467bca92fSTomi Valkeinen 9956d0c3831SAndrey Smirnov if (WARN_ON(value & DP_EN)) { 9966d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP0CTL, 0); 9976d0c3831SAndrey Smirnov if (ret) 9986d0c3831SAndrey Smirnov return ret; 9996d0c3831SAndrey Smirnov } 10006d0c3831SAndrey Smirnov 10016d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP0_SRCCTRL, tc_srcctrl(tc)); 10026d0c3831SAndrey Smirnov if (ret) 10036d0c3831SAndrey Smirnov return ret; 10049a63bd6fSTomi Valkeinen /* SSCG and BW27 on DP1 must be set to the same as on DP0 */ 10056d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP1_SRCCTRL, 10069a63bd6fSTomi Valkeinen (tc->link.spread ? DP0_SRCCTRL_SSCG : 0) | 1007e7dc8d40SThierry Reding ((tc->link.rate != 162000) ? DP0_SRCCTRL_BW27 : 0)); 10086d0c3831SAndrey Smirnov if (ret) 10096d0c3831SAndrey Smirnov return ret; 10107caff0fcSAndrey Gusakov 1011c49f60dfSAndrey Smirnov ret = tc_set_syspllparam(tc); 10126d0c3831SAndrey Smirnov if (ret) 10136d0c3831SAndrey Smirnov return ret; 1014adf41098STomi Valkeinen 10157caff0fcSAndrey Gusakov /* Setup Main Link */ 10164d9d54a7STomi Valkeinen dp_phy_ctrl = BGREN | PWR_SW_EN | PHY_A0_EN | PHY_M0_EN; 1017e7dc8d40SThierry Reding if (tc->link.num_lanes == 2) 10184d9d54a7STomi Valkeinen dp_phy_ctrl |= PHY_2LANE; 10196d0c3831SAndrey Smirnov 10206d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP_PHY_CTRL, dp_phy_ctrl); 10216d0c3831SAndrey Smirnov if (ret) 10226d0c3831SAndrey Smirnov return ret; 10237caff0fcSAndrey Gusakov 10247caff0fcSAndrey Gusakov /* PLL setup */ 1025134fb306SAndrey Smirnov ret = tc_pllupdate(tc, DP0_PLLCTRL); 10266d0c3831SAndrey Smirnov if (ret) 10276d0c3831SAndrey Smirnov return ret; 10287caff0fcSAndrey Gusakov 1029134fb306SAndrey Smirnov ret = tc_pllupdate(tc, DP1_PLLCTRL); 10306d0c3831SAndrey Smirnov if (ret) 10316d0c3831SAndrey Smirnov return ret; 10327caff0fcSAndrey Gusakov 10337caff0fcSAndrey Gusakov /* Reset/Enable Main Links */ 10347caff0fcSAndrey Gusakov dp_phy_ctrl |= DP_PHY_RST | PHY_M1_RST | PHY_M0_RST; 10356d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP_PHY_CTRL, dp_phy_ctrl); 10367caff0fcSAndrey Gusakov usleep_range(100, 200); 10377caff0fcSAndrey Gusakov dp_phy_ctrl &= ~(DP_PHY_RST | PHY_M1_RST | PHY_M0_RST); 10386d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP_PHY_CTRL, dp_phy_ctrl); 10397caff0fcSAndrey Gusakov 10408a6483acSTomi Valkeinen ret = tc_poll_timeout(tc, DP_PHY_CTRL, PHY_RDY, PHY_RDY, 500, 100000); 1041ebcce4e6SAndrey Smirnov if (ret) { 10427caff0fcSAndrey Gusakov dev_err(dev, "timeout waiting for phy become ready"); 1043ebcce4e6SAndrey Smirnov return ret; 10447caff0fcSAndrey Gusakov } 10457caff0fcSAndrey Gusakov 10467caff0fcSAndrey Gusakov /* Set misc: 8 bits per color */ 10477caff0fcSAndrey Gusakov ret = regmap_update_bits(tc->regmap, DP0_MISC, BPC_8, BPC_8); 10487caff0fcSAndrey Gusakov if (ret) 10496d0c3831SAndrey Smirnov return ret; 10507caff0fcSAndrey Gusakov 10517caff0fcSAndrey Gusakov /* 10527caff0fcSAndrey Gusakov * ASSR mode 10537caff0fcSAndrey Gusakov * on TC358767 side ASSR configured through strap pin 10547caff0fcSAndrey Gusakov * seems there is no way to change this setting from SW 10557caff0fcSAndrey Gusakov * 10567caff0fcSAndrey Gusakov * check is tc configured for same mode 10577caff0fcSAndrey Gusakov */ 10587caff0fcSAndrey Gusakov if (tc->assr != tc->link.assr) { 10597caff0fcSAndrey Gusakov dev_dbg(dev, "Trying to set display to ASSR: %d\n", 10607caff0fcSAndrey Gusakov tc->assr); 10617caff0fcSAndrey Gusakov /* try to set ASSR on display side */ 10627caff0fcSAndrey Gusakov tmp[0] = tc->assr; 10637caff0fcSAndrey Gusakov ret = drm_dp_dpcd_writeb(aux, DP_EDP_CONFIGURATION_SET, tmp[0]); 10647caff0fcSAndrey Gusakov if (ret < 0) 10657caff0fcSAndrey Gusakov goto err_dpcd_read; 10667caff0fcSAndrey Gusakov /* read back */ 10677caff0fcSAndrey Gusakov ret = drm_dp_dpcd_readb(aux, DP_EDP_CONFIGURATION_SET, tmp); 10687caff0fcSAndrey Gusakov if (ret < 0) 10697caff0fcSAndrey Gusakov goto err_dpcd_read; 10707caff0fcSAndrey Gusakov 10717caff0fcSAndrey Gusakov if (tmp[0] != tc->assr) { 107287291e5dSLucas Stach dev_dbg(dev, "Failed to switch display ASSR to %d, falling back to unscrambled mode\n", 10737caff0fcSAndrey Gusakov tc->assr); 10747caff0fcSAndrey Gusakov /* trying with disabled scrambler */ 1075e5607637STomi Valkeinen tc->link.scrambler_dis = true; 10767caff0fcSAndrey Gusakov } 10777caff0fcSAndrey Gusakov } 10787caff0fcSAndrey Gusakov 10797caff0fcSAndrey Gusakov /* Setup Link & DPRx Config for Training */ 1080e7dc8d40SThierry Reding tmp[0] = drm_dp_link_rate_to_bw_code(tc->link.rate); 1081e7dc8d40SThierry Reding tmp[1] = tc->link.num_lanes; 1082e7dc8d40SThierry Reding 1083e7dc8d40SThierry Reding if (drm_dp_enhanced_frame_cap(tc->link.dpcd)) 1084e7dc8d40SThierry Reding tmp[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN; 1085e7dc8d40SThierry Reding 1086e7dc8d40SThierry Reding ret = drm_dp_dpcd_write(aux, DP_LINK_BW_SET, tmp, 2); 10877caff0fcSAndrey Gusakov if (ret < 0) 10887caff0fcSAndrey Gusakov goto err_dpcd_write; 10897caff0fcSAndrey Gusakov 10907caff0fcSAndrey Gusakov /* DOWNSPREAD_CTRL */ 10917caff0fcSAndrey Gusakov tmp[0] = tc->link.spread ? DP_SPREAD_AMP_0_5 : 0x00; 10927caff0fcSAndrey Gusakov /* MAIN_LINK_CHANNEL_CODING_SET */ 10934b30bf41STomi Valkeinen tmp[1] = DP_SET_ANSI_8B10B; 10947caff0fcSAndrey Gusakov ret = drm_dp_dpcd_write(aux, DP_DOWNSPREAD_CTRL, tmp, 2); 10957caff0fcSAndrey Gusakov if (ret < 0) 10967caff0fcSAndrey Gusakov goto err_dpcd_write; 10977caff0fcSAndrey Gusakov 1098c28d1484STomi Valkeinen /* Reset voltage-swing & pre-emphasis */ 1099c28d1484STomi Valkeinen tmp[0] = tmp[1] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | 1100c28d1484STomi Valkeinen DP_TRAIN_PRE_EMPH_LEVEL_0; 1101c28d1484STomi Valkeinen ret = drm_dp_dpcd_write(aux, DP_TRAINING_LANE0_SET, tmp, 2); 1102c28d1484STomi Valkeinen if (ret < 0) 1103c28d1484STomi Valkeinen goto err_dpcd_write; 1104c28d1484STomi Valkeinen 1105f9538357STomi Valkeinen /* Clock-Recovery */ 1106f9538357STomi Valkeinen 1107f9538357STomi Valkeinen /* Set DPCD 0x102 for Training Pattern 1 */ 11086d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP0_SNKLTCTRL, 11096d0c3831SAndrey Smirnov DP_LINK_SCRAMBLING_DISABLE | 1110f9538357STomi Valkeinen DP_TRAINING_PATTERN_1); 11116d0c3831SAndrey Smirnov if (ret) 11126d0c3831SAndrey Smirnov return ret; 1113f9538357STomi Valkeinen 11146d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP0_LTLOOPCTRL, 1115f9538357STomi Valkeinen (15 << 28) | /* Defer Iteration Count */ 1116f9538357STomi Valkeinen (15 << 24) | /* Loop Iteration Count */ 1117f9538357STomi Valkeinen (0xd << 0)); /* Loop Timer Delay */ 11186d0c3831SAndrey Smirnov if (ret) 11196d0c3831SAndrey Smirnov return ret; 1120f9538357STomi Valkeinen 11216d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP0_SRCCTRL, 11226d0c3831SAndrey Smirnov tc_srcctrl(tc) | DP0_SRCCTRL_SCRMBLDIS | 11236d0c3831SAndrey Smirnov DP0_SRCCTRL_AUTOCORRECT | 11246d0c3831SAndrey Smirnov DP0_SRCCTRL_TP1); 11256d0c3831SAndrey Smirnov if (ret) 11266d0c3831SAndrey Smirnov return ret; 1127f9538357STomi Valkeinen 1128f9538357STomi Valkeinen /* Enable DP0 to start Link Training */ 11296d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP0CTL, 1130e7dc8d40SThierry Reding (drm_dp_enhanced_frame_cap(tc->link.dpcd) ? 1131e7dc8d40SThierry Reding EF_EN : 0) | DP_EN); 11326d0c3831SAndrey Smirnov if (ret) 11336d0c3831SAndrey Smirnov return ret; 1134f9538357STomi Valkeinen 1135f9538357STomi Valkeinen /* wait */ 11366d0c3831SAndrey Smirnov 1137f9538357STomi Valkeinen ret = tc_wait_link_training(tc); 1138f9538357STomi Valkeinen if (ret < 0) 11396d0c3831SAndrey Smirnov return ret; 11407caff0fcSAndrey Gusakov 1141f9538357STomi Valkeinen if (ret) { 1142f9538357STomi Valkeinen dev_err(tc->dev, "Link training phase 1 failed: %s\n", 1143f9538357STomi Valkeinen training_pattern1_errors[ret]); 11446d0c3831SAndrey Smirnov return -ENODEV; 1145f9538357STomi Valkeinen } 1146f9538357STomi Valkeinen 1147f9538357STomi Valkeinen /* Channel Equalization */ 1148f9538357STomi Valkeinen 1149f9538357STomi Valkeinen /* Set DPCD 0x102 for Training Pattern 2 */ 11506d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP0_SNKLTCTRL, 11516d0c3831SAndrey Smirnov DP_LINK_SCRAMBLING_DISABLE | 1152f9538357STomi Valkeinen DP_TRAINING_PATTERN_2); 11536d0c3831SAndrey Smirnov if (ret) 11546d0c3831SAndrey Smirnov return ret; 1155f9538357STomi Valkeinen 11566d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP0_SRCCTRL, 11576d0c3831SAndrey Smirnov tc_srcctrl(tc) | DP0_SRCCTRL_SCRMBLDIS | 11586d0c3831SAndrey Smirnov DP0_SRCCTRL_AUTOCORRECT | 11596d0c3831SAndrey Smirnov DP0_SRCCTRL_TP2); 11606d0c3831SAndrey Smirnov if (ret) 11616d0c3831SAndrey Smirnov return ret; 1162f9538357STomi Valkeinen 1163f9538357STomi Valkeinen /* wait */ 1164f9538357STomi Valkeinen ret = tc_wait_link_training(tc); 1165f9538357STomi Valkeinen if (ret < 0) 11666d0c3831SAndrey Smirnov return ret; 1167f9538357STomi Valkeinen 1168f9538357STomi Valkeinen if (ret) { 1169f9538357STomi Valkeinen dev_err(tc->dev, "Link training phase 2 failed: %s\n", 1170f9538357STomi Valkeinen training_pattern2_errors[ret]); 11716d0c3831SAndrey Smirnov return -ENODEV; 1172f9538357STomi Valkeinen } 11737caff0fcSAndrey Gusakov 11740776a269STomi Valkeinen /* 11750776a269STomi Valkeinen * Toshiba's documentation suggests to first clear DPCD 0x102, then 11760776a269STomi Valkeinen * clear the training pattern bit in DP0_SRCCTRL. Testing shows 11770776a269STomi Valkeinen * that the link sometimes drops if those steps are done in that order, 11780776a269STomi Valkeinen * but if the steps are done in reverse order, the link stays up. 11790776a269STomi Valkeinen * 11800776a269STomi Valkeinen * So we do the steps differently than documented here. 11810776a269STomi Valkeinen */ 11820776a269STomi Valkeinen 11830776a269STomi Valkeinen /* Clear Training Pattern, set AutoCorrect Mode = 1 */ 11846d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP0_SRCCTRL, tc_srcctrl(tc) | 11856d0c3831SAndrey Smirnov DP0_SRCCTRL_AUTOCORRECT); 11866d0c3831SAndrey Smirnov if (ret) 11876d0c3831SAndrey Smirnov return ret; 11880776a269STomi Valkeinen 11897caff0fcSAndrey Gusakov /* Clear DPCD 0x102 */ 11907caff0fcSAndrey Gusakov /* Note: Can Not use DP0_SNKLTCTRL (0x06E4) short cut */ 11917caff0fcSAndrey Gusakov tmp[0] = tc->link.scrambler_dis ? DP_LINK_SCRAMBLING_DISABLE : 0x00; 11927caff0fcSAndrey Gusakov ret = drm_dp_dpcd_writeb(aux, DP_TRAINING_PATTERN_SET, tmp[0]); 11937caff0fcSAndrey Gusakov if (ret < 0) 11947caff0fcSAndrey Gusakov goto err_dpcd_write; 11957caff0fcSAndrey Gusakov 11960bf25146STomi Valkeinen /* Check link status */ 11970bf25146STomi Valkeinen ret = drm_dp_dpcd_read_link_status(aux, tmp); 11987caff0fcSAndrey Gusakov if (ret < 0) 11997caff0fcSAndrey Gusakov goto err_dpcd_read; 12007caff0fcSAndrey Gusakov 12010bf25146STomi Valkeinen ret = 0; 12027caff0fcSAndrey Gusakov 12030bf25146STomi Valkeinen value = tmp[0] & DP_CHANNEL_EQ_BITS; 12040bf25146STomi Valkeinen 12050bf25146STomi Valkeinen if (value != DP_CHANNEL_EQ_BITS) { 12060bf25146STomi Valkeinen dev_err(tc->dev, "Lane 0 failed: %x\n", value); 12070bf25146STomi Valkeinen ret = -ENODEV; 12080bf25146STomi Valkeinen } 12090bf25146STomi Valkeinen 1210e7dc8d40SThierry Reding if (tc->link.num_lanes == 2) { 12110bf25146STomi Valkeinen value = (tmp[0] >> 4) & DP_CHANNEL_EQ_BITS; 12120bf25146STomi Valkeinen 12130bf25146STomi Valkeinen if (value != DP_CHANNEL_EQ_BITS) { 12140bf25146STomi Valkeinen dev_err(tc->dev, "Lane 1 failed: %x\n", value); 12150bf25146STomi Valkeinen ret = -ENODEV; 12160bf25146STomi Valkeinen } 12170bf25146STomi Valkeinen 12180bf25146STomi Valkeinen if (!(tmp[2] & DP_INTERLANE_ALIGN_DONE)) { 12190bf25146STomi Valkeinen dev_err(tc->dev, "Interlane align failed\n"); 12200bf25146STomi Valkeinen ret = -ENODEV; 12210bf25146STomi Valkeinen } 12220bf25146STomi Valkeinen } 12230bf25146STomi Valkeinen 12240bf25146STomi Valkeinen if (ret) { 12250bf25146STomi Valkeinen dev_err(dev, "0x0202 LANE0_1_STATUS: 0x%02x\n", tmp[0]); 12260bf25146STomi Valkeinen dev_err(dev, "0x0203 LANE2_3_STATUS 0x%02x\n", tmp[1]); 12270bf25146STomi Valkeinen dev_err(dev, "0x0204 LANE_ALIGN_STATUS_UPDATED: 0x%02x\n", tmp[2]); 12280bf25146STomi Valkeinen dev_err(dev, "0x0205 SINK_STATUS: 0x%02x\n", tmp[3]); 12290bf25146STomi Valkeinen dev_err(dev, "0x0206 ADJUST_REQUEST_LANE0_1: 0x%02x\n", tmp[4]); 12300bf25146STomi Valkeinen dev_err(dev, "0x0207 ADJUST_REQUEST_LANE2_3: 0x%02x\n", tmp[5]); 12316d0c3831SAndrey Smirnov return ret; 12327caff0fcSAndrey Gusakov } 12337caff0fcSAndrey Gusakov 12347caff0fcSAndrey Gusakov return 0; 12357caff0fcSAndrey Gusakov err_dpcd_read: 12367caff0fcSAndrey Gusakov dev_err(tc->dev, "Failed to read DPCD: %d\n", ret); 12377caff0fcSAndrey Gusakov return ret; 12387caff0fcSAndrey Gusakov err_dpcd_write: 12397caff0fcSAndrey Gusakov dev_err(tc->dev, "Failed to write DPCD: %d\n", ret); 12407caff0fcSAndrey Gusakov return ret; 12417caff0fcSAndrey Gusakov } 12427caff0fcSAndrey Gusakov 1243cb3263b2STomi Valkeinen static int tc_main_link_disable(struct tc_data *tc) 1244cb3263b2STomi Valkeinen { 1245cb3263b2STomi Valkeinen int ret; 1246cb3263b2STomi Valkeinen 1247cb3263b2STomi Valkeinen dev_dbg(tc->dev, "link disable\n"); 1248cb3263b2STomi Valkeinen 12496d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP0_SRCCTRL, 0); 12506d0c3831SAndrey Smirnov if (ret) 1251cb3263b2STomi Valkeinen return ret; 12526d0c3831SAndrey Smirnov 12536d0c3831SAndrey Smirnov return regmap_write(tc->regmap, DP0CTL, 0); 1254cb3263b2STomi Valkeinen } 1255cb3263b2STomi Valkeinen 1256d7fd32ecSMarek Vasut static int tc_dsi_rx_enable(struct tc_data *tc) 1257d7fd32ecSMarek Vasut { 1258d7fd32ecSMarek Vasut u32 value; 1259d7fd32ecSMarek Vasut int ret; 1260d7fd32ecSMarek Vasut 1261d7fd32ecSMarek Vasut regmap_write(tc->regmap, PPI_D0S_CLRSIPOCOUNT, 3); 1262d7fd32ecSMarek Vasut regmap_write(tc->regmap, PPI_D1S_CLRSIPOCOUNT, 3); 1263d7fd32ecSMarek Vasut regmap_write(tc->regmap, PPI_D2S_CLRSIPOCOUNT, 3); 1264d7fd32ecSMarek Vasut regmap_write(tc->regmap, PPI_D3S_CLRSIPOCOUNT, 3); 1265d7fd32ecSMarek Vasut regmap_write(tc->regmap, PPI_D0S_ATMR, 0); 1266d7fd32ecSMarek Vasut regmap_write(tc->regmap, PPI_D1S_ATMR, 0); 1267d7fd32ecSMarek Vasut regmap_write(tc->regmap, PPI_TX_RX_TA, TTA_GET | TTA_SURE); 1268d7fd32ecSMarek Vasut regmap_write(tc->regmap, PPI_LPTXTIMECNT, LPX_PERIOD); 1269d7fd32ecSMarek Vasut 12705bdaaf4fSMarek Vasut value = ((LANEENABLE_L0EN << tc->dsi->lanes) - LANEENABLE_L0EN) | 1271d7fd32ecSMarek Vasut LANEENABLE_CLEN; 1272d7fd32ecSMarek Vasut regmap_write(tc->regmap, PPI_LANEENABLE, value); 1273d7fd32ecSMarek Vasut regmap_write(tc->regmap, DSI_LANEENABLE, value); 1274d7fd32ecSMarek Vasut 1275d7fd32ecSMarek Vasut /* Set input interface */ 1276d7fd32ecSMarek Vasut value = DP0_AUDSRC_NO_INPUT; 1277d7fd32ecSMarek Vasut if (tc_test_pattern) 1278d7fd32ecSMarek Vasut value |= DP0_VIDSRC_COLOR_BAR; 1279d7fd32ecSMarek Vasut else 1280d7fd32ecSMarek Vasut value |= DP0_VIDSRC_DSI_RX; 1281d7fd32ecSMarek Vasut ret = regmap_write(tc->regmap, SYSCTRL, value); 1282d7fd32ecSMarek Vasut if (ret) 1283d7fd32ecSMarek Vasut return ret; 1284d7fd32ecSMarek Vasut 1285d7fd32ecSMarek Vasut usleep_range(120, 150); 1286d7fd32ecSMarek Vasut 1287d7fd32ecSMarek Vasut regmap_write(tc->regmap, PPI_STARTPPI, PPI_START_FUNCTION); 1288d7fd32ecSMarek Vasut regmap_write(tc->regmap, DSI_STARTDSI, DSI_RX_START); 1289d7fd32ecSMarek Vasut 1290d7fd32ecSMarek Vasut return 0; 1291d7fd32ecSMarek Vasut } 1292d7fd32ecSMarek Vasut 1293d7fd32ecSMarek Vasut static int tc_dpi_rx_enable(struct tc_data *tc) 1294d7fd32ecSMarek Vasut { 1295d7fd32ecSMarek Vasut u32 value; 1296d7fd32ecSMarek Vasut 1297d7fd32ecSMarek Vasut /* Set input interface */ 1298d7fd32ecSMarek Vasut value = DP0_AUDSRC_NO_INPUT; 1299d7fd32ecSMarek Vasut if (tc_test_pattern) 1300d7fd32ecSMarek Vasut value |= DP0_VIDSRC_COLOR_BAR; 1301d7fd32ecSMarek Vasut else 1302d7fd32ecSMarek Vasut value |= DP0_VIDSRC_DPI_RX; 1303d7fd32ecSMarek Vasut return regmap_write(tc->regmap, SYSCTRL, value); 1304d7fd32ecSMarek Vasut } 1305d7fd32ecSMarek Vasut 1306bbfd3190SMarek Vasut static int tc_dpi_stream_enable(struct tc_data *tc) 1307bbfd3190SMarek Vasut { 1308bbfd3190SMarek Vasut int ret; 1309bbfd3190SMarek Vasut 1310bbfd3190SMarek Vasut dev_dbg(tc->dev, "enable video stream\n"); 1311bbfd3190SMarek Vasut 1312bbfd3190SMarek Vasut /* Setup PLL */ 1313bbfd3190SMarek Vasut ret = tc_set_syspllparam(tc); 1314bbfd3190SMarek Vasut if (ret) 1315bbfd3190SMarek Vasut return ret; 1316bbfd3190SMarek Vasut 1317bbfd3190SMarek Vasut /* 1318bbfd3190SMarek Vasut * Initially PLLs are in bypass. Force PLL parameter update, 1319bbfd3190SMarek Vasut * disable PLL bypass, enable PLL 1320bbfd3190SMarek Vasut */ 1321bbfd3190SMarek Vasut ret = tc_pllupdate(tc, DP0_PLLCTRL); 1322bbfd3190SMarek Vasut if (ret) 1323bbfd3190SMarek Vasut return ret; 1324bbfd3190SMarek Vasut 1325bbfd3190SMarek Vasut ret = tc_pllupdate(tc, DP1_PLLCTRL); 1326bbfd3190SMarek Vasut if (ret) 1327bbfd3190SMarek Vasut return ret; 1328bbfd3190SMarek Vasut 1329bbfd3190SMarek Vasut /* Pixel PLL must always be enabled for DPI mode */ 1330bbfd3190SMarek Vasut ret = tc_pxl_pll_en(tc, clk_get_rate(tc->refclk), 1331bbfd3190SMarek Vasut 1000 * tc->mode.clock); 1332bbfd3190SMarek Vasut if (ret) 1333bbfd3190SMarek Vasut return ret; 1334bbfd3190SMarek Vasut 1335bbfd3190SMarek Vasut ret = tc_set_common_video_mode(tc, &tc->mode); 1336bbfd3190SMarek Vasut if (ret) 1337bbfd3190SMarek Vasut return ret; 1338bbfd3190SMarek Vasut 1339bbfd3190SMarek Vasut ret = tc_set_dpi_video_mode(tc, &tc->mode); 1340bbfd3190SMarek Vasut if (ret) 1341bbfd3190SMarek Vasut return ret; 1342bbfd3190SMarek Vasut 1343d7fd32ecSMarek Vasut return tc_dsi_rx_enable(tc); 1344bbfd3190SMarek Vasut } 1345bbfd3190SMarek Vasut 1346bbfd3190SMarek Vasut static int tc_dpi_stream_disable(struct tc_data *tc) 1347bbfd3190SMarek Vasut { 1348bbfd3190SMarek Vasut dev_dbg(tc->dev, "disable video stream\n"); 1349bbfd3190SMarek Vasut 1350bbfd3190SMarek Vasut tc_pxl_pll_dis(tc); 1351bbfd3190SMarek Vasut 1352bbfd3190SMarek Vasut return 0; 1353bbfd3190SMarek Vasut } 1354bbfd3190SMarek Vasut 1355a219062bSMarek Vasut static int tc_edp_stream_enable(struct tc_data *tc) 13567caff0fcSAndrey Gusakov { 13577caff0fcSAndrey Gusakov int ret; 13587caff0fcSAndrey Gusakov u32 value; 13597caff0fcSAndrey Gusakov 136080d57245STomi Valkeinen dev_dbg(tc->dev, "enable video stream\n"); 13617caff0fcSAndrey Gusakov 13623080c21aSMarek Vasut /* 13633080c21aSMarek Vasut * Pixel PLL must be enabled for DSI input mode and test pattern. 13643080c21aSMarek Vasut * 13653080c21aSMarek Vasut * Per TC9595XBG datasheet Revision 0.1 2018-12-27 Figure 4.18 13663080c21aSMarek Vasut * "Clock Mode Selection and Clock Sources", either Pixel PLL 13673080c21aSMarek Vasut * or DPI_PCLK supplies StrmClk. DPI_PCLK is only available in 13683080c21aSMarek Vasut * case valid Pixel Clock are supplied to the chip DPI input. 13693080c21aSMarek Vasut * In case built-in test pattern is desired OR DSI input mode 13703080c21aSMarek Vasut * is used, DPI_PCLK is not available and thus Pixel PLL must 13713080c21aSMarek Vasut * be used instead. 13723080c21aSMarek Vasut */ 13733080c21aSMarek Vasut if (tc->input_connector_dsi || tc_test_pattern) { 1374bb248368STomi Valkeinen ret = tc_pxl_pll_en(tc, clk_get_rate(tc->refclk), 137546648a3cSTomi Valkeinen 1000 * tc->mode.clock); 1376bb248368STomi Valkeinen if (ret) 13776d0c3831SAndrey Smirnov return ret; 1378bb248368STomi Valkeinen } 1379bb248368STomi Valkeinen 1380aebe58a7SMarek Vasut ret = tc_set_common_video_mode(tc, &tc->mode); 1381aebe58a7SMarek Vasut if (ret) 1382aebe58a7SMarek Vasut return ret; 1383aebe58a7SMarek Vasut 1384aebe58a7SMarek Vasut ret = tc_set_edp_video_mode(tc, &tc->mode); 13855761a259STomi Valkeinen if (ret) 138680d57245STomi Valkeinen return ret; 13875761a259STomi Valkeinen 13885761a259STomi Valkeinen /* Set M/N */ 13895761a259STomi Valkeinen ret = tc_stream_clock_calc(tc); 13905761a259STomi Valkeinen if (ret) 139180d57245STomi Valkeinen return ret; 13925761a259STomi Valkeinen 13937caff0fcSAndrey Gusakov value = VID_MN_GEN | DP_EN; 1394e7dc8d40SThierry Reding if (drm_dp_enhanced_frame_cap(tc->link.dpcd)) 13957caff0fcSAndrey Gusakov value |= EF_EN; 13966d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP0CTL, value); 13976d0c3831SAndrey Smirnov if (ret) 13986d0c3831SAndrey Smirnov return ret; 13997caff0fcSAndrey Gusakov /* 14007caff0fcSAndrey Gusakov * VID_EN assertion should be delayed by at least N * LSCLK 14017caff0fcSAndrey Gusakov * cycles from the time VID_MN_GEN is enabled in order to 14027caff0fcSAndrey Gusakov * generate stable values for VID_M. LSCLK is 270 MHz or 14037caff0fcSAndrey Gusakov * 162 MHz, VID_N is set to 32768 in tc_stream_clock_calc(), 14047caff0fcSAndrey Gusakov * so a delay of at least 203 us should suffice. 14057caff0fcSAndrey Gusakov */ 14067caff0fcSAndrey Gusakov usleep_range(500, 1000); 14077caff0fcSAndrey Gusakov value |= VID_EN; 14086d0c3831SAndrey Smirnov ret = regmap_write(tc->regmap, DP0CTL, value); 14096d0c3831SAndrey Smirnov if (ret) 14106d0c3831SAndrey Smirnov return ret; 141180d57245STomi Valkeinen 1412d7fd32ecSMarek Vasut /* Set input interface */ 14133080c21aSMarek Vasut if (tc->input_connector_dsi) 14143080c21aSMarek Vasut return tc_dsi_rx_enable(tc); 14153080c21aSMarek Vasut else 1416d7fd32ecSMarek Vasut return tc_dpi_rx_enable(tc); 14177caff0fcSAndrey Gusakov } 14187caff0fcSAndrey Gusakov 1419a219062bSMarek Vasut static int tc_edp_stream_disable(struct tc_data *tc) 142080d57245STomi Valkeinen { 142180d57245STomi Valkeinen int ret; 142280d57245STomi Valkeinen 142380d57245STomi Valkeinen dev_dbg(tc->dev, "disable video stream\n"); 142480d57245STomi Valkeinen 14256d0c3831SAndrey Smirnov ret = regmap_update_bits(tc->regmap, DP0CTL, VID_EN, 0); 14266d0c3831SAndrey Smirnov if (ret) 14276d0c3831SAndrey Smirnov return ret; 142880d57245STomi Valkeinen 1429bb248368STomi Valkeinen tc_pxl_pll_dis(tc); 1430bb248368STomi Valkeinen 14317caff0fcSAndrey Gusakov return 0; 14327caff0fcSAndrey Gusakov } 14337caff0fcSAndrey Gusakov 1434f5be6239SMarek Vasut static void 1435bbfd3190SMarek Vasut tc_dpi_bridge_atomic_enable(struct drm_bridge *bridge, 1436bbfd3190SMarek Vasut struct drm_bridge_state *old_bridge_state) 1437bbfd3190SMarek Vasut 1438bbfd3190SMarek Vasut { 1439bbfd3190SMarek Vasut struct tc_data *tc = bridge_to_tc(bridge); 1440bbfd3190SMarek Vasut int ret; 1441bbfd3190SMarek Vasut 1442bbfd3190SMarek Vasut ret = tc_dpi_stream_enable(tc); 1443bbfd3190SMarek Vasut if (ret < 0) { 1444bbfd3190SMarek Vasut dev_err(tc->dev, "main link stream start error: %d\n", ret); 1445bbfd3190SMarek Vasut tc_main_link_disable(tc); 1446bbfd3190SMarek Vasut return; 1447bbfd3190SMarek Vasut } 1448bbfd3190SMarek Vasut } 1449bbfd3190SMarek Vasut 1450bbfd3190SMarek Vasut static void 1451bbfd3190SMarek Vasut tc_dpi_bridge_atomic_disable(struct drm_bridge *bridge, 1452bbfd3190SMarek Vasut struct drm_bridge_state *old_bridge_state) 1453bbfd3190SMarek Vasut { 1454bbfd3190SMarek Vasut struct tc_data *tc = bridge_to_tc(bridge); 1455bbfd3190SMarek Vasut int ret; 1456bbfd3190SMarek Vasut 1457bbfd3190SMarek Vasut ret = tc_dpi_stream_disable(tc); 1458bbfd3190SMarek Vasut if (ret < 0) 1459bbfd3190SMarek Vasut dev_err(tc->dev, "main link stream stop error: %d\n", ret); 1460bbfd3190SMarek Vasut } 1461bbfd3190SMarek Vasut 1462bbfd3190SMarek Vasut static void 1463f5be6239SMarek Vasut tc_edp_bridge_atomic_enable(struct drm_bridge *bridge, 1464f5be6239SMarek Vasut struct drm_bridge_state *old_bridge_state) 14657caff0fcSAndrey Gusakov { 14667caff0fcSAndrey Gusakov struct tc_data *tc = bridge_to_tc(bridge); 14677caff0fcSAndrey Gusakov int ret; 14687caff0fcSAndrey Gusakov 1469f25ee501STomi Valkeinen ret = tc_get_display_props(tc); 1470f25ee501STomi Valkeinen if (ret < 0) { 1471f25ee501STomi Valkeinen dev_err(tc->dev, "failed to read display props: %d\n", ret); 1472f25ee501STomi Valkeinen return; 1473f25ee501STomi Valkeinen } 1474f25ee501STomi Valkeinen 1475cb3263b2STomi Valkeinen ret = tc_main_link_enable(tc); 14767caff0fcSAndrey Gusakov if (ret < 0) { 1477cb3263b2STomi Valkeinen dev_err(tc->dev, "main link enable error: %d\n", ret); 14787caff0fcSAndrey Gusakov return; 14797caff0fcSAndrey Gusakov } 14807caff0fcSAndrey Gusakov 1481a219062bSMarek Vasut ret = tc_edp_stream_enable(tc); 14827caff0fcSAndrey Gusakov if (ret < 0) { 14837caff0fcSAndrey Gusakov dev_err(tc->dev, "main link stream start error: %d\n", ret); 1484cb3263b2STomi Valkeinen tc_main_link_disable(tc); 14857caff0fcSAndrey Gusakov return; 14867caff0fcSAndrey Gusakov } 14877caff0fcSAndrey Gusakov } 14887caff0fcSAndrey Gusakov 1489f5be6239SMarek Vasut static void 1490f5be6239SMarek Vasut tc_edp_bridge_atomic_disable(struct drm_bridge *bridge, 1491f5be6239SMarek Vasut struct drm_bridge_state *old_bridge_state) 14927caff0fcSAndrey Gusakov { 14937caff0fcSAndrey Gusakov struct tc_data *tc = bridge_to_tc(bridge); 14947caff0fcSAndrey Gusakov int ret; 14957caff0fcSAndrey Gusakov 1496a219062bSMarek Vasut ret = tc_edp_stream_disable(tc); 14977caff0fcSAndrey Gusakov if (ret < 0) 14987caff0fcSAndrey Gusakov dev_err(tc->dev, "main link stream stop error: %d\n", ret); 1499cb3263b2STomi Valkeinen 1500cb3263b2STomi Valkeinen ret = tc_main_link_disable(tc); 1501cb3263b2STomi Valkeinen if (ret < 0) 1502cb3263b2STomi Valkeinen dev_err(tc->dev, "main link disable error: %d\n", ret); 15037caff0fcSAndrey Gusakov } 15047caff0fcSAndrey Gusakov 1505bbfd3190SMarek Vasut static int tc_dpi_atomic_check(struct drm_bridge *bridge, 1506bbfd3190SMarek Vasut struct drm_bridge_state *bridge_state, 1507bbfd3190SMarek Vasut struct drm_crtc_state *crtc_state, 1508bbfd3190SMarek Vasut struct drm_connector_state *conn_state) 1509bbfd3190SMarek Vasut { 1510bbfd3190SMarek Vasut /* DSI->DPI interface clock limitation: upto 100 MHz */ 1511*5fa9e161SLucas Stach if (crtc_state->adjusted_mode.clock > 100000) 1512*5fa9e161SLucas Stach return -EINVAL; 1513*5fa9e161SLucas Stach 1514*5fa9e161SLucas Stach return 0; 1515bbfd3190SMarek Vasut } 1516bbfd3190SMarek Vasut 151765fdbb71SMarek Vasut static int tc_edp_atomic_check(struct drm_bridge *bridge, 151865fdbb71SMarek Vasut struct drm_bridge_state *bridge_state, 151965fdbb71SMarek Vasut struct drm_crtc_state *crtc_state, 152065fdbb71SMarek Vasut struct drm_connector_state *conn_state) 152165fdbb71SMarek Vasut { 152265fdbb71SMarek Vasut /* DPI->(e)DP interface clock limitation: upto 154 MHz */ 1523*5fa9e161SLucas Stach if (crtc_state->adjusted_mode.clock > 154000) 1524*5fa9e161SLucas Stach return -EINVAL; 1525*5fa9e161SLucas Stach 1526*5fa9e161SLucas Stach return 0; 152765fdbb71SMarek Vasut } 152865fdbb71SMarek Vasut 1529a219062bSMarek Vasut static enum drm_mode_status 1530bbfd3190SMarek Vasut tc_dpi_mode_valid(struct drm_bridge *bridge, 1531bbfd3190SMarek Vasut const struct drm_display_info *info, 1532bbfd3190SMarek Vasut const struct drm_display_mode *mode) 1533bbfd3190SMarek Vasut { 1534bbfd3190SMarek Vasut /* DPI interface clock limitation: upto 100 MHz */ 1535bbfd3190SMarek Vasut if (mode->clock > 100000) 1536bbfd3190SMarek Vasut return MODE_CLOCK_HIGH; 1537bbfd3190SMarek Vasut 1538bbfd3190SMarek Vasut return MODE_OK; 1539bbfd3190SMarek Vasut } 1540bbfd3190SMarek Vasut 1541bbfd3190SMarek Vasut static enum drm_mode_status 1542a219062bSMarek Vasut tc_edp_mode_valid(struct drm_bridge *bridge, 154312c683e1SLaurent Pinchart const struct drm_display_info *info, 15444647a64fSTomi Valkeinen const struct drm_display_mode *mode) 15457caff0fcSAndrey Gusakov { 15464647a64fSTomi Valkeinen struct tc_data *tc = bridge_to_tc(bridge); 154751b9e62eSTomi Valkeinen u32 req, avail; 154851b9e62eSTomi Valkeinen u32 bits_per_pixel = 24; 154951b9e62eSTomi Valkeinen 155099fc8e96SAndrey Gusakov /* DPI interface clock limitation: upto 154 MHz */ 155199fc8e96SAndrey Gusakov if (mode->clock > 154000) 155299fc8e96SAndrey Gusakov return MODE_CLOCK_HIGH; 155399fc8e96SAndrey Gusakov 155451b9e62eSTomi Valkeinen req = mode->clock * bits_per_pixel / 8; 1555e7dc8d40SThierry Reding avail = tc->link.num_lanes * tc->link.rate; 155651b9e62eSTomi Valkeinen 155751b9e62eSTomi Valkeinen if (req > avail) 155851b9e62eSTomi Valkeinen return MODE_BAD; 155951b9e62eSTomi Valkeinen 15607caff0fcSAndrey Gusakov return MODE_OK; 15617caff0fcSAndrey Gusakov } 15627caff0fcSAndrey Gusakov 15637caff0fcSAndrey Gusakov static void tc_bridge_mode_set(struct drm_bridge *bridge, 156463f8f3baSLaurent Pinchart const struct drm_display_mode *mode, 156563f8f3baSLaurent Pinchart const struct drm_display_mode *adj) 15667caff0fcSAndrey Gusakov { 15677caff0fcSAndrey Gusakov struct tc_data *tc = bridge_to_tc(bridge); 15687caff0fcSAndrey Gusakov 1569d008bc33SVille Syrjälä drm_mode_copy(&tc->mode, mode); 15707caff0fcSAndrey Gusakov } 15717caff0fcSAndrey Gusakov 1572731f4badSSam Ravnborg static struct edid *tc_get_edid(struct drm_bridge *bridge, 1573731f4badSSam Ravnborg struct drm_connector *connector) 1574731f4badSSam Ravnborg { 1575731f4badSSam Ravnborg struct tc_data *tc = bridge_to_tc(bridge); 1576731f4badSSam Ravnborg 1577731f4badSSam Ravnborg return drm_get_edid(connector, &tc->aux.ddc); 1578731f4badSSam Ravnborg } 1579731f4badSSam Ravnborg 15807caff0fcSAndrey Gusakov static int tc_connector_get_modes(struct drm_connector *connector) 15817caff0fcSAndrey Gusakov { 15827caff0fcSAndrey Gusakov struct tc_data *tc = connector_to_tc(connector); 1583731f4badSSam Ravnborg int num_modes; 15847caff0fcSAndrey Gusakov struct edid *edid; 158532315730STomi Valkeinen int ret; 158632315730STomi Valkeinen 158732315730STomi Valkeinen ret = tc_get_display_props(tc); 158832315730STomi Valkeinen if (ret < 0) { 158932315730STomi Valkeinen dev_err(tc->dev, "failed to read display props: %d\n", ret); 159032315730STomi Valkeinen return 0; 159132315730STomi Valkeinen } 15927caff0fcSAndrey Gusakov 1593de5e6c02SSam Ravnborg if (tc->panel_bridge) { 1594de5e6c02SSam Ravnborg num_modes = drm_bridge_get_modes(tc->panel_bridge, connector); 1595731f4badSSam Ravnborg if (num_modes > 0) 1596731f4badSSam Ravnborg return num_modes; 1597de5e6c02SSam Ravnborg } 15987caff0fcSAndrey Gusakov 1599731f4badSSam Ravnborg edid = tc_get_edid(&tc->bridge, connector); 1600731f4badSSam Ravnborg num_modes = drm_add_edid_modes(connector, edid); 1601731f4badSSam Ravnborg kfree(edid); 16027caff0fcSAndrey Gusakov 1603731f4badSSam Ravnborg return num_modes; 16047caff0fcSAndrey Gusakov } 16057caff0fcSAndrey Gusakov 16067caff0fcSAndrey Gusakov static const struct drm_connector_helper_funcs tc_connector_helper_funcs = { 16077caff0fcSAndrey Gusakov .get_modes = tc_connector_get_modes, 16087caff0fcSAndrey Gusakov }; 16097caff0fcSAndrey Gusakov 1610136d73a8SSam Ravnborg static enum drm_connector_status tc_bridge_detect(struct drm_bridge *bridge) 1611f25ee501STomi Valkeinen { 1612136d73a8SSam Ravnborg struct tc_data *tc = bridge_to_tc(bridge); 1613f25ee501STomi Valkeinen bool conn; 1614f25ee501STomi Valkeinen u32 val; 1615f25ee501STomi Valkeinen int ret; 1616f25ee501STomi Valkeinen 16176d0c3831SAndrey Smirnov ret = regmap_read(tc->regmap, GPIOI, &val); 16186d0c3831SAndrey Smirnov if (ret) 16196d0c3831SAndrey Smirnov return connector_status_unknown; 1620f25ee501STomi Valkeinen 1621f25ee501STomi Valkeinen conn = val & BIT(tc->hpd_pin); 1622f25ee501STomi Valkeinen 1623f25ee501STomi Valkeinen if (conn) 1624f25ee501STomi Valkeinen return connector_status_connected; 1625f25ee501STomi Valkeinen else 1626f25ee501STomi Valkeinen return connector_status_disconnected; 1627f25ee501STomi Valkeinen } 1628f25ee501STomi Valkeinen 1629136d73a8SSam Ravnborg static enum drm_connector_status 1630136d73a8SSam Ravnborg tc_connector_detect(struct drm_connector *connector, bool force) 1631136d73a8SSam Ravnborg { 1632136d73a8SSam Ravnborg struct tc_data *tc = connector_to_tc(connector); 1633136d73a8SSam Ravnborg 1634136d73a8SSam Ravnborg if (tc->hpd_pin >= 0) 1635136d73a8SSam Ravnborg return tc_bridge_detect(&tc->bridge); 1636136d73a8SSam Ravnborg 1637de5e6c02SSam Ravnborg if (tc->panel_bridge) 1638136d73a8SSam Ravnborg return connector_status_connected; 1639136d73a8SSam Ravnborg else 1640136d73a8SSam Ravnborg return connector_status_unknown; 1641136d73a8SSam Ravnborg } 1642136d73a8SSam Ravnborg 16437caff0fcSAndrey Gusakov static const struct drm_connector_funcs tc_connector_funcs = { 1644f25ee501STomi Valkeinen .detect = tc_connector_detect, 16457caff0fcSAndrey Gusakov .fill_modes = drm_helper_probe_single_connector_modes, 1646fdd8326aSMarek Vasut .destroy = drm_connector_cleanup, 16477caff0fcSAndrey Gusakov .reset = drm_atomic_helper_connector_reset, 16487caff0fcSAndrey Gusakov .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 16497caff0fcSAndrey Gusakov .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 16507caff0fcSAndrey Gusakov }; 16517caff0fcSAndrey Gusakov 1652bbfd3190SMarek Vasut static int tc_dpi_bridge_attach(struct drm_bridge *bridge, 1653bbfd3190SMarek Vasut enum drm_bridge_attach_flags flags) 1654bbfd3190SMarek Vasut { 1655bbfd3190SMarek Vasut struct tc_data *tc = bridge_to_tc(bridge); 1656bbfd3190SMarek Vasut 1657bbfd3190SMarek Vasut if (!tc->panel_bridge) 1658bbfd3190SMarek Vasut return 0; 1659bbfd3190SMarek Vasut 1660bbfd3190SMarek Vasut return drm_bridge_attach(tc->bridge.encoder, tc->panel_bridge, 1661bbfd3190SMarek Vasut &tc->bridge, flags); 1662bbfd3190SMarek Vasut } 1663bbfd3190SMarek Vasut 1664a219062bSMarek Vasut static int tc_edp_bridge_attach(struct drm_bridge *bridge, 1665a25b988fSLaurent Pinchart enum drm_bridge_attach_flags flags) 16667caff0fcSAndrey Gusakov { 16677caff0fcSAndrey Gusakov u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24; 16687caff0fcSAndrey Gusakov struct tc_data *tc = bridge_to_tc(bridge); 16697caff0fcSAndrey Gusakov struct drm_device *drm = bridge->dev; 16707caff0fcSAndrey Gusakov int ret; 16717caff0fcSAndrey Gusakov 1672de5e6c02SSam Ravnborg if (tc->panel_bridge) { 1673de5e6c02SSam Ravnborg /* If a connector is required then this driver shall create it */ 1674de5e6c02SSam Ravnborg ret = drm_bridge_attach(tc->bridge.encoder, tc->panel_bridge, 1675de5e6c02SSam Ravnborg &tc->bridge, flags | DRM_BRIDGE_ATTACH_NO_CONNECTOR); 1676de5e6c02SSam Ravnborg if (ret) 1677de5e6c02SSam Ravnborg return ret; 1678a25b988fSLaurent Pinchart } 1679a25b988fSLaurent Pinchart 1680de5e6c02SSam Ravnborg if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR) 1681de5e6c02SSam Ravnborg return 0; 1682de5e6c02SSam Ravnborg 16836cba3fe4SLyude Paul tc->aux.drm_dev = drm; 168485ddbe2cSLyude Paul ret = drm_dp_aux_register(&tc->aux); 168585ddbe2cSLyude Paul if (ret < 0) 168685ddbe2cSLyude Paul return ret; 168785ddbe2cSLyude Paul 1688f25ee501STomi Valkeinen /* Create DP/eDP connector */ 16897caff0fcSAndrey Gusakov drm_connector_helper_add(&tc->connector, &tc_connector_helper_funcs); 1690de5e6c02SSam Ravnborg ret = drm_connector_init(drm, &tc->connector, &tc_connector_funcs, tc->bridge.type); 16917caff0fcSAndrey Gusakov if (ret) 169285ddbe2cSLyude Paul goto aux_unregister; 16937caff0fcSAndrey Gusakov 1694f25ee501STomi Valkeinen /* Don't poll if don't have HPD connected */ 1695f25ee501STomi Valkeinen if (tc->hpd_pin >= 0) { 1696f25ee501STomi Valkeinen if (tc->have_irq) 1697f25ee501STomi Valkeinen tc->connector.polled = DRM_CONNECTOR_POLL_HPD; 1698f25ee501STomi Valkeinen else 1699f25ee501STomi Valkeinen tc->connector.polled = DRM_CONNECTOR_POLL_CONNECT | 1700f25ee501STomi Valkeinen DRM_CONNECTOR_POLL_DISCONNECT; 1701f25ee501STomi Valkeinen } 1702f25ee501STomi Valkeinen 17037caff0fcSAndrey Gusakov drm_display_info_set_bus_formats(&tc->connector.display_info, 17047caff0fcSAndrey Gusakov &bus_format, 1); 17054842379cSTomi Valkeinen tc->connector.display_info.bus_flags = 17064842379cSTomi Valkeinen DRM_BUS_FLAG_DE_HIGH | 170788bc4178SLaurent Pinchart DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE | 170888bc4178SLaurent Pinchart DRM_BUS_FLAG_SYNC_DRIVE_NEGEDGE; 1709cde4c44dSDaniel Vetter drm_connector_attach_encoder(&tc->connector, tc->bridge.encoder); 17107caff0fcSAndrey Gusakov 17117caff0fcSAndrey Gusakov return 0; 171285ddbe2cSLyude Paul aux_unregister: 171385ddbe2cSLyude Paul drm_dp_aux_unregister(&tc->aux); 171485ddbe2cSLyude Paul return ret; 171585ddbe2cSLyude Paul } 171685ddbe2cSLyude Paul 1717a219062bSMarek Vasut static void tc_edp_bridge_detach(struct drm_bridge *bridge) 171885ddbe2cSLyude Paul { 171985ddbe2cSLyude Paul drm_dp_aux_unregister(&bridge_to_tc(bridge)->aux); 17207caff0fcSAndrey Gusakov } 17217caff0fcSAndrey Gusakov 1722bbfd3190SMarek Vasut #define MAX_INPUT_SEL_FORMATS 1 1723bbfd3190SMarek Vasut 1724bbfd3190SMarek Vasut static u32 * 1725bbfd3190SMarek Vasut tc_dpi_atomic_get_input_bus_fmts(struct drm_bridge *bridge, 1726bbfd3190SMarek Vasut struct drm_bridge_state *bridge_state, 1727bbfd3190SMarek Vasut struct drm_crtc_state *crtc_state, 1728bbfd3190SMarek Vasut struct drm_connector_state *conn_state, 1729bbfd3190SMarek Vasut u32 output_fmt, 1730bbfd3190SMarek Vasut unsigned int *num_input_fmts) 1731bbfd3190SMarek Vasut { 1732bbfd3190SMarek Vasut u32 *input_fmts; 1733bbfd3190SMarek Vasut 1734bbfd3190SMarek Vasut *num_input_fmts = 0; 1735bbfd3190SMarek Vasut 1736bbfd3190SMarek Vasut input_fmts = kcalloc(MAX_INPUT_SEL_FORMATS, sizeof(*input_fmts), 1737bbfd3190SMarek Vasut GFP_KERNEL); 1738bbfd3190SMarek Vasut if (!input_fmts) 1739bbfd3190SMarek Vasut return NULL; 1740bbfd3190SMarek Vasut 1741bbfd3190SMarek Vasut /* This is the DSI-end bus format */ 1742bbfd3190SMarek Vasut input_fmts[0] = MEDIA_BUS_FMT_RGB888_1X24; 1743bbfd3190SMarek Vasut *num_input_fmts = 1; 1744bbfd3190SMarek Vasut 1745bbfd3190SMarek Vasut return input_fmts; 1746bbfd3190SMarek Vasut } 1747bbfd3190SMarek Vasut 1748bbfd3190SMarek Vasut static const struct drm_bridge_funcs tc_dpi_bridge_funcs = { 1749bbfd3190SMarek Vasut .attach = tc_dpi_bridge_attach, 1750bbfd3190SMarek Vasut .mode_valid = tc_dpi_mode_valid, 1751bbfd3190SMarek Vasut .mode_set = tc_bridge_mode_set, 1752bbfd3190SMarek Vasut .atomic_check = tc_dpi_atomic_check, 1753bbfd3190SMarek Vasut .atomic_enable = tc_dpi_bridge_atomic_enable, 1754bbfd3190SMarek Vasut .atomic_disable = tc_dpi_bridge_atomic_disable, 1755bbfd3190SMarek Vasut .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state, 1756bbfd3190SMarek Vasut .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state, 1757bbfd3190SMarek Vasut .atomic_reset = drm_atomic_helper_bridge_reset, 1758bbfd3190SMarek Vasut .atomic_get_input_bus_fmts = tc_dpi_atomic_get_input_bus_fmts, 1759bbfd3190SMarek Vasut }; 1760bbfd3190SMarek Vasut 1761a219062bSMarek Vasut static const struct drm_bridge_funcs tc_edp_bridge_funcs = { 1762a219062bSMarek Vasut .attach = tc_edp_bridge_attach, 1763a219062bSMarek Vasut .detach = tc_edp_bridge_detach, 1764a219062bSMarek Vasut .mode_valid = tc_edp_mode_valid, 17657caff0fcSAndrey Gusakov .mode_set = tc_bridge_mode_set, 176665fdbb71SMarek Vasut .atomic_check = tc_edp_atomic_check, 1767f5be6239SMarek Vasut .atomic_enable = tc_edp_bridge_atomic_enable, 1768f5be6239SMarek Vasut .atomic_disable = tc_edp_bridge_atomic_disable, 1769136d73a8SSam Ravnborg .detect = tc_bridge_detect, 1770731f4badSSam Ravnborg .get_edid = tc_get_edid, 1771f5be6239SMarek Vasut .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state, 1772f5be6239SMarek Vasut .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state, 1773f5be6239SMarek Vasut .atomic_reset = drm_atomic_helper_bridge_reset, 17747caff0fcSAndrey Gusakov }; 17757caff0fcSAndrey Gusakov 17767caff0fcSAndrey Gusakov static bool tc_readable_reg(struct device *dev, unsigned int reg) 17777caff0fcSAndrey Gusakov { 17787caff0fcSAndrey Gusakov return reg != SYSCTRL; 17797caff0fcSAndrey Gusakov } 17807caff0fcSAndrey Gusakov 17817caff0fcSAndrey Gusakov static const struct regmap_range tc_volatile_ranges[] = { 17827caff0fcSAndrey Gusakov regmap_reg_range(DP0_AUXWDATA(0), DP0_AUXSTATUS), 17837caff0fcSAndrey Gusakov regmap_reg_range(DP0_LTSTAT, DP0_SNKLTCHGREQ), 17847caff0fcSAndrey Gusakov regmap_reg_range(DP_PHY_CTRL, DP_PHY_CTRL), 17857caff0fcSAndrey Gusakov regmap_reg_range(DP0_PLLCTRL, PXL_PLLCTRL), 17867caff0fcSAndrey Gusakov regmap_reg_range(VFUEN0, VFUEN0), 1787af9526f2STomi Valkeinen regmap_reg_range(INTSTS_G, INTSTS_G), 1788af9526f2STomi Valkeinen regmap_reg_range(GPIOI, GPIOI), 17897caff0fcSAndrey Gusakov }; 17907caff0fcSAndrey Gusakov 17917caff0fcSAndrey Gusakov static const struct regmap_access_table tc_volatile_table = { 17927caff0fcSAndrey Gusakov .yes_ranges = tc_volatile_ranges, 17937caff0fcSAndrey Gusakov .n_yes_ranges = ARRAY_SIZE(tc_volatile_ranges), 17947caff0fcSAndrey Gusakov }; 17957caff0fcSAndrey Gusakov 17967caff0fcSAndrey Gusakov static bool tc_writeable_reg(struct device *dev, unsigned int reg) 17977caff0fcSAndrey Gusakov { 17987caff0fcSAndrey Gusakov return (reg != TC_IDREG) && 17997caff0fcSAndrey Gusakov (reg != DP0_LTSTAT) && 18007caff0fcSAndrey Gusakov (reg != DP0_SNKLTCHGREQ); 18017caff0fcSAndrey Gusakov } 18027caff0fcSAndrey Gusakov 18037caff0fcSAndrey Gusakov static const struct regmap_config tc_regmap_config = { 18047caff0fcSAndrey Gusakov .name = "tc358767", 18057caff0fcSAndrey Gusakov .reg_bits = 16, 18067caff0fcSAndrey Gusakov .val_bits = 32, 18077caff0fcSAndrey Gusakov .reg_stride = 4, 18087caff0fcSAndrey Gusakov .max_register = PLL_DBG, 18097caff0fcSAndrey Gusakov .cache_type = REGCACHE_RBTREE, 18107caff0fcSAndrey Gusakov .readable_reg = tc_readable_reg, 18117caff0fcSAndrey Gusakov .volatile_table = &tc_volatile_table, 18127caff0fcSAndrey Gusakov .writeable_reg = tc_writeable_reg, 18137caff0fcSAndrey Gusakov .reg_format_endian = REGMAP_ENDIAN_BIG, 18147caff0fcSAndrey Gusakov .val_format_endian = REGMAP_ENDIAN_LITTLE, 18157caff0fcSAndrey Gusakov }; 18167caff0fcSAndrey Gusakov 1817f25ee501STomi Valkeinen static irqreturn_t tc_irq_handler(int irq, void *arg) 1818f25ee501STomi Valkeinen { 1819f25ee501STomi Valkeinen struct tc_data *tc = arg; 1820f25ee501STomi Valkeinen u32 val; 1821f25ee501STomi Valkeinen int r; 1822f25ee501STomi Valkeinen 1823f25ee501STomi Valkeinen r = regmap_read(tc->regmap, INTSTS_G, &val); 1824f25ee501STomi Valkeinen if (r) 1825f25ee501STomi Valkeinen return IRQ_NONE; 1826f25ee501STomi Valkeinen 1827f25ee501STomi Valkeinen if (!val) 1828f25ee501STomi Valkeinen return IRQ_NONE; 1829f25ee501STomi Valkeinen 1830f25ee501STomi Valkeinen if (val & INT_SYSERR) { 1831f25ee501STomi Valkeinen u32 stat = 0; 1832f25ee501STomi Valkeinen 1833f25ee501STomi Valkeinen regmap_read(tc->regmap, SYSSTAT, &stat); 1834f25ee501STomi Valkeinen 1835f25ee501STomi Valkeinen dev_err(tc->dev, "syserr %x\n", stat); 1836f25ee501STomi Valkeinen } 1837f25ee501STomi Valkeinen 1838f25ee501STomi Valkeinen if (tc->hpd_pin >= 0 && tc->bridge.dev) { 1839f25ee501STomi Valkeinen /* 1840f25ee501STomi Valkeinen * H is triggered when the GPIO goes high. 1841f25ee501STomi Valkeinen * 1842f25ee501STomi Valkeinen * LC is triggered when the GPIO goes low and stays low for 1843f25ee501STomi Valkeinen * the duration of LCNT 1844f25ee501STomi Valkeinen */ 1845f25ee501STomi Valkeinen bool h = val & INT_GPIO_H(tc->hpd_pin); 1846f25ee501STomi Valkeinen bool lc = val & INT_GPIO_LC(tc->hpd_pin); 1847f25ee501STomi Valkeinen 1848f25ee501STomi Valkeinen dev_dbg(tc->dev, "GPIO%d: %s %s\n", tc->hpd_pin, 1849f25ee501STomi Valkeinen h ? "H" : "", lc ? "LC" : ""); 1850f25ee501STomi Valkeinen 1851f25ee501STomi Valkeinen if (h || lc) 1852f25ee501STomi Valkeinen drm_kms_helper_hotplug_event(tc->bridge.dev); 1853f25ee501STomi Valkeinen } 1854f25ee501STomi Valkeinen 1855f25ee501STomi Valkeinen regmap_write(tc->regmap, INTSTS_G, val); 1856f25ee501STomi Valkeinen 1857f25ee501STomi Valkeinen return IRQ_HANDLED; 1858f25ee501STomi Valkeinen } 1859f25ee501STomi Valkeinen 1860bbfd3190SMarek Vasut static int tc_mipi_dsi_host_attach(struct tc_data *tc) 1861bbfd3190SMarek Vasut { 1862bbfd3190SMarek Vasut struct device *dev = tc->dev; 1863bbfd3190SMarek Vasut struct device_node *host_node; 1864bbfd3190SMarek Vasut struct device_node *endpoint; 1865bbfd3190SMarek Vasut struct mipi_dsi_device *dsi; 1866bbfd3190SMarek Vasut struct mipi_dsi_host *host; 1867bbfd3190SMarek Vasut const struct mipi_dsi_device_info info = { 1868bbfd3190SMarek Vasut .type = "tc358767", 1869bbfd3190SMarek Vasut .channel = 0, 1870bbfd3190SMarek Vasut .node = NULL, 1871bbfd3190SMarek Vasut }; 1872bbfd3190SMarek Vasut int dsi_lanes, ret; 1873bbfd3190SMarek Vasut 1874bbfd3190SMarek Vasut endpoint = of_graph_get_endpoint_by_regs(dev->of_node, 0, -1); 1875d8609fd1SMarek Vasut dsi_lanes = drm_of_get_data_lanes_count(endpoint, 1, 4); 1876bbfd3190SMarek Vasut host_node = of_graph_get_remote_port_parent(endpoint); 1877bbfd3190SMarek Vasut host = of_find_mipi_dsi_host_by_node(host_node); 1878bbfd3190SMarek Vasut of_node_put(host_node); 1879bbfd3190SMarek Vasut of_node_put(endpoint); 1880bbfd3190SMarek Vasut 1881bbfd3190SMarek Vasut if (!host) 1882bbfd3190SMarek Vasut return -EPROBE_DEFER; 1883bbfd3190SMarek Vasut 1884d8609fd1SMarek Vasut if (dsi_lanes < 0) 1885d8609fd1SMarek Vasut return dsi_lanes; 1886d8609fd1SMarek Vasut 1887bbfd3190SMarek Vasut dsi = mipi_dsi_device_register_full(host, &info); 1888bbfd3190SMarek Vasut if (IS_ERR(dsi)) 1889bbfd3190SMarek Vasut return dev_err_probe(dev, PTR_ERR(dsi), 1890bbfd3190SMarek Vasut "failed to create dsi device\n"); 1891bbfd3190SMarek Vasut 1892bbfd3190SMarek Vasut tc->dsi = dsi; 1893bbfd3190SMarek Vasut 18945bdaaf4fSMarek Vasut dsi->lanes = dsi_lanes; 1895bbfd3190SMarek Vasut dsi->format = MIPI_DSI_FMT_RGB888; 1896bbfd3190SMarek Vasut dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE; 1897bbfd3190SMarek Vasut 1898bbfd3190SMarek Vasut ret = mipi_dsi_attach(dsi); 1899bbfd3190SMarek Vasut if (ret < 0) { 1900bbfd3190SMarek Vasut dev_err(dev, "failed to attach dsi to host: %d\n", ret); 1901bbfd3190SMarek Vasut return ret; 1902bbfd3190SMarek Vasut } 1903bbfd3190SMarek Vasut 1904bbfd3190SMarek Vasut return 0; 1905bbfd3190SMarek Vasut } 1906bbfd3190SMarek Vasut 1907bbfd3190SMarek Vasut static int tc_probe_dpi_bridge_endpoint(struct tc_data *tc) 1908bbfd3190SMarek Vasut { 1909bbfd3190SMarek Vasut struct device *dev = tc->dev; 1910bbfd3190SMarek Vasut struct drm_panel *panel; 1911bbfd3190SMarek Vasut int ret; 1912bbfd3190SMarek Vasut 1913bbfd3190SMarek Vasut /* port@1 is the DPI input/output port */ 1914bbfd3190SMarek Vasut ret = drm_of_find_panel_or_bridge(dev->of_node, 1, 0, &panel, NULL); 1915bbfd3190SMarek Vasut if (ret && ret != -ENODEV) 1916bbfd3190SMarek Vasut return ret; 1917bbfd3190SMarek Vasut 1918bbfd3190SMarek Vasut if (panel) { 1919bbfd3190SMarek Vasut struct drm_bridge *panel_bridge; 1920bbfd3190SMarek Vasut 1921bbfd3190SMarek Vasut panel_bridge = devm_drm_panel_bridge_add(dev, panel); 1922bbfd3190SMarek Vasut if (IS_ERR(panel_bridge)) 1923bbfd3190SMarek Vasut return PTR_ERR(panel_bridge); 1924bbfd3190SMarek Vasut 1925bbfd3190SMarek Vasut tc->panel_bridge = panel_bridge; 1926bbfd3190SMarek Vasut tc->bridge.type = DRM_MODE_CONNECTOR_DPI; 1927bbfd3190SMarek Vasut tc->bridge.funcs = &tc_dpi_bridge_funcs; 1928bbfd3190SMarek Vasut 1929bbfd3190SMarek Vasut return 0; 1930bbfd3190SMarek Vasut } 1931bbfd3190SMarek Vasut 1932bbfd3190SMarek Vasut return ret; 1933bbfd3190SMarek Vasut } 1934bbfd3190SMarek Vasut 19358478095aSMarek Vasut static int tc_probe_edp_bridge_endpoint(struct tc_data *tc) 19367caff0fcSAndrey Gusakov { 19378478095aSMarek Vasut struct device *dev = tc->dev; 1938de5e6c02SSam Ravnborg struct drm_panel *panel; 19397caff0fcSAndrey Gusakov int ret; 19407caff0fcSAndrey Gusakov 19417caff0fcSAndrey Gusakov /* port@2 is the output port */ 1942de5e6c02SSam Ravnborg ret = drm_of_find_panel_or_bridge(dev->of_node, 2, 0, &panel, NULL); 1943d630213fSLucas Stach if (ret && ret != -ENODEV) 1944ebc94461SRob Herring return ret; 19457caff0fcSAndrey Gusakov 1946de5e6c02SSam Ravnborg if (panel) { 1947de5e6c02SSam Ravnborg struct drm_bridge *panel_bridge; 1948de5e6c02SSam Ravnborg 1949de5e6c02SSam Ravnborg panel_bridge = devm_drm_panel_bridge_add(dev, panel); 1950de5e6c02SSam Ravnborg if (IS_ERR(panel_bridge)) 1951de5e6c02SSam Ravnborg return PTR_ERR(panel_bridge); 1952de5e6c02SSam Ravnborg 1953de5e6c02SSam Ravnborg tc->panel_bridge = panel_bridge; 1954de5e6c02SSam Ravnborg tc->bridge.type = DRM_MODE_CONNECTOR_eDP; 1955de5e6c02SSam Ravnborg } else { 1956de5e6c02SSam Ravnborg tc->bridge.type = DRM_MODE_CONNECTOR_DisplayPort; 1957de5e6c02SSam Ravnborg } 1958de5e6c02SSam Ravnborg 1959dd1fd5abSMarek Vasut tc->bridge.funcs = &tc_edp_bridge_funcs; 1960dd1fd5abSMarek Vasut if (tc->hpd_pin >= 0) 1961dd1fd5abSMarek Vasut tc->bridge.ops |= DRM_BRIDGE_OP_DETECT; 1962dd1fd5abSMarek Vasut tc->bridge.ops |= DRM_BRIDGE_OP_EDID; 1963dd1fd5abSMarek Vasut 19649030a9e5SMarek Vasut return 0; 19658478095aSMarek Vasut } 19668478095aSMarek Vasut 196771f7d9c0SMarek Vasut static int tc_probe_bridge_endpoint(struct tc_data *tc) 196871f7d9c0SMarek Vasut { 196971f7d9c0SMarek Vasut struct device *dev = tc->dev; 197071f7d9c0SMarek Vasut struct of_endpoint endpoint; 197171f7d9c0SMarek Vasut struct device_node *node = NULL; 197271f7d9c0SMarek Vasut const u8 mode_dpi_to_edp = BIT(1) | BIT(2); 19731bb533b6SMarek Vasut const u8 mode_dpi_to_dp = BIT(1); 197471f7d9c0SMarek Vasut const u8 mode_dsi_to_edp = BIT(0) | BIT(2); 19751bb533b6SMarek Vasut const u8 mode_dsi_to_dp = BIT(0); 197671f7d9c0SMarek Vasut const u8 mode_dsi_to_dpi = BIT(0) | BIT(1); 197771f7d9c0SMarek Vasut u8 mode = 0; 197871f7d9c0SMarek Vasut 197971f7d9c0SMarek Vasut /* 198071f7d9c0SMarek Vasut * Determine bridge configuration. 198171f7d9c0SMarek Vasut * 198271f7d9c0SMarek Vasut * Port allocation: 198371f7d9c0SMarek Vasut * port@0 - DSI input 198471f7d9c0SMarek Vasut * port@1 - DPI input/output 198571f7d9c0SMarek Vasut * port@2 - eDP output 198671f7d9c0SMarek Vasut * 198771f7d9c0SMarek Vasut * Possible connections: 198871f7d9c0SMarek Vasut * DPI -> port@1 -> port@2 -> eDP :: [port@0 is not connected] 198971f7d9c0SMarek Vasut * DSI -> port@0 -> port@2 -> eDP :: [port@1 is not connected] 199071f7d9c0SMarek Vasut * DSI -> port@0 -> port@1 -> DPI :: [port@2 is not connected] 199171f7d9c0SMarek Vasut */ 199271f7d9c0SMarek Vasut 199371f7d9c0SMarek Vasut for_each_endpoint_of_node(dev->of_node, node) { 199471f7d9c0SMarek Vasut of_graph_parse_endpoint(node, &endpoint); 199514e7157aSLiang He if (endpoint.port > 2) { 199614e7157aSLiang He of_node_put(node); 199771f7d9c0SMarek Vasut return -EINVAL; 199814e7157aSLiang He } 199971f7d9c0SMarek Vasut mode |= BIT(endpoint.port); 200071f7d9c0SMarek Vasut } 200171f7d9c0SMarek Vasut 20023080c21aSMarek Vasut if (mode == mode_dpi_to_edp || mode == mode_dpi_to_dp) { 20033080c21aSMarek Vasut tc->input_connector_dsi = false; 200471f7d9c0SMarek Vasut return tc_probe_edp_bridge_endpoint(tc); 20053080c21aSMarek Vasut } else if (mode == mode_dsi_to_dpi) { 20063080c21aSMarek Vasut tc->input_connector_dsi = true; 2007bbfd3190SMarek Vasut return tc_probe_dpi_bridge_endpoint(tc); 20083080c21aSMarek Vasut } else if (mode == mode_dsi_to_edp || mode == mode_dsi_to_dp) { 20093080c21aSMarek Vasut tc->input_connector_dsi = true; 20103080c21aSMarek Vasut return tc_probe_edp_bridge_endpoint(tc); 20113080c21aSMarek Vasut } 20123080c21aSMarek Vasut 201371f7d9c0SMarek Vasut dev_warn(dev, "Invalid mode (0x%x) is not supported!\n", mode); 201471f7d9c0SMarek Vasut 201571f7d9c0SMarek Vasut return -EINVAL; 201671f7d9c0SMarek Vasut } 201771f7d9c0SMarek Vasut 20180b4c48f3SMarek Vasut static void tc_clk_disable(void *data) 20190b4c48f3SMarek Vasut { 20200b4c48f3SMarek Vasut struct clk *refclk = data; 20210b4c48f3SMarek Vasut 20220b4c48f3SMarek Vasut clk_disable_unprepare(refclk); 20230b4c48f3SMarek Vasut } 20240b4c48f3SMarek Vasut 20258478095aSMarek Vasut static int tc_probe(struct i2c_client *client, const struct i2c_device_id *id) 20268478095aSMarek Vasut { 20278478095aSMarek Vasut struct device *dev = &client->dev; 20288478095aSMarek Vasut struct tc_data *tc; 20298478095aSMarek Vasut int ret; 20308478095aSMarek Vasut 20318478095aSMarek Vasut tc = devm_kzalloc(dev, sizeof(*tc), GFP_KERNEL); 20328478095aSMarek Vasut if (!tc) 20338478095aSMarek Vasut return -ENOMEM; 20348478095aSMarek Vasut 20358478095aSMarek Vasut tc->dev = dev; 20368478095aSMarek Vasut 203771f7d9c0SMarek Vasut ret = tc_probe_bridge_endpoint(tc); 20388478095aSMarek Vasut if (ret) 20398478095aSMarek Vasut return ret; 20408478095aSMarek Vasut 20410b4c48f3SMarek Vasut tc->refclk = devm_clk_get(dev, "ref"); 20420b4c48f3SMarek Vasut if (IS_ERR(tc->refclk)) { 20430b4c48f3SMarek Vasut ret = PTR_ERR(tc->refclk); 20440b4c48f3SMarek Vasut dev_err(dev, "Failed to get refclk: %d\n", ret); 20450b4c48f3SMarek Vasut return ret; 20460b4c48f3SMarek Vasut } 20470b4c48f3SMarek Vasut 20480b4c48f3SMarek Vasut ret = clk_prepare_enable(tc->refclk); 20490b4c48f3SMarek Vasut if (ret) 20500b4c48f3SMarek Vasut return ret; 20510b4c48f3SMarek Vasut 20520b4c48f3SMarek Vasut ret = devm_add_action_or_reset(dev, tc_clk_disable, tc->refclk); 20530b4c48f3SMarek Vasut if (ret) 20540b4c48f3SMarek Vasut return ret; 20550b4c48f3SMarek Vasut 20560b4c48f3SMarek Vasut /* tRSTW = 100 cycles , at 13 MHz that is ~7.69 us */ 20570b4c48f3SMarek Vasut usleep_range(10, 15); 20580b4c48f3SMarek Vasut 20597caff0fcSAndrey Gusakov /* Shut down GPIO is optional */ 20607caff0fcSAndrey Gusakov tc->sd_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH); 20617caff0fcSAndrey Gusakov if (IS_ERR(tc->sd_gpio)) 20627caff0fcSAndrey Gusakov return PTR_ERR(tc->sd_gpio); 20637caff0fcSAndrey Gusakov 20647caff0fcSAndrey Gusakov if (tc->sd_gpio) { 20657caff0fcSAndrey Gusakov gpiod_set_value_cansleep(tc->sd_gpio, 0); 20667caff0fcSAndrey Gusakov usleep_range(5000, 10000); 20677caff0fcSAndrey Gusakov } 20687caff0fcSAndrey Gusakov 20697caff0fcSAndrey Gusakov /* Reset GPIO is optional */ 20707caff0fcSAndrey Gusakov tc->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); 20717caff0fcSAndrey Gusakov if (IS_ERR(tc->reset_gpio)) 20727caff0fcSAndrey Gusakov return PTR_ERR(tc->reset_gpio); 20737caff0fcSAndrey Gusakov 20747caff0fcSAndrey Gusakov if (tc->reset_gpio) { 20757caff0fcSAndrey Gusakov gpiod_set_value_cansleep(tc->reset_gpio, 1); 20767caff0fcSAndrey Gusakov usleep_range(5000, 10000); 20777caff0fcSAndrey Gusakov } 20787caff0fcSAndrey Gusakov 20797caff0fcSAndrey Gusakov tc->regmap = devm_regmap_init_i2c(client, &tc_regmap_config); 20807caff0fcSAndrey Gusakov if (IS_ERR(tc->regmap)) { 20817caff0fcSAndrey Gusakov ret = PTR_ERR(tc->regmap); 20827caff0fcSAndrey Gusakov dev_err(dev, "Failed to initialize regmap: %d\n", ret); 20837caff0fcSAndrey Gusakov return ret; 20847caff0fcSAndrey Gusakov } 20857caff0fcSAndrey Gusakov 2086f25ee501STomi Valkeinen ret = of_property_read_u32(dev->of_node, "toshiba,hpd-pin", 2087f25ee501STomi Valkeinen &tc->hpd_pin); 2088f25ee501STomi Valkeinen if (ret) { 2089f25ee501STomi Valkeinen tc->hpd_pin = -ENODEV; 2090f25ee501STomi Valkeinen } else { 2091f25ee501STomi Valkeinen if (tc->hpd_pin < 0 || tc->hpd_pin > 1) { 2092f25ee501STomi Valkeinen dev_err(dev, "failed to parse HPD number\n"); 2093f25ee501STomi Valkeinen return ret; 2094f25ee501STomi Valkeinen } 2095f25ee501STomi Valkeinen } 2096f25ee501STomi Valkeinen 2097f25ee501STomi Valkeinen if (client->irq > 0) { 2098f25ee501STomi Valkeinen /* enable SysErr */ 2099f25ee501STomi Valkeinen regmap_write(tc->regmap, INTCTL_G, INT_SYSERR); 2100f25ee501STomi Valkeinen 2101f25ee501STomi Valkeinen ret = devm_request_threaded_irq(dev, client->irq, 2102f25ee501STomi Valkeinen NULL, tc_irq_handler, 2103f25ee501STomi Valkeinen IRQF_ONESHOT, 2104f25ee501STomi Valkeinen "tc358767-irq", tc); 2105f25ee501STomi Valkeinen if (ret) { 2106f25ee501STomi Valkeinen dev_err(dev, "failed to register dp interrupt\n"); 2107f25ee501STomi Valkeinen return ret; 2108f25ee501STomi Valkeinen } 2109f25ee501STomi Valkeinen 2110f25ee501STomi Valkeinen tc->have_irq = true; 2111f25ee501STomi Valkeinen } 2112f25ee501STomi Valkeinen 21137caff0fcSAndrey Gusakov ret = regmap_read(tc->regmap, TC_IDREG, &tc->rev); 21147caff0fcSAndrey Gusakov if (ret) { 21157caff0fcSAndrey Gusakov dev_err(tc->dev, "can not read device ID: %d\n", ret); 21167caff0fcSAndrey Gusakov return ret; 21177caff0fcSAndrey Gusakov } 21187caff0fcSAndrey Gusakov 21197caff0fcSAndrey Gusakov if ((tc->rev != 0x6601) && (tc->rev != 0x6603)) { 21207caff0fcSAndrey Gusakov dev_err(tc->dev, "invalid device ID: 0x%08x\n", tc->rev); 21217caff0fcSAndrey Gusakov return -EINVAL; 21227caff0fcSAndrey Gusakov } 21237caff0fcSAndrey Gusakov 21247caff0fcSAndrey Gusakov tc->assr = (tc->rev == 0x6601); /* Enable ASSR for eDP panels */ 21257caff0fcSAndrey Gusakov 212652c2197aSLucas Stach if (!tc->reset_gpio) { 212752c2197aSLucas Stach /* 212852c2197aSLucas Stach * If the reset pin isn't present, do a software reset. It isn't 212952c2197aSLucas Stach * as thorough as the hardware reset, as we can't reset the I2C 213052c2197aSLucas Stach * communication block for obvious reasons, but it's getting the 213152c2197aSLucas Stach * chip into a defined state. 213252c2197aSLucas Stach */ 213352c2197aSLucas Stach regmap_update_bits(tc->regmap, SYSRSTENB, 213452c2197aSLucas Stach ENBLCD0 | ENBBM | ENBDSIRX | ENBREG | ENBHDCP, 213552c2197aSLucas Stach 0); 213652c2197aSLucas Stach regmap_update_bits(tc->regmap, SYSRSTENB, 213752c2197aSLucas Stach ENBLCD0 | ENBBM | ENBDSIRX | ENBREG | ENBHDCP, 213852c2197aSLucas Stach ENBLCD0 | ENBBM | ENBDSIRX | ENBREG | ENBHDCP); 213952c2197aSLucas Stach usleep_range(5000, 10000); 214052c2197aSLucas Stach } 214152c2197aSLucas Stach 2142f25ee501STomi Valkeinen if (tc->hpd_pin >= 0) { 2143f25ee501STomi Valkeinen u32 lcnt_reg = tc->hpd_pin == 0 ? INT_GP0_LCNT : INT_GP1_LCNT; 2144f25ee501STomi Valkeinen u32 h_lc = INT_GPIO_H(tc->hpd_pin) | INT_GPIO_LC(tc->hpd_pin); 2145f25ee501STomi Valkeinen 2146f25ee501STomi Valkeinen /* Set LCNT to 2ms */ 2147f25ee501STomi Valkeinen regmap_write(tc->regmap, lcnt_reg, 2148f25ee501STomi Valkeinen clk_get_rate(tc->refclk) * 2 / 1000); 2149f25ee501STomi Valkeinen /* We need the "alternate" mode for HPD */ 2150f25ee501STomi Valkeinen regmap_write(tc->regmap, GPIOM, BIT(tc->hpd_pin)); 2151f25ee501STomi Valkeinen 2152f25ee501STomi Valkeinen if (tc->have_irq) { 2153f25ee501STomi Valkeinen /* enable H & LC */ 2154f25ee501STomi Valkeinen regmap_update_bits(tc->regmap, INTCTL_G, h_lc, h_lc); 2155f25ee501STomi Valkeinen } 2156f25ee501STomi Valkeinen } 2157f25ee501STomi Valkeinen 2158bbfd3190SMarek Vasut if (tc->bridge.type != DRM_MODE_CONNECTOR_DPI) { /* (e)DP output */ 21597caff0fcSAndrey Gusakov ret = tc_aux_link_setup(tc); 21607caff0fcSAndrey Gusakov if (ret) 21617caff0fcSAndrey Gusakov return ret; 2162bbfd3190SMarek Vasut } 21637caff0fcSAndrey Gusakov 21647caff0fcSAndrey Gusakov tc->bridge.of_node = dev->of_node; 2165dc01732eSInki Dae drm_bridge_add(&tc->bridge); 21667caff0fcSAndrey Gusakov 21677caff0fcSAndrey Gusakov i2c_set_clientdata(client, tc); 21687caff0fcSAndrey Gusakov 21693080c21aSMarek Vasut if (tc->input_connector_dsi) { /* DSI input */ 2170bbfd3190SMarek Vasut ret = tc_mipi_dsi_host_attach(tc); 2171bbfd3190SMarek Vasut if (ret) { 2172bbfd3190SMarek Vasut drm_bridge_remove(&tc->bridge); 2173bbfd3190SMarek Vasut return ret; 2174bbfd3190SMarek Vasut } 2175bbfd3190SMarek Vasut } 2176bbfd3190SMarek Vasut 21777caff0fcSAndrey Gusakov return 0; 21787caff0fcSAndrey Gusakov } 21797caff0fcSAndrey Gusakov 21807caff0fcSAndrey Gusakov static int tc_remove(struct i2c_client *client) 21817caff0fcSAndrey Gusakov { 21827caff0fcSAndrey Gusakov struct tc_data *tc = i2c_get_clientdata(client); 21837caff0fcSAndrey Gusakov 21847caff0fcSAndrey Gusakov drm_bridge_remove(&tc->bridge); 21857caff0fcSAndrey Gusakov 21867caff0fcSAndrey Gusakov return 0; 21877caff0fcSAndrey Gusakov } 21887caff0fcSAndrey Gusakov 21897caff0fcSAndrey Gusakov static const struct i2c_device_id tc358767_i2c_ids[] = { 21907caff0fcSAndrey Gusakov { "tc358767", 0 }, 21917caff0fcSAndrey Gusakov { } 21927caff0fcSAndrey Gusakov }; 21937caff0fcSAndrey Gusakov MODULE_DEVICE_TABLE(i2c, tc358767_i2c_ids); 21947caff0fcSAndrey Gusakov 21957caff0fcSAndrey Gusakov static const struct of_device_id tc358767_of_ids[] = { 21967caff0fcSAndrey Gusakov { .compatible = "toshiba,tc358767", }, 21977caff0fcSAndrey Gusakov { } 21987caff0fcSAndrey Gusakov }; 21997caff0fcSAndrey Gusakov MODULE_DEVICE_TABLE(of, tc358767_of_ids); 22007caff0fcSAndrey Gusakov 22017caff0fcSAndrey Gusakov static struct i2c_driver tc358767_driver = { 22027caff0fcSAndrey Gusakov .driver = { 22037caff0fcSAndrey Gusakov .name = "tc358767", 22047caff0fcSAndrey Gusakov .of_match_table = tc358767_of_ids, 22057caff0fcSAndrey Gusakov }, 22067caff0fcSAndrey Gusakov .id_table = tc358767_i2c_ids, 22077caff0fcSAndrey Gusakov .probe = tc_probe, 22087caff0fcSAndrey Gusakov .remove = tc_remove, 22097caff0fcSAndrey Gusakov }; 22107caff0fcSAndrey Gusakov module_i2c_driver(tc358767_driver); 22117caff0fcSAndrey Gusakov 22127caff0fcSAndrey Gusakov MODULE_AUTHOR("Andrey Gusakov <andrey.gusakov@cogentembedded.com>"); 22137caff0fcSAndrey Gusakov MODULE_DESCRIPTION("tc358767 eDP encoder driver"); 22147caff0fcSAndrey Gusakov MODULE_LICENSE("GPL"); 2215