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