phy.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * USB PHY defines
  4. *
  5. * These APIs may be used between USB controllers. USB device drivers
  6. * (for either host or peripheral roles) don't use these calls; they
  7. * continue to use just usb_device and usb_gadget.
  8. */
  9. #ifndef __LINUX_USB_PHY_H
  10. #define __LINUX_USB_PHY_H
  11. #include <linux/extcon.h>
  12. #include <linux/notifier.h>
  13. #include <linux/usb.h>
  14. #include <uapi/linux/usb/charger.h>
  15. enum usb_phy_interface {
  16. USBPHY_INTERFACE_MODE_UNKNOWN,
  17. USBPHY_INTERFACE_MODE_UTMI,
  18. USBPHY_INTERFACE_MODE_UTMIW,
  19. USBPHY_INTERFACE_MODE_ULPI,
  20. USBPHY_INTERFACE_MODE_SERIAL,
  21. USBPHY_INTERFACE_MODE_HSIC,
  22. };
  23. enum usb_phy_events {
  24. USB_EVENT_NONE, /* no events or cable disconnected */
  25. USB_EVENT_VBUS, /* vbus valid event */
  26. USB_EVENT_ID, /* id was grounded */
  27. USB_EVENT_CHARGER, /* usb dedicated charger */
  28. USB_EVENT_ENUMERATED, /* gadget driver enumerated */
  29. };
  30. /* associate a type with PHY */
  31. enum usb_phy_type {
  32. USB_PHY_TYPE_UNDEFINED,
  33. USB_PHY_TYPE_USB2,
  34. USB_PHY_TYPE_USB3,
  35. };
  36. /* OTG defines lots of enumeration states before device reset */
  37. enum usb_otg_state {
  38. OTG_STATE_UNDEFINED = 0,
  39. /* single-role peripheral, and dual-role default-b */
  40. OTG_STATE_B_IDLE,
  41. OTG_STATE_B_SRP_INIT,
  42. OTG_STATE_B_PERIPHERAL,
  43. /* extra dual-role default-b states */
  44. OTG_STATE_B_WAIT_ACON,
  45. OTG_STATE_B_HOST,
  46. /* dual-role default-a */
  47. OTG_STATE_A_IDLE,
  48. OTG_STATE_A_WAIT_VRISE,
  49. OTG_STATE_A_WAIT_BCON,
  50. OTG_STATE_A_HOST,
  51. OTG_STATE_A_SUSPEND,
  52. OTG_STATE_A_PERIPHERAL,
  53. OTG_STATE_A_WAIT_VFALL,
  54. OTG_STATE_A_VBUS_ERR,
  55. };
  56. struct usb_phy;
  57. struct usb_otg;
  58. /* for phys connected thru an ULPI interface, the user must
  59. * provide access ops
  60. */
  61. struct usb_phy_io_ops {
  62. int (*read)(struct usb_phy *x, u32 reg);
  63. int (*write)(struct usb_phy *x, u32 val, u32 reg);
  64. };
  65. struct usb_charger_current {
  66. unsigned int sdp_min;
  67. unsigned int sdp_max;
  68. unsigned int dcp_min;
  69. unsigned int dcp_max;
  70. unsigned int cdp_min;
  71. unsigned int cdp_max;
  72. unsigned int aca_min;
  73. unsigned int aca_max;
  74. };
  75. struct usb_phy {
  76. struct device *dev;
  77. const char *label;
  78. unsigned int flags;
  79. enum usb_phy_type type;
  80. enum usb_phy_events last_event;
  81. struct usb_otg *otg;
  82. struct device *io_dev;
  83. struct usb_phy_io_ops *io_ops;
  84. void __iomem *io_priv;
  85. /* to support extcon device */
  86. struct extcon_dev *edev;
  87. struct extcon_dev *id_edev;
  88. struct notifier_block vbus_nb;
  89. struct notifier_block id_nb;
  90. struct notifier_block type_nb;
  91. /* Support USB charger */
  92. enum usb_charger_type chg_type;
  93. enum usb_charger_state chg_state;
  94. struct usb_charger_current chg_cur;
  95. struct work_struct chg_work;
  96. /* for notification of usb_phy_events */
  97. struct atomic_notifier_head notifier;
  98. /* to pass extra port status to the root hub */
  99. u16 port_status;
  100. u16 port_change;
  101. /* to support controllers that have multiple phys */
  102. struct list_head head;
  103. /* initialize/shutdown the phy */
  104. int (*init)(struct usb_phy *x);
  105. void (*shutdown)(struct usb_phy *x);
  106. /* enable/disable VBUS */
  107. int (*set_vbus)(struct usb_phy *x, int on);
  108. /* effective for B devices, ignored for A-peripheral */
  109. int (*set_power)(struct usb_phy *x,
  110. unsigned mA);
  111. /* Set phy into suspend mode */
  112. int (*set_suspend)(struct usb_phy *x,
  113. int suspend);
  114. /*
  115. * Set wakeup enable for PHY, in that case, the PHY can be
  116. * woken up from suspend status due to external events,
  117. * like vbus change, dp/dm change and id.
  118. */
  119. int (*set_wakeup)(struct usb_phy *x, bool enabled);
  120. /* notify phy connect status change */
  121. int (*notify_connect)(struct usb_phy *x,
  122. enum usb_device_speed speed);
  123. int (*notify_disconnect)(struct usb_phy *x,
  124. enum usb_device_speed speed);
  125. /*
  126. * Charger detection method can be implemented if you need to
  127. * manually detect the charger type.
  128. */
  129. enum usb_charger_type (*charger_detect)(struct usb_phy *x);
  130. };
  131. /* for board-specific init logic */
  132. extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
  133. extern int usb_add_phy_dev(struct usb_phy *);
  134. extern void usb_remove_phy(struct usb_phy *);
  135. /* helpers for direct access thru low-level io interface */
  136. static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
  137. {
  138. if (x && x->io_ops && x->io_ops->read)
  139. return x->io_ops->read(x, reg);
  140. return -EINVAL;
  141. }
  142. static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
  143. {
  144. if (x && x->io_ops && x->io_ops->write)
  145. return x->io_ops->write(x, val, reg);
  146. return -EINVAL;
  147. }
  148. static inline int
  149. usb_phy_init(struct usb_phy *x)
  150. {
  151. if (x && x->init)
  152. return x->init(x);
  153. return 0;
  154. }
  155. static inline void
  156. usb_phy_shutdown(struct usb_phy *x)
  157. {
  158. if (x && x->shutdown)
  159. x->shutdown(x);
  160. }
  161. static inline int
  162. usb_phy_vbus_on(struct usb_phy *x)
  163. {
  164. if (!x || !x->set_vbus)
  165. return 0;
  166. return x->set_vbus(x, true);
  167. }
  168. static inline int
  169. usb_phy_vbus_off(struct usb_phy *x)
  170. {
  171. if (!x || !x->set_vbus)
  172. return 0;
  173. return x->set_vbus(x, false);
  174. }
  175. /* for usb host and peripheral controller drivers */
  176. #if IS_ENABLED(CONFIG_USB_PHY)
  177. extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
  178. extern struct usb_phy *devm_usb_get_phy(struct device *dev,
  179. enum usb_phy_type type);
  180. extern struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
  181. const char *phandle, u8 index);
  182. extern struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
  183. struct device_node *node, struct notifier_block *nb);
  184. extern void usb_put_phy(struct usb_phy *);
  185. extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
  186. extern void usb_phy_set_event(struct usb_phy *x, unsigned long event);
  187. extern void usb_phy_set_charger_current(struct usb_phy *usb_phy,
  188. unsigned int mA);
  189. extern void usb_phy_get_charger_current(struct usb_phy *usb_phy,
  190. unsigned int *min, unsigned int *max);
  191. extern void usb_phy_set_charger_state(struct usb_phy *usb_phy,
  192. enum usb_charger_state state);
  193. #else
  194. static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
  195. {
  196. return ERR_PTR(-ENXIO);
  197. }
  198. static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
  199. enum usb_phy_type type)
  200. {
  201. return ERR_PTR(-ENXIO);
  202. }
  203. static inline struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
  204. const char *phandle, u8 index)
  205. {
  206. return ERR_PTR(-ENXIO);
  207. }
  208. static inline struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
  209. struct device_node *node, struct notifier_block *nb)
  210. {
  211. return ERR_PTR(-ENXIO);
  212. }
  213. static inline void usb_put_phy(struct usb_phy *x)
  214. {
  215. }
  216. static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
  217. {
  218. }
  219. static inline void usb_phy_set_event(struct usb_phy *x, unsigned long event)
  220. {
  221. }
  222. static inline void usb_phy_set_charger_current(struct usb_phy *usb_phy,
  223. unsigned int mA)
  224. {
  225. }
  226. static inline void usb_phy_get_charger_current(struct usb_phy *usb_phy,
  227. unsigned int *min,
  228. unsigned int *max)
  229. {
  230. }
  231. static inline void usb_phy_set_charger_state(struct usb_phy *usb_phy,
  232. enum usb_charger_state state)
  233. {
  234. }
  235. #endif
  236. static inline int
  237. usb_phy_set_power(struct usb_phy *x, unsigned mA)
  238. {
  239. if (!x)
  240. return 0;
  241. usb_phy_set_charger_current(x, mA);
  242. if (x->set_power)
  243. return x->set_power(x, mA);
  244. return 0;
  245. }
  246. /* Context: can sleep */
  247. static inline int
  248. usb_phy_set_suspend(struct usb_phy *x, int suspend)
  249. {
  250. if (x && x->set_suspend != NULL)
  251. return x->set_suspend(x, suspend);
  252. else
  253. return 0;
  254. }
  255. static inline int
  256. usb_phy_set_wakeup(struct usb_phy *x, bool enabled)
  257. {
  258. if (x && x->set_wakeup)
  259. return x->set_wakeup(x, enabled);
  260. else
  261. return 0;
  262. }
  263. static inline int
  264. usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
  265. {
  266. if (x && x->notify_connect)
  267. return x->notify_connect(x, speed);
  268. else
  269. return 0;
  270. }
  271. static inline int
  272. usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed)
  273. {
  274. if (x && x->notify_disconnect)
  275. return x->notify_disconnect(x, speed);
  276. else
  277. return 0;
  278. }
  279. /* notifiers */
  280. static inline int
  281. usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
  282. {
  283. return atomic_notifier_chain_register(&x->notifier, nb);
  284. }
  285. static inline void
  286. usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
  287. {
  288. atomic_notifier_chain_unregister(&x->notifier, nb);
  289. }
  290. static inline const char *usb_phy_type_string(enum usb_phy_type type)
  291. {
  292. switch (type) {
  293. case USB_PHY_TYPE_USB2:
  294. return "USB2 PHY";
  295. case USB_PHY_TYPE_USB3:
  296. return "USB3 PHY";
  297. default:
  298. return "UNKNOWN PHY TYPE";
  299. }
  300. }
  301. #endif /* __LINUX_USB_PHY_H */