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