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