| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * DisplayPort CEC-Tunneling-over-AUX support |
| 4 | * |
| 5 | * Copyright 2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/export.h> |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/slab.h> |
| 12 | |
| 13 | #include <media/cec.h> |
| 14 | |
| 15 | #include <drm/display/drm_dp_helper.h> |
| 16 | #include <drm/drm_connector.h> |
| 17 | #include <drm/drm_device.h> |
| 18 | #include <drm/drm_edid.h> |
| 19 | |
| 20 | /* |
| 21 | * Unfortunately it turns out that we have a chicken-and-egg situation |
| 22 | * here. Quite a few active (mini-)DP-to-HDMI or USB-C-to-HDMI adapters |
| 23 | * have a converter chip that supports CEC-Tunneling-over-AUX (usually the |
| 24 | * Parade PS176), but they do not wire up the CEC pin, thus making CEC |
| 25 | * useless. Note that MegaChips 2900-based adapters appear to have good |
| 26 | * support for CEC tunneling. Those adapters that I have tested using |
| 27 | * this chipset all have the CEC line connected. |
| 28 | * |
| 29 | * Sadly there is no way for this driver to know this. What happens is |
| 30 | * that a /dev/cecX device is created that is isolated and unable to see |
| 31 | * any of the other CEC devices. Quite literally the CEC wire is cut |
| 32 | * (or in this case, never connected in the first place). |
| 33 | * |
| 34 | * The reason so few adapters support this is that this tunneling protocol |
| 35 | * was never supported by any OS. So there was no easy way of testing it, |
| 36 | * and no incentive to correctly wire up the CEC pin. |
| 37 | * |
| 38 | * Hopefully by creating this driver it will be easier for vendors to |
| 39 | * finally fix their adapters and test the CEC functionality. |
| 40 | * |
| 41 | * I keep a list of known working adapters here: |
| 42 | * |
| 43 | * https://hverkuil.home.xs4all.nl/cec-status.txt |
| 44 | * |
| 45 | * Please mail me (hverkuil@kernel.org) if you find an adapter that works |
| 46 | * and is not yet listed there. |
| 47 | * |
| 48 | * Note that the current implementation does not support CEC over an MST hub. |
| 49 | * As far as I can see there is no mechanism defined in the DisplayPort |
| 50 | * standard to transport CEC interrupts over an MST device. It might be |
| 51 | * possible to do this through polling, but I have not been able to get that |
| 52 | * to work. |
| 53 | */ |
| 54 | |
| 55 | /** |
| 56 | * DOC: dp cec helpers |
| 57 | * |
| 58 | * These functions take care of supporting the CEC-Tunneling-over-AUX |
| 59 | * feature of DisplayPort-to-HDMI adapters. |
| 60 | */ |
| 61 | |
| 62 | /* |
| 63 | * When the EDID is unset because the HPD went low, then the CEC DPCD registers |
| 64 | * typically can no longer be read (true for a DP-to-HDMI adapter since it is |
| 65 | * powered by the HPD). However, some displays toggle the HPD off and on for a |
| 66 | * short period for one reason or another, and that would cause the CEC adapter |
| 67 | * to be removed and added again, even though nothing else changed. |
| 68 | * |
| 69 | * This module parameter sets a delay in seconds before the CEC adapter is |
| 70 | * actually unregistered. Only if the HPD does not return within that time will |
| 71 | * the CEC adapter be unregistered. |
| 72 | * |
| 73 | * If it is set to a value >= NEVER_UNREG_DELAY, then the CEC adapter will never |
| 74 | * be unregistered for as long as the connector remains registered. |
| 75 | * |
| 76 | * If it is set to 0, then the CEC adapter will be unregistered immediately as |
| 77 | * soon as the HPD disappears. |
| 78 | * |
| 79 | * The default is one second to prevent short HPD glitches from unregistering |
| 80 | * the CEC adapter. |
| 81 | * |
| 82 | * Note that for integrated HDMI branch devices that support CEC the DPCD |
| 83 | * registers remain available even if the HPD goes low since it is not powered |
| 84 | * by the HPD. In that case the CEC adapter will never be unregistered during |
| 85 | * the life time of the connector. At least, this is the theory since I do not |
| 86 | * have hardware with an integrated HDMI branch device that supports CEC. |
| 87 | */ |
| 88 | #define NEVER_UNREG_DELAY 1000 |
| 89 | static unsigned int drm_dp_cec_unregister_delay = 1; |
| 90 | module_param(drm_dp_cec_unregister_delay, uint, 0600); |
| 91 | MODULE_PARM_DESC(drm_dp_cec_unregister_delay, |
| 92 | "CEC unregister delay in seconds, 0: no delay, >= 1000: never unregister" ); |
| 93 | |
| 94 | static int drm_dp_cec_adap_enable(struct cec_adapter *adap, bool enable) |
| 95 | { |
| 96 | struct drm_dp_aux *aux = cec_get_drvdata(adap); |
| 97 | u32 val = enable ? DP_CEC_TUNNELING_ENABLE : 0; |
| 98 | ssize_t err = 0; |
| 99 | |
| 100 | err = drm_dp_dpcd_write_byte(aux, DP_CEC_TUNNELING_CONTROL, value: val); |
| 101 | return (enable && err < 0) ? err : 0; |
| 102 | } |
| 103 | |
| 104 | static int drm_dp_cec_adap_log_addr(struct cec_adapter *adap, u8 addr) |
| 105 | { |
| 106 | struct drm_dp_aux *aux = cec_get_drvdata(adap); |
| 107 | /* Bit 15 (logical address 15) should always be set */ |
| 108 | u16 la_mask = 1 << CEC_LOG_ADDR_BROADCAST; |
| 109 | u8 mask[2]; |
| 110 | ssize_t err; |
| 111 | |
| 112 | if (addr != CEC_LOG_ADDR_INVALID) |
| 113 | la_mask |= adap->log_addrs.log_addr_mask | (1 << addr); |
| 114 | mask[0] = la_mask & 0xff; |
| 115 | mask[1] = la_mask >> 8; |
| 116 | err = drm_dp_dpcd_write_data(aux, DP_CEC_LOGICAL_ADDRESS_MASK, buffer: mask, size: 2); |
| 117 | return (addr != CEC_LOG_ADDR_INVALID && err < 0) ? err : 0; |
| 118 | } |
| 119 | |
| 120 | static int drm_dp_cec_adap_transmit(struct cec_adapter *adap, u8 attempts, |
| 121 | u32 signal_free_time, struct cec_msg *msg) |
| 122 | { |
| 123 | struct drm_dp_aux *aux = cec_get_drvdata(adap); |
| 124 | unsigned int retries = min(5, attempts - 1); |
| 125 | ssize_t err; |
| 126 | |
| 127 | err = drm_dp_dpcd_write_data(aux, DP_CEC_TX_MESSAGE_BUFFER, |
| 128 | buffer: msg->msg, size: msg->len); |
| 129 | if (err < 0) |
| 130 | return err; |
| 131 | |
| 132 | return drm_dp_dpcd_write_byte(aux, DP_CEC_TX_MESSAGE_INFO, |
| 133 | value: (msg->len - 1) | (retries << 4) | |
| 134 | DP_CEC_TX_MESSAGE_SEND); |
| 135 | } |
| 136 | |
| 137 | static int drm_dp_cec_adap_monitor_all_enable(struct cec_adapter *adap, |
| 138 | bool enable) |
| 139 | { |
| 140 | struct drm_dp_aux *aux = cec_get_drvdata(adap); |
| 141 | ssize_t err; |
| 142 | u8 val; |
| 143 | |
| 144 | if (!(adap->capabilities & CEC_CAP_MONITOR_ALL)) |
| 145 | return 0; |
| 146 | |
| 147 | err = drm_dp_dpcd_read_byte(aux, DP_CEC_TUNNELING_CONTROL, valuep: &val); |
| 148 | if (!err) { |
| 149 | if (enable) |
| 150 | val |= DP_CEC_SNOOPING_ENABLE; |
| 151 | else |
| 152 | val &= ~DP_CEC_SNOOPING_ENABLE; |
| 153 | err = drm_dp_dpcd_write_byte(aux, DP_CEC_TUNNELING_CONTROL, value: val); |
| 154 | } |
| 155 | return (enable && err < 0) ? err : 0; |
| 156 | } |
| 157 | |
| 158 | static void drm_dp_cec_adap_status(struct cec_adapter *adap, |
| 159 | struct seq_file *file) |
| 160 | { |
| 161 | struct drm_dp_aux *aux = cec_get_drvdata(adap); |
| 162 | struct drm_dp_desc desc; |
| 163 | struct drm_dp_dpcd_ident *id = &desc.ident; |
| 164 | |
| 165 | if (drm_dp_read_desc(aux, desc: &desc, is_branch: true)) |
| 166 | return; |
| 167 | seq_printf(m: file, fmt: "OUI: %*phD\n" , |
| 168 | (int)sizeof(id->oui), id->oui); |
| 169 | seq_printf(m: file, fmt: "ID: %*pE\n" , |
| 170 | (int)strnlen(p: id->device_id, maxlen: sizeof(id->device_id)), |
| 171 | id->device_id); |
| 172 | seq_printf(m: file, fmt: "HW Rev: %d.%d\n" , id->hw_rev >> 4, id->hw_rev & 0xf); |
| 173 | /* |
| 174 | * Show this both in decimal and hex: at least one vendor |
| 175 | * always reports this in hex. |
| 176 | */ |
| 177 | seq_printf(m: file, fmt: "FW/SW Rev: %d.%d (0x%02x.0x%02x)\n" , |
| 178 | id->sw_major_rev, id->sw_minor_rev, |
| 179 | id->sw_major_rev, id->sw_minor_rev); |
| 180 | } |
| 181 | |
| 182 | static const struct cec_adap_ops drm_dp_cec_adap_ops = { |
| 183 | .adap_enable = drm_dp_cec_adap_enable, |
| 184 | .adap_log_addr = drm_dp_cec_adap_log_addr, |
| 185 | .adap_transmit = drm_dp_cec_adap_transmit, |
| 186 | .adap_monitor_all_enable = drm_dp_cec_adap_monitor_all_enable, |
| 187 | .adap_status = drm_dp_cec_adap_status, |
| 188 | }; |
| 189 | |
| 190 | static int drm_dp_cec_received(struct drm_dp_aux *aux) |
| 191 | { |
| 192 | struct cec_adapter *adap = aux->cec.adap; |
| 193 | struct cec_msg msg; |
| 194 | u8 rx_msg_info; |
| 195 | ssize_t err; |
| 196 | |
| 197 | err = drm_dp_dpcd_read_byte(aux, DP_CEC_RX_MESSAGE_INFO, valuep: &rx_msg_info); |
| 198 | if (err < 0) |
| 199 | return err; |
| 200 | |
| 201 | if (!(rx_msg_info & DP_CEC_RX_MESSAGE_ENDED)) |
| 202 | return 0; |
| 203 | |
| 204 | msg.len = (rx_msg_info & DP_CEC_RX_MESSAGE_LEN_MASK) + 1; |
| 205 | err = drm_dp_dpcd_read_data(aux, DP_CEC_RX_MESSAGE_BUFFER, buffer: msg.msg, size: msg.len); |
| 206 | if (err < 0) |
| 207 | return err; |
| 208 | |
| 209 | cec_received_msg(adap, msg: &msg); |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | static void drm_dp_cec_handle_irq(struct drm_dp_aux *aux) |
| 214 | { |
| 215 | struct cec_adapter *adap = aux->cec.adap; |
| 216 | u8 flags; |
| 217 | |
| 218 | if (drm_dp_dpcd_read_byte(aux, DP_CEC_TUNNELING_IRQ_FLAGS, valuep: &flags) < 0) |
| 219 | return; |
| 220 | |
| 221 | if (flags & DP_CEC_RX_MESSAGE_INFO_VALID) |
| 222 | drm_dp_cec_received(aux); |
| 223 | |
| 224 | if (flags & DP_CEC_TX_MESSAGE_SENT) |
| 225 | cec_transmit_attempt_done(adap, CEC_TX_STATUS_OK); |
| 226 | else if (flags & DP_CEC_TX_LINE_ERROR) |
| 227 | cec_transmit_attempt_done(adap, CEC_TX_STATUS_ERROR | |
| 228 | CEC_TX_STATUS_MAX_RETRIES); |
| 229 | else if (flags & |
| 230 | (DP_CEC_TX_ADDRESS_NACK_ERROR | DP_CEC_TX_DATA_NACK_ERROR)) |
| 231 | cec_transmit_attempt_done(adap, CEC_TX_STATUS_NACK | |
| 232 | CEC_TX_STATUS_MAX_RETRIES); |
| 233 | drm_dp_dpcd_write_byte(aux, DP_CEC_TUNNELING_IRQ_FLAGS, value: flags); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * drm_dp_cec_irq() - handle CEC interrupt, if any |
| 238 | * @aux: DisplayPort AUX channel |
| 239 | * |
| 240 | * Should be called when handling an IRQ_HPD request. If CEC-tunneling-over-AUX |
| 241 | * is present, then it will check for a CEC_IRQ and handle it accordingly. |
| 242 | */ |
| 243 | void drm_dp_cec_irq(struct drm_dp_aux *aux) |
| 244 | { |
| 245 | u8 cec_irq; |
| 246 | int ret; |
| 247 | |
| 248 | /* No transfer function was set, so not a DP connector */ |
| 249 | if (!aux->transfer) |
| 250 | return; |
| 251 | |
| 252 | mutex_lock(&aux->cec.lock); |
| 253 | if (!aux->cec.adap) |
| 254 | goto unlock; |
| 255 | |
| 256 | ret = drm_dp_dpcd_read_byte(aux, DP_DEVICE_SERVICE_IRQ_VECTOR_ESI1, |
| 257 | valuep: &cec_irq); |
| 258 | if (ret < 0 || !(cec_irq & DP_CEC_IRQ)) |
| 259 | goto unlock; |
| 260 | |
| 261 | drm_dp_cec_handle_irq(aux); |
| 262 | drm_dp_dpcd_write_byte(aux, DP_DEVICE_SERVICE_IRQ_VECTOR_ESI1, DP_CEC_IRQ); |
| 263 | unlock: |
| 264 | mutex_unlock(lock: &aux->cec.lock); |
| 265 | } |
| 266 | EXPORT_SYMBOL(drm_dp_cec_irq); |
| 267 | |
| 268 | static bool drm_dp_cec_cap(struct drm_dp_aux *aux, u8 *cec_cap) |
| 269 | { |
| 270 | u8 cap = 0; |
| 271 | |
| 272 | if (drm_dp_dpcd_read_byte(aux, DP_CEC_TUNNELING_CAPABILITY, valuep: &cap) < 0 || |
| 273 | !(cap & DP_CEC_TUNNELING_CAPABLE)) |
| 274 | return false; |
| 275 | if (cec_cap) |
| 276 | *cec_cap = cap; |
| 277 | return true; |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * Called if the HPD was low for more than drm_dp_cec_unregister_delay |
| 282 | * seconds. This unregisters the CEC adapter. |
| 283 | */ |
| 284 | static void drm_dp_cec_unregister_work(struct work_struct *work) |
| 285 | { |
| 286 | struct drm_dp_aux *aux = container_of(work, struct drm_dp_aux, |
| 287 | cec.unregister_work.work); |
| 288 | |
| 289 | mutex_lock(&aux->cec.lock); |
| 290 | cec_unregister_adapter(adap: aux->cec.adap); |
| 291 | aux->cec.adap = NULL; |
| 292 | mutex_unlock(lock: &aux->cec.lock); |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | * A new EDID is set. If there is no CEC adapter, then create one. If |
| 297 | * there was a CEC adapter, then check if the CEC adapter properties |
| 298 | * were unchanged and just update the CEC physical address. Otherwise |
| 299 | * unregister the old CEC adapter and create a new one. |
| 300 | */ |
| 301 | void drm_dp_cec_attach(struct drm_dp_aux *aux, u16 source_physical_address) |
| 302 | { |
| 303 | struct drm_connector *connector = aux->cec.connector; |
| 304 | u32 cec_caps = CEC_CAP_DEFAULTS | CEC_CAP_NEEDS_HPD | |
| 305 | CEC_CAP_CONNECTOR_INFO; |
| 306 | struct cec_connector_info conn_info; |
| 307 | unsigned int num_las = 1; |
| 308 | u8 cap; |
| 309 | |
| 310 | /* No transfer function was set, so not a DP connector */ |
| 311 | if (!aux->transfer) |
| 312 | return; |
| 313 | |
| 314 | cancel_delayed_work_sync(dwork: &aux->cec.unregister_work); |
| 315 | |
| 316 | mutex_lock(&aux->cec.lock); |
| 317 | if (!drm_dp_cec_cap(aux, cec_cap: &cap)) { |
| 318 | /* CEC is not supported, unregister any existing adapter */ |
| 319 | cec_unregister_adapter(adap: aux->cec.adap); |
| 320 | aux->cec.adap = NULL; |
| 321 | goto unlock; |
| 322 | } |
| 323 | |
| 324 | if (cap & DP_CEC_SNOOPING_CAPABLE) |
| 325 | cec_caps |= CEC_CAP_MONITOR_ALL; |
| 326 | if (cap & DP_CEC_MULTIPLE_LA_CAPABLE) |
| 327 | num_las = CEC_MAX_LOG_ADDRS; |
| 328 | |
| 329 | if (aux->cec.adap) { |
| 330 | /* Check if the adapter properties have changed */ |
| 331 | if ((aux->cec.adap->capabilities & CEC_CAP_MONITOR_ALL) == |
| 332 | (cec_caps & CEC_CAP_MONITOR_ALL) && |
| 333 | aux->cec.adap->available_log_addrs == num_las) { |
| 334 | /* Unchanged, so just set the phys addr */ |
| 335 | cec_s_phys_addr(adap: aux->cec.adap, phys_addr: source_physical_address, block: false); |
| 336 | goto unlock; |
| 337 | } |
| 338 | /* |
| 339 | * The capabilities changed, so unregister the old |
| 340 | * adapter first. |
| 341 | */ |
| 342 | cec_unregister_adapter(adap: aux->cec.adap); |
| 343 | } |
| 344 | |
| 345 | /* Create a new adapter */ |
| 346 | aux->cec.adap = cec_allocate_adapter(ops: &drm_dp_cec_adap_ops, |
| 347 | priv: aux, name: connector->name, caps: cec_caps, |
| 348 | available_las: num_las); |
| 349 | if (IS_ERR(ptr: aux->cec.adap)) { |
| 350 | aux->cec.adap = NULL; |
| 351 | goto unlock; |
| 352 | } |
| 353 | |
| 354 | cec_fill_conn_info_from_drm(conn_info: &conn_info, connector); |
| 355 | cec_s_conn_info(adap: aux->cec.adap, conn_info: &conn_info); |
| 356 | |
| 357 | if (cec_register_adapter(adap: aux->cec.adap, parent: connector->dev->dev)) { |
| 358 | cec_delete_adapter(adap: aux->cec.adap); |
| 359 | aux->cec.adap = NULL; |
| 360 | } else { |
| 361 | /* |
| 362 | * Update the phys addr for the new CEC adapter. When called |
| 363 | * from drm_dp_cec_register_connector() edid == NULL, so in |
| 364 | * that case the phys addr is just invalidated. |
| 365 | */ |
| 366 | cec_s_phys_addr(adap: aux->cec.adap, phys_addr: source_physical_address, block: false); |
| 367 | } |
| 368 | unlock: |
| 369 | mutex_unlock(lock: &aux->cec.lock); |
| 370 | } |
| 371 | EXPORT_SYMBOL(drm_dp_cec_attach); |
| 372 | |
| 373 | /* |
| 374 | * Note: Prefer calling drm_dp_cec_attach() with |
| 375 | * connector->display_info.source_physical_address if possible. |
| 376 | */ |
| 377 | void drm_dp_cec_set_edid(struct drm_dp_aux *aux, const struct edid *edid) |
| 378 | { |
| 379 | u16 pa = CEC_PHYS_ADDR_INVALID; |
| 380 | |
| 381 | if (edid && edid->extensions) |
| 382 | pa = cec_get_edid_phys_addr(edid: (const u8 *)edid, |
| 383 | EDID_LENGTH * (edid->extensions + 1), NULL); |
| 384 | |
| 385 | drm_dp_cec_attach(aux, pa); |
| 386 | } |
| 387 | EXPORT_SYMBOL(drm_dp_cec_set_edid); |
| 388 | |
| 389 | /* |
| 390 | * The EDID disappeared (likely because of the HPD going down). |
| 391 | */ |
| 392 | void drm_dp_cec_unset_edid(struct drm_dp_aux *aux) |
| 393 | { |
| 394 | /* No transfer function was set, so not a DP connector */ |
| 395 | if (!aux->transfer) |
| 396 | return; |
| 397 | |
| 398 | cancel_delayed_work_sync(dwork: &aux->cec.unregister_work); |
| 399 | |
| 400 | mutex_lock(&aux->cec.lock); |
| 401 | if (!aux->cec.adap) |
| 402 | goto unlock; |
| 403 | |
| 404 | cec_phys_addr_invalidate(adap: aux->cec.adap); |
| 405 | /* |
| 406 | * We're done if we want to keep the CEC device |
| 407 | * (drm_dp_cec_unregister_delay is >= NEVER_UNREG_DELAY) or if the |
| 408 | * DPCD still indicates the CEC capability (expected for an integrated |
| 409 | * HDMI branch device). |
| 410 | */ |
| 411 | if (drm_dp_cec_unregister_delay < NEVER_UNREG_DELAY && |
| 412 | !drm_dp_cec_cap(aux, NULL)) { |
| 413 | /* |
| 414 | * Unregister the CEC adapter after drm_dp_cec_unregister_delay |
| 415 | * seconds. This to debounce short HPD off-and-on cycles from |
| 416 | * displays. |
| 417 | */ |
| 418 | schedule_delayed_work(dwork: &aux->cec.unregister_work, |
| 419 | delay: drm_dp_cec_unregister_delay * HZ); |
| 420 | } |
| 421 | unlock: |
| 422 | mutex_unlock(lock: &aux->cec.lock); |
| 423 | } |
| 424 | EXPORT_SYMBOL(drm_dp_cec_unset_edid); |
| 425 | |
| 426 | /** |
| 427 | * drm_dp_cec_register_connector() - register a new connector |
| 428 | * @aux: DisplayPort AUX channel |
| 429 | * @connector: drm connector |
| 430 | * |
| 431 | * A new connector was registered with associated CEC adapter name and |
| 432 | * CEC adapter parent device. After registering the name and parent |
| 433 | * drm_dp_cec_set_edid() is called to check if the connector supports |
| 434 | * CEC and to register a CEC adapter if that is the case. |
| 435 | */ |
| 436 | void drm_dp_cec_register_connector(struct drm_dp_aux *aux, |
| 437 | struct drm_connector *connector) |
| 438 | { |
| 439 | WARN_ON(aux->cec.adap); |
| 440 | if (WARN_ON(!aux->transfer)) |
| 441 | return; |
| 442 | aux->cec.connector = connector; |
| 443 | INIT_DELAYED_WORK(&aux->cec.unregister_work, |
| 444 | drm_dp_cec_unregister_work); |
| 445 | } |
| 446 | EXPORT_SYMBOL(drm_dp_cec_register_connector); |
| 447 | |
| 448 | /** |
| 449 | * drm_dp_cec_unregister_connector() - unregister the CEC adapter, if any |
| 450 | * @aux: DisplayPort AUX channel |
| 451 | */ |
| 452 | void drm_dp_cec_unregister_connector(struct drm_dp_aux *aux) |
| 453 | { |
| 454 | if (!aux->cec.adap) |
| 455 | return; |
| 456 | cancel_delayed_work_sync(dwork: &aux->cec.unregister_work); |
| 457 | cec_unregister_adapter(adap: aux->cec.adap); |
| 458 | aux->cec.adap = NULL; |
| 459 | } |
| 460 | EXPORT_SYMBOL(drm_dp_cec_unregister_connector); |
| 461 | |