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 38 #include <drm/drm_atomic.h> 39 #include <drm/drm_atomic_helper.h> 40 #include <drm/drm_edid.h> 41 #include <drm/drm_print.h> 42 #include <drm/drm_probe_helper.h> 43 44 #define EDID_EXT_BLOCK_CNT 0x7E 45 46 #define STDP4028_IRQ_OUT_CONF_REG 0x02 47 #define STDP4028_DPTX_IRQ_EN_REG 0x3C 48 #define STDP4028_DPTX_IRQ_STS_REG 0x3D 49 #define STDP4028_DPTX_STS_REG 0x3E 50 51 #define STDP4028_DPTX_DP_IRQ_EN 0x1000 52 53 #define STDP4028_DPTX_HOTPLUG_IRQ_EN 0x0400 54 #define STDP4028_DPTX_LINK_CH_IRQ_EN 0x2000 55 #define STDP4028_DPTX_IRQ_CONFIG \ 56 (STDP4028_DPTX_LINK_CH_IRQ_EN | STDP4028_DPTX_HOTPLUG_IRQ_EN) 57 58 #define STDP4028_DPTX_HOTPLUG_STS 0x0200 59 #define STDP4028_DPTX_LINK_STS 0x1000 60 #define STDP4028_CON_STATE_CONNECTED \ 61 (STDP4028_DPTX_HOTPLUG_STS | STDP4028_DPTX_LINK_STS) 62 63 #define STDP4028_DPTX_HOTPLUG_CH_STS 0x0400 64 #define STDP4028_DPTX_LINK_CH_STS 0x2000 65 #define STDP4028_DPTX_IRQ_CLEAR \ 66 (STDP4028_DPTX_LINK_CH_STS | STDP4028_DPTX_HOTPLUG_CH_STS) 67 68 static DEFINE_MUTEX(ge_b850v3_lvds_dev_mutex); 69 70 struct ge_b850v3_lvds { 71 struct drm_connector connector; 72 struct drm_bridge bridge; 73 struct i2c_client *stdp4028_i2c; 74 struct i2c_client *stdp2690_i2c; 75 struct edid *edid; 76 }; 77 78 static struct ge_b850v3_lvds *ge_b850v3_lvds_ptr; 79 80 static u8 *stdp2690_get_edid(struct i2c_client *client) 81 { 82 struct i2c_adapter *adapter = client->adapter; 83 unsigned char start = 0x00; 84 unsigned int total_size; 85 u8 *block = kmalloc(EDID_LENGTH, GFP_KERNEL); 86 87 struct i2c_msg msgs[] = { 88 { 89 .addr = client->addr, 90 .flags = 0, 91 .len = 1, 92 .buf = &start, 93 }, { 94 .addr = client->addr, 95 .flags = I2C_M_RD, 96 .len = EDID_LENGTH, 97 .buf = block, 98 } 99 }; 100 101 if (!block) 102 return NULL; 103 104 if (i2c_transfer(adapter, msgs, 2) != 2) { 105 DRM_ERROR("Unable to read EDID.\n"); 106 goto err; 107 } 108 109 if (!drm_edid_block_valid(block, 0, false, NULL)) { 110 DRM_ERROR("Invalid EDID data\n"); 111 goto err; 112 } 113 114 total_size = (block[EDID_EXT_BLOCK_CNT] + 1) * EDID_LENGTH; 115 if (total_size > EDID_LENGTH) { 116 kfree(block); 117 block = kmalloc(total_size, GFP_KERNEL); 118 if (!block) 119 return NULL; 120 121 /* Yes, read the entire buffer, and do not skip the first 122 * EDID_LENGTH bytes. 123 */ 124 start = 0x00; 125 msgs[1].len = total_size; 126 msgs[1].buf = block; 127 128 if (i2c_transfer(adapter, msgs, 2) != 2) { 129 DRM_ERROR("Unable to read EDID extension blocks.\n"); 130 goto err; 131 } 132 if (!drm_edid_block_valid(block, 1, false, NULL)) { 133 DRM_ERROR("Invalid EDID data\n"); 134 goto err; 135 } 136 } 137 138 return block; 139 140 err: 141 kfree(block); 142 return NULL; 143 } 144 145 static int ge_b850v3_lvds_get_modes(struct drm_connector *connector) 146 { 147 struct i2c_client *client; 148 int num_modes = 0; 149 150 client = ge_b850v3_lvds_ptr->stdp2690_i2c; 151 152 kfree(ge_b850v3_lvds_ptr->edid); 153 ge_b850v3_lvds_ptr->edid = (struct edid *)stdp2690_get_edid(client); 154 155 if (ge_b850v3_lvds_ptr->edid) { 156 drm_connector_update_edid_property(connector, 157 ge_b850v3_lvds_ptr->edid); 158 num_modes = drm_add_edid_modes(connector, 159 ge_b850v3_lvds_ptr->edid); 160 } 161 162 return num_modes; 163 } 164 165 static enum drm_mode_status ge_b850v3_lvds_mode_valid( 166 struct drm_connector *connector, struct drm_display_mode *mode) 167 { 168 return MODE_OK; 169 } 170 171 static const struct 172 drm_connector_helper_funcs ge_b850v3_lvds_connector_helper_funcs = { 173 .get_modes = ge_b850v3_lvds_get_modes, 174 .mode_valid = ge_b850v3_lvds_mode_valid, 175 }; 176 177 static enum drm_connector_status ge_b850v3_lvds_detect( 178 struct drm_connector *connector, bool force) 179 { 180 struct i2c_client *stdp4028_i2c = 181 ge_b850v3_lvds_ptr->stdp4028_i2c; 182 s32 link_state; 183 184 link_state = i2c_smbus_read_word_data(stdp4028_i2c, 185 STDP4028_DPTX_STS_REG); 186 187 if (link_state == STDP4028_CON_STATE_CONNECTED) 188 return connector_status_connected; 189 190 if (link_state == 0) 191 return connector_status_disconnected; 192 193 return connector_status_unknown; 194 } 195 196 static const struct drm_connector_funcs ge_b850v3_lvds_connector_funcs = { 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_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