phy.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. /**
  132. * struct usb_phy_bind - represent the binding for the phy
  133. * @dev_name: the device name of the device that will bind to the phy
  134. * @phy_dev_name: the device name of the phy
  135. * @index: used if a single controller uses multiple phys
  136. * @phy: reference to the phy
  137. * @list: to maintain a linked list of the binding information
  138. */
  139. struct usb_phy_bind {
  140. const char *dev_name;
  141. const char *phy_dev_name;
  142. u8 index;
  143. struct usb_phy *phy;
  144. struct list_head list;
  145. };
  146. /* for board-specific init logic */
  147. extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
  148. extern int usb_add_phy_dev(struct usb_phy *);
  149. extern void usb_remove_phy(struct usb_phy *);
  150. /* helpers for direct access thru low-level io interface */
  151. static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
  152. {
  153. if (x && x->io_ops && x->io_ops->read)
  154. return x->io_ops->read(x, reg);
  155. return -EINVAL;
  156. }
  157. static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
  158. {
  159. if (x && x->io_ops && x->io_ops->write)
  160. return x->io_ops->write(x, val, reg);
  161. return -EINVAL;
  162. }
  163. static inline int
  164. usb_phy_init(struct usb_phy *x)
  165. {
  166. if (x && x->init)
  167. return x->init(x);
  168. return 0;
  169. }
  170. static inline void
  171. usb_phy_shutdown(struct usb_phy *x)
  172. {
  173. if (x && x->shutdown)
  174. x->shutdown(x);
  175. }
  176. static inline int
  177. usb_phy_vbus_on(struct usb_phy *x)
  178. {
  179. if (!x || !x->set_vbus)
  180. return 0;
  181. return x->set_vbus(x, true);
  182. }
  183. static inline int
  184. usb_phy_vbus_off(struct usb_phy *x)
  185. {
  186. if (!x || !x->set_vbus)
  187. return 0;
  188. return x->set_vbus(x, false);
  189. }
  190. /* for usb host and peripheral controller drivers */
  191. #if IS_ENABLED(CONFIG_USB_PHY)
  192. extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
  193. extern struct usb_phy *devm_usb_get_phy(struct device *dev,
  194. enum usb_phy_type type);
  195. extern struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index);
  196. extern struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index);
  197. extern struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
  198. const char *phandle, u8 index);
  199. extern struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
  200. struct device_node *node, struct notifier_block *nb);
  201. extern void usb_put_phy(struct usb_phy *);
  202. extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
  203. extern int usb_bind_phy(const char *dev_name, u8 index,
  204. const char *phy_dev_name);
  205. extern void usb_phy_set_event(struct usb_phy *x, unsigned long event);
  206. extern void usb_phy_set_charger_current(struct usb_phy *usb_phy,
  207. unsigned int mA);
  208. extern void usb_phy_get_charger_current(struct usb_phy *usb_phy,
  209. unsigned int *min, unsigned int *max);
  210. extern void usb_phy_set_charger_state(struct usb_phy *usb_phy,
  211. enum usb_charger_state state);
  212. #else
  213. static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
  214. {
  215. return ERR_PTR(-ENXIO);
  216. }
  217. static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
  218. enum usb_phy_type type)
  219. {
  220. return ERR_PTR(-ENXIO);
  221. }
  222. static inline struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
  223. {
  224. return ERR_PTR(-ENXIO);
  225. }
  226. static inline struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
  227. {
  228. return ERR_PTR(-ENXIO);
  229. }
  230. static inline struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
  231. const char *phandle, u8 index)
  232. {
  233. return ERR_PTR(-ENXIO);
  234. }
  235. static inline struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
  236. struct device_node *node, struct notifier_block *nb)
  237. {
  238. return ERR_PTR(-ENXIO);
  239. }
  240. static inline void usb_put_phy(struct usb_phy *x)
  241. {
  242. }
  243. static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
  244. {
  245. }
  246. static inline int usb_bind_phy(const char *dev_name, u8 index,
  247. const char *phy_dev_name)
  248. {
  249. return -EOPNOTSUPP;
  250. }
  251. static inline void usb_phy_set_event(struct usb_phy *x, unsigned long event)
  252. {
  253. }
  254. static inline void usb_phy_set_charger_current(struct usb_phy *usb_phy,
  255. unsigned int mA)
  256. {
  257. }
  258. static inline void usb_phy_get_charger_current(struct usb_phy *usb_phy,
  259. unsigned int *min,
  260. unsigned int *max)
  261. {
  262. }
  263. static inline void usb_phy_set_charger_state(struct usb_phy *usb_phy,
  264. enum usb_charger_state state)
  265. {
  266. }
  267. #endif
  268. static inline int
  269. usb_phy_set_power(struct usb_phy *x, unsigned mA)
  270. {
  271. if (!x)
  272. return 0;
  273. usb_phy_set_charger_current(x, mA);
  274. if (x->set_power)
  275. return x->set_power(x, mA);
  276. return 0;
  277. }
  278. /* Context: can sleep */
  279. static inline int
  280. usb_phy_set_suspend(struct usb_phy *x, int suspend)
  281. {
  282. if (x && x->set_suspend != NULL)
  283. return x->set_suspend(x, suspend);
  284. else
  285. return 0;
  286. }
  287. static inline int
  288. usb_phy_set_wakeup(struct usb_phy *x, bool enabled)
  289. {
  290. if (x && x->set_wakeup)
  291. return x->set_wakeup(x, enabled);
  292. else
  293. return 0;
  294. }
  295. static inline int
  296. usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
  297. {
  298. if (x && x->notify_connect)
  299. return x->notify_connect(x, speed);
  300. else
  301. return 0;
  302. }
  303. static inline int
  304. usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed)
  305. {
  306. if (x && x->notify_disconnect)
  307. return x->notify_disconnect(x, speed);
  308. else
  309. return 0;
  310. }
  311. /* notifiers */
  312. static inline int
  313. usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
  314. {
  315. return atomic_notifier_chain_register(&x->notifier, nb);
  316. }
  317. static inline void
  318. usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
  319. {
  320. atomic_notifier_chain_unregister(&x->notifier, nb);
  321. }
  322. static inline const char *usb_phy_type_string(enum usb_phy_type type)
  323. {
  324. switch (type) {
  325. case USB_PHY_TYPE_USB2:
  326. return "USB2 PHY";
  327. case USB_PHY_TYPE_USB3:
  328. return "USB3 PHY";
  329. default:
  330. return "UNKNOWN PHY TYPE";
  331. }
  332. }
  333. #endif /* __LINUX_USB_PHY_H */