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
  21static LIST_HEAD(phy_list);
  22static LIST_HEAD(phy_bind_list);
  23static DEFINE_SPINLOCK(phy_lock);
  24
  25struct phy_devm {
  26        struct usb_phy *phy;
  27        struct notifier_block *nb;
  28};
  29
  30static struct usb_phy *__usb_find_phy(struct list_head *list,
  31        enum usb_phy_type type)
  32{
  33        struct usb_phy  *phy = NULL;
  34
  35        list_for_each_entry(phy, list, head) {
  36                if (phy->type != type)
  37                        continue;
  38
  39                return phy;
  40        }
  41
  42        return ERR_PTR(-ENODEV);
  43}
  44
  45static struct usb_phy *__usb_find_phy_dev(struct device *dev,
  46        struct list_head *list, u8 index)
  47{
  48        struct usb_phy_bind *phy_bind = NULL;
  49
  50        list_for_each_entry(phy_bind, list, list) {
  51                if (!(strcmp(phy_bind->dev_name, dev_name(dev))) &&
  52                                phy_bind->index == index) {
  53                        if (phy_bind->phy)
  54                                return phy_bind->phy;
  55                        else
  56                                return ERR_PTR(-EPROBE_DEFER);
  57                }
  58        }
  59
  60        return ERR_PTR(-ENODEV);
  61}
  62
  63static struct usb_phy *__of_usb_find_phy(struct device_node *node)
  64{
  65        struct usb_phy  *phy;
  66
  67        if (!of_device_is_available(node))
  68                return ERR_PTR(-ENODEV);
  69
  70        list_for_each_entry(phy, &phy_list, head) {
  71                if (node != phy->dev->of_node)
  72                        continue;
  73
  74                return phy;
  75        }
  76
  77        return ERR_PTR(-EPROBE_DEFER);
  78}
  79
  80static void devm_usb_phy_release(struct device *dev, void *res)
  81{
  82        struct usb_phy *phy = *(struct usb_phy **)res;
  83
  84        usb_put_phy(phy);
  85}
  86
  87static void devm_usb_phy_release2(struct device *dev, void *_res)
  88{
  89        struct phy_devm *res = _res;
  90
  91        if (res->nb)
  92                usb_unregister_notifier(res->phy, res->nb);
  93        usb_put_phy(res->phy);
  94}
  95
  96static int devm_usb_phy_match(struct device *dev, void *res, void *match_data)
  97{
  98        struct usb_phy **phy = res;
  99
 100        return *phy == match_data;
 101}
 102
 103static int usb_add_extcon(struct usb_phy *x)
 104{
 105        int ret;
 106
 107        if (of_property_read_bool(x->dev->of_node, "extcon")) {
 108                x->edev = extcon_get_edev_by_phandle(x->dev, 0);
 109                if (IS_ERR(x->edev))
 110                        return PTR_ERR(x->edev);
 111
 112                x->id_edev = extcon_get_edev_by_phandle(x->dev, 1);
 113                if (IS_ERR(x->id_edev)) {
 114                        x->id_edev = NULL;
 115                        dev_info(x->dev, "No separate ID extcon device\n");
 116                }
 117
 118                if (x->vbus_nb.notifier_call) {
 119                        ret = devm_extcon_register_notifier(x->dev, x->edev,
 120                                                            EXTCON_USB,
 121                                                            &x->vbus_nb);
 122                        if (ret < 0) {
 123                                dev_err(x->dev,
 124                                        "register VBUS notifier failed\n");
 125                                return ret;
 126                        }
 127                }
 128
 129                if (x->id_nb.notifier_call) {
 130                        struct extcon_dev *id_ext;
 131
 132                        if (x->id_edev)
 133                                id_ext = x->id_edev;
 134                        else
 135                                id_ext = x->edev;
 136
 137                        ret = devm_extcon_register_notifier(x->dev, id_ext,
 138                                                            EXTCON_USB_HOST,
 139                                                            &x->id_nb);
 140                        if (ret < 0) {
 141                                dev_err(x->dev,
 142                                        "register ID notifier failed\n");
 143                                return ret;
 144                        }
 145                }
 146        }
 147
 148        return 0;
 149}
 150
 151/**
 152 * devm_usb_get_phy - find the USB PHY
 153 * @dev - device that requests this phy
 154 * @type - the type of the phy the controller requires
 155 *
 156 * Gets the phy using usb_get_phy(), and associates a device with it using
 157 * devres. On driver detach, release function is invoked on the devres data,
 158 * then, devres data is freed.
 159 *
 160 * For use by USB host and peripheral drivers.
 161 */
 162struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type)
 163{
 164        struct usb_phy **ptr, *phy;
 165
 166        ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
 167        if (!ptr)
 168                return ERR_PTR(-ENOMEM);
 169
 170        phy = usb_get_phy(type);
 171        if (!IS_ERR(phy)) {
 172                *ptr = phy;
 173                devres_add(dev, ptr);
 174        } else
 175                devres_free(ptr);
 176
 177        return phy;
 178}
 179EXPORT_SYMBOL_GPL(devm_usb_get_phy);
 180
 181/**
 182 * usb_get_phy - find the USB PHY
 183 * @type - the type of the phy the controller requires
 184 *
 185 * Returns the phy driver, after getting a refcount to it; or
 186 * -ENODEV if there is no such phy.  The caller is responsible for
 187 * calling usb_put_phy() to release that count.
 188 *
 189 * For use by USB host and peripheral drivers.
 190 */
 191struct usb_phy *usb_get_phy(enum usb_phy_type type)
 192{
 193        struct usb_phy  *phy = NULL;
 194        unsigned long   flags;
 195
 196        spin_lock_irqsave(&phy_lock, flags);
 197
 198        phy = __usb_find_phy(&phy_list, type);
 199        if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) {
 200                pr_debug("PHY: unable to find transceiver of type %s\n",
 201                        usb_phy_type_string(type));
 202                if (!IS_ERR(phy))
 203                        phy = ERR_PTR(-ENODEV);
 204
 205                goto err0;
 206        }
 207
 208        get_device(phy->dev);
 209
 210err0:
 211        spin_unlock_irqrestore(&phy_lock, flags);
 212
 213        return phy;
 214}
 215EXPORT_SYMBOL_GPL(usb_get_phy);
 216
 217/**
 218 * devm_usb_get_phy_by_node - find the USB PHY by device_node
 219 * @dev - device that requests this phy
 220 * @node - the device_node for the phy device.
 221 * @nb - a notifier_block to register with the phy.
 222 *
 223 * Returns the phy driver associated with the given device_node,
 224 * after getting a refcount to it, -ENODEV if there is no such phy or
 225 * -EPROBE_DEFER if the device is not yet loaded. While at that, it
 226 * also associates the device with
 227 * the phy using devres. On driver detach, release function is invoked
 228 * on the devres data, then, devres data is freed.
 229 *
 230 * For use by peripheral drivers for devices related to a phy,
 231 * such as a charger.
 232 */
 233struct  usb_phy *devm_usb_get_phy_by_node(struct device *dev,
 234                                          struct device_node *node,
 235                                          struct notifier_block *nb)
 236{
 237        struct usb_phy  *phy = ERR_PTR(-ENOMEM);
 238        struct phy_devm *ptr;
 239        unsigned long   flags;
 240
 241        ptr = devres_alloc(devm_usb_phy_release2, sizeof(*ptr), GFP_KERNEL);
 242        if (!ptr) {
 243                dev_dbg(dev, "failed to allocate memory for devres\n");
 244                goto err0;
 245        }
 246
 247        spin_lock_irqsave(&phy_lock, flags);
 248
 249        phy = __of_usb_find_phy(node);
 250        if (IS_ERR(phy)) {
 251                devres_free(ptr);
 252                goto err1;
 253        }
 254
 255        if (!try_module_get(phy->dev->driver->owner)) {
 256                phy = ERR_PTR(-ENODEV);
 257                devres_free(ptr);
 258                goto err1;
 259        }
 260        if (nb)
 261                usb_register_notifier(phy, nb);
 262        ptr->phy = phy;
 263        ptr->nb = nb;
 264        devres_add(dev, ptr);
 265
 266        get_device(phy->dev);
 267
 268err1:
 269        spin_unlock_irqrestore(&phy_lock, flags);
 270
 271err0:
 272
 273        return phy;
 274}
 275EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_node);
 276
 277/**
 278 * devm_usb_get_phy_by_phandle - find the USB PHY by phandle
 279 * @dev - device that requests this phy
 280 * @phandle - name of the property holding the phy phandle value
 281 * @index - the index of the phy
 282 *
 283 * Returns the phy driver associated with the given phandle value,
 284 * after getting a refcount to it, -ENODEV if there is no such phy or
 285 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
 286 * not yet loaded. While at that, it also associates the device with
 287 * the phy using devres. On driver detach, release function is invoked
 288 * on the devres data, then, devres data is freed.
 289 *
 290 * For use by USB host and peripheral drivers.
 291 */
 292struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
 293        const char *phandle, u8 index)
 294{
 295        struct device_node *node;
 296        struct usb_phy  *phy;
 297
 298        if (!dev->of_node) {
 299                dev_dbg(dev, "device does not have a device node entry\n");
 300                return ERR_PTR(-EINVAL);
 301        }
 302
 303        node = of_parse_phandle(dev->of_node, phandle, index);
 304        if (!node) {
 305                dev_dbg(dev, "failed to get %s phandle in %s node\n", phandle,
 306                        dev->of_node->full_name);
 307                return ERR_PTR(-ENODEV);
 308        }
 309        phy = devm_usb_get_phy_by_node(dev, node, NULL);
 310        of_node_put(node);
 311        return phy;
 312}
 313EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_phandle);
 314
 315/**
 316 * usb_get_phy_dev - find the USB PHY
 317 * @dev - device that requests this phy
 318 * @index - the index of the phy
 319 *
 320 * Returns the phy driver, after getting a refcount to it; or
 321 * -ENODEV if there is no such phy.  The caller is responsible for
 322 * calling usb_put_phy() to release that count.
 323 *
 324 * For use by USB host and peripheral drivers.
 325 */
 326struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
 327{
 328        struct usb_phy  *phy = NULL;
 329        unsigned long   flags;
 330
 331        spin_lock_irqsave(&phy_lock, flags);
 332
 333        phy = __usb_find_phy_dev(dev, &phy_bind_list, index);
 334        if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) {
 335                dev_dbg(dev, "unable to find transceiver\n");
 336                if (!IS_ERR(phy))
 337                        phy = ERR_PTR(-ENODEV);
 338
 339                goto err0;
 340        }
 341
 342        get_device(phy->dev);
 343
 344err0:
 345        spin_unlock_irqrestore(&phy_lock, flags);
 346
 347        return phy;
 348}
 349EXPORT_SYMBOL_GPL(usb_get_phy_dev);
 350
 351/**
 352 * devm_usb_get_phy_dev - find the USB PHY using device ptr and index
 353 * @dev - device that requests this phy
 354 * @index - the index of the phy
 355 *
 356 * Gets the phy using usb_get_phy_dev(), and associates a device with it using
 357 * devres. On driver detach, release function is invoked on the devres data,
 358 * then, devres data is freed.
 359 *
 360 * For use by USB host and peripheral drivers.
 361 */
 362struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
 363{
 364        struct usb_phy **ptr, *phy;
 365
 366        ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
 367        if (!ptr)
 368                return NULL;
 369
 370        phy = usb_get_phy_dev(dev, index);
 371        if (!IS_ERR(phy)) {
 372                *ptr = phy;
 373                devres_add(dev, ptr);
 374        } else
 375                devres_free(ptr);
 376
 377        return phy;
 378}
 379EXPORT_SYMBOL_GPL(devm_usb_get_phy_dev);
 380
 381/**
 382 * devm_usb_put_phy - release the USB PHY
 383 * @dev - device that wants to release this phy
 384 * @phy - the phy returned by devm_usb_get_phy()
 385 *
 386 * destroys the devres associated with this phy and invokes usb_put_phy
 387 * to release the phy.
 388 *
 389 * For use by USB host and peripheral drivers.
 390 */
 391void devm_usb_put_phy(struct device *dev, struct usb_phy *phy)
 392{
 393        int r;
 394
 395        r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy);
 396        dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
 397}
 398EXPORT_SYMBOL_GPL(devm_usb_put_phy);
 399
 400/**
 401 * usb_put_phy - release the USB PHY
 402 * @x: the phy returned by usb_get_phy()
 403 *
 404 * Releases a refcount the caller received from usb_get_phy().
 405 *
 406 * For use by USB host and peripheral drivers.
 407 */
 408void usb_put_phy(struct usb_phy *x)
 409{
 410        if (x) {
 411                struct module *owner = x->dev->driver->owner;
 412
 413                put_device(x->dev);
 414                module_put(owner);
 415        }
 416}
 417EXPORT_SYMBOL_GPL(usb_put_phy);
 418
 419/**
 420 * usb_add_phy - declare the USB PHY
 421 * @x: the USB phy to be used; or NULL
 422 * @type - the type of this PHY
 423 *
 424 * This call is exclusively for use by phy drivers, which
 425 * coordinate the activities of drivers for host and peripheral
 426 * controllers, and in some cases for VBUS current regulation.
 427 */
 428int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
 429{
 430        int             ret = 0;
 431        unsigned long   flags;
 432        struct usb_phy  *phy;
 433
 434        if (x->type != USB_PHY_TYPE_UNDEFINED) {
 435                dev_err(x->dev, "not accepting initialized PHY %s\n", x->label);
 436                return -EINVAL;
 437        }
 438
 439        ret = usb_add_extcon(x);
 440        if (ret)
 441                return ret;
 442
 443        ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier);
 444
 445        spin_lock_irqsave(&phy_lock, flags);
 446
 447        list_for_each_entry(phy, &phy_list, head) {
 448                if (phy->type == type) {
 449                        ret = -EBUSY;
 450                        dev_err(x->dev, "transceiver type %s already exists\n",
 451                                                usb_phy_type_string(type));
 452                        goto out;
 453                }
 454        }
 455
 456        x->type = type;
 457        list_add_tail(&x->head, &phy_list);
 458
 459out:
 460        spin_unlock_irqrestore(&phy_lock, flags);
 461        return ret;
 462}
 463EXPORT_SYMBOL_GPL(usb_add_phy);
 464
 465/**
 466 * usb_add_phy_dev - declare the USB PHY
 467 * @x: the USB phy to be used; or NULL
 468 *
 469 * This call is exclusively for use by phy drivers, which
 470 * coordinate the activities of drivers for host and peripheral
 471 * controllers, and in some cases for VBUS current regulation.
 472 */
 473int usb_add_phy_dev(struct usb_phy *x)
 474{
 475        struct usb_phy_bind *phy_bind;
 476        unsigned long flags;
 477        int ret;
 478
 479        if (!x->dev) {
 480                dev_err(x->dev, "no device provided for PHY\n");
 481                return -EINVAL;
 482        }
 483
 484        ret = usb_add_extcon(x);
 485        if (ret)
 486                return ret;
 487
 488        ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier);
 489
 490        spin_lock_irqsave(&phy_lock, flags);
 491        list_for_each_entry(phy_bind, &phy_bind_list, list)
 492                if (!(strcmp(phy_bind->phy_dev_name, dev_name(x->dev))))
 493                        phy_bind->phy = x;
 494
 495        list_add_tail(&x->head, &phy_list);
 496
 497        spin_unlock_irqrestore(&phy_lock, flags);
 498        return 0;
 499}
 500EXPORT_SYMBOL_GPL(usb_add_phy_dev);
 501
 502/**
 503 * usb_remove_phy - remove the OTG PHY
 504 * @x: the USB OTG PHY to be removed;
 505 *
 506 * This reverts the effects of usb_add_phy
 507 */
 508void usb_remove_phy(struct usb_phy *x)
 509{
 510        unsigned long   flags;
 511        struct usb_phy_bind *phy_bind;
 512
 513        spin_lock_irqsave(&phy_lock, flags);
 514        if (x) {
 515                list_for_each_entry(phy_bind, &phy_bind_list, list)
 516                        if (phy_bind->phy == x)
 517                                phy_bind->phy = NULL;
 518                list_del(&x->head);
 519        }
 520        spin_unlock_irqrestore(&phy_lock, flags);
 521}
 522EXPORT_SYMBOL_GPL(usb_remove_phy);
 523
 524/**
 525 * usb_bind_phy - bind the phy and the controller that uses the phy
 526 * @dev_name: the device name of the device that will bind to the phy
 527 * @index: index to specify the port number
 528 * @phy_dev_name: the device name of the phy
 529 *
 530 * Fills the phy_bind structure with the dev_name and phy_dev_name. This will
 531 * be used when the phy driver registers the phy and when the controller
 532 * requests this phy.
 533 *
 534 * To be used by platform specific initialization code.
 535 */
 536int usb_bind_phy(const char *dev_name, u8 index,
 537                                const char *phy_dev_name)
 538{
 539        struct usb_phy_bind *phy_bind;
 540        unsigned long flags;
 541
 542        phy_bind = kzalloc(sizeof(*phy_bind), GFP_KERNEL);
 543        if (!phy_bind)
 544                return -ENOMEM;
 545
 546        phy_bind->dev_name = dev_name;
 547        phy_bind->phy_dev_name = phy_dev_name;
 548        phy_bind->index = index;
 549
 550        spin_lock_irqsave(&phy_lock, flags);
 551        list_add_tail(&phy_bind->list, &phy_bind_list);
 552        spin_unlock_irqrestore(&phy_lock, flags);
 553
 554        return 0;
 555}
 556EXPORT_SYMBOL_GPL(usb_bind_phy);
 557
 558/**
 559 * usb_phy_set_event - set event to phy event
 560 * @x: the phy returned by usb_get_phy();
 561 *
 562 * This sets event to phy event
 563 */
 564void usb_phy_set_event(struct usb_phy *x, unsigned long event)
 565{
 566        x->last_event = event;
 567}
 568EXPORT_SYMBOL_GPL(usb_phy_set_event);
 569