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