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