phy.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* USB OTG (On The Go) defines */
  2. /*
  3. *
  4. * These APIs may be used between USB controllers. USB device drivers
  5. * (for either host or peripheral roles) don't use these calls; they
  6. * continue to use just usb_device and usb_gadget.
  7. */
  8. #ifndef __LINUX_USB_PHY_H
  9. #define __LINUX_USB_PHY_H
  10. #include <linux/notifier.h>
  11. enum usb_phy_events {
  12. USB_EVENT_NONE, /* no events or cable disconnected */
  13. USB_EVENT_VBUS, /* vbus valid event */
  14. USB_EVENT_ID, /* id was grounded */
  15. USB_EVENT_CHARGER, /* usb dedicated charger */
  16. USB_EVENT_ENUMERATED, /* gadget driver enumerated */
  17. };
  18. /* associate a type with PHY */
  19. enum usb_phy_type {
  20. USB_PHY_TYPE_UNDEFINED,
  21. USB_PHY_TYPE_USB2,
  22. USB_PHY_TYPE_USB3,
  23. };
  24. /* OTG defines lots of enumeration states before device reset */
  25. enum usb_otg_state {
  26. OTG_STATE_UNDEFINED = 0,
  27. /* single-role peripheral, and dual-role default-b */
  28. OTG_STATE_B_IDLE,
  29. OTG_STATE_B_SRP_INIT,
  30. OTG_STATE_B_PERIPHERAL,
  31. /* extra dual-role default-b states */
  32. OTG_STATE_B_WAIT_ACON,
  33. OTG_STATE_B_HOST,
  34. /* dual-role default-a */
  35. OTG_STATE_A_IDLE,
  36. OTG_STATE_A_WAIT_VRISE,
  37. OTG_STATE_A_WAIT_BCON,
  38. OTG_STATE_A_HOST,
  39. OTG_STATE_A_SUSPEND,
  40. OTG_STATE_A_PERIPHERAL,
  41. OTG_STATE_A_WAIT_VFALL,
  42. OTG_STATE_A_VBUS_ERR,
  43. };
  44. struct usb_phy;
  45. struct usb_otg;
  46. /* for transceivers connected thru an ULPI interface, the user must
  47. * provide access ops
  48. */
  49. struct usb_phy_io_ops {
  50. int (*read)(struct usb_phy *x, u32 reg);
  51. int (*write)(struct usb_phy *x, u32 val, u32 reg);
  52. };
  53. struct usb_phy {
  54. struct device *dev;
  55. const char *label;
  56. unsigned int flags;
  57. enum usb_phy_type type;
  58. enum usb_otg_state state;
  59. enum usb_phy_events last_event;
  60. struct usb_otg *otg;
  61. struct device *io_dev;
  62. struct usb_phy_io_ops *io_ops;
  63. void __iomem *io_priv;
  64. /* for notification of usb_phy_events */
  65. struct atomic_notifier_head notifier;
  66. /* to pass extra port status to the root hub */
  67. u16 port_status;
  68. u16 port_change;
  69. /* to support controllers that have multiple transceivers */
  70. struct list_head head;
  71. /* initialize/shutdown the OTG controller */
  72. int (*init)(struct usb_phy *x);
  73. void (*shutdown)(struct usb_phy *x);
  74. /* effective for B devices, ignored for A-peripheral */
  75. int (*set_power)(struct usb_phy *x,
  76. unsigned mA);
  77. /* for non-OTG B devices: set transceiver into suspend mode */
  78. int (*set_suspend)(struct usb_phy *x,
  79. int suspend);
  80. /* notify phy connect status change */
  81. int (*notify_connect)(struct usb_phy *x, int port);
  82. int (*notify_disconnect)(struct usb_phy *x, int port);
  83. };
  84. /* for board-specific init logic */
  85. extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
  86. extern void usb_remove_phy(struct usb_phy *);
  87. /* helpers for direct access thru low-level io interface */
  88. static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
  89. {
  90. if (x->io_ops && x->io_ops->read)
  91. return x->io_ops->read(x, reg);
  92. return -EINVAL;
  93. }
  94. static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
  95. {
  96. if (x->io_ops && x->io_ops->write)
  97. return x->io_ops->write(x, val, reg);
  98. return -EINVAL;
  99. }
  100. static inline int
  101. usb_phy_init(struct usb_phy *x)
  102. {
  103. if (x->init)
  104. return x->init(x);
  105. return 0;
  106. }
  107. static inline void
  108. usb_phy_shutdown(struct usb_phy *x)
  109. {
  110. if (x->shutdown)
  111. x->shutdown(x);
  112. }
  113. /* for usb host and peripheral controller drivers */
  114. #ifdef CONFIG_USB_OTG_UTILS
  115. extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
  116. extern struct usb_phy *devm_usb_get_phy(struct device *dev,
  117. enum usb_phy_type type);
  118. extern void usb_put_phy(struct usb_phy *);
  119. extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
  120. #else
  121. static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
  122. {
  123. return NULL;
  124. }
  125. static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
  126. enum usb_phy_type type)
  127. {
  128. return NULL;
  129. }
  130. static inline void usb_put_phy(struct usb_phy *x)
  131. {
  132. }
  133. static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
  134. {
  135. }
  136. #endif
  137. static inline int
  138. usb_phy_set_power(struct usb_phy *x, unsigned mA)
  139. {
  140. if (x && x->set_power)
  141. return x->set_power(x, mA);
  142. return 0;
  143. }
  144. /* Context: can sleep */
  145. static inline int
  146. usb_phy_set_suspend(struct usb_phy *x, int suspend)
  147. {
  148. if (x->set_suspend != NULL)
  149. return x->set_suspend(x, suspend);
  150. else
  151. return 0;
  152. }
  153. static inline int
  154. usb_phy_notify_connect(struct usb_phy *x, int port)
  155. {
  156. if (x->notify_connect)
  157. return x->notify_connect(x, port);
  158. else
  159. return 0;
  160. }
  161. static inline int
  162. usb_phy_notify_disconnect(struct usb_phy *x, int port)
  163. {
  164. if (x->notify_disconnect)
  165. return x->notify_disconnect(x, port);
  166. else
  167. return 0;
  168. }
  169. /* notifiers */
  170. static inline int
  171. usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
  172. {
  173. return atomic_notifier_chain_register(&x->notifier, nb);
  174. }
  175. static inline void
  176. usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
  177. {
  178. atomic_notifier_chain_unregister(&x->notifier, nb);
  179. }
  180. static inline const char *usb_phy_type_string(enum usb_phy_type type)
  181. {
  182. switch (type) {
  183. case USB_PHY_TYPE_USB2:
  184. return "USB2 PHY";
  185. case USB_PHY_TYPE_USB3:
  186. return "USB3 PHY";
  187. default:
  188. return "UNKNOWN PHY TYPE";
  189. }
  190. }
  191. #endif /* __LINUX_USB_PHY_H */