diff options
Diffstat (limited to 'drivers/power/ab8500_charger.c')
-rw-r--r-- | drivers/power/ab8500_charger.c | 1067 |
1 files changed, 861 insertions, 206 deletions
diff --git a/drivers/power/ab8500_charger.c b/drivers/power/ab8500_charger.c index 3be9c0ee3fc5..24b30b7ea5ca 100644 --- a/drivers/power/ab8500_charger.c +++ b/drivers/power/ab8500_charger.c | |||
@@ -31,6 +31,7 @@ | |||
31 | #include <linux/mfd/abx500/ab8500-gpadc.h> | 31 | #include <linux/mfd/abx500/ab8500-gpadc.h> |
32 | #include <linux/mfd/abx500/ux500_chargalg.h> | 32 | #include <linux/mfd/abx500/ux500_chargalg.h> |
33 | #include <linux/usb/otg.h> | 33 | #include <linux/usb/otg.h> |
34 | #include <linux/mutex.h> | ||
34 | 35 | ||
35 | /* Charger constants */ | 36 | /* Charger constants */ |
36 | #define NO_PW_CONN 0 | 37 | #define NO_PW_CONN 0 |
@@ -54,6 +55,7 @@ | |||
54 | 55 | ||
55 | #define MAIN_CH_INPUT_CURR_SHIFT 4 | 56 | #define MAIN_CH_INPUT_CURR_SHIFT 4 |
56 | #define VBUS_IN_CURR_LIM_SHIFT 4 | 57 | #define VBUS_IN_CURR_LIM_SHIFT 4 |
58 | #define AUTO_VBUS_IN_CURR_LIM_SHIFT 4 | ||
57 | 59 | ||
58 | #define LED_INDICATOR_PWM_ENA 0x01 | 60 | #define LED_INDICATOR_PWM_ENA 0x01 |
59 | #define LED_INDICATOR_PWM_DIS 0x00 | 61 | #define LED_INDICATOR_PWM_DIS 0x00 |
@@ -68,6 +70,11 @@ | |||
68 | #define MAIN_CH_NOK 0x01 | 70 | #define MAIN_CH_NOK 0x01 |
69 | #define VBUS_DET 0x80 | 71 | #define VBUS_DET 0x80 |
70 | 72 | ||
73 | #define MAIN_CH_STATUS2_MAINCHGDROP 0x80 | ||
74 | #define MAIN_CH_STATUS2_MAINCHARGERDETDBNC 0x40 | ||
75 | #define USB_CH_VBUSDROP 0x40 | ||
76 | #define USB_CH_VBUSDETDBNC 0x01 | ||
77 | |||
71 | /* UsbLineStatus register bit masks */ | 78 | /* UsbLineStatus register bit masks */ |
72 | #define AB8500_USB_LINK_STATUS 0x78 | 79 | #define AB8500_USB_LINK_STATUS 0x78 |
73 | #define AB8500_STD_HOST_SUSP 0x18 | 80 | #define AB8500_STD_HOST_SUSP 0x18 |
@@ -79,6 +86,17 @@ | |||
79 | /* Lowest charger voltage is 3.39V -> 0x4E */ | 86 | /* Lowest charger voltage is 3.39V -> 0x4E */ |
80 | #define LOW_VOLT_REG 0x4E | 87 | #define LOW_VOLT_REG 0x4E |
81 | 88 | ||
89 | /* Step up/down delay in us */ | ||
90 | #define STEP_UDELAY 1000 | ||
91 | |||
92 | #define CHARGER_STATUS_POLL 10 /* in ms */ | ||
93 | |||
94 | #define CHG_WD_INTERVAL (60 * HZ) | ||
95 | |||
96 | #define AB8500_SW_CONTROL_FALLBACK 0x03 | ||
97 | /* Wait for enumeration before charing in us */ | ||
98 | #define WAIT_ACA_RID_ENUMERATION (5 * 1000) | ||
99 | |||
82 | /* UsbLineStatus register - usb types */ | 100 | /* UsbLineStatus register - usb types */ |
83 | enum ab8500_charger_link_status { | 101 | enum ab8500_charger_link_status { |
84 | USB_STAT_NOT_CONFIGURED, | 102 | USB_STAT_NOT_CONFIGURED, |
@@ -97,6 +115,13 @@ enum ab8500_charger_link_status { | |||
97 | USB_STAT_HM_IDGND, | 115 | USB_STAT_HM_IDGND, |
98 | USB_STAT_RESERVED, | 116 | USB_STAT_RESERVED, |
99 | USB_STAT_NOT_VALID_LINK, | 117 | USB_STAT_NOT_VALID_LINK, |
118 | USB_STAT_PHY_EN, | ||
119 | USB_STAT_SUP_NO_IDGND_VBUS, | ||
120 | USB_STAT_SUP_IDGND_VBUS, | ||
121 | USB_STAT_CHARGER_LINE_1, | ||
122 | USB_STAT_CARKIT_1, | ||
123 | USB_STAT_CARKIT_2, | ||
124 | USB_STAT_ACA_DOCK_CHARGER, | ||
100 | }; | 125 | }; |
101 | 126 | ||
102 | enum ab8500_usb_state { | 127 | enum ab8500_usb_state { |
@@ -149,6 +174,7 @@ struct ab8500_charger_info { | |||
149 | int charger_voltage; | 174 | int charger_voltage; |
150 | int cv_active; | 175 | int cv_active; |
151 | bool wd_expired; | 176 | bool wd_expired; |
177 | int charger_current; | ||
152 | }; | 178 | }; |
153 | 179 | ||
154 | struct ab8500_charger_event_flags { | 180 | struct ab8500_charger_event_flags { |
@@ -159,12 +185,14 @@ struct ab8500_charger_event_flags { | |||
159 | bool usbchargernotok; | 185 | bool usbchargernotok; |
160 | bool chgwdexp; | 186 | bool chgwdexp; |
161 | bool vbus_collapse; | 187 | bool vbus_collapse; |
188 | bool vbus_drop_end; | ||
162 | }; | 189 | }; |
163 | 190 | ||
164 | struct ab8500_charger_usb_state { | 191 | struct ab8500_charger_usb_state { |
165 | bool usb_changed; | ||
166 | int usb_current; | 192 | int usb_current; |
193 | int usb_current_tmp; | ||
167 | enum ab8500_usb_state state; | 194 | enum ab8500_usb_state state; |
195 | enum ab8500_usb_state state_tmp; | ||
168 | spinlock_t usb_lock; | 196 | spinlock_t usb_lock; |
169 | }; | 197 | }; |
170 | 198 | ||
@@ -182,11 +210,17 @@ struct ab8500_charger_usb_state { | |||
182 | * charger is enabled | 210 | * charger is enabled |
183 | * @vbat Battery voltage | 211 | * @vbat Battery voltage |
184 | * @old_vbat Previously measured battery voltage | 212 | * @old_vbat Previously measured battery voltage |
213 | * @usb_device_is_unrecognised USB device is unrecognised by the hardware | ||
185 | * @autopower Indicate if we should have automatic pwron after pwrloss | 214 | * @autopower Indicate if we should have automatic pwron after pwrloss |
186 | * @autopower_cfg platform specific power config support for "pwron after pwrloss" | 215 | * @autopower_cfg platform specific power config support for "pwron after pwrloss" |
216 | * @invalid_charger_detect_state State when forcing AB to use invalid charger | ||
217 | * @is_usb_host: Indicate if last detected USB type is host | ||
218 | * @is_aca_rid: Incicate if accessory is ACA type | ||
219 | * @current_stepping_sessions: | ||
220 | * Counter for current stepping sessions | ||
187 | * @parent: Pointer to the struct ab8500 | 221 | * @parent: Pointer to the struct ab8500 |
188 | * @gpadc: Pointer to the struct gpadc | 222 | * @gpadc: Pointer to the struct gpadc |
189 | * @bat: Pointer to the abx500_bm platform data | 223 | * @bm: Platform specific battery management information |
190 | * @flags: Structure for information about events triggered | 224 | * @flags: Structure for information about events triggered |
191 | * @usb_state: Structure for usb stack information | 225 | * @usb_state: Structure for usb stack information |
192 | * @ac_chg: AC charger power supply | 226 | * @ac_chg: AC charger power supply |
@@ -195,19 +229,28 @@ struct ab8500_charger_usb_state { | |||
195 | * @usb: Structure that holds the USB charger properties | 229 | * @usb: Structure that holds the USB charger properties |
196 | * @regu: Pointer to the struct regulator | 230 | * @regu: Pointer to the struct regulator |
197 | * @charger_wq: Work queue for the IRQs and checking HW state | 231 | * @charger_wq: Work queue for the IRQs and checking HW state |
232 | * @usb_ipt_crnt_lock: Lock to protect VBUS input current setting from mutuals | ||
233 | * @pm_lock: Lock to prevent system to suspend | ||
198 | * @check_vbat_work Work for checking vbat threshold to adjust vbus current | 234 | * @check_vbat_work Work for checking vbat threshold to adjust vbus current |
199 | * @check_hw_failure_work: Work for checking HW state | 235 | * @check_hw_failure_work: Work for checking HW state |
200 | * @check_usbchgnotok_work: Work for checking USB charger not ok status | 236 | * @check_usbchgnotok_work: Work for checking USB charger not ok status |
201 | * @kick_wd_work: Work for kicking the charger watchdog in case | 237 | * @kick_wd_work: Work for kicking the charger watchdog in case |
202 | * of ABB rev 1.* due to the watchog logic bug | 238 | * of ABB rev 1.* due to the watchog logic bug |
239 | * @ac_charger_attached_work: Work for checking if AC charger is still | ||
240 | * connected | ||
241 | * @usb_charger_attached_work: Work for checking if USB charger is still | ||
242 | * connected | ||
203 | * @ac_work: Work for checking AC charger connection | 243 | * @ac_work: Work for checking AC charger connection |
204 | * @detect_usb_type_work: Work for detecting the USB type connected | 244 | * @detect_usb_type_work: Work for detecting the USB type connected |
205 | * @usb_link_status_work: Work for checking the new USB link status | 245 | * @usb_link_status_work: Work for checking the new USB link status |
206 | * @usb_state_changed_work: Work for checking USB state | 246 | * @usb_state_changed_work: Work for checking USB state |
247 | * @attach_work: Work for detecting USB type | ||
248 | * @vbus_drop_end_work: Work for detecting VBUS drop end | ||
207 | * @check_main_thermal_prot_work: | 249 | * @check_main_thermal_prot_work: |
208 | * Work for checking Main thermal status | 250 | * Work for checking Main thermal status |
209 | * @check_usb_thermal_prot_work: | 251 | * @check_usb_thermal_prot_work: |
210 | * Work for checking USB thermal status | 252 | * Work for checking USB thermal status |
253 | * @charger_attached_mutex: For controlling the wakelock | ||
211 | */ | 254 | */ |
212 | struct ab8500_charger { | 255 | struct ab8500_charger { |
213 | struct device *dev; | 256 | struct device *dev; |
@@ -219,11 +262,16 @@ struct ab8500_charger { | |||
219 | bool vddadc_en_usb; | 262 | bool vddadc_en_usb; |
220 | int vbat; | 263 | int vbat; |
221 | int old_vbat; | 264 | int old_vbat; |
265 | bool usb_device_is_unrecognised; | ||
222 | bool autopower; | 266 | bool autopower; |
223 | bool autopower_cfg; | 267 | bool autopower_cfg; |
268 | int invalid_charger_detect_state; | ||
269 | bool is_usb_host; | ||
270 | int is_aca_rid; | ||
271 | atomic_t current_stepping_sessions; | ||
224 | struct ab8500 *parent; | 272 | struct ab8500 *parent; |
225 | struct ab8500_gpadc *gpadc; | 273 | struct ab8500_gpadc *gpadc; |
226 | struct abx500_bm_data *bat; | 274 | struct abx500_bm_data *bm; |
227 | struct ab8500_charger_event_flags flags; | 275 | struct ab8500_charger_event_flags flags; |
228 | struct ab8500_charger_usb_state usb_state; | 276 | struct ab8500_charger_usb_state usb_state; |
229 | struct ux500_charger ac_chg; | 277 | struct ux500_charger ac_chg; |
@@ -232,18 +280,24 @@ struct ab8500_charger { | |||
232 | struct ab8500_charger_info usb; | 280 | struct ab8500_charger_info usb; |
233 | struct regulator *regu; | 281 | struct regulator *regu; |
234 | struct workqueue_struct *charger_wq; | 282 | struct workqueue_struct *charger_wq; |
283 | struct mutex usb_ipt_crnt_lock; | ||
235 | struct delayed_work check_vbat_work; | 284 | struct delayed_work check_vbat_work; |
236 | struct delayed_work check_hw_failure_work; | 285 | struct delayed_work check_hw_failure_work; |
237 | struct delayed_work check_usbchgnotok_work; | 286 | struct delayed_work check_usbchgnotok_work; |
238 | struct delayed_work kick_wd_work; | 287 | struct delayed_work kick_wd_work; |
288 | struct delayed_work usb_state_changed_work; | ||
289 | struct delayed_work attach_work; | ||
290 | struct delayed_work ac_charger_attached_work; | ||
291 | struct delayed_work usb_charger_attached_work; | ||
292 | struct delayed_work vbus_drop_end_work; | ||
239 | struct work_struct ac_work; | 293 | struct work_struct ac_work; |
240 | struct work_struct detect_usb_type_work; | 294 | struct work_struct detect_usb_type_work; |
241 | struct work_struct usb_link_status_work; | 295 | struct work_struct usb_link_status_work; |
242 | struct work_struct usb_state_changed_work; | ||
243 | struct work_struct check_main_thermal_prot_work; | 296 | struct work_struct check_main_thermal_prot_work; |
244 | struct work_struct check_usb_thermal_prot_work; | 297 | struct work_struct check_usb_thermal_prot_work; |
245 | struct usb_phy *usb_phy; | 298 | struct usb_phy *usb_phy; |
246 | struct notifier_block nb; | 299 | struct notifier_block nb; |
300 | struct mutex charger_attached_mutex; | ||
247 | }; | 301 | }; |
248 | 302 | ||
249 | /* AC properties */ | 303 | /* AC properties */ |
@@ -267,50 +321,65 @@ static enum power_supply_property ab8500_charger_usb_props[] = { | |||
267 | POWER_SUPPLY_PROP_CURRENT_NOW, | 321 | POWER_SUPPLY_PROP_CURRENT_NOW, |
268 | }; | 322 | }; |
269 | 323 | ||
270 | /** | 324 | /* |
271 | * ab8500_power_loss_handling - set how we handle powerloss. | 325 | * Function for enabling and disabling sw fallback mode |
272 | * @di: pointer to the ab8500_charger structure | 326 | * should always be disabled when no charger is connected. |
273 | * | ||
274 | * Magic nummbers are from STE HW department. | ||
275 | */ | 327 | */ |
276 | static void ab8500_power_loss_handling(struct ab8500_charger *di) | 328 | static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di, |
329 | bool fallback) | ||
277 | { | 330 | { |
331 | u8 val; | ||
278 | u8 reg; | 332 | u8 reg; |
333 | u8 bank; | ||
334 | u8 bit; | ||
279 | int ret; | 335 | int ret; |
280 | 336 | ||
281 | dev_dbg(di->dev, "Autopower : %d\n", di->autopower); | 337 | dev_dbg(di->dev, "SW Fallback: %d\n", fallback); |
282 | 338 | ||
283 | /* read the autopower register */ | 339 | if (is_ab8500(di->parent)) { |
284 | ret = abx500_get_register_interruptible(di->dev, 0x15, 0x00, ®); | 340 | bank = 0x15; |
285 | if (ret) { | 341 | reg = 0x0; |
286 | dev_err(di->dev, "%d write failed\n", __LINE__); | 342 | bit = 3; |
287 | return; | 343 | } else { |
344 | bank = AB8500_SYS_CTRL1_BLOCK; | ||
345 | reg = AB8500_SW_CONTROL_FALLBACK; | ||
346 | bit = 0; | ||
288 | } | 347 | } |
289 | 348 | ||
290 | /* enable the OPT emulation registers */ | 349 | /* read the register containing fallback bit */ |
291 | ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2); | 350 | ret = abx500_get_register_interruptible(di->dev, bank, reg, &val); |
292 | if (ret) { | 351 | if (ret < 0) { |
293 | dev_err(di->dev, "%d write failed\n", __LINE__); | 352 | dev_err(di->dev, "%d read failed\n", __LINE__); |
294 | return; | 353 | return; |
295 | } | 354 | } |
296 | 355 | ||
297 | if (di->autopower) | 356 | if (is_ab8500(di->parent)) { |
298 | reg |= 0x8; | 357 | /* enable the OPT emulation registers */ |
358 | ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2); | ||
359 | if (ret) { | ||
360 | dev_err(di->dev, "%d write failed\n", __LINE__); | ||
361 | goto disable_otp; | ||
362 | } | ||
363 | } | ||
364 | |||
365 | if (fallback) | ||
366 | val |= (1 << bit); | ||
299 | else | 367 | else |
300 | reg &= ~0x8; | 368 | val &= ~(1 << bit); |
301 | 369 | ||
302 | /* write back the changed value to autopower reg */ | 370 | /* write back the changed fallback bit value to register */ |
303 | ret = abx500_set_register_interruptible(di->dev, 0x15, 0x00, reg); | 371 | ret = abx500_set_register_interruptible(di->dev, bank, reg, val); |
304 | if (ret) { | 372 | if (ret) { |
305 | dev_err(di->dev, "%d write failed\n", __LINE__); | 373 | dev_err(di->dev, "%d write failed\n", __LINE__); |
306 | return; | ||
307 | } | 374 | } |
308 | 375 | ||
309 | /* disable the set OTP registers again */ | 376 | disable_otp: |
310 | ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0); | 377 | if (is_ab8500(di->parent)) { |
311 | if (ret) { | 378 | /* disable the set OTP registers again */ |
312 | dev_err(di->dev, "%d write failed\n", __LINE__); | 379 | ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0); |
313 | return; | 380 | if (ret) { |
381 | dev_err(di->dev, "%d write failed\n", __LINE__); | ||
382 | } | ||
314 | } | 383 | } |
315 | } | 384 | } |
316 | 385 | ||
@@ -329,12 +398,12 @@ static void ab8500_power_supply_changed(struct ab8500_charger *di, | |||
329 | !di->ac.charger_connected && | 398 | !di->ac.charger_connected && |
330 | di->autopower) { | 399 | di->autopower) { |
331 | di->autopower = false; | 400 | di->autopower = false; |
332 | ab8500_power_loss_handling(di); | 401 | ab8500_enable_disable_sw_fallback(di, false); |
333 | } else if (!di->autopower && | 402 | } else if (!di->autopower && |
334 | (di->ac.charger_connected || | 403 | (di->ac.charger_connected || |
335 | di->usb.charger_connected)) { | 404 | di->usb.charger_connected)) { |
336 | di->autopower = true; | 405 | di->autopower = true; |
337 | ab8500_power_loss_handling(di); | 406 | ab8500_enable_disable_sw_fallback(di, true); |
338 | } | 407 | } |
339 | } | 408 | } |
340 | power_supply_changed(psy); | 409 | power_supply_changed(psy); |
@@ -347,6 +416,19 @@ static void ab8500_charger_set_usb_connected(struct ab8500_charger *di, | |||
347 | dev_dbg(di->dev, "USB connected:%i\n", connected); | 416 | dev_dbg(di->dev, "USB connected:%i\n", connected); |
348 | di->usb.charger_connected = connected; | 417 | di->usb.charger_connected = connected; |
349 | sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present"); | 418 | sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present"); |
419 | |||
420 | if (connected) { | ||
421 | mutex_lock(&di->charger_attached_mutex); | ||
422 | mutex_unlock(&di->charger_attached_mutex); | ||
423 | |||
424 | queue_delayed_work(di->charger_wq, | ||
425 | &di->usb_charger_attached_work, | ||
426 | HZ); | ||
427 | } else { | ||
428 | cancel_delayed_work_sync(&di->usb_charger_attached_work); | ||
429 | mutex_lock(&di->charger_attached_mutex); | ||
430 | mutex_unlock(&di->charger_attached_mutex); | ||
431 | } | ||
350 | } | 432 | } |
351 | } | 433 | } |
352 | 434 | ||
@@ -500,6 +582,7 @@ static int ab8500_charger_usb_cv(struct ab8500_charger *di) | |||
500 | /** | 582 | /** |
501 | * ab8500_charger_detect_chargers() - Detect the connected chargers | 583 | * ab8500_charger_detect_chargers() - Detect the connected chargers |
502 | * @di: pointer to the ab8500_charger structure | 584 | * @di: pointer to the ab8500_charger structure |
585 | * @probe: if probe, don't delay and wait for HW | ||
503 | * | 586 | * |
504 | * Returns the type of charger connected. | 587 | * Returns the type of charger connected. |
505 | * For USB it will not mean we can actually charge from it | 588 | * For USB it will not mean we can actually charge from it |
@@ -513,7 +596,7 @@ static int ab8500_charger_usb_cv(struct ab8500_charger *di) | |||
513 | * USB_PW_CONN if the USB power supply is connected | 596 | * USB_PW_CONN if the USB power supply is connected |
514 | * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected | 597 | * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected |
515 | */ | 598 | */ |
516 | static int ab8500_charger_detect_chargers(struct ab8500_charger *di) | 599 | static int ab8500_charger_detect_chargers(struct ab8500_charger *di, bool probe) |
517 | { | 600 | { |
518 | int result = NO_PW_CONN; | 601 | int result = NO_PW_CONN; |
519 | int ret; | 602 | int ret; |
@@ -531,13 +614,25 @@ static int ab8500_charger_detect_chargers(struct ab8500_charger *di) | |||
531 | result = AC_PW_CONN; | 614 | result = AC_PW_CONN; |
532 | 615 | ||
533 | /* Check for USB charger */ | 616 | /* Check for USB charger */ |
617 | |||
618 | if (!probe) { | ||
619 | /* | ||
620 | * AB8500 says VBUS_DET_DBNC1 & VBUS_DET_DBNC100 | ||
621 | * when disconnecting ACA even though no | ||
622 | * charger was connected. Try waiting a little | ||
623 | * longer than the 100 ms of VBUS_DET_DBNC100... | ||
624 | */ | ||
625 | msleep(110); | ||
626 | } | ||
534 | ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, | 627 | ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, |
535 | AB8500_CH_USBCH_STAT1_REG, &val); | 628 | AB8500_CH_USBCH_STAT1_REG, &val); |
536 | if (ret < 0) { | 629 | if (ret < 0) { |
537 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); | 630 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); |
538 | return ret; | 631 | return ret; |
539 | } | 632 | } |
540 | 633 | dev_dbg(di->dev, | |
634 | "%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__, | ||
635 | val); | ||
541 | if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100)) | 636 | if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100)) |
542 | result |= USB_PW_CONN; | 637 | result |= USB_PW_CONN; |
543 | 638 | ||
@@ -554,31 +649,53 @@ static int ab8500_charger_detect_chargers(struct ab8500_charger *di) | |||
554 | * Returns error code in case of failure else 0 on success | 649 | * Returns error code in case of failure else 0 on success |
555 | */ | 650 | */ |
556 | static int ab8500_charger_max_usb_curr(struct ab8500_charger *di, | 651 | static int ab8500_charger_max_usb_curr(struct ab8500_charger *di, |
557 | enum ab8500_charger_link_status link_status) | 652 | enum ab8500_charger_link_status link_status) |
558 | { | 653 | { |
559 | int ret = 0; | 654 | int ret = 0; |
560 | 655 | ||
656 | di->usb_device_is_unrecognised = false; | ||
657 | |||
658 | /* | ||
659 | * Platform only supports USB 2.0. | ||
660 | * This means that charging current from USB source | ||
661 | * is maximum 500 mA. Every occurence of USB_STAT_*_HOST_* | ||
662 | * should set USB_CH_IP_CUR_LVL_0P5. | ||
663 | */ | ||
664 | |||
561 | switch (link_status) { | 665 | switch (link_status) { |
562 | case USB_STAT_STD_HOST_NC: | 666 | case USB_STAT_STD_HOST_NC: |
563 | case USB_STAT_STD_HOST_C_NS: | 667 | case USB_STAT_STD_HOST_C_NS: |
564 | case USB_STAT_STD_HOST_C_S: | 668 | case USB_STAT_STD_HOST_C_S: |
565 | dev_dbg(di->dev, "USB Type - Standard host is " | 669 | dev_dbg(di->dev, "USB Type - Standard host is " |
566 | "detected through USB driver\n"); | 670 | "detected through USB driver\n"); |
567 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09; | 671 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5; |
672 | di->is_usb_host = true; | ||
673 | di->is_aca_rid = 0; | ||
568 | break; | 674 | break; |
569 | case USB_STAT_HOST_CHG_HS_CHIRP: | 675 | case USB_STAT_HOST_CHG_HS_CHIRP: |
570 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5; | 676 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5; |
677 | di->is_usb_host = true; | ||
678 | di->is_aca_rid = 0; | ||
571 | break; | 679 | break; |
572 | case USB_STAT_HOST_CHG_HS: | 680 | case USB_STAT_HOST_CHG_HS: |
681 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5; | ||
682 | di->is_usb_host = true; | ||
683 | di->is_aca_rid = 0; | ||
684 | break; | ||
573 | case USB_STAT_ACA_RID_C_HS: | 685 | case USB_STAT_ACA_RID_C_HS: |
574 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P9; | 686 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P9; |
687 | di->is_usb_host = false; | ||
688 | di->is_aca_rid = 0; | ||
575 | break; | 689 | break; |
576 | case USB_STAT_ACA_RID_A: | 690 | case USB_STAT_ACA_RID_A: |
577 | /* | 691 | /* |
578 | * Dedicated charger level minus maximum current accessory | 692 | * Dedicated charger level minus maximum current accessory |
579 | * can consume (300mA). Closest level is 1100mA | 693 | * can consume (900mA). Closest level is 500mA |
580 | */ | 694 | */ |
581 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P1; | 695 | dev_dbg(di->dev, "USB_STAT_ACA_RID_A detected\n"); |
696 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5; | ||
697 | di->is_usb_host = false; | ||
698 | di->is_aca_rid = 1; | ||
582 | break; | 699 | break; |
583 | case USB_STAT_ACA_RID_B: | 700 | case USB_STAT_ACA_RID_B: |
584 | /* | 701 | /* |
@@ -586,34 +703,68 @@ static int ab8500_charger_max_usb_curr(struct ab8500_charger *di, | |||
586 | * 100mA for potential accessory). Closest level is 1300mA | 703 | * 100mA for potential accessory). Closest level is 1300mA |
587 | */ | 704 | */ |
588 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P3; | 705 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P3; |
706 | dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status, | ||
707 | di->max_usb_in_curr); | ||
708 | di->is_usb_host = false; | ||
709 | di->is_aca_rid = 1; | ||
589 | break; | 710 | break; |
590 | case USB_STAT_DEDICATED_CHG: | ||
591 | case USB_STAT_HOST_CHG_NM: | 711 | case USB_STAT_HOST_CHG_NM: |
712 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5; | ||
713 | di->is_usb_host = true; | ||
714 | di->is_aca_rid = 0; | ||
715 | break; | ||
716 | case USB_STAT_DEDICATED_CHG: | ||
717 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5; | ||
718 | di->is_usb_host = false; | ||
719 | di->is_aca_rid = 0; | ||
720 | break; | ||
592 | case USB_STAT_ACA_RID_C_HS_CHIRP: | 721 | case USB_STAT_ACA_RID_C_HS_CHIRP: |
593 | case USB_STAT_ACA_RID_C_NM: | 722 | case USB_STAT_ACA_RID_C_NM: |
594 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5; | 723 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5; |
724 | di->is_usb_host = false; | ||
725 | di->is_aca_rid = 1; | ||
595 | break; | 726 | break; |
596 | case USB_STAT_RESERVED: | ||
597 | /* | ||
598 | * This state is used to indicate that VBUS has dropped below | ||
599 | * the detection level 4 times in a row. This is due to the | ||
600 | * charger output current is set to high making the charger | ||
601 | * voltage collapse. This have to be propagated through to | ||
602 | * chargalg. This is done using the property | ||
603 | * POWER_SUPPLY_PROP_CURRENT_AVG = 1 | ||
604 | */ | ||
605 | di->flags.vbus_collapse = true; | ||
606 | dev_dbg(di->dev, "USB Type - USB_STAT_RESERVED " | ||
607 | "VBUS has collapsed\n"); | ||
608 | ret = -1; | ||
609 | break; | ||
610 | case USB_STAT_HM_IDGND: | ||
611 | case USB_STAT_NOT_CONFIGURED: | 727 | case USB_STAT_NOT_CONFIGURED: |
612 | case USB_STAT_NOT_VALID_LINK: | 728 | if (di->vbus_detected) { |
729 | di->usb_device_is_unrecognised = true; | ||
730 | dev_dbg(di->dev, "USB Type - Legacy charger.\n"); | ||
731 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5; | ||
732 | break; | ||
733 | } | ||
734 | case USB_STAT_HM_IDGND: | ||
613 | dev_err(di->dev, "USB Type - Charging not allowed\n"); | 735 | dev_err(di->dev, "USB Type - Charging not allowed\n"); |
614 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05; | 736 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05; |
615 | ret = -ENXIO; | 737 | ret = -ENXIO; |
616 | break; | 738 | break; |
739 | case USB_STAT_RESERVED: | ||
740 | if (is_ab8500(di->parent)) { | ||
741 | di->flags.vbus_collapse = true; | ||
742 | dev_err(di->dev, "USB Type - USB_STAT_RESERVED " | ||
743 | "VBUS has collapsed\n"); | ||
744 | ret = -ENXIO; | ||
745 | break; | ||
746 | } | ||
747 | if (is_ab9540(di->parent) || is_ab8505(di->parent)) { | ||
748 | dev_dbg(di->dev, "USB Type - Charging not allowed\n"); | ||
749 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05; | ||
750 | dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", | ||
751 | link_status, di->max_usb_in_curr); | ||
752 | ret = -ENXIO; | ||
753 | break; | ||
754 | } | ||
755 | break; | ||
756 | case USB_STAT_CARKIT_1: | ||
757 | case USB_STAT_CARKIT_2: | ||
758 | case USB_STAT_ACA_DOCK_CHARGER: | ||
759 | case USB_STAT_CHARGER_LINE_1: | ||
760 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5; | ||
761 | dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status, | ||
762 | di->max_usb_in_curr); | ||
763 | case USB_STAT_NOT_VALID_LINK: | ||
764 | dev_err(di->dev, "USB Type invalid - try charging anyway\n"); | ||
765 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5; | ||
766 | break; | ||
767 | |||
617 | default: | 768 | default: |
618 | dev_err(di->dev, "USB Type - Unknown\n"); | 769 | dev_err(di->dev, "USB Type - Unknown\n"); |
619 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05; | 770 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05; |
@@ -645,8 +796,14 @@ static int ab8500_charger_read_usb_type(struct ab8500_charger *di) | |||
645 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); | 796 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); |
646 | return ret; | 797 | return ret; |
647 | } | 798 | } |
648 | ret = abx500_get_register_interruptible(di->dev, AB8500_USB, | 799 | if (is_ab8500(di->parent)) { |
649 | AB8500_USB_LINE_STAT_REG, &val); | 800 | ret = abx500_get_register_interruptible(di->dev, AB8500_USB, |
801 | AB8500_USB_LINE_STAT_REG, &val); | ||
802 | } else { | ||
803 | if (is_ab9540(di->parent) || is_ab8505(di->parent)) | ||
804 | ret = abx500_get_register_interruptible(di->dev, | ||
805 | AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val); | ||
806 | } | ||
650 | if (ret < 0) { | 807 | if (ret < 0) { |
651 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); | 808 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); |
652 | return ret; | 809 | return ret; |
@@ -682,16 +839,25 @@ static int ab8500_charger_detect_usb_type(struct ab8500_charger *di) | |||
682 | ret = abx500_get_register_interruptible(di->dev, | 839 | ret = abx500_get_register_interruptible(di->dev, |
683 | AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, | 840 | AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, |
684 | &val); | 841 | &val); |
842 | dev_dbg(di->dev, "%s AB8500_IT_SOURCE21_REG %x\n", | ||
843 | __func__, val); | ||
685 | if (ret < 0) { | 844 | if (ret < 0) { |
686 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); | 845 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); |
687 | return ret; | 846 | return ret; |
688 | } | 847 | } |
689 | ret = abx500_get_register_interruptible(di->dev, AB8500_USB, | 848 | |
690 | AB8500_USB_LINE_STAT_REG, &val); | 849 | if (is_ab8500(di->parent)) |
850 | ret = abx500_get_register_interruptible(di->dev, | ||
851 | AB8500_USB, AB8500_USB_LINE_STAT_REG, &val); | ||
852 | else | ||
853 | ret = abx500_get_register_interruptible(di->dev, | ||
854 | AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val); | ||
691 | if (ret < 0) { | 855 | if (ret < 0) { |
692 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); | 856 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); |
693 | return ret; | 857 | return ret; |
694 | } | 858 | } |
859 | dev_dbg(di->dev, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__, | ||
860 | val); | ||
695 | /* | 861 | /* |
696 | * Until the IT source register is read the UsbLineStatus | 862 | * Until the IT source register is read the UsbLineStatus |
697 | * register is not updated, hence doing the same | 863 | * register is not updated, hence doing the same |
@@ -936,6 +1102,144 @@ static int ab8500_charger_get_usb_cur(struct ab8500_charger *di) | |||
936 | } | 1102 | } |
937 | 1103 | ||
938 | /** | 1104 | /** |
1105 | * ab8500_charger_set_current() - set charger current | ||
1106 | * @di: pointer to the ab8500_charger structure | ||
1107 | * @ich: charger current, in mA | ||
1108 | * @reg: select what charger register to set | ||
1109 | * | ||
1110 | * Set charger current. | ||
1111 | * There is no state machine in the AB to step up/down the charger | ||
1112 | * current to avoid dips and spikes on MAIN, VBUS and VBAT when | ||
1113 | * charging is started. Instead we need to implement | ||
1114 | * this charger current step-up/down here. | ||
1115 | * Returns error code in case of failure else 0(on success) | ||
1116 | */ | ||
1117 | static int ab8500_charger_set_current(struct ab8500_charger *di, | ||
1118 | int ich, int reg) | ||
1119 | { | ||
1120 | int ret = 0; | ||
1121 | int auto_curr_index, curr_index, prev_curr_index, shift_value, i; | ||
1122 | u8 reg_value; | ||
1123 | u32 step_udelay; | ||
1124 | bool no_stepping = false; | ||
1125 | |||
1126 | atomic_inc(&di->current_stepping_sessions); | ||
1127 | |||
1128 | ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, | ||
1129 | reg, ®_value); | ||
1130 | if (ret < 0) { | ||
1131 | dev_err(di->dev, "%s read failed\n", __func__); | ||
1132 | goto exit_set_current; | ||
1133 | } | ||
1134 | |||
1135 | switch (reg) { | ||
1136 | case AB8500_MCH_IPT_CURLVL_REG: | ||
1137 | shift_value = MAIN_CH_INPUT_CURR_SHIFT; | ||
1138 | prev_curr_index = (reg_value >> shift_value); | ||
1139 | curr_index = ab8500_current_to_regval(ich); | ||
1140 | step_udelay = STEP_UDELAY; | ||
1141 | if (!di->ac.charger_connected) | ||
1142 | no_stepping = true; | ||
1143 | break; | ||
1144 | case AB8500_USBCH_IPT_CRNTLVL_REG: | ||
1145 | shift_value = VBUS_IN_CURR_LIM_SHIFT; | ||
1146 | prev_curr_index = (reg_value >> shift_value); | ||
1147 | curr_index = ab8500_vbus_in_curr_to_regval(ich); | ||
1148 | step_udelay = STEP_UDELAY * 100; | ||
1149 | |||
1150 | ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, | ||
1151 | AB8500_CH_USBCH_STAT2_REG, ®_value); | ||
1152 | if (ret < 0) { | ||
1153 | dev_err(di->dev, "%s read failed\n", __func__); | ||
1154 | goto exit_set_current; | ||
1155 | } | ||
1156 | auto_curr_index = | ||
1157 | reg_value >> AUTO_VBUS_IN_CURR_LIM_SHIFT; | ||
1158 | |||
1159 | dev_dbg(di->dev, "%s Auto VBUS curr is %d mA\n", | ||
1160 | __func__, | ||
1161 | ab8500_charger_vbus_in_curr_map[auto_curr_index]); | ||
1162 | |||
1163 | prev_curr_index = min(prev_curr_index, auto_curr_index); | ||
1164 | |||
1165 | if (!di->usb.charger_connected) | ||
1166 | no_stepping = true; | ||
1167 | break; | ||
1168 | case AB8500_CH_OPT_CRNTLVL_REG: | ||
1169 | shift_value = 0; | ||
1170 | prev_curr_index = (reg_value >> shift_value); | ||
1171 | curr_index = ab8500_current_to_regval(ich); | ||
1172 | step_udelay = STEP_UDELAY; | ||
1173 | if (curr_index && (curr_index - prev_curr_index) > 1) | ||
1174 | step_udelay *= 100; | ||
1175 | |||
1176 | if (!di->usb.charger_connected && !di->ac.charger_connected) | ||
1177 | no_stepping = true; | ||
1178 | |||
1179 | break; | ||
1180 | default: | ||
1181 | dev_err(di->dev, "%s current register not valid\n", __func__); | ||
1182 | ret = -ENXIO; | ||
1183 | goto exit_set_current; | ||
1184 | } | ||
1185 | |||
1186 | if (curr_index < 0) { | ||
1187 | dev_err(di->dev, "requested current limit out-of-range\n"); | ||
1188 | ret = -ENXIO; | ||
1189 | goto exit_set_current; | ||
1190 | } | ||
1191 | |||
1192 | /* only update current if it's been changed */ | ||
1193 | if (prev_curr_index == curr_index) { | ||
1194 | dev_dbg(di->dev, "%s current not changed for reg: 0x%02x\n", | ||
1195 | __func__, reg); | ||
1196 | ret = 0; | ||
1197 | goto exit_set_current; | ||
1198 | } | ||
1199 | |||
1200 | dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n", | ||
1201 | __func__, ich, reg); | ||
1202 | |||
1203 | if (no_stepping) { | ||
1204 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, | ||
1205 | reg, (u8)curr_index << shift_value); | ||
1206 | if (ret) | ||
1207 | dev_err(di->dev, "%s write failed\n", __func__); | ||
1208 | } else if (prev_curr_index > curr_index) { | ||
1209 | for (i = prev_curr_index - 1; i >= curr_index; i--) { | ||
1210 | dev_dbg(di->dev, "curr change_1 to: %x for 0x%02x\n", | ||
1211 | (u8) i << shift_value, reg); | ||
1212 | ret = abx500_set_register_interruptible(di->dev, | ||
1213 | AB8500_CHARGER, reg, (u8)i << shift_value); | ||
1214 | if (ret) { | ||
1215 | dev_err(di->dev, "%s write failed\n", __func__); | ||
1216 | goto exit_set_current; | ||
1217 | } | ||
1218 | if (i != curr_index) | ||
1219 | usleep_range(step_udelay, step_udelay * 2); | ||
1220 | } | ||
1221 | } else { | ||
1222 | for (i = prev_curr_index + 1; i <= curr_index; i++) { | ||
1223 | dev_dbg(di->dev, "curr change_2 to: %x for 0x%02x\n", | ||
1224 | (u8)i << shift_value, reg); | ||
1225 | ret = abx500_set_register_interruptible(di->dev, | ||
1226 | AB8500_CHARGER, reg, (u8)i << shift_value); | ||
1227 | if (ret) { | ||
1228 | dev_err(di->dev, "%s write failed\n", __func__); | ||
1229 | goto exit_set_current; | ||
1230 | } | ||
1231 | if (i != curr_index) | ||
1232 | usleep_range(step_udelay, step_udelay * 2); | ||
1233 | } | ||
1234 | } | ||
1235 | |||
1236 | exit_set_current: | ||
1237 | atomic_dec(&di->current_stepping_sessions); | ||
1238 | |||
1239 | return ret; | ||
1240 | } | ||
1241 | |||
1242 | /** | ||
939 | * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit | 1243 | * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit |
940 | * @di: pointer to the ab8500_charger structure | 1244 | * @di: pointer to the ab8500_charger structure |
941 | * @ich_in: charger input current limit | 1245 | * @ich_in: charger input current limit |
@@ -946,12 +1250,11 @@ static int ab8500_charger_get_usb_cur(struct ab8500_charger *di) | |||
946 | static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di, | 1250 | static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di, |
947 | int ich_in) | 1251 | int ich_in) |
948 | { | 1252 | { |
949 | int ret; | ||
950 | int input_curr_index; | ||
951 | int min_value; | 1253 | int min_value; |
1254 | int ret; | ||
952 | 1255 | ||
953 | /* We should always use to lowest current limit */ | 1256 | /* We should always use to lowest current limit */ |
954 | min_value = min(di->bat->chg_params->usb_curr_max, ich_in); | 1257 | min_value = min(di->bm->chg_params->usb_curr_max, ich_in); |
955 | 1258 | ||
956 | switch (min_value) { | 1259 | switch (min_value) { |
957 | case 100: | 1260 | case 100: |
@@ -966,22 +1269,47 @@ static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di, | |||
966 | break; | 1269 | break; |
967 | } | 1270 | } |
968 | 1271 | ||
969 | input_curr_index = ab8500_vbus_in_curr_to_regval(min_value); | 1272 | dev_info(di->dev, "VBUS input current limit set to %d mA\n", min_value); |
970 | if (input_curr_index < 0) { | ||
971 | dev_err(di->dev, "VBUS input current limit too high\n"); | ||
972 | return -ENXIO; | ||
973 | } | ||
974 | 1273 | ||
975 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, | 1274 | mutex_lock(&di->usb_ipt_crnt_lock); |
976 | AB8500_USBCH_IPT_CRNTLVL_REG, | 1275 | ret = ab8500_charger_set_current(di, min_value, |
977 | input_curr_index << VBUS_IN_CURR_LIM_SHIFT); | 1276 | AB8500_USBCH_IPT_CRNTLVL_REG); |
978 | if (ret) | 1277 | mutex_unlock(&di->usb_ipt_crnt_lock); |
979 | dev_err(di->dev, "%s write failed\n", __func__); | ||
980 | 1278 | ||
981 | return ret; | 1279 | return ret; |
982 | } | 1280 | } |
983 | 1281 | ||
984 | /** | 1282 | /** |
1283 | * ab8500_charger_set_main_in_curr() - set main charger input current | ||
1284 | * @di: pointer to the ab8500_charger structure | ||
1285 | * @ich_in: input charger current, in mA | ||
1286 | * | ||
1287 | * Set main charger input current. | ||
1288 | * Returns error code in case of failure else 0(on success) | ||
1289 | */ | ||
1290 | static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di, | ||
1291 | int ich_in) | ||
1292 | { | ||
1293 | return ab8500_charger_set_current(di, ich_in, | ||
1294 | AB8500_MCH_IPT_CURLVL_REG); | ||
1295 | } | ||
1296 | |||
1297 | /** | ||
1298 | * ab8500_charger_set_output_curr() - set charger output current | ||
1299 | * @di: pointer to the ab8500_charger structure | ||
1300 | * @ich_out: output charger current, in mA | ||
1301 | * | ||
1302 | * Set charger output current. | ||
1303 | * Returns error code in case of failure else 0(on success) | ||
1304 | */ | ||
1305 | static int ab8500_charger_set_output_curr(struct ab8500_charger *di, | ||
1306 | int ich_out) | ||
1307 | { | ||
1308 | return ab8500_charger_set_current(di, ich_out, | ||
1309 | AB8500_CH_OPT_CRNTLVL_REG); | ||
1310 | } | ||
1311 | |||
1312 | /** | ||
985 | * ab8500_charger_led_en() - turn on/off chargign led | 1313 | * ab8500_charger_led_en() - turn on/off chargign led |
986 | * @di: pointer to the ab8500_charger structure | 1314 | * @di: pointer to the ab8500_charger structure |
987 | * @on: flag to turn on/off the chargign led | 1315 | * @on: flag to turn on/off the chargign led |
@@ -1074,7 +1402,7 @@ static int ab8500_charger_ac_en(struct ux500_charger *charger, | |||
1074 | volt_index = ab8500_voltage_to_regval(vset); | 1402 | volt_index = ab8500_voltage_to_regval(vset); |
1075 | curr_index = ab8500_current_to_regval(iset); | 1403 | curr_index = ab8500_current_to_regval(iset); |
1076 | input_curr_index = ab8500_current_to_regval( | 1404 | input_curr_index = ab8500_current_to_regval( |
1077 | di->bat->chg_params->ac_curr_max); | 1405 | di->bm->chg_params->ac_curr_max); |
1078 | if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) { | 1406 | if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) { |
1079 | dev_err(di->dev, | 1407 | dev_err(di->dev, |
1080 | "Charger voltage or current too high, " | 1408 | "Charger voltage or current too high, " |
@@ -1090,23 +1418,24 @@ static int ab8500_charger_ac_en(struct ux500_charger *charger, | |||
1090 | return ret; | 1418 | return ret; |
1091 | } | 1419 | } |
1092 | /* MainChInputCurr: current that can be drawn from the charger*/ | 1420 | /* MainChInputCurr: current that can be drawn from the charger*/ |
1093 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, | 1421 | ret = ab8500_charger_set_main_in_curr(di, |
1094 | AB8500_MCH_IPT_CURLVL_REG, | 1422 | di->bm->chg_params->ac_curr_max); |
1095 | input_curr_index << MAIN_CH_INPUT_CURR_SHIFT); | ||
1096 | if (ret) { | 1423 | if (ret) { |
1097 | dev_err(di->dev, "%s write failed\n", __func__); | 1424 | dev_err(di->dev, "%s Failed to set MainChInputCurr\n", |
1425 | __func__); | ||
1098 | return ret; | 1426 | return ret; |
1099 | } | 1427 | } |
1100 | /* ChOutputCurentLevel: protected output current */ | 1428 | /* ChOutputCurentLevel: protected output current */ |
1101 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, | 1429 | ret = ab8500_charger_set_output_curr(di, iset); |
1102 | AB8500_CH_OPT_CRNTLVL_REG, (u8) curr_index); | ||
1103 | if (ret) { | 1430 | if (ret) { |
1104 | dev_err(di->dev, "%s write failed\n", __func__); | 1431 | dev_err(di->dev, "%s " |
1432 | "Failed to set ChOutputCurentLevel\n", | ||
1433 | __func__); | ||
1105 | return ret; | 1434 | return ret; |
1106 | } | 1435 | } |
1107 | 1436 | ||
1108 | /* Check if VBAT overshoot control should be enabled */ | 1437 | /* Check if VBAT overshoot control should be enabled */ |
1109 | if (!di->bat->enable_overshoot) | 1438 | if (!di->bm->enable_overshoot) |
1110 | overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N; | 1439 | overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N; |
1111 | 1440 | ||
1112 | /* Enable Main Charger */ | 1441 | /* Enable Main Charger */ |
@@ -1158,12 +1487,11 @@ static int ab8500_charger_ac_en(struct ux500_charger *charger, | |||
1158 | return ret; | 1487 | return ret; |
1159 | } | 1488 | } |
1160 | 1489 | ||
1161 | ret = abx500_set_register_interruptible(di->dev, | 1490 | ret = ab8500_charger_set_output_curr(di, 0); |
1162 | AB8500_CHARGER, | ||
1163 | AB8500_CH_OPT_CRNTLVL_REG, CH_OP_CUR_LVL_0P1); | ||
1164 | if (ret) { | 1491 | if (ret) { |
1165 | dev_err(di->dev, | 1492 | dev_err(di->dev, "%s " |
1166 | "%s write failed\n", __func__); | 1493 | "Failed to set ChOutputCurentLevel\n", |
1494 | __func__); | ||
1167 | return ret; | 1495 | return ret; |
1168 | } | 1496 | } |
1169 | } else { | 1497 | } else { |
@@ -1259,24 +1587,13 @@ static int ab8500_charger_usb_en(struct ux500_charger *charger, | |||
1259 | dev_err(di->dev, "%s write failed\n", __func__); | 1587 | dev_err(di->dev, "%s write failed\n", __func__); |
1260 | return ret; | 1588 | return ret; |
1261 | } | 1589 | } |
1262 | /* USBChInputCurr: current that can be drawn from the usb */ | ||
1263 | ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr); | ||
1264 | if (ret) { | ||
1265 | dev_err(di->dev, "setting USBChInputCurr failed\n"); | ||
1266 | return ret; | ||
1267 | } | ||
1268 | /* ChOutputCurentLevel: protected output current */ | ||
1269 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, | ||
1270 | AB8500_CH_OPT_CRNTLVL_REG, (u8) curr_index); | ||
1271 | if (ret) { | ||
1272 | dev_err(di->dev, "%s write failed\n", __func__); | ||
1273 | return ret; | ||
1274 | } | ||
1275 | /* Check if VBAT overshoot control should be enabled */ | 1590 | /* Check if VBAT overshoot control should be enabled */ |
1276 | if (!di->bat->enable_overshoot) | 1591 | if (!di->bm->enable_overshoot) |
1277 | overshoot = USB_CHG_NO_OVERSHOOT_ENA_N; | 1592 | overshoot = USB_CHG_NO_OVERSHOOT_ENA_N; |
1278 | 1593 | ||
1279 | /* Enable USB Charger */ | 1594 | /* Enable USB Charger */ |
1595 | dev_dbg(di->dev, | ||
1596 | "Enabling USB with write to AB8500_USBCH_CTRL1_REG\n"); | ||
1280 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, | 1597 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, |
1281 | AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot); | 1598 | AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot); |
1282 | if (ret) { | 1599 | if (ret) { |
@@ -1289,11 +1606,29 @@ static int ab8500_charger_usb_en(struct ux500_charger *charger, | |||
1289 | if (ret < 0) | 1606 | if (ret < 0) |
1290 | dev_err(di->dev, "failed to enable LED\n"); | 1607 | dev_err(di->dev, "failed to enable LED\n"); |
1291 | 1608 | ||
1609 | di->usb.charger_online = 1; | ||
1610 | |||
1611 | /* USBChInputCurr: current that can be drawn from the usb */ | ||
1612 | ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr); | ||
1613 | if (ret) { | ||
1614 | dev_err(di->dev, "setting USBChInputCurr failed\n"); | ||
1615 | return ret; | ||
1616 | } | ||
1617 | |||
1618 | /* ChOutputCurentLevel: protected output current */ | ||
1619 | ret = ab8500_charger_set_output_curr(di, ich_out); | ||
1620 | if (ret) { | ||
1621 | dev_err(di->dev, "%s " | ||
1622 | "Failed to set ChOutputCurentLevel\n", | ||
1623 | __func__); | ||
1624 | return ret; | ||
1625 | } | ||
1626 | |||
1292 | queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ); | 1627 | queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ); |
1293 | 1628 | ||
1294 | di->usb.charger_online = 1; | ||
1295 | } else { | 1629 | } else { |
1296 | /* Disable USB charging */ | 1630 | /* Disable USB charging */ |
1631 | dev_dbg(di->dev, "%s Disabled USB charging\n", __func__); | ||
1297 | ret = abx500_set_register_interruptible(di->dev, | 1632 | ret = abx500_set_register_interruptible(di->dev, |
1298 | AB8500_CHARGER, | 1633 | AB8500_CHARGER, |
1299 | AB8500_USBCH_CTRL1_REG, 0); | 1634 | AB8500_USBCH_CTRL1_REG, 0); |
@@ -1306,7 +1641,21 @@ static int ab8500_charger_usb_en(struct ux500_charger *charger, | |||
1306 | ret = ab8500_charger_led_en(di, false); | 1641 | ret = ab8500_charger_led_en(di, false); |
1307 | if (ret < 0) | 1642 | if (ret < 0) |
1308 | dev_err(di->dev, "failed to disable LED\n"); | 1643 | dev_err(di->dev, "failed to disable LED\n"); |
1644 | /* USBChInputCurr: current that can be drawn from the usb */ | ||
1645 | ret = ab8500_charger_set_vbus_in_curr(di, 0); | ||
1646 | if (ret) { | ||
1647 | dev_err(di->dev, "setting USBChInputCurr failed\n"); | ||
1648 | return ret; | ||
1649 | } | ||
1309 | 1650 | ||
1651 | /* ChOutputCurentLevel: protected output current */ | ||
1652 | ret = ab8500_charger_set_output_curr(di, 0); | ||
1653 | if (ret) { | ||
1654 | dev_err(di->dev, "%s " | ||
1655 | "Failed to reset ChOutputCurentLevel\n", | ||
1656 | __func__); | ||
1657 | return ret; | ||
1658 | } | ||
1310 | di->usb.charger_online = 0; | 1659 | di->usb.charger_online = 0; |
1311 | di->usb.wd_expired = false; | 1660 | di->usb.wd_expired = false; |
1312 | 1661 | ||
@@ -1366,7 +1715,6 @@ static int ab8500_charger_update_charger_current(struct ux500_charger *charger, | |||
1366 | int ich_out) | 1715 | int ich_out) |
1367 | { | 1716 | { |
1368 | int ret; | 1717 | int ret; |
1369 | int curr_index; | ||
1370 | struct ab8500_charger *di; | 1718 | struct ab8500_charger *di; |
1371 | 1719 | ||
1372 | if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS) | 1720 | if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS) |
@@ -1376,18 +1724,11 @@ static int ab8500_charger_update_charger_current(struct ux500_charger *charger, | |||
1376 | else | 1724 | else |
1377 | return -ENXIO; | 1725 | return -ENXIO; |
1378 | 1726 | ||
1379 | curr_index = ab8500_current_to_regval(ich_out); | 1727 | ret = ab8500_charger_set_output_curr(di, ich_out); |
1380 | if (curr_index < 0) { | ||
1381 | dev_err(di->dev, | ||
1382 | "Charger current too high, " | ||
1383 | "charging not started\n"); | ||
1384 | return -ENXIO; | ||
1385 | } | ||
1386 | |||
1387 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, | ||
1388 | AB8500_CH_OPT_CRNTLVL_REG, (u8) curr_index); | ||
1389 | if (ret) { | 1728 | if (ret) { |
1390 | dev_err(di->dev, "%s write failed\n", __func__); | 1729 | dev_err(di->dev, "%s " |
1730 | "Failed to set ChOutputCurentLevel\n", | ||
1731 | __func__); | ||
1391 | return ret; | 1732 | return ret; |
1392 | } | 1733 | } |
1393 | 1734 | ||
@@ -1597,7 +1938,7 @@ static void ab8500_charger_ac_work(struct work_struct *work) | |||
1597 | * synchronously, we have the check if the main charger is | 1938 | * synchronously, we have the check if the main charger is |
1598 | * connected by reading the status register | 1939 | * connected by reading the status register |
1599 | */ | 1940 | */ |
1600 | ret = ab8500_charger_detect_chargers(di); | 1941 | ret = ab8500_charger_detect_chargers(di, false); |
1601 | if (ret < 0) | 1942 | if (ret < 0) |
1602 | return; | 1943 | return; |
1603 | 1944 | ||
@@ -1612,6 +1953,84 @@ static void ab8500_charger_ac_work(struct work_struct *work) | |||
1612 | sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present"); | 1953 | sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present"); |
1613 | } | 1954 | } |
1614 | 1955 | ||
1956 | static void ab8500_charger_usb_attached_work(struct work_struct *work) | ||
1957 | { | ||
1958 | struct ab8500_charger *di = container_of(work, | ||
1959 | struct ab8500_charger, | ||
1960 | usb_charger_attached_work.work); | ||
1961 | int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC); | ||
1962 | int ret, i; | ||
1963 | u8 statval; | ||
1964 | |||
1965 | for (i = 0; i < 10; i++) { | ||
1966 | ret = abx500_get_register_interruptible(di->dev, | ||
1967 | AB8500_CHARGER, | ||
1968 | AB8500_CH_USBCH_STAT1_REG, | ||
1969 | &statval); | ||
1970 | if (ret < 0) { | ||
1971 | dev_err(di->dev, "ab8500 read failed %d\n", __LINE__); | ||
1972 | goto reschedule; | ||
1973 | } | ||
1974 | if ((statval & usbch) != usbch) | ||
1975 | goto reschedule; | ||
1976 | |||
1977 | msleep(CHARGER_STATUS_POLL); | ||
1978 | } | ||
1979 | |||
1980 | ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0); | ||
1981 | |||
1982 | mutex_lock(&di->charger_attached_mutex); | ||
1983 | mutex_unlock(&di->charger_attached_mutex); | ||
1984 | |||
1985 | return; | ||
1986 | |||
1987 | reschedule: | ||
1988 | queue_delayed_work(di->charger_wq, | ||
1989 | &di->usb_charger_attached_work, | ||
1990 | HZ); | ||
1991 | } | ||
1992 | |||
1993 | static void ab8500_charger_ac_attached_work(struct work_struct *work) | ||
1994 | { | ||
1995 | |||
1996 | struct ab8500_charger *di = container_of(work, | ||
1997 | struct ab8500_charger, | ||
1998 | ac_charger_attached_work.work); | ||
1999 | int mainch = (MAIN_CH_STATUS2_MAINCHGDROP | | ||
2000 | MAIN_CH_STATUS2_MAINCHARGERDETDBNC); | ||
2001 | int ret, i; | ||
2002 | u8 statval; | ||
2003 | |||
2004 | for (i = 0; i < 10; i++) { | ||
2005 | ret = abx500_get_register_interruptible(di->dev, | ||
2006 | AB8500_CHARGER, | ||
2007 | AB8500_CH_STATUS2_REG, | ||
2008 | &statval); | ||
2009 | if (ret < 0) { | ||
2010 | dev_err(di->dev, "ab8500 read failed %d\n", __LINE__); | ||
2011 | goto reschedule; | ||
2012 | } | ||
2013 | |||
2014 | if ((statval & mainch) != mainch) | ||
2015 | goto reschedule; | ||
2016 | |||
2017 | msleep(CHARGER_STATUS_POLL); | ||
2018 | } | ||
2019 | |||
2020 | ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0); | ||
2021 | queue_work(di->charger_wq, &di->ac_work); | ||
2022 | |||
2023 | mutex_lock(&di->charger_attached_mutex); | ||
2024 | mutex_unlock(&di->charger_attached_mutex); | ||
2025 | |||
2026 | return; | ||
2027 | |||
2028 | reschedule: | ||
2029 | queue_delayed_work(di->charger_wq, | ||
2030 | &di->ac_charger_attached_work, | ||
2031 | HZ); | ||
2032 | } | ||
2033 | |||
1615 | /** | 2034 | /** |
1616 | * ab8500_charger_detect_usb_type_work() - work to detect USB type | 2035 | * ab8500_charger_detect_usb_type_work() - work to detect USB type |
1617 | * @work: Pointer to the work_struct structure | 2036 | * @work: Pointer to the work_struct structure |
@@ -1630,16 +2049,18 @@ static void ab8500_charger_detect_usb_type_work(struct work_struct *work) | |||
1630 | * synchronously, we have the check if is | 2049 | * synchronously, we have the check if is |
1631 | * connected by reading the status register | 2050 | * connected by reading the status register |
1632 | */ | 2051 | */ |
1633 | ret = ab8500_charger_detect_chargers(di); | 2052 | ret = ab8500_charger_detect_chargers(di, false); |
1634 | if (ret < 0) | 2053 | if (ret < 0) |
1635 | return; | 2054 | return; |
1636 | 2055 | ||
1637 | if (!(ret & USB_PW_CONN)) { | 2056 | if (!(ret & USB_PW_CONN)) { |
1638 | di->vbus_detected = 0; | 2057 | dev_dbg(di->dev, "%s di->vbus_detected = false\n", __func__); |
2058 | di->vbus_detected = false; | ||
1639 | ab8500_charger_set_usb_connected(di, false); | 2059 | ab8500_charger_set_usb_connected(di, false); |
1640 | ab8500_power_supply_changed(di, &di->usb_chg.psy); | 2060 | ab8500_power_supply_changed(di, &di->usb_chg.psy); |
1641 | } else { | 2061 | } else { |
1642 | di->vbus_detected = 1; | 2062 | dev_dbg(di->dev, "%s di->vbus_detected = true\n", __func__); |
2063 | di->vbus_detected = true; | ||
1643 | 2064 | ||
1644 | if (is_ab8500_1p1_or_earlier(di->parent)) { | 2065 | if (is_ab8500_1p1_or_earlier(di->parent)) { |
1645 | ret = ab8500_charger_detect_usb_type(di); | 2066 | ret = ab8500_charger_detect_usb_type(di); |
@@ -1649,7 +2070,8 @@ static void ab8500_charger_detect_usb_type_work(struct work_struct *work) | |||
1649 | &di->usb_chg.psy); | 2070 | &di->usb_chg.psy); |
1650 | } | 2071 | } |
1651 | } else { | 2072 | } else { |
1652 | /* For ABB cut2.0 and onwards we have an IRQ, | 2073 | /* |
2074 | * For ABB cut2.0 and onwards we have an IRQ, | ||
1653 | * USB_LINK_STATUS that will be triggered when the USB | 2075 | * USB_LINK_STATUS that will be triggered when the USB |
1654 | * link status changes. The exception is USB connected | 2076 | * link status changes. The exception is USB connected |
1655 | * during startup. Then we don't get a | 2077 | * during startup. Then we don't get a |
@@ -1670,6 +2092,29 @@ static void ab8500_charger_detect_usb_type_work(struct work_struct *work) | |||
1670 | } | 2092 | } |
1671 | 2093 | ||
1672 | /** | 2094 | /** |
2095 | * ab8500_charger_usb_link_attach_work() - work to detect USB type | ||
2096 | * @work: pointer to the work_struct structure | ||
2097 | * | ||
2098 | * Detect the type of USB plugged | ||
2099 | */ | ||
2100 | static void ab8500_charger_usb_link_attach_work(struct work_struct *work) | ||
2101 | { | ||
2102 | struct ab8500_charger *di = | ||
2103 | container_of(work, struct ab8500_charger, attach_work.work); | ||
2104 | int ret; | ||
2105 | |||
2106 | /* Update maximum input current if USB enumeration is not detected */ | ||
2107 | if (!di->usb.charger_online) { | ||
2108 | ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr); | ||
2109 | if (ret) | ||
2110 | return; | ||
2111 | } | ||
2112 | |||
2113 | ab8500_charger_set_usb_connected(di, true); | ||
2114 | ab8500_power_supply_changed(di, &di->usb_chg.psy); | ||
2115 | } | ||
2116 | |||
2117 | /** | ||
1673 | * ab8500_charger_usb_link_status_work() - work to detect USB type | 2118 | * ab8500_charger_usb_link_status_work() - work to detect USB type |
1674 | * @work: pointer to the work_struct structure | 2119 | * @work: pointer to the work_struct structure |
1675 | * | 2120 | * |
@@ -1677,7 +2122,9 @@ static void ab8500_charger_detect_usb_type_work(struct work_struct *work) | |||
1677 | */ | 2122 | */ |
1678 | static void ab8500_charger_usb_link_status_work(struct work_struct *work) | 2123 | static void ab8500_charger_usb_link_status_work(struct work_struct *work) |
1679 | { | 2124 | { |
2125 | int detected_chargers; | ||
1680 | int ret; | 2126 | int ret; |
2127 | u8 val; | ||
1681 | 2128 | ||
1682 | struct ab8500_charger *di = container_of(work, | 2129 | struct ab8500_charger *di = container_of(work, |
1683 | struct ab8500_charger, usb_link_status_work); | 2130 | struct ab8500_charger, usb_link_status_work); |
@@ -1687,31 +2134,95 @@ static void ab8500_charger_usb_link_status_work(struct work_struct *work) | |||
1687 | * synchronously, we have the check if is | 2134 | * synchronously, we have the check if is |
1688 | * connected by reading the status register | 2135 | * connected by reading the status register |
1689 | */ | 2136 | */ |
1690 | ret = ab8500_charger_detect_chargers(di); | 2137 | detected_chargers = ab8500_charger_detect_chargers(di, false); |
1691 | if (ret < 0) | 2138 | if (detected_chargers < 0) |
1692 | return; | 2139 | return; |
1693 | 2140 | ||
1694 | if (!(ret & USB_PW_CONN)) { | 2141 | /* |
1695 | di->vbus_detected = 0; | 2142 | * Some chargers that breaks the USB spec is |
2143 | * identified as invalid by AB8500 and it refuse | ||
2144 | * to start the charging process. but by jumping | ||
2145 | * thru a few hoops it can be forced to start. | ||
2146 | */ | ||
2147 | ret = abx500_get_register_interruptible(di->dev, AB8500_USB, | ||
2148 | AB8500_USB_LINE_STAT_REG, &val); | ||
2149 | if (ret >= 0) | ||
2150 | dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val); | ||
2151 | else | ||
2152 | dev_dbg(di->dev, "Error reading USB link status\n"); | ||
2153 | |||
2154 | if (detected_chargers & USB_PW_CONN) { | ||
2155 | if (((val & AB8500_USB_LINK_STATUS) >> 3) == USB_STAT_NOT_VALID_LINK && | ||
2156 | di->invalid_charger_detect_state == 0) { | ||
2157 | dev_dbg(di->dev, "Invalid charger detected, state= 0\n"); | ||
2158 | /*Enable charger*/ | ||
2159 | abx500_mask_and_set_register_interruptible(di->dev, | ||
2160 | AB8500_CHARGER, AB8500_USBCH_CTRL1_REG, 0x01, 0x01); | ||
2161 | /*Enable charger detection*/ | ||
2162 | abx500_mask_and_set_register_interruptible(di->dev, AB8500_USB, | ||
2163 | AB8500_MCH_IPT_CURLVL_REG, 0x01, 0x01); | ||
2164 | di->invalid_charger_detect_state = 1; | ||
2165 | /*exit and wait for new link status interrupt.*/ | ||
2166 | return; | ||
2167 | |||
2168 | } | ||
2169 | if (di->invalid_charger_detect_state == 1) { | ||
2170 | dev_dbg(di->dev, "Invalid charger detected, state= 1\n"); | ||
2171 | /*Stop charger detection*/ | ||
2172 | abx500_mask_and_set_register_interruptible(di->dev, AB8500_USB, | ||
2173 | AB8500_MCH_IPT_CURLVL_REG, 0x01, 0x00); | ||
2174 | /*Check link status*/ | ||
2175 | ret = abx500_get_register_interruptible(di->dev, AB8500_USB, | ||
2176 | AB8500_USB_LINE_STAT_REG, &val); | ||
2177 | dev_dbg(di->dev, "USB link status= 0x%02x\n", | ||
2178 | (val & AB8500_USB_LINK_STATUS) >> 3); | ||
2179 | di->invalid_charger_detect_state = 2; | ||
2180 | } | ||
2181 | } else { | ||
2182 | di->invalid_charger_detect_state = 0; | ||
2183 | } | ||
2184 | |||
2185 | if (!(detected_chargers & USB_PW_CONN)) { | ||
2186 | di->vbus_detected = false; | ||
1696 | ab8500_charger_set_usb_connected(di, false); | 2187 | ab8500_charger_set_usb_connected(di, false); |
1697 | ab8500_power_supply_changed(di, &di->usb_chg.psy); | 2188 | ab8500_power_supply_changed(di, &di->usb_chg.psy); |
1698 | } else { | 2189 | return; |
1699 | di->vbus_detected = 1; | 2190 | } |
1700 | ret = ab8500_charger_read_usb_type(di); | ||
1701 | if (!ret) { | ||
1702 | /* Update maximum input current */ | ||
1703 | ret = ab8500_charger_set_vbus_in_curr(di, | ||
1704 | di->max_usb_in_curr); | ||
1705 | if (ret) | ||
1706 | return; | ||
1707 | 2191 | ||
1708 | ab8500_charger_set_usb_connected(di, true); | 2192 | dev_dbg(di->dev,"%s di->vbus_detected = true\n",__func__); |
1709 | ab8500_power_supply_changed(di, &di->usb_chg.psy); | 2193 | di->vbus_detected = true; |
1710 | } else if (ret == -ENXIO) { | 2194 | ret = ab8500_charger_read_usb_type(di); |
2195 | if (ret) { | ||
2196 | if (ret == -ENXIO) { | ||
1711 | /* No valid charger type detected */ | 2197 | /* No valid charger type detected */ |
1712 | ab8500_charger_set_usb_connected(di, false); | 2198 | ab8500_charger_set_usb_connected(di, false); |
1713 | ab8500_power_supply_changed(di, &di->usb_chg.psy); | 2199 | ab8500_power_supply_changed(di, &di->usb_chg.psy); |
1714 | } | 2200 | } |
2201 | return; | ||
2202 | } | ||
2203 | |||
2204 | if (di->usb_device_is_unrecognised) { | ||
2205 | dev_dbg(di->dev, | ||
2206 | "Potential Legacy Charger device. " | ||
2207 | "Delay work for %d msec for USB enum " | ||
2208 | "to finish", | ||
2209 | WAIT_ACA_RID_ENUMERATION); | ||
2210 | queue_delayed_work(di->charger_wq, | ||
2211 | &di->attach_work, | ||
2212 | msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION)); | ||
2213 | } else if (di->is_aca_rid == 1) { | ||
2214 | /* Only wait once */ | ||
2215 | di->is_aca_rid++; | ||
2216 | dev_dbg(di->dev, | ||
2217 | "%s Wait %d msec for USB enum to finish", | ||
2218 | __func__, WAIT_ACA_RID_ENUMERATION); | ||
2219 | queue_delayed_work(di->charger_wq, | ||
2220 | &di->attach_work, | ||
2221 | msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION)); | ||
2222 | } else { | ||
2223 | queue_delayed_work(di->charger_wq, | ||
2224 | &di->attach_work, | ||
2225 | 0); | ||
1715 | } | 2226 | } |
1716 | } | 2227 | } |
1717 | 2228 | ||
@@ -1721,24 +2232,20 @@ static void ab8500_charger_usb_state_changed_work(struct work_struct *work) | |||
1721 | unsigned long flags; | 2232 | unsigned long flags; |
1722 | 2233 | ||
1723 | struct ab8500_charger *di = container_of(work, | 2234 | struct ab8500_charger *di = container_of(work, |
1724 | struct ab8500_charger, usb_state_changed_work); | 2235 | struct ab8500_charger, usb_state_changed_work.work); |
1725 | 2236 | ||
1726 | if (!di->vbus_detected) | 2237 | if (!di->vbus_detected) { |
2238 | dev_dbg(di->dev, | ||
2239 | "%s !di->vbus_detected\n", | ||
2240 | __func__); | ||
1727 | return; | 2241 | return; |
2242 | } | ||
1728 | 2243 | ||
1729 | spin_lock_irqsave(&di->usb_state.usb_lock, flags); | 2244 | spin_lock_irqsave(&di->usb_state.usb_lock, flags); |
1730 | di->usb_state.usb_changed = false; | 2245 | di->usb_state.state = di->usb_state.state_tmp; |
2246 | di->usb_state.usb_current = di->usb_state.usb_current_tmp; | ||
1731 | spin_unlock_irqrestore(&di->usb_state.usb_lock, flags); | 2247 | spin_unlock_irqrestore(&di->usb_state.usb_lock, flags); |
1732 | 2248 | ||
1733 | /* | ||
1734 | * wait for some time until you get updates from the usb stack | ||
1735 | * and negotiations are completed | ||
1736 | */ | ||
1737 | msleep(250); | ||
1738 | |||
1739 | if (di->usb_state.usb_changed) | ||
1740 | return; | ||
1741 | |||
1742 | dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n", | 2249 | dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n", |
1743 | __func__, di->usb_state.state, di->usb_state.usb_current); | 2250 | __func__, di->usb_state.state, di->usb_state.usb_current); |
1744 | 2251 | ||
@@ -1892,6 +2399,10 @@ static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di) | |||
1892 | dev_dbg(di->dev, "Main charger unplugged\n"); | 2399 | dev_dbg(di->dev, "Main charger unplugged\n"); |
1893 | queue_work(di->charger_wq, &di->ac_work); | 2400 | queue_work(di->charger_wq, &di->ac_work); |
1894 | 2401 | ||
2402 | cancel_delayed_work_sync(&di->ac_charger_attached_work); | ||
2403 | mutex_lock(&di->charger_attached_mutex); | ||
2404 | mutex_unlock(&di->charger_attached_mutex); | ||
2405 | |||
1895 | return IRQ_HANDLED; | 2406 | return IRQ_HANDLED; |
1896 | } | 2407 | } |
1897 | 2408 | ||
@@ -1909,6 +2420,11 @@ static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di) | |||
1909 | dev_dbg(di->dev, "Main charger plugged\n"); | 2420 | dev_dbg(di->dev, "Main charger plugged\n"); |
1910 | queue_work(di->charger_wq, &di->ac_work); | 2421 | queue_work(di->charger_wq, &di->ac_work); |
1911 | 2422 | ||
2423 | mutex_lock(&di->charger_attached_mutex); | ||
2424 | mutex_unlock(&di->charger_attached_mutex); | ||
2425 | queue_delayed_work(di->charger_wq, | ||
2426 | &di->ac_charger_attached_work, | ||
2427 | HZ); | ||
1912 | return IRQ_HANDLED; | 2428 | return IRQ_HANDLED; |
1913 | } | 2429 | } |
1914 | 2430 | ||
@@ -1971,6 +2487,21 @@ static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di) | |||
1971 | return IRQ_HANDLED; | 2487 | return IRQ_HANDLED; |
1972 | } | 2488 | } |
1973 | 2489 | ||
2490 | static void ab8500_charger_vbus_drop_end_work(struct work_struct *work) | ||
2491 | { | ||
2492 | struct ab8500_charger *di = container_of(work, | ||
2493 | struct ab8500_charger, vbus_drop_end_work.work); | ||
2494 | |||
2495 | di->flags.vbus_drop_end = false; | ||
2496 | |||
2497 | /* Reset the drop counter */ | ||
2498 | abx500_set_register_interruptible(di->dev, | ||
2499 | AB8500_CHARGER, AB8500_CHARGER_CTRL, 0x01); | ||
2500 | |||
2501 | if (di->usb.charger_connected) | ||
2502 | ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr); | ||
2503 | } | ||
2504 | |||
1974 | /** | 2505 | /** |
1975 | * ab8500_charger_vbusdetf_handler() - VBUS falling detected | 2506 | * ab8500_charger_vbusdetf_handler() - VBUS falling detected |
1976 | * @irq: interrupt number | 2507 | * @irq: interrupt number |
@@ -1982,6 +2513,7 @@ static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di) | |||
1982 | { | 2513 | { |
1983 | struct ab8500_charger *di = _di; | 2514 | struct ab8500_charger *di = _di; |
1984 | 2515 | ||
2516 | di->vbus_detected = false; | ||
1985 | dev_dbg(di->dev, "VBUS falling detected\n"); | 2517 | dev_dbg(di->dev, "VBUS falling detected\n"); |
1986 | queue_work(di->charger_wq, &di->detect_usb_type_work); | 2518 | queue_work(di->charger_wq, &di->detect_usb_type_work); |
1987 | 2519 | ||
@@ -2001,6 +2533,7 @@ static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di) | |||
2001 | 2533 | ||
2002 | di->vbus_detected = true; | 2534 | di->vbus_detected = true; |
2003 | dev_dbg(di->dev, "VBUS rising detected\n"); | 2535 | dev_dbg(di->dev, "VBUS rising detected\n"); |
2536 | |||
2004 | queue_work(di->charger_wq, &di->detect_usb_type_work); | 2537 | queue_work(di->charger_wq, &di->detect_usb_type_work); |
2005 | 2538 | ||
2006 | return IRQ_HANDLED; | 2539 | return IRQ_HANDLED; |
@@ -2109,6 +2642,25 @@ static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di) | |||
2109 | } | 2642 | } |
2110 | 2643 | ||
2111 | /** | 2644 | /** |
2645 | * ab8500_charger_vbuschdropend_handler() - VBUS drop removed | ||
2646 | * @irq: interrupt number | ||
2647 | * @_di: pointer to the ab8500_charger structure | ||
2648 | * | ||
2649 | * Returns IRQ status(IRQ_HANDLED) | ||
2650 | */ | ||
2651 | static irqreturn_t ab8500_charger_vbuschdropend_handler(int irq, void *_di) | ||
2652 | { | ||
2653 | struct ab8500_charger *di = _di; | ||
2654 | |||
2655 | dev_dbg(di->dev, "VBUS charger drop ended\n"); | ||
2656 | di->flags.vbus_drop_end = true; | ||
2657 | queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work, | ||
2658 | round_jiffies(30 * HZ)); | ||
2659 | |||
2660 | return IRQ_HANDLED; | ||
2661 | } | ||
2662 | |||
2663 | /** | ||
2112 | * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected | 2664 | * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected |
2113 | * @irq: interrupt number | 2665 | * @irq: interrupt number |
2114 | * @_di: pointer to the ab8500_charger structure | 2666 | * @_di: pointer to the ab8500_charger structure |
@@ -2148,6 +2700,7 @@ static int ab8500_charger_ac_get_property(struct power_supply *psy, | |||
2148 | union power_supply_propval *val) | 2700 | union power_supply_propval *val) |
2149 | { | 2701 | { |
2150 | struct ab8500_charger *di; | 2702 | struct ab8500_charger *di; |
2703 | int ret; | ||
2151 | 2704 | ||
2152 | di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy)); | 2705 | di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy)); |
2153 | 2706 | ||
@@ -2169,7 +2722,10 @@ static int ab8500_charger_ac_get_property(struct power_supply *psy, | |||
2169 | val->intval = di->ac.charger_connected; | 2722 | val->intval = di->ac.charger_connected; |
2170 | break; | 2723 | break; |
2171 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: | 2724 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
2172 | di->ac.charger_voltage = ab8500_charger_get_ac_voltage(di); | 2725 | ret = ab8500_charger_get_ac_voltage(di); |
2726 | if (ret >= 0) | ||
2727 | di->ac.charger_voltage = ret; | ||
2728 | /* On error, use previous value */ | ||
2173 | val->intval = di->ac.charger_voltage * 1000; | 2729 | val->intval = di->ac.charger_voltage * 1000; |
2174 | break; | 2730 | break; |
2175 | case POWER_SUPPLY_PROP_VOLTAGE_AVG: | 2731 | case POWER_SUPPLY_PROP_VOLTAGE_AVG: |
@@ -2181,7 +2737,10 @@ static int ab8500_charger_ac_get_property(struct power_supply *psy, | |||
2181 | val->intval = di->ac.cv_active; | 2737 | val->intval = di->ac.cv_active; |
2182 | break; | 2738 | break; |
2183 | case POWER_SUPPLY_PROP_CURRENT_NOW: | 2739 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
2184 | val->intval = ab8500_charger_get_ac_current(di) * 1000; | 2740 | ret = ab8500_charger_get_ac_current(di); |
2741 | if (ret >= 0) | ||
2742 | di->ac.charger_current = ret; | ||
2743 | val->intval = di->ac.charger_current * 1000; | ||
2185 | break; | 2744 | break; |
2186 | default: | 2745 | default: |
2187 | return -EINVAL; | 2746 | return -EINVAL; |
@@ -2208,6 +2767,7 @@ static int ab8500_charger_usb_get_property(struct power_supply *psy, | |||
2208 | union power_supply_propval *val) | 2767 | union power_supply_propval *val) |
2209 | { | 2768 | { |
2210 | struct ab8500_charger *di; | 2769 | struct ab8500_charger *di; |
2770 | int ret; | ||
2211 | 2771 | ||
2212 | di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy)); | 2772 | di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy)); |
2213 | 2773 | ||
@@ -2231,7 +2791,9 @@ static int ab8500_charger_usb_get_property(struct power_supply *psy, | |||
2231 | val->intval = di->usb.charger_connected; | 2791 | val->intval = di->usb.charger_connected; |
2232 | break; | 2792 | break; |
2233 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: | 2793 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
2234 | di->usb.charger_voltage = ab8500_charger_get_vbus_voltage(di); | 2794 | ret = ab8500_charger_get_vbus_voltage(di); |
2795 | if (ret >= 0) | ||
2796 | di->usb.charger_voltage = ret; | ||
2235 | val->intval = di->usb.charger_voltage * 1000; | 2797 | val->intval = di->usb.charger_voltage * 1000; |
2236 | break; | 2798 | break; |
2237 | case POWER_SUPPLY_PROP_VOLTAGE_AVG: | 2799 | case POWER_SUPPLY_PROP_VOLTAGE_AVG: |
@@ -2243,7 +2805,10 @@ static int ab8500_charger_usb_get_property(struct power_supply *psy, | |||
2243 | val->intval = di->usb.cv_active; | 2805 | val->intval = di->usb.cv_active; |
2244 | break; | 2806 | break; |
2245 | case POWER_SUPPLY_PROP_CURRENT_NOW: | 2807 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
2246 | val->intval = ab8500_charger_get_usb_current(di) * 1000; | 2808 | ret = ab8500_charger_get_usb_current(di); |
2809 | if (ret >= 0) | ||
2810 | di->usb.charger_current = ret; | ||
2811 | val->intval = di->usb.charger_current * 1000; | ||
2247 | break; | 2812 | break; |
2248 | case POWER_SUPPLY_PROP_CURRENT_AVG: | 2813 | case POWER_SUPPLY_PROP_CURRENT_AVG: |
2249 | /* | 2814 | /* |
@@ -2293,13 +2858,23 @@ static int ab8500_charger_init_hw_registers(struct ab8500_charger *di) | |||
2293 | } | 2858 | } |
2294 | } | 2859 | } |
2295 | 2860 | ||
2296 | /* VBUS OVV set to 6.3V and enable automatic current limitiation */ | 2861 | if (is_ab9540_2p0(di->parent) || is_ab8505_2p0(di->parent)) |
2297 | ret = abx500_set_register_interruptible(di->dev, | 2862 | ret = abx500_mask_and_set_register_interruptible(di->dev, |
2298 | AB8500_CHARGER, | 2863 | AB8500_CHARGER, |
2299 | AB8500_USBCH_CTRL2_REG, | 2864 | AB8500_USBCH_CTRL2_REG, |
2300 | VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA); | 2865 | VBUS_AUTO_IN_CURR_LIM_ENA, |
2866 | VBUS_AUTO_IN_CURR_LIM_ENA); | ||
2867 | else | ||
2868 | /* | ||
2869 | * VBUS OVV set to 6.3V and enable automatic current limitation | ||
2870 | */ | ||
2871 | ret = abx500_set_register_interruptible(di->dev, | ||
2872 | AB8500_CHARGER, | ||
2873 | AB8500_USBCH_CTRL2_REG, | ||
2874 | VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA); | ||
2301 | if (ret) { | 2875 | if (ret) { |
2302 | dev_err(di->dev, "failed to set VBUS OVV\n"); | 2876 | dev_err(di->dev, |
2877 | "failed to set automatic current limitation\n"); | ||
2303 | goto out; | 2878 | goto out; |
2304 | } | 2879 | } |
2305 | 2880 | ||
@@ -2355,12 +2930,26 @@ static int ab8500_charger_init_hw_registers(struct ab8500_charger *di) | |||
2355 | goto out; | 2930 | goto out; |
2356 | } | 2931 | } |
2357 | 2932 | ||
2933 | /* Set charger watchdog timeout */ | ||
2934 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, | ||
2935 | AB8500_CH_WD_TIMER_REG, WD_TIMER); | ||
2936 | if (ret) { | ||
2937 | dev_err(di->dev, "failed to set charger watchdog timeout\n"); | ||
2938 | goto out; | ||
2939 | } | ||
2940 | |||
2941 | ret = ab8500_charger_led_en(di, false); | ||
2942 | if (ret < 0) { | ||
2943 | dev_err(di->dev, "failed to disable LED\n"); | ||
2944 | goto out; | ||
2945 | } | ||
2946 | |||
2358 | /* Backup battery voltage and current */ | 2947 | /* Backup battery voltage and current */ |
2359 | ret = abx500_set_register_interruptible(di->dev, | 2948 | ret = abx500_set_register_interruptible(di->dev, |
2360 | AB8500_RTC, | 2949 | AB8500_RTC, |
2361 | AB8500_RTC_BACKUP_CHG_REG, | 2950 | AB8500_RTC_BACKUP_CHG_REG, |
2362 | di->bat->bkup_bat_v | | 2951 | di->bm->bkup_bat_v | |
2363 | di->bat->bkup_bat_i); | 2952 | di->bm->bkup_bat_i); |
2364 | if (ret) { | 2953 | if (ret) { |
2365 | dev_err(di->dev, "failed to setup backup battery charging\n"); | 2954 | dev_err(di->dev, "failed to setup backup battery charging\n"); |
2366 | goto out; | 2955 | goto out; |
@@ -2394,6 +2983,7 @@ static struct ab8500_charger_interrupts ab8500_charger_irq[] = { | |||
2394 | {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler}, | 2983 | {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler}, |
2395 | {"VBUS_OVV", ab8500_charger_vbusovv_handler}, | 2984 | {"VBUS_OVV", ab8500_charger_vbusovv_handler}, |
2396 | {"CH_WD_EXP", ab8500_charger_chwdexp_handler}, | 2985 | {"CH_WD_EXP", ab8500_charger_chwdexp_handler}, |
2986 | {"VBUS_CH_DROP_END", ab8500_charger_vbuschdropend_handler}, | ||
2397 | }; | 2987 | }; |
2398 | 2988 | ||
2399 | static int ab8500_charger_usb_notifier_call(struct notifier_block *nb, | 2989 | static int ab8500_charger_usb_notifier_call(struct notifier_block *nb, |
@@ -2404,6 +2994,9 @@ static int ab8500_charger_usb_notifier_call(struct notifier_block *nb, | |||
2404 | enum ab8500_usb_state bm_usb_state; | 2994 | enum ab8500_usb_state bm_usb_state; |
2405 | unsigned mA = *((unsigned *)power); | 2995 | unsigned mA = *((unsigned *)power); |
2406 | 2996 | ||
2997 | if (!di) | ||
2998 | return NOTIFY_DONE; | ||
2999 | |||
2407 | if (event != USB_EVENT_VBUS) { | 3000 | if (event != USB_EVENT_VBUS) { |
2408 | dev_dbg(di->dev, "not a standard host, returning\n"); | 3001 | dev_dbg(di->dev, "not a standard host, returning\n"); |
2409 | return NOTIFY_DONE; | 3002 | return NOTIFY_DONE; |
@@ -2427,13 +3020,15 @@ static int ab8500_charger_usb_notifier_call(struct notifier_block *nb, | |||
2427 | __func__, bm_usb_state, mA); | 3020 | __func__, bm_usb_state, mA); |
2428 | 3021 | ||
2429 | spin_lock(&di->usb_state.usb_lock); | 3022 | spin_lock(&di->usb_state.usb_lock); |
2430 | di->usb_state.usb_changed = true; | 3023 | di->usb_state.state_tmp = bm_usb_state; |
3024 | di->usb_state.usb_current_tmp = mA; | ||
2431 | spin_unlock(&di->usb_state.usb_lock); | 3025 | spin_unlock(&di->usb_state.usb_lock); |
2432 | 3026 | ||
2433 | di->usb_state.state = bm_usb_state; | 3027 | /* |
2434 | di->usb_state.usb_current = mA; | 3028 | * wait for some time until you get updates from the usb stack |
2435 | 3029 | * and negotiations are completed | |
2436 | queue_work(di->charger_wq, &di->usb_state_changed_work); | 3030 | */ |
3031 | queue_delayed_work(di->charger_wq, &di->usb_state_changed_work, HZ/2); | ||
2437 | 3032 | ||
2438 | return NOTIFY_OK; | 3033 | return NOTIFY_OK; |
2439 | } | 3034 | } |
@@ -2473,6 +3068,9 @@ static int ab8500_charger_resume(struct platform_device *pdev) | |||
2473 | &di->check_hw_failure_work, 0); | 3068 | &di->check_hw_failure_work, 0); |
2474 | } | 3069 | } |
2475 | 3070 | ||
3071 | if (di->flags.vbus_drop_end) | ||
3072 | queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work, 0); | ||
3073 | |||
2476 | return 0; | 3074 | return 0; |
2477 | } | 3075 | } |
2478 | 3076 | ||
@@ -2485,6 +3083,23 @@ static int ab8500_charger_suspend(struct platform_device *pdev, | |||
2485 | if (delayed_work_pending(&di->check_hw_failure_work)) | 3083 | if (delayed_work_pending(&di->check_hw_failure_work)) |
2486 | cancel_delayed_work(&di->check_hw_failure_work); | 3084 | cancel_delayed_work(&di->check_hw_failure_work); |
2487 | 3085 | ||
3086 | if (delayed_work_pending(&di->vbus_drop_end_work)) | ||
3087 | cancel_delayed_work(&di->vbus_drop_end_work); | ||
3088 | |||
3089 | flush_delayed_work(&di->attach_work); | ||
3090 | flush_delayed_work(&di->usb_charger_attached_work); | ||
3091 | flush_delayed_work(&di->ac_charger_attached_work); | ||
3092 | flush_delayed_work(&di->check_usbchgnotok_work); | ||
3093 | flush_delayed_work(&di->check_vbat_work); | ||
3094 | flush_delayed_work(&di->kick_wd_work); | ||
3095 | |||
3096 | flush_work(&di->usb_link_status_work); | ||
3097 | flush_work(&di->ac_work); | ||
3098 | flush_work(&di->detect_usb_type_work); | ||
3099 | |||
3100 | if (atomic_read(&di->current_stepping_sessions)) | ||
3101 | return -EAGAIN; | ||
3102 | |||
2488 | return 0; | 3103 | return 0; |
2489 | } | 3104 | } |
2490 | #else | 3105 | #else |
@@ -2509,9 +3124,6 @@ static int ab8500_charger_remove(struct platform_device *pdev) | |||
2509 | free_irq(irq, di); | 3124 | free_irq(irq, di); |
2510 | } | 3125 | } |
2511 | 3126 | ||
2512 | /* disable the regulator */ | ||
2513 | regulator_put(di->regu); | ||
2514 | |||
2515 | /* Backup battery voltage and current disable */ | 3127 | /* Backup battery voltage and current disable */ |
2516 | ret = abx500_mask_and_set_register_interruptible(di->dev, | 3128 | ret = abx500_mask_and_set_register_interruptible(di->dev, |
2517 | AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0); | 3129 | AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0); |
@@ -2525,8 +3137,12 @@ static int ab8500_charger_remove(struct platform_device *pdev) | |||
2525 | destroy_workqueue(di->charger_wq); | 3137 | destroy_workqueue(di->charger_wq); |
2526 | 3138 | ||
2527 | flush_scheduled_work(); | 3139 | flush_scheduled_work(); |
2528 | power_supply_unregister(&di->usb_chg.psy); | 3140 | if(di->usb_chg.enabled) |
2529 | power_supply_unregister(&di->ac_chg.psy); | 3141 | power_supply_unregister(&di->usb_chg.psy); |
3142 | #if !defined(CONFIG_CHARGER_PM2301) | ||
3143 | if(di->ac_chg.enabled) | ||
3144 | power_supply_unregister(&di->ac_chg.psy); | ||
3145 | #endif | ||
2530 | platform_set_drvdata(pdev, NULL); | 3146 | platform_set_drvdata(pdev, NULL); |
2531 | 3147 | ||
2532 | return 0; | 3148 | return 0; |
@@ -2541,32 +3157,31 @@ static char *supply_interface[] = { | |||
2541 | static int ab8500_charger_probe(struct platform_device *pdev) | 3157 | static int ab8500_charger_probe(struct platform_device *pdev) |
2542 | { | 3158 | { |
2543 | struct device_node *np = pdev->dev.of_node; | 3159 | struct device_node *np = pdev->dev.of_node; |
3160 | struct abx500_bm_data *plat = pdev->dev.platform_data; | ||
2544 | struct ab8500_charger *di; | 3161 | struct ab8500_charger *di; |
2545 | int irq, i, charger_status, ret = 0; | 3162 | int irq, i, charger_status, ret = 0, ch_stat; |
2546 | 3163 | ||
2547 | di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL); | 3164 | di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL); |
2548 | if (!di) { | 3165 | if (!di) { |
2549 | dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__); | 3166 | dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__); |
2550 | return -ENOMEM; | 3167 | return -ENOMEM; |
2551 | } | 3168 | } |
2552 | di->bat = pdev->mfd_cell->platform_data; | 3169 | |
2553 | if (!di->bat) { | 3170 | if (!plat) { |
2554 | if (np) { | 3171 | dev_err(&pdev->dev, "no battery management data supplied\n"); |
2555 | ret = bmdevs_of_probe(&pdev->dev, np, &di->bat); | 3172 | return -EINVAL; |
2556 | if (ret) { | 3173 | } |
2557 | dev_err(&pdev->dev, | 3174 | di->bm = plat; |
2558 | "failed to get battery information\n"); | 3175 | |
2559 | return ret; | 3176 | if (np) { |
2560 | } | 3177 | ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm); |
2561 | di->autopower_cfg = of_property_read_bool(np, "autopower_cfg"); | 3178 | if (ret) { |
2562 | } else { | 3179 | dev_err(&pdev->dev, "failed to get battery information\n"); |
2563 | dev_err(&pdev->dev, "missing dt node for ab8500_charger\n"); | 3180 | return ret; |
2564 | return -EINVAL; | ||
2565 | } | 3181 | } |
2566 | } else { | 3182 | di->autopower_cfg = of_property_read_bool(np, "autopower_cfg"); |
2567 | dev_info(&pdev->dev, "falling back to legacy platform data\n"); | 3183 | } else |
2568 | di->autopower_cfg = false; | 3184 | di->autopower_cfg = false; |
2569 | } | ||
2570 | 3185 | ||
2571 | /* get parent data */ | 3186 | /* get parent data */ |
2572 | di->dev = &pdev->dev; | 3187 | di->dev = &pdev->dev; |
@@ -2575,8 +3190,10 @@ static int ab8500_charger_probe(struct platform_device *pdev) | |||
2575 | 3190 | ||
2576 | /* initialize lock */ | 3191 | /* initialize lock */ |
2577 | spin_lock_init(&di->usb_state.usb_lock); | 3192 | spin_lock_init(&di->usb_state.usb_lock); |
3193 | mutex_init(&di->usb_ipt_crnt_lock); | ||
2578 | 3194 | ||
2579 | di->autopower = false; | 3195 | di->autopower = false; |
3196 | di->invalid_charger_detect_state = 0; | ||
2580 | 3197 | ||
2581 | /* AC supply */ | 3198 | /* AC supply */ |
2582 | /* power_supply base class */ | 3199 | /* power_supply base class */ |
@@ -2595,6 +3212,9 @@ static int ab8500_charger_probe(struct platform_device *pdev) | |||
2595 | ARRAY_SIZE(ab8500_charger_voltage_map) - 1]; | 3212 | ARRAY_SIZE(ab8500_charger_voltage_map) - 1]; |
2596 | di->ac_chg.max_out_curr = ab8500_charger_current_map[ | 3213 | di->ac_chg.max_out_curr = ab8500_charger_current_map[ |
2597 | ARRAY_SIZE(ab8500_charger_current_map) - 1]; | 3214 | ARRAY_SIZE(ab8500_charger_current_map) - 1]; |
3215 | di->ac_chg.wdt_refresh = CHG_WD_INTERVAL; | ||
3216 | di->ac_chg.enabled = di->bm->ac_enabled; | ||
3217 | di->ac_chg.external = false; | ||
2598 | 3218 | ||
2599 | /* USB supply */ | 3219 | /* USB supply */ |
2600 | /* power_supply base class */ | 3220 | /* power_supply base class */ |
@@ -2613,7 +3233,9 @@ static int ab8500_charger_probe(struct platform_device *pdev) | |||
2613 | ARRAY_SIZE(ab8500_charger_voltage_map) - 1]; | 3233 | ARRAY_SIZE(ab8500_charger_voltage_map) - 1]; |
2614 | di->usb_chg.max_out_curr = ab8500_charger_current_map[ | 3234 | di->usb_chg.max_out_curr = ab8500_charger_current_map[ |
2615 | ARRAY_SIZE(ab8500_charger_current_map) - 1]; | 3235 | ARRAY_SIZE(ab8500_charger_current_map) - 1]; |
2616 | 3236 | di->usb_chg.wdt_refresh = CHG_WD_INTERVAL; | |
3237 | di->usb_chg.enabled = di->bm->usb_enabled; | ||
3238 | di->usb_chg.external = false; | ||
2617 | 3239 | ||
2618 | /* Create a work queue for the charger */ | 3240 | /* Create a work queue for the charger */ |
2619 | di->charger_wq = | 3241 | di->charger_wq = |
@@ -2623,12 +3245,19 @@ static int ab8500_charger_probe(struct platform_device *pdev) | |||
2623 | return -ENOMEM; | 3245 | return -ENOMEM; |
2624 | } | 3246 | } |
2625 | 3247 | ||
3248 | mutex_init(&di->charger_attached_mutex); | ||
3249 | |||
2626 | /* Init work for HW failure check */ | 3250 | /* Init work for HW failure check */ |
2627 | INIT_DEFERRABLE_WORK(&di->check_hw_failure_work, | 3251 | INIT_DEFERRABLE_WORK(&di->check_hw_failure_work, |
2628 | ab8500_charger_check_hw_failure_work); | 3252 | ab8500_charger_check_hw_failure_work); |
2629 | INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work, | 3253 | INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work, |
2630 | ab8500_charger_check_usbchargernotok_work); | 3254 | ab8500_charger_check_usbchargernotok_work); |
2631 | 3255 | ||
3256 | INIT_DELAYED_WORK(&di->ac_charger_attached_work, | ||
3257 | ab8500_charger_ac_attached_work); | ||
3258 | INIT_DELAYED_WORK(&di->usb_charger_attached_work, | ||
3259 | ab8500_charger_usb_attached_work); | ||
3260 | |||
2632 | /* | 3261 | /* |
2633 | * For ABB revision 1.0 and 1.1 there is a bug in the watchdog | 3262 | * For ABB revision 1.0 and 1.1 there is a bug in the watchdog |
2634 | * logic. That means we have to continously kick the charger | 3263 | * logic. That means we have to continously kick the charger |
@@ -2644,6 +3273,15 @@ static int ab8500_charger_probe(struct platform_device *pdev) | |||
2644 | INIT_DEFERRABLE_WORK(&di->check_vbat_work, | 3273 | INIT_DEFERRABLE_WORK(&di->check_vbat_work, |
2645 | ab8500_charger_check_vbat_work); | 3274 | ab8500_charger_check_vbat_work); |
2646 | 3275 | ||
3276 | INIT_DELAYED_WORK(&di->attach_work, | ||
3277 | ab8500_charger_usb_link_attach_work); | ||
3278 | |||
3279 | INIT_DELAYED_WORK(&di->usb_state_changed_work, | ||
3280 | ab8500_charger_usb_state_changed_work); | ||
3281 | |||
3282 | INIT_DELAYED_WORK(&di->vbus_drop_end_work, | ||
3283 | ab8500_charger_vbus_drop_end_work); | ||
3284 | |||
2647 | /* Init work for charger detection */ | 3285 | /* Init work for charger detection */ |
2648 | INIT_WORK(&di->usb_link_status_work, | 3286 | INIT_WORK(&di->usb_link_status_work, |
2649 | ab8500_charger_usb_link_status_work); | 3287 | ab8500_charger_usb_link_status_work); |
@@ -2651,9 +3289,6 @@ static int ab8500_charger_probe(struct platform_device *pdev) | |||
2651 | INIT_WORK(&di->detect_usb_type_work, | 3289 | INIT_WORK(&di->detect_usb_type_work, |
2652 | ab8500_charger_detect_usb_type_work); | 3290 | ab8500_charger_detect_usb_type_work); |
2653 | 3291 | ||
2654 | INIT_WORK(&di->usb_state_changed_work, | ||
2655 | ab8500_charger_usb_state_changed_work); | ||
2656 | |||
2657 | /* Init work for checking HW status */ | 3292 | /* Init work for checking HW status */ |
2658 | INIT_WORK(&di->check_main_thermal_prot_work, | 3293 | INIT_WORK(&di->check_main_thermal_prot_work, |
2659 | ab8500_charger_check_main_thermal_prot_work); | 3294 | ab8500_charger_check_main_thermal_prot_work); |
@@ -2665,7 +3300,7 @@ static int ab8500_charger_probe(struct platform_device *pdev) | |||
2665 | * is a charger connected to avoid erroneous BTEMP_HIGH/LOW | 3300 | * is a charger connected to avoid erroneous BTEMP_HIGH/LOW |
2666 | * interrupts during charging | 3301 | * interrupts during charging |
2667 | */ | 3302 | */ |
2668 | di->regu = regulator_get(di->dev, "vddadc"); | 3303 | di->regu = devm_regulator_get(di->dev, "vddadc"); |
2669 | if (IS_ERR(di->regu)) { | 3304 | if (IS_ERR(di->regu)) { |
2670 | ret = PTR_ERR(di->regu); | 3305 | ret = PTR_ERR(di->regu); |
2671 | dev_err(di->dev, "failed to get vddadc regulator\n"); | 3306 | dev_err(di->dev, "failed to get vddadc regulator\n"); |
@@ -2677,21 +3312,25 @@ static int ab8500_charger_probe(struct platform_device *pdev) | |||
2677 | ret = ab8500_charger_init_hw_registers(di); | 3312 | ret = ab8500_charger_init_hw_registers(di); |
2678 | if (ret) { | 3313 | if (ret) { |
2679 | dev_err(di->dev, "failed to initialize ABB registers\n"); | 3314 | dev_err(di->dev, "failed to initialize ABB registers\n"); |
2680 | goto free_regulator; | 3315 | goto free_charger_wq; |
2681 | } | 3316 | } |
2682 | 3317 | ||
2683 | /* Register AC charger class */ | 3318 | /* Register AC charger class */ |
2684 | ret = power_supply_register(di->dev, &di->ac_chg.psy); | 3319 | if(di->ac_chg.enabled) { |
2685 | if (ret) { | 3320 | ret = power_supply_register(di->dev, &di->ac_chg.psy); |
2686 | dev_err(di->dev, "failed to register AC charger\n"); | 3321 | if (ret) { |
2687 | goto free_regulator; | 3322 | dev_err(di->dev, "failed to register AC charger\n"); |
3323 | goto free_charger_wq; | ||
3324 | } | ||
2688 | } | 3325 | } |
2689 | 3326 | ||
2690 | /* Register USB charger class */ | 3327 | /* Register USB charger class */ |
2691 | ret = power_supply_register(di->dev, &di->usb_chg.psy); | 3328 | if(di->usb_chg.enabled) { |
2692 | if (ret) { | 3329 | ret = power_supply_register(di->dev, &di->usb_chg.psy); |
2693 | dev_err(di->dev, "failed to register USB charger\n"); | 3330 | if (ret) { |
2694 | goto free_ac; | 3331 | dev_err(di->dev, "failed to register USB charger\n"); |
3332 | goto free_ac; | ||
3333 | } | ||
2695 | } | 3334 | } |
2696 | 3335 | ||
2697 | di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2); | 3336 | di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2); |
@@ -2708,7 +3347,7 @@ static int ab8500_charger_probe(struct platform_device *pdev) | |||
2708 | } | 3347 | } |
2709 | 3348 | ||
2710 | /* Identify the connected charger types during startup */ | 3349 | /* Identify the connected charger types during startup */ |
2711 | charger_status = ab8500_charger_detect_chargers(di); | 3350 | charger_status = ab8500_charger_detect_chargers(di, true); |
2712 | if (charger_status & AC_PW_CONN) { | 3351 | if (charger_status & AC_PW_CONN) { |
2713 | di->ac.charger_connected = 1; | 3352 | di->ac.charger_connected = 1; |
2714 | di->ac_conn = true; | 3353 | di->ac_conn = true; |
@@ -2717,7 +3356,6 @@ static int ab8500_charger_probe(struct platform_device *pdev) | |||
2717 | } | 3356 | } |
2718 | 3357 | ||
2719 | if (charger_status & USB_PW_CONN) { | 3358 | if (charger_status & USB_PW_CONN) { |
2720 | dev_dbg(di->dev, "VBUS Detect during startup\n"); | ||
2721 | di->vbus_detected = true; | 3359 | di->vbus_detected = true; |
2722 | di->vbus_detected_start = true; | 3360 | di->vbus_detected_start = true; |
2723 | queue_work(di->charger_wq, | 3361 | queue_work(di->charger_wq, |
@@ -2742,6 +3380,23 @@ static int ab8500_charger_probe(struct platform_device *pdev) | |||
2742 | 3380 | ||
2743 | platform_set_drvdata(pdev, di); | 3381 | platform_set_drvdata(pdev, di); |
2744 | 3382 | ||
3383 | mutex_lock(&di->charger_attached_mutex); | ||
3384 | |||
3385 | ch_stat = ab8500_charger_detect_chargers(di, false); | ||
3386 | |||
3387 | if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) { | ||
3388 | queue_delayed_work(di->charger_wq, | ||
3389 | &di->ac_charger_attached_work, | ||
3390 | HZ); | ||
3391 | } | ||
3392 | if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) { | ||
3393 | queue_delayed_work(di->charger_wq, | ||
3394 | &di->usb_charger_attached_work, | ||
3395 | HZ); | ||
3396 | } | ||
3397 | |||
3398 | mutex_unlock(&di->charger_attached_mutex); | ||
3399 | |||
2745 | return ret; | 3400 | return ret; |
2746 | 3401 | ||
2747 | free_irq: | 3402 | free_irq: |
@@ -2755,11 +3410,11 @@ free_irq: | |||
2755 | put_usb_phy: | 3410 | put_usb_phy: |
2756 | usb_put_phy(di->usb_phy); | 3411 | usb_put_phy(di->usb_phy); |
2757 | free_usb: | 3412 | free_usb: |
2758 | power_supply_unregister(&di->usb_chg.psy); | 3413 | if(di->usb_chg.enabled) |
3414 | power_supply_unregister(&di->usb_chg.psy); | ||
2759 | free_ac: | 3415 | free_ac: |
2760 | power_supply_unregister(&di->ac_chg.psy); | 3416 | if(di->ac_chg.enabled) |
2761 | free_regulator: | 3417 | power_supply_unregister(&di->ac_chg.psy); |
2762 | regulator_put(di->regu); | ||
2763 | free_charger_wq: | 3418 | free_charger_wq: |
2764 | destroy_workqueue(di->charger_wq); | 3419 | destroy_workqueue(di->charger_wq); |
2765 | return ret; | 3420 | return ret; |