otg.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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, it may include OTG operation
  14. * in the future.
  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. /**
  23. * hw_read_otgsc returns otgsc register bits value.
  24. * @mask: bitfield mask
  25. */
  26. u32 hw_read_otgsc(struct ci_hdrc *ci, u32 mask)
  27. {
  28. return hw_read(ci, OP_OTGSC, mask);
  29. }
  30. /**
  31. * hw_write_otgsc updates target bits of OTGSC register.
  32. * @mask: bitfield mask
  33. * @data: to be written
  34. */
  35. void hw_write_otgsc(struct ci_hdrc *ci, u32 mask, u32 data)
  36. {
  37. hw_write(ci, OP_OTGSC, mask | OTGSC_INT_STATUS_BITS, data);
  38. }
  39. /**
  40. * ci_otg_role - pick role based on ID pin state
  41. * @ci: the controller
  42. */
  43. enum ci_role ci_otg_role(struct ci_hdrc *ci)
  44. {
  45. enum ci_role role = hw_read_otgsc(ci, OTGSC_ID)
  46. ? CI_ROLE_GADGET
  47. : CI_ROLE_HOST;
  48. return role;
  49. }
  50. void ci_handle_vbus_change(struct ci_hdrc *ci)
  51. {
  52. if (!ci->is_otg)
  53. return;
  54. if (hw_read_otgsc(ci, OTGSC_BSV))
  55. usb_gadget_vbus_connect(&ci->gadget);
  56. else
  57. usb_gadget_vbus_disconnect(&ci->gadget);
  58. }
  59. #define CI_VBUS_STABLE_TIMEOUT_MS 5000
  60. static void ci_handle_id_switch(struct ci_hdrc *ci)
  61. {
  62. enum ci_role role = ci_otg_role(ci);
  63. if (role != ci->role) {
  64. dev_dbg(ci->dev, "switching from %s to %s\n",
  65. ci_role(ci)->name, ci->roles[role]->name);
  66. ci_role_stop(ci);
  67. /* wait vbus lower than OTGSC_BSV */
  68. hw_wait_reg(ci, OP_OTGSC, OTGSC_BSV, 0,
  69. CI_VBUS_STABLE_TIMEOUT_MS);
  70. ci_role_start(ci, role);
  71. }
  72. }
  73. /**
  74. * ci_otg_work - perform otg (vbus/id) event handle
  75. * @work: work struct
  76. */
  77. static void ci_otg_work(struct work_struct *work)
  78. {
  79. struct ci_hdrc *ci = container_of(work, struct ci_hdrc, work);
  80. if (ci->id_event) {
  81. ci->id_event = false;
  82. ci_handle_id_switch(ci);
  83. } else if (ci->b_sess_valid_event) {
  84. ci->b_sess_valid_event = false;
  85. ci_handle_vbus_change(ci);
  86. } else
  87. dev_err(ci->dev, "unexpected event occurs at %s\n", __func__);
  88. enable_irq(ci->irq);
  89. }
  90. /**
  91. * ci_hdrc_otg_init - initialize otg struct
  92. * ci: the controller
  93. */
  94. int ci_hdrc_otg_init(struct ci_hdrc *ci)
  95. {
  96. INIT_WORK(&ci->work, ci_otg_work);
  97. ci->wq = create_singlethread_workqueue("ci_otg");
  98. if (!ci->wq) {
  99. dev_err(ci->dev, "can't create workqueue\n");
  100. return -ENODEV;
  101. }
  102. return 0;
  103. }
  104. /**
  105. * ci_hdrc_otg_destroy - destroy otg struct
  106. * ci: the controller
  107. */
  108. void ci_hdrc_otg_destroy(struct ci_hdrc *ci)
  109. {
  110. if (ci->wq) {
  111. flush_workqueue(ci->wq);
  112. destroy_workqueue(ci->wq);
  113. }
  114. /* Disable all OTG irq and clear status */
  115. hw_write_otgsc(ci, OTGSC_INT_EN_BITS | OTGSC_INT_STATUS_BITS,
  116. OTGSC_INT_STATUS_BITS);
  117. }