Merge branch 'regulator-4.20' into regulator-next
[linux-2.6-block.git] / drivers / usb / common / common.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0
e538dfda
MN
2/*
3 * Provides code common for host and device side USB.
4 *
e538dfda
MN
5 * If either host side (ie. CONFIG_USB=y) or device side USB stack
6 * (ie. CONFIG_USB_GADGET=y) is compiled in the kernel, this module is
7 * compiled-in as well. Otherwise, if either of the two stacks is
8 * compiled as module, this file is compiled as module as well.
9 */
10
11#include <linux/kernel.h>
12#include <linux/module.h>
1c9af653 13#include <linux/of.h>
e538dfda 14#include <linux/usb/ch9.h>
1c9af653 15#include <linux/usb/of.h>
7009bdd7 16#include <linux/usb/otg.h>
98bfb394 17#include <linux/of_platform.h>
7009bdd7
FB
18
19const char *usb_otg_state_string(enum usb_otg_state state)
20{
21 static const char *const names[] = {
22 [OTG_STATE_A_IDLE] = "a_idle",
23 [OTG_STATE_A_WAIT_VRISE] = "a_wait_vrise",
24 [OTG_STATE_A_WAIT_BCON] = "a_wait_bcon",
25 [OTG_STATE_A_HOST] = "a_host",
26 [OTG_STATE_A_SUSPEND] = "a_suspend",
27 [OTG_STATE_A_PERIPHERAL] = "a_peripheral",
28 [OTG_STATE_A_WAIT_VFALL] = "a_wait_vfall",
29 [OTG_STATE_A_VBUS_ERR] = "a_vbus_err",
30 [OTG_STATE_B_IDLE] = "b_idle",
31 [OTG_STATE_B_SRP_INIT] = "b_srp_init",
32 [OTG_STATE_B_PERIPHERAL] = "b_peripheral",
33 [OTG_STATE_B_WAIT_ACON] = "b_wait_acon",
34 [OTG_STATE_B_HOST] = "b_host",
35 };
36
37 if (state < 0 || state >= ARRAY_SIZE(names))
38 return "UNDEFINED";
39
40 return names[state];
41}
42EXPORT_SYMBOL_GPL(usb_otg_state_string);
e538dfda 43
1494a1f6
FB
44static const char *const speed_names[] = {
45 [USB_SPEED_UNKNOWN] = "UNKNOWN",
46 [USB_SPEED_LOW] = "low-speed",
47 [USB_SPEED_FULL] = "full-speed",
48 [USB_SPEED_HIGH] = "high-speed",
49 [USB_SPEED_WIRELESS] = "wireless",
50 [USB_SPEED_SUPER] = "super-speed",
8a1b2725 51 [USB_SPEED_SUPER_PLUS] = "super-speed-plus",
1494a1f6
FB
52};
53
e538dfda
MN
54const char *usb_speed_string(enum usb_device_speed speed)
55{
1494a1f6 56 if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
e538dfda 57 speed = USB_SPEED_UNKNOWN;
1494a1f6 58 return speed_names[speed];
e538dfda
MN
59}
60EXPORT_SYMBOL_GPL(usb_speed_string);
61
63863b98
HK
62enum usb_device_speed usb_get_maximum_speed(struct device *dev)
63{
64 const char *maximum_speed;
efc2cd79 65 int ret;
63863b98 66
efc2cd79
HK
67 ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
68 if (ret < 0)
63863b98
HK
69 return USB_SPEED_UNKNOWN;
70
efc2cd79 71 ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed);
63863b98 72
efc2cd79 73 return (ret < 0) ? USB_SPEED_UNKNOWN : ret;
63863b98
HK
74}
75EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
76
d1e3d757
FB
77const char *usb_state_string(enum usb_device_state state)
78{
79 static const char *const names[] = {
80 [USB_STATE_NOTATTACHED] = "not attached",
81 [USB_STATE_ATTACHED] = "attached",
82 [USB_STATE_POWERED] = "powered",
83 [USB_STATE_RECONNECTING] = "reconnecting",
84 [USB_STATE_UNAUTHENTICATED] = "unauthenticated",
85 [USB_STATE_DEFAULT] = "default",
f9076e28 86 [USB_STATE_ADDRESS] = "addressed",
d1e3d757
FB
87 [USB_STATE_CONFIGURED] = "configured",
88 [USB_STATE_SUSPENDED] = "suspended",
89 };
90
91 if (state < 0 || state >= ARRAY_SIZE(names))
92 return "UNKNOWN";
93
94 return names[state];
95}
96EXPORT_SYMBOL_GPL(usb_state_string);
97
1c9af653
MG
98static const char *const usb_dr_modes[] = {
99 [USB_DR_MODE_UNKNOWN] = "",
100 [USB_DR_MODE_HOST] = "host",
101 [USB_DR_MODE_PERIPHERAL] = "peripheral",
102 [USB_DR_MODE_OTG] = "otg",
103};
104
98bfb394
BL
105static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
106{
efc2cd79 107 int ret;
98bfb394 108
efc2cd79
HK
109 ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str);
110 return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret;
98bfb394
BL
111}
112
06e7114f 113enum usb_dr_mode usb_get_dr_mode(struct device *dev)
1c9af653
MG
114{
115 const char *dr_mode;
98bfb394 116 int err;
1c9af653 117
06e7114f 118 err = device_property_read_string(dev, "dr_mode", &dr_mode);
1c9af653
MG
119 if (err < 0)
120 return USB_DR_MODE_UNKNOWN;
121
98bfb394 122 return usb_get_dr_mode_from_string(dr_mode);
1c9af653 123}
06e7114f 124EXPORT_SYMBOL_GPL(usb_get_dr_mode);
1494a1f6 125
06e7114f 126#ifdef CONFIG_OF
98bfb394
BL
127/**
128 * of_usb_get_dr_mode_by_phy - Get dual role mode for the controller device
129 * which is associated with the given phy device_node
130 * @np: Pointer to the given phy device_node
ce15ed4c
HG
131 * @arg0: phandle args[0] for phy's with #phy-cells >= 1, or -1 for
132 * phys which do not have phy-cells
98bfb394
BL
133 *
134 * In dts a usb controller associates with phy devices. The function gets
135 * the string from property 'dr_mode' of the controller associated with the
136 * given phy device node, and returns the correspondig enum usb_dr_mode.
137 */
ce15ed4c 138enum usb_dr_mode of_usb_get_dr_mode_by_phy(struct device_node *np, int arg0)
98bfb394
BL
139{
140 struct device_node *controller = NULL;
ce15ed4c 141 struct of_phandle_args args;
98bfb394
BL
142 const char *dr_mode;
143 int index;
144 int err;
145
146 do {
147 controller = of_find_node_with_property(controller, "phys");
148 index = 0;
149 do {
ce15ed4c
HG
150 if (arg0 == -1) {
151 args.np = of_parse_phandle(controller, "phys",
152 index);
153 args.args_count = 0;
154 } else {
155 err = of_parse_phandle_with_args(controller,
156 "phys", "#phy-cells",
157 index, &args);
158 if (err)
159 break;
160 }
161
162 of_node_put(args.np);
163 if (args.np == np && (args.args_count == 0 ||
164 args.args[0] == arg0))
98bfb394
BL
165 goto finish;
166 index++;
ce15ed4c 167 } while (args.np);
98bfb394
BL
168 } while (controller);
169
170finish:
171 err = of_property_read_string(controller, "dr_mode", &dr_mode);
172 of_node_put(controller);
173
174 if (err < 0)
175 return USB_DR_MODE_UNKNOWN;
176
177 return usb_get_dr_mode_from_string(dr_mode);
178}
179EXPORT_SYMBOL_GPL(of_usb_get_dr_mode_by_phy);
180
05f8b35a
PC
181/**
182 * of_usb_host_tpl_support - to get if Targeted Peripheral List is supported
183 * for given targeted hosts (non-PC hosts)
184 * @np: Pointer to the given device_node
185 *
186 * The function gets if the targeted hosts support TPL or not
187 */
188bool of_usb_host_tpl_support(struct device_node *np)
189{
334007d5 190 return of_property_read_bool(np, "tpl-support");
05f8b35a
PC
191}
192EXPORT_SYMBOL_GPL(of_usb_host_tpl_support);
929412d9
LJ
193
194/**
195 * of_usb_update_otg_caps - to update usb otg capabilities according to
196 * the passed properties in DT.
197 * @np: Pointer to the given device_node
198 * @otg_caps: Pointer to the target usb_otg_caps to be set
199 *
200 * The function updates the otg capabilities
201 */
202int of_usb_update_otg_caps(struct device_node *np,
203 struct usb_otg_caps *otg_caps)
204{
205 u32 otg_rev;
206
207 if (!otg_caps)
208 return -EINVAL;
209
210 if (!of_property_read_u32(np, "otg-rev", &otg_rev)) {
211 switch (otg_rev) {
212 case 0x0100:
213 case 0x0120:
214 case 0x0130:
215 case 0x0200:
216 /* Choose the lesser one if it's already been set */
217 if (otg_caps->otg_rev)
218 otg_caps->otg_rev = min_t(u16, otg_rev,
219 otg_caps->otg_rev);
220 else
221 otg_caps->otg_rev = otg_rev;
222 break;
223 default:
d9241ff2
RH
224 pr_err("%pOF: unsupported otg-rev: 0x%x\n",
225 np, otg_rev);
929412d9
LJ
226 return -EINVAL;
227 }
228 } else {
229 /*
230 * otg-rev is mandatory for otg properties, if not passed
231 * we set it to be 0 and assume it's a legacy otg device.
232 * Non-dt platform can set it afterwards.
233 */
234 otg_caps->otg_rev = 0;
235 }
236
334007d5 237 if (of_property_read_bool(np, "hnp-disable"))
929412d9 238 otg_caps->hnp_support = false;
334007d5 239 if (of_property_read_bool(np, "srp-disable"))
929412d9 240 otg_caps->srp_support = false;
334007d5 241 if (of_property_read_bool(np, "adp-disable") ||
929412d9
LJ
242 (otg_caps->otg_rev < 0x0200))
243 otg_caps->adp_support = false;
244
245 return 0;
246}
247EXPORT_SYMBOL_GPL(of_usb_update_otg_caps);
248
fa827966
YS
249/**
250 * usb_of_get_companion_dev - Find the companion device
251 * @dev: the device pointer to find a companion
252 *
253 * Find the companion device from platform bus.
254 *
255 * Takes a reference to the returned struct device which needs to be dropped
256 * after use.
257 *
258 * Return: On success, a pointer to the companion device, %NULL on failure.
259 */
260struct device *usb_of_get_companion_dev(struct device *dev)
261{
262 struct device_node *node;
263 struct platform_device *pdev = NULL;
264
265 node = of_parse_phandle(dev->of_node, "companion", 0);
266 if (node)
267 pdev = of_find_device_by_node(node);
268
269 of_node_put(node);
270
271 return pdev ? &pdev->dev : NULL;
272}
273EXPORT_SYMBOL_GPL(usb_of_get_companion_dev);
1c9af653
MG
274#endif
275
e538dfda 276MODULE_LICENSE("GPL");