1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2016 MediaTek Inc.
4  */
5 
6 #include <linux/delay.h>
7 #include <linux/err.h>
8 #include <linux/gpio/consumer.h>
9 #include <linux/i2c.h>
10 #include <linux/module.h>
11 #include <linux/of_graph.h>
12 #include <linux/regulator/consumer.h>
13 
14 #include <drm/drm_bridge.h>
15 #include <drm/drm_mipi_dsi.h>
16 #include <drm/drm_of.h>
17 #include <drm/drm_panel.h>
18 #include <drm/drm_print.h>
19 
20 #define PAGE2_GPIO_H		0xa7
21 #define  PS_GPIO9		BIT(1)
22 #define PAGE2_I2C_BYPASS	0xea
23 #define  I2C_BYPASS_EN		0xd0
24 #define PAGE2_MCS_EN		0xf3
25 #define  MCS_EN			BIT(0)
26 
27 #define PAGE3_SET_ADD		0xfe
28 #define  VDO_CTL_ADD		0x13
29 #define  VDO_DIS		0x18
30 #define  VDO_EN			0x1c
31 
32 #define NUM_MIPI_LANES		4
33 
34 /*
35  * PS8640 uses multiple addresses:
36  * page[0]: for DP control
37  * page[1]: for VIDEO Bridge
38  * page[2]: for control top
39  * page[3]: for DSI Link Control1
40  * page[4]: for MIPI Phy
41  * page[5]: for VPLL
42  * page[6]: for DSI Link Control2
43  * page[7]: for SPI ROM mapping
44  */
45 enum page_addr_offset {
46 	PAGE0_DP_CNTL = 0,
47 	PAGE1_VDO_BDG,
48 	PAGE2_TOP_CNTL,
49 	PAGE3_DSI_CNTL1,
50 	PAGE4_MIPI_PHY,
51 	PAGE5_VPLL,
52 	PAGE6_DSI_CNTL2,
53 	PAGE7_SPI_CNTL,
54 	MAX_DEVS
55 };
56 
57 enum ps8640_vdo_control {
58 	DISABLE = VDO_DIS,
59 	ENABLE = VDO_EN,
60 };
61 
62 struct ps8640 {
63 	struct drm_bridge bridge;
64 	struct drm_bridge *panel_bridge;
65 	struct mipi_dsi_device *dsi;
66 	struct i2c_client *page[MAX_DEVS];
67 	struct regulator_bulk_data supplies[2];
68 	struct gpio_desc *gpio_reset;
69 	struct gpio_desc *gpio_powerdown;
70 	bool powered;
71 };
72 
73 static inline struct ps8640 *bridge_to_ps8640(struct drm_bridge *e)
74 {
75 	return container_of(e, struct ps8640, bridge);
76 }
77 
78 static int ps8640_bridge_vdo_control(struct ps8640 *ps_bridge,
79 				     const enum ps8640_vdo_control ctrl)
80 {
81 	struct i2c_client *client = ps_bridge->page[PAGE3_DSI_CNTL1];
82 	u8 vdo_ctrl_buf[] = { VDO_CTL_ADD, ctrl };
83 	int ret;
84 
85 	ret = i2c_smbus_write_i2c_block_data(client, PAGE3_SET_ADD,
86 					     sizeof(vdo_ctrl_buf),
87 					     vdo_ctrl_buf);
88 	if (ret < 0) {
89 		DRM_ERROR("failed to %sable VDO: %d\n",
90 			  ctrl == ENABLE ? "en" : "dis", ret);
91 		return ret;
92 	}
93 
94 	return 0;
95 }
96 
97 static void ps8640_bridge_poweron(struct ps8640 *ps_bridge)
98 {
99 	struct i2c_client *client = ps_bridge->page[PAGE2_TOP_CNTL];
100 	unsigned long timeout;
101 	int ret, status;
102 
103 	if (ps_bridge->powered)
104 		return;
105 
106 	ret = regulator_bulk_enable(ARRAY_SIZE(ps_bridge->supplies),
107 				    ps_bridge->supplies);
108 	if (ret < 0) {
109 		DRM_ERROR("cannot enable regulators %d\n", ret);
110 		return;
111 	}
112 
113 	gpiod_set_value(ps_bridge->gpio_powerdown, 0);
114 	gpiod_set_value(ps_bridge->gpio_reset, 1);
115 	usleep_range(2000, 2500);
116 	gpiod_set_value(ps_bridge->gpio_reset, 0);
117 
118 	/*
119 	 * Wait for the ps8640 embedded MCU to be ready
120 	 * First wait 200ms and then check the MCU ready flag every 20ms
121 	 */
122 	msleep(200);
123 
124 	timeout = jiffies + msecs_to_jiffies(200) + 1;
125 
126 	while (time_is_after_jiffies(timeout)) {
127 		status = i2c_smbus_read_byte_data(client, PAGE2_GPIO_H);
128 		if (status < 0) {
129 			DRM_ERROR("failed read PAGE2_GPIO_H: %d\n", status);
130 			goto err_regulators_disable;
131 		}
132 		if ((status & PS_GPIO9) == PS_GPIO9)
133 			break;
134 
135 		msleep(20);
136 	}
137 
138 	msleep(50);
139 
140 	/*
141 	 * The Manufacturer Command Set (MCS) is a device dependent interface
142 	 * intended for factory programming of the display module default
143 	 * parameters. Once the display module is configured, the MCS shall be
144 	 * disabled by the manufacturer. Once disabled, all MCS commands are
145 	 * ignored by the display interface.
146 	 */
147 	status = i2c_smbus_read_byte_data(client, PAGE2_MCS_EN);
148 	if (status < 0) {
149 		DRM_ERROR("failed read PAGE2_MCS_EN: %d\n", status);
150 		goto err_regulators_disable;
151 	}
152 
153 	ret = i2c_smbus_write_byte_data(client, PAGE2_MCS_EN,
154 					status & ~MCS_EN);
155 	if (ret < 0) {
156 		DRM_ERROR("failed write PAGE2_MCS_EN: %d\n", ret);
157 		goto err_regulators_disable;
158 	}
159 
160 	/* Switch access edp panel's edid through i2c */
161 	ret = i2c_smbus_write_byte_data(client, PAGE2_I2C_BYPASS,
162 					I2C_BYPASS_EN);
163 	if (ret < 0) {
164 		DRM_ERROR("failed write PAGE2_I2C_BYPASS: %d\n", ret);
165 		goto err_regulators_disable;
166 	}
167 
168 	ps_bridge->powered = true;
169 
170 	return;
171 
172 err_regulators_disable:
173 	regulator_bulk_disable(ARRAY_SIZE(ps_bridge->supplies),
174 			       ps_bridge->supplies);
175 }
176 
177 static void ps8640_bridge_poweroff(struct ps8640 *ps_bridge)
178 {
179 	int ret;
180 
181 	if (!ps_bridge->powered)
182 		return;
183 
184 	gpiod_set_value(ps_bridge->gpio_reset, 1);
185 	gpiod_set_value(ps_bridge->gpio_powerdown, 1);
186 	ret = regulator_bulk_disable(ARRAY_SIZE(ps_bridge->supplies),
187 				     ps_bridge->supplies);
188 	if (ret < 0)
189 		DRM_ERROR("cannot disable regulators %d\n", ret);
190 
191 	ps_bridge->powered = false;
192 }
193 
194 static void ps8640_pre_enable(struct drm_bridge *bridge)
195 {
196 	struct ps8640 *ps_bridge = bridge_to_ps8640(bridge);
197 	int ret;
198 
199 	ps8640_bridge_poweron(ps_bridge);
200 
201 	ret = ps8640_bridge_vdo_control(ps_bridge, ENABLE);
202 	if (ret < 0)
203 		ps8640_bridge_poweroff(ps_bridge);
204 }
205 
206 static void ps8640_post_disable(struct drm_bridge *bridge)
207 {
208 	struct ps8640 *ps_bridge = bridge_to_ps8640(bridge);
209 
210 	ps8640_bridge_vdo_control(ps_bridge, DISABLE);
211 	ps8640_bridge_poweroff(ps_bridge);
212 }
213 
214 static int ps8640_bridge_attach(struct drm_bridge *bridge,
215 				enum drm_bridge_attach_flags flags)
216 {
217 	struct ps8640 *ps_bridge = bridge_to_ps8640(bridge);
218 	struct device *dev = &ps_bridge->page[0]->dev;
219 	struct device_node *in_ep, *dsi_node;
220 	struct mipi_dsi_device *dsi;
221 	struct mipi_dsi_host *host;
222 	int ret;
223 	const struct mipi_dsi_device_info info = { .type = "ps8640",
224 						   .channel = 0,
225 						   .node = NULL,
226 						 };
227 
228 	if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR))
229 		return -EINVAL;
230 
231 	/* port@0 is ps8640 dsi input port */
232 	in_ep = of_graph_get_endpoint_by_regs(dev->of_node, 0, -1);
233 	if (!in_ep)
234 		return -ENODEV;
235 
236 	dsi_node = of_graph_get_remote_port_parent(in_ep);
237 	of_node_put(in_ep);
238 	if (!dsi_node)
239 		return -ENODEV;
240 
241 	host = of_find_mipi_dsi_host_by_node(dsi_node);
242 	of_node_put(dsi_node);
243 	if (!host)
244 		return -ENODEV;
245 
246 	dsi = mipi_dsi_device_register_full(host, &info);
247 	if (IS_ERR(dsi)) {
248 		dev_err(dev, "failed to create dsi device\n");
249 		ret = PTR_ERR(dsi);
250 		return ret;
251 	}
252 
253 	ps_bridge->dsi = dsi;
254 
255 	dsi->host = host;
256 	dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
257 			  MIPI_DSI_MODE_VIDEO_SYNC_PULSE;
258 	dsi->format = MIPI_DSI_FMT_RGB888;
259 	dsi->lanes = NUM_MIPI_LANES;
260 	ret = mipi_dsi_attach(dsi);
261 	if (ret)
262 		goto err_dsi_attach;
263 
264 	/* Attach the panel-bridge to the dsi bridge */
265 	return drm_bridge_attach(bridge->encoder, ps_bridge->panel_bridge,
266 				 &ps_bridge->bridge, flags);
267 
268 err_dsi_attach:
269 	mipi_dsi_device_unregister(dsi);
270 	return ret;
271 }
272 
273 static struct edid *ps8640_bridge_get_edid(struct drm_bridge *bridge,
274 					   struct drm_connector *connector)
275 {
276 	struct ps8640 *ps_bridge = bridge_to_ps8640(bridge);
277 	bool poweroff = !ps_bridge->powered;
278 	struct edid *edid;
279 
280 	/*
281 	 * When we end calling get_edid() triggered by an ioctl, i.e
282 	 *
283 	 *   drm_mode_getconnector (ioctl)
284 	 *     -> drm_helper_probe_single_connector_modes
285 	 *        -> drm_bridge_connector_get_modes
286 	 *           -> ps8640_bridge_get_edid
287 	 *
288 	 * We need to make sure that what we need is enabled before reading
289 	 * EDID, for this chip, we need to do a full poweron, otherwise it will
290 	 * fail.
291 	 */
292 	drm_bridge_chain_pre_enable(bridge);
293 
294 	edid = drm_get_edid(connector,
295 			    ps_bridge->page[PAGE0_DP_CNTL]->adapter);
296 
297 	/*
298 	 * If we call the get_edid() function without having enabled the chip
299 	 * before, return the chip to its original power state.
300 	 */
301 	if (poweroff)
302 		drm_bridge_chain_post_disable(bridge);
303 
304 	return edid;
305 }
306 
307 static const struct drm_bridge_funcs ps8640_bridge_funcs = {
308 	.attach = ps8640_bridge_attach,
309 	.get_edid = ps8640_bridge_get_edid,
310 	.post_disable = ps8640_post_disable,
311 	.pre_enable = ps8640_pre_enable,
312 };
313 
314 static int ps8640_probe(struct i2c_client *client)
315 {
316 	struct device *dev = &client->dev;
317 	struct device_node *np = dev->of_node;
318 	struct ps8640 *ps_bridge;
319 	struct drm_panel *panel;
320 	int ret;
321 	u32 i;
322 
323 	ps_bridge = devm_kzalloc(dev, sizeof(*ps_bridge), GFP_KERNEL);
324 	if (!ps_bridge)
325 		return -ENOMEM;
326 
327 	/* port@1 is ps8640 output port */
328 	ret = drm_of_find_panel_or_bridge(np, 1, 0, &panel, NULL);
329 	if (ret < 0)
330 		return ret;
331 	if (!panel)
332 		return -ENODEV;
333 
334 	ps_bridge->panel_bridge = devm_drm_panel_bridge_add(dev, panel);
335 	if (IS_ERR(ps_bridge->panel_bridge))
336 		return PTR_ERR(ps_bridge->panel_bridge);
337 
338 	ps_bridge->supplies[0].supply = "vdd33";
339 	ps_bridge->supplies[1].supply = "vdd12";
340 	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ps_bridge->supplies),
341 				      ps_bridge->supplies);
342 	if (ret)
343 		return ret;
344 
345 	ps_bridge->gpio_powerdown = devm_gpiod_get(&client->dev, "powerdown",
346 						   GPIOD_OUT_HIGH);
347 	if (IS_ERR(ps_bridge->gpio_powerdown))
348 		return PTR_ERR(ps_bridge->gpio_powerdown);
349 
350 	/*
351 	 * Assert the reset to avoid the bridge being initialized prematurely
352 	 */
353 	ps_bridge->gpio_reset = devm_gpiod_get(&client->dev, "reset",
354 					       GPIOD_OUT_HIGH);
355 	if (IS_ERR(ps_bridge->gpio_reset))
356 		return PTR_ERR(ps_bridge->gpio_reset);
357 
358 	ps_bridge->bridge.funcs = &ps8640_bridge_funcs;
359 	ps_bridge->bridge.of_node = dev->of_node;
360 	ps_bridge->bridge.ops = DRM_BRIDGE_OP_EDID;
361 	ps_bridge->bridge.type = DRM_MODE_CONNECTOR_eDP;
362 
363 	ps_bridge->page[PAGE0_DP_CNTL] = client;
364 
365 	for (i = 1; i < ARRAY_SIZE(ps_bridge->page); i++) {
366 		ps_bridge->page[i] = devm_i2c_new_dummy_device(&client->dev,
367 							     client->adapter,
368 							     client->addr + i);
369 		if (IS_ERR(ps_bridge->page[i])) {
370 			dev_err(dev, "failed i2c dummy device, address %02x\n",
371 				client->addr + i);
372 			return PTR_ERR(ps_bridge->page[i]);
373 		}
374 	}
375 
376 	i2c_set_clientdata(client, ps_bridge);
377 
378 	drm_bridge_add(&ps_bridge->bridge);
379 
380 	return 0;
381 }
382 
383 static int ps8640_remove(struct i2c_client *client)
384 {
385 	struct ps8640 *ps_bridge = i2c_get_clientdata(client);
386 
387 	drm_bridge_remove(&ps_bridge->bridge);
388 
389 	return 0;
390 }
391 
392 static const struct of_device_id ps8640_match[] = {
393 	{ .compatible = "parade,ps8640" },
394 	{ }
395 };
396 MODULE_DEVICE_TABLE(of, ps8640_match);
397 
398 static struct i2c_driver ps8640_driver = {
399 	.probe_new = ps8640_probe,
400 	.remove = ps8640_remove,
401 	.driver = {
402 		.name = "ps8640",
403 		.of_match_table = ps8640_match,
404 	},
405 };
406 module_i2c_driver(ps8640_driver);
407 
408 MODULE_AUTHOR("Jitao Shi <jitao.shi@mediatek.com>");
409 MODULE_AUTHOR("CK Hu <ck.hu@mediatek.com>");
410 MODULE_AUTHOR("Enric Balletbo i Serra <enric.balletbo@collabora.com>");
411 MODULE_DESCRIPTION("PARADE ps8640 DSI-eDP converter driver");
412 MODULE_LICENSE("GPL v2");
413