2 * phy.c -- USB phy handling
4 * Copyright (C) 2004-2013 Texas Instruments
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 #include <linux/kernel.h>
12 #include <linux/export.h>
13 #include <linux/err.h>
14 #include <linux/device.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
19 #include <linux/usb/phy.h>
21 /* Default current range by charger type. */
22 #define DEFAULT_SDP_CUR_MIN 2
23 #define DEFAULT_SDP_CUR_MAX 500
24 #define DEFAULT_SDP_CUR_MIN_SS 150
25 #define DEFAULT_SDP_CUR_MAX_SS 900
26 #define DEFAULT_DCP_CUR_MIN 500
27 #define DEFAULT_DCP_CUR_MAX 5000
28 #define DEFAULT_CDP_CUR_MIN 1500
29 #define DEFAULT_CDP_CUR_MAX 5000
30 #define DEFAULT_ACA_CUR_MIN 1500
31 #define DEFAULT_ACA_CUR_MAX 5000
33 static LIST_HEAD(phy_list);
34 static LIST_HEAD(phy_bind_list);
35 static DEFINE_SPINLOCK(phy_lock);
39 struct notifier_block *nb;
42 static struct usb_phy *__usb_find_phy(struct list_head *list,
43 enum usb_phy_type type)
45 struct usb_phy *phy = NULL;
47 list_for_each_entry(phy, list, head) {
48 if (phy->type != type)
54 return ERR_PTR(-ENODEV);
57 static struct usb_phy *__usb_find_phy_dev(struct device *dev,
58 struct list_head *list, u8 index)
60 struct usb_phy_bind *phy_bind = NULL;
62 list_for_each_entry(phy_bind, list, list) {
63 if (!(strcmp(phy_bind->dev_name, dev_name(dev))) &&
64 phy_bind->index == index) {
68 return ERR_PTR(-EPROBE_DEFER);
72 return ERR_PTR(-ENODEV);
75 static struct usb_phy *__of_usb_find_phy(struct device_node *node)
79 if (!of_device_is_available(node))
80 return ERR_PTR(-ENODEV);
82 list_for_each_entry(phy, &phy_list, head) {
83 if (node != phy->dev->of_node)
89 return ERR_PTR(-EPROBE_DEFER);
92 static void usb_phy_set_default_current(struct usb_phy *usb_phy)
94 usb_phy->chg_cur.sdp_min = DEFAULT_SDP_CUR_MIN;
95 usb_phy->chg_cur.sdp_max = DEFAULT_SDP_CUR_MAX;
96 usb_phy->chg_cur.dcp_min = DEFAULT_DCP_CUR_MIN;
97 usb_phy->chg_cur.dcp_max = DEFAULT_DCP_CUR_MAX;
98 usb_phy->chg_cur.cdp_min = DEFAULT_CDP_CUR_MIN;
99 usb_phy->chg_cur.cdp_max = DEFAULT_CDP_CUR_MAX;
100 usb_phy->chg_cur.aca_min = DEFAULT_ACA_CUR_MIN;
101 usb_phy->chg_cur.aca_max = DEFAULT_ACA_CUR_MAX;
105 * usb_phy_notify_charger_work - notify the USB charger state
106 * @work - the charger work to notify the USB charger state
108 * This work can be issued when USB charger state has been changed or
109 * USB charger current has been changed, then we can notify the current
110 * what can be drawn to power user and the charger state to userspace.
112 * If we get the charger type from extcon subsystem, we can notify the
113 * charger state to power user automatically by usb_phy_get_charger_type()
114 * issuing from extcon subsystem.
116 * If we get the charger type from ->charger_detect() instead of extcon
117 * subsystem, the usb phy driver should issue usb_phy_set_charger_state()
118 * to set charger state when the charger state has been changed.
120 static void usb_phy_notify_charger_work(struct work_struct *work)
122 struct usb_phy *usb_phy = container_of(work, struct usb_phy, chg_work);
123 char uchger_state[50] = { 0 };
124 char *envp[] = { uchger_state, NULL };
125 unsigned int min, max;
127 switch (usb_phy->chg_state) {
128 case USB_CHARGER_PRESENT:
129 usb_phy_get_charger_current(usb_phy, &min, &max);
131 atomic_notifier_call_chain(&usb_phy->notifier, max, usb_phy);
132 snprintf(uchger_state, ARRAY_SIZE(uchger_state),
133 "USB_CHARGER_STATE=%s", "USB_CHARGER_PRESENT");
135 case USB_CHARGER_ABSENT:
136 usb_phy_set_default_current(usb_phy);
138 atomic_notifier_call_chain(&usb_phy->notifier, 0, usb_phy);
139 snprintf(uchger_state, ARRAY_SIZE(uchger_state),
140 "USB_CHARGER_STATE=%s", "USB_CHARGER_ABSENT");
143 dev_warn(usb_phy->dev, "Unknown USB charger state: %d\n",
148 kobject_uevent_env(&usb_phy->dev->kobj, KOBJ_CHANGE, envp);
151 static void __usb_phy_get_charger_type(struct usb_phy *usb_phy)
153 if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_SDP) > 0) {
154 usb_phy->chg_type = SDP_TYPE;
155 usb_phy->chg_state = USB_CHARGER_PRESENT;
156 } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_CDP) > 0) {
157 usb_phy->chg_type = CDP_TYPE;
158 usb_phy->chg_state = USB_CHARGER_PRESENT;
159 } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_DCP) > 0) {
160 usb_phy->chg_type = DCP_TYPE;
161 usb_phy->chg_state = USB_CHARGER_PRESENT;
162 } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_ACA) > 0) {
163 usb_phy->chg_type = ACA_TYPE;
164 usb_phy->chg_state = USB_CHARGER_PRESENT;
166 usb_phy->chg_type = UNKNOWN_TYPE;
167 usb_phy->chg_state = USB_CHARGER_ABSENT;
170 schedule_work(&usb_phy->chg_work);
174 * usb_phy_get_charger_type - get charger type from extcon subsystem
175 * @nb -the notifier block to determine charger type
176 * @state - the cable state
177 * @data - private data
179 * Determin the charger type from extcon subsystem which also means the
180 * charger state has been chaned, then we should notify this event.
182 static int usb_phy_get_charger_type(struct notifier_block *nb,
183 unsigned long state, void *data)
185 struct usb_phy *usb_phy = container_of(nb, struct usb_phy, type_nb);
187 __usb_phy_get_charger_type(usb_phy);
192 * usb_phy_set_charger_current - set the USB charger current
193 * @usb_phy - the USB phy to be used
194 * @mA - the current need to be set
196 * Usually we only change the charger default current when USB finished the
197 * enumeration as one SDP charger. As one SDP charger, usb_phy_set_power()
198 * will issue this function to change charger current when after setting USB
199 * configuration, or suspend/resume USB. For other type charger, we should
200 * use the default charger current and we do not suggest to issue this function
201 * to change the charger current.
203 * When USB charger current has been changed, we need to notify the power users.
205 void usb_phy_set_charger_current(struct usb_phy *usb_phy, unsigned int mA)
207 switch (usb_phy->chg_type) {
209 if (usb_phy->chg_cur.sdp_max == mA)
212 usb_phy->chg_cur.sdp_max = (mA > DEFAULT_SDP_CUR_MAX_SS) ?
213 DEFAULT_SDP_CUR_MAX_SS : mA;
216 if (usb_phy->chg_cur.dcp_max == mA)
219 usb_phy->chg_cur.dcp_max = (mA > DEFAULT_DCP_CUR_MAX) ?
220 DEFAULT_DCP_CUR_MAX : mA;
223 if (usb_phy->chg_cur.cdp_max == mA)
226 usb_phy->chg_cur.cdp_max = (mA > DEFAULT_CDP_CUR_MAX) ?
227 DEFAULT_CDP_CUR_MAX : mA;
230 if (usb_phy->chg_cur.aca_max == mA)
233 usb_phy->chg_cur.aca_max = (mA > DEFAULT_ACA_CUR_MAX) ?
234 DEFAULT_ACA_CUR_MAX : mA;
240 schedule_work(&usb_phy->chg_work);
242 EXPORT_SYMBOL_GPL(usb_phy_set_charger_current);
245 * usb_phy_get_charger_current - get the USB charger current
246 * @usb_phy - the USB phy to be used
247 * @min - the minimum current
248 * @max - the maximum current
250 * Usually we will notify the maximum current to power user, but for some
251 * special case, power user also need the minimum current value. Then the
252 * power user can issue this function to get the suitable current.
254 void usb_phy_get_charger_current(struct usb_phy *usb_phy,
255 unsigned int *min, unsigned int *max)
257 switch (usb_phy->chg_type) {
259 *min = usb_phy->chg_cur.sdp_min;
260 *max = usb_phy->chg_cur.sdp_max;
263 *min = usb_phy->chg_cur.dcp_min;
264 *max = usb_phy->chg_cur.dcp_max;
267 *min = usb_phy->chg_cur.cdp_min;
268 *max = usb_phy->chg_cur.cdp_max;
271 *min = usb_phy->chg_cur.aca_min;
272 *max = usb_phy->chg_cur.aca_max;
280 EXPORT_SYMBOL_GPL(usb_phy_get_charger_current);
283 * usb_phy_set_charger_state - set the USB charger state
284 * @usb_phy - the USB phy to be used
285 * @state - the new state need to be set for charger
287 * The usb phy driver can issue this function when the usb phy driver
288 * detected the charger state has been changed, in this case the charger
289 * type should be get from ->charger_detect().
291 void usb_phy_set_charger_state(struct usb_phy *usb_phy,
292 enum usb_charger_state state)
294 if (usb_phy->chg_state == state || !usb_phy->charger_detect)
297 usb_phy->chg_state = state;
298 if (usb_phy->chg_state == USB_CHARGER_PRESENT)
299 usb_phy->chg_type = usb_phy->charger_detect(usb_phy);
301 usb_phy->chg_type = UNKNOWN_TYPE;
303 schedule_work(&usb_phy->chg_work);
305 EXPORT_SYMBOL_GPL(usb_phy_set_charger_state);
307 static void devm_usb_phy_release(struct device *dev, void *res)
309 struct usb_phy *phy = *(struct usb_phy **)res;
314 static void devm_usb_phy_release2(struct device *dev, void *_res)
316 struct phy_devm *res = _res;
319 usb_unregister_notifier(res->phy, res->nb);
320 usb_put_phy(res->phy);
323 static int devm_usb_phy_match(struct device *dev, void *res, void *match_data)
325 struct usb_phy **phy = res;
327 return *phy == match_data;
330 static int usb_add_extcon(struct usb_phy *x)
334 if (of_property_read_bool(x->dev->of_node, "extcon")) {
335 x->edev = extcon_get_edev_by_phandle(x->dev, 0);
337 return PTR_ERR(x->edev);
339 x->id_edev = extcon_get_edev_by_phandle(x->dev, 1);
340 if (IS_ERR(x->id_edev)) {
342 dev_info(x->dev, "No separate ID extcon device\n");
345 if (x->vbus_nb.notifier_call) {
346 ret = devm_extcon_register_notifier(x->dev, x->edev,
351 "register VBUS notifier failed\n");
355 x->type_nb.notifier_call = usb_phy_get_charger_type;
357 ret = devm_extcon_register_notifier(x->dev, x->edev,
362 "register extcon USB SDP failed.\n");
366 ret = devm_extcon_register_notifier(x->dev, x->edev,
371 "register extcon USB CDP failed.\n");
375 ret = devm_extcon_register_notifier(x->dev, x->edev,
380 "register extcon USB DCP failed.\n");
384 ret = devm_extcon_register_notifier(x->dev, x->edev,
389 "register extcon USB ACA failed.\n");
394 if (x->id_nb.notifier_call) {
395 struct extcon_dev *id_ext;
402 ret = devm_extcon_register_notifier(x->dev, id_ext,
407 "register ID notifier failed\n");
413 usb_phy_set_default_current(x);
414 INIT_WORK(&x->chg_work, usb_phy_notify_charger_work);
415 x->chg_type = UNKNOWN_TYPE;
416 x->chg_state = USB_CHARGER_DEFAULT;
417 if (x->type_nb.notifier_call)
418 __usb_phy_get_charger_type(x);
424 * devm_usb_get_phy - find the USB PHY
425 * @dev - device that requests this phy
426 * @type - the type of the phy the controller requires
428 * Gets the phy using usb_get_phy(), and associates a device with it using
429 * devres. On driver detach, release function is invoked on the devres data,
430 * then, devres data is freed.
432 * For use by USB host and peripheral drivers.
434 struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type)
436 struct usb_phy **ptr, *phy;
438 ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
440 return ERR_PTR(-ENOMEM);
442 phy = usb_get_phy(type);
445 devres_add(dev, ptr);
451 EXPORT_SYMBOL_GPL(devm_usb_get_phy);
454 * usb_get_phy - find the USB PHY
455 * @type - the type of the phy the controller requires
457 * Returns the phy driver, after getting a refcount to it; or
458 * -ENODEV if there is no such phy. The caller is responsible for
459 * calling usb_put_phy() to release that count.
461 * For use by USB host and peripheral drivers.
463 struct usb_phy *usb_get_phy(enum usb_phy_type type)
465 struct usb_phy *phy = NULL;
468 spin_lock_irqsave(&phy_lock, flags);
470 phy = __usb_find_phy(&phy_list, type);
471 if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) {
472 pr_debug("PHY: unable to find transceiver of type %s\n",
473 usb_phy_type_string(type));
475 phy = ERR_PTR(-ENODEV);
480 get_device(phy->dev);
483 spin_unlock_irqrestore(&phy_lock, flags);
487 EXPORT_SYMBOL_GPL(usb_get_phy);
490 * devm_usb_get_phy_by_node - find the USB PHY by device_node
491 * @dev - device that requests this phy
492 * @node - the device_node for the phy device.
493 * @nb - a notifier_block to register with the phy.
495 * Returns the phy driver associated with the given device_node,
496 * after getting a refcount to it, -ENODEV if there is no such phy or
497 * -EPROBE_DEFER if the device is not yet loaded. While at that, it
498 * also associates the device with
499 * the phy using devres. On driver detach, release function is invoked
500 * on the devres data, then, devres data is freed.
502 * For use by peripheral drivers for devices related to a phy,
505 struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
506 struct device_node *node,
507 struct notifier_block *nb)
509 struct usb_phy *phy = ERR_PTR(-ENOMEM);
510 struct phy_devm *ptr;
513 ptr = devres_alloc(devm_usb_phy_release2, sizeof(*ptr), GFP_KERNEL);
515 dev_dbg(dev, "failed to allocate memory for devres\n");
519 spin_lock_irqsave(&phy_lock, flags);
521 phy = __of_usb_find_phy(node);
527 if (!try_module_get(phy->dev->driver->owner)) {
528 phy = ERR_PTR(-ENODEV);
533 usb_register_notifier(phy, nb);
536 devres_add(dev, ptr);
538 get_device(phy->dev);
541 spin_unlock_irqrestore(&phy_lock, flags);
547 EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_node);
550 * devm_usb_get_phy_by_phandle - find the USB PHY by phandle
551 * @dev - device that requests this phy
552 * @phandle - name of the property holding the phy phandle value
553 * @index - the index of the phy
555 * Returns the phy driver associated with the given phandle value,
556 * after getting a refcount to it, -ENODEV if there is no such phy or
557 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
558 * not yet loaded. While at that, it also associates the device with
559 * the phy using devres. On driver detach, release function is invoked
560 * on the devres data, then, devres data is freed.
562 * For use by USB host and peripheral drivers.
564 struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
565 const char *phandle, u8 index)
567 struct device_node *node;
571 dev_dbg(dev, "device does not have a device node entry\n");
572 return ERR_PTR(-EINVAL);
575 node = of_parse_phandle(dev->of_node, phandle, index);
577 dev_dbg(dev, "failed to get %s phandle in %pOF node\n", phandle,
579 return ERR_PTR(-ENODEV);
581 phy = devm_usb_get_phy_by_node(dev, node, NULL);
585 EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_phandle);
588 * usb_get_phy_dev - find the USB PHY
589 * @dev - device that requests this phy
590 * @index - the index of the phy
592 * Returns the phy driver, after getting a refcount to it; or
593 * -ENODEV if there is no such phy. The caller is responsible for
594 * calling usb_put_phy() to release that count.
596 * For use by USB host and peripheral drivers.
598 struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
600 struct usb_phy *phy = NULL;
603 spin_lock_irqsave(&phy_lock, flags);
605 phy = __usb_find_phy_dev(dev, &phy_bind_list, index);
606 if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) {
607 dev_dbg(dev, "unable to find transceiver\n");
609 phy = ERR_PTR(-ENODEV);
614 get_device(phy->dev);
617 spin_unlock_irqrestore(&phy_lock, flags);
621 EXPORT_SYMBOL_GPL(usb_get_phy_dev);
624 * devm_usb_get_phy_dev - find the USB PHY using device ptr and index
625 * @dev - device that requests this phy
626 * @index - the index of the phy
628 * Gets the phy using usb_get_phy_dev(), and associates a device with it using
629 * devres. On driver detach, release function is invoked on the devres data,
630 * then, devres data is freed.
632 * For use by USB host and peripheral drivers.
634 struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
636 struct usb_phy **ptr, *phy;
638 ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
642 phy = usb_get_phy_dev(dev, index);
645 devres_add(dev, ptr);
651 EXPORT_SYMBOL_GPL(devm_usb_get_phy_dev);
654 * devm_usb_put_phy - release the USB PHY
655 * @dev - device that wants to release this phy
656 * @phy - the phy returned by devm_usb_get_phy()
658 * destroys the devres associated with this phy and invokes usb_put_phy
659 * to release the phy.
661 * For use by USB host and peripheral drivers.
663 void devm_usb_put_phy(struct device *dev, struct usb_phy *phy)
667 r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy);
668 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
670 EXPORT_SYMBOL_GPL(devm_usb_put_phy);
673 * usb_put_phy - release the USB PHY
674 * @x: the phy returned by usb_get_phy()
676 * Releases a refcount the caller received from usb_get_phy().
678 * For use by USB host and peripheral drivers.
680 void usb_put_phy(struct usb_phy *x)
683 struct module *owner = x->dev->driver->owner;
689 EXPORT_SYMBOL_GPL(usb_put_phy);
692 * usb_add_phy - declare the USB PHY
693 * @x: the USB phy to be used; or NULL
694 * @type - the type of this PHY
696 * This call is exclusively for use by phy drivers, which
697 * coordinate the activities of drivers for host and peripheral
698 * controllers, and in some cases for VBUS current regulation.
700 int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
706 if (x->type != USB_PHY_TYPE_UNDEFINED) {
707 dev_err(x->dev, "not accepting initialized PHY %s\n", x->label);
711 ret = usb_add_extcon(x);
715 ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier);
717 spin_lock_irqsave(&phy_lock, flags);
719 list_for_each_entry(phy, &phy_list, head) {
720 if (phy->type == type) {
722 dev_err(x->dev, "transceiver type %s already exists\n",
723 usb_phy_type_string(type));
729 list_add_tail(&x->head, &phy_list);
732 spin_unlock_irqrestore(&phy_lock, flags);
735 EXPORT_SYMBOL_GPL(usb_add_phy);
738 * usb_add_phy_dev - declare the USB PHY
739 * @x: the USB phy to be used; or NULL
741 * This call is exclusively for use by phy drivers, which
742 * coordinate the activities of drivers for host and peripheral
743 * controllers, and in some cases for VBUS current regulation.
745 int usb_add_phy_dev(struct usb_phy *x)
747 struct usb_phy_bind *phy_bind;
752 dev_err(x->dev, "no device provided for PHY\n");
756 ret = usb_add_extcon(x);
760 ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier);
762 spin_lock_irqsave(&phy_lock, flags);
763 list_for_each_entry(phy_bind, &phy_bind_list, list)
764 if (!(strcmp(phy_bind->phy_dev_name, dev_name(x->dev))))
767 list_add_tail(&x->head, &phy_list);
769 spin_unlock_irqrestore(&phy_lock, flags);
772 EXPORT_SYMBOL_GPL(usb_add_phy_dev);
775 * usb_remove_phy - remove the OTG PHY
776 * @x: the USB OTG PHY to be removed;
778 * This reverts the effects of usb_add_phy
780 void usb_remove_phy(struct usb_phy *x)
783 struct usb_phy_bind *phy_bind;
785 spin_lock_irqsave(&phy_lock, flags);
787 list_for_each_entry(phy_bind, &phy_bind_list, list)
788 if (phy_bind->phy == x)
789 phy_bind->phy = NULL;
792 spin_unlock_irqrestore(&phy_lock, flags);
794 EXPORT_SYMBOL_GPL(usb_remove_phy);
797 * usb_bind_phy - bind the phy and the controller that uses the phy
798 * @dev_name: the device name of the device that will bind to the phy
799 * @index: index to specify the port number
800 * @phy_dev_name: the device name of the phy
802 * Fills the phy_bind structure with the dev_name and phy_dev_name. This will
803 * be used when the phy driver registers the phy and when the controller
806 * To be used by platform specific initialization code.
808 int usb_bind_phy(const char *dev_name, u8 index,
809 const char *phy_dev_name)
811 struct usb_phy_bind *phy_bind;
814 phy_bind = kzalloc(sizeof(*phy_bind), GFP_KERNEL);
818 phy_bind->dev_name = dev_name;
819 phy_bind->phy_dev_name = phy_dev_name;
820 phy_bind->index = index;
822 spin_lock_irqsave(&phy_lock, flags);
823 list_add_tail(&phy_bind->list, &phy_bind_list);
824 spin_unlock_irqrestore(&phy_lock, flags);
828 EXPORT_SYMBOL_GPL(usb_bind_phy);
831 * usb_phy_set_event - set event to phy event
832 * @x: the phy returned by usb_get_phy();
834 * This sets event to phy event
836 void usb_phy_set_event(struct usb_phy *x, unsigned long event)
838 x->last_event = event;
840 EXPORT_SYMBOL_GPL(usb_phy_set_event);