otg.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. * ci_otg_role - pick role based on ID pin state
  24. * @ci: the controller
  25. */
  26. enum ci_role ci_otg_role(struct ci_hdrc *ci)
  27. {
  28. u32 sts = hw_read(ci, OP_OTGSC, ~0);
  29. enum ci_role role = sts & OTGSC_ID
  30. ? CI_ROLE_GADGET
  31. : CI_ROLE_HOST;
  32. return role;
  33. }
  34. void ci_handle_vbus_change(struct ci_hdrc *ci)
  35. {
  36. u32 otgsc;
  37. if (!ci->is_otg)
  38. return;
  39. otgsc = hw_read(ci, OP_OTGSC, ~0);
  40. if (otgsc & OTGSC_BSV)
  41. usb_gadget_vbus_connect(&ci->gadget);
  42. else
  43. usb_gadget_vbus_disconnect(&ci->gadget);
  44. }
  45. #define CI_VBUS_STABLE_TIMEOUT_MS 5000
  46. static void ci_handle_id_switch(struct ci_hdrc *ci)
  47. {
  48. enum ci_role role = ci_otg_role(ci);
  49. if (role != ci->role) {
  50. dev_dbg(ci->dev, "switching from %s to %s\n",
  51. ci_role(ci)->name, ci->roles[role]->name);
  52. ci_role_stop(ci);
  53. /* wait vbus lower than OTGSC_BSV */
  54. hw_wait_reg(ci, OP_OTGSC, OTGSC_BSV, 0,
  55. CI_VBUS_STABLE_TIMEOUT_MS);
  56. ci_role_start(ci, role);
  57. }
  58. }
  59. /**
  60. * ci_otg_work - perform otg (vbus/id) event handle
  61. * @work: work struct
  62. */
  63. static void ci_otg_work(struct work_struct *work)
  64. {
  65. struct ci_hdrc *ci = container_of(work, struct ci_hdrc, work);
  66. if (ci->id_event) {
  67. ci->id_event = false;
  68. ci_handle_id_switch(ci);
  69. } else if (ci->b_sess_valid_event) {
  70. ci->b_sess_valid_event = false;
  71. ci_handle_vbus_change(ci);
  72. } else
  73. dev_err(ci->dev, "unexpected event occurs at %s\n", __func__);
  74. enable_irq(ci->irq);
  75. }
  76. /**
  77. * ci_hdrc_otg_init - initialize otg struct
  78. * ci: the controller
  79. */
  80. int ci_hdrc_otg_init(struct ci_hdrc *ci)
  81. {
  82. INIT_WORK(&ci->work, ci_otg_work);
  83. ci->wq = create_singlethread_workqueue("ci_otg");
  84. if (!ci->wq) {
  85. dev_err(ci->dev, "can't create workqueue\n");
  86. return -ENODEV;
  87. }
  88. return 0;
  89. }
  90. /**
  91. * ci_hdrc_otg_destroy - destroy otg struct
  92. * ci: the controller
  93. */
  94. void ci_hdrc_otg_destroy(struct ci_hdrc *ci)
  95. {
  96. if (ci->wq) {
  97. flush_workqueue(ci->wq);
  98. destroy_workqueue(ci->wq);
  99. }
  100. ci_disable_otg_interrupt(ci, OTGSC_INT_EN_BITS);
  101. ci_clear_otg_interrupt(ci, OTGSC_INT_STATUS_BITS);
  102. }