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
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()
83 static struct usb_phy *__device_to_usb_phy(const struct device *dev) in __device_to_usb_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
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()
148 static int usb_phy_uevent(const struct device *dev, struct kobj_uevent_env *env) in usb_phy_uevent()
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
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()
333 static void devm_usb_phy_release(struct device *dev, void *res) in devm_usb_phy_release()
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()
340 static void devm_usb_phy_release2(struct device *dev, void *_res) in devm_usb_phy_release2()
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()
349 static int devm_usb_phy_match(struct device *dev, void *res, void *match_data) in devm_usb_phy_match()
351 struct usb_phy **phy = res; in devm_usb_phy_match() local
353 return *phy == match_data; in devm_usb_phy_match()
358 usb_phy->chg_type = UNKNOWN_TYPE; in usb_charger_init()
359 usb_phy->chg_state = USB_CHARGER_DEFAULT; in usb_charger_init()
361 INIT_WORK(&usb_phy->chg_work, usb_phy_notify_charger_work); in usb_charger_init()
368 if (of_property_read_bool(x->dev->of_node, "extcon")) { in usb_add_extcon()
369 x->edev = extcon_get_edev_by_phandle(x->dev, 0); in usb_add_extcon()
370 if (IS_ERR(x->edev)) in usb_add_extcon()
371 return PTR_ERR(x->edev); in usb_add_extcon()
373 x->id_edev = extcon_get_edev_by_phandle(x->dev, 1); in usb_add_extcon()
374 if (IS_ERR(x->id_edev)) { in usb_add_extcon()
375 x->id_edev = NULL; in usb_add_extcon()
376 dev_info(x->dev, "No separate ID extcon device\n"); in usb_add_extcon()
379 if (x->vbus_nb.notifier_call) { in usb_add_extcon()
380 ret = devm_extcon_register_notifier(x->dev, x->edev, in usb_add_extcon()
382 &x->vbus_nb); in usb_add_extcon()
384 dev_err(x->dev, in usb_add_extcon()
389 x->type_nb.notifier_call = usb_phy_get_charger_type; in usb_add_extcon()
391 ret = devm_extcon_register_notifier(x->dev, x->edev, in usb_add_extcon()
393 &x->type_nb); in usb_add_extcon()
395 dev_err(x->dev, in usb_add_extcon()
400 ret = devm_extcon_register_notifier(x->dev, x->edev, in usb_add_extcon()
402 &x->type_nb); in usb_add_extcon()
404 dev_err(x->dev, in usb_add_extcon()
409 ret = devm_extcon_register_notifier(x->dev, x->edev, in usb_add_extcon()
411 &x->type_nb); in usb_add_extcon()
413 dev_err(x->dev, in usb_add_extcon()
418 ret = devm_extcon_register_notifier(x->dev, x->edev, in usb_add_extcon()
420 &x->type_nb); in usb_add_extcon()
422 dev_err(x->dev, in usb_add_extcon()
428 if (x->id_nb.notifier_call) { in usb_add_extcon()
431 if (x->id_edev) in usb_add_extcon()
432 id_ext = x->id_edev; in usb_add_extcon()
434 id_ext = x->edev; in usb_add_extcon()
436 ret = devm_extcon_register_notifier(x->dev, id_ext, in usb_add_extcon()
438 &x->id_nb); in usb_add_extcon()
440 dev_err(x->dev, in usb_add_extcon()
447 if (x->type_nb.notifier_call) in usb_add_extcon()
454 * devm_usb_get_phy - find the USB PHY
455 * @dev: device that requests this phy
456 * @type: the type of the phy the controller requires
458 * Gets the phy using usb_get_phy(), and associates a device with it using
464 struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type) in devm_usb_get_phy()
466 struct usb_phy **ptr, *phy; in devm_usb_get_phy() local
470 return ERR_PTR(-ENOMEM); in devm_usb_get_phy()
472 phy = usb_get_phy(type); in devm_usb_get_phy()
473 if (!IS_ERR(phy)) { in devm_usb_get_phy()
474 *ptr = phy; in devm_usb_get_phy()
479 return phy; in devm_usb_get_phy()
484 * usb_get_phy - find the USB PHY
485 * @type: the type of the phy the controller requires
487 * Returns the phy driver, after getting a refcount to it; or
488 * -ENODEV if there is no such phy. The caller is responsible for
495 struct usb_phy *phy = NULL; in usb_get_phy() local
500 phy = __usb_find_phy(&phy_list, type); in usb_get_phy()
501 if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) { in usb_get_phy()
502 pr_debug("PHY: unable to find transceiver of type %s\n", in usb_get_phy()
504 if (!IS_ERR(phy)) in usb_get_phy()
505 phy = ERR_PTR(-ENODEV); in usb_get_phy()
510 get_device(phy->dev); in usb_get_phy()
515 return phy; in usb_get_phy()
520 * devm_usb_get_phy_by_node - find the USB PHY by device_node
521 * @dev: device that requests this phy
522 * @node: the device_node for the phy device.
523 * @nb: a notifier_block to register with the phy.
525 * Returns the phy driver associated with the given device_node,
526 * after getting a refcount to it, -ENODEV if there is no such phy or
527 * -EPROBE_DEFER if the device is not yet loaded. While at that, it
528 * also associates the device with
529 * the phy using devres. On driver detach, release function is invoked
532 * For use by peripheral drivers for devices related to a phy,
535 struct usb_phy *devm_usb_get_phy_by_node(struct device *dev, in devm_usb_get_phy_by_node()
539 struct usb_phy *phy = ERR_PTR(-ENOMEM); in devm_usb_get_phy_by_node() local
551 phy = __of_usb_find_phy(node); in devm_usb_get_phy_by_node()
552 if (IS_ERR(phy)) { in devm_usb_get_phy_by_node()
557 if (!try_module_get(phy->dev->driver->owner)) { in devm_usb_get_phy_by_node()
558 phy = ERR_PTR(-ENODEV); in devm_usb_get_phy_by_node()
563 usb_register_notifier(phy, nb); in devm_usb_get_phy_by_node()
564 ptr->phy = phy; in devm_usb_get_phy_by_node()
565 ptr->nb = nb; in devm_usb_get_phy_by_node()
568 get_device(phy->dev); in devm_usb_get_phy_by_node()
575 return phy; in devm_usb_get_phy_by_node()
580 * devm_usb_get_phy_by_phandle - find the USB PHY by phandle
581 * @dev: device that requests this phy
582 * @phandle: name of the property holding the phy phandle value
583 * @index: the index of the phy
585 * Returns the phy driver associated with the given phandle value,
586 * after getting a refcount to it, -ENODEV if there is no such phy or
587 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
588 * not yet loaded. While at that, it also associates the device with
589 * the phy using devres. On driver detach, release function is invoked
594 struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev, in devm_usb_get_phy_by_phandle()
598 struct usb_phy *phy; in devm_usb_get_phy_by_phandle() local
600 if (!dev->of_node) { in devm_usb_get_phy_by_phandle()
601 dev_dbg(dev, "device does not have a device node entry\n"); in devm_usb_get_phy_by_phandle()
602 return ERR_PTR(-EINVAL); in devm_usb_get_phy_by_phandle()
605 node = of_parse_phandle(dev->of_node, phandle, index); in devm_usb_get_phy_by_phandle()
608 dev->of_node); in devm_usb_get_phy_by_phandle()
609 return ERR_PTR(-ENODEV); in devm_usb_get_phy_by_phandle()
611 phy = devm_usb_get_phy_by_node(dev, node, NULL); in devm_usb_get_phy_by_phandle()
613 return phy; in devm_usb_get_phy_by_phandle()
618 * devm_usb_put_phy - release the USB PHY
619 * @dev: device that wants to release this phy
620 * @phy: the phy returned by devm_usb_get_phy()
622 * destroys the devres associated with this phy and invokes usb_put_phy
623 * to release the phy.
627 void devm_usb_put_phy(struct device *dev, struct usb_phy *phy) in devm_usb_put_phy() argument
631 r = devres_release(dev, devm_usb_phy_release, devm_usb_phy_match, phy); in devm_usb_put_phy()
632 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n"); in devm_usb_put_phy()
637 * usb_put_phy - release the USB PHY
638 * @x: the phy returned by usb_get_phy()
647 struct module *owner = x->dev->driver->owner; in usb_put_phy()
649 put_device(x->dev); in usb_put_phy()
656 * usb_add_phy: declare the USB PHY
657 * @x: the USB phy to be used; or NULL
658 * @type: the type of this PHY
660 * This call is exclusively for use by phy drivers, which
668 struct usb_phy *phy; in usb_add_phy() local
670 if (x->type != USB_PHY_TYPE_UNDEFINED) { in usb_add_phy()
671 dev_err(x->dev, "not accepting initialized PHY %s\n", x->label); in usb_add_phy()
672 return -EINVAL; in usb_add_phy()
680 ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier); in usb_add_phy()
684 list_for_each_entry(phy, &phy_list, head) { in usb_add_phy()
685 if (phy->type == type) { in usb_add_phy()
686 ret = -EBUSY; in usb_add_phy()
687 dev_err(x->dev, "transceiver type %s already exists\n", in usb_add_phy()
693 x->type = type; in usb_add_phy()
694 list_add_tail(&x->head, &phy_list); in usb_add_phy()
708 * usb_add_phy_dev - declare the USB PHY
709 * @x: the USB phy to be used; or NULL
711 * This call is exclusively for use by phy drivers, which
720 if (!x->dev) { in usb_add_phy_dev()
721 dev_err(x->dev, "no device provided for PHY\n"); in usb_add_phy_dev()
722 return -EINVAL; in usb_add_phy_dev()
730 x->dev->type = &usb_phy_dev_type; in usb_add_phy_dev()
732 ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier); in usb_add_phy_dev()
735 list_add_tail(&x->head, &phy_list); in usb_add_phy_dev()
743 * usb_remove_phy - remove the OTG PHY
744 * @x: the USB OTG PHY to be removed;
754 list_del(&x->head); in usb_remove_phy()
760 * usb_phy_set_event - set event to phy event
761 * @x: the phy returned by usb_get_phy();
764 * This sets event to phy event
768 x->last_event = event; in usb_phy_set_event()