drm_dp_cec.c 13 KB

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