162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2018 Samsung Electronics Co., Ltd
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Authors:
662306a36Sopenharmony_ci *	Andrzej Hajda <a.hajda@samsung.com>
762306a36Sopenharmony_ci *	Maciej Purski <m.purski@samsung.com>
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/delay.h>
1162306a36Sopenharmony_ci#include <linux/gpio/consumer.h>
1262306a36Sopenharmony_ci#include <linux/mod_devicetable.h>
1362306a36Sopenharmony_ci#include <linux/module.h>
1462306a36Sopenharmony_ci#include <linux/of_graph.h>
1562306a36Sopenharmony_ci#include <linux/regulator/consumer.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include <video/mipi_display.h>
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci#include <drm/drm_atomic_helper.h>
2062306a36Sopenharmony_ci#include <drm/drm_mipi_dsi.h>
2162306a36Sopenharmony_ci#include <drm/drm_of.h>
2262306a36Sopenharmony_ci#include <drm/drm_print.h>
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#define FLD_MASK(start, end)    (((1 << ((start) - (end) + 1)) - 1) << (end))
2562306a36Sopenharmony_ci#define FLD_VAL(val, start, end) (((val) << (end)) & FLD_MASK(start, end))
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci/* PPI layer registers */
2862306a36Sopenharmony_ci#define PPI_STARTPPI		0x0104 /* START control bit */
2962306a36Sopenharmony_ci#define PPI_LPTXTIMECNT		0x0114 /* LPTX timing signal */
3062306a36Sopenharmony_ci#define PPI_LANEENABLE		0x0134 /* Enables each lane */
3162306a36Sopenharmony_ci#define PPI_TX_RX_TA		0x013C /* BTA timing parameters */
3262306a36Sopenharmony_ci#define PPI_D0S_CLRSIPOCOUNT	0x0164 /* Assertion timer for Lane 0 */
3362306a36Sopenharmony_ci#define PPI_D1S_CLRSIPOCOUNT	0x0168 /* Assertion timer for Lane 1 */
3462306a36Sopenharmony_ci#define PPI_D2S_CLRSIPOCOUNT	0x016C /* Assertion timer for Lane 2 */
3562306a36Sopenharmony_ci#define PPI_D3S_CLRSIPOCOUNT	0x0170 /* Assertion timer for Lane 3 */
3662306a36Sopenharmony_ci#define PPI_START_FUNCTION	1
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci/* DSI layer registers */
3962306a36Sopenharmony_ci#define DSI_STARTDSI		0x0204 /* START control bit of DSI-TX */
4062306a36Sopenharmony_ci#define DSI_LANEENABLE		0x0210 /* Enables each lane */
4162306a36Sopenharmony_ci#define DSI_RX_START		1
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci/* Video path registers */
4462306a36Sopenharmony_ci#define VP_CTRL			0x0450 /* Video Path Control */
4562306a36Sopenharmony_ci#define VP_CTRL_MSF		BIT(0) /* Magic square in RGB666 */
4662306a36Sopenharmony_ci#define VP_CTRL_VTGEN		BIT(4) /* Use chip clock for timing */
4762306a36Sopenharmony_ci#define VP_CTRL_EVTMODE		BIT(5) /* Event mode */
4862306a36Sopenharmony_ci#define VP_CTRL_RGB888		BIT(8) /* RGB888 mode */
4962306a36Sopenharmony_ci#define VP_CTRL_VSDELAY(v)	FLD_VAL(v, 31, 20) /* VSYNC delay */
5062306a36Sopenharmony_ci#define VP_CTRL_HSPOL		BIT(17) /* Polarity of HSYNC signal */
5162306a36Sopenharmony_ci#define VP_CTRL_DEPOL		BIT(18) /* Polarity of DE signal */
5262306a36Sopenharmony_ci#define VP_CTRL_VSPOL		BIT(19) /* Polarity of VSYNC signal */
5362306a36Sopenharmony_ci#define VP_HTIM1		0x0454 /* Horizontal Timing Control 1 */
5462306a36Sopenharmony_ci#define VP_HTIM1_HBP(v)		FLD_VAL(v, 24, 16)
5562306a36Sopenharmony_ci#define VP_HTIM1_HSYNC(v)	FLD_VAL(v, 8, 0)
5662306a36Sopenharmony_ci#define VP_HTIM2		0x0458 /* Horizontal Timing Control 2 */
5762306a36Sopenharmony_ci#define VP_HTIM2_HFP(v)		FLD_VAL(v, 24, 16)
5862306a36Sopenharmony_ci#define VP_HTIM2_HACT(v)	FLD_VAL(v, 10, 0)
5962306a36Sopenharmony_ci#define VP_VTIM1		0x045C /* Vertical Timing Control 1 */
6062306a36Sopenharmony_ci#define VP_VTIM1_VBP(v)		FLD_VAL(v, 23, 16)
6162306a36Sopenharmony_ci#define VP_VTIM1_VSYNC(v)	FLD_VAL(v, 7, 0)
6262306a36Sopenharmony_ci#define VP_VTIM2		0x0460 /* Vertical Timing Control 2 */
6362306a36Sopenharmony_ci#define VP_VTIM2_VFP(v)		FLD_VAL(v, 23, 16)
6462306a36Sopenharmony_ci#define VP_VTIM2_VACT(v)	FLD_VAL(v, 10, 0)
6562306a36Sopenharmony_ci#define VP_VFUEN		0x0464 /* Video Frame Timing Update Enable */
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci/* LVDS registers */
6862306a36Sopenharmony_ci#define LV_MX0003		0x0480 /* Mux input bit 0 to 3 */
6962306a36Sopenharmony_ci#define LV_MX0407		0x0484 /* Mux input bit 4 to 7 */
7062306a36Sopenharmony_ci#define LV_MX0811		0x0488 /* Mux input bit 8 to 11 */
7162306a36Sopenharmony_ci#define LV_MX1215		0x048C /* Mux input bit 12 to 15 */
7262306a36Sopenharmony_ci#define LV_MX1619		0x0490 /* Mux input bit 16 to 19 */
7362306a36Sopenharmony_ci#define LV_MX2023		0x0494 /* Mux input bit 20 to 23 */
7462306a36Sopenharmony_ci#define LV_MX2427		0x0498 /* Mux input bit 24 to 27 */
7562306a36Sopenharmony_ci#define LV_MX(b0, b1, b2, b3)	(FLD_VAL(b0, 4, 0) | FLD_VAL(b1, 12, 8) | \
7662306a36Sopenharmony_ci				FLD_VAL(b2, 20, 16) | FLD_VAL(b3, 28, 24))
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci/* Input bit numbers used in mux registers */
7962306a36Sopenharmony_cienum {
8062306a36Sopenharmony_ci	LVI_R0,
8162306a36Sopenharmony_ci	LVI_R1,
8262306a36Sopenharmony_ci	LVI_R2,
8362306a36Sopenharmony_ci	LVI_R3,
8462306a36Sopenharmony_ci	LVI_R4,
8562306a36Sopenharmony_ci	LVI_R5,
8662306a36Sopenharmony_ci	LVI_R6,
8762306a36Sopenharmony_ci	LVI_R7,
8862306a36Sopenharmony_ci	LVI_G0,
8962306a36Sopenharmony_ci	LVI_G1,
9062306a36Sopenharmony_ci	LVI_G2,
9162306a36Sopenharmony_ci	LVI_G3,
9262306a36Sopenharmony_ci	LVI_G4,
9362306a36Sopenharmony_ci	LVI_G5,
9462306a36Sopenharmony_ci	LVI_G6,
9562306a36Sopenharmony_ci	LVI_G7,
9662306a36Sopenharmony_ci	LVI_B0,
9762306a36Sopenharmony_ci	LVI_B1,
9862306a36Sopenharmony_ci	LVI_B2,
9962306a36Sopenharmony_ci	LVI_B3,
10062306a36Sopenharmony_ci	LVI_B4,
10162306a36Sopenharmony_ci	LVI_B5,
10262306a36Sopenharmony_ci	LVI_B6,
10362306a36Sopenharmony_ci	LVI_B7,
10462306a36Sopenharmony_ci	LVI_HS,
10562306a36Sopenharmony_ci	LVI_VS,
10662306a36Sopenharmony_ci	LVI_DE,
10762306a36Sopenharmony_ci	LVI_L0
10862306a36Sopenharmony_ci};
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci#define LV_CFG			0x049C /* LVDS Configuration */
11162306a36Sopenharmony_ci#define LV_PHY0			0x04A0 /* LVDS PHY 0 */
11262306a36Sopenharmony_ci#define LV_PHY0_RST(v)		FLD_VAL(v, 22, 22) /* PHY reset */
11362306a36Sopenharmony_ci#define LV_PHY0_IS(v)		FLD_VAL(v, 15, 14)
11462306a36Sopenharmony_ci#define LV_PHY0_ND(v)		FLD_VAL(v, 4, 0) /* Frequency range select */
11562306a36Sopenharmony_ci#define LV_PHY0_PRBS_ON(v)	FLD_VAL(v, 20, 16) /* Clock/Data Flag pins */
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci/* System registers */
11862306a36Sopenharmony_ci#define SYS_RST			0x0504 /* System Reset */
11962306a36Sopenharmony_ci#define SYS_ID			0x0580 /* System ID */
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci#define SYS_RST_I2CS		BIT(0) /* Reset I2C-Slave controller */
12262306a36Sopenharmony_ci#define SYS_RST_I2CM		BIT(1) /* Reset I2C-Master controller */
12362306a36Sopenharmony_ci#define SYS_RST_LCD		BIT(2) /* Reset LCD controller */
12462306a36Sopenharmony_ci#define SYS_RST_BM		BIT(3) /* Reset Bus Management controller */
12562306a36Sopenharmony_ci#define SYS_RST_DSIRX		BIT(4) /* Reset DSI-RX and App controller */
12662306a36Sopenharmony_ci#define SYS_RST_REG		BIT(5) /* Reset Register module */
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci#define LPX_PERIOD		2
12962306a36Sopenharmony_ci#define TTA_SURE		3
13062306a36Sopenharmony_ci#define TTA_GET			0x20000
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci/* Lane enable PPI and DSI register bits */
13362306a36Sopenharmony_ci#define LANEENABLE_CLEN		BIT(0)
13462306a36Sopenharmony_ci#define LANEENABLE_L0EN		BIT(1)
13562306a36Sopenharmony_ci#define LANEENABLE_L1EN		BIT(2)
13662306a36Sopenharmony_ci#define LANEENABLE_L2EN		BIT(3)
13762306a36Sopenharmony_ci#define LANEENABLE_L3EN		BIT(4)
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci/* LVCFG fields */
14062306a36Sopenharmony_ci#define LV_CFG_LVEN		BIT(0)
14162306a36Sopenharmony_ci#define LV_CFG_LVDLINK		BIT(1)
14262306a36Sopenharmony_ci#define LV_CFG_CLKPOL1		BIT(2)
14362306a36Sopenharmony_ci#define LV_CFG_CLKPOL2		BIT(3)
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_cistatic const char * const tc358764_supplies[] = {
14662306a36Sopenharmony_ci	"vddc", "vddio", "vddlvds"
14762306a36Sopenharmony_ci};
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_cistruct tc358764 {
15062306a36Sopenharmony_ci	struct device *dev;
15162306a36Sopenharmony_ci	struct drm_bridge bridge;
15262306a36Sopenharmony_ci	struct drm_bridge *next_bridge;
15362306a36Sopenharmony_ci	struct regulator_bulk_data supplies[ARRAY_SIZE(tc358764_supplies)];
15462306a36Sopenharmony_ci	struct gpio_desc *gpio_reset;
15562306a36Sopenharmony_ci	int error;
15662306a36Sopenharmony_ci};
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_cistatic int tc358764_clear_error(struct tc358764 *ctx)
15962306a36Sopenharmony_ci{
16062306a36Sopenharmony_ci	int ret = ctx->error;
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci	ctx->error = 0;
16362306a36Sopenharmony_ci	return ret;
16462306a36Sopenharmony_ci}
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_cistatic void tc358764_read(struct tc358764 *ctx, u16 addr, u32 *val)
16762306a36Sopenharmony_ci{
16862306a36Sopenharmony_ci	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
16962306a36Sopenharmony_ci	ssize_t ret;
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci	if (ctx->error)
17262306a36Sopenharmony_ci		return;
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci	cpu_to_le16s(&addr);
17562306a36Sopenharmony_ci	ret = mipi_dsi_generic_read(dsi, &addr, sizeof(addr), val, sizeof(*val));
17662306a36Sopenharmony_ci	if (ret >= 0)
17762306a36Sopenharmony_ci		le32_to_cpus(val);
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci	dev_dbg(ctx->dev, "read: addr=0x%04x data=0x%08x\n", addr, *val);
18062306a36Sopenharmony_ci}
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_cistatic void tc358764_write(struct tc358764 *ctx, u16 addr, u32 val)
18362306a36Sopenharmony_ci{
18462306a36Sopenharmony_ci	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
18562306a36Sopenharmony_ci	ssize_t ret;
18662306a36Sopenharmony_ci	u8 data[6];
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci	if (ctx->error)
18962306a36Sopenharmony_ci		return;
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci	data[0] = addr;
19262306a36Sopenharmony_ci	data[1] = addr >> 8;
19362306a36Sopenharmony_ci	data[2] = val;
19462306a36Sopenharmony_ci	data[3] = val >> 8;
19562306a36Sopenharmony_ci	data[4] = val >> 16;
19662306a36Sopenharmony_ci	data[5] = val >> 24;
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci	ret = mipi_dsi_generic_write(dsi, data, sizeof(data));
19962306a36Sopenharmony_ci	if (ret < 0)
20062306a36Sopenharmony_ci		ctx->error = ret;
20162306a36Sopenharmony_ci}
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_cistatic inline struct tc358764 *bridge_to_tc358764(struct drm_bridge *bridge)
20462306a36Sopenharmony_ci{
20562306a36Sopenharmony_ci	return container_of(bridge, struct tc358764, bridge);
20662306a36Sopenharmony_ci}
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_cistatic int tc358764_init(struct tc358764 *ctx)
20962306a36Sopenharmony_ci{
21062306a36Sopenharmony_ci	u32 v = 0;
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci	tc358764_read(ctx, SYS_ID, &v);
21362306a36Sopenharmony_ci	if (ctx->error)
21462306a36Sopenharmony_ci		return tc358764_clear_error(ctx);
21562306a36Sopenharmony_ci	dev_info(ctx->dev, "ID: %#x\n", v);
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci	/* configure PPI counters */
21862306a36Sopenharmony_ci	tc358764_write(ctx, PPI_TX_RX_TA, TTA_GET | TTA_SURE);
21962306a36Sopenharmony_ci	tc358764_write(ctx, PPI_LPTXTIMECNT, LPX_PERIOD);
22062306a36Sopenharmony_ci	tc358764_write(ctx, PPI_D0S_CLRSIPOCOUNT, 5);
22162306a36Sopenharmony_ci	tc358764_write(ctx, PPI_D1S_CLRSIPOCOUNT, 5);
22262306a36Sopenharmony_ci	tc358764_write(ctx, PPI_D2S_CLRSIPOCOUNT, 5);
22362306a36Sopenharmony_ci	tc358764_write(ctx, PPI_D3S_CLRSIPOCOUNT, 5);
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci	/* enable four data lanes and clock lane */
22662306a36Sopenharmony_ci	tc358764_write(ctx, PPI_LANEENABLE, LANEENABLE_L3EN | LANEENABLE_L2EN |
22762306a36Sopenharmony_ci		       LANEENABLE_L1EN | LANEENABLE_L0EN | LANEENABLE_CLEN);
22862306a36Sopenharmony_ci	tc358764_write(ctx, DSI_LANEENABLE, LANEENABLE_L3EN | LANEENABLE_L2EN |
22962306a36Sopenharmony_ci		       LANEENABLE_L1EN | LANEENABLE_L0EN | LANEENABLE_CLEN);
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci	/* start */
23262306a36Sopenharmony_ci	tc358764_write(ctx, PPI_STARTPPI, PPI_START_FUNCTION);
23362306a36Sopenharmony_ci	tc358764_write(ctx, DSI_STARTDSI, DSI_RX_START);
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci	/* configure video path */
23662306a36Sopenharmony_ci	tc358764_write(ctx, VP_CTRL, VP_CTRL_VSDELAY(15) | VP_CTRL_RGB888 |
23762306a36Sopenharmony_ci		       VP_CTRL_EVTMODE | VP_CTRL_HSPOL | VP_CTRL_VSPOL);
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci	/* reset PHY */
24062306a36Sopenharmony_ci	tc358764_write(ctx, LV_PHY0, LV_PHY0_RST(1) |
24162306a36Sopenharmony_ci		       LV_PHY0_PRBS_ON(4) | LV_PHY0_IS(2) | LV_PHY0_ND(6));
24262306a36Sopenharmony_ci	tc358764_write(ctx, LV_PHY0, LV_PHY0_PRBS_ON(4) | LV_PHY0_IS(2) |
24362306a36Sopenharmony_ci		       LV_PHY0_ND(6));
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	/* reset bridge */
24662306a36Sopenharmony_ci	tc358764_write(ctx, SYS_RST, SYS_RST_LCD);
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci	/* set bit order */
24962306a36Sopenharmony_ci	tc358764_write(ctx, LV_MX0003, LV_MX(LVI_R0, LVI_R1, LVI_R2, LVI_R3));
25062306a36Sopenharmony_ci	tc358764_write(ctx, LV_MX0407, LV_MX(LVI_R4, LVI_R7, LVI_R5, LVI_G0));
25162306a36Sopenharmony_ci	tc358764_write(ctx, LV_MX0811, LV_MX(LVI_G1, LVI_G2, LVI_G6, LVI_G7));
25262306a36Sopenharmony_ci	tc358764_write(ctx, LV_MX1215, LV_MX(LVI_G3, LVI_G4, LVI_G5, LVI_B0));
25362306a36Sopenharmony_ci	tc358764_write(ctx, LV_MX1619, LV_MX(LVI_B6, LVI_B7, LVI_B1, LVI_B2));
25462306a36Sopenharmony_ci	tc358764_write(ctx, LV_MX2023, LV_MX(LVI_B3, LVI_B4, LVI_B5, LVI_L0));
25562306a36Sopenharmony_ci	tc358764_write(ctx, LV_MX2427, LV_MX(LVI_HS, LVI_VS, LVI_DE, LVI_R6));
25662306a36Sopenharmony_ci	tc358764_write(ctx, LV_CFG, LV_CFG_CLKPOL2 | LV_CFG_CLKPOL1 |
25762306a36Sopenharmony_ci		       LV_CFG_LVEN);
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_ci	return tc358764_clear_error(ctx);
26062306a36Sopenharmony_ci}
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_cistatic void tc358764_reset(struct tc358764 *ctx)
26362306a36Sopenharmony_ci{
26462306a36Sopenharmony_ci	gpiod_set_value(ctx->gpio_reset, 1);
26562306a36Sopenharmony_ci	usleep_range(1000, 2000);
26662306a36Sopenharmony_ci	gpiod_set_value(ctx->gpio_reset, 0);
26762306a36Sopenharmony_ci	usleep_range(1000, 2000);
26862306a36Sopenharmony_ci}
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_cistatic void tc358764_post_disable(struct drm_bridge *bridge)
27162306a36Sopenharmony_ci{
27262306a36Sopenharmony_ci	struct tc358764 *ctx = bridge_to_tc358764(bridge);
27362306a36Sopenharmony_ci	int ret;
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ci	tc358764_reset(ctx);
27662306a36Sopenharmony_ci	usleep_range(10000, 15000);
27762306a36Sopenharmony_ci	ret = regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
27862306a36Sopenharmony_ci	if (ret < 0)
27962306a36Sopenharmony_ci		dev_err(ctx->dev, "error disabling regulators (%d)\n", ret);
28062306a36Sopenharmony_ci}
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_cistatic void tc358764_pre_enable(struct drm_bridge *bridge)
28362306a36Sopenharmony_ci{
28462306a36Sopenharmony_ci	struct tc358764 *ctx = bridge_to_tc358764(bridge);
28562306a36Sopenharmony_ci	int ret;
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ci	ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
28862306a36Sopenharmony_ci	if (ret < 0)
28962306a36Sopenharmony_ci		dev_err(ctx->dev, "error enabling regulators (%d)\n", ret);
29062306a36Sopenharmony_ci	usleep_range(10000, 15000);
29162306a36Sopenharmony_ci	tc358764_reset(ctx);
29262306a36Sopenharmony_ci	ret = tc358764_init(ctx);
29362306a36Sopenharmony_ci	if (ret < 0)
29462306a36Sopenharmony_ci		dev_err(ctx->dev, "error initializing bridge (%d)\n", ret);
29562306a36Sopenharmony_ci}
29662306a36Sopenharmony_ci
29762306a36Sopenharmony_cistatic int tc358764_attach(struct drm_bridge *bridge,
29862306a36Sopenharmony_ci			   enum drm_bridge_attach_flags flags)
29962306a36Sopenharmony_ci{
30062306a36Sopenharmony_ci	struct tc358764 *ctx = bridge_to_tc358764(bridge);
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_ci	return drm_bridge_attach(bridge->encoder, ctx->next_bridge, bridge, flags);
30362306a36Sopenharmony_ci}
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_cistatic const struct drm_bridge_funcs tc358764_bridge_funcs = {
30662306a36Sopenharmony_ci	.post_disable = tc358764_post_disable,
30762306a36Sopenharmony_ci	.pre_enable = tc358764_pre_enable,
30862306a36Sopenharmony_ci	.attach = tc358764_attach,
30962306a36Sopenharmony_ci};
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_cistatic int tc358764_parse_dt(struct tc358764 *ctx)
31262306a36Sopenharmony_ci{
31362306a36Sopenharmony_ci	struct device *dev = ctx->dev;
31462306a36Sopenharmony_ci
31562306a36Sopenharmony_ci	ctx->gpio_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
31662306a36Sopenharmony_ci	if (IS_ERR(ctx->gpio_reset)) {
31762306a36Sopenharmony_ci		dev_err(dev, "no reset GPIO pin provided\n");
31862306a36Sopenharmony_ci		return PTR_ERR(ctx->gpio_reset);
31962306a36Sopenharmony_ci	}
32062306a36Sopenharmony_ci
32162306a36Sopenharmony_ci	ctx->next_bridge = devm_drm_of_get_bridge(dev, dev->of_node, 1, 0);
32262306a36Sopenharmony_ci	if (IS_ERR(ctx->next_bridge))
32362306a36Sopenharmony_ci		return PTR_ERR(ctx->next_bridge);
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ci	return 0;
32662306a36Sopenharmony_ci}
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_cistatic int tc358764_configure_regulators(struct tc358764 *ctx)
32962306a36Sopenharmony_ci{
33062306a36Sopenharmony_ci	int i, ret;
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(ctx->supplies); ++i)
33362306a36Sopenharmony_ci		ctx->supplies[i].supply = tc358764_supplies[i];
33462306a36Sopenharmony_ci
33562306a36Sopenharmony_ci	ret = devm_regulator_bulk_get(ctx->dev, ARRAY_SIZE(ctx->supplies),
33662306a36Sopenharmony_ci				      ctx->supplies);
33762306a36Sopenharmony_ci	if (ret < 0)
33862306a36Sopenharmony_ci		dev_err(ctx->dev, "failed to get regulators: %d\n", ret);
33962306a36Sopenharmony_ci
34062306a36Sopenharmony_ci	return ret;
34162306a36Sopenharmony_ci}
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_cistatic int tc358764_probe(struct mipi_dsi_device *dsi)
34462306a36Sopenharmony_ci{
34562306a36Sopenharmony_ci	struct device *dev = &dsi->dev;
34662306a36Sopenharmony_ci	struct tc358764 *ctx;
34762306a36Sopenharmony_ci	int ret;
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ci	ctx = devm_kzalloc(dev, sizeof(struct tc358764), GFP_KERNEL);
35062306a36Sopenharmony_ci	if (!ctx)
35162306a36Sopenharmony_ci		return -ENOMEM;
35262306a36Sopenharmony_ci
35362306a36Sopenharmony_ci	mipi_dsi_set_drvdata(dsi, ctx);
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci	ctx->dev = dev;
35662306a36Sopenharmony_ci
35762306a36Sopenharmony_ci	dsi->lanes = 4;
35862306a36Sopenharmony_ci	dsi->format = MIPI_DSI_FMT_RGB888;
35962306a36Sopenharmony_ci	dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST
36062306a36Sopenharmony_ci		| MIPI_DSI_MODE_VIDEO_AUTO_VERT | MIPI_DSI_MODE_LPM;
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ci	ret = tc358764_parse_dt(ctx);
36362306a36Sopenharmony_ci	if (ret < 0)
36462306a36Sopenharmony_ci		return ret;
36562306a36Sopenharmony_ci
36662306a36Sopenharmony_ci	ret = tc358764_configure_regulators(ctx);
36762306a36Sopenharmony_ci	if (ret < 0)
36862306a36Sopenharmony_ci		return ret;
36962306a36Sopenharmony_ci
37062306a36Sopenharmony_ci	ctx->bridge.funcs = &tc358764_bridge_funcs;
37162306a36Sopenharmony_ci	ctx->bridge.of_node = dev->of_node;
37262306a36Sopenharmony_ci	ctx->bridge.pre_enable_prev_first = true;
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_ci	drm_bridge_add(&ctx->bridge);
37562306a36Sopenharmony_ci
37662306a36Sopenharmony_ci	ret = mipi_dsi_attach(dsi);
37762306a36Sopenharmony_ci	if (ret < 0) {
37862306a36Sopenharmony_ci		drm_bridge_remove(&ctx->bridge);
37962306a36Sopenharmony_ci		dev_err(dev, "failed to attach dsi\n");
38062306a36Sopenharmony_ci	}
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_ci	return ret;
38362306a36Sopenharmony_ci}
38462306a36Sopenharmony_ci
38562306a36Sopenharmony_cistatic void tc358764_remove(struct mipi_dsi_device *dsi)
38662306a36Sopenharmony_ci{
38762306a36Sopenharmony_ci	struct tc358764 *ctx = mipi_dsi_get_drvdata(dsi);
38862306a36Sopenharmony_ci
38962306a36Sopenharmony_ci	mipi_dsi_detach(dsi);
39062306a36Sopenharmony_ci	drm_bridge_remove(&ctx->bridge);
39162306a36Sopenharmony_ci}
39262306a36Sopenharmony_ci
39362306a36Sopenharmony_cistatic const struct of_device_id tc358764_of_match[] = {
39462306a36Sopenharmony_ci	{ .compatible = "toshiba,tc358764" },
39562306a36Sopenharmony_ci	{ }
39662306a36Sopenharmony_ci};
39762306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, tc358764_of_match);
39862306a36Sopenharmony_ci
39962306a36Sopenharmony_cistatic struct mipi_dsi_driver tc358764_driver = {
40062306a36Sopenharmony_ci	.probe = tc358764_probe,
40162306a36Sopenharmony_ci	.remove = tc358764_remove,
40262306a36Sopenharmony_ci	.driver = {
40362306a36Sopenharmony_ci		.name = "tc358764",
40462306a36Sopenharmony_ci		.owner = THIS_MODULE,
40562306a36Sopenharmony_ci		.of_match_table = tc358764_of_match,
40662306a36Sopenharmony_ci	},
40762306a36Sopenharmony_ci};
40862306a36Sopenharmony_cimodule_mipi_dsi_driver(tc358764_driver);
40962306a36Sopenharmony_ci
41062306a36Sopenharmony_ciMODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
41162306a36Sopenharmony_ciMODULE_AUTHOR("Maciej Purski <m.purski@samsung.com>");
41262306a36Sopenharmony_ciMODULE_DESCRIPTION("MIPI-DSI based Driver for TC358764 DSI/LVDS Bridge");
41362306a36Sopenharmony_ciMODULE_LICENSE("GPL v2");
414