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