linux/drivers/usb/phy/phy.c
<<
>>
Prefs
   1/*
   2 * phy.c -- USB phy handling
   3 *
   4 * Copyright (C) 2004-2013 Texas Instruments
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License as published by
   8 * the Free Software Foundation; either version 2 of the License, or
   9 * (at your option) any later version.
  10 */
  11#include <linux/kernel.h>
  12#include <linux/export.h>
  13#include <linux/err.h>
  14#include <linux/device.h>
  15#include <linux/module.h>
  16#include <linux/slab.h>
  17#include <linux/of.h>
  18
  19#include <linux/usb/phy.h>
  20
  21/* Default current range by charger type. */
  22#define DEFAULT_SDP_CUR_MIN     2
  23#define DEFAULT_SDP_CUR_MAX     500
  24#define DEFAULT_SDP_CUR_MIN_SS  150
  25#define DEFAULT_SDP_CUR_MAX_SS  900
  26#define DEFAULT_DCP_CUR_MIN     500
  27#define DEFAULT_DCP_CUR_MAX     5000
  28#define DEFAULT_CDP_CUR_MIN     1500
  29#define DEFAULT_CDP_CUR_MAX     5000
  30#define DEFAULT_ACA_CUR_MIN     1500
  31#define DEFAULT_ACA_CUR_MAX     5000
  32
  33static LIST_HEAD(phy_list);
  34static LIST_HEAD(phy_bind_list);
  35static DEFINE_SPINLOCK(phy_lock);
  36
  37struct phy_devm {
  38        struct usb_phy *phy;
  39        struct notifier_block *nb;
  40};
  41
  42static struct usb_phy *__usb_find_phy(struct list_head *list,
  43        enum usb_phy_type type)
  44{
  45        struct usb_phy  *phy = NULL;
  46
  47        list_for_each_entry(phy, list, head) {
  48                if (phy->type != type)
  49                        continue;
  50
  51                return phy;
  52        }
  53
  54        return ERR_PTR(-ENODEV);
  55}
  56
  57static struct usb_phy *__usb_find_phy_dev(struct device *dev,
  58        struct list_head *list, u8 index)
  59{
  60        struct usb_phy_bind *phy_bind = NULL;
  61
  62        list_for_each_entry(phy_bind, list, list) {
  63                if (!(strcmp(phy_bind->dev_name, dev_name(dev))) &&
  64                                phy_bind->index == index) {
  65                        if (phy_bind->phy)
  66                                return phy_bind->phy;
  67                        else
  68                                return ERR_PTR(-EPROBE_DEFER);
  69                }
  70        }
  71
  72        return ERR_PTR(-ENODEV);
  73}
  74
  75static struct usb_phy *__of_usb_find_phy(struct device_node *node)
  76{
  77        struct usb_phy  *phy;
  78
  79        if (!of_device_is_available(node))
  80                return ERR_PTR(-ENODEV);
  81
  82        list_for_each_entry(phy, &phy_list, head) {
  83                if (node != phy->dev->of_node)
  84                        continue;
  85
  86                return phy;
  87        }
  88
  89        return ERR_PTR(-EPROBE_DEFER);
  90}
  91
  92static void usb_phy_set_default_current(struct usb_phy *usb_phy)
  93{
  94        usb_phy->chg_cur.sdp_min = DEFAULT_SDP_CUR_MIN;
  95        usb_phy->chg_cur.sdp_max = DEFAULT_SDP_CUR_MAX;
  96        usb_phy->chg_cur.dcp_min = DEFAULT_DCP_CUR_MIN;
  97        usb_phy->chg_cur.dcp_max = DEFAULT_DCP_CUR_MAX;
  98        usb_phy->chg_cur.cdp_min = DEFAULT_CDP_CUR_MIN;
  99        usb_phy->chg_cur.cdp_max = DEFAULT_CDP_CUR_MAX;
 100        usb_phy->chg_cur.aca_min = DEFAULT_ACA_CUR_MIN;
 101        usb_phy->chg_cur.aca_max = DEFAULT_ACA_CUR_MAX;
 102}
 103
 104/**
 105 * usb_phy_notify_charger_work - notify the USB charger state
 106 * @work - the charger work to notify the USB charger state
 107 *
 108 * This work can be issued when USB charger state has been changed or
 109 * USB charger current has been changed, then we can notify the current
 110 * what can be drawn to power user and the charger state to userspace.
 111 *
 112 * If we get the charger type from extcon subsystem, we can notify the
 113 * charger state to power user automatically by usb_phy_get_charger_type()
 114 * issuing from extcon subsystem.
 115 *
 116 * If we get the charger type from ->charger_detect() instead of extcon
 117 * subsystem, the usb phy driver should issue usb_phy_set_charger_state()
 118 * to set charger state when the charger state has been changed.
 119 */
 120static void usb_phy_notify_charger_work(struct work_struct *work)
 121{
 122        struct usb_phy *usb_phy = container_of(work, struct usb_phy, chg_work);
 123        char uchger_state[50] = { 0 };
 124        char *envp[] = { uchger_state, NULL };
 125        unsigned int min, max;
 126
 127        switch (usb_phy->chg_state) {
 128        case USB_CHARGER_PRESENT:
 129                usb_phy_get_charger_current(usb_phy, &min, &max);
 130
 131                atomic_notifier_call_chain(&usb_phy->notifier, max, usb_phy);
 132                snprintf(uchger_state, ARRAY_SIZE(uchger_state),
 133                         "USB_CHARGER_STATE=%s", "USB_CHARGER_PRESENT");
 134                break;
 135        case USB_CHARGER_ABSENT:
 136                usb_phy_set_default_current(usb_phy);
 137
 138                atomic_notifier_call_chain(&usb_phy->notifier, 0, usb_phy);
 139                snprintf(uchger_state, ARRAY_SIZE(uchger_state),
 140                         "USB_CHARGER_STATE=%s", "USB_CHARGER_ABSENT");
 141                break;
 142        default:
 143                dev_warn(usb_phy->dev, "Unknown USB charger state: %d\n",
 144                         usb_phy->chg_state);
 145                return;
 146        }
 147
 148        kobject_uevent_env(&usb_phy->dev->kobj, KOBJ_CHANGE, envp);
 149}
 150
 151static void __usb_phy_get_charger_type(struct usb_phy *usb_phy)
 152{
 153        if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_SDP) > 0) {
 154                usb_phy->chg_type = SDP_TYPE;
 155                usb_phy->chg_state = USB_CHARGER_PRESENT;
 156        } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_CDP) > 0) {
 157                usb_phy->chg_type = CDP_TYPE;
 158                usb_phy->chg_state = USB_CHARGER_PRESENT;
 159        } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_DCP) > 0) {
 160                usb_phy->chg_type = DCP_TYPE;
 161                usb_phy->chg_state = USB_CHARGER_PRESENT;
 162        } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_ACA) > 0) {
 163                usb_phy->chg_type = ACA_TYPE;
 164                usb_phy->chg_state = USB_CHARGER_PRESENT;
 165        } else {
 166                usb_phy->chg_type = UNKNOWN_TYPE;
 167                usb_phy->chg_state = USB_CHARGER_ABSENT;
 168        }
 169
 170        schedule_work(&usb_phy->chg_work);
 171}
 172
 173/**
 174 * usb_phy_get_charger_type - get charger type from extcon subsystem
 175 * @nb -the notifier block to determine charger type
 176 * @state - the cable state
 177 * @data - private data
 178 *
 179 * Determin the charger type from extcon subsystem which also means the
 180 * charger state has been chaned, then we should notify this event.
 181 */
 182static int usb_phy_get_charger_type(struct notifier_block *nb,
 183                                    unsigned long state, void *data)
 184{
 185        struct usb_phy *usb_phy = container_of(nb, struct usb_phy, type_nb);
 186
 187        __usb_phy_get_charger_type(usb_phy);
 188        return NOTIFY_OK;
 189}
 190
 191/**
 192 * usb_phy_set_charger_current - set the USB charger current
 193 * @usb_phy - the USB phy to be used
 194 * @mA - the current need to be set
 195 *
 196 * Usually we only change the charger default current when USB finished the
 197 * enumeration as one SDP charger. As one SDP charger, usb_phy_set_power()
 198 * will issue this function to change charger current when after setting USB
 199 * configuration, or suspend/resume USB. For other type charger, we should
 200 * use the default charger current and we do not suggest to issue this function
 201 * to change the charger current.
 202 *
 203 * When USB charger current has been changed, we need to notify the power users.
 204 */
 205void usb_phy_set_charger_current(struct usb_phy *usb_phy, unsigned int mA)
 206{
 207        switch (usb_phy->chg_type) {
 208        case SDP_TYPE:
 209                if (usb_phy->chg_cur.sdp_max == mA)
 210                        return;
 211
 212                usb_phy->chg_cur.sdp_max = (mA > DEFAULT_SDP_CUR_MAX_SS) ?
 213                        DEFAULT_SDP_CUR_MAX_SS : mA;
 214                break;
 215        case DCP_TYPE:
 216                if (usb_phy->chg_cur.dcp_max == mA)
 217                        return;
 218
 219                usb_phy->chg_cur.dcp_max = (mA > DEFAULT_DCP_CUR_MAX) ?
 220                        DEFAULT_DCP_CUR_MAX : mA;
 221                break;
 222        case CDP_TYPE:
 223                if (usb_phy->chg_cur.cdp_max == mA)
 224                        return;
 225
 226                usb_phy->chg_cur.cdp_max = (mA > DEFAULT_CDP_CUR_MAX) ?
 227                        DEFAULT_CDP_CUR_MAX : mA;
 228                break;
 229        case ACA_TYPE:
 230                if (usb_phy->chg_cur.aca_max == mA)
 231                        return;
 232
 233                usb_phy->chg_cur.aca_max = (mA > DEFAULT_ACA_CUR_MAX) ?
 234                        DEFAULT_ACA_CUR_MAX : mA;
 235                break;
 236        default:
 237                return;
 238        }
 239
 240        schedule_work(&usb_phy->chg_work);
 241}
 242EXPORT_SYMBOL_GPL(usb_phy_set_charger_current);
 243
 244/**
 245 * usb_phy_get_charger_current - get the USB charger current
 246 * @usb_phy - the USB phy to be used
 247 * @min - the minimum current
 248 * @max - the maximum current
 249 *
 250 * Usually we will notify the maximum current to power user, but for some
 251 * special case, power user also need the minimum current value. Then the
 252 * power user can issue this function to get the suitable current.
 253 */
 254void usb_phy_get_charger_current(struct usb_phy *usb_phy,
 255                                 unsigned int *min, unsigned int *max)
 256{
 257        switch (usb_phy->chg_type) {
 258        case SDP_TYPE:
 259                *min = usb_phy->chg_cur.sdp_min;
 260                *max = usb_phy->chg_cur.sdp_max;
 261                break;
 262        case DCP_TYPE:
 263                *min = usb_phy->chg_cur.dcp_min;
 264                *max = usb_phy->chg_cur.dcp_max;
 265                break;
 266        case CDP_TYPE:
 267                *min = usb_phy->chg_cur.cdp_min;
 268                *max = usb_phy->chg_cur.cdp_max;
 269                break;
 270        case ACA_TYPE:
 271                *min = usb_phy->chg_cur.aca_min;
 272                *max = usb_phy->chg_cur.aca_max;
 273                break;
 274        default:
 275                *min = 0;
 276                *max = 0;
 277                break;
 278        }
 279}
 280EXPORT_SYMBOL_GPL(usb_phy_get_charger_current);
 281
 282/**
 283 * usb_phy_set_charger_state - set the USB charger state
 284 * @usb_phy - the USB phy to be used
 285 * @state - the new state need to be set for charger
 286 *
 287 * The usb phy driver can issue this function when the usb phy driver
 288 * detected the charger state has been changed, in this case the charger
 289 * type should be get from ->charger_detect().
 290 */
 291void usb_phy_set_charger_state(struct usb_phy *usb_phy,
 292                               enum usb_charger_state state)
 293{
 294        if (usb_phy->chg_state == state || !usb_phy->charger_detect)
 295                return;
 296
 297        usb_phy->chg_state = state;
 298        if (usb_phy->chg_state == USB_CHARGER_PRESENT)
 299                usb_phy->chg_type = usb_phy->charger_detect(usb_phy);
 300        else
 301                usb_phy->chg_type = UNKNOWN_TYPE;
 302
 303        schedule_work(&usb_phy->chg_work);
 304}
 305EXPORT_SYMBOL_GPL(usb_phy_set_charger_state);
 306
 307static void devm_usb_phy_release(struct device *dev, void *res)
 308{
 309        struct usb_phy *phy = *(struct usb_phy **)res;
 310
 311        usb_put_phy(phy);
 312}
 313
 314static void devm_usb_phy_release2(struct device *dev, void *_res)
 315{
 316        struct phy_devm *res = _res;
 317
 318        if (res->nb)
 319                usb_unregister_notifier(res->phy, res->nb);
 320        usb_put_phy(res->phy);
 321}
 322
 323static int devm_usb_phy_match(struct device *dev, void *res, void *match_data)
 324{
 325        struct usb_phy **phy = res;
 326
 327        return *phy == match_data;
 328}
 329
 330static int usb_add_extcon(struct usb_phy *x)
 331{
 332        int ret;
 333
 334        if (of_property_read_bool(x->dev->of_node, "extcon")) {
 335                x->edev = extcon_get_edev_by_phandle(x->dev, 0);
 336                if (IS_ERR(x->edev))
 337                        return PTR_ERR(x->edev);
 338
 339                x->id_edev = extcon_get_edev_by_phandle(x->dev, 1);
 340                if (IS_ERR(x->id_edev)) {
 341                        x->id_edev = NULL;
 342                        dev_info(x->dev, "No separate ID extcon device\n");
 343                }
 344
 345                if (x->vbus_nb.notifier_call) {
 346                        ret = devm_extcon_register_notifier(x->dev, x->edev,
 347                                                            EXTCON_USB,
 348                                                            &x->vbus_nb);
 349                        if (ret < 0) {
 350                                dev_err(x->dev,
 351                                        "register VBUS notifier failed\n");
 352                                return ret;
 353                        }
 354                } else {
 355                        x->type_nb.notifier_call = usb_phy_get_charger_type;
 356
 357                        ret = devm_extcon_register_notifier(x->dev, x->edev,
 358                                                            EXTCON_CHG_USB_SDP,
 359                                                            &x->type_nb);
 360                        if (ret) {
 361                                dev_err(x->dev,
 362                                        "register extcon USB SDP failed.\n");
 363                                return ret;
 364                        }
 365
 366                        ret = devm_extcon_register_notifier(x->dev, x->edev,
 367                                                            EXTCON_CHG_USB_CDP,
 368                                                            &x->type_nb);
 369                        if (ret) {
 370                                dev_err(x->dev,
 371                                        "register extcon USB CDP failed.\n");
 372                                return ret;
 373                        }
 374
 375                        ret = devm_extcon_register_notifier(x->dev, x->edev,
 376                                                            EXTCON_CHG_USB_DCP,
 377                                                            &x->type_nb);
 378                        if (ret) {
 379                                dev_err(x->dev,
 380                                        "register extcon USB DCP failed.\n");
 381                                return ret;
 382                        }
 383
 384                        ret = devm_extcon_register_notifier(x->dev, x->edev,
 385                                                            EXTCON_CHG_USB_ACA,
 386                                                            &x->type_nb);
 387                        if (ret) {
 388                                dev_err(x->dev,
 389                                        "register extcon USB ACA failed.\n");
 390                                return ret;
 391                        }
 392                }
 393
 394                if (x->id_nb.notifier_call) {
 395                        struct extcon_dev *id_ext;
 396
 397                        if (x->id_edev)
 398                                id_ext = x->id_edev;
 399                        else
 400                                id_ext = x->edev;
 401
 402                        ret = devm_extcon_register_notifier(x->dev, id_ext,
 403                                                            EXTCON_USB_HOST,
 404                                                            &x->id_nb);
 405                        if (ret < 0) {
 406                                dev_err(x->dev,
 407                                        "register ID notifier failed\n");
 408                                return ret;
 409                        }
 410                }
 411        }
 412
 413        usb_phy_set_default_current(x);
 414        INIT_WORK(&x->chg_work, usb_phy_notify_charger_work);
 415        x->chg_type = UNKNOWN_TYPE;
 416        x->chg_state = USB_CHARGER_DEFAULT;
 417        if (x->type_nb.notifier_call)
 418                __usb_phy_get_charger_type(x);
 419
 420        return 0;
 421}
 422
 423/**
 424 * devm_usb_get_phy - find the USB PHY
 425 * @dev - device that requests this phy
 426 * @type - the type of the phy the controller requires
 427 *
 428 * Gets the phy using usb_get_phy(), and associates a device with it using
 429 * devres. On driver detach, release function is invoked on the devres data,
 430 * then, devres data is freed.
 431 *
 432 * For use by USB host and peripheral drivers.
 433 */
 434struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type)
 435{
 436        struct usb_phy **ptr, *phy;
 437
 438        ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
 439        if (!ptr)
 440                return ERR_PTR(-ENOMEM);
 441
 442        phy = usb_get_phy(type);
 443        if (!IS_ERR(phy)) {
 444                *ptr = phy;
 445                devres_add(dev, ptr);
 446        } else
 447                devres_free(ptr);
 448
 449        return phy;
 450}
 451EXPORT_SYMBOL_GPL(devm_usb_get_phy);
 452
 453/**
 454 * usb_get_phy - find the USB PHY
 455 * @type - the type of the phy the controller requires
 456 *
 457 * Returns the phy driver, after getting a refcount to it; or
 458 * -ENODEV if there is no such phy.  The caller is responsible for
 459 * calling usb_put_phy() to release that count.
 460 *
 461 * For use by USB host and peripheral drivers.
 462 */
 463struct usb_phy *usb_get_phy(enum usb_phy_type type)
 464{
 465        struct usb_phy  *phy = NULL;
 466        unsigned long   flags;
 467
 468        spin_lock_irqsave(&phy_lock, flags);
 469
 470        phy = __usb_find_phy(&phy_list, type);
 471        if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) {
 472                pr_debug("PHY: unable to find transceiver of type %s\n",
 473                        usb_phy_type_string(type));
 474                if (!IS_ERR(phy))
 475                        phy = ERR_PTR(-ENODEV);
 476
 477                goto err0;
 478        }
 479
 480        get_device(phy->dev);
 481
 482err0:
 483        spin_unlock_irqrestore(&phy_lock, flags);
 484
 485        return phy;
 486}
 487EXPORT_SYMBOL_GPL(usb_get_phy);
 488
 489/**
 490 * devm_usb_get_phy_by_node - find the USB PHY by device_node
 491 * @dev - device that requests this phy
 492 * @node - the device_node for the phy device.
 493 * @nb - a notifier_block to register with the phy.
 494 *
 495 * Returns the phy driver associated with the given device_node,
 496 * after getting a refcount to it, -ENODEV if there is no such phy or
 497 * -EPROBE_DEFER if the device is not yet loaded. While at that, it
 498 * also associates the device with
 499 * the phy using devres. On driver detach, release function is invoked
 500 * on the devres data, then, devres data is freed.
 501 *
 502 * For use by peripheral drivers for devices related to a phy,
 503 * such as a charger.
 504 */
 505struct  usb_phy *devm_usb_get_phy_by_node(struct device *dev,
 506                                          struct device_node *node,
 507                                          struct notifier_block *nb)
 508{
 509        struct usb_phy  *phy = ERR_PTR(-ENOMEM);
 510        struct phy_devm *ptr;
 511        unsigned long   flags;
 512
 513        ptr = devres_alloc(devm_usb_phy_release2, sizeof(*ptr), GFP_KERNEL);
 514        if (!ptr) {
 515                dev_dbg(dev, "failed to allocate memory for devres\n");
 516                goto err0;
 517        }
 518
 519        spin_lock_irqsave(&phy_lock, flags);
 520
 521        phy = __of_usb_find_phy(node);
 522        if (IS_ERR(phy)) {
 523                devres_free(ptr);
 524                goto err1;
 525        }
 526
 527        if (!try_module_get(phy->dev->driver->owner)) {
 528                phy = ERR_PTR(-ENODEV);
 529                devres_free(ptr);
 530                goto err1;
 531        }
 532        if (nb)
 533                usb_register_notifier(phy, nb);
 534        ptr->phy = phy;
 535        ptr->nb = nb;
 536        devres_add(dev, ptr);
 537
 538        get_device(phy->dev);
 539
 540err1:
 541        spin_unlock_irqrestore(&phy_lock, flags);
 542
 543err0:
 544
 545        return phy;
 546}
 547EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_node);
 548
 549/**
 550 * devm_usb_get_phy_by_phandle - find the USB PHY by phandle
 551 * @dev - device that requests this phy
 552 * @phandle - name of the property holding the phy phandle value
 553 * @index - the index of the phy
 554 *
 555 * Returns the phy driver associated with the given phandle value,
 556 * after getting a refcount to it, -ENODEV if there is no such phy or
 557 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
 558 * not yet loaded. While at that, it also associates the device with
 559 * the phy using devres. On driver detach, release function is invoked
 560 * on the devres data, then, devres data is freed.
 561 *
 562 * For use by USB host and peripheral drivers.
 563 */
 564struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
 565        const char *phandle, u8 index)
 566{
 567        struct device_node *node;
 568        struct usb_phy  *phy;
 569
 570        if (!dev->of_node) {
 571                dev_dbg(dev, "device does not have a device node entry\n");
 572                return ERR_PTR(-EINVAL);
 573        }
 574
 575        node = of_parse_phandle(dev->of_node, phandle, index);
 576        if (!node) {
 577                dev_dbg(dev, "failed to get %s phandle in %pOF node\n", phandle,
 578                        dev->of_node);
 579                return ERR_PTR(-ENODEV);
 580        }
 581        phy = devm_usb_get_phy_by_node(dev, node, NULL);
 582        of_node_put(node);
 583        return phy;
 584}
 585EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_phandle);
 586
 587/**
 588 * usb_get_phy_dev - find the USB PHY
 589 * @dev - device that requests this phy
 590 * @index - the index of the phy
 591 *
 592 * Returns the phy driver, after getting a refcount to it; or
 593 * -ENODEV if there is no such phy.  The caller is responsible for
 594 * calling usb_put_phy() to release that count.
 595 *
 596 * For use by USB host and peripheral drivers.
 597 */
 598struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
 599{
 600        struct usb_phy  *phy = NULL;
 601        unsigned long   flags;
 602
 603        spin_lock_irqsave(&phy_lock, flags);
 604
 605        phy = __usb_find_phy_dev(dev, &phy_bind_list, index);
 606        if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) {
 607                dev_dbg(dev, "unable to find transceiver\n");
 608                if (!IS_ERR(phy))
 609                        phy = ERR_PTR(-ENODEV);
 610
 611                goto err0;
 612        }
 613
 614        get_device(phy->dev);
 615
 616err0:
 617        spin_unlock_irqrestore(&phy_lock, flags);
 618
 619        return phy;
 620}
 621EXPORT_SYMBOL_GPL(usb_get_phy_dev);
 622
 623/**
 624 * devm_usb_get_phy_dev - find the USB PHY using device ptr and index
 625 * @dev - device that requests this phy
 626 * @index - the index of the phy
 627 *
 628 * Gets the phy using usb_get_phy_dev(), and associates a device with it using
 629 * devres. On driver detach, release function is invoked on the devres data,
 630 * then, devres data is freed.
 631 *
 632 * For use by USB host and peripheral drivers.
 633 */
 634struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
 635{
 636        struct usb_phy **ptr, *phy;
 637
 638        ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
 639        if (!ptr)
 640                return NULL;
 641
 642        phy = usb_get_phy_dev(dev, index);
 643        if (!IS_ERR(phy)) {
 644                *ptr = phy;
 645                devres_add(dev, ptr);
 646        } else
 647                devres_free(ptr);
 648
 649        return phy;
 650}
 651EXPORT_SYMBOL_GPL(devm_usb_get_phy_dev);
 652
 653/**
 654 * devm_usb_put_phy - release the USB PHY
 655 * @dev - device that wants to release this phy
 656 * @phy - the phy returned by devm_usb_get_phy()
 657 *
 658 * destroys the devres associated with this phy and invokes usb_put_phy
 659 * to release the phy.
 660 *
 661 * For use by USB host and peripheral drivers.
 662 */
 663void devm_usb_put_phy(struct device *dev, struct usb_phy *phy)
 664{
 665        int r;
 666
 667        r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy);
 668        dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
 669}
 670EXPORT_SYMBOL_GPL(devm_usb_put_phy);
 671
 672/**
 673 * usb_put_phy - release the USB PHY
 674 * @x: the phy returned by usb_get_phy()
 675 *
 676 * Releases a refcount the caller received from usb_get_phy().
 677 *
 678 * For use by USB host and peripheral drivers.
 679 */
 680void usb_put_phy(struct usb_phy *x)
 681{
 682        if (x) {
 683                struct module *owner = x->dev->driver->owner;
 684
 685                put_device(x->dev);
 686                module_put(owner);
 687        }
 688}
 689EXPORT_SYMBOL_GPL(usb_put_phy);
 690
 691/**
 692 * usb_add_phy - declare the USB PHY
 693 * @x: the USB phy to be used; or NULL
 694 * @type - the type of this PHY
 695 *
 696 * This call is exclusively for use by phy drivers, which
 697 * coordinate the activities of drivers for host and peripheral
 698 * controllers, and in some cases for VBUS current regulation.
 699 */
 700int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
 701{
 702        int             ret = 0;
 703        unsigned long   flags;
 704        struct usb_phy  *phy;
 705
 706        if (x->type != USB_PHY_TYPE_UNDEFINED) {
 707                dev_err(x->dev, "not accepting initialized PHY %s\n", x->label);
 708                return -EINVAL;
 709        }
 710
 711        ret = usb_add_extcon(x);
 712        if (ret)
 713                return ret;
 714
 715        ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier);
 716
 717        spin_lock_irqsave(&phy_lock, flags);
 718
 719        list_for_each_entry(phy, &phy_list, head) {
 720                if (phy->type == type) {
 721                        ret = -EBUSY;
 722                        dev_err(x->dev, "transceiver type %s already exists\n",
 723                                                usb_phy_type_string(type));
 724                        goto out;
 725                }
 726        }
 727
 728        x->type = type;
 729        list_add_tail(&x->head, &phy_list);
 730
 731out:
 732        spin_unlock_irqrestore(&phy_lock, flags);
 733        return ret;
 734}
 735EXPORT_SYMBOL_GPL(usb_add_phy);
 736
 737/**
 738 * usb_add_phy_dev - declare the USB PHY
 739 * @x: the USB phy to be used; or NULL
 740 *
 741 * This call is exclusively for use by phy drivers, which
 742 * coordinate the activities of drivers for host and peripheral
 743 * controllers, and in some cases for VBUS current regulation.
 744 */
 745int usb_add_phy_dev(struct usb_phy *x)
 746{
 747        struct usb_phy_bind *phy_bind;
 748        unsigned long flags;
 749        int ret;
 750
 751        if (!x->dev) {
 752                dev_err(x->dev, "no device provided for PHY\n");
 753                return -EINVAL;
 754        }
 755
 756        ret = usb_add_extcon(x);
 757        if (ret)
 758                return ret;
 759
 760        ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier);
 761
 762        spin_lock_irqsave(&phy_lock, flags);
 763        list_for_each_entry(phy_bind, &phy_bind_list, list)
 764                if (!(strcmp(phy_bind->phy_dev_name, dev_name(x->dev))))
 765                        phy_bind->phy = x;
 766
 767        list_add_tail(&x->head, &phy_list);
 768
 769        spin_unlock_irqrestore(&phy_lock, flags);
 770        return 0;
 771}
 772EXPORT_SYMBOL_GPL(usb_add_phy_dev);
 773
 774/**
 775 * usb_remove_phy - remove the OTG PHY
 776 * @x: the USB OTG PHY to be removed;
 777 *
 778 * This reverts the effects of usb_add_phy
 779 */
 780void usb_remove_phy(struct usb_phy *x)
 781{
 782        unsigned long   flags;
 783        struct usb_phy_bind *phy_bind;
 784
 785        spin_lock_irqsave(&phy_lock, flags);
 786        if (x) {
 787                list_for_each_entry(phy_bind, &phy_bind_list, list)
 788                        if (phy_bind->phy == x)
 789                                phy_bind->phy = NULL;
 790                list_del(&x->head);
 791        }
 792        spin_unlock_irqrestore(&phy_lock, flags);
 793}
 794EXPORT_SYMBOL_GPL(usb_remove_phy);
 795
 796/**
 797 * usb_bind_phy - bind the phy and the controller that uses the phy
 798 * @dev_name: the device name of the device that will bind to the phy
 799 * @index: index to specify the port number
 800 * @phy_dev_name: the device name of the phy
 801 *
 802 * Fills the phy_bind structure with the dev_name and phy_dev_name. This will
 803 * be used when the phy driver registers the phy and when the controller
 804 * requests this phy.
 805 *
 806 * To be used by platform specific initialization code.
 807 */
 808int usb_bind_phy(const char *dev_name, u8 index,
 809                                const char *phy_dev_name)
 810{
 811        struct usb_phy_bind *phy_bind;
 812        unsigned long flags;
 813
 814        phy_bind = kzalloc(sizeof(*phy_bind), GFP_KERNEL);
 815        if (!phy_bind)
 816                return -ENOMEM;
 817
 818        phy_bind->dev_name = dev_name;
 819        phy_bind->phy_dev_name = phy_dev_name;
 820        phy_bind->index = index;
 821
 822        spin_lock_irqsave(&phy_lock, flags);
 823        list_add_tail(&phy_bind->list, &phy_bind_list);
 824        spin_unlock_irqrestore(&phy_lock, flags);
 825
 826        return 0;
 827}
 828EXPORT_SYMBOL_GPL(usb_bind_phy);
 829
 830/**
 831 * usb_phy_set_event - set event to phy event
 832 * @x: the phy returned by usb_get_phy();
 833 *
 834 * This sets event to phy event
 835 */
 836void usb_phy_set_event(struct usb_phy *x, unsigned long event)
 837{
 838        x->last_event = event;
 839}
 840EXPORT_SYMBOL_GPL(usb_phy_set_event);
 841