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