phy.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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_phy_events last_event;
  68. struct usb_otg *otg;
  69. struct device *io_dev;
  70. struct usb_phy_io_ops *io_ops;
  71. void __iomem *io_priv;
  72. /* for notification of usb_phy_events */
  73. struct atomic_notifier_head notifier;
  74. /* to pass extra port status to the root hub */
  75. u16 port_status;
  76. u16 port_change;
  77. /* to support controllers that have multiple transceivers */
  78. struct list_head head;
  79. /* initialize/shutdown the OTG controller */
  80. int (*init)(struct usb_phy *x);
  81. void (*shutdown)(struct usb_phy *x);
  82. /* enable/disable VBUS */
  83. int (*set_vbus)(struct usb_phy *x, int on);
  84. /* effective for B devices, ignored for A-peripheral */
  85. int (*set_power)(struct usb_phy *x,
  86. unsigned mA);
  87. /* for non-OTG B devices: set transceiver into suspend mode */
  88. int (*set_suspend)(struct usb_phy *x,
  89. int suspend);
  90. /*
  91. * Set wakeup enable for PHY, in that case, the PHY can be
  92. * woken up from suspend status due to external events,
  93. * like vbus change, dp/dm change and id.
  94. */
  95. int (*set_wakeup)(struct usb_phy *x, bool enabled);
  96. /* notify phy connect status change */
  97. int (*notify_connect)(struct usb_phy *x,
  98. enum usb_device_speed speed);
  99. int (*notify_disconnect)(struct usb_phy *x,
  100. enum usb_device_speed speed);
  101. };
  102. /**
  103. * struct usb_phy_bind - represent the binding for the phy
  104. * @dev_name: the device name of the device that will bind to the phy
  105. * @phy_dev_name: the device name of the phy
  106. * @index: used if a single controller uses multiple phys
  107. * @phy: reference to the phy
  108. * @list: to maintain a linked list of the binding information
  109. */
  110. struct usb_phy_bind {
  111. const char *dev_name;
  112. const char *phy_dev_name;
  113. u8 index;
  114. struct usb_phy *phy;
  115. struct list_head list;
  116. };
  117. /* for board-specific init logic */
  118. extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
  119. extern int usb_add_phy_dev(struct usb_phy *);
  120. extern void usb_remove_phy(struct usb_phy *);
  121. /* helpers for direct access thru low-level io interface */
  122. static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
  123. {
  124. if (x && x->io_ops && x->io_ops->read)
  125. return x->io_ops->read(x, reg);
  126. return -EINVAL;
  127. }
  128. static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
  129. {
  130. if (x && x->io_ops && x->io_ops->write)
  131. return x->io_ops->write(x, val, reg);
  132. return -EINVAL;
  133. }
  134. static inline int
  135. usb_phy_init(struct usb_phy *x)
  136. {
  137. if (x && x->init)
  138. return x->init(x);
  139. return 0;
  140. }
  141. static inline void
  142. usb_phy_shutdown(struct usb_phy *x)
  143. {
  144. if (x && x->shutdown)
  145. x->shutdown(x);
  146. }
  147. static inline int
  148. usb_phy_vbus_on(struct usb_phy *x)
  149. {
  150. if (!x || !x->set_vbus)
  151. return 0;
  152. return x->set_vbus(x, true);
  153. }
  154. static inline int
  155. usb_phy_vbus_off(struct usb_phy *x)
  156. {
  157. if (!x || !x->set_vbus)
  158. return 0;
  159. return x->set_vbus(x, false);
  160. }
  161. /* for usb host and peripheral controller drivers */
  162. #if IS_ENABLED(CONFIG_USB_PHY)
  163. extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
  164. extern struct usb_phy *devm_usb_get_phy(struct device *dev,
  165. enum usb_phy_type type);
  166. extern struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index);
  167. extern struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index);
  168. extern struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
  169. const char *phandle, u8 index);
  170. extern void usb_put_phy(struct usb_phy *);
  171. extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
  172. extern int usb_bind_phy(const char *dev_name, u8 index,
  173. const char *phy_dev_name);
  174. extern void usb_phy_set_event(struct usb_phy *x, unsigned long event);
  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. static inline void usb_phy_set_event(struct usb_phy *x, unsigned long event)
  210. {
  211. }
  212. #endif
  213. static inline int
  214. usb_phy_set_power(struct usb_phy *x, unsigned mA)
  215. {
  216. if (x && x->set_power)
  217. return x->set_power(x, mA);
  218. return 0;
  219. }
  220. /* Context: can sleep */
  221. static inline int
  222. usb_phy_set_suspend(struct usb_phy *x, int suspend)
  223. {
  224. if (x && x->set_suspend != NULL)
  225. return x->set_suspend(x, suspend);
  226. else
  227. return 0;
  228. }
  229. static inline int
  230. usb_phy_set_wakeup(struct usb_phy *x, bool enabled)
  231. {
  232. if (x && x->set_wakeup)
  233. return x->set_wakeup(x, enabled);
  234. else
  235. return 0;
  236. }
  237. static inline int
  238. usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
  239. {
  240. if (x && x->notify_connect)
  241. return x->notify_connect(x, speed);
  242. else
  243. return 0;
  244. }
  245. static inline int
  246. usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed)
  247. {
  248. if (x && x->notify_disconnect)
  249. return x->notify_disconnect(x, speed);
  250. else
  251. return 0;
  252. }
  253. /* notifiers */
  254. static inline int
  255. usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
  256. {
  257. return atomic_notifier_chain_register(&x->notifier, nb);
  258. }
  259. static inline void
  260. usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
  261. {
  262. atomic_notifier_chain_unregister(&x->notifier, nb);
  263. }
  264. static inline const char *usb_phy_type_string(enum usb_phy_type type)
  265. {
  266. switch (type) {
  267. case USB_PHY_TYPE_USB2:
  268. return "USB2 PHY";
  269. case USB_PHY_TYPE_USB3:
  270. return "USB3 PHY";
  271. default:
  272. return "UNKNOWN PHY TYPE";
  273. }
  274. }
  275. #endif /* __LINUX_USB_PHY_H */