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