• Home
  • Raw
  • Download

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
40 struct usb_phy *phy = NULL; in __usb_find_phy() local
42 list_for_each_entry(phy, list, head) { in __usb_find_phy()
43 if (phy->type != type) in __usb_find_phy()
46 return phy; in __usb_find_phy()
49 return ERR_PTR(-ENODEV); in __usb_find_phy()
54 struct usb_phy *phy; in __of_usb_find_phy() local
57 return ERR_PTR(-ENODEV); in __of_usb_find_phy()
59 list_for_each_entry(phy, &phy_list, head) { in __of_usb_find_phy()
60 if (node != phy->dev->of_node) in __of_usb_find_phy()
63 return phy; in __of_usb_find_phy()
66 return ERR_PTR(-EPROBE_DEFER); in __of_usb_find_phy()
71 usb_phy->chg_cur.sdp_min = DEFAULT_SDP_CUR_MIN; in usb_phy_set_default_current()
72 usb_phy->chg_cur.sdp_max = DEFAULT_SDP_CUR_MAX; in usb_phy_set_default_current()
73 usb_phy->chg_cur.dcp_min = DEFAULT_DCP_CUR_MIN; in usb_phy_set_default_current()
74 usb_phy->chg_cur.dcp_max = DEFAULT_DCP_CUR_MAX; in usb_phy_set_default_current()
75 usb_phy->chg_cur.cdp_min = DEFAULT_CDP_CUR_MIN; in usb_phy_set_default_current()
76 usb_phy->chg_cur.cdp_max = DEFAULT_CDP_CUR_MAX; in usb_phy_set_default_current()
77 usb_phy->chg_cur.aca_min = DEFAULT_ACA_CUR_MIN; in usb_phy_set_default_current()
78 usb_phy->chg_cur.aca_max = DEFAULT_ACA_CUR_MAX; in usb_phy_set_default_current()
82 * usb_phy_notify_charger_work - notify the USB charger state
83 * @work - the charger work to notify the USB charger state
85 * This work can be issued when USB charger state has been changed or
86 * USB charger current has been changed, then we can notify the current
93 * If we get the charger type from ->charger_detect() instead of extcon
94 * subsystem, the usb phy driver should issue usb_phy_set_charger_state()
104 switch (usb_phy->chg_state) { in usb_phy_notify_charger_work()
108 atomic_notifier_call_chain(&usb_phy->notifier, max, usb_phy); in usb_phy_notify_charger_work()
115 atomic_notifier_call_chain(&usb_phy->notifier, 0, usb_phy); in usb_phy_notify_charger_work()
120 dev_warn(usb_phy->dev, "Unknown USB charger state: %d\n", in usb_phy_notify_charger_work()
121 usb_phy->chg_state); in usb_phy_notify_charger_work()
125 kobject_uevent_env(&usb_phy->dev->kobj, KOBJ_CHANGE, envp); in usb_phy_notify_charger_work()
130 if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_SDP) > 0) { in __usb_phy_get_charger_type()
131 usb_phy->chg_type = SDP_TYPE; in __usb_phy_get_charger_type()
132 usb_phy->chg_state = USB_CHARGER_PRESENT; in __usb_phy_get_charger_type()
133 } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_CDP) > 0) { in __usb_phy_get_charger_type()
134 usb_phy->chg_type = CDP_TYPE; in __usb_phy_get_charger_type()
135 usb_phy->chg_state = USB_CHARGER_PRESENT; in __usb_phy_get_charger_type()
136 } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_DCP) > 0) { in __usb_phy_get_charger_type()
137 usb_phy->chg_type = DCP_TYPE; in __usb_phy_get_charger_type()
138 usb_phy->chg_state = USB_CHARGER_PRESENT; in __usb_phy_get_charger_type()
139 } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_ACA) > 0) { in __usb_phy_get_charger_type()
140 usb_phy->chg_type = ACA_TYPE; in __usb_phy_get_charger_type()
141 usb_phy->chg_state = USB_CHARGER_PRESENT; in __usb_phy_get_charger_type()
143 usb_phy->chg_type = UNKNOWN_TYPE; in __usb_phy_get_charger_type()
144 usb_phy->chg_state = USB_CHARGER_ABSENT; in __usb_phy_get_charger_type()
147 schedule_work(&usb_phy->chg_work); in __usb_phy_get_charger_type()
151 * usb_phy_get_charger_type - get charger type from extcon subsystem
152 * @nb -the notifier block to determine charger type
153 * @state - the cable state
154 * @data - private data
169 * usb_phy_set_charger_current - set the USB charger current
170 * @usb_phy - the USB phy to be used
171 * @mA - the current need to be set
173 * Usually we only change the charger default current when USB finished the
175 * will issue this function to change charger current when after setting USB
176 * configuration, or suspend/resume USB. For other type charger, we should
180 * When USB charger current has been changed, we need to notify the power users.
184 switch (usb_phy->chg_type) { in usb_phy_set_charger_current()
186 if (usb_phy->chg_cur.sdp_max == mA) in usb_phy_set_charger_current()
189 usb_phy->chg_cur.sdp_max = (mA > DEFAULT_SDP_CUR_MAX_SS) ? in usb_phy_set_charger_current()
193 if (usb_phy->chg_cur.dcp_max == mA) in usb_phy_set_charger_current()
196 usb_phy->chg_cur.dcp_max = (mA > DEFAULT_DCP_CUR_MAX) ? in usb_phy_set_charger_current()
200 if (usb_phy->chg_cur.cdp_max == mA) in usb_phy_set_charger_current()
203 usb_phy->chg_cur.cdp_max = (mA > DEFAULT_CDP_CUR_MAX) ? in usb_phy_set_charger_current()
207 if (usb_phy->chg_cur.aca_max == mA) in usb_phy_set_charger_current()
210 usb_phy->chg_cur.aca_max = (mA > DEFAULT_ACA_CUR_MAX) ? in usb_phy_set_charger_current()
217 schedule_work(&usb_phy->chg_work); in usb_phy_set_charger_current()
222 * usb_phy_get_charger_current - get the USB charger current
223 * @usb_phy - the USB phy to be used
224 * @min - the minimum current
225 * @max - the maximum current
234 switch (usb_phy->chg_type) { in usb_phy_get_charger_current()
236 *min = usb_phy->chg_cur.sdp_min; in usb_phy_get_charger_current()
237 *max = usb_phy->chg_cur.sdp_max; in usb_phy_get_charger_current()
240 *min = usb_phy->chg_cur.dcp_min; in usb_phy_get_charger_current()
241 *max = usb_phy->chg_cur.dcp_max; in usb_phy_get_charger_current()
244 *min = usb_phy->chg_cur.cdp_min; in usb_phy_get_charger_current()
245 *max = usb_phy->chg_cur.cdp_max; in usb_phy_get_charger_current()
248 *min = usb_phy->chg_cur.aca_min; in usb_phy_get_charger_current()
249 *max = usb_phy->chg_cur.aca_max; in usb_phy_get_charger_current()
260 * usb_phy_set_charger_state - set the USB charger state
261 * @usb_phy - the USB phy to be used
262 * @state - the new state need to be set for charger
264 * The usb phy driver can issue this function when the usb phy driver
266 * type should be get from ->charger_detect().
271 if (usb_phy->chg_state == state || !usb_phy->charger_detect) in usb_phy_set_charger_state()
274 usb_phy->chg_state = state; in usb_phy_set_charger_state()
275 if (usb_phy->chg_state == USB_CHARGER_PRESENT) in usb_phy_set_charger_state()
276 usb_phy->chg_type = usb_phy->charger_detect(usb_phy); in usb_phy_set_charger_state()
278 usb_phy->chg_type = UNKNOWN_TYPE; in usb_phy_set_charger_state()
280 schedule_work(&usb_phy->chg_work); in usb_phy_set_charger_state()
286 struct usb_phy *phy = *(struct usb_phy **)res; in devm_usb_phy_release() local
288 usb_put_phy(phy); in devm_usb_phy_release()
295 if (res->nb) in devm_usb_phy_release2()
296 usb_unregister_notifier(res->phy, res->nb); in devm_usb_phy_release2()
297 usb_put_phy(res->phy); in devm_usb_phy_release2()
302 struct usb_phy **phy = res; in devm_usb_phy_match() local
304 return *phy == match_data; in devm_usb_phy_match()
309 usb_phy->chg_type = UNKNOWN_TYPE; in usb_charger_init()
310 usb_phy->chg_state = USB_CHARGER_DEFAULT; in usb_charger_init()
312 INIT_WORK(&usb_phy->chg_work, usb_phy_notify_charger_work); in usb_charger_init()
319 if (of_property_read_bool(x->dev->of_node, "extcon")) { in usb_add_extcon()
320 x->edev = extcon_get_edev_by_phandle(x->dev, 0); in usb_add_extcon()
321 if (IS_ERR(x->edev)) in usb_add_extcon()
322 return PTR_ERR(x->edev); in usb_add_extcon()
324 x->id_edev = extcon_get_edev_by_phandle(x->dev, 1); in usb_add_extcon()
325 if (IS_ERR(x->id_edev)) { in usb_add_extcon()
326 x->id_edev = NULL; in usb_add_extcon()
327 dev_info(x->dev, "No separate ID extcon device\n"); in usb_add_extcon()
330 if (x->vbus_nb.notifier_call) { in usb_add_extcon()
331 ret = devm_extcon_register_notifier(x->dev, x->edev, in usb_add_extcon()
333 &x->vbus_nb); in usb_add_extcon()
335 dev_err(x->dev, in usb_add_extcon()
340 x->type_nb.notifier_call = usb_phy_get_charger_type; in usb_add_extcon()
342 ret = devm_extcon_register_notifier(x->dev, x->edev, in usb_add_extcon()
344 &x->type_nb); in usb_add_extcon()
346 dev_err(x->dev, in usb_add_extcon()
347 "register extcon USB SDP failed.\n"); in usb_add_extcon()
351 ret = devm_extcon_register_notifier(x->dev, x->edev, in usb_add_extcon()
353 &x->type_nb); in usb_add_extcon()
355 dev_err(x->dev, in usb_add_extcon()
356 "register extcon USB CDP failed.\n"); in usb_add_extcon()
360 ret = devm_extcon_register_notifier(x->dev, x->edev, in usb_add_extcon()
362 &x->type_nb); in usb_add_extcon()
364 dev_err(x->dev, in usb_add_extcon()
365 "register extcon USB DCP failed.\n"); in usb_add_extcon()
369 ret = devm_extcon_register_notifier(x->dev, x->edev, in usb_add_extcon()
371 &x->type_nb); in usb_add_extcon()
373 dev_err(x->dev, in usb_add_extcon()
374 "register extcon USB ACA failed.\n"); in usb_add_extcon()
379 if (x->id_nb.notifier_call) { in usb_add_extcon()
382 if (x->id_edev) in usb_add_extcon()
383 id_ext = x->id_edev; in usb_add_extcon()
385 id_ext = x->edev; in usb_add_extcon()
387 ret = devm_extcon_register_notifier(x->dev, id_ext, in usb_add_extcon()
389 &x->id_nb); in usb_add_extcon()
391 dev_err(x->dev, in usb_add_extcon()
398 if (x->type_nb.notifier_call) in usb_add_extcon()
405 * devm_usb_get_phy - find the USB PHY
406 * @dev - device that requests this phy
407 * @type - the type of the phy the controller requires
409 * Gets the phy using usb_get_phy(), and associates a device with it using
413 * For use by USB host and peripheral drivers.
417 struct usb_phy **ptr, *phy; in devm_usb_get_phy() local
421 return ERR_PTR(-ENOMEM); in devm_usb_get_phy()
423 phy = usb_get_phy(type); in devm_usb_get_phy()
424 if (!IS_ERR(phy)) { in devm_usb_get_phy()
425 *ptr = phy; in devm_usb_get_phy()
430 return phy; in devm_usb_get_phy()
435 * usb_get_phy - find the USB PHY
436 * @type - the type of the phy the controller requires
438 * Returns the phy driver, after getting a refcount to it; or
439 * -ENODEV if there is no such phy. The caller is responsible for
442 * For use by USB host and peripheral drivers.
446 struct usb_phy *phy = NULL; in usb_get_phy() local
451 phy = __usb_find_phy(&phy_list, type); in usb_get_phy()
452 if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) { in usb_get_phy()
453 pr_debug("PHY: unable to find transceiver of type %s\n", in usb_get_phy()
455 if (!IS_ERR(phy)) in usb_get_phy()
456 phy = ERR_PTR(-ENODEV); in usb_get_phy()
461 get_device(phy->dev); in usb_get_phy()
466 return phy; in usb_get_phy()
471 * devm_usb_get_phy_by_node - find the USB PHY by device_node
472 * @dev - device that requests this phy
473 * @node - the device_node for the phy device.
474 * @nb - a notifier_block to register with the phy.
476 * Returns the phy driver associated with the given device_node,
477 * after getting a refcount to it, -ENODEV if there is no such phy or
478 * -EPROBE_DEFER if the device is not yet loaded. While at that, it
480 * the phy using devres. On driver detach, release function is invoked
483 * For use by peripheral drivers for devices related to a phy,
490 struct usb_phy *phy = ERR_PTR(-ENOMEM); in devm_usb_get_phy_by_node() local
502 phy = __of_usb_find_phy(node); in devm_usb_get_phy_by_node()
503 if (IS_ERR(phy)) { in devm_usb_get_phy_by_node()
508 if (!try_module_get(phy->dev->driver->owner)) { in devm_usb_get_phy_by_node()
509 phy = ERR_PTR(-ENODEV); in devm_usb_get_phy_by_node()
514 usb_register_notifier(phy, nb); in devm_usb_get_phy_by_node()
515 ptr->phy = phy; in devm_usb_get_phy_by_node()
516 ptr->nb = nb; in devm_usb_get_phy_by_node()
519 get_device(phy->dev); in devm_usb_get_phy_by_node()
526 return phy; in devm_usb_get_phy_by_node()
531 * devm_usb_get_phy_by_phandle - find the USB PHY by phandle
532 * @dev - device that requests this phy
533 * @phandle - name of the property holding the phy phandle value
534 * @index - the index of the phy
536 * Returns the phy driver associated with the given phandle value,
537 * after getting a refcount to it, -ENODEV if there is no such phy or
538 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
540 * the phy using devres. On driver detach, release function is invoked
543 * For use by USB host and peripheral drivers.
549 struct usb_phy *phy; in devm_usb_get_phy_by_phandle() local
551 if (!dev->of_node) { in devm_usb_get_phy_by_phandle()
553 return ERR_PTR(-EINVAL); in devm_usb_get_phy_by_phandle()
556 node = of_parse_phandle(dev->of_node, phandle, index); in devm_usb_get_phy_by_phandle()
559 dev->of_node); in devm_usb_get_phy_by_phandle()
560 return ERR_PTR(-ENODEV); in devm_usb_get_phy_by_phandle()
562 phy = devm_usb_get_phy_by_node(dev, node, NULL); in devm_usb_get_phy_by_phandle()
564 return phy; in devm_usb_get_phy_by_phandle()
569 * devm_usb_put_phy - release the USB PHY
570 * @dev - device that wants to release this phy
571 * @phy - the phy returned by devm_usb_get_phy()
573 * destroys the devres associated with this phy and invokes usb_put_phy
574 * to release the phy.
576 * For use by USB host and peripheral drivers.
578 void devm_usb_put_phy(struct device *dev, struct usb_phy *phy) in devm_usb_put_phy() argument
582 r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy); in devm_usb_put_phy()
583 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n"); in devm_usb_put_phy()
588 * usb_put_phy - release the USB PHY
589 * @x: the phy returned by usb_get_phy()
593 * For use by USB host and peripheral drivers.
598 struct module *owner = x->dev->driver->owner; in usb_put_phy()
600 put_device(x->dev); in usb_put_phy()
607 * usb_add_phy - declare the USB PHY
608 * @x: the USB phy to be used; or NULL
609 * @type - the type of this PHY
611 * This call is exclusively for use by phy drivers, which
619 struct usb_phy *phy; in usb_add_phy() local
621 if (x->type != USB_PHY_TYPE_UNDEFINED) { in usb_add_phy()
622 dev_err(x->dev, "not accepting initialized PHY %s\n", x->label); in usb_add_phy()
623 return -EINVAL; in usb_add_phy()
631 ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier); in usb_add_phy()
635 list_for_each_entry(phy, &phy_list, head) { in usb_add_phy()
636 if (phy->type == type) { in usb_add_phy()
637 ret = -EBUSY; in usb_add_phy()
638 dev_err(x->dev, "transceiver type %s already exists\n", in usb_add_phy()
644 x->type = type; in usb_add_phy()
645 list_add_tail(&x->head, &phy_list); in usb_add_phy()
654 * usb_add_phy_dev - declare the USB PHY
655 * @x: the USB phy to be used; or NULL
657 * This call is exclusively for use by phy drivers, which
666 if (!x->dev) { in usb_add_phy_dev()
667 dev_err(x->dev, "no device provided for PHY\n"); in usb_add_phy_dev()
668 return -EINVAL; in usb_add_phy_dev()
676 ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier); in usb_add_phy_dev()
679 list_add_tail(&x->head, &phy_list); in usb_add_phy_dev()
687 * usb_remove_phy - remove the OTG PHY
688 * @x: the USB OTG PHY to be removed;
698 list_del(&x->head); in usb_remove_phy()
704 * usb_phy_set_event - set event to phy event
705 * @x: the phy returned by usb_get_phy();
707 * This sets event to phy event
711 x->last_event = event; in usb_phy_set_event()