dp_altmode.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/slab.h>
  6. #include <linux/device.h>
  7. #include <linux/delay.h>
  8. #include <linux/module.h>
  9. #include <linux/kthread.h>
  10. #include <linux/soc/qcom/altmode-glink.h>
  11. #include <linux/usb/dwc3-msm.h>
  12. #include <linux/usb/pd_vdo.h>
  13. #include "dp_altmode.h"
  14. #include "dp_debug.h"
  15. #include "sde_dbg.h"
  16. #define ALTMODE_CONFIGURE_MASK (0x3f)
  17. #define ALTMODE_HPD_STATE_MASK (0x40)
  18. #define ALTMODE_HPD_IRQ_MASK (0x80)
  19. struct dp_altmode_private {
  20. bool forced_disconnect;
  21. struct device *dev;
  22. struct dp_hpd_cb *dp_cb;
  23. struct dp_altmode dp_altmode;
  24. struct altmode_client *amclient;
  25. bool connected;
  26. };
  27. enum dp_altmode_pin_assignment {
  28. DPAM_HPD_OUT,
  29. DPAM_HPD_A,
  30. DPAM_HPD_B,
  31. DPAM_HPD_C,
  32. DPAM_HPD_D,
  33. DPAM_HPD_E,
  34. DPAM_HPD_F,
  35. };
  36. static int dp_altmode_set_usb_dp_mode(struct dp_altmode_private *altmode)
  37. {
  38. int rc;
  39. struct device_node *np;
  40. struct device_node *usb_node;
  41. struct platform_device *usb_pdev;
  42. int timeout = 250;
  43. u32 lanes = 4;
  44. if (!altmode || !altmode->dev) {
  45. DP_ERR("invalid args\n");
  46. return -EINVAL;
  47. }
  48. np = altmode->dev->of_node;
  49. if (altmode->connected && altmode->dp_altmode.base.force_multi_func)
  50. lanes = 2;
  51. usb_node = of_parse_phandle(np, "usb-controller", 0);
  52. if (!usb_node) {
  53. DP_ERR("unable to get usb node\n");
  54. return -EINVAL;
  55. }
  56. usb_pdev = of_find_device_by_node(usb_node);
  57. if (!usb_pdev) {
  58. of_node_put(usb_node);
  59. DP_ERR("unable to get usb pdev\n");
  60. return -EINVAL;
  61. }
  62. while (timeout) {
  63. rc = dwc3_msm_set_dp_mode(&usb_pdev->dev, altmode->connected, lanes);
  64. if (rc != -EBUSY)
  65. break;
  66. DP_WARN("USB busy, retry\n");
  67. /* wait for hw recommended delay for usb */
  68. msleep(20);
  69. timeout--;
  70. }
  71. of_node_put(usb_node);
  72. platform_device_put(usb_pdev);
  73. if (rc)
  74. DP_ERR("Error releasing SS lanes: %d\n", rc);
  75. return rc;
  76. }
  77. static void dp_altmode_send_pan_ack(struct altmode_client *amclient,
  78. u8 port_index)
  79. {
  80. int rc;
  81. struct altmode_pan_ack_msg ack;
  82. ack.cmd_type = ALTMODE_PAN_ACK;
  83. ack.port_index = port_index;
  84. rc = altmode_send_data(amclient, &ack, sizeof(ack));
  85. if (rc < 0) {
  86. DP_ERR("failed: %d\n", rc);
  87. return;
  88. }
  89. DP_DEBUG("port=%d\n", port_index);
  90. }
  91. static int dp_altmode_notify(void *priv, void *data, size_t len)
  92. {
  93. int rc = 0;
  94. struct dp_altmode_private *altmode =
  95. (struct dp_altmode_private *) priv;
  96. u8 port_index, dp_data, orientation;
  97. u8 *payload = (u8 *) data;
  98. u8 pin, hpd_state, hpd_irq;
  99. bool force_multi_func = altmode->dp_altmode.base.force_multi_func;
  100. port_index = payload[0];
  101. orientation = payload[1];
  102. dp_data = payload[8];
  103. pin = dp_data & ALTMODE_CONFIGURE_MASK;
  104. hpd_state = (dp_data & ALTMODE_HPD_STATE_MASK) >> 6;
  105. hpd_irq = (dp_data & ALTMODE_HPD_IRQ_MASK) >> 7;
  106. altmode->dp_altmode.base.hpd_high = !!hpd_state;
  107. altmode->dp_altmode.base.hpd_irq = !!hpd_irq;
  108. altmode->dp_altmode.base.multi_func = force_multi_func ? true :
  109. !(pin == DPAM_HPD_C || pin == DPAM_HPD_E || pin == DPAM_HPD_OUT);
  110. DP_DEBUG("payload=0x%x\n", dp_data);
  111. DP_DEBUG("port_index=%d, orientation=%d, pin=%d, hpd_state=%d\n",
  112. port_index, orientation, pin, hpd_state);
  113. DP_DEBUG("multi_func=%d, hpd_high=%d, hpd_irq=%d\n",
  114. altmode->dp_altmode.base.multi_func,
  115. altmode->dp_altmode.base.hpd_high,
  116. altmode->dp_altmode.base.hpd_irq);
  117. DP_DEBUG("connected=%d\n", altmode->connected);
  118. SDE_EVT32_EXTERNAL(dp_data, port_index, orientation, pin, hpd_state,
  119. altmode->dp_altmode.base.multi_func,
  120. altmode->dp_altmode.base.hpd_high,
  121. altmode->dp_altmode.base.hpd_irq, altmode->connected);
  122. if (!pin) {
  123. /* Cable detach */
  124. if (altmode->connected) {
  125. altmode->connected = false;
  126. altmode->dp_altmode.base.alt_mode_cfg_done = false;
  127. altmode->dp_altmode.base.orientation = ORIENTATION_NONE;
  128. if (altmode->dp_cb && altmode->dp_cb->disconnect)
  129. altmode->dp_cb->disconnect(altmode->dev);
  130. rc = dp_altmode_set_usb_dp_mode(altmode);
  131. if (rc)
  132. DP_ERR("failed to clear usb dp mode, rc: %d\n", rc);
  133. }
  134. goto ack;
  135. }
  136. /* Configure */
  137. if (!altmode->connected) {
  138. altmode->connected = true;
  139. altmode->dp_altmode.base.alt_mode_cfg_done = true;
  140. altmode->forced_disconnect = false;
  141. switch (orientation) {
  142. case 0:
  143. orientation = ORIENTATION_CC1;
  144. break;
  145. case 1:
  146. orientation = ORIENTATION_CC2;
  147. break;
  148. case 2:
  149. orientation = ORIENTATION_NONE;
  150. break;
  151. default:
  152. orientation = ORIENTATION_NONE;
  153. break;
  154. }
  155. altmode->dp_altmode.base.orientation = orientation;
  156. rc = dp_altmode_set_usb_dp_mode(altmode);
  157. if (rc)
  158. goto ack;
  159. if (altmode->dp_cb && altmode->dp_cb->configure)
  160. altmode->dp_cb->configure(altmode->dev);
  161. goto ack;
  162. }
  163. /* Attention */
  164. if (altmode->forced_disconnect)
  165. goto ack;
  166. if (altmode->dp_cb && altmode->dp_cb->attention)
  167. altmode->dp_cb->attention(altmode->dev);
  168. ack:
  169. dp_altmode_send_pan_ack(altmode->amclient, port_index);
  170. return rc;
  171. }
  172. static void dp_altmode_register(void *priv)
  173. {
  174. struct dp_altmode_private *altmode = priv;
  175. struct altmode_client_data cd = {
  176. .callback = &dp_altmode_notify,
  177. };
  178. cd.name = "displayport";
  179. cd.svid = USB_SID_DISPLAYPORT;
  180. cd.priv = altmode;
  181. altmode->amclient = altmode_register_client(altmode->dev, &cd);
  182. if (IS_ERR_OR_NULL(altmode->amclient))
  183. DP_ERR("failed to register as client: %ld\n",
  184. PTR_ERR(altmode->amclient));
  185. else
  186. DP_DEBUG("success\n");
  187. }
  188. static int dp_altmode_simulate_connect(struct dp_hpd *dp_hpd, bool hpd)
  189. {
  190. struct dp_altmode *dp_altmode;
  191. struct dp_altmode_private *altmode;
  192. dp_altmode = container_of(dp_hpd, struct dp_altmode, base);
  193. altmode = container_of(dp_altmode, struct dp_altmode_private,
  194. dp_altmode);
  195. dp_altmode->base.hpd_high = hpd;
  196. altmode->forced_disconnect = !hpd;
  197. altmode->dp_altmode.base.alt_mode_cfg_done = hpd;
  198. if (hpd)
  199. altmode->dp_cb->configure(altmode->dev);
  200. else
  201. altmode->dp_cb->disconnect(altmode->dev);
  202. return 0;
  203. }
  204. static int dp_altmode_simulate_attention(struct dp_hpd *dp_hpd, int vdo)
  205. {
  206. struct dp_altmode *dp_altmode;
  207. struct dp_altmode_private *altmode;
  208. struct dp_altmode *status;
  209. dp_altmode = container_of(dp_hpd, struct dp_altmode, base);
  210. altmode = container_of(dp_altmode, struct dp_altmode_private,
  211. dp_altmode);
  212. status = &altmode->dp_altmode;
  213. status->base.hpd_high = (vdo & BIT(7)) ? true : false;
  214. status->base.hpd_irq = (vdo & BIT(8)) ? true : false;
  215. if (altmode->dp_cb && altmode->dp_cb->attention)
  216. altmode->dp_cb->attention(altmode->dev);
  217. return 0;
  218. }
  219. struct dp_hpd *dp_altmode_get(struct device *dev, struct dp_hpd_cb *cb)
  220. {
  221. int rc = 0;
  222. struct dp_altmode_private *altmode;
  223. struct dp_altmode *dp_altmode;
  224. if (!cb) {
  225. DP_ERR("invalid cb data\n");
  226. return ERR_PTR(-EINVAL);
  227. }
  228. altmode = kzalloc(sizeof(*altmode), GFP_KERNEL);
  229. if (!altmode)
  230. return ERR_PTR(-ENOMEM);
  231. altmode->dev = dev;
  232. altmode->dp_cb = cb;
  233. dp_altmode = &altmode->dp_altmode;
  234. dp_altmode->base.register_hpd = NULL;
  235. dp_altmode->base.simulate_connect = dp_altmode_simulate_connect;
  236. dp_altmode->base.simulate_attention = dp_altmode_simulate_attention;
  237. rc = altmode_register_notifier(dev, dp_altmode_register, altmode);
  238. if (rc < 0) {
  239. DP_ERR("altmode probe notifier registration failed: %d\n", rc);
  240. goto error;
  241. }
  242. DP_DEBUG("success\n");
  243. return &dp_altmode->base;
  244. error:
  245. kfree(altmode);
  246. return ERR_PTR(rc);
  247. }
  248. void dp_altmode_put(struct dp_hpd *dp_hpd)
  249. {
  250. struct dp_altmode *dp_altmode;
  251. struct dp_altmode_private *altmode;
  252. dp_altmode = container_of(dp_hpd, struct dp_altmode, base);
  253. if (!dp_altmode)
  254. return;
  255. altmode = container_of(dp_altmode, struct dp_altmode_private,
  256. dp_altmode);
  257. altmode_deregister_client(altmode->amclient);
  258. altmode_deregister_notifier(altmode->dev, altmode);
  259. kfree(altmode);
  260. }