Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * otg.c -- USB OTG utility code |
| 3 | * |
| 4 | * Copyright (C) 2004 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 | |
| 12 | #include <linux/kernel.h> |
Paul Gortmaker | f940fcd | 2011-05-27 09:56:31 -0400 | [diff] [blame] | 13 | #include <linux/export.h> |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame^] | 14 | #include <linux/err.h> |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 15 | #include <linux/device.h> |
| 16 | |
| 17 | #include <linux/usb/otg.h> |
| 18 | |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame^] | 19 | static LIST_HEAD(phy_list); |
| 20 | static DEFINE_SPINLOCK(phy_lock); |
| 21 | |
| 22 | static struct usb_phy *__usb_find_phy(struct list_head *list, |
| 23 | enum usb_phy_type type) |
| 24 | { |
| 25 | struct usb_phy *phy = NULL; |
| 26 | |
| 27 | list_for_each_entry(phy, list, head) { |
| 28 | if (phy->type != type) |
| 29 | continue; |
| 30 | |
| 31 | return phy; |
| 32 | } |
| 33 | |
| 34 | return ERR_PTR(-ENODEV); |
| 35 | } |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 36 | |
| 37 | /** |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame^] | 38 | * usb_get_phy - find the USB PHY |
| 39 | * @type - the type of the phy the controller requires |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 40 | * |
Kishon Vijay Abraham I | 721002e | 2012-06-22 17:02:45 +0530 | [diff] [blame] | 41 | * Returns the phy driver, after getting a refcount to it; or |
| 42 | * null if there is no such phy. The caller is responsible for |
| 43 | * calling usb_put_phy() to release that count. |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 44 | * |
| 45 | * For use by USB host and peripheral drivers. |
| 46 | */ |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame^] | 47 | struct usb_phy *usb_get_phy(enum usb_phy_type type) |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 48 | { |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame^] | 49 | struct usb_phy *phy = NULL; |
| 50 | unsigned long flags; |
| 51 | |
| 52 | spin_lock_irqsave(&phy_lock, flags); |
| 53 | |
| 54 | phy = __usb_find_phy(&phy_list, type); |
| 55 | if (IS_ERR(phy)) { |
| 56 | pr_err("unable to find transceiver of type %s\n", |
| 57 | usb_phy_type_string(type)); |
| 58 | return phy; |
| 59 | } |
| 60 | |
| 61 | get_device(phy->dev); |
| 62 | |
| 63 | spin_unlock_irqrestore(&phy_lock, flags); |
| 64 | |
Heikki Krogerus | 7a8a3a9 | 2012-02-13 13:24:04 +0200 | [diff] [blame] | 65 | return phy; |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 66 | } |
Kishon Vijay Abraham I | 721002e | 2012-06-22 17:02:45 +0530 | [diff] [blame] | 67 | EXPORT_SYMBOL(usb_get_phy); |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 68 | |
| 69 | /** |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame^] | 70 | * usb_put_phy - release the USB PHY |
Kishon Vijay Abraham I | 721002e | 2012-06-22 17:02:45 +0530 | [diff] [blame] | 71 | * @x: the phy returned by usb_get_phy() |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 72 | * |
Kishon Vijay Abraham I | 721002e | 2012-06-22 17:02:45 +0530 | [diff] [blame] | 73 | * Releases a refcount the caller received from usb_get_phy(). |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 74 | * |
| 75 | * For use by USB host and peripheral drivers. |
| 76 | */ |
Kishon Vijay Abraham I | 721002e | 2012-06-22 17:02:45 +0530 | [diff] [blame] | 77 | void usb_put_phy(struct usb_phy *x) |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 78 | { |
Robert Jarzmik | ecf85e4 | 2009-04-21 20:33:10 -0700 | [diff] [blame] | 79 | if (x) |
| 80 | put_device(x->dev); |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 81 | } |
Kishon Vijay Abraham I | 721002e | 2012-06-22 17:02:45 +0530 | [diff] [blame] | 82 | EXPORT_SYMBOL(usb_put_phy); |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 83 | |
| 84 | /** |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame^] | 85 | * usb_add_phy - declare the USB PHY |
Kishon Vijay Abraham I | 721002e | 2012-06-22 17:02:45 +0530 | [diff] [blame] | 86 | * @x: the USB phy to be used; or NULL |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame^] | 87 | * @type - the type of this PHY |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 88 | * |
Kishon Vijay Abraham I | 721002e | 2012-06-22 17:02:45 +0530 | [diff] [blame] | 89 | * This call is exclusively for use by phy drivers, which |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 90 | * coordinate the activities of drivers for host and peripheral |
| 91 | * controllers, and in some cases for VBUS current regulation. |
| 92 | */ |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame^] | 93 | int usb_add_phy(struct usb_phy *x, enum usb_phy_type type) |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 94 | { |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame^] | 95 | int ret = 0; |
| 96 | unsigned long flags; |
| 97 | struct usb_phy *phy; |
| 98 | |
| 99 | if (x && x->type != USB_PHY_TYPE_UNDEFINED) { |
| 100 | dev_err(x->dev, "not accepting initialized PHY %s\n", x->label); |
| 101 | return -EINVAL; |
| 102 | } |
| 103 | |
| 104 | spin_lock_irqsave(&phy_lock, flags); |
| 105 | |
| 106 | list_for_each_entry(phy, &phy_list, head) { |
| 107 | if (phy->type == type) { |
| 108 | ret = -EBUSY; |
| 109 | dev_err(x->dev, "transceiver type %s already exists\n", |
| 110 | usb_phy_type_string(type)); |
| 111 | goto out; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | x->type = type; |
| 116 | list_add_tail(&x->head, &phy_list); |
| 117 | |
| 118 | out: |
| 119 | spin_unlock_irqrestore(&phy_lock, flags); |
| 120 | return ret; |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 121 | } |
Kishon Vijay Abraham I | 721002e | 2012-06-22 17:02:45 +0530 | [diff] [blame] | 122 | EXPORT_SYMBOL(usb_add_phy); |
Anatolij Gustschin | 3dacdf1 | 2011-04-15 16:18:38 +0200 | [diff] [blame] | 123 | |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame^] | 124 | /** |
| 125 | * usb_remove_phy - remove the OTG PHY |
| 126 | * @x: the USB OTG PHY to be removed; |
| 127 | * |
| 128 | * This reverts the effects of usb_add_phy |
| 129 | */ |
| 130 | void usb_remove_phy(struct usb_phy *x) |
| 131 | { |
| 132 | unsigned long flags; |
| 133 | |
| 134 | spin_lock_irqsave(&phy_lock, flags); |
| 135 | if (x) |
| 136 | list_del(&x->head); |
| 137 | spin_unlock_irqrestore(&phy_lock, flags); |
| 138 | } |
| 139 | EXPORT_SYMBOL(usb_remove_phy); |
| 140 | |
Anatolij Gustschin | 3dacdf1 | 2011-04-15 16:18:38 +0200 | [diff] [blame] | 141 | const char *otg_state_string(enum usb_otg_state state) |
| 142 | { |
| 143 | switch (state) { |
| 144 | case OTG_STATE_A_IDLE: |
| 145 | return "a_idle"; |
| 146 | case OTG_STATE_A_WAIT_VRISE: |
| 147 | return "a_wait_vrise"; |
| 148 | case OTG_STATE_A_WAIT_BCON: |
| 149 | return "a_wait_bcon"; |
| 150 | case OTG_STATE_A_HOST: |
| 151 | return "a_host"; |
| 152 | case OTG_STATE_A_SUSPEND: |
| 153 | return "a_suspend"; |
| 154 | case OTG_STATE_A_PERIPHERAL: |
| 155 | return "a_peripheral"; |
| 156 | case OTG_STATE_A_WAIT_VFALL: |
| 157 | return "a_wait_vfall"; |
| 158 | case OTG_STATE_A_VBUS_ERR: |
| 159 | return "a_vbus_err"; |
| 160 | case OTG_STATE_B_IDLE: |
| 161 | return "b_idle"; |
| 162 | case OTG_STATE_B_SRP_INIT: |
| 163 | return "b_srp_init"; |
| 164 | case OTG_STATE_B_PERIPHERAL: |
| 165 | return "b_peripheral"; |
| 166 | case OTG_STATE_B_WAIT_ACON: |
| 167 | return "b_wait_acon"; |
| 168 | case OTG_STATE_B_HOST: |
| 169 | return "b_host"; |
| 170 | default: |
| 171 | return "UNDEFINED"; |
| 172 | } |
| 173 | } |
| 174 | EXPORT_SYMBOL(otg_state_string); |