drm_dp_cec.c 14 KB

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