phy.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. /* enable/disable VBUS */
  76. int (*set_vbus)(struct usb_phy *x, int on);
  77. /* effective for B devices, ignored for A-peripheral */
  78. int (*set_power)(struct usb_phy *x,
  79. unsigned mA);
  80. /* for non-OTG B devices: set transceiver into suspend mode */
  81. int (*set_suspend)(struct usb_phy *x,
  82. int suspend);
  83. /* notify phy connect status change */
  84. int (*notify_connect)(struct usb_phy *x,
  85. enum usb_device_speed speed);
  86. int (*notify_disconnect)(struct usb_phy *x,
  87. enum usb_device_speed speed);
  88. };
  89. /**
  90. * struct usb_phy_bind - represent the binding for the phy
  91. * @dev_name: the device name of the device that will bind to the phy
  92. * @phy_dev_name: the device name of the phy
  93. * @index: used if a single controller uses multiple phys
  94. * @phy: reference to the phy
  95. * @list: to maintain a linked list of the binding information
  96. */
  97. struct usb_phy_bind {
  98. const char *dev_name;
  99. const char *phy_dev_name;
  100. u8 index;
  101. struct usb_phy *phy;
  102. struct list_head list;
  103. };
  104. /* for board-specific init logic */
  105. extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
  106. extern int usb_add_phy_dev(struct usb_phy *);
  107. extern void usb_remove_phy(struct usb_phy *);
  108. /* helpers for direct access thru low-level io interface */
  109. static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
  110. {
  111. if (x->io_ops && x->io_ops->read)
  112. return x->io_ops->read(x, reg);
  113. return -EINVAL;
  114. }
  115. static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
  116. {
  117. if (x->io_ops && x->io_ops->write)
  118. return x->io_ops->write(x, val, reg);
  119. return -EINVAL;
  120. }
  121. static inline int
  122. usb_phy_init(struct usb_phy *x)
  123. {
  124. if (x->init)
  125. return x->init(x);
  126. return 0;
  127. }
  128. static inline void
  129. usb_phy_shutdown(struct usb_phy *x)
  130. {
  131. if (x->shutdown)
  132. x->shutdown(x);
  133. }
  134. static inline int
  135. usb_phy_vbus_on(struct usb_phy *x)
  136. {
  137. if (!x->set_vbus)
  138. return 0;
  139. return x->set_vbus(x, true);
  140. }
  141. static inline int
  142. usb_phy_vbus_off(struct usb_phy *x)
  143. {
  144. if (!x->set_vbus)
  145. return 0;
  146. return x->set_vbus(x, false);
  147. }
  148. /* for usb host and peripheral controller drivers */
  149. #if IS_ENABLED(CONFIG_USB_PHY)
  150. extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
  151. extern struct usb_phy *devm_usb_get_phy(struct device *dev,
  152. enum usb_phy_type type);
  153. extern struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index);
  154. extern struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index);
  155. extern struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
  156. const char *phandle, u8 index);
  157. extern void usb_put_phy(struct usb_phy *);
  158. extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
  159. extern int usb_bind_phy(const char *dev_name, u8 index,
  160. const char *phy_dev_name);
  161. #else
  162. static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
  163. {
  164. return ERR_PTR(-ENXIO);
  165. }
  166. static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
  167. enum usb_phy_type type)
  168. {
  169. return ERR_PTR(-ENXIO);
  170. }
  171. static inline struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
  172. {
  173. return ERR_PTR(-ENXIO);
  174. }
  175. static inline struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
  176. {
  177. return ERR_PTR(-ENXIO);
  178. }
  179. static inline struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
  180. const char *phandle, u8 index)
  181. {
  182. return ERR_PTR(-ENXIO);
  183. }
  184. static inline void usb_put_phy(struct usb_phy *x)
  185. {
  186. }
  187. static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
  188. {
  189. }
  190. static inline int usb_bind_phy(const char *dev_name, u8 index,
  191. const char *phy_dev_name)
  192. {
  193. return -EOPNOTSUPP;
  194. }
  195. #endif
  196. static inline int
  197. usb_phy_set_power(struct usb_phy *x, unsigned mA)
  198. {
  199. if (x && x->set_power)
  200. return x->set_power(x, mA);
  201. return 0;
  202. }
  203. /* Context: can sleep */
  204. static inline int
  205. usb_phy_set_suspend(struct usb_phy *x, int suspend)
  206. {
  207. if (x->set_suspend != NULL)
  208. return x->set_suspend(x, suspend);
  209. else
  210. return 0;
  211. }
  212. static inline int
  213. usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
  214. {
  215. if (x->notify_connect)
  216. return x->notify_connect(x, speed);
  217. else
  218. return 0;
  219. }
  220. static inline int
  221. usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed)
  222. {
  223. if (x->notify_disconnect)
  224. return x->notify_disconnect(x, speed);
  225. else
  226. return 0;
  227. }
  228. /* notifiers */
  229. static inline int
  230. usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
  231. {
  232. return atomic_notifier_chain_register(&x->notifier, nb);
  233. }
  234. static inline void
  235. usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
  236. {
  237. atomic_notifier_chain_unregister(&x->notifier, nb);
  238. }
  239. static inline const char *usb_phy_type_string(enum usb_phy_type type)
  240. {
  241. switch (type) {
  242. case USB_PHY_TYPE_USB2:
  243. return "USB2 PHY";
  244. case USB_PHY_TYPE_USB3:
  245. return "USB3 PHY";
  246. default:
  247. return "UNKNOWN PHY TYPE";
  248. }
  249. }
  250. #endif /* __LINUX_USB_PHY_H */