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