dp_altmode.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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_VERBOSE(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. switch (orientation) {
  135. case 0:
  136. orientation = ORIENTATION_CC1;
  137. break;
  138. case 1:
  139. orientation = ORIENTATION_CC2;
  140. break;
  141. case 2:
  142. orientation = ORIENTATION_NONE;
  143. break;
  144. default:
  145. orientation = ORIENTATION_NONE;
  146. break;
  147. }
  148. altmode->dp_altmode.base.orientation = orientation;
  149. if (!altmode->dp_altmode.base.multi_func) {
  150. rc = dp_altmode_release_ss_lanes(altmode);
  151. if (rc)
  152. goto ack;
  153. }
  154. if (altmode->dp_cb && altmode->dp_cb->configure)
  155. altmode->dp_cb->configure(altmode->dev);
  156. goto ack;
  157. }
  158. /* Attention */
  159. if (altmode->forced_disconnect)
  160. goto ack;
  161. if (altmode->dp_cb && altmode->dp_cb->attention)
  162. altmode->dp_cb->attention(altmode->dev);
  163. ack:
  164. dp_altmode_send_pan_ack(altmode->amclient, port_index);
  165. return rc;
  166. }
  167. static void dp_altmode_register(void *priv)
  168. {
  169. struct dp_altmode_private *altmode = priv;
  170. struct altmode_client_data cd = {
  171. .callback = &dp_altmode_notify,
  172. };
  173. cd.name = "displayport";
  174. cd.svid = USB_SID_DISPLAYPORT;
  175. cd.priv = altmode;
  176. altmode->amclient = altmode_register_client(altmode->dev, &cd);
  177. if (IS_ERR_OR_NULL(altmode->amclient))
  178. DP_ERR("failed to register as client: %d\n",
  179. PTR_ERR(altmode->amclient));
  180. else
  181. DP_DEBUG("success\n");
  182. }
  183. static int dp_altmode_simulate_connect(struct dp_hpd *dp_hpd, bool hpd)
  184. {
  185. struct dp_altmode *dp_altmode;
  186. struct dp_altmode_private *altmode;
  187. dp_altmode = container_of(dp_hpd, struct dp_altmode, base);
  188. altmode = container_of(dp_altmode, struct dp_altmode_private,
  189. dp_altmode);
  190. dp_altmode->base.hpd_high = hpd;
  191. altmode->forced_disconnect = !hpd;
  192. altmode->dp_altmode.base.alt_mode_cfg_done = hpd;
  193. if (hpd)
  194. altmode->dp_cb->configure(altmode->dev);
  195. else
  196. altmode->dp_cb->disconnect(altmode->dev);
  197. return 0;
  198. }
  199. static int dp_altmode_simulate_attention(struct dp_hpd *dp_hpd, int vdo)
  200. {
  201. struct dp_altmode *dp_altmode;
  202. struct dp_altmode_private *altmode;
  203. struct dp_altmode *status;
  204. dp_altmode = container_of(dp_hpd, struct dp_altmode, base);
  205. altmode = container_of(dp_altmode, struct dp_altmode_private,
  206. dp_altmode);
  207. status = &altmode->dp_altmode;
  208. status->base.hpd_high = (vdo & BIT(7)) ? true : false;
  209. status->base.hpd_irq = (vdo & BIT(8)) ? true : false;
  210. if (altmode->dp_cb && altmode->dp_cb->attention)
  211. altmode->dp_cb->attention(altmode->dev);
  212. return 0;
  213. }
  214. struct dp_hpd *dp_altmode_get(struct device *dev, struct dp_hpd_cb *cb)
  215. {
  216. int rc = 0;
  217. struct dp_altmode_private *altmode;
  218. struct dp_altmode *dp_altmode;
  219. if (!cb) {
  220. DP_ERR("invalid cb data\n");
  221. return ERR_PTR(-EINVAL);
  222. }
  223. altmode = kzalloc(sizeof(*altmode), GFP_KERNEL);
  224. if (!altmode)
  225. return ERR_PTR(-ENOMEM);
  226. altmode->dev = dev;
  227. altmode->dp_cb = cb;
  228. dp_altmode = &altmode->dp_altmode;
  229. dp_altmode->base.register_hpd = NULL;
  230. dp_altmode->base.simulate_connect = dp_altmode_simulate_connect;
  231. dp_altmode->base.simulate_attention = dp_altmode_simulate_attention;
  232. rc = altmode_register_notifier(dev, dp_altmode_register, altmode);
  233. if (rc < 0) {
  234. DP_ERR("altmode probe notifier registration failed: %d\n", rc);
  235. goto error;
  236. }
  237. DP_DEBUG("success\n");
  238. return &dp_altmode->base;
  239. error:
  240. kfree(altmode);
  241. return ERR_PTR(rc);
  242. }
  243. void dp_altmode_put(struct dp_hpd *dp_hpd)
  244. {
  245. struct dp_altmode *dp_altmode;
  246. struct dp_altmode_private *altmode;
  247. dp_altmode = container_of(dp_hpd, struct dp_altmode, base);
  248. if (!dp_altmode)
  249. return;
  250. altmode = container_of(dp_altmode, struct dp_altmode_private,
  251. dp_altmode);
  252. altmode_deregister_client(altmode->amclient);
  253. altmode_deregister_notifier(altmode->dev, altmode);
  254. kfree(altmode);
  255. }