phy.h 7.0 KB

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