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