18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * TC358775 DSI to LVDS bridge driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2020 SMART Wireless Computing
68c2ecf20Sopenharmony_ci * Author: Vinay Simha BN <simhavcs@gmail.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci/* #define DEBUG */
108c2ecf20Sopenharmony_ci#include <linux/bitfield.h>
118c2ecf20Sopenharmony_ci#include <linux/clk.h>
128c2ecf20Sopenharmony_ci#include <linux/device.h>
138c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h>
148c2ecf20Sopenharmony_ci#include <linux/i2c.h>
158c2ecf20Sopenharmony_ci#include <linux/kernel.h>
168c2ecf20Sopenharmony_ci#include <linux/module.h>
178c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h>
188c2ecf20Sopenharmony_ci#include <linux/slab.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include <drm/drm_atomic_helper.h>
238c2ecf20Sopenharmony_ci#include <drm/drm_bridge.h>
248c2ecf20Sopenharmony_ci#include <drm/drm_crtc_helper.h>
258c2ecf20Sopenharmony_ci#include <drm/drm_dp_helper.h>
268c2ecf20Sopenharmony_ci#include <drm/drm_mipi_dsi.h>
278c2ecf20Sopenharmony_ci#include <drm/drm_of.h>
288c2ecf20Sopenharmony_ci#include <drm/drm_panel.h>
298c2ecf20Sopenharmony_ci#include <drm/drm_probe_helper.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define FLD_VAL(val, start, end) FIELD_PREP(GENMASK(start, end), val)
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/* Registers */
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* DSI D-PHY Layer Registers */
368c2ecf20Sopenharmony_ci#define D0W_DPHYCONTTX  0x0004  /* Data Lane 0 DPHY Tx Control */
378c2ecf20Sopenharmony_ci#define CLW_DPHYCONTRX  0x0020  /* Clock Lane DPHY Rx Control */
388c2ecf20Sopenharmony_ci#define D0W_DPHYCONTRX  0x0024  /* Data Lane 0 DPHY Rx Control */
398c2ecf20Sopenharmony_ci#define D1W_DPHYCONTRX  0x0028  /* Data Lane 1 DPHY Rx Control */
408c2ecf20Sopenharmony_ci#define D2W_DPHYCONTRX  0x002C  /* Data Lane 2 DPHY Rx Control */
418c2ecf20Sopenharmony_ci#define D3W_DPHYCONTRX  0x0030  /* Data Lane 3 DPHY Rx Control */
428c2ecf20Sopenharmony_ci#define COM_DPHYCONTRX  0x0038  /* DPHY Rx Common Control */
438c2ecf20Sopenharmony_ci#define CLW_CNTRL       0x0040  /* Clock Lane Control */
448c2ecf20Sopenharmony_ci#define D0W_CNTRL       0x0044  /* Data Lane 0 Control */
458c2ecf20Sopenharmony_ci#define D1W_CNTRL       0x0048  /* Data Lane 1 Control */
468c2ecf20Sopenharmony_ci#define D2W_CNTRL       0x004C  /* Data Lane 2 Control */
478c2ecf20Sopenharmony_ci#define D3W_CNTRL       0x0050  /* Data Lane 3 Control */
488c2ecf20Sopenharmony_ci#define DFTMODE_CNTRL   0x0054  /* DFT Mode Control */
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci/* DSI PPI Layer Registers */
518c2ecf20Sopenharmony_ci#define PPI_STARTPPI    0x0104  /* START control bit of PPI-TX function. */
528c2ecf20Sopenharmony_ci#define PPI_START_FUNCTION      1
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define PPI_BUSYPPI     0x0108
558c2ecf20Sopenharmony_ci#define PPI_LINEINITCNT 0x0110  /* Line Initialization Wait Counter  */
568c2ecf20Sopenharmony_ci#define PPI_LPTXTIMECNT 0x0114
578c2ecf20Sopenharmony_ci#define PPI_LANEENABLE  0x0134  /* Enables each lane at the PPI layer. */
588c2ecf20Sopenharmony_ci#define PPI_TX_RX_TA    0x013C  /* DSI Bus Turn Around timing parameters */
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci/* Analog timer function enable */
618c2ecf20Sopenharmony_ci#define PPI_CLS_ATMR    0x0140  /* Delay for Clock Lane in LPRX  */
628c2ecf20Sopenharmony_ci#define PPI_D0S_ATMR    0x0144  /* Delay for Data Lane 0 in LPRX */
638c2ecf20Sopenharmony_ci#define PPI_D1S_ATMR    0x0148  /* Delay for Data Lane 1 in LPRX */
648c2ecf20Sopenharmony_ci#define PPI_D2S_ATMR    0x014C  /* Delay for Data Lane 2 in LPRX */
658c2ecf20Sopenharmony_ci#define PPI_D3S_ATMR    0x0150  /* Delay for Data Lane 3 in LPRX */
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#define PPI_D0S_CLRSIPOCOUNT    0x0164  /* For lane 0 */
688c2ecf20Sopenharmony_ci#define PPI_D1S_CLRSIPOCOUNT    0x0168  /* For lane 1 */
698c2ecf20Sopenharmony_ci#define PPI_D2S_CLRSIPOCOUNT    0x016C  /* For lane 2 */
708c2ecf20Sopenharmony_ci#define PPI_D3S_CLRSIPOCOUNT    0x0170  /* For lane 3 */
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci#define CLS_PRE         0x0180  /* Digital Counter inside of PHY IO */
738c2ecf20Sopenharmony_ci#define D0S_PRE         0x0184  /* Digital Counter inside of PHY IO */
748c2ecf20Sopenharmony_ci#define D1S_PRE         0x0188  /* Digital Counter inside of PHY IO */
758c2ecf20Sopenharmony_ci#define D2S_PRE         0x018C  /* Digital Counter inside of PHY IO */
768c2ecf20Sopenharmony_ci#define D3S_PRE         0x0190  /* Digital Counter inside of PHY IO */
778c2ecf20Sopenharmony_ci#define CLS_PREP        0x01A0  /* Digital Counter inside of PHY IO */
788c2ecf20Sopenharmony_ci#define D0S_PREP        0x01A4  /* Digital Counter inside of PHY IO */
798c2ecf20Sopenharmony_ci#define D1S_PREP        0x01A8  /* Digital Counter inside of PHY IO */
808c2ecf20Sopenharmony_ci#define D2S_PREP        0x01AC  /* Digital Counter inside of PHY IO */
818c2ecf20Sopenharmony_ci#define D3S_PREP        0x01B0  /* Digital Counter inside of PHY IO */
828c2ecf20Sopenharmony_ci#define CLS_ZERO        0x01C0  /* Digital Counter inside of PHY IO */
838c2ecf20Sopenharmony_ci#define D0S_ZERO        0x01C4  /* Digital Counter inside of PHY IO */
848c2ecf20Sopenharmony_ci#define D1S_ZERO        0x01C8  /* Digital Counter inside of PHY IO */
858c2ecf20Sopenharmony_ci#define D2S_ZERO        0x01CC  /* Digital Counter inside of PHY IO */
868c2ecf20Sopenharmony_ci#define D3S_ZERO        0x01D0  /* Digital Counter inside of PHY IO */
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci#define PPI_CLRFLG      0x01E0  /* PRE Counters has reached set values */
898c2ecf20Sopenharmony_ci#define PPI_CLRSIPO     0x01E4  /* Clear SIPO values, Slave mode use only. */
908c2ecf20Sopenharmony_ci#define HSTIMEOUT       0x01F0  /* HS Rx Time Out Counter */
918c2ecf20Sopenharmony_ci#define HSTIMEOUTENABLE 0x01F4  /* Enable HS Rx Time Out Counter */
928c2ecf20Sopenharmony_ci#define DSI_STARTDSI    0x0204  /* START control bit of DSI-TX function */
938c2ecf20Sopenharmony_ci#define DSI_RX_START	1
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci#define DSI_BUSYDSI     0x0208
968c2ecf20Sopenharmony_ci#define DSI_LANEENABLE  0x0210  /* Enables each lane at the Protocol layer. */
978c2ecf20Sopenharmony_ci#define DSI_LANESTATUS0 0x0214  /* Displays lane is in HS RX mode. */
988c2ecf20Sopenharmony_ci#define DSI_LANESTATUS1 0x0218  /* Displays lane is in ULPS or STOP state */
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci#define DSI_INTSTATUS   0x0220  /* Interrupt Status */
1018c2ecf20Sopenharmony_ci#define DSI_INTMASK     0x0224  /* Interrupt Mask */
1028c2ecf20Sopenharmony_ci#define DSI_INTCLR      0x0228  /* Interrupt Clear */
1038c2ecf20Sopenharmony_ci#define DSI_LPTXTO      0x0230  /* Low Power Tx Time Out Counter */
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci#define DSIERRCNT       0x0300  /* DSI Error Count */
1068c2ecf20Sopenharmony_ci#define APLCTRL         0x0400  /* Application Layer Control */
1078c2ecf20Sopenharmony_ci#define RDPKTLN         0x0404  /* Command Read Packet Length */
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci#define VPCTRL          0x0450  /* Video Path Control */
1108c2ecf20Sopenharmony_ci#define HTIM1           0x0454  /* Horizontal Timing Control 1 */
1118c2ecf20Sopenharmony_ci#define HTIM2           0x0458  /* Horizontal Timing Control 2 */
1128c2ecf20Sopenharmony_ci#define VTIM1           0x045C  /* Vertical Timing Control 1 */
1138c2ecf20Sopenharmony_ci#define VTIM2           0x0460  /* Vertical Timing Control 2 */
1148c2ecf20Sopenharmony_ci#define VFUEN           0x0464  /* Video Frame Timing Update Enable */
1158c2ecf20Sopenharmony_ci#define VFUEN_EN	BIT(0)  /* Upload Enable */
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci/* Mux Input Select for LVDS LINK Input */
1188c2ecf20Sopenharmony_ci#define LV_MX0003        0x0480  /* Bit 0 to 3 */
1198c2ecf20Sopenharmony_ci#define LV_MX0407        0x0484  /* Bit 4 to 7 */
1208c2ecf20Sopenharmony_ci#define LV_MX0811        0x0488  /* Bit 8 to 11 */
1218c2ecf20Sopenharmony_ci#define LV_MX1215        0x048C  /* Bit 12 to 15 */
1228c2ecf20Sopenharmony_ci#define LV_MX1619        0x0490  /* Bit 16 to 19 */
1238c2ecf20Sopenharmony_ci#define LV_MX2023        0x0494  /* Bit 20 to 23 */
1248c2ecf20Sopenharmony_ci#define LV_MX2427        0x0498  /* Bit 24 to 27 */
1258c2ecf20Sopenharmony_ci#define LV_MX(b0, b1, b2, b3)	(FLD_VAL(b0, 4, 0) | FLD_VAL(b1, 12, 8) | \
1268c2ecf20Sopenharmony_ci				FLD_VAL(b2, 20, 16) | FLD_VAL(b3, 28, 24))
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci/* Input bit numbers used in mux registers */
1298c2ecf20Sopenharmony_cienum {
1308c2ecf20Sopenharmony_ci	LVI_R0,
1318c2ecf20Sopenharmony_ci	LVI_R1,
1328c2ecf20Sopenharmony_ci	LVI_R2,
1338c2ecf20Sopenharmony_ci	LVI_R3,
1348c2ecf20Sopenharmony_ci	LVI_R4,
1358c2ecf20Sopenharmony_ci	LVI_R5,
1368c2ecf20Sopenharmony_ci	LVI_R6,
1378c2ecf20Sopenharmony_ci	LVI_R7,
1388c2ecf20Sopenharmony_ci	LVI_G0,
1398c2ecf20Sopenharmony_ci	LVI_G1,
1408c2ecf20Sopenharmony_ci	LVI_G2,
1418c2ecf20Sopenharmony_ci	LVI_G3,
1428c2ecf20Sopenharmony_ci	LVI_G4,
1438c2ecf20Sopenharmony_ci	LVI_G5,
1448c2ecf20Sopenharmony_ci	LVI_G6,
1458c2ecf20Sopenharmony_ci	LVI_G7,
1468c2ecf20Sopenharmony_ci	LVI_B0,
1478c2ecf20Sopenharmony_ci	LVI_B1,
1488c2ecf20Sopenharmony_ci	LVI_B2,
1498c2ecf20Sopenharmony_ci	LVI_B3,
1508c2ecf20Sopenharmony_ci	LVI_B4,
1518c2ecf20Sopenharmony_ci	LVI_B5,
1528c2ecf20Sopenharmony_ci	LVI_B6,
1538c2ecf20Sopenharmony_ci	LVI_B7,
1548c2ecf20Sopenharmony_ci	LVI_HS,
1558c2ecf20Sopenharmony_ci	LVI_VS,
1568c2ecf20Sopenharmony_ci	LVI_DE,
1578c2ecf20Sopenharmony_ci	LVI_L0
1588c2ecf20Sopenharmony_ci};
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci#define LVCFG           0x049C  /* LVDS Configuration  */
1618c2ecf20Sopenharmony_ci#define LVPHY0          0x04A0  /* LVDS PHY 0 */
1628c2ecf20Sopenharmony_ci#define LV_PHY0_RST(v)          FLD_VAL(v, 22, 22) /* PHY reset */
1638c2ecf20Sopenharmony_ci#define LV_PHY0_IS(v)           FLD_VAL(v, 15, 14)
1648c2ecf20Sopenharmony_ci#define LV_PHY0_ND(v)           FLD_VAL(v, 4, 0) /* Frequency range select */
1658c2ecf20Sopenharmony_ci#define LV_PHY0_PRBS_ON(v)      FLD_VAL(v, 20, 16) /* Clock/Data Flag pins */
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci#define LVPHY1          0x04A4  /* LVDS PHY 1 */
1688c2ecf20Sopenharmony_ci#define SYSSTAT         0x0500  /* System Status  */
1698c2ecf20Sopenharmony_ci#define SYSRST          0x0504  /* System Reset  */
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci#define SYS_RST_I2CS	BIT(0) /* Reset I2C-Slave controller */
1728c2ecf20Sopenharmony_ci#define SYS_RST_I2CM	BIT(1) /* Reset I2C-Master controller */
1738c2ecf20Sopenharmony_ci#define SYS_RST_LCD	BIT(2) /* Reset LCD controller */
1748c2ecf20Sopenharmony_ci#define SYS_RST_BM	BIT(3) /* Reset Bus Management controller */
1758c2ecf20Sopenharmony_ci#define SYS_RST_DSIRX	BIT(4) /* Reset DSI-RX and App controller */
1768c2ecf20Sopenharmony_ci#define SYS_RST_REG	BIT(5) /* Reset Register module */
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci/* GPIO Registers */
1798c2ecf20Sopenharmony_ci#define GPIOC           0x0520  /* GPIO Control  */
1808c2ecf20Sopenharmony_ci#define GPIOO           0x0524  /* GPIO Output  */
1818c2ecf20Sopenharmony_ci#define GPIOI           0x0528  /* GPIO Input  */
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci/* I2C Registers */
1848c2ecf20Sopenharmony_ci#define I2CTIMCTRL      0x0540  /* I2C IF Timing and Enable Control */
1858c2ecf20Sopenharmony_ci#define I2CMADDR        0x0544  /* I2C Master Addressing */
1868c2ecf20Sopenharmony_ci#define WDATAQ          0x0548  /* Write Data Queue */
1878c2ecf20Sopenharmony_ci#define RDATAQ          0x054C  /* Read Data Queue */
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci/* Chip ID and Revision ID Register */
1908c2ecf20Sopenharmony_ci#define IDREG           0x0580
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci#define LPX_PERIOD		4
1938c2ecf20Sopenharmony_ci#define TTA_GET			0x40000
1948c2ecf20Sopenharmony_ci#define TTA_SURE		6
1958c2ecf20Sopenharmony_ci#define SINGLE_LINK		1
1968c2ecf20Sopenharmony_ci#define DUAL_LINK		2
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci#define TC358775XBG_ID  0x00007500
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci/* Debug Registers */
2018c2ecf20Sopenharmony_ci#define DEBUG00         0x05A0  /* Debug */
2028c2ecf20Sopenharmony_ci#define DEBUG01         0x05A4  /* LVDS Data */
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci#define DSI_CLEN_BIT		BIT(0)
2058c2ecf20Sopenharmony_ci#define DIVIDE_BY_3		3 /* PCLK=DCLK/3 */
2068c2ecf20Sopenharmony_ci#define DIVIDE_BY_6		6 /* PCLK=DCLK/6 */
2078c2ecf20Sopenharmony_ci#define LVCFG_LVEN_BIT		BIT(0)
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci#define L0EN BIT(1)
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci#define TC358775_VPCTRL_VSDELAY__MASK	0x3FF00000
2128c2ecf20Sopenharmony_ci#define TC358775_VPCTRL_VSDELAY__SHIFT	20
2138c2ecf20Sopenharmony_cistatic inline u32 TC358775_VPCTRL_VSDELAY(uint32_t val)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	return ((val) << TC358775_VPCTRL_VSDELAY__SHIFT) &
2168c2ecf20Sopenharmony_ci			TC358775_VPCTRL_VSDELAY__MASK;
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci#define TC358775_VPCTRL_OPXLFMT__MASK	0x00000100
2208c2ecf20Sopenharmony_ci#define TC358775_VPCTRL_OPXLFMT__SHIFT	8
2218c2ecf20Sopenharmony_cistatic inline u32 TC358775_VPCTRL_OPXLFMT(uint32_t val)
2228c2ecf20Sopenharmony_ci{
2238c2ecf20Sopenharmony_ci	return ((val) << TC358775_VPCTRL_OPXLFMT__SHIFT) &
2248c2ecf20Sopenharmony_ci			TC358775_VPCTRL_OPXLFMT__MASK;
2258c2ecf20Sopenharmony_ci}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci#define TC358775_VPCTRL_MSF__MASK	0x00000001
2288c2ecf20Sopenharmony_ci#define TC358775_VPCTRL_MSF__SHIFT	0
2298c2ecf20Sopenharmony_cistatic inline u32 TC358775_VPCTRL_MSF(uint32_t val)
2308c2ecf20Sopenharmony_ci{
2318c2ecf20Sopenharmony_ci	return ((val) << TC358775_VPCTRL_MSF__SHIFT) &
2328c2ecf20Sopenharmony_ci			TC358775_VPCTRL_MSF__MASK;
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci#define TC358775_LVCFG_PCLKDIV__MASK	0x000000f0
2368c2ecf20Sopenharmony_ci#define TC358775_LVCFG_PCLKDIV__SHIFT	4
2378c2ecf20Sopenharmony_cistatic inline u32 TC358775_LVCFG_PCLKDIV(uint32_t val)
2388c2ecf20Sopenharmony_ci{
2398c2ecf20Sopenharmony_ci	return ((val) << TC358775_LVCFG_PCLKDIV__SHIFT) &
2408c2ecf20Sopenharmony_ci			TC358775_LVCFG_PCLKDIV__MASK;
2418c2ecf20Sopenharmony_ci}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci#define TC358775_LVCFG_LVDLINK__MASK                         0x00000002
2448c2ecf20Sopenharmony_ci#define TC358775_LVCFG_LVDLINK__SHIFT                        0
2458c2ecf20Sopenharmony_cistatic inline u32 TC358775_LVCFG_LVDLINK(uint32_t val)
2468c2ecf20Sopenharmony_ci{
2478c2ecf20Sopenharmony_ci	return ((val) << TC358775_LVCFG_LVDLINK__SHIFT) &
2488c2ecf20Sopenharmony_ci			TC358775_LVCFG_LVDLINK__MASK;
2498c2ecf20Sopenharmony_ci}
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_cienum tc358775_ports {
2528c2ecf20Sopenharmony_ci	TC358775_DSI_IN,
2538c2ecf20Sopenharmony_ci	TC358775_LVDS_OUT0,
2548c2ecf20Sopenharmony_ci	TC358775_LVDS_OUT1,
2558c2ecf20Sopenharmony_ci};
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_cistruct tc_data {
2588c2ecf20Sopenharmony_ci	struct i2c_client	*i2c;
2598c2ecf20Sopenharmony_ci	struct device		*dev;
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	struct drm_bridge	bridge;
2628c2ecf20Sopenharmony_ci	struct drm_bridge	*panel_bridge;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	struct device_node *host_node;
2658c2ecf20Sopenharmony_ci	struct mipi_dsi_device *dsi;
2668c2ecf20Sopenharmony_ci	u8 num_dsi_lanes;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	struct regulator	*vdd;
2698c2ecf20Sopenharmony_ci	struct regulator	*vddio;
2708c2ecf20Sopenharmony_ci	struct gpio_desc	*reset_gpio;
2718c2ecf20Sopenharmony_ci	struct gpio_desc	*stby_gpio;
2728c2ecf20Sopenharmony_ci	u8			lvds_link; /* single-link or dual-link */
2738c2ecf20Sopenharmony_ci	u8			bpc;
2748c2ecf20Sopenharmony_ci};
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_cistatic inline struct tc_data *bridge_to_tc(struct drm_bridge *b)
2778c2ecf20Sopenharmony_ci{
2788c2ecf20Sopenharmony_ci	return container_of(b, struct tc_data, bridge);
2798c2ecf20Sopenharmony_ci}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_cistatic void tc_bridge_pre_enable(struct drm_bridge *bridge)
2828c2ecf20Sopenharmony_ci{
2838c2ecf20Sopenharmony_ci	struct tc_data *tc = bridge_to_tc(bridge);
2848c2ecf20Sopenharmony_ci	struct device *dev = &tc->dsi->dev;
2858c2ecf20Sopenharmony_ci	int ret;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	ret = regulator_enable(tc->vddio);
2888c2ecf20Sopenharmony_ci	if (ret < 0)
2898c2ecf20Sopenharmony_ci		dev_err(dev, "regulator vddio enable failed, %d\n", ret);
2908c2ecf20Sopenharmony_ci	usleep_range(10000, 11000);
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	ret = regulator_enable(tc->vdd);
2938c2ecf20Sopenharmony_ci	if (ret < 0)
2948c2ecf20Sopenharmony_ci		dev_err(dev, "regulator vdd enable failed, %d\n", ret);
2958c2ecf20Sopenharmony_ci	usleep_range(10000, 11000);
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	gpiod_set_value(tc->stby_gpio, 0);
2988c2ecf20Sopenharmony_ci	usleep_range(10000, 11000);
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	gpiod_set_value(tc->reset_gpio, 0);
3018c2ecf20Sopenharmony_ci	usleep_range(10, 20);
3028c2ecf20Sopenharmony_ci}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_cistatic void tc_bridge_post_disable(struct drm_bridge *bridge)
3058c2ecf20Sopenharmony_ci{
3068c2ecf20Sopenharmony_ci	struct tc_data *tc = bridge_to_tc(bridge);
3078c2ecf20Sopenharmony_ci	struct device *dev = &tc->dsi->dev;
3088c2ecf20Sopenharmony_ci	int ret;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	gpiod_set_value(tc->reset_gpio, 1);
3118c2ecf20Sopenharmony_ci	usleep_range(10, 20);
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	gpiod_set_value(tc->stby_gpio, 1);
3148c2ecf20Sopenharmony_ci	usleep_range(10000, 11000);
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	ret = regulator_disable(tc->vdd);
3178c2ecf20Sopenharmony_ci	if (ret < 0)
3188c2ecf20Sopenharmony_ci		dev_err(dev, "regulator vdd disable failed, %d\n", ret);
3198c2ecf20Sopenharmony_ci	usleep_range(10000, 11000);
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	ret = regulator_disable(tc->vddio);
3228c2ecf20Sopenharmony_ci	if (ret < 0)
3238c2ecf20Sopenharmony_ci		dev_err(dev, "regulator vddio disable failed, %d\n", ret);
3248c2ecf20Sopenharmony_ci	usleep_range(10000, 11000);
3258c2ecf20Sopenharmony_ci}
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_cistatic void d2l_read(struct i2c_client *i2c, u16 addr, u32 *val)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	int ret;
3308c2ecf20Sopenharmony_ci	u8 buf_addr[2];
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	put_unaligned_be16(addr, buf_addr);
3338c2ecf20Sopenharmony_ci	ret = i2c_master_send(i2c, buf_addr, sizeof(buf_addr));
3348c2ecf20Sopenharmony_ci	if (ret < 0)
3358c2ecf20Sopenharmony_ci		goto fail;
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	ret = i2c_master_recv(i2c, (u8 *)val, sizeof(*val));
3388c2ecf20Sopenharmony_ci	if (ret < 0)
3398c2ecf20Sopenharmony_ci		goto fail;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	pr_debug("d2l: I2C : addr:%04x value:%08x\n", addr, *val);
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_cifail:
3448c2ecf20Sopenharmony_ci	dev_err(&i2c->dev, "Error %d reading from subaddress 0x%x\n",
3458c2ecf20Sopenharmony_ci		ret, addr);
3468c2ecf20Sopenharmony_ci}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_cistatic void d2l_write(struct i2c_client *i2c, u16 addr, u32 val)
3498c2ecf20Sopenharmony_ci{
3508c2ecf20Sopenharmony_ci	u8 data[6];
3518c2ecf20Sopenharmony_ci	int ret;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	put_unaligned_be16(addr, data);
3548c2ecf20Sopenharmony_ci	put_unaligned_le32(val, data + 2);
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	ret = i2c_master_send(i2c, data, ARRAY_SIZE(data));
3578c2ecf20Sopenharmony_ci	if (ret < 0)
3588c2ecf20Sopenharmony_ci		dev_err(&i2c->dev, "Error %d writing to subaddress 0x%x\n",
3598c2ecf20Sopenharmony_ci			ret, addr);
3608c2ecf20Sopenharmony_ci}
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci/* helper function to access bus_formats */
3638c2ecf20Sopenharmony_cistatic struct drm_connector *get_connector(struct drm_encoder *encoder)
3648c2ecf20Sopenharmony_ci{
3658c2ecf20Sopenharmony_ci	struct drm_device *dev = encoder->dev;
3668c2ecf20Sopenharmony_ci	struct drm_connector *connector;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
3698c2ecf20Sopenharmony_ci		if (connector->encoder == encoder)
3708c2ecf20Sopenharmony_ci			return connector;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	return NULL;
3738c2ecf20Sopenharmony_ci}
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_cistatic void tc_bridge_enable(struct drm_bridge *bridge)
3768c2ecf20Sopenharmony_ci{
3778c2ecf20Sopenharmony_ci	struct tc_data *tc = bridge_to_tc(bridge);
3788c2ecf20Sopenharmony_ci	u32 hback_porch, hsync_len, hfront_porch, hactive, htime1, htime2;
3798c2ecf20Sopenharmony_ci	u32 vback_porch, vsync_len, vfront_porch, vactive, vtime1, vtime2;
3808c2ecf20Sopenharmony_ci	u32 val = 0;
3818c2ecf20Sopenharmony_ci	u16 dsiclk, clkdiv, byteclk, t1, t2, t3, vsdelay;
3828c2ecf20Sopenharmony_ci	struct drm_display_mode *mode;
3838c2ecf20Sopenharmony_ci	struct drm_connector *connector = get_connector(bridge->encoder);
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	mode = &bridge->encoder->crtc->state->adjusted_mode;
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	hback_porch = mode->htotal - mode->hsync_end;
3888c2ecf20Sopenharmony_ci	hsync_len  = mode->hsync_end - mode->hsync_start;
3898c2ecf20Sopenharmony_ci	vback_porch = mode->vtotal - mode->vsync_end;
3908c2ecf20Sopenharmony_ci	vsync_len  = mode->vsync_end - mode->vsync_start;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	htime1 = (hback_porch << 16) + hsync_len;
3938c2ecf20Sopenharmony_ci	vtime1 = (vback_porch << 16) + vsync_len;
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	hfront_porch = mode->hsync_start - mode->hdisplay;
3968c2ecf20Sopenharmony_ci	hactive = mode->hdisplay;
3978c2ecf20Sopenharmony_ci	vfront_porch = mode->vsync_start - mode->vdisplay;
3988c2ecf20Sopenharmony_ci	vactive = mode->vdisplay;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	htime2 = (hfront_porch << 16) + hactive;
4018c2ecf20Sopenharmony_ci	vtime2 = (vfront_porch << 16) + vactive;
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	d2l_read(tc->i2c, IDREG, &val);
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	dev_info(tc->dev, "DSI2LVDS Chip ID.%02x Revision ID. %02x **\n",
4068c2ecf20Sopenharmony_ci		 (val >> 8) & 0xFF, val & 0xFF);
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	d2l_write(tc->i2c, SYSRST, SYS_RST_REG | SYS_RST_DSIRX | SYS_RST_BM |
4098c2ecf20Sopenharmony_ci		  SYS_RST_LCD | SYS_RST_I2CM | SYS_RST_I2CS);
4108c2ecf20Sopenharmony_ci	usleep_range(30000, 40000);
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	d2l_write(tc->i2c, PPI_TX_RX_TA, TTA_GET | TTA_SURE);
4138c2ecf20Sopenharmony_ci	d2l_write(tc->i2c, PPI_LPTXTIMECNT, LPX_PERIOD);
4148c2ecf20Sopenharmony_ci	d2l_write(tc->i2c, PPI_D0S_CLRSIPOCOUNT, 3);
4158c2ecf20Sopenharmony_ci	d2l_write(tc->i2c, PPI_D1S_CLRSIPOCOUNT, 3);
4168c2ecf20Sopenharmony_ci	d2l_write(tc->i2c, PPI_D2S_CLRSIPOCOUNT, 3);
4178c2ecf20Sopenharmony_ci	d2l_write(tc->i2c, PPI_D3S_CLRSIPOCOUNT, 3);
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	val = ((L0EN << tc->num_dsi_lanes) - L0EN) | DSI_CLEN_BIT;
4208c2ecf20Sopenharmony_ci	d2l_write(tc->i2c, PPI_LANEENABLE, val);
4218c2ecf20Sopenharmony_ci	d2l_write(tc->i2c, DSI_LANEENABLE, val);
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	d2l_write(tc->i2c, PPI_STARTPPI, PPI_START_FUNCTION);
4248c2ecf20Sopenharmony_ci	d2l_write(tc->i2c, DSI_STARTDSI, DSI_RX_START);
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	if (tc->bpc == 8)
4278c2ecf20Sopenharmony_ci		val = TC358775_VPCTRL_OPXLFMT(1);
4288c2ecf20Sopenharmony_ci	else /* bpc = 6; */
4298c2ecf20Sopenharmony_ci		val = TC358775_VPCTRL_MSF(1);
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	dsiclk = mode->crtc_clock * 3 * tc->bpc / tc->num_dsi_lanes / 1000;
4328c2ecf20Sopenharmony_ci	clkdiv = dsiclk / DIVIDE_BY_3 * tc->lvds_link;
4338c2ecf20Sopenharmony_ci	byteclk = dsiclk / 4;
4348c2ecf20Sopenharmony_ci	t1 = hactive * (tc->bpc * 3 / 8) / tc->num_dsi_lanes;
4358c2ecf20Sopenharmony_ci	t2 = ((100000 / clkdiv)) * (hactive + hback_porch + hsync_len + hfront_porch) / 1000;
4368c2ecf20Sopenharmony_ci	t3 = ((t2 * byteclk) / 100) - (hactive * (tc->bpc * 3 / 8) /
4378c2ecf20Sopenharmony_ci		tc->num_dsi_lanes);
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	vsdelay = (clkdiv * (t1 + t3) / byteclk) - hback_porch - hsync_len - hactive;
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	val |= TC358775_VPCTRL_VSDELAY(vsdelay);
4428c2ecf20Sopenharmony_ci	d2l_write(tc->i2c, VPCTRL, val);
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	d2l_write(tc->i2c, HTIM1, htime1);
4458c2ecf20Sopenharmony_ci	d2l_write(tc->i2c, VTIM1, vtime1);
4468c2ecf20Sopenharmony_ci	d2l_write(tc->i2c, HTIM2, htime2);
4478c2ecf20Sopenharmony_ci	d2l_write(tc->i2c, VTIM2, vtime2);
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	d2l_write(tc->i2c, VFUEN, VFUEN_EN);
4508c2ecf20Sopenharmony_ci	d2l_write(tc->i2c, SYSRST, SYS_RST_LCD);
4518c2ecf20Sopenharmony_ci	d2l_write(tc->i2c, LVPHY0, LV_PHY0_PRBS_ON(4) | LV_PHY0_ND(6));
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	dev_dbg(tc->dev, "bus_formats %04x bpc %d\n",
4548c2ecf20Sopenharmony_ci		connector->display_info.bus_formats[0],
4558c2ecf20Sopenharmony_ci		tc->bpc);
4568c2ecf20Sopenharmony_ci	/*
4578c2ecf20Sopenharmony_ci	 * Default hardware register settings of tc358775 configured
4588c2ecf20Sopenharmony_ci	 * with MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA jeida-24 format
4598c2ecf20Sopenharmony_ci	 */
4608c2ecf20Sopenharmony_ci	if (connector->display_info.bus_formats[0] ==
4618c2ecf20Sopenharmony_ci		MEDIA_BUS_FMT_RGB888_1X7X4_SPWG) {
4628c2ecf20Sopenharmony_ci		/* VESA-24 */
4638c2ecf20Sopenharmony_ci		d2l_write(tc->i2c, LV_MX0003, LV_MX(LVI_R0, LVI_R1, LVI_R2, LVI_R3));
4648c2ecf20Sopenharmony_ci		d2l_write(tc->i2c, LV_MX0407, LV_MX(LVI_R4, LVI_R7, LVI_R5, LVI_G0));
4658c2ecf20Sopenharmony_ci		d2l_write(tc->i2c, LV_MX0811, LV_MX(LVI_G1, LVI_G2, LVI_G6, LVI_G7));
4668c2ecf20Sopenharmony_ci		d2l_write(tc->i2c, LV_MX1215, LV_MX(LVI_G3, LVI_G4, LVI_G5, LVI_B0));
4678c2ecf20Sopenharmony_ci		d2l_write(tc->i2c, LV_MX1619, LV_MX(LVI_B6, LVI_B7, LVI_B1, LVI_B2));
4688c2ecf20Sopenharmony_ci		d2l_write(tc->i2c, LV_MX2023, LV_MX(LVI_B3, LVI_B4, LVI_B5, LVI_L0));
4698c2ecf20Sopenharmony_ci		d2l_write(tc->i2c, LV_MX2427, LV_MX(LVI_HS, LVI_VS, LVI_DE, LVI_R6));
4708c2ecf20Sopenharmony_ci	} else { /*  MEDIA_BUS_FMT_RGB666_1X7X3_SPWG - JEIDA-18 */
4718c2ecf20Sopenharmony_ci		d2l_write(tc->i2c, LV_MX0003, LV_MX(LVI_R0, LVI_R1, LVI_R2, LVI_R3));
4728c2ecf20Sopenharmony_ci		d2l_write(tc->i2c, LV_MX0407, LV_MX(LVI_R4, LVI_L0, LVI_R5, LVI_G0));
4738c2ecf20Sopenharmony_ci		d2l_write(tc->i2c, LV_MX0811, LV_MX(LVI_G1, LVI_G2, LVI_L0, LVI_L0));
4748c2ecf20Sopenharmony_ci		d2l_write(tc->i2c, LV_MX1215, LV_MX(LVI_G3, LVI_G4, LVI_G5, LVI_B0));
4758c2ecf20Sopenharmony_ci		d2l_write(tc->i2c, LV_MX1619, LV_MX(LVI_L0, LVI_L0, LVI_B1, LVI_B2));
4768c2ecf20Sopenharmony_ci		d2l_write(tc->i2c, LV_MX2023, LV_MX(LVI_B3, LVI_B4, LVI_B5, LVI_L0));
4778c2ecf20Sopenharmony_ci		d2l_write(tc->i2c, LV_MX2427, LV_MX(LVI_HS, LVI_VS, LVI_DE, LVI_L0));
4788c2ecf20Sopenharmony_ci	}
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	d2l_write(tc->i2c, VFUEN, VFUEN_EN);
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	val = LVCFG_LVEN_BIT;
4838c2ecf20Sopenharmony_ci	if (tc->lvds_link == DUAL_LINK) {
4848c2ecf20Sopenharmony_ci		val |= TC358775_LVCFG_LVDLINK(1);
4858c2ecf20Sopenharmony_ci		val |= TC358775_LVCFG_PCLKDIV(DIVIDE_BY_6);
4868c2ecf20Sopenharmony_ci	} else {
4878c2ecf20Sopenharmony_ci		val |= TC358775_LVCFG_PCLKDIV(DIVIDE_BY_3);
4888c2ecf20Sopenharmony_ci	}
4898c2ecf20Sopenharmony_ci	d2l_write(tc->i2c, LVCFG, val);
4908c2ecf20Sopenharmony_ci}
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_cistatic enum drm_mode_status
4938c2ecf20Sopenharmony_citc_mode_valid(struct drm_bridge *bridge,
4948c2ecf20Sopenharmony_ci	      const struct drm_display_info *info,
4958c2ecf20Sopenharmony_ci	      const struct drm_display_mode *mode)
4968c2ecf20Sopenharmony_ci{
4978c2ecf20Sopenharmony_ci	struct tc_data *tc = bridge_to_tc(bridge);
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	/*
5008c2ecf20Sopenharmony_ci	 * Maximum pixel clock speed 135MHz for single-link
5018c2ecf20Sopenharmony_ci	 * 270MHz for dual-link
5028c2ecf20Sopenharmony_ci	 */
5038c2ecf20Sopenharmony_ci	if ((mode->clock > 135000 && tc->lvds_link == SINGLE_LINK) ||
5048c2ecf20Sopenharmony_ci	    (mode->clock > 270000 && tc->lvds_link == DUAL_LINK))
5058c2ecf20Sopenharmony_ci		return MODE_CLOCK_HIGH;
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	switch (info->bus_formats[0]) {
5088c2ecf20Sopenharmony_ci	case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
5098c2ecf20Sopenharmony_ci	case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
5108c2ecf20Sopenharmony_ci		/* RGB888 */
5118c2ecf20Sopenharmony_ci		tc->bpc = 8;
5128c2ecf20Sopenharmony_ci		break;
5138c2ecf20Sopenharmony_ci	case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
5148c2ecf20Sopenharmony_ci		/* RGB666 */
5158c2ecf20Sopenharmony_ci		tc->bpc = 6;
5168c2ecf20Sopenharmony_ci		break;
5178c2ecf20Sopenharmony_ci	default:
5188c2ecf20Sopenharmony_ci		dev_warn(tc->dev,
5198c2ecf20Sopenharmony_ci			 "unsupported LVDS bus format 0x%04x\n",
5208c2ecf20Sopenharmony_ci			 info->bus_formats[0]);
5218c2ecf20Sopenharmony_ci		return MODE_NOMODE;
5228c2ecf20Sopenharmony_ci	}
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	return MODE_OK;
5258c2ecf20Sopenharmony_ci}
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_cistatic int tc358775_parse_dt(struct device_node *np, struct tc_data *tc)
5288c2ecf20Sopenharmony_ci{
5298c2ecf20Sopenharmony_ci	struct device_node *endpoint;
5308c2ecf20Sopenharmony_ci	struct device_node *parent;
5318c2ecf20Sopenharmony_ci	struct device_node *remote;
5328c2ecf20Sopenharmony_ci	struct property *prop;
5338c2ecf20Sopenharmony_ci	int len = 0;
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	/*
5368c2ecf20Sopenharmony_ci	 * To get the data-lanes of dsi, we need to access the dsi0_out of port1
5378c2ecf20Sopenharmony_ci	 *  of dsi0 endpoint from bridge port0 of d2l_in
5388c2ecf20Sopenharmony_ci	 */
5398c2ecf20Sopenharmony_ci	endpoint = of_graph_get_endpoint_by_regs(tc->dev->of_node,
5408c2ecf20Sopenharmony_ci						 TC358775_DSI_IN, -1);
5418c2ecf20Sopenharmony_ci	if (endpoint) {
5428c2ecf20Sopenharmony_ci		/* dsi0_out node */
5438c2ecf20Sopenharmony_ci		parent = of_graph_get_remote_port_parent(endpoint);
5448c2ecf20Sopenharmony_ci		of_node_put(endpoint);
5458c2ecf20Sopenharmony_ci		if (parent) {
5468c2ecf20Sopenharmony_ci			/* dsi0 port 1 */
5478c2ecf20Sopenharmony_ci			endpoint = of_graph_get_endpoint_by_regs(parent, 1, -1);
5488c2ecf20Sopenharmony_ci			of_node_put(parent);
5498c2ecf20Sopenharmony_ci			if (endpoint) {
5508c2ecf20Sopenharmony_ci				prop = of_find_property(endpoint, "data-lanes",
5518c2ecf20Sopenharmony_ci							&len);
5528c2ecf20Sopenharmony_ci				of_node_put(endpoint);
5538c2ecf20Sopenharmony_ci				if (!prop) {
5548c2ecf20Sopenharmony_ci					dev_err(tc->dev,
5558c2ecf20Sopenharmony_ci						"failed to find data lane\n");
5568c2ecf20Sopenharmony_ci					return -EPROBE_DEFER;
5578c2ecf20Sopenharmony_ci				}
5588c2ecf20Sopenharmony_ci			}
5598c2ecf20Sopenharmony_ci		}
5608c2ecf20Sopenharmony_ci	}
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	tc->num_dsi_lanes = len / sizeof(u32);
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	if (tc->num_dsi_lanes < 1 || tc->num_dsi_lanes > 4)
5658c2ecf20Sopenharmony_ci		return -EINVAL;
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	tc->host_node = of_graph_get_remote_node(np, 0, 0);
5688c2ecf20Sopenharmony_ci	if (!tc->host_node)
5698c2ecf20Sopenharmony_ci		return -ENODEV;
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	of_node_put(tc->host_node);
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	tc->lvds_link = SINGLE_LINK;
5748c2ecf20Sopenharmony_ci	endpoint = of_graph_get_endpoint_by_regs(tc->dev->of_node,
5758c2ecf20Sopenharmony_ci						 TC358775_LVDS_OUT1, -1);
5768c2ecf20Sopenharmony_ci	if (endpoint) {
5778c2ecf20Sopenharmony_ci		remote = of_graph_get_remote_port_parent(endpoint);
5788c2ecf20Sopenharmony_ci		of_node_put(endpoint);
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci		if (remote) {
5818c2ecf20Sopenharmony_ci			if (of_device_is_available(remote))
5828c2ecf20Sopenharmony_ci				tc->lvds_link = DUAL_LINK;
5838c2ecf20Sopenharmony_ci			of_node_put(remote);
5848c2ecf20Sopenharmony_ci		}
5858c2ecf20Sopenharmony_ci	}
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	dev_dbg(tc->dev, "no.of dsi lanes: %d\n", tc->num_dsi_lanes);
5888c2ecf20Sopenharmony_ci	dev_dbg(tc->dev, "operating in %d-link mode\n",	tc->lvds_link);
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	return 0;
5918c2ecf20Sopenharmony_ci}
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_cistatic int tc_bridge_attach(struct drm_bridge *bridge,
5948c2ecf20Sopenharmony_ci			    enum drm_bridge_attach_flags flags)
5958c2ecf20Sopenharmony_ci{
5968c2ecf20Sopenharmony_ci	struct tc_data *tc = bridge_to_tc(bridge);
5978c2ecf20Sopenharmony_ci	struct device *dev = &tc->i2c->dev;
5988c2ecf20Sopenharmony_ci	struct mipi_dsi_host *host;
5998c2ecf20Sopenharmony_ci	struct mipi_dsi_device *dsi;
6008c2ecf20Sopenharmony_ci	int ret;
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	const struct mipi_dsi_device_info info = { .type = "tc358775",
6038c2ecf20Sopenharmony_ci							.channel = 0,
6048c2ecf20Sopenharmony_ci							.node = NULL,
6058c2ecf20Sopenharmony_ci						};
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	host = of_find_mipi_dsi_host_by_node(tc->host_node);
6088c2ecf20Sopenharmony_ci	if (!host) {
6098c2ecf20Sopenharmony_ci		dev_err(dev, "failed to find dsi host\n");
6108c2ecf20Sopenharmony_ci		return -EPROBE_DEFER;
6118c2ecf20Sopenharmony_ci	}
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	dsi = mipi_dsi_device_register_full(host, &info);
6148c2ecf20Sopenharmony_ci	if (IS_ERR(dsi)) {
6158c2ecf20Sopenharmony_ci		dev_err(dev, "failed to create dsi device\n");
6168c2ecf20Sopenharmony_ci		ret = PTR_ERR(dsi);
6178c2ecf20Sopenharmony_ci		goto err_dsi_device;
6188c2ecf20Sopenharmony_ci	}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	tc->dsi = dsi;
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	dsi->lanes = tc->num_dsi_lanes;
6238c2ecf20Sopenharmony_ci	dsi->format = MIPI_DSI_FMT_RGB888;
6248c2ecf20Sopenharmony_ci	dsi->mode_flags = MIPI_DSI_MODE_VIDEO;
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	ret = mipi_dsi_attach(dsi);
6278c2ecf20Sopenharmony_ci	if (ret < 0) {
6288c2ecf20Sopenharmony_ci		dev_err(dev, "failed to attach dsi to host\n");
6298c2ecf20Sopenharmony_ci		goto err_dsi_attach;
6308c2ecf20Sopenharmony_ci	}
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	/* Attach the panel-bridge to the dsi bridge */
6338c2ecf20Sopenharmony_ci	return drm_bridge_attach(bridge->encoder, tc->panel_bridge,
6348c2ecf20Sopenharmony_ci				 &tc->bridge, flags);
6358c2ecf20Sopenharmony_cierr_dsi_attach:
6368c2ecf20Sopenharmony_ci	mipi_dsi_device_unregister(dsi);
6378c2ecf20Sopenharmony_cierr_dsi_device:
6388c2ecf20Sopenharmony_ci	return ret;
6398c2ecf20Sopenharmony_ci}
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_cistatic const struct drm_bridge_funcs tc_bridge_funcs = {
6428c2ecf20Sopenharmony_ci	.attach = tc_bridge_attach,
6438c2ecf20Sopenharmony_ci	.pre_enable = tc_bridge_pre_enable,
6448c2ecf20Sopenharmony_ci	.enable = tc_bridge_enable,
6458c2ecf20Sopenharmony_ci	.mode_valid = tc_mode_valid,
6468c2ecf20Sopenharmony_ci	.post_disable = tc_bridge_post_disable,
6478c2ecf20Sopenharmony_ci};
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_cistatic int tc_probe(struct i2c_client *client, const struct i2c_device_id *id)
6508c2ecf20Sopenharmony_ci{
6518c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
6528c2ecf20Sopenharmony_ci	struct drm_panel *panel;
6538c2ecf20Sopenharmony_ci	struct tc_data *tc;
6548c2ecf20Sopenharmony_ci	int ret;
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	tc = devm_kzalloc(dev, sizeof(*tc), GFP_KERNEL);
6578c2ecf20Sopenharmony_ci	if (!tc)
6588c2ecf20Sopenharmony_ci		return -ENOMEM;
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	tc->dev = dev;
6618c2ecf20Sopenharmony_ci	tc->i2c = client;
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci	ret = drm_of_find_panel_or_bridge(dev->of_node, TC358775_LVDS_OUT0,
6648c2ecf20Sopenharmony_ci					  0, &panel, NULL);
6658c2ecf20Sopenharmony_ci	if (ret < 0)
6668c2ecf20Sopenharmony_ci		return ret;
6678c2ecf20Sopenharmony_ci	if (!panel)
6688c2ecf20Sopenharmony_ci		return -ENODEV;
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	tc->panel_bridge = devm_drm_panel_bridge_add(dev, panel);
6718c2ecf20Sopenharmony_ci	if (IS_ERR(tc->panel_bridge))
6728c2ecf20Sopenharmony_ci		return PTR_ERR(tc->panel_bridge);
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	ret = tc358775_parse_dt(dev->of_node, tc);
6758c2ecf20Sopenharmony_ci	if (ret)
6768c2ecf20Sopenharmony_ci		return ret;
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	tc->vddio = devm_regulator_get(dev, "vddio-supply");
6798c2ecf20Sopenharmony_ci	if (IS_ERR(tc->vddio)) {
6808c2ecf20Sopenharmony_ci		ret = PTR_ERR(tc->vddio);
6818c2ecf20Sopenharmony_ci		dev_err(dev, "vddio-supply not found\n");
6828c2ecf20Sopenharmony_ci		return ret;
6838c2ecf20Sopenharmony_ci	}
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	tc->vdd = devm_regulator_get(dev, "vdd-supply");
6868c2ecf20Sopenharmony_ci	if (IS_ERR(tc->vdd)) {
6878c2ecf20Sopenharmony_ci		ret = PTR_ERR(tc->vdd);
6888c2ecf20Sopenharmony_ci		dev_err(dev, "vdd-supply not found\n");
6898c2ecf20Sopenharmony_ci		return ret;
6908c2ecf20Sopenharmony_ci	}
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci	tc->stby_gpio = devm_gpiod_get(dev, "stby", GPIOD_OUT_HIGH);
6938c2ecf20Sopenharmony_ci	if (IS_ERR(tc->stby_gpio)) {
6948c2ecf20Sopenharmony_ci		ret = PTR_ERR(tc->stby_gpio);
6958c2ecf20Sopenharmony_ci		dev_err(dev, "cannot get stby-gpio %d\n", ret);
6968c2ecf20Sopenharmony_ci		return ret;
6978c2ecf20Sopenharmony_ci	}
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	tc->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
7008c2ecf20Sopenharmony_ci	if (IS_ERR(tc->reset_gpio)) {
7018c2ecf20Sopenharmony_ci		ret = PTR_ERR(tc->reset_gpio);
7028c2ecf20Sopenharmony_ci		dev_err(dev, "cannot get reset-gpios %d\n", ret);
7038c2ecf20Sopenharmony_ci		return ret;
7048c2ecf20Sopenharmony_ci	}
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	tc->bridge.funcs = &tc_bridge_funcs;
7078c2ecf20Sopenharmony_ci	tc->bridge.of_node = dev->of_node;
7088c2ecf20Sopenharmony_ci	drm_bridge_add(&tc->bridge);
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, tc);
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	return 0;
7138c2ecf20Sopenharmony_ci}
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_cistatic int tc_remove(struct i2c_client *client)
7168c2ecf20Sopenharmony_ci{
7178c2ecf20Sopenharmony_ci	struct tc_data *tc = i2c_get_clientdata(client);
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	drm_bridge_remove(&tc->bridge);
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	return 0;
7228c2ecf20Sopenharmony_ci}
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_cistatic const struct i2c_device_id tc358775_i2c_ids[] = {
7258c2ecf20Sopenharmony_ci	{ "tc358775", 0 },
7268c2ecf20Sopenharmony_ci	{ }
7278c2ecf20Sopenharmony_ci};
7288c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, tc358775_i2c_ids);
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_cistatic const struct of_device_id tc358775_of_ids[] = {
7318c2ecf20Sopenharmony_ci	{ .compatible = "toshiba,tc358775", },
7328c2ecf20Sopenharmony_ci	{ }
7338c2ecf20Sopenharmony_ci};
7348c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, tc358775_of_ids);
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_cistatic struct i2c_driver tc358775_driver = {
7378c2ecf20Sopenharmony_ci	.driver = {
7388c2ecf20Sopenharmony_ci		.name = "tc358775",
7398c2ecf20Sopenharmony_ci		.of_match_table = tc358775_of_ids,
7408c2ecf20Sopenharmony_ci	},
7418c2ecf20Sopenharmony_ci	.id_table = tc358775_i2c_ids,
7428c2ecf20Sopenharmony_ci	.probe = tc_probe,
7438c2ecf20Sopenharmony_ci	.remove	= tc_remove,
7448c2ecf20Sopenharmony_ci};
7458c2ecf20Sopenharmony_cimodule_i2c_driver(tc358775_driver);
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ciMODULE_AUTHOR("Vinay Simha BN <simhavcs@gmail.com>");
7488c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("TC358775 DSI/LVDS bridge driver");
7498c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
750