brintos

brintos / linux-shallow public Read only

0
0
Text · 14.1 KiB · 007ceb2 Raw
469 lines · c
1// SPDX-License-Identifier: GPL-2.02/*3 * DisplayPort CEC-Tunneling-over-AUX support4 *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 situation21 * here. Quite a few active (mini-)DP-to-HDMI or USB-C-to-HDMI adapters22 * have a converter chip that supports CEC-Tunneling-over-AUX (usually the23 * Parade PS176), but they do not wire up the CEC pin, thus making CEC24 * useless. Note that MegaChips 2900-based adapters appear to have good25 * support for CEC tunneling. Those adapters that I have tested using26 * this chipset all have the CEC line connected.27 *28 * Sadly there is no way for this driver to know this. What happens is29 * that a /dev/cecX device is created that is isolated and unable to see30 * any of the other CEC devices. Quite literally the CEC wire is cut31 * (or in this case, never connected in the first place).32 *33 * The reason so few adapters support this is that this tunneling protocol34 * 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 to38 * 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.txt43 *44 * Please mail me (hverkuil@xs4all.nl) if you find an adapter that works45 * 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 DisplayPort49 * standard to transport CEC interrupts over an MST device. It might be50 * possible to do this through polling, but I have not been able to get that51 * to work.52 */53 54/**55 * DOC: dp cec helpers56 *57 * These functions take care of supporting the CEC-Tunneling-over-AUX58 * feature of DisplayPort-to-HDMI adapters.59 */60 61/*62 * When the EDID is unset because the HPD went low, then the CEC DPCD registers63 * typically can no longer be read (true for a DP-to-HDMI adapter since it is64 * powered by the HPD). However, some displays toggle the HPD off and on for a65 * short period for one reason or another, and that would cause the CEC adapter66 * 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 is69 * actually unregistered. Only if the HPD does not return within that time will70 * the CEC adapter be unregistered.71 *72 * If it is set to a value >= NEVER_UNREG_DELAY, then the CEC adapter will never73 * 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 as76 * soon as the HPD disappears.77 *78 * The default is one second to prevent short HPD glitches from unregistering79 * the CEC adapter.80 *81 * Note that for integrated HDMI branch devices that support CEC the DPCD82 * registers remain available even if the HPD goes low since it is not powered83 * by the HPD. In that case the CEC adapter will never be unregistered during84 * the life time of the connector. At least, this is the theory since I do not85 * have hardware with an integrated HDMI branch device that supports CEC.86 */87#define NEVER_UNREG_DELAY 100088static 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_writeb(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(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(aux, DP_CEC_TX_MESSAGE_BUFFER,127				msg->msg, msg->len);128	if (err < 0)129		return err;130 131	err = drm_dp_dpcd_writeb(aux, DP_CEC_TX_MESSAGE_INFO,132				 (msg->len - 1) | (retries << 4) |133				 DP_CEC_TX_MESSAGE_SEND);134	return err < 0 ? err : 0;135}136 137static int drm_dp_cec_adap_monitor_all_enable(struct cec_adapter *adap,138					      bool enable)139{140	struct drm_dp_aux *aux = cec_get_drvdata(adap);141	ssize_t err;142	u8 val;143 144	if (!(adap->capabilities & CEC_CAP_MONITOR_ALL))145		return 0;146 147	err = drm_dp_dpcd_readb(aux, DP_CEC_TUNNELING_CONTROL, &val);148	if (err >= 0) {149		if (enable)150			val |= DP_CEC_SNOOPING_ENABLE;151		else152			val &= ~DP_CEC_SNOOPING_ENABLE;153		err = drm_dp_dpcd_writeb(aux, DP_CEC_TUNNELING_CONTROL, val);154	}155	return (enable && err < 0) ? err : 0;156}157 158static void drm_dp_cec_adap_status(struct cec_adapter *adap,159				   struct seq_file *file)160{161	struct drm_dp_aux *aux = cec_get_drvdata(adap);162	struct drm_dp_desc desc;163	struct drm_dp_dpcd_ident *id = &desc.ident;164 165	if (drm_dp_read_desc(aux, &desc, true))166		return;167	seq_printf(file, "OUI: %*phD\n",168		   (int)sizeof(id->oui), id->oui);169	seq_printf(file, "ID: %*pE\n",170		   (int)strnlen(id->device_id, sizeof(id->device_id)),171		   id->device_id);172	seq_printf(file, "HW Rev: %d.%d\n", id->hw_rev >> 4, id->hw_rev & 0xf);173	/*174	 * Show this both in decimal and hex: at least one vendor175	 * always reports this in hex.176	 */177	seq_printf(file, "FW/SW Rev: %d.%d (0x%02x.0x%02x)\n",178		   id->sw_major_rev, id->sw_minor_rev,179		   id->sw_major_rev, id->sw_minor_rev);180}181 182static const struct cec_adap_ops drm_dp_cec_adap_ops = {183	.adap_enable = drm_dp_cec_adap_enable,184	.adap_log_addr = drm_dp_cec_adap_log_addr,185	.adap_transmit = drm_dp_cec_adap_transmit,186	.adap_monitor_all_enable = drm_dp_cec_adap_monitor_all_enable,187	.adap_status = drm_dp_cec_adap_status,188};189 190static int drm_dp_cec_received(struct drm_dp_aux *aux)191{192	struct cec_adapter *adap = aux->cec.adap;193	struct cec_msg msg;194	u8 rx_msg_info;195	ssize_t err;196 197	err = drm_dp_dpcd_readb(aux, DP_CEC_RX_MESSAGE_INFO, &rx_msg_info);198	if (err < 0)199		return err;200 201	if (!(rx_msg_info & DP_CEC_RX_MESSAGE_ENDED))202		return 0;203 204	msg.len = (rx_msg_info & DP_CEC_RX_MESSAGE_LEN_MASK) + 1;205	err = drm_dp_dpcd_read(aux, DP_CEC_RX_MESSAGE_BUFFER, msg.msg, msg.len);206	if (err < 0)207		return err;208 209	cec_received_msg(adap, &msg);210	return 0;211}212 213static void drm_dp_cec_handle_irq(struct drm_dp_aux *aux)214{215	struct cec_adapter *adap = aux->cec.adap;216	u8 flags;217 218	if (drm_dp_dpcd_readb(aux, DP_CEC_TUNNELING_IRQ_FLAGS, &flags) < 0)219		return;220 221	if (flags & DP_CEC_RX_MESSAGE_INFO_VALID)222		drm_dp_cec_received(aux);223 224	if (flags & DP_CEC_TX_MESSAGE_SENT)225		cec_transmit_attempt_done(adap, CEC_TX_STATUS_OK);226	else if (flags & DP_CEC_TX_LINE_ERROR)227		cec_transmit_attempt_done(adap, CEC_TX_STATUS_ERROR |228						CEC_TX_STATUS_MAX_RETRIES);229	else if (flags &230		 (DP_CEC_TX_ADDRESS_NACK_ERROR | DP_CEC_TX_DATA_NACK_ERROR))231		cec_transmit_attempt_done(adap, CEC_TX_STATUS_NACK |232						CEC_TX_STATUS_MAX_RETRIES);233	drm_dp_dpcd_writeb(aux, DP_CEC_TUNNELING_IRQ_FLAGS, flags);234}235 236/**237 * drm_dp_cec_irq() - handle CEC interrupt, if any238 * @aux: DisplayPort AUX channel239 *240 * Should be called when handling an IRQ_HPD request. If CEC-tunneling-over-AUX241 * is present, then it will check for a CEC_IRQ and handle it accordingly.242 */243void drm_dp_cec_irq(struct drm_dp_aux *aux)244{245	u8 cec_irq;246	int ret;247 248	/* No transfer function was set, so not a DP connector */249	if (!aux->transfer)250		return;251 252	mutex_lock(&aux->cec.lock);253	if (!aux->cec.adap)254		goto unlock;255 256	ret = drm_dp_dpcd_readb(aux, DP_DEVICE_SERVICE_IRQ_VECTOR_ESI1,257				&cec_irq);258	if (ret < 0 || !(cec_irq & DP_CEC_IRQ))259		goto unlock;260 261	drm_dp_cec_handle_irq(aux);262	drm_dp_dpcd_writeb(aux, DP_DEVICE_SERVICE_IRQ_VECTOR_ESI1, DP_CEC_IRQ);263unlock:264	mutex_unlock(&aux->cec.lock);265}266EXPORT_SYMBOL(drm_dp_cec_irq);267 268static bool drm_dp_cec_cap(struct drm_dp_aux *aux, u8 *cec_cap)269{270	u8 cap = 0;271 272	if (drm_dp_dpcd_readb(aux, DP_CEC_TUNNELING_CAPABILITY, &cap) != 1 ||273	    !(cap & DP_CEC_TUNNELING_CAPABLE))274		return false;275	if (cec_cap)276		*cec_cap = cap;277	return true;278}279 280/*281 * Called if the HPD was low for more than drm_dp_cec_unregister_delay282 * seconds. This unregisters the CEC adapter.283 */284static void drm_dp_cec_unregister_work(struct work_struct *work)285{286	struct drm_dp_aux *aux = container_of(work, struct drm_dp_aux,287					      cec.unregister_work.work);288 289	mutex_lock(&aux->cec.lock);290	cec_unregister_adapter(aux->cec.adap);291	aux->cec.adap = NULL;292	mutex_unlock(&aux->cec.lock);293}294 295/*296 * A new EDID is set. If there is no CEC adapter, then create one. If297 * there was a CEC adapter, then check if the CEC adapter properties298 * were unchanged and just update the CEC physical address. Otherwise299 * unregister the old CEC adapter and create a new one.300 */301void drm_dp_cec_attach(struct drm_dp_aux *aux, u16 source_physical_address)302{303	struct drm_connector *connector = aux->cec.connector;304	u32 cec_caps = CEC_CAP_DEFAULTS | CEC_CAP_NEEDS_HPD |305		       CEC_CAP_CONNECTOR_INFO;306	struct cec_connector_info conn_info;307	unsigned int num_las = 1;308	u8 cap;309 310	/* No transfer function was set, so not a DP connector */311	if (!aux->transfer)312		return;313 314#ifndef CONFIG_MEDIA_CEC_RC315	/*316	 * CEC_CAP_RC is part of CEC_CAP_DEFAULTS, but it is stripped by317	 * cec_allocate_adapter() if CONFIG_MEDIA_CEC_RC is undefined.318	 *319	 * Do this here as well to ensure the tests against cec_caps are320	 * correct.321	 */322	cec_caps &= ~CEC_CAP_RC;323#endif324	cancel_delayed_work_sync(&aux->cec.unregister_work);325 326	mutex_lock(&aux->cec.lock);327	if (!drm_dp_cec_cap(aux, &cap)) {328		/* CEC is not supported, unregister any existing adapter */329		cec_unregister_adapter(aux->cec.adap);330		aux->cec.adap = NULL;331		goto unlock;332	}333 334	if (cap & DP_CEC_SNOOPING_CAPABLE)335		cec_caps |= CEC_CAP_MONITOR_ALL;336	if (cap & DP_CEC_MULTIPLE_LA_CAPABLE)337		num_las = CEC_MAX_LOG_ADDRS;338 339	if (aux->cec.adap) {340		if (aux->cec.adap->capabilities == cec_caps &&341		    aux->cec.adap->available_log_addrs == num_las) {342			/* Unchanged, so just set the phys addr */343			cec_s_phys_addr(aux->cec.adap, source_physical_address, false);344			goto unlock;345		}346		/*347		 * The capabilities changed, so unregister the old348		 * adapter first.349		 */350		cec_unregister_adapter(aux->cec.adap);351	}352 353	/* Create a new adapter */354	aux->cec.adap = cec_allocate_adapter(&drm_dp_cec_adap_ops,355					     aux, connector->name, cec_caps,356					     num_las);357	if (IS_ERR(aux->cec.adap)) {358		aux->cec.adap = NULL;359		goto unlock;360	}361 362	cec_fill_conn_info_from_drm(&conn_info, connector);363	cec_s_conn_info(aux->cec.adap, &conn_info);364 365	if (cec_register_adapter(aux->cec.adap, connector->dev->dev)) {366		cec_delete_adapter(aux->cec.adap);367		aux->cec.adap = NULL;368	} else {369		/*370		 * Update the phys addr for the new CEC adapter. When called371		 * from drm_dp_cec_register_connector() edid == NULL, so in372		 * that case the phys addr is just invalidated.373		 */374		cec_s_phys_addr(aux->cec.adap, source_physical_address, false);375	}376unlock:377	mutex_unlock(&aux->cec.lock);378}379EXPORT_SYMBOL(drm_dp_cec_attach);380 381/*382 * Note: Prefer calling drm_dp_cec_attach() with383 * connector->display_info.source_physical_address if possible.384 */385void drm_dp_cec_set_edid(struct drm_dp_aux *aux, const struct edid *edid)386{387	u16 pa = CEC_PHYS_ADDR_INVALID;388 389	if (edid && edid->extensions)390		pa = cec_get_edid_phys_addr((const u8 *)edid,391					    EDID_LENGTH * (edid->extensions + 1), NULL);392 393	drm_dp_cec_attach(aux, pa);394}395EXPORT_SYMBOL(drm_dp_cec_set_edid);396 397/*398 * The EDID disappeared (likely because of the HPD going down).399 */400void drm_dp_cec_unset_edid(struct drm_dp_aux *aux)401{402	/* No transfer function was set, so not a DP connector */403	if (!aux->transfer)404		return;405 406	cancel_delayed_work_sync(&aux->cec.unregister_work);407 408	mutex_lock(&aux->cec.lock);409	if (!aux->cec.adap)410		goto unlock;411 412	cec_phys_addr_invalidate(aux->cec.adap);413	/*414	 * We're done if we want to keep the CEC device415	 * (drm_dp_cec_unregister_delay is >= NEVER_UNREG_DELAY) or if the416	 * DPCD still indicates the CEC capability (expected for an integrated417	 * HDMI branch device).418	 */419	if (drm_dp_cec_unregister_delay < NEVER_UNREG_DELAY &&420	    !drm_dp_cec_cap(aux, NULL)) {421		/*422		 * Unregister the CEC adapter after drm_dp_cec_unregister_delay423		 * seconds. This to debounce short HPD off-and-on cycles from424		 * displays.425		 */426		schedule_delayed_work(&aux->cec.unregister_work,427				      drm_dp_cec_unregister_delay * HZ);428	}429unlock:430	mutex_unlock(&aux->cec.lock);431}432EXPORT_SYMBOL(drm_dp_cec_unset_edid);433 434/**435 * drm_dp_cec_register_connector() - register a new connector436 * @aux: DisplayPort AUX channel437 * @connector: drm connector438 *439 * A new connector was registered with associated CEC adapter name and440 * CEC adapter parent device. After registering the name and parent441 * drm_dp_cec_set_edid() is called to check if the connector supports442 * CEC and to register a CEC adapter if that is the case.443 */444void drm_dp_cec_register_connector(struct drm_dp_aux *aux,445				   struct drm_connector *connector)446{447	WARN_ON(aux->cec.adap);448	if (WARN_ON(!aux->transfer))449		return;450	aux->cec.connector = connector;451	INIT_DELAYED_WORK(&aux->cec.unregister_work,452			  drm_dp_cec_unregister_work);453}454EXPORT_SYMBOL(drm_dp_cec_register_connector);455 456/**457 * drm_dp_cec_unregister_connector() - unregister the CEC adapter, if any458 * @aux: DisplayPort AUX channel459 */460void drm_dp_cec_unregister_connector(struct drm_dp_aux *aux)461{462	if (!aux->cec.adap)463		return;464	cancel_delayed_work_sync(&aux->cec.unregister_work);465	cec_unregister_adapter(aux->cec.adap);466	aux->cec.adap = NULL;467}468EXPORT_SYMBOL(drm_dp_cec_unregister_connector);469