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