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
21 * Sadly there is no way for this driver to know this. What happens is
22 * that a /dev/cecX device is created that is isolated and unable to see
23 * any of the other CEC devices. Quite literally the CEC wire is cut
24 * (or in this case, never connected in the first place).
26 * The reason so few adapters support this is that this tunneling protocol
27 * was never supported by any OS. So there was no easy way of testing it,
28 * and no incentive to correctly wire up the CEC pin.
30 * Hopefully by creating this driver it will be easier for vendors to
31 * finally fix their adapters and test the CEC functionality.
33 * I keep a list of known working adapters here:
35 * https://hverkuil.home.xs4all.nl/cec-status.txt
37 * Please mail me (hverkuil@xs4all.nl) if you find an adapter that works
38 * and is not yet listed there.
40 * Note that the current implementation does not support CEC over an MST hub.
41 * As far as I can see there is no mechanism defined in the DisplayPort
42 * standard to transport CEC interrupts over an MST device. It might be
43 * possible to do this through polling, but I have not been able to get that
50 * These functions take care of supporting the CEC-Tunneling-over-AUX
51 * feature of DisplayPort-to-HDMI adapters.
55 * When the EDID is unset because the HPD went low, then the CEC DPCD registers
56 * typically can no longer be read (true for a DP-to-HDMI adapter since it is
57 * powered by the HPD). However, some displays toggle the HPD off and on for a
58 * short period for one reason or another, and that would cause the CEC adapter
59 * to be removed and added again, even though nothing else changed.
61 * This module parameter sets a delay in seconds before the CEC adapter is
62 * actually unregistered. Only if the HPD does not return within that time will
63 * the CEC adapter be unregistered.
65 * If it is set to a value >= NEVER_UNREG_DELAY, then the CEC adapter will never
66 * be unregistered for as long as the connector remains registered.
68 * If it is set to 0, then the CEC adapter will be unregistered immediately as
69 * soon as the HPD disappears.
71 * The default is one second to prevent short HPD glitches from unregistering
74 * Note that for integrated HDMI branch devices that support CEC the DPCD
75 * registers remain available even if the HPD goes low since it is not powered
76 * by the HPD. In that case the CEC adapter will never be unregistered during
77 * the life time of the connector. At least, this is the theory since I do not
78 * have hardware with an integrated HDMI branch device that supports CEC.
80 #define NEVER_UNREG_DELAY 1000
81 static unsigned int drm_dp_cec_unregister_delay
= 1;
82 module_param(drm_dp_cec_unregister_delay
, uint
, 0600);
83 MODULE_PARM_DESC(drm_dp_cec_unregister_delay
,
84 "CEC unregister delay in seconds, 0: no delay, >= 1000: never unregister");
86 static int drm_dp_cec_adap_enable(struct cec_adapter
*adap
, bool enable
)
88 struct drm_dp_aux
*aux
= cec_get_drvdata(adap
);
89 u32 val
= enable
? DP_CEC_TUNNELING_ENABLE
: 0;
92 err
= drm_dp_dpcd_writeb(aux
, DP_CEC_TUNNELING_CONTROL
, val
);
93 return (enable
&& err
< 0) ? err
: 0;
96 static int drm_dp_cec_adap_log_addr(struct cec_adapter
*adap
, u8 addr
)
98 struct drm_dp_aux
*aux
= cec_get_drvdata(adap
);
99 /* Bit 15 (logical address 15) should always be set */
100 u16 la_mask
= 1 << CEC_LOG_ADDR_BROADCAST
;
104 if (addr
!= CEC_LOG_ADDR_INVALID
)
105 la_mask
|= adap
->log_addrs
.log_addr_mask
| (1 << addr
);
106 mask
[0] = la_mask
& 0xff;
107 mask
[1] = la_mask
>> 8;
108 err
= drm_dp_dpcd_write(aux
, DP_CEC_LOGICAL_ADDRESS_MASK
, mask
, 2);
109 return (addr
!= CEC_LOG_ADDR_INVALID
&& err
< 0) ? err
: 0;
112 static int drm_dp_cec_adap_transmit(struct cec_adapter
*adap
, u8 attempts
,
113 u32 signal_free_time
, struct cec_msg
*msg
)
115 struct drm_dp_aux
*aux
= cec_get_drvdata(adap
);
116 unsigned int retries
= min(5, attempts
- 1);
119 err
= drm_dp_dpcd_write(aux
, DP_CEC_TX_MESSAGE_BUFFER
,
124 err
= drm_dp_dpcd_writeb(aux
, DP_CEC_TX_MESSAGE_INFO
,
125 (msg
->len
- 1) | (retries
<< 4) |
126 DP_CEC_TX_MESSAGE_SEND
);
127 return err
< 0 ? err
: 0;
130 static int drm_dp_cec_adap_monitor_all_enable(struct cec_adapter
*adap
,
133 struct drm_dp_aux
*aux
= cec_get_drvdata(adap
);
137 if (!(adap
->capabilities
& CEC_CAP_MONITOR_ALL
))
140 err
= drm_dp_dpcd_readb(aux
, DP_CEC_TUNNELING_CONTROL
, &val
);
143 val
|= DP_CEC_SNOOPING_ENABLE
;
145 val
&= ~DP_CEC_SNOOPING_ENABLE
;
146 err
= drm_dp_dpcd_writeb(aux
, DP_CEC_TUNNELING_CONTROL
, val
);
148 return (enable
&& err
< 0) ? err
: 0;
151 static void drm_dp_cec_adap_status(struct cec_adapter
*adap
,
152 struct seq_file
*file
)
154 struct drm_dp_aux
*aux
= cec_get_drvdata(adap
);
155 struct drm_dp_desc desc
;
156 struct drm_dp_dpcd_ident
*id
= &desc
.ident
;
158 if (drm_dp_read_desc(aux
, &desc
, true))
160 seq_printf(file
, "OUI: %*phD\n",
161 (int)sizeof(id
->oui
), id
->oui
);
162 seq_printf(file
, "ID: %*pE\n",
163 (int)strnlen(id
->device_id
, sizeof(id
->device_id
)),
165 seq_printf(file
, "HW Rev: %d.%d\n", id
->hw_rev
>> 4, id
->hw_rev
& 0xf);
167 * Show this both in decimal and hex: at least one vendor
168 * always reports this in hex.
170 seq_printf(file
, "FW/SW Rev: %d.%d (0x%02x.0x%02x)\n",
171 id
->sw_major_rev
, id
->sw_minor_rev
,
172 id
->sw_major_rev
, id
->sw_minor_rev
);
175 static const struct cec_adap_ops drm_dp_cec_adap_ops
= {
176 .adap_enable
= drm_dp_cec_adap_enable
,
177 .adap_log_addr
= drm_dp_cec_adap_log_addr
,
178 .adap_transmit
= drm_dp_cec_adap_transmit
,
179 .adap_monitor_all_enable
= drm_dp_cec_adap_monitor_all_enable
,
180 .adap_status
= drm_dp_cec_adap_status
,
183 static int drm_dp_cec_received(struct drm_dp_aux
*aux
)
185 struct cec_adapter
*adap
= aux
->cec
.adap
;
190 err
= drm_dp_dpcd_readb(aux
, DP_CEC_RX_MESSAGE_INFO
, &rx_msg_info
);
194 if (!(rx_msg_info
& DP_CEC_RX_MESSAGE_ENDED
))
197 msg
.len
= (rx_msg_info
& DP_CEC_RX_MESSAGE_LEN_MASK
) + 1;
198 err
= drm_dp_dpcd_read(aux
, DP_CEC_RX_MESSAGE_BUFFER
, msg
.msg
, msg
.len
);
202 cec_received_msg(adap
, &msg
);
206 static void drm_dp_cec_handle_irq(struct drm_dp_aux
*aux
)
208 struct cec_adapter
*adap
= aux
->cec
.adap
;
211 if (drm_dp_dpcd_readb(aux
, DP_CEC_TUNNELING_IRQ_FLAGS
, &flags
) < 0)
214 if (flags
& DP_CEC_RX_MESSAGE_INFO_VALID
)
215 drm_dp_cec_received(aux
);
217 if (flags
& DP_CEC_TX_MESSAGE_SENT
)
218 cec_transmit_attempt_done(adap
, CEC_TX_STATUS_OK
);
219 else if (flags
& DP_CEC_TX_LINE_ERROR
)
220 cec_transmit_attempt_done(adap
, CEC_TX_STATUS_ERROR
|
221 CEC_TX_STATUS_MAX_RETRIES
);
223 (DP_CEC_TX_ADDRESS_NACK_ERROR
| DP_CEC_TX_DATA_NACK_ERROR
))
224 cec_transmit_attempt_done(adap
, CEC_TX_STATUS_NACK
|
225 CEC_TX_STATUS_MAX_RETRIES
);
226 drm_dp_dpcd_writeb(aux
, DP_CEC_TUNNELING_IRQ_FLAGS
, flags
);
230 * drm_dp_cec_irq() - handle CEC interrupt, if any
231 * @aux: DisplayPort AUX channel
233 * Should be called when handling an IRQ_HPD request. If CEC-tunneling-over-AUX
234 * is present, then it will check for a CEC_IRQ and handle it accordingly.
236 void drm_dp_cec_irq(struct drm_dp_aux
*aux
)
241 mutex_lock(&aux
->cec
.lock
);
245 ret
= drm_dp_dpcd_readb(aux
, DP_DEVICE_SERVICE_IRQ_VECTOR_ESI1
,
247 if (ret
< 0 || !(cec_irq
& DP_CEC_IRQ
))
250 drm_dp_cec_handle_irq(aux
);
251 drm_dp_dpcd_writeb(aux
, DP_DEVICE_SERVICE_IRQ_VECTOR_ESI1
, DP_CEC_IRQ
);
253 mutex_unlock(&aux
->cec
.lock
);
255 EXPORT_SYMBOL(drm_dp_cec_irq
);
257 static bool drm_dp_cec_cap(struct drm_dp_aux
*aux
, u8
*cec_cap
)
261 if (drm_dp_dpcd_readb(aux
, DP_CEC_TUNNELING_CAPABILITY
, &cap
) != 1 ||
262 !(cap
& DP_CEC_TUNNELING_CAPABLE
))
270 * Called if the HPD was low for more than drm_dp_cec_unregister_delay
271 * seconds. This unregisters the CEC adapter.
273 static void drm_dp_cec_unregister_work(struct work_struct
*work
)
275 struct drm_dp_aux
*aux
= container_of(work
, struct drm_dp_aux
,
276 cec
.unregister_work
.work
);
278 mutex_lock(&aux
->cec
.lock
);
279 cec_unregister_adapter(aux
->cec
.adap
);
280 aux
->cec
.adap
= NULL
;
281 mutex_unlock(&aux
->cec
.lock
);
285 * A new EDID is set. If there is no CEC adapter, then create one. If
286 * there was a CEC adapter, then check if the CEC adapter properties
287 * were unchanged and just update the CEC physical address. Otherwise
288 * unregister the old CEC adapter and create a new one.
290 void drm_dp_cec_set_edid(struct drm_dp_aux
*aux
, const struct edid
*edid
)
292 u32 cec_caps
= CEC_CAP_DEFAULTS
| CEC_CAP_NEEDS_HPD
;
293 unsigned int num_las
= 1;
296 #ifndef CONFIG_MEDIA_CEC_RC
298 * CEC_CAP_RC is part of CEC_CAP_DEFAULTS, but it is stripped by
299 * cec_allocate_adapter() if CONFIG_MEDIA_CEC_RC is undefined.
301 * Do this here as well to ensure the tests against cec_caps are
304 cec_caps
&= ~CEC_CAP_RC
;
306 cancel_delayed_work_sync(&aux
->cec
.unregister_work
);
308 mutex_lock(&aux
->cec
.lock
);
309 if (!drm_dp_cec_cap(aux
, &cap
)) {
310 /* CEC is not supported, unregister any existing adapter */
311 cec_unregister_adapter(aux
->cec
.adap
);
312 aux
->cec
.adap
= NULL
;
316 if (cap
& DP_CEC_SNOOPING_CAPABLE
)
317 cec_caps
|= CEC_CAP_MONITOR_ALL
;
318 if (cap
& DP_CEC_MULTIPLE_LA_CAPABLE
)
319 num_las
= CEC_MAX_LOG_ADDRS
;
322 if (aux
->cec
.adap
->capabilities
== cec_caps
&&
323 aux
->cec
.adap
->available_log_addrs
== num_las
) {
324 /* Unchanged, so just set the phys addr */
325 cec_s_phys_addr_from_edid(aux
->cec
.adap
, edid
);
329 * The capabilities changed, so unregister the old
332 cec_unregister_adapter(aux
->cec
.adap
);
335 /* Create a new adapter */
336 aux
->cec
.adap
= cec_allocate_adapter(&drm_dp_cec_adap_ops
,
337 aux
, aux
->cec
.name
, cec_caps
,
339 if (IS_ERR(aux
->cec
.adap
)) {
340 aux
->cec
.adap
= NULL
;
343 if (cec_register_adapter(aux
->cec
.adap
, aux
->cec
.parent
)) {
344 cec_delete_adapter(aux
->cec
.adap
);
345 aux
->cec
.adap
= NULL
;
348 * Update the phys addr for the new CEC adapter. When called
349 * from drm_dp_cec_register_connector() edid == NULL, so in
350 * that case the phys addr is just invalidated.
352 cec_s_phys_addr_from_edid(aux
->cec
.adap
, edid
);
355 mutex_unlock(&aux
->cec
.lock
);
357 EXPORT_SYMBOL(drm_dp_cec_set_edid
);
360 * The EDID disappeared (likely because of the HPD going down).
362 void drm_dp_cec_unset_edid(struct drm_dp_aux
*aux
)
364 cancel_delayed_work_sync(&aux
->cec
.unregister_work
);
366 mutex_lock(&aux
->cec
.lock
);
370 cec_phys_addr_invalidate(aux
->cec
.adap
);
372 * We're done if we want to keep the CEC device
373 * (drm_dp_cec_unregister_delay is >= NEVER_UNREG_DELAY) or if the
374 * DPCD still indicates the CEC capability (expected for an integrated
375 * HDMI branch device).
377 if (drm_dp_cec_unregister_delay
< NEVER_UNREG_DELAY
&&
378 !drm_dp_cec_cap(aux
, NULL
)) {
380 * Unregister the CEC adapter after drm_dp_cec_unregister_delay
381 * seconds. This to debounce short HPD off-and-on cycles from
384 schedule_delayed_work(&aux
->cec
.unregister_work
,
385 drm_dp_cec_unregister_delay
* HZ
);
388 mutex_unlock(&aux
->cec
.lock
);
390 EXPORT_SYMBOL(drm_dp_cec_unset_edid
);
393 * drm_dp_cec_register_connector() - register a new connector
394 * @aux: DisplayPort AUX channel
395 * @name: name of the CEC device
396 * @parent: parent device
398 * A new connector was registered with associated CEC adapter name and
399 * CEC adapter parent device. After registering the name and parent
400 * drm_dp_cec_set_edid() is called to check if the connector supports
401 * CEC and to register a CEC adapter if that is the case.
403 void drm_dp_cec_register_connector(struct drm_dp_aux
*aux
, const char *name
,
404 struct device
*parent
)
406 WARN_ON(aux
->cec
.adap
);
407 aux
->cec
.name
= name
;
408 aux
->cec
.parent
= parent
;
409 INIT_DELAYED_WORK(&aux
->cec
.unregister_work
,
410 drm_dp_cec_unregister_work
);
412 drm_dp_cec_set_edid(aux
, NULL
);
414 EXPORT_SYMBOL(drm_dp_cec_register_connector
);
417 * drm_dp_cec_unregister_connector() - unregister the CEC adapter, if any
418 * @aux: DisplayPort AUX channel
420 void drm_dp_cec_unregister_connector(struct drm_dp_aux
*aux
)
424 cancel_delayed_work_sync(&aux
->cec
.unregister_work
);
425 cec_unregister_adapter(aux
->cec
.adap
);
426 aux
->cec
.adap
= NULL
;
428 EXPORT_SYMBOL(drm_dp_cec_unregister_connector
);