Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * USB PHY defines
0004  *
0005  * These APIs may be used between USB controllers.  USB device drivers
0006  * (for either host or peripheral roles) don't use these calls; they
0007  * continue to use just usb_device and usb_gadget.
0008  */
0009 
0010 #ifndef __LINUX_USB_PHY_H
0011 #define __LINUX_USB_PHY_H
0012 
0013 #include <linux/extcon.h>
0014 #include <linux/notifier.h>
0015 #include <linux/usb.h>
0016 #include <uapi/linux/usb/charger.h>
0017 
0018 enum usb_phy_interface {
0019     USBPHY_INTERFACE_MODE_UNKNOWN,
0020     USBPHY_INTERFACE_MODE_UTMI,
0021     USBPHY_INTERFACE_MODE_UTMIW,
0022     USBPHY_INTERFACE_MODE_ULPI,
0023     USBPHY_INTERFACE_MODE_SERIAL,
0024     USBPHY_INTERFACE_MODE_HSIC,
0025 };
0026 
0027 enum usb_phy_events {
0028     USB_EVENT_NONE,         /* no events or cable disconnected */
0029     USB_EVENT_VBUS,         /* vbus valid event */
0030     USB_EVENT_ID,           /* id was grounded */
0031     USB_EVENT_CHARGER,      /* usb dedicated charger */
0032     USB_EVENT_ENUMERATED,   /* gadget driver enumerated */
0033 };
0034 
0035 /* associate a type with PHY */
0036 enum usb_phy_type {
0037     USB_PHY_TYPE_UNDEFINED,
0038     USB_PHY_TYPE_USB2,
0039     USB_PHY_TYPE_USB3,
0040 };
0041 
0042 /* OTG defines lots of enumeration states before device reset */
0043 enum usb_otg_state {
0044     OTG_STATE_UNDEFINED = 0,
0045 
0046     /* single-role peripheral, and dual-role default-b */
0047     OTG_STATE_B_IDLE,
0048     OTG_STATE_B_SRP_INIT,
0049     OTG_STATE_B_PERIPHERAL,
0050 
0051     /* extra dual-role default-b states */
0052     OTG_STATE_B_WAIT_ACON,
0053     OTG_STATE_B_HOST,
0054 
0055     /* dual-role default-a */
0056     OTG_STATE_A_IDLE,
0057     OTG_STATE_A_WAIT_VRISE,
0058     OTG_STATE_A_WAIT_BCON,
0059     OTG_STATE_A_HOST,
0060     OTG_STATE_A_SUSPEND,
0061     OTG_STATE_A_PERIPHERAL,
0062     OTG_STATE_A_WAIT_VFALL,
0063     OTG_STATE_A_VBUS_ERR,
0064 };
0065 
0066 struct usb_phy;
0067 struct usb_otg;
0068 
0069 /* for phys connected thru an ULPI interface, the user must
0070  * provide access ops
0071  */
0072 struct usb_phy_io_ops {
0073     int (*read)(struct usb_phy *x, u32 reg);
0074     int (*write)(struct usb_phy *x, u32 val, u32 reg);
0075 };
0076 
0077 struct usb_charger_current {
0078     unsigned int sdp_min;
0079     unsigned int sdp_max;
0080     unsigned int dcp_min;
0081     unsigned int dcp_max;
0082     unsigned int cdp_min;
0083     unsigned int cdp_max;
0084     unsigned int aca_min;
0085     unsigned int aca_max;
0086 };
0087 
0088 struct usb_phy {
0089     struct device       *dev;
0090     const char      *label;
0091     unsigned int         flags;
0092 
0093     enum usb_phy_type   type;
0094     enum usb_phy_events last_event;
0095 
0096     struct usb_otg      *otg;
0097 
0098     struct device       *io_dev;
0099     struct usb_phy_io_ops   *io_ops;
0100     void __iomem        *io_priv;
0101 
0102     /* to support extcon device */
0103     struct extcon_dev   *edev;
0104     struct extcon_dev   *id_edev;
0105     struct notifier_block   vbus_nb;
0106     struct notifier_block   id_nb;
0107     struct notifier_block   type_nb;
0108 
0109     /* Support USB charger */
0110     enum usb_charger_type   chg_type;
0111     enum usb_charger_state  chg_state;
0112     struct usb_charger_current  chg_cur;
0113     struct work_struct      chg_work;
0114 
0115     /* for notification of usb_phy_events */
0116     struct atomic_notifier_head notifier;
0117 
0118     /* to pass extra port status to the root hub */
0119     u16         port_status;
0120     u16         port_change;
0121 
0122     /* to support controllers that have multiple phys */
0123     struct list_head    head;
0124 
0125     /* initialize/shutdown the phy */
0126     int (*init)(struct usb_phy *x);
0127     void    (*shutdown)(struct usb_phy *x);
0128 
0129     /* enable/disable VBUS */
0130     int (*set_vbus)(struct usb_phy *x, int on);
0131 
0132     /* effective for B devices, ignored for A-peripheral */
0133     int (*set_power)(struct usb_phy *x,
0134                 unsigned mA);
0135 
0136     /* Set phy into suspend mode */
0137     int (*set_suspend)(struct usb_phy *x,
0138                 int suspend);
0139 
0140     /*
0141      * Set wakeup enable for PHY, in that case, the PHY can be
0142      * woken up from suspend status due to external events,
0143      * like vbus change, dp/dm change and id.
0144      */
0145     int (*set_wakeup)(struct usb_phy *x, bool enabled);
0146 
0147     /* notify phy connect status change */
0148     int (*notify_connect)(struct usb_phy *x,
0149             enum usb_device_speed speed);
0150     int (*notify_disconnect)(struct usb_phy *x,
0151             enum usb_device_speed speed);
0152 
0153     /*
0154      * Charger detection method can be implemented if you need to
0155      * manually detect the charger type.
0156      */
0157     enum usb_charger_type (*charger_detect)(struct usb_phy *x);
0158 };
0159 
0160 /* for board-specific init logic */
0161 extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
0162 extern int usb_add_phy_dev(struct usb_phy *);
0163 extern void usb_remove_phy(struct usb_phy *);
0164 
0165 /* helpers for direct access thru low-level io interface */
0166 static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
0167 {
0168     if (x && x->io_ops && x->io_ops->read)
0169         return x->io_ops->read(x, reg);
0170 
0171     return -EINVAL;
0172 }
0173 
0174 static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
0175 {
0176     if (x && x->io_ops && x->io_ops->write)
0177         return x->io_ops->write(x, val, reg);
0178 
0179     return -EINVAL;
0180 }
0181 
0182 static inline int
0183 usb_phy_init(struct usb_phy *x)
0184 {
0185     if (x && x->init)
0186         return x->init(x);
0187 
0188     return 0;
0189 }
0190 
0191 static inline void
0192 usb_phy_shutdown(struct usb_phy *x)
0193 {
0194     if (x && x->shutdown)
0195         x->shutdown(x);
0196 }
0197 
0198 static inline int
0199 usb_phy_vbus_on(struct usb_phy *x)
0200 {
0201     if (!x || !x->set_vbus)
0202         return 0;
0203 
0204     return x->set_vbus(x, true);
0205 }
0206 
0207 static inline int
0208 usb_phy_vbus_off(struct usb_phy *x)
0209 {
0210     if (!x || !x->set_vbus)
0211         return 0;
0212 
0213     return x->set_vbus(x, false);
0214 }
0215 
0216 /* for usb host and peripheral controller drivers */
0217 #if IS_ENABLED(CONFIG_USB_PHY)
0218 extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
0219 extern struct usb_phy *devm_usb_get_phy(struct device *dev,
0220     enum usb_phy_type type);
0221 extern struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
0222     const char *phandle, u8 index);
0223 extern struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
0224     struct device_node *node, struct notifier_block *nb);
0225 extern void usb_put_phy(struct usb_phy *);
0226 extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
0227 extern void usb_phy_set_event(struct usb_phy *x, unsigned long event);
0228 extern void usb_phy_set_charger_current(struct usb_phy *usb_phy,
0229                     unsigned int mA);
0230 extern void usb_phy_get_charger_current(struct usb_phy *usb_phy,
0231                     unsigned int *min, unsigned int *max);
0232 extern void usb_phy_set_charger_state(struct usb_phy *usb_phy,
0233                       enum usb_charger_state state);
0234 #else
0235 static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
0236 {
0237     return ERR_PTR(-ENXIO);
0238 }
0239 
0240 static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
0241     enum usb_phy_type type)
0242 {
0243     return ERR_PTR(-ENXIO);
0244 }
0245 
0246 static inline struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
0247     const char *phandle, u8 index)
0248 {
0249     return ERR_PTR(-ENXIO);
0250 }
0251 
0252 static inline struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
0253     struct device_node *node, struct notifier_block *nb)
0254 {
0255     return ERR_PTR(-ENXIO);
0256 }
0257 
0258 static inline void usb_put_phy(struct usb_phy *x)
0259 {
0260 }
0261 
0262 static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
0263 {
0264 }
0265 
0266 static inline void usb_phy_set_event(struct usb_phy *x, unsigned long event)
0267 {
0268 }
0269 
0270 static inline void usb_phy_set_charger_current(struct usb_phy *usb_phy,
0271                            unsigned int mA)
0272 {
0273 }
0274 
0275 static inline void usb_phy_get_charger_current(struct usb_phy *usb_phy,
0276                            unsigned int *min,
0277                            unsigned int *max)
0278 {
0279 }
0280 
0281 static inline void usb_phy_set_charger_state(struct usb_phy *usb_phy,
0282                          enum usb_charger_state state)
0283 {
0284 }
0285 #endif
0286 
0287 static inline int
0288 usb_phy_set_power(struct usb_phy *x, unsigned mA)
0289 {
0290     if (!x)
0291         return 0;
0292 
0293     usb_phy_set_charger_current(x, mA);
0294 
0295     if (x->set_power)
0296         return x->set_power(x, mA);
0297     return 0;
0298 }
0299 
0300 /* Context: can sleep */
0301 static inline int
0302 usb_phy_set_suspend(struct usb_phy *x, int suspend)
0303 {
0304     if (x && x->set_suspend != NULL)
0305         return x->set_suspend(x, suspend);
0306     else
0307         return 0;
0308 }
0309 
0310 static inline int
0311 usb_phy_set_wakeup(struct usb_phy *x, bool enabled)
0312 {
0313     if (x && x->set_wakeup)
0314         return x->set_wakeup(x, enabled);
0315     else
0316         return 0;
0317 }
0318 
0319 static inline int
0320 usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
0321 {
0322     if (x && x->notify_connect)
0323         return x->notify_connect(x, speed);
0324     else
0325         return 0;
0326 }
0327 
0328 static inline int
0329 usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed)
0330 {
0331     if (x && x->notify_disconnect)
0332         return x->notify_disconnect(x, speed);
0333     else
0334         return 0;
0335 }
0336 
0337 /* notifiers */
0338 static inline int
0339 usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
0340 {
0341     return atomic_notifier_chain_register(&x->notifier, nb);
0342 }
0343 
0344 static inline void
0345 usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
0346 {
0347     atomic_notifier_chain_unregister(&x->notifier, nb);
0348 }
0349 
0350 static inline const char *usb_phy_type_string(enum usb_phy_type type)
0351 {
0352     switch (type) {
0353     case USB_PHY_TYPE_USB2:
0354         return "USB2 PHY";
0355     case USB_PHY_TYPE_USB3:
0356         return "USB3 PHY";
0357     default:
0358         return "UNKNOWN PHY TYPE";
0359     }
0360 }
0361 #endif /* __LINUX_USB_PHY_H */