Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / bridge / megachips-stdpxxxx-ge-b850v3-fw.c
blobd2808c4a6fb1cfa12ddaab5e4ada9ae578955bb7
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++)
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>
24 #include <linux/of.h>
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>
33 #define EDID_EXT_BLOCK_CNT 0x7E
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
40 #define STDP4028_DPTX_DP_IRQ_EN 0x1000
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)
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)
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)
57 static DEFINE_MUTEX(ge_b850v3_lvds_dev_mutex);
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;
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[] = {
77 .addr = client->addr,
78 .flags = 0,
79 .len = 1,
80 .buf = &start,
81 }, {
82 .addr = client->addr,
83 .flags = I2C_M_RD,
84 .len = EDID_LENGTH,
85 .buf = block,
89 if (!block)
90 return NULL;
92 if (i2c_transfer(adapter, msgs, 2) != 2) {
93 DRM_ERROR("Unable to read EDID.\n");
94 goto err;
97 if (!drm_edid_block_valid(block, 0, false, NULL)) {
98 DRM_ERROR("Invalid EDID data\n");
99 goto err;
102 total_size = (block[EDID_EXT_BLOCK_CNT] + 1) * EDID_LENGTH;
103 if (total_size > EDID_LENGTH) {
104 kfree(block);
105 block = kmalloc(total_size, GFP_KERNEL);
106 if (!block)
107 return NULL;
109 /* Yes, read the entire buffer, and do not skip the first
110 * EDID_LENGTH bytes.
112 start = 0x00;
113 msgs[1].len = total_size;
114 msgs[1].buf = block;
116 if (i2c_transfer(adapter, msgs, 2) != 2) {
117 DRM_ERROR("Unable to read EDID extension blocks.\n");
118 goto err;
120 if (!drm_edid_block_valid(block, 1, false, NULL)) {
121 DRM_ERROR("Invalid EDID data\n");
122 goto err;
126 return block;
128 err:
129 kfree(block);
130 return NULL;
133 static struct edid *ge_b850v3_lvds_get_edid(struct drm_bridge *bridge,
134 struct drm_connector *connector)
136 struct i2c_client *client;
138 client = ge_b850v3_lvds_ptr->stdp2690_i2c;
140 return (struct edid *)stdp2690_get_edid(client);
143 static int ge_b850v3_lvds_get_modes(struct drm_connector *connector)
145 struct edid *edid;
146 int num_modes;
148 edid = ge_b850v3_lvds_get_edid(&ge_b850v3_lvds_ptr->bridge, connector);
150 drm_connector_update_edid_property(connector, edid);
151 num_modes = drm_add_edid_modes(connector, edid);
152 kfree(edid);
154 return num_modes;
157 static enum drm_mode_status ge_b850v3_lvds_mode_valid(
158 struct drm_connector *connector, struct drm_display_mode *mode)
160 return MODE_OK;
163 static const struct
164 drm_connector_helper_funcs ge_b850v3_lvds_connector_helper_funcs = {
165 .get_modes = ge_b850v3_lvds_get_modes,
166 .mode_valid = ge_b850v3_lvds_mode_valid,
169 static enum drm_connector_status ge_b850v3_lvds_bridge_detect(struct drm_bridge *bridge)
171 struct i2c_client *stdp4028_i2c =
172 ge_b850v3_lvds_ptr->stdp4028_i2c;
173 s32 link_state;
175 link_state = i2c_smbus_read_word_data(stdp4028_i2c,
176 STDP4028_DPTX_STS_REG);
178 if (link_state == STDP4028_CON_STATE_CONNECTED)
179 return connector_status_connected;
181 if (link_state == 0)
182 return connector_status_disconnected;
184 return connector_status_unknown;
187 static enum drm_connector_status ge_b850v3_lvds_detect(struct drm_connector *connector,
188 bool force)
190 return ge_b850v3_lvds_bridge_detect(&ge_b850v3_lvds_ptr->bridge);
193 static const struct drm_connector_funcs ge_b850v3_lvds_connector_funcs = {
194 .fill_modes = drm_helper_probe_single_connector_modes,
195 .detect = ge_b850v3_lvds_detect,
196 .destroy = drm_connector_cleanup,
197 .reset = drm_atomic_helper_connector_reset,
198 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
199 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
202 static int ge_b850v3_lvds_create_connector(struct drm_bridge *bridge)
204 struct drm_connector *connector = &ge_b850v3_lvds_ptr->connector;
205 int ret;
207 if (!bridge->encoder) {
208 DRM_ERROR("Parent encoder object not found");
209 return -ENODEV;
212 connector->polled = DRM_CONNECTOR_POLL_HPD;
214 drm_connector_helper_add(connector,
215 &ge_b850v3_lvds_connector_helper_funcs);
217 ret = drm_connector_init(bridge->dev, connector,
218 &ge_b850v3_lvds_connector_funcs,
219 DRM_MODE_CONNECTOR_DisplayPort);
220 if (ret) {
221 DRM_ERROR("Failed to initialize connector with drm\n");
222 return ret;
225 return drm_connector_attach_encoder(connector, bridge->encoder);
228 static irqreturn_t ge_b850v3_lvds_irq_handler(int irq, void *dev_id)
230 struct i2c_client *stdp4028_i2c
231 = ge_b850v3_lvds_ptr->stdp4028_i2c;
233 i2c_smbus_write_word_data(stdp4028_i2c,
234 STDP4028_DPTX_IRQ_STS_REG,
235 STDP4028_DPTX_IRQ_CLEAR);
237 if (ge_b850v3_lvds_ptr->bridge.dev)
238 drm_kms_helper_hotplug_event(ge_b850v3_lvds_ptr->bridge.dev);
240 return IRQ_HANDLED;
243 static int ge_b850v3_lvds_attach(struct drm_bridge *bridge,
244 enum drm_bridge_attach_flags flags)
246 struct i2c_client *stdp4028_i2c
247 = ge_b850v3_lvds_ptr->stdp4028_i2c;
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);
254 /* Enable interrupts */
255 i2c_smbus_write_word_data(stdp4028_i2c,
256 STDP4028_DPTX_IRQ_EN_REG,
257 STDP4028_DPTX_IRQ_CONFIG);
259 if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
260 return 0;
262 return ge_b850v3_lvds_create_connector(bridge);
265 static const struct drm_bridge_funcs ge_b850v3_lvds_funcs = {
266 .attach = ge_b850v3_lvds_attach,
267 .detect = ge_b850v3_lvds_bridge_detect,
268 .get_edid = ge_b850v3_lvds_get_edid,
271 static int ge_b850v3_lvds_init(struct device *dev)
273 mutex_lock(&ge_b850v3_lvds_dev_mutex);
275 if (ge_b850v3_lvds_ptr)
276 goto success;
278 ge_b850v3_lvds_ptr = devm_kzalloc(dev,
279 sizeof(*ge_b850v3_lvds_ptr),
280 GFP_KERNEL);
282 if (!ge_b850v3_lvds_ptr) {
283 mutex_unlock(&ge_b850v3_lvds_dev_mutex);
284 return -ENOMEM;
287 success:
288 mutex_unlock(&ge_b850v3_lvds_dev_mutex);
289 return 0;
292 static void ge_b850v3_lvds_remove(void)
294 mutex_lock(&ge_b850v3_lvds_dev_mutex);
296 * This check is to avoid both the drivers
297 * removing the bridge in their remove() function
299 if (!ge_b850v3_lvds_ptr)
300 goto out;
302 drm_bridge_remove(&ge_b850v3_lvds_ptr->bridge);
304 ge_b850v3_lvds_ptr = NULL;
305 out:
306 mutex_unlock(&ge_b850v3_lvds_dev_mutex);
309 static int stdp4028_ge_b850v3_fw_probe(struct i2c_client *stdp4028_i2c,
310 const struct i2c_device_id *id)
312 struct device *dev = &stdp4028_i2c->dev;
313 int ret;
315 ret = ge_b850v3_lvds_init(dev);
317 if (ret)
318 return ret;
320 ge_b850v3_lvds_ptr->stdp4028_i2c = stdp4028_i2c;
321 i2c_set_clientdata(stdp4028_i2c, ge_b850v3_lvds_ptr);
323 /* drm bridge initialization */
324 ge_b850v3_lvds_ptr->bridge.funcs = &ge_b850v3_lvds_funcs;
325 ge_b850v3_lvds_ptr->bridge.ops = DRM_BRIDGE_OP_DETECT |
326 DRM_BRIDGE_OP_EDID;
327 ge_b850v3_lvds_ptr->bridge.type = DRM_MODE_CONNECTOR_DisplayPort;
328 ge_b850v3_lvds_ptr->bridge.of_node = dev->of_node;
329 drm_bridge_add(&ge_b850v3_lvds_ptr->bridge);
331 /* Clear pending interrupts since power up. */
332 i2c_smbus_write_word_data(stdp4028_i2c,
333 STDP4028_DPTX_IRQ_STS_REG,
334 STDP4028_DPTX_IRQ_CLEAR);
336 if (!stdp4028_i2c->irq)
337 return 0;
339 return devm_request_threaded_irq(&stdp4028_i2c->dev,
340 stdp4028_i2c->irq, NULL,
341 ge_b850v3_lvds_irq_handler,
342 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
343 "ge-b850v3-lvds-dp", ge_b850v3_lvds_ptr);
346 static int stdp4028_ge_b850v3_fw_remove(struct i2c_client *stdp4028_i2c)
348 ge_b850v3_lvds_remove();
350 return 0;
353 static const struct i2c_device_id stdp4028_ge_b850v3_fw_i2c_table[] = {
354 {"stdp4028_ge_fw", 0},
357 MODULE_DEVICE_TABLE(i2c, stdp4028_ge_b850v3_fw_i2c_table);
359 static const struct of_device_id stdp4028_ge_b850v3_fw_match[] = {
360 { .compatible = "megachips,stdp4028-ge-b850v3-fw" },
363 MODULE_DEVICE_TABLE(of, stdp4028_ge_b850v3_fw_match);
365 static struct i2c_driver stdp4028_ge_b850v3_fw_driver = {
366 .id_table = stdp4028_ge_b850v3_fw_i2c_table,
367 .probe = stdp4028_ge_b850v3_fw_probe,
368 .remove = stdp4028_ge_b850v3_fw_remove,
369 .driver = {
370 .name = "stdp4028-ge-b850v3-fw",
371 .of_match_table = stdp4028_ge_b850v3_fw_match,
375 static int stdp2690_ge_b850v3_fw_probe(struct i2c_client *stdp2690_i2c,
376 const struct i2c_device_id *id)
378 struct device *dev = &stdp2690_i2c->dev;
379 int ret;
381 ret = ge_b850v3_lvds_init(dev);
383 if (ret)
384 return ret;
386 ge_b850v3_lvds_ptr->stdp2690_i2c = stdp2690_i2c;
387 i2c_set_clientdata(stdp2690_i2c, ge_b850v3_lvds_ptr);
389 return 0;
392 static int stdp2690_ge_b850v3_fw_remove(struct i2c_client *stdp2690_i2c)
394 ge_b850v3_lvds_remove();
396 return 0;
399 static const struct i2c_device_id stdp2690_ge_b850v3_fw_i2c_table[] = {
400 {"stdp2690_ge_fw", 0},
403 MODULE_DEVICE_TABLE(i2c, stdp2690_ge_b850v3_fw_i2c_table);
405 static const struct of_device_id stdp2690_ge_b850v3_fw_match[] = {
406 { .compatible = "megachips,stdp2690-ge-b850v3-fw" },
409 MODULE_DEVICE_TABLE(of, stdp2690_ge_b850v3_fw_match);
411 static struct i2c_driver stdp2690_ge_b850v3_fw_driver = {
412 .id_table = stdp2690_ge_b850v3_fw_i2c_table,
413 .probe = stdp2690_ge_b850v3_fw_probe,
414 .remove = stdp2690_ge_b850v3_fw_remove,
415 .driver = {
416 .name = "stdp2690-ge-b850v3-fw",
417 .of_match_table = stdp2690_ge_b850v3_fw_match,
421 static int __init stdpxxxx_ge_b850v3_init(void)
423 int ret;
425 ret = i2c_add_driver(&stdp4028_ge_b850v3_fw_driver);
426 if (ret)
427 return ret;
429 return i2c_add_driver(&stdp2690_ge_b850v3_fw_driver);
431 module_init(stdpxxxx_ge_b850v3_init);
433 static void __exit stdpxxxx_ge_b850v3_exit(void)
435 i2c_del_driver(&stdp2690_ge_b850v3_fw_driver);
436 i2c_del_driver(&stdp4028_ge_b850v3_fw_driver);
438 module_exit(stdpxxxx_ge_b850v3_exit);
440 MODULE_AUTHOR("Peter Senna Tschudin <peter.senna@collabora.com>");
441 MODULE_AUTHOR("Martyn Welch <martyn.welch@collabora.co.uk>");
442 MODULE_DESCRIPTION("GE LVDS to DP++ display bridge)");
443 MODULE_LICENSE("GPL v2");