rk30:phone loquat:modify the battery capacity reported
[firefly-linux-kernel-4.4.55.git] / drivers / power / twl6030_bci_battery.c
1 /*
2  * linux/drivers/power/twl6030_bci_battery.c
3  *
4  * OMAP4:TWL6030 battery driver for Linux
5  *
6  * Copyright (C) 2008-2009 Texas Instruments, Inc.
7  * Author: Texas Instruments, Inc.
8  *
9  * This package is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  *
13  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/device.h>
21 #include <linux/interrupt.h>
22 #include <linux/delay.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
25 #include <linux/i2c/twl.h>
26 #include <linux/power_supply.h>
27 #include <linux/i2c/twl6030-gpadc.h>
28 #include <linux/i2c/bq2415x.h>
29 #include <linux/wakelock.h>
30 #include <linux/usb/otg.h>
31
32 #include <mach/gpio.h>
33
34 #define CONTROLLER_INT_MASK     0x00
35 #define CONTROLLER_CTRL1        0x01
36 #define CONTROLLER_WDG          0x02
37 #define CONTROLLER_STAT1        0x03
38 #define CHARGERUSB_INT_STATUS   0x04
39 #define CHARGERUSB_INT_MASK     0x05
40 #define CHARGERUSB_STATUS_INT1  0x06
41 #define CHARGERUSB_STATUS_INT2  0x07
42 #define CHARGERUSB_CTRL1        0x08
43 #define CHARGERUSB_CTRL2        0x09
44 #define CHARGERUSB_CTRL3        0x0A
45 #define CHARGERUSB_STAT1        0x0B
46 #define CHARGERUSB_VOREG        0x0C
47 #define CHARGERUSB_VICHRG       0x0D
48 #define CHARGERUSB_CINLIMIT     0x0E
49 #define CHARGERUSB_CTRLLIMIT1   0x0F
50 #define CHARGERUSB_CTRLLIMIT2   0x10
51 #define ANTICOLLAPSE_CTRL1      0x11
52 #define ANTICOLLAPSE_CTRL2      0x12
53
54 /* TWL6032 registers 0xDA to 0xDE - TWL6032_MODULE_CHARGER */
55 #define CONTROLLER_CTRL2        0x00
56 #define CONTROLLER_VSEL_COMP    0x01
57 #define CHARGERUSB_VSYSREG      0x02
58 #define CHARGERUSB_VICHRG_PC    0x03
59 #define LINEAR_CHRG_STS         0x04
60
61 /* TWL6032 Charger Mode Register */
62 #define CHARGER_MODE_REG        0xD4
63 #define CHARGER_MODE_POWERPATH  BIT(3)
64 #define CHARGER_MODE_AUTOCHARGE BIT(6)
65
66 #define LINEAR_CHRG_STS_CRYSTL_OSC_OK   0x40
67 #define LINEAR_CHRG_STS_END_OF_CHARGE   0x20
68 #define LINEAR_CHRG_STS_VBATOV          0x10
69 #define LINEAR_CHRG_STS_VSYSOV          0x08
70 #define LINEAR_CHRG_STS_DPPM_STS        0x04
71 #define LINEAR_CHRG_STS_CV_STS          0x02
72 #define LINEAR_CHRG_STS_CC_STS          0x01
73
74 #define FG_REG_00       0x00
75 #define FG_REG_01       0x01
76 #define FG_REG_02       0x02
77 #define FG_REG_03       0x03
78 #define FG_REG_04       0x04
79 #define FG_REG_05       0x05
80 #define FG_REG_06       0x06
81 #define FG_REG_07       0x07
82 #define FG_REG_08       0x08
83 #define FG_REG_09       0x09
84 #define FG_REG_10       0x0A
85 #define FG_REG_11       0x0B
86
87 /* CONTROLLER_INT_MASK */
88 #define MVAC_FAULT              (1 << 7)
89 #define MAC_EOC                 (1 << 6)
90 #define LINCH_GATED             (1 << 5)
91 #define MBAT_REMOVED            (1 << 4)
92 #define MFAULT_WDG              (1 << 3)
93 #define MBAT_TEMP               (1 << 2)
94 #define MVBUS_DET               (1 << 1)
95 #define MVAC_DET                (1 << 0)
96
97 /* CONTROLLER_CTRL1 */
98 #define CONTROLLER_CTRL1_EN_LINCH       (1 << 5)
99 #define CONTROLLER_CTRL1_EN_CHARGER     (1 << 4)
100 #define CONTROLLER_CTRL1_SEL_CHARGER    (1 << 3)
101
102 /* CONTROLLER_STAT1 */
103 #define CONTROLLER_STAT1_EXTCHRG_STATZ  (1 << 7)
104 #define CONTROLLER_STAT1_LINCH_GATED    (1 << 6)
105 #define CONTROLLER_STAT1_CHRG_DET_N     (1 << 5)
106 #define CONTROLLER_STAT1_FAULT_WDG      (1 << 4)
107 #define CONTROLLER_STAT1_VAC_DET        (1 << 3)
108 #define VAC_DET (1 << 3)
109 #define CONTROLLER_STAT1_VBUS_DET       (1 << 2)
110 #define VBUS_DET        (1 << 2)
111 #define CONTROLLER_STAT1_BAT_REMOVED    (1 << 1)
112 #define CONTROLLER_STAT1_BAT_TEMP_OVRANGE (1 << 0)
113
114 /* CHARGERUSB_INT_STATUS */
115 #define EN_LINCH                (1 << 4)
116 #define CURRENT_TERM_INT        (1 << 3)
117 #define CHARGERUSB_STAT         (1 << 2)
118 #define CHARGERUSB_THMREG       (1 << 1)
119 #define CHARGERUSB_FAULT        (1 << 0)
120
121 /* CHARGERUSB_INT_MASK */
122 #define MASK_MCURRENT_TERM              (1 << 3)
123 #define MASK_MCHARGERUSB_STAT           (1 << 2)
124 #define MASK_MCHARGERUSB_THMREG         (1 << 1)
125 #define MASK_MCHARGERUSB_FAULT          (1 << 0)
126
127 /* CHARGERUSB_STATUS_INT1 */
128 #define CHARGERUSB_STATUS_INT1_TMREG    (1 << 7)
129 #define CHARGERUSB_STATUS_INT1_NO_BAT   (1 << 6)
130 #define CHARGERUSB_STATUS_INT1_BST_OCP  (1 << 5)
131 #define CHARGERUSB_STATUS_INT1_TH_SHUTD (1 << 4)
132 #define CHARGERUSB_STATUS_INT1_BAT_OVP  (1 << 3)
133 #define CHARGERUSB_STATUS_INT1_POOR_SRC (1 << 2)
134 #define CHARGERUSB_STATUS_INT1_SLP_MODE (1 << 1)
135 #define CHARGERUSB_STATUS_INT1_VBUS_OVP (1 << 0)
136
137 /* CHARGERUSB_STATUS_INT2 */
138 #define ICCLOOP         (1 << 3)
139 #define CURRENT_TERM    (1 << 2)
140 #define CHARGE_DONE     (1 << 1)
141 #define ANTICOLLAPSE    (1 << 0)
142
143 /* CHARGERUSB_CTRL1 */
144 #define SUSPEND_BOOT    (1 << 7)
145 #define OPA_MODE        (1 << 6)
146 #define HZ_MODE         (1 << 5)
147 #define TERM            (1 << 4)
148
149 /* CHARGERUSB_CTRL2 */
150 #define CHARGERUSB_CTRL2_VITERM_50      (0 << 5)
151 #define CHARGERUSB_CTRL2_VITERM_100     (1 << 5)
152 #define CHARGERUSB_CTRL2_VITERM_150     (2 << 5)
153 #define CHARGERUSB_CTRL2_VITERM_400     (7 << 5)
154
155 /* CHARGERUSB_CTRL3 */
156 #define VBUSCHRG_LDO_OVRD       (1 << 7)
157 #define CHARGE_ONCE             (1 << 6)
158 #define BST_HW_PR_DIS           (1 << 5)
159 #define AUTOSUPPLY              (1 << 3)
160 #define BUCK_HSILIM             (1 << 0)
161
162 /* CHARGERUSB_VOREG */
163 #define CHARGERUSB_VOREG_3P52           0x01
164 #define CHARGERUSB_VOREG_4P0            0x19
165 #define CHARGERUSB_VOREG_4P2            0x23
166 #define CHARGERUSB_VOREG_4P76           0x3F
167
168 /* CHARGERUSB_VICHRG */
169 #define CHARGERUSB_VICHRG_300           0x0
170 #define CHARGERUSB_VICHRG_500           0x4
171 #define CHARGERUSB_VICHRG_1500          0xE
172
173 /* CHARGERUSB_CINLIMIT */
174 #define CHARGERUSB_CIN_LIMIT_100        0x1
175 #define CHARGERUSB_CIN_LIMIT_300        0x5
176 #define CHARGERUSB_CIN_LIMIT_500        0x9
177 #define CHARGERUSB_CIN_LIMIT_NONE       0xF
178
179 /* CHARGERUSB_CTRLLIMIT1 */
180 #define VOREGL_4P16                     0x21
181 #define VOREGL_4P56                     0x35
182
183 /* CHARGERUSB_CTRLLIMIT2 */
184 #define CHARGERUSB_CTRLLIMIT2_1500      0x0E
185 #define         LOCK_LIMIT              (1 << 4)
186
187 /* ANTICOLLAPSE_CTRL2 */
188 #define BUCK_VTH_SHIFT                  5
189
190 /* FG_REG_00 */
191 #define CC_ACTIVE_MODE_SHIFT    6
192 #define CC_AUTOCLEAR            (1 << 2)
193 #define CC_CAL_EN               (1 << 1)
194 #define CC_PAUSE                (1 << 0)
195
196 #define REG_TOGGLE1             0x90
197 #define FGDITHS                 (1 << 7)
198 #define FGDITHR                 (1 << 6)
199 #define FGS                     (1 << 5)
200 #define FGR                     (1 << 4)
201
202 /* TWL6030_GPADC_CTRL */
203 #define GPADC_CTRL_TEMP1_EN     (1 << 0)    /* input ch 1 */
204 #define GPADC_CTRL_TEMP2_EN     (1 << 1)    /* input ch 4 */
205 #define GPADC_CTRL_SCALER_EN    (1 << 2)    /* input ch 2 */
206 #define GPADC_CTRL_SCALER_DIV4  (1 << 3)
207 #define GPADC_CTRL_SCALER_EN_CH11       (1 << 4)    /* input ch 11 */
208 #define GPADC_CTRL_TEMP1_EN_MONITOR     (1 << 5)
209 #define GPADC_CTRL_TEMP2_EN_MONITOR     (1 << 6)
210 #define GPADC_CTRL_ISOURCE_EN           (1 << 7)
211
212 #define GPADC_ISOURCE_22uA              22
213 #define GPADC_ISOURCE_7uA               7
214
215 /* TWL6030/6032 BATTERY VOLTAGE GPADC CHANNELS */
216
217 #define TWL6030_GPADC_VBAT_CHNL 0x07
218 #define TWL6032_GPADC_VBAT_CHNL 0x12
219
220 /* TWL6030_GPADC_CTRL2 */
221 #define GPADC_CTRL2_CH18_SCALER_EN      BIT(2)
222
223 #define ENABLE_ISOURCE          0x80
224
225 #define REG_MISC1               0xE4
226 #define VAC_MEAS                0x04
227 #define VBAT_MEAS               0x02
228 #define BB_MEAS                 0x01
229
230 #define REG_USB_VBUS_CTRL_SET   0x04
231 #define VBUS_MEAS               0x01
232 #define REG_USB_ID_CTRL_SET     0x06
233 #define ID_MEAS                 0x01
234
235 #define BBSPOR_CFG              0xE6
236 #define BB_CHG_EN               (1 << 3)
237
238 #define STS_HW_CONDITIONS       0x21
239 #define STS_USB_ID              (1 << 2)        /* Level status of USB ID */
240
241 #define BATTERY_RESISTOR        10000
242 #define SIMULATOR_RESISTOR      5000
243 #define BATTERY_DETECT_THRESHOLD        ((BATTERY_RESISTOR + SIMULATOR_RESISTOR) / 2)   //battery voltage threshold divided by 22uA
244 #define CHARGING_CAPACITY_UPDATE_PERIOD (1000 * 60 * 1)
245
246 /* To get VBUS input limit from twl6030_usb */
247 #if CONFIG_TWL6030_USB
248 extern unsigned int twl6030_get_usb_max_power(struct otg_transceiver *x);
249 #else
250 static inline unsigned int twl6030_get_usb_max_power(struct otg_transceiver *x)
251 {
252         return 0;
253 };
254 #endif
255
256 /* Ptr to thermistor table */
257 static const unsigned int fuelgauge_rate[4] = {1, 4, 16, 64};
258 static struct wake_lock chrg_lock;
259
260
261 struct twl6030_bci_device_info {
262         struct device           *dev;
263
264         int                     voltage_mV;
265         int                     bk_voltage_mV;
266         int                     current_uA;
267         int                     current_avg_uA;
268         int                     temp_C;
269         int                     charge_status;
270         int                     vac_priority;
271         int                     bat_health;
272         int                     charger_source;
273
274         int                     fuelgauge_mode;
275         int                     timer_n2;
276         int                     timer_n1;
277         s32                     charge_n1;
278         s32                     charge_n2;
279         s16                     cc_offset;
280         u8                      usb_online;
281         u8                      ac_online;
282         u8                      stat1;
283         u8                      linear_stat;
284         u8                      status_int1;
285         u8                      status_int2;
286
287         u8                      gpadc_vbat_chnl;
288         u8                      watchdog_duration;
289         u16                     current_avg_interval;
290         u16                     monitoring_interval;
291         unsigned int            min_vbus;
292
293         struct                  twl4030_bci_platform_data *platform_data;
294
295         unsigned int            charger_incurrentmA;
296         unsigned int            charger_outcurrentmA;
297         unsigned long           usb_max_power;
298         unsigned long           event;
299
300         unsigned int            capacity;
301         unsigned int            capacity_debounce_count;
302         unsigned long           ac_next_refresh;
303         unsigned int            prev_capacity;
304         unsigned int            wakelock_enabled;
305
306         struct power_supply     bat;
307         struct power_supply     usb;
308         struct power_supply     ac;
309         struct power_supply     bk_bat;
310
311         struct otg_transceiver  *otg;
312         struct notifier_block   nb;
313         struct work_struct      usb_work;
314
315         struct workqueue_struct *freezable_work;
316
317         struct delayed_work     twl6030_bci_monitor_work;
318         struct delayed_work     twl6030_current_avg_work;
319
320         unsigned long           features;
321
322         int                     use_hw_charger;
323         int                     use_power_path;
324
325         /* max scale current based on sense resitor */
326         int                     current_max_scale;
327         struct delayed_work work;
328         unsigned int interval;
329 };
330
331 static BLOCKING_NOTIFIER_HEAD(notifier_list);
332 extern u32 wakeup_timer_seconds;
333
334 static void twl6030_config_min_vbus_reg(struct twl6030_bci_device_info *di,
335                                                 unsigned int value)
336 {
337         u8 rd_reg = 0;
338         int ret;
339
340         /* not required on TWL6032 */
341         if (di->features & TWL6032_SUBCLASS)
342                 return;
343
344         if (value > 4760 || value < 4200) {
345                 dev_err(di->dev, "invalid min vbus\n");
346                 return;
347         }
348
349         ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &rd_reg,
350                                         ANTICOLLAPSE_CTRL2);
351         if (ret)
352                 goto err;
353         rd_reg = rd_reg & 0x1F;
354         rd_reg = rd_reg | (((value - 4200)/80) << BUCK_VTH_SHIFT);
355         ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, rd_reg,
356                                         ANTICOLLAPSE_CTRL2);
357
358         if (!ret)
359                 return;
360 err:
361         pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
362 }
363
364 static void twl6030_config_iterm_reg(struct twl6030_bci_device_info *di,
365                                                 unsigned int term_currentmA)
366 {
367         int ret;
368
369         if ((term_currentmA > 400) || (term_currentmA < 50)) {
370                 dev_err(di->dev, "invalid termination current\n");
371                 return;
372         }
373
374         term_currentmA = ((term_currentmA - 50)/50) << 5;
375         ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, term_currentmA,
376                                                 CHARGERUSB_CTRL2);
377         if (ret)
378                 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
379 }
380
381 static unsigned int twl6030_get_iterm_reg(struct twl6030_bci_device_info *di)
382 {
383         int ret;
384         unsigned int currentmA;
385         u8 val = 0;
386
387         ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &val, CHARGERUSB_CTRL2);
388         if (ret) {
389                 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
390                 currentmA = 0;
391         } else
392                 currentmA = 50 + (val >> 5) * 50;
393
394         return currentmA;
395 }
396
397 static void twl6030_config_voreg_reg(struct twl6030_bci_device_info *di,
398                                                         unsigned int voltagemV)
399 {
400         int ret;
401
402         if ((voltagemV < 3500) || (voltagemV > 4760)) {
403                 dev_err(di->dev, "invalid charger_voltagemV\n");
404                 return;
405         }
406
407         voltagemV = (voltagemV - 3500) / 20;
408         ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, voltagemV,
409                                                 CHARGERUSB_VOREG);
410         if (ret)
411                 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
412 }
413
414 static unsigned int twl6030_get_voreg_reg(struct twl6030_bci_device_info *di)
415 {
416         int ret;
417         unsigned int voltagemV;
418         u8 val = 0;
419
420         ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &val, CHARGERUSB_VOREG);
421         if (ret) {
422                 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
423                 voltagemV = 0;
424         } else
425                 voltagemV = 3500 + (val * 20);
426
427         return voltagemV;
428 }
429
430 static void twl6030_config_vichrg_reg(struct twl6030_bci_device_info *di,
431                                                         unsigned int currentmA)
432 {
433         int ret;
434
435         if ((currentmA >= 300) && (currentmA <= 450))
436                 currentmA = (currentmA - 300) / 50;
437         else if ((currentmA >= 500) && (currentmA <= 1500))
438                 currentmA = (currentmA - 500) / 100 + 4;
439         else {
440                 dev_err(di->dev, "invalid charger_currentmA\n");
441                 return;
442         }
443
444         ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, currentmA,
445                                                 CHARGERUSB_VICHRG);
446         if (ret)
447                 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
448 }
449
450 static void twl6030_config_cinlimit_reg(struct twl6030_bci_device_info *di,
451                                                         unsigned int currentmA)
452 {
453         int ret;
454
455         if ((currentmA >= 50) && (currentmA <= 750))
456                 currentmA = (currentmA - 50) / 50;
457         else if ((currentmA > 750) && (currentmA <= 1500) &&
458                         (di->features & TWL6032_SUBCLASS)) {
459                         currentmA = ((currentmA % 100) ? 0x30 : 0x20) +
460                                                 ((currentmA - 100) / 100);
461         } else if (currentmA < 50) {
462                 dev_err(di->dev, "invalid input current limit\n");
463                 return;
464         } else {
465                 /* This is no current limit */
466                 currentmA = 0x0F;
467         }
468
469         ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, currentmA,
470                                         CHARGERUSB_CINLIMIT);
471         if (ret)
472                 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
473 }
474
475 static void twl6030_config_limit1_reg(struct twl6030_bci_device_info *di,
476                                                         unsigned int voltagemV)
477 {
478         int ret;
479
480         if ((voltagemV < 3500) || (voltagemV > 4760)) {
481                 dev_err(di->dev, "invalid max_charger_voltagemV\n");
482                 return;
483         }
484
485         voltagemV = (voltagemV - 3500) / 20;
486         ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, voltagemV,
487                                                 CHARGERUSB_CTRLLIMIT1);
488         if (ret)
489                 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
490 }
491
492 static unsigned int twl6030_get_limit1_reg(struct twl6030_bci_device_info *di)
493 {
494         int ret;
495         unsigned int voltagemV;
496         u8 val = 0;
497
498         ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &val,
499                                 CHARGERUSB_CTRLLIMIT1);
500         if (ret) {
501                 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
502                 voltagemV = 0;
503         } else
504                 voltagemV = 3500 + (val * 20);
505
506         return voltagemV;
507 }
508
509 static void twl6030_config_limit2_reg(struct twl6030_bci_device_info *di,
510                                                         unsigned int currentmA)
511 {
512         int ret;
513
514         if ((currentmA >= 300) && (currentmA <= 450))
515                 currentmA = (currentmA - 300) / 50;
516         else if ((currentmA >= 500) && (currentmA <= 1500))
517                 currentmA = (currentmA - 500) / 100 + 4;
518         else {
519                 dev_err(di->dev, "invalid max_charger_currentmA\n");
520                 return;
521         }
522
523         currentmA |= LOCK_LIMIT;
524         ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, currentmA,
525                                                 CHARGERUSB_CTRLLIMIT2);
526         if (ret)
527                 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
528 }
529
530 static const int vichrg[] = {
531         300, 350, 400, 450, 500, 600, 700, 800,
532         900, 1000, 1100, 1200, 1300, 1400, 1500, 300
533 };
534
535 static unsigned int twl6030_get_limit2_reg(struct twl6030_bci_device_info *di)
536 {
537         int ret;
538         unsigned int currentmA;
539         u8 val = 0;
540
541         ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &val,
542                                         CHARGERUSB_CTRLLIMIT2);
543         if (ret) {
544                 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
545                 currentmA = 0;
546         } else
547                 currentmA = vichrg[val & 0xF];
548
549         return currentmA;
550 }
551
552 /*
553  * Return channel value
554  * Or < 0 on failure.
555  */
556 static int twl6030_get_gpadc_conversion(struct twl6030_bci_device_info *di,
557                                         int channel_no)
558 {
559         struct twl6030_gpadc_request req;
560         int temp = 0;
561         int ret;
562
563         req.channels = (1 << channel_no);
564         req.method = TWL6030_GPADC_SW2;
565         req.active = 0;
566         req.func_cb = NULL;
567         ret = twl6030_gpadc_conversion(&req);
568         if (ret < 0)
569                 return ret;
570
571         if (req.rbuf[channel_no] > 0)
572                 temp = req.rbuf[channel_no];
573
574         return temp;
575 }
576
577 static int is_battery_present(struct twl6030_bci_device_info *di)
578 {
579         int val;
580         static unsigned int current_src_val;
581
582         /*
583          * Prevent charging on batteries were id resistor is
584          * less than 5K.
585          */
586         val = twl6030_get_gpadc_conversion(di,di->gpadc_vbat_chnl);
587
588         /*
589          * twl6030_get_gpadc_conversion for
590          * 6030 return resistance, for 6032 - voltage and
591          * it should be converted to resistance before
592          * using.
593          */
594         if (!current_src_val) {
595                 u8 reg = 0;
596
597                 if (twl_i2c_read_u8(TWL_MODULE_MADC, &reg,
598                                         TWL6030_GPADC_CTRL))
599                         pr_err("%s: Error reading TWL6030_GPADC_CTRL\n",
600                                 __func__);
601
602                 current_src_val = (reg & GPADC_CTRL_ISOURCE_EN) ?
603                                         GPADC_ISOURCE_22uA :
604                                         GPADC_ISOURCE_7uA;
605         }
606
607         val = (val * 1000) / current_src_val;
608
609         if (val < BATTERY_DETECT_THRESHOLD)
610                 return 0;
611
612         return 1;
613 }
614
615 static void twl6030_stop_usb_charger(struct twl6030_bci_device_info *di)
616 {
617         int ret;
618         u8 reg;
619
620         if (di->use_hw_charger) {
621                 ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &reg,
622                                 CHARGERUSB_CTRL1);
623                 if (ret)
624                         goto err;
625                 reg |= HZ_MODE;
626                 ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, reg,
627                                 CHARGERUSB_CTRL1);
628                 if (ret)
629                         goto err;
630
631                 return;
632         }
633
634         di->charger_source = 0;
635         di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
636
637         ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, 0, CONTROLLER_CTRL1);
638
639 err:
640         if (ret)
641                 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
642 }
643
644 static void twl6030_start_usb_charger(struct twl6030_bci_device_info *di)
645 {
646         int ret;
647         u8 reg;
648
649         if (di->use_hw_charger) {
650                 ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &reg,
651                                 CHARGERUSB_CTRL1);
652                 if (ret)
653                         goto err;
654
655                 reg &= ~HZ_MODE;
656                 ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, reg,
657                                 CHARGERUSB_CTRL1);
658                 if (ret)
659                         goto err;
660
661                 return;
662         }
663
664         if (!is_battery_present(di)) {
665                 dev_info(di->dev, "BATTERY NOT DETECTED!\n");
666                 return;
667         }
668
669         if (di->charger_source == POWER_SUPPLY_TYPE_MAINS)
670                 return;
671
672         dev_dbg(di->dev, "USB input current limit %dmA\n",
673                                         di->charger_incurrentmA);
674         if (di->charger_incurrentmA < 50) {
675                 ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER,
676                                         0, CONTROLLER_CTRL1);
677                 if (ret)
678                         goto err;
679
680                 return;
681         }
682
683         twl6030_config_vichrg_reg(di, di->charger_outcurrentmA);
684         twl6030_config_cinlimit_reg(di, di->charger_incurrentmA);
685         twl6030_config_voreg_reg(di, di->platform_data->max_bat_voltagemV);
686         twl6030_config_iterm_reg(di, di->platform_data->termination_currentmA);
687
688         if (di->charger_incurrentmA >= 50) {
689                 reg = CONTROLLER_CTRL1_EN_CHARGER;
690
691                 if (di->use_power_path)
692                         reg |= CONTROLLER_CTRL1_EN_LINCH;
693
694                 ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, reg,
695                                 CONTROLLER_CTRL1);
696                 if (ret)
697                         goto err;
698
699                 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
700         }
701         return;
702 err:
703         pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
704 }
705
706 static void twl6030_stop_ac_charger(struct twl6030_bci_device_info *di)
707 {
708         long int events;
709         int ret;
710
711         di->charger_source = 0;
712         di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
713         events = BQ2415x_STOP_CHARGING;
714
715         if (di->use_hw_charger)
716                 return;
717
718         blocking_notifier_call_chain(&notifier_list, events, NULL);
719
720         ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, 0, CONTROLLER_CTRL1);
721         if (ret)
722                 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
723
724         if (di->wakelock_enabled)
725                 wake_unlock(&chrg_lock);
726 }
727
728 static void twl6030_start_ac_charger(struct twl6030_bci_device_info *di)
729 {
730         long int events;
731         int ret;
732
733         if (!is_battery_present(di)) {
734                 dev_info(di->dev, "BATTERY NOT DETECTED!\n");
735                 return;
736         }
737         dev_dbg(di->dev, "AC charger detected\n");
738         di->charger_source = POWER_SUPPLY_TYPE_MAINS;
739         di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
740         events = BQ2415x_START_CHARGING;
741
742         if (di->use_hw_charger)
743                 return;
744
745         blocking_notifier_call_chain(&notifier_list, events, NULL);
746
747         ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER,
748                         CONTROLLER_CTRL1_EN_CHARGER |
749                         CONTROLLER_CTRL1_SEL_CHARGER,
750                         CONTROLLER_CTRL1);
751         if (ret)
752                 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
753
754         if (di->wakelock_enabled)
755                 wake_lock(&chrg_lock);
756 }
757
758 static void twl6030_stop_charger(struct twl6030_bci_device_info *di)
759 {
760         if (di->charger_source == POWER_SUPPLY_TYPE_MAINS)
761                 twl6030_stop_ac_charger(di);
762         else if (di->charger_source == POWER_SUPPLY_TYPE_USB)
763                 twl6030_stop_usb_charger(di);
764 }
765
766 static void twl6032_charger_ctrl_interrupt(struct twl6030_bci_device_info *di)
767 {
768         u8 stat_toggle, stat_reset, stat_set = 0;
769         u8 present_state = 0, linear_state;
770         u8 present_status = 0;
771         int err;
772
773         err = twl_i2c_read_u8(TWL6032_MODULE_CHARGER, &present_state,
774                         LINEAR_CHRG_STS);
775         if (err < 0) {
776                 dev_err(di->dev, "%s: Error access to TWL6030 (%d)\n",
777                                                                 __func__, err);
778                 return;
779         }
780
781         err = twl_i2c_read_u8(TWL6032_MODULE_CHARGER, &present_status,
782                         CHARGERUSB_INT_STATUS);
783         if (err < 0) {
784                 dev_err(di->dev, "%s: Error access to TWL6030 (%d)\n",
785                                                                 __func__, err);
786                 return;
787         }
788
789         linear_state = di->linear_stat;
790
791         stat_toggle = linear_state ^ present_state;
792         stat_set = stat_toggle & present_state;
793         stat_reset = stat_toggle & linear_state;
794         di->linear_stat = present_state;
795
796         if (stat_set & LINEAR_CHRG_STS_CRYSTL_OSC_OK)
797                 dev_dbg(di->dev, "Linear status: CRYSTAL OSC OK\n");
798         if (present_state & LINEAR_CHRG_STS_END_OF_CHARGE) {
799                 dev_dbg(di->dev, "Linear status: END OF CHARGE\n");
800                 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
801         }
802         if (present_status & EN_LINCH) {
803                 dev_dbg(di->dev, "Linear status: START OF CHARGE\n");
804                 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
805         }
806
807         if (stat_set & LINEAR_CHRG_STS_VBATOV) {
808                 dev_dbg(di->dev, "Linear Status: VBATOV\n");
809                 di->bat_health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
810         }
811         if (stat_reset & LINEAR_CHRG_STS_VBATOV) {
812                 dev_dbg(di->dev, "Linear Status: VBATOV\n");
813                 di->bat_health = POWER_SUPPLY_HEALTH_GOOD;
814         }
815
816         if (stat_set & LINEAR_CHRG_STS_VSYSOV)
817                 dev_dbg(di->dev, "Linear Status: VSYSOV\n");
818         if (stat_set & LINEAR_CHRG_STS_DPPM_STS)
819                 dev_dbg(di->dev, "Linear Status: DPPM STS\n");
820         if (stat_set & LINEAR_CHRG_STS_CV_STS)
821                 dev_dbg(di->dev, "Linear Status: CV STS\n");
822         if (stat_set & LINEAR_CHRG_STS_CC_STS)
823                 dev_dbg(di->dev, "Linear Status: CC STS\n");
824 }
825
826 /*
827  * Interrupt service routine
828  *
829  * Attends to TWL 6030 power module interruptions events, specifically
830  * USB_PRES (USB charger presence) CHG_PRES (AC charger presence) events
831  *
832  */
833 static irqreturn_t twl6030charger_ctrl_interrupt(int irq, void *_di)
834 {
835         struct twl6030_bci_device_info *di = _di;
836         int ret;
837         int charger_fault = 0;
838         long int events;
839         u8 stat_toggle, stat_reset, stat_set = 0;
840         u8 charge_state = 0;
841         u8 present_charge_state = 0;
842         u8 ac_or_vbus, no_ac_and_vbus = 0;
843         u8 hw_state = 0, temp = 0;
844
845         /* read charger controller_stat1 */
846         ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &present_charge_state,
847                 CONTROLLER_STAT1);
848         if (ret) {
849                 /*
850                  * Since present state read failed, charger_state is no
851                  * longer valid, reset to zero inorder to detect next events
852                  */
853                 charge_state = 0;
854                 return IRQ_NONE;
855         }
856
857         ret = twl_i2c_read_u8(TWL6030_MODULE_ID0, &hw_state, STS_HW_CONDITIONS);
858         if (ret)
859                 goto err;
860
861         charge_state = di->stat1;
862
863         stat_toggle = charge_state ^ present_charge_state;
864         stat_set = stat_toggle & present_charge_state;
865         stat_reset = stat_toggle & charge_state;
866
867         no_ac_and_vbus = !((present_charge_state) & (VBUS_DET | VAC_DET));
868         ac_or_vbus = charge_state & (VBUS_DET | VAC_DET);
869         if (no_ac_and_vbus && ac_or_vbus) {
870                 di->charger_source = 0;
871                 dev_dbg(di->dev, "No Charging source\n");
872                 /* disable charging when no source present */
873         }
874
875         charge_state = present_charge_state;
876         di->stat1 = present_charge_state;
877         if ((charge_state & VAC_DET) &&
878                 (charge_state & CONTROLLER_STAT1_EXTCHRG_STATZ)) {
879                 events = BQ2415x_CHARGER_FAULT;
880                 blocking_notifier_call_chain(&notifier_list, events, NULL);
881         }
882
883         if (stat_reset & VBUS_DET) {
884                 /* On a USB detach, UNMASK VBUS OVP if masked*/
885                 ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &temp,
886                                 CHARGERUSB_INT_MASK);
887                 if (ret)
888                         goto err;
889
890                 if (temp & MASK_MCHARGERUSB_FAULT) {
891                         ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER,
892                                         (temp & ~MASK_MCHARGERUSB_FAULT),
893                                         CHARGERUSB_INT_MASK);
894                         if (ret)
895                                 goto err;
896                 }
897                 di->usb_online = 0;
898                 dev_info(di->dev, "usb removed\n");
899                 twl6030_stop_usb_charger(di);
900                 if (present_charge_state & VAC_DET)
901                         twl6030_start_ac_charger(di);
902
903         }
904
905         if (stat_set & VBUS_DET) {
906                 /* In HOST mode (ID GROUND) when a device is connected,
907                  * Mask VBUS OVP interrupt and do no enable usb
908                  * charging
909                  */
910                 if (hw_state & STS_USB_ID) {
911                         ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER,
912                                         &temp,
913                                         CHARGERUSB_INT_MASK);
914                         if (ret)
915                                 goto err;
916
917                         if (!(temp & MASK_MCHARGERUSB_FAULT)) {
918                                 ret = twl_i2c_write_u8(
919                                                 TWL6030_MODULE_CHARGER,
920                                                 (temp | MASK_MCHARGERUSB_FAULT),
921                                                 CHARGERUSB_INT_MASK);
922                                 if (ret)
923                                         goto err;
924                         }
925                 } else {
926                         di->usb_online = POWER_SUPPLY_TYPE_USB;
927                         if ((present_charge_state & VAC_DET) &&
928                                         (di->vac_priority == 2))
929                                 dev_info(di->dev, "USB charger detected"
930                                                 ", continue with VAC\n");
931                         else {
932                                 di->charger_source =
933                                                 POWER_SUPPLY_TYPE_USB;
934                                 di->charge_status =
935                                                 POWER_SUPPLY_STATUS_CHARGING;
936                         }
937                         dev_info(di->dev, "vbus detect\n");
938                 }
939         }
940
941         if (stat_reset & VAC_DET) {
942                 di->ac_online = 0;
943                 dev_info(di->dev, "vac removed\n");
944                 twl6030_stop_ac_charger(di);
945                 if (present_charge_state & VBUS_DET) {
946                         di->charger_source = POWER_SUPPLY_TYPE_USB;
947                         di->charge_status =
948                                         POWER_SUPPLY_STATUS_CHARGING;
949                         twl6030_start_usb_charger(di);
950                 }
951         }
952         if (stat_set & VAC_DET) {
953                 di->ac_online = POWER_SUPPLY_TYPE_MAINS;
954                 if ((present_charge_state & VBUS_DET) &&
955                                 (di->vac_priority == 3))
956                         dev_info(di->dev,
957                                         "AC charger detected"
958                                         ", continue with VBUS\n");
959                 else
960                         twl6030_start_ac_charger(di);
961         }
962
963         if (stat_set & CONTROLLER_STAT1_FAULT_WDG) {
964                 charger_fault = 1;
965                 dev_info(di->dev, "Fault watchdog fired\n");
966         }
967         if (stat_reset & CONTROLLER_STAT1_FAULT_WDG)
968                 dev_err(di->dev, "Fault watchdog recovered\n");
969         if (stat_set & CONTROLLER_STAT1_BAT_REMOVED)
970                 dev_err(di->dev, "Battery removed\n");
971         if (stat_reset & CONTROLLER_STAT1_BAT_REMOVED)
972                 dev_err(di->dev, "Battery inserted\n");
973         if (stat_set & CONTROLLER_STAT1_BAT_TEMP_OVRANGE) {
974                 dev_err(di->dev, "Battery temperature overrange\n");
975                 di->bat_health = POWER_SUPPLY_HEALTH_OVERHEAT;
976         }
977         if (stat_reset & CONTROLLER_STAT1_BAT_TEMP_OVRANGE) {
978                 dev_dbg(di->dev, "Battery temperature within range\n");
979                 di->bat_health = POWER_SUPPLY_HEALTH_GOOD;
980         }
981         if (di->features & TWL6032_SUBCLASS)
982                 twl6032_charger_ctrl_interrupt(di);
983
984         if (charger_fault) {
985                 twl6030_stop_usb_charger(di);
986                 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
987                 dev_dbg(di->dev, "Charger Fault stop charging\n");
988         }
989
990         if (di->capacity != -1)
991                 power_supply_changed(&di->bat);
992         else {
993                 cancel_delayed_work(&di->twl6030_bci_monitor_work);
994                 queue_delayed_work(di->freezable_work, &di->twl6030_bci_monitor_work, 0);
995         }
996 err:
997         return IRQ_HANDLED;
998 }
999
1000 static irqreturn_t twl6030charger_fault_interrupt(int irq, void *_di)
1001 {
1002         struct twl6030_bci_device_info *di = _di;
1003         int charger_fault = 0;
1004         int ret;
1005
1006         u8 usb_charge_sts = 0, usb_charge_sts1 = 0, usb_charge_sts2 = 0;
1007
1008         ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &usb_charge_sts,
1009                                                 CHARGERUSB_INT_STATUS);
1010         if (ret)
1011                 goto err;
1012
1013         ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &usb_charge_sts1,
1014                                                 CHARGERUSB_STATUS_INT1);
1015         if (ret)
1016                 goto err;
1017
1018         ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &usb_charge_sts2,
1019                                                 CHARGERUSB_STATUS_INT2);
1020         if (ret)
1021                 goto err;
1022
1023         di->status_int1 = usb_charge_sts1;
1024         di->status_int2 = usb_charge_sts2;
1025         if (usb_charge_sts & CURRENT_TERM_INT)
1026                 dev_dbg(di->dev, "USB CURRENT_TERM_INT\n");
1027         if (usb_charge_sts & CHARGERUSB_THMREG)
1028                 dev_dbg(di->dev, "USB CHARGERUSB_THMREG\n");
1029         if (usb_charge_sts & CHARGERUSB_FAULT)
1030                 dev_dbg(di->dev, "USB CHARGERUSB_FAULT\n");
1031
1032         if (usb_charge_sts1 & CHARGERUSB_STATUS_INT1_TMREG)
1033                 dev_dbg(di->dev, "USB CHARGER Thermal regulation activated\n");
1034         if (usb_charge_sts1 & CHARGERUSB_STATUS_INT1_NO_BAT)
1035                 dev_dbg(di->dev, "No Battery Present\n");
1036         if (usb_charge_sts1 & CHARGERUSB_STATUS_INT1_BST_OCP)
1037                 dev_dbg(di->dev, "USB CHARGER Boost Over current protection\n");
1038         if (usb_charge_sts1 & CHARGERUSB_STATUS_INT1_TH_SHUTD) {
1039                 charger_fault = 1;
1040                 dev_dbg(di->dev, "USB CHARGER Thermal Shutdown\n");
1041         }
1042         if (usb_charge_sts1 & CHARGERUSB_STATUS_INT1_BAT_OVP)
1043                 dev_dbg(di->dev, "USB CHARGER Bat Over Voltage Protection\n");
1044         if (usb_charge_sts1 & CHARGERUSB_STATUS_INT1_POOR_SRC)
1045                 dev_dbg(di->dev, "USB CHARGER Poor input source\n");
1046         if (usb_charge_sts1 & CHARGERUSB_STATUS_INT1_SLP_MODE)
1047                 dev_dbg(di->dev, "USB CHARGER Sleep mode\n");
1048         if (usb_charge_sts1 & CHARGERUSB_STATUS_INT1_VBUS_OVP)
1049                 dev_dbg(di->dev, "USB CHARGER VBUS over voltage\n");
1050
1051         if (usb_charge_sts2 & CHARGE_DONE) {
1052                 di->charge_status = POWER_SUPPLY_STATUS_FULL;
1053                 dev_dbg(di->dev, "USB charge done\n");
1054         }
1055         if (usb_charge_sts2 & CURRENT_TERM)
1056                 dev_dbg(di->dev, "USB CURRENT_TERM\n");
1057         if (usb_charge_sts2 & ICCLOOP)
1058                 dev_dbg(di->dev, "USB ICCLOOP\n");
1059         if (usb_charge_sts2 & ANTICOLLAPSE)
1060                 dev_dbg(di->dev, "USB ANTICOLLAPSE\n");
1061
1062         if (charger_fault) {
1063                 twl6030_stop_usb_charger(di);
1064                 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1065                 dev_dbg(di->dev, "Charger Fault stop charging\n");
1066         }
1067         dev_info(di->dev, "Charger fault detected STS, INT1, INT2 %x %x %x\n",
1068             usb_charge_sts, usb_charge_sts1, usb_charge_sts2);
1069
1070         power_supply_changed(&di->bat);
1071 err:
1072         return IRQ_HANDLED;
1073 }
1074
1075 /*
1076  * In HW charger mode on 6032 irq routines must only deal with updating
1077  * state of charger. The hardware deals with start/stop conditions
1078  * automatically.
1079  */
1080 static irqreturn_t twl6032charger_ctrl_interrupt_hw(int irq, void *_di)
1081 {
1082         struct twl6030_bci_device_info *di = _di;
1083         u8 stat1, linear;
1084         int charger_stop = 0, end_of_charge = 0;
1085         int ret;
1086
1087         /* read charger controller_stat1 */
1088         ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &stat1,
1089                         CONTROLLER_STAT1);
1090         if (ret)
1091                 goto out;
1092
1093         ret = twl_i2c_read_u8(TWL6032_MODULE_CHARGER, &linear,
1094                         LINEAR_CHRG_STS);
1095         if (ret < 0)
1096                 goto out;
1097
1098         if (!(stat1 & (VBUS_DET | VAC_DET))) {
1099                 charger_stop = 1;
1100                 di->ac_online = di->usb_online = 0;
1101         }
1102
1103         if (!(di->usb_online || di->ac_online)) {
1104                 if (stat1 & VBUS_DET) {
1105                         di->usb_online = 1;
1106                         di->bat_health = POWER_SUPPLY_HEALTH_GOOD;
1107                 } else if (stat1 & VAC_DET) {
1108                         di->ac_online = 1;
1109                         di->bat_health = POWER_SUPPLY_HEALTH_GOOD;
1110                 }
1111         }
1112
1113         if (stat1 & CONTROLLER_STAT1_FAULT_WDG) {
1114                 charger_stop = 1;
1115                 di->bat_health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
1116                  dev_err(di->dev, "Charger error : Fault watchdog\n");
1117         }
1118         if (stat1 & CONTROLLER_STAT1_BAT_REMOVED) {
1119                 charger_stop = 1;
1120                 di->bat_health = POWER_SUPPLY_HEALTH_DEAD;
1121                 dev_info(di->dev, "Battery removed\n");
1122         }
1123         if (stat1 & CONTROLLER_STAT1_BAT_TEMP_OVRANGE) {
1124                 charger_stop = 1;
1125                  dev_dbg(di->dev,
1126                         "Charger error : Battery temperature overrange\n");
1127                 di->bat_health = POWER_SUPPLY_HEALTH_OVERHEAT;
1128         }
1129
1130         if ((stat1 & CONTROLLER_STAT1_LINCH_GATED) &&
1131                         di->use_power_path) {
1132
1133                 charger_stop = 1;
1134
1135                 if (linear & LINEAR_CHRG_STS_CRYSTL_OSC_OK) {
1136                         di->bat_health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
1137                          dev_dbg(di->dev, "Charger error: CRYSTAL OSC OK\n");
1138                 }
1139
1140                 if (linear & LINEAR_CHRG_STS_END_OF_CHARGE) {
1141                         end_of_charge = 1;
1142                         di->bat_health = POWER_SUPPLY_HEALTH_GOOD;
1143                         dev_dbg(di->dev, "Charger: Full charge\n");
1144                 }
1145
1146                 if (linear & LINEAR_CHRG_STS_VBATOV) {
1147                         di->bat_health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
1148                          dev_dbg(di->dev,
1149                                 "Charger error : Linear Status: VBATOV\n");
1150                 }
1151
1152                 if (linear & LINEAR_CHRG_STS_VSYSOV) {
1153                         di->bat_health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
1154                          dev_dbg(di->dev,
1155                                 "Charger error : Linear Status: VSYSOV\n");
1156                 }
1157         }
1158
1159         if (charger_stop) {
1160                 if (!(stat1 & (VBUS_DET | VAC_DET))) {
1161                         di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
1162                 } else {
1163                         if (end_of_charge)
1164                                 di->charge_status =
1165                                         POWER_SUPPLY_STATUS_FULL;
1166                         else
1167                                 di->charge_status =
1168                                         POWER_SUPPLY_STATUS_NOT_CHARGING;
1169                 }
1170         }
1171
1172         power_supply_changed(&di->bat);
1173
1174 out:
1175         return IRQ_HANDLED;
1176 }
1177
1178 static irqreturn_t twl6032charger_fault_interrupt_hw(int irq, void *_di)
1179 {
1180         struct twl6030_bci_device_info *di = _di;
1181         int charger_stop = 0, charger_start = 0;
1182         int ret;
1183         u8 sts, sts_int1, sts_int2, stat1;
1184
1185         ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &sts,
1186                                                 CHARGERUSB_INT_STATUS);
1187         if (ret)
1188                 goto out;
1189
1190         ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &sts_int1,
1191                                                 CHARGERUSB_STATUS_INT1);
1192         if (ret)
1193                 goto out;
1194
1195         ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &sts_int2,
1196                                                 CHARGERUSB_STATUS_INT2);
1197         if (ret)
1198                 goto out;
1199
1200         ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &stat1,
1201                                                 CONTROLLER_STAT1);
1202         if (ret)
1203                 goto out;
1204
1205         if (sts & EN_LINCH) {
1206                 charger_start = 1;
1207                 dev_dbg(di->dev, "Charger: EN_LINCH\n");
1208                 goto out;
1209         }
1210
1211         if ((sts & CURRENT_TERM_INT) && !di->use_power_path) {
1212                 dev_dbg(di->dev, "Charger: CURRENT_TERM_INT\n");
1213
1214                 if (sts_int2 & CURRENT_TERM) {
1215                         charger_stop = 1;
1216                          dev_dbg(di->dev, "Charger error: CURRENT_TERM\n");
1217                 }
1218         }
1219
1220         if (sts & CHARGERUSB_STAT) {
1221                 dev_dbg(di->dev, "Charger: CHARGEUSB_STAT\n");
1222
1223                 if (sts_int2 & ANTICOLLAPSE)
1224                          dev_dbg(di->dev, "Charger error: ANTICOLLAPSE\n");
1225         }
1226
1227         if (sts & CHARGERUSB_THMREG) {
1228                 dev_dbg(di->dev, "Charger: CHARGERUSB_THMREG\n");
1229
1230                 if (sts_int1 & CHARGERUSB_STATUS_INT1_TMREG)
1231                         dev_dbg(di->dev, "Charger error: TMREG\n");
1232         }
1233
1234         if (sts & CHARGERUSB_FAULT) {
1235                 dev_dbg(di->dev, "Charger: CHARGERUSB_FAULT\n");
1236
1237                 charger_stop = 1;
1238
1239                 if (!di->use_power_path) {
1240                         if (sts_int1 & CHARGERUSB_STATUS_INT1_NO_BAT) {
1241                                 di->bat_health = POWER_SUPPLY_HEALTH_DEAD;
1242                                 dev_err(di->dev,
1243                                         "Charger error : NO_BAT\n");
1244                         }
1245                         if (sts_int1 & CHARGERUSB_STATUS_INT1_BAT_OVP) {
1246                                 di->bat_health =
1247                                         POWER_SUPPLY_HEALTH_OVERVOLTAGE;
1248                                 dev_dbg(di->dev, "Charger error : BAT_OVP\n");
1249                         }
1250                 }
1251
1252                 if (sts_int1 & CHARGERUSB_STATUS_INT1_BST_OCP) {
1253                         di->bat_health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
1254                         printk(KERN_ERR "Charger error : BST_OCP\n");
1255                 }
1256                 if (sts_int1 & CHARGERUSB_STATUS_INT1_TH_SHUTD) {
1257                         di->bat_health = POWER_SUPPLY_HEALTH_OVERHEAT;
1258                         printk(KERN_ERR "Charger error : TH_SHUTD\n");
1259                 }
1260                 if (sts_int1 & CHARGERUSB_STATUS_INT1_POOR_SRC) {
1261                         di->bat_health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
1262                         printk(KERN_ERR "Charger error : POOR_SRC\n");
1263                 }
1264                 if (sts_int1 & CHARGERUSB_STATUS_INT1_SLP_MODE)
1265                         dev_dbg(di->dev, "Charger error: SLP_MODE\n");
1266
1267                 if (sts_int1 & CHARGERUSB_STATUS_INT1_VBUS_OVP) {
1268                         di->bat_health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
1269                         printk(KERN_ERR "Charger error : VBUS_OVP\n");
1270                 }
1271         }
1272
1273         if (charger_stop) {
1274                 if (!(stat1 & (VBUS_DET | VAC_DET)))
1275                         di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
1276                 else
1277                         di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1278         }
1279
1280 out:
1281         if (charger_start) {
1282                 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
1283                 di->bat_health = POWER_SUPPLY_HEALTH_GOOD;
1284         }
1285
1286         power_supply_changed(&di->bat);
1287
1288         return IRQ_HANDLED;
1289 }
1290
1291 static void twl6030battery_current(struct twl6030_bci_device_info *di)
1292 {
1293         int ret = 0;
1294         u16 read_value = 0;
1295         s16 temp = 0;
1296         int current_now = 0;
1297
1298         /* FG_REG_10, 11 is 14 bit signed instantaneous current sample value */
1299         ret = twl_i2c_read(TWL6030_MODULE_GASGAUGE, (u8 *)&read_value,
1300                                                                 FG_REG_10, 2);
1301         if (ret < 0) {
1302                 dev_info(di->dev, "failed to read FG_REG_10: current_now\n");
1303                 return;
1304         }
1305
1306         temp = ((s16)(read_value << 2) >> 2);
1307         current_now = temp - di->cc_offset;
1308
1309         /* current drawn per sec */
1310         current_now = current_now * fuelgauge_rate[di->fuelgauge_mode];
1311         /* current in mAmperes */
1312         current_now = (current_now * di->current_max_scale) >> 13;
1313         /* current in uAmperes */
1314         current_now = current_now * 1000;
1315         di->current_uA = current_now;
1316
1317         return;
1318 }
1319
1320 /*
1321  * Setup the twl6030 BCI module to enable backup
1322  * battery charging.
1323  */
1324 static int twl6030backupbatt_setup(void)
1325 {
1326         int ret;
1327         u8 rd_reg = 0;
1328
1329         ret = twl_i2c_read_u8(TWL6030_MODULE_ID0, &rd_reg, BBSPOR_CFG);
1330         if (ret)
1331                 return ret;
1332
1333         rd_reg |= BB_CHG_EN;
1334         ret = twl_i2c_write_u8(TWL6030_MODULE_ID0, rd_reg, BBSPOR_CFG);
1335
1336         return ret;
1337 }
1338
1339 /*
1340  * Setup the twl6030 BCI module to measure battery
1341  * temperature
1342  */
1343 static int twl6030battery_temp_setup(bool enable)
1344 {
1345         int ret;
1346         u8 rd_reg = 0;
1347
1348         ret = twl_i2c_read_u8(TWL_MODULE_MADC, &rd_reg, TWL6030_GPADC_CTRL);
1349         if (ret)
1350                 return ret;
1351
1352         if (enable)
1353                 rd_reg |= (GPADC_CTRL_TEMP1_EN | GPADC_CTRL_TEMP2_EN |
1354                         GPADC_CTRL_TEMP1_EN_MONITOR |
1355                         GPADC_CTRL_TEMP2_EN_MONITOR | GPADC_CTRL_SCALER_DIV4);
1356         else
1357                 rd_reg ^= (GPADC_CTRL_TEMP1_EN | GPADC_CTRL_TEMP2_EN |
1358                         GPADC_CTRL_TEMP1_EN_MONITOR |
1359                         GPADC_CTRL_TEMP2_EN_MONITOR | GPADC_CTRL_SCALER_DIV4);
1360
1361         ret = twl_i2c_write_u8(TWL_MODULE_MADC, rd_reg, TWL6030_GPADC_CTRL);
1362
1363         return ret;
1364 }
1365
1366 static int twl6030battery_voltage_setup(struct twl6030_bci_device_info *di)
1367 {
1368         int ret;
1369         u8 rd_reg = 0;
1370
1371         ret = twl_i2c_read_u8(TWL6030_MODULE_ID0, &rd_reg, REG_MISC1);
1372         if (ret)
1373                 return ret;
1374
1375         rd_reg = rd_reg | VAC_MEAS | VBAT_MEAS | BB_MEAS;
1376         ret = twl_i2c_write_u8(TWL6030_MODULE_ID0, rd_reg, REG_MISC1);
1377         if (ret)
1378                 return ret;
1379
1380         ret = twl_i2c_read_u8(TWL_MODULE_USB, &rd_reg, REG_USB_VBUS_CTRL_SET);
1381         if (ret)
1382                 return ret;
1383
1384         rd_reg = rd_reg | VBUS_MEAS;
1385         ret = twl_i2c_write_u8(TWL_MODULE_USB, rd_reg, REG_USB_VBUS_CTRL_SET);
1386         if (ret)
1387                 return ret;
1388
1389         ret = twl_i2c_read_u8(TWL_MODULE_USB, &rd_reg, REG_USB_ID_CTRL_SET);
1390         if (ret)
1391                 return ret;
1392
1393         rd_reg = rd_reg | ID_MEAS;
1394         ret = twl_i2c_write_u8(TWL_MODULE_USB, rd_reg, REG_USB_ID_CTRL_SET);
1395         if (ret)
1396                 return ret;
1397
1398         if (di->features & TWL6032_SUBCLASS)
1399                 ret = twl_i2c_write_u8(TWL_MODULE_MADC,
1400                                         GPADC_CTRL2_CH18_SCALER_EN,
1401                                         TWL6030_GPADC_CTRL2);
1402
1403         return ret;
1404 }
1405
1406 static int twl6030battery_current_setup(bool enable)
1407 {
1408         int ret = 0;
1409         u8  reg = 0;
1410
1411         /*
1412          * Writing 0 to REG_TOGGLE1 has no effect, so
1413          * can directly set/reset FG.
1414          */
1415         if (enable)
1416                 reg = FGDITHS | FGS;
1417         else
1418                 reg = FGDITHR | FGR;
1419
1420         ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, reg, REG_TOGGLE1);
1421         if (ret)
1422                 return ret;
1423
1424          ret = twl_i2c_write_u8(TWL6030_MODULE_GASGAUGE, CC_CAL_EN, FG_REG_00);
1425
1426         return ret;
1427 }
1428
1429 static enum power_supply_property twl6030_bci_battery_props[] = {
1430         POWER_SUPPLY_PROP_STATUS,
1431         POWER_SUPPLY_PROP_HEALTH,
1432         POWER_SUPPLY_PROP_ONLINE,
1433         POWER_SUPPLY_PROP_VOLTAGE_NOW,
1434         POWER_SUPPLY_PROP_CURRENT_NOW,
1435         POWER_SUPPLY_PROP_CURRENT_AVG,
1436         POWER_SUPPLY_PROP_CAPACITY,
1437         POWER_SUPPLY_PROP_TEMP,
1438 };
1439
1440 static enum power_supply_property twl6030_usb_props[] = {
1441         POWER_SUPPLY_PROP_ONLINE,
1442         POWER_SUPPLY_PROP_VOLTAGE_NOW,
1443 };
1444
1445 static enum power_supply_property twl6030_ac_props[] = {
1446         POWER_SUPPLY_PROP_ONLINE,
1447         POWER_SUPPLY_PROP_VOLTAGE_NOW,
1448 };
1449
1450 static enum power_supply_property twl6030_bk_bci_battery_props[] = {
1451         POWER_SUPPLY_PROP_VOLTAGE_NOW,
1452 };
1453
1454 static void twl6030_current_avg(struct work_struct *work)
1455 {
1456         s32 samples = 0;
1457         s16 cc_offset = 0;
1458         int current_avg_uA = 0;
1459         int ret;
1460         struct twl6030_bci_device_info *di = container_of(work,
1461                 struct twl6030_bci_device_info,
1462                 twl6030_current_avg_work.work);
1463
1464         di->charge_n2 = di->charge_n1;
1465         di->timer_n2 = di->timer_n1;
1466
1467         /* FG_REG_01, 02, 03 is 24 bit unsigned sample counter value */
1468         ret = twl_i2c_read(TWL6030_MODULE_GASGAUGE, (u8 *) &di->timer_n1,
1469                                                         FG_REG_01, 3);
1470         if (ret < 0)
1471                 goto err;
1472         /*
1473          * FG_REG_04, 5, 6, 7 is 32 bit signed accumulator value
1474          * accumulates instantaneous current value
1475          */
1476         ret = twl_i2c_read(TWL6030_MODULE_GASGAUGE, (u8 *) &di->charge_n1,
1477                                                         FG_REG_04, 4);
1478         if (ret < 0)
1479                 goto err;
1480         /* FG_REG_08, 09 is 10 bit signed calibration offset value */
1481         ret = twl_i2c_read(TWL6030_MODULE_GASGAUGE, (u8 *) &cc_offset,
1482                                                         FG_REG_08, 2);
1483         if (ret < 0)
1484                 goto err;
1485         cc_offset = ((s16)(cc_offset << 6) >> 6);
1486         di->cc_offset = cc_offset;
1487
1488         samples = di->timer_n1 - di->timer_n2;
1489         /* check for timer overflow */
1490         if (di->timer_n1 < di->timer_n2)
1491                 samples = samples + (1 << 24);
1492
1493         /* offset is accumulative over number of samples */
1494         cc_offset = cc_offset * samples;
1495
1496         current_avg_uA = ((di->charge_n1 - di->charge_n2 - cc_offset)
1497                                         * di->current_max_scale) /
1498                                         fuelgauge_rate[di->fuelgauge_mode];
1499         /* clock is a fixed 32Khz */
1500         current_avg_uA >>= 15;
1501
1502         /* Correct for the fuelguage sampling rate */
1503         samples /= fuelgauge_rate[di->fuelgauge_mode] * 4;
1504
1505         /*
1506          * Only update the current average if we have had a valid number
1507          * of samples in the accumulation.
1508          */
1509         if (samples) {
1510                 current_avg_uA = current_avg_uA / samples;
1511                 di->current_avg_uA = current_avg_uA * 1000;
1512         }
1513
1514         queue_delayed_work(di->freezable_work, &di->twl6030_current_avg_work,
1515                 msecs_to_jiffies(1000 * di->current_avg_interval));
1516         return;
1517 err:
1518         pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
1519 }
1520 #define BATT_NUM  52
1521 struct batt_vol_cal{
1522         u32 disp_cal;
1523         u32 dis_charge_vol;
1524         u32 charge_vol;
1525 };
1526 static struct batt_vol_cal  batt_table[BATT_NUM] = {
1527         {0,3400,3520},{1,3420,3525},{2,3420,3575},{3,3475,3600},{5,3505,3620},{7,3525,3644},
1528         {9,3540,3662},{11,3557,3670},{13,3570,3684},{15,3580,3700},{17,3610,3715},
1529         {19,3630,3720},{21,3640,3748},{23,3652,3756},{25,3662,3775},{27,3672,3790},
1530         {29,3680,3810},{31,3687,3814},{33,3693,3818},{35,3699,3822},{37,3705,3825},
1531         {39,3710,3830},{41,3714,3832},{43,3718,3834},{45,3722,3836},{47,3726,3837},
1532         {49,3730,3839},{51,3734,3841},{53,3738,3842},{55,3742,3844},{57,3746,3844},
1533         {59,3750,3855},{61,3756,3860},{63,3764,3864},{65,3774,3871},{67,3786,3890},
1534         {69,3800,3910},{71,3808,3930},{73,3817,3977},{75,3827,3977},{77,3845,3997},
1535         {79,3950,4030},{81,3964,4047},{83,3982,4064},{85,4002,4080},{87,4026,4096},
1536         {89,4030,4132},{91,4034,4144},{93,4055,4150},{95,4085,4195},{97,4085,4195},{100,4120,4200},
1537 };
1538 static int capacity_changed(struct twl6030_bci_device_info *di)
1539 {
1540         int curr_capacity = di->capacity;
1541         int charger_source = di->charger_source;
1542         int charging_disabled = 0;
1543         struct batt_vol_cal *p;
1544         p = batt_table;
1545         int i=0;
1546         /* Because system load is always greater than
1547          * termination current, we will never get a CHARGE DONE
1548          * int from BQ. And charging will alwys be in progress.
1549          * We consider Vbat>3900 to be a full battery.
1550          * Since Voltage measured during charging is Voreg ~4.2v,
1551          * we dont update capacity if we are charging.
1552          */
1553
1554         /* if it has been more than 10 minutes since our last update
1555          * and we are charging we force a update.
1556          */
1557         if (time_after(jiffies, di->ac_next_refresh)
1558                 && (di->charger_source != POWER_SUPPLY_TYPE_BATTERY)) {
1559                 charging_disabled = 1;
1560                 di->ac_next_refresh = jiffies +
1561                         msecs_to_jiffies(CHARGING_CAPACITY_UPDATE_PERIOD);
1562                 di->capacity = -1;
1563
1564                 /* We have to disable charging to read correct
1565                  * voltages.
1566                  */
1567                 twl6030_stop_charger(di);
1568                 /*voltage setteling time*/
1569                 msleep(200);
1570                 di->voltage_mV = twl6030_get_gpadc_conversion(di,
1571                                                 di->gpadc_vbat_chnl);
1572                 
1573         }
1574
1575         /* Setting the capacity level only makes sense when on
1576          * the battery is powering the board.
1577          */
1578          if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING){  //charge
1579                 if(di->voltage_mV >= p[BATT_NUM - 1].charge_vol){
1580                         curr_capacity = 100;
1581                 }       
1582                 else{
1583                         if(di->voltage_mV <= p[0].charge_vol){
1584                                 curr_capacity = 0;
1585                         }
1586                         else{
1587                                 for(i = 0; i < BATT_NUM - 1; i++){
1588
1589                                         if((p[i].charge_vol <= di->voltage_mV) && (di->voltage_mV < (p[i+1].charge_vol))){
1590                                                 curr_capacity = p[i].disp_cal ;
1591                                                 break;
1592                                         }
1593                                 }
1594                         }  
1595                 }
1596
1597         }
1598         else if (di->charge_status == POWER_SUPPLY_STATUS_DISCHARGING){  //discharge
1599                 if(di->voltage_mV >= p[BATT_NUM - 1].dis_charge_vol){
1600                         curr_capacity = 100;
1601                 }       
1602                 else{
1603                         if(di->voltage_mV <= p[0].dis_charge_vol){
1604                                 curr_capacity = 0;
1605                         }
1606                         else{
1607                                 for(i = 0; i < BATT_NUM - 1; i++){
1608                                         if(((p[i].dis_charge_vol) <= di->voltage_mV) && (di->voltage_mV < (p[i+1].dis_charge_vol))){
1609                                                 curr_capacity = p[i].disp_cal ;
1610                                                 break;
1611                                         }
1612                                 }
1613                         }  
1614
1615                 }
1616
1617
1618         }
1619         
1620         /*      
1621         if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING) {
1622                 if (di->voltage_mV < 3520)
1623                         curr_capacity = 0;
1624                 else if (di->voltage_mV < 3600 && di->voltage_mV >= 3500)
1625                         curr_capacity = 20;
1626                 else if (di->voltage_mV < 3700 && di->voltage_mV >= 3600)
1627                         curr_capacity = 50;
1628                 else if (di->voltage_mV < 3800 && di->voltage_mV >= 3700)
1629                         curr_capacity = 75;
1630                 else if (di->voltage_mV < 3900 && di->voltage_mV >= 3800)
1631                         curr_capacity = 90;
1632                 else if (di->voltage_mV >= 3900)
1633                                 curr_capacity = 100;
1634         }
1635         
1636 */
1637
1638
1639         /* if we disabled charging to check capacity,
1640          * enable it again after we read the
1641          * correct voltage.
1642          */
1643         if (charging_disabled) {
1644                 if (charger_source == POWER_SUPPLY_TYPE_MAINS)
1645                         twl6030_start_ac_charger(di);
1646                 else if (charger_source == POWER_SUPPLY_TYPE_USB)
1647                         twl6030_start_usb_charger(di);
1648         }
1649
1650         /* if battery is not present we assume it is on battery simulator and
1651          * current capacity is set to 100%
1652          */
1653         if (!is_battery_present(di))
1654                 curr_capacity = 100;
1655          
1656
1657        /* Debouncing of voltage change. */
1658         if (di->capacity == -1) {
1659                 di->capacity = curr_capacity;
1660                 di->capacity_debounce_count = 0;
1661                 return 1;
1662         }
1663 /*
1664         if (curr_capacity != di->prev_capacity) {
1665                 di->prev_capacity = curr_capacity;
1666                 di->capacity_debounce_count = 0;
1667         } else if (++di->capacity_debounce_count >= 4) {
1668                 di->capacity = curr_capacity;
1669                 di->capacity_debounce_count = 0;
1670                 return 1;
1671         }
1672 */
1673         return 1;
1674 }
1675
1676 void twl6030_batt_vol_level(struct twl6030_bci_device_info *di, int batt_vol, int *level)
1677
1678 {
1679         int i =0, status =0;
1680         static int chg_plus = 1000;
1681         static int chg_minus = 1000;
1682         static int chg_curr = 0;
1683         static int chg_num = 60;
1684         static int disp_plus = 1000;
1685         static int disp_minus = 1000;
1686         static int disp_minus2 = 1000;
1687         static int disp_curr = 0;
1688         static int disp_num = 50;
1689         static int batt_level_all = 0;
1690         static int batt_level[20];
1691         static int avr_num = 0;
1692         static int avr_int = 0;
1693
1694
1695         *level = di->capacity;
1696
1697         if (status == POWER_SUPPLY_STATUS_DISCHARGING 
1698                         && batt_vol >= batt_table[BATT_NUM-1].charge_vol) {
1699                 *level = 100;
1700                 return ;
1701         }
1702
1703         if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING) 
1704         {
1705                 disp_plus = 0;
1706                 disp_minus = 0;
1707                 disp_minus2 = 0;
1708                 disp_curr = 0;
1709                 for(i = 0; i < BATT_NUM; i++){        
1710                         if(batt_vol >= batt_table[i].charge_vol && 
1711                                          batt_vol < batt_table[i+1].charge_vol)
1712                                 break;     
1713                 }
1714                 if(batt_vol <= batt_table[0].charge_vol)
1715                         i = 0;
1716                 if(batt_vol >= batt_table[BATT_NUM-1].charge_vol)
1717                         i = BATT_NUM-1;
1718                 if(avr_int==0){
1719                         batt_level[avr_num] = batt_table[i].disp_cal;
1720                         batt_level_all += batt_level[avr_num];
1721                         avr_num++;
1722                         if(avr_num >= 20)
1723                         {
1724                                 avr_num = 0;
1725                                 avr_int = 1;
1726                         }
1727                         else
1728                         {
1729                                 *level = batt_table[i].disp_cal;
1730                                 return ;
1731                         }
1732                 }
1733                 else {
1734                         batt_level_all -= batt_level[avr_num];
1735                         batt_level[avr_num]=batt_table[i].disp_cal;
1736                         batt_level_all += batt_level[avr_num];
1737                         avr_num++;
1738                 }
1739                 if(avr_num >= 20) 
1740                         avr_num = 0;
1741                 *level = batt_level_all/20;
1742                 if ((chg_plus == 1000) && (chg_minus == 1000))
1743                 {
1744                         *level = *level;
1745                         chg_plus = 0;
1746                         chg_minus = 0;
1747                         chg_curr = 0;
1748
1749                 }
1750                 else
1751                 {                       
1752
1753                         if (*level >= (di->capacity +1))
1754                         {
1755                                 chg_minus = 0;
1756                                 chg_curr = 0;
1757                                 if(*level < 85)
1758                                         chg_num =10;
1759                                 else
1760                                         chg_num = 5;
1761                                 if (++chg_plus > chg_num)
1762                                 {
1763                                         *level = di->capacity + 1;
1764                                         chg_plus = 0;
1765                                 
1766                                 }
1767                                 else
1768                                 {
1769                                         *level = di->capacity;
1770                                 }
1771                         }
1772                         else
1773                         {
1774                                 chg_plus = 0;
1775                                 chg_minus = 0;
1776                                 chg_curr = 0;
1777                                 *level = di->capacity;
1778                         }
1779                 }
1780                 
1781
1782                 if (*level >= 100)
1783                         *level = 100;
1784                 if (*level < 0)
1785                         *level = 0;
1786         }
1787         else 
1788         {
1789                 chg_plus = 0;
1790                 chg_minus = 0;
1791                 chg_curr = 0;
1792                 for(i = 0; i < BATT_NUM; i++){        
1793                         if(batt_vol >= batt_table[i].dis_charge_vol && 
1794                                          batt_vol < batt_table[i+1].dis_charge_vol)
1795                                 break;     
1796                 }
1797                 if(batt_vol <= batt_table[0].dis_charge_vol)
1798                         i = 0;
1799                 if(batt_vol >= batt_table[BATT_NUM-1].dis_charge_vol)
1800                         i = BATT_NUM-1;
1801                 if(avr_int==0){
1802                         batt_level[avr_num] =batt_table[i].disp_cal;
1803                         batt_level_all += batt_level[avr_num];
1804                         avr_num++;
1805                         if(avr_num >= 20)
1806                         {
1807                                 avr_num = 0;
1808                                 avr_int = 1;
1809                         }
1810                         else
1811                         {
1812                                 *level = batt_table[i].disp_cal;
1813                                 return ;
1814                         }
1815                 }
1816                 else {
1817                         batt_level_all -= batt_level[avr_num];
1818                         batt_level[avr_num]=batt_table[i].disp_cal;
1819                         batt_level_all += batt_level[avr_num];
1820                         avr_num++;
1821                 }
1822                 if(avr_num >= 20) 
1823                         avr_num = 0;
1824                 *level = batt_level_all/20;
1825                 if ((disp_plus == 1000) && (disp_minus == 1000))
1826                 {
1827                         *level = *level;
1828                         disp_plus = 0;
1829                         disp_minus = 0;
1830                         disp_minus2 =0;
1831                         disp_curr = 0;
1832                 }
1833                 else
1834                 {       
1835                         if(*level <= (di->capacity -20))
1836                         {
1837                                 disp_plus = 0;
1838                                 disp_curr = 0;
1839                                 disp_minus2 = 0;
1840                                 disp_num = 1;
1841                                  if (++disp_minus > disp_num)
1842                                 {
1843                                         *level = di->capacity - 20;
1844                                         disp_minus = 0;
1845                                 }
1846                                 else
1847                                 {
1848                                         *level = di->capacity;
1849                                 }
1850                         }
1851                         else if (*level <= (di->capacity-1))    
1852                         {
1853                                 disp_plus = 0;
1854                                 disp_curr = 0;
1855                                 disp_minus = 0;
1856                                 if((*level < 17) || (*level > 85))
1857                                         disp_num = 30;
1858                                 else
1859                                         disp_num = 80;
1860                 
1861                                  if (++disp_minus2 > disp_num)
1862                                 {
1863                                         *level = di->capacity - 1;
1864                                         disp_minus2 = 0;
1865                                 }
1866                                 else
1867                                 {
1868
1869                                         *level = di->capacity;
1870                                 }
1871                         }
1872                         else
1873                         {
1874                                 disp_plus = 0;
1875                                 disp_minus = 0;
1876                                 disp_minus2 = 0;
1877                                 disp_curr = 0;
1878                                 *level = di->capacity;
1879                         }
1880                 }
1881
1882                 if (*level >= 100)
1883                         *level = 100;
1884                 if (*level < 0)
1885                         *level = 0;
1886         }
1887 }
1888
1889 static int twl6030_set_watchdog(struct twl6030_bci_device_info *di, int val)
1890 {
1891         di->watchdog_duration = val;
1892
1893         dev_dbg(di->dev, "Watchdog reset %d", val);
1894
1895         return twl_i2c_write_u8(TWL6030_MODULE_CHARGER, val, CONTROLLER_WDG);
1896
1897 }
1898
1899 static void twl6030_bci_battery_work(struct work_struct *work)
1900 {
1901         struct twl6030_bci_device_info *di = container_of(work,
1902                 struct twl6030_bci_device_info, twl6030_bci_monitor_work.work);
1903         struct twl6030_gpadc_request req;
1904         int adc_code;
1905         int temp;
1906         int ret;
1907         int level;
1908
1909         /* Kick the charger watchdog */
1910         if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING)
1911                 twl6030_set_watchdog(di, di->watchdog_duration);
1912         
1913         req.method = TWL6030_GPADC_SW2;
1914         req.channels = (1 << 1) | (1 << di->gpadc_vbat_chnl) | (1 << 8);
1915         req.active = 0;
1916         req.func_cb = NULL;
1917         ret = twl6030_gpadc_conversion(&req);
1918
1919         queue_delayed_work(di->freezable_work, &di->twl6030_bci_monitor_work,
1920                         msecs_to_jiffies(1000 * di->monitoring_interval));
1921         
1922         if (ret < 0) {
1923                 dev_dbg(di->dev, "gpadc conversion failed: %d\n", ret);
1924                 return;
1925         }
1926         
1927         if (req.rbuf[di->gpadc_vbat_chnl] > 0)
1928                 di->voltage_mV = req.rbuf[di->gpadc_vbat_chnl];
1929
1930         if (req.rbuf[8] > 0)
1931                 di->bk_voltage_mV = req.rbuf[8];
1932
1933         if (di->platform_data->battery_tmp_tbl == NULL)
1934                 return;
1935
1936         adc_code = req.rbuf[1];
1937         for (temp = 0; temp < di->platform_data->tblsize; temp++) {
1938                 if (adc_code >= di->platform_data->
1939                                 battery_tmp_tbl[temp])
1940                         break;
1941         }
1942
1943         /* first 2 values are for negative temperature */
1944         di->temp_C = (temp - 2); /* in degrees Celsius */
1945         if (capacity_changed(di)){
1946                 twl6030_batt_vol_level(di, di->voltage_mV, &level);
1947                  di->capacity = level;
1948                 power_supply_changed(&di->bat);
1949                 }
1950 }
1951
1952 static void twl6030_current_mode_changed(struct twl6030_bci_device_info *di)
1953 {
1954         int ret;
1955
1956         /* FG_REG_01, 02, 03 is 24 bit unsigned sample counter value */
1957         ret = twl_i2c_read(TWL6030_MODULE_GASGAUGE, (u8 *) &di->timer_n1,
1958                                                         FG_REG_01, 3);
1959         if (ret < 0)
1960                 goto err;
1961         /*
1962          * FG_REG_04, 5, 6, 7 is 32 bit signed accumulator value
1963          * accumulates instantaneous current value
1964          */
1965         ret = twl_i2c_read(TWL6030_MODULE_GASGAUGE, (u8 *) &di->charge_n1,
1966                                                         FG_REG_04, 4);
1967         if (ret < 0)
1968                 goto err;
1969
1970         cancel_delayed_work(&di->twl6030_current_avg_work);
1971         queue_delayed_work(di->freezable_work, &di->twl6030_current_avg_work,
1972                 msecs_to_jiffies(1000 * di->current_avg_interval));
1973         return;
1974 err:
1975         pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
1976 }
1977
1978 static void twl6030_work_interval_changed(struct twl6030_bci_device_info *di)
1979 {
1980         cancel_delayed_work(&di->twl6030_bci_monitor_work);
1981         queue_delayed_work(di->freezable_work, &di->twl6030_bci_monitor_work,
1982                 msecs_to_jiffies(1000 * di->monitoring_interval));
1983 }
1984
1985 #define to_twl6030_bci_device_info(x) container_of((x), \
1986                         struct twl6030_bci_device_info, bat);
1987
1988 static void twl6030_bci_battery_external_power_changed(struct power_supply *psy)
1989 {
1990         struct twl6030_bci_device_info *di = to_twl6030_bci_device_info(psy);
1991
1992         cancel_delayed_work(&di->twl6030_bci_monitor_work);
1993         queue_delayed_work(di->freezable_work, &di->twl6030_bci_monitor_work, 0);
1994 }
1995
1996 #define to_twl6030_ac_device_info(x) container_of((x), \
1997                 struct twl6030_bci_device_info, ac);
1998
1999 static int twl6030_ac_get_property(struct power_supply *psy,
2000                                         enum power_supply_property psp,
2001                                         union power_supply_propval *val)
2002 {
2003         struct twl6030_bci_device_info *di = to_twl6030_ac_device_info(psy);
2004
2005         switch (psp) {
2006         case POWER_SUPPLY_PROP_ONLINE:
2007                 val->intval = di->ac_online;
2008                 break;
2009         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2010                 val->intval = twl6030_get_gpadc_conversion(di, 9) * 1000;
2011                 break;
2012         default:
2013                 return -EINVAL;
2014         }
2015
2016         return 0;
2017 }
2018
2019 #define to_twl6030_usb_device_info(x) container_of((x), \
2020                 struct twl6030_bci_device_info, usb);
2021
2022 static int twl6030_usb_get_property(struct power_supply *psy,
2023                                         enum power_supply_property psp,
2024                                         union power_supply_propval *val)
2025 {
2026         struct twl6030_bci_device_info *di = to_twl6030_usb_device_info(psy);
2027
2028         switch (psp) {
2029         case POWER_SUPPLY_PROP_ONLINE:
2030                 val->intval = di->usb_online;
2031                 break;
2032         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2033                 val->intval = twl6030_get_gpadc_conversion(di, 10) * 1000;
2034                 break;
2035         default:
2036                 return -EINVAL;
2037         }
2038
2039         return 0;
2040 }
2041
2042 #define to_twl6030_bk_bci_device_info(x) container_of((x), \
2043                 struct twl6030_bci_device_info, bk_bat);
2044
2045 static int twl6030_bk_bci_battery_get_property(struct power_supply *psy,
2046                                         enum power_supply_property psp,
2047                                         union power_supply_propval *val)
2048 {
2049         struct twl6030_bci_device_info *di = to_twl6030_bk_bci_device_info(psy);
2050
2051         switch (psp) {
2052         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2053                 val->intval = di->bk_voltage_mV * 1000;
2054                 break;
2055         default:
2056                 return -EINVAL;
2057         }
2058
2059         return 0;
2060 }
2061
2062 static int twl6030_bci_battery_get_property(struct power_supply *psy,
2063                                         enum power_supply_property psp,
2064                                         union power_supply_propval *val)
2065 {
2066         struct twl6030_bci_device_info *di;
2067
2068         di = to_twl6030_bci_device_info(psy);
2069
2070         switch (psp) {
2071         case POWER_SUPPLY_PROP_STATUS:
2072                 val->intval = di->charge_status;
2073                 break;
2074         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2075                 di->voltage_mV = twl6030_get_gpadc_conversion(di,
2076                                                 di->gpadc_vbat_chnl);
2077                 val->intval = di->voltage_mV * 1000;
2078                 break;
2079         case POWER_SUPPLY_PROP_CURRENT_NOW:
2080                 twl6030battery_current(di);
2081                 val->intval = di->current_uA;
2082                 break;
2083         case POWER_SUPPLY_PROP_TEMP:
2084                 val->intval = di->temp_C;
2085                 break;
2086         case POWER_SUPPLY_PROP_ONLINE:
2087                 val->intval = di->charger_source;
2088                 break;
2089         case POWER_SUPPLY_PROP_CURRENT_AVG:
2090                 val->intval = di->current_avg_uA;
2091                 break;
2092         case POWER_SUPPLY_PROP_HEALTH:
2093                 val->intval = di->bat_health;
2094                 break;
2095         case POWER_SUPPLY_PROP_CAPACITY:
2096                 val->intval = di->capacity;
2097                 break;
2098         default:
2099                 return -EINVAL;
2100         }
2101         return 0;
2102 }
2103
2104 int twl6030_register_notifier(struct notifier_block *nb,
2105                                 unsigned int events)
2106 {
2107         return blocking_notifier_chain_register(&notifier_list, nb);
2108 }
2109 EXPORT_SYMBOL_GPL(twl6030_register_notifier);
2110
2111 int twl6030_unregister_notifier(struct notifier_block *nb,
2112                                 unsigned int events)
2113 {
2114         return blocking_notifier_chain_unregister(&notifier_list, nb);
2115 }
2116 EXPORT_SYMBOL_GPL(twl6030_unregister_notifier);
2117
2118 static void twl6030_usb_charger_work(struct work_struct *work)
2119 {
2120         struct twl6030_bci_device_info  *di =
2121                 container_of(work, struct twl6030_bci_device_info, usb_work);
2122
2123         switch (di->event) {
2124         case USB_EVENT_CHARGER:
2125                 /* POWER_SUPPLY_TYPE_USB_DCP */
2126                 di->usb_online = POWER_SUPPLY_TYPE_USB_DCP;
2127                 di->charger_incurrentmA = 1800;
2128                 break;
2129         case USB_EVENT_VBUS:
2130                 switch (di->usb_online) {
2131                 case POWER_SUPPLY_TYPE_USB_CDP:
2132                         /*
2133                          * Only 500mA here or high speed chirp
2134                          * handshaking may break
2135                          */
2136                         di->charger_incurrentmA = 500;
2137                 case POWER_SUPPLY_TYPE_USB:
2138                         break;
2139                 }
2140                 break;
2141         case USB_EVENT_NONE:
2142                 di->usb_online = 0;
2143                 di->charger_incurrentmA = 0;
2144                 break;
2145         case USB_EVENT_ENUMERATED:
2146                 if (di->usb_online == POWER_SUPPLY_TYPE_USB_CDP)
2147                         di->charger_incurrentmA = 560;
2148                 else
2149                         di->charger_incurrentmA = di->usb_max_power;
2150                 break;
2151         default:
2152                 return;
2153         }
2154         twl6030_start_usb_charger(di);
2155         power_supply_changed(&di->usb);
2156 }
2157
2158 static int twl6030_usb_notifier_call(struct notifier_block *nb,
2159                 unsigned long event, void *data)
2160 {
2161         struct twl6030_bci_device_info *di =
2162                 container_of(nb, struct twl6030_bci_device_info, nb);
2163
2164         di->event = event;
2165         switch (event) {
2166         case USB_EVENT_VBUS:
2167                 di->usb_online = *((unsigned int *) data);
2168                 break;
2169         case USB_EVENT_ENUMERATED:
2170                 di->usb_max_power = *((unsigned int *) data);
2171                 break;
2172         case USB_EVENT_CHARGER:
2173         case USB_EVENT_NONE:
2174                 break;
2175         case USB_EVENT_ID:
2176         default:
2177                 return NOTIFY_OK;
2178         }
2179
2180         schedule_work(&di->usb_work);
2181
2182         return NOTIFY_OK;
2183 }
2184
2185 static ssize_t set_fg_mode(struct device *dev,
2186                 struct device_attribute *attr, const char *buf, size_t count)
2187 {
2188         long val;
2189         int status = count;
2190         int ret;
2191         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2192
2193         if ((strict_strtol(buf, 10, &val) < 0) || (val > 3))
2194                 return -EINVAL;
2195         di->fuelgauge_mode = val;
2196         ret = twl_i2c_write_u8(TWL6030_MODULE_GASGAUGE, (val << 6) | CC_CAL_EN,
2197                                                         FG_REG_00);
2198         if (ret)
2199                 return -EIO;
2200         twl6030_current_mode_changed(di);
2201         return status;
2202 }
2203
2204 static ssize_t show_fg_mode(struct device *dev,
2205                 struct device_attribute *attr, char *buf)
2206 {
2207         int val;
2208         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2209
2210         val = di->fuelgauge_mode;
2211         return sprintf(buf, "%d\n", val);
2212 }
2213
2214 static ssize_t set_charge_src(struct device *dev,
2215                 struct device_attribute *attr, const char *buf, size_t count)
2216 {
2217         long val;
2218         int status = count;
2219         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2220
2221         if ((strict_strtol(buf, 10, &val) < 0) || (val < 2) || (val > 3))
2222                 return -EINVAL;
2223         di->vac_priority = val;
2224         return status;
2225 }
2226
2227 static ssize_t show_charge_src(struct device *dev,
2228                         struct device_attribute *attr, char *buf)
2229 {
2230         int val;
2231         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2232
2233         val = di->vac_priority;
2234         return sprintf(buf, "%d\n", val);
2235 }
2236
2237 static ssize_t show_vbus_voltage(struct device *dev,
2238                 struct device_attribute *attr, char *buf)
2239 {
2240         int val;
2241         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2242
2243         val = twl6030_get_gpadc_conversion(di, 10);
2244
2245         return sprintf(buf, "%d\n", val);
2246 }
2247
2248 static ssize_t show_id_level(struct device *dev, struct device_attribute *attr,
2249                                                         char *buf)
2250 {
2251         int val;
2252         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2253
2254         val = twl6030_get_gpadc_conversion(di, 14);
2255
2256         return sprintf(buf, "%d\n", val);
2257 }
2258
2259 static ssize_t set_watchdog(struct device *dev,
2260                 struct device_attribute *attr, const char *buf, size_t count)
2261 {
2262         long val;
2263         int status = count;
2264         int ret;
2265         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2266
2267         if ((strict_strtol(buf, 10, &val) < 0) || (val < 1) || (val > 127))
2268                 return -EINVAL;
2269         ret = twl6030_set_watchdog(di, val);
2270         if (ret)
2271                 return -EIO;
2272
2273         return status;
2274 }
2275
2276 static ssize_t show_watchdog(struct device *dev,
2277                 struct device_attribute *attr, char *buf)
2278 {
2279         int val;
2280         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2281
2282         val = di->watchdog_duration;
2283         return sprintf(buf, "%d\n", val);
2284 }
2285
2286 static ssize_t show_fg_counter(struct device *dev,
2287                 struct device_attribute *attr, char *buf)
2288 {
2289         int fg_counter = 0;
2290         int ret;
2291
2292         ret = twl_i2c_read(TWL6030_MODULE_GASGAUGE, (u8 *) &fg_counter,
2293                                                         FG_REG_01, 3);
2294         if (ret < 0)
2295                 return -EIO;
2296         return sprintf(buf, "%d\n", fg_counter);
2297 }
2298
2299 static ssize_t show_fg_accumulator(struct device *dev,
2300                 struct device_attribute *attr, char *buf)
2301 {
2302         long fg_accum = 0;
2303         int ret;
2304
2305         ret = twl_i2c_read(TWL6030_MODULE_GASGAUGE, (u8 *) &fg_accum,
2306                                                         FG_REG_04, 4);
2307         if (ret > 0)
2308                 return -EIO;
2309
2310         return sprintf(buf, "%ld\n", fg_accum);
2311 }
2312
2313 static ssize_t show_fg_offset(struct device *dev,
2314                 struct device_attribute *attr, char *buf)
2315 {
2316         s16 fg_offset = 0;
2317         int ret;
2318
2319         ret = twl_i2c_read(TWL6030_MODULE_GASGAUGE, (u8 *) &fg_offset,
2320                                                         FG_REG_08, 2);
2321         if (ret < 0)
2322                 return -EIO;
2323         fg_offset = ((s16)(fg_offset << 6) >> 6);
2324
2325         return sprintf(buf, "%d\n", fg_offset);
2326 }
2327
2328 static ssize_t set_fg_clear(struct device *dev, struct device_attribute *attr,
2329                                                 const char *buf, size_t count)
2330 {
2331         long val;
2332         int status = count;
2333         int ret;
2334
2335         if ((strict_strtol(buf, 10, &val) < 0) || (val != 1))
2336                 return -EINVAL;
2337         ret = twl_i2c_write_u8(TWL6030_MODULE_GASGAUGE, CC_AUTOCLEAR,
2338                                                         FG_REG_00);
2339         if (ret)
2340                 return -EIO;
2341
2342         return status;
2343 }
2344
2345 static ssize_t set_fg_cal(struct device *dev, struct device_attribute *attr,
2346                                                 const char *buf, size_t count)
2347 {
2348         long val;
2349         int status = count;
2350         int ret;
2351
2352         if ((strict_strtol(buf, 10, &val) < 0) || (val != 1))
2353                 return -EINVAL;
2354         ret = twl_i2c_write_u8(TWL6030_MODULE_GASGAUGE, CC_CAL_EN, FG_REG_00);
2355         if (ret)
2356                 return -EIO;
2357
2358         return status;
2359 }
2360
2361 static ssize_t set_charging(struct device *dev, struct device_attribute *attr,
2362                                           const char *buf, size_t count)
2363 {
2364         int status = count;
2365         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2366
2367         if (strncmp(buf, "startac", 7) == 0) {
2368                 if (di->charger_source == POWER_SUPPLY_TYPE_USB)
2369                         twl6030_stop_usb_charger(di);
2370                 twl6030_start_ac_charger(di);
2371         } else if (strncmp(buf, "startusb", 8) == 0) {
2372                 if (di->charger_source == POWER_SUPPLY_TYPE_MAINS)
2373                         twl6030_stop_ac_charger(di);
2374                 di->charger_source = POWER_SUPPLY_TYPE_USB;
2375                 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
2376                 twl6030_start_usb_charger(di);
2377         } else if (strncmp(buf, "stop" , 4) == 0)
2378                 twl6030_stop_charger(di);
2379         else
2380                 return -EINVAL;
2381
2382         return status;
2383 }
2384
2385 static ssize_t set_regulation_voltage(struct device *dev,
2386         struct device_attribute *attr, const char *buf, size_t count)
2387 {
2388         long val;
2389         int status = count;
2390         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2391
2392         if ((strict_strtol(buf, 10, &val) < 0) || (val < 3500)
2393                         || (val > di->platform_data->max_charger_voltagemV))
2394                 return -EINVAL;
2395         di->platform_data->max_bat_voltagemV = val;
2396         twl6030_config_voreg_reg(di, val);
2397
2398         return status;
2399 }
2400
2401 static ssize_t show_regulation_voltage(struct device *dev,
2402         struct device_attribute *attr, char *buf)
2403 {
2404         unsigned int val;
2405         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2406
2407         val = di->platform_data->max_bat_voltagemV;
2408         return sprintf(buf, "%u\n", val);
2409 }
2410
2411 static ssize_t set_termination_current(struct device *dev,
2412         struct device_attribute *attr, const char *buf, size_t count)
2413 {
2414         long val;
2415         int status = count;
2416         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2417
2418         if ((strict_strtol(buf, 10, &val) < 0) || (val < 50) || (val > 400))
2419                 return -EINVAL;
2420         di->platform_data->termination_currentmA = val;
2421         twl6030_config_iterm_reg(di, val);
2422
2423         return status;
2424 }
2425
2426 static ssize_t show_termination_current(struct device *dev,
2427                         struct device_attribute *attr, char *buf)
2428 {
2429         unsigned int val;
2430         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2431
2432         val = di->platform_data->termination_currentmA;
2433         return sprintf(buf, "%u\n", val);
2434 }
2435
2436 static ssize_t set_cin_limit(struct device *dev,
2437                 struct device_attribute *attr, const char *buf, size_t count)
2438 {
2439         long val;
2440         int status = count;
2441         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2442
2443         if ((strict_strtol(buf, 10, &val) < 0) || (val < 50) || (val > 1500))
2444                 return -EINVAL;
2445         di->charger_incurrentmA = val;
2446         twl6030_config_cinlimit_reg(di, val);
2447
2448         return status;
2449 }
2450
2451 static ssize_t show_cin_limit(struct device *dev, struct device_attribute *attr,
2452                                                                   char *buf)
2453 {
2454         unsigned int val;
2455         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2456
2457         val = di->charger_incurrentmA;
2458         return sprintf(buf, "%u\n", val);
2459 }
2460
2461 static ssize_t set_charge_current(struct device *dev,
2462                 struct device_attribute *attr, const char *buf, size_t count)
2463 {
2464         long val;
2465         int status = count;
2466         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2467
2468         if ((strict_strtol(buf, 10, &val) < 0) || (val < 300)
2469                         || (val > di->platform_data->max_charger_currentmA))
2470                 return -EINVAL;
2471         di->charger_outcurrentmA = val;
2472         twl6030_config_vichrg_reg(di, val);
2473
2474         return status;
2475 }
2476
2477 static ssize_t show_charge_current(struct device *dev,
2478                 struct device_attribute *attr, char *buf)
2479 {
2480         unsigned int val;
2481         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2482
2483         val = di->charger_outcurrentmA;
2484         return sprintf(buf, "%u\n", val);
2485 }
2486
2487 static ssize_t set_min_vbus(struct device *dev, struct device_attribute *attr,
2488                                   const char *buf, size_t count)
2489 {
2490         long val;
2491         int status = count;
2492         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2493
2494         if ((strict_strtol(buf, 10, &val) < 0) || (val < 4200) || (val > 4760))
2495                 return -EINVAL;
2496         di->min_vbus = val;
2497         twl6030_config_min_vbus_reg(di, val);
2498
2499         return status;
2500 }
2501
2502 static ssize_t show_min_vbus(struct device *dev, struct device_attribute *attr,
2503                                   char *buf)
2504 {
2505         unsigned int val;
2506         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2507
2508         val = di->min_vbus;
2509         return sprintf(buf, "%u\n", val);
2510 }
2511
2512 static ssize_t set_current_avg_interval(struct device *dev,
2513           struct device_attribute *attr, const char *buf, size_t count)
2514 {
2515         long val;
2516         int status = count;
2517         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2518
2519         if ((strict_strtol(buf, 10, &val) < 0) || (val < 10) || (val > 3600))
2520                 return -EINVAL;
2521         di->current_avg_interval = val;
2522         twl6030_current_mode_changed(di);
2523
2524         return status;
2525 }
2526
2527 static ssize_t show_current_avg_interval(struct device *dev,
2528                                   struct device_attribute *attr, char *buf)
2529 {
2530         unsigned int val;
2531         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2532
2533         val = di->current_avg_interval;
2534         return sprintf(buf, "%u\n", val);
2535 }
2536
2537 static ssize_t set_wakelock_enable(struct device *dev,
2538           struct device_attribute *attr, const char *buf, size_t count)
2539 {
2540         long val;
2541         int status = count;
2542         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2543
2544         if ((strict_strtol(buf, 10, &val) < 0) || (val < 0) || (val > 1))
2545                 return -EINVAL;
2546
2547         if ((val) && (di->charger_source == POWER_SUPPLY_TYPE_MAINS))
2548                 wake_lock(&chrg_lock);
2549         else
2550                 wake_unlock(&chrg_lock);
2551
2552         di->wakelock_enabled = val;
2553         return status;
2554 }
2555
2556 static ssize_t show_wakelock_enable(struct device *dev,
2557                                   struct device_attribute *attr, char *buf)
2558 {
2559         unsigned int val;
2560         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2561
2562         val = di->wakelock_enabled;
2563         return sprintf(buf, "%u\n", val);
2564 }
2565
2566 static ssize_t set_monitoring_interval(struct device *dev,
2567           struct device_attribute *attr, const char *buf, size_t count)
2568 {
2569         long val;
2570         int status = count;
2571         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2572
2573         if ((strict_strtol(buf, 10, &val) < 0) || (val < 10) || (val > 3600))
2574                 return -EINVAL;
2575         di->monitoring_interval = val;
2576         twl6030_work_interval_changed(di);
2577
2578         return status;
2579 }
2580
2581 static ssize_t show_monitoring_interval(struct device *dev,
2582                   struct device_attribute *attr, char *buf)
2583 {
2584         unsigned int val;
2585         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2586
2587         val = di->monitoring_interval;
2588         return sprintf(buf, "%u\n", val);
2589 }
2590
2591 static ssize_t show_bsi(struct device *dev,
2592                   struct device_attribute *attr, char *buf)
2593 {
2594         int val;
2595         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2596
2597         val = twl6030_get_gpadc_conversion(di, 0);
2598         return sprintf(buf, "%d\n", val);
2599 }
2600
2601 static ssize_t show_stat1(struct device *dev,
2602                 struct device_attribute *attr, char *buf)
2603 {
2604         unsigned val;
2605         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2606
2607         val = di->stat1;
2608         return sprintf(buf, "%u\n", val);
2609 }
2610
2611 static ssize_t show_status_int1(struct device *dev,
2612                 struct device_attribute *attr, char *buf)
2613 {
2614         unsigned val;
2615         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2616
2617         val = di->status_int1;
2618         return sprintf(buf, "%u\n", val);
2619 }
2620
2621 static ssize_t show_status_int2(struct device *dev,
2622                 struct device_attribute *attr, char *buf)
2623 {
2624         unsigned val;
2625         struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2626
2627         val = di->status_int2;
2628         return sprintf(buf, "%u\n", val);
2629 }
2630
2631 static DEVICE_ATTR(fg_mode, S_IWUSR | S_IRUGO, show_fg_mode, set_fg_mode);
2632 static DEVICE_ATTR(charge_src, S_IWUSR | S_IRUGO, show_charge_src,
2633                 set_charge_src);
2634 static DEVICE_ATTR(vbus_voltage, S_IRUGO, show_vbus_voltage, NULL);
2635 static DEVICE_ATTR(id_level, S_IRUGO, show_id_level, NULL);
2636 static DEVICE_ATTR(watchdog, S_IWUSR | S_IRUGO, show_watchdog, set_watchdog);
2637 static DEVICE_ATTR(fg_counter, S_IRUGO, show_fg_counter, NULL);
2638 static DEVICE_ATTR(fg_accumulator, S_IRUGO, show_fg_accumulator, NULL);
2639 static DEVICE_ATTR(fg_offset, S_IRUGO, show_fg_offset, NULL);
2640 static DEVICE_ATTR(fg_clear, S_IWUSR, NULL, set_fg_clear);
2641 static DEVICE_ATTR(fg_cal, S_IWUSR, NULL, set_fg_cal);
2642 static DEVICE_ATTR(charging, S_IWUSR | S_IRUGO, NULL, set_charging);
2643 static DEVICE_ATTR(regulation_voltage, S_IWUSR | S_IRUGO,
2644                 show_regulation_voltage, set_regulation_voltage);
2645 static DEVICE_ATTR(termination_current, S_IWUSR | S_IRUGO,
2646                 show_termination_current, set_termination_current);
2647 static DEVICE_ATTR(cin_limit, S_IWUSR | S_IRUGO, show_cin_limit,
2648                 set_cin_limit);
2649 static DEVICE_ATTR(charge_current, S_IWUSR | S_IRUGO, show_charge_current,
2650                 set_charge_current);
2651 static DEVICE_ATTR(min_vbus, S_IWUSR | S_IRUGO, show_min_vbus, set_min_vbus);
2652 static DEVICE_ATTR(monitoring_interval, S_IWUSR | S_IRUGO,
2653                 show_monitoring_interval, set_monitoring_interval);
2654 static DEVICE_ATTR(current_avg_interval, S_IWUSR | S_IRUGO,
2655                 show_current_avg_interval, set_current_avg_interval);
2656 static DEVICE_ATTR(wakelock_enable, S_IWUSR | S_IRUGO,
2657                 show_wakelock_enable, set_wakelock_enable);
2658 static DEVICE_ATTR(bsi, S_IRUGO, show_bsi, NULL);
2659 static DEVICE_ATTR(stat1, S_IRUGO, show_stat1, NULL);
2660 static DEVICE_ATTR(status_int1, S_IRUGO, show_status_int1, NULL);
2661 static DEVICE_ATTR(status_int2, S_IRUGO, show_status_int2, NULL);
2662
2663 static struct attribute *twl6030_bci_attributes[] = {
2664         &dev_attr_fg_mode.attr,
2665         &dev_attr_charge_src.attr,
2666         &dev_attr_vbus_voltage.attr,
2667         &dev_attr_id_level.attr,
2668         &dev_attr_watchdog.attr,
2669         &dev_attr_fg_counter.attr,
2670         &dev_attr_fg_accumulator.attr,
2671         &dev_attr_fg_offset.attr,
2672         &dev_attr_fg_clear.attr,
2673         &dev_attr_fg_cal.attr,
2674         &dev_attr_charging.attr,
2675         &dev_attr_regulation_voltage.attr,
2676         &dev_attr_termination_current.attr,
2677         &dev_attr_cin_limit.attr,
2678         &dev_attr_charge_current.attr,
2679         &dev_attr_min_vbus.attr,
2680         &dev_attr_monitoring_interval.attr,
2681         &dev_attr_current_avg_interval.attr,
2682         &dev_attr_bsi.attr,
2683         &dev_attr_stat1.attr,
2684         &dev_attr_status_int1.attr,
2685         &dev_attr_status_int2.attr,
2686         &dev_attr_wakelock_enable.attr,
2687         NULL,
2688 };
2689
2690 static const struct attribute_group twl6030_bci_attr_group = {
2691         .attrs = twl6030_bci_attributes,
2692 };
2693
2694 static char *twl6030_bci_supplied_to[] = {
2695         "twl6030_battery",
2696 };
2697
2698 extern int dwc_vbus_status(void);
2699
2700 static void twl6030_battery_update_status(struct twl6030_bci_device_info *di)
2701 {
2702         power_supply_changed(&di->bat);
2703 }
2704 static void twl6030_battery_work(struct work_struct *work)
2705 {
2706         struct twl6030_bci_device_info *di = container_of(work, struct twl6030_bci_device_info, work.work); 
2707         twl6030_battery_update_status(di);
2708         //set charging current
2709                 twl_i2c_write_u8(TWL6030_MODULE_CHARGER,0x00,CHARGERUSB_CTRL1);
2710                 if(2 == dwc_vbus_status()){
2711                         twl_i2c_write_u8(TWL6030_MODULE_CHARGER, 0x2e,CHARGERUSB_CINLIMIT);     //set vbus input current is 1.5A
2712                         twl_i2c_write_u8(TWL6030_MODULE_CHARGER, 0x0b,CHARGERUSB_VICHRG);       //set mos output current is 1A
2713                 }
2714                 else if(1 == dwc_vbus_status()){
2715                         twl_i2c_write_u8(TWL6030_MODULE_CHARGER, 0x2e,CHARGERUSB_CINLIMIT);//set vbus input current is 500ma
2716                         twl_i2c_write_u8(TWL6030_MODULE_CHARGER, 0x09,CHARGERUSB_VICHRG);       //set mos output current is 500ma
2717                 }
2718         /* reschedule for the next time */
2719         queue_delayed_work(di->freezable_work, &di->work, di->interval);
2720 }
2721
2722 static int __devinit twl6030_bci_battery_probe(struct platform_device *pdev)
2723 {
2724         struct twl4030_bci_platform_data *pdata = pdev->dev.platform_data;
2725         struct twl6030_bci_device_info *di;
2726         int irq;
2727         int ret;
2728         u8 controller_stat = 0;
2729         u8 chargerusb_ctrl1 = 0;
2730         u8 hw_state = 0;
2731         u8 reg = 0;
2732         printk("%s\n", __func__);
2733         if (!pdata) {
2734                 dev_info(&pdev->dev, "platform_data not available\n");
2735                 return -EINVAL;
2736         }
2737
2738         di = kzalloc(sizeof(*di), GFP_KERNEL);
2739         if (!di)
2740                 return -ENOMEM;
2741
2742         di->platform_data = kmemdup(pdata, sizeof(*pdata), GFP_KERNEL);
2743         if (!di->platform_data) {
2744                 kfree(di);
2745                 return -ENOMEM;
2746         }
2747
2748         if (pdata->monitoring_interval == 0) {
2749                 di->monitoring_interval = 10;
2750                 di->current_avg_interval = 10;
2751         } else {
2752                 di->monitoring_interval = pdata->monitoring_interval;
2753                 di->current_avg_interval = pdata->monitoring_interval;
2754         }
2755
2756         di->platform_data = pdata;
2757         di->features = pdata->features;
2758         di->dev = &pdev->dev;
2759
2760         if (di->features & TWL6032_SUBCLASS) {
2761                 ret = twl_i2c_read_u8(TWL_MODULE_RTC, &reg, CHARGER_MODE_REG);
2762                 if (ret)
2763                         goto temp_setup_fail;
2764
2765                 if (reg & CHARGER_MODE_POWERPATH) {
2766                         dev_dbg(di->dev, "Charger: PowerPath\n");
2767                         di->use_power_path = 1;
2768                 } else {
2769                         dev_dbg(di->dev, "Charger: NON PowerPath\n");
2770                         di->use_power_path = 0;
2771                 }
2772
2773                 if (reg & CHARGER_MODE_AUTOCHARGE) {
2774                         dev_dbg(di->dev, "Charger: AutoCharge\n");
2775                         di->use_hw_charger = 1;
2776                 } else {
2777                         dev_dbg(di->dev, "Charger: NON AutoCharge\n");
2778                         di->use_hw_charger = 0;
2779                 }
2780         } else {
2781                 di->use_power_path = 0;
2782                 di->use_hw_charger = 0;
2783         }
2784
2785         if (di->use_hw_charger) {
2786                 di->platform_data->max_charger_currentmA =
2787                                 twl6030_get_limit2_reg(di);
2788                 di->platform_data->max_charger_voltagemV =
2789                                 twl6030_get_limit1_reg(di);
2790                 di->platform_data->termination_currentmA =
2791                                 twl6030_get_iterm_reg(di);
2792                 di->platform_data->max_bat_voltagemV =
2793                                 twl6030_get_voreg_reg(di);
2794         }
2795
2796         di->bat.name = "twl6030_battery";
2797         di->bat.supplied_to = twl6030_bci_supplied_to;
2798         di->bat.num_supplicants = ARRAY_SIZE(twl6030_bci_supplied_to);
2799         di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
2800         di->bat.properties = twl6030_bci_battery_props;
2801         di->bat.num_properties = ARRAY_SIZE(twl6030_bci_battery_props);
2802         di->bat.get_property = twl6030_bci_battery_get_property;
2803         di->bat.external_power_changed =
2804                         twl6030_bci_battery_external_power_changed;
2805         di->bat_health = POWER_SUPPLY_HEALTH_GOOD;
2806
2807         di->usb.name = "twl6030_usb";
2808         di->usb.type = POWER_SUPPLY_TYPE_USB;
2809         di->usb.properties = twl6030_usb_props;
2810         di->usb.num_properties = ARRAY_SIZE(twl6030_usb_props);
2811         di->usb.get_property = twl6030_usb_get_property;
2812
2813         di->ac.name = "twl6030_ac";
2814         di->ac.type = POWER_SUPPLY_TYPE_MAINS;
2815         di->ac.properties = twl6030_ac_props;
2816         di->ac.num_properties = ARRAY_SIZE(twl6030_ac_props);
2817         di->ac.get_property = twl6030_ac_get_property;
2818
2819         di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
2820
2821         di->bk_bat.name = "twl6030_bk_battery";
2822         di->bk_bat.type = POWER_SUPPLY_TYPE_BATTERY;
2823         di->bk_bat.properties = twl6030_bk_bci_battery_props;
2824         di->bk_bat.num_properties = ARRAY_SIZE(twl6030_bk_bci_battery_props);
2825         di->bk_bat.get_property = twl6030_bk_bci_battery_get_property;
2826
2827         di->vac_priority = 2;
2828         di->capacity = -1;
2829         di->capacity_debounce_count = 0;
2830         di->ac_next_refresh = jiffies - 1;
2831         platform_set_drvdata(pdev, di);
2832
2833         /* calculate current max scale from sense */
2834         if (pdata->sense_resistor_mohm) {
2835                 di->current_max_scale = (62000) / pdata->sense_resistor_mohm;
2836         } else {
2837                 /* Set sensible defaults if platform data is missing */
2838                 if (di->features & TWL6032_SUBCLASS)
2839                         di->current_max_scale = 3100;
2840                 else
2841                         di->current_max_scale = 6200;
2842         }
2843
2844         wake_lock_init(&chrg_lock, WAKE_LOCK_SUSPEND, "ac_chrg_wake_lock");
2845         /* settings for temperature sensing */
2846         ret = twl6030battery_temp_setup(true);
2847         if (ret)
2848                 goto temp_setup_fail;
2849
2850         /* request charger fault interruption choosing between sw/hw mode */
2851         irq = platform_get_irq(pdev, 1);
2852         if (!di->use_hw_charger)
2853                 ret = request_threaded_irq(irq, NULL,
2854                                 twl6030charger_fault_interrupt,
2855                                 0, "twl_bci_fault", di);
2856         else
2857                 ret = request_threaded_irq(irq, NULL,
2858                                 twl6032charger_fault_interrupt_hw,
2859                                 0, "twl_bci_fault", di);
2860
2861         if (ret) {
2862                 dev_info(&pdev->dev, "could not request irq %d, status %d\n",
2863                         irq, ret);
2864                 goto temp_setup_fail;
2865         }
2866
2867         /* request charger ctrl interruption choosing between sw/hw mode */
2868         irq = platform_get_irq(pdev, 0);
2869         if (!di->use_hw_charger)
2870                 ret = request_threaded_irq(irq, NULL,
2871                                 twl6030charger_ctrl_interrupt,
2872                                 0, "twl_bci_ctrl", di);
2873         else
2874                 ret = request_threaded_irq(irq, NULL,
2875                                 twl6032charger_ctrl_interrupt_hw,
2876                                 0, "twl_bci_ctrl", di);
2877
2878         if (ret) {
2879                 dev_info(&pdev->dev, "could not request irq %d, status %d\n",
2880                         irq, ret);
2881                 goto chg_irq_fail;
2882         }
2883
2884         ret = power_supply_register(&pdev->dev, &di->bat);
2885         if (ret) {
2886                 dev_info(&pdev->dev, "failed to register main battery\n");
2887                 goto batt_failed;
2888         }
2889
2890         ret = power_supply_register(&pdev->dev, &di->usb);
2891         if (ret) {
2892                 dev_info(&pdev->dev, "failed to register usb power supply\n");
2893                 goto usb_failed;
2894         }
2895
2896         ret = power_supply_register(&pdev->dev, &di->ac);
2897         if (ret) {
2898                 dev_info(&pdev->dev, "failed to register ac power supply\n");
2899                 goto ac_failed;
2900         }
2901
2902         ret = power_supply_register(&pdev->dev, &di->bk_bat);
2903         if (ret) {
2904                 dev_info(&pdev->dev, "failed to register backup battery\n");
2905                 goto bk_batt_failed;
2906         }
2907         di->charge_n1 = 0;
2908         di->timer_n1 = 0;
2909
2910         //di->freezable_work = create_freezable_workqueue("battery");
2911         di->freezable_work = system_freezable_wq;
2912         INIT_DELAYED_WORK(&di->twl6030_bci_monitor_work,
2913                                 twl6030_bci_battery_work);
2914         queue_delayed_work(di->freezable_work, &di->twl6030_bci_monitor_work, 0);
2915
2916         INIT_DELAYED_WORK_DEFERRABLE(&di->twl6030_current_avg_work,
2917                                                 twl6030_current_avg);
2918         queue_delayed_work(di->freezable_work, &di->twl6030_current_avg_work, 500);
2919
2920         ret = twl6030battery_voltage_setup(di);
2921         if (ret)
2922                 dev_info(&pdev->dev, "voltage measurement setup failed\n");
2923
2924         ret = twl6030battery_current_setup(true);
2925         if (ret)
2926                 dev_info(&pdev->dev, "current measurement setup failed\n");
2927
2928         /* initialize for USB charging */
2929         if (!di->use_hw_charger) {
2930                 twl6030_config_limit1_reg(di, pdata->max_charger_voltagemV);
2931                 twl6030_config_limit2_reg(di,
2932                                 di->platform_data->max_charger_currentmA);
2933         }
2934         ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, MBAT_TEMP,
2935                                                 CONTROLLER_INT_MASK);
2936         if (ret)
2937                 goto bk_batt_failed;
2938
2939         ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, MASK_MCHARGERUSB_THMREG,
2940                                                 CHARGERUSB_INT_MASK);
2941         if (ret)
2942                 goto bk_batt_failed;
2943
2944         ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &controller_stat,
2945                 CONTROLLER_STAT1);
2946         if (ret)
2947                 goto bk_batt_failed;
2948
2949         di->stat1 = controller_stat;
2950         di->charger_outcurrentmA = di->platform_data->max_charger_currentmA;
2951
2952         twl6030_set_watchdog(di, 32);
2953
2954         INIT_WORK(&di->usb_work, twl6030_usb_charger_work);
2955         di->nb.notifier_call = twl6030_usb_notifier_call;
2956         di->otg = otg_get_transceiver();
2957         if (di->otg) {
2958                 ret = otg_register_notifier(di->otg, &di->nb);
2959                 if (ret)
2960                         dev_err(&pdev->dev, "otg register notifier"
2961                                                 " failed %d\n", ret);
2962         } else
2963                 dev_err(&pdev->dev, "otg_get_transceiver failed %d\n", ret);
2964
2965         if (di->features & TWL6032_SUBCLASS) {
2966                 di->charger_incurrentmA = 1000;
2967                 di->gpadc_vbat_chnl = TWL6032_GPADC_VBAT_CHNL;
2968         } else {
2969                 di->charger_incurrentmA = twl6030_get_usb_max_power(di->otg);
2970                 di->gpadc_vbat_chnl = TWL6030_GPADC_VBAT_CHNL;
2971         }
2972
2973         di->voltage_mV = twl6030_get_gpadc_conversion(di, di->gpadc_vbat_chnl);
2974         dev_info(&pdev->dev, "Battery Voltage at Bootup is %d mV\n",
2975                                                         di->voltage_mV);
2976
2977         ret = twl_i2c_read_u8(TWL6030_MODULE_ID0, &hw_state, STS_HW_CONDITIONS);
2978         if (ret)
2979                 goto  bk_batt_failed;
2980         if (!is_battery_present(di)) {
2981                 if (!(hw_state & STS_USB_ID)) {
2982                         dev_dbg(di->dev, "Put USB in HZ mode\n");
2983                         ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER,
2984                                         &chargerusb_ctrl1, CHARGERUSB_CTRL1);
2985                         if (ret)
2986                                 goto  bk_batt_failed;
2987
2988                         chargerusb_ctrl1 |= HZ_MODE;
2989                         ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER,
2990                                          chargerusb_ctrl1, CHARGERUSB_CTRL1);
2991                         if (ret)
2992                                 goto  bk_batt_failed;
2993                 }
2994         } else if (!di->use_hw_charger) {
2995                 if (controller_stat & VAC_DET) {
2996                         di->ac_online = POWER_SUPPLY_TYPE_MAINS;
2997                         twl6030_start_ac_charger(di);
2998                 } else if (controller_stat & VBUS_DET) {
2999                         /*
3000                          * In HOST mode (ID GROUND) with a device connected,
3001                          * do no enable usb charging
3002                          */
3003                         if (!(hw_state & STS_USB_ID)) {
3004                                 di->usb_online = POWER_SUPPLY_TYPE_USB;
3005                                 di->charger_source = POWER_SUPPLY_TYPE_USB;
3006                                 di->charge_status =
3007                                                 POWER_SUPPLY_STATUS_CHARGING;
3008                                 di->event = USB_EVENT_VBUS;
3009                                 schedule_work(&di->usb_work);
3010                         }
3011                 }
3012         } else {
3013                 int fault, charge_usb, charge_ac;
3014
3015                 twl_i2c_read_u8(TWL6032_MODULE_CHARGER, &reg,
3016                                 CHARGERUSB_INT_STATUS);
3017
3018                 fault = !(di->stat1 & CONTROLLER_STAT1_LINCH_GATED) &&
3019                                 !(di->stat1 & CONTROLLER_STAT1_FAULT_WDG);
3020                 charge_usb = (di->stat1 & VBUS_DET) &&
3021                                 !(reg & CHARGERUSB_FAULT);
3022                 charge_ac = (di->stat1 & VAC_DET) &&
3023                                 !(di->stat1 & CONTROLLER_STAT1_EXTCHRG_STATZ);
3024
3025                 dev_dbg(di->dev, "boot charge state fault %d, usb %d, ac %d\n",
3026                                 fault, charge_usb, charge_ac);
3027
3028                 if (fault && (charge_usb || charge_ac))
3029                         di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
3030                 else {
3031                         if (di->stat1 & (VBUS_DET | VAC_DET))
3032                                 di->charge_status =
3033                                         POWER_SUPPLY_STATUS_NOT_CHARGING;
3034                         else
3035                                 di->charge_status =
3036                                         POWER_SUPPLY_STATUS_DISCHARGING;
3037                 }
3038         }
3039         
3040         di->interval = msecs_to_jiffies(1 * 1000);
3041         INIT_DELAYED_WORK(&di->work, twl6030_battery_work);
3042         queue_delayed_work(di->freezable_work, &di->work, 1*HZ);
3043         
3044         ret = twl6030backupbatt_setup();
3045         if (ret)
3046                 dev_info(&pdev->dev, "Backup Bat charging setup failed\n");
3047
3048         twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
3049                                                 REG_INT_MSK_LINE_C);
3050         twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
3051                                                 REG_INT_MSK_STS_C);
3052         twl6030_interrupt_unmask(TWL6030_CHARGER_FAULT_INT_MASK,
3053                                                 REG_INT_MSK_LINE_C);
3054         twl6030_interrupt_unmask(TWL6030_CHARGER_FAULT_INT_MASK,
3055                                                 REG_INT_MSK_STS_C);
3056
3057         ret = sysfs_create_group(&pdev->dev.kobj, &twl6030_bci_attr_group);
3058         if (ret)
3059                 dev_info(&pdev->dev, "could not create sysfs files\n");
3060
3061         return 0;
3062
3063 bk_batt_failed:
3064         cancel_delayed_work(&di->twl6030_bci_monitor_work);
3065         power_supply_unregister(&di->ac);
3066 ac_failed:
3067         power_supply_unregister(&di->usb);
3068 usb_failed:
3069         power_supply_unregister(&di->bat);
3070 batt_failed:
3071         free_irq(irq, di);
3072 chg_irq_fail:
3073         irq = platform_get_irq(pdev, 1);
3074         free_irq(irq, di);
3075 temp_setup_fail:
3076         wake_lock_destroy(&chrg_lock);
3077         platform_set_drvdata(pdev, NULL);
3078         kfree(di);
3079
3080         return ret;
3081 }
3082
3083 static int __devexit twl6030_bci_battery_remove(struct platform_device *pdev)
3084 {
3085         struct twl6030_bci_device_info *di = platform_get_drvdata(pdev);
3086         int irq;
3087
3088         twl6030_interrupt_mask(TWL6030_CHARGER_CTRL_INT_MASK,
3089                                                 REG_INT_MSK_LINE_C);
3090         twl6030_interrupt_mask(TWL6030_CHARGER_CTRL_INT_MASK,
3091                                                 REG_INT_MSK_STS_C);
3092         twl6030_interrupt_mask(TWL6030_CHARGER_FAULT_INT_MASK,
3093                                                 REG_INT_MSK_LINE_C);
3094         twl6030_interrupt_mask(TWL6030_CHARGER_FAULT_INT_MASK,
3095                                                 REG_INT_MSK_STS_C);
3096
3097         irq = platform_get_irq(pdev, 0);
3098         free_irq(irq, di);
3099
3100         irq = platform_get_irq(pdev, 1);
3101         free_irq(irq, di);
3102
3103         otg_unregister_notifier(di->otg, &di->nb);
3104         sysfs_remove_group(&pdev->dev.kobj, &twl6030_bci_attr_group);
3105         cancel_delayed_work(&di->twl6030_bci_monitor_work);
3106         cancel_delayed_work(&di->twl6030_current_avg_work);
3107         flush_scheduled_work();
3108         power_supply_unregister(&di->bat);
3109         power_supply_unregister(&di->usb);
3110         power_supply_unregister(&di->ac);
3111         power_supply_unregister(&di->bk_bat);
3112         wake_lock_destroy(&chrg_lock);
3113         platform_set_drvdata(pdev, NULL);
3114         kfree(di->platform_data);
3115         kfree(di);
3116
3117         return 0;
3118 }
3119
3120 #ifdef CONFIG_PM
3121 static int twl6030_bci_battery_suspend(struct device *dev)
3122 {
3123 //      struct platform_device *pdev = to_platform_device(dev);
3124 //      struct twl6030_bci_device_info *di = platform_get_drvdata(pdev);
3125         long int events;
3126         u8 rd_reg = 0;
3127         int ret;
3128
3129         /* mask to prevent wakeup due to 32s timeout from External charger */
3130         ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &rd_reg,
3131                                                 CONTROLLER_INT_MASK);
3132         if (ret)
3133                 goto err;
3134
3135         rd_reg |= MVAC_FAULT;
3136         ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, MBAT_TEMP,
3137                                                 CONTROLLER_INT_MASK);
3138         if (ret)
3139                 goto err;
3140
3141         //cancel_delayed_work(&di->work);
3142         //cancel_delayed_work(&di->twl6030_bci_monitor_work);
3143         //cancel_delayed_work(&di->twl6030_current_avg_work);
3144
3145         /* We cannot tolarate a sleep longer than 30 seconds
3146          * while on ac charging we have to reset the BQ watchdog timer.
3147          */
3148 //      if ((di->charger_source == POWER_SUPPLY_TYPE_MAINS) &&
3149 //              ((wakeup_timer_seconds > 25) || !wakeup_timer_seconds)) {
3150 //              wakeup_timer_seconds = 25;
3151 //      }
3152
3153         /*reset the BQ watch dog*/
3154         events = BQ2415x_RESET_TIMER;
3155         blocking_notifier_call_chain(&notifier_list, events, NULL);
3156
3157         ret = twl6030battery_temp_setup(false);
3158         if (ret) {
3159                 pr_err("%s: Temp measurement setup failed (%d)!\n",
3160                                 __func__, ret);
3161                 return ret;
3162         }
3163
3164         ret = twl6030battery_current_setup(false);
3165         if (ret) {
3166                 pr_err("%s: Current measurement setup failed (%d)!\n",
3167                                 __func__, ret);
3168                 return ret;
3169         }
3170
3171         return 0;
3172 err:
3173         pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
3174         return ret;
3175 }
3176
3177 static int twl6030_bci_battery_resume(struct device *dev)
3178 {
3179 //      struct platform_device *pdev = to_platform_device(dev);
3180 //      struct twl6030_bci_device_info *di = platform_get_drvdata(pdev);
3181         long int events;
3182         u8 rd_reg = 0;
3183         int ret;
3184
3185         ret = twl6030battery_temp_setup(true);
3186         if (ret) {
3187                 pr_err("%s: Temp measurement setup failed (%d)!\n",
3188                                 __func__, ret);
3189                 return ret;
3190         }
3191
3192         ret = twl6030battery_current_setup(true);
3193         if (ret) {
3194                 pr_err("%s: Current measurement setup failed (%d)!\n",
3195                                 __func__, ret);
3196                 return ret;
3197         }
3198
3199         ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &rd_reg, CONTROLLER_INT_MASK);
3200         if (ret)
3201                 goto err;
3202
3203         rd_reg &= ~(0xFF & MVAC_FAULT);
3204         ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, MBAT_TEMP,
3205                                                 CONTROLLER_INT_MASK);
3206         if (ret)
3207                 goto err;
3208
3209         //queue_delayed_work(di->freezable_work, &di->twl6030_bci_monitor_work, 0);
3210         //queue_delayed_work(di->freezable_work, &di->twl6030_current_avg_work, 50);
3211         //queue_delayed_work(di->freezable_work, &di->work, di->interval);
3212
3213         events = BQ2415x_RESET_TIMER;
3214         blocking_notifier_call_chain(&notifier_list, events, NULL);
3215
3216         return 0;
3217 err:
3218         pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
3219         return ret;
3220 }
3221 #else
3222 #define twl6030_bci_battery_suspend     NULL
3223 #define twl6030_bci_battery_resume      NULL
3224 #endif /* CONFIG_PM */
3225
3226 static const struct dev_pm_ops pm_ops = {
3227         .suspend        = twl6030_bci_battery_suspend,
3228         .resume         = twl6030_bci_battery_resume,
3229 };
3230
3231 static struct platform_driver twl6030_bci_battery_driver = {
3232         .probe          = twl6030_bci_battery_probe,
3233         .remove         = __devexit_p(twl6030_bci_battery_remove),
3234         .driver         = {
3235                 .name   = "twl6030_bci",
3236                 .pm     = &pm_ops,
3237         },
3238 };
3239
3240 static int __init twl6030_battery_init(void)
3241 {
3242         return platform_driver_register(&twl6030_bci_battery_driver);
3243 }
3244 module_init(twl6030_battery_init);
3245
3246 static void __exit twl6030_battery_exit(void)
3247 {
3248         platform_driver_unregister(&twl6030_bci_battery_driver);
3249 }
3250 module_exit(twl6030_battery_exit);
3251
3252 MODULE_LICENSE("GPL");
3253 MODULE_ALIAS("platform:twl6030_bci");
3254 MODULE_AUTHOR("Texas Instruments Inc");