dp_altmode.c 7.4 KB

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