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