dp_altmode.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. struct altmode_client_data altmode;
  26. const char *parent_name;
  27. bool connected;
  28. struct notifier_block nb;
  29. };
  30. enum dp_altmode_pin_assignment {
  31. DPAM_HPD_OUT,
  32. DPAM_HPD_A,
  33. DPAM_HPD_B,
  34. DPAM_HPD_C,
  35. DPAM_HPD_D,
  36. DPAM_HPD_E,
  37. DPAM_HPD_F,
  38. };
  39. static int dp_altmode_release_ss_lanes(struct dp_altmode_private *altmode)
  40. {
  41. int rc;
  42. struct device_node *np;
  43. struct device_node *usb_node;
  44. struct platform_device *usb_pdev;
  45. int timeout = 250;
  46. if (!altmode || !altmode->dev) {
  47. DP_ERR("invalid args\n");
  48. return -EINVAL;
  49. }
  50. np = altmode->dev->of_node;
  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_release_ss_lane(&usb_pdev->dev);
  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);
  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_VERBOSE(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. }
  131. goto ack;
  132. }
  133. /* Configure */
  134. if (!altmode->connected) {
  135. altmode->connected = true;
  136. altmode->dp_altmode.base.alt_mode_cfg_done = true;
  137. switch (orientation) {
  138. case 0:
  139. orientation = ORIENTATION_CC1;
  140. break;
  141. case 1:
  142. orientation = ORIENTATION_CC2;
  143. break;
  144. case 2:
  145. orientation = ORIENTATION_NONE;
  146. break;
  147. default:
  148. orientation = ORIENTATION_NONE;
  149. break;
  150. }
  151. altmode->dp_altmode.base.orientation = orientation;
  152. if (!altmode->dp_altmode.base.multi_func) {
  153. rc = dp_altmode_release_ss_lanes(altmode);
  154. if (rc)
  155. goto ack;
  156. }
  157. if (altmode->dp_cb && altmode->dp_cb->configure)
  158. altmode->dp_cb->configure(altmode->dev);
  159. goto ack;
  160. }
  161. /* Attention */
  162. if (altmode->forced_disconnect)
  163. goto ack;
  164. if (altmode->dp_cb && altmode->dp_cb->attention)
  165. altmode->dp_cb->attention(altmode->dev);
  166. ack:
  167. dp_altmode_send_pan_ack(altmode->amclient, port_index);
  168. return rc;
  169. }
  170. static int dp_altmode_register(struct notifier_block *nb, unsigned long ebt,
  171. void *pdev)
  172. {
  173. int rc;
  174. struct platform_device *altmode_pdev = pdev;
  175. struct dp_altmode_private *altmode = container_of(nb,
  176. struct dp_altmode_private, nb);
  177. altmode->amclient = altmode_register_client(&altmode_pdev->dev,
  178. &altmode->altmode);
  179. if (IS_ERR_OR_NULL(altmode->amclient)) {
  180. rc = PTR_ERR(altmode->amclient);
  181. DP_ERR("failed to register dp altmode client: %d\n", rc);
  182. return -EINVAL;
  183. }
  184. DP_DEBUG("success\n");
  185. return 0;
  186. }
  187. static int dp_altmode_simulate_connect(struct dp_hpd *dp_hpd, bool hpd)
  188. {
  189. struct dp_altmode *dp_altmode;
  190. struct dp_altmode_private *altmode;
  191. dp_altmode = container_of(dp_hpd, struct dp_altmode, base);
  192. altmode = container_of(dp_altmode, struct dp_altmode_private,
  193. dp_altmode);
  194. dp_altmode->base.hpd_high = hpd;
  195. altmode->forced_disconnect = !hpd;
  196. altmode->dp_altmode.base.alt_mode_cfg_done = hpd;
  197. if (hpd)
  198. altmode->dp_cb->configure(altmode->dev);
  199. else
  200. altmode->dp_cb->disconnect(altmode->dev);
  201. return 0;
  202. }
  203. static int dp_altmode_simulate_attention(struct dp_hpd *dp_hpd, int vdo)
  204. {
  205. struct dp_altmode *dp_altmode;
  206. struct dp_altmode_private *altmode;
  207. struct dp_altmode *status;
  208. dp_altmode = container_of(dp_hpd, struct dp_altmode, base);
  209. altmode = container_of(dp_altmode, struct dp_altmode_private,
  210. dp_altmode);
  211. status = &altmode->dp_altmode;
  212. status->base.hpd_high = (vdo & BIT(7)) ? true : false;
  213. status->base.hpd_irq = (vdo & BIT(8)) ? true : false;
  214. if (altmode->dp_cb && altmode->dp_cb->attention)
  215. altmode->dp_cb->attention(altmode->dev);
  216. return 0;
  217. }
  218. struct dp_hpd *dp_altmode_get(struct device *dev, struct dp_hpd_cb *cb)
  219. {
  220. int rc = 0;
  221. struct dp_altmode_private *altmode;
  222. struct dp_altmode *dp_altmode;
  223. const char *altmode_str = "qcom,altmode-parent";
  224. struct altmode_client_data client_data = {
  225. .callback = &dp_altmode_notify,
  226. };
  227. if (!cb) {
  228. DP_ERR("invalid cb data\n");
  229. return ERR_PTR(-EINVAL);
  230. }
  231. altmode = kzalloc(sizeof(*altmode), GFP_KERNEL);
  232. if (!altmode)
  233. return ERR_PTR(-ENOMEM);
  234. altmode->dev = dev;
  235. altmode->dp_cb = cb;
  236. client_data.name = "displayport";
  237. client_data.svid = USB_SID_DISPLAYPORT;
  238. client_data.priv = altmode;
  239. altmode->altmode = client_data;
  240. dp_altmode = &altmode->dp_altmode;
  241. dp_altmode->base.register_hpd = NULL;
  242. dp_altmode->base.simulate_connect = dp_altmode_simulate_connect;
  243. dp_altmode->base.simulate_attention = dp_altmode_simulate_attention;
  244. rc = of_property_read_string(altmode->dev->of_node, altmode_str,
  245. &altmode->parent_name);
  246. if (rc < 0) {
  247. DP_ERR("No altmode parent device specified\n");
  248. goto error;
  249. }
  250. DP_DEBUG("parent_name=%s\n", altmode->parent_name);
  251. altmode->nb.notifier_call = dp_altmode_register;
  252. rc = altmode_register_notifier(altmode->parent_name, &altmode->nb);
  253. if (rc < 0) {
  254. DP_ERR("altmode probe notifier registration failed: %d\n", rc);
  255. goto error;
  256. }
  257. DP_DEBUG("success\n");
  258. return &dp_altmode->base;
  259. error:
  260. kfree(altmode);
  261. return ERR_PTR(rc);
  262. }
  263. void dp_altmode_put(struct dp_hpd *dp_hpd)
  264. {
  265. struct dp_altmode *dp_altmode;
  266. struct dp_altmode_private *altmode;
  267. dp_altmode = container_of(dp_hpd, struct dp_altmode, base);
  268. if (!dp_altmode)
  269. return;
  270. altmode = container_of(dp_altmode, struct dp_altmode_private,
  271. dp_altmode);
  272. altmode_deregister_client(altmode->amclient);
  273. kfree(altmode);
  274. }