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_edid.h> 40 #include <drm/drm_probe_helper.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_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 .fill_modes = drm_helper_probe_single_connector_modes, 197 .detect = ge_b850v3_lvds_detect, 198 .destroy = drm_connector_cleanup, 199 .reset = drm_atomic_helper_connector_reset, 200 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 201 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 202 }; 203 204 static irqreturn_t ge_b850v3_lvds_irq_handler(int irq, void *dev_id) 205 { 206 struct i2c_client *stdp4028_i2c 207 = ge_b850v3_lvds_ptr->stdp4028_i2c; 208 209 i2c_smbus_write_word_data(stdp4028_i2c, 210 STDP4028_DPTX_IRQ_STS_REG, 211 STDP4028_DPTX_IRQ_CLEAR); 212 213 if (ge_b850v3_lvds_ptr->connector.dev) 214 drm_kms_helper_hotplug_event(ge_b850v3_lvds_ptr->connector.dev); 215 216 return IRQ_HANDLED; 217 } 218 219 static int ge_b850v3_lvds_attach(struct drm_bridge *bridge) 220 { 221 struct drm_connector *connector = &ge_b850v3_lvds_ptr->connector; 222 struct i2c_client *stdp4028_i2c 223 = ge_b850v3_lvds_ptr->stdp4028_i2c; 224 int ret; 225 226 if (!bridge->encoder) { 227 DRM_ERROR("Parent encoder object not found"); 228 return -ENODEV; 229 } 230 231 connector->polled = DRM_CONNECTOR_POLL_HPD; 232 233 drm_connector_helper_add(connector, 234 &ge_b850v3_lvds_connector_helper_funcs); 235 236 ret = drm_connector_init(bridge->dev, connector, 237 &ge_b850v3_lvds_connector_funcs, 238 DRM_MODE_CONNECTOR_DisplayPort); 239 if (ret) { 240 DRM_ERROR("Failed to initialize connector with drm\n"); 241 return ret; 242 } 243 244 ret = drm_connector_attach_encoder(connector, bridge->encoder); 245 if (ret) 246 return ret; 247 248 /* Configures the bridge to re-enable interrupts after each ack. */ 249 i2c_smbus_write_word_data(stdp4028_i2c, 250 STDP4028_IRQ_OUT_CONF_REG, 251 STDP4028_DPTX_DP_IRQ_EN); 252 253 /* Enable interrupts */ 254 i2c_smbus_write_word_data(stdp4028_i2c, 255 STDP4028_DPTX_IRQ_EN_REG, 256 STDP4028_DPTX_IRQ_CONFIG); 257 258 return 0; 259 } 260 261 static const struct drm_bridge_funcs ge_b850v3_lvds_funcs = { 262 .attach = ge_b850v3_lvds_attach, 263 }; 264 265 static int ge_b850v3_lvds_init(struct device *dev) 266 { 267 mutex_lock(&ge_b850v3_lvds_dev_mutex); 268 269 if (ge_b850v3_lvds_ptr) 270 goto success; 271 272 ge_b850v3_lvds_ptr = devm_kzalloc(dev, 273 sizeof(*ge_b850v3_lvds_ptr), 274 GFP_KERNEL); 275 276 if (!ge_b850v3_lvds_ptr) { 277 mutex_unlock(&ge_b850v3_lvds_dev_mutex); 278 return -ENOMEM; 279 } 280 281 success: 282 mutex_unlock(&ge_b850v3_lvds_dev_mutex); 283 return 0; 284 } 285 286 static void ge_b850v3_lvds_remove(void) 287 { 288 mutex_lock(&ge_b850v3_lvds_dev_mutex); 289 /* 290 * This check is to avoid both the drivers 291 * removing the bridge in their remove() function 292 */ 293 if (!ge_b850v3_lvds_ptr) 294 goto out; 295 296 drm_bridge_remove(&ge_b850v3_lvds_ptr->bridge); 297 298 kfree(ge_b850v3_lvds_ptr->edid); 299 300 ge_b850v3_lvds_ptr = NULL; 301 out: 302 mutex_unlock(&ge_b850v3_lvds_dev_mutex); 303 } 304 305 static int stdp4028_ge_b850v3_fw_probe(struct i2c_client *stdp4028_i2c, 306 const struct i2c_device_id *id) 307 { 308 struct device *dev = &stdp4028_i2c->dev; 309 310 ge_b850v3_lvds_init(dev); 311 312 ge_b850v3_lvds_ptr->stdp4028_i2c = stdp4028_i2c; 313 i2c_set_clientdata(stdp4028_i2c, ge_b850v3_lvds_ptr); 314 315 /* drm bridge initialization */ 316 ge_b850v3_lvds_ptr->bridge.funcs = &ge_b850v3_lvds_funcs; 317 ge_b850v3_lvds_ptr->bridge.of_node = dev->of_node; 318 drm_bridge_add(&ge_b850v3_lvds_ptr->bridge); 319 320 /* Clear pending interrupts since power up. */ 321 i2c_smbus_write_word_data(stdp4028_i2c, 322 STDP4028_DPTX_IRQ_STS_REG, 323 STDP4028_DPTX_IRQ_CLEAR); 324 325 if (!stdp4028_i2c->irq) 326 return 0; 327 328 return devm_request_threaded_irq(&stdp4028_i2c->dev, 329 stdp4028_i2c->irq, NULL, 330 ge_b850v3_lvds_irq_handler, 331 IRQF_TRIGGER_HIGH | IRQF_ONESHOT, 332 "ge-b850v3-lvds-dp", ge_b850v3_lvds_ptr); 333 } 334 335 static int stdp4028_ge_b850v3_fw_remove(struct i2c_client *stdp4028_i2c) 336 { 337 ge_b850v3_lvds_remove(); 338 339 return 0; 340 } 341 342 static const struct i2c_device_id stdp4028_ge_b850v3_fw_i2c_table[] = { 343 {"stdp4028_ge_fw", 0}, 344 {}, 345 }; 346 MODULE_DEVICE_TABLE(i2c, stdp4028_ge_b850v3_fw_i2c_table); 347 348 static const struct of_device_id stdp4028_ge_b850v3_fw_match[] = { 349 { .compatible = "megachips,stdp4028-ge-b850v3-fw" }, 350 {}, 351 }; 352 MODULE_DEVICE_TABLE(of, stdp4028_ge_b850v3_fw_match); 353 354 static struct i2c_driver stdp4028_ge_b850v3_fw_driver = { 355 .id_table = stdp4028_ge_b850v3_fw_i2c_table, 356 .probe = stdp4028_ge_b850v3_fw_probe, 357 .remove = stdp4028_ge_b850v3_fw_remove, 358 .driver = { 359 .name = "stdp4028-ge-b850v3-fw", 360 .of_match_table = stdp4028_ge_b850v3_fw_match, 361 }, 362 }; 363 364 static int stdp2690_ge_b850v3_fw_probe(struct i2c_client *stdp2690_i2c, 365 const struct i2c_device_id *id) 366 { 367 struct device *dev = &stdp2690_i2c->dev; 368 369 ge_b850v3_lvds_init(dev); 370 371 ge_b850v3_lvds_ptr->stdp2690_i2c = stdp2690_i2c; 372 i2c_set_clientdata(stdp2690_i2c, ge_b850v3_lvds_ptr); 373 374 return 0; 375 } 376 377 static int stdp2690_ge_b850v3_fw_remove(struct i2c_client *stdp2690_i2c) 378 { 379 ge_b850v3_lvds_remove(); 380 381 return 0; 382 } 383 384 static const struct i2c_device_id stdp2690_ge_b850v3_fw_i2c_table[] = { 385 {"stdp2690_ge_fw", 0}, 386 {}, 387 }; 388 MODULE_DEVICE_TABLE(i2c, stdp2690_ge_b850v3_fw_i2c_table); 389 390 static const struct of_device_id stdp2690_ge_b850v3_fw_match[] = { 391 { .compatible = "megachips,stdp2690-ge-b850v3-fw" }, 392 {}, 393 }; 394 MODULE_DEVICE_TABLE(of, stdp2690_ge_b850v3_fw_match); 395 396 static struct i2c_driver stdp2690_ge_b850v3_fw_driver = { 397 .id_table = stdp2690_ge_b850v3_fw_i2c_table, 398 .probe = stdp2690_ge_b850v3_fw_probe, 399 .remove = stdp2690_ge_b850v3_fw_remove, 400 .driver = { 401 .name = "stdp2690-ge-b850v3-fw", 402 .of_match_table = stdp2690_ge_b850v3_fw_match, 403 }, 404 }; 405 406 static int __init stdpxxxx_ge_b850v3_init(void) 407 { 408 int ret; 409 410 ret = i2c_add_driver(&stdp4028_ge_b850v3_fw_driver); 411 if (ret) 412 return ret; 413 414 return i2c_add_driver(&stdp2690_ge_b850v3_fw_driver); 415 } 416 module_init(stdpxxxx_ge_b850v3_init); 417 418 static void __exit stdpxxxx_ge_b850v3_exit(void) 419 { 420 i2c_del_driver(&stdp2690_ge_b850v3_fw_driver); 421 i2c_del_driver(&stdp4028_ge_b850v3_fw_driver); 422 } 423 module_exit(stdpxxxx_ge_b850v3_exit); 424 425 MODULE_AUTHOR("Peter Senna Tschudin <peter.senna@collabora.com>"); 426 MODULE_AUTHOR("Martyn Welch <martyn.welch@collabora.co.uk>"); 427 MODULE_DESCRIPTION("GE LVDS to DP++ display bridge)"); 428 MODULE_LICENSE("GPL v2"); 429