blob: f40b24c852e3a81dfc8c6009508063d05550011e [file] [log] [blame]
Hans Verkuil1d4cf022018-07-11 15:29:07 +02001// 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#include <drm/drm_dp_helper.h>
12#include <media/cec.h>
13
14/*
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.
20 *
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).
25 *
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.
29 *
30 * Hopefully by creating this driver it will be easier for vendors to
31 * finally fix their adapters and test the CEC functionality.
32 *
33 * I keep a list of known working adapters here:
34 *
35 * https://hverkuil.home.xs4all.nl/cec-status.txt
36 *
37 * Please mail me (hverkuil@xs4all.nl) if you find an adapter that works
38 * and is not yet listed there.
39 *
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
44 * to work.
45 */
46
47/**
48 * DOC: dp cec helpers
49 *
50 * These functions take care of supporting the CEC-Tunneling-over-AUX
51 * feature of DisplayPort-to-HDMI adapters.
52 */
53
54/*
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.
60 *
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.
64 *
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.
67 *
68 * If it is set to 0, then the CEC adapter will be unregistered immediately as
69 * soon as the HPD disappears.
70 *
71 * The default is one second to prevent short HPD glitches from unregistering
72 * the CEC adapter.
73 *
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.
79 */
80#define NEVER_UNREG_DELAY 1000
81static unsigned int drm_dp_cec_unregister_delay = 1;
82module_param(drm_dp_cec_unregister_delay, uint, 0600);
83MODULE_PARM_DESC(drm_dp_cec_unregister_delay,
84 "CEC unregister delay in seconds, 0: no delay, >= 1000: never unregister");
85
86static int drm_dp_cec_adap_enable(struct cec_adapter *adap, bool enable)
87{
88 struct drm_dp_aux *aux = cec_get_drvdata(adap);
89 u32 val = enable ? DP_CEC_TUNNELING_ENABLE : 0;
90 ssize_t err = 0;
91
92 err = drm_dp_dpcd_writeb(aux, DP_CEC_TUNNELING_CONTROL, val);
93 return (enable && err < 0) ? err : 0;
94}
95
96static int drm_dp_cec_adap_log_addr(struct cec_adapter *adap, u8 addr)
97{
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;
101 u8 mask[2];
102 ssize_t err;
103
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;
110}
111
112static int drm_dp_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
113 u32 signal_free_time, struct cec_msg *msg)
114{
115 struct drm_dp_aux *aux = cec_get_drvdata(adap);
116 unsigned int retries = min(5, attempts - 1);
117 ssize_t err;
118
119 err = drm_dp_dpcd_write(aux, DP_CEC_TX_MESSAGE_BUFFER,
120 msg->msg, msg->len);
121 if (err < 0)
122 return err;
123
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;
128}
129
130static int drm_dp_cec_adap_monitor_all_enable(struct cec_adapter *adap,
131 bool enable)
132{
133 struct drm_dp_aux *aux = cec_get_drvdata(adap);
134 ssize_t err;
135 u8 val;
136
137 if (!(adap->capabilities & CEC_CAP_MONITOR_ALL))
138 return 0;
139
140 err = drm_dp_dpcd_readb(aux, DP_CEC_TUNNELING_CONTROL, &val);
141 if (err >= 0) {
142 if (enable)
143 val |= DP_CEC_SNOOPING_ENABLE;
144 else
145 val &= ~DP_CEC_SNOOPING_ENABLE;
146 err = drm_dp_dpcd_writeb(aux, DP_CEC_TUNNELING_CONTROL, val);
147 }
148 return (enable && err < 0) ? err : 0;
149}
150
151static void drm_dp_cec_adap_status(struct cec_adapter *adap,
152 struct seq_file *file)
153{
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;
157
158 if (drm_dp_read_desc(aux, &desc, true))
159 return;
160 seq_printf(file, "OUI: %*pdH\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)),
164 id->device_id);
165 seq_printf(file, "HW Rev: %d.%d\n", id->hw_rev >> 4, id->hw_rev & 0xf);
166 /*
167 * Show this both in decimal and hex: at least one vendor
168 * always reports this in hex.
169 */
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);
173}
174
175static 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,
181};
182
183static int drm_dp_cec_received(struct drm_dp_aux *aux)
184{
185 struct cec_adapter *adap = aux->cec.adap;
186 struct cec_msg msg;
187 u8 rx_msg_info;
188 ssize_t err;
189
190 err = drm_dp_dpcd_readb(aux, DP_CEC_RX_MESSAGE_INFO, &rx_msg_info);
191 if (err < 0)
192 return err;
193
194 if (!(rx_msg_info & DP_CEC_RX_MESSAGE_ENDED))
195 return 0;
196
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);
199 if (err < 0)
200 return err;
201
202 cec_received_msg(adap, &msg);
203 return 0;
204}
205
206static void drm_dp_cec_handle_irq(struct drm_dp_aux *aux)
207{
208 struct cec_adapter *adap = aux->cec.adap;
209 u8 flags;
210
211 if (drm_dp_dpcd_readb(aux, DP_CEC_TUNNELING_IRQ_FLAGS, &flags) < 0)
212 return;
213
214 if (flags & DP_CEC_RX_MESSAGE_INFO_VALID)
215 drm_dp_cec_received(aux);
216
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);
222 else if (flags &
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);
227}
228
229/**
230 * drm_dp_cec_irq() - handle CEC interrupt, if any
231 * @aux: DisplayPort AUX channel
232 *
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.
235 */
236void drm_dp_cec_irq(struct drm_dp_aux *aux)
237{
238 u8 cec_irq;
239 int ret;
240
241 mutex_lock(&aux->cec.lock);
242 if (!aux->cec.adap)
243 goto unlock;
244
245 ret = drm_dp_dpcd_readb(aux, DP_DEVICE_SERVICE_IRQ_VECTOR_ESI1,
246 &cec_irq);
247 if (ret < 0 || !(cec_irq & DP_CEC_IRQ))
248 goto unlock;
249
250 drm_dp_cec_handle_irq(aux);
251 drm_dp_dpcd_writeb(aux, DP_DEVICE_SERVICE_IRQ_VECTOR_ESI1, DP_CEC_IRQ);
252unlock:
253 mutex_unlock(&aux->cec.lock);
254}
255EXPORT_SYMBOL(drm_dp_cec_irq);
256
257static bool drm_dp_cec_cap(struct drm_dp_aux *aux, u8 *cec_cap)
258{
259 u8 cap = 0;
260
261 if (drm_dp_dpcd_readb(aux, DP_CEC_TUNNELING_CAPABILITY, &cap) != 1 ||
262 !(cap & DP_CEC_TUNNELING_CAPABLE))
263 return false;
264 if (cec_cap)
265 *cec_cap = cap;
266 return true;
267}
268
269/*
270 * Called if the HPD was low for more than drm_dp_cec_unregister_delay
271 * seconds. This unregisters the CEC adapter.
272 */
273static void drm_dp_cec_unregister_work(struct work_struct *work)
274{
275 struct drm_dp_aux *aux = container_of(work, struct drm_dp_aux,
276 cec.unregister_work.work);
277
278 mutex_lock(&aux->cec.lock);
279 cec_unregister_adapter(aux->cec.adap);
280 aux->cec.adap = NULL;
281 mutex_unlock(&aux->cec.lock);
282}
283
284/*
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.
289 */
290void drm_dp_cec_set_edid(struct drm_dp_aux *aux, const struct edid *edid)
291{
Venkata Prahlad Valluru2db4fa02020-06-10 01:26:47 +0530292 u32 cec_caps = CEC_CAP_DEFAULTS;
Hans Verkuil1d4cf022018-07-11 15:29:07 +0200293 unsigned int num_las = 1;
294 u8 cap;
295
296#ifndef CONFIG_MEDIA_CEC_RC
297 /*
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.
300 *
301 * Do this here as well to ensure the tests against cec_caps are
302 * correct.
303 */
304 cec_caps &= ~CEC_CAP_RC;
305#endif
306 cancel_delayed_work_sync(&aux->cec.unregister_work);
307
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;
313 goto unlock;
314 }
315
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;
320
321 if (aux->cec.adap) {
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);
326 goto unlock;
327 }
328 /*
329 * The capabilities changed, so unregister the old
330 * adapter first.
331 */
332 cec_unregister_adapter(aux->cec.adap);
333 }
334
335 /* Create a new adapter */
336 aux->cec.adap = cec_allocate_adapter(&drm_dp_cec_adap_ops,
337 aux, aux->cec.name, cec_caps,
Venkata Prahlad Valluru2db4fa02020-06-10 01:26:47 +0530338 num_las, aux->cec.parent);
Hans Verkuil1d4cf022018-07-11 15:29:07 +0200339 if (IS_ERR(aux->cec.adap)) {
340 aux->cec.adap = NULL;
341 goto unlock;
342 }
Venkata Prahlad Valluru2db4fa02020-06-10 01:26:47 +0530343 if (cec_register_adapter(aux->cec.adap)) {
Hans Verkuil1d4cf022018-07-11 15:29:07 +0200344 cec_delete_adapter(aux->cec.adap);
345 aux->cec.adap = NULL;
346 } else {
347 /*
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.
351 */
352 cec_s_phys_addr_from_edid(aux->cec.adap, edid);
353 }
354unlock:
355 mutex_unlock(&aux->cec.lock);
356}
357EXPORT_SYMBOL(drm_dp_cec_set_edid);
358
359/*
360 * The EDID disappeared (likely because of the HPD going down).
361 */
362void drm_dp_cec_unset_edid(struct drm_dp_aux *aux)
363{
364 cancel_delayed_work_sync(&aux->cec.unregister_work);
365
366 mutex_lock(&aux->cec.lock);
367 if (!aux->cec.adap)
368 goto unlock;
369
370 cec_phys_addr_invalidate(aux->cec.adap);
371 /*
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).
376 */
377 if (drm_dp_cec_unregister_delay < NEVER_UNREG_DELAY &&
378 !drm_dp_cec_cap(aux, NULL)) {
379 /*
380 * Unregister the CEC adapter after drm_dp_cec_unregister_delay
381 * seconds. This to debounce short HPD off-and-on cycles from
382 * displays.
383 */
384 schedule_delayed_work(&aux->cec.unregister_work,
385 drm_dp_cec_unregister_delay * HZ);
386 }
387unlock:
388 mutex_unlock(&aux->cec.lock);
389}
390EXPORT_SYMBOL(drm_dp_cec_unset_edid);
391
392/**
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
397 *
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.
402 */
403void drm_dp_cec_register_connector(struct drm_dp_aux *aux, const char *name,
404 struct device *parent)
405{
Venkata Prahlad Valluru2db4fa02020-06-10 01:26:47 +0530406 if (aux->cec.adap) {
407 WARN_ON(aux->cec.adap);
408 return;
409 }
Hans Verkuil1d4cf022018-07-11 15:29:07 +0200410 aux->cec.name = name;
411 aux->cec.parent = parent;
412 INIT_DELAYED_WORK(&aux->cec.unregister_work,
413 drm_dp_cec_unregister_work);
414
415 drm_dp_cec_set_edid(aux, NULL);
416}
417EXPORT_SYMBOL(drm_dp_cec_register_connector);
418
419/**
420 * drm_dp_cec_unregister_connector() - unregister the CEC adapter, if any
421 * @aux: DisplayPort AUX channel
422 */
423void drm_dp_cec_unregister_connector(struct drm_dp_aux *aux)
424{
425 if (!aux->cec.adap)
426 return;
427 cancel_delayed_work_sync(&aux->cec.unregister_work);
428 cec_unregister_adapter(aux->cec.adap);
429 aux->cec.adap = NULL;
430}
431EXPORT_SYMBOL(drm_dp_cec_unregister_connector);