Lines Matching +full:phy +full:- +full:device

1 // SPDX-License-Identifier: GPL-2.0+
3 * phy.c -- USB phy handling
5 * Copyright (C) 2004-2013 Texas Instruments
10 #include <linux/device.h>
15 #include <linux/usb/phy.h>
33 struct usb_phy *phy; member
48 struct usb_phy *phy = NULL; in __usb_find_phy() local
50 list_for_each_entry(phy, list, head) { in __usb_find_phy()
51 if (phy->type != type) in __usb_find_phy()
54 return phy; in __usb_find_phy()
57 return ERR_PTR(-ENODEV); in __usb_find_phy()
62 struct usb_phy *phy; in __of_usb_find_phy() local
65 return ERR_PTR(-ENODEV); in __of_usb_find_phy()
67 list_for_each_entry(phy, &phy_list, head) { in __of_usb_find_phy()
68 if (node != phy->dev->of_node) in __of_usb_find_phy()
71 return phy; in __of_usb_find_phy()
74 return ERR_PTR(-EPROBE_DEFER); in __of_usb_find_phy()
79 usb_phy->chg_cur.sdp_min = DEFAULT_SDP_CUR_MIN; in usb_phy_set_default_current()
80 usb_phy->chg_cur.sdp_max = DEFAULT_SDP_CUR_MAX; in usb_phy_set_default_current()
81 usb_phy->chg_cur.dcp_min = DEFAULT_DCP_CUR_MIN; in usb_phy_set_default_current()
82 usb_phy->chg_cur.dcp_max = DEFAULT_DCP_CUR_MAX; in usb_phy_set_default_current()
83 usb_phy->chg_cur.cdp_min = DEFAULT_CDP_CUR_MIN; in usb_phy_set_default_current()
84 usb_phy->chg_cur.cdp_max = DEFAULT_CDP_CUR_MAX; in usb_phy_set_default_current()
85 usb_phy->chg_cur.aca_min = DEFAULT_ACA_CUR_MIN; in usb_phy_set_default_current()
86 usb_phy->chg_cur.aca_max = DEFAULT_ACA_CUR_MAX; in usb_phy_set_default_current()
90 * usb_phy_notify_charger_work - notify the USB charger state
101 * If we get the charger type from ->charger_detect() instead of extcon
102 * subsystem, the usb phy driver should issue usb_phy_set_charger_state()
113 switch (usb_phy->chg_state) { in usb_phy_notify_charger_work()
117 atomic_notifier_call_chain(&usb_phy->notifier, max, usb_phy); in usb_phy_notify_charger_work()
124 atomic_notifier_call_chain(&usb_phy->notifier, 0, usb_phy); in usb_phy_notify_charger_work()
129 dev_warn(usb_phy->dev, "Unknown USB charger state: %d\n", in usb_phy_notify_charger_work()
130 usb_phy->chg_state); in usb_phy_notify_charger_work()
135 "USB_CHARGER_TYPE=%s", usb_chger_type[usb_phy->chg_type]); in usb_phy_notify_charger_work()
136 kobject_uevent_env(&usb_phy->dev->kobj, KOBJ_CHANGE, envp); in usb_phy_notify_charger_work()
141 if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_SDP) > 0) { in __usb_phy_get_charger_type()
142 usb_phy->chg_type = SDP_TYPE; in __usb_phy_get_charger_type()
143 usb_phy->chg_state = USB_CHARGER_PRESENT; in __usb_phy_get_charger_type()
144 } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_CDP) > 0) { in __usb_phy_get_charger_type()
145 usb_phy->chg_type = CDP_TYPE; in __usb_phy_get_charger_type()
146 usb_phy->chg_state = USB_CHARGER_PRESENT; in __usb_phy_get_charger_type()
147 } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_DCP) > 0) { in __usb_phy_get_charger_type()
148 usb_phy->chg_type = DCP_TYPE; in __usb_phy_get_charger_type()
149 usb_phy->chg_state = USB_CHARGER_PRESENT; in __usb_phy_get_charger_type()
150 } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_ACA) > 0) { in __usb_phy_get_charger_type()
151 usb_phy->chg_type = ACA_TYPE; in __usb_phy_get_charger_type()
152 usb_phy->chg_state = USB_CHARGER_PRESENT; in __usb_phy_get_charger_type()
154 usb_phy->chg_type = UNKNOWN_TYPE; in __usb_phy_get_charger_type()
155 usb_phy->chg_state = USB_CHARGER_ABSENT; in __usb_phy_get_charger_type()
158 schedule_work(&usb_phy->chg_work); in __usb_phy_get_charger_type()
162 * usb_phy_get_charger_type - get charger type from extcon subsystem
180 * usb_phy_set_charger_current - set the USB charger current
181 * @usb_phy: the USB phy to be used
195 switch (usb_phy->chg_type) { in usb_phy_set_charger_current()
197 if (usb_phy->chg_cur.sdp_max == mA) in usb_phy_set_charger_current()
200 usb_phy->chg_cur.sdp_max = (mA > DEFAULT_SDP_CUR_MAX_SS) ? in usb_phy_set_charger_current()
204 if (usb_phy->chg_cur.dcp_max == mA) in usb_phy_set_charger_current()
207 usb_phy->chg_cur.dcp_max = (mA > DEFAULT_DCP_CUR_MAX) ? in usb_phy_set_charger_current()
211 if (usb_phy->chg_cur.cdp_max == mA) in usb_phy_set_charger_current()
214 usb_phy->chg_cur.cdp_max = (mA > DEFAULT_CDP_CUR_MAX) ? in usb_phy_set_charger_current()
218 if (usb_phy->chg_cur.aca_max == mA) in usb_phy_set_charger_current()
221 usb_phy->chg_cur.aca_max = (mA > DEFAULT_ACA_CUR_MAX) ? in usb_phy_set_charger_current()
228 schedule_work(&usb_phy->chg_work); in usb_phy_set_charger_current()
233 * usb_phy_get_charger_current - get the USB charger current
234 * @usb_phy: the USB phy to be used
245 switch (usb_phy->chg_type) { in usb_phy_get_charger_current()
247 *min = usb_phy->chg_cur.sdp_min; in usb_phy_get_charger_current()
248 *max = usb_phy->chg_cur.sdp_max; in usb_phy_get_charger_current()
251 *min = usb_phy->chg_cur.dcp_min; in usb_phy_get_charger_current()
252 *max = usb_phy->chg_cur.dcp_max; in usb_phy_get_charger_current()
255 *min = usb_phy->chg_cur.cdp_min; in usb_phy_get_charger_current()
256 *max = usb_phy->chg_cur.cdp_max; in usb_phy_get_charger_current()
259 *min = usb_phy->chg_cur.aca_min; in usb_phy_get_charger_current()
260 *max = usb_phy->chg_cur.aca_max; in usb_phy_get_charger_current()
271 * usb_phy_set_charger_state - set the USB charger state
272 * @usb_phy: the USB phy to be used
275 * The usb phy driver can issue this function when the usb phy driver
277 * type should be get from ->charger_detect().
282 if (usb_phy->chg_state == state || !usb_phy->charger_detect) in usb_phy_set_charger_state()
285 usb_phy->chg_state = state; in usb_phy_set_charger_state()
286 if (usb_phy->chg_state == USB_CHARGER_PRESENT) in usb_phy_set_charger_state()
287 usb_phy->chg_type = usb_phy->charger_detect(usb_phy); in usb_phy_set_charger_state()
289 usb_phy->chg_type = UNKNOWN_TYPE; in usb_phy_set_charger_state()
291 schedule_work(&usb_phy->chg_work); in usb_phy_set_charger_state()
295 static void devm_usb_phy_release(struct device *dev, void *res) in devm_usb_phy_release()
297 struct usb_phy *phy = *(struct usb_phy **)res; in devm_usb_phy_release() local
299 usb_put_phy(phy); in devm_usb_phy_release()
302 static void devm_usb_phy_release2(struct device *dev, void *_res) in devm_usb_phy_release2()
306 if (res->nb) in devm_usb_phy_release2()
307 usb_unregister_notifier(res->phy, res->nb); in devm_usb_phy_release2()
308 usb_put_phy(res->phy); in devm_usb_phy_release2()
311 static int devm_usb_phy_match(struct device *dev, void *res, void *match_data) in devm_usb_phy_match()
313 struct usb_phy **phy = res; in devm_usb_phy_match() local
315 return *phy == match_data; in devm_usb_phy_match()
320 usb_phy->chg_type = UNKNOWN_TYPE; in usb_charger_init()
321 usb_phy->chg_state = USB_CHARGER_DEFAULT; in usb_charger_init()
323 INIT_WORK(&usb_phy->chg_work, usb_phy_notify_charger_work); in usb_charger_init()
330 if (of_property_read_bool(x->dev->of_node, "extcon")) { in usb_add_extcon()
331 x->edev = extcon_get_edev_by_phandle(x->dev, 0); in usb_add_extcon()
332 if (IS_ERR(x->edev)) in usb_add_extcon()
333 return PTR_ERR(x->edev); in usb_add_extcon()
335 x->id_edev = extcon_get_edev_by_phandle(x->dev, 1); in usb_add_extcon()
336 if (IS_ERR(x->id_edev)) { in usb_add_extcon()
337 x->id_edev = NULL; in usb_add_extcon()
338 dev_info(x->dev, "No separate ID extcon device\n"); in usb_add_extcon()
341 if (x->vbus_nb.notifier_call) { in usb_add_extcon()
342 ret = devm_extcon_register_notifier(x->dev, x->edev, in usb_add_extcon()
344 &x->vbus_nb); in usb_add_extcon()
346 dev_err(x->dev, in usb_add_extcon()
351 x->type_nb.notifier_call = usb_phy_get_charger_type; in usb_add_extcon()
353 ret = devm_extcon_register_notifier(x->dev, x->edev, in usb_add_extcon()
355 &x->type_nb); in usb_add_extcon()
357 dev_err(x->dev, in usb_add_extcon()
362 ret = devm_extcon_register_notifier(x->dev, x->edev, in usb_add_extcon()
364 &x->type_nb); in usb_add_extcon()
366 dev_err(x->dev, in usb_add_extcon()
371 ret = devm_extcon_register_notifier(x->dev, x->edev, in usb_add_extcon()
373 &x->type_nb); in usb_add_extcon()
375 dev_err(x->dev, in usb_add_extcon()
380 ret = devm_extcon_register_notifier(x->dev, x->edev, in usb_add_extcon()
382 &x->type_nb); in usb_add_extcon()
384 dev_err(x->dev, in usb_add_extcon()
390 if (x->id_nb.notifier_call) { in usb_add_extcon()
393 if (x->id_edev) in usb_add_extcon()
394 id_ext = x->id_edev; in usb_add_extcon()
396 id_ext = x->edev; in usb_add_extcon()
398 ret = devm_extcon_register_notifier(x->dev, id_ext, in usb_add_extcon()
400 &x->id_nb); in usb_add_extcon()
402 dev_err(x->dev, in usb_add_extcon()
409 if (x->type_nb.notifier_call) in usb_add_extcon()
416 * devm_usb_get_phy - find the USB PHY
417 * @dev: device that requests this phy
418 * @type: the type of the phy the controller requires
420 * Gets the phy using usb_get_phy(), and associates a device with it using
426 struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type) in devm_usb_get_phy()
428 struct usb_phy **ptr, *phy; in devm_usb_get_phy() local
432 return ERR_PTR(-ENOMEM); in devm_usb_get_phy()
434 phy = usb_get_phy(type); in devm_usb_get_phy()
435 if (!IS_ERR(phy)) { in devm_usb_get_phy()
436 *ptr = phy; in devm_usb_get_phy()
441 return phy; in devm_usb_get_phy()
446 * usb_get_phy - find the USB PHY
447 * @type: the type of the phy the controller requires
449 * Returns the phy driver, after getting a refcount to it; or
450 * -ENODEV if there is no such phy. The caller is responsible for
457 struct usb_phy *phy = NULL; in usb_get_phy() local
462 phy = __usb_find_phy(&phy_list, type); in usb_get_phy()
463 if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) { in usb_get_phy()
464 pr_debug("PHY: unable to find transceiver of type %s\n", in usb_get_phy()
466 if (!IS_ERR(phy)) in usb_get_phy()
467 phy = ERR_PTR(-ENODEV); in usb_get_phy()
472 get_device(phy->dev); in usb_get_phy()
477 return phy; in usb_get_phy()
482 * devm_usb_get_phy_by_node - find the USB PHY by device_node
483 * @dev: device that requests this phy
484 * @node: the device_node for the phy device.
485 * @nb: a notifier_block to register with the phy.
487 * Returns the phy driver associated with the given device_node,
488 * after getting a refcount to it, -ENODEV if there is no such phy or
489 * -EPROBE_DEFER if the device is not yet loaded. While at that, it
490 * also associates the device with
491 * the phy using devres. On driver detach, release function is invoked
494 * For use by peripheral drivers for devices related to a phy,
497 struct usb_phy *devm_usb_get_phy_by_node(struct device *dev, in devm_usb_get_phy_by_node()
501 struct usb_phy *phy = ERR_PTR(-ENOMEM); in devm_usb_get_phy_by_node() local
513 phy = __of_usb_find_phy(node); in devm_usb_get_phy_by_node()
514 if (IS_ERR(phy)) { in devm_usb_get_phy_by_node()
519 if (!try_module_get(phy->dev->driver->owner)) { in devm_usb_get_phy_by_node()
520 phy = ERR_PTR(-ENODEV); in devm_usb_get_phy_by_node()
525 usb_register_notifier(phy, nb); in devm_usb_get_phy_by_node()
526 ptr->phy = phy; in devm_usb_get_phy_by_node()
527 ptr->nb = nb; in devm_usb_get_phy_by_node()
530 get_device(phy->dev); in devm_usb_get_phy_by_node()
537 return phy; in devm_usb_get_phy_by_node()
542 * devm_usb_get_phy_by_phandle - find the USB PHY by phandle
543 * @dev: device that requests this phy
544 * @phandle: name of the property holding the phy phandle value
545 * @index: the index of the phy
547 * Returns the phy driver associated with the given phandle value,
548 * after getting a refcount to it, -ENODEV if there is no such phy or
549 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
550 * not yet loaded. While at that, it also associates the device with
551 * the phy using devres. On driver detach, release function is invoked
556 struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev, in devm_usb_get_phy_by_phandle()
560 struct usb_phy *phy; in devm_usb_get_phy_by_phandle() local
562 if (!dev->of_node) { in devm_usb_get_phy_by_phandle()
563 dev_dbg(dev, "device does not have a device node entry\n"); in devm_usb_get_phy_by_phandle()
564 return ERR_PTR(-EINVAL); in devm_usb_get_phy_by_phandle()
567 node = of_parse_phandle(dev->of_node, phandle, index); in devm_usb_get_phy_by_phandle()
570 dev->of_node); in devm_usb_get_phy_by_phandle()
571 return ERR_PTR(-ENODEV); in devm_usb_get_phy_by_phandle()
573 phy = devm_usb_get_phy_by_node(dev, node, NULL); in devm_usb_get_phy_by_phandle()
575 return phy; in devm_usb_get_phy_by_phandle()
580 * devm_usb_put_phy - release the USB PHY
581 * @dev: device that wants to release this phy
582 * @phy: the phy returned by devm_usb_get_phy()
584 * destroys the devres associated with this phy and invokes usb_put_phy
585 * to release the phy.
589 void devm_usb_put_phy(struct device *dev, struct usb_phy *phy) in devm_usb_put_phy() argument
593 r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy); in devm_usb_put_phy()
594 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n"); in devm_usb_put_phy()
599 * usb_put_phy - release the USB PHY
600 * @x: the phy returned by usb_get_phy()
609 struct module *owner = x->dev->driver->owner; in usb_put_phy()
611 put_device(x->dev); in usb_put_phy()
618 * usb_add_phy: declare the USB PHY
619 * @x: the USB phy to be used; or NULL
620 * @type: the type of this PHY
622 * This call is exclusively for use by phy drivers, which
630 struct usb_phy *phy; in usb_add_phy() local
632 if (x->type != USB_PHY_TYPE_UNDEFINED) { in usb_add_phy()
633 dev_err(x->dev, "not accepting initialized PHY %s\n", x->label); in usb_add_phy()
634 return -EINVAL; in usb_add_phy()
642 ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier); in usb_add_phy()
646 list_for_each_entry(phy, &phy_list, head) { in usb_add_phy()
647 if (phy->type == type) { in usb_add_phy()
648 ret = -EBUSY; in usb_add_phy()
649 dev_err(x->dev, "transceiver type %s already exists\n", in usb_add_phy()
655 x->type = type; in usb_add_phy()
656 list_add_tail(&x->head, &phy_list); in usb_add_phy()
665 * usb_add_phy_dev - declare the USB PHY
666 * @x: the USB phy to be used; or NULL
668 * This call is exclusively for use by phy drivers, which
677 if (!x->dev) { in usb_add_phy_dev()
678 dev_err(x->dev, "no device provided for PHY\n"); in usb_add_phy_dev()
679 return -EINVAL; in usb_add_phy_dev()
687 ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier); in usb_add_phy_dev()
690 list_add_tail(&x->head, &phy_list); in usb_add_phy_dev()
698 * usb_remove_phy - remove the OTG PHY
699 * @x: the USB OTG PHY to be removed;
709 list_del(&x->head); in usb_remove_phy()
715 * usb_phy_set_event - set event to phy event
716 * @x: the phy returned by usb_get_phy();
719 * This sets event to phy event
723 x->last_event = event; in usb_phy_set_event()