Lines Matching +full:usb +full:- +full:phy

1 // SPDX-License-Identifier: GPL-2.0+
3 * phy.c -- USB phy handling
5 * Copyright (C) 2004-2013 Texas Instruments
15 #include <linux/usb/phy.h>
33 struct usb_phy *phy; member
54 struct usb_phy *phy = NULL; in __usb_find_phy() local
56 list_for_each_entry(phy, list, head) { in __usb_find_phy()
57 if (phy->type != type) in __usb_find_phy()
60 return phy; in __usb_find_phy()
63 return ERR_PTR(-ENODEV); in __usb_find_phy()
68 struct usb_phy *phy; in __of_usb_find_phy() local
71 return ERR_PTR(-ENODEV); in __of_usb_find_phy()
73 list_for_each_entry(phy, &phy_list, head) { in __of_usb_find_phy()
74 if (node != phy->dev->of_node) in __of_usb_find_phy()
77 return phy; in __of_usb_find_phy()
80 return ERR_PTR(-EPROBE_DEFER); in __of_usb_find_phy()
88 if (usb_phy->dev == dev) in __device_to_usb_phy()
97 usb_phy->chg_cur.sdp_min = DEFAULT_SDP_CUR_MIN; in usb_phy_set_default_current()
98 usb_phy->chg_cur.sdp_max = DEFAULT_SDP_CUR_MAX; in usb_phy_set_default_current()
99 usb_phy->chg_cur.dcp_min = DEFAULT_DCP_CUR_MIN; in usb_phy_set_default_current()
100 usb_phy->chg_cur.dcp_max = DEFAULT_DCP_CUR_MAX; in usb_phy_set_default_current()
101 usb_phy->chg_cur.cdp_min = DEFAULT_CDP_CUR_MIN; in usb_phy_set_default_current()
102 usb_phy->chg_cur.cdp_max = DEFAULT_CDP_CUR_MAX; in usb_phy_set_default_current()
103 usb_phy->chg_cur.aca_min = DEFAULT_ACA_CUR_MIN; in usb_phy_set_default_current()
104 usb_phy->chg_cur.aca_max = DEFAULT_ACA_CUR_MAX; in usb_phy_set_default_current()
108 * usb_phy_notify_charger_work - notify the USB charger state
109 * @work: the charger work to notify the USB charger state
111 * This work can be issued when USB charger state has been changed or
112 * USB charger current has been changed, then we can notify the current
119 * If we get the charger type from ->charger_detect() instead of extcon
120 * subsystem, the usb phy driver should issue usb_phy_set_charger_state()
128 switch (usb_phy->chg_state) { in usb_phy_notify_charger_work()
132 atomic_notifier_call_chain(&usb_phy->notifier, max, usb_phy); in usb_phy_notify_charger_work()
137 atomic_notifier_call_chain(&usb_phy->notifier, 0, usb_phy); in usb_phy_notify_charger_work()
140 dev_warn(usb_phy->dev, "Unknown USB charger state: %d\n", in usb_phy_notify_charger_work()
141 usb_phy->chg_state); in usb_phy_notify_charger_work()
145 kobject_uevent(&usb_phy->dev->kobj, KOBJ_CHANGE); in usb_phy_notify_charger_work()
160 return -ENODEV; in usb_phy_uevent()
163 "USB_CHARGER_STATE=%s", usb_chger_state[usb_phy->chg_state]); in usb_phy_uevent()
166 "USB_CHARGER_TYPE=%s", usb_chger_type[usb_phy->chg_type]); in usb_phy_uevent()
169 return -ENOMEM; in usb_phy_uevent()
172 return -ENOMEM; in usb_phy_uevent()
179 if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_SDP) > 0) { in __usb_phy_get_charger_type()
180 usb_phy->chg_type = SDP_TYPE; in __usb_phy_get_charger_type()
181 usb_phy->chg_state = USB_CHARGER_PRESENT; in __usb_phy_get_charger_type()
182 } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_CDP) > 0) { in __usb_phy_get_charger_type()
183 usb_phy->chg_type = CDP_TYPE; in __usb_phy_get_charger_type()
184 usb_phy->chg_state = USB_CHARGER_PRESENT; in __usb_phy_get_charger_type()
185 } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_DCP) > 0) { in __usb_phy_get_charger_type()
186 usb_phy->chg_type = DCP_TYPE; in __usb_phy_get_charger_type()
187 usb_phy->chg_state = USB_CHARGER_PRESENT; in __usb_phy_get_charger_type()
188 } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_ACA) > 0) { in __usb_phy_get_charger_type()
189 usb_phy->chg_type = ACA_TYPE; in __usb_phy_get_charger_type()
190 usb_phy->chg_state = USB_CHARGER_PRESENT; in __usb_phy_get_charger_type()
192 usb_phy->chg_type = UNKNOWN_TYPE; in __usb_phy_get_charger_type()
193 usb_phy->chg_state = USB_CHARGER_ABSENT; in __usb_phy_get_charger_type()
196 schedule_work(&usb_phy->chg_work); in __usb_phy_get_charger_type()
200 * usb_phy_get_charger_type - get charger type from extcon subsystem
218 * usb_phy_set_charger_current - set the USB charger current
219 * @usb_phy: the USB phy to be used
222 * Usually we only change the charger default current when USB finished the
224 * will issue this function to change charger current when after setting USB
225 * configuration, or suspend/resume USB. For other type charger, we should
229 * When USB charger current has been changed, we need to notify the power users.
233 switch (usb_phy->chg_type) { in usb_phy_set_charger_current()
235 if (usb_phy->chg_cur.sdp_max == mA) in usb_phy_set_charger_current()
238 usb_phy->chg_cur.sdp_max = (mA > DEFAULT_SDP_CUR_MAX_SS) ? in usb_phy_set_charger_current()
242 if (usb_phy->chg_cur.dcp_max == mA) in usb_phy_set_charger_current()
245 usb_phy->chg_cur.dcp_max = (mA > DEFAULT_DCP_CUR_MAX) ? in usb_phy_set_charger_current()
249 if (usb_phy->chg_cur.cdp_max == mA) in usb_phy_set_charger_current()
252 usb_phy->chg_cur.cdp_max = (mA > DEFAULT_CDP_CUR_MAX) ? in usb_phy_set_charger_current()
256 if (usb_phy->chg_cur.aca_max == mA) in usb_phy_set_charger_current()
259 usb_phy->chg_cur.aca_max = (mA > DEFAULT_ACA_CUR_MAX) ? in usb_phy_set_charger_current()
266 schedule_work(&usb_phy->chg_work); in usb_phy_set_charger_current()
271 * usb_phy_get_charger_current - get the USB charger current
272 * @usb_phy: the USB phy to be used
283 switch (usb_phy->chg_type) { in usb_phy_get_charger_current()
285 *min = usb_phy->chg_cur.sdp_min; in usb_phy_get_charger_current()
286 *max = usb_phy->chg_cur.sdp_max; in usb_phy_get_charger_current()
289 *min = usb_phy->chg_cur.dcp_min; in usb_phy_get_charger_current()
290 *max = usb_phy->chg_cur.dcp_max; in usb_phy_get_charger_current()
293 *min = usb_phy->chg_cur.cdp_min; in usb_phy_get_charger_current()
294 *max = usb_phy->chg_cur.cdp_max; in usb_phy_get_charger_current()
297 *min = usb_phy->chg_cur.aca_min; in usb_phy_get_charger_current()
298 *max = usb_phy->chg_cur.aca_max; in usb_phy_get_charger_current()
309 * usb_phy_set_charger_state - set the USB charger state
310 * @usb_phy: the USB phy to be used
313 * The usb phy driver can issue this function when the usb phy driver
315 * type should be get from ->charger_detect().
320 if (usb_phy->chg_state == state || !usb_phy->charger_detect) in usb_phy_set_charger_state()
323 usb_phy->chg_state = state; in usb_phy_set_charger_state()
324 if (usb_phy->chg_state == USB_CHARGER_PRESENT) in usb_phy_set_charger_state()
325 usb_phy->chg_type = usb_phy->charger_detect(usb_phy); in usb_phy_set_charger_state()
327 usb_phy->chg_type = UNKNOWN_TYPE; in usb_phy_set_charger_state()
329 schedule_work(&usb_phy->chg_work); in usb_phy_set_charger_state()
335 struct usb_phy *phy = *(struct usb_phy **)res; in devm_usb_phy_release() local
337 usb_put_phy(phy); in devm_usb_phy_release()
344 if (res->nb) in devm_usb_phy_release2()
345 usb_unregister_notifier(res->phy, res->nb); in devm_usb_phy_release2()
346 usb_put_phy(res->phy); in devm_usb_phy_release2()
351 usb_phy->chg_type = UNKNOWN_TYPE; in usb_charger_init()
352 usb_phy->chg_state = USB_CHARGER_DEFAULT; in usb_charger_init()
354 INIT_WORK(&usb_phy->chg_work, usb_phy_notify_charger_work); in usb_charger_init()
361 if (of_property_present(x->dev->of_node, "extcon")) { in usb_add_extcon()
362 x->edev = extcon_get_edev_by_phandle(x->dev, 0); in usb_add_extcon()
363 if (IS_ERR(x->edev)) in usb_add_extcon()
364 return PTR_ERR(x->edev); in usb_add_extcon()
366 x->id_edev = extcon_get_edev_by_phandle(x->dev, 1); in usb_add_extcon()
367 if (IS_ERR(x->id_edev)) { in usb_add_extcon()
368 x->id_edev = NULL; in usb_add_extcon()
369 dev_info(x->dev, "No separate ID extcon device\n"); in usb_add_extcon()
372 if (x->vbus_nb.notifier_call) { in usb_add_extcon()
373 ret = devm_extcon_register_notifier(x->dev, x->edev, in usb_add_extcon()
375 &x->vbus_nb); in usb_add_extcon()
377 dev_err(x->dev, in usb_add_extcon()
382 x->type_nb.notifier_call = usb_phy_get_charger_type; in usb_add_extcon()
384 ret = devm_extcon_register_notifier(x->dev, x->edev, in usb_add_extcon()
386 &x->type_nb); in usb_add_extcon()
388 dev_err(x->dev, in usb_add_extcon()
389 "register extcon USB SDP failed.\n"); in usb_add_extcon()
393 ret = devm_extcon_register_notifier(x->dev, x->edev, in usb_add_extcon()
395 &x->type_nb); in usb_add_extcon()
397 dev_err(x->dev, in usb_add_extcon()
398 "register extcon USB CDP failed.\n"); in usb_add_extcon()
402 ret = devm_extcon_register_notifier(x->dev, x->edev, in usb_add_extcon()
404 &x->type_nb); in usb_add_extcon()
406 dev_err(x->dev, in usb_add_extcon()
407 "register extcon USB DCP failed.\n"); in usb_add_extcon()
411 ret = devm_extcon_register_notifier(x->dev, x->edev, in usb_add_extcon()
413 &x->type_nb); in usb_add_extcon()
415 dev_err(x->dev, in usb_add_extcon()
416 "register extcon USB ACA failed.\n"); in usb_add_extcon()
421 if (x->id_nb.notifier_call) { in usb_add_extcon()
424 if (x->id_edev) in usb_add_extcon()
425 id_ext = x->id_edev; in usb_add_extcon()
427 id_ext = x->edev; in usb_add_extcon()
429 ret = devm_extcon_register_notifier(x->dev, id_ext, in usb_add_extcon()
431 &x->id_nb); in usb_add_extcon()
433 dev_err(x->dev, in usb_add_extcon()
440 if (x->type_nb.notifier_call) in usb_add_extcon()
447 * devm_usb_get_phy - find the USB PHY
448 * @dev: device that requests this phy
449 * @type: the type of the phy the controller requires
451 * Gets the phy using usb_get_phy(), and associates a device with it using
455 * For use by USB host and peripheral drivers.
459 struct usb_phy **ptr, *phy; in devm_usb_get_phy() local
463 return ERR_PTR(-ENOMEM); in devm_usb_get_phy()
465 phy = usb_get_phy(type); in devm_usb_get_phy()
466 if (!IS_ERR(phy)) { in devm_usb_get_phy()
467 *ptr = phy; in devm_usb_get_phy()
472 return phy; in devm_usb_get_phy()
477 * usb_get_phy - find the USB PHY
478 * @type: the type of the phy the controller requires
480 * Returns the phy driver, after getting a refcount to it; or
481 * -ENODEV if there is no such phy. The caller is responsible for
484 * For use by USB host and peripheral drivers.
488 struct usb_phy *phy = NULL; in usb_get_phy() local
493 phy = __usb_find_phy(&phy_list, type); in usb_get_phy()
494 if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) { in usb_get_phy()
495 pr_debug("PHY: unable to find transceiver of type %s\n", in usb_get_phy()
497 if (!IS_ERR(phy)) in usb_get_phy()
498 phy = ERR_PTR(-ENODEV); in usb_get_phy()
503 get_device(phy->dev); in usb_get_phy()
508 return phy; in usb_get_phy()
513 * devm_usb_get_phy_by_node - find the USB PHY by device_node
514 * @dev: device that requests this phy
515 * @node: the device_node for the phy device.
516 * @nb: a notifier_block to register with the phy.
518 * Returns the phy driver associated with the given device_node,
519 * after getting a refcount to it, -ENODEV if there is no such phy or
520 * -EPROBE_DEFER if the device is not yet loaded. While at that, it
522 * the phy using devres. On driver detach, release function is invoked
525 * For use by peripheral drivers for devices related to a phy,
532 struct usb_phy *phy = ERR_PTR(-ENOMEM); in devm_usb_get_phy_by_node() local
544 phy = __of_usb_find_phy(node); in devm_usb_get_phy_by_node()
545 if (IS_ERR(phy)) { in devm_usb_get_phy_by_node()
550 if (!try_module_get(phy->dev->driver->owner)) { in devm_usb_get_phy_by_node()
551 phy = ERR_PTR(-ENODEV); in devm_usb_get_phy_by_node()
556 usb_register_notifier(phy, nb); in devm_usb_get_phy_by_node()
557 ptr->phy = phy; in devm_usb_get_phy_by_node()
558 ptr->nb = nb; in devm_usb_get_phy_by_node()
561 get_device(phy->dev); in devm_usb_get_phy_by_node()
568 return phy; in devm_usb_get_phy_by_node()
573 * devm_usb_get_phy_by_phandle - find the USB PHY by phandle
574 * @dev: device that requests this phy
575 * @phandle: name of the property holding the phy phandle value
576 * @index: the index of the phy
578 * Returns the phy driver associated with the given phandle value,
579 * after getting a refcount to it, -ENODEV if there is no such phy or
580 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
582 * the phy using devres. On driver detach, release function is invoked
585 * For use by USB host and peripheral drivers.
591 struct usb_phy *phy; in devm_usb_get_phy_by_phandle() local
593 if (!dev->of_node) { in devm_usb_get_phy_by_phandle()
595 return ERR_PTR(-EINVAL); in devm_usb_get_phy_by_phandle()
598 node = of_parse_phandle(dev->of_node, phandle, index); in devm_usb_get_phy_by_phandle()
601 dev->of_node); in devm_usb_get_phy_by_phandle()
602 return ERR_PTR(-ENODEV); in devm_usb_get_phy_by_phandle()
604 phy = devm_usb_get_phy_by_node(dev, node, NULL); in devm_usb_get_phy_by_phandle()
606 return phy; in devm_usb_get_phy_by_phandle()
611 * usb_put_phy - release the USB PHY
612 * @x: the phy returned by usb_get_phy()
616 * For use by USB host and peripheral drivers.
621 struct module *owner = x->dev->driver->owner; in usb_put_phy()
623 put_device(x->dev); in usb_put_phy()
630 * usb_add_phy: declare the USB PHY
631 * @x: the USB phy to be used; or NULL
632 * @type: the type of this PHY
634 * This call is exclusively for use by phy drivers, which
642 struct usb_phy *phy; in usb_add_phy() local
644 if (x->type != USB_PHY_TYPE_UNDEFINED) { in usb_add_phy()
645 dev_err(x->dev, "not accepting initialized PHY %s\n", x->label); in usb_add_phy()
646 return -EINVAL; in usb_add_phy()
654 ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier); in usb_add_phy()
658 list_for_each_entry(phy, &phy_list, head) { in usb_add_phy()
659 if (phy->type == type) { in usb_add_phy()
660 ret = -EBUSY; in usb_add_phy()
661 dev_err(x->dev, "transceiver type %s already exists\n", in usb_add_phy()
667 x->type = type; in usb_add_phy()
668 list_add_tail(&x->head, &phy_list); in usb_add_phy()
682 * usb_add_phy_dev - declare the USB PHY
683 * @x: the USB phy to be used; or NULL
685 * This call is exclusively for use by phy drivers, which
694 if (!x->dev) { in usb_add_phy_dev()
695 dev_err(x->dev, "no device provided for PHY\n"); in usb_add_phy_dev()
696 return -EINVAL; in usb_add_phy_dev()
704 x->dev->type = &usb_phy_dev_type; in usb_add_phy_dev()
706 ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier); in usb_add_phy_dev()
709 list_add_tail(&x->head, &phy_list); in usb_add_phy_dev()
717 * usb_remove_phy - remove the OTG PHY
718 * @x: the USB OTG PHY to be removed;
728 list_del(&x->head); in usb_remove_phy()
734 * usb_phy_set_event - set event to phy event
735 * @x: the phy returned by usb_get_phy();
738 * This sets event to phy event
742 x->last_event = event; in usb_phy_set_event()