drm_dp_cec.c 13 KB

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