Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * phy.c -- USB phy handling
0004  *
0005  * Copyright (C) 2004-2013 Texas Instruments
0006  */
0007 #include <linux/kernel.h>
0008 #include <linux/export.h>
0009 #include <linux/err.h>
0010 #include <linux/device.h>
0011 #include <linux/module.h>
0012 #include <linux/slab.h>
0013 #include <linux/of.h>
0014 
0015 #include <linux/usb/phy.h>
0016 
0017 /* Default current range by charger type. */
0018 #define DEFAULT_SDP_CUR_MIN 2
0019 #define DEFAULT_SDP_CUR_MAX 500
0020 #define DEFAULT_SDP_CUR_MIN_SS  150
0021 #define DEFAULT_SDP_CUR_MAX_SS  900
0022 #define DEFAULT_DCP_CUR_MIN 500
0023 #define DEFAULT_DCP_CUR_MAX 5000
0024 #define DEFAULT_CDP_CUR_MIN 1500
0025 #define DEFAULT_CDP_CUR_MAX 5000
0026 #define DEFAULT_ACA_CUR_MIN 1500
0027 #define DEFAULT_ACA_CUR_MAX 5000
0028 
0029 static LIST_HEAD(phy_list);
0030 static DEFINE_SPINLOCK(phy_lock);
0031 
0032 struct phy_devm {
0033     struct usb_phy *phy;
0034     struct notifier_block *nb;
0035 };
0036 
0037 static const char *const usb_chger_type[] = {
0038     [UNKNOWN_TYPE]          = "USB_CHARGER_UNKNOWN_TYPE",
0039     [SDP_TYPE]          = "USB_CHARGER_SDP_TYPE",
0040     [CDP_TYPE]          = "USB_CHARGER_CDP_TYPE",
0041     [DCP_TYPE]          = "USB_CHARGER_DCP_TYPE",
0042     [ACA_TYPE]          = "USB_CHARGER_ACA_TYPE",
0043 };
0044 
0045 static const char *const usb_chger_state[] = {
0046     [USB_CHARGER_DEFAULT]   = "USB_CHARGER_DEFAULT",
0047     [USB_CHARGER_PRESENT]   = "USB_CHARGER_PRESENT",
0048     [USB_CHARGER_ABSENT]    = "USB_CHARGER_ABSENT",
0049 };
0050 
0051 static struct usb_phy *__usb_find_phy(struct list_head *list,
0052     enum usb_phy_type type)
0053 {
0054     struct usb_phy  *phy = NULL;
0055 
0056     list_for_each_entry(phy, list, head) {
0057         if (phy->type != type)
0058             continue;
0059 
0060         return phy;
0061     }
0062 
0063     return ERR_PTR(-ENODEV);
0064 }
0065 
0066 static struct usb_phy *__of_usb_find_phy(struct device_node *node)
0067 {
0068     struct usb_phy  *phy;
0069 
0070     if (!of_device_is_available(node))
0071         return ERR_PTR(-ENODEV);
0072 
0073     list_for_each_entry(phy, &phy_list, head) {
0074         if (node != phy->dev->of_node)
0075             continue;
0076 
0077         return phy;
0078     }
0079 
0080     return ERR_PTR(-EPROBE_DEFER);
0081 }
0082 
0083 static struct usb_phy *__device_to_usb_phy(struct device *dev)
0084 {
0085     struct usb_phy *usb_phy;
0086 
0087     list_for_each_entry(usb_phy, &phy_list, head) {
0088         if (usb_phy->dev == dev)
0089             return usb_phy;
0090     }
0091 
0092     return NULL;
0093 }
0094 
0095 static void usb_phy_set_default_current(struct usb_phy *usb_phy)
0096 {
0097     usb_phy->chg_cur.sdp_min = DEFAULT_SDP_CUR_MIN;
0098     usb_phy->chg_cur.sdp_max = DEFAULT_SDP_CUR_MAX;
0099     usb_phy->chg_cur.dcp_min = DEFAULT_DCP_CUR_MIN;
0100     usb_phy->chg_cur.dcp_max = DEFAULT_DCP_CUR_MAX;
0101     usb_phy->chg_cur.cdp_min = DEFAULT_CDP_CUR_MIN;
0102     usb_phy->chg_cur.cdp_max = DEFAULT_CDP_CUR_MAX;
0103     usb_phy->chg_cur.aca_min = DEFAULT_ACA_CUR_MIN;
0104     usb_phy->chg_cur.aca_max = DEFAULT_ACA_CUR_MAX;
0105 }
0106 
0107 /**
0108  * usb_phy_notify_charger_work - notify the USB charger state
0109  * @work: the charger work to notify the USB charger state
0110  *
0111  * This work can be issued when USB charger state has been changed or
0112  * USB charger current has been changed, then we can notify the current
0113  * what can be drawn to power user and the charger state to userspace.
0114  *
0115  * If we get the charger type from extcon subsystem, we can notify the
0116  * charger state to power user automatically by usb_phy_get_charger_type()
0117  * issuing from extcon subsystem.
0118  *
0119  * If we get the charger type from ->charger_detect() instead of extcon
0120  * subsystem, the usb phy driver should issue usb_phy_set_charger_state()
0121  * to set charger state when the charger state has been changed.
0122  */
0123 static void usb_phy_notify_charger_work(struct work_struct *work)
0124 {
0125     struct usb_phy *usb_phy = container_of(work, struct usb_phy, chg_work);
0126     unsigned int min, max;
0127 
0128     switch (usb_phy->chg_state) {
0129     case USB_CHARGER_PRESENT:
0130         usb_phy_get_charger_current(usb_phy, &min, &max);
0131 
0132         atomic_notifier_call_chain(&usb_phy->notifier, max, usb_phy);
0133         break;
0134     case USB_CHARGER_ABSENT:
0135         usb_phy_set_default_current(usb_phy);
0136 
0137         atomic_notifier_call_chain(&usb_phy->notifier, 0, usb_phy);
0138         break;
0139     default:
0140         dev_warn(usb_phy->dev, "Unknown USB charger state: %d\n",
0141              usb_phy->chg_state);
0142         return;
0143     }
0144 
0145     kobject_uevent(&usb_phy->dev->kobj, KOBJ_CHANGE);
0146 }
0147 
0148 static int usb_phy_uevent(struct device *dev, struct kobj_uevent_env *env)
0149 {
0150     struct usb_phy *usb_phy;
0151     char uchger_state[50] = { 0 };
0152     char uchger_type[50] = { 0 };
0153     unsigned long flags;
0154 
0155     spin_lock_irqsave(&phy_lock, flags);
0156     usb_phy = __device_to_usb_phy(dev);
0157     spin_unlock_irqrestore(&phy_lock, flags);
0158 
0159     if (!usb_phy)
0160         return -ENODEV;
0161 
0162     snprintf(uchger_state, ARRAY_SIZE(uchger_state),
0163          "USB_CHARGER_STATE=%s", usb_chger_state[usb_phy->chg_state]);
0164 
0165     snprintf(uchger_type, ARRAY_SIZE(uchger_type),
0166          "USB_CHARGER_TYPE=%s", usb_chger_type[usb_phy->chg_type]);
0167 
0168     if (add_uevent_var(env, uchger_state))
0169         return -ENOMEM;
0170 
0171     if (add_uevent_var(env, uchger_type))
0172         return -ENOMEM;
0173 
0174     return 0;
0175 }
0176 
0177 static void __usb_phy_get_charger_type(struct usb_phy *usb_phy)
0178 {
0179     if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_SDP) > 0) {
0180         usb_phy->chg_type = SDP_TYPE;
0181         usb_phy->chg_state = USB_CHARGER_PRESENT;
0182     } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_CDP) > 0) {
0183         usb_phy->chg_type = CDP_TYPE;
0184         usb_phy->chg_state = USB_CHARGER_PRESENT;
0185     } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_DCP) > 0) {
0186         usb_phy->chg_type = DCP_TYPE;
0187         usb_phy->chg_state = USB_CHARGER_PRESENT;
0188     } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_ACA) > 0) {
0189         usb_phy->chg_type = ACA_TYPE;
0190         usb_phy->chg_state = USB_CHARGER_PRESENT;
0191     } else {
0192         usb_phy->chg_type = UNKNOWN_TYPE;
0193         usb_phy->chg_state = USB_CHARGER_ABSENT;
0194     }
0195 
0196     schedule_work(&usb_phy->chg_work);
0197 }
0198 
0199 /**
0200  * usb_phy_get_charger_type - get charger type from extcon subsystem
0201  * @nb: the notifier block to determine charger type
0202  * @state: the cable state
0203  * @data: private data
0204  *
0205  * Determin the charger type from extcon subsystem which also means the
0206  * charger state has been chaned, then we should notify this event.
0207  */
0208 static int usb_phy_get_charger_type(struct notifier_block *nb,
0209                     unsigned long state, void *data)
0210 {
0211     struct usb_phy *usb_phy = container_of(nb, struct usb_phy, type_nb);
0212 
0213     __usb_phy_get_charger_type(usb_phy);
0214     return NOTIFY_OK;
0215 }
0216 
0217 /**
0218  * usb_phy_set_charger_current - set the USB charger current
0219  * @usb_phy: the USB phy to be used
0220  * @mA: the current need to be set
0221  *
0222  * Usually we only change the charger default current when USB finished the
0223  * enumeration as one SDP charger. As one SDP charger, usb_phy_set_power()
0224  * will issue this function to change charger current when after setting USB
0225  * configuration, or suspend/resume USB. For other type charger, we should
0226  * use the default charger current and we do not suggest to issue this function
0227  * to change the charger current.
0228  *
0229  * When USB charger current has been changed, we need to notify the power users.
0230  */
0231 void usb_phy_set_charger_current(struct usb_phy *usb_phy, unsigned int mA)
0232 {
0233     switch (usb_phy->chg_type) {
0234     case SDP_TYPE:
0235         if (usb_phy->chg_cur.sdp_max == mA)
0236             return;
0237 
0238         usb_phy->chg_cur.sdp_max = (mA > DEFAULT_SDP_CUR_MAX_SS) ?
0239             DEFAULT_SDP_CUR_MAX_SS : mA;
0240         break;
0241     case DCP_TYPE:
0242         if (usb_phy->chg_cur.dcp_max == mA)
0243             return;
0244 
0245         usb_phy->chg_cur.dcp_max = (mA > DEFAULT_DCP_CUR_MAX) ?
0246             DEFAULT_DCP_CUR_MAX : mA;
0247         break;
0248     case CDP_TYPE:
0249         if (usb_phy->chg_cur.cdp_max == mA)
0250             return;
0251 
0252         usb_phy->chg_cur.cdp_max = (mA > DEFAULT_CDP_CUR_MAX) ?
0253             DEFAULT_CDP_CUR_MAX : mA;
0254         break;
0255     case ACA_TYPE:
0256         if (usb_phy->chg_cur.aca_max == mA)
0257             return;
0258 
0259         usb_phy->chg_cur.aca_max = (mA > DEFAULT_ACA_CUR_MAX) ?
0260             DEFAULT_ACA_CUR_MAX : mA;
0261         break;
0262     default:
0263         return;
0264     }
0265 
0266     schedule_work(&usb_phy->chg_work);
0267 }
0268 EXPORT_SYMBOL_GPL(usb_phy_set_charger_current);
0269 
0270 /**
0271  * usb_phy_get_charger_current - get the USB charger current
0272  * @usb_phy: the USB phy to be used
0273  * @min: the minimum current
0274  * @max: the maximum current
0275  *
0276  * Usually we will notify the maximum current to power user, but for some
0277  * special case, power user also need the minimum current value. Then the
0278  * power user can issue this function to get the suitable current.
0279  */
0280 void usb_phy_get_charger_current(struct usb_phy *usb_phy,
0281                  unsigned int *min, unsigned int *max)
0282 {
0283     switch (usb_phy->chg_type) {
0284     case SDP_TYPE:
0285         *min = usb_phy->chg_cur.sdp_min;
0286         *max = usb_phy->chg_cur.sdp_max;
0287         break;
0288     case DCP_TYPE:
0289         *min = usb_phy->chg_cur.dcp_min;
0290         *max = usb_phy->chg_cur.dcp_max;
0291         break;
0292     case CDP_TYPE:
0293         *min = usb_phy->chg_cur.cdp_min;
0294         *max = usb_phy->chg_cur.cdp_max;
0295         break;
0296     case ACA_TYPE:
0297         *min = usb_phy->chg_cur.aca_min;
0298         *max = usb_phy->chg_cur.aca_max;
0299         break;
0300     default:
0301         *min = 0;
0302         *max = 0;
0303         break;
0304     }
0305 }
0306 EXPORT_SYMBOL_GPL(usb_phy_get_charger_current);
0307 
0308 /**
0309  * usb_phy_set_charger_state - set the USB charger state
0310  * @usb_phy: the USB phy to be used
0311  * @state: the new state need to be set for charger
0312  *
0313  * The usb phy driver can issue this function when the usb phy driver
0314  * detected the charger state has been changed, in this case the charger
0315  * type should be get from ->charger_detect().
0316  */
0317 void usb_phy_set_charger_state(struct usb_phy *usb_phy,
0318                    enum usb_charger_state state)
0319 {
0320     if (usb_phy->chg_state == state || !usb_phy->charger_detect)
0321         return;
0322 
0323     usb_phy->chg_state = state;
0324     if (usb_phy->chg_state == USB_CHARGER_PRESENT)
0325         usb_phy->chg_type = usb_phy->charger_detect(usb_phy);
0326     else
0327         usb_phy->chg_type = UNKNOWN_TYPE;
0328 
0329     schedule_work(&usb_phy->chg_work);
0330 }
0331 EXPORT_SYMBOL_GPL(usb_phy_set_charger_state);
0332 
0333 static void devm_usb_phy_release(struct device *dev, void *res)
0334 {
0335     struct usb_phy *phy = *(struct usb_phy **)res;
0336 
0337     usb_put_phy(phy);
0338 }
0339 
0340 static void devm_usb_phy_release2(struct device *dev, void *_res)
0341 {
0342     struct phy_devm *res = _res;
0343 
0344     if (res->nb)
0345         usb_unregister_notifier(res->phy, res->nb);
0346     usb_put_phy(res->phy);
0347 }
0348 
0349 static int devm_usb_phy_match(struct device *dev, void *res, void *match_data)
0350 {
0351     struct usb_phy **phy = res;
0352 
0353     return *phy == match_data;
0354 }
0355 
0356 static void usb_charger_init(struct usb_phy *usb_phy)
0357 {
0358     usb_phy->chg_type = UNKNOWN_TYPE;
0359     usb_phy->chg_state = USB_CHARGER_DEFAULT;
0360     usb_phy_set_default_current(usb_phy);
0361     INIT_WORK(&usb_phy->chg_work, usb_phy_notify_charger_work);
0362 }
0363 
0364 static int usb_add_extcon(struct usb_phy *x)
0365 {
0366     int ret;
0367 
0368     if (of_property_read_bool(x->dev->of_node, "extcon")) {
0369         x->edev = extcon_get_edev_by_phandle(x->dev, 0);
0370         if (IS_ERR(x->edev))
0371             return PTR_ERR(x->edev);
0372 
0373         x->id_edev = extcon_get_edev_by_phandle(x->dev, 1);
0374         if (IS_ERR(x->id_edev)) {
0375             x->id_edev = NULL;
0376             dev_info(x->dev, "No separate ID extcon device\n");
0377         }
0378 
0379         if (x->vbus_nb.notifier_call) {
0380             ret = devm_extcon_register_notifier(x->dev, x->edev,
0381                                 EXTCON_USB,
0382                                 &x->vbus_nb);
0383             if (ret < 0) {
0384                 dev_err(x->dev,
0385                     "register VBUS notifier failed\n");
0386                 return ret;
0387             }
0388         } else {
0389             x->type_nb.notifier_call = usb_phy_get_charger_type;
0390 
0391             ret = devm_extcon_register_notifier(x->dev, x->edev,
0392                                 EXTCON_CHG_USB_SDP,
0393                                 &x->type_nb);
0394             if (ret) {
0395                 dev_err(x->dev,
0396                     "register extcon USB SDP failed.\n");
0397                 return ret;
0398             }
0399 
0400             ret = devm_extcon_register_notifier(x->dev, x->edev,
0401                                 EXTCON_CHG_USB_CDP,
0402                                 &x->type_nb);
0403             if (ret) {
0404                 dev_err(x->dev,
0405                     "register extcon USB CDP failed.\n");
0406                 return ret;
0407             }
0408 
0409             ret = devm_extcon_register_notifier(x->dev, x->edev,
0410                                 EXTCON_CHG_USB_DCP,
0411                                 &x->type_nb);
0412             if (ret) {
0413                 dev_err(x->dev,
0414                     "register extcon USB DCP failed.\n");
0415                 return ret;
0416             }
0417 
0418             ret = devm_extcon_register_notifier(x->dev, x->edev,
0419                                 EXTCON_CHG_USB_ACA,
0420                                 &x->type_nb);
0421             if (ret) {
0422                 dev_err(x->dev,
0423                     "register extcon USB ACA failed.\n");
0424                 return ret;
0425             }
0426         }
0427 
0428         if (x->id_nb.notifier_call) {
0429             struct extcon_dev *id_ext;
0430 
0431             if (x->id_edev)
0432                 id_ext = x->id_edev;
0433             else
0434                 id_ext = x->edev;
0435 
0436             ret = devm_extcon_register_notifier(x->dev, id_ext,
0437                                 EXTCON_USB_HOST,
0438                                 &x->id_nb);
0439             if (ret < 0) {
0440                 dev_err(x->dev,
0441                     "register ID notifier failed\n");
0442                 return ret;
0443             }
0444         }
0445     }
0446 
0447     if (x->type_nb.notifier_call)
0448         __usb_phy_get_charger_type(x);
0449 
0450     return 0;
0451 }
0452 
0453 /**
0454  * devm_usb_get_phy - find the USB PHY
0455  * @dev: device that requests this phy
0456  * @type: the type of the phy the controller requires
0457  *
0458  * Gets the phy using usb_get_phy(), and associates a device with it using
0459  * devres. On driver detach, release function is invoked on the devres data,
0460  * then, devres data is freed.
0461  *
0462  * For use by USB host and peripheral drivers.
0463  */
0464 struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type)
0465 {
0466     struct usb_phy **ptr, *phy;
0467 
0468     ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
0469     if (!ptr)
0470         return ERR_PTR(-ENOMEM);
0471 
0472     phy = usb_get_phy(type);
0473     if (!IS_ERR(phy)) {
0474         *ptr = phy;
0475         devres_add(dev, ptr);
0476     } else
0477         devres_free(ptr);
0478 
0479     return phy;
0480 }
0481 EXPORT_SYMBOL_GPL(devm_usb_get_phy);
0482 
0483 /**
0484  * usb_get_phy - find the USB PHY
0485  * @type: the type of the phy the controller requires
0486  *
0487  * Returns the phy driver, after getting a refcount to it; or
0488  * -ENODEV if there is no such phy.  The caller is responsible for
0489  * calling usb_put_phy() to release that count.
0490  *
0491  * For use by USB host and peripheral drivers.
0492  */
0493 struct usb_phy *usb_get_phy(enum usb_phy_type type)
0494 {
0495     struct usb_phy  *phy = NULL;
0496     unsigned long   flags;
0497 
0498     spin_lock_irqsave(&phy_lock, flags);
0499 
0500     phy = __usb_find_phy(&phy_list, type);
0501     if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) {
0502         pr_debug("PHY: unable to find transceiver of type %s\n",
0503             usb_phy_type_string(type));
0504         if (!IS_ERR(phy))
0505             phy = ERR_PTR(-ENODEV);
0506 
0507         goto err0;
0508     }
0509 
0510     get_device(phy->dev);
0511 
0512 err0:
0513     spin_unlock_irqrestore(&phy_lock, flags);
0514 
0515     return phy;
0516 }
0517 EXPORT_SYMBOL_GPL(usb_get_phy);
0518 
0519 /**
0520  * devm_usb_get_phy_by_node - find the USB PHY by device_node
0521  * @dev: device that requests this phy
0522  * @node: the device_node for the phy device.
0523  * @nb: a notifier_block to register with the phy.
0524  *
0525  * Returns the phy driver associated with the given device_node,
0526  * after getting a refcount to it, -ENODEV if there is no such phy or
0527  * -EPROBE_DEFER if the device is not yet loaded. While at that, it
0528  * also associates the device with
0529  * the phy using devres. On driver detach, release function is invoked
0530  * on the devres data, then, devres data is freed.
0531  *
0532  * For use by peripheral drivers for devices related to a phy,
0533  * such as a charger.
0534  */
0535 struct  usb_phy *devm_usb_get_phy_by_node(struct device *dev,
0536                       struct device_node *node,
0537                       struct notifier_block *nb)
0538 {
0539     struct usb_phy  *phy = ERR_PTR(-ENOMEM);
0540     struct phy_devm *ptr;
0541     unsigned long   flags;
0542 
0543     ptr = devres_alloc(devm_usb_phy_release2, sizeof(*ptr), GFP_KERNEL);
0544     if (!ptr) {
0545         dev_dbg(dev, "failed to allocate memory for devres\n");
0546         goto err0;
0547     }
0548 
0549     spin_lock_irqsave(&phy_lock, flags);
0550 
0551     phy = __of_usb_find_phy(node);
0552     if (IS_ERR(phy)) {
0553         devres_free(ptr);
0554         goto err1;
0555     }
0556 
0557     if (!try_module_get(phy->dev->driver->owner)) {
0558         phy = ERR_PTR(-ENODEV);
0559         devres_free(ptr);
0560         goto err1;
0561     }
0562     if (nb)
0563         usb_register_notifier(phy, nb);
0564     ptr->phy = phy;
0565     ptr->nb = nb;
0566     devres_add(dev, ptr);
0567 
0568     get_device(phy->dev);
0569 
0570 err1:
0571     spin_unlock_irqrestore(&phy_lock, flags);
0572 
0573 err0:
0574 
0575     return phy;
0576 }
0577 EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_node);
0578 
0579 /**
0580  * devm_usb_get_phy_by_phandle - find the USB PHY by phandle
0581  * @dev: device that requests this phy
0582  * @phandle: name of the property holding the phy phandle value
0583  * @index: the index of the phy
0584  *
0585  * Returns the phy driver associated with the given phandle value,
0586  * after getting a refcount to it, -ENODEV if there is no such phy or
0587  * -EPROBE_DEFER if there is a phandle to the phy, but the device is
0588  * not yet loaded. While at that, it also associates the device with
0589  * the phy using devres. On driver detach, release function is invoked
0590  * on the devres data, then, devres data is freed.
0591  *
0592  * For use by USB host and peripheral drivers.
0593  */
0594 struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
0595     const char *phandle, u8 index)
0596 {
0597     struct device_node *node;
0598     struct usb_phy  *phy;
0599 
0600     if (!dev->of_node) {
0601         dev_dbg(dev, "device does not have a device node entry\n");
0602         return ERR_PTR(-EINVAL);
0603     }
0604 
0605     node = of_parse_phandle(dev->of_node, phandle, index);
0606     if (!node) {
0607         dev_dbg(dev, "failed to get %s phandle in %pOF node\n", phandle,
0608             dev->of_node);
0609         return ERR_PTR(-ENODEV);
0610     }
0611     phy = devm_usb_get_phy_by_node(dev, node, NULL);
0612     of_node_put(node);
0613     return phy;
0614 }
0615 EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_phandle);
0616 
0617 /**
0618  * devm_usb_put_phy - release the USB PHY
0619  * @dev: device that wants to release this phy
0620  * @phy: the phy returned by devm_usb_get_phy()
0621  *
0622  * destroys the devres associated with this phy and invokes usb_put_phy
0623  * to release the phy.
0624  *
0625  * For use by USB host and peripheral drivers.
0626  */
0627 void devm_usb_put_phy(struct device *dev, struct usb_phy *phy)
0628 {
0629     int r;
0630 
0631     r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy);
0632     dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
0633 }
0634 EXPORT_SYMBOL_GPL(devm_usb_put_phy);
0635 
0636 /**
0637  * usb_put_phy - release the USB PHY
0638  * @x: the phy returned by usb_get_phy()
0639  *
0640  * Releases a refcount the caller received from usb_get_phy().
0641  *
0642  * For use by USB host and peripheral drivers.
0643  */
0644 void usb_put_phy(struct usb_phy *x)
0645 {
0646     if (x) {
0647         struct module *owner = x->dev->driver->owner;
0648 
0649         put_device(x->dev);
0650         module_put(owner);
0651     }
0652 }
0653 EXPORT_SYMBOL_GPL(usb_put_phy);
0654 
0655 /**
0656  * usb_add_phy: declare the USB PHY
0657  * @x: the USB phy to be used; or NULL
0658  * @type: the type of this PHY
0659  *
0660  * This call is exclusively for use by phy drivers, which
0661  * coordinate the activities of drivers for host and peripheral
0662  * controllers, and in some cases for VBUS current regulation.
0663  */
0664 int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
0665 {
0666     int     ret = 0;
0667     unsigned long   flags;
0668     struct usb_phy  *phy;
0669 
0670     if (x->type != USB_PHY_TYPE_UNDEFINED) {
0671         dev_err(x->dev, "not accepting initialized PHY %s\n", x->label);
0672         return -EINVAL;
0673     }
0674 
0675     usb_charger_init(x);
0676     ret = usb_add_extcon(x);
0677     if (ret)
0678         return ret;
0679 
0680     ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier);
0681 
0682     spin_lock_irqsave(&phy_lock, flags);
0683 
0684     list_for_each_entry(phy, &phy_list, head) {
0685         if (phy->type == type) {
0686             ret = -EBUSY;
0687             dev_err(x->dev, "transceiver type %s already exists\n",
0688                         usb_phy_type_string(type));
0689             goto out;
0690         }
0691     }
0692 
0693     x->type = type;
0694     list_add_tail(&x->head, &phy_list);
0695 
0696 out:
0697     spin_unlock_irqrestore(&phy_lock, flags);
0698     return ret;
0699 }
0700 EXPORT_SYMBOL_GPL(usb_add_phy);
0701 
0702 static struct device_type usb_phy_dev_type = {
0703     .name = "usb_phy",
0704     .uevent = usb_phy_uevent,
0705 };
0706 
0707 /**
0708  * usb_add_phy_dev - declare the USB PHY
0709  * @x: the USB phy to be used; or NULL
0710  *
0711  * This call is exclusively for use by phy drivers, which
0712  * coordinate the activities of drivers for host and peripheral
0713  * controllers, and in some cases for VBUS current regulation.
0714  */
0715 int usb_add_phy_dev(struct usb_phy *x)
0716 {
0717     unsigned long flags;
0718     int ret;
0719 
0720     if (!x->dev) {
0721         dev_err(x->dev, "no device provided for PHY\n");
0722         return -EINVAL;
0723     }
0724 
0725     usb_charger_init(x);
0726     ret = usb_add_extcon(x);
0727     if (ret)
0728         return ret;
0729 
0730     x->dev->type = &usb_phy_dev_type;
0731 
0732     ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier);
0733 
0734     spin_lock_irqsave(&phy_lock, flags);
0735     list_add_tail(&x->head, &phy_list);
0736     spin_unlock_irqrestore(&phy_lock, flags);
0737 
0738     return 0;
0739 }
0740 EXPORT_SYMBOL_GPL(usb_add_phy_dev);
0741 
0742 /**
0743  * usb_remove_phy - remove the OTG PHY
0744  * @x: the USB OTG PHY to be removed;
0745  *
0746  * This reverts the effects of usb_add_phy
0747  */
0748 void usb_remove_phy(struct usb_phy *x)
0749 {
0750     unsigned long   flags;
0751 
0752     spin_lock_irqsave(&phy_lock, flags);
0753     if (x)
0754         list_del(&x->head);
0755     spin_unlock_irqrestore(&phy_lock, flags);
0756 }
0757 EXPORT_SYMBOL_GPL(usb_remove_phy);
0758 
0759 /**
0760  * usb_phy_set_event - set event to phy event
0761  * @x: the phy returned by usb_get_phy();
0762  * @event: event to set
0763  *
0764  * This sets event to phy event
0765  */
0766 void usb_phy_set_event(struct usb_phy *x, unsigned long event)
0767 {
0768     x->last_event = event;
0769 }
0770 EXPORT_SYMBOL_GPL(usb_phy_set_event);