phy.h 7.4 KB

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