phy.h 5.8 KB

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