xref: /linux/drivers/gpu/drm/bridge/megachips-stdpxxxx-ge-b850v3-fw.c (revision fd7d598270724cc787982ea48bbe17ad383a8b7f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Driver for MegaChips STDP4028 with GE B850v3 firmware (LVDS-DP)
4  * Driver for MegaChips STDP2690 with GE B850v3 firmware (DP-DP++)
5 
6  * Copyright (c) 2017, Collabora Ltd.
7  * Copyright (c) 2017, General Electric Company
8 
9 
10  * This driver creates a drm_bridge and a drm_connector for the LVDS to DP++
11  * display bridge of the GE B850v3. There are two physical bridges on the video
12  * signal pipeline: a STDP4028(LVDS to DP) and a STDP2690(DP to DP++). The
13  * physical bridges are automatically configured by the input video signal, and
14  * the driver has no access to the video processing pipeline. The driver is
15  * only needed to read EDID from the STDP2690 and to handle HPD events from the
16  * STDP4028. The driver communicates with both bridges over i2c. The video
17  * signal pipeline is as follows:
18  *
19  *   Host -> LVDS|--(STDP4028)--|DP -> DP|--(STDP2690)--|DP++ -> Video output
20  */
21 
22 #include <linux/i2c.h>
23 #include <linux/module.h>
24 #include <linux/of.h>
25 
26 #include <drm/drm_atomic.h>
27 #include <drm/drm_atomic_helper.h>
28 #include <drm/drm_bridge.h>
29 #include <drm/drm_edid.h>
30 #include <drm/drm_print.h>
31 #include <drm/drm_probe_helper.h>
32 
33 #define EDID_EXT_BLOCK_CNT 0x7E
34 
35 #define STDP4028_IRQ_OUT_CONF_REG 0x02
36 #define STDP4028_DPTX_IRQ_EN_REG 0x3C
37 #define STDP4028_DPTX_IRQ_STS_REG 0x3D
38 #define STDP4028_DPTX_STS_REG 0x3E
39 
40 #define STDP4028_DPTX_DP_IRQ_EN 0x1000
41 
42 #define STDP4028_DPTX_HOTPLUG_IRQ_EN 0x0400
43 #define STDP4028_DPTX_LINK_CH_IRQ_EN 0x2000
44 #define STDP4028_DPTX_IRQ_CONFIG \
45 		(STDP4028_DPTX_LINK_CH_IRQ_EN | STDP4028_DPTX_HOTPLUG_IRQ_EN)
46 
47 #define STDP4028_DPTX_HOTPLUG_STS 0x0200
48 #define STDP4028_DPTX_LINK_STS 0x1000
49 #define STDP4028_CON_STATE_CONNECTED \
50 		(STDP4028_DPTX_HOTPLUG_STS | STDP4028_DPTX_LINK_STS)
51 
52 #define STDP4028_DPTX_HOTPLUG_CH_STS 0x0400
53 #define STDP4028_DPTX_LINK_CH_STS 0x2000
54 #define STDP4028_DPTX_IRQ_CLEAR \
55 		(STDP4028_DPTX_LINK_CH_STS | STDP4028_DPTX_HOTPLUG_CH_STS)
56 
57 static DEFINE_MUTEX(ge_b850v3_lvds_dev_mutex);
58 
59 struct ge_b850v3_lvds {
60 	struct drm_connector connector;
61 	struct drm_bridge bridge;
62 	struct i2c_client *stdp4028_i2c;
63 	struct i2c_client *stdp2690_i2c;
64 };
65 
66 static struct ge_b850v3_lvds *ge_b850v3_lvds_ptr;
67 
68 static int stdp2690_read_block(void *context, u8 *buf, unsigned int block, size_t len)
69 {
70 	struct i2c_client *client = context;
71 	struct i2c_adapter *adapter = client->adapter;
72 	unsigned char start = block * EDID_LENGTH;
73 
74 	struct i2c_msg msgs[] = {
75 		{
76 			.addr	= client->addr,
77 			.flags	= 0,
78 			.len	= 1,
79 			.buf	= &start,
80 		}, {
81 			.addr	= client->addr,
82 			.flags	= I2C_M_RD,
83 			.len	= len,
84 			.buf	= buf,
85 		}
86 	};
87 
88 	if (i2c_transfer(adapter, msgs, 2) != 2)
89 		return -1;
90 
91 	return 0;
92 }
93 
94 static struct edid *ge_b850v3_lvds_get_edid(struct drm_bridge *bridge,
95 					    struct drm_connector *connector)
96 {
97 	struct i2c_client *client;
98 
99 	client = ge_b850v3_lvds_ptr->stdp2690_i2c;
100 
101 	return drm_do_get_edid(connector, stdp2690_read_block, client);
102 }
103 
104 static int ge_b850v3_lvds_get_modes(struct drm_connector *connector)
105 {
106 	struct edid *edid;
107 	int num_modes;
108 
109 	edid = ge_b850v3_lvds_get_edid(&ge_b850v3_lvds_ptr->bridge, connector);
110 
111 	drm_connector_update_edid_property(connector, edid);
112 	num_modes = drm_add_edid_modes(connector, edid);
113 	kfree(edid);
114 
115 	return num_modes;
116 }
117 
118 static enum drm_mode_status ge_b850v3_lvds_mode_valid(
119 		struct drm_connector *connector, struct drm_display_mode *mode)
120 {
121 	return MODE_OK;
122 }
123 
124 static const struct
125 drm_connector_helper_funcs ge_b850v3_lvds_connector_helper_funcs = {
126 	.get_modes = ge_b850v3_lvds_get_modes,
127 	.mode_valid = ge_b850v3_lvds_mode_valid,
128 };
129 
130 static enum drm_connector_status ge_b850v3_lvds_bridge_detect(struct drm_bridge *bridge)
131 {
132 	struct i2c_client *stdp4028_i2c =
133 			ge_b850v3_lvds_ptr->stdp4028_i2c;
134 	s32 link_state;
135 
136 	link_state = i2c_smbus_read_word_data(stdp4028_i2c,
137 					      STDP4028_DPTX_STS_REG);
138 
139 	if (link_state == STDP4028_CON_STATE_CONNECTED)
140 		return connector_status_connected;
141 
142 	if (link_state == 0)
143 		return connector_status_disconnected;
144 
145 	return connector_status_unknown;
146 }
147 
148 static enum drm_connector_status ge_b850v3_lvds_detect(struct drm_connector *connector,
149 						       bool force)
150 {
151 	return ge_b850v3_lvds_bridge_detect(&ge_b850v3_lvds_ptr->bridge);
152 }
153 
154 static const struct drm_connector_funcs ge_b850v3_lvds_connector_funcs = {
155 	.fill_modes = drm_helper_probe_single_connector_modes,
156 	.detect = ge_b850v3_lvds_detect,
157 	.destroy = drm_connector_cleanup,
158 	.reset = drm_atomic_helper_connector_reset,
159 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
160 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
161 };
162 
163 static int ge_b850v3_lvds_create_connector(struct drm_bridge *bridge)
164 {
165 	struct drm_connector *connector = &ge_b850v3_lvds_ptr->connector;
166 	int ret;
167 
168 	if (!bridge->encoder) {
169 		DRM_ERROR("Parent encoder object not found");
170 		return -ENODEV;
171 	}
172 
173 	connector->polled = DRM_CONNECTOR_POLL_HPD;
174 
175 	drm_connector_helper_add(connector,
176 				 &ge_b850v3_lvds_connector_helper_funcs);
177 
178 	ret = drm_connector_init(bridge->dev, connector,
179 				 &ge_b850v3_lvds_connector_funcs,
180 				 DRM_MODE_CONNECTOR_DisplayPort);
181 	if (ret) {
182 		DRM_ERROR("Failed to initialize connector with drm\n");
183 		return ret;
184 	}
185 
186 	return drm_connector_attach_encoder(connector, bridge->encoder);
187 }
188 
189 static irqreturn_t ge_b850v3_lvds_irq_handler(int irq, void *dev_id)
190 {
191 	struct i2c_client *stdp4028_i2c
192 			= ge_b850v3_lvds_ptr->stdp4028_i2c;
193 
194 	i2c_smbus_write_word_data(stdp4028_i2c,
195 				  STDP4028_DPTX_IRQ_STS_REG,
196 				  STDP4028_DPTX_IRQ_CLEAR);
197 
198 	if (ge_b850v3_lvds_ptr->bridge.dev)
199 		drm_kms_helper_hotplug_event(ge_b850v3_lvds_ptr->bridge.dev);
200 
201 	return IRQ_HANDLED;
202 }
203 
204 static int ge_b850v3_lvds_attach(struct drm_bridge *bridge,
205 				 enum drm_bridge_attach_flags flags)
206 {
207 	struct i2c_client *stdp4028_i2c
208 			= ge_b850v3_lvds_ptr->stdp4028_i2c;
209 
210 	/* Configures the bridge to re-enable interrupts after each ack. */
211 	i2c_smbus_write_word_data(stdp4028_i2c,
212 				  STDP4028_IRQ_OUT_CONF_REG,
213 				  STDP4028_DPTX_DP_IRQ_EN);
214 
215 	/* Enable interrupts */
216 	i2c_smbus_write_word_data(stdp4028_i2c,
217 				  STDP4028_DPTX_IRQ_EN_REG,
218 				  STDP4028_DPTX_IRQ_CONFIG);
219 
220 	if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
221 		return 0;
222 
223 	return ge_b850v3_lvds_create_connector(bridge);
224 }
225 
226 static const struct drm_bridge_funcs ge_b850v3_lvds_funcs = {
227 	.attach = ge_b850v3_lvds_attach,
228 	.detect = ge_b850v3_lvds_bridge_detect,
229 	.get_edid = ge_b850v3_lvds_get_edid,
230 };
231 
232 static int ge_b850v3_lvds_init(struct device *dev)
233 {
234 	mutex_lock(&ge_b850v3_lvds_dev_mutex);
235 
236 	if (ge_b850v3_lvds_ptr)
237 		goto success;
238 
239 	ge_b850v3_lvds_ptr = devm_kzalloc(dev,
240 					  sizeof(*ge_b850v3_lvds_ptr),
241 					  GFP_KERNEL);
242 
243 	if (!ge_b850v3_lvds_ptr) {
244 		mutex_unlock(&ge_b850v3_lvds_dev_mutex);
245 		return -ENOMEM;
246 	}
247 
248 success:
249 	mutex_unlock(&ge_b850v3_lvds_dev_mutex);
250 	return 0;
251 }
252 
253 static void ge_b850v3_lvds_remove(void)
254 {
255 	mutex_lock(&ge_b850v3_lvds_dev_mutex);
256 	/*
257 	 * This check is to avoid both the drivers
258 	 * removing the bridge in their remove() function
259 	 */
260 	if (!ge_b850v3_lvds_ptr ||
261 	    !ge_b850v3_lvds_ptr->stdp2690_i2c ||
262 		!ge_b850v3_lvds_ptr->stdp4028_i2c)
263 		goto out;
264 
265 	drm_bridge_remove(&ge_b850v3_lvds_ptr->bridge);
266 
267 	ge_b850v3_lvds_ptr = NULL;
268 out:
269 	mutex_unlock(&ge_b850v3_lvds_dev_mutex);
270 }
271 
272 static int ge_b850v3_register(void)
273 {
274 	struct i2c_client *stdp4028_i2c = ge_b850v3_lvds_ptr->stdp4028_i2c;
275 	struct device *dev = &stdp4028_i2c->dev;
276 
277 	/* drm bridge initialization */
278 	ge_b850v3_lvds_ptr->bridge.funcs = &ge_b850v3_lvds_funcs;
279 	ge_b850v3_lvds_ptr->bridge.ops = DRM_BRIDGE_OP_DETECT |
280 					 DRM_BRIDGE_OP_EDID;
281 	ge_b850v3_lvds_ptr->bridge.type = DRM_MODE_CONNECTOR_DisplayPort;
282 	ge_b850v3_lvds_ptr->bridge.of_node = dev->of_node;
283 	drm_bridge_add(&ge_b850v3_lvds_ptr->bridge);
284 
285 	/* Clear pending interrupts since power up. */
286 	i2c_smbus_write_word_data(stdp4028_i2c,
287 				  STDP4028_DPTX_IRQ_STS_REG,
288 				  STDP4028_DPTX_IRQ_CLEAR);
289 
290 	if (!stdp4028_i2c->irq)
291 		return 0;
292 
293 	return devm_request_threaded_irq(&stdp4028_i2c->dev,
294 			stdp4028_i2c->irq, NULL,
295 			ge_b850v3_lvds_irq_handler,
296 			IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
297 			"ge-b850v3-lvds-dp", ge_b850v3_lvds_ptr);
298 }
299 
300 static int stdp4028_ge_b850v3_fw_probe(struct i2c_client *stdp4028_i2c)
301 {
302 	struct device *dev = &stdp4028_i2c->dev;
303 	int ret;
304 
305 	ret = ge_b850v3_lvds_init(dev);
306 
307 	if (ret)
308 		return ret;
309 
310 	ge_b850v3_lvds_ptr->stdp4028_i2c = stdp4028_i2c;
311 	i2c_set_clientdata(stdp4028_i2c, ge_b850v3_lvds_ptr);
312 
313 	/* Only register after both bridges are probed */
314 	if (!ge_b850v3_lvds_ptr->stdp2690_i2c)
315 		return 0;
316 
317 	return ge_b850v3_register();
318 }
319 
320 static void stdp4028_ge_b850v3_fw_remove(struct i2c_client *stdp4028_i2c)
321 {
322 	ge_b850v3_lvds_remove();
323 }
324 
325 static const struct i2c_device_id stdp4028_ge_b850v3_fw_i2c_table[] = {
326 	{"stdp4028_ge_fw", 0},
327 	{},
328 };
329 MODULE_DEVICE_TABLE(i2c, stdp4028_ge_b850v3_fw_i2c_table);
330 
331 static const struct of_device_id stdp4028_ge_b850v3_fw_match[] = {
332 	{ .compatible = "megachips,stdp4028-ge-b850v3-fw" },
333 	{},
334 };
335 MODULE_DEVICE_TABLE(of, stdp4028_ge_b850v3_fw_match);
336 
337 static struct i2c_driver stdp4028_ge_b850v3_fw_driver = {
338 	.id_table	= stdp4028_ge_b850v3_fw_i2c_table,
339 	.probe		= stdp4028_ge_b850v3_fw_probe,
340 	.remove		= stdp4028_ge_b850v3_fw_remove,
341 	.driver		= {
342 		.name		= "stdp4028-ge-b850v3-fw",
343 		.of_match_table = stdp4028_ge_b850v3_fw_match,
344 	},
345 };
346 
347 static int stdp2690_ge_b850v3_fw_probe(struct i2c_client *stdp2690_i2c)
348 {
349 	struct device *dev = &stdp2690_i2c->dev;
350 	int ret;
351 
352 	ret = ge_b850v3_lvds_init(dev);
353 
354 	if (ret)
355 		return ret;
356 
357 	ge_b850v3_lvds_ptr->stdp2690_i2c = stdp2690_i2c;
358 	i2c_set_clientdata(stdp2690_i2c, ge_b850v3_lvds_ptr);
359 
360 	/* Only register after both bridges are probed */
361 	if (!ge_b850v3_lvds_ptr->stdp4028_i2c)
362 		return 0;
363 
364 	return ge_b850v3_register();
365 }
366 
367 static void stdp2690_ge_b850v3_fw_remove(struct i2c_client *stdp2690_i2c)
368 {
369 	ge_b850v3_lvds_remove();
370 }
371 
372 static const struct i2c_device_id stdp2690_ge_b850v3_fw_i2c_table[] = {
373 	{"stdp2690_ge_fw", 0},
374 	{},
375 };
376 MODULE_DEVICE_TABLE(i2c, stdp2690_ge_b850v3_fw_i2c_table);
377 
378 static const struct of_device_id stdp2690_ge_b850v3_fw_match[] = {
379 	{ .compatible = "megachips,stdp2690-ge-b850v3-fw" },
380 	{},
381 };
382 MODULE_DEVICE_TABLE(of, stdp2690_ge_b850v3_fw_match);
383 
384 static struct i2c_driver stdp2690_ge_b850v3_fw_driver = {
385 	.id_table	= stdp2690_ge_b850v3_fw_i2c_table,
386 	.probe		= stdp2690_ge_b850v3_fw_probe,
387 	.remove		= stdp2690_ge_b850v3_fw_remove,
388 	.driver		= {
389 		.name		= "stdp2690-ge-b850v3-fw",
390 		.of_match_table = stdp2690_ge_b850v3_fw_match,
391 	},
392 };
393 
394 static int __init stdpxxxx_ge_b850v3_init(void)
395 {
396 	int ret;
397 
398 	ret = i2c_add_driver(&stdp4028_ge_b850v3_fw_driver);
399 	if (ret)
400 		return ret;
401 
402 	ret = i2c_add_driver(&stdp2690_ge_b850v3_fw_driver);
403 	if (ret)
404 		i2c_del_driver(&stdp4028_ge_b850v3_fw_driver);
405 
406 	return ret;
407 }
408 module_init(stdpxxxx_ge_b850v3_init);
409 
410 static void __exit stdpxxxx_ge_b850v3_exit(void)
411 {
412 	i2c_del_driver(&stdp2690_ge_b850v3_fw_driver);
413 	i2c_del_driver(&stdp4028_ge_b850v3_fw_driver);
414 }
415 module_exit(stdpxxxx_ge_b850v3_exit);
416 
417 MODULE_AUTHOR("Peter Senna Tschudin <peter.senna@collabora.com>");
418 MODULE_AUTHOR("Martyn Welch <martyn.welch@collabora.co.uk>");
419 MODULE_DESCRIPTION("GE LVDS to DP++ display bridge)");
420 MODULE_LICENSE("GPL v2");
421