2 * linux/drivers/power/twl6030_bci_battery.c
4 * OMAP4:TWL6030 battery driver for Linux
6 * Copyright (C) 2008-2009 Texas Instruments, Inc.
7 * Author: Texas Instruments, Inc.
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.
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.
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>
32 #include <mach/gpio.h>
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
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
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)
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
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
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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
168 /* CHARGERUSB_VICHRG */
169 #define CHARGERUSB_VICHRG_300 0x0
170 #define CHARGERUSB_VICHRG_500 0x4
171 #define CHARGERUSB_VICHRG_1500 0xE
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
179 /* CHARGERUSB_CTRLLIMIT1 */
180 #define VOREGL_4P16 0x21
181 #define VOREGL_4P56 0x35
183 /* CHARGERUSB_CTRLLIMIT2 */
184 #define CHARGERUSB_CTRLLIMIT2_1500 0x0E
185 #define LOCK_LIMIT (1 << 4)
187 /* ANTICOLLAPSE_CTRL2 */
188 #define BUCK_VTH_SHIFT 5
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)
196 #define REG_TOGGLE1 0x90
197 #define FGDITHS (1 << 7)
198 #define FGDITHR (1 << 6)
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)
212 #define GPADC_ISOURCE_22uA 22
213 #define GPADC_ISOURCE_7uA 7
215 /* TWL6030/6032 BATTERY VOLTAGE GPADC CHANNELS */
217 #define TWL6030_GPADC_VBAT_CHNL 0x07
218 #define TWL6032_GPADC_VBAT_CHNL 0x12
220 /* TWL6030_GPADC_CTRL2 */
221 #define GPADC_CTRL2_CH18_SCALER_EN BIT(2)
223 #define ENABLE_ISOURCE 0x80
225 #define REG_MISC1 0xE4
226 #define VAC_MEAS 0x04
227 #define VBAT_MEAS 0x02
230 #define REG_USB_VBUS_CTRL_SET 0x04
231 #define VBUS_MEAS 0x01
232 #define REG_USB_ID_CTRL_SET 0x06
235 #define BBSPOR_CFG 0xE6
236 #define BB_CHG_EN (1 << 3)
238 #define STS_HW_CONDITIONS 0x21
239 #define STS_USB_ID (1 << 2) /* Level status of USB ID */
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)
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);
250 static inline unsigned int twl6030_get_usb_max_power(struct otg_transceiver *x)
256 /* Ptr to thermistor table */
257 static const unsigned int fuelgauge_rate[4] = {1, 4, 16, 64};
258 static struct wake_lock chrg_lock;
261 struct twl6030_bci_device_info {
288 u8 watchdog_duration;
289 u16 current_avg_interval;
290 u16 monitoring_interval;
291 unsigned int min_vbus;
293 struct twl4030_bci_platform_data *platform_data;
295 unsigned int charger_incurrentmA;
296 unsigned int charger_outcurrentmA;
297 unsigned long usb_max_power;
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;
306 struct power_supply bat;
307 struct power_supply usb;
308 struct power_supply ac;
309 struct power_supply bk_bat;
311 struct otg_transceiver *otg;
312 struct notifier_block nb;
313 struct work_struct usb_work;
315 struct workqueue_struct *freezable_work;
317 struct delayed_work twl6030_bci_monitor_work;
318 struct delayed_work twl6030_current_avg_work;
320 unsigned long features;
325 /* max scale current based on sense resitor */
326 int current_max_scale;
327 struct delayed_work work;
328 unsigned int interval;
331 static BLOCKING_NOTIFIER_HEAD(notifier_list);
332 extern u32 wakeup_timer_seconds;
334 static void twl6030_config_min_vbus_reg(struct twl6030_bci_device_info *di,
340 /* not required on TWL6032 */
341 if (di->features & TWL6032_SUBCLASS)
344 if (value > 4760 || value < 4200) {
345 dev_err(di->dev, "invalid min vbus\n");
349 ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &rd_reg,
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,
361 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
364 static void twl6030_config_iterm_reg(struct twl6030_bci_device_info *di,
365 unsigned int term_currentmA)
369 if ((term_currentmA > 400) || (term_currentmA < 50)) {
370 dev_err(di->dev, "invalid termination current\n");
374 term_currentmA = ((term_currentmA - 50)/50) << 5;
375 ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, term_currentmA,
378 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
381 static unsigned int twl6030_get_iterm_reg(struct twl6030_bci_device_info *di)
384 unsigned int currentmA;
387 ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &val, CHARGERUSB_CTRL2);
389 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
392 currentmA = 50 + (val >> 5) * 50;
397 static void twl6030_config_voreg_reg(struct twl6030_bci_device_info *di,
398 unsigned int voltagemV)
402 if ((voltagemV < 3500) || (voltagemV > 4760)) {
403 dev_err(di->dev, "invalid charger_voltagemV\n");
407 voltagemV = (voltagemV - 3500) / 20;
408 ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, voltagemV,
411 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
414 static unsigned int twl6030_get_voreg_reg(struct twl6030_bci_device_info *di)
417 unsigned int voltagemV;
420 ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &val, CHARGERUSB_VOREG);
422 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
425 voltagemV = 3500 + (val * 20);
430 static void twl6030_config_vichrg_reg(struct twl6030_bci_device_info *di,
431 unsigned int currentmA)
435 if ((currentmA >= 300) && (currentmA <= 450))
436 currentmA = (currentmA - 300) / 50;
437 else if ((currentmA >= 500) && (currentmA <= 1500))
438 currentmA = (currentmA - 500) / 100 + 4;
440 dev_err(di->dev, "invalid charger_currentmA\n");
444 ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, currentmA,
447 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
450 static void twl6030_config_cinlimit_reg(struct twl6030_bci_device_info *di,
451 unsigned int currentmA)
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");
465 /* This is no current limit */
469 ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, currentmA,
470 CHARGERUSB_CINLIMIT);
472 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
475 static void twl6030_config_limit1_reg(struct twl6030_bci_device_info *di,
476 unsigned int voltagemV)
480 if ((voltagemV < 3500) || (voltagemV > 4760)) {
481 dev_err(di->dev, "invalid max_charger_voltagemV\n");
485 voltagemV = (voltagemV - 3500) / 20;
486 ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, voltagemV,
487 CHARGERUSB_CTRLLIMIT1);
489 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
492 static unsigned int twl6030_get_limit1_reg(struct twl6030_bci_device_info *di)
495 unsigned int voltagemV;
498 ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &val,
499 CHARGERUSB_CTRLLIMIT1);
501 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
504 voltagemV = 3500 + (val * 20);
509 static void twl6030_config_limit2_reg(struct twl6030_bci_device_info *di,
510 unsigned int currentmA)
514 if ((currentmA >= 300) && (currentmA <= 450))
515 currentmA = (currentmA - 300) / 50;
516 else if ((currentmA >= 500) && (currentmA <= 1500))
517 currentmA = (currentmA - 500) / 100 + 4;
519 dev_err(di->dev, "invalid max_charger_currentmA\n");
523 currentmA |= LOCK_LIMIT;
524 ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, currentmA,
525 CHARGERUSB_CTRLLIMIT2);
527 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
530 static const int vichrg[] = {
531 300, 350, 400, 450, 500, 600, 700, 800,
532 900, 1000, 1100, 1200, 1300, 1400, 1500, 300
535 static unsigned int twl6030_get_limit2_reg(struct twl6030_bci_device_info *di)
538 unsigned int currentmA;
541 ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &val,
542 CHARGERUSB_CTRLLIMIT2);
544 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
547 currentmA = vichrg[val & 0xF];
553 * Return channel value
556 static int twl6030_get_gpadc_conversion(struct twl6030_bci_device_info *di,
559 struct twl6030_gpadc_request req;
563 req.channels = (1 << channel_no);
564 req.method = TWL6030_GPADC_SW2;
567 ret = twl6030_gpadc_conversion(&req);
571 if (req.rbuf[channel_no] > 0)
572 temp = req.rbuf[channel_no];
577 static int is_battery_present(struct twl6030_bci_device_info *di)
580 static unsigned int current_src_val;
583 * Prevent charging on batteries were id resistor is
586 val = twl6030_get_gpadc_conversion(di,di->gpadc_vbat_chnl);
589 * twl6030_get_gpadc_conversion for
590 * 6030 return resistance, for 6032 - voltage and
591 * it should be converted to resistance before
594 if (!current_src_val) {
597 if (twl_i2c_read_u8(TWL_MODULE_MADC, ®,
599 pr_err("%s: Error reading TWL6030_GPADC_CTRL\n",
602 current_src_val = (reg & GPADC_CTRL_ISOURCE_EN) ?
607 val = (val * 1000) / current_src_val;
609 if (val < BATTERY_DETECT_THRESHOLD)
615 static void twl6030_stop_usb_charger(struct twl6030_bci_device_info *di)
620 if (di->use_hw_charger) {
621 ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, ®,
626 ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, reg,
634 di->charger_source = 0;
635 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
637 ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, 0, CONTROLLER_CTRL1);
641 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
644 static void twl6030_start_usb_charger(struct twl6030_bci_device_info *di)
649 if (di->use_hw_charger) {
650 ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, ®,
656 ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, reg,
664 if (!is_battery_present(di)) {
665 dev_info(di->dev, "BATTERY NOT DETECTED!\n");
669 if (di->charger_source == POWER_SUPPLY_TYPE_MAINS)
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);
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);
688 if (di->charger_incurrentmA >= 50) {
689 reg = CONTROLLER_CTRL1_EN_CHARGER;
691 if (di->use_power_path)
692 reg |= CONTROLLER_CTRL1_EN_LINCH;
694 ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, reg,
699 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
703 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
706 static void twl6030_stop_ac_charger(struct twl6030_bci_device_info *di)
711 di->charger_source = 0;
712 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
713 events = BQ2415x_STOP_CHARGING;
715 if (di->use_hw_charger)
718 blocking_notifier_call_chain(¬ifier_list, events, NULL);
720 ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, 0, CONTROLLER_CTRL1);
722 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
724 if (di->wakelock_enabled)
725 wake_unlock(&chrg_lock);
728 static void twl6030_start_ac_charger(struct twl6030_bci_device_info *di)
733 if (!is_battery_present(di)) {
734 dev_info(di->dev, "BATTERY NOT DETECTED!\n");
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;
742 if (di->use_hw_charger)
745 blocking_notifier_call_chain(¬ifier_list, events, NULL);
747 ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER,
748 CONTROLLER_CTRL1_EN_CHARGER |
749 CONTROLLER_CTRL1_SEL_CHARGER,
752 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
754 if (di->wakelock_enabled)
755 wake_lock(&chrg_lock);
758 static void twl6030_stop_charger(struct twl6030_bci_device_info *di)
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);
766 static void twl6032_charger_ctrl_interrupt(struct twl6030_bci_device_info *di)
768 u8 stat_toggle, stat_reset, stat_set = 0;
769 u8 present_state = 0, linear_state;
770 u8 present_status = 0;
773 err = twl_i2c_read_u8(TWL6032_MODULE_CHARGER, &present_state,
776 dev_err(di->dev, "%s: Error access to TWL6030 (%d)\n",
781 err = twl_i2c_read_u8(TWL6032_MODULE_CHARGER, &present_status,
782 CHARGERUSB_INT_STATUS);
784 dev_err(di->dev, "%s: Error access to TWL6030 (%d)\n",
789 linear_state = di->linear_stat;
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;
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;
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;
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;
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;
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");
827 * Interrupt service routine
829 * Attends to TWL 6030 power module interruptions events, specifically
830 * USB_PRES (USB charger presence) CHG_PRES (AC charger presence) events
833 static irqreturn_t twl6030charger_ctrl_interrupt(int irq, void *_di)
835 struct twl6030_bci_device_info *di = _di;
837 int charger_fault = 0;
839 u8 stat_toggle, stat_reset, stat_set = 0;
841 u8 present_charge_state = 0;
842 u8 ac_or_vbus, no_ac_and_vbus = 0;
843 u8 hw_state = 0, temp = 0;
845 /* read charger controller_stat1 */
846 ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &present_charge_state,
850 * Since present state read failed, charger_state is no
851 * longer valid, reset to zero inorder to detect next events
857 ret = twl_i2c_read_u8(TWL6030_MODULE_ID0, &hw_state, STS_HW_CONDITIONS);
861 charge_state = di->stat1;
863 stat_toggle = charge_state ^ present_charge_state;
864 stat_set = stat_toggle & present_charge_state;
865 stat_reset = stat_toggle & charge_state;
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 */
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(¬ifier_list, events, NULL);
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);
890 if (temp & MASK_MCHARGERUSB_FAULT) {
891 ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER,
892 (temp & ~MASK_MCHARGERUSB_FAULT),
893 CHARGERUSB_INT_MASK);
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);
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
910 if (hw_state & STS_USB_ID) {
911 ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER,
913 CHARGERUSB_INT_MASK);
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);
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");
933 POWER_SUPPLY_TYPE_USB;
935 POWER_SUPPLY_STATUS_CHARGING;
937 dev_info(di->dev, "vbus detect\n");
941 if (stat_reset & VAC_DET) {
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;
948 POWER_SUPPLY_STATUS_CHARGING;
949 twl6030_start_usb_charger(di);
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))
957 "AC charger detected"
958 ", continue with VBUS\n");
960 twl6030_start_ac_charger(di);
963 if (stat_set & CONTROLLER_STAT1_FAULT_WDG) {
965 dev_info(di->dev, "Fault watchdog fired\n");
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;
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;
981 if (di->features & TWL6032_SUBCLASS)
982 twl6032_charger_ctrl_interrupt(di);
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");
990 if (di->capacity != -1)
991 power_supply_changed(&di->bat);
993 cancel_delayed_work(&di->twl6030_bci_monitor_work);
994 queue_delayed_work(di->freezable_work, &di->twl6030_bci_monitor_work, 0);
1000 static irqreturn_t twl6030charger_fault_interrupt(int irq, void *_di)
1002 struct twl6030_bci_device_info *di = _di;
1003 int charger_fault = 0;
1006 u8 usb_charge_sts = 0, usb_charge_sts1 = 0, usb_charge_sts2 = 0;
1008 ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &usb_charge_sts,
1009 CHARGERUSB_INT_STATUS);
1013 ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &usb_charge_sts1,
1014 CHARGERUSB_STATUS_INT1);
1018 ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &usb_charge_sts2,
1019 CHARGERUSB_STATUS_INT2);
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");
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) {
1040 dev_dbg(di->dev, "USB CHARGER Thermal Shutdown\n");
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");
1051 if (usb_charge_sts2 & CHARGE_DONE) {
1052 di->charge_status = POWER_SUPPLY_STATUS_FULL;
1053 dev_dbg(di->dev, "USB charge done\n");
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");
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");
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);
1070 power_supply_changed(&di->bat);
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
1080 static irqreturn_t twl6032charger_ctrl_interrupt_hw(int irq, void *_di)
1082 struct twl6030_bci_device_info *di = _di;
1084 int charger_stop = 0, end_of_charge = 0;
1087 /* read charger controller_stat1 */
1088 ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &stat1,
1093 ret = twl_i2c_read_u8(TWL6032_MODULE_CHARGER, &linear,
1098 if (!(stat1 & (VBUS_DET | VAC_DET))) {
1100 di->ac_online = di->usb_online = 0;
1103 if (!(di->usb_online || di->ac_online)) {
1104 if (stat1 & VBUS_DET) {
1106 di->bat_health = POWER_SUPPLY_HEALTH_GOOD;
1107 } else if (stat1 & VAC_DET) {
1109 di->bat_health = POWER_SUPPLY_HEALTH_GOOD;
1113 if (stat1 & CONTROLLER_STAT1_FAULT_WDG) {
1115 di->bat_health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
1116 dev_err(di->dev, "Charger error : Fault watchdog\n");
1118 if (stat1 & CONTROLLER_STAT1_BAT_REMOVED) {
1120 di->bat_health = POWER_SUPPLY_HEALTH_DEAD;
1121 dev_info(di->dev, "Battery removed\n");
1123 if (stat1 & CONTROLLER_STAT1_BAT_TEMP_OVRANGE) {
1126 "Charger error : Battery temperature overrange\n");
1127 di->bat_health = POWER_SUPPLY_HEALTH_OVERHEAT;
1130 if ((stat1 & CONTROLLER_STAT1_LINCH_GATED) &&
1131 di->use_power_path) {
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");
1140 if (linear & LINEAR_CHRG_STS_END_OF_CHARGE) {
1142 di->bat_health = POWER_SUPPLY_HEALTH_GOOD;
1143 dev_dbg(di->dev, "Charger: Full charge\n");
1146 if (linear & LINEAR_CHRG_STS_VBATOV) {
1147 di->bat_health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
1149 "Charger error : Linear Status: VBATOV\n");
1152 if (linear & LINEAR_CHRG_STS_VSYSOV) {
1153 di->bat_health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
1155 "Charger error : Linear Status: VSYSOV\n");
1160 if (!(stat1 & (VBUS_DET | VAC_DET))) {
1161 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
1165 POWER_SUPPLY_STATUS_FULL;
1168 POWER_SUPPLY_STATUS_NOT_CHARGING;
1172 power_supply_changed(&di->bat);
1178 static irqreturn_t twl6032charger_fault_interrupt_hw(int irq, void *_di)
1180 struct twl6030_bci_device_info *di = _di;
1181 int charger_stop = 0, charger_start = 0;
1183 u8 sts, sts_int1, sts_int2, stat1;
1185 ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &sts,
1186 CHARGERUSB_INT_STATUS);
1190 ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &sts_int1,
1191 CHARGERUSB_STATUS_INT1);
1195 ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &sts_int2,
1196 CHARGERUSB_STATUS_INT2);
1200 ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &stat1,
1205 if (sts & EN_LINCH) {
1207 dev_dbg(di->dev, "Charger: EN_LINCH\n");
1211 if ((sts & CURRENT_TERM_INT) && !di->use_power_path) {
1212 dev_dbg(di->dev, "Charger: CURRENT_TERM_INT\n");
1214 if (sts_int2 & CURRENT_TERM) {
1216 dev_dbg(di->dev, "Charger error: CURRENT_TERM\n");
1220 if (sts & CHARGERUSB_STAT) {
1221 dev_dbg(di->dev, "Charger: CHARGEUSB_STAT\n");
1223 if (sts_int2 & ANTICOLLAPSE)
1224 dev_dbg(di->dev, "Charger error: ANTICOLLAPSE\n");
1227 if (sts & CHARGERUSB_THMREG) {
1228 dev_dbg(di->dev, "Charger: CHARGERUSB_THMREG\n");
1230 if (sts_int1 & CHARGERUSB_STATUS_INT1_TMREG)
1231 dev_dbg(di->dev, "Charger error: TMREG\n");
1234 if (sts & CHARGERUSB_FAULT) {
1235 dev_dbg(di->dev, "Charger: CHARGERUSB_FAULT\n");
1239 if (!di->use_power_path) {
1240 if (sts_int1 & CHARGERUSB_STATUS_INT1_NO_BAT) {
1241 di->bat_health = POWER_SUPPLY_HEALTH_DEAD;
1243 "Charger error : NO_BAT\n");
1245 if (sts_int1 & CHARGERUSB_STATUS_INT1_BAT_OVP) {
1247 POWER_SUPPLY_HEALTH_OVERVOLTAGE;
1248 dev_dbg(di->dev, "Charger error : BAT_OVP\n");
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");
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");
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");
1264 if (sts_int1 & CHARGERUSB_STATUS_INT1_SLP_MODE)
1265 dev_dbg(di->dev, "Charger error: SLP_MODE\n");
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");
1274 if (!(stat1 & (VBUS_DET | VAC_DET)))
1275 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
1277 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1281 if (charger_start) {
1282 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
1283 di->bat_health = POWER_SUPPLY_HEALTH_GOOD;
1286 power_supply_changed(&di->bat);
1291 static void twl6030battery_current(struct twl6030_bci_device_info *di)
1296 int current_now = 0;
1298 /* FG_REG_10, 11 is 14 bit signed instantaneous current sample value */
1299 ret = twl_i2c_read(TWL6030_MODULE_GASGAUGE, (u8 *)&read_value,
1302 dev_info(di->dev, "failed to read FG_REG_10: current_now\n");
1306 temp = ((s16)(read_value << 2) >> 2);
1307 current_now = temp - di->cc_offset;
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;
1321 * Setup the twl6030 BCI module to enable backup
1324 static int twl6030backupbatt_setup(void)
1329 ret = twl_i2c_read_u8(TWL6030_MODULE_ID0, &rd_reg, BBSPOR_CFG);
1333 rd_reg |= BB_CHG_EN;
1334 ret = twl_i2c_write_u8(TWL6030_MODULE_ID0, rd_reg, BBSPOR_CFG);
1340 * Setup the twl6030 BCI module to measure battery
1343 static int twl6030battery_temp_setup(bool enable)
1348 ret = twl_i2c_read_u8(TWL_MODULE_MADC, &rd_reg, TWL6030_GPADC_CTRL);
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);
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);
1361 ret = twl_i2c_write_u8(TWL_MODULE_MADC, rd_reg, TWL6030_GPADC_CTRL);
1366 static int twl6030battery_voltage_setup(struct twl6030_bci_device_info *di)
1371 ret = twl_i2c_read_u8(TWL6030_MODULE_ID0, &rd_reg, REG_MISC1);
1375 rd_reg = rd_reg | VAC_MEAS | VBAT_MEAS | BB_MEAS;
1376 ret = twl_i2c_write_u8(TWL6030_MODULE_ID0, rd_reg, REG_MISC1);
1380 ret = twl_i2c_read_u8(TWL_MODULE_USB, &rd_reg, REG_USB_VBUS_CTRL_SET);
1384 rd_reg = rd_reg | VBUS_MEAS;
1385 ret = twl_i2c_write_u8(TWL_MODULE_USB, rd_reg, REG_USB_VBUS_CTRL_SET);
1389 ret = twl_i2c_read_u8(TWL_MODULE_USB, &rd_reg, REG_USB_ID_CTRL_SET);
1393 rd_reg = rd_reg | ID_MEAS;
1394 ret = twl_i2c_write_u8(TWL_MODULE_USB, rd_reg, REG_USB_ID_CTRL_SET);
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);
1406 static int twl6030battery_current_setup(bool enable)
1412 * Writing 0 to REG_TOGGLE1 has no effect, so
1413 * can directly set/reset FG.
1416 reg = FGDITHS | FGS;
1418 reg = FGDITHR | FGR;
1420 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, reg, REG_TOGGLE1);
1424 ret = twl_i2c_write_u8(TWL6030_MODULE_GASGAUGE, CC_CAL_EN, FG_REG_00);
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,
1440 static enum power_supply_property twl6030_usb_props[] = {
1441 POWER_SUPPLY_PROP_ONLINE,
1442 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1445 static enum power_supply_property twl6030_ac_props[] = {
1446 POWER_SUPPLY_PROP_ONLINE,
1447 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1450 static enum power_supply_property twl6030_bk_bci_battery_props[] = {
1451 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1454 static void twl6030_current_avg(struct work_struct *work)
1458 int current_avg_uA = 0;
1460 struct twl6030_bci_device_info *di = container_of(work,
1461 struct twl6030_bci_device_info,
1462 twl6030_current_avg_work.work);
1464 di->charge_n2 = di->charge_n1;
1465 di->timer_n2 = di->timer_n1;
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,
1473 * FG_REG_04, 5, 6, 7 is 32 bit signed accumulator value
1474 * accumulates instantaneous current value
1476 ret = twl_i2c_read(TWL6030_MODULE_GASGAUGE, (u8 *) &di->charge_n1,
1480 /* FG_REG_08, 09 is 10 bit signed calibration offset value */
1481 ret = twl_i2c_read(TWL6030_MODULE_GASGAUGE, (u8 *) &cc_offset,
1485 cc_offset = ((s16)(cc_offset << 6) >> 6);
1486 di->cc_offset = cc_offset;
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);
1493 /* offset is accumulative over number of samples */
1494 cc_offset = cc_offset * samples;
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;
1502 /* Correct for the fuelguage sampling rate */
1503 samples /= fuelgauge_rate[di->fuelgauge_mode] * 4;
1506 * Only update the current average if we have had a valid number
1507 * of samples in the accumulation.
1510 current_avg_uA = current_avg_uA / samples;
1511 di->current_avg_uA = current_avg_uA * 1000;
1514 queue_delayed_work(di->freezable_work, &di->twl6030_current_avg_work,
1515 msecs_to_jiffies(1000 * di->current_avg_interval));
1518 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
1521 struct batt_vol_cal{
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},
1538 static int capacity_changed(struct twl6030_bci_device_info *di)
1540 int curr_capacity = di->capacity;
1541 int charger_source = di->charger_source;
1542 int charging_disabled = 0;
1543 struct batt_vol_cal *p;
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.
1554 /* if it has been more than 10 minutes since our last update
1555 * and we are charging we force a update.
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);
1564 /* We have to disable charging to read correct
1567 twl6030_stop_charger(di);
1568 /*voltage setteling time*/
1570 di->voltage_mV = twl6030_get_gpadc_conversion(di,
1571 di->gpadc_vbat_chnl);
1575 /* Setting the capacity level only makes sense when on
1576 * the battery is powering the board.
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;
1583 if(di->voltage_mV <= p[0].charge_vol){
1587 for(i = 0; i < BATT_NUM - 1; i++){
1589 if((p[i].charge_vol <= di->voltage_mV) && (di->voltage_mV < (p[i+1].charge_vol))){
1590 curr_capacity = p[i].disp_cal ;
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;
1603 if(di->voltage_mV <= p[0].dis_charge_vol){
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 ;
1621 if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING) {
1622 if (di->voltage_mV < 3520)
1624 else if (di->voltage_mV < 3600 && di->voltage_mV >= 3500)
1626 else if (di->voltage_mV < 3700 && di->voltage_mV >= 3600)
1628 else if (di->voltage_mV < 3800 && di->voltage_mV >= 3700)
1630 else if (di->voltage_mV < 3900 && di->voltage_mV >= 3800)
1632 else if (di->voltage_mV >= 3900)
1633 curr_capacity = 100;
1639 /* if we disabled charging to check capacity,
1640 * enable it again after we read the
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);
1650 /* if battery is not present we assume it is on battery simulator and
1651 * current capacity is set to 100%
1653 if (!is_battery_present(di))
1654 curr_capacity = 100;
1657 /* Debouncing of voltage change. */
1658 if (di->capacity == -1) {
1659 di->capacity = curr_capacity;
1660 di->capacity_debounce_count = 0;
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;
1676 void twl6030_batt_vol_level(struct twl6030_bci_device_info *di, int batt_vol, int *level)
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;
1695 *level = di->capacity;
1697 if (status == POWER_SUPPLY_STATUS_DISCHARGING
1698 && batt_vol >= batt_table[BATT_NUM-1].charge_vol) {
1703 if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING)
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)
1714 if(batt_vol <= batt_table[0].charge_vol)
1716 if(batt_vol >= batt_table[BATT_NUM-1].charge_vol)
1719 batt_level[avr_num] = batt_table[i].disp_cal;
1720 batt_level_all += batt_level[avr_num];
1729 *level = batt_table[i].disp_cal;
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];
1741 *level = batt_level_all/20;
1742 if ((chg_plus == 1000) && (chg_minus == 1000))
1753 if (*level >= (di->capacity +1))
1761 if (++chg_plus > chg_num)
1763 *level = di->capacity + 1;
1769 *level = di->capacity;
1777 *level = di->capacity;
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)
1797 if(batt_vol <= batt_table[0].dis_charge_vol)
1799 if(batt_vol >= batt_table[BATT_NUM-1].dis_charge_vol)
1802 batt_level[avr_num] =batt_table[i].disp_cal;
1803 batt_level_all += batt_level[avr_num];
1812 *level = batt_table[i].disp_cal;
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];
1824 *level = batt_level_all/20;
1825 if ((disp_plus == 1000) && (disp_minus == 1000))
1835 if(*level <= (di->capacity -20))
1841 if (++disp_minus > disp_num)
1843 *level = di->capacity - 20;
1848 *level = di->capacity;
1851 else if (*level <= (di->capacity-1))
1856 if((*level < 17) || (*level > 85))
1861 if (++disp_minus2 > disp_num)
1863 *level = di->capacity - 1;
1869 *level = di->capacity;
1878 *level = di->capacity;
1889 static int twl6030_set_watchdog(struct twl6030_bci_device_info *di, int val)
1891 di->watchdog_duration = val;
1893 dev_dbg(di->dev, "Watchdog reset %d", val);
1895 return twl_i2c_write_u8(TWL6030_MODULE_CHARGER, val, CONTROLLER_WDG);
1899 static void twl6030_bci_battery_work(struct work_struct *work)
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;
1909 /* Kick the charger watchdog */
1910 if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING)
1911 twl6030_set_watchdog(di, di->watchdog_duration);
1913 req.method = TWL6030_GPADC_SW2;
1914 req.channels = (1 << 1) | (1 << di->gpadc_vbat_chnl) | (1 << 8);
1917 ret = twl6030_gpadc_conversion(&req);
1919 queue_delayed_work(di->freezable_work, &di->twl6030_bci_monitor_work,
1920 msecs_to_jiffies(1000 * di->monitoring_interval));
1923 dev_dbg(di->dev, "gpadc conversion failed: %d\n", ret);
1927 if (req.rbuf[di->gpadc_vbat_chnl] > 0)
1928 di->voltage_mV = req.rbuf[di->gpadc_vbat_chnl];
1930 if (req.rbuf[8] > 0)
1931 di->bk_voltage_mV = req.rbuf[8];
1933 if (di->platform_data->battery_tmp_tbl == NULL)
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])
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);
1952 static void twl6030_current_mode_changed(struct twl6030_bci_device_info *di)
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,
1962 * FG_REG_04, 5, 6, 7 is 32 bit signed accumulator value
1963 * accumulates instantaneous current value
1965 ret = twl_i2c_read(TWL6030_MODULE_GASGAUGE, (u8 *) &di->charge_n1,
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));
1975 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
1978 static void twl6030_work_interval_changed(struct twl6030_bci_device_info *di)
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));
1985 #define to_twl6030_bci_device_info(x) container_of((x), \
1986 struct twl6030_bci_device_info, bat);
1988 static void twl6030_bci_battery_external_power_changed(struct power_supply *psy)
1990 struct twl6030_bci_device_info *di = to_twl6030_bci_device_info(psy);
1992 cancel_delayed_work(&di->twl6030_bci_monitor_work);
1993 queue_delayed_work(di->freezable_work, &di->twl6030_bci_monitor_work, 0);
1996 #define to_twl6030_ac_device_info(x) container_of((x), \
1997 struct twl6030_bci_device_info, ac);
1999 static int twl6030_ac_get_property(struct power_supply *psy,
2000 enum power_supply_property psp,
2001 union power_supply_propval *val)
2003 struct twl6030_bci_device_info *di = to_twl6030_ac_device_info(psy);
2006 case POWER_SUPPLY_PROP_ONLINE:
2007 val->intval = di->ac_online;
2009 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2010 val->intval = twl6030_get_gpadc_conversion(di, 9) * 1000;
2019 #define to_twl6030_usb_device_info(x) container_of((x), \
2020 struct twl6030_bci_device_info, usb);
2022 static int twl6030_usb_get_property(struct power_supply *psy,
2023 enum power_supply_property psp,
2024 union power_supply_propval *val)
2026 struct twl6030_bci_device_info *di = to_twl6030_usb_device_info(psy);
2029 case POWER_SUPPLY_PROP_ONLINE:
2030 val->intval = di->usb_online;
2032 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2033 val->intval = twl6030_get_gpadc_conversion(di, 10) * 1000;
2042 #define to_twl6030_bk_bci_device_info(x) container_of((x), \
2043 struct twl6030_bci_device_info, bk_bat);
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)
2049 struct twl6030_bci_device_info *di = to_twl6030_bk_bci_device_info(psy);
2052 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2053 val->intval = di->bk_voltage_mV * 1000;
2062 static int twl6030_bci_battery_get_property(struct power_supply *psy,
2063 enum power_supply_property psp,
2064 union power_supply_propval *val)
2066 struct twl6030_bci_device_info *di;
2068 di = to_twl6030_bci_device_info(psy);
2071 case POWER_SUPPLY_PROP_STATUS:
2072 val->intval = di->charge_status;
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;
2079 case POWER_SUPPLY_PROP_CURRENT_NOW:
2080 twl6030battery_current(di);
2081 val->intval = di->current_uA;
2083 case POWER_SUPPLY_PROP_TEMP:
2084 val->intval = di->temp_C;
2086 case POWER_SUPPLY_PROP_ONLINE:
2087 val->intval = di->charger_source;
2089 case POWER_SUPPLY_PROP_CURRENT_AVG:
2090 val->intval = di->current_avg_uA;
2092 case POWER_SUPPLY_PROP_HEALTH:
2093 val->intval = di->bat_health;
2095 case POWER_SUPPLY_PROP_CAPACITY:
2096 val->intval = di->capacity;
2104 int twl6030_register_notifier(struct notifier_block *nb,
2105 unsigned int events)
2107 return blocking_notifier_chain_register(¬ifier_list, nb);
2109 EXPORT_SYMBOL_GPL(twl6030_register_notifier);
2111 int twl6030_unregister_notifier(struct notifier_block *nb,
2112 unsigned int events)
2114 return blocking_notifier_chain_unregister(¬ifier_list, nb);
2116 EXPORT_SYMBOL_GPL(twl6030_unregister_notifier);
2118 static void twl6030_usb_charger_work(struct work_struct *work)
2120 struct twl6030_bci_device_info *di =
2121 container_of(work, struct twl6030_bci_device_info, usb_work);
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;
2129 case USB_EVENT_VBUS:
2130 switch (di->usb_online) {
2131 case POWER_SUPPLY_TYPE_USB_CDP:
2133 * Only 500mA here or high speed chirp
2134 * handshaking may break
2136 di->charger_incurrentmA = 500;
2137 case POWER_SUPPLY_TYPE_USB:
2141 case USB_EVENT_NONE:
2143 di->charger_incurrentmA = 0;
2145 case USB_EVENT_ENUMERATED:
2146 if (di->usb_online == POWER_SUPPLY_TYPE_USB_CDP)
2147 di->charger_incurrentmA = 560;
2149 di->charger_incurrentmA = di->usb_max_power;
2154 twl6030_start_usb_charger(di);
2155 power_supply_changed(&di->usb);
2158 static int twl6030_usb_notifier_call(struct notifier_block *nb,
2159 unsigned long event, void *data)
2161 struct twl6030_bci_device_info *di =
2162 container_of(nb, struct twl6030_bci_device_info, nb);
2166 case USB_EVENT_VBUS:
2167 di->usb_online = *((unsigned int *) data);
2169 case USB_EVENT_ENUMERATED:
2170 di->usb_max_power = *((unsigned int *) data);
2172 case USB_EVENT_CHARGER:
2173 case USB_EVENT_NONE:
2180 schedule_work(&di->usb_work);
2185 static ssize_t set_fg_mode(struct device *dev,
2186 struct device_attribute *attr, const char *buf, size_t count)
2191 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2193 if ((strict_strtol(buf, 10, &val) < 0) || (val > 3))
2195 di->fuelgauge_mode = val;
2196 ret = twl_i2c_write_u8(TWL6030_MODULE_GASGAUGE, (val << 6) | CC_CAL_EN,
2200 twl6030_current_mode_changed(di);
2204 static ssize_t show_fg_mode(struct device *dev,
2205 struct device_attribute *attr, char *buf)
2208 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2210 val = di->fuelgauge_mode;
2211 return sprintf(buf, "%d\n", val);
2214 static ssize_t set_charge_src(struct device *dev,
2215 struct device_attribute *attr, const char *buf, size_t count)
2219 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2221 if ((strict_strtol(buf, 10, &val) < 0) || (val < 2) || (val > 3))
2223 di->vac_priority = val;
2227 static ssize_t show_charge_src(struct device *dev,
2228 struct device_attribute *attr, char *buf)
2231 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2233 val = di->vac_priority;
2234 return sprintf(buf, "%d\n", val);
2237 static ssize_t show_vbus_voltage(struct device *dev,
2238 struct device_attribute *attr, char *buf)
2241 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2243 val = twl6030_get_gpadc_conversion(di, 10);
2245 return sprintf(buf, "%d\n", val);
2248 static ssize_t show_id_level(struct device *dev, struct device_attribute *attr,
2252 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2254 val = twl6030_get_gpadc_conversion(di, 14);
2256 return sprintf(buf, "%d\n", val);
2259 static ssize_t set_watchdog(struct device *dev,
2260 struct device_attribute *attr, const char *buf, size_t count)
2265 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2267 if ((strict_strtol(buf, 10, &val) < 0) || (val < 1) || (val > 127))
2269 ret = twl6030_set_watchdog(di, val);
2276 static ssize_t show_watchdog(struct device *dev,
2277 struct device_attribute *attr, char *buf)
2280 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2282 val = di->watchdog_duration;
2283 return sprintf(buf, "%d\n", val);
2286 static ssize_t show_fg_counter(struct device *dev,
2287 struct device_attribute *attr, char *buf)
2292 ret = twl_i2c_read(TWL6030_MODULE_GASGAUGE, (u8 *) &fg_counter,
2296 return sprintf(buf, "%d\n", fg_counter);
2299 static ssize_t show_fg_accumulator(struct device *dev,
2300 struct device_attribute *attr, char *buf)
2305 ret = twl_i2c_read(TWL6030_MODULE_GASGAUGE, (u8 *) &fg_accum,
2310 return sprintf(buf, "%ld\n", fg_accum);
2313 static ssize_t show_fg_offset(struct device *dev,
2314 struct device_attribute *attr, char *buf)
2319 ret = twl_i2c_read(TWL6030_MODULE_GASGAUGE, (u8 *) &fg_offset,
2323 fg_offset = ((s16)(fg_offset << 6) >> 6);
2325 return sprintf(buf, "%d\n", fg_offset);
2328 static ssize_t set_fg_clear(struct device *dev, struct device_attribute *attr,
2329 const char *buf, size_t count)
2335 if ((strict_strtol(buf, 10, &val) < 0) || (val != 1))
2337 ret = twl_i2c_write_u8(TWL6030_MODULE_GASGAUGE, CC_AUTOCLEAR,
2345 static ssize_t set_fg_cal(struct device *dev, struct device_attribute *attr,
2346 const char *buf, size_t count)
2352 if ((strict_strtol(buf, 10, &val) < 0) || (val != 1))
2354 ret = twl_i2c_write_u8(TWL6030_MODULE_GASGAUGE, CC_CAL_EN, FG_REG_00);
2361 static ssize_t set_charging(struct device *dev, struct device_attribute *attr,
2362 const char *buf, size_t count)
2365 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
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);
2385 static ssize_t set_regulation_voltage(struct device *dev,
2386 struct device_attribute *attr, const char *buf, size_t count)
2390 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2392 if ((strict_strtol(buf, 10, &val) < 0) || (val < 3500)
2393 || (val > di->platform_data->max_charger_voltagemV))
2395 di->platform_data->max_bat_voltagemV = val;
2396 twl6030_config_voreg_reg(di, val);
2401 static ssize_t show_regulation_voltage(struct device *dev,
2402 struct device_attribute *attr, char *buf)
2405 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2407 val = di->platform_data->max_bat_voltagemV;
2408 return sprintf(buf, "%u\n", val);
2411 static ssize_t set_termination_current(struct device *dev,
2412 struct device_attribute *attr, const char *buf, size_t count)
2416 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2418 if ((strict_strtol(buf, 10, &val) < 0) || (val < 50) || (val > 400))
2420 di->platform_data->termination_currentmA = val;
2421 twl6030_config_iterm_reg(di, val);
2426 static ssize_t show_termination_current(struct device *dev,
2427 struct device_attribute *attr, char *buf)
2430 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2432 val = di->platform_data->termination_currentmA;
2433 return sprintf(buf, "%u\n", val);
2436 static ssize_t set_cin_limit(struct device *dev,
2437 struct device_attribute *attr, const char *buf, size_t count)
2441 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2443 if ((strict_strtol(buf, 10, &val) < 0) || (val < 50) || (val > 1500))
2445 di->charger_incurrentmA = val;
2446 twl6030_config_cinlimit_reg(di, val);
2451 static ssize_t show_cin_limit(struct device *dev, struct device_attribute *attr,
2455 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2457 val = di->charger_incurrentmA;
2458 return sprintf(buf, "%u\n", val);
2461 static ssize_t set_charge_current(struct device *dev,
2462 struct device_attribute *attr, const char *buf, size_t count)
2466 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2468 if ((strict_strtol(buf, 10, &val) < 0) || (val < 300)
2469 || (val > di->platform_data->max_charger_currentmA))
2471 di->charger_outcurrentmA = val;
2472 twl6030_config_vichrg_reg(di, val);
2477 static ssize_t show_charge_current(struct device *dev,
2478 struct device_attribute *attr, char *buf)
2481 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2483 val = di->charger_outcurrentmA;
2484 return sprintf(buf, "%u\n", val);
2487 static ssize_t set_min_vbus(struct device *dev, struct device_attribute *attr,
2488 const char *buf, size_t count)
2492 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2494 if ((strict_strtol(buf, 10, &val) < 0) || (val < 4200) || (val > 4760))
2497 twl6030_config_min_vbus_reg(di, val);
2502 static ssize_t show_min_vbus(struct device *dev, struct device_attribute *attr,
2506 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2509 return sprintf(buf, "%u\n", val);
2512 static ssize_t set_current_avg_interval(struct device *dev,
2513 struct device_attribute *attr, const char *buf, size_t count)
2517 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2519 if ((strict_strtol(buf, 10, &val) < 0) || (val < 10) || (val > 3600))
2521 di->current_avg_interval = val;
2522 twl6030_current_mode_changed(di);
2527 static ssize_t show_current_avg_interval(struct device *dev,
2528 struct device_attribute *attr, char *buf)
2531 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2533 val = di->current_avg_interval;
2534 return sprintf(buf, "%u\n", val);
2537 static ssize_t set_wakelock_enable(struct device *dev,
2538 struct device_attribute *attr, const char *buf, size_t count)
2542 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2544 if ((strict_strtol(buf, 10, &val) < 0) || (val < 0) || (val > 1))
2547 if ((val) && (di->charger_source == POWER_SUPPLY_TYPE_MAINS))
2548 wake_lock(&chrg_lock);
2550 wake_unlock(&chrg_lock);
2552 di->wakelock_enabled = val;
2556 static ssize_t show_wakelock_enable(struct device *dev,
2557 struct device_attribute *attr, char *buf)
2560 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2562 val = di->wakelock_enabled;
2563 return sprintf(buf, "%u\n", val);
2566 static ssize_t set_monitoring_interval(struct device *dev,
2567 struct device_attribute *attr, const char *buf, size_t count)
2571 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2573 if ((strict_strtol(buf, 10, &val) < 0) || (val < 10) || (val > 3600))
2575 di->monitoring_interval = val;
2576 twl6030_work_interval_changed(di);
2581 static ssize_t show_monitoring_interval(struct device *dev,
2582 struct device_attribute *attr, char *buf)
2585 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2587 val = di->monitoring_interval;
2588 return sprintf(buf, "%u\n", val);
2591 static ssize_t show_bsi(struct device *dev,
2592 struct device_attribute *attr, char *buf)
2595 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2597 val = twl6030_get_gpadc_conversion(di, 0);
2598 return sprintf(buf, "%d\n", val);
2601 static ssize_t show_stat1(struct device *dev,
2602 struct device_attribute *attr, char *buf)
2605 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2608 return sprintf(buf, "%u\n", val);
2611 static ssize_t show_status_int1(struct device *dev,
2612 struct device_attribute *attr, char *buf)
2615 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2617 val = di->status_int1;
2618 return sprintf(buf, "%u\n", val);
2621 static ssize_t show_status_int2(struct device *dev,
2622 struct device_attribute *attr, char *buf)
2625 struct twl6030_bci_device_info *di = dev_get_drvdata(dev);
2627 val = di->status_int2;
2628 return sprintf(buf, "%u\n", val);
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,
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,
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);
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,
2683 &dev_attr_stat1.attr,
2684 &dev_attr_status_int1.attr,
2685 &dev_attr_status_int2.attr,
2686 &dev_attr_wakelock_enable.attr,
2690 static const struct attribute_group twl6030_bci_attr_group = {
2691 .attrs = twl6030_bci_attributes,
2694 static char *twl6030_bci_supplied_to[] = {
2698 extern int dwc_vbus_status(void);
2700 static void twl6030_battery_update_status(struct twl6030_bci_device_info *di)
2702 power_supply_changed(&di->bat);
2704 static void twl6030_battery_work(struct work_struct *work)
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
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
2718 /* reschedule for the next time */
2719 queue_delayed_work(di->freezable_work, &di->work, di->interval);
2722 static int __devinit twl6030_bci_battery_probe(struct platform_device *pdev)
2724 struct twl4030_bci_platform_data *pdata = pdev->dev.platform_data;
2725 struct twl6030_bci_device_info *di;
2728 u8 controller_stat = 0;
2729 u8 chargerusb_ctrl1 = 0;
2732 printk("%s\n", __func__);
2734 dev_info(&pdev->dev, "platform_data not available\n");
2738 di = kzalloc(sizeof(*di), GFP_KERNEL);
2742 di->platform_data = kmemdup(pdata, sizeof(*pdata), GFP_KERNEL);
2743 if (!di->platform_data) {
2748 if (pdata->monitoring_interval == 0) {
2749 di->monitoring_interval = 10;
2750 di->current_avg_interval = 10;
2752 di->monitoring_interval = pdata->monitoring_interval;
2753 di->current_avg_interval = pdata->monitoring_interval;
2756 di->platform_data = pdata;
2757 di->features = pdata->features;
2758 di->dev = &pdev->dev;
2760 if (di->features & TWL6032_SUBCLASS) {
2761 ret = twl_i2c_read_u8(TWL_MODULE_RTC, ®, CHARGER_MODE_REG);
2763 goto temp_setup_fail;
2765 if (reg & CHARGER_MODE_POWERPATH) {
2766 dev_dbg(di->dev, "Charger: PowerPath\n");
2767 di->use_power_path = 1;
2769 dev_dbg(di->dev, "Charger: NON PowerPath\n");
2770 di->use_power_path = 0;
2773 if (reg & CHARGER_MODE_AUTOCHARGE) {
2774 dev_dbg(di->dev, "Charger: AutoCharge\n");
2775 di->use_hw_charger = 1;
2777 dev_dbg(di->dev, "Charger: NON AutoCharge\n");
2778 di->use_hw_charger = 0;
2781 di->use_power_path = 0;
2782 di->use_hw_charger = 0;
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);
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;
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;
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;
2819 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
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;
2827 di->vac_priority = 2;
2829 di->capacity_debounce_count = 0;
2830 di->ac_next_refresh = jiffies - 1;
2831 platform_set_drvdata(pdev, di);
2833 /* calculate current max scale from sense */
2834 if (pdata->sense_resistor_mohm) {
2835 di->current_max_scale = (62000) / pdata->sense_resistor_mohm;
2837 /* Set sensible defaults if platform data is missing */
2838 if (di->features & TWL6032_SUBCLASS)
2839 di->current_max_scale = 3100;
2841 di->current_max_scale = 6200;
2844 wake_lock_init(&chrg_lock, WAKE_LOCK_SUSPEND, "ac_chrg_wake_lock");
2845 /* settings for temperature sensing */
2846 ret = twl6030battery_temp_setup(true);
2848 goto temp_setup_fail;
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);
2857 ret = request_threaded_irq(irq, NULL,
2858 twl6032charger_fault_interrupt_hw,
2859 0, "twl_bci_fault", di);
2862 dev_info(&pdev->dev, "could not request irq %d, status %d\n",
2864 goto temp_setup_fail;
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);
2874 ret = request_threaded_irq(irq, NULL,
2875 twl6032charger_ctrl_interrupt_hw,
2876 0, "twl_bci_ctrl", di);
2879 dev_info(&pdev->dev, "could not request irq %d, status %d\n",
2884 ret = power_supply_register(&pdev->dev, &di->bat);
2886 dev_info(&pdev->dev, "failed to register main battery\n");
2890 ret = power_supply_register(&pdev->dev, &di->usb);
2892 dev_info(&pdev->dev, "failed to register usb power supply\n");
2896 ret = power_supply_register(&pdev->dev, &di->ac);
2898 dev_info(&pdev->dev, "failed to register ac power supply\n");
2902 ret = power_supply_register(&pdev->dev, &di->bk_bat);
2904 dev_info(&pdev->dev, "failed to register backup battery\n");
2905 goto bk_batt_failed;
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);
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);
2920 ret = twl6030battery_voltage_setup(di);
2922 dev_info(&pdev->dev, "voltage measurement setup failed\n");
2924 ret = twl6030battery_current_setup(true);
2926 dev_info(&pdev->dev, "current measurement setup failed\n");
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);
2934 ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, MBAT_TEMP,
2935 CONTROLLER_INT_MASK);
2937 goto bk_batt_failed;
2939 ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, MASK_MCHARGERUSB_THMREG,
2940 CHARGERUSB_INT_MASK);
2942 goto bk_batt_failed;
2944 ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &controller_stat,
2947 goto bk_batt_failed;
2949 di->stat1 = controller_stat;
2950 di->charger_outcurrentmA = di->platform_data->max_charger_currentmA;
2952 twl6030_set_watchdog(di, 32);
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();
2958 ret = otg_register_notifier(di->otg, &di->nb);
2960 dev_err(&pdev->dev, "otg register notifier"
2961 " failed %d\n", ret);
2963 dev_err(&pdev->dev, "otg_get_transceiver failed %d\n", ret);
2965 if (di->features & TWL6032_SUBCLASS) {
2966 di->charger_incurrentmA = 1000;
2967 di->gpadc_vbat_chnl = TWL6032_GPADC_VBAT_CHNL;
2969 di->charger_incurrentmA = twl6030_get_usb_max_power(di->otg);
2970 di->gpadc_vbat_chnl = TWL6030_GPADC_VBAT_CHNL;
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",
2977 ret = twl_i2c_read_u8(TWL6030_MODULE_ID0, &hw_state, STS_HW_CONDITIONS);
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);
2986 goto bk_batt_failed;
2988 chargerusb_ctrl1 |= HZ_MODE;
2989 ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER,
2990 chargerusb_ctrl1, CHARGERUSB_CTRL1);
2992 goto bk_batt_failed;
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) {
3000 * In HOST mode (ID GROUND) with a device connected,
3001 * do no enable usb charging
3003 if (!(hw_state & STS_USB_ID)) {
3004 di->usb_online = POWER_SUPPLY_TYPE_USB;
3005 di->charger_source = POWER_SUPPLY_TYPE_USB;
3007 POWER_SUPPLY_STATUS_CHARGING;
3008 di->event = USB_EVENT_VBUS;
3009 schedule_work(&di->usb_work);
3013 int fault, charge_usb, charge_ac;
3015 twl_i2c_read_u8(TWL6032_MODULE_CHARGER, ®,
3016 CHARGERUSB_INT_STATUS);
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);
3025 dev_dbg(di->dev, "boot charge state fault %d, usb %d, ac %d\n",
3026 fault, charge_usb, charge_ac);
3028 if (fault && (charge_usb || charge_ac))
3029 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
3031 if (di->stat1 & (VBUS_DET | VAC_DET))
3033 POWER_SUPPLY_STATUS_NOT_CHARGING;
3036 POWER_SUPPLY_STATUS_DISCHARGING;
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);
3044 ret = twl6030backupbatt_setup();
3046 dev_info(&pdev->dev, "Backup Bat charging setup failed\n");
3048 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
3049 REG_INT_MSK_LINE_C);
3050 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
3052 twl6030_interrupt_unmask(TWL6030_CHARGER_FAULT_INT_MASK,
3053 REG_INT_MSK_LINE_C);
3054 twl6030_interrupt_unmask(TWL6030_CHARGER_FAULT_INT_MASK,
3057 ret = sysfs_create_group(&pdev->dev.kobj, &twl6030_bci_attr_group);
3059 dev_info(&pdev->dev, "could not create sysfs files\n");
3064 cancel_delayed_work(&di->twl6030_bci_monitor_work);
3065 power_supply_unregister(&di->ac);
3067 power_supply_unregister(&di->usb);
3069 power_supply_unregister(&di->bat);
3073 irq = platform_get_irq(pdev, 1);
3076 wake_lock_destroy(&chrg_lock);
3077 platform_set_drvdata(pdev, NULL);
3083 static int __devexit twl6030_bci_battery_remove(struct platform_device *pdev)
3085 struct twl6030_bci_device_info *di = platform_get_drvdata(pdev);
3088 twl6030_interrupt_mask(TWL6030_CHARGER_CTRL_INT_MASK,
3089 REG_INT_MSK_LINE_C);
3090 twl6030_interrupt_mask(TWL6030_CHARGER_CTRL_INT_MASK,
3092 twl6030_interrupt_mask(TWL6030_CHARGER_FAULT_INT_MASK,
3093 REG_INT_MSK_LINE_C);
3094 twl6030_interrupt_mask(TWL6030_CHARGER_FAULT_INT_MASK,
3097 irq = platform_get_irq(pdev, 0);
3100 irq = platform_get_irq(pdev, 1);
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);
3121 static int twl6030_bci_battery_suspend(struct device *dev)
3123 // struct platform_device *pdev = to_platform_device(dev);
3124 // struct twl6030_bci_device_info *di = platform_get_drvdata(pdev);
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);
3135 rd_reg |= MVAC_FAULT;
3136 ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, MBAT_TEMP,
3137 CONTROLLER_INT_MASK);
3141 //cancel_delayed_work(&di->work);
3142 //cancel_delayed_work(&di->twl6030_bci_monitor_work);
3143 //cancel_delayed_work(&di->twl6030_current_avg_work);
3145 /* We cannot tolarate a sleep longer than 30 seconds
3146 * while on ac charging we have to reset the BQ watchdog timer.
3148 // if ((di->charger_source == POWER_SUPPLY_TYPE_MAINS) &&
3149 // ((wakeup_timer_seconds > 25) || !wakeup_timer_seconds)) {
3150 // wakeup_timer_seconds = 25;
3153 /*reset the BQ watch dog*/
3154 events = BQ2415x_RESET_TIMER;
3155 blocking_notifier_call_chain(¬ifier_list, events, NULL);
3157 ret = twl6030battery_temp_setup(false);
3159 pr_err("%s: Temp measurement setup failed (%d)!\n",
3164 ret = twl6030battery_current_setup(false);
3166 pr_err("%s: Current measurement setup failed (%d)!\n",
3173 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
3177 static int twl6030_bci_battery_resume(struct device *dev)
3179 // struct platform_device *pdev = to_platform_device(dev);
3180 // struct twl6030_bci_device_info *di = platform_get_drvdata(pdev);
3185 ret = twl6030battery_temp_setup(true);
3187 pr_err("%s: Temp measurement setup failed (%d)!\n",
3192 ret = twl6030battery_current_setup(true);
3194 pr_err("%s: Current measurement setup failed (%d)!\n",
3199 ret = twl_i2c_read_u8(TWL6030_MODULE_CHARGER, &rd_reg, CONTROLLER_INT_MASK);
3203 rd_reg &= ~(0xFF & MVAC_FAULT);
3204 ret = twl_i2c_write_u8(TWL6030_MODULE_CHARGER, MBAT_TEMP,
3205 CONTROLLER_INT_MASK);
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);
3213 events = BQ2415x_RESET_TIMER;
3214 blocking_notifier_call_chain(¬ifier_list, events, NULL);
3218 pr_err("%s: Error access to TWL6030 (%d)\n", __func__, ret);
3222 #define twl6030_bci_battery_suspend NULL
3223 #define twl6030_bci_battery_resume NULL
3224 #endif /* CONFIG_PM */
3226 static const struct dev_pm_ops pm_ops = {
3227 .suspend = twl6030_bci_battery_suspend,
3228 .resume = twl6030_bci_battery_resume,
3231 static struct platform_driver twl6030_bci_battery_driver = {
3232 .probe = twl6030_bci_battery_probe,
3233 .remove = __devexit_p(twl6030_bci_battery_remove),
3235 .name = "twl6030_bci",
3240 static int __init twl6030_battery_init(void)
3242 return platform_driver_register(&twl6030_bci_battery_driver);
3244 module_init(twl6030_battery_init);
3246 static void __exit twl6030_battery_exit(void)
3248 platform_driver_unregister(&twl6030_bci_battery_driver);
3250 module_exit(twl6030_battery_exit);
3252 MODULE_LICENSE("GPL");
3253 MODULE_ALIAS("platform:twl6030_bci");
3254 MODULE_AUTHOR("Texas Instruments Inc");