xref: /linux/drivers/gpu/drm/bridge/megachips-stdpxxxx-ge-b850v3-fw.c (revision 2c142b63c8ee982cdfdba49a616027c266294838)
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 
stdp2690_read_block(void * context,u8 * buf,unsigned int block,size_t len)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 
ge_b850v3_lvds_edid_read(struct drm_bridge * bridge,struct drm_connector * connector)94 static const struct drm_edid *ge_b850v3_lvds_edid_read(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_edid_read_custom(connector, stdp2690_read_block, client);
102 }
103 
ge_b850v3_lvds_get_modes(struct drm_connector * connector)104 static int ge_b850v3_lvds_get_modes(struct drm_connector *connector)
105 {
106 	const struct drm_edid *drm_edid;
107 	int num_modes;
108 
109 	drm_edid = ge_b850v3_lvds_edid_read(&ge_b850v3_lvds_ptr->bridge, connector);
110 
111 	drm_edid_connector_update(connector, drm_edid);
112 	num_modes = drm_edid_connector_add_modes(connector);
113 	drm_edid_free(drm_edid);
114 
115 	return num_modes;
116 }
117 
118 static const struct
119 drm_connector_helper_funcs ge_b850v3_lvds_connector_helper_funcs = {
120 	.get_modes = ge_b850v3_lvds_get_modes,
121 };
122 
123 static enum drm_connector_status
ge_b850v3_lvds_bridge_detect(struct drm_bridge * bridge,struct drm_connector * connector)124 ge_b850v3_lvds_bridge_detect(struct drm_bridge *bridge, struct drm_connector *connector)
125 {
126 	struct i2c_client *stdp4028_i2c =
127 			ge_b850v3_lvds_ptr->stdp4028_i2c;
128 	s32 link_state;
129 
130 	link_state = i2c_smbus_read_word_data(stdp4028_i2c,
131 					      STDP4028_DPTX_STS_REG);
132 
133 	if (link_state == STDP4028_CON_STATE_CONNECTED)
134 		return connector_status_connected;
135 
136 	if (link_state == 0)
137 		return connector_status_disconnected;
138 
139 	return connector_status_unknown;
140 }
141 
ge_b850v3_lvds_detect(struct drm_connector * connector,bool force)142 static enum drm_connector_status ge_b850v3_lvds_detect(struct drm_connector *connector,
143 						       bool force)
144 {
145 	return ge_b850v3_lvds_bridge_detect(&ge_b850v3_lvds_ptr->bridge, connector);
146 }
147 
148 static const struct drm_connector_funcs ge_b850v3_lvds_connector_funcs = {
149 	.fill_modes = drm_helper_probe_single_connector_modes,
150 	.detect = ge_b850v3_lvds_detect,
151 	.destroy = drm_connector_cleanup,
152 	.reset = drm_atomic_helper_connector_reset,
153 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
154 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
155 };
156 
ge_b850v3_lvds_create_connector(struct drm_bridge * bridge)157 static int ge_b850v3_lvds_create_connector(struct drm_bridge *bridge)
158 {
159 	struct drm_connector *connector = &ge_b850v3_lvds_ptr->connector;
160 	int ret;
161 
162 	connector->polled = DRM_CONNECTOR_POLL_HPD;
163 
164 	drm_connector_helper_add(connector,
165 				 &ge_b850v3_lvds_connector_helper_funcs);
166 
167 	ret = drm_connector_init(bridge->dev, connector,
168 				 &ge_b850v3_lvds_connector_funcs,
169 				 DRM_MODE_CONNECTOR_DisplayPort);
170 	if (ret) {
171 		DRM_ERROR("Failed to initialize connector with drm\n");
172 		return ret;
173 	}
174 
175 	return drm_connector_attach_encoder(connector, bridge->encoder);
176 }
177 
ge_b850v3_lvds_irq_handler(int irq,void * dev_id)178 static irqreturn_t ge_b850v3_lvds_irq_handler(int irq, void *dev_id)
179 {
180 	struct i2c_client *stdp4028_i2c
181 			= ge_b850v3_lvds_ptr->stdp4028_i2c;
182 
183 	i2c_smbus_write_word_data(stdp4028_i2c,
184 				  STDP4028_DPTX_IRQ_STS_REG,
185 				  STDP4028_DPTX_IRQ_CLEAR);
186 
187 	if (ge_b850v3_lvds_ptr->bridge.dev)
188 		drm_kms_helper_hotplug_event(ge_b850v3_lvds_ptr->bridge.dev);
189 
190 	return IRQ_HANDLED;
191 }
192 
ge_b850v3_lvds_attach(struct drm_bridge * bridge,struct drm_encoder * encoder,enum drm_bridge_attach_flags flags)193 static int ge_b850v3_lvds_attach(struct drm_bridge *bridge,
194 				 struct drm_encoder *encoder,
195 				 enum drm_bridge_attach_flags flags)
196 {
197 	struct i2c_client *stdp4028_i2c
198 			= ge_b850v3_lvds_ptr->stdp4028_i2c;
199 
200 	/* Configures the bridge to re-enable interrupts after each ack. */
201 	i2c_smbus_write_word_data(stdp4028_i2c,
202 				  STDP4028_IRQ_OUT_CONF_REG,
203 				  STDP4028_DPTX_DP_IRQ_EN);
204 
205 	/* Enable interrupts */
206 	i2c_smbus_write_word_data(stdp4028_i2c,
207 				  STDP4028_DPTX_IRQ_EN_REG,
208 				  STDP4028_DPTX_IRQ_CONFIG);
209 
210 	if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
211 		return 0;
212 
213 	return ge_b850v3_lvds_create_connector(bridge);
214 }
215 
216 static const struct drm_bridge_funcs ge_b850v3_lvds_funcs = {
217 	.attach = ge_b850v3_lvds_attach,
218 	.detect = ge_b850v3_lvds_bridge_detect,
219 	.edid_read = ge_b850v3_lvds_edid_read,
220 };
221 
ge_b850v3_lvds_init(struct device * dev)222 static int ge_b850v3_lvds_init(struct device *dev)
223 {
224 	mutex_lock(&ge_b850v3_lvds_dev_mutex);
225 
226 	if (ge_b850v3_lvds_ptr)
227 		goto success;
228 
229 	ge_b850v3_lvds_ptr = devm_drm_bridge_alloc(dev, struct ge_b850v3_lvds, bridge,
230 						   &ge_b850v3_lvds_funcs);
231 	if (IS_ERR(ge_b850v3_lvds_ptr)) {
232 		mutex_unlock(&ge_b850v3_lvds_dev_mutex);
233 		return PTR_ERR(ge_b850v3_lvds_ptr);
234 	}
235 
236 success:
237 	mutex_unlock(&ge_b850v3_lvds_dev_mutex);
238 	return 0;
239 }
240 
ge_b850v3_lvds_remove(void)241 static void ge_b850v3_lvds_remove(void)
242 {
243 	mutex_lock(&ge_b850v3_lvds_dev_mutex);
244 	/*
245 	 * This check is to avoid both the drivers
246 	 * removing the bridge in their remove() function
247 	 */
248 	if (!ge_b850v3_lvds_ptr ||
249 	    !ge_b850v3_lvds_ptr->stdp2690_i2c ||
250 		!ge_b850v3_lvds_ptr->stdp4028_i2c)
251 		goto out;
252 
253 	drm_bridge_remove(&ge_b850v3_lvds_ptr->bridge);
254 	ge_b850v3_lvds_ptr = NULL;
255 out:
256 	mutex_unlock(&ge_b850v3_lvds_dev_mutex);
257 }
258 
ge_b850v3_register(void)259 static int ge_b850v3_register(void)
260 {
261 	struct i2c_client *stdp4028_i2c = ge_b850v3_lvds_ptr->stdp4028_i2c;
262 	struct device *dev = &stdp4028_i2c->dev;
263 	int ret;
264 
265 	/* drm bridge initialization */
266 	ge_b850v3_lvds_ptr->bridge.ops = DRM_BRIDGE_OP_DETECT |
267 					 DRM_BRIDGE_OP_EDID;
268 	ge_b850v3_lvds_ptr->bridge.type = DRM_MODE_CONNECTOR_DisplayPort;
269 	ge_b850v3_lvds_ptr->bridge.of_node = dev->of_node;
270 	drm_bridge_add(&ge_b850v3_lvds_ptr->bridge);
271 
272 	/* Clear pending interrupts since power up. */
273 	i2c_smbus_write_word_data(stdp4028_i2c,
274 				  STDP4028_DPTX_IRQ_STS_REG,
275 				  STDP4028_DPTX_IRQ_CLEAR);
276 
277 	if (!stdp4028_i2c->irq)
278 		return 0;
279 
280 	ret = devm_request_threaded_irq(&stdp4028_i2c->dev,
281 					stdp4028_i2c->irq, NULL,
282 					ge_b850v3_lvds_irq_handler,
283 					IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
284 					"ge-b850v3-lvds-dp", ge_b850v3_lvds_ptr);
285 	if (ret)
286 		drm_bridge_remove(&ge_b850v3_lvds_ptr->bridge);
287 
288 	return ret;
289 }
290 
stdp4028_ge_b850v3_fw_probe(struct i2c_client * stdp4028_i2c)291 static int stdp4028_ge_b850v3_fw_probe(struct i2c_client *stdp4028_i2c)
292 {
293 	struct device *dev = &stdp4028_i2c->dev;
294 	int ret;
295 
296 	ret = ge_b850v3_lvds_init(dev);
297 
298 	if (ret)
299 		return ret;
300 
301 	ge_b850v3_lvds_ptr->stdp4028_i2c = stdp4028_i2c;
302 	i2c_set_clientdata(stdp4028_i2c, ge_b850v3_lvds_ptr);
303 
304 	/* Only register after both bridges are probed */
305 	if (!ge_b850v3_lvds_ptr->stdp2690_i2c)
306 		return 0;
307 
308 	return ge_b850v3_register();
309 }
310 
stdp4028_ge_b850v3_fw_remove(struct i2c_client * stdp4028_i2c)311 static void stdp4028_ge_b850v3_fw_remove(struct i2c_client *stdp4028_i2c)
312 {
313 	ge_b850v3_lvds_remove();
314 }
315 
316 static const struct i2c_device_id stdp4028_ge_b850v3_fw_i2c_table[] = {
317 	{ "stdp4028_ge_fw" },
318 	{}
319 };
320 MODULE_DEVICE_TABLE(i2c, stdp4028_ge_b850v3_fw_i2c_table);
321 
322 static const struct of_device_id stdp4028_ge_b850v3_fw_match[] = {
323 	{ .compatible = "megachips,stdp4028-ge-b850v3-fw" },
324 	{},
325 };
326 MODULE_DEVICE_TABLE(of, stdp4028_ge_b850v3_fw_match);
327 
328 static struct i2c_driver stdp4028_ge_b850v3_fw_driver = {
329 	.id_table	= stdp4028_ge_b850v3_fw_i2c_table,
330 	.probe		= stdp4028_ge_b850v3_fw_probe,
331 	.remove		= stdp4028_ge_b850v3_fw_remove,
332 	.driver		= {
333 		.name		= "stdp4028-ge-b850v3-fw",
334 		.of_match_table = stdp4028_ge_b850v3_fw_match,
335 	},
336 };
337 
stdp2690_ge_b850v3_fw_probe(struct i2c_client * stdp2690_i2c)338 static int stdp2690_ge_b850v3_fw_probe(struct i2c_client *stdp2690_i2c)
339 {
340 	struct device *dev = &stdp2690_i2c->dev;
341 	int ret;
342 
343 	ret = ge_b850v3_lvds_init(dev);
344 
345 	if (ret)
346 		return ret;
347 
348 	ge_b850v3_lvds_ptr->stdp2690_i2c = stdp2690_i2c;
349 	i2c_set_clientdata(stdp2690_i2c, ge_b850v3_lvds_ptr);
350 
351 	/* Only register after both bridges are probed */
352 	if (!ge_b850v3_lvds_ptr->stdp4028_i2c)
353 		return 0;
354 
355 	return ge_b850v3_register();
356 }
357 
stdp2690_ge_b850v3_fw_remove(struct i2c_client * stdp2690_i2c)358 static void stdp2690_ge_b850v3_fw_remove(struct i2c_client *stdp2690_i2c)
359 {
360 	ge_b850v3_lvds_remove();
361 }
362 
363 static const struct i2c_device_id stdp2690_ge_b850v3_fw_i2c_table[] = {
364 	{ "stdp2690_ge_fw" },
365 	{}
366 };
367 MODULE_DEVICE_TABLE(i2c, stdp2690_ge_b850v3_fw_i2c_table);
368 
369 static const struct of_device_id stdp2690_ge_b850v3_fw_match[] = {
370 	{ .compatible = "megachips,stdp2690-ge-b850v3-fw" },
371 	{},
372 };
373 MODULE_DEVICE_TABLE(of, stdp2690_ge_b850v3_fw_match);
374 
375 static struct i2c_driver stdp2690_ge_b850v3_fw_driver = {
376 	.id_table	= stdp2690_ge_b850v3_fw_i2c_table,
377 	.probe		= stdp2690_ge_b850v3_fw_probe,
378 	.remove		= stdp2690_ge_b850v3_fw_remove,
379 	.driver		= {
380 		.name		= "stdp2690-ge-b850v3-fw",
381 		.of_match_table = stdp2690_ge_b850v3_fw_match,
382 	},
383 };
384 
stdpxxxx_ge_b850v3_init(void)385 static int __init stdpxxxx_ge_b850v3_init(void)
386 {
387 	int ret;
388 
389 	ret = i2c_add_driver(&stdp4028_ge_b850v3_fw_driver);
390 	if (ret)
391 		return ret;
392 
393 	ret = i2c_add_driver(&stdp2690_ge_b850v3_fw_driver);
394 	if (ret)
395 		i2c_del_driver(&stdp4028_ge_b850v3_fw_driver);
396 
397 	return ret;
398 }
399 module_init(stdpxxxx_ge_b850v3_init);
400 
stdpxxxx_ge_b850v3_exit(void)401 static void __exit stdpxxxx_ge_b850v3_exit(void)
402 {
403 	i2c_del_driver(&stdp2690_ge_b850v3_fw_driver);
404 	i2c_del_driver(&stdp4028_ge_b850v3_fw_driver);
405 }
406 module_exit(stdpxxxx_ge_b850v3_exit);
407 
408 MODULE_AUTHOR("Peter Senna Tschudin <peter.senna@collabora.com>");
409 MODULE_AUTHOR("Martyn Welch <martyn.welch@collabora.co.uk>");
410 MODULE_DESCRIPTION("GE LVDS to DP++ display bridge)");
411 MODULE_LICENSE("GPL v2");
412