ab8500-fg: Allow capacity to raise from 1% when charging
[linux-2.6-block.git] / drivers / power / ab8500_charger.c
CommitLineData
84edbeea
AM
1/*
2 * Copyright (C) ST-Ericsson SA 2012
3 *
4 * Charger driver for AB8500
5 *
6 * License Terms: GNU General Public License v2
7 * Author:
8 * Johan Palsson <johan.palsson@stericsson.com>
9 * Karl Komierowski <karl.komierowski@stericsson.com>
10 * Arun R Murthy <arun.murthy@stericsson.com>
11 */
12
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/interrupt.h>
17#include <linux/delay.h>
8891716e 18#include <linux/notifier.h>
84edbeea
AM
19#include <linux/slab.h>
20#include <linux/platform_device.h>
21#include <linux/power_supply.h>
22#include <linux/completion.h>
23#include <linux/regulator/consumer.h>
24#include <linux/err.h>
25#include <linux/workqueue.h>
26#include <linux/kobject.h>
4aef72db
R
27#include <linux/of.h>
28#include <linux/mfd/core.h>
84edbeea
AM
29#include <linux/mfd/abx500/ab8500.h>
30#include <linux/mfd/abx500.h>
31#include <linux/mfd/abx500/ab8500-bm.h>
32#include <linux/mfd/abx500/ab8500-gpadc.h>
33#include <linux/mfd/abx500/ux500_chargalg.h>
34#include <linux/usb/otg.h>
b269fff4 35#include <linux/mutex.h>
84edbeea
AM
36
37/* Charger constants */
38#define NO_PW_CONN 0
39#define AC_PW_CONN 1
40#define USB_PW_CONN 2
41
42#define MAIN_WDOG_ENA 0x01
43#define MAIN_WDOG_KICK 0x02
44#define MAIN_WDOG_DIS 0x00
45#define CHARG_WD_KICK 0x01
46#define MAIN_CH_ENA 0x01
47#define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02
48#define USB_CH_ENA 0x01
49#define USB_CHG_NO_OVERSHOOT_ENA_N 0x02
50#define MAIN_CH_DET 0x01
51#define MAIN_CH_CV_ON 0x04
52#define USB_CH_CV_ON 0x08
53#define VBUS_DET_DBNC100 0x02
54#define VBUS_DET_DBNC1 0x01
55#define OTP_ENABLE_WD 0x01
4dcdf577 56#define DROP_COUNT_RESET 0x01
84edbeea
AM
57
58#define MAIN_CH_INPUT_CURR_SHIFT 4
59#define VBUS_IN_CURR_LIM_SHIFT 4
34c11a70 60#define AUTO_VBUS_IN_CURR_LIM_SHIFT 4
f7470b5d 61#define VBUS_IN_CURR_LIM_RETRY_SET_TIME 30 /* seconds */
84edbeea
AM
62
63#define LED_INDICATOR_PWM_ENA 0x01
64#define LED_INDICATOR_PWM_DIS 0x00
65#define LED_IND_CUR_5MA 0x04
66#define LED_INDICATOR_PWM_DUTY_252_256 0xBF
67
68/* HW failure constants */
69#define MAIN_CH_TH_PROT 0x02
70#define VBUS_CH_NOK 0x08
71#define USB_CH_TH_PROT 0x02
72#define VBUS_OVV_TH 0x01
73#define MAIN_CH_NOK 0x01
74#define VBUS_DET 0x80
75
b269fff4
LJ
76#define MAIN_CH_STATUS2_MAINCHGDROP 0x80
77#define MAIN_CH_STATUS2_MAINCHARGERDETDBNC 0x40
78#define USB_CH_VBUSDROP 0x40
79#define USB_CH_VBUSDETDBNC 0x01
80
84edbeea
AM
81/* UsbLineStatus register bit masks */
82#define AB8500_USB_LINK_STATUS 0x78
83#define AB8500_STD_HOST_SUSP 0x18
84
85/* Watchdog timeout constant */
86#define WD_TIMER 0x30 /* 4min */
87#define WD_KICK_INTERVAL (60 * HZ)
88
89/* Lowest charger voltage is 3.39V -> 0x4E */
90#define LOW_VOLT_REG 0x4E
91
f8e96dff
JB
92/* Step up/down delay in us */
93#define STEP_UDELAY 1000
94
b269fff4
LJ
95#define CHARGER_STATUS_POLL 10 /* in ms */
96
e07a5645
LP
97#define CHG_WD_INTERVAL (60 * HZ)
98
b0163222 99#define AB8500_SW_CONTROL_FALLBACK 0x03
34c11a70
POH
100/* Wait for enumeration before charing in us */
101#define WAIT_ACA_RID_ENUMERATION (5 * 1000)
8891716e
LJ
102/*External charger control*/
103#define AB8500_SYS_CHARGER_CONTROL_REG 0x52
104#define EXTERNAL_CHARGER_DISABLE_REG_VAL 0x03
105#define EXTERNAL_CHARGER_ENABLE_REG_VAL 0x07
b0163222 106
84edbeea
AM
107/* UsbLineStatus register - usb types */
108enum ab8500_charger_link_status {
109 USB_STAT_NOT_CONFIGURED,
110 USB_STAT_STD_HOST_NC,
111 USB_STAT_STD_HOST_C_NS,
112 USB_STAT_STD_HOST_C_S,
113 USB_STAT_HOST_CHG_NM,
114 USB_STAT_HOST_CHG_HS,
115 USB_STAT_HOST_CHG_HS_CHIRP,
116 USB_STAT_DEDICATED_CHG,
117 USB_STAT_ACA_RID_A,
118 USB_STAT_ACA_RID_B,
119 USB_STAT_ACA_RID_C_NM,
120 USB_STAT_ACA_RID_C_HS,
121 USB_STAT_ACA_RID_C_HS_CHIRP,
122 USB_STAT_HM_IDGND,
123 USB_STAT_RESERVED,
124 USB_STAT_NOT_VALID_LINK,
74a8e349
HB
125 USB_STAT_PHY_EN,
126 USB_STAT_SUP_NO_IDGND_VBUS,
127 USB_STAT_SUP_IDGND_VBUS,
128 USB_STAT_CHARGER_LINE_1,
129 USB_STAT_CARKIT_1,
130 USB_STAT_CARKIT_2,
131 USB_STAT_ACA_DOCK_CHARGER,
84edbeea
AM
132};
133
134enum ab8500_usb_state {
135 AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
136 AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
137 AB8500_BM_USB_STATE_CONFIGURED,
138 AB8500_BM_USB_STATE_SUSPEND,
139 AB8500_BM_USB_STATE_RESUME,
140 AB8500_BM_USB_STATE_MAX,
141};
142
143/* VBUS input current limits supported in AB8500 in mA */
144#define USB_CH_IP_CUR_LVL_0P05 50
145#define USB_CH_IP_CUR_LVL_0P09 98
146#define USB_CH_IP_CUR_LVL_0P19 193
147#define USB_CH_IP_CUR_LVL_0P29 290
148#define USB_CH_IP_CUR_LVL_0P38 380
149#define USB_CH_IP_CUR_LVL_0P45 450
150#define USB_CH_IP_CUR_LVL_0P5 500
151#define USB_CH_IP_CUR_LVL_0P6 600
152#define USB_CH_IP_CUR_LVL_0P7 700
153#define USB_CH_IP_CUR_LVL_0P8 800
154#define USB_CH_IP_CUR_LVL_0P9 900
155#define USB_CH_IP_CUR_LVL_1P0 1000
156#define USB_CH_IP_CUR_LVL_1P1 1100
157#define USB_CH_IP_CUR_LVL_1P3 1300
158#define USB_CH_IP_CUR_LVL_1P4 1400
159#define USB_CH_IP_CUR_LVL_1P5 1500
160
161#define VBAT_TRESH_IP_CUR_RED 3800
162
163#define to_ab8500_charger_usb_device_info(x) container_of((x), \
164 struct ab8500_charger, usb_chg)
165#define to_ab8500_charger_ac_device_info(x) container_of((x), \
166 struct ab8500_charger, ac_chg)
167
168/**
169 * struct ab8500_charger_interrupts - ab8500 interupts
170 * @name: name of the interrupt
171 * @isr function pointer to the isr
172 */
173struct ab8500_charger_interrupts {
174 char *name;
175 irqreturn_t (*isr)(int irq, void *data);
176};
177
178struct ab8500_charger_info {
179 int charger_connected;
180 int charger_online;
181 int charger_voltage;
182 int cv_active;
183 bool wd_expired;
a864c5a8 184 int charger_current;
84edbeea
AM
185};
186
187struct ab8500_charger_event_flags {
188 bool mainextchnotok;
189 bool main_thermal_prot;
190 bool usb_thermal_prot;
191 bool vbus_ovv;
192 bool usbchargernotok;
193 bool chgwdexp;
194 bool vbus_collapse;
34c11a70 195 bool vbus_drop_end;
84edbeea
AM
196};
197
198struct ab8500_charger_usb_state {
84edbeea 199 int usb_current;
34c11a70 200 int usb_current_tmp;
84edbeea 201 enum ab8500_usb_state state;
34c11a70 202 enum ab8500_usb_state state_tmp;
84edbeea
AM
203 spinlock_t usb_lock;
204};
205
f7470b5d
LJ
206struct ab8500_charger_max_usb_in_curr {
207 int usb_type_max;
208 int set_max;
209 int calculated_max;
210};
211
84edbeea
AM
212/**
213 * struct ab8500_charger - ab8500 Charger device information
214 * @dev: Pointer to the structure device
84edbeea
AM
215 * @vbus_detected: VBUS detected
216 * @vbus_detected_start:
217 * VBUS detected during startup
218 * @ac_conn: This will be true when the AC charger has been plugged
219 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
220 * charger is enabled
221 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
222 * charger is enabled
223 * @vbat Battery voltage
224 * @old_vbat Previously measured battery voltage
4b45f4a9 225 * @usb_device_is_unrecognised USB device is unrecognised by the hardware
84edbeea 226 * @autopower Indicate if we should have automatic pwron after pwrloss
4aef72db 227 * @autopower_cfg platform specific power config support for "pwron after pwrloss"
ff38090a 228 * @invalid_charger_detect_state State when forcing AB to use invalid charger
34c11a70
POH
229 * @is_aca_rid: Incicate if accessory is ACA type
230 * @current_stepping_sessions:
231 * Counter for current stepping sessions
84edbeea
AM
232 * @parent: Pointer to the struct ab8500
233 * @gpadc: Pointer to the struct gpadc
b0284de0 234 * @bm: Platform specific battery management information
84edbeea
AM
235 * @flags: Structure for information about events triggered
236 * @usb_state: Structure for usb stack information
f7470b5d 237 * @max_usb_in_curr: Max USB charger input current
84edbeea
AM
238 * @ac_chg: AC charger power supply
239 * @usb_chg: USB charger power supply
240 * @ac: Structure that holds the AC charger properties
241 * @usb: Structure that holds the USB charger properties
242 * @regu: Pointer to the struct regulator
243 * @charger_wq: Work queue for the IRQs and checking HW state
34c11a70
POH
244 * @usb_ipt_crnt_lock: Lock to protect VBUS input current setting from mutuals
245 * @pm_lock: Lock to prevent system to suspend
84edbeea
AM
246 * @check_vbat_work Work for checking vbat threshold to adjust vbus current
247 * @check_hw_failure_work: Work for checking HW state
248 * @check_usbchgnotok_work: Work for checking USB charger not ok status
249 * @kick_wd_work: Work for kicking the charger watchdog in case
250 * of ABB rev 1.* due to the watchog logic bug
b269fff4
LJ
251 * @ac_charger_attached_work: Work for checking if AC charger is still
252 * connected
253 * @usb_charger_attached_work: Work for checking if USB charger is still
254 * connected
84edbeea
AM
255 * @ac_work: Work for checking AC charger connection
256 * @detect_usb_type_work: Work for detecting the USB type connected
257 * @usb_link_status_work: Work for checking the new USB link status
258 * @usb_state_changed_work: Work for checking USB state
34c11a70
POH
259 * @attach_work: Work for detecting USB type
260 * @vbus_drop_end_work: Work for detecting VBUS drop end
84edbeea
AM
261 * @check_main_thermal_prot_work:
262 * Work for checking Main thermal status
263 * @check_usb_thermal_prot_work:
264 * Work for checking USB thermal status
b269fff4 265 * @charger_attached_mutex: For controlling the wakelock
84edbeea
AM
266 */
267struct ab8500_charger {
268 struct device *dev;
84edbeea
AM
269 bool vbus_detected;
270 bool vbus_detected_start;
271 bool ac_conn;
272 bool vddadc_en_ac;
273 bool vddadc_en_usb;
274 int vbat;
275 int old_vbat;
4b45f4a9 276 bool usb_device_is_unrecognised;
84edbeea 277 bool autopower;
4aef72db 278 bool autopower_cfg;
ff38090a 279 int invalid_charger_detect_state;
34c11a70
POH
280 int is_aca_rid;
281 atomic_t current_stepping_sessions;
84edbeea
AM
282 struct ab8500 *parent;
283 struct ab8500_gpadc *gpadc;
b0284de0 284 struct abx500_bm_data *bm;
84edbeea
AM
285 struct ab8500_charger_event_flags flags;
286 struct ab8500_charger_usb_state usb_state;
f7470b5d 287 struct ab8500_charger_max_usb_in_curr max_usb_in_curr;
84edbeea
AM
288 struct ux500_charger ac_chg;
289 struct ux500_charger usb_chg;
290 struct ab8500_charger_info ac;
291 struct ab8500_charger_info usb;
292 struct regulator *regu;
293 struct workqueue_struct *charger_wq;
34c11a70 294 struct mutex usb_ipt_crnt_lock;
84edbeea
AM
295 struct delayed_work check_vbat_work;
296 struct delayed_work check_hw_failure_work;
297 struct delayed_work check_usbchgnotok_work;
298 struct delayed_work kick_wd_work;
34c11a70 299 struct delayed_work usb_state_changed_work;
4b45f4a9 300 struct delayed_work attach_work;
b269fff4
LJ
301 struct delayed_work ac_charger_attached_work;
302 struct delayed_work usb_charger_attached_work;
34c11a70 303 struct delayed_work vbus_drop_end_work;
84edbeea
AM
304 struct work_struct ac_work;
305 struct work_struct detect_usb_type_work;
306 struct work_struct usb_link_status_work;
84edbeea
AM
307 struct work_struct check_main_thermal_prot_work;
308 struct work_struct check_usb_thermal_prot_work;
efd71c89 309 struct usb_phy *usb_phy;
84edbeea 310 struct notifier_block nb;
b269fff4 311 struct mutex charger_attached_mutex;
84edbeea
AM
312};
313
314/* AC properties */
315static enum power_supply_property ab8500_charger_ac_props[] = {
316 POWER_SUPPLY_PROP_HEALTH,
317 POWER_SUPPLY_PROP_PRESENT,
318 POWER_SUPPLY_PROP_ONLINE,
319 POWER_SUPPLY_PROP_VOLTAGE_NOW,
320 POWER_SUPPLY_PROP_VOLTAGE_AVG,
321 POWER_SUPPLY_PROP_CURRENT_NOW,
322};
323
324/* USB properties */
325static enum power_supply_property ab8500_charger_usb_props[] = {
326 POWER_SUPPLY_PROP_HEALTH,
327 POWER_SUPPLY_PROP_CURRENT_AVG,
328 POWER_SUPPLY_PROP_PRESENT,
329 POWER_SUPPLY_PROP_ONLINE,
330 POWER_SUPPLY_PROP_VOLTAGE_NOW,
331 POWER_SUPPLY_PROP_VOLTAGE_AVG,
332 POWER_SUPPLY_PROP_CURRENT_NOW,
333};
334
defbfa9c
POH
335/*
336 * Function for enabling and disabling sw fallback mode
337 * should always be disabled when no charger is connected.
84edbeea 338 */
defbfa9c
POH
339static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
340 bool fallback)
84edbeea 341{
b0163222 342 u8 val;
84edbeea 343 u8 reg;
b0163222
NG
344 u8 bank;
345 u8 bit;
84edbeea
AM
346 int ret;
347
defbfa9c 348 dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
84edbeea 349
b0163222
NG
350 if (is_ab8500(di->parent)) {
351 bank = 0x15;
352 reg = 0x0;
353 bit = 3;
354 } else {
355 bank = AB8500_SYS_CTRL1_BLOCK;
356 reg = AB8500_SW_CONTROL_FALLBACK;
357 bit = 0;
358 }
359
defbfa9c 360 /* read the register containing fallback bit */
b0163222
NG
361 ret = abx500_get_register_interruptible(di->dev, bank, reg, &val);
362 if (ret < 0) {
363 dev_err(di->dev, "%d read failed\n", __LINE__);
84edbeea
AM
364 return;
365 }
366
b0163222
NG
367 if (is_ab8500(di->parent)) {
368 /* enable the OPT emulation registers */
369 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
370 if (ret) {
371 dev_err(di->dev, "%d write failed\n", __LINE__);
372 goto disable_otp;
373 }
84edbeea
AM
374 }
375
defbfa9c 376 if (fallback)
b0163222 377 val |= (1 << bit);
84edbeea 378 else
b0163222 379 val &= ~(1 << bit);
84edbeea 380
defbfa9c 381 /* write back the changed fallback bit value to register */
b0163222 382 ret = abx500_set_register_interruptible(di->dev, bank, reg, val);
84edbeea
AM
383 if (ret) {
384 dev_err(di->dev, "%d write failed\n", __LINE__);
84edbeea
AM
385 }
386
b0163222
NG
387disable_otp:
388 if (is_ab8500(di->parent)) {
389 /* disable the set OTP registers again */
390 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
391 if (ret) {
392 dev_err(di->dev, "%d write failed\n", __LINE__);
393 }
84edbeea
AM
394 }
395}
396
397/**
398 * ab8500_power_supply_changed - a wrapper with local extentions for
399 * power_supply_changed
400 * @di: pointer to the ab8500_charger structure
401 * @psy: pointer to power_supply_that have changed.
402 *
403 */
404static void ab8500_power_supply_changed(struct ab8500_charger *di,
405 struct power_supply *psy)
406{
4aef72db 407 if (di->autopower_cfg) {
84edbeea
AM
408 if (!di->usb.charger_connected &&
409 !di->ac.charger_connected &&
410 di->autopower) {
411 di->autopower = false;
defbfa9c 412 ab8500_enable_disable_sw_fallback(di, false);
84edbeea
AM
413 } else if (!di->autopower &&
414 (di->ac.charger_connected ||
415 di->usb.charger_connected)) {
416 di->autopower = true;
defbfa9c 417 ab8500_enable_disable_sw_fallback(di, true);
84edbeea
AM
418 }
419 }
420 power_supply_changed(psy);
421}
422
423static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
424 bool connected)
425{
426 if (connected != di->usb.charger_connected) {
427 dev_dbg(di->dev, "USB connected:%i\n", connected);
428 di->usb.charger_connected = connected;
f7470b5d
LJ
429
430 if (!connected)
431 di->flags.vbus_drop_end = false;
432
84edbeea 433 sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present");
b269fff4
LJ
434
435 if (connected) {
436 mutex_lock(&di->charger_attached_mutex);
437 mutex_unlock(&di->charger_attached_mutex);
438
439 queue_delayed_work(di->charger_wq,
440 &di->usb_charger_attached_work,
441 HZ);
442 } else {
443 cancel_delayed_work_sync(&di->usb_charger_attached_work);
444 mutex_lock(&di->charger_attached_mutex);
445 mutex_unlock(&di->charger_attached_mutex);
446 }
84edbeea
AM
447 }
448}
449
450/**
451 * ab8500_charger_get_ac_voltage() - get ac charger voltage
452 * @di: pointer to the ab8500_charger structure
453 *
454 * Returns ac charger voltage (on success)
455 */
456static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
457{
458 int vch;
459
460 /* Only measure voltage if the charger is connected */
461 if (di->ac.charger_connected) {
462 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
463 if (vch < 0)
464 dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
465 } else {
466 vch = 0;
467 }
468 return vch;
469}
470
471/**
472 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
473 * @di: pointer to the ab8500_charger structure
474 *
475 * Returns ac charger CV mode (on success) else error code
476 */
477static int ab8500_charger_ac_cv(struct ab8500_charger *di)
478{
479 u8 val;
480 int ret = 0;
481
482 /* Only check CV mode if the charger is online */
483 if (di->ac.charger_online) {
484 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
485 AB8500_CH_STATUS1_REG, &val);
486 if (ret < 0) {
487 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
488 return 0;
489 }
490
491 if (val & MAIN_CH_CV_ON)
492 ret = 1;
493 else
494 ret = 0;
495 }
496
497 return ret;
498}
499
500/**
501 * ab8500_charger_get_vbus_voltage() - get vbus voltage
502 * @di: pointer to the ab8500_charger structure
503 *
504 * This function returns the vbus voltage.
505 * Returns vbus voltage (on success)
506 */
507static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
508{
509 int vch;
510
511 /* Only measure voltage if the charger is connected */
512 if (di->usb.charger_connected) {
513 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
514 if (vch < 0)
515 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
516 } else {
517 vch = 0;
518 }
519 return vch;
520}
521
522/**
523 * ab8500_charger_get_usb_current() - get usb charger current
524 * @di: pointer to the ab8500_charger structure
525 *
526 * This function returns the usb charger current.
527 * Returns usb current (on success) and error code on failure
528 */
529static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
530{
531 int ich;
532
533 /* Only measure current if the charger is online */
534 if (di->usb.charger_online) {
535 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
536 if (ich < 0)
537 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
538 } else {
539 ich = 0;
540 }
541 return ich;
542}
543
544/**
545 * ab8500_charger_get_ac_current() - get ac charger current
546 * @di: pointer to the ab8500_charger structure
547 *
548 * This function returns the ac charger current.
549 * Returns ac current (on success) and error code on failure.
550 */
551static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
552{
553 int ich;
554
555 /* Only measure current if the charger is online */
556 if (di->ac.charger_online) {
557 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
558 if (ich < 0)
559 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
560 } else {
561 ich = 0;
562 }
563 return ich;
564}
565
566/**
567 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
568 * @di: pointer to the ab8500_charger structure
569 *
570 * Returns ac charger CV mode (on success) else error code
571 */
572static int ab8500_charger_usb_cv(struct ab8500_charger *di)
573{
574 int ret;
575 u8 val;
576
577 /* Only check CV mode if the charger is online */
578 if (di->usb.charger_online) {
579 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
580 AB8500_CH_USBCH_STAT1_REG, &val);
581 if (ret < 0) {
582 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
583 return 0;
584 }
585
586 if (val & USB_CH_CV_ON)
587 ret = 1;
588 else
589 ret = 0;
590 } else {
591 ret = 0;
592 }
593
594 return ret;
595}
596
597/**
598 * ab8500_charger_detect_chargers() - Detect the connected chargers
599 * @di: pointer to the ab8500_charger structure
34c11a70 600 * @probe: if probe, don't delay and wait for HW
84edbeea
AM
601 *
602 * Returns the type of charger connected.
603 * For USB it will not mean we can actually charge from it
604 * but that there is a USB cable connected that we have to
605 * identify. This is used during startup when we don't get
606 * interrupts of the charger detection
607 *
608 * Returns an integer value, that means,
609 * NO_PW_CONN no power supply is connected
610 * AC_PW_CONN if the AC power supply is connected
611 * USB_PW_CONN if the USB power supply is connected
612 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
613 */
34c11a70 614static int ab8500_charger_detect_chargers(struct ab8500_charger *di, bool probe)
84edbeea
AM
615{
616 int result = NO_PW_CONN;
617 int ret;
618 u8 val;
619
620 /* Check for AC charger */
621 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
622 AB8500_CH_STATUS1_REG, &val);
623 if (ret < 0) {
624 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
625 return ret;
626 }
627
628 if (val & MAIN_CH_DET)
629 result = AC_PW_CONN;
630
631 /* Check for USB charger */
34c11a70
POH
632
633 if (!probe) {
634 /*
635 * AB8500 says VBUS_DET_DBNC1 & VBUS_DET_DBNC100
636 * when disconnecting ACA even though no
637 * charger was connected. Try waiting a little
638 * longer than the 100 ms of VBUS_DET_DBNC100...
639 */
640 msleep(110);
641 }
84edbeea
AM
642 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
643 AB8500_CH_USBCH_STAT1_REG, &val);
644 if (ret < 0) {
645 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
646 return ret;
647 }
34c11a70
POH
648 dev_dbg(di->dev,
649 "%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__,
650 val);
84edbeea
AM
651 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
652 result |= USB_PW_CONN;
653
654 return result;
655}
656
657/**
658 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
659 * @di: pointer to the ab8500_charger structure
660 * @link_status: the identified USB type
661 *
662 * Get the maximum current that is allowed to be drawn from the host
663 * based on the USB type.
664 * Returns error code in case of failure else 0 on success
665 */
666static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
74a8e349 667 enum ab8500_charger_link_status link_status)
84edbeea
AM
668{
669 int ret = 0;
670
4b45f4a9
MC
671 di->usb_device_is_unrecognised = false;
672
34c11a70
POH
673 /*
674 * Platform only supports USB 2.0.
675 * This means that charging current from USB source
676 * is maximum 500 mA. Every occurence of USB_STAT_*_HOST_*
677 * should set USB_CH_IP_CUR_LVL_0P5.
678 */
679
84edbeea
AM
680 switch (link_status) {
681 case USB_STAT_STD_HOST_NC:
682 case USB_STAT_STD_HOST_C_NS:
683 case USB_STAT_STD_HOST_C_S:
684 dev_dbg(di->dev, "USB Type - Standard host is "
34c11a70 685 "detected through USB driver\n");
f7470b5d 686 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
34c11a70 687 di->is_aca_rid = 0;
84edbeea
AM
688 break;
689 case USB_STAT_HOST_CHG_HS_CHIRP:
f7470b5d 690 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
34c11a70 691 di->is_aca_rid = 0;
84edbeea
AM
692 break;
693 case USB_STAT_HOST_CHG_HS:
f7470b5d 694 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
34c11a70
POH
695 di->is_aca_rid = 0;
696 break;
84edbeea 697 case USB_STAT_ACA_RID_C_HS:
f7470b5d 698 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P9;
34c11a70 699 di->is_aca_rid = 0;
84edbeea
AM
700 break;
701 case USB_STAT_ACA_RID_A:
702 /*
703 * Dedicated charger level minus maximum current accessory
34c11a70 704 * can consume (900mA). Closest level is 500mA
84edbeea 705 */
34c11a70 706 dev_dbg(di->dev, "USB_STAT_ACA_RID_A detected\n");
f7470b5d 707 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
34c11a70 708 di->is_aca_rid = 1;
84edbeea
AM
709 break;
710 case USB_STAT_ACA_RID_B:
711 /*
712 * Dedicated charger level minus 120mA (20mA for ACA and
713 * 100mA for potential accessory). Closest level is 1300mA
714 */
f7470b5d 715 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P3;
74a8e349 716 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
f7470b5d 717 di->max_usb_in_curr.usb_type_max);
34c11a70 718 di->is_aca_rid = 1;
84edbeea 719 break;
84edbeea 720 case USB_STAT_HOST_CHG_NM:
f7470b5d 721 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
34c11a70
POH
722 di->is_aca_rid = 0;
723 break;
74a8e349 724 case USB_STAT_DEDICATED_CHG:
f7470b5d 725 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
34c11a70
POH
726 di->is_aca_rid = 0;
727 break;
74a8e349 728 case USB_STAT_ACA_RID_C_HS_CHIRP:
34c11a70 729 case USB_STAT_ACA_RID_C_NM:
f7470b5d 730 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
34c11a70 731 di->is_aca_rid = 1;
84edbeea 732 break;
4b45f4a9
MC
733 case USB_STAT_NOT_CONFIGURED:
734 if (di->vbus_detected) {
735 di->usb_device_is_unrecognised = true;
736 dev_dbg(di->dev, "USB Type - Legacy charger.\n");
f7470b5d
LJ
737 di->max_usb_in_curr.usb_type_max =
738 USB_CH_IP_CUR_LVL_1P5;
4b45f4a9
MC
739 break;
740 }
84edbeea 741 case USB_STAT_HM_IDGND:
84edbeea 742 dev_err(di->dev, "USB Type - Charging not allowed\n");
f7470b5d 743 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
84edbeea
AM
744 ret = -ENXIO;
745 break;
74a8e349
HB
746 case USB_STAT_RESERVED:
747 if (is_ab8500(di->parent)) {
748 di->flags.vbus_collapse = true;
749 dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
750 "VBUS has collapsed\n");
751 ret = -ENXIO;
752 break;
753 }
754 if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
755 dev_dbg(di->dev, "USB Type - Charging not allowed\n");
f7470b5d
LJ
756 di->max_usb_in_curr.usb_type_max =
757 USB_CH_IP_CUR_LVL_0P05;
74a8e349 758 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
f7470b5d
LJ
759 link_status,
760 di->max_usb_in_curr.usb_type_max);
74a8e349
HB
761 ret = -ENXIO;
762 break;
763 }
764 break;
765 case USB_STAT_CARKIT_1:
766 case USB_STAT_CARKIT_2:
767 case USB_STAT_ACA_DOCK_CHARGER:
768 case USB_STAT_CHARGER_LINE_1:
f7470b5d 769 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
74a8e349 770 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
f7470b5d 771 di->max_usb_in_curr.usb_type_max);
ff38090a
HS
772 case USB_STAT_NOT_VALID_LINK:
773 dev_err(di->dev, "USB Type invalid - try charging anyway\n");
f7470b5d 774 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
74a8e349
HB
775 break;
776
84edbeea
AM
777 default:
778 dev_err(di->dev, "USB Type - Unknown\n");
f7470b5d 779 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
84edbeea
AM
780 ret = -ENXIO;
781 break;
782 };
783
f7470b5d 784 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
84edbeea 785 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
f7470b5d 786 link_status, di->max_usb_in_curr.set_max);
84edbeea
AM
787
788 return ret;
789}
790
791/**
792 * ab8500_charger_read_usb_type() - read the type of usb connected
793 * @di: pointer to the ab8500_charger structure
794 *
795 * Detect the type of the plugged USB
796 * Returns error code in case of failure else 0 on success
797 */
798static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
799{
800 int ret;
801 u8 val;
802
803 ret = abx500_get_register_interruptible(di->dev,
804 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
805 if (ret < 0) {
806 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
807 return ret;
808 }
74a8e349
HB
809 if (is_ab8500(di->parent)) {
810 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
811 AB8500_USB_LINE_STAT_REG, &val);
812 } else {
813 if (is_ab9540(di->parent) || is_ab8505(di->parent))
814 ret = abx500_get_register_interruptible(di->dev,
815 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
816 }
84edbeea
AM
817 if (ret < 0) {
818 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
819 return ret;
820 }
821
822 /* get the USB type */
823 val = (val & AB8500_USB_LINK_STATUS) >> 3;
824 ret = ab8500_charger_max_usb_curr(di,
825 (enum ab8500_charger_link_status) val);
826
827 return ret;
828}
829
830/**
831 * ab8500_charger_detect_usb_type() - get the type of usb connected
832 * @di: pointer to the ab8500_charger structure
833 *
834 * Detect the type of the plugged USB
835 * Returns error code in case of failure else 0 on success
836 */
837static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
838{
839 int i, ret;
840 u8 val;
841
842 /*
843 * On getting the VBUS rising edge detect interrupt there
844 * is a 250ms delay after which the register UsbLineStatus
845 * is filled with valid data.
846 */
847 for (i = 0; i < 10; i++) {
848 msleep(250);
849 ret = abx500_get_register_interruptible(di->dev,
850 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
851 &val);
34c11a70
POH
852 dev_dbg(di->dev, "%s AB8500_IT_SOURCE21_REG %x\n",
853 __func__, val);
84edbeea
AM
854 if (ret < 0) {
855 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
856 return ret;
857 }
74a8e349
HB
858
859 if (is_ab8500(di->parent))
860 ret = abx500_get_register_interruptible(di->dev,
861 AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
862 else
863 ret = abx500_get_register_interruptible(di->dev,
864 AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
84edbeea
AM
865 if (ret < 0) {
866 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
867 return ret;
868 }
34c11a70
POH
869 dev_dbg(di->dev, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__,
870 val);
84edbeea
AM
871 /*
872 * Until the IT source register is read the UsbLineStatus
873 * register is not updated, hence doing the same
874 * Revisit this:
875 */
876
877 /* get the USB type */
878 val = (val & AB8500_USB_LINK_STATUS) >> 3;
879 if (val)
880 break;
881 }
882 ret = ab8500_charger_max_usb_curr(di,
883 (enum ab8500_charger_link_status) val);
884
885 return ret;
886}
887
888/*
889 * This array maps the raw hex value to charger voltage used by the AB8500
890 * Values taken from the UM0836
891 */
892static int ab8500_charger_voltage_map[] = {
893 3500 ,
894 3525 ,
895 3550 ,
896 3575 ,
897 3600 ,
898 3625 ,
899 3650 ,
900 3675 ,
901 3700 ,
902 3725 ,
903 3750 ,
904 3775 ,
905 3800 ,
906 3825 ,
907 3850 ,
908 3875 ,
909 3900 ,
910 3925 ,
911 3950 ,
912 3975 ,
913 4000 ,
914 4025 ,
915 4050 ,
916 4060 ,
917 4070 ,
918 4080 ,
919 4090 ,
920 4100 ,
921 4110 ,
922 4120 ,
923 4130 ,
924 4140 ,
925 4150 ,
926 4160 ,
927 4170 ,
928 4180 ,
929 4190 ,
930 4200 ,
931 4210 ,
932 4220 ,
933 4230 ,
934 4240 ,
935 4250 ,
936 4260 ,
937 4270 ,
938 4280 ,
939 4290 ,
940 4300 ,
941 4310 ,
942 4320 ,
943 4330 ,
944 4340 ,
945 4350 ,
946 4360 ,
947 4370 ,
948 4380 ,
949 4390 ,
950 4400 ,
951 4410 ,
952 4420 ,
953 4430 ,
954 4440 ,
955 4450 ,
956 4460 ,
957 4470 ,
958 4480 ,
959 4490 ,
960 4500 ,
961 4510 ,
962 4520 ,
963 4530 ,
964 4540 ,
965 4550 ,
966 4560 ,
967 4570 ,
968 4580 ,
969 4590 ,
970 4600 ,
971};
972
973/*
974 * This array maps the raw hex value to charger current used by the AB8500
975 * Values taken from the UM0836
976 */
977static int ab8500_charger_current_map[] = {
978 100 ,
979 200 ,
980 300 ,
981 400 ,
982 500 ,
983 600 ,
984 700 ,
985 800 ,
986 900 ,
987 1000 ,
988 1100 ,
989 1200 ,
990 1300 ,
991 1400 ,
992 1500 ,
993};
994
995/*
996 * This array maps the raw hex value to VBUS input current used by the AB8500
997 * Values taken from the UM0836
998 */
999static int ab8500_charger_vbus_in_curr_map[] = {
1000 USB_CH_IP_CUR_LVL_0P05,
1001 USB_CH_IP_CUR_LVL_0P09,
1002 USB_CH_IP_CUR_LVL_0P19,
1003 USB_CH_IP_CUR_LVL_0P29,
1004 USB_CH_IP_CUR_LVL_0P38,
1005 USB_CH_IP_CUR_LVL_0P45,
1006 USB_CH_IP_CUR_LVL_0P5,
1007 USB_CH_IP_CUR_LVL_0P6,
1008 USB_CH_IP_CUR_LVL_0P7,
1009 USB_CH_IP_CUR_LVL_0P8,
1010 USB_CH_IP_CUR_LVL_0P9,
1011 USB_CH_IP_CUR_LVL_1P0,
1012 USB_CH_IP_CUR_LVL_1P1,
1013 USB_CH_IP_CUR_LVL_1P3,
1014 USB_CH_IP_CUR_LVL_1P4,
1015 USB_CH_IP_CUR_LVL_1P5,
1016};
1017
1018static int ab8500_voltage_to_regval(int voltage)
1019{
1020 int i;
1021
1022 /* Special case for voltage below 3.5V */
1023 if (voltage < ab8500_charger_voltage_map[0])
1024 return LOW_VOLT_REG;
1025
1026 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
1027 if (voltage < ab8500_charger_voltage_map[i])
1028 return i - 1;
1029 }
1030
1031 /* If not last element, return error */
1032 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
1033 if (voltage == ab8500_charger_voltage_map[i])
1034 return i;
1035 else
1036 return -1;
1037}
1038
1039static int ab8500_current_to_regval(int curr)
1040{
1041 int i;
1042
1043 if (curr < ab8500_charger_current_map[0])
1044 return 0;
1045
1046 for (i = 0; i < ARRAY_SIZE(ab8500_charger_current_map); i++) {
1047 if (curr < ab8500_charger_current_map[i])
1048 return i - 1;
1049 }
1050
1051 /* If not last element, return error */
1052 i = ARRAY_SIZE(ab8500_charger_current_map) - 1;
1053 if (curr == ab8500_charger_current_map[i])
1054 return i;
1055 else
1056 return -1;
1057}
1058
1059static int ab8500_vbus_in_curr_to_regval(int curr)
1060{
1061 int i;
1062
1063 if (curr < ab8500_charger_vbus_in_curr_map[0])
1064 return 0;
1065
1066 for (i = 0; i < ARRAY_SIZE(ab8500_charger_vbus_in_curr_map); i++) {
1067 if (curr < ab8500_charger_vbus_in_curr_map[i])
1068 return i - 1;
1069 }
1070
1071 /* If not last element, return error */
1072 i = ARRAY_SIZE(ab8500_charger_vbus_in_curr_map) - 1;
1073 if (curr == ab8500_charger_vbus_in_curr_map[i])
1074 return i;
1075 else
1076 return -1;
1077}
1078
1079/**
1080 * ab8500_charger_get_usb_cur() - get usb current
1081 * @di: pointer to the ab8500_charger structre
1082 *
1083 * The usb stack provides the maximum current that can be drawn from
1084 * the standard usb host. This will be in mA.
1085 * This function converts current in mA to a value that can be written
1086 * to the register. Returns -1 if charging is not allowed
1087 */
1088static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
1089{
f7470b5d 1090 int ret = 0;
84edbeea
AM
1091 switch (di->usb_state.usb_current) {
1092 case 100:
f7470b5d 1093 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P09;
84edbeea
AM
1094 break;
1095 case 200:
f7470b5d 1096 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P19;
84edbeea
AM
1097 break;
1098 case 300:
f7470b5d 1099 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P29;
84edbeea
AM
1100 break;
1101 case 400:
f7470b5d 1102 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P38;
84edbeea
AM
1103 break;
1104 case 500:
f7470b5d 1105 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
84edbeea
AM
1106 break;
1107 default:
f7470b5d
LJ
1108 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
1109 ret = -EPERM;
84edbeea
AM
1110 break;
1111 };
f7470b5d
LJ
1112 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
1113 return ret;
1114}
1115
1116/**
1117 * ab8500_charger_check_continue_stepping() - Check to allow stepping
1118 * @di: pointer to the ab8500_charger structure
1119 * @reg: select what charger register to check
1120 *
1121 * Check if current stepping should be allowed to continue.
1122 * Checks if charger source has not collapsed. If it has, further stepping
1123 * is not allowed.
1124 */
1125static bool ab8500_charger_check_continue_stepping(struct ab8500_charger *di,
1126 int reg)
1127{
1128 if (reg == AB8500_USBCH_IPT_CRNTLVL_REG)
1129 return !di->flags.vbus_drop_end;
1130 else
1131 return true;
84edbeea
AM
1132}
1133
f8e96dff
JB
1134/**
1135 * ab8500_charger_set_current() - set charger current
1136 * @di: pointer to the ab8500_charger structure
1137 * @ich: charger current, in mA
1138 * @reg: select what charger register to set
1139 *
1140 * Set charger current.
1141 * There is no state machine in the AB to step up/down the charger
1142 * current to avoid dips and spikes on MAIN, VBUS and VBAT when
1143 * charging is started. Instead we need to implement
1144 * this charger current step-up/down here.
1145 * Returns error code in case of failure else 0(on success)
1146 */
1147static int ab8500_charger_set_current(struct ab8500_charger *di,
1148 int ich, int reg)
1149{
34c11a70
POH
1150 int ret = 0;
1151 int auto_curr_index, curr_index, prev_curr_index, shift_value, i;
f8e96dff 1152 u8 reg_value;
34c11a70
POH
1153 u32 step_udelay;
1154 bool no_stepping = false;
1155
1156 atomic_inc(&di->current_stepping_sessions);
1157
1158 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1159 reg, &reg_value);
1160 if (ret < 0) {
1161 dev_err(di->dev, "%s read failed\n", __func__);
1162 goto exit_set_current;
1163 }
f8e96dff
JB
1164
1165 switch (reg) {
1166 case AB8500_MCH_IPT_CURLVL_REG:
1167 shift_value = MAIN_CH_INPUT_CURR_SHIFT;
34c11a70 1168 prev_curr_index = (reg_value >> shift_value);
f8e96dff 1169 curr_index = ab8500_current_to_regval(ich);
34c11a70
POH
1170 step_udelay = STEP_UDELAY;
1171 if (!di->ac.charger_connected)
1172 no_stepping = true;
f8e96dff
JB
1173 break;
1174 case AB8500_USBCH_IPT_CRNTLVL_REG:
1175 shift_value = VBUS_IN_CURR_LIM_SHIFT;
34c11a70 1176 prev_curr_index = (reg_value >> shift_value);
f8e96dff 1177 curr_index = ab8500_vbus_in_curr_to_regval(ich);
34c11a70
POH
1178 step_udelay = STEP_UDELAY * 100;
1179
1180 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1181 AB8500_CH_USBCH_STAT2_REG, &reg_value);
1182 if (ret < 0) {
1183 dev_err(di->dev, "%s read failed\n", __func__);
1184 goto exit_set_current;
1185 }
1186 auto_curr_index =
1187 reg_value >> AUTO_VBUS_IN_CURR_LIM_SHIFT;
1188
1189 dev_dbg(di->dev, "%s Auto VBUS curr is %d mA\n",
1190 __func__,
1191 ab8500_charger_vbus_in_curr_map[auto_curr_index]);
1192
1193 prev_curr_index = min(prev_curr_index, auto_curr_index);
1194
1195 if (!di->usb.charger_connected)
1196 no_stepping = true;
f8e96dff
JB
1197 break;
1198 case AB8500_CH_OPT_CRNTLVL_REG:
1199 shift_value = 0;
34c11a70 1200 prev_curr_index = (reg_value >> shift_value);
f8e96dff 1201 curr_index = ab8500_current_to_regval(ich);
34c11a70
POH
1202 step_udelay = STEP_UDELAY;
1203 if (curr_index && (curr_index - prev_curr_index) > 1)
1204 step_udelay *= 100;
1205
1206 if (!di->usb.charger_connected && !di->ac.charger_connected)
1207 no_stepping = true;
1208
f8e96dff
JB
1209 break;
1210 default:
1211 dev_err(di->dev, "%s current register not valid\n", __func__);
34c11a70
POH
1212 ret = -ENXIO;
1213 goto exit_set_current;
f8e96dff
JB
1214 }
1215
1216 if (curr_index < 0) {
1217 dev_err(di->dev, "requested current limit out-of-range\n");
34c11a70
POH
1218 ret = -ENXIO;
1219 goto exit_set_current;
f8e96dff 1220 }
f8e96dff
JB
1221
1222 /* only update current if it's been changed */
34c11a70
POH
1223 if (prev_curr_index == curr_index) {
1224 dev_dbg(di->dev, "%s current not changed for reg: 0x%02x\n",
1225 __func__, reg);
1226 ret = 0;
1227 goto exit_set_current;
1228 }
f8e96dff
JB
1229
1230 dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
1231 __func__, ich, reg);
1232
34c11a70
POH
1233 if (no_stepping) {
1234 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1235 reg, (u8)curr_index << shift_value);
1236 if (ret)
1237 dev_err(di->dev, "%s write failed\n", __func__);
1238 } else if (prev_curr_index > curr_index) {
f8e96dff 1239 for (i = prev_curr_index - 1; i >= curr_index; i--) {
34c11a70
POH
1240 dev_dbg(di->dev, "curr change_1 to: %x for 0x%02x\n",
1241 (u8) i << shift_value, reg);
f8e96dff 1242 ret = abx500_set_register_interruptible(di->dev,
34c11a70 1243 AB8500_CHARGER, reg, (u8)i << shift_value);
f8e96dff
JB
1244 if (ret) {
1245 dev_err(di->dev, "%s write failed\n", __func__);
34c11a70 1246 goto exit_set_current;
f8e96dff 1247 }
34c11a70
POH
1248 if (i != curr_index)
1249 usleep_range(step_udelay, step_udelay * 2);
f8e96dff
JB
1250 }
1251 } else {
f7470b5d
LJ
1252 bool allow = true;
1253 for (i = prev_curr_index + 1; i <= curr_index && allow; i++) {
34c11a70
POH
1254 dev_dbg(di->dev, "curr change_2 to: %x for 0x%02x\n",
1255 (u8)i << shift_value, reg);
f8e96dff 1256 ret = abx500_set_register_interruptible(di->dev,
34c11a70 1257 AB8500_CHARGER, reg, (u8)i << shift_value);
f8e96dff
JB
1258 if (ret) {
1259 dev_err(di->dev, "%s write failed\n", __func__);
34c11a70 1260 goto exit_set_current;
f8e96dff 1261 }
34c11a70
POH
1262 if (i != curr_index)
1263 usleep_range(step_udelay, step_udelay * 2);
f7470b5d
LJ
1264
1265 allow = ab8500_charger_check_continue_stepping(di, reg);
f8e96dff
JB
1266 }
1267 }
34c11a70
POH
1268
1269exit_set_current:
1270 atomic_dec(&di->current_stepping_sessions);
1271
f8e96dff
JB
1272 return ret;
1273}
1274
84edbeea
AM
1275/**
1276 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
1277 * @di: pointer to the ab8500_charger structure
1278 * @ich_in: charger input current limit
1279 *
1280 * Sets the current that can be drawn from the USB host
1281 * Returns error code in case of failure else 0(on success)
1282 */
1283static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
1284 int ich_in)
1285{
84edbeea 1286 int min_value;
34c11a70 1287 int ret;
84edbeea
AM
1288
1289 /* We should always use to lowest current limit */
b0284de0 1290 min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
f7470b5d
LJ
1291 if (di->max_usb_in_curr.set_max > 0)
1292 min_value = min(di->max_usb_in_curr.set_max, min_value);
1293
1294 if (di->usb_state.usb_current >= 0)
1295 min_value = min(di->usb_state.usb_current, min_value);
84edbeea
AM
1296
1297 switch (min_value) {
1298 case 100:
1299 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1300 min_value = USB_CH_IP_CUR_LVL_0P05;
1301 break;
1302 case 500:
1303 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
1304 min_value = USB_CH_IP_CUR_LVL_0P45;
1305 break;
1306 default:
1307 break;
1308 }
1309
34c11a70
POH
1310 dev_info(di->dev, "VBUS input current limit set to %d mA\n", min_value);
1311
1312 mutex_lock(&di->usb_ipt_crnt_lock);
1313 ret = ab8500_charger_set_current(di, min_value,
f8e96dff 1314 AB8500_USBCH_IPT_CRNTLVL_REG);
34c11a70
POH
1315 mutex_unlock(&di->usb_ipt_crnt_lock);
1316
1317 return ret;
f8e96dff 1318}
84edbeea 1319
f8e96dff
JB
1320/**
1321 * ab8500_charger_set_main_in_curr() - set main charger input current
1322 * @di: pointer to the ab8500_charger structure
1323 * @ich_in: input charger current, in mA
1324 *
1325 * Set main charger input current.
1326 * Returns error code in case of failure else 0(on success)
1327 */
1328static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
1329 int ich_in)
1330{
1331 return ab8500_charger_set_current(di, ich_in,
1332 AB8500_MCH_IPT_CURLVL_REG);
1333}
84edbeea 1334
f8e96dff
JB
1335/**
1336 * ab8500_charger_set_output_curr() - set charger output current
1337 * @di: pointer to the ab8500_charger structure
1338 * @ich_out: output charger current, in mA
1339 *
1340 * Set charger output current.
1341 * Returns error code in case of failure else 0(on success)
1342 */
1343static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
1344 int ich_out)
1345{
1346 return ab8500_charger_set_current(di, ich_out,
1347 AB8500_CH_OPT_CRNTLVL_REG);
84edbeea
AM
1348}
1349
1350/**
1351 * ab8500_charger_led_en() - turn on/off chargign led
1352 * @di: pointer to the ab8500_charger structure
1353 * @on: flag to turn on/off the chargign led
1354 *
1355 * Power ON/OFF charging LED indication
1356 * Returns error code in case of failure else 0(on success)
1357 */
1358static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
1359{
1360 int ret;
1361
1362 if (on) {
1363 /* Power ON charging LED indicator, set LED current to 5mA */
1364 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1365 AB8500_LED_INDICATOR_PWM_CTRL,
1366 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
1367 if (ret) {
1368 dev_err(di->dev, "Power ON LED failed\n");
1369 return ret;
1370 }
1371 /* LED indicator PWM duty cycle 252/256 */
1372 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1373 AB8500_LED_INDICATOR_PWM_DUTY,
1374 LED_INDICATOR_PWM_DUTY_252_256);
1375 if (ret) {
1376 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1377 return ret;
1378 }
1379 } else {
1380 /* Power off charging LED indicator */
1381 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1382 AB8500_LED_INDICATOR_PWM_CTRL,
1383 LED_INDICATOR_PWM_DIS);
1384 if (ret) {
1385 dev_err(di->dev, "Power-off LED failed\n");
1386 return ret;
1387 }
1388 }
1389
1390 return ret;
1391}
1392
1393/**
1394 * ab8500_charger_ac_en() - enable or disable ac charging
1395 * @di: pointer to the ab8500_charger structure
1396 * @enable: enable/disable flag
1397 * @vset: charging voltage
1398 * @iset: charging current
1399 *
1400 * Enable/Disable AC/Mains charging and turns on/off the charging led
1401 * respectively.
1402 **/
1403static int ab8500_charger_ac_en(struct ux500_charger *charger,
1404 int enable, int vset, int iset)
1405{
1406 int ret;
1407 int volt_index;
1408 int curr_index;
1409 int input_curr_index;
1410 u8 overshoot = 0;
1411
1412 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1413
1414 if (enable) {
1415 /* Check if AC is connected */
1416 if (!di->ac.charger_connected) {
1417 dev_err(di->dev, "AC charger not connected\n");
1418 return -ENXIO;
1419 }
1420
1421 /* Enable AC charging */
1422 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1423
1424 /*
1425 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1426 * will be triggered everytime we enable the VDD ADC supply.
1427 * This will turn off charging for a short while.
1428 * It can be avoided by having the supply on when
1429 * there is a charger enabled. Normally the VDD ADC supply
1430 * is enabled everytime a GPADC conversion is triggered. We will
1431 * force it to be enabled from this driver to have
1432 * the GPADC module independant of the AB8500 chargers
1433 */
1434 if (!di->vddadc_en_ac) {
1435 regulator_enable(di->regu);
1436 di->vddadc_en_ac = true;
1437 }
1438
1439 /* Check if the requested voltage or current is valid */
1440 volt_index = ab8500_voltage_to_regval(vset);
1441 curr_index = ab8500_current_to_regval(iset);
1442 input_curr_index = ab8500_current_to_regval(
b0284de0 1443 di->bm->chg_params->ac_curr_max);
84edbeea
AM
1444 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1445 dev_err(di->dev,
1446 "Charger voltage or current too high, "
1447 "charging not started\n");
1448 return -ENXIO;
1449 }
1450
1451 /* ChVoltLevel: maximum battery charging voltage */
1452 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1453 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1454 if (ret) {
1455 dev_err(di->dev, "%s write failed\n", __func__);
1456 return ret;
1457 }
1458 /* MainChInputCurr: current that can be drawn from the charger*/
f8e96dff 1459 ret = ab8500_charger_set_main_in_curr(di,
b0284de0 1460 di->bm->chg_params->ac_curr_max);
84edbeea 1461 if (ret) {
f8e96dff
JB
1462 dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
1463 __func__);
84edbeea
AM
1464 return ret;
1465 }
1466 /* ChOutputCurentLevel: protected output current */
f8e96dff 1467 ret = ab8500_charger_set_output_curr(di, iset);
84edbeea 1468 if (ret) {
f8e96dff
JB
1469 dev_err(di->dev, "%s "
1470 "Failed to set ChOutputCurentLevel\n",
1471 __func__);
84edbeea
AM
1472 return ret;
1473 }
1474
1475 /* Check if VBAT overshoot control should be enabled */
b0284de0 1476 if (!di->bm->enable_overshoot)
84edbeea
AM
1477 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1478
1479 /* Enable Main Charger */
1480 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1481 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1482 if (ret) {
1483 dev_err(di->dev, "%s write failed\n", __func__);
1484 return ret;
1485 }
1486
1487 /* Power on charging LED indication */
1488 ret = ab8500_charger_led_en(di, true);
1489 if (ret < 0)
1490 dev_err(di->dev, "failed to enable LED\n");
1491
1492 di->ac.charger_online = 1;
1493 } else {
1494 /* Disable AC charging */
1495 if (is_ab8500_1p1_or_earlier(di->parent)) {
1496 /*
1497 * For ABB revision 1.0 and 1.1 there is a bug in the
1498 * watchdog logic. That means we have to continously
1499 * kick the charger watchdog even when no charger is
1500 * connected. This is only valid once the AC charger
1501 * has been enabled. This is a bug that is not handled
1502 * by the algorithm and the watchdog have to be kicked
1503 * by the charger driver when the AC charger
1504 * is disabled
1505 */
1506 if (di->ac_conn) {
1507 queue_delayed_work(di->charger_wq,
1508 &di->kick_wd_work,
1509 round_jiffies(WD_KICK_INTERVAL));
1510 }
1511
1512 /*
1513 * We can't turn off charging completely
1514 * due to a bug in AB8500 cut1.
1515 * If we do, charging will not start again.
1516 * That is why we set the lowest voltage
1517 * and current possible
1518 */
1519 ret = abx500_set_register_interruptible(di->dev,
1520 AB8500_CHARGER,
1521 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1522 if (ret) {
1523 dev_err(di->dev,
1524 "%s write failed\n", __func__);
1525 return ret;
1526 }
1527
f8e96dff 1528 ret = ab8500_charger_set_output_curr(di, 0);
84edbeea 1529 if (ret) {
f8e96dff
JB
1530 dev_err(di->dev, "%s "
1531 "Failed to set ChOutputCurentLevel\n",
1532 __func__);
84edbeea
AM
1533 return ret;
1534 }
1535 } else {
1536 ret = abx500_set_register_interruptible(di->dev,
1537 AB8500_CHARGER,
1538 AB8500_MCH_CTRL1, 0);
1539 if (ret) {
1540 dev_err(di->dev,
1541 "%s write failed\n", __func__);
1542 return ret;
1543 }
1544 }
1545
1546 ret = ab8500_charger_led_en(di, false);
1547 if (ret < 0)
1548 dev_err(di->dev, "failed to disable LED\n");
1549
1550 di->ac.charger_online = 0;
1551 di->ac.wd_expired = false;
1552
1553 /* Disable regulator if enabled */
1554 if (di->vddadc_en_ac) {
1555 regulator_disable(di->regu);
1556 di->vddadc_en_ac = false;
1557 }
1558
1559 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1560 }
1561 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1562
1563 return ret;
1564}
1565
1566/**
1567 * ab8500_charger_usb_en() - enable usb charging
1568 * @di: pointer to the ab8500_charger structure
1569 * @enable: enable/disable flag
1570 * @vset: charging voltage
1571 * @ich_out: charger output current
1572 *
1573 * Enable/Disable USB charging and turns on/off the charging led respectively.
1574 * Returns error code in case of failure else 0(on success)
1575 */
1576static int ab8500_charger_usb_en(struct ux500_charger *charger,
1577 int enable, int vset, int ich_out)
1578{
1579 int ret;
1580 int volt_index;
1581 int curr_index;
1582 u8 overshoot = 0;
1583
1584 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1585
1586 if (enable) {
1587 /* Check if USB is connected */
1588 if (!di->usb.charger_connected) {
1589 dev_err(di->dev, "USB charger not connected\n");
1590 return -ENXIO;
1591 }
1592
1593 /*
1594 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1595 * will be triggered everytime we enable the VDD ADC supply.
1596 * This will turn off charging for a short while.
1597 * It can be avoided by having the supply on when
1598 * there is a charger enabled. Normally the VDD ADC supply
1599 * is enabled everytime a GPADC conversion is triggered. We will
1600 * force it to be enabled from this driver to have
1601 * the GPADC module independant of the AB8500 chargers
1602 */
1603 if (!di->vddadc_en_usb) {
1604 regulator_enable(di->regu);
1605 di->vddadc_en_usb = true;
1606 }
1607
1608 /* Enable USB charging */
1609 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1610
1611 /* Check if the requested voltage or current is valid */
1612 volt_index = ab8500_voltage_to_regval(vset);
1613 curr_index = ab8500_current_to_regval(ich_out);
1614 if (volt_index < 0 || curr_index < 0) {
1615 dev_err(di->dev,
1616 "Charger voltage or current too high, "
1617 "charging not started\n");
1618 return -ENXIO;
1619 }
1620
1621 /* ChVoltLevel: max voltage upto which battery can be charged */
1622 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1623 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1624 if (ret) {
1625 dev_err(di->dev, "%s write failed\n", __func__);
1626 return ret;
1627 }
84edbeea 1628 /* Check if VBAT overshoot control should be enabled */
b0284de0 1629 if (!di->bm->enable_overshoot)
84edbeea
AM
1630 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1631
1632 /* Enable USB Charger */
34c11a70
POH
1633 dev_dbg(di->dev,
1634 "Enabling USB with write to AB8500_USBCH_CTRL1_REG\n");
84edbeea
AM
1635 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1636 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1637 if (ret) {
1638 dev_err(di->dev, "%s write failed\n", __func__);
1639 return ret;
1640 }
1641
1642 /* If success power on charging LED indication */
1643 ret = ab8500_charger_led_en(di, true);
1644 if (ret < 0)
1645 dev_err(di->dev, "failed to enable LED\n");
1646
34c11a70
POH
1647 di->usb.charger_online = 1;
1648
1649 /* USBChInputCurr: current that can be drawn from the usb */
f7470b5d
LJ
1650 ret = ab8500_charger_set_vbus_in_curr(di,
1651 di->max_usb_in_curr.usb_type_max);
34c11a70
POH
1652 if (ret) {
1653 dev_err(di->dev, "setting USBChInputCurr failed\n");
1654 return ret;
1655 }
1656
1657 /* ChOutputCurentLevel: protected output current */
1658 ret = ab8500_charger_set_output_curr(di, ich_out);
1659 if (ret) {
1660 dev_err(di->dev, "%s "
1661 "Failed to set ChOutputCurentLevel\n",
1662 __func__);
1663 return ret;
1664 }
1665
84edbeea
AM
1666 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1667
84edbeea
AM
1668 } else {
1669 /* Disable USB charging */
34c11a70 1670 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
84edbeea
AM
1671 ret = abx500_set_register_interruptible(di->dev,
1672 AB8500_CHARGER,
1673 AB8500_USBCH_CTRL1_REG, 0);
1674 if (ret) {
1675 dev_err(di->dev,
1676 "%s write failed\n", __func__);
1677 return ret;
1678 }
1679
1680 ret = ab8500_charger_led_en(di, false);
1681 if (ret < 0)
1682 dev_err(di->dev, "failed to disable LED\n");
34c11a70
POH
1683 /* USBChInputCurr: current that can be drawn from the usb */
1684 ret = ab8500_charger_set_vbus_in_curr(di, 0);
1685 if (ret) {
1686 dev_err(di->dev, "setting USBChInputCurr failed\n");
1687 return ret;
1688 }
84edbeea 1689
34c11a70
POH
1690 /* ChOutputCurentLevel: protected output current */
1691 ret = ab8500_charger_set_output_curr(di, 0);
1692 if (ret) {
1693 dev_err(di->dev, "%s "
1694 "Failed to reset ChOutputCurentLevel\n",
1695 __func__);
1696 return ret;
1697 }
84edbeea
AM
1698 di->usb.charger_online = 0;
1699 di->usb.wd_expired = false;
1700
1701 /* Disable regulator if enabled */
1702 if (di->vddadc_en_usb) {
1703 regulator_disable(di->regu);
1704 di->vddadc_en_usb = false;
1705 }
1706
1707 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1708
1709 /* Cancel any pending Vbat check work */
1710 if (delayed_work_pending(&di->check_vbat_work))
1711 cancel_delayed_work(&di->check_vbat_work);
1712
1713 }
1714 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1715
1716 return ret;
1717}
1718
8891716e
LJ
1719static int ab8500_external_charger_prepare(struct notifier_block *charger_nb,
1720 unsigned long event, void *data)
1721{
1722 int ret;
1723 struct device *dev = data;
1724 /*Toggle External charger control pin*/
1725 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1726 AB8500_SYS_CHARGER_CONTROL_REG,
1727 EXTERNAL_CHARGER_DISABLE_REG_VAL);
1728 if (ret < 0) {
1729 dev_err(dev, "write reg failed %d\n", ret);
1730 goto out;
1731 }
1732 ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
1733 AB8500_SYS_CHARGER_CONTROL_REG,
1734 EXTERNAL_CHARGER_ENABLE_REG_VAL);
1735 if (ret < 0)
1736 dev_err(dev, "Write reg failed %d\n", ret);
1737
1738out:
1739 return ret;
1740}
1741
4dcdf577
LJ
1742/**
1743 * ab8500_charger_usb_check_enable() - enable usb charging
1744 * @charger: pointer to the ux500_charger structure
1745 * @vset: charging voltage
1746 * @iset: charger output current
1747 *
1748 * Check if the VBUS charger has been disconnected and reconnected without
1749 * AB8500 rising an interrupt. Returns 0 on success.
1750 */
1751static int ab8500_charger_usb_check_enable(struct ux500_charger *charger,
1752 int vset, int iset)
1753{
1754 u8 usbch_ctrl1 = 0;
1755 int ret = 0;
1756
1757 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1758
1759 if (!di->usb.charger_connected)
1760 return ret;
1761
1762 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1763 AB8500_USBCH_CTRL1_REG, &usbch_ctrl1);
1764 if (ret < 0) {
1765 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1766 return ret;
1767 }
1768 dev_dbg(di->dev, "USB charger ctrl: 0x%02x\n", usbch_ctrl1);
1769
1770 if (!(usbch_ctrl1 & USB_CH_ENA)) {
1771 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1772
1773 ret = abx500_mask_and_set_register_interruptible(di->dev,
1774 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1775 DROP_COUNT_RESET, DROP_COUNT_RESET);
1776 if (ret < 0) {
1777 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1778 return ret;
1779 }
1780
1781 ret = ab8500_charger_usb_en(&di->usb_chg, true, vset, iset);
1782 if (ret < 0) {
1783 dev_err(di->dev, "Failed to enable VBUS charger %d\n",
1784 __LINE__);
1785 return ret;
1786 }
1787 }
1788 return ret;
1789}
1790
1791/**
1792 * ab8500_charger_ac_check_enable() - enable usb charging
1793 * @charger: pointer to the ux500_charger structure
1794 * @vset: charging voltage
1795 * @iset: charger output current
1796 *
1797 * Check if the AC charger has been disconnected and reconnected without
1798 * AB8500 rising an interrupt. Returns 0 on success.
1799 */
1800static int ab8500_charger_ac_check_enable(struct ux500_charger *charger,
1801 int vset, int iset)
1802{
1803 u8 mainch_ctrl1 = 0;
1804 int ret = 0;
1805
1806 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1807
1808 if (!di->ac.charger_connected)
1809 return ret;
1810
1811 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1812 AB8500_MCH_CTRL1, &mainch_ctrl1);
1813 if (ret < 0) {
1814 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
1815 return ret;
1816 }
1817 dev_dbg(di->dev, "AC charger ctrl: 0x%02x\n", mainch_ctrl1);
1818
1819 if (!(mainch_ctrl1 & MAIN_CH_ENA)) {
1820 dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
1821
1822 ret = abx500_mask_and_set_register_interruptible(di->dev,
1823 AB8500_CHARGER, AB8500_CHARGER_CTRL,
1824 DROP_COUNT_RESET, DROP_COUNT_RESET);
1825
1826 if (ret < 0) {
1827 dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
1828 return ret;
1829 }
1830
1831 ret = ab8500_charger_ac_en(&di->usb_chg, true, vset, iset);
1832 if (ret < 0) {
1833 dev_err(di->dev, "failed to enable AC charger %d\n",
1834 __LINE__);
1835 return ret;
1836 }
1837 }
1838 return ret;
1839}
1840
84edbeea
AM
1841/**
1842 * ab8500_charger_watchdog_kick() - kick charger watchdog
1843 * @di: pointer to the ab8500_charger structure
1844 *
1845 * Kick charger watchdog
1846 * Returns error code in case of failure else 0(on success)
1847 */
1848static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1849{
1850 int ret;
1851 struct ab8500_charger *di;
1852
1853 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1854 di = to_ab8500_charger_ac_device_info(charger);
1855 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1856 di = to_ab8500_charger_usb_device_info(charger);
1857 else
1858 return -ENXIO;
1859
1860 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1861 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1862 if (ret)
1863 dev_err(di->dev, "Failed to kick WD!\n");
1864
1865 return ret;
1866}
1867
1868/**
1869 * ab8500_charger_update_charger_current() - update charger current
1870 * @di: pointer to the ab8500_charger structure
1871 *
1872 * Update the charger output current for the specified charger
1873 * Returns error code in case of failure else 0(on success)
1874 */
1875static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1876 int ich_out)
1877{
1878 int ret;
84edbeea
AM
1879 struct ab8500_charger *di;
1880
1881 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1882 di = to_ab8500_charger_ac_device_info(charger);
1883 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1884 di = to_ab8500_charger_usb_device_info(charger);
1885 else
1886 return -ENXIO;
1887
f8e96dff 1888 ret = ab8500_charger_set_output_curr(di, ich_out);
84edbeea 1889 if (ret) {
f8e96dff
JB
1890 dev_err(di->dev, "%s "
1891 "Failed to set ChOutputCurentLevel\n",
1892 __func__);
84edbeea
AM
1893 return ret;
1894 }
1895
1896 /* Reset the main and usb drop input current measurement counter */
1897 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
4dcdf577 1898 AB8500_CHARGER_CTRL, DROP_COUNT_RESET);
84edbeea
AM
1899 if (ret) {
1900 dev_err(di->dev, "%s write failed\n", __func__);
1901 return ret;
1902 }
1903
1904 return ret;
1905}
1906
1907static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1908{
1909 struct power_supply *psy;
1910 struct power_supply *ext;
1911 struct ab8500_charger *di;
1912 union power_supply_propval ret;
1913 int i, j;
1914 bool psy_found = false;
1915 struct ux500_charger *usb_chg;
1916
1917 usb_chg = (struct ux500_charger *)data;
1918 psy = &usb_chg->psy;
1919
1920 di = to_ab8500_charger_usb_device_info(usb_chg);
1921
1922 ext = dev_get_drvdata(dev);
1923
1924 /* For all psy where the driver name appears in any supplied_to */
1925 for (i = 0; i < ext->num_supplicants; i++) {
1926 if (!strcmp(ext->supplied_to[i], psy->name))
1927 psy_found = true;
1928 }
1929
1930 if (!psy_found)
1931 return 0;
1932
1933 /* Go through all properties for the psy */
1934 for (j = 0; j < ext->num_properties; j++) {
1935 enum power_supply_property prop;
1936 prop = ext->properties[j];
1937
1938 if (ext->get_property(ext, prop, &ret))
1939 continue;
1940
1941 switch (prop) {
1942 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1943 switch (ext->type) {
1944 case POWER_SUPPLY_TYPE_BATTERY:
1945 di->vbat = ret.intval / 1000;
1946 break;
1947 default:
1948 break;
1949 }
1950 break;
1951 default:
1952 break;
1953 }
1954 }
1955 return 0;
1956}
1957
1958/**
1959 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1960 * @work pointer to the work_struct structure
1961 *
1962 * Due to a asic bug it is necessary to lower the input current to the vbus
1963 * charger when charging with at some specific levels. This issue is only valid
1964 * for below a certain battery voltage. This function makes sure that the
1965 * the allowed current limit isn't exceeded.
1966 */
1967static void ab8500_charger_check_vbat_work(struct work_struct *work)
1968{
1969 int t = 10;
1970 struct ab8500_charger *di = container_of(work,
1971 struct ab8500_charger, check_vbat_work.work);
1972
1973 class_for_each_device(power_supply_class, NULL,
1974 &di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1975
1976 /* First run old_vbat is 0. */
1977 if (di->old_vbat == 0)
1978 di->old_vbat = di->vbat;
1979
1980 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1981 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1982 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1983 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1984
1985 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
f7470b5d
LJ
1986 " old: %d\n", di->max_usb_in_curr.usb_type_max,
1987 di->vbat, di->old_vbat);
1988 ab8500_charger_set_vbus_in_curr(di,
1989 di->max_usb_in_curr.usb_type_max);
84edbeea
AM
1990 power_supply_changed(&di->usb_chg.psy);
1991 }
1992
1993 di->old_vbat = di->vbat;
1994
1995 /*
1996 * No need to check the battery voltage every second when not close to
1997 * the threshold.
1998 */
1999 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
2000 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
2001 t = 1;
2002
2003 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
2004}
2005
2006/**
2007 * ab8500_charger_check_hw_failure_work() - check main charger failure
2008 * @work: pointer to the work_struct structure
2009 *
2010 * Work queue function for checking the main charger status
2011 */
2012static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
2013{
2014 int ret;
2015 u8 reg_value;
2016
2017 struct ab8500_charger *di = container_of(work,
2018 struct ab8500_charger, check_hw_failure_work.work);
2019
2020 /* Check if the status bits for HW failure is still active */
2021 if (di->flags.mainextchnotok) {
2022 ret = abx500_get_register_interruptible(di->dev,
2023 AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
2024 if (ret < 0) {
2025 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2026 return;
2027 }
2028 if (!(reg_value & MAIN_CH_NOK)) {
2029 di->flags.mainextchnotok = false;
2030 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2031 }
2032 }
2033 if (di->flags.vbus_ovv) {
2034 ret = abx500_get_register_interruptible(di->dev,
2035 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
2036 &reg_value);
2037 if (ret < 0) {
2038 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2039 return;
2040 }
2041 if (!(reg_value & VBUS_OVV_TH)) {
2042 di->flags.vbus_ovv = false;
2043 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2044 }
2045 }
2046 /* If we still have a failure, schedule a new check */
2047 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2048 queue_delayed_work(di->charger_wq,
2049 &di->check_hw_failure_work, round_jiffies(HZ));
2050 }
2051}
2052
2053/**
2054 * ab8500_charger_kick_watchdog_work() - kick the watchdog
2055 * @work: pointer to the work_struct structure
2056 *
2057 * Work queue function for kicking the charger watchdog.
2058 *
2059 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2060 * logic. That means we have to continously kick the charger
2061 * watchdog even when no charger is connected. This is only
2062 * valid once the AC charger has been enabled. This is
2063 * a bug that is not handled by the algorithm and the
2064 * watchdog have to be kicked by the charger driver
2065 * when the AC charger is disabled
2066 */
2067static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
2068{
2069 int ret;
2070
2071 struct ab8500_charger *di = container_of(work,
2072 struct ab8500_charger, kick_wd_work.work);
2073
2074 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2075 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2076 if (ret)
2077 dev_err(di->dev, "Failed to kick WD!\n");
2078
2079 /* Schedule a new watchdog kick */
2080 queue_delayed_work(di->charger_wq,
2081 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
2082}
2083
2084/**
2085 * ab8500_charger_ac_work() - work to get and set main charger status
2086 * @work: pointer to the work_struct structure
2087 *
2088 * Work queue function for checking the main charger status
2089 */
2090static void ab8500_charger_ac_work(struct work_struct *work)
2091{
2092 int ret;
2093
2094 struct ab8500_charger *di = container_of(work,
2095 struct ab8500_charger, ac_work);
2096
2097 /*
2098 * Since we can't be sure that the events are received
2099 * synchronously, we have the check if the main charger is
2100 * connected by reading the status register
2101 */
34c11a70 2102 ret = ab8500_charger_detect_chargers(di, false);
84edbeea
AM
2103 if (ret < 0)
2104 return;
2105
2106 if (ret & AC_PW_CONN) {
2107 di->ac.charger_connected = 1;
2108 di->ac_conn = true;
2109 } else {
2110 di->ac.charger_connected = 0;
2111 }
2112
2113 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2114 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
2115}
2116
b269fff4
LJ
2117static void ab8500_charger_usb_attached_work(struct work_struct *work)
2118{
2119 struct ab8500_charger *di = container_of(work,
2120 struct ab8500_charger,
2121 usb_charger_attached_work.work);
2122 int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
2123 int ret, i;
2124 u8 statval;
2125
2126 for (i = 0; i < 10; i++) {
2127 ret = abx500_get_register_interruptible(di->dev,
2128 AB8500_CHARGER,
2129 AB8500_CH_USBCH_STAT1_REG,
2130 &statval);
2131 if (ret < 0) {
2132 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2133 goto reschedule;
2134 }
2135 if ((statval & usbch) != usbch)
2136 goto reschedule;
2137
2138 msleep(CHARGER_STATUS_POLL);
2139 }
2140
2141 ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
2142
2143 mutex_lock(&di->charger_attached_mutex);
2144 mutex_unlock(&di->charger_attached_mutex);
2145
2146 return;
2147
2148reschedule:
2149 queue_delayed_work(di->charger_wq,
2150 &di->usb_charger_attached_work,
2151 HZ);
2152}
2153
2154static void ab8500_charger_ac_attached_work(struct work_struct *work)
2155{
2156
2157 struct ab8500_charger *di = container_of(work,
2158 struct ab8500_charger,
2159 ac_charger_attached_work.work);
2160 int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
2161 MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
2162 int ret, i;
2163 u8 statval;
2164
2165 for (i = 0; i < 10; i++) {
2166 ret = abx500_get_register_interruptible(di->dev,
2167 AB8500_CHARGER,
2168 AB8500_CH_STATUS2_REG,
2169 &statval);
2170 if (ret < 0) {
2171 dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
2172 goto reschedule;
2173 }
2174
2175 if ((statval & mainch) != mainch)
2176 goto reschedule;
2177
2178 msleep(CHARGER_STATUS_POLL);
2179 }
2180
2181 ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
2182 queue_work(di->charger_wq, &di->ac_work);
2183
2184 mutex_lock(&di->charger_attached_mutex);
2185 mutex_unlock(&di->charger_attached_mutex);
2186
2187 return;
2188
2189reschedule:
2190 queue_delayed_work(di->charger_wq,
2191 &di->ac_charger_attached_work,
2192 HZ);
2193}
2194
84edbeea
AM
2195/**
2196 * ab8500_charger_detect_usb_type_work() - work to detect USB type
2197 * @work: Pointer to the work_struct structure
2198 *
2199 * Detect the type of USB plugged
2200 */
64eb9b02 2201static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
84edbeea
AM
2202{
2203 int ret;
2204
2205 struct ab8500_charger *di = container_of(work,
2206 struct ab8500_charger, detect_usb_type_work);
2207
2208 /*
2209 * Since we can't be sure that the events are received
2210 * synchronously, we have the check if is
2211 * connected by reading the status register
2212 */
34c11a70 2213 ret = ab8500_charger_detect_chargers(di, false);
84edbeea
AM
2214 if (ret < 0)
2215 return;
2216
2217 if (!(ret & USB_PW_CONN)) {
34c11a70
POH
2218 dev_dbg(di->dev, "%s di->vbus_detected = false\n", __func__);
2219 di->vbus_detected = false;
84edbeea
AM
2220 ab8500_charger_set_usb_connected(di, false);
2221 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2222 } else {
34c11a70
POH
2223 dev_dbg(di->dev, "%s di->vbus_detected = true\n", __func__);
2224 di->vbus_detected = true;
84edbeea
AM
2225
2226 if (is_ab8500_1p1_or_earlier(di->parent)) {
2227 ret = ab8500_charger_detect_usb_type(di);
2228 if (!ret) {
2229 ab8500_charger_set_usb_connected(di, true);
2230 ab8500_power_supply_changed(di,
2231 &di->usb_chg.psy);
2232 }
2233 } else {
34c11a70
POH
2234 /*
2235 * For ABB cut2.0 and onwards we have an IRQ,
84edbeea
AM
2236 * USB_LINK_STATUS that will be triggered when the USB
2237 * link status changes. The exception is USB connected
2238 * during startup. Then we don't get a
2239 * USB_LINK_STATUS IRQ
2240 */
2241 if (di->vbus_detected_start) {
2242 di->vbus_detected_start = false;
2243 ret = ab8500_charger_detect_usb_type(di);
2244 if (!ret) {
2245 ab8500_charger_set_usb_connected(di,
2246 true);
2247 ab8500_power_supply_changed(di,
2248 &di->usb_chg.psy);
2249 }
2250 }
2251 }
2252 }
2253}
2254
4b45f4a9 2255/**
34c11a70 2256 * ab8500_charger_usb_link_attach_work() - work to detect USB type
4b45f4a9
MC
2257 * @work: pointer to the work_struct structure
2258 *
2259 * Detect the type of USB plugged
2260 */
2261static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
2262{
2263 struct ab8500_charger *di =
2264 container_of(work, struct ab8500_charger, attach_work.work);
2265 int ret;
2266
2267 /* Update maximum input current if USB enumeration is not detected */
2268 if (!di->usb.charger_online) {
f7470b5d
LJ
2269 ret = ab8500_charger_set_vbus_in_curr(di,
2270 di->max_usb_in_curr.usb_type_max);
4b45f4a9
MC
2271 if (ret)
2272 return;
2273 }
2274
2275 ab8500_charger_set_usb_connected(di, true);
2276 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2277}
2278
84edbeea
AM
2279/**
2280 * ab8500_charger_usb_link_status_work() - work to detect USB type
2281 * @work: pointer to the work_struct structure
2282 *
2283 * Detect the type of USB plugged
2284 */
2285static void ab8500_charger_usb_link_status_work(struct work_struct *work)
2286{
ff38090a 2287 int detected_chargers;
84edbeea 2288 int ret;
ff38090a 2289 u8 val;
84edbeea
AM
2290
2291 struct ab8500_charger *di = container_of(work,
2292 struct ab8500_charger, usb_link_status_work);
2293
2294 /*
2295 * Since we can't be sure that the events are received
2296 * synchronously, we have the check if is
2297 * connected by reading the status register
2298 */
34c11a70 2299 detected_chargers = ab8500_charger_detect_chargers(di, false);
ff38090a 2300 if (detected_chargers < 0)
84edbeea
AM
2301 return;
2302
ff38090a
HS
2303 /*
2304 * Some chargers that breaks the USB spec is
2305 * identified as invalid by AB8500 and it refuse
2306 * to start the charging process. but by jumping
2307 * thru a few hoops it can be forced to start.
2308 */
2309 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2310 AB8500_USB_LINE_STAT_REG, &val);
2311 if (ret >= 0)
2312 dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val);
2313 else
2314 dev_dbg(di->dev, "Error reading USB link status\n");
2315
2316 if (detected_chargers & USB_PW_CONN) {
2317 if (((val & AB8500_USB_LINK_STATUS) >> 3) == USB_STAT_NOT_VALID_LINK &&
2318 di->invalid_charger_detect_state == 0) {
2319 dev_dbg(di->dev, "Invalid charger detected, state= 0\n");
2320 /*Enable charger*/
2321 abx500_mask_and_set_register_interruptible(di->dev,
2322 AB8500_CHARGER, AB8500_USBCH_CTRL1_REG, 0x01, 0x01);
2323 /*Enable charger detection*/
2324 abx500_mask_and_set_register_interruptible(di->dev, AB8500_USB,
2325 AB8500_MCH_IPT_CURLVL_REG, 0x01, 0x01);
2326 di->invalid_charger_detect_state = 1;
2327 /*exit and wait for new link status interrupt.*/
2328 return;
2329
2330 }
2331 if (di->invalid_charger_detect_state == 1) {
2332 dev_dbg(di->dev, "Invalid charger detected, state= 1\n");
2333 /*Stop charger detection*/
2334 abx500_mask_and_set_register_interruptible(di->dev, AB8500_USB,
2335 AB8500_MCH_IPT_CURLVL_REG, 0x01, 0x00);
2336 /*Check link status*/
2337 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
2338 AB8500_USB_LINE_STAT_REG, &val);
2339 dev_dbg(di->dev, "USB link status= 0x%02x\n",
2340 (val & AB8500_USB_LINK_STATUS) >> 3);
2341 di->invalid_charger_detect_state = 2;
2342 }
2343 } else {
2344 di->invalid_charger_detect_state = 0;
2345 }
2346
2347 if (!(detected_chargers & USB_PW_CONN)) {
34c11a70 2348 di->vbus_detected = false;
84edbeea
AM
2349 ab8500_charger_set_usb_connected(di, false);
2350 ab8500_power_supply_changed(di, &di->usb_chg.psy);
4b45f4a9
MC
2351 return;
2352 }
84edbeea 2353
34c11a70
POH
2354 dev_dbg(di->dev,"%s di->vbus_detected = true\n",__func__);
2355 di->vbus_detected = true;
4b45f4a9 2356 ret = ab8500_charger_read_usb_type(di);
34c11a70
POH
2357 if (ret) {
2358 if (ret == -ENXIO) {
2359 /* No valid charger type detected */
2360 ab8500_charger_set_usb_connected(di, false);
2361 ab8500_power_supply_changed(di, &di->usb_chg.psy);
84edbeea 2362 }
34c11a70
POH
2363 return;
2364 }
2365
2366 if (di->usb_device_is_unrecognised) {
2367 dev_dbg(di->dev,
2368 "Potential Legacy Charger device. "
2369 "Delay work for %d msec for USB enum "
2370 "to finish",
2371 WAIT_ACA_RID_ENUMERATION);
2372 queue_delayed_work(di->charger_wq,
2373 &di->attach_work,
2374 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2375 } else if (di->is_aca_rid == 1) {
2376 /* Only wait once */
2377 di->is_aca_rid++;
2378 dev_dbg(di->dev,
2379 "%s Wait %d msec for USB enum to finish",
2380 __func__, WAIT_ACA_RID_ENUMERATION);
2381 queue_delayed_work(di->charger_wq,
2382 &di->attach_work,
2383 msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
2384 } else {
2385 queue_delayed_work(di->charger_wq,
2386 &di->attach_work,
2387 0);
84edbeea
AM
2388 }
2389}
2390
2391static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
2392{
2393 int ret;
2394 unsigned long flags;
2395
2396 struct ab8500_charger *di = container_of(work,
34c11a70 2397 struct ab8500_charger, usb_state_changed_work.work);
84edbeea 2398
34c11a70
POH
2399 if (!di->vbus_detected) {
2400 dev_dbg(di->dev,
2401 "%s !di->vbus_detected\n",
2402 __func__);
84edbeea 2403 return;
34c11a70 2404 }
84edbeea
AM
2405
2406 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
34c11a70
POH
2407 di->usb_state.state = di->usb_state.state_tmp;
2408 di->usb_state.usb_current = di->usb_state.usb_current_tmp;
84edbeea
AM
2409 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
2410
84edbeea
AM
2411 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
2412 __func__, di->usb_state.state, di->usb_state.usb_current);
2413
2414 switch (di->usb_state.state) {
2415 case AB8500_BM_USB_STATE_RESET_HS:
2416 case AB8500_BM_USB_STATE_RESET_FS:
2417 case AB8500_BM_USB_STATE_SUSPEND:
2418 case AB8500_BM_USB_STATE_MAX:
2419 ab8500_charger_set_usb_connected(di, false);
2420 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2421 break;
2422
2423 case AB8500_BM_USB_STATE_RESUME:
2424 /*
2425 * when suspend->resume there should be delay
2426 * of 1sec for enabling charging
2427 */
2428 msleep(1000);
2429 /* Intentional fall through */
2430 case AB8500_BM_USB_STATE_CONFIGURED:
2431 /*
2432 * USB is configured, enable charging with the charging
2433 * input current obtained from USB driver
2434 */
2435 if (!ab8500_charger_get_usb_cur(di)) {
2436 /* Update maximum input current */
2437 ret = ab8500_charger_set_vbus_in_curr(di,
f7470b5d 2438 di->max_usb_in_curr.usb_type_max);
84edbeea
AM
2439 if (ret)
2440 return;
2441
2442 ab8500_charger_set_usb_connected(di, true);
2443 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2444 }
2445 break;
2446
2447 default:
2448 break;
2449 };
2450}
2451
2452/**
2453 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
2454 * @work: pointer to the work_struct structure
2455 *
2456 * Work queue function for checking the USB charger Not OK status
2457 */
2458static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
2459{
2460 int ret;
2461 u8 reg_value;
2462 bool prev_status;
2463
2464 struct ab8500_charger *di = container_of(work,
2465 struct ab8500_charger, check_usbchgnotok_work.work);
2466
2467 /* Check if the status bit for usbchargernotok is still active */
2468 ret = abx500_get_register_interruptible(di->dev,
2469 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
2470 if (ret < 0) {
2471 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2472 return;
2473 }
2474 prev_status = di->flags.usbchargernotok;
2475
2476 if (reg_value & VBUS_CH_NOK) {
2477 di->flags.usbchargernotok = true;
2478 /* Check again in 1sec */
2479 queue_delayed_work(di->charger_wq,
2480 &di->check_usbchgnotok_work, HZ);
2481 } else {
2482 di->flags.usbchargernotok = false;
2483 di->flags.vbus_collapse = false;
2484 }
2485
2486 if (prev_status != di->flags.usbchargernotok)
2487 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2488}
2489
2490/**
2491 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
2492 * @work: pointer to the work_struct structure
2493 *
2494 * Work queue function for checking the Main thermal prot status
2495 */
2496static void ab8500_charger_check_main_thermal_prot_work(
2497 struct work_struct *work)
2498{
2499 int ret;
2500 u8 reg_value;
2501
2502 struct ab8500_charger *di = container_of(work,
2503 struct ab8500_charger, check_main_thermal_prot_work);
2504
2505 /* Check if the status bit for main_thermal_prot is still active */
2506 ret = abx500_get_register_interruptible(di->dev,
2507 AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
2508 if (ret < 0) {
2509 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2510 return;
2511 }
2512 if (reg_value & MAIN_CH_TH_PROT)
2513 di->flags.main_thermal_prot = true;
2514 else
2515 di->flags.main_thermal_prot = false;
2516
2517 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2518}
2519
2520/**
2521 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
2522 * @work: pointer to the work_struct structure
2523 *
2524 * Work queue function for checking the USB thermal prot status
2525 */
2526static void ab8500_charger_check_usb_thermal_prot_work(
2527 struct work_struct *work)
2528{
2529 int ret;
2530 u8 reg_value;
2531
2532 struct ab8500_charger *di = container_of(work,
2533 struct ab8500_charger, check_usb_thermal_prot_work);
2534
2535 /* Check if the status bit for usb_thermal_prot is still active */
2536 ret = abx500_get_register_interruptible(di->dev,
2537 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
2538 if (ret < 0) {
2539 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2540 return;
2541 }
2542 if (reg_value & USB_CH_TH_PROT)
2543 di->flags.usb_thermal_prot = true;
2544 else
2545 di->flags.usb_thermal_prot = false;
2546
2547 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2548}
2549
2550/**
2551 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
2552 * @irq: interrupt number
2553 * @_di: pointer to the ab8500_charger structure
2554 *
2555 * Returns IRQ status(IRQ_HANDLED)
2556 */
2557static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
2558{
2559 struct ab8500_charger *di = _di;
2560
2561 dev_dbg(di->dev, "Main charger unplugged\n");
2562 queue_work(di->charger_wq, &di->ac_work);
2563
b269fff4
LJ
2564 cancel_delayed_work_sync(&di->ac_charger_attached_work);
2565 mutex_lock(&di->charger_attached_mutex);
2566 mutex_unlock(&di->charger_attached_mutex);
2567
84edbeea
AM
2568 return IRQ_HANDLED;
2569}
2570
2571/**
2572 * ab8500_charger_mainchplugdet_handler() - main charger plugged
2573 * @irq: interrupt number
2574 * @_di: pointer to the ab8500_charger structure
2575 *
2576 * Returns IRQ status(IRQ_HANDLED)
2577 */
2578static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
2579{
2580 struct ab8500_charger *di = _di;
2581
2582 dev_dbg(di->dev, "Main charger plugged\n");
2583 queue_work(di->charger_wq, &di->ac_work);
2584
b269fff4
LJ
2585 mutex_lock(&di->charger_attached_mutex);
2586 mutex_unlock(&di->charger_attached_mutex);
2587 queue_delayed_work(di->charger_wq,
2588 &di->ac_charger_attached_work,
2589 HZ);
84edbeea
AM
2590 return IRQ_HANDLED;
2591}
2592
2593/**
2594 * ab8500_charger_mainextchnotok_handler() - main charger not ok
2595 * @irq: interrupt number
2596 * @_di: pointer to the ab8500_charger structure
2597 *
2598 * Returns IRQ status(IRQ_HANDLED)
2599 */
2600static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
2601{
2602 struct ab8500_charger *di = _di;
2603
2604 dev_dbg(di->dev, "Main charger not ok\n");
2605 di->flags.mainextchnotok = true;
2606 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2607
2608 /* Schedule a new HW failure check */
2609 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2610
2611 return IRQ_HANDLED;
2612}
2613
2614/**
2615 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
2616 * thermal protection threshold
2617 * @irq: interrupt number
2618 * @_di: pointer to the ab8500_charger structure
2619 *
2620 * Returns IRQ status(IRQ_HANDLED)
2621 */
2622static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
2623{
2624 struct ab8500_charger *di = _di;
2625
2626 dev_dbg(di->dev,
2627 "Die temp above Main charger thermal protection threshold\n");
2628 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2629
2630 return IRQ_HANDLED;
2631}
2632
2633/**
2634 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
2635 * thermal protection threshold
2636 * @irq: interrupt number
2637 * @_di: pointer to the ab8500_charger structure
2638 *
2639 * Returns IRQ status(IRQ_HANDLED)
2640 */
2641static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
2642{
2643 struct ab8500_charger *di = _di;
2644
2645 dev_dbg(di->dev,
2646 "Die temp ok for Main charger thermal protection threshold\n");
2647 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
2648
2649 return IRQ_HANDLED;
2650}
2651
34c11a70
POH
2652static void ab8500_charger_vbus_drop_end_work(struct work_struct *work)
2653{
2654 struct ab8500_charger *di = container_of(work,
2655 struct ab8500_charger, vbus_drop_end_work.work);
f7470b5d
LJ
2656 int ret;
2657 u8 reg_value;
34c11a70
POH
2658
2659 di->flags.vbus_drop_end = false;
2660
2661 /* Reset the drop counter */
2662 abx500_set_register_interruptible(di->dev,
2663 AB8500_CHARGER, AB8500_CHARGER_CTRL, 0x01);
f7470b5d
LJ
2664 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
2665 AB8500_CH_USBCH_STAT2_REG,
2666 &reg_value);
2667 if (ret < 0) {
2668 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
2669 } else {
2670 int curr = ab8500_charger_vbus_in_curr_map[
2671 reg_value >> AUTO_VBUS_IN_CURR_LIM_SHIFT];
2672 if (di->max_usb_in_curr.calculated_max != curr) {
2673 /* USB source is collapsing */
2674 di->max_usb_in_curr.calculated_max = curr;
2675 dev_dbg(di->dev,
2676 "VBUS input current limiting to %d mA\n",
2677 di->max_usb_in_curr.calculated_max);
2678 } else {
2679 /*
2680 * USB source can not give more than this amount.
2681 * Taking more will collapse the source.
2682 */
2683 di->max_usb_in_curr.set_max =
2684 di->max_usb_in_curr.calculated_max;
2685 dev_dbg(di->dev,
2686 "VBUS input current limited to %d mA\n",
2687 di->max_usb_in_curr.set_max);
2688 return;
2689 }
2690 }
34c11a70
POH
2691
2692 if (di->usb.charger_connected)
f7470b5d
LJ
2693 ab8500_charger_set_vbus_in_curr(di,
2694 di->max_usb_in_curr.usb_type_max);
34c11a70
POH
2695}
2696
84edbeea
AM
2697/**
2698 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
2699 * @irq: interrupt number
2700 * @_di: pointer to the ab8500_charger structure
2701 *
2702 * Returns IRQ status(IRQ_HANDLED)
2703 */
2704static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
2705{
2706 struct ab8500_charger *di = _di;
2707
34c11a70 2708 di->vbus_detected = false;
84edbeea
AM
2709 dev_dbg(di->dev, "VBUS falling detected\n");
2710 queue_work(di->charger_wq, &di->detect_usb_type_work);
2711
2712 return IRQ_HANDLED;
2713}
2714
2715/**
2716 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
2717 * @irq: interrupt number
2718 * @_di: pointer to the ab8500_charger structure
2719 *
2720 * Returns IRQ status(IRQ_HANDLED)
2721 */
2722static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
2723{
2724 struct ab8500_charger *di = _di;
2725
2726 di->vbus_detected = true;
2727 dev_dbg(di->dev, "VBUS rising detected\n");
34c11a70 2728
84edbeea
AM
2729 queue_work(di->charger_wq, &di->detect_usb_type_work);
2730
2731 return IRQ_HANDLED;
2732}
2733
2734/**
2735 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2736 * @irq: interrupt number
2737 * @_di: pointer to the ab8500_charger structure
2738 *
2739 * Returns IRQ status(IRQ_HANDLED)
2740 */
2741static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2742{
2743 struct ab8500_charger *di = _di;
2744
2745 dev_dbg(di->dev, "USB link status changed\n");
2746
2747 queue_work(di->charger_wq, &di->usb_link_status_work);
2748
2749 return IRQ_HANDLED;
2750}
2751
2752/**
2753 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2754 * thermal protection threshold
2755 * @irq: interrupt number
2756 * @_di: pointer to the ab8500_charger structure
2757 *
2758 * Returns IRQ status(IRQ_HANDLED)
2759 */
2760static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2761{
2762 struct ab8500_charger *di = _di;
2763
2764 dev_dbg(di->dev,
2765 "Die temp above USB charger thermal protection threshold\n");
2766 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2767
2768 return IRQ_HANDLED;
2769}
2770
2771/**
2772 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2773 * thermal protection threshold
2774 * @irq: interrupt number
2775 * @_di: pointer to the ab8500_charger structure
2776 *
2777 * Returns IRQ status(IRQ_HANDLED)
2778 */
2779static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2780{
2781 struct ab8500_charger *di = _di;
2782
2783 dev_dbg(di->dev,
2784 "Die temp ok for USB charger thermal protection threshold\n");
2785 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2786
2787 return IRQ_HANDLED;
2788}
2789
2790/**
2791 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2792 * @irq: interrupt number
2793 * @_di: pointer to the ab8500_charger structure
2794 *
2795 * Returns IRQ status(IRQ_HANDLED)
2796 */
2797static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2798{
2799 struct ab8500_charger *di = _di;
2800
2801 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2802 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2803
2804 return IRQ_HANDLED;
2805}
2806
2807/**
2808 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2809 * @irq: interrupt number
2810 * @_di: pointer to the ab8500_charger structure
2811 *
2812 * Returns IRQ status(IRQ_HANDLED)
2813 */
2814static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2815{
2816 struct ab8500_charger *di = _di;
2817
2818 dev_dbg(di->dev, "Charger watchdog expired\n");
2819
2820 /*
2821 * The charger that was online when the watchdog expired
2822 * needs to be restarted for charging to start again
2823 */
2824 if (di->ac.charger_online) {
2825 di->ac.wd_expired = true;
2826 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2827 }
2828 if (di->usb.charger_online) {
2829 di->usb.wd_expired = true;
2830 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2831 }
2832
2833 return IRQ_HANDLED;
2834}
2835
34c11a70
POH
2836/**
2837 * ab8500_charger_vbuschdropend_handler() - VBUS drop removed
2838 * @irq: interrupt number
2839 * @_di: pointer to the ab8500_charger structure
2840 *
2841 * Returns IRQ status(IRQ_HANDLED)
2842 */
2843static irqreturn_t ab8500_charger_vbuschdropend_handler(int irq, void *_di)
2844{
2845 struct ab8500_charger *di = _di;
2846
2847 dev_dbg(di->dev, "VBUS charger drop ended\n");
2848 di->flags.vbus_drop_end = true;
f7470b5d
LJ
2849
2850 /*
2851 * VBUS might have dropped due to bad connection.
2852 * Schedule a new input limit set to the value SW requests.
2853 */
34c11a70 2854 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work,
f7470b5d 2855 round_jiffies(VBUS_IN_CURR_LIM_RETRY_SET_TIME * HZ));
34c11a70
POH
2856
2857 return IRQ_HANDLED;
2858}
2859
84edbeea
AM
2860/**
2861 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2862 * @irq: interrupt number
2863 * @_di: pointer to the ab8500_charger structure
2864 *
2865 * Returns IRQ status(IRQ_HANDLED)
2866 */
2867static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2868{
2869 struct ab8500_charger *di = _di;
2870
2871 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2872 di->flags.vbus_ovv = true;
2873 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2874
2875 /* Schedule a new HW failure check */
2876 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2877
2878 return IRQ_HANDLED;
2879}
2880
2881/**
2882 * ab8500_charger_ac_get_property() - get the ac/mains properties
2883 * @psy: pointer to the power_supply structure
2884 * @psp: pointer to the power_supply_property structure
2885 * @val: pointer to the power_supply_propval union
2886 *
2887 * This function gets called when an application tries to get the ac/mains
2888 * properties by reading the sysfs files.
2889 * AC/Mains properties are online, present and voltage.
2890 * online: ac/mains charging is in progress or not
2891 * present: presence of the ac/mains
2892 * voltage: AC/Mains voltage
2893 * Returns error code in case of failure else 0(on success)
2894 */
2895static int ab8500_charger_ac_get_property(struct power_supply *psy,
2896 enum power_supply_property psp,
2897 union power_supply_propval *val)
2898{
2899 struct ab8500_charger *di;
a864c5a8 2900 int ret;
84edbeea
AM
2901
2902 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2903
2904 switch (psp) {
2905 case POWER_SUPPLY_PROP_HEALTH:
2906 if (di->flags.mainextchnotok)
2907 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2908 else if (di->ac.wd_expired || di->usb.wd_expired)
2909 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2910 else if (di->flags.main_thermal_prot)
2911 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2912 else
2913 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2914 break;
2915 case POWER_SUPPLY_PROP_ONLINE:
2916 val->intval = di->ac.charger_online;
2917 break;
2918 case POWER_SUPPLY_PROP_PRESENT:
2919 val->intval = di->ac.charger_connected;
2920 break;
2921 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
a864c5a8
JA
2922 ret = ab8500_charger_get_ac_voltage(di);
2923 if (ret >= 0)
2924 di->ac.charger_voltage = ret;
2925 /* On error, use previous value */
84edbeea
AM
2926 val->intval = di->ac.charger_voltage * 1000;
2927 break;
2928 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2929 /*
2930 * This property is used to indicate when CV mode is entered
2931 * for the AC charger
2932 */
2933 di->ac.cv_active = ab8500_charger_ac_cv(di);
2934 val->intval = di->ac.cv_active;
2935 break;
2936 case POWER_SUPPLY_PROP_CURRENT_NOW:
a864c5a8
JA
2937 ret = ab8500_charger_get_ac_current(di);
2938 if (ret >= 0)
2939 di->ac.charger_current = ret;
2940 val->intval = di->ac.charger_current * 1000;
84edbeea
AM
2941 break;
2942 default:
2943 return -EINVAL;
2944 }
2945 return 0;
2946}
2947
2948/**
2949 * ab8500_charger_usb_get_property() - get the usb properties
2950 * @psy: pointer to the power_supply structure
2951 * @psp: pointer to the power_supply_property structure
2952 * @val: pointer to the power_supply_propval union
2953 *
2954 * This function gets called when an application tries to get the usb
2955 * properties by reading the sysfs files.
2956 * USB properties are online, present and voltage.
2957 * online: usb charging is in progress or not
2958 * present: presence of the usb
2959 * voltage: vbus voltage
2960 * Returns error code in case of failure else 0(on success)
2961 */
2962static int ab8500_charger_usb_get_property(struct power_supply *psy,
2963 enum power_supply_property psp,
2964 union power_supply_propval *val)
2965{
2966 struct ab8500_charger *di;
a864c5a8 2967 int ret;
84edbeea
AM
2968
2969 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
2970
2971 switch (psp) {
2972 case POWER_SUPPLY_PROP_HEALTH:
2973 if (di->flags.usbchargernotok)
2974 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2975 else if (di->ac.wd_expired || di->usb.wd_expired)
2976 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2977 else if (di->flags.usb_thermal_prot)
2978 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2979 else if (di->flags.vbus_ovv)
2980 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
2981 else
2982 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2983 break;
2984 case POWER_SUPPLY_PROP_ONLINE:
2985 val->intval = di->usb.charger_online;
2986 break;
2987 case POWER_SUPPLY_PROP_PRESENT:
2988 val->intval = di->usb.charger_connected;
2989 break;
2990 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
a864c5a8
JA
2991 ret = ab8500_charger_get_vbus_voltage(di);
2992 if (ret >= 0)
2993 di->usb.charger_voltage = ret;
84edbeea
AM
2994 val->intval = di->usb.charger_voltage * 1000;
2995 break;
2996 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2997 /*
2998 * This property is used to indicate when CV mode is entered
2999 * for the USB charger
3000 */
3001 di->usb.cv_active = ab8500_charger_usb_cv(di);
3002 val->intval = di->usb.cv_active;
3003 break;
3004 case POWER_SUPPLY_PROP_CURRENT_NOW:
a864c5a8
JA
3005 ret = ab8500_charger_get_usb_current(di);
3006 if (ret >= 0)
3007 di->usb.charger_current = ret;
3008 val->intval = di->usb.charger_current * 1000;
84edbeea
AM
3009 break;
3010 case POWER_SUPPLY_PROP_CURRENT_AVG:
3011 /*
3012 * This property is used to indicate when VBUS has collapsed
3013 * due to too high output current from the USB charger
3014 */
3015 if (di->flags.vbus_collapse)
3016 val->intval = 1;
3017 else
3018 val->intval = 0;
3019 break;
3020 default:
3021 return -EINVAL;
3022 }
3023 return 0;
3024}
3025
3026/**
3027 * ab8500_charger_init_hw_registers() - Set up charger related registers
3028 * @di: pointer to the ab8500_charger structure
3029 *
3030 * Set up charger OVV, watchdog and maximum voltage registers as well as
3031 * charging of the backup battery
3032 */
3033static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
3034{
3035 int ret = 0;
0f4aa401 3036 u8 bup_vch_range = 0, vbup33_vrtcn = 0;
84edbeea
AM
3037
3038 /* Setup maximum charger current and voltage for ABB cut2.0 */
3039 if (!is_ab8500_1p1_or_earlier(di->parent)) {
3040 ret = abx500_set_register_interruptible(di->dev,
3041 AB8500_CHARGER,
3042 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
3043 if (ret) {
3044 dev_err(di->dev,
3045 "failed to set CH_VOLT_LVL_MAX_REG\n");
3046 goto out;
3047 }
3048
3049 ret = abx500_set_register_interruptible(di->dev,
3050 AB8500_CHARGER,
3051 AB8500_CH_OPT_CRNTLVL_MAX_REG, CH_OP_CUR_LVL_1P6);
3052 if (ret) {
3053 dev_err(di->dev,
3054 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
3055 goto out;
3056 }
3057 }
3058
0ed5107f
JA
3059 if (is_ab9540_2p0(di->parent) || is_ab8505_2p0(di->parent))
3060 ret = abx500_mask_and_set_register_interruptible(di->dev,
3061 AB8500_CHARGER,
3062 AB8500_USBCH_CTRL2_REG,
3063 VBUS_AUTO_IN_CURR_LIM_ENA,
3064 VBUS_AUTO_IN_CURR_LIM_ENA);
3065 else
3066 /*
3067 * VBUS OVV set to 6.3V and enable automatic current limitation
3068 */
3069 ret = abx500_set_register_interruptible(di->dev,
3070 AB8500_CHARGER,
3071 AB8500_USBCH_CTRL2_REG,
3072 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
84edbeea 3073 if (ret) {
0ed5107f
JA
3074 dev_err(di->dev,
3075 "failed to set automatic current limitation\n");
84edbeea
AM
3076 goto out;
3077 }
3078
3079 /* Enable main watchdog in OTP */
3080 ret = abx500_set_register_interruptible(di->dev,
3081 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
3082 if (ret) {
3083 dev_err(di->dev, "failed to enable main WD in OTP\n");
3084 goto out;
3085 }
3086
3087 /* Enable main watchdog */
3088 ret = abx500_set_register_interruptible(di->dev,
3089 AB8500_SYS_CTRL2_BLOCK,
3090 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
3091 if (ret) {
3092 dev_err(di->dev, "faile to enable main watchdog\n");
3093 goto out;
3094 }
3095
3096 /*
3097 * Due to internal synchronisation, Enable and Kick watchdog bits
3098 * cannot be enabled in a single write.
3099 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
3100 * between writing Enable then Kick bits.
3101 */
3102 udelay(63);
3103
3104 /* Kick main watchdog */
3105 ret = abx500_set_register_interruptible(di->dev,
3106 AB8500_SYS_CTRL2_BLOCK,
3107 AB8500_MAIN_WDOG_CTRL_REG,
3108 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
3109 if (ret) {
3110 dev_err(di->dev, "failed to kick main watchdog\n");
3111 goto out;
3112 }
3113
3114 /* Disable main watchdog */
3115 ret = abx500_set_register_interruptible(di->dev,
3116 AB8500_SYS_CTRL2_BLOCK,
3117 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
3118 if (ret) {
3119 dev_err(di->dev, "failed to disable main watchdog\n");
3120 goto out;
3121 }
3122
3123 /* Set watchdog timeout */
3124 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3125 AB8500_CH_WD_TIMER_REG, WD_TIMER);
3126 if (ret) {
3127 dev_err(di->dev, "failed to set charger watchdog timeout\n");
3128 goto out;
3129 }
3130
34c11a70
POH
3131 /* Set charger watchdog timeout */
3132 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3133 AB8500_CH_WD_TIMER_REG, WD_TIMER);
3134 if (ret) {
3135 dev_err(di->dev, "failed to set charger watchdog timeout\n");
3136 goto out;
3137 }
3138
3139 ret = ab8500_charger_led_en(di, false);
3140 if (ret < 0) {
3141 dev_err(di->dev, "failed to disable LED\n");
3142 goto out;
3143 }
3144
84edbeea 3145 /* Backup battery voltage and current */
0f4aa401
YQ
3146 if (di->bm->bkup_bat_v > BUP_VCH_SEL_3P1V)
3147 bup_vch_range = BUP_VCH_RANGE;
3148 if (di->bm->bkup_bat_v == BUP_VCH_SEL_3P3V)
3149 vbup33_vrtcn = VBUP33_VRTCN;
3150
84edbeea
AM
3151 ret = abx500_set_register_interruptible(di->dev,
3152 AB8500_RTC,
3153 AB8500_RTC_BACKUP_CHG_REG,
0f4aa401 3154 (di->bm->bkup_bat_v & 0x3) | di->bm->bkup_bat_i);
84edbeea
AM
3155 if (ret) {
3156 dev_err(di->dev, "failed to setup backup battery charging\n");
3157 goto out;
3158 }
0f4aa401
YQ
3159 if (is_ab8540(di->parent)) {
3160 ret = abx500_set_register_interruptible(di->dev,
3161 AB8500_RTC,
3162 AB8500_RTC_CTRL1_REG,
3163 bup_vch_range | vbup33_vrtcn);
3164 if (ret) {
3165 dev_err(di->dev, "failed to setup backup battery charging\n");
3166 goto out;
3167 }
3168 }
84edbeea
AM
3169
3170 /* Enable backup battery charging */
3171 abx500_mask_and_set_register_interruptible(di->dev,
3172 AB8500_RTC, AB8500_RTC_CTRL_REG,
3173 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
3174 if (ret < 0)
3175 dev_err(di->dev, "%s mask and set failed\n", __func__);
3176
3177out:
3178 return ret;
3179}
3180
3181/*
3182 * ab8500 charger driver interrupts and their respective isr
3183 */
3184static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
3185 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
3186 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
3187 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
3188 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
3189 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
3190 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
3191 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
3192 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
3193 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
3194 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
3195 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
3196 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
3197 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
34c11a70 3198 {"VBUS_CH_DROP_END", ab8500_charger_vbuschdropend_handler},
84edbeea
AM
3199};
3200
3201static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
3202 unsigned long event, void *power)
3203{
3204 struct ab8500_charger *di =
3205 container_of(nb, struct ab8500_charger, nb);
3206 enum ab8500_usb_state bm_usb_state;
3207 unsigned mA = *((unsigned *)power);
3208
c9ade0fc
LJ
3209 if (!di)
3210 return NOTIFY_DONE;
3211
84edbeea
AM
3212 if (event != USB_EVENT_VBUS) {
3213 dev_dbg(di->dev, "not a standard host, returning\n");
3214 return NOTIFY_DONE;
3215 }
3216
3217 /* TODO: State is fabricate here. See if charger really needs USB
3218 * state or if mA is enough
3219 */
3220 if ((di->usb_state.usb_current == 2) && (mA > 2))
3221 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
3222 else if (mA == 0)
3223 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
3224 else if (mA == 2)
3225 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
3226 else if (mA >= 8) /* 8, 100, 500 */
3227 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
3228 else /* Should never occur */
3229 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
3230
3231 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
3232 __func__, bm_usb_state, mA);
3233
3234 spin_lock(&di->usb_state.usb_lock);
34c11a70
POH
3235 di->usb_state.state_tmp = bm_usb_state;
3236 di->usb_state.usb_current_tmp = mA;
84edbeea
AM
3237 spin_unlock(&di->usb_state.usb_lock);
3238
34c11a70
POH
3239 /*
3240 * wait for some time until you get updates from the usb stack
3241 * and negotiations are completed
3242 */
3243 queue_delayed_work(di->charger_wq, &di->usb_state_changed_work, HZ/2);
84edbeea
AM
3244
3245 return NOTIFY_OK;
3246}
3247
3248#if defined(CONFIG_PM)
3249static int ab8500_charger_resume(struct platform_device *pdev)
3250{
3251 int ret;
3252 struct ab8500_charger *di = platform_get_drvdata(pdev);
3253
3254 /*
3255 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3256 * logic. That means we have to continously kick the charger
3257 * watchdog even when no charger is connected. This is only
3258 * valid once the AC charger has been enabled. This is
3259 * a bug that is not handled by the algorithm and the
3260 * watchdog have to be kicked by the charger driver
3261 * when the AC charger is disabled
3262 */
3263 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
3264 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
3265 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
3266 if (ret)
3267 dev_err(di->dev, "Failed to kick WD!\n");
3268
3269 /* If not already pending start a new timer */
3270 if (!delayed_work_pending(
3271 &di->kick_wd_work)) {
3272 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
3273 round_jiffies(WD_KICK_INTERVAL));
3274 }
3275 }
3276
3277 /* If we still have a HW failure, schedule a new check */
3278 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
3279 queue_delayed_work(di->charger_wq,
3280 &di->check_hw_failure_work, 0);
3281 }
3282
34c11a70
POH
3283 if (di->flags.vbus_drop_end)
3284 queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work, 0);
3285
84edbeea
AM
3286 return 0;
3287}
3288
3289static int ab8500_charger_suspend(struct platform_device *pdev,
3290 pm_message_t state)
3291{
3292 struct ab8500_charger *di = platform_get_drvdata(pdev);
3293
3294 /* Cancel any pending HW failure check */
3295 if (delayed_work_pending(&di->check_hw_failure_work))
3296 cancel_delayed_work(&di->check_hw_failure_work);
3297
34c11a70
POH
3298 if (delayed_work_pending(&di->vbus_drop_end_work))
3299 cancel_delayed_work(&di->vbus_drop_end_work);
3300
53ef1f59
JA
3301 flush_delayed_work(&di->attach_work);
3302 flush_delayed_work(&di->usb_charger_attached_work);
3303 flush_delayed_work(&di->ac_charger_attached_work);
3304 flush_delayed_work(&di->check_usbchgnotok_work);
3305 flush_delayed_work(&di->check_vbat_work);
3306 flush_delayed_work(&di->kick_wd_work);
3307
3308 flush_work(&di->usb_link_status_work);
3309 flush_work(&di->ac_work);
3310 flush_work(&di->detect_usb_type_work);
3311
34c11a70
POH
3312 if (atomic_read(&di->current_stepping_sessions))
3313 return -EAGAIN;
3314
84edbeea
AM
3315 return 0;
3316}
3317#else
3318#define ab8500_charger_suspend NULL
3319#define ab8500_charger_resume NULL
3320#endif
3321
8891716e
LJ
3322static struct notifier_block charger_nb = {
3323 .notifier_call = ab8500_external_charger_prepare,
3324};
3325
415ec69f 3326static int ab8500_charger_remove(struct platform_device *pdev)
84edbeea
AM
3327{
3328 struct ab8500_charger *di = platform_get_drvdata(pdev);
3329 int i, irq, ret;
3330
3331 /* Disable AC charging */
3332 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
3333
3334 /* Disable USB charging */
3335 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
3336
3337 /* Disable interrupts */
3338 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3339 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3340 free_irq(irq, di);
3341 }
3342
84edbeea
AM
3343 /* Backup battery voltage and current disable */
3344 ret = abx500_mask_and_set_register_interruptible(di->dev,
3345 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
3346 if (ret < 0)
3347 dev_err(di->dev, "%s mask and set failed\n", __func__);
3348
efd71c89 3349 usb_unregister_notifier(di->usb_phy, &di->nb);
721002ec 3350 usb_put_phy(di->usb_phy);
84edbeea
AM
3351
3352 /* Delete the work queue */
3353 destroy_workqueue(di->charger_wq);
3354
8891716e
LJ
3355 /* Unregister external charger enable notifier */
3356 if (!di->ac_chg.enabled)
3357 blocking_notifier_chain_unregister(
3358 &charger_notifier_list, &charger_nb);
3359
84edbeea 3360 flush_scheduled_work();
72a90ddb 3361 if (di->usb_chg.enabled)
01ec8c54 3362 power_supply_unregister(&di->usb_chg.psy);
34c11a70 3363#if !defined(CONFIG_CHARGER_PM2301)
72a90ddb 3364 if (di->ac_chg.enabled)
01ec8c54 3365 power_supply_unregister(&di->ac_chg.psy);
34c11a70 3366#endif
84edbeea 3367 platform_set_drvdata(pdev, NULL);
84edbeea
AM
3368
3369 return 0;
3370}
3371
4aef72db
R
3372static char *supply_interface[] = {
3373 "ab8500_chargalg",
3374 "ab8500_fg",
3375 "ab8500_btemp",
3376};
3377
c8afa640 3378static int ab8500_charger_probe(struct platform_device *pdev)
84edbeea 3379{
4aef72db 3380 struct device_node *np = pdev->dev.of_node;
7722b799 3381 struct abx500_bm_data *plat = pdev->dev.platform_data;
2aac3de1 3382 struct ab8500_charger *di;
b269fff4 3383 int irq, i, charger_status, ret = 0, ch_stat;
84edbeea 3384
4aef72db
R
3385 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
3386 if (!di) {
3387 dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
84edbeea 3388 return -ENOMEM;
4aef72db 3389 }
7722b799
LJ
3390
3391 if (!plat) {
3392 dev_err(&pdev->dev, "no battery management data supplied\n");
3393 return -EINVAL;
3394 }
3395 di->bm = plat;
3396
3397 if (np) {
3398 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
3399 if (ret) {
3400 dev_err(&pdev->dev, "failed to get battery information\n");
3401 return ret;
4aef72db 3402 }
7722b799
LJ
3403 di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
3404 } else
4aef72db 3405 di->autopower_cfg = false;
84edbeea
AM
3406
3407 /* get parent data */
3408 di->dev = &pdev->dev;
3409 di->parent = dev_get_drvdata(pdev->dev.parent);
3410 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
3411
3412 /* initialize lock */
3413 spin_lock_init(&di->usb_state.usb_lock);
34c11a70 3414 mutex_init(&di->usb_ipt_crnt_lock);
84edbeea 3415
84edbeea 3416 di->autopower = false;
ff38090a 3417 di->invalid_charger_detect_state = 0;
84edbeea
AM
3418
3419 /* AC supply */
3420 /* power_supply base class */
3421 di->ac_chg.psy.name = "ab8500_ac";
3422 di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS;
3423 di->ac_chg.psy.properties = ab8500_charger_ac_props;
3424 di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props);
3425 di->ac_chg.psy.get_property = ab8500_charger_ac_get_property;
4aef72db
R
3426 di->ac_chg.psy.supplied_to = supply_interface;
3427 di->ac_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
84edbeea
AM
3428 /* ux500_charger sub-class */
3429 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
4dcdf577 3430 di->ac_chg.ops.check_enable = &ab8500_charger_ac_check_enable;
84edbeea
AM
3431 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3432 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3433 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
3434 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3435 di->ac_chg.max_out_curr = ab8500_charger_current_map[
3436 ARRAY_SIZE(ab8500_charger_current_map) - 1];
e07a5645 3437 di->ac_chg.wdt_refresh = CHG_WD_INTERVAL;
97034a1e 3438 di->ac_chg.enabled = di->bm->ac_enabled;
e07a5645 3439 di->ac_chg.external = false;
84edbeea 3440
8891716e
LJ
3441 /*notifier for external charger enabling*/
3442 if (!di->ac_chg.enabled)
3443 blocking_notifier_chain_register(
3444 &charger_notifier_list, &charger_nb);
3445
84edbeea
AM
3446 /* USB supply */
3447 /* power_supply base class */
3448 di->usb_chg.psy.name = "ab8500_usb";
3449 di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB;
3450 di->usb_chg.psy.properties = ab8500_charger_usb_props;
3451 di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props);
3452 di->usb_chg.psy.get_property = ab8500_charger_usb_get_property;
4aef72db
R
3453 di->usb_chg.psy.supplied_to = supply_interface;
3454 di->usb_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
84edbeea
AM
3455 /* ux500_charger sub-class */
3456 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
4dcdf577 3457 di->usb_chg.ops.check_enable = &ab8500_charger_usb_check_enable;
84edbeea
AM
3458 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
3459 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
3460 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
3461 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
3462 di->usb_chg.max_out_curr = ab8500_charger_current_map[
3463 ARRAY_SIZE(ab8500_charger_current_map) - 1];
e07a5645 3464 di->usb_chg.wdt_refresh = CHG_WD_INTERVAL;
97034a1e 3465 di->usb_chg.enabled = di->bm->usb_enabled;
e07a5645 3466 di->usb_chg.external = false;
f7470b5d 3467 di->usb_state.usb_current = -1;
84edbeea
AM
3468
3469 /* Create a work queue for the charger */
3470 di->charger_wq =
3471 create_singlethread_workqueue("ab8500_charger_wq");
3472 if (di->charger_wq == NULL) {
3473 dev_err(di->dev, "failed to create work queue\n");
4aef72db 3474 return -ENOMEM;
84edbeea
AM
3475 }
3476
b269fff4
LJ
3477 mutex_init(&di->charger_attached_mutex);
3478
84edbeea 3479 /* Init work for HW failure check */
203b42f7 3480 INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
84edbeea 3481 ab8500_charger_check_hw_failure_work);
203b42f7 3482 INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
84edbeea
AM
3483 ab8500_charger_check_usbchargernotok_work);
3484
b269fff4
LJ
3485 INIT_DELAYED_WORK(&di->ac_charger_attached_work,
3486 ab8500_charger_ac_attached_work);
3487 INIT_DELAYED_WORK(&di->usb_charger_attached_work,
3488 ab8500_charger_usb_attached_work);
3489
84edbeea
AM
3490 /*
3491 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
3492 * logic. That means we have to continously kick the charger
3493 * watchdog even when no charger is connected. This is only
3494 * valid once the AC charger has been enabled. This is
3495 * a bug that is not handled by the algorithm and the
3496 * watchdog have to be kicked by the charger driver
3497 * when the AC charger is disabled
3498 */
203b42f7 3499 INIT_DEFERRABLE_WORK(&di->kick_wd_work,
84edbeea
AM
3500 ab8500_charger_kick_watchdog_work);
3501
203b42f7 3502 INIT_DEFERRABLE_WORK(&di->check_vbat_work,
84edbeea
AM
3503 ab8500_charger_check_vbat_work);
3504
4b45f4a9
MC
3505 INIT_DELAYED_WORK(&di->attach_work,
3506 ab8500_charger_usb_link_attach_work);
3507
34c11a70
POH
3508 INIT_DELAYED_WORK(&di->usb_state_changed_work,
3509 ab8500_charger_usb_state_changed_work);
3510
3511 INIT_DELAYED_WORK(&di->vbus_drop_end_work,
3512 ab8500_charger_vbus_drop_end_work);
3513
84edbeea
AM
3514 /* Init work for charger detection */
3515 INIT_WORK(&di->usb_link_status_work,
3516 ab8500_charger_usb_link_status_work);
3517 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
3518 INIT_WORK(&di->detect_usb_type_work,
3519 ab8500_charger_detect_usb_type_work);
3520
84edbeea
AM
3521 /* Init work for checking HW status */
3522 INIT_WORK(&di->check_main_thermal_prot_work,
3523 ab8500_charger_check_main_thermal_prot_work);
3524 INIT_WORK(&di->check_usb_thermal_prot_work,
3525 ab8500_charger_check_usb_thermal_prot_work);
3526
3527 /*
3528 * VDD ADC supply needs to be enabled from this driver when there
3529 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
3530 * interrupts during charging
3531 */
8feffd10 3532 di->regu = devm_regulator_get(di->dev, "vddadc");
84edbeea
AM
3533 if (IS_ERR(di->regu)) {
3534 ret = PTR_ERR(di->regu);
3535 dev_err(di->dev, "failed to get vddadc regulator\n");
3536 goto free_charger_wq;
3537 }
3538
3539
3540 /* Initialize OVV, and other registers */
3541 ret = ab8500_charger_init_hw_registers(di);
3542 if (ret) {
3543 dev_err(di->dev, "failed to initialize ABB registers\n");
8feffd10 3544 goto free_charger_wq;
84edbeea
AM
3545 }
3546
3547 /* Register AC charger class */
72a90ddb 3548 if (di->ac_chg.enabled) {
01ec8c54
MJ
3549 ret = power_supply_register(di->dev, &di->ac_chg.psy);
3550 if (ret) {
3551 dev_err(di->dev, "failed to register AC charger\n");
3552 goto free_charger_wq;
3553 }
84edbeea
AM
3554 }
3555
3556 /* Register USB charger class */
72a90ddb 3557 if (di->usb_chg.enabled) {
01ec8c54
MJ
3558 ret = power_supply_register(di->dev, &di->usb_chg.psy);
3559 if (ret) {
3560 dev_err(di->dev, "failed to register USB charger\n");
3561 goto free_ac;
3562 }
84edbeea
AM
3563 }
3564
662dca54 3565 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
ded017ee 3566 if (IS_ERR_OR_NULL(di->usb_phy)) {
efd71c89 3567 dev_err(di->dev, "failed to get usb transceiver\n");
84edbeea
AM
3568 ret = -EINVAL;
3569 goto free_usb;
3570 }
3571 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
efd71c89 3572 ret = usb_register_notifier(di->usb_phy, &di->nb);
84edbeea 3573 if (ret) {
efd71c89
AV
3574 dev_err(di->dev, "failed to register usb notifier\n");
3575 goto put_usb_phy;
84edbeea
AM
3576 }
3577
3578 /* Identify the connected charger types during startup */
34c11a70 3579 charger_status = ab8500_charger_detect_chargers(di, true);
84edbeea
AM
3580 if (charger_status & AC_PW_CONN) {
3581 di->ac.charger_connected = 1;
3582 di->ac_conn = true;
3583 ab8500_power_supply_changed(di, &di->ac_chg.psy);
3584 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
3585 }
3586
3587 if (charger_status & USB_PW_CONN) {
84edbeea
AM
3588 di->vbus_detected = true;
3589 di->vbus_detected_start = true;
3590 queue_work(di->charger_wq,
3591 &di->detect_usb_type_work);
3592 }
3593
3594 /* Register interrupts */
3595 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3596 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3597 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
3598 IRQF_SHARED | IRQF_NO_SUSPEND,
3599 ab8500_charger_irq[i].name, di);
3600
3601 if (ret != 0) {
3602 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
3603 , ab8500_charger_irq[i].name, irq, ret);
3604 goto free_irq;
3605 }
3606 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
3607 ab8500_charger_irq[i].name, irq, ret);
3608 }
3609
3610 platform_set_drvdata(pdev, di);
3611
b269fff4
LJ
3612 mutex_lock(&di->charger_attached_mutex);
3613
34c11a70 3614 ch_stat = ab8500_charger_detect_chargers(di, false);
b269fff4
LJ
3615
3616 if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
3617 queue_delayed_work(di->charger_wq,
3618 &di->ac_charger_attached_work,
3619 HZ);
3620 }
3621 if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
3622 queue_delayed_work(di->charger_wq,
3623 &di->usb_charger_attached_work,
3624 HZ);
3625 }
3626
3627 mutex_unlock(&di->charger_attached_mutex);
3628
84edbeea
AM
3629 return ret;
3630
3631free_irq:
efd71c89 3632 usb_unregister_notifier(di->usb_phy, &di->nb);
84edbeea
AM
3633
3634 /* We also have to free all successfully registered irqs */
3635 for (i = i - 1; i >= 0; i--) {
3636 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3637 free_irq(irq, di);
3638 }
efd71c89 3639put_usb_phy:
721002ec 3640 usb_put_phy(di->usb_phy);
84edbeea 3641free_usb:
72a90ddb 3642 if (di->usb_chg.enabled)
01ec8c54 3643 power_supply_unregister(&di->usb_chg.psy);
84edbeea 3644free_ac:
72a90ddb 3645 if (di->ac_chg.enabled)
01ec8c54 3646 power_supply_unregister(&di->ac_chg.psy);
84edbeea
AM
3647free_charger_wq:
3648 destroy_workqueue(di->charger_wq);
84edbeea
AM
3649 return ret;
3650}
3651
4aef72db
R
3652static const struct of_device_id ab8500_charger_match[] = {
3653 { .compatible = "stericsson,ab8500-charger", },
3654 { },
3655};
3656
84edbeea
AM
3657static struct platform_driver ab8500_charger_driver = {
3658 .probe = ab8500_charger_probe,
28ea73f4 3659 .remove = ab8500_charger_remove,
84edbeea
AM
3660 .suspend = ab8500_charger_suspend,
3661 .resume = ab8500_charger_resume,
3662 .driver = {
3663 .name = "ab8500-charger",
3664 .owner = THIS_MODULE,
4aef72db 3665 .of_match_table = ab8500_charger_match,
84edbeea
AM
3666 },
3667};
3668
3669static int __init ab8500_charger_init(void)
3670{
3671 return platform_driver_register(&ab8500_charger_driver);
3672}
3673
3674static void __exit ab8500_charger_exit(void)
3675{
3676 platform_driver_unregister(&ab8500_charger_driver);
3677}
3678
3679subsys_initcall_sync(ab8500_charger_init);
3680module_exit(ab8500_charger_exit);
3681
3682MODULE_LICENSE("GPL v2");
3683MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
3684MODULE_ALIAS("platform:ab8500-charger");
3685MODULE_DESCRIPTION("AB8500 charger management driver");