blob: a23065820eadbc918f923fb684c5c1abc870d36c [file] [log] [blame]
Tony Lindgren3cb22d62008-11-24 12:02:21 -08001/*
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 Gortmakerf940fcd2011-05-27 09:56:31 -040013#include <linux/export.h>
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +053014#include <linux/err.h>
Tony Lindgren3cb22d62008-11-24 12:02:21 -080015#include <linux/device.h>
16
17#include <linux/usb/otg.h>
18
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +053019static LIST_HEAD(phy_list);
20static DEFINE_SPINLOCK(phy_lock);
21
22static 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 Lindgren3cb22d62008-11-24 12:02:21 -080036
37/**
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +053038 * usb_get_phy - find the USB PHY
39 * @type - the type of the phy the controller requires
Tony Lindgren3cb22d62008-11-24 12:02:21 -080040 *
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +053041 * 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 Lindgren3cb22d62008-11-24 12:02:21 -080044 *
45 * For use by USB host and peripheral drivers.
46 */
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +053047struct usb_phy *usb_get_phy(enum usb_phy_type type)
Tony Lindgren3cb22d62008-11-24 12:02:21 -080048{
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +053049 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 Krogerus7a8a3a92012-02-13 13:24:04 +020065 return phy;
Tony Lindgren3cb22d62008-11-24 12:02:21 -080066}
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +053067EXPORT_SYMBOL(usb_get_phy);
Tony Lindgren3cb22d62008-11-24 12:02:21 -080068
69/**
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +053070 * usb_put_phy - release the USB PHY
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +053071 * @x: the phy returned by usb_get_phy()
Tony Lindgren3cb22d62008-11-24 12:02:21 -080072 *
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +053073 * Releases a refcount the caller received from usb_get_phy().
Tony Lindgren3cb22d62008-11-24 12:02:21 -080074 *
75 * For use by USB host and peripheral drivers.
76 */
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +053077void usb_put_phy(struct usb_phy *x)
Tony Lindgren3cb22d62008-11-24 12:02:21 -080078{
Robert Jarzmikecf85e42009-04-21 20:33:10 -070079 if (x)
80 put_device(x->dev);
Tony Lindgren3cb22d62008-11-24 12:02:21 -080081}
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +053082EXPORT_SYMBOL(usb_put_phy);
Tony Lindgren3cb22d62008-11-24 12:02:21 -080083
84/**
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +053085 * usb_add_phy - declare the USB PHY
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +053086 * @x: the USB phy to be used; or NULL
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +053087 * @type - the type of this PHY
Tony Lindgren3cb22d62008-11-24 12:02:21 -080088 *
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +053089 * This call is exclusively for use by phy drivers, which
Tony Lindgren3cb22d62008-11-24 12:02:21 -080090 * coordinate the activities of drivers for host and peripheral
91 * controllers, and in some cases for VBUS current regulation.
92 */
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +053093int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
Tony Lindgren3cb22d62008-11-24 12:02:21 -080094{
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +053095 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
118out:
119 spin_unlock_irqrestore(&phy_lock, flags);
120 return ret;
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800121}
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530122EXPORT_SYMBOL(usb_add_phy);
Anatolij Gustschin3dacdf12011-04-15 16:18:38 +0200123
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530124/**
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 */
130void 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}
139EXPORT_SYMBOL(usb_remove_phy);
140
Anatolij Gustschin3dacdf12011-04-15 16:18:38 +0200141const 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}
174EXPORT_SYMBOL(otg_state_string);