phy.h 9.2 KB

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