Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * phy.c -- USB phy handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2004-2013 Texas Instruments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/usb/phy.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) /* Default current range by charger type. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #define DEFAULT_SDP_CUR_MIN	2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #define DEFAULT_SDP_CUR_MAX	500
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define DEFAULT_SDP_CUR_MIN_SS	150
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define DEFAULT_SDP_CUR_MAX_SS	900
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define DEFAULT_DCP_CUR_MIN	500
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define DEFAULT_DCP_CUR_MAX	5000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define DEFAULT_CDP_CUR_MIN	1500
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define DEFAULT_CDP_CUR_MAX	5000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define DEFAULT_ACA_CUR_MIN	1500
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define DEFAULT_ACA_CUR_MAX	5000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) static LIST_HEAD(phy_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static DEFINE_SPINLOCK(phy_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) struct phy_devm {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct usb_phy *phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct notifier_block *nb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static const char *const usb_chger_type[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	[UNKNOWN_TYPE]			= "USB_CHARGER_UNKNOWN_TYPE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	[SDP_TYPE]			= "USB_CHARGER_SDP_TYPE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	[CDP_TYPE]			= "USB_CHARGER_CDP_TYPE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	[DCP_TYPE]			= "USB_CHARGER_DCP_TYPE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	[ACA_TYPE]			= "USB_CHARGER_ACA_TYPE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static struct usb_phy *__usb_find_phy(struct list_head *list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	enum usb_phy_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	struct usb_phy  *phy = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	list_for_each_entry(phy, list, head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		if (phy->type != type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		return phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static struct usb_phy *__of_usb_find_phy(struct device_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	struct usb_phy  *phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (!of_device_is_available(node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	list_for_each_entry(phy, &phy_list, head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		if (node != phy->dev->of_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		return phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	return ERR_PTR(-EPROBE_DEFER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) static void usb_phy_set_default_current(struct usb_phy *usb_phy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	usb_phy->chg_cur.sdp_min = DEFAULT_SDP_CUR_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	usb_phy->chg_cur.sdp_max = DEFAULT_SDP_CUR_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	usb_phy->chg_cur.dcp_min = DEFAULT_DCP_CUR_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	usb_phy->chg_cur.dcp_max = DEFAULT_DCP_CUR_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	usb_phy->chg_cur.cdp_min = DEFAULT_CDP_CUR_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	usb_phy->chg_cur.cdp_max = DEFAULT_CDP_CUR_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	usb_phy->chg_cur.aca_min = DEFAULT_ACA_CUR_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	usb_phy->chg_cur.aca_max = DEFAULT_ACA_CUR_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  * usb_phy_notify_charger_work - notify the USB charger state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  * @work: the charger work to notify the USB charger state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  * This work can be issued when USB charger state has been changed or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94)  * USB charger current has been changed, then we can notify the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95)  * what can be drawn to power user and the charger state to userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)  * If we get the charger type from extcon subsystem, we can notify the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  * charger state to power user automatically by usb_phy_get_charger_type()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  * issuing from extcon subsystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)  * If we get the charger type from ->charger_detect() instead of extcon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)  * subsystem, the usb phy driver should issue usb_phy_set_charger_state()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  * to set charger state when the charger state has been changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static void usb_phy_notify_charger_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	struct usb_phy *usb_phy = container_of(work, struct usb_phy, chg_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	char uchger_state[50] = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	char uchger_type[50] = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	char *envp[] = { uchger_state, uchger_type, NULL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	unsigned int min, max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	switch (usb_phy->chg_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	case USB_CHARGER_PRESENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		usb_phy_get_charger_current(usb_phy, &min, &max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		atomic_notifier_call_chain(&usb_phy->notifier, max, usb_phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		snprintf(uchger_state, ARRAY_SIZE(uchger_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 			 "USB_CHARGER_STATE=%s", "USB_CHARGER_PRESENT");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	case USB_CHARGER_ABSENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		usb_phy_set_default_current(usb_phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		atomic_notifier_call_chain(&usb_phy->notifier, 0, usb_phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		snprintf(uchger_state, ARRAY_SIZE(uchger_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			 "USB_CHARGER_STATE=%s", "USB_CHARGER_ABSENT");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		dev_warn(usb_phy->dev, "Unknown USB charger state: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			 usb_phy->chg_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	snprintf(uchger_type, ARRAY_SIZE(uchger_type),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		 "USB_CHARGER_TYPE=%s", usb_chger_type[usb_phy->chg_type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	kobject_uevent_env(&usb_phy->dev->kobj, KOBJ_CHANGE, envp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static void __usb_phy_get_charger_type(struct usb_phy *usb_phy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_SDP) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		usb_phy->chg_type = SDP_TYPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		usb_phy->chg_state = USB_CHARGER_PRESENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	} else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_CDP) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		usb_phy->chg_type = CDP_TYPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		usb_phy->chg_state = USB_CHARGER_PRESENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	} else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_DCP) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		usb_phy->chg_type = DCP_TYPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		usb_phy->chg_state = USB_CHARGER_PRESENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	} else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_ACA) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		usb_phy->chg_type = ACA_TYPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		usb_phy->chg_state = USB_CHARGER_PRESENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		usb_phy->chg_type = UNKNOWN_TYPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		usb_phy->chg_state = USB_CHARGER_ABSENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	schedule_work(&usb_phy->chg_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)  * usb_phy_get_charger_type - get charger type from extcon subsystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)  * @nb: the notifier block to determine charger type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)  * @state: the cable state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)  * @data: private data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)  * Determin the charger type from extcon subsystem which also means the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  * charger state has been chaned, then we should notify this event.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static int usb_phy_get_charger_type(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 				    unsigned long state, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	struct usb_phy *usb_phy = container_of(nb, struct usb_phy, type_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	__usb_phy_get_charger_type(usb_phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)  * usb_phy_set_charger_current - set the USB charger current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)  * @usb_phy: the USB phy to be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)  * @mA: the current need to be set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)  * Usually we only change the charger default current when USB finished the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)  * enumeration as one SDP charger. As one SDP charger, usb_phy_set_power()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)  * will issue this function to change charger current when after setting USB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)  * configuration, or suspend/resume USB. For other type charger, we should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)  * use the default charger current and we do not suggest to issue this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)  * to change the charger current.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)  * When USB charger current has been changed, we need to notify the power users.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) void usb_phy_set_charger_current(struct usb_phy *usb_phy, unsigned int mA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	switch (usb_phy->chg_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	case SDP_TYPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		if (usb_phy->chg_cur.sdp_max == mA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		usb_phy->chg_cur.sdp_max = (mA > DEFAULT_SDP_CUR_MAX_SS) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			DEFAULT_SDP_CUR_MAX_SS : mA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	case DCP_TYPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		if (usb_phy->chg_cur.dcp_max == mA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		usb_phy->chg_cur.dcp_max = (mA > DEFAULT_DCP_CUR_MAX) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 			DEFAULT_DCP_CUR_MAX : mA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	case CDP_TYPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		if (usb_phy->chg_cur.cdp_max == mA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		usb_phy->chg_cur.cdp_max = (mA > DEFAULT_CDP_CUR_MAX) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			DEFAULT_CDP_CUR_MAX : mA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	case ACA_TYPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		if (usb_phy->chg_cur.aca_max == mA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		usb_phy->chg_cur.aca_max = (mA > DEFAULT_ACA_CUR_MAX) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			DEFAULT_ACA_CUR_MAX : mA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	schedule_work(&usb_phy->chg_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) EXPORT_SYMBOL_GPL(usb_phy_set_charger_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)  * usb_phy_get_charger_current - get the USB charger current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)  * @usb_phy: the USB phy to be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)  * @min: the minimum current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)  * @max: the maximum current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)  * Usually we will notify the maximum current to power user, but for some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)  * special case, power user also need the minimum current value. Then the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)  * power user can issue this function to get the suitable current.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) void usb_phy_get_charger_current(struct usb_phy *usb_phy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 				 unsigned int *min, unsigned int *max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	switch (usb_phy->chg_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	case SDP_TYPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		*min = usb_phy->chg_cur.sdp_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		*max = usb_phy->chg_cur.sdp_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	case DCP_TYPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		*min = usb_phy->chg_cur.dcp_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		*max = usb_phy->chg_cur.dcp_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	case CDP_TYPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		*min = usb_phy->chg_cur.cdp_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		*max = usb_phy->chg_cur.cdp_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	case ACA_TYPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		*min = usb_phy->chg_cur.aca_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		*max = usb_phy->chg_cur.aca_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		*min = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		*max = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) EXPORT_SYMBOL_GPL(usb_phy_get_charger_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)  * usb_phy_set_charger_state - set the USB charger state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)  * @usb_phy: the USB phy to be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)  * @state: the new state need to be set for charger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)  * The usb phy driver can issue this function when the usb phy driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)  * detected the charger state has been changed, in this case the charger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)  * type should be get from ->charger_detect().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) void usb_phy_set_charger_state(struct usb_phy *usb_phy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 			       enum usb_charger_state state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	if (usb_phy->chg_state == state || !usb_phy->charger_detect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	usb_phy->chg_state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	if (usb_phy->chg_state == USB_CHARGER_PRESENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		usb_phy->chg_type = usb_phy->charger_detect(usb_phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		usb_phy->chg_type = UNKNOWN_TYPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	schedule_work(&usb_phy->chg_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) EXPORT_SYMBOL_GPL(usb_phy_set_charger_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) static void devm_usb_phy_release(struct device *dev, void *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	struct usb_phy *phy = *(struct usb_phy **)res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	usb_put_phy(phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static void devm_usb_phy_release2(struct device *dev, void *_res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	struct phy_devm *res = _res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	if (res->nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		usb_unregister_notifier(res->phy, res->nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	usb_put_phy(res->phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) static int devm_usb_phy_match(struct device *dev, void *res, void *match_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	struct usb_phy **phy = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	return *phy == match_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) static void usb_charger_init(struct usb_phy *usb_phy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	usb_phy->chg_type = UNKNOWN_TYPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	usb_phy->chg_state = USB_CHARGER_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	usb_phy_set_default_current(usb_phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	INIT_WORK(&usb_phy->chg_work, usb_phy_notify_charger_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static int usb_add_extcon(struct usb_phy *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	if (of_property_read_bool(x->dev->of_node, "extcon")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		x->edev = extcon_get_edev_by_phandle(x->dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		if (IS_ERR(x->edev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 			return PTR_ERR(x->edev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		x->id_edev = extcon_get_edev_by_phandle(x->dev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		if (IS_ERR(x->id_edev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 			x->id_edev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 			dev_info(x->dev, "No separate ID extcon device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		if (x->vbus_nb.notifier_call) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			ret = devm_extcon_register_notifier(x->dev, x->edev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 							    EXTCON_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 							    &x->vbus_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 			if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 				dev_err(x->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 					"register VBUS notifier failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			x->type_nb.notifier_call = usb_phy_get_charger_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 			ret = devm_extcon_register_notifier(x->dev, x->edev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 							    EXTCON_CHG_USB_SDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 							    &x->type_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 				dev_err(x->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 					"register extcon USB SDP failed.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 			ret = devm_extcon_register_notifier(x->dev, x->edev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 							    EXTCON_CHG_USB_CDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 							    &x->type_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 				dev_err(x->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 					"register extcon USB CDP failed.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 			ret = devm_extcon_register_notifier(x->dev, x->edev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 							    EXTCON_CHG_USB_DCP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 							    &x->type_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 				dev_err(x->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 					"register extcon USB DCP failed.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 			ret = devm_extcon_register_notifier(x->dev, x->edev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 							    EXTCON_CHG_USB_ACA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 							    &x->type_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 				dev_err(x->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 					"register extcon USB ACA failed.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		if (x->id_nb.notifier_call) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 			struct extcon_dev *id_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 			if (x->id_edev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 				id_ext = x->id_edev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 				id_ext = x->edev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 			ret = devm_extcon_register_notifier(x->dev, id_ext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 							    EXTCON_USB_HOST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 							    &x->id_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 			if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 				dev_err(x->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 					"register ID notifier failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	if (x->type_nb.notifier_call)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		__usb_phy_get_charger_type(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)  * devm_usb_get_phy - find the USB PHY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)  * @dev: device that requests this phy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)  * @type: the type of the phy the controller requires
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)  * Gets the phy using usb_get_phy(), and associates a device with it using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)  * devres. On driver detach, release function is invoked on the devres data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)  * then, devres data is freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)  * For use by USB host and peripheral drivers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	struct usb_phy **ptr, *phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	if (!ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	phy = usb_get_phy(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	if (!IS_ERR(phy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		*ptr = phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		devres_add(dev, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		devres_free(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	return phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) EXPORT_SYMBOL_GPL(devm_usb_get_phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)  * usb_get_phy - find the USB PHY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)  * @type: the type of the phy the controller requires
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)  * Returns the phy driver, after getting a refcount to it; or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)  * -ENODEV if there is no such phy.  The caller is responsible for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)  * calling usb_put_phy() to release that count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)  * For use by USB host and peripheral drivers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) struct usb_phy *usb_get_phy(enum usb_phy_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	struct usb_phy	*phy = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	unsigned long	flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	spin_lock_irqsave(&phy_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	phy = __usb_find_phy(&phy_list, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		pr_debug("PHY: unable to find transceiver of type %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 			usb_phy_type_string(type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		if (!IS_ERR(phy))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 			phy = ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		goto err0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	get_device(phy->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) err0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	spin_unlock_irqrestore(&phy_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	return phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) EXPORT_SYMBOL_GPL(usb_get_phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)  * devm_usb_get_phy_by_node - find the USB PHY by device_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)  * @dev: device that requests this phy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)  * @node: the device_node for the phy device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)  * @nb: a notifier_block to register with the phy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)  * Returns the phy driver associated with the given device_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)  * after getting a refcount to it, -ENODEV if there is no such phy or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)  * -EPROBE_DEFER if the device is not yet loaded. While at that, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)  * also associates the device with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)  * the phy using devres. On driver detach, release function is invoked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)  * on the devres data, then, devres data is freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)  * For use by peripheral drivers for devices related to a phy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)  * such as a charger.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) struct  usb_phy *devm_usb_get_phy_by_node(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 					  struct device_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 					  struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	struct usb_phy	*phy = ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	struct phy_devm	*ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	unsigned long	flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	ptr = devres_alloc(devm_usb_phy_release2, sizeof(*ptr), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	if (!ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		dev_dbg(dev, "failed to allocate memory for devres\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		goto err0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	spin_lock_irqsave(&phy_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	phy = __of_usb_find_phy(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	if (IS_ERR(phy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		devres_free(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		goto err1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	if (!try_module_get(phy->dev->driver->owner)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		phy = ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		devres_free(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		goto err1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	if (nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		usb_register_notifier(phy, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	ptr->phy = phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	ptr->nb = nb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	devres_add(dev, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	get_device(phy->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) err1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	spin_unlock_irqrestore(&phy_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) err0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	return phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)  * devm_usb_get_phy_by_phandle - find the USB PHY by phandle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)  * @dev: device that requests this phy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)  * @phandle: name of the property holding the phy phandle value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)  * @index: the index of the phy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)  * Returns the phy driver associated with the given phandle value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)  * after getting a refcount to it, -ENODEV if there is no such phy or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)  * -EPROBE_DEFER if there is a phandle to the phy, but the device is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)  * not yet loaded. While at that, it also associates the device with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)  * the phy using devres. On driver detach, release function is invoked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)  * on the devres data, then, devres data is freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)  * For use by USB host and peripheral drivers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	const char *phandle, u8 index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	struct device_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	struct usb_phy	*phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	if (!dev->of_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 		dev_dbg(dev, "device does not have a device node entry\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	node = of_parse_phandle(dev->of_node, phandle, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	if (!node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 		dev_dbg(dev, "failed to get %s phandle in %pOF node\n", phandle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 			dev->of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 		return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	phy = devm_usb_get_phy_by_node(dev, node, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	of_node_put(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	return phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_phandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)  * devm_usb_put_phy - release the USB PHY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)  * @dev: device that wants to release this phy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)  * @phy: the phy returned by devm_usb_get_phy()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)  * destroys the devres associated with this phy and invokes usb_put_phy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)  * to release the phy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)  * For use by USB host and peripheral drivers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) void devm_usb_put_phy(struct device *dev, struct usb_phy *phy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) EXPORT_SYMBOL_GPL(devm_usb_put_phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)  * usb_put_phy - release the USB PHY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)  * @x: the phy returned by usb_get_phy()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)  * Releases a refcount the caller received from usb_get_phy().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)  * For use by USB host and peripheral drivers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) void usb_put_phy(struct usb_phy *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	if (x) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 		struct module *owner = x->dev->driver->owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 		put_device(x->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 		module_put(owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) EXPORT_SYMBOL_GPL(usb_put_phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)  * usb_add_phy: declare the USB PHY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)  * @x: the USB phy to be used; or NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)  * @type: the type of this PHY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)  * This call is exclusively for use by phy drivers, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623)  * coordinate the activities of drivers for host and peripheral
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)  * controllers, and in some cases for VBUS current regulation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 	int		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	unsigned long	flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	struct usb_phy	*phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	if (x->type != USB_PHY_TYPE_UNDEFINED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 		dev_err(x->dev, "not accepting initialized PHY %s\n", x->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	usb_charger_init(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	ret = usb_add_extcon(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	spin_lock_irqsave(&phy_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	list_for_each_entry(phy, &phy_list, head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 		if (phy->type == type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 			ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 			dev_err(x->dev, "transceiver type %s already exists\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 						usb_phy_type_string(type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	x->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	list_add_tail(&x->head, &phy_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 	spin_unlock_irqrestore(&phy_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) EXPORT_SYMBOL_GPL(usb_add_phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)  * usb_add_phy_dev - declare the USB PHY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)  * @x: the USB phy to be used; or NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)  * This call is exclusively for use by phy drivers, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)  * coordinate the activities of drivers for host and peripheral
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670)  * controllers, and in some cases for VBUS current regulation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) int usb_add_phy_dev(struct usb_phy *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	if (!x->dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 		dev_err(x->dev, "no device provided for PHY\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	usb_charger_init(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	ret = usb_add_extcon(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	spin_lock_irqsave(&phy_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	list_add_tail(&x->head, &phy_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	spin_unlock_irqrestore(&phy_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) EXPORT_SYMBOL_GPL(usb_add_phy_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)  * usb_remove_phy - remove the OTG PHY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)  * @x: the USB OTG PHY to be removed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701)  * This reverts the effects of usb_add_phy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) void usb_remove_phy(struct usb_phy *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	unsigned long	flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 	spin_lock_irqsave(&phy_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 	if (x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 		list_del(&x->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	spin_unlock_irqrestore(&phy_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) EXPORT_SYMBOL_GPL(usb_remove_phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715)  * usb_phy_set_event - set event to phy event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)  * @x: the phy returned by usb_get_phy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)  * @event: event to set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719)  * This sets event to phy event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) void usb_phy_set_event(struct usb_phy *x, unsigned long event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	x->last_event = event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) EXPORT_SYMBOL_GPL(usb_phy_set_event);