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