Linux Audio

Check our new training course

Linux kernel drivers training

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