1 // SPDX-License-Identifier: GPL-2.0
3 * DisplayPort CEC-Tunneling-over-AUX support
5 * Copyright 2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <drm/drm_dp_helper.h>
12 #include <media/cec.h>
15 * Unfortunately it turns out that we have a chicken-and-egg situation
16 * here. Quite a few active (mini-)DP-to-HDMI or USB-C-to-HDMI adapters
17 * have a converter chip that supports CEC-Tunneling-over-AUX (usually the
18 * Parade PS176), but they do not wire up the CEC pin, thus making CEC
19 * useless. Note that MegaChips 2900-based adapters appear to have good
20 * support for CEC tunneling. Those adapters that I have tested using
21 * this chipset all have the CEC line connected.
23 * Sadly there is no way for this driver to know this. What happens is
24 * that a /dev/cecX device is created that is isolated and unable to see
25 * any of the other CEC devices. Quite literally the CEC wire is cut
26 * (or in this case, never connected in the first place).
28 * The reason so few adapters support this is that this tunneling protocol
29 * was never supported by any OS. So there was no easy way of testing it,
30 * and no incentive to correctly wire up the CEC pin.
32 * Hopefully by creating this driver it will be easier for vendors to
33 * finally fix their adapters and test the CEC functionality.
35 * I keep a list of known working adapters here:
37 * https://hverkuil.home.xs4all.nl/cec-status.txt
39 * Please mail me (hverkuil@xs4all.nl) if you find an adapter that works
40 * and is not yet listed there.
42 * Note that the current implementation does not support CEC over an MST hub.
43 * As far as I can see there is no mechanism defined in the DisplayPort
44 * standard to transport CEC interrupts over an MST device. It might be
45 * possible to do this through polling, but I have not been able to get that
52 * These functions take care of supporting the CEC-Tunneling-over-AUX
53 * feature of DisplayPort-to-HDMI adapters.
57 * When the EDID is unset because the HPD went low, then the CEC DPCD registers
58 * typically can no longer be read (true for a DP-to-HDMI adapter since it is
59 * powered by the HPD). However, some displays toggle the HPD off and on for a
60 * short period for one reason or another, and that would cause the CEC adapter
61 * to be removed and added again, even though nothing else changed.
63 * This module parameter sets a delay in seconds before the CEC adapter is
64 * actually unregistered. Only if the HPD does not return within that time will
65 * the CEC adapter be unregistered.
67 * If it is set to a value >= NEVER_UNREG_DELAY, then the CEC adapter will never
68 * be unregistered for as long as the connector remains registered.
70 * If it is set to 0, then the CEC adapter will be unregistered immediately as
71 * soon as the HPD disappears.
73 * The default is one second to prevent short HPD glitches from unregistering
76 * Note that for integrated HDMI branch devices that support CEC the DPCD
77 * registers remain available even if the HPD goes low since it is not powered
78 * by the HPD. In that case the CEC adapter will never be unregistered during
79 * the life time of the connector. At least, this is the theory since I do not
80 * have hardware with an integrated HDMI branch device that supports CEC.
82 #define NEVER_UNREG_DELAY 1000
83 static unsigned int drm_dp_cec_unregister_delay
= 1;
84 module_param(drm_dp_cec_unregister_delay
, uint
, 0600);
85 MODULE_PARM_DESC(drm_dp_cec_unregister_delay
,
86 "CEC unregister delay in seconds, 0: no delay, >= 1000: never unregister");
88 static int drm_dp_cec_adap_enable(struct cec_adapter
*adap
, bool enable
)
90 struct drm_dp_aux
*aux
= cec_get_drvdata(adap
);
91 u32 val
= enable
? DP_CEC_TUNNELING_ENABLE
: 0;
94 err
= drm_dp_dpcd_writeb(aux
, DP_CEC_TUNNELING_CONTROL
, val
);
95 return (enable
&& err
< 0) ? err
: 0;
98 static int drm_dp_cec_adap_log_addr(struct cec_adapter
*adap
, u8 addr
)
100 struct drm_dp_aux
*aux
= cec_get_drvdata(adap
);
101 /* Bit 15 (logical address 15) should always be set */
102 u16 la_mask
= 1 << CEC_LOG_ADDR_BROADCAST
;
106 if (addr
!= CEC_LOG_ADDR_INVALID
)
107 la_mask
|= adap
->log_addrs
.log_addr_mask
| (1 << addr
);
108 mask
[0] = la_mask
& 0xff;
109 mask
[1] = la_mask
>> 8;
110 err
= drm_dp_dpcd_write(aux
, DP_CEC_LOGICAL_ADDRESS_MASK
, mask
, 2);
111 return (addr
!= CEC_LOG_ADDR_INVALID
&& err
< 0) ? err
: 0;
114 static int drm_dp_cec_adap_transmit(struct cec_adapter
*adap
, u8 attempts
,
115 u32 signal_free_time
, struct cec_msg
*msg
)
117 struct drm_dp_aux
*aux
= cec_get_drvdata(adap
);
118 unsigned int retries
= min(5, attempts
- 1);
121 err
= drm_dp_dpcd_write(aux
, DP_CEC_TX_MESSAGE_BUFFER
,
126 err
= drm_dp_dpcd_writeb(aux
, DP_CEC_TX_MESSAGE_INFO
,
127 (msg
->len
- 1) | (retries
<< 4) |
128 DP_CEC_TX_MESSAGE_SEND
);
129 return err
< 0 ? err
: 0;
132 static int drm_dp_cec_adap_monitor_all_enable(struct cec_adapter
*adap
,
135 struct drm_dp_aux
*aux
= cec_get_drvdata(adap
);
139 if (!(adap
->capabilities
& CEC_CAP_MONITOR_ALL
))
142 err
= drm_dp_dpcd_readb(aux
, DP_CEC_TUNNELING_CONTROL
, &val
);
145 val
|= DP_CEC_SNOOPING_ENABLE
;
147 val
&= ~DP_CEC_SNOOPING_ENABLE
;
148 err
= drm_dp_dpcd_writeb(aux
, DP_CEC_TUNNELING_CONTROL
, val
);
150 return (enable
&& err
< 0) ? err
: 0;
153 static void drm_dp_cec_adap_status(struct cec_adapter
*adap
,
154 struct seq_file
*file
)
156 struct drm_dp_aux
*aux
= cec_get_drvdata(adap
);
157 struct drm_dp_desc desc
;
158 struct drm_dp_dpcd_ident
*id
= &desc
.ident
;
160 if (drm_dp_read_desc(aux
, &desc
, true))
162 seq_printf(file
, "OUI: %*phD\n",
163 (int)sizeof(id
->oui
), id
->oui
);
164 seq_printf(file
, "ID: %*pE\n",
165 (int)strnlen(id
->device_id
, sizeof(id
->device_id
)),
167 seq_printf(file
, "HW Rev: %d.%d\n", id
->hw_rev
>> 4, id
->hw_rev
& 0xf);
169 * Show this both in decimal and hex: at least one vendor
170 * always reports this in hex.
172 seq_printf(file
, "FW/SW Rev: %d.%d (0x%02x.0x%02x)\n",
173 id
->sw_major_rev
, id
->sw_minor_rev
,
174 id
->sw_major_rev
, id
->sw_minor_rev
);
177 static const struct cec_adap_ops drm_dp_cec_adap_ops
= {
178 .adap_enable
= drm_dp_cec_adap_enable
,
179 .adap_log_addr
= drm_dp_cec_adap_log_addr
,
180 .adap_transmit
= drm_dp_cec_adap_transmit
,
181 .adap_monitor_all_enable
= drm_dp_cec_adap_monitor_all_enable
,
182 .adap_status
= drm_dp_cec_adap_status
,
185 static int drm_dp_cec_received(struct drm_dp_aux
*aux
)
187 struct cec_adapter
*adap
= aux
->cec
.adap
;
192 err
= drm_dp_dpcd_readb(aux
, DP_CEC_RX_MESSAGE_INFO
, &rx_msg_info
);
196 if (!(rx_msg_info
& DP_CEC_RX_MESSAGE_ENDED
))
199 msg
.len
= (rx_msg_info
& DP_CEC_RX_MESSAGE_LEN_MASK
) + 1;
200 err
= drm_dp_dpcd_read(aux
, DP_CEC_RX_MESSAGE_BUFFER
, msg
.msg
, msg
.len
);
204 cec_received_msg(adap
, &msg
);
208 static void drm_dp_cec_handle_irq(struct drm_dp_aux
*aux
)
210 struct cec_adapter
*adap
= aux
->cec
.adap
;
213 if (drm_dp_dpcd_readb(aux
, DP_CEC_TUNNELING_IRQ_FLAGS
, &flags
) < 0)
216 if (flags
& DP_CEC_RX_MESSAGE_INFO_VALID
)
217 drm_dp_cec_received(aux
);
219 if (flags
& DP_CEC_TX_MESSAGE_SENT
)
220 cec_transmit_attempt_done(adap
, CEC_TX_STATUS_OK
);
221 else if (flags
& DP_CEC_TX_LINE_ERROR
)
222 cec_transmit_attempt_done(adap
, CEC_TX_STATUS_ERROR
|
223 CEC_TX_STATUS_MAX_RETRIES
);
225 (DP_CEC_TX_ADDRESS_NACK_ERROR
| DP_CEC_TX_DATA_NACK_ERROR
))
226 cec_transmit_attempt_done(adap
, CEC_TX_STATUS_NACK
|
227 CEC_TX_STATUS_MAX_RETRIES
);
228 drm_dp_dpcd_writeb(aux
, DP_CEC_TUNNELING_IRQ_FLAGS
, flags
);
232 * drm_dp_cec_irq() - handle CEC interrupt, if any
233 * @aux: DisplayPort AUX channel
235 * Should be called when handling an IRQ_HPD request. If CEC-tunneling-over-AUX
236 * is present, then it will check for a CEC_IRQ and handle it accordingly.
238 void drm_dp_cec_irq(struct drm_dp_aux
*aux
)
243 /* No transfer function was set, so not a DP connector */
247 mutex_lock(&aux
->cec
.lock
);
251 ret
= drm_dp_dpcd_readb(aux
, DP_DEVICE_SERVICE_IRQ_VECTOR_ESI1
,
253 if (ret
< 0 || !(cec_irq
& DP_CEC_IRQ
))
256 drm_dp_cec_handle_irq(aux
);
257 drm_dp_dpcd_writeb(aux
, DP_DEVICE_SERVICE_IRQ_VECTOR_ESI1
, DP_CEC_IRQ
);
259 mutex_unlock(&aux
->cec
.lock
);
261 EXPORT_SYMBOL(drm_dp_cec_irq
);
263 static bool drm_dp_cec_cap(struct drm_dp_aux
*aux
, u8
*cec_cap
)
267 if (drm_dp_dpcd_readb(aux
, DP_CEC_TUNNELING_CAPABILITY
, &cap
) != 1 ||
268 !(cap
& DP_CEC_TUNNELING_CAPABLE
))
276 * Called if the HPD was low for more than drm_dp_cec_unregister_delay
277 * seconds. This unregisters the CEC adapter.
279 static void drm_dp_cec_unregister_work(struct work_struct
*work
)
281 struct drm_dp_aux
*aux
= container_of(work
, struct drm_dp_aux
,
282 cec
.unregister_work
.work
);
284 mutex_lock(&aux
->cec
.lock
);
285 cec_unregister_adapter(aux
->cec
.adap
);
286 aux
->cec
.adap
= NULL
;
287 mutex_unlock(&aux
->cec
.lock
);
291 * A new EDID is set. If there is no CEC adapter, then create one. If
292 * there was a CEC adapter, then check if the CEC adapter properties
293 * were unchanged and just update the CEC physical address. Otherwise
294 * unregister the old CEC adapter and create a new one.
296 void drm_dp_cec_set_edid(struct drm_dp_aux
*aux
, const struct edid
*edid
)
298 u32 cec_caps
= CEC_CAP_DEFAULTS
| CEC_CAP_NEEDS_HPD
;
299 unsigned int num_las
= 1;
302 /* No transfer function was set, so not a DP connector */
306 #ifndef CONFIG_MEDIA_CEC_RC
308 * CEC_CAP_RC is part of CEC_CAP_DEFAULTS, but it is stripped by
309 * cec_allocate_adapter() if CONFIG_MEDIA_CEC_RC is undefined.
311 * Do this here as well to ensure the tests against cec_caps are
314 cec_caps
&= ~CEC_CAP_RC
;
316 cancel_delayed_work_sync(&aux
->cec
.unregister_work
);
318 mutex_lock(&aux
->cec
.lock
);
319 if (!drm_dp_cec_cap(aux
, &cap
)) {
320 /* CEC is not supported, unregister any existing adapter */
321 cec_unregister_adapter(aux
->cec
.adap
);
322 aux
->cec
.adap
= NULL
;
326 if (cap
& DP_CEC_SNOOPING_CAPABLE
)
327 cec_caps
|= CEC_CAP_MONITOR_ALL
;
328 if (cap
& DP_CEC_MULTIPLE_LA_CAPABLE
)
329 num_las
= CEC_MAX_LOG_ADDRS
;
332 if (aux
->cec
.adap
->capabilities
== cec_caps
&&
333 aux
->cec
.adap
->available_log_addrs
== num_las
) {
334 /* Unchanged, so just set the phys addr */
335 cec_s_phys_addr_from_edid(aux
->cec
.adap
, edid
);
339 * The capabilities changed, so unregister the old
342 cec_unregister_adapter(aux
->cec
.adap
);
345 /* Create a new adapter */
346 aux
->cec
.adap
= cec_allocate_adapter(&drm_dp_cec_adap_ops
,
347 aux
, aux
->cec
.name
, cec_caps
,
349 if (IS_ERR(aux
->cec
.adap
)) {
350 aux
->cec
.adap
= NULL
;
353 if (cec_register_adapter(aux
->cec
.adap
, aux
->cec
.parent
)) {
354 cec_delete_adapter(aux
->cec
.adap
);
355 aux
->cec
.adap
= NULL
;
358 * Update the phys addr for the new CEC adapter. When called
359 * from drm_dp_cec_register_connector() edid == NULL, so in
360 * that case the phys addr is just invalidated.
362 cec_s_phys_addr_from_edid(aux
->cec
.adap
, edid
);
365 mutex_unlock(&aux
->cec
.lock
);
367 EXPORT_SYMBOL(drm_dp_cec_set_edid
);
370 * The EDID disappeared (likely because of the HPD going down).
372 void drm_dp_cec_unset_edid(struct drm_dp_aux
*aux
)
374 /* No transfer function was set, so not a DP connector */
378 cancel_delayed_work_sync(&aux
->cec
.unregister_work
);
380 mutex_lock(&aux
->cec
.lock
);
384 cec_phys_addr_invalidate(aux
->cec
.adap
);
386 * We're done if we want to keep the CEC device
387 * (drm_dp_cec_unregister_delay is >= NEVER_UNREG_DELAY) or if the
388 * DPCD still indicates the CEC capability (expected for an integrated
389 * HDMI branch device).
391 if (drm_dp_cec_unregister_delay
< NEVER_UNREG_DELAY
&&
392 !drm_dp_cec_cap(aux
, NULL
)) {
394 * Unregister the CEC adapter after drm_dp_cec_unregister_delay
395 * seconds. This to debounce short HPD off-and-on cycles from
398 schedule_delayed_work(&aux
->cec
.unregister_work
,
399 drm_dp_cec_unregister_delay
* HZ
);
402 mutex_unlock(&aux
->cec
.lock
);
404 EXPORT_SYMBOL(drm_dp_cec_unset_edid
);
407 * drm_dp_cec_register_connector() - register a new connector
408 * @aux: DisplayPort AUX channel
409 * @name: name of the CEC device
410 * @parent: parent device
412 * A new connector was registered with associated CEC adapter name and
413 * CEC adapter parent device. After registering the name and parent
414 * drm_dp_cec_set_edid() is called to check if the connector supports
415 * CEC and to register a CEC adapter if that is the case.
417 void drm_dp_cec_register_connector(struct drm_dp_aux
*aux
, const char *name
,
418 struct device
*parent
)
420 WARN_ON(aux
->cec
.adap
);
421 if (WARN_ON(!aux
->transfer
))
423 aux
->cec
.name
= name
;
424 aux
->cec
.parent
= parent
;
425 INIT_DELAYED_WORK(&aux
->cec
.unregister_work
,
426 drm_dp_cec_unregister_work
);
428 EXPORT_SYMBOL(drm_dp_cec_register_connector
);
431 * drm_dp_cec_unregister_connector() - unregister the CEC adapter, if any
432 * @aux: DisplayPort AUX channel
434 void drm_dp_cec_unregister_connector(struct drm_dp_aux
*aux
)
438 cancel_delayed_work_sync(&aux
->cec
.unregister_work
);
439 cec_unregister_adapter(aux
->cec
.adap
);
440 aux
->cec
.adap
= NULL
;
442 EXPORT_SYMBOL(drm_dp_cec_unregister_connector
);