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