xref: /linux/drivers/gpu/drm/bridge/parade-ps8640.c (revision 255490f9150da7c6dabe468f3a877b92fd0f02c1)
1bc1aee7fSJitao Shi // SPDX-License-Identifier: GPL-2.0-only
2bc1aee7fSJitao Shi /*
3bc1aee7fSJitao Shi  * Copyright (c) 2016 MediaTek Inc.
4bc1aee7fSJitao Shi  */
5bc1aee7fSJitao Shi 
6bc1aee7fSJitao Shi #include <linux/delay.h>
7bc1aee7fSJitao Shi #include <linux/err.h>
8bc1aee7fSJitao Shi #include <linux/gpio/consumer.h>
9bc1aee7fSJitao Shi #include <linux/i2c.h>
10bc1aee7fSJitao Shi #include <linux/module.h>
11bc1aee7fSJitao Shi #include <linux/of_graph.h>
12826cff3fSPhilip Chen #include <linux/pm_runtime.h>
13692d8db0SPhilip Chen #include <linux/regmap.h>
14bc1aee7fSJitao Shi #include <linux/regulator/consumer.h>
15bc1aee7fSJitao Shi 
16da68386dSThomas Zimmermann #include <drm/display/drm_dp_aux_bus.h>
17da68386dSThomas Zimmermann #include <drm/display/drm_dp_helper.h>
18bc1aee7fSJitao Shi #include <drm/drm_bridge.h>
19*255490f9SVille Syrjälä #include <drm/drm_edid.h>
20bc1aee7fSJitao Shi #include <drm/drm_mipi_dsi.h>
21bc1aee7fSJitao Shi #include <drm/drm_of.h>
22bc1aee7fSJitao Shi #include <drm/drm_panel.h>
23bc1aee7fSJitao Shi #include <drm/drm_print.h>
24bc1aee7fSJitao Shi 
2513afcdd7SPhilip Chen #define PAGE0_AUXCH_CFG3	0x76
2613afcdd7SPhilip Chen #define  AUXCH_CFG3_RESET	0xff
2713afcdd7SPhilip Chen #define PAGE0_SWAUX_ADDR_7_0	0x7d
2813afcdd7SPhilip Chen #define PAGE0_SWAUX_ADDR_15_8	0x7e
2913afcdd7SPhilip Chen #define PAGE0_SWAUX_ADDR_23_16	0x7f
3013afcdd7SPhilip Chen #define  SWAUX_ADDR_MASK	GENMASK(19, 0)
3113afcdd7SPhilip Chen #define PAGE0_SWAUX_LENGTH	0x80
3213afcdd7SPhilip Chen #define  SWAUX_LENGTH_MASK	GENMASK(3, 0)
3313afcdd7SPhilip Chen #define  SWAUX_NO_PAYLOAD	BIT(7)
3413afcdd7SPhilip Chen #define PAGE0_SWAUX_WDATA	0x81
3513afcdd7SPhilip Chen #define PAGE0_SWAUX_RDATA	0x82
3613afcdd7SPhilip Chen #define PAGE0_SWAUX_CTRL	0x83
3713afcdd7SPhilip Chen #define  SWAUX_SEND		BIT(0)
3813afcdd7SPhilip Chen #define PAGE0_SWAUX_STATUS	0x84
3913afcdd7SPhilip Chen #define  SWAUX_M_MASK		GENMASK(4, 0)
4013afcdd7SPhilip Chen #define  SWAUX_STATUS_MASK	GENMASK(7, 5)
4113afcdd7SPhilip Chen #define  SWAUX_STATUS_NACK	(0x1 << 5)
4213afcdd7SPhilip Chen #define  SWAUX_STATUS_DEFER	(0x2 << 5)
4313afcdd7SPhilip Chen #define  SWAUX_STATUS_ACKM	(0x3 << 5)
4413afcdd7SPhilip Chen #define  SWAUX_STATUS_INVALID	(0x4 << 5)
4513afcdd7SPhilip Chen #define  SWAUX_STATUS_I2C_NACK	(0x5 << 5)
4613afcdd7SPhilip Chen #define  SWAUX_STATUS_I2C_DEFER	(0x6 << 5)
4713afcdd7SPhilip Chen #define  SWAUX_STATUS_TIMEOUT	(0x7 << 5)
4813afcdd7SPhilip Chen 
49bc1aee7fSJitao Shi #define PAGE2_GPIO_H		0xa7
50bc1aee7fSJitao Shi #define  PS_GPIO9		BIT(1)
51bc1aee7fSJitao Shi #define PAGE2_I2C_BYPASS	0xea
52bc1aee7fSJitao Shi #define  I2C_BYPASS_EN		0xd0
53bc1aee7fSJitao Shi #define PAGE2_MCS_EN		0xf3
54bc1aee7fSJitao Shi #define  MCS_EN			BIT(0)
5528210a3fSPhilip Chen 
56bc1aee7fSJitao Shi #define PAGE3_SET_ADD		0xfe
57bc1aee7fSJitao Shi #define  VDO_CTL_ADD		0x13
58bc1aee7fSJitao Shi #define  VDO_DIS		0x18
59bc1aee7fSJitao Shi #define  VDO_EN			0x1c
6028210a3fSPhilip Chen 
6128210a3fSPhilip Chen #define NUM_MIPI_LANES		4
62bc1aee7fSJitao Shi 
63692d8db0SPhilip Chen #define COMMON_PS8640_REGMAP_CONFIG \
64692d8db0SPhilip Chen 	.reg_bits = 8, \
65692d8db0SPhilip Chen 	.val_bits = 8, \
66692d8db0SPhilip Chen 	.cache_type = REGCACHE_NONE
67692d8db0SPhilip Chen 
68bc1aee7fSJitao Shi /*
69bc1aee7fSJitao Shi  * PS8640 uses multiple addresses:
70bc1aee7fSJitao Shi  * page[0]: for DP control
71bc1aee7fSJitao Shi  * page[1]: for VIDEO Bridge
72bc1aee7fSJitao Shi  * page[2]: for control top
73bc1aee7fSJitao Shi  * page[3]: for DSI Link Control1
74bc1aee7fSJitao Shi  * page[4]: for MIPI Phy
75bc1aee7fSJitao Shi  * page[5]: for VPLL
76bc1aee7fSJitao Shi  * page[6]: for DSI Link Control2
77bc1aee7fSJitao Shi  * page[7]: for SPI ROM mapping
78bc1aee7fSJitao Shi  */
79bc1aee7fSJitao Shi enum page_addr_offset {
80bc1aee7fSJitao Shi 	PAGE0_DP_CNTL = 0,
81bc1aee7fSJitao Shi 	PAGE1_VDO_BDG,
82bc1aee7fSJitao Shi 	PAGE2_TOP_CNTL,
83bc1aee7fSJitao Shi 	PAGE3_DSI_CNTL1,
84bc1aee7fSJitao Shi 	PAGE4_MIPI_PHY,
85bc1aee7fSJitao Shi 	PAGE5_VPLL,
86bc1aee7fSJitao Shi 	PAGE6_DSI_CNTL2,
87bc1aee7fSJitao Shi 	PAGE7_SPI_CNTL,
88bc1aee7fSJitao Shi 	MAX_DEVS
89bc1aee7fSJitao Shi };
90bc1aee7fSJitao Shi 
91bc1aee7fSJitao Shi enum ps8640_vdo_control {
92bc1aee7fSJitao Shi 	DISABLE = VDO_DIS,
93bc1aee7fSJitao Shi 	ENABLE = VDO_EN,
94bc1aee7fSJitao Shi };
95bc1aee7fSJitao Shi 
96bc1aee7fSJitao Shi struct ps8640 {
97bc1aee7fSJitao Shi 	struct drm_bridge bridge;
98bc1aee7fSJitao Shi 	struct drm_bridge *panel_bridge;
9913afcdd7SPhilip Chen 	struct drm_dp_aux aux;
100bc1aee7fSJitao Shi 	struct mipi_dsi_device *dsi;
101bc1aee7fSJitao Shi 	struct i2c_client *page[MAX_DEVS];
102692d8db0SPhilip Chen 	struct regmap	*regmap[MAX_DEVS];
103bc1aee7fSJitao Shi 	struct regulator_bulk_data supplies[2];
104bc1aee7fSJitao Shi 	struct gpio_desc *gpio_reset;
105bc1aee7fSJitao Shi 	struct gpio_desc *gpio_powerdown;
1069294914dSAngeloGioacchino Del Regno 	struct device_link *link;
107826cff3fSPhilip Chen 	bool pre_enabled;
108bc1aee7fSJitao Shi };
109bc1aee7fSJitao Shi 
110692d8db0SPhilip Chen static const struct regmap_config ps8640_regmap_config[] = {
111692d8db0SPhilip Chen 	[PAGE0_DP_CNTL] = {
112692d8db0SPhilip Chen 		COMMON_PS8640_REGMAP_CONFIG,
113692d8db0SPhilip Chen 		.max_register = 0xbf,
114692d8db0SPhilip Chen 	},
115692d8db0SPhilip Chen 	[PAGE1_VDO_BDG] = {
116692d8db0SPhilip Chen 		COMMON_PS8640_REGMAP_CONFIG,
117692d8db0SPhilip Chen 		.max_register = 0xff,
118692d8db0SPhilip Chen 	},
119692d8db0SPhilip Chen 	[PAGE2_TOP_CNTL] = {
120692d8db0SPhilip Chen 		COMMON_PS8640_REGMAP_CONFIG,
121692d8db0SPhilip Chen 		.max_register = 0xff,
122692d8db0SPhilip Chen 	},
123692d8db0SPhilip Chen 	[PAGE3_DSI_CNTL1] = {
124692d8db0SPhilip Chen 		COMMON_PS8640_REGMAP_CONFIG,
125692d8db0SPhilip Chen 		.max_register = 0xff,
126692d8db0SPhilip Chen 	},
127692d8db0SPhilip Chen 	[PAGE4_MIPI_PHY] = {
128692d8db0SPhilip Chen 		COMMON_PS8640_REGMAP_CONFIG,
129692d8db0SPhilip Chen 		.max_register = 0xff,
130692d8db0SPhilip Chen 	},
131692d8db0SPhilip Chen 	[PAGE5_VPLL] = {
132692d8db0SPhilip Chen 		COMMON_PS8640_REGMAP_CONFIG,
133692d8db0SPhilip Chen 		.max_register = 0x7f,
134692d8db0SPhilip Chen 	},
135692d8db0SPhilip Chen 	[PAGE6_DSI_CNTL2] = {
136692d8db0SPhilip Chen 		COMMON_PS8640_REGMAP_CONFIG,
137692d8db0SPhilip Chen 		.max_register = 0xff,
138692d8db0SPhilip Chen 	},
139692d8db0SPhilip Chen 	[PAGE7_SPI_CNTL] = {
140692d8db0SPhilip Chen 		COMMON_PS8640_REGMAP_CONFIG,
141692d8db0SPhilip Chen 		.max_register = 0xff,
142692d8db0SPhilip Chen 	},
143692d8db0SPhilip Chen };
144692d8db0SPhilip Chen 
145bc1aee7fSJitao Shi static inline struct ps8640 *bridge_to_ps8640(struct drm_bridge *e)
146bc1aee7fSJitao Shi {
147bc1aee7fSJitao Shi 	return container_of(e, struct ps8640, bridge);
148bc1aee7fSJitao Shi }
149bc1aee7fSJitao Shi 
15013afcdd7SPhilip Chen static inline struct ps8640 *aux_to_ps8640(struct drm_dp_aux *aux)
15113afcdd7SPhilip Chen {
15213afcdd7SPhilip Chen 	return container_of(aux, struct ps8640, aux);
15313afcdd7SPhilip Chen }
15413afcdd7SPhilip Chen 
155e9d9f958SPhilip Chen static bool ps8640_of_panel_on_aux_bus(struct device *dev)
156e9d9f958SPhilip Chen {
157e9d9f958SPhilip Chen 	struct device_node *bus, *panel;
158e9d9f958SPhilip Chen 
159e9d9f958SPhilip Chen 	bus = of_get_child_by_name(dev->of_node, "aux-bus");
160e9d9f958SPhilip Chen 	if (!bus)
161e9d9f958SPhilip Chen 		return false;
162e9d9f958SPhilip Chen 
163e9d9f958SPhilip Chen 	panel = of_get_child_by_name(bus, "panel");
164e9d9f958SPhilip Chen 	of_node_put(bus);
165e9d9f958SPhilip Chen 	if (!panel)
166e9d9f958SPhilip Chen 		return false;
167e9d9f958SPhilip Chen 	of_node_put(panel);
168e9d9f958SPhilip Chen 
169e9d9f958SPhilip Chen 	return true;
170e9d9f958SPhilip Chen }
171e9d9f958SPhilip Chen 
172826cff3fSPhilip Chen static int ps8640_ensure_hpd(struct ps8640 *ps_bridge)
173826cff3fSPhilip Chen {
174826cff3fSPhilip Chen 	struct regmap *map = ps_bridge->regmap[PAGE2_TOP_CNTL];
175826cff3fSPhilip Chen 	struct device *dev = &ps_bridge->page[PAGE2_TOP_CNTL]->dev;
176826cff3fSPhilip Chen 	int status;
177826cff3fSPhilip Chen 	int ret;
178826cff3fSPhilip Chen 
179826cff3fSPhilip Chen 	/*
180826cff3fSPhilip Chen 	 * Apparently something about the firmware in the chip signals that
181826cff3fSPhilip Chen 	 * HPD goes high by reporting GPIO9 as high (even though HPD isn't
182826cff3fSPhilip Chen 	 * actually connected to GPIO9).
183826cff3fSPhilip Chen 	 */
184826cff3fSPhilip Chen 	ret = regmap_read_poll_timeout(map, PAGE2_GPIO_H, status,
185826cff3fSPhilip Chen 				       status & PS_GPIO9, 20 * 1000, 200 * 1000);
186826cff3fSPhilip Chen 
187826cff3fSPhilip Chen 	if (ret < 0)
188826cff3fSPhilip Chen 		dev_warn(dev, "HPD didn't go high: %d\n", ret);
189826cff3fSPhilip Chen 
190826cff3fSPhilip Chen 	return ret;
191826cff3fSPhilip Chen }
192826cff3fSPhilip Chen 
193826cff3fSPhilip Chen static ssize_t ps8640_aux_transfer_msg(struct drm_dp_aux *aux,
19413afcdd7SPhilip Chen 				       struct drm_dp_aux_msg *msg)
19513afcdd7SPhilip Chen {
19613afcdd7SPhilip Chen 	struct ps8640 *ps_bridge = aux_to_ps8640(aux);
19713afcdd7SPhilip Chen 	struct regmap *map = ps_bridge->regmap[PAGE0_DP_CNTL];
19813afcdd7SPhilip Chen 	struct device *dev = &ps_bridge->page[PAGE0_DP_CNTL]->dev;
19913afcdd7SPhilip Chen 	unsigned int len = msg->size;
20013afcdd7SPhilip Chen 	unsigned int data;
20113afcdd7SPhilip Chen 	unsigned int base;
20213afcdd7SPhilip Chen 	int ret;
20313afcdd7SPhilip Chen 	u8 request = msg->request &
20413afcdd7SPhilip Chen 		     ~(DP_AUX_I2C_MOT | DP_AUX_I2C_WRITE_STATUS_UPDATE);
20513afcdd7SPhilip Chen 	u8 *buf = msg->buffer;
20613afcdd7SPhilip Chen 	u8 addr_len[PAGE0_SWAUX_LENGTH + 1 - PAGE0_SWAUX_ADDR_7_0];
20713afcdd7SPhilip Chen 	u8 i;
20813afcdd7SPhilip Chen 	bool is_native_aux = false;
20913afcdd7SPhilip Chen 
21013afcdd7SPhilip Chen 	if (len > DP_AUX_MAX_PAYLOAD_BYTES)
21113afcdd7SPhilip Chen 		return -EINVAL;
21213afcdd7SPhilip Chen 
21313afcdd7SPhilip Chen 	if (msg->address & ~SWAUX_ADDR_MASK)
21413afcdd7SPhilip Chen 		return -EINVAL;
21513afcdd7SPhilip Chen 
21613afcdd7SPhilip Chen 	switch (request) {
21713afcdd7SPhilip Chen 	case DP_AUX_NATIVE_WRITE:
21813afcdd7SPhilip Chen 	case DP_AUX_NATIVE_READ:
21913afcdd7SPhilip Chen 		is_native_aux = true;
22013afcdd7SPhilip Chen 		fallthrough;
22113afcdd7SPhilip Chen 	case DP_AUX_I2C_WRITE:
22213afcdd7SPhilip Chen 	case DP_AUX_I2C_READ:
22313afcdd7SPhilip Chen 		break;
22413afcdd7SPhilip Chen 	default:
22513afcdd7SPhilip Chen 		return -EINVAL;
22613afcdd7SPhilip Chen 	}
22713afcdd7SPhilip Chen 
22813afcdd7SPhilip Chen 	ret = regmap_write(map, PAGE0_AUXCH_CFG3, AUXCH_CFG3_RESET);
22913afcdd7SPhilip Chen 	if (ret) {
23013afcdd7SPhilip Chen 		DRM_DEV_ERROR(dev, "failed to write PAGE0_AUXCH_CFG3: %d\n",
23113afcdd7SPhilip Chen 			      ret);
23213afcdd7SPhilip Chen 		return ret;
23313afcdd7SPhilip Chen 	}
23413afcdd7SPhilip Chen 
23513afcdd7SPhilip Chen 	/* Assume it's good */
23613afcdd7SPhilip Chen 	msg->reply = 0;
23713afcdd7SPhilip Chen 
23813afcdd7SPhilip Chen 	base = PAGE0_SWAUX_ADDR_7_0;
23913afcdd7SPhilip Chen 	addr_len[PAGE0_SWAUX_ADDR_7_0 - base] = msg->address;
24013afcdd7SPhilip Chen 	addr_len[PAGE0_SWAUX_ADDR_15_8 - base] = msg->address >> 8;
24113afcdd7SPhilip Chen 	addr_len[PAGE0_SWAUX_ADDR_23_16 - base] = (msg->address >> 16) |
24213afcdd7SPhilip Chen 						  (msg->request << 4);
24313afcdd7SPhilip Chen 	addr_len[PAGE0_SWAUX_LENGTH - base] = (len == 0) ? SWAUX_NO_PAYLOAD :
24413afcdd7SPhilip Chen 					      ((len - 1) & SWAUX_LENGTH_MASK);
24513afcdd7SPhilip Chen 
24613afcdd7SPhilip Chen 	regmap_bulk_write(map, PAGE0_SWAUX_ADDR_7_0, addr_len,
24713afcdd7SPhilip Chen 			  ARRAY_SIZE(addr_len));
24813afcdd7SPhilip Chen 
24913afcdd7SPhilip Chen 	if (len && (request == DP_AUX_NATIVE_WRITE ||
25013afcdd7SPhilip Chen 		    request == DP_AUX_I2C_WRITE)) {
25113afcdd7SPhilip Chen 		/* Write to the internal FIFO buffer */
25213afcdd7SPhilip Chen 		for (i = 0; i < len; i++) {
25313afcdd7SPhilip Chen 			ret = regmap_write(map, PAGE0_SWAUX_WDATA, buf[i]);
25413afcdd7SPhilip Chen 			if (ret) {
25513afcdd7SPhilip Chen 				DRM_DEV_ERROR(dev,
25613afcdd7SPhilip Chen 					      "failed to write WDATA: %d\n",
25713afcdd7SPhilip Chen 					      ret);
25813afcdd7SPhilip Chen 				return ret;
25913afcdd7SPhilip Chen 			}
26013afcdd7SPhilip Chen 		}
26113afcdd7SPhilip Chen 	}
26213afcdd7SPhilip Chen 
26313afcdd7SPhilip Chen 	regmap_write(map, PAGE0_SWAUX_CTRL, SWAUX_SEND);
26413afcdd7SPhilip Chen 
26513afcdd7SPhilip Chen 	/* Zero delay loop because i2c transactions are slow already */
26613afcdd7SPhilip Chen 	regmap_read_poll_timeout(map, PAGE0_SWAUX_CTRL, data,
26713afcdd7SPhilip Chen 				 !(data & SWAUX_SEND), 0, 50 * 1000);
26813afcdd7SPhilip Chen 
26913afcdd7SPhilip Chen 	regmap_read(map, PAGE0_SWAUX_STATUS, &data);
27013afcdd7SPhilip Chen 	if (ret) {
27113afcdd7SPhilip Chen 		DRM_DEV_ERROR(dev, "failed to read PAGE0_SWAUX_STATUS: %d\n",
27213afcdd7SPhilip Chen 			      ret);
27313afcdd7SPhilip Chen 		return ret;
27413afcdd7SPhilip Chen 	}
27513afcdd7SPhilip Chen 
27613afcdd7SPhilip Chen 	switch (data & SWAUX_STATUS_MASK) {
27713afcdd7SPhilip Chen 	/* Ignore the DEFER cases as they are already handled in hardware */
27813afcdd7SPhilip Chen 	case SWAUX_STATUS_NACK:
27913afcdd7SPhilip Chen 	case SWAUX_STATUS_I2C_NACK:
28013afcdd7SPhilip Chen 		/*
28113afcdd7SPhilip Chen 		 * The programming guide is not clear about whether a I2C NACK
28213afcdd7SPhilip Chen 		 * would trigger SWAUX_STATUS_NACK or SWAUX_STATUS_I2C_NACK. So
28313afcdd7SPhilip Chen 		 * we handle both cases together.
28413afcdd7SPhilip Chen 		 */
28513afcdd7SPhilip Chen 		if (is_native_aux)
28613afcdd7SPhilip Chen 			msg->reply |= DP_AUX_NATIVE_REPLY_NACK;
28713afcdd7SPhilip Chen 		else
28813afcdd7SPhilip Chen 			msg->reply |= DP_AUX_I2C_REPLY_NACK;
28913afcdd7SPhilip Chen 
29013afcdd7SPhilip Chen 		fallthrough;
29113afcdd7SPhilip Chen 	case SWAUX_STATUS_ACKM:
29213afcdd7SPhilip Chen 		len = data & SWAUX_M_MASK;
29313afcdd7SPhilip Chen 		break;
29413afcdd7SPhilip Chen 	case SWAUX_STATUS_INVALID:
29513afcdd7SPhilip Chen 		return -EOPNOTSUPP;
29613afcdd7SPhilip Chen 	case SWAUX_STATUS_TIMEOUT:
29713afcdd7SPhilip Chen 		return -ETIMEDOUT;
29813afcdd7SPhilip Chen 	}
29913afcdd7SPhilip Chen 
30013afcdd7SPhilip Chen 	if (len && (request == DP_AUX_NATIVE_READ ||
30113afcdd7SPhilip Chen 		    request == DP_AUX_I2C_READ)) {
30213afcdd7SPhilip Chen 		/* Read from the internal FIFO buffer */
30313afcdd7SPhilip Chen 		for (i = 0; i < len; i++) {
30413afcdd7SPhilip Chen 			ret = regmap_read(map, PAGE0_SWAUX_RDATA, &data);
30513afcdd7SPhilip Chen 			if (ret) {
30613afcdd7SPhilip Chen 				DRM_DEV_ERROR(dev,
30713afcdd7SPhilip Chen 					      "failed to read RDATA: %d\n",
30813afcdd7SPhilip Chen 					      ret);
30913afcdd7SPhilip Chen 				return ret;
31013afcdd7SPhilip Chen 			}
31113afcdd7SPhilip Chen 
31213afcdd7SPhilip Chen 			buf[i] = data;
31313afcdd7SPhilip Chen 		}
31413afcdd7SPhilip Chen 	}
31513afcdd7SPhilip Chen 
31613afcdd7SPhilip Chen 	return len;
31713afcdd7SPhilip Chen }
31813afcdd7SPhilip Chen 
319826cff3fSPhilip Chen static ssize_t ps8640_aux_transfer(struct drm_dp_aux *aux,
320826cff3fSPhilip Chen 				   struct drm_dp_aux_msg *msg)
321826cff3fSPhilip Chen {
322826cff3fSPhilip Chen 	struct ps8640 *ps_bridge = aux_to_ps8640(aux);
323826cff3fSPhilip Chen 	struct device *dev = &ps_bridge->page[PAGE0_DP_CNTL]->dev;
324826cff3fSPhilip Chen 	int ret;
325826cff3fSPhilip Chen 
326826cff3fSPhilip Chen 	pm_runtime_get_sync(dev);
327826cff3fSPhilip Chen 	ret = ps8640_ensure_hpd(ps_bridge);
328826cff3fSPhilip Chen 	if (!ret)
329826cff3fSPhilip Chen 		ret = ps8640_aux_transfer_msg(aux, msg);
330826cff3fSPhilip Chen 	pm_runtime_mark_last_busy(dev);
331826cff3fSPhilip Chen 	pm_runtime_put_autosuspend(dev);
332826cff3fSPhilip Chen 
333826cff3fSPhilip Chen 	return ret;
334826cff3fSPhilip Chen }
335826cff3fSPhilip Chen 
336826cff3fSPhilip Chen static void ps8640_bridge_vdo_control(struct ps8640 *ps_bridge,
337bc1aee7fSJitao Shi 				      const enum ps8640_vdo_control ctrl)
338bc1aee7fSJitao Shi {
339692d8db0SPhilip Chen 	struct regmap *map = ps_bridge->regmap[PAGE3_DSI_CNTL1];
340826cff3fSPhilip Chen 	struct device *dev = &ps_bridge->page[PAGE3_DSI_CNTL1]->dev;
341bc1aee7fSJitao Shi 	u8 vdo_ctrl_buf[] = { VDO_CTL_ADD, ctrl };
342bc1aee7fSJitao Shi 	int ret;
343bc1aee7fSJitao Shi 
344692d8db0SPhilip Chen 	ret = regmap_bulk_write(map, PAGE3_SET_ADD,
345692d8db0SPhilip Chen 				vdo_ctrl_buf, sizeof(vdo_ctrl_buf));
346692d8db0SPhilip Chen 
347826cff3fSPhilip Chen 	if (ret < 0)
348826cff3fSPhilip Chen 		dev_err(dev, "failed to %sable VDO: %d\n",
34994d4c132SEnric Balletbo i Serra 			ctrl == ENABLE ? "en" : "dis", ret);
35094d4c132SEnric Balletbo i Serra }
351bc1aee7fSJitao Shi 
352826cff3fSPhilip Chen static int __maybe_unused ps8640_resume(struct device *dev)
353bc1aee7fSJitao Shi {
354826cff3fSPhilip Chen 	struct ps8640 *ps_bridge = dev_get_drvdata(dev);
355826cff3fSPhilip Chen 	int ret;
35646f20630SEnric Balletbo i Serra 
357bc1aee7fSJitao Shi 	ret = regulator_bulk_enable(ARRAY_SIZE(ps_bridge->supplies),
358bc1aee7fSJitao Shi 				    ps_bridge->supplies);
359bc1aee7fSJitao Shi 	if (ret < 0) {
360826cff3fSPhilip Chen 		dev_err(dev, "cannot enable regulators %d\n", ret);
361826cff3fSPhilip Chen 		return ret;
362bc1aee7fSJitao Shi 	}
363bc1aee7fSJitao Shi 
364bc1aee7fSJitao Shi 	gpiod_set_value(ps_bridge->gpio_powerdown, 0);
365bc1aee7fSJitao Shi 	gpiod_set_value(ps_bridge->gpio_reset, 1);
366bc1aee7fSJitao Shi 	usleep_range(2000, 2500);
367bc1aee7fSJitao Shi 	gpiod_set_value(ps_bridge->gpio_reset, 0);
368bc1aee7fSJitao Shi 
369bc1aee7fSJitao Shi 	/*
370826cff3fSPhilip Chen 	 * Mystery 200 ms delay for the "MCU to be ready". It's unclear if
371826cff3fSPhilip Chen 	 * this is truly necessary since the MCU will already signal that
372826cff3fSPhilip Chen 	 * things are "good to go" by signaling HPD on "gpio 9". See
373826cff3fSPhilip Chen 	 * ps8640_ensure_hpd(). For now we'll keep this mystery delay just in
374826cff3fSPhilip Chen 	 * case.
375bc1aee7fSJitao Shi 	 */
376bc1aee7fSJitao Shi 	msleep(200);
377bc1aee7fSJitao Shi 
378826cff3fSPhilip Chen 	return 0;
379bc1aee7fSJitao Shi }
380bc1aee7fSJitao Shi 
381826cff3fSPhilip Chen static int __maybe_unused ps8640_suspend(struct device *dev)
382826cff3fSPhilip Chen {
383826cff3fSPhilip Chen 	struct ps8640 *ps_bridge = dev_get_drvdata(dev);
384826cff3fSPhilip Chen 	int ret;
385826cff3fSPhilip Chen 
386826cff3fSPhilip Chen 	gpiod_set_value(ps_bridge->gpio_reset, 1);
387826cff3fSPhilip Chen 	gpiod_set_value(ps_bridge->gpio_powerdown, 1);
388826cff3fSPhilip Chen 	ret = regulator_bulk_disable(ARRAY_SIZE(ps_bridge->supplies),
389826cff3fSPhilip Chen 				     ps_bridge->supplies);
390826cff3fSPhilip Chen 	if (ret < 0)
391826cff3fSPhilip Chen 		dev_err(dev, "cannot disable regulators %d\n", ret);
392826cff3fSPhilip Chen 
393826cff3fSPhilip Chen 	return ret;
394826cff3fSPhilip Chen }
395826cff3fSPhilip Chen 
396826cff3fSPhilip Chen static const struct dev_pm_ops ps8640_pm_ops = {
397826cff3fSPhilip Chen 	SET_RUNTIME_PM_OPS(ps8640_suspend, ps8640_resume, NULL)
398826cff3fSPhilip Chen 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
399826cff3fSPhilip Chen 				pm_runtime_force_resume)
400826cff3fSPhilip Chen };
401826cff3fSPhilip Chen 
402826cff3fSPhilip Chen static void ps8640_pre_enable(struct drm_bridge *bridge)
403826cff3fSPhilip Chen {
404826cff3fSPhilip Chen 	struct ps8640 *ps_bridge = bridge_to_ps8640(bridge);
405826cff3fSPhilip Chen 	struct regmap *map = ps_bridge->regmap[PAGE2_TOP_CNTL];
406826cff3fSPhilip Chen 	struct device *dev = &ps_bridge->page[PAGE0_DP_CNTL]->dev;
407826cff3fSPhilip Chen 	int ret;
408826cff3fSPhilip Chen 
409826cff3fSPhilip Chen 	pm_runtime_get_sync(dev);
410826cff3fSPhilip Chen 	ps8640_ensure_hpd(ps_bridge);
411bc1aee7fSJitao Shi 
412bc1aee7fSJitao Shi 	/*
413bc1aee7fSJitao Shi 	 * The Manufacturer Command Set (MCS) is a device dependent interface
414bc1aee7fSJitao Shi 	 * intended for factory programming of the display module default
415bc1aee7fSJitao Shi 	 * parameters. Once the display module is configured, the MCS shall be
416bc1aee7fSJitao Shi 	 * disabled by the manufacturer. Once disabled, all MCS commands are
417bc1aee7fSJitao Shi 	 * ignored by the display interface.
418bc1aee7fSJitao Shi 	 */
419bc1aee7fSJitao Shi 
420692d8db0SPhilip Chen 	ret = regmap_update_bits(map, PAGE2_MCS_EN, MCS_EN, 0);
421826cff3fSPhilip Chen 	if (ret < 0)
422826cff3fSPhilip Chen 		dev_warn(dev, "failed write PAGE2_MCS_EN: %d\n", ret);
423bc1aee7fSJitao Shi 
424bc1aee7fSJitao Shi 	/* Switch access edp panel's edid through i2c */
425692d8db0SPhilip Chen 	ret = regmap_write(map, PAGE2_I2C_BYPASS, I2C_BYPASS_EN);
426bc1aee7fSJitao Shi 	if (ret < 0)
427826cff3fSPhilip Chen 		dev_warn(dev, "failed write PAGE2_MCS_EN: %d\n", ret);
42846f20630SEnric Balletbo i Serra 
429826cff3fSPhilip Chen 	ps8640_bridge_vdo_control(ps_bridge, ENABLE);
43046f20630SEnric Balletbo i Serra 
431826cff3fSPhilip Chen 	ps_bridge->pre_enabled = true;
43246f20630SEnric Balletbo i Serra }
43346f20630SEnric Balletbo i Serra 
43446f20630SEnric Balletbo i Serra static void ps8640_post_disable(struct drm_bridge *bridge)
43546f20630SEnric Balletbo i Serra {
43646f20630SEnric Balletbo i Serra 	struct ps8640 *ps_bridge = bridge_to_ps8640(bridge);
43746f20630SEnric Balletbo i Serra 
438826cff3fSPhilip Chen 	ps_bridge->pre_enabled = false;
439826cff3fSPhilip Chen 
44046f20630SEnric Balletbo i Serra 	ps8640_bridge_vdo_control(ps_bridge, DISABLE);
441826cff3fSPhilip Chen 	pm_runtime_put_sync_suspend(&ps_bridge->page[PAGE0_DP_CNTL]->dev);
442bc1aee7fSJitao Shi }
443bc1aee7fSJitao Shi 
444a25b988fSLaurent Pinchart static int ps8640_bridge_attach(struct drm_bridge *bridge,
445a25b988fSLaurent Pinchart 				enum drm_bridge_attach_flags flags)
446bc1aee7fSJitao Shi {
447bc1aee7fSJitao Shi 	struct ps8640 *ps_bridge = bridge_to_ps8640(bridge);
448bc1aee7fSJitao Shi 	struct device *dev = &ps_bridge->page[0]->dev;
449bc1aee7fSJitao Shi 	int ret;
450812a65baSEnric Balletbo i Serra 
451812a65baSEnric Balletbo i Serra 	if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR))
452812a65baSEnric Balletbo i Serra 		return -EINVAL;
453812a65baSEnric Balletbo i Serra 
454f8378c04SDouglas Anderson 	ps_bridge->aux.drm_dev = bridge->dev;
45513afcdd7SPhilip Chen 	ret = drm_dp_aux_register(&ps_bridge->aux);
45613afcdd7SPhilip Chen 	if (ret) {
45713afcdd7SPhilip Chen 		dev_err(dev, "failed to register DP AUX channel: %d\n", ret);
458fe93ae80SMaxime Ripard 		return ret;
45913afcdd7SPhilip Chen 	}
460bc1aee7fSJitao Shi 
4619294914dSAngeloGioacchino Del Regno 	ps_bridge->link = device_link_add(bridge->dev->dev, dev, DL_FLAG_STATELESS);
4629294914dSAngeloGioacchino Del Regno 	if (!ps_bridge->link) {
4639294914dSAngeloGioacchino Del Regno 		dev_err(dev, "failed to create device link");
4649294914dSAngeloGioacchino Del Regno 		ret = -EINVAL;
4659294914dSAngeloGioacchino Del Regno 		goto err_devlink;
4669294914dSAngeloGioacchino Del Regno 	}
4679294914dSAngeloGioacchino Del Regno 
468bc1aee7fSJitao Shi 	/* Attach the panel-bridge to the dsi bridge */
4699294914dSAngeloGioacchino Del Regno 	ret = drm_bridge_attach(bridge->encoder, ps_bridge->panel_bridge,
470a25b988fSLaurent Pinchart 				&ps_bridge->bridge, flags);
4719294914dSAngeloGioacchino Del Regno 	if (ret)
4729294914dSAngeloGioacchino Del Regno 		goto err_bridge_attach;
4739294914dSAngeloGioacchino Del Regno 
4749294914dSAngeloGioacchino Del Regno 	return 0;
4759294914dSAngeloGioacchino Del Regno 
4769294914dSAngeloGioacchino Del Regno err_bridge_attach:
4779294914dSAngeloGioacchino Del Regno 	device_link_del(ps_bridge->link);
4789294914dSAngeloGioacchino Del Regno err_devlink:
4799294914dSAngeloGioacchino Del Regno 	drm_dp_aux_unregister(&ps_bridge->aux);
4809294914dSAngeloGioacchino Del Regno 
4819294914dSAngeloGioacchino Del Regno 	return ret;
482bc1aee7fSJitao Shi }
483bc1aee7fSJitao Shi 
48413afcdd7SPhilip Chen static void ps8640_bridge_detach(struct drm_bridge *bridge)
48513afcdd7SPhilip Chen {
4869294914dSAngeloGioacchino Del Regno 	struct ps8640 *ps_bridge = bridge_to_ps8640(bridge);
4879294914dSAngeloGioacchino Del Regno 
4889294914dSAngeloGioacchino Del Regno 	drm_dp_aux_unregister(&ps_bridge->aux);
4899294914dSAngeloGioacchino Del Regno 	if (ps_bridge->link)
4909294914dSAngeloGioacchino Del Regno 		device_link_del(ps_bridge->link);
49113afcdd7SPhilip Chen }
49213afcdd7SPhilip Chen 
493d82c12abSEnric Balletbo i Serra static struct edid *ps8640_bridge_get_edid(struct drm_bridge *bridge,
494d82c12abSEnric Balletbo i Serra 					   struct drm_connector *connector)
495d82c12abSEnric Balletbo i Serra {
496d82c12abSEnric Balletbo i Serra 	struct ps8640 *ps_bridge = bridge_to_ps8640(bridge);
497826cff3fSPhilip Chen 	bool poweroff = !ps_bridge->pre_enabled;
49846f20630SEnric Balletbo i Serra 	struct edid *edid;
499d82c12abSEnric Balletbo i Serra 
50046f20630SEnric Balletbo i Serra 	/*
50146f20630SEnric Balletbo i Serra 	 * When we end calling get_edid() triggered by an ioctl, i.e
50246f20630SEnric Balletbo i Serra 	 *
50346f20630SEnric Balletbo i Serra 	 *   drm_mode_getconnector (ioctl)
50446f20630SEnric Balletbo i Serra 	 *     -> drm_helper_probe_single_connector_modes
50546f20630SEnric Balletbo i Serra 	 *        -> drm_bridge_connector_get_modes
50646f20630SEnric Balletbo i Serra 	 *           -> ps8640_bridge_get_edid
50746f20630SEnric Balletbo i Serra 	 *
50846f20630SEnric Balletbo i Serra 	 * We need to make sure that what we need is enabled before reading
50946f20630SEnric Balletbo i Serra 	 * EDID, for this chip, we need to do a full poweron, otherwise it will
51046f20630SEnric Balletbo i Serra 	 * fail.
51146f20630SEnric Balletbo i Serra 	 */
51246f20630SEnric Balletbo i Serra 	drm_bridge_chain_pre_enable(bridge);
51346f20630SEnric Balletbo i Serra 
51446f20630SEnric Balletbo i Serra 	edid = drm_get_edid(connector,
515d82c12abSEnric Balletbo i Serra 			    ps_bridge->page[PAGE0_DP_CNTL]->adapter);
51646f20630SEnric Balletbo i Serra 
51746f20630SEnric Balletbo i Serra 	/*
51846f20630SEnric Balletbo i Serra 	 * If we call the get_edid() function without having enabled the chip
51946f20630SEnric Balletbo i Serra 	 * before, return the chip to its original power state.
52046f20630SEnric Balletbo i Serra 	 */
52146f20630SEnric Balletbo i Serra 	if (poweroff)
52246f20630SEnric Balletbo i Serra 		drm_bridge_chain_post_disable(bridge);
52346f20630SEnric Balletbo i Serra 
52446f20630SEnric Balletbo i Serra 	return edid;
525d82c12abSEnric Balletbo i Serra }
526d82c12abSEnric Balletbo i Serra 
527826cff3fSPhilip Chen static void ps8640_runtime_disable(void *data)
528826cff3fSPhilip Chen {
529826cff3fSPhilip Chen 	pm_runtime_dont_use_autosuspend(data);
530826cff3fSPhilip Chen 	pm_runtime_disable(data);
531826cff3fSPhilip Chen }
532826cff3fSPhilip Chen 
533bc1aee7fSJitao Shi static const struct drm_bridge_funcs ps8640_bridge_funcs = {
534bc1aee7fSJitao Shi 	.attach = ps8640_bridge_attach,
53513afcdd7SPhilip Chen 	.detach = ps8640_bridge_detach,
536d82c12abSEnric Balletbo i Serra 	.get_edid = ps8640_bridge_get_edid,
537bc1aee7fSJitao Shi 	.post_disable = ps8640_post_disable,
538bc1aee7fSJitao Shi 	.pre_enable = ps8640_pre_enable,
539bc1aee7fSJitao Shi };
540bc1aee7fSJitao Shi 
54110e619f1SDouglas Anderson static int ps8640_bridge_get_dsi_resources(struct device *dev, struct ps8640 *ps_bridge)
5427abbc26fSMaxime Ripard {
5437abbc26fSMaxime Ripard 	struct device_node *in_ep, *dsi_node;
5447abbc26fSMaxime Ripard 	struct mipi_dsi_device *dsi;
5457abbc26fSMaxime Ripard 	struct mipi_dsi_host *host;
5467abbc26fSMaxime Ripard 	const struct mipi_dsi_device_info info = { .type = "ps8640",
5477abbc26fSMaxime Ripard 						   .channel = 0,
5487abbc26fSMaxime Ripard 						   .node = NULL,
5497abbc26fSMaxime Ripard 						 };
5507abbc26fSMaxime Ripard 
5517abbc26fSMaxime Ripard 	/* port@0 is ps8640 dsi input port */
5527abbc26fSMaxime Ripard 	in_ep = of_graph_get_endpoint_by_regs(dev->of_node, 0, -1);
5537abbc26fSMaxime Ripard 	if (!in_ep)
5547abbc26fSMaxime Ripard 		return -ENODEV;
5557abbc26fSMaxime Ripard 
5567abbc26fSMaxime Ripard 	dsi_node = of_graph_get_remote_port_parent(in_ep);
5577abbc26fSMaxime Ripard 	of_node_put(in_ep);
5587abbc26fSMaxime Ripard 	if (!dsi_node)
5597abbc26fSMaxime Ripard 		return -ENODEV;
5607abbc26fSMaxime Ripard 
5617abbc26fSMaxime Ripard 	host = of_find_mipi_dsi_host_by_node(dsi_node);
5627abbc26fSMaxime Ripard 	of_node_put(dsi_node);
5637abbc26fSMaxime Ripard 	if (!host)
5647abbc26fSMaxime Ripard 		return -EPROBE_DEFER;
5657abbc26fSMaxime Ripard 
5667abbc26fSMaxime Ripard 	dsi = devm_mipi_dsi_device_register_full(dev, host, &info);
5677abbc26fSMaxime Ripard 	if (IS_ERR(dsi)) {
5687abbc26fSMaxime Ripard 		dev_err(dev, "failed to create dsi device\n");
5697abbc26fSMaxime Ripard 		return PTR_ERR(dsi);
5707abbc26fSMaxime Ripard 	}
5717abbc26fSMaxime Ripard 
5727abbc26fSMaxime Ripard 	ps_bridge->dsi = dsi;
5737abbc26fSMaxime Ripard 
5747abbc26fSMaxime Ripard 	dsi->host = host;
5757abbc26fSMaxime Ripard 	dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
5767abbc26fSMaxime Ripard 			  MIPI_DSI_MODE_VIDEO_SYNC_PULSE;
5777abbc26fSMaxime Ripard 	dsi->format = MIPI_DSI_FMT_RGB888;
5787abbc26fSMaxime Ripard 	dsi->lanes = NUM_MIPI_LANES;
5797abbc26fSMaxime Ripard 
58010e619f1SDouglas Anderson 	return 0;
58110e619f1SDouglas Anderson }
58210e619f1SDouglas Anderson 
58310e619f1SDouglas Anderson static int ps8640_bridge_link_panel(struct drm_dp_aux *aux)
58410e619f1SDouglas Anderson {
58510e619f1SDouglas Anderson 	struct ps8640 *ps_bridge = aux_to_ps8640(aux);
58610e619f1SDouglas Anderson 	struct device *dev = aux->dev;
58710e619f1SDouglas Anderson 	struct device_node *np = dev->of_node;
58810e619f1SDouglas Anderson 	int ret;
58910e619f1SDouglas Anderson 
59010e619f1SDouglas Anderson 	/*
59110e619f1SDouglas Anderson 	 * NOTE about returning -EPROBE_DEFER from this function: if we
59210e619f1SDouglas Anderson 	 * return an error (most relevant to -EPROBE_DEFER) it will only
59310e619f1SDouglas Anderson 	 * be passed out to ps8640_probe() if it called this directly (AKA the
59410e619f1SDouglas Anderson 	 * panel isn't under the "aux-bus" node). That should be fine because
59510e619f1SDouglas Anderson 	 * if the panel is under "aux-bus" it's guaranteed to have probed by
59610e619f1SDouglas Anderson 	 * the time this function has been called.
59710e619f1SDouglas Anderson 	 */
59810e619f1SDouglas Anderson 
59910e619f1SDouglas Anderson 	/* port@1 is ps8640 output port */
60010e619f1SDouglas Anderson 	ps_bridge->panel_bridge = devm_drm_of_get_bridge(dev, np, 1, 0);
60110e619f1SDouglas Anderson 	if (IS_ERR(ps_bridge->panel_bridge))
60210e619f1SDouglas Anderson 		return PTR_ERR(ps_bridge->panel_bridge);
60310e619f1SDouglas Anderson 
60410e619f1SDouglas Anderson 	ret = devm_drm_bridge_add(dev, &ps_bridge->bridge);
60510e619f1SDouglas Anderson 	if (ret)
60610e619f1SDouglas Anderson 		return ret;
60710e619f1SDouglas Anderson 
60810e619f1SDouglas Anderson 	return devm_mipi_dsi_attach(dev, ps_bridge->dsi);
6097abbc26fSMaxime Ripard }
6107abbc26fSMaxime Ripard 
611bc1aee7fSJitao Shi static int ps8640_probe(struct i2c_client *client)
612bc1aee7fSJitao Shi {
613bc1aee7fSJitao Shi 	struct device *dev = &client->dev;
614bc1aee7fSJitao Shi 	struct ps8640 *ps_bridge;
615bc1aee7fSJitao Shi 	int ret;
616bc1aee7fSJitao Shi 	u32 i;
617bc1aee7fSJitao Shi 
618bc1aee7fSJitao Shi 	ps_bridge = devm_kzalloc(dev, sizeof(*ps_bridge), GFP_KERNEL);
619bc1aee7fSJitao Shi 	if (!ps_bridge)
620bc1aee7fSJitao Shi 		return -ENOMEM;
621bc1aee7fSJitao Shi 
622bc1aee7fSJitao Shi 	ps_bridge->supplies[0].supply = "vdd33";
623bc1aee7fSJitao Shi 	ps_bridge->supplies[1].supply = "vdd12";
624bc1aee7fSJitao Shi 	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ps_bridge->supplies),
625bc1aee7fSJitao Shi 				      ps_bridge->supplies);
626bc1aee7fSJitao Shi 	if (ret)
627bc1aee7fSJitao Shi 		return ret;
628bc1aee7fSJitao Shi 
629bc1aee7fSJitao Shi 	ps_bridge->gpio_powerdown = devm_gpiod_get(&client->dev, "powerdown",
630bc1aee7fSJitao Shi 						   GPIOD_OUT_HIGH);
631bc1aee7fSJitao Shi 	if (IS_ERR(ps_bridge->gpio_powerdown))
632bc1aee7fSJitao Shi 		return PTR_ERR(ps_bridge->gpio_powerdown);
633bc1aee7fSJitao Shi 
634bc1aee7fSJitao Shi 	/*
635bc1aee7fSJitao Shi 	 * Assert the reset to avoid the bridge being initialized prematurely
636bc1aee7fSJitao Shi 	 */
637bc1aee7fSJitao Shi 	ps_bridge->gpio_reset = devm_gpiod_get(&client->dev, "reset",
638bc1aee7fSJitao Shi 					       GPIOD_OUT_HIGH);
639bc1aee7fSJitao Shi 	if (IS_ERR(ps_bridge->gpio_reset))
640bc1aee7fSJitao Shi 		return PTR_ERR(ps_bridge->gpio_reset);
641bc1aee7fSJitao Shi 
642bc1aee7fSJitao Shi 	ps_bridge->bridge.funcs = &ps8640_bridge_funcs;
643bc1aee7fSJitao Shi 	ps_bridge->bridge.of_node = dev->of_node;
644d82c12abSEnric Balletbo i Serra 	ps_bridge->bridge.type = DRM_MODE_CONNECTOR_eDP;
645bc1aee7fSJitao Shi 
646e9d9f958SPhilip Chen 	/*
647e9d9f958SPhilip Chen 	 * In the device tree, if panel is listed under aux-bus of the bridge
648e9d9f958SPhilip Chen 	 * node, panel driver should be able to retrieve EDID by itself using
649e9d9f958SPhilip Chen 	 * aux-bus. So let's not set DRM_BRIDGE_OP_EDID here.
650e9d9f958SPhilip Chen 	 */
651e9d9f958SPhilip Chen 	if (!ps8640_of_panel_on_aux_bus(&client->dev))
652e9d9f958SPhilip Chen 		ps_bridge->bridge.ops = DRM_BRIDGE_OP_EDID;
653e9d9f958SPhilip Chen 
65410e619f1SDouglas Anderson 	/*
65510e619f1SDouglas Anderson 	 * Get MIPI DSI resources early. These can return -EPROBE_DEFER so
65610e619f1SDouglas Anderson 	 * we want to get them out of the way sooner.
65710e619f1SDouglas Anderson 	 */
65810e619f1SDouglas Anderson 	ret = ps8640_bridge_get_dsi_resources(&client->dev, ps_bridge);
65910e619f1SDouglas Anderson 	if (ret)
66010e619f1SDouglas Anderson 		return ret;
66110e619f1SDouglas Anderson 
662bc1aee7fSJitao Shi 	ps_bridge->page[PAGE0_DP_CNTL] = client;
663bc1aee7fSJitao Shi 
664692d8db0SPhilip Chen 	ps_bridge->regmap[PAGE0_DP_CNTL] = devm_regmap_init_i2c(client, ps8640_regmap_config);
665692d8db0SPhilip Chen 	if (IS_ERR(ps_bridge->regmap[PAGE0_DP_CNTL]))
666692d8db0SPhilip Chen 		return PTR_ERR(ps_bridge->regmap[PAGE0_DP_CNTL]);
667692d8db0SPhilip Chen 
668bc1aee7fSJitao Shi 	for (i = 1; i < ARRAY_SIZE(ps_bridge->page); i++) {
669bc1aee7fSJitao Shi 		ps_bridge->page[i] = devm_i2c_new_dummy_device(&client->dev,
670bc1aee7fSJitao Shi 							     client->adapter,
671bc1aee7fSJitao Shi 							     client->addr + i);
672692d8db0SPhilip Chen 		if (IS_ERR(ps_bridge->page[i]))
673bc1aee7fSJitao Shi 			return PTR_ERR(ps_bridge->page[i]);
674692d8db0SPhilip Chen 
675692d8db0SPhilip Chen 		ps_bridge->regmap[i] = devm_regmap_init_i2c(ps_bridge->page[i],
676692d8db0SPhilip Chen 							    ps8640_regmap_config + i);
677692d8db0SPhilip Chen 		if (IS_ERR(ps_bridge->regmap[i]))
678692d8db0SPhilip Chen 			return PTR_ERR(ps_bridge->regmap[i]);
679bc1aee7fSJitao Shi 	}
680bc1aee7fSJitao Shi 
681bc1aee7fSJitao Shi 	i2c_set_clientdata(client, ps_bridge);
682bc1aee7fSJitao Shi 
68313afcdd7SPhilip Chen 	ps_bridge->aux.name = "parade-ps8640-aux";
68413afcdd7SPhilip Chen 	ps_bridge->aux.dev = dev;
68513afcdd7SPhilip Chen 	ps_bridge->aux.transfer = ps8640_aux_transfer;
68613afcdd7SPhilip Chen 	drm_dp_aux_init(&ps_bridge->aux);
68713afcdd7SPhilip Chen 
688826cff3fSPhilip Chen 	pm_runtime_enable(dev);
689826cff3fSPhilip Chen 	/*
690826cff3fSPhilip Chen 	 * Powering on ps8640 takes ~300ms. To avoid wasting time on power
691aa70a099Syangcong 	 * cycling ps8640 too often, set autosuspend_delay to 1000ms to ensure
692826cff3fSPhilip Chen 	 * the bridge wouldn't suspend in between each _aux_transfer_msg() call
693826cff3fSPhilip Chen 	 * during EDID read (~20ms in my experiment) and in between the last
694826cff3fSPhilip Chen 	 * _aux_transfer_msg() call during EDID read and the _pre_enable() call
695826cff3fSPhilip Chen 	 * (~100ms in my experiment).
696826cff3fSPhilip Chen 	 */
697aa70a099Syangcong 	pm_runtime_set_autosuspend_delay(dev, 1000);
698826cff3fSPhilip Chen 	pm_runtime_use_autosuspend(dev);
699826cff3fSPhilip Chen 	pm_suspend_ignore_children(dev, true);
700826cff3fSPhilip Chen 	ret = devm_add_action_or_reset(dev, ps8640_runtime_disable, dev);
701826cff3fSPhilip Chen 	if (ret)
702826cff3fSPhilip Chen 		return ret;
703826cff3fSPhilip Chen 
70410e619f1SDouglas Anderson 	ret = devm_of_dp_aux_populate_bus(&ps_bridge->aux, ps8640_bridge_link_panel);
705e9d9f958SPhilip Chen 
70610e619f1SDouglas Anderson 	/*
70710e619f1SDouglas Anderson 	 * If devm_of_dp_aux_populate_bus() returns -ENODEV then it's up to
70810e619f1SDouglas Anderson 	 * usa to call ps8640_bridge_link_panel() directly. NOTE: in this case
70910e619f1SDouglas Anderson 	 * the function is allowed to -EPROBE_DEFER.
71010e619f1SDouglas Anderson 	 */
71110e619f1SDouglas Anderson 	if (ret == -ENODEV)
71210e619f1SDouglas Anderson 		return ps8640_bridge_link_panel(&ps_bridge->aux);
713e9d9f958SPhilip Chen 
7147abbc26fSMaxime Ripard 	return ret;
715bc1aee7fSJitao Shi }
716bc1aee7fSJitao Shi 
717bc1aee7fSJitao Shi static const struct of_device_id ps8640_match[] = {
718bc1aee7fSJitao Shi 	{ .compatible = "parade,ps8640" },
719bc1aee7fSJitao Shi 	{ }
720bc1aee7fSJitao Shi };
721bc1aee7fSJitao Shi MODULE_DEVICE_TABLE(of, ps8640_match);
722bc1aee7fSJitao Shi 
723bc1aee7fSJitao Shi static struct i2c_driver ps8640_driver = {
724bc1aee7fSJitao Shi 	.probe_new = ps8640_probe,
725bc1aee7fSJitao Shi 	.driver = {
726bc1aee7fSJitao Shi 		.name = "ps8640",
727bc1aee7fSJitao Shi 		.of_match_table = ps8640_match,
728826cff3fSPhilip Chen 		.pm = &ps8640_pm_ops,
729bc1aee7fSJitao Shi 	},
730bc1aee7fSJitao Shi };
731bc1aee7fSJitao Shi module_i2c_driver(ps8640_driver);
732bc1aee7fSJitao Shi 
733bc1aee7fSJitao Shi MODULE_AUTHOR("Jitao Shi <jitao.shi@mediatek.com>");
734bc1aee7fSJitao Shi MODULE_AUTHOR("CK Hu <ck.hu@mediatek.com>");
735bc1aee7fSJitao Shi MODULE_AUTHOR("Enric Balletbo i Serra <enric.balletbo@collabora.com>");
736bc1aee7fSJitao Shi MODULE_DESCRIPTION("PARADE ps8640 DSI-eDP converter driver");
737bc1aee7fSJitao Shi MODULE_LICENSE("GPL v2");
738