18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2018 Samsung Electronics Co., Ltd
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Authors:
68c2ecf20Sopenharmony_ci *	Andrzej Hajda <a.hajda@samsung.com>
78c2ecf20Sopenharmony_ci *	Maciej Purski <m.purski@samsung.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/delay.h>
118c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h>
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/of_graph.h>
148c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <video/mipi_display.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <drm/drm_atomic_helper.h>
198c2ecf20Sopenharmony_ci#include <drm/drm_bridge.h>
208c2ecf20Sopenharmony_ci#include <drm/drm_crtc.h>
218c2ecf20Sopenharmony_ci#include <drm/drm_fb_helper.h>
228c2ecf20Sopenharmony_ci#include <drm/drm_mipi_dsi.h>
238c2ecf20Sopenharmony_ci#include <drm/drm_of.h>
248c2ecf20Sopenharmony_ci#include <drm/drm_panel.h>
258c2ecf20Sopenharmony_ci#include <drm/drm_print.h>
268c2ecf20Sopenharmony_ci#include <drm/drm_probe_helper.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define FLD_MASK(start, end)    (((1 << ((start) - (end) + 1)) - 1) << (end))
298c2ecf20Sopenharmony_ci#define FLD_VAL(val, start, end) (((val) << (end)) & FLD_MASK(start, end))
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/* PPI layer registers */
328c2ecf20Sopenharmony_ci#define PPI_STARTPPI		0x0104 /* START control bit */
338c2ecf20Sopenharmony_ci#define PPI_LPTXTIMECNT		0x0114 /* LPTX timing signal */
348c2ecf20Sopenharmony_ci#define PPI_LANEENABLE		0x0134 /* Enables each lane */
358c2ecf20Sopenharmony_ci#define PPI_TX_RX_TA		0x013C /* BTA timing parameters */
368c2ecf20Sopenharmony_ci#define PPI_D0S_CLRSIPOCOUNT	0x0164 /* Assertion timer for Lane 0 */
378c2ecf20Sopenharmony_ci#define PPI_D1S_CLRSIPOCOUNT	0x0168 /* Assertion timer for Lane 1 */
388c2ecf20Sopenharmony_ci#define PPI_D2S_CLRSIPOCOUNT	0x016C /* Assertion timer for Lane 2 */
398c2ecf20Sopenharmony_ci#define PPI_D3S_CLRSIPOCOUNT	0x0170 /* Assertion timer for Lane 3 */
408c2ecf20Sopenharmony_ci#define PPI_START_FUNCTION	1
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/* DSI layer registers */
438c2ecf20Sopenharmony_ci#define DSI_STARTDSI		0x0204 /* START control bit of DSI-TX */
448c2ecf20Sopenharmony_ci#define DSI_LANEENABLE		0x0210 /* Enables each lane */
458c2ecf20Sopenharmony_ci#define DSI_RX_START		1
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci/* Video path registers */
488c2ecf20Sopenharmony_ci#define VP_CTRL			0x0450 /* Video Path Control */
498c2ecf20Sopenharmony_ci#define VP_CTRL_MSF(v)		FLD_VAL(v, 0, 0) /* Magic square in RGB666 */
508c2ecf20Sopenharmony_ci#define VP_CTRL_VTGEN(v)	FLD_VAL(v, 4, 4) /* Use chip clock for timing */
518c2ecf20Sopenharmony_ci#define VP_CTRL_EVTMODE(v)	FLD_VAL(v, 5, 5) /* Event mode */
528c2ecf20Sopenharmony_ci#define VP_CTRL_RGB888(v)	FLD_VAL(v, 8, 8) /* RGB888 mode */
538c2ecf20Sopenharmony_ci#define VP_CTRL_VSDELAY(v)	FLD_VAL(v, 31, 20) /* VSYNC delay */
548c2ecf20Sopenharmony_ci#define VP_CTRL_HSPOL		BIT(17) /* Polarity of HSYNC signal */
558c2ecf20Sopenharmony_ci#define VP_CTRL_DEPOL		BIT(18) /* Polarity of DE signal */
568c2ecf20Sopenharmony_ci#define VP_CTRL_VSPOL		BIT(19) /* Polarity of VSYNC signal */
578c2ecf20Sopenharmony_ci#define VP_HTIM1		0x0454 /* Horizontal Timing Control 1 */
588c2ecf20Sopenharmony_ci#define VP_HTIM1_HBP(v)		FLD_VAL(v, 24, 16)
598c2ecf20Sopenharmony_ci#define VP_HTIM1_HSYNC(v)	FLD_VAL(v, 8, 0)
608c2ecf20Sopenharmony_ci#define VP_HTIM2		0x0458 /* Horizontal Timing Control 2 */
618c2ecf20Sopenharmony_ci#define VP_HTIM2_HFP(v)		FLD_VAL(v, 24, 16)
628c2ecf20Sopenharmony_ci#define VP_HTIM2_HACT(v)	FLD_VAL(v, 10, 0)
638c2ecf20Sopenharmony_ci#define VP_VTIM1		0x045C /* Vertical Timing Control 1 */
648c2ecf20Sopenharmony_ci#define VP_VTIM1_VBP(v)		FLD_VAL(v, 23, 16)
658c2ecf20Sopenharmony_ci#define VP_VTIM1_VSYNC(v)	FLD_VAL(v, 7, 0)
668c2ecf20Sopenharmony_ci#define VP_VTIM2		0x0460 /* Vertical Timing Control 2 */
678c2ecf20Sopenharmony_ci#define VP_VTIM2_VFP(v)		FLD_VAL(v, 23, 16)
688c2ecf20Sopenharmony_ci#define VP_VTIM2_VACT(v)	FLD_VAL(v, 10, 0)
698c2ecf20Sopenharmony_ci#define VP_VFUEN		0x0464 /* Video Frame Timing Update Enable */
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/* LVDS registers */
728c2ecf20Sopenharmony_ci#define LV_MX0003		0x0480 /* Mux input bit 0 to 3 */
738c2ecf20Sopenharmony_ci#define LV_MX0407		0x0484 /* Mux input bit 4 to 7 */
748c2ecf20Sopenharmony_ci#define LV_MX0811		0x0488 /* Mux input bit 8 to 11 */
758c2ecf20Sopenharmony_ci#define LV_MX1215		0x048C /* Mux input bit 12 to 15 */
768c2ecf20Sopenharmony_ci#define LV_MX1619		0x0490 /* Mux input bit 16 to 19 */
778c2ecf20Sopenharmony_ci#define LV_MX2023		0x0494 /* Mux input bit 20 to 23 */
788c2ecf20Sopenharmony_ci#define LV_MX2427		0x0498 /* Mux input bit 24 to 27 */
798c2ecf20Sopenharmony_ci#define LV_MX(b0, b1, b2, b3)	(FLD_VAL(b0, 4, 0) | FLD_VAL(b1, 12, 8) | \
808c2ecf20Sopenharmony_ci				FLD_VAL(b2, 20, 16) | FLD_VAL(b3, 28, 24))
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci/* Input bit numbers used in mux registers */
838c2ecf20Sopenharmony_cienum {
848c2ecf20Sopenharmony_ci	LVI_R0,
858c2ecf20Sopenharmony_ci	LVI_R1,
868c2ecf20Sopenharmony_ci	LVI_R2,
878c2ecf20Sopenharmony_ci	LVI_R3,
888c2ecf20Sopenharmony_ci	LVI_R4,
898c2ecf20Sopenharmony_ci	LVI_R5,
908c2ecf20Sopenharmony_ci	LVI_R6,
918c2ecf20Sopenharmony_ci	LVI_R7,
928c2ecf20Sopenharmony_ci	LVI_G0,
938c2ecf20Sopenharmony_ci	LVI_G1,
948c2ecf20Sopenharmony_ci	LVI_G2,
958c2ecf20Sopenharmony_ci	LVI_G3,
968c2ecf20Sopenharmony_ci	LVI_G4,
978c2ecf20Sopenharmony_ci	LVI_G5,
988c2ecf20Sopenharmony_ci	LVI_G6,
998c2ecf20Sopenharmony_ci	LVI_G7,
1008c2ecf20Sopenharmony_ci	LVI_B0,
1018c2ecf20Sopenharmony_ci	LVI_B1,
1028c2ecf20Sopenharmony_ci	LVI_B2,
1038c2ecf20Sopenharmony_ci	LVI_B3,
1048c2ecf20Sopenharmony_ci	LVI_B4,
1058c2ecf20Sopenharmony_ci	LVI_B5,
1068c2ecf20Sopenharmony_ci	LVI_B6,
1078c2ecf20Sopenharmony_ci	LVI_B7,
1088c2ecf20Sopenharmony_ci	LVI_HS,
1098c2ecf20Sopenharmony_ci	LVI_VS,
1108c2ecf20Sopenharmony_ci	LVI_DE,
1118c2ecf20Sopenharmony_ci	LVI_L0
1128c2ecf20Sopenharmony_ci};
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci#define LV_CFG			0x049C /* LVDS Configuration */
1158c2ecf20Sopenharmony_ci#define LV_PHY0			0x04A0 /* LVDS PHY 0 */
1168c2ecf20Sopenharmony_ci#define LV_PHY0_RST(v)		FLD_VAL(v, 22, 22) /* PHY reset */
1178c2ecf20Sopenharmony_ci#define LV_PHY0_IS(v)		FLD_VAL(v, 15, 14)
1188c2ecf20Sopenharmony_ci#define LV_PHY0_ND(v)		FLD_VAL(v, 4, 0) /* Frequency range select */
1198c2ecf20Sopenharmony_ci#define LV_PHY0_PRBS_ON(v)	FLD_VAL(v, 20, 16) /* Clock/Data Flag pins */
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci/* System registers */
1228c2ecf20Sopenharmony_ci#define SYS_RST			0x0504 /* System Reset */
1238c2ecf20Sopenharmony_ci#define SYS_ID			0x0580 /* System ID */
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci#define SYS_RST_I2CS		BIT(0) /* Reset I2C-Slave controller */
1268c2ecf20Sopenharmony_ci#define SYS_RST_I2CM		BIT(1) /* Reset I2C-Master controller */
1278c2ecf20Sopenharmony_ci#define SYS_RST_LCD		BIT(2) /* Reset LCD controller */
1288c2ecf20Sopenharmony_ci#define SYS_RST_BM		BIT(3) /* Reset Bus Management controller */
1298c2ecf20Sopenharmony_ci#define SYS_RST_DSIRX		BIT(4) /* Reset DSI-RX and App controller */
1308c2ecf20Sopenharmony_ci#define SYS_RST_REG		BIT(5) /* Reset Register module */
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci#define LPX_PERIOD		2
1338c2ecf20Sopenharmony_ci#define TTA_SURE		3
1348c2ecf20Sopenharmony_ci#define TTA_GET			0x20000
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci/* Lane enable PPI and DSI register bits */
1378c2ecf20Sopenharmony_ci#define LANEENABLE_CLEN		BIT(0)
1388c2ecf20Sopenharmony_ci#define LANEENABLE_L0EN		BIT(1)
1398c2ecf20Sopenharmony_ci#define LANEENABLE_L1EN		BIT(2)
1408c2ecf20Sopenharmony_ci#define LANEENABLE_L2EN		BIT(3)
1418c2ecf20Sopenharmony_ci#define LANEENABLE_L3EN		BIT(4)
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci/* LVCFG fields */
1448c2ecf20Sopenharmony_ci#define LV_CFG_LVEN		BIT(0)
1458c2ecf20Sopenharmony_ci#define LV_CFG_LVDLINK		BIT(1)
1468c2ecf20Sopenharmony_ci#define LV_CFG_CLKPOL1		BIT(2)
1478c2ecf20Sopenharmony_ci#define LV_CFG_CLKPOL2		BIT(3)
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistatic const char * const tc358764_supplies[] = {
1508c2ecf20Sopenharmony_ci	"vddc", "vddio", "vddlvds"
1518c2ecf20Sopenharmony_ci};
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistruct tc358764 {
1548c2ecf20Sopenharmony_ci	struct device *dev;
1558c2ecf20Sopenharmony_ci	struct drm_bridge bridge;
1568c2ecf20Sopenharmony_ci	struct regulator_bulk_data supplies[ARRAY_SIZE(tc358764_supplies)];
1578c2ecf20Sopenharmony_ci	struct gpio_desc *gpio_reset;
1588c2ecf20Sopenharmony_ci	struct drm_bridge *panel_bridge;
1598c2ecf20Sopenharmony_ci	int error;
1608c2ecf20Sopenharmony_ci};
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic int tc358764_clear_error(struct tc358764 *ctx)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	int ret = ctx->error;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	ctx->error = 0;
1678c2ecf20Sopenharmony_ci	return ret;
1688c2ecf20Sopenharmony_ci}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistatic void tc358764_read(struct tc358764 *ctx, u16 addr, u32 *val)
1718c2ecf20Sopenharmony_ci{
1728c2ecf20Sopenharmony_ci	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
1738c2ecf20Sopenharmony_ci	ssize_t ret;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	if (ctx->error)
1768c2ecf20Sopenharmony_ci		return;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	cpu_to_le16s(&addr);
1798c2ecf20Sopenharmony_ci	ret = mipi_dsi_generic_read(dsi, &addr, sizeof(addr), val, sizeof(*val));
1808c2ecf20Sopenharmony_ci	if (ret >= 0)
1818c2ecf20Sopenharmony_ci		le32_to_cpus(val);
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	dev_dbg(ctx->dev, "read: addr=0x%04x data=0x%08x\n", addr, *val);
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic void tc358764_write(struct tc358764 *ctx, u16 addr, u32 val)
1878c2ecf20Sopenharmony_ci{
1888c2ecf20Sopenharmony_ci	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
1898c2ecf20Sopenharmony_ci	ssize_t ret;
1908c2ecf20Sopenharmony_ci	u8 data[6];
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	if (ctx->error)
1938c2ecf20Sopenharmony_ci		return;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	data[0] = addr;
1968c2ecf20Sopenharmony_ci	data[1] = addr >> 8;
1978c2ecf20Sopenharmony_ci	data[2] = val;
1988c2ecf20Sopenharmony_ci	data[3] = val >> 8;
1998c2ecf20Sopenharmony_ci	data[4] = val >> 16;
2008c2ecf20Sopenharmony_ci	data[5] = val >> 24;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	ret = mipi_dsi_generic_write(dsi, data, sizeof(data));
2038c2ecf20Sopenharmony_ci	if (ret < 0)
2048c2ecf20Sopenharmony_ci		ctx->error = ret;
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic inline struct tc358764 *bridge_to_tc358764(struct drm_bridge *bridge)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	return container_of(bridge, struct tc358764, bridge);
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cistatic int tc358764_init(struct tc358764 *ctx)
2138c2ecf20Sopenharmony_ci{
2148c2ecf20Sopenharmony_ci	u32 v = 0;
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	tc358764_read(ctx, SYS_ID, &v);
2178c2ecf20Sopenharmony_ci	if (ctx->error)
2188c2ecf20Sopenharmony_ci		return tc358764_clear_error(ctx);
2198c2ecf20Sopenharmony_ci	dev_info(ctx->dev, "ID: %#x\n", v);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	/* configure PPI counters */
2228c2ecf20Sopenharmony_ci	tc358764_write(ctx, PPI_TX_RX_TA, TTA_GET | TTA_SURE);
2238c2ecf20Sopenharmony_ci	tc358764_write(ctx, PPI_LPTXTIMECNT, LPX_PERIOD);
2248c2ecf20Sopenharmony_ci	tc358764_write(ctx, PPI_D0S_CLRSIPOCOUNT, 5);
2258c2ecf20Sopenharmony_ci	tc358764_write(ctx, PPI_D1S_CLRSIPOCOUNT, 5);
2268c2ecf20Sopenharmony_ci	tc358764_write(ctx, PPI_D2S_CLRSIPOCOUNT, 5);
2278c2ecf20Sopenharmony_ci	tc358764_write(ctx, PPI_D3S_CLRSIPOCOUNT, 5);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	/* enable four data lanes and clock lane */
2308c2ecf20Sopenharmony_ci	tc358764_write(ctx, PPI_LANEENABLE, LANEENABLE_L3EN | LANEENABLE_L2EN |
2318c2ecf20Sopenharmony_ci		       LANEENABLE_L1EN | LANEENABLE_L0EN | LANEENABLE_CLEN);
2328c2ecf20Sopenharmony_ci	tc358764_write(ctx, DSI_LANEENABLE, LANEENABLE_L3EN | LANEENABLE_L2EN |
2338c2ecf20Sopenharmony_ci		       LANEENABLE_L1EN | LANEENABLE_L0EN | LANEENABLE_CLEN);
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	/* start */
2368c2ecf20Sopenharmony_ci	tc358764_write(ctx, PPI_STARTPPI, PPI_START_FUNCTION);
2378c2ecf20Sopenharmony_ci	tc358764_write(ctx, DSI_STARTDSI, DSI_RX_START);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	/* configure video path */
2408c2ecf20Sopenharmony_ci	tc358764_write(ctx, VP_CTRL, VP_CTRL_VSDELAY(15) | VP_CTRL_RGB888(1) |
2418c2ecf20Sopenharmony_ci		       VP_CTRL_EVTMODE(1) | VP_CTRL_HSPOL | VP_CTRL_VSPOL);
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	/* reset PHY */
2448c2ecf20Sopenharmony_ci	tc358764_write(ctx, LV_PHY0, LV_PHY0_RST(1) |
2458c2ecf20Sopenharmony_ci		       LV_PHY0_PRBS_ON(4) | LV_PHY0_IS(2) | LV_PHY0_ND(6));
2468c2ecf20Sopenharmony_ci	tc358764_write(ctx, LV_PHY0, LV_PHY0_PRBS_ON(4) | LV_PHY0_IS(2) |
2478c2ecf20Sopenharmony_ci		       LV_PHY0_ND(6));
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	/* reset bridge */
2508c2ecf20Sopenharmony_ci	tc358764_write(ctx, SYS_RST, SYS_RST_LCD);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	/* set bit order */
2538c2ecf20Sopenharmony_ci	tc358764_write(ctx, LV_MX0003, LV_MX(LVI_R0, LVI_R1, LVI_R2, LVI_R3));
2548c2ecf20Sopenharmony_ci	tc358764_write(ctx, LV_MX0407, LV_MX(LVI_R4, LVI_R7, LVI_R5, LVI_G0));
2558c2ecf20Sopenharmony_ci	tc358764_write(ctx, LV_MX0811, LV_MX(LVI_G1, LVI_G2, LVI_G6, LVI_G7));
2568c2ecf20Sopenharmony_ci	tc358764_write(ctx, LV_MX1215, LV_MX(LVI_G3, LVI_G4, LVI_G5, LVI_B0));
2578c2ecf20Sopenharmony_ci	tc358764_write(ctx, LV_MX1619, LV_MX(LVI_B6, LVI_B7, LVI_B1, LVI_B2));
2588c2ecf20Sopenharmony_ci	tc358764_write(ctx, LV_MX2023, LV_MX(LVI_B3, LVI_B4, LVI_B5, LVI_L0));
2598c2ecf20Sopenharmony_ci	tc358764_write(ctx, LV_MX2427, LV_MX(LVI_HS, LVI_VS, LVI_DE, LVI_R6));
2608c2ecf20Sopenharmony_ci	tc358764_write(ctx, LV_CFG, LV_CFG_CLKPOL2 | LV_CFG_CLKPOL1 |
2618c2ecf20Sopenharmony_ci		       LV_CFG_LVEN);
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	return tc358764_clear_error(ctx);
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic void tc358764_reset(struct tc358764 *ctx)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci	gpiod_set_value(ctx->gpio_reset, 1);
2698c2ecf20Sopenharmony_ci	usleep_range(1000, 2000);
2708c2ecf20Sopenharmony_ci	gpiod_set_value(ctx->gpio_reset, 0);
2718c2ecf20Sopenharmony_ci	usleep_range(1000, 2000);
2728c2ecf20Sopenharmony_ci}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_cistatic void tc358764_post_disable(struct drm_bridge *bridge)
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	struct tc358764 *ctx = bridge_to_tc358764(bridge);
2778c2ecf20Sopenharmony_ci	int ret;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	tc358764_reset(ctx);
2808c2ecf20Sopenharmony_ci	usleep_range(10000, 15000);
2818c2ecf20Sopenharmony_ci	ret = regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
2828c2ecf20Sopenharmony_ci	if (ret < 0)
2838c2ecf20Sopenharmony_ci		dev_err(ctx->dev, "error disabling regulators (%d)\n", ret);
2848c2ecf20Sopenharmony_ci}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_cistatic void tc358764_pre_enable(struct drm_bridge *bridge)
2878c2ecf20Sopenharmony_ci{
2888c2ecf20Sopenharmony_ci	struct tc358764 *ctx = bridge_to_tc358764(bridge);
2898c2ecf20Sopenharmony_ci	int ret;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
2928c2ecf20Sopenharmony_ci	if (ret < 0)
2938c2ecf20Sopenharmony_ci		dev_err(ctx->dev, "error enabling regulators (%d)\n", ret);
2948c2ecf20Sopenharmony_ci	usleep_range(10000, 15000);
2958c2ecf20Sopenharmony_ci	tc358764_reset(ctx);
2968c2ecf20Sopenharmony_ci	ret = tc358764_init(ctx);
2978c2ecf20Sopenharmony_ci	if (ret < 0)
2988c2ecf20Sopenharmony_ci		dev_err(ctx->dev, "error initializing bridge (%d)\n", ret);
2998c2ecf20Sopenharmony_ci}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_cistatic int tc358764_attach(struct drm_bridge *bridge,
3028c2ecf20Sopenharmony_ci			   enum drm_bridge_attach_flags flags)
3038c2ecf20Sopenharmony_ci{
3048c2ecf20Sopenharmony_ci	struct tc358764 *ctx = bridge_to_tc358764(bridge);
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	return drm_bridge_attach(bridge->encoder, ctx->panel_bridge,
3078c2ecf20Sopenharmony_ci				 bridge, flags);
3088c2ecf20Sopenharmony_ci}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_cistatic const struct drm_bridge_funcs tc358764_bridge_funcs = {
3118c2ecf20Sopenharmony_ci	.post_disable = tc358764_post_disable,
3128c2ecf20Sopenharmony_ci	.pre_enable = tc358764_pre_enable,
3138c2ecf20Sopenharmony_ci	.attach = tc358764_attach,
3148c2ecf20Sopenharmony_ci};
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_cistatic int tc358764_parse_dt(struct tc358764 *ctx)
3178c2ecf20Sopenharmony_ci{
3188c2ecf20Sopenharmony_ci	struct drm_bridge *panel_bridge;
3198c2ecf20Sopenharmony_ci	struct device *dev = ctx->dev;
3208c2ecf20Sopenharmony_ci	struct drm_panel *panel;
3218c2ecf20Sopenharmony_ci	int ret;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	ctx->gpio_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
3248c2ecf20Sopenharmony_ci	if (IS_ERR(ctx->gpio_reset)) {
3258c2ecf20Sopenharmony_ci		dev_err(dev, "no reset GPIO pin provided\n");
3268c2ecf20Sopenharmony_ci		return PTR_ERR(ctx->gpio_reset);
3278c2ecf20Sopenharmony_ci	}
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	ret = drm_of_find_panel_or_bridge(dev->of_node, 1, 0, &panel, NULL);
3308c2ecf20Sopenharmony_ci	if (ret)
3318c2ecf20Sopenharmony_ci		return ret;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	panel_bridge = devm_drm_panel_bridge_add(dev, panel);
3348c2ecf20Sopenharmony_ci	if (IS_ERR(panel_bridge))
3358c2ecf20Sopenharmony_ci		return PTR_ERR(panel_bridge);
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	ctx->panel_bridge = panel_bridge;
3388c2ecf20Sopenharmony_ci	return 0;
3398c2ecf20Sopenharmony_ci}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_cistatic int tc358764_configure_regulators(struct tc358764 *ctx)
3428c2ecf20Sopenharmony_ci{
3438c2ecf20Sopenharmony_ci	int i, ret;
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(ctx->supplies); ++i)
3468c2ecf20Sopenharmony_ci		ctx->supplies[i].supply = tc358764_supplies[i];
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	ret = devm_regulator_bulk_get(ctx->dev, ARRAY_SIZE(ctx->supplies),
3498c2ecf20Sopenharmony_ci				      ctx->supplies);
3508c2ecf20Sopenharmony_ci	if (ret < 0)
3518c2ecf20Sopenharmony_ci		dev_err(ctx->dev, "failed to get regulators: %d\n", ret);
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	return ret;
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_cistatic int tc358764_probe(struct mipi_dsi_device *dsi)
3578c2ecf20Sopenharmony_ci{
3588c2ecf20Sopenharmony_ci	struct device *dev = &dsi->dev;
3598c2ecf20Sopenharmony_ci	struct tc358764 *ctx;
3608c2ecf20Sopenharmony_ci	int ret;
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	ctx = devm_kzalloc(dev, sizeof(struct tc358764), GFP_KERNEL);
3638c2ecf20Sopenharmony_ci	if (!ctx)
3648c2ecf20Sopenharmony_ci		return -ENOMEM;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	mipi_dsi_set_drvdata(dsi, ctx);
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	ctx->dev = dev;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	dsi->lanes = 4;
3718c2ecf20Sopenharmony_ci	dsi->format = MIPI_DSI_FMT_RGB888;
3728c2ecf20Sopenharmony_ci	dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST
3738c2ecf20Sopenharmony_ci		| MIPI_DSI_MODE_VIDEO_AUTO_VERT | MIPI_DSI_MODE_LPM;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	ret = tc358764_parse_dt(ctx);
3768c2ecf20Sopenharmony_ci	if (ret < 0)
3778c2ecf20Sopenharmony_ci		return ret;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	ret = tc358764_configure_regulators(ctx);
3808c2ecf20Sopenharmony_ci	if (ret < 0)
3818c2ecf20Sopenharmony_ci		return ret;
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	ctx->bridge.funcs = &tc358764_bridge_funcs;
3848c2ecf20Sopenharmony_ci	ctx->bridge.type = DRM_MODE_CONNECTOR_LVDS;
3858c2ecf20Sopenharmony_ci	ctx->bridge.of_node = dev->of_node;
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	drm_bridge_add(&ctx->bridge);
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	ret = mipi_dsi_attach(dsi);
3908c2ecf20Sopenharmony_ci	if (ret < 0) {
3918c2ecf20Sopenharmony_ci		drm_bridge_remove(&ctx->bridge);
3928c2ecf20Sopenharmony_ci		dev_err(dev, "failed to attach dsi\n");
3938c2ecf20Sopenharmony_ci	}
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	return ret;
3968c2ecf20Sopenharmony_ci}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_cistatic int tc358764_remove(struct mipi_dsi_device *dsi)
3998c2ecf20Sopenharmony_ci{
4008c2ecf20Sopenharmony_ci	struct tc358764 *ctx = mipi_dsi_get_drvdata(dsi);
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	mipi_dsi_detach(dsi);
4038c2ecf20Sopenharmony_ci	drm_bridge_remove(&ctx->bridge);
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	return 0;
4068c2ecf20Sopenharmony_ci}
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_cistatic const struct of_device_id tc358764_of_match[] = {
4098c2ecf20Sopenharmony_ci	{ .compatible = "toshiba,tc358764" },
4108c2ecf20Sopenharmony_ci	{ }
4118c2ecf20Sopenharmony_ci};
4128c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, tc358764_of_match);
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_cistatic struct mipi_dsi_driver tc358764_driver = {
4158c2ecf20Sopenharmony_ci	.probe = tc358764_probe,
4168c2ecf20Sopenharmony_ci	.remove = tc358764_remove,
4178c2ecf20Sopenharmony_ci	.driver = {
4188c2ecf20Sopenharmony_ci		.name = "tc358764",
4198c2ecf20Sopenharmony_ci		.owner = THIS_MODULE,
4208c2ecf20Sopenharmony_ci		.of_match_table = tc358764_of_match,
4218c2ecf20Sopenharmony_ci	},
4228c2ecf20Sopenharmony_ci};
4238c2ecf20Sopenharmony_cimodule_mipi_dsi_driver(tc358764_driver);
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ciMODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
4268c2ecf20Sopenharmony_ciMODULE_AUTHOR("Maciej Purski <m.purski@samsung.com>");
4278c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("MIPI-DSI based Driver for TC358764 DSI/LVDS Bridge");
4288c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
429