Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usb: phy: Add USB charger support

This patch introduces the usb charger support based on usb phy that
makes an enhancement to a power driver. The basic conception of the
usb charger is that, when one usb charger is added or removed by
reporting from the extcon device state change, the usb charger will
report to power user to set the current limitation.

Power user can register a notifiee on the usb phy by issuing
usb_register_notifier() to get notified by charger status changes
or charger current changes.

we can notify what current to be drawn to power user according to
different charger type, and now we have 2 methods to get charger type.
One is get charger type from extcon subsystem, which also means the
charger state changes. Another is we can get the charger type from
USB controller detecting or PMIC detecting, and the charger state
changes should be told by issuing usb_phy_set_charger_state().

Signed-off-by: Baolin Wang <baolin.wang@linaro.org>
Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>

authored by

Baolin Wang and committed by
Felipe Balbi
a9081a00 44dd8a98

+321
+272
drivers/usb/phy/phy.c
··· 18 18 19 19 #include <linux/usb/phy.h> 20 20 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 32 + 21 33 static LIST_HEAD(phy_list); 22 34 static LIST_HEAD(phy_bind_list); 23 35 static DEFINE_SPINLOCK(phy_lock); ··· 89 77 return ERR_PTR(-EPROBE_DEFER); 90 78 } 91 79 80 + static void usb_phy_set_default_current(struct usb_phy *usb_phy) 81 + { 82 + usb_phy->chg_cur.sdp_min = DEFAULT_SDP_CUR_MIN; 83 + usb_phy->chg_cur.sdp_max = DEFAULT_SDP_CUR_MAX; 84 + usb_phy->chg_cur.dcp_min = DEFAULT_DCP_CUR_MIN; 85 + usb_phy->chg_cur.dcp_max = DEFAULT_DCP_CUR_MAX; 86 + usb_phy->chg_cur.cdp_min = DEFAULT_CDP_CUR_MIN; 87 + usb_phy->chg_cur.cdp_max = DEFAULT_CDP_CUR_MAX; 88 + usb_phy->chg_cur.aca_min = DEFAULT_ACA_CUR_MIN; 89 + usb_phy->chg_cur.aca_max = DEFAULT_ACA_CUR_MAX; 90 + } 91 + 92 + /** 93 + * usb_phy_notify_charger_work - notify the USB charger state 94 + * @work - the charger work to notify the USB charger state 95 + * 96 + * This work can be issued when USB charger state has been changed or 97 + * USB charger current has been changed, then we can notify the current 98 + * what can be drawn to power user and the charger state to userspace. 99 + * 100 + * If we get the charger type from extcon subsystem, we can notify the 101 + * charger state to power user automatically by usb_phy_get_charger_type() 102 + * issuing from extcon subsystem. 103 + * 104 + * If we get the charger type from ->charger_detect() instead of extcon 105 + * subsystem, the usb phy driver should issue usb_phy_set_charger_state() 106 + * to set charger state when the charger state has been changed. 107 + */ 108 + static void usb_phy_notify_charger_work(struct work_struct *work) 109 + { 110 + struct usb_phy *usb_phy = container_of(work, struct usb_phy, chg_work); 111 + char uchger_state[50] = { 0 }; 112 + char *envp[] = { uchger_state, NULL }; 113 + unsigned int min, max; 114 + 115 + switch (usb_phy->chg_state) { 116 + case USB_CHARGER_PRESENT: 117 + usb_phy_get_charger_current(usb_phy, &min, &max); 118 + 119 + atomic_notifier_call_chain(&usb_phy->notifier, max, usb_phy); 120 + snprintf(uchger_state, ARRAY_SIZE(uchger_state), 121 + "USB_CHARGER_STATE=%s", "USB_CHARGER_PRESENT"); 122 + break; 123 + case USB_CHARGER_ABSENT: 124 + usb_phy_set_default_current(usb_phy); 125 + 126 + atomic_notifier_call_chain(&usb_phy->notifier, 0, usb_phy); 127 + snprintf(uchger_state, ARRAY_SIZE(uchger_state), 128 + "USB_CHARGER_STATE=%s", "USB_CHARGER_ABSENT"); 129 + break; 130 + default: 131 + dev_warn(usb_phy->dev, "Unknown USB charger state: %d\n", 132 + usb_phy->chg_state); 133 + return; 134 + } 135 + 136 + kobject_uevent_env(&usb_phy->dev->kobj, KOBJ_CHANGE, envp); 137 + } 138 + 139 + static void __usb_phy_get_charger_type(struct usb_phy *usb_phy) 140 + { 141 + if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_SDP) > 0) { 142 + usb_phy->chg_type = SDP_TYPE; 143 + usb_phy->chg_state = USB_CHARGER_PRESENT; 144 + } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_CDP) > 0) { 145 + usb_phy->chg_type = CDP_TYPE; 146 + usb_phy->chg_state = USB_CHARGER_PRESENT; 147 + } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_DCP) > 0) { 148 + usb_phy->chg_type = DCP_TYPE; 149 + usb_phy->chg_state = USB_CHARGER_PRESENT; 150 + } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_ACA) > 0) { 151 + usb_phy->chg_type = ACA_TYPE; 152 + usb_phy->chg_state = USB_CHARGER_PRESENT; 153 + } else { 154 + usb_phy->chg_type = UNKNOWN_TYPE; 155 + usb_phy->chg_state = USB_CHARGER_ABSENT; 156 + } 157 + 158 + schedule_work(&usb_phy->chg_work); 159 + } 160 + 161 + /** 162 + * usb_phy_get_charger_type - get charger type from extcon subsystem 163 + * @nb -the notifier block to determine charger type 164 + * @state - the cable state 165 + * @data - private data 166 + * 167 + * Determin the charger type from extcon subsystem which also means the 168 + * charger state has been chaned, then we should notify this event. 169 + */ 170 + static int usb_phy_get_charger_type(struct notifier_block *nb, 171 + unsigned long state, void *data) 172 + { 173 + struct usb_phy *usb_phy = container_of(nb, struct usb_phy, type_nb); 174 + 175 + __usb_phy_get_charger_type(usb_phy); 176 + return NOTIFY_OK; 177 + } 178 + 179 + /** 180 + * usb_phy_set_charger_current - set the USB charger current 181 + * @usb_phy - the USB phy to be used 182 + * @mA - the current need to be set 183 + * 184 + * Usually we only change the charger default current when USB finished the 185 + * enumeration as one SDP charger. As one SDP charger, usb_phy_set_power() 186 + * will issue this function to change charger current when after setting USB 187 + * configuration, or suspend/resume USB. For other type charger, we should 188 + * use the default charger current and we do not suggest to issue this function 189 + * to change the charger current. 190 + * 191 + * When USB charger current has been changed, we need to notify the power users. 192 + */ 193 + void usb_phy_set_charger_current(struct usb_phy *usb_phy, unsigned int mA) 194 + { 195 + switch (usb_phy->chg_type) { 196 + case SDP_TYPE: 197 + if (usb_phy->chg_cur.sdp_max == mA) 198 + return; 199 + 200 + usb_phy->chg_cur.sdp_max = (mA > DEFAULT_SDP_CUR_MAX_SS) ? 201 + DEFAULT_SDP_CUR_MAX_SS : mA; 202 + break; 203 + case DCP_TYPE: 204 + if (usb_phy->chg_cur.dcp_max == mA) 205 + return; 206 + 207 + usb_phy->chg_cur.dcp_max = (mA > DEFAULT_DCP_CUR_MAX) ? 208 + DEFAULT_DCP_CUR_MAX : mA; 209 + break; 210 + case CDP_TYPE: 211 + if (usb_phy->chg_cur.cdp_max == mA) 212 + return; 213 + 214 + usb_phy->chg_cur.cdp_max = (mA > DEFAULT_CDP_CUR_MAX) ? 215 + DEFAULT_CDP_CUR_MAX : mA; 216 + break; 217 + case ACA_TYPE: 218 + if (usb_phy->chg_cur.aca_max == mA) 219 + return; 220 + 221 + usb_phy->chg_cur.aca_max = (mA > DEFAULT_ACA_CUR_MAX) ? 222 + DEFAULT_ACA_CUR_MAX : mA; 223 + break; 224 + default: 225 + return; 226 + } 227 + 228 + schedule_work(&usb_phy->chg_work); 229 + } 230 + EXPORT_SYMBOL_GPL(usb_phy_set_charger_current); 231 + 232 + /** 233 + * usb_phy_get_charger_current - get the USB charger current 234 + * @usb_phy - the USB phy to be used 235 + * @min - the minimum current 236 + * @max - the maximum current 237 + * 238 + * Usually we will notify the maximum current to power user, but for some 239 + * special case, power user also need the minimum current value. Then the 240 + * power user can issue this function to get the suitable current. 241 + */ 242 + void usb_phy_get_charger_current(struct usb_phy *usb_phy, 243 + unsigned int *min, unsigned int *max) 244 + { 245 + switch (usb_phy->chg_type) { 246 + case SDP_TYPE: 247 + *min = usb_phy->chg_cur.sdp_min; 248 + *max = usb_phy->chg_cur.sdp_max; 249 + break; 250 + case DCP_TYPE: 251 + *min = usb_phy->chg_cur.dcp_min; 252 + *max = usb_phy->chg_cur.dcp_max; 253 + break; 254 + case CDP_TYPE: 255 + *min = usb_phy->chg_cur.cdp_min; 256 + *max = usb_phy->chg_cur.cdp_max; 257 + break; 258 + case ACA_TYPE: 259 + *min = usb_phy->chg_cur.aca_min; 260 + *max = usb_phy->chg_cur.aca_max; 261 + break; 262 + default: 263 + *min = 0; 264 + *max = 0; 265 + break; 266 + } 267 + } 268 + EXPORT_SYMBOL_GPL(usb_phy_get_charger_current); 269 + 270 + /** 271 + * usb_phy_set_charger_state - set the USB charger state 272 + * @usb_phy - the USB phy to be used 273 + * @state - the new state need to be set for charger 274 + * 275 + * The usb phy driver can issue this function when the usb phy driver 276 + * detected the charger state has been changed, in this case the charger 277 + * type should be get from ->charger_detect(). 278 + */ 279 + void usb_phy_set_charger_state(struct usb_phy *usb_phy, 280 + enum usb_charger_state state) 281 + { 282 + if (usb_phy->chg_state == state || !usb_phy->charger_detect) 283 + return; 284 + 285 + usb_phy->chg_state = state; 286 + if (usb_phy->chg_state == USB_CHARGER_PRESENT) 287 + usb_phy->chg_type = usb_phy->charger_detect(usb_phy); 288 + else 289 + usb_phy->chg_type = UNKNOWN_TYPE; 290 + 291 + schedule_work(&usb_phy->chg_work); 292 + } 293 + EXPORT_SYMBOL_GPL(usb_phy_set_charger_state); 294 + 92 295 static void devm_usb_phy_release(struct device *dev, void *res) 93 296 { 94 297 struct usb_phy *phy = *(struct usb_phy **)res; ··· 351 124 "register VBUS notifier failed\n"); 352 125 return ret; 353 126 } 127 + } else { 128 + x->type_nb.notifier_call = usb_phy_get_charger_type; 129 + 130 + ret = devm_extcon_register_notifier(x->dev, x->edev, 131 + EXTCON_CHG_USB_SDP, 132 + &x->type_nb); 133 + if (ret) { 134 + dev_err(x->dev, 135 + "register extcon USB SDP failed.\n"); 136 + return ret; 137 + } 138 + 139 + ret = devm_extcon_register_notifier(x->dev, x->edev, 140 + EXTCON_CHG_USB_CDP, 141 + &x->type_nb); 142 + if (ret) { 143 + dev_err(x->dev, 144 + "register extcon USB CDP failed.\n"); 145 + return ret; 146 + } 147 + 148 + ret = devm_extcon_register_notifier(x->dev, x->edev, 149 + EXTCON_CHG_USB_DCP, 150 + &x->type_nb); 151 + if (ret) { 152 + dev_err(x->dev, 153 + "register extcon USB DCP failed.\n"); 154 + return ret; 155 + } 156 + 157 + ret = devm_extcon_register_notifier(x->dev, x->edev, 158 + EXTCON_CHG_USB_ACA, 159 + &x->type_nb); 160 + if (ret) { 161 + dev_err(x->dev, 162 + "register extcon USB ACA failed.\n"); 163 + return ret; 164 + } 354 165 } 355 166 356 167 if (x->id_nb.notifier_call) { ··· 409 144 } 410 145 } 411 146 } 147 + 148 + usb_phy_set_default_current(x); 149 + INIT_WORK(&x->chg_work, usb_phy_notify_charger_work); 150 + x->chg_type = UNKNOWN_TYPE; 151 + x->chg_state = USB_CHARGER_DEFAULT; 152 + if (x->type_nb.notifier_call) 153 + __usb_phy_get_charger_type(x); 412 154 413 155 return 0; 414 156 }
+49
include/linux/usb/phy.h
··· 12 12 #include <linux/extcon.h> 13 13 #include <linux/notifier.h> 14 14 #include <linux/usb.h> 15 + #include <uapi/linux/usb/charger.h> 15 16 16 17 enum usb_phy_interface { 17 18 USBPHY_INTERFACE_MODE_UNKNOWN, ··· 73 72 int (*write)(struct usb_phy *x, u32 val, u32 reg); 74 73 }; 75 74 75 + struct usb_charger_current { 76 + unsigned int sdp_min; 77 + unsigned int sdp_max; 78 + unsigned int dcp_min; 79 + unsigned int dcp_max; 80 + unsigned int cdp_min; 81 + unsigned int cdp_max; 82 + unsigned int aca_min; 83 + unsigned int aca_max; 84 + }; 85 + 76 86 struct usb_phy { 77 87 struct device *dev; 78 88 const char *label; ··· 103 91 struct extcon_dev *id_edev; 104 92 struct notifier_block vbus_nb; 105 93 struct notifier_block id_nb; 94 + struct notifier_block type_nb; 95 + 96 + /* Support USB charger */ 97 + enum usb_charger_type chg_type; 98 + enum usb_charger_state chg_state; 99 + struct usb_charger_current chg_cur; 100 + struct work_struct chg_work; 106 101 107 102 /* for notification of usb_phy_events */ 108 103 struct atomic_notifier_head notifier; ··· 148 129 enum usb_device_speed speed); 149 130 int (*notify_disconnect)(struct usb_phy *x, 150 131 enum usb_device_speed speed); 132 + 133 + /* 134 + * Charger detection method can be implemented if you need to 135 + * manually detect the charger type. 136 + */ 137 + enum usb_charger_type (*charger_detect)(struct usb_phy *x); 151 138 }; 152 139 153 140 /** ··· 244 219 extern int usb_bind_phy(const char *dev_name, u8 index, 245 220 const char *phy_dev_name); 246 221 extern void usb_phy_set_event(struct usb_phy *x, unsigned long event); 222 + extern void usb_phy_set_charger_current(struct usb_phy *usb_phy, 223 + unsigned int mA); 224 + extern void usb_phy_get_charger_current(struct usb_phy *usb_phy, 225 + unsigned int *min, unsigned int *max); 226 + extern void usb_phy_set_charger_state(struct usb_phy *usb_phy, 227 + enum usb_charger_state state); 247 228 #else 248 229 static inline struct usb_phy *usb_get_phy(enum usb_phy_type type) 249 230 { ··· 301 270 static inline void usb_phy_set_event(struct usb_phy *x, unsigned long event) 302 271 { 303 272 } 273 + 274 + static inline void usb_phy_set_charger_current(struct usb_phy *usb_phy, 275 + unsigned int mA) 276 + { 277 + } 278 + 279 + static inline void usb_phy_get_charger_current(struct usb_phy *usb_phy, 280 + unsigned int *min, 281 + unsigned int *max) 282 + { 283 + } 284 + 285 + static inline void usb_phy_set_charger_state(struct usb_phy *usb_phy, 286 + enum usb_charger_state state) 287 + { 288 + } 304 289 #endif 305 290 306 291 static inline int 307 292 usb_phy_set_power(struct usb_phy *x, unsigned mA) 308 293 { 294 + usb_phy_set_charger_current(x, mA); 295 + 309 296 if (x && x->set_power) 310 297 return x->set_power(x, mA); 311 298 return 0;