1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for MegaChips STDP4028 with GE B850v3 firmware (LVDS-DP)
4 * Driver for MegaChips STDP2690 with GE B850v3 firmware (DP-DP++)
6 * Copyright (c) 2017, Collabora Ltd.
7 * Copyright (c) 2017, General Electric Company
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:
19 * Host -> LVDS|--(STDP4028)--|DP -> DP|--(STDP2690)--|DP++ -> Video output
22 #include <linux/i2c.h>
23 #include <linux/module.h>
26 #include <drm/drm_atomic.h>
27 #include <drm/drm_atomic_helper.h>
28 #include <drm/drm_edid.h>
29 #include <drm/drm_print.h>
30 #include <drm/drm_probe_helper.h>
32 #define EDID_EXT_BLOCK_CNT 0x7E
34 #define STDP4028_IRQ_OUT_CONF_REG 0x02
35 #define STDP4028_DPTX_IRQ_EN_REG 0x3C
36 #define STDP4028_DPTX_IRQ_STS_REG 0x3D
37 #define STDP4028_DPTX_STS_REG 0x3E
39 #define STDP4028_DPTX_DP_IRQ_EN 0x1000
41 #define STDP4028_DPTX_HOTPLUG_IRQ_EN 0x0400
42 #define STDP4028_DPTX_LINK_CH_IRQ_EN 0x2000
43 #define STDP4028_DPTX_IRQ_CONFIG \
44 (STDP4028_DPTX_LINK_CH_IRQ_EN | STDP4028_DPTX_HOTPLUG_IRQ_EN)
46 #define STDP4028_DPTX_HOTPLUG_STS 0x0200
47 #define STDP4028_DPTX_LINK_STS 0x1000
48 #define STDP4028_CON_STATE_CONNECTED \
49 (STDP4028_DPTX_HOTPLUG_STS | STDP4028_DPTX_LINK_STS)
51 #define STDP4028_DPTX_HOTPLUG_CH_STS 0x0400
52 #define STDP4028_DPTX_LINK_CH_STS 0x2000
53 #define STDP4028_DPTX_IRQ_CLEAR \
54 (STDP4028_DPTX_LINK_CH_STS | STDP4028_DPTX_HOTPLUG_CH_STS)
56 static DEFINE_MUTEX(ge_b850v3_lvds_dev_mutex);
58 struct ge_b850v3_lvds {
59 struct drm_connector connector;
60 struct drm_bridge bridge;
61 struct i2c_client *stdp4028_i2c;
62 struct i2c_client *stdp2690_i2c;
66 static struct ge_b850v3_lvds *ge_b850v3_lvds_ptr;
68 static u8 *stdp2690_get_edid(struct i2c_client *client)
70 struct i2c_adapter *adapter = client->adapter;
71 unsigned char start = 0x00;
72 unsigned int total_size;
73 u8 *block = kmalloc(EDID_LENGTH, GFP_KERNEL);
75 struct i2c_msg msgs[] = {
92 if (i2c_transfer(adapter, msgs, 2) != 2) {
93 DRM_ERROR("Unable to read EDID.\n");
97 if (!drm_edid_block_valid(block, 0, false, NULL)) {
98 DRM_ERROR("Invalid EDID data\n");
102 total_size = (block[EDID_EXT_BLOCK_CNT] + 1) * EDID_LENGTH;
103 if (total_size > EDID_LENGTH) {
105 block = kmalloc(total_size, GFP_KERNEL);
109 /* Yes, read the entire buffer, and do not skip the first
113 msgs[1].len = total_size;
116 if (i2c_transfer(adapter, msgs, 2) != 2) {
117 DRM_ERROR("Unable to read EDID extension blocks.\n");
120 if (!drm_edid_block_valid(block, 1, false, NULL)) {
121 DRM_ERROR("Invalid EDID data\n");
133 static int ge_b850v3_lvds_get_modes(struct drm_connector *connector)
135 struct i2c_client *client;
138 client = ge_b850v3_lvds_ptr->stdp2690_i2c;
140 kfree(ge_b850v3_lvds_ptr->edid);
141 ge_b850v3_lvds_ptr->edid = (struct edid *)stdp2690_get_edid(client);
143 if (ge_b850v3_lvds_ptr->edid) {
144 drm_connector_update_edid_property(connector,
145 ge_b850v3_lvds_ptr->edid);
146 num_modes = drm_add_edid_modes(connector,
147 ge_b850v3_lvds_ptr->edid);
153 static enum drm_mode_status ge_b850v3_lvds_mode_valid(
154 struct drm_connector *connector, struct drm_display_mode *mode)
160 drm_connector_helper_funcs ge_b850v3_lvds_connector_helper_funcs = {
161 .get_modes = ge_b850v3_lvds_get_modes,
162 .mode_valid = ge_b850v3_lvds_mode_valid,
165 static enum drm_connector_status ge_b850v3_lvds_detect(
166 struct drm_connector *connector, bool force)
168 struct i2c_client *stdp4028_i2c =
169 ge_b850v3_lvds_ptr->stdp4028_i2c;
172 link_state = i2c_smbus_read_word_data(stdp4028_i2c,
173 STDP4028_DPTX_STS_REG);
175 if (link_state == STDP4028_CON_STATE_CONNECTED)
176 return connector_status_connected;
179 return connector_status_disconnected;
181 return connector_status_unknown;
184 static const struct drm_connector_funcs ge_b850v3_lvds_connector_funcs = {
185 .fill_modes = drm_helper_probe_single_connector_modes,
186 .detect = ge_b850v3_lvds_detect,
187 .destroy = drm_connector_cleanup,
188 .reset = drm_atomic_helper_connector_reset,
189 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
190 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
193 static irqreturn_t ge_b850v3_lvds_irq_handler(int irq, void *dev_id)
195 struct i2c_client *stdp4028_i2c
196 = ge_b850v3_lvds_ptr->stdp4028_i2c;
198 i2c_smbus_write_word_data(stdp4028_i2c,
199 STDP4028_DPTX_IRQ_STS_REG,
200 STDP4028_DPTX_IRQ_CLEAR);
202 if (ge_b850v3_lvds_ptr->connector.dev)
203 drm_kms_helper_hotplug_event(ge_b850v3_lvds_ptr->connector.dev);
208 static int ge_b850v3_lvds_attach(struct drm_bridge *bridge)
210 struct drm_connector *connector = &ge_b850v3_lvds_ptr->connector;
211 struct i2c_client *stdp4028_i2c
212 = ge_b850v3_lvds_ptr->stdp4028_i2c;
215 if (!bridge->encoder) {
216 DRM_ERROR("Parent encoder object not found");
220 connector->polled = DRM_CONNECTOR_POLL_HPD;
222 drm_connector_helper_add(connector,
223 &ge_b850v3_lvds_connector_helper_funcs);
225 ret = drm_connector_init(bridge->dev, connector,
226 &ge_b850v3_lvds_connector_funcs,
227 DRM_MODE_CONNECTOR_DisplayPort);
229 DRM_ERROR("Failed to initialize connector with drm\n");
233 ret = drm_connector_attach_encoder(connector, bridge->encoder);
237 /* Configures the bridge to re-enable interrupts after each ack. */
238 i2c_smbus_write_word_data(stdp4028_i2c,
239 STDP4028_IRQ_OUT_CONF_REG,
240 STDP4028_DPTX_DP_IRQ_EN);
242 /* Enable interrupts */
243 i2c_smbus_write_word_data(stdp4028_i2c,
244 STDP4028_DPTX_IRQ_EN_REG,
245 STDP4028_DPTX_IRQ_CONFIG);
250 static const struct drm_bridge_funcs ge_b850v3_lvds_funcs = {
251 .attach = ge_b850v3_lvds_attach,
254 static int ge_b850v3_lvds_init(struct device *dev)
256 mutex_lock(&ge_b850v3_lvds_dev_mutex);
258 if (ge_b850v3_lvds_ptr)
261 ge_b850v3_lvds_ptr = devm_kzalloc(dev,
262 sizeof(*ge_b850v3_lvds_ptr),
265 if (!ge_b850v3_lvds_ptr) {
266 mutex_unlock(&ge_b850v3_lvds_dev_mutex);
271 mutex_unlock(&ge_b850v3_lvds_dev_mutex);
275 static void ge_b850v3_lvds_remove(void)
277 mutex_lock(&ge_b850v3_lvds_dev_mutex);
279 * This check is to avoid both the drivers
280 * removing the bridge in their remove() function
282 if (!ge_b850v3_lvds_ptr)
285 drm_bridge_remove(&ge_b850v3_lvds_ptr->bridge);
287 kfree(ge_b850v3_lvds_ptr->edid);
289 ge_b850v3_lvds_ptr = NULL;
291 mutex_unlock(&ge_b850v3_lvds_dev_mutex);
294 static int stdp4028_ge_b850v3_fw_probe(struct i2c_client *stdp4028_i2c,
295 const struct i2c_device_id *id)
297 struct device *dev = &stdp4028_i2c->dev;
299 ge_b850v3_lvds_init(dev);
301 ge_b850v3_lvds_ptr->stdp4028_i2c = stdp4028_i2c;
302 i2c_set_clientdata(stdp4028_i2c, ge_b850v3_lvds_ptr);
304 /* drm bridge initialization */
305 ge_b850v3_lvds_ptr->bridge.funcs = &ge_b850v3_lvds_funcs;
306 ge_b850v3_lvds_ptr->bridge.of_node = dev->of_node;
307 drm_bridge_add(&ge_b850v3_lvds_ptr->bridge);
309 /* Clear pending interrupts since power up. */
310 i2c_smbus_write_word_data(stdp4028_i2c,
311 STDP4028_DPTX_IRQ_STS_REG,
312 STDP4028_DPTX_IRQ_CLEAR);
314 if (!stdp4028_i2c->irq)
317 return devm_request_threaded_irq(&stdp4028_i2c->dev,
318 stdp4028_i2c->irq, NULL,
319 ge_b850v3_lvds_irq_handler,
320 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
321 "ge-b850v3-lvds-dp", ge_b850v3_lvds_ptr);
324 static int stdp4028_ge_b850v3_fw_remove(struct i2c_client *stdp4028_i2c)
326 ge_b850v3_lvds_remove();
331 static const struct i2c_device_id stdp4028_ge_b850v3_fw_i2c_table[] = {
332 {"stdp4028_ge_fw", 0},
335 MODULE_DEVICE_TABLE(i2c, stdp4028_ge_b850v3_fw_i2c_table);
337 static const struct of_device_id stdp4028_ge_b850v3_fw_match[] = {
338 { .compatible = "megachips,stdp4028-ge-b850v3-fw" },
341 MODULE_DEVICE_TABLE(of, stdp4028_ge_b850v3_fw_match);
343 static struct i2c_driver stdp4028_ge_b850v3_fw_driver = {
344 .id_table = stdp4028_ge_b850v3_fw_i2c_table,
345 .probe = stdp4028_ge_b850v3_fw_probe,
346 .remove = stdp4028_ge_b850v3_fw_remove,
348 .name = "stdp4028-ge-b850v3-fw",
349 .of_match_table = stdp4028_ge_b850v3_fw_match,
353 static int stdp2690_ge_b850v3_fw_probe(struct i2c_client *stdp2690_i2c,
354 const struct i2c_device_id *id)
356 struct device *dev = &stdp2690_i2c->dev;
358 ge_b850v3_lvds_init(dev);
360 ge_b850v3_lvds_ptr->stdp2690_i2c = stdp2690_i2c;
361 i2c_set_clientdata(stdp2690_i2c, ge_b850v3_lvds_ptr);
366 static int stdp2690_ge_b850v3_fw_remove(struct i2c_client *stdp2690_i2c)
368 ge_b850v3_lvds_remove();
373 static const struct i2c_device_id stdp2690_ge_b850v3_fw_i2c_table[] = {
374 {"stdp2690_ge_fw", 0},
377 MODULE_DEVICE_TABLE(i2c, stdp2690_ge_b850v3_fw_i2c_table);
379 static const struct of_device_id stdp2690_ge_b850v3_fw_match[] = {
380 { .compatible = "megachips,stdp2690-ge-b850v3-fw" },
383 MODULE_DEVICE_TABLE(of, stdp2690_ge_b850v3_fw_match);
385 static struct i2c_driver stdp2690_ge_b850v3_fw_driver = {
386 .id_table = stdp2690_ge_b850v3_fw_i2c_table,
387 .probe = stdp2690_ge_b850v3_fw_probe,
388 .remove = stdp2690_ge_b850v3_fw_remove,
390 .name = "stdp2690-ge-b850v3-fw",
391 .of_match_table = stdp2690_ge_b850v3_fw_match,
395 static int __init stdpxxxx_ge_b850v3_init(void)
399 ret = i2c_add_driver(&stdp4028_ge_b850v3_fw_driver);
403 return i2c_add_driver(&stdp2690_ge_b850v3_fw_driver);
405 module_init(stdpxxxx_ge_b850v3_init);
407 static void __exit stdpxxxx_ge_b850v3_exit(void)
409 i2c_del_driver(&stdp2690_ge_b850v3_fw_driver);
410 i2c_del_driver(&stdp4028_ge_b850v3_fw_driver);
412 module_exit(stdpxxxx_ge_b850v3_exit);
414 MODULE_AUTHOR("Peter Senna Tschudin <peter.senna@collabora.com>");
415 MODULE_AUTHOR("Martyn Welch <martyn.welch@collabora.co.uk>");
416 MODULE_DESCRIPTION("GE LVDS to DP++ display bridge)");
417 MODULE_LICENSE("GPL v2");