Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * DisplayPort CEC-Tunneling-over-AUX support
0004  *
0005  * Copyright 2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
0006  */
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/slab.h>
0011 
0012 #include <media/cec.h>
0013 
0014 #include <drm/display/drm_dp_helper.h>
0015 #include <drm/drm_connector.h>
0016 #include <drm/drm_device.h>
0017 
0018 /*
0019  * Unfortunately it turns out that we have a chicken-and-egg situation
0020  * here. Quite a few active (mini-)DP-to-HDMI or USB-C-to-HDMI adapters
0021  * have a converter chip that supports CEC-Tunneling-over-AUX (usually the
0022  * Parade PS176), but they do not wire up the CEC pin, thus making CEC
0023  * useless. Note that MegaChips 2900-based adapters appear to have good
0024  * support for CEC tunneling. Those adapters that I have tested using
0025  * this chipset all have the CEC line connected.
0026  *
0027  * Sadly there is no way for this driver to know this. What happens is
0028  * that a /dev/cecX device is created that is isolated and unable to see
0029  * any of the other CEC devices. Quite literally the CEC wire is cut
0030  * (or in this case, never connected in the first place).
0031  *
0032  * The reason so few adapters support this is that this tunneling protocol
0033  * was never supported by any OS. So there was no easy way of testing it,
0034  * and no incentive to correctly wire up the CEC pin.
0035  *
0036  * Hopefully by creating this driver it will be easier for vendors to
0037  * finally fix their adapters and test the CEC functionality.
0038  *
0039  * I keep a list of known working adapters here:
0040  *
0041  * https://hverkuil.home.xs4all.nl/cec-status.txt
0042  *
0043  * Please mail me (hverkuil@xs4all.nl) if you find an adapter that works
0044  * and is not yet listed there.
0045  *
0046  * Note that the current implementation does not support CEC over an MST hub.
0047  * As far as I can see there is no mechanism defined in the DisplayPort
0048  * standard to transport CEC interrupts over an MST device. It might be
0049  * possible to do this through polling, but I have not been able to get that
0050  * to work.
0051  */
0052 
0053 /**
0054  * DOC: dp cec helpers
0055  *
0056  * These functions take care of supporting the CEC-Tunneling-over-AUX
0057  * feature of DisplayPort-to-HDMI adapters.
0058  */
0059 
0060 /*
0061  * When the EDID is unset because the HPD went low, then the CEC DPCD registers
0062  * typically can no longer be read (true for a DP-to-HDMI adapter since it is
0063  * powered by the HPD). However, some displays toggle the HPD off and on for a
0064  * short period for one reason or another, and that would cause the CEC adapter
0065  * to be removed and added again, even though nothing else changed.
0066  *
0067  * This module parameter sets a delay in seconds before the CEC adapter is
0068  * actually unregistered. Only if the HPD does not return within that time will
0069  * the CEC adapter be unregistered.
0070  *
0071  * If it is set to a value >= NEVER_UNREG_DELAY, then the CEC adapter will never
0072  * be unregistered for as long as the connector remains registered.
0073  *
0074  * If it is set to 0, then the CEC adapter will be unregistered immediately as
0075  * soon as the HPD disappears.
0076  *
0077  * The default is one second to prevent short HPD glitches from unregistering
0078  * the CEC adapter.
0079  *
0080  * Note that for integrated HDMI branch devices that support CEC the DPCD
0081  * registers remain available even if the HPD goes low since it is not powered
0082  * by the HPD. In that case the CEC adapter will never be unregistered during
0083  * the life time of the connector. At least, this is the theory since I do not
0084  * have hardware with an integrated HDMI branch device that supports CEC.
0085  */
0086 #define NEVER_UNREG_DELAY 1000
0087 static unsigned int drm_dp_cec_unregister_delay = 1;
0088 module_param(drm_dp_cec_unregister_delay, uint, 0600);
0089 MODULE_PARM_DESC(drm_dp_cec_unregister_delay,
0090          "CEC unregister delay in seconds, 0: no delay, >= 1000: never unregister");
0091 
0092 static int drm_dp_cec_adap_enable(struct cec_adapter *adap, bool enable)
0093 {
0094     struct drm_dp_aux *aux = cec_get_drvdata(adap);
0095     u32 val = enable ? DP_CEC_TUNNELING_ENABLE : 0;
0096     ssize_t err = 0;
0097 
0098     err = drm_dp_dpcd_writeb(aux, DP_CEC_TUNNELING_CONTROL, val);
0099     return (enable && err < 0) ? err : 0;
0100 }
0101 
0102 static int drm_dp_cec_adap_log_addr(struct cec_adapter *adap, u8 addr)
0103 {
0104     struct drm_dp_aux *aux = cec_get_drvdata(adap);
0105     /* Bit 15 (logical address 15) should always be set */
0106     u16 la_mask = 1 << CEC_LOG_ADDR_BROADCAST;
0107     u8 mask[2];
0108     ssize_t err;
0109 
0110     if (addr != CEC_LOG_ADDR_INVALID)
0111         la_mask |= adap->log_addrs.log_addr_mask | (1 << addr);
0112     mask[0] = la_mask & 0xff;
0113     mask[1] = la_mask >> 8;
0114     err = drm_dp_dpcd_write(aux, DP_CEC_LOGICAL_ADDRESS_MASK, mask, 2);
0115     return (addr != CEC_LOG_ADDR_INVALID && err < 0) ? err : 0;
0116 }
0117 
0118 static int drm_dp_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
0119                     u32 signal_free_time, struct cec_msg *msg)
0120 {
0121     struct drm_dp_aux *aux = cec_get_drvdata(adap);
0122     unsigned int retries = min(5, attempts - 1);
0123     ssize_t err;
0124 
0125     err = drm_dp_dpcd_write(aux, DP_CEC_TX_MESSAGE_BUFFER,
0126                 msg->msg, msg->len);
0127     if (err < 0)
0128         return err;
0129 
0130     err = drm_dp_dpcd_writeb(aux, DP_CEC_TX_MESSAGE_INFO,
0131                  (msg->len - 1) | (retries << 4) |
0132                  DP_CEC_TX_MESSAGE_SEND);
0133     return err < 0 ? err : 0;
0134 }
0135 
0136 static int drm_dp_cec_adap_monitor_all_enable(struct cec_adapter *adap,
0137                           bool enable)
0138 {
0139     struct drm_dp_aux *aux = cec_get_drvdata(adap);
0140     ssize_t err;
0141     u8 val;
0142 
0143     if (!(adap->capabilities & CEC_CAP_MONITOR_ALL))
0144         return 0;
0145 
0146     err = drm_dp_dpcd_readb(aux, DP_CEC_TUNNELING_CONTROL, &val);
0147     if (err >= 0) {
0148         if (enable)
0149             val |= DP_CEC_SNOOPING_ENABLE;
0150         else
0151             val &= ~DP_CEC_SNOOPING_ENABLE;
0152         err = drm_dp_dpcd_writeb(aux, DP_CEC_TUNNELING_CONTROL, val);
0153     }
0154     return (enable && err < 0) ? err : 0;
0155 }
0156 
0157 static void drm_dp_cec_adap_status(struct cec_adapter *adap,
0158                    struct seq_file *file)
0159 {
0160     struct drm_dp_aux *aux = cec_get_drvdata(adap);
0161     struct drm_dp_desc desc;
0162     struct drm_dp_dpcd_ident *id = &desc.ident;
0163 
0164     if (drm_dp_read_desc(aux, &desc, true))
0165         return;
0166     seq_printf(file, "OUI: %*phD\n",
0167            (int)sizeof(id->oui), id->oui);
0168     seq_printf(file, "ID: %*pE\n",
0169            (int)strnlen(id->device_id, sizeof(id->device_id)),
0170            id->device_id);
0171     seq_printf(file, "HW Rev: %d.%d\n", id->hw_rev >> 4, id->hw_rev & 0xf);
0172     /*
0173      * Show this both in decimal and hex: at least one vendor
0174      * always reports this in hex.
0175      */
0176     seq_printf(file, "FW/SW Rev: %d.%d (0x%02x.0x%02x)\n",
0177            id->sw_major_rev, id->sw_minor_rev,
0178            id->sw_major_rev, id->sw_minor_rev);
0179 }
0180 
0181 static const struct cec_adap_ops drm_dp_cec_adap_ops = {
0182     .adap_enable = drm_dp_cec_adap_enable,
0183     .adap_log_addr = drm_dp_cec_adap_log_addr,
0184     .adap_transmit = drm_dp_cec_adap_transmit,
0185     .adap_monitor_all_enable = drm_dp_cec_adap_monitor_all_enable,
0186     .adap_status = drm_dp_cec_adap_status,
0187 };
0188 
0189 static int drm_dp_cec_received(struct drm_dp_aux *aux)
0190 {
0191     struct cec_adapter *adap = aux->cec.adap;
0192     struct cec_msg msg;
0193     u8 rx_msg_info;
0194     ssize_t err;
0195 
0196     err = drm_dp_dpcd_readb(aux, DP_CEC_RX_MESSAGE_INFO, &rx_msg_info);
0197     if (err < 0)
0198         return err;
0199 
0200     if (!(rx_msg_info & DP_CEC_RX_MESSAGE_ENDED))
0201         return 0;
0202 
0203     msg.len = (rx_msg_info & DP_CEC_RX_MESSAGE_LEN_MASK) + 1;
0204     err = drm_dp_dpcd_read(aux, DP_CEC_RX_MESSAGE_BUFFER, msg.msg, msg.len);
0205     if (err < 0)
0206         return err;
0207 
0208     cec_received_msg(adap, &msg);
0209     return 0;
0210 }
0211 
0212 static void drm_dp_cec_handle_irq(struct drm_dp_aux *aux)
0213 {
0214     struct cec_adapter *adap = aux->cec.adap;
0215     u8 flags;
0216 
0217     if (drm_dp_dpcd_readb(aux, DP_CEC_TUNNELING_IRQ_FLAGS, &flags) < 0)
0218         return;
0219 
0220     if (flags & DP_CEC_RX_MESSAGE_INFO_VALID)
0221         drm_dp_cec_received(aux);
0222 
0223     if (flags & DP_CEC_TX_MESSAGE_SENT)
0224         cec_transmit_attempt_done(adap, CEC_TX_STATUS_OK);
0225     else if (flags & DP_CEC_TX_LINE_ERROR)
0226         cec_transmit_attempt_done(adap, CEC_TX_STATUS_ERROR |
0227                         CEC_TX_STATUS_MAX_RETRIES);
0228     else if (flags &
0229          (DP_CEC_TX_ADDRESS_NACK_ERROR | DP_CEC_TX_DATA_NACK_ERROR))
0230         cec_transmit_attempt_done(adap, CEC_TX_STATUS_NACK |
0231                         CEC_TX_STATUS_MAX_RETRIES);
0232     drm_dp_dpcd_writeb(aux, DP_CEC_TUNNELING_IRQ_FLAGS, flags);
0233 }
0234 
0235 /**
0236  * drm_dp_cec_irq() - handle CEC interrupt, if any
0237  * @aux: DisplayPort AUX channel
0238  *
0239  * Should be called when handling an IRQ_HPD request. If CEC-tunneling-over-AUX
0240  * is present, then it will check for a CEC_IRQ and handle it accordingly.
0241  */
0242 void drm_dp_cec_irq(struct drm_dp_aux *aux)
0243 {
0244     u8 cec_irq;
0245     int ret;
0246 
0247     /* No transfer function was set, so not a DP connector */
0248     if (!aux->transfer)
0249         return;
0250 
0251     mutex_lock(&aux->cec.lock);
0252     if (!aux->cec.adap)
0253         goto unlock;
0254 
0255     ret = drm_dp_dpcd_readb(aux, DP_DEVICE_SERVICE_IRQ_VECTOR_ESI1,
0256                 &cec_irq);
0257     if (ret < 0 || !(cec_irq & DP_CEC_IRQ))
0258         goto unlock;
0259 
0260     drm_dp_cec_handle_irq(aux);
0261     drm_dp_dpcd_writeb(aux, DP_DEVICE_SERVICE_IRQ_VECTOR_ESI1, DP_CEC_IRQ);
0262 unlock:
0263     mutex_unlock(&aux->cec.lock);
0264 }
0265 EXPORT_SYMBOL(drm_dp_cec_irq);
0266 
0267 static bool drm_dp_cec_cap(struct drm_dp_aux *aux, u8 *cec_cap)
0268 {
0269     u8 cap = 0;
0270 
0271     if (drm_dp_dpcd_readb(aux, DP_CEC_TUNNELING_CAPABILITY, &cap) != 1 ||
0272         !(cap & DP_CEC_TUNNELING_CAPABLE))
0273         return false;
0274     if (cec_cap)
0275         *cec_cap = cap;
0276     return true;
0277 }
0278 
0279 /*
0280  * Called if the HPD was low for more than drm_dp_cec_unregister_delay
0281  * seconds. This unregisters the CEC adapter.
0282  */
0283 static void drm_dp_cec_unregister_work(struct work_struct *work)
0284 {
0285     struct drm_dp_aux *aux = container_of(work, struct drm_dp_aux,
0286                           cec.unregister_work.work);
0287 
0288     mutex_lock(&aux->cec.lock);
0289     cec_unregister_adapter(aux->cec.adap);
0290     aux->cec.adap = NULL;
0291     mutex_unlock(&aux->cec.lock);
0292 }
0293 
0294 /*
0295  * A new EDID is set. If there is no CEC adapter, then create one. If
0296  * there was a CEC adapter, then check if the CEC adapter properties
0297  * were unchanged and just update the CEC physical address. Otherwise
0298  * unregister the old CEC adapter and create a new one.
0299  */
0300 void drm_dp_cec_set_edid(struct drm_dp_aux *aux, const struct edid *edid)
0301 {
0302     struct drm_connector *connector = aux->cec.connector;
0303     u32 cec_caps = CEC_CAP_DEFAULTS | CEC_CAP_NEEDS_HPD |
0304                CEC_CAP_CONNECTOR_INFO;
0305     struct cec_connector_info conn_info;
0306     unsigned int num_las = 1;
0307     u8 cap;
0308 
0309     /* No transfer function was set, so not a DP connector */
0310     if (!aux->transfer)
0311         return;
0312 
0313 #ifndef CONFIG_MEDIA_CEC_RC
0314     /*
0315      * CEC_CAP_RC is part of CEC_CAP_DEFAULTS, but it is stripped by
0316      * cec_allocate_adapter() if CONFIG_MEDIA_CEC_RC is undefined.
0317      *
0318      * Do this here as well to ensure the tests against cec_caps are
0319      * correct.
0320      */
0321     cec_caps &= ~CEC_CAP_RC;
0322 #endif
0323     cancel_delayed_work_sync(&aux->cec.unregister_work);
0324 
0325     mutex_lock(&aux->cec.lock);
0326     if (!drm_dp_cec_cap(aux, &cap)) {
0327         /* CEC is not supported, unregister any existing adapter */
0328         cec_unregister_adapter(aux->cec.adap);
0329         aux->cec.adap = NULL;
0330         goto unlock;
0331     }
0332 
0333     if (cap & DP_CEC_SNOOPING_CAPABLE)
0334         cec_caps |= CEC_CAP_MONITOR_ALL;
0335     if (cap & DP_CEC_MULTIPLE_LA_CAPABLE)
0336         num_las = CEC_MAX_LOG_ADDRS;
0337 
0338     if (aux->cec.adap) {
0339         if (aux->cec.adap->capabilities == cec_caps &&
0340             aux->cec.adap->available_log_addrs == num_las) {
0341             /* Unchanged, so just set the phys addr */
0342             cec_s_phys_addr_from_edid(aux->cec.adap, edid);
0343             goto unlock;
0344         }
0345         /*
0346          * The capabilities changed, so unregister the old
0347          * adapter first.
0348          */
0349         cec_unregister_adapter(aux->cec.adap);
0350     }
0351 
0352     /* Create a new adapter */
0353     aux->cec.adap = cec_allocate_adapter(&drm_dp_cec_adap_ops,
0354                          aux, connector->name, cec_caps,
0355                          num_las);
0356     if (IS_ERR(aux->cec.adap)) {
0357         aux->cec.adap = NULL;
0358         goto unlock;
0359     }
0360 
0361     cec_fill_conn_info_from_drm(&conn_info, connector);
0362     cec_s_conn_info(aux->cec.adap, &conn_info);
0363 
0364     if (cec_register_adapter(aux->cec.adap, connector->dev->dev)) {
0365         cec_delete_adapter(aux->cec.adap);
0366         aux->cec.adap = NULL;
0367     } else {
0368         /*
0369          * Update the phys addr for the new CEC adapter. When called
0370          * from drm_dp_cec_register_connector() edid == NULL, so in
0371          * that case the phys addr is just invalidated.
0372          */
0373         cec_s_phys_addr_from_edid(aux->cec.adap, edid);
0374     }
0375 unlock:
0376     mutex_unlock(&aux->cec.lock);
0377 }
0378 EXPORT_SYMBOL(drm_dp_cec_set_edid);
0379 
0380 /*
0381  * The EDID disappeared (likely because of the HPD going down).
0382  */
0383 void drm_dp_cec_unset_edid(struct drm_dp_aux *aux)
0384 {
0385     /* No transfer function was set, so not a DP connector */
0386     if (!aux->transfer)
0387         return;
0388 
0389     cancel_delayed_work_sync(&aux->cec.unregister_work);
0390 
0391     mutex_lock(&aux->cec.lock);
0392     if (!aux->cec.adap)
0393         goto unlock;
0394 
0395     cec_phys_addr_invalidate(aux->cec.adap);
0396     /*
0397      * We're done if we want to keep the CEC device
0398      * (drm_dp_cec_unregister_delay is >= NEVER_UNREG_DELAY) or if the
0399      * DPCD still indicates the CEC capability (expected for an integrated
0400      * HDMI branch device).
0401      */
0402     if (drm_dp_cec_unregister_delay < NEVER_UNREG_DELAY &&
0403         !drm_dp_cec_cap(aux, NULL)) {
0404         /*
0405          * Unregister the CEC adapter after drm_dp_cec_unregister_delay
0406          * seconds. This to debounce short HPD off-and-on cycles from
0407          * displays.
0408          */
0409         schedule_delayed_work(&aux->cec.unregister_work,
0410                       drm_dp_cec_unregister_delay * HZ);
0411     }
0412 unlock:
0413     mutex_unlock(&aux->cec.lock);
0414 }
0415 EXPORT_SYMBOL(drm_dp_cec_unset_edid);
0416 
0417 /**
0418  * drm_dp_cec_register_connector() - register a new connector
0419  * @aux: DisplayPort AUX channel
0420  * @connector: drm connector
0421  *
0422  * A new connector was registered with associated CEC adapter name and
0423  * CEC adapter parent device. After registering the name and parent
0424  * drm_dp_cec_set_edid() is called to check if the connector supports
0425  * CEC and to register a CEC adapter if that is the case.
0426  */
0427 void drm_dp_cec_register_connector(struct drm_dp_aux *aux,
0428                    struct drm_connector *connector)
0429 {
0430     WARN_ON(aux->cec.adap);
0431     if (WARN_ON(!aux->transfer))
0432         return;
0433     aux->cec.connector = connector;
0434     INIT_DELAYED_WORK(&aux->cec.unregister_work,
0435               drm_dp_cec_unregister_work);
0436 }
0437 EXPORT_SYMBOL(drm_dp_cec_register_connector);
0438 
0439 /**
0440  * drm_dp_cec_unregister_connector() - unregister the CEC adapter, if any
0441  * @aux: DisplayPort AUX channel
0442  */
0443 void drm_dp_cec_unregister_connector(struct drm_dp_aux *aux)
0444 {
0445     if (!aux->cec.adap)
0446         return;
0447     cancel_delayed_work_sync(&aux->cec.unregister_work);
0448     cec_unregister_adapter(aux->cec.adap);
0449     aux->cec.adap = NULL;
0450 }
0451 EXPORT_SYMBOL(drm_dp_cec_unregister_connector);