drm_dp_cec.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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/drm_connector.h>
  12. #include <drm/drm_device.h>
  13. #include <drm/drm_dp_helper.h>
  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. * DOC: dp cec helpers
  50. *
  51. * These functions take care of supporting the CEC-Tunneling-over-AUX
  52. * feature of DisplayPort-to-HDMI adapters.
  53. */
  54. /*
  55. * When the EDID is unset because the HPD went low, then the CEC DPCD registers
  56. * typically can no longer be read (true for a DP-to-HDMI adapter since it is
  57. * powered by the HPD). However, some displays toggle the HPD off and on for a
  58. * short period for one reason or another, and that would cause the CEC adapter
  59. * to be removed and added again, even though nothing else changed.
  60. *
  61. * This module parameter sets a delay in seconds before the CEC adapter is
  62. * actually unregistered. Only if the HPD does not return within that time will
  63. * the CEC adapter be unregistered.
  64. *
  65. * If it is set to a value >= NEVER_UNREG_DELAY, then the CEC adapter will never
  66. * be unregistered for as long as the connector remains registered.
  67. *
  68. * If it is set to 0, then the CEC adapter will be unregistered immediately as
  69. * soon as the HPD disappears.
  70. *
  71. * The default is one second to prevent short HPD glitches from unregistering
  72. * the CEC adapter.
  73. *
  74. * Note that for integrated HDMI branch devices that support CEC the DPCD
  75. * registers remain available even if the HPD goes low since it is not powered
  76. * by the HPD. In that case the CEC adapter will never be unregistered during
  77. * the life time of the connector. At least, this is the theory since I do not
  78. * have hardware with an integrated HDMI branch device that supports CEC.
  79. */
  80. #define NEVER_UNREG_DELAY 1000
  81. static unsigned int drm_dp_cec_unregister_delay = 1;
  82. module_param(drm_dp_cec_unregister_delay, uint, 0600);
  83. MODULE_PARM_DESC(drm_dp_cec_unregister_delay,
  84. "CEC unregister delay in seconds, 0: no delay, >= 1000: never unregister");
  85. static int drm_dp_cec_adap_enable(struct cec_adapter *adap, bool enable)
  86. {
  87. struct drm_dp_aux *aux = cec_get_drvdata(adap);
  88. u32 val = enable ? DP_CEC_TUNNELING_ENABLE : 0;
  89. ssize_t err = 0;
  90. err = drm_dp_dpcd_writeb(aux, DP_CEC_TUNNELING_CONTROL, val);
  91. return (enable && err < 0) ? err : 0;
  92. }
  93. static int drm_dp_cec_adap_log_addr(struct cec_adapter *adap, u8 addr)
  94. {
  95. struct drm_dp_aux *aux = cec_get_drvdata(adap);
  96. /* Bit 15 (logical address 15) should always be set */
  97. u16 la_mask = 1 << CEC_LOG_ADDR_BROADCAST;
  98. u8 mask[2];
  99. ssize_t err;
  100. if (addr != CEC_LOG_ADDR_INVALID)
  101. la_mask |= adap->log_addrs.log_addr_mask | (1 << addr);
  102. mask[0] = la_mask & 0xff;
  103. mask[1] = la_mask >> 8;
  104. err = drm_dp_dpcd_write(aux, DP_CEC_LOGICAL_ADDRESS_MASK, mask, 2);
  105. return (addr != CEC_LOG_ADDR_INVALID && err < 0) ? err : 0;
  106. }
  107. static int drm_dp_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
  108. u32 signal_free_time, struct cec_msg *msg)
  109. {
  110. struct drm_dp_aux *aux = cec_get_drvdata(adap);
  111. unsigned int retries = min(5, attempts - 1);
  112. ssize_t err;
  113. err = drm_dp_dpcd_write(aux, DP_CEC_TX_MESSAGE_BUFFER,
  114. msg->msg, msg->len);
  115. if (err < 0)
  116. return err;
  117. err = drm_dp_dpcd_writeb(aux, DP_CEC_TX_MESSAGE_INFO,
  118. (msg->len - 1) | (retries << 4) |
  119. DP_CEC_TX_MESSAGE_SEND);
  120. return err < 0 ? err : 0;
  121. }
  122. static int drm_dp_cec_adap_monitor_all_enable(struct cec_adapter *adap,
  123. bool enable)
  124. {
  125. struct drm_dp_aux *aux = cec_get_drvdata(adap);
  126. ssize_t err;
  127. u8 val;
  128. if (!(adap->capabilities & CEC_CAP_MONITOR_ALL))
  129. return 0;
  130. err = drm_dp_dpcd_readb(aux, DP_CEC_TUNNELING_CONTROL, &val);
  131. if (err >= 0) {
  132. if (enable)
  133. val |= DP_CEC_SNOOPING_ENABLE;
  134. else
  135. val &= ~DP_CEC_SNOOPING_ENABLE;
  136. err = drm_dp_dpcd_writeb(aux, DP_CEC_TUNNELING_CONTROL, val);
  137. }
  138. return (enable && err < 0) ? err : 0;
  139. }
  140. static void drm_dp_cec_adap_status(struct cec_adapter *adap,
  141. struct seq_file *file)
  142. {
  143. struct drm_dp_aux *aux = cec_get_drvdata(adap);
  144. struct drm_dp_desc desc;
  145. struct drm_dp_dpcd_ident *id = &desc.ident;
  146. if (drm_dp_read_desc(aux, &desc, true))
  147. return;
  148. seq_printf(file, "OUI: %*phD\n",
  149. (int)sizeof(id->oui), id->oui);
  150. seq_printf(file, "ID: %*pE\n",
  151. (int)strnlen(id->device_id, sizeof(id->device_id)),
  152. id->device_id);
  153. seq_printf(file, "HW Rev: %d.%d\n", id->hw_rev >> 4, id->hw_rev & 0xf);
  154. /*
  155. * Show this both in decimal and hex: at least one vendor
  156. * always reports this in hex.
  157. */
  158. seq_printf(file, "FW/SW Rev: %d.%d (0x%02x.0x%02x)\n",
  159. id->sw_major_rev, id->sw_minor_rev,
  160. id->sw_major_rev, id->sw_minor_rev);
  161. }
  162. static const struct cec_adap_ops drm_dp_cec_adap_ops = {
  163. .adap_enable = drm_dp_cec_adap_enable,
  164. .adap_log_addr = drm_dp_cec_adap_log_addr,
  165. .adap_transmit = drm_dp_cec_adap_transmit,
  166. .adap_monitor_all_enable = drm_dp_cec_adap_monitor_all_enable,
  167. .adap_status = drm_dp_cec_adap_status,
  168. };
  169. static int drm_dp_cec_received(struct drm_dp_aux *aux)
  170. {
  171. struct cec_adapter *adap = aux->cec.adap;
  172. struct cec_msg msg;
  173. u8 rx_msg_info;
  174. ssize_t err;
  175. err = drm_dp_dpcd_readb(aux, DP_CEC_RX_MESSAGE_INFO, &rx_msg_info);
  176. if (err < 0)
  177. return err;
  178. if (!(rx_msg_info & DP_CEC_RX_MESSAGE_ENDED))
  179. return 0;
  180. msg.len = (rx_msg_info & DP_CEC_RX_MESSAGE_LEN_MASK) + 1;
  181. err = drm_dp_dpcd_read(aux, DP_CEC_RX_MESSAGE_BUFFER, msg.msg, msg.len);
  182. if (err < 0)
  183. return err;
  184. cec_received_msg(adap, &msg);
  185. return 0;
  186. }
  187. static void drm_dp_cec_handle_irq(struct drm_dp_aux *aux)
  188. {
  189. struct cec_adapter *adap = aux->cec.adap;
  190. u8 flags;
  191. if (drm_dp_dpcd_readb(aux, DP_CEC_TUNNELING_IRQ_FLAGS, &flags) < 0)
  192. return;
  193. if (flags & DP_CEC_RX_MESSAGE_INFO_VALID)
  194. drm_dp_cec_received(aux);
  195. if (flags & DP_CEC_TX_MESSAGE_SENT)
  196. cec_transmit_attempt_done(adap, CEC_TX_STATUS_OK);
  197. else if (flags & DP_CEC_TX_LINE_ERROR)
  198. cec_transmit_attempt_done(adap, CEC_TX_STATUS_ERROR |
  199. CEC_TX_STATUS_MAX_RETRIES);
  200. else if (flags &
  201. (DP_CEC_TX_ADDRESS_NACK_ERROR | DP_CEC_TX_DATA_NACK_ERROR))
  202. cec_transmit_attempt_done(adap, CEC_TX_STATUS_NACK |
  203. CEC_TX_STATUS_MAX_RETRIES);
  204. drm_dp_dpcd_writeb(aux, DP_CEC_TUNNELING_IRQ_FLAGS, flags);
  205. }
  206. /**
  207. * drm_dp_cec_irq() - handle CEC interrupt, if any
  208. * @aux: DisplayPort AUX channel
  209. *
  210. * Should be called when handling an IRQ_HPD request. If CEC-tunneling-over-AUX
  211. * is present, then it will check for a CEC_IRQ and handle it accordingly.
  212. */
  213. void drm_dp_cec_irq(struct drm_dp_aux *aux)
  214. {
  215. u8 cec_irq;
  216. int ret;
  217. /* No transfer function was set, so not a DP connector */
  218. if (!aux->transfer)
  219. return;
  220. mutex_lock(&aux->cec.lock);
  221. if (!aux->cec.adap)
  222. goto unlock;
  223. ret = drm_dp_dpcd_readb(aux, DP_DEVICE_SERVICE_IRQ_VECTOR_ESI1,
  224. &cec_irq);
  225. if (ret < 0 || !(cec_irq & DP_CEC_IRQ))
  226. goto unlock;
  227. drm_dp_cec_handle_irq(aux);
  228. drm_dp_dpcd_writeb(aux, DP_DEVICE_SERVICE_IRQ_VECTOR_ESI1, DP_CEC_IRQ);
  229. unlock:
  230. mutex_unlock(&aux->cec.lock);
  231. }
  232. EXPORT_SYMBOL(drm_dp_cec_irq);
  233. static bool drm_dp_cec_cap(struct drm_dp_aux *aux, u8 *cec_cap)
  234. {
  235. u8 cap = 0;
  236. if (drm_dp_dpcd_readb(aux, DP_CEC_TUNNELING_CAPABILITY, &cap) != 1 ||
  237. !(cap & DP_CEC_TUNNELING_CAPABLE))
  238. return false;
  239. if (cec_cap)
  240. *cec_cap = cap;
  241. return true;
  242. }
  243. /*
  244. * Called if the HPD was low for more than drm_dp_cec_unregister_delay
  245. * seconds. This unregisters the CEC adapter.
  246. */
  247. static void drm_dp_cec_unregister_work(struct work_struct *work)
  248. {
  249. struct drm_dp_aux *aux = container_of(work, struct drm_dp_aux,
  250. cec.unregister_work.work);
  251. mutex_lock(&aux->cec.lock);
  252. cec_unregister_adapter(aux->cec.adap);
  253. aux->cec.adap = NULL;
  254. mutex_unlock(&aux->cec.lock);
  255. }
  256. /*
  257. * A new EDID is set. If there is no CEC adapter, then create one. If
  258. * there was a CEC adapter, then check if the CEC adapter properties
  259. * were unchanged and just update the CEC physical address. Otherwise
  260. * unregister the old CEC adapter and create a new one.
  261. */
  262. void drm_dp_cec_set_edid(struct drm_dp_aux *aux, const struct edid *edid)
  263. {
  264. struct drm_connector *connector = aux->cec.connector;
  265. u32 cec_caps = CEC_CAP_DEFAULTS | CEC_CAP_NEEDS_HPD |
  266. CEC_CAP_CONNECTOR_INFO;
  267. struct cec_connector_info conn_info;
  268. unsigned int num_las = 1;
  269. u8 cap;
  270. /* No transfer function was set, so not a DP connector */
  271. if (!aux->transfer)
  272. return;
  273. #ifndef CONFIG_MEDIA_CEC_RC
  274. /*
  275. * CEC_CAP_RC is part of CEC_CAP_DEFAULTS, but it is stripped by
  276. * cec_allocate_adapter() if CONFIG_MEDIA_CEC_RC is undefined.
  277. *
  278. * Do this here as well to ensure the tests against cec_caps are
  279. * correct.
  280. */
  281. cec_caps &= ~CEC_CAP_RC;
  282. #endif
  283. cancel_delayed_work_sync(&aux->cec.unregister_work);
  284. mutex_lock(&aux->cec.lock);
  285. if (!drm_dp_cec_cap(aux, &cap)) {
  286. /* CEC is not supported, unregister any existing adapter */
  287. cec_unregister_adapter(aux->cec.adap);
  288. aux->cec.adap = NULL;
  289. goto unlock;
  290. }
  291. if (cap & DP_CEC_SNOOPING_CAPABLE)
  292. cec_caps |= CEC_CAP_MONITOR_ALL;
  293. if (cap & DP_CEC_MULTIPLE_LA_CAPABLE)
  294. num_las = CEC_MAX_LOG_ADDRS;
  295. if (aux->cec.adap) {
  296. if (aux->cec.adap->capabilities == cec_caps &&
  297. aux->cec.adap->available_log_addrs == num_las) {
  298. /* Unchanged, so just set the phys addr */
  299. cec_s_phys_addr_from_edid(aux->cec.adap, edid);
  300. goto unlock;
  301. }
  302. /*
  303. * The capabilities changed, so unregister the old
  304. * adapter first.
  305. */
  306. cec_unregister_adapter(aux->cec.adap);
  307. }
  308. /* Create a new adapter */
  309. aux->cec.adap = cec_allocate_adapter(&drm_dp_cec_adap_ops,
  310. aux, connector->name, cec_caps,
  311. num_las);
  312. if (IS_ERR(aux->cec.adap)) {
  313. aux->cec.adap = NULL;
  314. goto unlock;
  315. }
  316. cec_fill_conn_info_from_drm(&conn_info, connector);
  317. cec_s_conn_info(aux->cec.adap, &conn_info);
  318. if (cec_register_adapter(aux->cec.adap, connector->dev->dev)) {
  319. cec_delete_adapter(aux->cec.adap);
  320. aux->cec.adap = NULL;
  321. } else {
  322. /*
  323. * Update the phys addr for the new CEC adapter. When called
  324. * from drm_dp_cec_register_connector() edid == NULL, so in
  325. * that case the phys addr is just invalidated.
  326. */
  327. cec_s_phys_addr_from_edid(aux->cec.adap, edid);
  328. }
  329. unlock:
  330. mutex_unlock(&aux->cec.lock);
  331. }
  332. EXPORT_SYMBOL(drm_dp_cec_set_edid);
  333. /*
  334. * The EDID disappeared (likely because of the HPD going down).
  335. */
  336. void drm_dp_cec_unset_edid(struct drm_dp_aux *aux)
  337. {
  338. /* No transfer function was set, so not a DP connector */
  339. if (!aux->transfer)
  340. return;
  341. cancel_delayed_work_sync(&aux->cec.unregister_work);
  342. mutex_lock(&aux->cec.lock);
  343. if (!aux->cec.adap)
  344. goto unlock;
  345. cec_phys_addr_invalidate(aux->cec.adap);
  346. /*
  347. * We're done if we want to keep the CEC device
  348. * (drm_dp_cec_unregister_delay is >= NEVER_UNREG_DELAY) or if the
  349. * DPCD still indicates the CEC capability (expected for an integrated
  350. * HDMI branch device).
  351. */
  352. if (drm_dp_cec_unregister_delay < NEVER_UNREG_DELAY &&
  353. !drm_dp_cec_cap(aux, NULL)) {
  354. /*
  355. * Unregister the CEC adapter after drm_dp_cec_unregister_delay
  356. * seconds. This to debounce short HPD off-and-on cycles from
  357. * displays.
  358. */
  359. schedule_delayed_work(&aux->cec.unregister_work,
  360. drm_dp_cec_unregister_delay * HZ);
  361. }
  362. unlock:
  363. mutex_unlock(&aux->cec.lock);
  364. }
  365. EXPORT_SYMBOL(drm_dp_cec_unset_edid);
  366. /**
  367. * drm_dp_cec_register_connector() - register a new connector
  368. * @aux: DisplayPort AUX channel
  369. * @connector: drm connector
  370. *
  371. * A new connector was registered with associated CEC adapter name and
  372. * CEC adapter parent device. After registering the name and parent
  373. * drm_dp_cec_set_edid() is called to check if the connector supports
  374. * CEC and to register a CEC adapter if that is the case.
  375. */
  376. void drm_dp_cec_register_connector(struct drm_dp_aux *aux,
  377. struct drm_connector *connector)
  378. {
  379. WARN_ON(aux->cec.adap);
  380. if (WARN_ON(!aux->transfer))
  381. return;
  382. aux->cec.connector = connector;
  383. INIT_DELAYED_WORK(&aux->cec.unregister_work,
  384. drm_dp_cec_unregister_work);
  385. }
  386. EXPORT_SYMBOL(drm_dp_cec_register_connector);
  387. /**
  388. * drm_dp_cec_unregister_connector() - unregister the CEC adapter, if any
  389. * @aux: DisplayPort AUX channel
  390. */
  391. void drm_dp_cec_unregister_connector(struct drm_dp_aux *aux)
  392. {
  393. if (!aux->cec.adap)
  394. return;
  395. cancel_delayed_work_sync(&aux->cec.unregister_work);
  396. cec_unregister_adapter(aux->cec.adap);
  397. aux->cec.adap = NULL;
  398. }
  399. EXPORT_SYMBOL(drm_dp_cec_unregister_connector);