Lines Matching +full:x +full:- +full:max
1 // SPDX-License-Identifier: GPL-2.0+
3 * phy.c -- USB phy handling
5 * Copyright (C) 2004-2013 Texas Instruments
57 if (phy->type != type) in __usb_find_phy()
63 return ERR_PTR(-ENODEV); in __usb_find_phy()
71 return ERR_PTR(-ENODEV); in __of_usb_find_phy()
74 if (node != phy->dev->of_node) 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
119 * If we get the charger type from ->charger_detect() instead of extcon
126 unsigned int min, max; in usb_phy_notify_charger_work() local
128 switch (usb_phy->chg_state) { in usb_phy_notify_charger_work()
130 usb_phy_get_charger_current(usb_phy, &min, &max); 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
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
274 * @max: the maximum current
281 unsigned int *min, unsigned int *max) in usb_phy_get_charger_current() argument
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()
302 *max = 0; in usb_phy_get_charger_current()
309 * usb_phy_set_charger_state - set the USB charger state
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()
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()
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()
364 static int usb_add_extcon(struct usb_phy *x) in usb_add_extcon() argument
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()
448 __usb_phy_get_charger_type(x); in usb_add_extcon()
454 * devm_usb_get_phy - find the USB PHY
470 return ERR_PTR(-ENOMEM); in devm_usb_get_phy()
484 * usb_get_phy - find the USB PHY
488 * -ENODEV if there is no such phy. The caller is responsible for
501 if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) { in usb_get_phy()
505 phy = ERR_PTR(-ENODEV); in usb_get_phy()
510 get_device(phy->dev); in usb_get_phy()
520 * devm_usb_get_phy_by_node - find the USB PHY by 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
539 struct usb_phy *phy = ERR_PTR(-ENOMEM); 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()
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()
580 * devm_usb_get_phy_by_phandle - find the USB PHY by phandle
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
600 if (!dev->of_node) { 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()
618 * devm_usb_put_phy - release the USB PHY
637 * usb_put_phy - release the USB PHY
638 * @x: the phy returned by usb_get_phy()
644 void usb_put_phy(struct usb_phy *x) in usb_put_phy() argument
646 if (x) { in usb_put_phy()
647 struct module *owner = x->dev->driver->owner; in usb_put_phy()
649 put_device(x->dev); in usb_put_phy()
657 * @x: the USB phy to be used; or NULL
664 int usb_add_phy(struct usb_phy *x, enum usb_phy_type type) in usb_add_phy() argument
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()
675 usb_charger_init(x); in usb_add_phy()
676 ret = usb_add_extcon(x); in usb_add_phy()
680 ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier); 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
715 int usb_add_phy_dev(struct usb_phy *x) in usb_add_phy_dev() argument
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()
725 usb_charger_init(x); in usb_add_phy_dev()
726 ret = usb_add_extcon(x); 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;
748 void usb_remove_phy(struct usb_phy *x) in usb_remove_phy() argument
753 if (x) in usb_remove_phy()
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();
766 void usb_phy_set_event(struct usb_phy *x, unsigned long event) in usb_phy_set_event() argument
768 x->last_event = event; in usb_phy_set_event()