dp_altmode.c 7.7 KB

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