phy.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * USB PHY defines
  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 phys 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 phys */
  78. struct list_head head;
  79. /* initialize/shutdown the phy */
  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. /* Set phy 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 struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
  171. struct device_node *node, struct notifier_block *nb);
  172. extern void usb_put_phy(struct usb_phy *);
  173. extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
  174. extern int usb_bind_phy(const char *dev_name, u8 index,
  175. const char *phy_dev_name);
  176. extern void usb_phy_set_event(struct usb_phy *x, unsigned long event);
  177. #else
  178. static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
  179. {
  180. return ERR_PTR(-ENXIO);
  181. }
  182. static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
  183. enum usb_phy_type type)
  184. {
  185. return ERR_PTR(-ENXIO);
  186. }
  187. static inline struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
  188. {
  189. return ERR_PTR(-ENXIO);
  190. }
  191. static inline struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
  192. {
  193. return ERR_PTR(-ENXIO);
  194. }
  195. static inline struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
  196. const char *phandle, u8 index)
  197. {
  198. return ERR_PTR(-ENXIO);
  199. }
  200. static inline struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
  201. struct device_node *node, struct notifier_block *nb)
  202. {
  203. return ERR_PTR(-ENXIO);
  204. }
  205. static inline void usb_put_phy(struct usb_phy *x)
  206. {
  207. }
  208. static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
  209. {
  210. }
  211. static inline int usb_bind_phy(const char *dev_name, u8 index,
  212. const char *phy_dev_name)
  213. {
  214. return -EOPNOTSUPP;
  215. }
  216. static inline void usb_phy_set_event(struct usb_phy *x, unsigned long event)
  217. {
  218. }
  219. #endif
  220. static inline int
  221. usb_phy_set_power(struct usb_phy *x, unsigned mA)
  222. {
  223. if (x && x->set_power)
  224. return x->set_power(x, mA);
  225. return 0;
  226. }
  227. /* Context: can sleep */
  228. static inline int
  229. usb_phy_set_suspend(struct usb_phy *x, int suspend)
  230. {
  231. if (x && x->set_suspend != NULL)
  232. return x->set_suspend(x, suspend);
  233. else
  234. return 0;
  235. }
  236. static inline int
  237. usb_phy_set_wakeup(struct usb_phy *x, bool enabled)
  238. {
  239. if (x && x->set_wakeup)
  240. return x->set_wakeup(x, enabled);
  241. else
  242. return 0;
  243. }
  244. static inline int
  245. usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
  246. {
  247. if (x && x->notify_connect)
  248. return x->notify_connect(x, speed);
  249. else
  250. return 0;
  251. }
  252. static inline int
  253. usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed)
  254. {
  255. if (x && x->notify_disconnect)
  256. return x->notify_disconnect(x, speed);
  257. else
  258. return 0;
  259. }
  260. /* notifiers */
  261. static inline int
  262. usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
  263. {
  264. return atomic_notifier_chain_register(&x->notifier, nb);
  265. }
  266. static inline void
  267. usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
  268. {
  269. atomic_notifier_chain_unregister(&x->notifier, nb);
  270. }
  271. static inline const char *usb_phy_type_string(enum usb_phy_type type)
  272. {
  273. switch (type) {
  274. case USB_PHY_TYPE_USB2:
  275. return "USB2 PHY";
  276. case USB_PHY_TYPE_USB3:
  277. return "USB3 PHY";
  278. default:
  279. return "UNKNOWN PHY TYPE";
  280. }
  281. }
  282. #endif /* __LINUX_USB_PHY_H */