xref: /linux/drivers/gpu/drm/bridge/chipone-icn6211.c (revision 72bd9ea389c70ac948f48d20c0e4ae70c0153940)
1ce517f18SJagan Teki // SPDX-License-Identifier: GPL-2.0+
2ce517f18SJagan Teki /*
3ce517f18SJagan Teki  * Copyright (C) 2020 Amarula Solutions(India)
4ce517f18SJagan Teki  * Author: Jagan Teki <jagan@amarulasolutions.com>
5ce517f18SJagan Teki  */
6ce517f18SJagan Teki 
73b26a291SJagan Teki #include <drm/drm_atomic_helper.h>
8ce517f18SJagan Teki #include <drm/drm_of.h>
9ce517f18SJagan Teki #include <drm/drm_print.h>
10ce517f18SJagan Teki #include <drm/drm_mipi_dsi.h>
11ce517f18SJagan Teki 
129180c30cSJonathan Liu #include <linux/bitfield.h>
139180c30cSJonathan Liu #include <linux/bits.h>
14ce517f18SJagan Teki #include <linux/delay.h>
15ce517f18SJagan Teki #include <linux/gpio/consumer.h>
168dde6f74SMarek Vasut #include <linux/i2c.h>
17*72bd9ea3SVille Syrjälä #include <linux/media-bus-format.h>
18ce517f18SJagan Teki #include <linux/module.h>
19ce517f18SJagan Teki #include <linux/of_device.h>
20fb47723aSMarek Vasut #include <linux/regmap.h>
21ce517f18SJagan Teki #include <linux/regulator/consumer.h>
22ce517f18SJagan Teki 
232dcec57bSMarek Vasut #define VENDOR_ID		0x00
242dcec57bSMarek Vasut #define DEVICE_ID_H		0x01
252dcec57bSMarek Vasut #define DEVICE_ID_L		0x02
262dcec57bSMarek Vasut #define VERSION_ID		0x03
272dcec57bSMarek Vasut #define FIRMWARE_VERSION	0x08
282dcec57bSMarek Vasut #define CONFIG_FINISH		0x09
292dcec57bSMarek Vasut #define PD_CTRL(n)		(0x0a + ((n) & 0x3)) /* 0..3 */
302dcec57bSMarek Vasut #define RST_CTRL(n)		(0x0e + ((n) & 0x1)) /* 0..1 */
312dcec57bSMarek Vasut #define SYS_CTRL(n)		(0x10 + ((n) & 0x7)) /* 0..4 */
329180c30cSJonathan Liu #define SYS_CTRL_1_CLK_PHASE_MSK	GENMASK(5, 4)
339180c30cSJonathan Liu #define CLK_PHASE_0			0
349180c30cSJonathan Liu #define CLK_PHASE_1_4			1
359180c30cSJonathan Liu #define CLK_PHASE_1_2			2
369180c30cSJonathan Liu #define CLK_PHASE_3_4			3
372dcec57bSMarek Vasut #define RGB_DRV(n)		(0x18 + ((n) & 0x3)) /* 0..3 */
382dcec57bSMarek Vasut #define RGB_DLY(n)		(0x1c + ((n) & 0x1)) /* 0..1 */
392dcec57bSMarek Vasut #define RGB_TEST_CTRL		0x1e
402dcec57bSMarek Vasut #define ATE_PLL_EN		0x1f
41ce517f18SJagan Teki #define HACTIVE_LI		0x20
42ce517f18SJagan Teki #define VACTIVE_LI		0x21
43ce517f18SJagan Teki #define VACTIVE_HACTIVE_HI	0x22
44ce517f18SJagan Teki #define HFP_LI			0x23
45ce517f18SJagan Teki #define HSYNC_LI		0x24
46ce517f18SJagan Teki #define HBP_LI			0x25
47ce517f18SJagan Teki #define HFP_HSW_HBP_HI		0x26
48c0ff7a64SMarek Vasut #define HFP_HSW_HBP_HI_HFP(n)		(((n) & 0x300) >> 4)
49c0ff7a64SMarek Vasut #define HFP_HSW_HBP_HI_HS(n)		(((n) & 0x300) >> 6)
50c0ff7a64SMarek Vasut #define HFP_HSW_HBP_HI_HBP(n)		(((n) & 0x300) >> 8)
51ce517f18SJagan Teki #define VFP			0x27
52ce517f18SJagan Teki #define VSYNC			0x28
53ce517f18SJagan Teki #define VBP			0x29
542dcec57bSMarek Vasut #define BIST_POL		0x2a
552dcec57bSMarek Vasut #define BIST_POL_BIST_MODE(n)		(((n) & 0xf) << 4)
562dcec57bSMarek Vasut #define BIST_POL_BIST_GEN		BIT(3)
572dcec57bSMarek Vasut #define BIST_POL_HSYNC_POL		BIT(2)
582dcec57bSMarek Vasut #define BIST_POL_VSYNC_POL		BIT(1)
592dcec57bSMarek Vasut #define BIST_POL_DE_POL			BIT(0)
602dcec57bSMarek Vasut #define BIST_RED		0x2b
612dcec57bSMarek Vasut #define BIST_GREEN		0x2c
622dcec57bSMarek Vasut #define BIST_BLUE		0x2d
632dcec57bSMarek Vasut #define BIST_CHESS_X		0x2e
642dcec57bSMarek Vasut #define BIST_CHESS_Y		0x2f
652dcec57bSMarek Vasut #define BIST_CHESS_XY_H		0x30
662dcec57bSMarek Vasut #define BIST_FRAME_TIME_L	0x31
672dcec57bSMarek Vasut #define BIST_FRAME_TIME_H	0x32
682dcec57bSMarek Vasut #define FIFO_MAX_ADDR_LOW	0x33
692dcec57bSMarek Vasut #define SYNC_EVENT_DLY		0x34
702dcec57bSMarek Vasut #define HSW_MIN			0x35
712dcec57bSMarek Vasut #define HFP_MIN			0x36
722dcec57bSMarek Vasut #define LOGIC_RST_NUM		0x37
732dcec57bSMarek Vasut #define OSC_CTRL(n)		(0x48 + ((n) & 0x7)) /* 0..5 */
742dcec57bSMarek Vasut #define BG_CTRL			0x4e
752dcec57bSMarek Vasut #define LDO_PLL			0x4f
762dcec57bSMarek Vasut #define PLL_CTRL(n)		(0x50 + ((n) & 0xf)) /* 0..15 */
772dcec57bSMarek Vasut #define PLL_CTRL_6_EXTERNAL		0x90
782dcec57bSMarek Vasut #define PLL_CTRL_6_MIPI_CLK		0x92
792dcec57bSMarek Vasut #define PLL_CTRL_6_INTERNAL		0x93
802dcec57bSMarek Vasut #define PLL_REM(n)		(0x60 + ((n) & 0x3)) /* 0..2 */
812dcec57bSMarek Vasut #define PLL_DIV(n)		(0x63 + ((n) & 0x3)) /* 0..2 */
822dcec57bSMarek Vasut #define PLL_FRAC(n)		(0x66 + ((n) & 0x3)) /* 0..2 */
832dcec57bSMarek Vasut #define PLL_INT(n)		(0x69 + ((n) & 0x1)) /* 0..1 */
842dcec57bSMarek Vasut #define PLL_REF_DIV		0x6b
852dcec57bSMarek Vasut #define PLL_REF_DIV_P(n)		((n) & 0xf)
862dcec57bSMarek Vasut #define PLL_REF_DIV_Pe			BIT(4)
872dcec57bSMarek Vasut #define PLL_REF_DIV_S(n)		(((n) & 0x7) << 5)
882dcec57bSMarek Vasut #define PLL_SSC_P(n)		(0x6c + ((n) & 0x3)) /* 0..2 */
892dcec57bSMarek Vasut #define PLL_SSC_STEP(n)		(0x6f + ((n) & 0x3)) /* 0..2 */
902dcec57bSMarek Vasut #define PLL_SSC_OFFSET(n)	(0x72 + ((n) & 0x3)) /* 0..3 */
912dcec57bSMarek Vasut #define GPIO_OEN		0x79
922dcec57bSMarek Vasut #define MIPI_CFG_PW		0x7a
932dcec57bSMarek Vasut #define MIPI_CFG_PW_CONFIG_DSI		0xc1
942dcec57bSMarek Vasut #define MIPI_CFG_PW_CONFIG_I2C		0x3e
952dcec57bSMarek Vasut #define GPIO_SEL(n)		(0x7b + ((n) & 0x1)) /* 0..1 */
962dcec57bSMarek Vasut #define IRQ_SEL			0x7d
972dcec57bSMarek Vasut #define DBG_SEL			0x7e
982dcec57bSMarek Vasut #define DBG_SIGNAL		0x7f
992dcec57bSMarek Vasut #define MIPI_ERR_VECTOR_L	0x80
1002dcec57bSMarek Vasut #define MIPI_ERR_VECTOR_H	0x81
1012dcec57bSMarek Vasut #define MIPI_ERR_VECTOR_EN_L	0x82
1022dcec57bSMarek Vasut #define MIPI_ERR_VECTOR_EN_H	0x83
1032dcec57bSMarek Vasut #define MIPI_MAX_SIZE_L		0x84
1042dcec57bSMarek Vasut #define MIPI_MAX_SIZE_H		0x85
1052dcec57bSMarek Vasut #define DSI_CTRL		0x86
1062dcec57bSMarek Vasut #define DSI_CTRL_UNKNOWN		0x28
1072dcec57bSMarek Vasut #define DSI_CTRL_DSI_LANES(n)		((n) & 0x3)
1082dcec57bSMarek Vasut #define MIPI_PN_SWAP		0x87
1092dcec57bSMarek Vasut #define MIPI_PN_SWAP_CLK		BIT(4)
1102dcec57bSMarek Vasut #define MIPI_PN_SWAP_D(n)		BIT((n) & 0x3)
111a24191b1SMarek Vasut #define MIPI_SOT_SYNC_BIT(n)	(0x88 + ((n) & 0x1)) /* 0..1 */
1122dcec57bSMarek Vasut #define MIPI_ULPS_CTRL		0x8a
1132dcec57bSMarek Vasut #define MIPI_CLK_CHK_VAR	0x8e
1142dcec57bSMarek Vasut #define MIPI_CLK_CHK_INI	0x8f
1152dcec57bSMarek Vasut #define MIPI_T_TERM_EN		0x90
1162dcec57bSMarek Vasut #define MIPI_T_HS_SETTLE	0x91
1172dcec57bSMarek Vasut #define MIPI_T_TA_SURE_PRE	0x92
1182dcec57bSMarek Vasut #define MIPI_T_LPX_SET		0x94
1192dcec57bSMarek Vasut #define MIPI_T_CLK_MISS		0x95
1202dcec57bSMarek Vasut #define MIPI_INIT_TIME_L	0x96
1212dcec57bSMarek Vasut #define MIPI_INIT_TIME_H	0x97
1222dcec57bSMarek Vasut #define MIPI_T_CLK_TERM_EN	0x99
1232dcec57bSMarek Vasut #define MIPI_T_CLK_SETTLE	0x9a
1242dcec57bSMarek Vasut #define MIPI_TO_HS_RX_L		0x9e
1252dcec57bSMarek Vasut #define MIPI_TO_HS_RX_H		0x9f
126a24191b1SMarek Vasut #define MIPI_PHY(n)		(0xa0 + ((n) & 0x7)) /* 0..5 */
1272dcec57bSMarek Vasut #define MIPI_PD_RX		0xb0
1282dcec57bSMarek Vasut #define MIPI_PD_TERM		0xb1
1292dcec57bSMarek Vasut #define MIPI_PD_HSRX		0xb2
1302dcec57bSMarek Vasut #define MIPI_PD_LPTX		0xb3
1312dcec57bSMarek Vasut #define MIPI_PD_LPRX		0xb4
1322dcec57bSMarek Vasut #define MIPI_PD_CK_LANE		0xb5
1332dcec57bSMarek Vasut #define MIPI_FORCE_0		0xb6
1342dcec57bSMarek Vasut #define MIPI_RST_CTRL		0xb7
1352dcec57bSMarek Vasut #define MIPI_RST_NUM		0xb8
136a24191b1SMarek Vasut #define MIPI_DBG_SET(n)		(0xc0 + ((n) & 0xf)) /* 0..9 */
1372dcec57bSMarek Vasut #define MIPI_DBG_SEL		0xe0
1382dcec57bSMarek Vasut #define MIPI_DBG_DATA		0xe1
1392dcec57bSMarek Vasut #define MIPI_ATE_TEST_SEL	0xe2
140a24191b1SMarek Vasut #define MIPI_ATE_STATUS(n)	(0xe3 + ((n) & 0x1)) /* 0..1 */
141ce517f18SJagan Teki 
142ce517f18SJagan Teki struct chipone {
143ce517f18SJagan Teki 	struct device *dev;
144fb47723aSMarek Vasut 	struct regmap *regmap;
1458dde6f74SMarek Vasut 	struct i2c_client *client;
146ce517f18SJagan Teki 	struct drm_bridge bridge;
14750d76e3dSJagan Teki 	struct drm_display_mode mode;
148ce517f18SJagan Teki 	struct drm_bridge *panel_bridge;
149f30cf0ecSMarek Vasut 	struct mipi_dsi_device *dsi;
150ce517f18SJagan Teki 	struct gpio_desc *enable_gpio;
151ce517f18SJagan Teki 	struct regulator *vdd1;
152ce517f18SJagan Teki 	struct regulator *vdd2;
153ce517f18SJagan Teki 	struct regulator *vdd3;
1548dde6f74SMarek Vasut 	bool interface_i2c;
155ce517f18SJagan Teki };
156ce517f18SJagan Teki 
157fb47723aSMarek Vasut static const struct regmap_range chipone_dsi_readable_ranges[] = {
158fb47723aSMarek Vasut 	regmap_reg_range(VENDOR_ID, VERSION_ID),
159fb47723aSMarek Vasut 	regmap_reg_range(FIRMWARE_VERSION, PLL_SSC_OFFSET(3)),
160fb47723aSMarek Vasut 	regmap_reg_range(GPIO_OEN, MIPI_ULPS_CTRL),
161fb47723aSMarek Vasut 	regmap_reg_range(MIPI_CLK_CHK_VAR, MIPI_T_TA_SURE_PRE),
162fb47723aSMarek Vasut 	regmap_reg_range(MIPI_T_LPX_SET, MIPI_INIT_TIME_H),
163fb47723aSMarek Vasut 	regmap_reg_range(MIPI_T_CLK_TERM_EN, MIPI_T_CLK_SETTLE),
164a24191b1SMarek Vasut 	regmap_reg_range(MIPI_TO_HS_RX_L, MIPI_PHY(5)),
165fb47723aSMarek Vasut 	regmap_reg_range(MIPI_PD_RX, MIPI_RST_NUM),
166a24191b1SMarek Vasut 	regmap_reg_range(MIPI_DBG_SET(0), MIPI_DBG_SET(9)),
167a24191b1SMarek Vasut 	regmap_reg_range(MIPI_DBG_SEL, MIPI_ATE_STATUS(1)),
168fb47723aSMarek Vasut };
169fb47723aSMarek Vasut 
170fb47723aSMarek Vasut static const struct regmap_access_table chipone_dsi_readable_table = {
171fb47723aSMarek Vasut 	.yes_ranges = chipone_dsi_readable_ranges,
172fb47723aSMarek Vasut 	.n_yes_ranges = ARRAY_SIZE(chipone_dsi_readable_ranges),
173fb47723aSMarek Vasut };
174fb47723aSMarek Vasut 
175fb47723aSMarek Vasut static const struct regmap_range chipone_dsi_writeable_ranges[] = {
176fb47723aSMarek Vasut 	regmap_reg_range(CONFIG_FINISH, PLL_SSC_OFFSET(3)),
177fb47723aSMarek Vasut 	regmap_reg_range(GPIO_OEN, MIPI_ULPS_CTRL),
178fb47723aSMarek Vasut 	regmap_reg_range(MIPI_CLK_CHK_VAR, MIPI_T_TA_SURE_PRE),
179fb47723aSMarek Vasut 	regmap_reg_range(MIPI_T_LPX_SET, MIPI_INIT_TIME_H),
180fb47723aSMarek Vasut 	regmap_reg_range(MIPI_T_CLK_TERM_EN, MIPI_T_CLK_SETTLE),
181a24191b1SMarek Vasut 	regmap_reg_range(MIPI_TO_HS_RX_L, MIPI_PHY(5)),
182fb47723aSMarek Vasut 	regmap_reg_range(MIPI_PD_RX, MIPI_RST_NUM),
183a24191b1SMarek Vasut 	regmap_reg_range(MIPI_DBG_SET(0), MIPI_DBG_SET(9)),
184a24191b1SMarek Vasut 	regmap_reg_range(MIPI_DBG_SEL, MIPI_ATE_STATUS(1)),
185fb47723aSMarek Vasut };
186fb47723aSMarek Vasut 
187fb47723aSMarek Vasut static const struct regmap_access_table chipone_dsi_writeable_table = {
188fb47723aSMarek Vasut 	.yes_ranges = chipone_dsi_writeable_ranges,
189fb47723aSMarek Vasut 	.n_yes_ranges = ARRAY_SIZE(chipone_dsi_writeable_ranges),
190fb47723aSMarek Vasut };
191fb47723aSMarek Vasut 
192fb47723aSMarek Vasut static const struct regmap_config chipone_regmap_config = {
193fb47723aSMarek Vasut 	.reg_bits = 8,
194fb47723aSMarek Vasut 	.val_bits = 8,
195fb47723aSMarek Vasut 	.rd_table = &chipone_dsi_readable_table,
196fb47723aSMarek Vasut 	.wr_table = &chipone_dsi_writeable_table,
197fb47723aSMarek Vasut 	.cache_type = REGCACHE_RBTREE,
198a24191b1SMarek Vasut 	.max_register = MIPI_ATE_STATUS(1),
199fb47723aSMarek Vasut };
200fb47723aSMarek Vasut 
201fb47723aSMarek Vasut static int chipone_dsi_read(void *context,
202fb47723aSMarek Vasut 			    const void *reg, size_t reg_size,
203fb47723aSMarek Vasut 			    void *val, size_t val_size)
204fb47723aSMarek Vasut {
205fb47723aSMarek Vasut 	struct mipi_dsi_device *dsi = context;
206fb47723aSMarek Vasut 	const u16 reg16 = (val_size << 8) | *(u8 *)reg;
207fb47723aSMarek Vasut 	int ret;
208fb47723aSMarek Vasut 
209fb47723aSMarek Vasut 	ret = mipi_dsi_generic_read(dsi, &reg16, 2, val, val_size);
210fb47723aSMarek Vasut 
211fb47723aSMarek Vasut 	return ret == val_size ? 0 : -EINVAL;
212fb47723aSMarek Vasut }
213fb47723aSMarek Vasut 
214fb47723aSMarek Vasut static int chipone_dsi_write(void *context, const void *data, size_t count)
215fb47723aSMarek Vasut {
216fb47723aSMarek Vasut 	struct mipi_dsi_device *dsi = context;
217fb47723aSMarek Vasut 
218fb47723aSMarek Vasut 	return mipi_dsi_generic_write(dsi, data, 2);
219fb47723aSMarek Vasut }
220fb47723aSMarek Vasut 
221fb47723aSMarek Vasut static const struct regmap_bus chipone_dsi_regmap_bus = {
222fb47723aSMarek Vasut 	.read				= chipone_dsi_read,
223fb47723aSMarek Vasut 	.write				= chipone_dsi_write,
224fb47723aSMarek Vasut 	.reg_format_endian_default	= REGMAP_ENDIAN_NATIVE,
225fb47723aSMarek Vasut 	.val_format_endian_default	= REGMAP_ENDIAN_NATIVE,
226fb47723aSMarek Vasut };
227fb47723aSMarek Vasut 
228ce517f18SJagan Teki static inline struct chipone *bridge_to_chipone(struct drm_bridge *bridge)
229ce517f18SJagan Teki {
230ce517f18SJagan Teki 	return container_of(bridge, struct chipone, bridge);
231ce517f18SJagan Teki }
232ce517f18SJagan Teki 
23317a9c1aaSMarek Vasut static void chipone_readb(struct chipone *icn, u8 reg, u8 *val)
23417a9c1aaSMarek Vasut {
235fb47723aSMarek Vasut 	int ret, pval;
236fb47723aSMarek Vasut 
237fb47723aSMarek Vasut 	ret = regmap_read(icn->regmap, reg, &pval);
238fb47723aSMarek Vasut 
239fb47723aSMarek Vasut 	*val = ret ? 0 : pval & 0xff;
24017a9c1aaSMarek Vasut }
24117a9c1aaSMarek Vasut 
24233f1036bSMarek Vasut static int chipone_writeb(struct chipone *icn, u8 reg, u8 val)
243ce517f18SJagan Teki {
244fb47723aSMarek Vasut 	return regmap_write(icn->regmap, reg, val);
245ce517f18SJagan Teki }
246ce517f18SJagan Teki 
247f30cf0ecSMarek Vasut static void chipone_configure_pll(struct chipone *icn,
248f30cf0ecSMarek Vasut 				  const struct drm_display_mode *mode)
249f30cf0ecSMarek Vasut {
250f30cf0ecSMarek Vasut 	unsigned int best_p = 0, best_m = 0, best_s = 0;
25121d139a9SMarek Vasut 	unsigned int mode_clock = mode->clock * 1000;
252f30cf0ecSMarek Vasut 	unsigned int delta, min_delta = 0xffffffff;
253f30cf0ecSMarek Vasut 	unsigned int freq_p, freq_s, freq_out;
254f30cf0ecSMarek Vasut 	unsigned int p_min, p_max;
255f30cf0ecSMarek Vasut 	unsigned int p, m, s;
256f30cf0ecSMarek Vasut 	unsigned int fin;
25721d139a9SMarek Vasut 	bool best_p_pot;
25821d139a9SMarek Vasut 	u8 ref_div;
259f30cf0ecSMarek Vasut 
260f30cf0ecSMarek Vasut 	/*
26121d139a9SMarek Vasut 	 * DSI byte clock frequency (input into PLL) is calculated as:
26221d139a9SMarek Vasut 	 *  DSI_CLK = mode clock * bpp / dsi_data_lanes / 8
263f30cf0ecSMarek Vasut 	 *
264f30cf0ecSMarek Vasut 	 * DPI pixel clock frequency (output from PLL) is mode clock.
265f30cf0ecSMarek Vasut 	 *
266f30cf0ecSMarek Vasut 	 * The chip contains fractional PLL which works as follows:
267f30cf0ecSMarek Vasut 	 *  DPI_CLK = ((DSI_CLK / P) * M) / S
26821d139a9SMarek Vasut 	 * P is pre-divider, register PLL_REF_DIV[3:0] is 1:n divider
269f30cf0ecSMarek Vasut 	 *                   register PLL_REF_DIV[4] is extra 1:2 divider
270f30cf0ecSMarek Vasut 	 * M is integer multiplier, register PLL_INT(0) is multiplier
271f30cf0ecSMarek Vasut 	 * S is post-divider, register PLL_REF_DIV[7:5] is 2^(n+1) divider
272f30cf0ecSMarek Vasut 	 *
273f30cf0ecSMarek Vasut 	 * It seems the PLL input clock after applying P pre-divider have
274f30cf0ecSMarek Vasut 	 * to be lower than 20 MHz.
275f30cf0ecSMarek Vasut 	 */
27621d139a9SMarek Vasut 	fin = mode_clock * mipi_dsi_pixel_format_to_bpp(icn->dsi->format) /
27721d139a9SMarek Vasut 	      icn->dsi->lanes / 8; /* in Hz */
278f30cf0ecSMarek Vasut 
279f30cf0ecSMarek Vasut 	/* Minimum value of P predivider for PLL input in 5..20 MHz */
28021d139a9SMarek Vasut 	p_min = clamp(DIV_ROUND_UP(fin, 20000000), 1U, 31U);
28121d139a9SMarek Vasut 	p_max = clamp(fin / 5000000, 1U, 31U);
282f30cf0ecSMarek Vasut 
283f30cf0ecSMarek Vasut 	for (p = p_min; p < p_max; p++) {	/* PLL_REF_DIV[4,3:0] */
28421d139a9SMarek Vasut 		if (p > 16 && p & 1)		/* P > 16 uses extra /2 */
28521d139a9SMarek Vasut 			continue;
28621d139a9SMarek Vasut 		freq_p = fin / p;
287f30cf0ecSMarek Vasut 		if (freq_p == 0)		/* Divider too high */
288f30cf0ecSMarek Vasut 			break;
289f30cf0ecSMarek Vasut 
290f30cf0ecSMarek Vasut 		for (s = 0; s < 0x7; s++) {	/* PLL_REF_DIV[7:5] */
291f30cf0ecSMarek Vasut 			freq_s = freq_p / BIT(s + 1);
292f30cf0ecSMarek Vasut 			if (freq_s == 0)	/* Divider too high */
293f30cf0ecSMarek Vasut 				break;
294f30cf0ecSMarek Vasut 
29521d139a9SMarek Vasut 			m = mode_clock / freq_s;
296f30cf0ecSMarek Vasut 
297f30cf0ecSMarek Vasut 			/* Multiplier is 8 bit */
298f30cf0ecSMarek Vasut 			if (m > 0xff)
299f30cf0ecSMarek Vasut 				continue;
300f30cf0ecSMarek Vasut 
301f30cf0ecSMarek Vasut 			/* Limit PLL VCO frequency to 1 GHz */
30221d139a9SMarek Vasut 			freq_out = (fin * m) / p;
30321d139a9SMarek Vasut 			if (freq_out > 1000000000)
304f30cf0ecSMarek Vasut 				continue;
305f30cf0ecSMarek Vasut 
306f30cf0ecSMarek Vasut 			/* Apply post-divider */
307f30cf0ecSMarek Vasut 			freq_out /= BIT(s + 1);
308f30cf0ecSMarek Vasut 
30921d139a9SMarek Vasut 			delta = abs(mode_clock - freq_out);
310f30cf0ecSMarek Vasut 			if (delta < min_delta) {
311f30cf0ecSMarek Vasut 				best_p = p;
312f30cf0ecSMarek Vasut 				best_m = m;
313f30cf0ecSMarek Vasut 				best_s = s;
314f30cf0ecSMarek Vasut 				min_delta = delta;
315f30cf0ecSMarek Vasut 			}
316f30cf0ecSMarek Vasut 		}
317f30cf0ecSMarek Vasut 	}
318f30cf0ecSMarek Vasut 
31921d139a9SMarek Vasut 	best_p_pot = !(best_p & 1);
32021d139a9SMarek Vasut 
321f30cf0ecSMarek Vasut 	dev_dbg(icn->dev,
32221d139a9SMarek Vasut 		"PLL: P[3:0]=%d P[4]=2*%d M=%d S[7:5]=2^%d delta=%d => DSI f_in=%d Hz ; DPI f_out=%d Hz\n",
32321d139a9SMarek Vasut 		best_p >> best_p_pot, best_p_pot, best_m, best_s + 1,
32421d139a9SMarek Vasut 		min_delta, fin, (fin * best_m) / (best_p << (best_s + 1)));
32521d139a9SMarek Vasut 
32621d139a9SMarek Vasut 	ref_div = PLL_REF_DIV_P(best_p >> best_p_pot) | PLL_REF_DIV_S(best_s);
32721d139a9SMarek Vasut 	if (best_p_pot)	/* Prefer /2 pre-divider */
32821d139a9SMarek Vasut 		ref_div |= PLL_REF_DIV_Pe;
329f30cf0ecSMarek Vasut 
330f30cf0ecSMarek Vasut 	/* Clock source selection fixed to MIPI DSI clock lane */
33133f1036bSMarek Vasut 	chipone_writeb(icn, PLL_CTRL(6), PLL_CTRL_6_MIPI_CLK);
33221d139a9SMarek Vasut 	chipone_writeb(icn, PLL_REF_DIV, ref_div);
33333f1036bSMarek Vasut 	chipone_writeb(icn, PLL_INT(0), best_m);
334f30cf0ecSMarek Vasut }
335f30cf0ecSMarek Vasut 
3363b26a291SJagan Teki static void chipone_atomic_enable(struct drm_bridge *bridge,
3373b26a291SJagan Teki 				  struct drm_bridge_state *old_bridge_state)
338ce517f18SJagan Teki {
339ce517f18SJagan Teki 	struct chipone *icn = bridge_to_chipone(bridge);
3402dff97f2SMarek Vasut 	struct drm_atomic_state *state = old_bridge_state->base.state;
34150d76e3dSJagan Teki 	struct drm_display_mode *mode = &icn->mode;
3422dff97f2SMarek Vasut 	const struct drm_bridge_state *bridge_state;
343c0ff7a64SMarek Vasut 	u16 hfp, hbp, hsync;
3442dff97f2SMarek Vasut 	u32 bus_flags;
3459180c30cSJonathan Liu 	u8 pol, sys_ctrl_1, id[4];
34617a9c1aaSMarek Vasut 
34717a9c1aaSMarek Vasut 	chipone_readb(icn, VENDOR_ID, id);
34817a9c1aaSMarek Vasut 	chipone_readb(icn, DEVICE_ID_H, id + 1);
34917a9c1aaSMarek Vasut 	chipone_readb(icn, DEVICE_ID_L, id + 2);
35017a9c1aaSMarek Vasut 	chipone_readb(icn, VERSION_ID, id + 3);
35117a9c1aaSMarek Vasut 
35217a9c1aaSMarek Vasut 	dev_dbg(icn->dev,
35317a9c1aaSMarek Vasut 		"Chip IDs: Vendor=0x%02x Device=0x%02x:0x%02x Version=0x%02x\n",
35417a9c1aaSMarek Vasut 		id[0], id[1], id[2], id[3]);
35517a9c1aaSMarek Vasut 
35617a9c1aaSMarek Vasut 	if (id[0] != 0xc1 || id[1] != 0x62 || id[2] != 0x11) {
35717a9c1aaSMarek Vasut 		dev_dbg(icn->dev, "Invalid Chip IDs, aborting configuration\n");
35817a9c1aaSMarek Vasut 		return;
35917a9c1aaSMarek Vasut 	}
3602dff97f2SMarek Vasut 
3612dff97f2SMarek Vasut 	/* Get the DPI flags from the bridge state. */
3622dff97f2SMarek Vasut 	bridge_state = drm_atomic_get_new_bridge_state(state, bridge);
3632dff97f2SMarek Vasut 	bus_flags = bridge_state->output_bus_cfg.flags;
364ce517f18SJagan Teki 
3658dde6f74SMarek Vasut 	if (icn->interface_i2c)
36633f1036bSMarek Vasut 		chipone_writeb(icn, MIPI_CFG_PW, MIPI_CFG_PW_CONFIG_I2C);
3678dde6f74SMarek Vasut 	else
36833f1036bSMarek Vasut 		chipone_writeb(icn, MIPI_CFG_PW, MIPI_CFG_PW_CONFIG_DSI);
369ce517f18SJagan Teki 
37033f1036bSMarek Vasut 	chipone_writeb(icn, HACTIVE_LI, mode->hdisplay & 0xff);
371ce517f18SJagan Teki 
37233f1036bSMarek Vasut 	chipone_writeb(icn, VACTIVE_LI, mode->vdisplay & 0xff);
373ce517f18SJagan Teki 
3742dcec57bSMarek Vasut 	/*
375ce517f18SJagan Teki 	 * lsb nibble: 2nd nibble of hdisplay
376ce517f18SJagan Teki 	 * msb nibble: 2nd nibble of vdisplay
377ce517f18SJagan Teki 	 */
37833f1036bSMarek Vasut 	chipone_writeb(icn, VACTIVE_HACTIVE_HI,
379ce517f18SJagan Teki 		       ((mode->hdisplay >> 8) & 0xf) |
380ce517f18SJagan Teki 		       (((mode->vdisplay >> 8) & 0xf) << 4));
381ce517f18SJagan Teki 
382c0ff7a64SMarek Vasut 	hfp = mode->hsync_start - mode->hdisplay;
383c0ff7a64SMarek Vasut 	hsync = mode->hsync_end - mode->hsync_start;
384c0ff7a64SMarek Vasut 	hbp = mode->htotal - mode->hsync_end;
385ce517f18SJagan Teki 
38633f1036bSMarek Vasut 	chipone_writeb(icn, HFP_LI, hfp & 0xff);
38733f1036bSMarek Vasut 	chipone_writeb(icn, HSYNC_LI, hsync & 0xff);
38833f1036bSMarek Vasut 	chipone_writeb(icn, HBP_LI, hbp & 0xff);
389c0ff7a64SMarek Vasut 	/* Top two bits of Horizontal Front porch/Sync/Back porch */
39033f1036bSMarek Vasut 	chipone_writeb(icn, HFP_HSW_HBP_HI,
391c0ff7a64SMarek Vasut 		       HFP_HSW_HBP_HI_HFP(hfp) |
392c0ff7a64SMarek Vasut 		       HFP_HSW_HBP_HI_HS(hsync) |
393c0ff7a64SMarek Vasut 		       HFP_HSW_HBP_HI_HBP(hbp));
394ce517f18SJagan Teki 
39533f1036bSMarek Vasut 	chipone_writeb(icn, VFP, mode->vsync_start - mode->vdisplay);
396ce517f18SJagan Teki 
39733f1036bSMarek Vasut 	chipone_writeb(icn, VSYNC, mode->vsync_end - mode->vsync_start);
398ce517f18SJagan Teki 
39933f1036bSMarek Vasut 	chipone_writeb(icn, VBP, mode->vtotal - mode->vsync_end);
400ce517f18SJagan Teki 
401ce517f18SJagan Teki 	/* dsi specific sequence */
40233f1036bSMarek Vasut 	chipone_writeb(icn, SYNC_EVENT_DLY, 0x80);
40333f1036bSMarek Vasut 	chipone_writeb(icn, HFP_MIN, hfp & 0xff);
4044ab85930SMarek Vasut 
4054ab85930SMarek Vasut 	/* DSI data lane count */
4064ab85930SMarek Vasut 	chipone_writeb(icn, DSI_CTRL,
4074ab85930SMarek Vasut 		       DSI_CTRL_UNKNOWN | DSI_CTRL_DSI_LANES(icn->dsi->lanes - 1));
4084ab85930SMarek Vasut 
40933f1036bSMarek Vasut 	chipone_writeb(icn, MIPI_PD_CK_LANE, 0xa0);
41033f1036bSMarek Vasut 	chipone_writeb(icn, PLL_CTRL(12), 0xff);
41133f1036bSMarek Vasut 	chipone_writeb(icn, MIPI_PN_SWAP, 0x00);
4122dff97f2SMarek Vasut 
4132dff97f2SMarek Vasut 	/* DPI HS/VS/DE polarity */
4142dff97f2SMarek Vasut 	pol = ((mode->flags & DRM_MODE_FLAG_PHSYNC) ? BIST_POL_HSYNC_POL : 0) |
4152dff97f2SMarek Vasut 	      ((mode->flags & DRM_MODE_FLAG_PVSYNC) ? BIST_POL_VSYNC_POL : 0) |
4162dff97f2SMarek Vasut 	      ((bus_flags & DRM_BUS_FLAG_DE_HIGH) ? BIST_POL_DE_POL : 0);
41733f1036bSMarek Vasut 	chipone_writeb(icn, BIST_POL, pol);
4182dff97f2SMarek Vasut 
419f30cf0ecSMarek Vasut 	/* Configure PLL settings */
420f30cf0ecSMarek Vasut 	chipone_configure_pll(icn, mode);
421f30cf0ecSMarek Vasut 
42233f1036bSMarek Vasut 	chipone_writeb(icn, SYS_CTRL(0), 0x40);
4239180c30cSJonathan Liu 	sys_ctrl_1 = 0x88;
4249180c30cSJonathan Liu 
4259180c30cSJonathan Liu 	if (bus_flags & DRM_BUS_FLAG_PIXDATA_DRIVE_POSEDGE)
4269180c30cSJonathan Liu 		sys_ctrl_1 |= FIELD_PREP(SYS_CTRL_1_CLK_PHASE_MSK, CLK_PHASE_0);
4279180c30cSJonathan Liu 	else
4289180c30cSJonathan Liu 		sys_ctrl_1 |= FIELD_PREP(SYS_CTRL_1_CLK_PHASE_MSK, CLK_PHASE_1_2);
4299180c30cSJonathan Liu 
4309180c30cSJonathan Liu 	chipone_writeb(icn, SYS_CTRL(1), sys_ctrl_1);
431ce517f18SJagan Teki 
432ce517f18SJagan Teki 	/* icn6211 specific sequence */
43333f1036bSMarek Vasut 	chipone_writeb(icn, MIPI_FORCE_0, 0x20);
43433f1036bSMarek Vasut 	chipone_writeb(icn, PLL_CTRL(1), 0x20);
43533f1036bSMarek Vasut 	chipone_writeb(icn, CONFIG_FINISH, 0x10);
436ce517f18SJagan Teki 
437ce517f18SJagan Teki 	usleep_range(10000, 11000);
438ce517f18SJagan Teki }
439ce517f18SJagan Teki 
4403b26a291SJagan Teki static void chipone_atomic_pre_enable(struct drm_bridge *bridge,
4413b26a291SJagan Teki 				      struct drm_bridge_state *old_bridge_state)
442ce517f18SJagan Teki {
443ce517f18SJagan Teki 	struct chipone *icn = bridge_to_chipone(bridge);
444ce517f18SJagan Teki 	int ret;
445ce517f18SJagan Teki 
446ce517f18SJagan Teki 	if (icn->vdd1) {
447ce517f18SJagan Teki 		ret = regulator_enable(icn->vdd1);
448ce517f18SJagan Teki 		if (ret)
449ce517f18SJagan Teki 			DRM_DEV_ERROR(icn->dev,
450ce517f18SJagan Teki 				      "failed to enable VDD1 regulator: %d\n", ret);
451ce517f18SJagan Teki 	}
452ce517f18SJagan Teki 
453ce517f18SJagan Teki 	if (icn->vdd2) {
454ce517f18SJagan Teki 		ret = regulator_enable(icn->vdd2);
455ce517f18SJagan Teki 		if (ret)
456ce517f18SJagan Teki 			DRM_DEV_ERROR(icn->dev,
457ce517f18SJagan Teki 				      "failed to enable VDD2 regulator: %d\n", ret);
458ce517f18SJagan Teki 	}
459ce517f18SJagan Teki 
460ce517f18SJagan Teki 	if (icn->vdd3) {
461ce517f18SJagan Teki 		ret = regulator_enable(icn->vdd3);
462ce517f18SJagan Teki 		if (ret)
463ce517f18SJagan Teki 			DRM_DEV_ERROR(icn->dev,
464ce517f18SJagan Teki 				      "failed to enable VDD3 regulator: %d\n", ret);
465ce517f18SJagan Teki 	}
466ce517f18SJagan Teki 
467ce517f18SJagan Teki 	gpiod_set_value(icn->enable_gpio, 1);
468ce517f18SJagan Teki 
469ce517f18SJagan Teki 	usleep_range(10000, 11000);
470ce517f18SJagan Teki }
471ce517f18SJagan Teki 
4723b26a291SJagan Teki static void chipone_atomic_post_disable(struct drm_bridge *bridge,
4733b26a291SJagan Teki 					struct drm_bridge_state *old_bridge_state)
474ce517f18SJagan Teki {
475ce517f18SJagan Teki 	struct chipone *icn = bridge_to_chipone(bridge);
476ce517f18SJagan Teki 
477ce517f18SJagan Teki 	if (icn->vdd1)
478ce517f18SJagan Teki 		regulator_disable(icn->vdd1);
479ce517f18SJagan Teki 
480ce517f18SJagan Teki 	if (icn->vdd2)
481ce517f18SJagan Teki 		regulator_disable(icn->vdd2);
482ce517f18SJagan Teki 
483ce517f18SJagan Teki 	if (icn->vdd3)
484ce517f18SJagan Teki 		regulator_disable(icn->vdd3);
485ce517f18SJagan Teki 
486ce517f18SJagan Teki 	gpiod_set_value(icn->enable_gpio, 0);
487ce517f18SJagan Teki }
488ce517f18SJagan Teki 
48950d76e3dSJagan Teki static void chipone_mode_set(struct drm_bridge *bridge,
49050d76e3dSJagan Teki 			     const struct drm_display_mode *mode,
49150d76e3dSJagan Teki 			     const struct drm_display_mode *adjusted_mode)
49250d76e3dSJagan Teki {
49350d76e3dSJagan Teki 	struct chipone *icn = bridge_to_chipone(bridge);
49450d76e3dSJagan Teki 
49550d76e3dSJagan Teki 	drm_mode_copy(&icn->mode, adjusted_mode);
4968dde6f74SMarek Vasut };
4978dde6f74SMarek Vasut 
4988dde6f74SMarek Vasut static int chipone_dsi_attach(struct chipone *icn)
4998dde6f74SMarek Vasut {
5008dde6f74SMarek Vasut 	struct mipi_dsi_device *dsi = icn->dsi;
5014ab85930SMarek Vasut 	struct device *dev = icn->dev;
5024ab85930SMarek Vasut 	int dsi_lanes, ret;
5038dde6f74SMarek Vasut 
5044af48f1dSMarek Vasut 	dsi_lanes = drm_of_get_data_lanes_count_ep(dev->of_node, 0, 0, 1, 4);
5054ab85930SMarek Vasut 
5064ab85930SMarek Vasut 	/*
5074ab85930SMarek Vasut 	 * If the 'data-lanes' property does not exist in DT or is invalid,
5084ab85930SMarek Vasut 	 * default to previously hard-coded behavior, which was 4 data lanes.
5094ab85930SMarek Vasut 	 */
5104af48f1dSMarek Vasut 	if (dsi_lanes < 0)
5114ab85930SMarek Vasut 		icn->dsi->lanes = 4;
5124af48f1dSMarek Vasut 	else
5134af48f1dSMarek Vasut 		icn->dsi->lanes = dsi_lanes;
5144ab85930SMarek Vasut 
5158dde6f74SMarek Vasut 	dsi->format = MIPI_DSI_FMT_RGB888;
5168dde6f74SMarek Vasut 	dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |
5178dde6f74SMarek Vasut 			  MIPI_DSI_MODE_LPM | MIPI_DSI_MODE_NO_EOT_PACKET;
5188dde6f74SMarek Vasut 
5198dde6f74SMarek Vasut 	ret = mipi_dsi_attach(dsi);
5208dde6f74SMarek Vasut 	if (ret < 0)
5218dde6f74SMarek Vasut 		dev_err(icn->dev, "failed to attach dsi\n");
5228dde6f74SMarek Vasut 
5238dde6f74SMarek Vasut 	return ret;
5248dde6f74SMarek Vasut }
5258dde6f74SMarek Vasut 
5268dde6f74SMarek Vasut static int chipone_dsi_host_attach(struct chipone *icn)
5278dde6f74SMarek Vasut {
5288dde6f74SMarek Vasut 	struct device *dev = icn->dev;
5298dde6f74SMarek Vasut 	struct device_node *host_node;
5308dde6f74SMarek Vasut 	struct device_node *endpoint;
5318dde6f74SMarek Vasut 	struct mipi_dsi_device *dsi;
5328dde6f74SMarek Vasut 	struct mipi_dsi_host *host;
5338dde6f74SMarek Vasut 	int ret = 0;
5348dde6f74SMarek Vasut 
5358dde6f74SMarek Vasut 	const struct mipi_dsi_device_info info = {
5368dde6f74SMarek Vasut 		.type = "chipone",
5378dde6f74SMarek Vasut 		.channel = 0,
5388dde6f74SMarek Vasut 		.node = NULL,
5398dde6f74SMarek Vasut 	};
5408dde6f74SMarek Vasut 
5418dde6f74SMarek Vasut 	endpoint = of_graph_get_endpoint_by_regs(dev->of_node, 0, 0);
5428dde6f74SMarek Vasut 	host_node = of_graph_get_remote_port_parent(endpoint);
5438dde6f74SMarek Vasut 	of_node_put(endpoint);
5448dde6f74SMarek Vasut 
5458dde6f74SMarek Vasut 	if (!host_node)
5468dde6f74SMarek Vasut 		return -EINVAL;
5478dde6f74SMarek Vasut 
5488dde6f74SMarek Vasut 	host = of_find_mipi_dsi_host_by_node(host_node);
5498dde6f74SMarek Vasut 	of_node_put(host_node);
5508dde6f74SMarek Vasut 	if (!host) {
5518dde6f74SMarek Vasut 		dev_err(dev, "failed to find dsi host\n");
5528dde6f74SMarek Vasut 		return -EPROBE_DEFER;
5538dde6f74SMarek Vasut 	}
5548dde6f74SMarek Vasut 
5558dde6f74SMarek Vasut 	dsi = mipi_dsi_device_register_full(host, &info);
5568dde6f74SMarek Vasut 	if (IS_ERR(dsi)) {
5578dde6f74SMarek Vasut 		return dev_err_probe(dev, PTR_ERR(dsi),
5588dde6f74SMarek Vasut 				     "failed to create dsi device\n");
5598dde6f74SMarek Vasut 	}
5608dde6f74SMarek Vasut 
5618dde6f74SMarek Vasut 	icn->dsi = dsi;
5628dde6f74SMarek Vasut 
5638dde6f74SMarek Vasut 	ret = chipone_dsi_attach(icn);
5648dde6f74SMarek Vasut 	if (ret < 0)
5658dde6f74SMarek Vasut 		mipi_dsi_device_unregister(dsi);
5668dde6f74SMarek Vasut 
5678dde6f74SMarek Vasut 	return ret;
56850d76e3dSJagan Teki }
56950d76e3dSJagan Teki 
570ce517f18SJagan Teki static int chipone_attach(struct drm_bridge *bridge, enum drm_bridge_attach_flags flags)
571ce517f18SJagan Teki {
572ce517f18SJagan Teki 	struct chipone *icn = bridge_to_chipone(bridge);
573ce517f18SJagan Teki 
574ce517f18SJagan Teki 	return drm_bridge_attach(bridge->encoder, icn->panel_bridge, bridge, flags);
575ce517f18SJagan Teki }
576ce517f18SJagan Teki 
577cda3822aSMarek Vasut #define MAX_INPUT_SEL_FORMATS	1
578cda3822aSMarek Vasut 
579cda3822aSMarek Vasut static u32 *
580cda3822aSMarek Vasut chipone_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
581cda3822aSMarek Vasut 				  struct drm_bridge_state *bridge_state,
582cda3822aSMarek Vasut 				  struct drm_crtc_state *crtc_state,
583cda3822aSMarek Vasut 				  struct drm_connector_state *conn_state,
584cda3822aSMarek Vasut 				  u32 output_fmt,
585cda3822aSMarek Vasut 				  unsigned int *num_input_fmts)
586cda3822aSMarek Vasut {
587cda3822aSMarek Vasut 	u32 *input_fmts;
588cda3822aSMarek Vasut 
589cda3822aSMarek Vasut 	*num_input_fmts = 0;
590cda3822aSMarek Vasut 
591cda3822aSMarek Vasut 	input_fmts = kcalloc(MAX_INPUT_SEL_FORMATS, sizeof(*input_fmts),
592cda3822aSMarek Vasut 			     GFP_KERNEL);
593cda3822aSMarek Vasut 	if (!input_fmts)
594cda3822aSMarek Vasut 		return NULL;
595cda3822aSMarek Vasut 
596cda3822aSMarek Vasut 	/* This is the DSI-end bus format */
597cda3822aSMarek Vasut 	input_fmts[0] = MEDIA_BUS_FMT_RGB888_1X24;
598cda3822aSMarek Vasut 	*num_input_fmts = 1;
599cda3822aSMarek Vasut 
600cda3822aSMarek Vasut 	return input_fmts;
601cda3822aSMarek Vasut }
602cda3822aSMarek Vasut 
603ce517f18SJagan Teki static const struct drm_bridge_funcs chipone_bridge_funcs = {
6043b26a291SJagan Teki 	.atomic_duplicate_state	= drm_atomic_helper_bridge_duplicate_state,
6053b26a291SJagan Teki 	.atomic_destroy_state	= drm_atomic_helper_bridge_destroy_state,
6063b26a291SJagan Teki 	.atomic_reset		= drm_atomic_helper_bridge_reset,
6073b26a291SJagan Teki 	.atomic_pre_enable	= chipone_atomic_pre_enable,
6083b26a291SJagan Teki 	.atomic_enable		= chipone_atomic_enable,
6093b26a291SJagan Teki 	.atomic_post_disable	= chipone_atomic_post_disable,
61050d76e3dSJagan Teki 	.mode_set		= chipone_mode_set,
611ce517f18SJagan Teki 	.attach			= chipone_attach,
612cda3822aSMarek Vasut 	.atomic_get_input_bus_fmts = chipone_atomic_get_input_bus_fmts,
613ce517f18SJagan Teki };
614ce517f18SJagan Teki 
615ce517f18SJagan Teki static int chipone_parse_dt(struct chipone *icn)
616ce517f18SJagan Teki {
617ce517f18SJagan Teki 	struct device *dev = icn->dev;
618ce517f18SJagan Teki 	int ret;
619ce517f18SJagan Teki 
620ce517f18SJagan Teki 	icn->vdd1 = devm_regulator_get_optional(dev, "vdd1");
621ce517f18SJagan Teki 	if (IS_ERR(icn->vdd1)) {
622ce517f18SJagan Teki 		ret = PTR_ERR(icn->vdd1);
623ce517f18SJagan Teki 		if (ret == -EPROBE_DEFER)
624ce517f18SJagan Teki 			return -EPROBE_DEFER;
625ce517f18SJagan Teki 		icn->vdd1 = NULL;
626ce517f18SJagan Teki 		DRM_DEV_DEBUG(dev, "failed to get VDD1 regulator: %d\n", ret);
627ce517f18SJagan Teki 	}
628ce517f18SJagan Teki 
629ce517f18SJagan Teki 	icn->vdd2 = devm_regulator_get_optional(dev, "vdd2");
630ce517f18SJagan Teki 	if (IS_ERR(icn->vdd2)) {
631ce517f18SJagan Teki 		ret = PTR_ERR(icn->vdd2);
632ce517f18SJagan Teki 		if (ret == -EPROBE_DEFER)
633ce517f18SJagan Teki 			return -EPROBE_DEFER;
634ce517f18SJagan Teki 		icn->vdd2 = NULL;
635ce517f18SJagan Teki 		DRM_DEV_DEBUG(dev, "failed to get VDD2 regulator: %d\n", ret);
636ce517f18SJagan Teki 	}
637ce517f18SJagan Teki 
638ce517f18SJagan Teki 	icn->vdd3 = devm_regulator_get_optional(dev, "vdd3");
639ce517f18SJagan Teki 	if (IS_ERR(icn->vdd3)) {
640ce517f18SJagan Teki 		ret = PTR_ERR(icn->vdd3);
641ce517f18SJagan Teki 		if (ret == -EPROBE_DEFER)
642ce517f18SJagan Teki 			return -EPROBE_DEFER;
643ce517f18SJagan Teki 		icn->vdd3 = NULL;
644ce517f18SJagan Teki 		DRM_DEV_DEBUG(dev, "failed to get VDD3 regulator: %d\n", ret);
645ce517f18SJagan Teki 	}
646ce517f18SJagan Teki 
647ce517f18SJagan Teki 	icn->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
648ce517f18SJagan Teki 	if (IS_ERR(icn->enable_gpio)) {
649ce517f18SJagan Teki 		DRM_DEV_ERROR(dev, "failed to get enable GPIO\n");
650ce517f18SJagan Teki 		return PTR_ERR(icn->enable_gpio);
651ce517f18SJagan Teki 	}
652ce517f18SJagan Teki 
653c803ae6dSJosé Expósito 	icn->panel_bridge = devm_drm_of_get_bridge(dev, dev->of_node, 1, 0);
654ce517f18SJagan Teki 	if (IS_ERR(icn->panel_bridge))
655ce517f18SJagan Teki 		return PTR_ERR(icn->panel_bridge);
656ce517f18SJagan Teki 
657ce517f18SJagan Teki 	return 0;
658ce517f18SJagan Teki }
659ce517f18SJagan Teki 
6608dde6f74SMarek Vasut static int chipone_common_probe(struct device *dev, struct chipone **icnr)
661ce517f18SJagan Teki {
662ce517f18SJagan Teki 	struct chipone *icn;
663ce517f18SJagan Teki 	int ret;
664ce517f18SJagan Teki 
665ce517f18SJagan Teki 	icn = devm_kzalloc(dev, sizeof(struct chipone), GFP_KERNEL);
666ce517f18SJagan Teki 	if (!icn)
667ce517f18SJagan Teki 		return -ENOMEM;
668ce517f18SJagan Teki 
669ce517f18SJagan Teki 	icn->dev = dev;
670ce517f18SJagan Teki 
671ce517f18SJagan Teki 	ret = chipone_parse_dt(icn);
672ce517f18SJagan Teki 	if (ret)
673ce517f18SJagan Teki 		return ret;
674ce517f18SJagan Teki 
675ce517f18SJagan Teki 	icn->bridge.funcs = &chipone_bridge_funcs;
676ce517f18SJagan Teki 	icn->bridge.type = DRM_MODE_CONNECTOR_DPI;
677ce517f18SJagan Teki 	icn->bridge.of_node = dev->of_node;
678ce517f18SJagan Teki 
6798dde6f74SMarek Vasut 	*icnr = icn;
680ce517f18SJagan Teki 
681ce517f18SJagan Teki 	return ret;
682ce517f18SJagan Teki }
683ce517f18SJagan Teki 
6848dde6f74SMarek Vasut static int chipone_dsi_probe(struct mipi_dsi_device *dsi)
6858dde6f74SMarek Vasut {
6868dde6f74SMarek Vasut 	struct device *dev = &dsi->dev;
6878dde6f74SMarek Vasut 	struct chipone *icn;
6888dde6f74SMarek Vasut 	int ret;
6898dde6f74SMarek Vasut 
6908dde6f74SMarek Vasut 	ret = chipone_common_probe(dev, &icn);
6918dde6f74SMarek Vasut 	if (ret)
6928dde6f74SMarek Vasut 		return ret;
6938dde6f74SMarek Vasut 
694fb47723aSMarek Vasut 	icn->regmap = devm_regmap_init(dev, &chipone_dsi_regmap_bus,
695fb47723aSMarek Vasut 				       dsi, &chipone_regmap_config);
696fb47723aSMarek Vasut 	if (IS_ERR(icn->regmap))
697fb47723aSMarek Vasut 		return PTR_ERR(icn->regmap);
698fb47723aSMarek Vasut 
6998dde6f74SMarek Vasut 	icn->interface_i2c = false;
7008dde6f74SMarek Vasut 	icn->dsi = dsi;
7018dde6f74SMarek Vasut 
7028dde6f74SMarek Vasut 	mipi_dsi_set_drvdata(dsi, icn);
7038dde6f74SMarek Vasut 
7048dde6f74SMarek Vasut 	drm_bridge_add(&icn->bridge);
7058dde6f74SMarek Vasut 
7068dde6f74SMarek Vasut 	ret = chipone_dsi_attach(icn);
7078dde6f74SMarek Vasut 	if (ret)
7088dde6f74SMarek Vasut 		drm_bridge_remove(&icn->bridge);
7098dde6f74SMarek Vasut 
7108dde6f74SMarek Vasut 	return ret;
7118dde6f74SMarek Vasut }
7128dde6f74SMarek Vasut 
7138dde6f74SMarek Vasut static int chipone_i2c_probe(struct i2c_client *client,
7148dde6f74SMarek Vasut 			     const struct i2c_device_id *id)
7158dde6f74SMarek Vasut {
7168dde6f74SMarek Vasut 	struct device *dev = &client->dev;
7178dde6f74SMarek Vasut 	struct chipone *icn;
7188dde6f74SMarek Vasut 	int ret;
7198dde6f74SMarek Vasut 
7208dde6f74SMarek Vasut 	ret = chipone_common_probe(dev, &icn);
7218dde6f74SMarek Vasut 	if (ret)
7228dde6f74SMarek Vasut 		return ret;
7238dde6f74SMarek Vasut 
724fb47723aSMarek Vasut 	icn->regmap = devm_regmap_init_i2c(client, &chipone_regmap_config);
725fb47723aSMarek Vasut 	if (IS_ERR(icn->regmap))
726fb47723aSMarek Vasut 		return PTR_ERR(icn->regmap);
727fb47723aSMarek Vasut 
7288dde6f74SMarek Vasut 	icn->interface_i2c = true;
7298dde6f74SMarek Vasut 	icn->client = client;
7308dde6f74SMarek Vasut 	dev_set_drvdata(dev, icn);
7318dde6f74SMarek Vasut 	i2c_set_clientdata(client, icn);
7328dde6f74SMarek Vasut 
7338dde6f74SMarek Vasut 	drm_bridge_add(&icn->bridge);
7348dde6f74SMarek Vasut 
7358dde6f74SMarek Vasut 	return chipone_dsi_host_attach(icn);
7368dde6f74SMarek Vasut }
7378dde6f74SMarek Vasut 
7388dde6f74SMarek Vasut static int chipone_dsi_remove(struct mipi_dsi_device *dsi)
739ce517f18SJagan Teki {
740ce517f18SJagan Teki 	struct chipone *icn = mipi_dsi_get_drvdata(dsi);
741ce517f18SJagan Teki 
742ce517f18SJagan Teki 	mipi_dsi_detach(dsi);
743ce517f18SJagan Teki 	drm_bridge_remove(&icn->bridge);
744ce517f18SJagan Teki 
745ce517f18SJagan Teki 	return 0;
746ce517f18SJagan Teki }
747ce517f18SJagan Teki 
748ce517f18SJagan Teki static const struct of_device_id chipone_of_match[] = {
749ce517f18SJagan Teki 	{ .compatible = "chipone,icn6211", },
750ce517f18SJagan Teki 	{ /* sentinel */ }
751ce517f18SJagan Teki };
752ce517f18SJagan Teki MODULE_DEVICE_TABLE(of, chipone_of_match);
753ce517f18SJagan Teki 
7548dde6f74SMarek Vasut static struct mipi_dsi_driver chipone_dsi_driver = {
7558dde6f74SMarek Vasut 	.probe = chipone_dsi_probe,
7568dde6f74SMarek Vasut 	.remove = chipone_dsi_remove,
757ce517f18SJagan Teki 	.driver = {
758ce517f18SJagan Teki 		.name = "chipone-icn6211",
759ce517f18SJagan Teki 		.owner = THIS_MODULE,
760ce517f18SJagan Teki 		.of_match_table = chipone_of_match,
761ce517f18SJagan Teki 	},
762ce517f18SJagan Teki };
7638dde6f74SMarek Vasut 
7648dde6f74SMarek Vasut static struct i2c_device_id chipone_i2c_id[] = {
7658dde6f74SMarek Vasut 	{ "chipone,icn6211" },
7668dde6f74SMarek Vasut 	{},
7678dde6f74SMarek Vasut };
7688dde6f74SMarek Vasut MODULE_DEVICE_TABLE(i2c, chipone_i2c_id);
7698dde6f74SMarek Vasut 
7708dde6f74SMarek Vasut static struct i2c_driver chipone_i2c_driver = {
7718dde6f74SMarek Vasut 	.probe = chipone_i2c_probe,
7728dde6f74SMarek Vasut 	.id_table = chipone_i2c_id,
7738dde6f74SMarek Vasut 	.driver = {
7748dde6f74SMarek Vasut 		.name = "chipone-icn6211-i2c",
7758dde6f74SMarek Vasut 		.of_match_table = chipone_of_match,
7768dde6f74SMarek Vasut 	},
7778dde6f74SMarek Vasut };
7788dde6f74SMarek Vasut 
7798dde6f74SMarek Vasut static int __init chipone_init(void)
7808dde6f74SMarek Vasut {
7818dde6f74SMarek Vasut 	if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
7828dde6f74SMarek Vasut 		mipi_dsi_driver_register(&chipone_dsi_driver);
7838dde6f74SMarek Vasut 
7848dde6f74SMarek Vasut 	return i2c_add_driver(&chipone_i2c_driver);
7858dde6f74SMarek Vasut }
7868dde6f74SMarek Vasut module_init(chipone_init);
7878dde6f74SMarek Vasut 
7887a828f1fSMarek Vasut static void __exit chipone_exit(void)
7898dde6f74SMarek Vasut {
7908dde6f74SMarek Vasut 	i2c_del_driver(&chipone_i2c_driver);
7918dde6f74SMarek Vasut 
7928dde6f74SMarek Vasut 	if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
7938dde6f74SMarek Vasut 		mipi_dsi_driver_unregister(&chipone_dsi_driver);
7948dde6f74SMarek Vasut }
7958dde6f74SMarek Vasut module_exit(chipone_exit);
796ce517f18SJagan Teki 
797ce517f18SJagan Teki MODULE_AUTHOR("Jagan Teki <jagan@amarulasolutions.com>");
798ce517f18SJagan Teki MODULE_DESCRIPTION("Chipone ICN6211 MIPI-DSI to RGB Converter Bridge");
799ce517f18SJagan Teki MODULE_LICENSE("GPL");
800