xref: /linux/drivers/gpu/drm/bridge/chipone-icn6211.c (revision 378e0f9f0b3e03a84db5ed5d3da3850871e9209e)
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>
14*378e0f9fSMarek Vasut #include <linux/clk.h>
15ce517f18SJagan Teki #include <linux/delay.h>
16ce517f18SJagan Teki #include <linux/gpio/consumer.h>
178dde6f74SMarek Vasut #include <linux/i2c.h>
1872bd9ea3SVille Syrjälä #include <linux/media-bus-format.h>
19ce517f18SJagan Teki #include <linux/module.h>
20ce517f18SJagan Teki #include <linux/of_device.h>
21fb47723aSMarek Vasut #include <linux/regmap.h>
22ce517f18SJagan Teki #include <linux/regulator/consumer.h>
23ce517f18SJagan Teki 
242dcec57bSMarek Vasut #define VENDOR_ID		0x00
252dcec57bSMarek Vasut #define DEVICE_ID_H		0x01
262dcec57bSMarek Vasut #define DEVICE_ID_L		0x02
272dcec57bSMarek Vasut #define VERSION_ID		0x03
282dcec57bSMarek Vasut #define FIRMWARE_VERSION	0x08
292dcec57bSMarek Vasut #define CONFIG_FINISH		0x09
302dcec57bSMarek Vasut #define PD_CTRL(n)		(0x0a + ((n) & 0x3)) /* 0..3 */
312dcec57bSMarek Vasut #define RST_CTRL(n)		(0x0e + ((n) & 0x1)) /* 0..1 */
322dcec57bSMarek Vasut #define SYS_CTRL(n)		(0x10 + ((n) & 0x7)) /* 0..4 */
339180c30cSJonathan Liu #define SYS_CTRL_1_CLK_PHASE_MSK	GENMASK(5, 4)
349180c30cSJonathan Liu #define CLK_PHASE_0			0
359180c30cSJonathan Liu #define CLK_PHASE_1_4			1
369180c30cSJonathan Liu #define CLK_PHASE_1_2			2
379180c30cSJonathan Liu #define CLK_PHASE_3_4			3
382dcec57bSMarek Vasut #define RGB_DRV(n)		(0x18 + ((n) & 0x3)) /* 0..3 */
392dcec57bSMarek Vasut #define RGB_DLY(n)		(0x1c + ((n) & 0x1)) /* 0..1 */
402dcec57bSMarek Vasut #define RGB_TEST_CTRL		0x1e
412dcec57bSMarek Vasut #define ATE_PLL_EN		0x1f
42ce517f18SJagan Teki #define HACTIVE_LI		0x20
43ce517f18SJagan Teki #define VACTIVE_LI		0x21
44ce517f18SJagan Teki #define VACTIVE_HACTIVE_HI	0x22
45ce517f18SJagan Teki #define HFP_LI			0x23
46ce517f18SJagan Teki #define HSYNC_LI		0x24
47ce517f18SJagan Teki #define HBP_LI			0x25
48ce517f18SJagan Teki #define HFP_HSW_HBP_HI		0x26
49c0ff7a64SMarek Vasut #define HFP_HSW_HBP_HI_HFP(n)		(((n) & 0x300) >> 4)
50c0ff7a64SMarek Vasut #define HFP_HSW_HBP_HI_HS(n)		(((n) & 0x300) >> 6)
51c0ff7a64SMarek Vasut #define HFP_HSW_HBP_HI_HBP(n)		(((n) & 0x300) >> 8)
52ce517f18SJagan Teki #define VFP			0x27
53ce517f18SJagan Teki #define VSYNC			0x28
54ce517f18SJagan Teki #define VBP			0x29
552dcec57bSMarek Vasut #define BIST_POL		0x2a
562dcec57bSMarek Vasut #define BIST_POL_BIST_MODE(n)		(((n) & 0xf) << 4)
572dcec57bSMarek Vasut #define BIST_POL_BIST_GEN		BIT(3)
582dcec57bSMarek Vasut #define BIST_POL_HSYNC_POL		BIT(2)
592dcec57bSMarek Vasut #define BIST_POL_VSYNC_POL		BIT(1)
602dcec57bSMarek Vasut #define BIST_POL_DE_POL			BIT(0)
612dcec57bSMarek Vasut #define BIST_RED		0x2b
622dcec57bSMarek Vasut #define BIST_GREEN		0x2c
632dcec57bSMarek Vasut #define BIST_BLUE		0x2d
642dcec57bSMarek Vasut #define BIST_CHESS_X		0x2e
652dcec57bSMarek Vasut #define BIST_CHESS_Y		0x2f
662dcec57bSMarek Vasut #define BIST_CHESS_XY_H		0x30
672dcec57bSMarek Vasut #define BIST_FRAME_TIME_L	0x31
682dcec57bSMarek Vasut #define BIST_FRAME_TIME_H	0x32
692dcec57bSMarek Vasut #define FIFO_MAX_ADDR_LOW	0x33
702dcec57bSMarek Vasut #define SYNC_EVENT_DLY		0x34
712dcec57bSMarek Vasut #define HSW_MIN			0x35
722dcec57bSMarek Vasut #define HFP_MIN			0x36
732dcec57bSMarek Vasut #define LOGIC_RST_NUM		0x37
742dcec57bSMarek Vasut #define OSC_CTRL(n)		(0x48 + ((n) & 0x7)) /* 0..5 */
752dcec57bSMarek Vasut #define BG_CTRL			0x4e
762dcec57bSMarek Vasut #define LDO_PLL			0x4f
772dcec57bSMarek Vasut #define PLL_CTRL(n)		(0x50 + ((n) & 0xf)) /* 0..15 */
782dcec57bSMarek Vasut #define PLL_CTRL_6_EXTERNAL		0x90
792dcec57bSMarek Vasut #define PLL_CTRL_6_MIPI_CLK		0x92
802dcec57bSMarek Vasut #define PLL_CTRL_6_INTERNAL		0x93
812dcec57bSMarek Vasut #define PLL_REM(n)		(0x60 + ((n) & 0x3)) /* 0..2 */
822dcec57bSMarek Vasut #define PLL_DIV(n)		(0x63 + ((n) & 0x3)) /* 0..2 */
832dcec57bSMarek Vasut #define PLL_FRAC(n)		(0x66 + ((n) & 0x3)) /* 0..2 */
842dcec57bSMarek Vasut #define PLL_INT(n)		(0x69 + ((n) & 0x1)) /* 0..1 */
852dcec57bSMarek Vasut #define PLL_REF_DIV		0x6b
862dcec57bSMarek Vasut #define PLL_REF_DIV_P(n)		((n) & 0xf)
872dcec57bSMarek Vasut #define PLL_REF_DIV_Pe			BIT(4)
882dcec57bSMarek Vasut #define PLL_REF_DIV_S(n)		(((n) & 0x7) << 5)
892dcec57bSMarek Vasut #define PLL_SSC_P(n)		(0x6c + ((n) & 0x3)) /* 0..2 */
902dcec57bSMarek Vasut #define PLL_SSC_STEP(n)		(0x6f + ((n) & 0x3)) /* 0..2 */
912dcec57bSMarek Vasut #define PLL_SSC_OFFSET(n)	(0x72 + ((n) & 0x3)) /* 0..3 */
922dcec57bSMarek Vasut #define GPIO_OEN		0x79
932dcec57bSMarek Vasut #define MIPI_CFG_PW		0x7a
942dcec57bSMarek Vasut #define MIPI_CFG_PW_CONFIG_DSI		0xc1
952dcec57bSMarek Vasut #define MIPI_CFG_PW_CONFIG_I2C		0x3e
962dcec57bSMarek Vasut #define GPIO_SEL(n)		(0x7b + ((n) & 0x1)) /* 0..1 */
972dcec57bSMarek Vasut #define IRQ_SEL			0x7d
982dcec57bSMarek Vasut #define DBG_SEL			0x7e
992dcec57bSMarek Vasut #define DBG_SIGNAL		0x7f
1002dcec57bSMarek Vasut #define MIPI_ERR_VECTOR_L	0x80
1012dcec57bSMarek Vasut #define MIPI_ERR_VECTOR_H	0x81
1022dcec57bSMarek Vasut #define MIPI_ERR_VECTOR_EN_L	0x82
1032dcec57bSMarek Vasut #define MIPI_ERR_VECTOR_EN_H	0x83
1042dcec57bSMarek Vasut #define MIPI_MAX_SIZE_L		0x84
1052dcec57bSMarek Vasut #define MIPI_MAX_SIZE_H		0x85
1062dcec57bSMarek Vasut #define DSI_CTRL		0x86
1072dcec57bSMarek Vasut #define DSI_CTRL_UNKNOWN		0x28
1082dcec57bSMarek Vasut #define DSI_CTRL_DSI_LANES(n)		((n) & 0x3)
1092dcec57bSMarek Vasut #define MIPI_PN_SWAP		0x87
1102dcec57bSMarek Vasut #define MIPI_PN_SWAP_CLK		BIT(4)
1112dcec57bSMarek Vasut #define MIPI_PN_SWAP_D(n)		BIT((n) & 0x3)
112a24191b1SMarek Vasut #define MIPI_SOT_SYNC_BIT(n)	(0x88 + ((n) & 0x1)) /* 0..1 */
1132dcec57bSMarek Vasut #define MIPI_ULPS_CTRL		0x8a
1142dcec57bSMarek Vasut #define MIPI_CLK_CHK_VAR	0x8e
1152dcec57bSMarek Vasut #define MIPI_CLK_CHK_INI	0x8f
1162dcec57bSMarek Vasut #define MIPI_T_TERM_EN		0x90
1172dcec57bSMarek Vasut #define MIPI_T_HS_SETTLE	0x91
1182dcec57bSMarek Vasut #define MIPI_T_TA_SURE_PRE	0x92
1192dcec57bSMarek Vasut #define MIPI_T_LPX_SET		0x94
1202dcec57bSMarek Vasut #define MIPI_T_CLK_MISS		0x95
1212dcec57bSMarek Vasut #define MIPI_INIT_TIME_L	0x96
1222dcec57bSMarek Vasut #define MIPI_INIT_TIME_H	0x97
1232dcec57bSMarek Vasut #define MIPI_T_CLK_TERM_EN	0x99
1242dcec57bSMarek Vasut #define MIPI_T_CLK_SETTLE	0x9a
1252dcec57bSMarek Vasut #define MIPI_TO_HS_RX_L		0x9e
1262dcec57bSMarek Vasut #define MIPI_TO_HS_RX_H		0x9f
127a24191b1SMarek Vasut #define MIPI_PHY(n)		(0xa0 + ((n) & 0x7)) /* 0..5 */
1282dcec57bSMarek Vasut #define MIPI_PD_RX		0xb0
1292dcec57bSMarek Vasut #define MIPI_PD_TERM		0xb1
1302dcec57bSMarek Vasut #define MIPI_PD_HSRX		0xb2
1312dcec57bSMarek Vasut #define MIPI_PD_LPTX		0xb3
1322dcec57bSMarek Vasut #define MIPI_PD_LPRX		0xb4
1332dcec57bSMarek Vasut #define MIPI_PD_CK_LANE		0xb5
1342dcec57bSMarek Vasut #define MIPI_FORCE_0		0xb6
1352dcec57bSMarek Vasut #define MIPI_RST_CTRL		0xb7
1362dcec57bSMarek Vasut #define MIPI_RST_NUM		0xb8
137a24191b1SMarek Vasut #define MIPI_DBG_SET(n)		(0xc0 + ((n) & 0xf)) /* 0..9 */
1382dcec57bSMarek Vasut #define MIPI_DBG_SEL		0xe0
1392dcec57bSMarek Vasut #define MIPI_DBG_DATA		0xe1
1402dcec57bSMarek Vasut #define MIPI_ATE_TEST_SEL	0xe2
141a24191b1SMarek Vasut #define MIPI_ATE_STATUS(n)	(0xe3 + ((n) & 0x1)) /* 0..1 */
142ce517f18SJagan Teki 
143ce517f18SJagan Teki struct chipone {
144ce517f18SJagan Teki 	struct device *dev;
145fb47723aSMarek Vasut 	struct regmap *regmap;
1468dde6f74SMarek Vasut 	struct i2c_client *client;
147ce517f18SJagan Teki 	struct drm_bridge bridge;
14850d76e3dSJagan Teki 	struct drm_display_mode mode;
149ce517f18SJagan Teki 	struct drm_bridge *panel_bridge;
150f30cf0ecSMarek Vasut 	struct mipi_dsi_device *dsi;
151ce517f18SJagan Teki 	struct gpio_desc *enable_gpio;
152ce517f18SJagan Teki 	struct regulator *vdd1;
153ce517f18SJagan Teki 	struct regulator *vdd2;
154ce517f18SJagan Teki 	struct regulator *vdd3;
155*378e0f9fSMarek Vasut 	struct clk *refclk;
156*378e0f9fSMarek Vasut 	unsigned long refclk_rate;
1578dde6f74SMarek Vasut 	bool interface_i2c;
158ce517f18SJagan Teki };
159ce517f18SJagan Teki 
160fb47723aSMarek Vasut static const struct regmap_range chipone_dsi_readable_ranges[] = {
161fb47723aSMarek Vasut 	regmap_reg_range(VENDOR_ID, VERSION_ID),
162fb47723aSMarek Vasut 	regmap_reg_range(FIRMWARE_VERSION, PLL_SSC_OFFSET(3)),
163fb47723aSMarek Vasut 	regmap_reg_range(GPIO_OEN, MIPI_ULPS_CTRL),
164fb47723aSMarek Vasut 	regmap_reg_range(MIPI_CLK_CHK_VAR, MIPI_T_TA_SURE_PRE),
165fb47723aSMarek Vasut 	regmap_reg_range(MIPI_T_LPX_SET, MIPI_INIT_TIME_H),
166fb47723aSMarek Vasut 	regmap_reg_range(MIPI_T_CLK_TERM_EN, MIPI_T_CLK_SETTLE),
167a24191b1SMarek Vasut 	regmap_reg_range(MIPI_TO_HS_RX_L, MIPI_PHY(5)),
168fb47723aSMarek Vasut 	regmap_reg_range(MIPI_PD_RX, MIPI_RST_NUM),
169a24191b1SMarek Vasut 	regmap_reg_range(MIPI_DBG_SET(0), MIPI_DBG_SET(9)),
170a24191b1SMarek Vasut 	regmap_reg_range(MIPI_DBG_SEL, MIPI_ATE_STATUS(1)),
171fb47723aSMarek Vasut };
172fb47723aSMarek Vasut 
173fb47723aSMarek Vasut static const struct regmap_access_table chipone_dsi_readable_table = {
174fb47723aSMarek Vasut 	.yes_ranges = chipone_dsi_readable_ranges,
175fb47723aSMarek Vasut 	.n_yes_ranges = ARRAY_SIZE(chipone_dsi_readable_ranges),
176fb47723aSMarek Vasut };
177fb47723aSMarek Vasut 
178fb47723aSMarek Vasut static const struct regmap_range chipone_dsi_writeable_ranges[] = {
179fb47723aSMarek Vasut 	regmap_reg_range(CONFIG_FINISH, PLL_SSC_OFFSET(3)),
180fb47723aSMarek Vasut 	regmap_reg_range(GPIO_OEN, MIPI_ULPS_CTRL),
181fb47723aSMarek Vasut 	regmap_reg_range(MIPI_CLK_CHK_VAR, MIPI_T_TA_SURE_PRE),
182fb47723aSMarek Vasut 	regmap_reg_range(MIPI_T_LPX_SET, MIPI_INIT_TIME_H),
183fb47723aSMarek Vasut 	regmap_reg_range(MIPI_T_CLK_TERM_EN, MIPI_T_CLK_SETTLE),
184a24191b1SMarek Vasut 	regmap_reg_range(MIPI_TO_HS_RX_L, MIPI_PHY(5)),
185fb47723aSMarek Vasut 	regmap_reg_range(MIPI_PD_RX, MIPI_RST_NUM),
186a24191b1SMarek Vasut 	regmap_reg_range(MIPI_DBG_SET(0), MIPI_DBG_SET(9)),
187a24191b1SMarek Vasut 	regmap_reg_range(MIPI_DBG_SEL, MIPI_ATE_STATUS(1)),
188fb47723aSMarek Vasut };
189fb47723aSMarek Vasut 
190fb47723aSMarek Vasut static const struct regmap_access_table chipone_dsi_writeable_table = {
191fb47723aSMarek Vasut 	.yes_ranges = chipone_dsi_writeable_ranges,
192fb47723aSMarek Vasut 	.n_yes_ranges = ARRAY_SIZE(chipone_dsi_writeable_ranges),
193fb47723aSMarek Vasut };
194fb47723aSMarek Vasut 
195fb47723aSMarek Vasut static const struct regmap_config chipone_regmap_config = {
196fb47723aSMarek Vasut 	.reg_bits = 8,
197fb47723aSMarek Vasut 	.val_bits = 8,
198fb47723aSMarek Vasut 	.rd_table = &chipone_dsi_readable_table,
199fb47723aSMarek Vasut 	.wr_table = &chipone_dsi_writeable_table,
200fb47723aSMarek Vasut 	.cache_type = REGCACHE_RBTREE,
201a24191b1SMarek Vasut 	.max_register = MIPI_ATE_STATUS(1),
202fb47723aSMarek Vasut };
203fb47723aSMarek Vasut 
204fb47723aSMarek Vasut static int chipone_dsi_read(void *context,
205fb47723aSMarek Vasut 			    const void *reg, size_t reg_size,
206fb47723aSMarek Vasut 			    void *val, size_t val_size)
207fb47723aSMarek Vasut {
208fb47723aSMarek Vasut 	struct mipi_dsi_device *dsi = context;
209fb47723aSMarek Vasut 	const u16 reg16 = (val_size << 8) | *(u8 *)reg;
210fb47723aSMarek Vasut 	int ret;
211fb47723aSMarek Vasut 
212fb47723aSMarek Vasut 	ret = mipi_dsi_generic_read(dsi, &reg16, 2, val, val_size);
213fb47723aSMarek Vasut 
214fb47723aSMarek Vasut 	return ret == val_size ? 0 : -EINVAL;
215fb47723aSMarek Vasut }
216fb47723aSMarek Vasut 
217fb47723aSMarek Vasut static int chipone_dsi_write(void *context, const void *data, size_t count)
218fb47723aSMarek Vasut {
219fb47723aSMarek Vasut 	struct mipi_dsi_device *dsi = context;
220fb47723aSMarek Vasut 
221fb47723aSMarek Vasut 	return mipi_dsi_generic_write(dsi, data, 2);
222fb47723aSMarek Vasut }
223fb47723aSMarek Vasut 
224fb47723aSMarek Vasut static const struct regmap_bus chipone_dsi_regmap_bus = {
225fb47723aSMarek Vasut 	.read				= chipone_dsi_read,
226fb47723aSMarek Vasut 	.write				= chipone_dsi_write,
227fb47723aSMarek Vasut 	.reg_format_endian_default	= REGMAP_ENDIAN_NATIVE,
228fb47723aSMarek Vasut 	.val_format_endian_default	= REGMAP_ENDIAN_NATIVE,
229fb47723aSMarek Vasut };
230fb47723aSMarek Vasut 
231ce517f18SJagan Teki static inline struct chipone *bridge_to_chipone(struct drm_bridge *bridge)
232ce517f18SJagan Teki {
233ce517f18SJagan Teki 	return container_of(bridge, struct chipone, bridge);
234ce517f18SJagan Teki }
235ce517f18SJagan Teki 
23617a9c1aaSMarek Vasut static void chipone_readb(struct chipone *icn, u8 reg, u8 *val)
23717a9c1aaSMarek Vasut {
238fb47723aSMarek Vasut 	int ret, pval;
239fb47723aSMarek Vasut 
240fb47723aSMarek Vasut 	ret = regmap_read(icn->regmap, reg, &pval);
241fb47723aSMarek Vasut 
242fb47723aSMarek Vasut 	*val = ret ? 0 : pval & 0xff;
24317a9c1aaSMarek Vasut }
24417a9c1aaSMarek Vasut 
24533f1036bSMarek Vasut static int chipone_writeb(struct chipone *icn, u8 reg, u8 val)
246ce517f18SJagan Teki {
247fb47723aSMarek Vasut 	return regmap_write(icn->regmap, reg, val);
248ce517f18SJagan Teki }
249ce517f18SJagan Teki 
250f30cf0ecSMarek Vasut static void chipone_configure_pll(struct chipone *icn,
251f30cf0ecSMarek Vasut 				  const struct drm_display_mode *mode)
252f30cf0ecSMarek Vasut {
253f30cf0ecSMarek Vasut 	unsigned int best_p = 0, best_m = 0, best_s = 0;
25421d139a9SMarek Vasut 	unsigned int mode_clock = mode->clock * 1000;
255f30cf0ecSMarek Vasut 	unsigned int delta, min_delta = 0xffffffff;
256f30cf0ecSMarek Vasut 	unsigned int freq_p, freq_s, freq_out;
257f30cf0ecSMarek Vasut 	unsigned int p_min, p_max;
258f30cf0ecSMarek Vasut 	unsigned int p, m, s;
259f30cf0ecSMarek Vasut 	unsigned int fin;
26021d139a9SMarek Vasut 	bool best_p_pot;
26121d139a9SMarek Vasut 	u8 ref_div;
262f30cf0ecSMarek Vasut 
263f30cf0ecSMarek Vasut 	/*
26421d139a9SMarek Vasut 	 * DSI byte clock frequency (input into PLL) is calculated as:
2654d054ca9SMarek Vasut 	 *  DSI_CLK = HS clock / 4
266f30cf0ecSMarek Vasut 	 *
267f30cf0ecSMarek Vasut 	 * DPI pixel clock frequency (output from PLL) is mode clock.
268f30cf0ecSMarek Vasut 	 *
269f30cf0ecSMarek Vasut 	 * The chip contains fractional PLL which works as follows:
270f30cf0ecSMarek Vasut 	 *  DPI_CLK = ((DSI_CLK / P) * M) / S
27121d139a9SMarek Vasut 	 * P is pre-divider, register PLL_REF_DIV[3:0] is 1:n divider
272f30cf0ecSMarek Vasut 	 *                   register PLL_REF_DIV[4] is extra 1:2 divider
273f30cf0ecSMarek Vasut 	 * M is integer multiplier, register PLL_INT(0) is multiplier
274f30cf0ecSMarek Vasut 	 * S is post-divider, register PLL_REF_DIV[7:5] is 2^(n+1) divider
275f30cf0ecSMarek Vasut 	 *
276f30cf0ecSMarek Vasut 	 * It seems the PLL input clock after applying P pre-divider have
277f30cf0ecSMarek Vasut 	 * to be lower than 20 MHz.
278f30cf0ecSMarek Vasut 	 */
279*378e0f9fSMarek Vasut 	if (icn->refclk)
280*378e0f9fSMarek Vasut 		fin = icn->refclk_rate;
281*378e0f9fSMarek Vasut 	else
2824d054ca9SMarek Vasut 		fin = icn->dsi->hs_rate / 4; /* in Hz */
283f30cf0ecSMarek Vasut 
284f30cf0ecSMarek Vasut 	/* Minimum value of P predivider for PLL input in 5..20 MHz */
28521d139a9SMarek Vasut 	p_min = clamp(DIV_ROUND_UP(fin, 20000000), 1U, 31U);
28621d139a9SMarek Vasut 	p_max = clamp(fin / 5000000, 1U, 31U);
287f30cf0ecSMarek Vasut 
288f30cf0ecSMarek Vasut 	for (p = p_min; p < p_max; p++) {	/* PLL_REF_DIV[4,3:0] */
28921d139a9SMarek Vasut 		if (p > 16 && p & 1)		/* P > 16 uses extra /2 */
29021d139a9SMarek Vasut 			continue;
29121d139a9SMarek Vasut 		freq_p = fin / p;
292f30cf0ecSMarek Vasut 		if (freq_p == 0)		/* Divider too high */
293f30cf0ecSMarek Vasut 			break;
294f30cf0ecSMarek Vasut 
295f30cf0ecSMarek Vasut 		for (s = 0; s < 0x7; s++) {	/* PLL_REF_DIV[7:5] */
296f30cf0ecSMarek Vasut 			freq_s = freq_p / BIT(s + 1);
297f30cf0ecSMarek Vasut 			if (freq_s == 0)	/* Divider too high */
298f30cf0ecSMarek Vasut 				break;
299f30cf0ecSMarek Vasut 
30021d139a9SMarek Vasut 			m = mode_clock / freq_s;
301f30cf0ecSMarek Vasut 
302f30cf0ecSMarek Vasut 			/* Multiplier is 8 bit */
303f30cf0ecSMarek Vasut 			if (m > 0xff)
304f30cf0ecSMarek Vasut 				continue;
305f30cf0ecSMarek Vasut 
306f30cf0ecSMarek Vasut 			/* Limit PLL VCO frequency to 1 GHz */
30721d139a9SMarek Vasut 			freq_out = (fin * m) / p;
30821d139a9SMarek Vasut 			if (freq_out > 1000000000)
309f30cf0ecSMarek Vasut 				continue;
310f30cf0ecSMarek Vasut 
311f30cf0ecSMarek Vasut 			/* Apply post-divider */
312f30cf0ecSMarek Vasut 			freq_out /= BIT(s + 1);
313f30cf0ecSMarek Vasut 
31421d139a9SMarek Vasut 			delta = abs(mode_clock - freq_out);
315f30cf0ecSMarek Vasut 			if (delta < min_delta) {
316f30cf0ecSMarek Vasut 				best_p = p;
317f30cf0ecSMarek Vasut 				best_m = m;
318f30cf0ecSMarek Vasut 				best_s = s;
319f30cf0ecSMarek Vasut 				min_delta = delta;
320f30cf0ecSMarek Vasut 			}
321f30cf0ecSMarek Vasut 		}
322f30cf0ecSMarek Vasut 	}
323f30cf0ecSMarek Vasut 
32421d139a9SMarek Vasut 	best_p_pot = !(best_p & 1);
32521d139a9SMarek Vasut 
326f30cf0ecSMarek Vasut 	dev_dbg(icn->dev,
327*378e0f9fSMarek Vasut 		"PLL: P[3:0]=%d P[4]=2*%d M=%d S[7:5]=2^%d delta=%d => DSI f_in(%s)=%d Hz ; DPI f_out=%d Hz\n",
32821d139a9SMarek Vasut 		best_p >> best_p_pot, best_p_pot, best_m, best_s + 1,
329*378e0f9fSMarek Vasut 		min_delta, icn->refclk ? "EXT" : "DSI", fin,
330*378e0f9fSMarek Vasut 		(fin * best_m) / (best_p << (best_s + 1)));
33121d139a9SMarek Vasut 
33221d139a9SMarek Vasut 	ref_div = PLL_REF_DIV_P(best_p >> best_p_pot) | PLL_REF_DIV_S(best_s);
33321d139a9SMarek Vasut 	if (best_p_pot)	/* Prefer /2 pre-divider */
33421d139a9SMarek Vasut 		ref_div |= PLL_REF_DIV_Pe;
335f30cf0ecSMarek Vasut 
336*378e0f9fSMarek Vasut 	/* Clock source selection either external clock or MIPI DSI clock lane */
337*378e0f9fSMarek Vasut 	chipone_writeb(icn, PLL_CTRL(6),
338*378e0f9fSMarek Vasut 		       icn->refclk ? PLL_CTRL_6_EXTERNAL : PLL_CTRL_6_MIPI_CLK);
33921d139a9SMarek Vasut 	chipone_writeb(icn, PLL_REF_DIV, ref_div);
34033f1036bSMarek Vasut 	chipone_writeb(icn, PLL_INT(0), best_m);
341f30cf0ecSMarek Vasut }
342f30cf0ecSMarek Vasut 
3433b26a291SJagan Teki static void chipone_atomic_enable(struct drm_bridge *bridge,
3443b26a291SJagan Teki 				  struct drm_bridge_state *old_bridge_state)
345ce517f18SJagan Teki {
346ce517f18SJagan Teki 	struct chipone *icn = bridge_to_chipone(bridge);
3472dff97f2SMarek Vasut 	struct drm_atomic_state *state = old_bridge_state->base.state;
34850d76e3dSJagan Teki 	struct drm_display_mode *mode = &icn->mode;
3492dff97f2SMarek Vasut 	const struct drm_bridge_state *bridge_state;
350c0ff7a64SMarek Vasut 	u16 hfp, hbp, hsync;
3512dff97f2SMarek Vasut 	u32 bus_flags;
3529180c30cSJonathan Liu 	u8 pol, sys_ctrl_1, id[4];
35317a9c1aaSMarek Vasut 
35417a9c1aaSMarek Vasut 	chipone_readb(icn, VENDOR_ID, id);
35517a9c1aaSMarek Vasut 	chipone_readb(icn, DEVICE_ID_H, id + 1);
35617a9c1aaSMarek Vasut 	chipone_readb(icn, DEVICE_ID_L, id + 2);
35717a9c1aaSMarek Vasut 	chipone_readb(icn, VERSION_ID, id + 3);
35817a9c1aaSMarek Vasut 
35917a9c1aaSMarek Vasut 	dev_dbg(icn->dev,
36017a9c1aaSMarek Vasut 		"Chip IDs: Vendor=0x%02x Device=0x%02x:0x%02x Version=0x%02x\n",
36117a9c1aaSMarek Vasut 		id[0], id[1], id[2], id[3]);
36217a9c1aaSMarek Vasut 
36317a9c1aaSMarek Vasut 	if (id[0] != 0xc1 || id[1] != 0x62 || id[2] != 0x11) {
36417a9c1aaSMarek Vasut 		dev_dbg(icn->dev, "Invalid Chip IDs, aborting configuration\n");
36517a9c1aaSMarek Vasut 		return;
36617a9c1aaSMarek Vasut 	}
3672dff97f2SMarek Vasut 
3682dff97f2SMarek Vasut 	/* Get the DPI flags from the bridge state. */
3692dff97f2SMarek Vasut 	bridge_state = drm_atomic_get_new_bridge_state(state, bridge);
3702dff97f2SMarek Vasut 	bus_flags = bridge_state->output_bus_cfg.flags;
371ce517f18SJagan Teki 
3728dde6f74SMarek Vasut 	if (icn->interface_i2c)
37333f1036bSMarek Vasut 		chipone_writeb(icn, MIPI_CFG_PW, MIPI_CFG_PW_CONFIG_I2C);
3748dde6f74SMarek Vasut 	else
37533f1036bSMarek Vasut 		chipone_writeb(icn, MIPI_CFG_PW, MIPI_CFG_PW_CONFIG_DSI);
376ce517f18SJagan Teki 
37733f1036bSMarek Vasut 	chipone_writeb(icn, HACTIVE_LI, mode->hdisplay & 0xff);
378ce517f18SJagan Teki 
37933f1036bSMarek Vasut 	chipone_writeb(icn, VACTIVE_LI, mode->vdisplay & 0xff);
380ce517f18SJagan Teki 
3812dcec57bSMarek Vasut 	/*
382ce517f18SJagan Teki 	 * lsb nibble: 2nd nibble of hdisplay
383ce517f18SJagan Teki 	 * msb nibble: 2nd nibble of vdisplay
384ce517f18SJagan Teki 	 */
38533f1036bSMarek Vasut 	chipone_writeb(icn, VACTIVE_HACTIVE_HI,
386ce517f18SJagan Teki 		       ((mode->hdisplay >> 8) & 0xf) |
387ce517f18SJagan Teki 		       (((mode->vdisplay >> 8) & 0xf) << 4));
388ce517f18SJagan Teki 
389c0ff7a64SMarek Vasut 	hfp = mode->hsync_start - mode->hdisplay;
390c0ff7a64SMarek Vasut 	hsync = mode->hsync_end - mode->hsync_start;
391c0ff7a64SMarek Vasut 	hbp = mode->htotal - mode->hsync_end;
392ce517f18SJagan Teki 
39333f1036bSMarek Vasut 	chipone_writeb(icn, HFP_LI, hfp & 0xff);
39433f1036bSMarek Vasut 	chipone_writeb(icn, HSYNC_LI, hsync & 0xff);
39533f1036bSMarek Vasut 	chipone_writeb(icn, HBP_LI, hbp & 0xff);
396c0ff7a64SMarek Vasut 	/* Top two bits of Horizontal Front porch/Sync/Back porch */
39733f1036bSMarek Vasut 	chipone_writeb(icn, HFP_HSW_HBP_HI,
398c0ff7a64SMarek Vasut 		       HFP_HSW_HBP_HI_HFP(hfp) |
399c0ff7a64SMarek Vasut 		       HFP_HSW_HBP_HI_HS(hsync) |
400c0ff7a64SMarek Vasut 		       HFP_HSW_HBP_HI_HBP(hbp));
401ce517f18SJagan Teki 
40233f1036bSMarek Vasut 	chipone_writeb(icn, VFP, mode->vsync_start - mode->vdisplay);
403ce517f18SJagan Teki 
40433f1036bSMarek Vasut 	chipone_writeb(icn, VSYNC, mode->vsync_end - mode->vsync_start);
405ce517f18SJagan Teki 
40633f1036bSMarek Vasut 	chipone_writeb(icn, VBP, mode->vtotal - mode->vsync_end);
407ce517f18SJagan Teki 
408ce517f18SJagan Teki 	/* dsi specific sequence */
40933f1036bSMarek Vasut 	chipone_writeb(icn, SYNC_EVENT_DLY, 0x80);
41033f1036bSMarek Vasut 	chipone_writeb(icn, HFP_MIN, hfp & 0xff);
4114ab85930SMarek Vasut 
4124ab85930SMarek Vasut 	/* DSI data lane count */
4134ab85930SMarek Vasut 	chipone_writeb(icn, DSI_CTRL,
4144ab85930SMarek Vasut 		       DSI_CTRL_UNKNOWN | DSI_CTRL_DSI_LANES(icn->dsi->lanes - 1));
4154ab85930SMarek Vasut 
41633f1036bSMarek Vasut 	chipone_writeb(icn, MIPI_PD_CK_LANE, 0xa0);
41733f1036bSMarek Vasut 	chipone_writeb(icn, PLL_CTRL(12), 0xff);
41833f1036bSMarek Vasut 	chipone_writeb(icn, MIPI_PN_SWAP, 0x00);
4192dff97f2SMarek Vasut 
4202dff97f2SMarek Vasut 	/* DPI HS/VS/DE polarity */
4212dff97f2SMarek Vasut 	pol = ((mode->flags & DRM_MODE_FLAG_PHSYNC) ? BIST_POL_HSYNC_POL : 0) |
4222dff97f2SMarek Vasut 	      ((mode->flags & DRM_MODE_FLAG_PVSYNC) ? BIST_POL_VSYNC_POL : 0) |
4232dff97f2SMarek Vasut 	      ((bus_flags & DRM_BUS_FLAG_DE_HIGH) ? BIST_POL_DE_POL : 0);
42433f1036bSMarek Vasut 	chipone_writeb(icn, BIST_POL, pol);
4252dff97f2SMarek Vasut 
426f30cf0ecSMarek Vasut 	/* Configure PLL settings */
427f30cf0ecSMarek Vasut 	chipone_configure_pll(icn, mode);
428f30cf0ecSMarek Vasut 
42933f1036bSMarek Vasut 	chipone_writeb(icn, SYS_CTRL(0), 0x40);
4309180c30cSJonathan Liu 	sys_ctrl_1 = 0x88;
4319180c30cSJonathan Liu 
4329180c30cSJonathan Liu 	if (bus_flags & DRM_BUS_FLAG_PIXDATA_DRIVE_POSEDGE)
4339180c30cSJonathan Liu 		sys_ctrl_1 |= FIELD_PREP(SYS_CTRL_1_CLK_PHASE_MSK, CLK_PHASE_0);
4349180c30cSJonathan Liu 	else
4359180c30cSJonathan Liu 		sys_ctrl_1 |= FIELD_PREP(SYS_CTRL_1_CLK_PHASE_MSK, CLK_PHASE_1_2);
4369180c30cSJonathan Liu 
4379180c30cSJonathan Liu 	chipone_writeb(icn, SYS_CTRL(1), sys_ctrl_1);
438ce517f18SJagan Teki 
439ce517f18SJagan Teki 	/* icn6211 specific sequence */
44033f1036bSMarek Vasut 	chipone_writeb(icn, MIPI_FORCE_0, 0x20);
44133f1036bSMarek Vasut 	chipone_writeb(icn, PLL_CTRL(1), 0x20);
44233f1036bSMarek Vasut 	chipone_writeb(icn, CONFIG_FINISH, 0x10);
443ce517f18SJagan Teki 
444ce517f18SJagan Teki 	usleep_range(10000, 11000);
445ce517f18SJagan Teki }
446ce517f18SJagan Teki 
4473b26a291SJagan Teki static void chipone_atomic_pre_enable(struct drm_bridge *bridge,
4483b26a291SJagan Teki 				      struct drm_bridge_state *old_bridge_state)
449ce517f18SJagan Teki {
450ce517f18SJagan Teki 	struct chipone *icn = bridge_to_chipone(bridge);
451ce517f18SJagan Teki 	int ret;
452ce517f18SJagan Teki 
453ce517f18SJagan Teki 	if (icn->vdd1) {
454ce517f18SJagan Teki 		ret = regulator_enable(icn->vdd1);
455ce517f18SJagan Teki 		if (ret)
456ce517f18SJagan Teki 			DRM_DEV_ERROR(icn->dev,
457ce517f18SJagan Teki 				      "failed to enable VDD1 regulator: %d\n", ret);
458ce517f18SJagan Teki 	}
459ce517f18SJagan Teki 
460ce517f18SJagan Teki 	if (icn->vdd2) {
461ce517f18SJagan Teki 		ret = regulator_enable(icn->vdd2);
462ce517f18SJagan Teki 		if (ret)
463ce517f18SJagan Teki 			DRM_DEV_ERROR(icn->dev,
464ce517f18SJagan Teki 				      "failed to enable VDD2 regulator: %d\n", ret);
465ce517f18SJagan Teki 	}
466ce517f18SJagan Teki 
467ce517f18SJagan Teki 	if (icn->vdd3) {
468ce517f18SJagan Teki 		ret = regulator_enable(icn->vdd3);
469ce517f18SJagan Teki 		if (ret)
470ce517f18SJagan Teki 			DRM_DEV_ERROR(icn->dev,
471ce517f18SJagan Teki 				      "failed to enable VDD3 regulator: %d\n", ret);
472ce517f18SJagan Teki 	}
473ce517f18SJagan Teki 
474*378e0f9fSMarek Vasut 	ret = clk_prepare_enable(icn->refclk);
475*378e0f9fSMarek Vasut 	if (ret)
476*378e0f9fSMarek Vasut 		DRM_DEV_ERROR(icn->dev,
477*378e0f9fSMarek Vasut 			      "failed to enable RECLK clock: %d\n", ret);
478*378e0f9fSMarek Vasut 
479ce517f18SJagan Teki 	gpiod_set_value(icn->enable_gpio, 1);
480ce517f18SJagan Teki 
481ce517f18SJagan Teki 	usleep_range(10000, 11000);
482ce517f18SJagan Teki }
483ce517f18SJagan Teki 
4843b26a291SJagan Teki static void chipone_atomic_post_disable(struct drm_bridge *bridge,
4853b26a291SJagan Teki 					struct drm_bridge_state *old_bridge_state)
486ce517f18SJagan Teki {
487ce517f18SJagan Teki 	struct chipone *icn = bridge_to_chipone(bridge);
488ce517f18SJagan Teki 
489*378e0f9fSMarek Vasut 	clk_disable_unprepare(icn->refclk);
490*378e0f9fSMarek Vasut 
491ce517f18SJagan Teki 	if (icn->vdd1)
492ce517f18SJagan Teki 		regulator_disable(icn->vdd1);
493ce517f18SJagan Teki 
494ce517f18SJagan Teki 	if (icn->vdd2)
495ce517f18SJagan Teki 		regulator_disable(icn->vdd2);
496ce517f18SJagan Teki 
497ce517f18SJagan Teki 	if (icn->vdd3)
498ce517f18SJagan Teki 		regulator_disable(icn->vdd3);
499ce517f18SJagan Teki 
500ce517f18SJagan Teki 	gpiod_set_value(icn->enable_gpio, 0);
501ce517f18SJagan Teki }
502ce517f18SJagan Teki 
50350d76e3dSJagan Teki static void chipone_mode_set(struct drm_bridge *bridge,
50450d76e3dSJagan Teki 			     const struct drm_display_mode *mode,
50550d76e3dSJagan Teki 			     const struct drm_display_mode *adjusted_mode)
50650d76e3dSJagan Teki {
50750d76e3dSJagan Teki 	struct chipone *icn = bridge_to_chipone(bridge);
50850d76e3dSJagan Teki 
50950d76e3dSJagan Teki 	drm_mode_copy(&icn->mode, adjusted_mode);
5108dde6f74SMarek Vasut };
5118dde6f74SMarek Vasut 
5128dde6f74SMarek Vasut static int chipone_dsi_attach(struct chipone *icn)
5138dde6f74SMarek Vasut {
5148dde6f74SMarek Vasut 	struct mipi_dsi_device *dsi = icn->dsi;
5154ab85930SMarek Vasut 	struct device *dev = icn->dev;
5164ab85930SMarek Vasut 	int dsi_lanes, ret;
5178dde6f74SMarek Vasut 
5184af48f1dSMarek Vasut 	dsi_lanes = drm_of_get_data_lanes_count_ep(dev->of_node, 0, 0, 1, 4);
5194ab85930SMarek Vasut 
5204ab85930SMarek Vasut 	/*
5214ab85930SMarek Vasut 	 * If the 'data-lanes' property does not exist in DT or is invalid,
5224ab85930SMarek Vasut 	 * default to previously hard-coded behavior, which was 4 data lanes.
5234ab85930SMarek Vasut 	 */
5244af48f1dSMarek Vasut 	if (dsi_lanes < 0)
5254ab85930SMarek Vasut 		icn->dsi->lanes = 4;
5264af48f1dSMarek Vasut 	else
5274af48f1dSMarek Vasut 		icn->dsi->lanes = dsi_lanes;
5284ab85930SMarek Vasut 
5298dde6f74SMarek Vasut 	dsi->format = MIPI_DSI_FMT_RGB888;
5308dde6f74SMarek Vasut 	dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |
5318dde6f74SMarek Vasut 			  MIPI_DSI_MODE_LPM | MIPI_DSI_MODE_NO_EOT_PACKET;
5324d054ca9SMarek Vasut 	dsi->hs_rate = 500000000;
5334d054ca9SMarek Vasut 	dsi->lp_rate = 16000000;
5348dde6f74SMarek Vasut 
5358dde6f74SMarek Vasut 	ret = mipi_dsi_attach(dsi);
5368dde6f74SMarek Vasut 	if (ret < 0)
5378dde6f74SMarek Vasut 		dev_err(icn->dev, "failed to attach dsi\n");
5388dde6f74SMarek Vasut 
5398dde6f74SMarek Vasut 	return ret;
5408dde6f74SMarek Vasut }
5418dde6f74SMarek Vasut 
5428dde6f74SMarek Vasut static int chipone_dsi_host_attach(struct chipone *icn)
5438dde6f74SMarek Vasut {
5448dde6f74SMarek Vasut 	struct device *dev = icn->dev;
5458dde6f74SMarek Vasut 	struct device_node *host_node;
5468dde6f74SMarek Vasut 	struct device_node *endpoint;
5478dde6f74SMarek Vasut 	struct mipi_dsi_device *dsi;
5488dde6f74SMarek Vasut 	struct mipi_dsi_host *host;
5498dde6f74SMarek Vasut 	int ret = 0;
5508dde6f74SMarek Vasut 
5518dde6f74SMarek Vasut 	const struct mipi_dsi_device_info info = {
5528dde6f74SMarek Vasut 		.type = "chipone",
5538dde6f74SMarek Vasut 		.channel = 0,
5548dde6f74SMarek Vasut 		.node = NULL,
5558dde6f74SMarek Vasut 	};
5568dde6f74SMarek Vasut 
5578dde6f74SMarek Vasut 	endpoint = of_graph_get_endpoint_by_regs(dev->of_node, 0, 0);
5588dde6f74SMarek Vasut 	host_node = of_graph_get_remote_port_parent(endpoint);
5598dde6f74SMarek Vasut 	of_node_put(endpoint);
5608dde6f74SMarek Vasut 
5618dde6f74SMarek Vasut 	if (!host_node)
5628dde6f74SMarek Vasut 		return -EINVAL;
5638dde6f74SMarek Vasut 
5648dde6f74SMarek Vasut 	host = of_find_mipi_dsi_host_by_node(host_node);
5658dde6f74SMarek Vasut 	of_node_put(host_node);
5668dde6f74SMarek Vasut 	if (!host) {
5678dde6f74SMarek Vasut 		dev_err(dev, "failed to find dsi host\n");
5688dde6f74SMarek Vasut 		return -EPROBE_DEFER;
5698dde6f74SMarek Vasut 	}
5708dde6f74SMarek Vasut 
5718dde6f74SMarek Vasut 	dsi = mipi_dsi_device_register_full(host, &info);
5728dde6f74SMarek Vasut 	if (IS_ERR(dsi)) {
5738dde6f74SMarek Vasut 		return dev_err_probe(dev, PTR_ERR(dsi),
5748dde6f74SMarek Vasut 				     "failed to create dsi device\n");
5758dde6f74SMarek Vasut 	}
5768dde6f74SMarek Vasut 
5778dde6f74SMarek Vasut 	icn->dsi = dsi;
5788dde6f74SMarek Vasut 
5798dde6f74SMarek Vasut 	ret = chipone_dsi_attach(icn);
5808dde6f74SMarek Vasut 	if (ret < 0)
5818dde6f74SMarek Vasut 		mipi_dsi_device_unregister(dsi);
5828dde6f74SMarek Vasut 
5838dde6f74SMarek Vasut 	return ret;
58450d76e3dSJagan Teki }
58550d76e3dSJagan Teki 
586ce517f18SJagan Teki static int chipone_attach(struct drm_bridge *bridge, enum drm_bridge_attach_flags flags)
587ce517f18SJagan Teki {
588ce517f18SJagan Teki 	struct chipone *icn = bridge_to_chipone(bridge);
589ce517f18SJagan Teki 
590ce517f18SJagan Teki 	return drm_bridge_attach(bridge->encoder, icn->panel_bridge, bridge, flags);
591ce517f18SJagan Teki }
592ce517f18SJagan Teki 
593cda3822aSMarek Vasut #define MAX_INPUT_SEL_FORMATS	1
594cda3822aSMarek Vasut 
595cda3822aSMarek Vasut static u32 *
596cda3822aSMarek Vasut chipone_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
597cda3822aSMarek Vasut 				  struct drm_bridge_state *bridge_state,
598cda3822aSMarek Vasut 				  struct drm_crtc_state *crtc_state,
599cda3822aSMarek Vasut 				  struct drm_connector_state *conn_state,
600cda3822aSMarek Vasut 				  u32 output_fmt,
601cda3822aSMarek Vasut 				  unsigned int *num_input_fmts)
602cda3822aSMarek Vasut {
603cda3822aSMarek Vasut 	u32 *input_fmts;
604cda3822aSMarek Vasut 
605cda3822aSMarek Vasut 	*num_input_fmts = 0;
606cda3822aSMarek Vasut 
607cda3822aSMarek Vasut 	input_fmts = kcalloc(MAX_INPUT_SEL_FORMATS, sizeof(*input_fmts),
608cda3822aSMarek Vasut 			     GFP_KERNEL);
609cda3822aSMarek Vasut 	if (!input_fmts)
610cda3822aSMarek Vasut 		return NULL;
611cda3822aSMarek Vasut 
612cda3822aSMarek Vasut 	/* This is the DSI-end bus format */
613cda3822aSMarek Vasut 	input_fmts[0] = MEDIA_BUS_FMT_RGB888_1X24;
614cda3822aSMarek Vasut 	*num_input_fmts = 1;
615cda3822aSMarek Vasut 
616cda3822aSMarek Vasut 	return input_fmts;
617cda3822aSMarek Vasut }
618cda3822aSMarek Vasut 
619ce517f18SJagan Teki static const struct drm_bridge_funcs chipone_bridge_funcs = {
6203b26a291SJagan Teki 	.atomic_duplicate_state	= drm_atomic_helper_bridge_duplicate_state,
6213b26a291SJagan Teki 	.atomic_destroy_state	= drm_atomic_helper_bridge_destroy_state,
6223b26a291SJagan Teki 	.atomic_reset		= drm_atomic_helper_bridge_reset,
6233b26a291SJagan Teki 	.atomic_pre_enable	= chipone_atomic_pre_enable,
6243b26a291SJagan Teki 	.atomic_enable		= chipone_atomic_enable,
6253b26a291SJagan Teki 	.atomic_post_disable	= chipone_atomic_post_disable,
62650d76e3dSJagan Teki 	.mode_set		= chipone_mode_set,
627ce517f18SJagan Teki 	.attach			= chipone_attach,
628cda3822aSMarek Vasut 	.atomic_get_input_bus_fmts = chipone_atomic_get_input_bus_fmts,
629ce517f18SJagan Teki };
630ce517f18SJagan Teki 
631ce517f18SJagan Teki static int chipone_parse_dt(struct chipone *icn)
632ce517f18SJagan Teki {
633ce517f18SJagan Teki 	struct device *dev = icn->dev;
634ce517f18SJagan Teki 	int ret;
635ce517f18SJagan Teki 
636*378e0f9fSMarek Vasut 	icn->refclk = devm_clk_get_optional(dev, "refclk");
637*378e0f9fSMarek Vasut 	if (IS_ERR(icn->refclk)) {
638*378e0f9fSMarek Vasut 		ret = PTR_ERR(icn->refclk);
639*378e0f9fSMarek Vasut 		DRM_DEV_ERROR(dev, "failed to get REFCLK clock: %d\n", ret);
640*378e0f9fSMarek Vasut 		return ret;
641*378e0f9fSMarek Vasut 	} else if (icn->refclk) {
642*378e0f9fSMarek Vasut 		icn->refclk_rate = clk_get_rate(icn->refclk);
643*378e0f9fSMarek Vasut 		if (icn->refclk_rate < 10000000 || icn->refclk_rate > 154000000) {
644*378e0f9fSMarek Vasut 			DRM_DEV_ERROR(dev, "REFCLK out of range: %ld Hz\n",
645*378e0f9fSMarek Vasut 				      icn->refclk_rate);
646*378e0f9fSMarek Vasut 			return -EINVAL;
647*378e0f9fSMarek Vasut 		}
648*378e0f9fSMarek Vasut 	}
649*378e0f9fSMarek Vasut 
650ce517f18SJagan Teki 	icn->vdd1 = devm_regulator_get_optional(dev, "vdd1");
651ce517f18SJagan Teki 	if (IS_ERR(icn->vdd1)) {
652ce517f18SJagan Teki 		ret = PTR_ERR(icn->vdd1);
653ce517f18SJagan Teki 		if (ret == -EPROBE_DEFER)
654ce517f18SJagan Teki 			return -EPROBE_DEFER;
655ce517f18SJagan Teki 		icn->vdd1 = NULL;
656ce517f18SJagan Teki 		DRM_DEV_DEBUG(dev, "failed to get VDD1 regulator: %d\n", ret);
657ce517f18SJagan Teki 	}
658ce517f18SJagan Teki 
659ce517f18SJagan Teki 	icn->vdd2 = devm_regulator_get_optional(dev, "vdd2");
660ce517f18SJagan Teki 	if (IS_ERR(icn->vdd2)) {
661ce517f18SJagan Teki 		ret = PTR_ERR(icn->vdd2);
662ce517f18SJagan Teki 		if (ret == -EPROBE_DEFER)
663ce517f18SJagan Teki 			return -EPROBE_DEFER;
664ce517f18SJagan Teki 		icn->vdd2 = NULL;
665ce517f18SJagan Teki 		DRM_DEV_DEBUG(dev, "failed to get VDD2 regulator: %d\n", ret);
666ce517f18SJagan Teki 	}
667ce517f18SJagan Teki 
668ce517f18SJagan Teki 	icn->vdd3 = devm_regulator_get_optional(dev, "vdd3");
669ce517f18SJagan Teki 	if (IS_ERR(icn->vdd3)) {
670ce517f18SJagan Teki 		ret = PTR_ERR(icn->vdd3);
671ce517f18SJagan Teki 		if (ret == -EPROBE_DEFER)
672ce517f18SJagan Teki 			return -EPROBE_DEFER;
673ce517f18SJagan Teki 		icn->vdd3 = NULL;
674ce517f18SJagan Teki 		DRM_DEV_DEBUG(dev, "failed to get VDD3 regulator: %d\n", ret);
675ce517f18SJagan Teki 	}
676ce517f18SJagan Teki 
677ce517f18SJagan Teki 	icn->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
678ce517f18SJagan Teki 	if (IS_ERR(icn->enable_gpio)) {
679ce517f18SJagan Teki 		DRM_DEV_ERROR(dev, "failed to get enable GPIO\n");
680ce517f18SJagan Teki 		return PTR_ERR(icn->enable_gpio);
681ce517f18SJagan Teki 	}
682ce517f18SJagan Teki 
683c803ae6dSJosé Expósito 	icn->panel_bridge = devm_drm_of_get_bridge(dev, dev->of_node, 1, 0);
684ce517f18SJagan Teki 	if (IS_ERR(icn->panel_bridge))
685ce517f18SJagan Teki 		return PTR_ERR(icn->panel_bridge);
686ce517f18SJagan Teki 
687ce517f18SJagan Teki 	return 0;
688ce517f18SJagan Teki }
689ce517f18SJagan Teki 
6908dde6f74SMarek Vasut static int chipone_common_probe(struct device *dev, struct chipone **icnr)
691ce517f18SJagan Teki {
692ce517f18SJagan Teki 	struct chipone *icn;
693ce517f18SJagan Teki 	int ret;
694ce517f18SJagan Teki 
695ce517f18SJagan Teki 	icn = devm_kzalloc(dev, sizeof(struct chipone), GFP_KERNEL);
696ce517f18SJagan Teki 	if (!icn)
697ce517f18SJagan Teki 		return -ENOMEM;
698ce517f18SJagan Teki 
699ce517f18SJagan Teki 	icn->dev = dev;
700ce517f18SJagan Teki 
701ce517f18SJagan Teki 	ret = chipone_parse_dt(icn);
702ce517f18SJagan Teki 	if (ret)
703ce517f18SJagan Teki 		return ret;
704ce517f18SJagan Teki 
705ce517f18SJagan Teki 	icn->bridge.funcs = &chipone_bridge_funcs;
706ce517f18SJagan Teki 	icn->bridge.type = DRM_MODE_CONNECTOR_DPI;
707ce517f18SJagan Teki 	icn->bridge.of_node = dev->of_node;
708ce517f18SJagan Teki 
7098dde6f74SMarek Vasut 	*icnr = icn;
710ce517f18SJagan Teki 
711ce517f18SJagan Teki 	return ret;
712ce517f18SJagan Teki }
713ce517f18SJagan Teki 
7148dde6f74SMarek Vasut static int chipone_dsi_probe(struct mipi_dsi_device *dsi)
7158dde6f74SMarek Vasut {
7168dde6f74SMarek Vasut 	struct device *dev = &dsi->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(dev, &chipone_dsi_regmap_bus,
725fb47723aSMarek Vasut 				       dsi, &chipone_regmap_config);
726fb47723aSMarek Vasut 	if (IS_ERR(icn->regmap))
727fb47723aSMarek Vasut 		return PTR_ERR(icn->regmap);
728fb47723aSMarek Vasut 
7298dde6f74SMarek Vasut 	icn->interface_i2c = false;
7308dde6f74SMarek Vasut 	icn->dsi = dsi;
7318dde6f74SMarek Vasut 
7328dde6f74SMarek Vasut 	mipi_dsi_set_drvdata(dsi, icn);
7338dde6f74SMarek Vasut 
7348dde6f74SMarek Vasut 	drm_bridge_add(&icn->bridge);
7358dde6f74SMarek Vasut 
7368dde6f74SMarek Vasut 	ret = chipone_dsi_attach(icn);
7378dde6f74SMarek Vasut 	if (ret)
7388dde6f74SMarek Vasut 		drm_bridge_remove(&icn->bridge);
7398dde6f74SMarek Vasut 
7408dde6f74SMarek Vasut 	return ret;
7418dde6f74SMarek Vasut }
7428dde6f74SMarek Vasut 
7438dde6f74SMarek Vasut static int chipone_i2c_probe(struct i2c_client *client,
7448dde6f74SMarek Vasut 			     const struct i2c_device_id *id)
7458dde6f74SMarek Vasut {
7468dde6f74SMarek Vasut 	struct device *dev = &client->dev;
7478dde6f74SMarek Vasut 	struct chipone *icn;
7488dde6f74SMarek Vasut 	int ret;
7498dde6f74SMarek Vasut 
7508dde6f74SMarek Vasut 	ret = chipone_common_probe(dev, &icn);
7518dde6f74SMarek Vasut 	if (ret)
7528dde6f74SMarek Vasut 		return ret;
7538dde6f74SMarek Vasut 
754fb47723aSMarek Vasut 	icn->regmap = devm_regmap_init_i2c(client, &chipone_regmap_config);
755fb47723aSMarek Vasut 	if (IS_ERR(icn->regmap))
756fb47723aSMarek Vasut 		return PTR_ERR(icn->regmap);
757fb47723aSMarek Vasut 
7588dde6f74SMarek Vasut 	icn->interface_i2c = true;
7598dde6f74SMarek Vasut 	icn->client = client;
7608dde6f74SMarek Vasut 	dev_set_drvdata(dev, icn);
7618dde6f74SMarek Vasut 	i2c_set_clientdata(client, icn);
7628dde6f74SMarek Vasut 
7638dde6f74SMarek Vasut 	drm_bridge_add(&icn->bridge);
7648dde6f74SMarek Vasut 
7658dde6f74SMarek Vasut 	return chipone_dsi_host_attach(icn);
7668dde6f74SMarek Vasut }
7678dde6f74SMarek Vasut 
76879abca2bSUwe Kleine-König static void chipone_dsi_remove(struct mipi_dsi_device *dsi)
769ce517f18SJagan Teki {
770ce517f18SJagan Teki 	struct chipone *icn = mipi_dsi_get_drvdata(dsi);
771ce517f18SJagan Teki 
772ce517f18SJagan Teki 	mipi_dsi_detach(dsi);
773ce517f18SJagan Teki 	drm_bridge_remove(&icn->bridge);
774ce517f18SJagan Teki }
775ce517f18SJagan Teki 
776ce517f18SJagan Teki static const struct of_device_id chipone_of_match[] = {
777ce517f18SJagan Teki 	{ .compatible = "chipone,icn6211", },
778ce517f18SJagan Teki 	{ /* sentinel */ }
779ce517f18SJagan Teki };
780ce517f18SJagan Teki MODULE_DEVICE_TABLE(of, chipone_of_match);
781ce517f18SJagan Teki 
7828dde6f74SMarek Vasut static struct mipi_dsi_driver chipone_dsi_driver = {
7838dde6f74SMarek Vasut 	.probe = chipone_dsi_probe,
7848dde6f74SMarek Vasut 	.remove = chipone_dsi_remove,
785ce517f18SJagan Teki 	.driver = {
786ce517f18SJagan Teki 		.name = "chipone-icn6211",
787ce517f18SJagan Teki 		.owner = THIS_MODULE,
788ce517f18SJagan Teki 		.of_match_table = chipone_of_match,
789ce517f18SJagan Teki 	},
790ce517f18SJagan Teki };
7918dde6f74SMarek Vasut 
7928dde6f74SMarek Vasut static struct i2c_device_id chipone_i2c_id[] = {
7938dde6f74SMarek Vasut 	{ "chipone,icn6211" },
7948dde6f74SMarek Vasut 	{},
7958dde6f74SMarek Vasut };
7968dde6f74SMarek Vasut MODULE_DEVICE_TABLE(i2c, chipone_i2c_id);
7978dde6f74SMarek Vasut 
7988dde6f74SMarek Vasut static struct i2c_driver chipone_i2c_driver = {
7998dde6f74SMarek Vasut 	.probe = chipone_i2c_probe,
8008dde6f74SMarek Vasut 	.id_table = chipone_i2c_id,
8018dde6f74SMarek Vasut 	.driver = {
8028dde6f74SMarek Vasut 		.name = "chipone-icn6211-i2c",
8038dde6f74SMarek Vasut 		.of_match_table = chipone_of_match,
8048dde6f74SMarek Vasut 	},
8058dde6f74SMarek Vasut };
8068dde6f74SMarek Vasut 
8078dde6f74SMarek Vasut static int __init chipone_init(void)
8088dde6f74SMarek Vasut {
8098dde6f74SMarek Vasut 	if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
8108dde6f74SMarek Vasut 		mipi_dsi_driver_register(&chipone_dsi_driver);
8118dde6f74SMarek Vasut 
8128dde6f74SMarek Vasut 	return i2c_add_driver(&chipone_i2c_driver);
8138dde6f74SMarek Vasut }
8148dde6f74SMarek Vasut module_init(chipone_init);
8158dde6f74SMarek Vasut 
8167a828f1fSMarek Vasut static void __exit chipone_exit(void)
8178dde6f74SMarek Vasut {
8188dde6f74SMarek Vasut 	i2c_del_driver(&chipone_i2c_driver);
8198dde6f74SMarek Vasut 
8208dde6f74SMarek Vasut 	if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
8218dde6f74SMarek Vasut 		mipi_dsi_driver_unregister(&chipone_dsi_driver);
8228dde6f74SMarek Vasut }
8238dde6f74SMarek Vasut module_exit(chipone_exit);
824ce517f18SJagan Teki 
825ce517f18SJagan Teki MODULE_AUTHOR("Jagan Teki <jagan@amarulasolutions.com>");
826ce517f18SJagan Teki MODULE_DESCRIPTION("Chipone ICN6211 MIPI-DSI to RGB Converter Bridge");
827ce517f18SJagan Teki MODULE_LICENSE("GPL");
828