phy.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 int usb_add_phy_dev(struct usb_phy *);
  105. extern void usb_remove_phy(struct usb_phy *);
  106. /* helpers for direct access thru low-level io interface */
  107. static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
  108. {
  109. if (x->io_ops && x->io_ops->read)
  110. return x->io_ops->read(x, reg);
  111. return -EINVAL;
  112. }
  113. static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
  114. {
  115. if (x->io_ops && x->io_ops->write)
  116. return x->io_ops->write(x, val, reg);
  117. return -EINVAL;
  118. }
  119. static inline int
  120. usb_phy_init(struct usb_phy *x)
  121. {
  122. if (x->init)
  123. return x->init(x);
  124. return 0;
  125. }
  126. static inline void
  127. usb_phy_shutdown(struct usb_phy *x)
  128. {
  129. if (x->shutdown)
  130. x->shutdown(x);
  131. }
  132. /* for usb host and peripheral controller drivers */
  133. #ifdef CONFIG_USB_OTG_UTILS
  134. extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
  135. extern struct usb_phy *devm_usb_get_phy(struct device *dev,
  136. enum usb_phy_type type);
  137. extern struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index);
  138. extern struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index);
  139. extern struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
  140. const char *phandle, u8 index);
  141. extern void usb_put_phy(struct usb_phy *);
  142. extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
  143. extern int usb_bind_phy(const char *dev_name, u8 index,
  144. const char *phy_dev_name);
  145. #else
  146. static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
  147. {
  148. return NULL;
  149. }
  150. static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
  151. enum usb_phy_type type)
  152. {
  153. return NULL;
  154. }
  155. static inline struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
  156. {
  157. return NULL;
  158. }
  159. static inline struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
  160. {
  161. return NULL;
  162. }
  163. static inline struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
  164. const char *phandle, u8 index)
  165. {
  166. return NULL;
  167. }
  168. static inline void usb_put_phy(struct usb_phy *x)
  169. {
  170. }
  171. static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
  172. {
  173. }
  174. static inline int usb_bind_phy(const char *dev_name, u8 index,
  175. const char *phy_dev_name)
  176. {
  177. return -EOPNOTSUPP;
  178. }
  179. #endif
  180. static inline int
  181. usb_phy_set_power(struct usb_phy *x, unsigned mA)
  182. {
  183. if (x && x->set_power)
  184. return x->set_power(x, mA);
  185. return 0;
  186. }
  187. /* Context: can sleep */
  188. static inline int
  189. usb_phy_set_suspend(struct usb_phy *x, int suspend)
  190. {
  191. if (x->set_suspend != NULL)
  192. return x->set_suspend(x, suspend);
  193. else
  194. return 0;
  195. }
  196. static inline int
  197. usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
  198. {
  199. if (x->notify_connect)
  200. return x->notify_connect(x, speed);
  201. else
  202. return 0;
  203. }
  204. static inline int
  205. usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed)
  206. {
  207. if (x->notify_disconnect)
  208. return x->notify_disconnect(x, speed);
  209. else
  210. return 0;
  211. }
  212. /* notifiers */
  213. static inline int
  214. usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
  215. {
  216. return atomic_notifier_chain_register(&x->notifier, nb);
  217. }
  218. static inline void
  219. usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
  220. {
  221. atomic_notifier_chain_unregister(&x->notifier, nb);
  222. }
  223. static inline const char *usb_phy_type_string(enum usb_phy_type type)
  224. {
  225. switch (type) {
  226. case USB_PHY_TYPE_USB2:
  227. return "USB2 PHY";
  228. case USB_PHY_TYPE_USB3:
  229. return "USB3 PHY";
  230. default:
  231. return "UNKNOWN PHY TYPE";
  232. }
  233. }
  234. #endif /* __LINUX_USB_PHY_H */