otg.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * otg.c - ChipIdea USB IP core OTG driver
  3. *
  4. * Copyright (C) 2013 Freescale Semiconductor, Inc.
  5. *
  6. * Author: Peter Chen
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. /*
  13. * This file mainly handles otgsc register, OTG fsm operations for HNP and SRP
  14. * are also included.
  15. */
  16. #include <linux/usb/otg.h>
  17. #include <linux/usb/gadget.h>
  18. #include <linux/usb/chipidea.h>
  19. #include "ci.h"
  20. #include "bits.h"
  21. #include "otg.h"
  22. #include "otg_fsm.h"
  23. /**
  24. * hw_read_otgsc returns otgsc register bits value.
  25. * @mask: bitfield mask
  26. */
  27. u32 hw_read_otgsc(struct ci_hdrc *ci, u32 mask)
  28. {
  29. struct ci_hdrc_cable *cable;
  30. u32 val = hw_read(ci, OP_OTGSC, mask);
  31. /*
  32. * If using extcon framework for VBUS and/or ID signal
  33. * detection overwrite OTGSC register value
  34. */
  35. cable = &ci->platdata->vbus_extcon;
  36. if (!IS_ERR(cable->edev)) {
  37. if (cable->changed)
  38. val |= OTGSC_BSVIS;
  39. else
  40. val &= ~OTGSC_BSVIS;
  41. cable->changed = false;
  42. if (cable->state)
  43. val |= OTGSC_BSV;
  44. else
  45. val &= ~OTGSC_BSV;
  46. }
  47. cable = &ci->platdata->id_extcon;
  48. if (!IS_ERR(cable->edev)) {
  49. if (cable->changed)
  50. val |= OTGSC_IDIS;
  51. else
  52. val &= ~OTGSC_IDIS;
  53. cable->changed = false;
  54. if (cable->state)
  55. val |= OTGSC_ID;
  56. else
  57. val &= ~OTGSC_ID;
  58. }
  59. return val;
  60. }
  61. /**
  62. * hw_write_otgsc updates target bits of OTGSC register.
  63. * @mask: bitfield mask
  64. * @data: to be written
  65. */
  66. void hw_write_otgsc(struct ci_hdrc *ci, u32 mask, u32 data)
  67. {
  68. hw_write(ci, OP_OTGSC, mask | OTGSC_INT_STATUS_BITS, data);
  69. }
  70. /**
  71. * ci_otg_role - pick role based on ID pin state
  72. * @ci: the controller
  73. */
  74. enum ci_role ci_otg_role(struct ci_hdrc *ci)
  75. {
  76. enum ci_role role = hw_read_otgsc(ci, OTGSC_ID)
  77. ? CI_ROLE_GADGET
  78. : CI_ROLE_HOST;
  79. return role;
  80. }
  81. void ci_handle_vbus_change(struct ci_hdrc *ci)
  82. {
  83. if (!ci->is_otg)
  84. return;
  85. if (hw_read_otgsc(ci, OTGSC_BSV))
  86. usb_gadget_vbus_connect(&ci->gadget);
  87. else
  88. usb_gadget_vbus_disconnect(&ci->gadget);
  89. }
  90. #define CI_VBUS_STABLE_TIMEOUT_MS 5000
  91. static void ci_handle_id_switch(struct ci_hdrc *ci)
  92. {
  93. enum ci_role role = ci_otg_role(ci);
  94. if (role != ci->role) {
  95. dev_dbg(ci->dev, "switching from %s to %s\n",
  96. ci_role(ci)->name, ci->roles[role]->name);
  97. ci_role_stop(ci);
  98. if (role == CI_ROLE_GADGET)
  99. /* wait vbus lower than OTGSC_BSV */
  100. hw_wait_reg(ci, OP_OTGSC, OTGSC_BSV, 0,
  101. CI_VBUS_STABLE_TIMEOUT_MS);
  102. ci_role_start(ci, role);
  103. }
  104. }
  105. /**
  106. * ci_otg_work - perform otg (vbus/id) event handle
  107. * @work: work struct
  108. */
  109. static void ci_otg_work(struct work_struct *work)
  110. {
  111. struct ci_hdrc *ci = container_of(work, struct ci_hdrc, work);
  112. if (ci_otg_is_fsm_mode(ci) && !ci_otg_fsm_work(ci)) {
  113. enable_irq(ci->irq);
  114. return;
  115. }
  116. pm_runtime_get_sync(ci->dev);
  117. if (ci->id_event) {
  118. ci->id_event = false;
  119. ci_handle_id_switch(ci);
  120. } else if (ci->b_sess_valid_event) {
  121. ci->b_sess_valid_event = false;
  122. ci_handle_vbus_change(ci);
  123. } else
  124. dev_err(ci->dev, "unexpected event occurs at %s\n", __func__);
  125. pm_runtime_put_sync(ci->dev);
  126. enable_irq(ci->irq);
  127. }
  128. /**
  129. * ci_hdrc_otg_init - initialize otg struct
  130. * ci: the controller
  131. */
  132. int ci_hdrc_otg_init(struct ci_hdrc *ci)
  133. {
  134. INIT_WORK(&ci->work, ci_otg_work);
  135. ci->wq = create_freezable_workqueue("ci_otg");
  136. if (!ci->wq) {
  137. dev_err(ci->dev, "can't create workqueue\n");
  138. return -ENODEV;
  139. }
  140. if (ci_otg_is_fsm_mode(ci))
  141. return ci_hdrc_otg_fsm_init(ci);
  142. return 0;
  143. }
  144. /**
  145. * ci_hdrc_otg_destroy - destroy otg struct
  146. * ci: the controller
  147. */
  148. void ci_hdrc_otg_destroy(struct ci_hdrc *ci)
  149. {
  150. if (ci->wq) {
  151. flush_workqueue(ci->wq);
  152. destroy_workqueue(ci->wq);
  153. }
  154. /* Disable all OTG irq and clear status */
  155. hw_write_otgsc(ci, OTGSC_INT_EN_BITS | OTGSC_INT_STATUS_BITS,
  156. OTGSC_INT_STATUS_BITS);
  157. if (ci_otg_is_fsm_mode(ci))
  158. ci_hdrc_otg_fsm_remove(ci);
  159. }