1 // SPDX-License-Identifier: GPL-2.0+
3 * phy.c -- USB phy handling
5 * Copyright (C) 2004-2013 Texas Instruments
7 #include <linux/kernel.h>
8 #include <linux/export.h>
10 #include <linux/device.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
15 #include <linux/usb/phy.h>
17 /* Default current range by charger type. */
18 #define DEFAULT_SDP_CUR_MIN 2
19 #define DEFAULT_SDP_CUR_MAX 500
20 #define DEFAULT_SDP_CUR_MIN_SS 150
21 #define DEFAULT_SDP_CUR_MAX_SS 900
22 #define DEFAULT_DCP_CUR_MIN 500
23 #define DEFAULT_DCP_CUR_MAX 5000
24 #define DEFAULT_CDP_CUR_MIN 1500
25 #define DEFAULT_CDP_CUR_MAX 5000
26 #define DEFAULT_ACA_CUR_MIN 1500
27 #define DEFAULT_ACA_CUR_MAX 5000
29 static LIST_HEAD(phy_list);
30 static DEFINE_SPINLOCK(phy_lock);
34 struct notifier_block *nb;
37 static const char *const usb_chger_type[] = {
38 [UNKNOWN_TYPE] = "USB_CHARGER_UNKNOWN_TYPE",
39 [SDP_TYPE] = "USB_CHARGER_SDP_TYPE",
40 [CDP_TYPE] = "USB_CHARGER_CDP_TYPE",
41 [DCP_TYPE] = "USB_CHARGER_DCP_TYPE",
42 [ACA_TYPE] = "USB_CHARGER_ACA_TYPE",
45 static const char *const usb_chger_state[] = {
46 [USB_CHARGER_DEFAULT] = "USB_CHARGER_DEFAULT",
47 [USB_CHARGER_PRESENT] = "USB_CHARGER_PRESENT",
48 [USB_CHARGER_ABSENT] = "USB_CHARGER_ABSENT",
51 static struct usb_phy *__usb_find_phy(struct list_head *list,
52 enum usb_phy_type type)
54 struct usb_phy *phy = NULL;
56 list_for_each_entry(phy, list, head) {
57 if (phy->type != type)
63 return ERR_PTR(-ENODEV);
66 static struct usb_phy *__of_usb_find_phy(struct device_node *node)
70 if (!of_device_is_available(node))
71 return ERR_PTR(-ENODEV);
73 list_for_each_entry(phy, &phy_list, head) {
74 if (node != phy->dev->of_node)
80 return ERR_PTR(-EPROBE_DEFER);
83 static struct usb_phy *__device_to_usb_phy(struct device *dev)
85 struct usb_phy *usb_phy;
87 list_for_each_entry(usb_phy, &phy_list, head) {
88 if (usb_phy->dev == dev)
95 static void usb_phy_set_default_current(struct usb_phy *usb_phy)
97 usb_phy->chg_cur.sdp_min = DEFAULT_SDP_CUR_MIN;
98 usb_phy->chg_cur.sdp_max = DEFAULT_SDP_CUR_MAX;
99 usb_phy->chg_cur.dcp_min = DEFAULT_DCP_CUR_MIN;
100 usb_phy->chg_cur.dcp_max = DEFAULT_DCP_CUR_MAX;
101 usb_phy->chg_cur.cdp_min = DEFAULT_CDP_CUR_MIN;
102 usb_phy->chg_cur.cdp_max = DEFAULT_CDP_CUR_MAX;
103 usb_phy->chg_cur.aca_min = DEFAULT_ACA_CUR_MIN;
104 usb_phy->chg_cur.aca_max = DEFAULT_ACA_CUR_MAX;
108 * usb_phy_notify_charger_work - notify the USB charger state
109 * @work: the charger work to notify the USB charger state
111 * This work can be issued when USB charger state has been changed or
112 * USB charger current has been changed, then we can notify the current
113 * what can be drawn to power user and the charger state to userspace.
115 * If we get the charger type from extcon subsystem, we can notify the
116 * charger state to power user automatically by usb_phy_get_charger_type()
117 * issuing from extcon subsystem.
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()
121 * to set charger state when the charger state has been changed.
123 static void usb_phy_notify_charger_work(struct work_struct *work)
125 struct usb_phy *usb_phy = container_of(work, struct usb_phy, chg_work);
126 unsigned int min, max;
128 switch (usb_phy->chg_state) {
129 case USB_CHARGER_PRESENT:
130 usb_phy_get_charger_current(usb_phy, &min, &max);
132 atomic_notifier_call_chain(&usb_phy->notifier, max, usb_phy);
134 case USB_CHARGER_ABSENT:
135 usb_phy_set_default_current(usb_phy);
137 atomic_notifier_call_chain(&usb_phy->notifier, 0, usb_phy);
140 dev_warn(usb_phy->dev, "Unknown USB charger state: %d\n",
145 kobject_uevent(&usb_phy->dev->kobj, KOBJ_CHANGE);
148 static int usb_phy_uevent(struct device *dev, struct kobj_uevent_env *env)
150 struct usb_phy *usb_phy;
151 char uchger_state[50] = { 0 };
152 char uchger_type[50] = { 0 };
154 usb_phy = __device_to_usb_phy(dev);
156 snprintf(uchger_state, ARRAY_SIZE(uchger_state),
157 "USB_CHARGER_STATE=%s", usb_chger_state[usb_phy->chg_state]);
159 snprintf(uchger_type, ARRAY_SIZE(uchger_type),
160 "USB_CHARGER_TYPE=%s", usb_chger_type[usb_phy->chg_type]);
162 if (add_uevent_var(env, uchger_state))
165 if (add_uevent_var(env, uchger_type))
171 static void __usb_phy_get_charger_type(struct usb_phy *usb_phy)
173 if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_SDP) > 0) {
174 usb_phy->chg_type = SDP_TYPE;
175 usb_phy->chg_state = USB_CHARGER_PRESENT;
176 } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_CDP) > 0) {
177 usb_phy->chg_type = CDP_TYPE;
178 usb_phy->chg_state = USB_CHARGER_PRESENT;
179 } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_DCP) > 0) {
180 usb_phy->chg_type = DCP_TYPE;
181 usb_phy->chg_state = USB_CHARGER_PRESENT;
182 } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_ACA) > 0) {
183 usb_phy->chg_type = ACA_TYPE;
184 usb_phy->chg_state = USB_CHARGER_PRESENT;
186 usb_phy->chg_type = UNKNOWN_TYPE;
187 usb_phy->chg_state = USB_CHARGER_ABSENT;
190 schedule_work(&usb_phy->chg_work);
194 * usb_phy_get_charger_type - get charger type from extcon subsystem
195 * @nb: the notifier block to determine charger type
196 * @state: the cable state
197 * @data: private data
199 * Determin the charger type from extcon subsystem which also means the
200 * charger state has been chaned, then we should notify this event.
202 static int usb_phy_get_charger_type(struct notifier_block *nb,
203 unsigned long state, void *data)
205 struct usb_phy *usb_phy = container_of(nb, struct usb_phy, type_nb);
207 __usb_phy_get_charger_type(usb_phy);
212 * usb_phy_set_charger_current - set the USB charger current
213 * @usb_phy: the USB phy to be used
214 * @mA: the current need to be set
216 * Usually we only change the charger default current when USB finished the
217 * enumeration as one SDP charger. As one SDP charger, usb_phy_set_power()
218 * will issue this function to change charger current when after setting USB
219 * configuration, or suspend/resume USB. For other type charger, we should
220 * use the default charger current and we do not suggest to issue this function
221 * to change the charger current.
223 * When USB charger current has been changed, we need to notify the power users.
225 void usb_phy_set_charger_current(struct usb_phy *usb_phy, unsigned int mA)
227 switch (usb_phy->chg_type) {
229 if (usb_phy->chg_cur.sdp_max == mA)
232 usb_phy->chg_cur.sdp_max = (mA > DEFAULT_SDP_CUR_MAX_SS) ?
233 DEFAULT_SDP_CUR_MAX_SS : mA;
236 if (usb_phy->chg_cur.dcp_max == mA)
239 usb_phy->chg_cur.dcp_max = (mA > DEFAULT_DCP_CUR_MAX) ?
240 DEFAULT_DCP_CUR_MAX : mA;
243 if (usb_phy->chg_cur.cdp_max == mA)
246 usb_phy->chg_cur.cdp_max = (mA > DEFAULT_CDP_CUR_MAX) ?
247 DEFAULT_CDP_CUR_MAX : mA;
250 if (usb_phy->chg_cur.aca_max == mA)
253 usb_phy->chg_cur.aca_max = (mA > DEFAULT_ACA_CUR_MAX) ?
254 DEFAULT_ACA_CUR_MAX : mA;
260 schedule_work(&usb_phy->chg_work);
262 EXPORT_SYMBOL_GPL(usb_phy_set_charger_current);
265 * usb_phy_get_charger_current - get the USB charger current
266 * @usb_phy: the USB phy to be used
267 * @min: the minimum current
268 * @max: the maximum current
270 * Usually we will notify the maximum current to power user, but for some
271 * special case, power user also need the minimum current value. Then the
272 * power user can issue this function to get the suitable current.
274 void usb_phy_get_charger_current(struct usb_phy *usb_phy,
275 unsigned int *min, unsigned int *max)
277 switch (usb_phy->chg_type) {
279 *min = usb_phy->chg_cur.sdp_min;
280 *max = usb_phy->chg_cur.sdp_max;
283 *min = usb_phy->chg_cur.dcp_min;
284 *max = usb_phy->chg_cur.dcp_max;
287 *min = usb_phy->chg_cur.cdp_min;
288 *max = usb_phy->chg_cur.cdp_max;
291 *min = usb_phy->chg_cur.aca_min;
292 *max = usb_phy->chg_cur.aca_max;
300 EXPORT_SYMBOL_GPL(usb_phy_get_charger_current);
303 * usb_phy_set_charger_state - set the USB charger state
304 * @usb_phy: the USB phy to be used
305 * @state: the new state need to be set for charger
307 * The usb phy driver can issue this function when the usb phy driver
308 * detected the charger state has been changed, in this case the charger
309 * type should be get from ->charger_detect().
311 void usb_phy_set_charger_state(struct usb_phy *usb_phy,
312 enum usb_charger_state state)
314 if (usb_phy->chg_state == state || !usb_phy->charger_detect)
317 usb_phy->chg_state = state;
318 if (usb_phy->chg_state == USB_CHARGER_PRESENT)
319 usb_phy->chg_type = usb_phy->charger_detect(usb_phy);
321 usb_phy->chg_type = UNKNOWN_TYPE;
323 schedule_work(&usb_phy->chg_work);
325 EXPORT_SYMBOL_GPL(usb_phy_set_charger_state);
327 static void devm_usb_phy_release(struct device *dev, void *res)
329 struct usb_phy *phy = *(struct usb_phy **)res;
334 static void devm_usb_phy_release2(struct device *dev, void *_res)
336 struct phy_devm *res = _res;
339 usb_unregister_notifier(res->phy, res->nb);
340 usb_put_phy(res->phy);
343 static int devm_usb_phy_match(struct device *dev, void *res, void *match_data)
345 struct usb_phy **phy = res;
347 return *phy == match_data;
350 static void usb_charger_init(struct usb_phy *usb_phy)
352 usb_phy->chg_type = UNKNOWN_TYPE;
353 usb_phy->chg_state = USB_CHARGER_DEFAULT;
354 usb_phy_set_default_current(usb_phy);
355 INIT_WORK(&usb_phy->chg_work, usb_phy_notify_charger_work);
358 static int usb_add_extcon(struct usb_phy *x)
362 if (of_property_read_bool(x->dev->of_node, "extcon")) {
363 x->edev = extcon_get_edev_by_phandle(x->dev, 0);
365 return PTR_ERR(x->edev);
367 x->id_edev = extcon_get_edev_by_phandle(x->dev, 1);
368 if (IS_ERR(x->id_edev)) {
370 dev_info(x->dev, "No separate ID extcon device\n");
373 if (x->vbus_nb.notifier_call) {
374 ret = devm_extcon_register_notifier(x->dev, x->edev,
379 "register VBUS notifier failed\n");
383 x->type_nb.notifier_call = usb_phy_get_charger_type;
385 ret = devm_extcon_register_notifier(x->dev, x->edev,
390 "register extcon USB SDP failed.\n");
394 ret = devm_extcon_register_notifier(x->dev, x->edev,
399 "register extcon USB CDP failed.\n");
403 ret = devm_extcon_register_notifier(x->dev, x->edev,
408 "register extcon USB DCP failed.\n");
412 ret = devm_extcon_register_notifier(x->dev, x->edev,
417 "register extcon USB ACA failed.\n");
422 if (x->id_nb.notifier_call) {
423 struct extcon_dev *id_ext;
430 ret = devm_extcon_register_notifier(x->dev, id_ext,
435 "register ID notifier failed\n");
441 if (x->type_nb.notifier_call)
442 __usb_phy_get_charger_type(x);
448 * devm_usb_get_phy - find the USB PHY
449 * @dev: device that requests this phy
450 * @type: the type of the phy the controller requires
452 * Gets the phy using usb_get_phy(), and associates a device with it using
453 * devres. On driver detach, release function is invoked on the devres data,
454 * then, devres data is freed.
456 * For use by USB host and peripheral drivers.
458 struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type)
460 struct usb_phy **ptr, *phy;
462 ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
464 return ERR_PTR(-ENOMEM);
466 phy = usb_get_phy(type);
469 devres_add(dev, ptr);
475 EXPORT_SYMBOL_GPL(devm_usb_get_phy);
478 * usb_get_phy - find the USB PHY
479 * @type: the type of the phy the controller requires
481 * Returns the phy driver, after getting a refcount to it; or
482 * -ENODEV if there is no such phy. The caller is responsible for
483 * calling usb_put_phy() to release that count.
485 * For use by USB host and peripheral drivers.
487 struct usb_phy *usb_get_phy(enum usb_phy_type type)
489 struct usb_phy *phy = NULL;
492 spin_lock_irqsave(&phy_lock, flags);
494 phy = __usb_find_phy(&phy_list, type);
495 if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) {
496 pr_debug("PHY: unable to find transceiver of type %s\n",
497 usb_phy_type_string(type));
499 phy = ERR_PTR(-ENODEV);
504 get_device(phy->dev);
507 spin_unlock_irqrestore(&phy_lock, flags);
511 EXPORT_SYMBOL_GPL(usb_get_phy);
514 * devm_usb_get_phy_by_node - find the USB PHY by device_node
515 * @dev: device that requests this phy
516 * @node: the device_node for the phy device.
517 * @nb: a notifier_block to register with the phy.
519 * Returns the phy driver associated with the given device_node,
520 * after getting a refcount to it, -ENODEV if there is no such phy or
521 * -EPROBE_DEFER if the device is not yet loaded. While at that, it
522 * also associates the device with
523 * the phy using devres. On driver detach, release function is invoked
524 * on the devres data, then, devres data is freed.
526 * For use by peripheral drivers for devices related to a phy,
529 struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
530 struct device_node *node,
531 struct notifier_block *nb)
533 struct usb_phy *phy = ERR_PTR(-ENOMEM);
534 struct phy_devm *ptr;
537 ptr = devres_alloc(devm_usb_phy_release2, sizeof(*ptr), GFP_KERNEL);
539 dev_dbg(dev, "failed to allocate memory for devres\n");
543 spin_lock_irqsave(&phy_lock, flags);
545 phy = __of_usb_find_phy(node);
551 if (!try_module_get(phy->dev->driver->owner)) {
552 phy = ERR_PTR(-ENODEV);
557 usb_register_notifier(phy, nb);
560 devres_add(dev, ptr);
562 get_device(phy->dev);
565 spin_unlock_irqrestore(&phy_lock, flags);
571 EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_node);
574 * devm_usb_get_phy_by_phandle - find the USB PHY by phandle
575 * @dev: device that requests this phy
576 * @phandle: name of the property holding the phy phandle value
577 * @index: the index of the phy
579 * Returns the phy driver associated with the given phandle value,
580 * after getting a refcount to it, -ENODEV if there is no such phy or
581 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
582 * not yet loaded. While at that, it also associates the device with
583 * the phy using devres. On driver detach, release function is invoked
584 * on the devres data, then, devres data is freed.
586 * For use by USB host and peripheral drivers.
588 struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
589 const char *phandle, u8 index)
591 struct device_node *node;
595 dev_dbg(dev, "device does not have a device node entry\n");
596 return ERR_PTR(-EINVAL);
599 node = of_parse_phandle(dev->of_node, phandle, index);
601 dev_dbg(dev, "failed to get %s phandle in %pOF node\n", phandle,
603 return ERR_PTR(-ENODEV);
605 phy = devm_usb_get_phy_by_node(dev, node, NULL);
609 EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_phandle);
612 * devm_usb_put_phy - release the USB PHY
613 * @dev: device that wants to release this phy
614 * @phy: the phy returned by devm_usb_get_phy()
616 * destroys the devres associated with this phy and invokes usb_put_phy
617 * to release the phy.
619 * For use by USB host and peripheral drivers.
621 void devm_usb_put_phy(struct device *dev, struct usb_phy *phy)
625 r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy);
626 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
628 EXPORT_SYMBOL_GPL(devm_usb_put_phy);
631 * usb_put_phy - release the USB PHY
632 * @x: the phy returned by usb_get_phy()
634 * Releases a refcount the caller received from usb_get_phy().
636 * For use by USB host and peripheral drivers.
638 void usb_put_phy(struct usb_phy *x)
641 struct module *owner = x->dev->driver->owner;
647 EXPORT_SYMBOL_GPL(usb_put_phy);
650 * usb_add_phy: declare the USB PHY
651 * @x: the USB phy to be used; or NULL
652 * @type: the type of this PHY
654 * This call is exclusively for use by phy drivers, which
655 * coordinate the activities of drivers for host and peripheral
656 * controllers, and in some cases for VBUS current regulation.
658 int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
664 if (x->type != USB_PHY_TYPE_UNDEFINED) {
665 dev_err(x->dev, "not accepting initialized PHY %s\n", x->label);
670 ret = usb_add_extcon(x);
674 ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier);
676 spin_lock_irqsave(&phy_lock, flags);
678 list_for_each_entry(phy, &phy_list, head) {
679 if (phy->type == type) {
681 dev_err(x->dev, "transceiver type %s already exists\n",
682 usb_phy_type_string(type));
688 list_add_tail(&x->head, &phy_list);
691 spin_unlock_irqrestore(&phy_lock, flags);
694 EXPORT_SYMBOL_GPL(usb_add_phy);
696 static struct device_type usb_phy_dev_type = {
698 .uevent = usb_phy_uevent,
702 * usb_add_phy_dev - declare the USB PHY
703 * @x: the USB phy to be used; or NULL
705 * This call is exclusively for use by phy drivers, which
706 * coordinate the activities of drivers for host and peripheral
707 * controllers, and in some cases for VBUS current regulation.
709 int usb_add_phy_dev(struct usb_phy *x)
715 dev_err(x->dev, "no device provided for PHY\n");
720 ret = usb_add_extcon(x);
724 x->dev->type = &usb_phy_dev_type;
726 ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier);
728 spin_lock_irqsave(&phy_lock, flags);
729 list_add_tail(&x->head, &phy_list);
730 spin_unlock_irqrestore(&phy_lock, flags);
734 EXPORT_SYMBOL_GPL(usb_add_phy_dev);
737 * usb_remove_phy - remove the OTG PHY
738 * @x: the USB OTG PHY to be removed;
740 * This reverts the effects of usb_add_phy
742 void usb_remove_phy(struct usb_phy *x)
746 spin_lock_irqsave(&phy_lock, flags);
749 spin_unlock_irqrestore(&phy_lock, flags);
751 EXPORT_SYMBOL_GPL(usb_remove_phy);
754 * usb_phy_set_event - set event to phy event
755 * @x: the phy returned by usb_get_phy();
756 * @event: event to set
758 * This sets event to phy event
760 void usb_phy_set_event(struct usb_phy *x, unsigned long event)
762 x->last_event = event;
764 EXPORT_SYMBOL_GPL(usb_phy_set_event);