2 * Driver for MegaChips STDP4028 with GE B850v3 firmware (LVDS-DP)
3 * Driver for MegaChips STDP2690 with GE B850v3 firmware (DP-DP++)
5 * Copyright (c) 2017, Collabora Ltd.
6 * Copyright (c) 2017, General Electric Company
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.
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
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/>.
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:
29 * Host -> LVDS|--(STDP4028)--|DP -> DP|--(STDP2690)--|DP++ -> Video output
33 #include <linux/gpio.h>
34 #include <linux/i2c.h>
35 #include <linux/module.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>
43 #define EDID_EXT_BLOCK_CNT 0x7E
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
50 #define STDP4028_DPTX_DP_IRQ_EN 0x1000
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)
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)
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)
67 static DEFINE_MUTEX(ge_b850v3_lvds_dev_mutex
);
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
;
77 static struct ge_b850v3_lvds
*ge_b850v3_lvds_ptr
;
79 static u8
*stdp2690_get_edid(struct i2c_client
*client
)
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
);
86 struct i2c_msg msgs
[] = {
103 if (i2c_transfer(adapter
, msgs
, 2) != 2) {
104 DRM_ERROR("Unable to read EDID.\n");
108 if (!drm_edid_block_valid(block
, 0, false, NULL
)) {
109 DRM_ERROR("Invalid EDID data\n");
113 total_size
= (block
[EDID_EXT_BLOCK_CNT
] + 1) * EDID_LENGTH
;
114 if (total_size
> EDID_LENGTH
) {
116 block
= kmalloc(total_size
, GFP_KERNEL
);
120 /* Yes, read the entire buffer, and do not skip the first
124 msgs
[1].len
= total_size
;
127 if (i2c_transfer(adapter
, msgs
, 2) != 2) {
128 DRM_ERROR("Unable to read EDID extension blocks.\n");
131 if (!drm_edid_block_valid(block
, 1, false, NULL
)) {
132 DRM_ERROR("Invalid EDID data\n");
144 static int ge_b850v3_lvds_get_modes(struct drm_connector
*connector
)
146 struct i2c_client
*client
;
149 client
= ge_b850v3_lvds_ptr
->stdp2690_i2c
;
151 kfree(ge_b850v3_lvds_ptr
->edid
);
152 ge_b850v3_lvds_ptr
->edid
= (struct edid
*)stdp2690_get_edid(client
);
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
);
164 static enum drm_mode_status
ge_b850v3_lvds_mode_valid(
165 struct drm_connector
*connector
, struct drm_display_mode
*mode
)
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
,
176 static enum drm_connector_status
ge_b850v3_lvds_detect(
177 struct drm_connector
*connector
, bool force
)
179 struct i2c_client
*stdp4028_i2c
=
180 ge_b850v3_lvds_ptr
->stdp4028_i2c
;
183 link_state
= i2c_smbus_read_word_data(stdp4028_i2c
,
184 STDP4028_DPTX_STS_REG
);
186 if (link_state
== STDP4028_CON_STATE_CONNECTED
)
187 return connector_status_connected
;
190 return connector_status_disconnected
;
192 return connector_status_unknown
;
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
,
205 static irqreturn_t
ge_b850v3_lvds_irq_handler(int irq
, void *dev_id
)
207 struct i2c_client
*stdp4028_i2c
208 = ge_b850v3_lvds_ptr
->stdp4028_i2c
;
210 i2c_smbus_write_word_data(stdp4028_i2c
,
211 STDP4028_DPTX_IRQ_STS_REG
,
212 STDP4028_DPTX_IRQ_CLEAR
);
214 if (ge_b850v3_lvds_ptr
->connector
.dev
)
215 drm_kms_helper_hotplug_event(ge_b850v3_lvds_ptr
->connector
.dev
);
220 static int ge_b850v3_lvds_attach(struct drm_bridge
*bridge
)
222 struct drm_connector
*connector
= &ge_b850v3_lvds_ptr
->connector
;
223 struct i2c_client
*stdp4028_i2c
224 = ge_b850v3_lvds_ptr
->stdp4028_i2c
;
227 if (!bridge
->encoder
) {
228 DRM_ERROR("Parent encoder object not found");
232 connector
->polled
= DRM_CONNECTOR_POLL_HPD
;
234 drm_connector_helper_add(connector
,
235 &ge_b850v3_lvds_connector_helper_funcs
);
237 ret
= drm_connector_init(bridge
->dev
, connector
,
238 &ge_b850v3_lvds_connector_funcs
,
239 DRM_MODE_CONNECTOR_DisplayPort
);
241 DRM_ERROR("Failed to initialize connector with drm\n");
245 ret
= drm_mode_connector_attach_encoder(connector
, bridge
->encoder
);
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
);
262 static const struct drm_bridge_funcs ge_b850v3_lvds_funcs
= {
263 .attach
= ge_b850v3_lvds_attach
,
266 static int ge_b850v3_lvds_init(struct device
*dev
)
268 mutex_lock(&ge_b850v3_lvds_dev_mutex
);
270 if (ge_b850v3_lvds_ptr
)
273 ge_b850v3_lvds_ptr
= devm_kzalloc(dev
,
274 sizeof(*ge_b850v3_lvds_ptr
),
277 if (!ge_b850v3_lvds_ptr
) {
278 mutex_unlock(&ge_b850v3_lvds_dev_mutex
);
283 mutex_unlock(&ge_b850v3_lvds_dev_mutex
);
287 static void ge_b850v3_lvds_remove(void)
289 mutex_lock(&ge_b850v3_lvds_dev_mutex
);
291 * This check is to avoid both the drivers
292 * removing the bridge in their remove() function
294 if (!ge_b850v3_lvds_ptr
)
297 drm_bridge_remove(&ge_b850v3_lvds_ptr
->bridge
);
299 kfree(ge_b850v3_lvds_ptr
->edid
);
301 ge_b850v3_lvds_ptr
= NULL
;
303 mutex_unlock(&ge_b850v3_lvds_dev_mutex
);
306 static int stdp4028_ge_b850v3_fw_probe(struct i2c_client
*stdp4028_i2c
,
307 const struct i2c_device_id
*id
)
309 struct device
*dev
= &stdp4028_i2c
->dev
;
311 ge_b850v3_lvds_init(dev
);
313 ge_b850v3_lvds_ptr
->stdp4028_i2c
= stdp4028_i2c
;
314 i2c_set_clientdata(stdp4028_i2c
, ge_b850v3_lvds_ptr
);
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
);
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
);
326 if (!stdp4028_i2c
->irq
)
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
);
336 static int stdp4028_ge_b850v3_fw_remove(struct i2c_client
*stdp4028_i2c
)
338 ge_b850v3_lvds_remove();
343 static const struct i2c_device_id stdp4028_ge_b850v3_fw_i2c_table
[] = {
344 {"stdp4028_ge_fw", 0},
347 MODULE_DEVICE_TABLE(i2c
, stdp4028_ge_b850v3_fw_i2c_table
);
349 static const struct of_device_id stdp4028_ge_b850v3_fw_match
[] = {
350 { .compatible
= "megachips,stdp4028-ge-b850v3-fw" },
353 MODULE_DEVICE_TABLE(of
, stdp4028_ge_b850v3_fw_match
);
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
,
360 .name
= "stdp4028-ge-b850v3-fw",
361 .of_match_table
= stdp4028_ge_b850v3_fw_match
,
365 static int stdp2690_ge_b850v3_fw_probe(struct i2c_client
*stdp2690_i2c
,
366 const struct i2c_device_id
*id
)
368 struct device
*dev
= &stdp2690_i2c
->dev
;
370 ge_b850v3_lvds_init(dev
);
372 ge_b850v3_lvds_ptr
->stdp2690_i2c
= stdp2690_i2c
;
373 i2c_set_clientdata(stdp2690_i2c
, ge_b850v3_lvds_ptr
);
378 static int stdp2690_ge_b850v3_fw_remove(struct i2c_client
*stdp2690_i2c
)
380 ge_b850v3_lvds_remove();
385 static const struct i2c_device_id stdp2690_ge_b850v3_fw_i2c_table
[] = {
386 {"stdp2690_ge_fw", 0},
389 MODULE_DEVICE_TABLE(i2c
, stdp2690_ge_b850v3_fw_i2c_table
);
391 static const struct of_device_id stdp2690_ge_b850v3_fw_match
[] = {
392 { .compatible
= "megachips,stdp2690-ge-b850v3-fw" },
395 MODULE_DEVICE_TABLE(of
, stdp2690_ge_b850v3_fw_match
);
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
,
402 .name
= "stdp2690-ge-b850v3-fw",
403 .of_match_table
= stdp2690_ge_b850v3_fw_match
,
407 static int __init
stdpxxxx_ge_b850v3_init(void)
411 ret
= i2c_add_driver(&stdp4028_ge_b850v3_fw_driver
);
415 return i2c_add_driver(&stdp2690_ge_b850v3_fw_driver
);
417 module_init(stdpxxxx_ge_b850v3_init
);
419 static void __exit
stdpxxxx_ge_b850v3_exit(void)
421 i2c_del_driver(&stdp2690_ge_b850v3_fw_driver
);
422 i2c_del_driver(&stdp4028_ge_b850v3_fw_driver
);
424 module_exit(stdpxxxx_ge_b850v3_exit
);
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");