Lines Matching +full:usb +full:- +full:sdp

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>
51 if (phy->type != type) in __usb_find_phy()
57 return ERR_PTR(-ENODEV); in __usb_find_phy()
65 return ERR_PTR(-ENODEV); in __of_usb_find_phy()
68 if (node != phy->dev->of_node) 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
91 * @work: the charger work to notify the USB charger state
93 * This work can be issued when USB charger state has been changed or
94 * USB charger current has been changed, then we can notify the current
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
184 * Usually we only change the charger default current when USB finished the
185 * enumeration as one SDP charger. As one SDP charger, usb_phy_set_power()
186 * will issue this function to change charger current when after setting USB
187 * configuration, or suspend/resume USB. For other type charger, we should
191 * When USB charger current has been changed, we need to notify the power users.
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()
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()
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()
358 "register extcon USB SDP failed.\n"); 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()
367 "register extcon USB CDP failed.\n"); 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()
376 "register extcon USB DCP failed.\n"); 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()
385 "register extcon USB ACA failed.\n"); 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
424 * For use by USB host and peripheral drivers.
432 return ERR_PTR(-ENOMEM); in devm_usb_get_phy()
446 * usb_get_phy - find the USB PHY
450 * -ENODEV if there is no such phy. The caller is responsible for
453 * For use by USB host and peripheral drivers.
463 if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) { in usb_get_phy()
467 phy = ERR_PTR(-ENODEV); in usb_get_phy()
472 get_device(phy->dev); in usb_get_phy()
482 * devm_usb_get_phy_by_node - find the USB PHY by 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
501 struct usb_phy *phy = ERR_PTR(-ENOMEM); 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()
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()
542 * devm_usb_get_phy_by_phandle - find the USB PHY by phandle
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
554 * For use by USB host and peripheral drivers.
562 if (!dev->of_node) { 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()
580 * devm_usb_put_phy - release the USB PHY
587 * For use by USB host and peripheral drivers.
599 * usb_put_phy - release the USB PHY
604 * For use by USB host and peripheral drivers.
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
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()
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
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
723 x->last_event = event; in usb_phy_set_event()