2 * linux/drivers/power/twl4030_bci_battery.c
4 * OMAP2430/3430 BCI battery driver for Linux
6 * Copyright (C) 2008 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/i2c/twl4030.h>
25 #include <linux/power_supply.h>
26 #include <linux/i2c/twl4030-madc.h>
28 #define T2_BATTERY_VOLT 0x04
29 #define T2_BATTERY_TEMP 0x06
30 #define T2_BATTERY_CUR 0x08
32 /* charger constants */
34 #define AC_PW_CONN 0x01
35 #define USB_PW_CONN 0x02
37 /* TWL4030_MODULE_USB */
38 #define REG_POWER_CTRL 0x0AC
40 #define REG_PHY_CLK_CTRL 0x0FE
41 #define REG_PHY_CLK_CTRL_STS 0x0FF
42 #define PHY_DPLL_CLK 0x01
44 #define REG_BCICTL1 0x023
45 #define REG_BCICTL2 0x024
50 /* Boot BCI flag bits */
51 #define BCIAUTOWEN 0x020
52 #define CONFIG_DONE 0x010
53 #define BCIAUTOUSB 0x002
54 #define BCIAUTOAC 0x001
55 #define BCIMSTAT_MASK 0x03F
57 /* Boot BCI register */
58 #define REG_BOOT_BCI 0x007
59 #define REG_CTRL1 0x00
60 #define REG_SW1SELECT_MSB 0x07
61 #define SW1_CH9_SEL 0x02
62 #define REG_CTRL_SW1 0x012
63 #define SW1_TRIGGER 0x020
65 #define REG_GPCH9 0x049
66 #define REG_STS_HW_CONDITIONS 0x0F
67 #define STS_VBUS 0x080
69 #define REG_BCIMSTATEC 0x02
70 #define REG_BCIMFSTS4 0x010
71 #define REG_BCIMFSTS2 0x00E
72 #define REG_BCIMFSTS3 0x00F
73 #define REG_BCIMFSTS1 0x001
74 #define USBFASTMCHG 0x004
75 #define BATSTSPCHG 0x004
76 #define BATSTSMCHG 0x040
81 #define REG_BB_CFG 0x012
84 /* Power supply charge interrupt */
85 #define REG_PWR_ISR1 0x00
86 #define REG_PWR_IMR1 0x01
87 #define REG_PWR_EDR1 0x05
88 #define REG_PWR_SIH_CTRL 0x007
90 #define USB_PRES 0x004
91 #define CHG_PRES 0x002
93 #define USB_PRES_RISING 0x020
94 #define USB_PRES_FALLING 0x010
95 #define CHG_PRES_RISING 0x008
96 #define CHG_PRES_FALLING 0x004
97 #define AC_STATEC 0x20
100 /* interrupt status registers */
101 #define REG_BCIISR1A 0x0
102 #define REG_BCIISR2A 0x01
104 /* Interrupt flags bits BCIISR1 */
105 #define BATSTS_ISR1 0x080
106 #define VBATLVL_ISR1 0x001
108 /* Interrupt mask registers for int1*/
109 #define REG_BCIIMR1A 0x002
110 #define REG_BCIIMR2A 0x003
112 /* Interrupt masks for BCIIMR1 */
113 #define BATSTS_IMR1 0x080
114 #define VBATLVL_IMR1 0x001
116 /* Interrupt edge detection register */
117 #define REG_BCIEDR1 0x00A
118 #define REG_BCIEDR2 0x00B
119 #define REG_BCIEDR3 0x00C
122 #define BATSTS_EDRRISIN 0x080
123 #define BATSTS_EDRFALLING 0x040
126 #define VBATLVL_EDRRISIN 0x02
128 /* Step size and prescaler ratio */
129 #define TEMP_STEP_SIZE 147
130 #define TEMP_PSR_R 100
132 #define VOLT_STEP_SIZE 588
133 #define VOLT_PSR_R 100
135 #define CURR_STEP_SIZE 147
136 #define CURR_PSR_R1 44
137 #define CURR_PSR_R2 80
139 #define BK_VOLT_STEP_SIZE 441
140 #define BK_VOLT_PSR_R 100
145 /* Ptr to thermistor table */
148 struct twl4030_bci_device_info {
151 unsigned long update_time;
159 struct power_supply bat;
160 struct power_supply bk_bat;
161 struct delayed_work twl4030_bci_monitor_work;
162 struct delayed_work twl4030_bk_bci_monitor_work;
165 static int usb_charger_flag;
166 static int LVL_1, LVL_2, LVL_3, LVL_4;
168 static int read_bci_val(u8 reg_1);
169 static inline int clear_n_set(u8 mod_no, u8 clear, u8 set, u8 reg);
170 static int twl4030charger_presence(void);
173 * Report and clear the charger presence event.
175 static inline int twl4030charger_presence_evt(void)
178 u8 chg_sts, set = 0, clear = 0;
180 /* read charger power supply status */
181 ret = twl4030_i2c_read_u8(TWL4030_MODULE_PM_MASTER, &chg_sts,
182 REG_STS_HW_CONDITIONS);
186 if (chg_sts & STS_CHG) { /* If the AC charger have been connected */
187 /* configuring falling edge detection for CHG_PRES */
188 set = CHG_PRES_FALLING;
189 clear = CHG_PRES_RISING;
190 } else { /* If the AC charger have been disconnected */
191 /* configuring rising edge detection for CHG_PRES */
192 set = CHG_PRES_RISING;
193 clear = CHG_PRES_FALLING;
196 /* Update the interrupt edge detection register */
197 clear_n_set(TWL4030_MODULE_INT, clear, set, REG_PWR_EDR1);
203 * Interrupt service routine
205 * Attends to TWL 4030 power module interruptions events, specifically
206 * USB_PRES (USB charger presence) CHG_PRES (AC charger presence) events
209 static irqreturn_t twl4030charger_interrupt(int irq, void *_di)
211 struct twl4030_bci_device_info *di = _di;
213 #ifdef CONFIG_LOCKDEP
214 /* WORKAROUND for lockdep forcing IRQF_DISABLED on us, which
215 * we don't want and can't tolerate. Although it might be
216 * friendlier not to borrow this thread context...
221 twl4030charger_presence_evt();
222 power_supply_changed(&di->bat);
228 * This function handles the twl4030 battery presence interrupt
230 static int twl4030battery_presence_evt(void)
233 u8 batstsmchg, batstspchg;
235 /* check for the battery presence in main charge*/
236 ret = twl4030_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE,
237 &batstsmchg, REG_BCIMFSTS3);
241 /* check for the battery presence in precharge */
242 ret = twl4030_i2c_read_u8(TWL4030_MODULE_PRECHARGE,
243 &batstspchg, REG_BCIMFSTS1);
248 * REVISIT: Physically inserting/removing the batt
249 * does not seem to generate an int on 3430ES2 SDP.
251 if ((batstspchg & BATSTSPCHG) || (batstsmchg & BATSTSMCHG)) {
252 /* In case of the battery insertion event */
253 ret = clear_n_set(TWL4030_MODULE_INTERRUPTS, BATSTS_EDRRISIN,
254 BATSTS_EDRFALLING, REG_BCIEDR2);
258 /* In case of the battery removal event */
259 ret = clear_n_set(TWL4030_MODULE_INTERRUPTS, BATSTS_EDRFALLING,
260 BATSTS_EDRRISIN, REG_BCIEDR2);
269 * This function handles the twl4030 battery voltage level interrupt.
271 static int twl4030battery_level_evt(void)
276 /* checking for threshold event */
277 ret = twl4030_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE,
278 &mfst, REG_BCIMFSTS2);
282 /* REVISIT could use a bitmap */
283 if (mfst & VBATOV4) {
288 } else if (mfst & VBATOV3) {
293 } else if (mfst & VBATOV2) {
309 * Interrupt service routine
311 * Attends to BCI interruptions events,
312 * specifically BATSTS (battery connection and removal)
313 * VBATOV (main battery voltage threshold) events
316 static irqreturn_t twl4030battery_interrupt(int irq, void *_di)
318 u8 isr1a_val, isr2a_val, clear_2a, clear_1a;
321 #ifdef CONFIG_LOCKDEP
322 /* WORKAROUND for lockdep forcing IRQF_DISABLED on us, which
323 * we don't want and can't tolerate. Although it might be
324 * friendlier not to borrow this thread context...
329 ret = twl4030_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &isr1a_val,
334 ret = twl4030_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &isr2a_val,
339 clear_2a = (isr2a_val & VBATLVL_ISR1) ? (VBATLVL_ISR1) : 0;
340 clear_1a = (isr1a_val & BATSTS_ISR1) ? (BATSTS_ISR1) : 0;
342 /* cleaning BCI interrupt status flags */
343 ret = twl4030_i2c_write_u8(TWL4030_MODULE_INTERRUPTS,
344 clear_1a , REG_BCIISR1A);
348 ret = twl4030_i2c_write_u8(TWL4030_MODULE_INTERRUPTS,
349 clear_2a , REG_BCIISR2A);
353 /* battery connetion or removal event */
354 if (isr1a_val & BATSTS_ISR1)
355 twl4030battery_presence_evt();
356 /* battery voltage threshold event*/
357 else if (isr2a_val & VBATLVL_ISR1)
358 twl4030battery_level_evt();
366 * Enable/Disable hardware battery level event notifications.
368 static int twl4030battery_hw_level_en(int enable)
373 /* unmask VBATOV interrupt for INT1 */
374 ret = clear_n_set(TWL4030_MODULE_INTERRUPTS, VBATLVL_IMR1,
379 /* configuring interrupt edge detection for VBATOv */
380 ret = clear_n_set(TWL4030_MODULE_INTERRUPTS, 0,
381 VBATLVL_EDRRISIN, REG_BCIEDR3);
385 /* mask VBATOV interrupt for INT1 */
386 ret = clear_n_set(TWL4030_MODULE_INTERRUPTS, 0,
387 VBATLVL_IMR1, REG_BCIIMR2A);
396 * Enable/disable hardware battery presence event notifications.
398 static int twl4030battery_hw_presence_en(int enable)
403 /* unmask BATSTS interrupt for INT1 */
404 ret = clear_n_set(TWL4030_MODULE_INTERRUPTS, BATSTS_IMR1,
409 /* configuring interrupt edge for BATSTS */
410 ret = clear_n_set(TWL4030_MODULE_INTERRUPTS, 0,
411 BATSTS_EDRRISIN | BATSTS_EDRFALLING, REG_BCIEDR2);
415 /* mask BATSTS interrupt for INT1 */
416 ret = clear_n_set(TWL4030_MODULE_INTERRUPTS, 0,
417 BATSTS_IMR1, REG_BCIIMR1A);
426 * Enable/Disable AC Charge funtionality.
428 static int twl4030charger_ac_en(int enable)
433 /* forcing the field BCIAUTOAC (BOOT_BCI[0) to 1 */
434 ret = clear_n_set(TWL4030_MODULE_PM_MASTER, 0,
435 (CONFIG_DONE | BCIAUTOWEN | BCIAUTOAC),
440 /* forcing the field BCIAUTOAC (BOOT_BCI[0) to 0*/
441 ret = clear_n_set(TWL4030_MODULE_PM_MASTER, BCIAUTOAC,
442 (CONFIG_DONE | BCIAUTOWEN),
452 * Enable/Disable USB Charge funtionality.
454 int twl4030charger_usb_en(int enable)
458 unsigned long timeout;
461 /* Check for USB charger conneted */
462 ret = twl4030charger_presence();
466 if (!(ret & USB_PW_CONN))
469 /* forcing the field BCIAUTOUSB (BOOT_BCI[1]) to 1 */
470 ret = clear_n_set(TWL4030_MODULE_PM_MASTER, 0,
471 (CONFIG_DONE | BCIAUTOWEN | BCIAUTOUSB),
476 ret = clear_n_set(TWL4030_MODULE_USB, 0, PHY_DPLL_CLK,
482 timeout = jiffies + msecs_to_jiffies(50);
484 while ((!(value & PHY_DPLL_CLK)) &&
485 time_before(jiffies, timeout)) {
487 ret = twl4030_i2c_read_u8(TWL4030_MODULE_USB, &value,
488 REG_PHY_CLK_CTRL_STS);
493 /* OTG_EN (POWER_CTRL[5]) to 1 */
494 ret = clear_n_set(TWL4030_MODULE_USB, 0, OTG_EN,
501 /* forcing USBFASTMCHG(BCIMFSTS4[2]) to 1 */
502 ret = clear_n_set(TWL4030_MODULE_MAIN_CHARGE, 0,
503 USBFASTMCHG, REG_BCIMFSTS4);
507 twl4030charger_presence();
508 ret = clear_n_set(TWL4030_MODULE_PM_MASTER, BCIAUTOUSB,
509 (CONFIG_DONE | BCIAUTOWEN), REG_BOOT_BCI);
518 * Return battery temperature
521 static int twl4030battery_temperature(void)
524 int temp, curr, volt, res, ret;
526 /* Getting and calculating the thermistor voltage */
527 ret = read_bci_val(T2_BATTERY_TEMP);
531 volt = (ret * TEMP_STEP_SIZE) / TEMP_PSR_R;
533 /* Getting and calculating the supply current in micro ampers */
534 ret = twl4030_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE, &val,
539 curr = ((val & ITHSENS) + 1) * 10;
541 /* Getting and calculating the thermistor resistance in ohms*/
542 res = volt * 1000 / curr;
544 /*calculating temperature*/
545 for (temp = 58; temp >= 0; temp--) {
546 int actual = therm_tbl[temp];
547 if ((actual - res) >= 0)
551 /* Negative temperature */
565 * Return battery voltage
568 static int twl4030battery_voltage(void)
570 int volt = read_bci_val(T2_BATTERY_VOLT);
572 return (volt * VOLT_STEP_SIZE) / VOLT_PSR_R;
576 * Return the battery current
579 static int twl4030battery_current(void)
581 int ret, curr = read_bci_val(T2_BATTERY_CUR);
584 ret = twl4030_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE, &val,
589 if (val & CGAIN) /* slope of 0.44 mV/mA */
590 return (curr * CURR_STEP_SIZE) / CURR_PSR_R1;
591 else /* slope of 0.88 mV/mA */
592 return (curr * CURR_STEP_SIZE) / CURR_PSR_R2;
596 * Return the battery backup voltage
599 static int twl4030backupbatt_voltage(void)
601 struct twl4030_madc_request req;
604 req.channels = (1 << 9);
606 req.method = TWL4030_MADC_SW1;
609 twl4030_madc_conversion(&req);
610 temp = (u16)req.rbuf[9];
612 return (temp * BK_VOLT_STEP_SIZE) / BK_VOLT_PSR_R;
616 * Returns an integer value, that means,
617 * NO_PW_CONN no power supply is connected
618 * AC_PW_CONN if the AC power supply is connected
619 * USB_PW_CONN if the USB power supply is connected
620 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
624 static int twl4030charger_presence(void)
629 ret = twl4030_i2c_read_u8(TWL4030_MODULE_PM_MASTER, &hwsts,
630 REG_STS_HW_CONDITIONS);
632 pr_err("twl4030_bci: error reading STS_HW_CONDITIONS\n");
636 ret = (hwsts & STS_CHG) ? AC_PW_CONN : NO_PW_CONN;
637 ret += (hwsts & STS_VBUS) ? USB_PW_CONN : NO_PW_CONN;
639 if (ret & USB_PW_CONN)
640 usb_charger_flag = 1;
642 usb_charger_flag = 0;
649 * Returns the main charge FSM status
652 static int twl4030bci_status(void)
657 ret = twl4030_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE,
658 &status, REG_BCIMSTATEC);
660 pr_err("twl4030_bci: error reading BCIMSTATEC\n");
664 return (int) (status & BCIMSTAT_MASK);
667 static int read_bci_val(u8 reg)
673 ret = twl4030_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE, &val,
678 temp = ((int)(val & 0x03)) << 8;
681 ret = twl4030_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE, &val,
690 * Settup the twl4030 BCI module to enable backup
693 static int twl4030backupbatt_voltage_setup(void)
697 /* Starting backup batery charge */
698 ret = clear_n_set(TWL4030_MODULE_PM_RECEIVER, 0, BBCHEN,
707 * Settup the twl4030 BCI module to measure battery
710 static int twl4030battery_temp_setup(void)
714 /* Enabling thermistor current */
715 ret = clear_n_set(TWL4030_MODULE_MAIN_CHARGE, 0, ITHEN,
724 * Sets and clears bits on an given register on a given module
726 static inline int clear_n_set(u8 mod_no, u8 clear, u8 set, u8 reg)
731 /* Gets the initial register value */
732 ret = twl4030_i2c_read_u8(mod_no, &val, reg);
736 /* Clearing all those bits to clear */
739 /* Setting all those bits to set */
742 /* Update the register */
743 ret = twl4030_i2c_write_u8(mod_no, val, reg);
750 static enum power_supply_property twl4030_bci_battery_props[] = {
751 POWER_SUPPLY_PROP_STATUS,
752 POWER_SUPPLY_PROP_ONLINE,
753 POWER_SUPPLY_PROP_VOLTAGE_NOW,
754 POWER_SUPPLY_PROP_CURRENT_NOW,
755 POWER_SUPPLY_PROP_CAPACITY,
756 POWER_SUPPLY_PROP_TEMP,
759 static enum power_supply_property twl4030_bk_bci_battery_props[] = {
760 POWER_SUPPLY_PROP_VOLTAGE_NOW,
764 twl4030_bk_bci_battery_read_status(struct twl4030_bci_device_info *di)
766 di->bk_voltage_uV = twl4030backupbatt_voltage();
769 static void twl4030_bk_bci_battery_work(struct work_struct *work)
771 struct twl4030_bci_device_info *di = container_of(work,
772 struct twl4030_bci_device_info,
773 twl4030_bk_bci_monitor_work.work);
775 twl4030_bk_bci_battery_read_status(di);
776 schedule_delayed_work(&di->twl4030_bk_bci_monitor_work, 500);
779 static void twl4030_bci_battery_read_status(struct twl4030_bci_device_info *di)
781 di->temp_C = twl4030battery_temperature();
782 di->voltage_uV = twl4030battery_voltage();
783 di->current_uA = twl4030battery_current();
787 twl4030_bci_battery_update_status(struct twl4030_bci_device_info *di)
789 twl4030_bci_battery_read_status(di);
790 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
792 if (power_supply_am_i_supplied(&di->bat))
793 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
795 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
798 static void twl4030_bci_battery_work(struct work_struct *work)
800 struct twl4030_bci_device_info *di = container_of(work,
801 struct twl4030_bci_device_info, twl4030_bci_monitor_work.work);
803 twl4030_bci_battery_update_status(di);
804 schedule_delayed_work(&di->twl4030_bci_monitor_work, 100);
808 #define to_twl4030_bci_device_info(x) container_of((x), \
809 struct twl4030_bci_device_info, bat);
811 static void twl4030_bci_battery_external_power_changed(struct power_supply *psy)
813 struct twl4030_bci_device_info *di = to_twl4030_bci_device_info(psy);
815 cancel_delayed_work(&di->twl4030_bci_monitor_work);
816 schedule_delayed_work(&di->twl4030_bci_monitor_work, 0);
819 #define to_twl4030_bk_bci_device_info(x) container_of((x), \
820 struct twl4030_bci_device_info, bk_bat);
822 static int twl4030_bk_bci_battery_get_property(struct power_supply *psy,
823 enum power_supply_property psp,
824 union power_supply_propval *val)
826 struct twl4030_bci_device_info *di = to_twl4030_bk_bci_device_info(psy);
829 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
830 val->intval = di->bk_voltage_uV;
839 static int twl4030_bci_battery_get_property(struct power_supply *psy,
840 enum power_supply_property psp,
841 union power_supply_propval *val)
843 struct twl4030_bci_device_info *di;
846 di = to_twl4030_bci_device_info(psy);
849 case POWER_SUPPLY_PROP_STATUS:
850 val->intval = di->charge_status;
857 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
858 val->intval = di->voltage_uV;
860 case POWER_SUPPLY_PROP_CURRENT_NOW:
861 val->intval = di->current_uA;
863 case POWER_SUPPLY_PROP_TEMP:
864 val->intval = di->temp_C;
866 case POWER_SUPPLY_PROP_ONLINE:
867 status = twl4030bci_status();
868 if ((status & AC_STATEC) == AC_STATEC)
869 val->intval = POWER_SUPPLY_TYPE_MAINS;
870 else if (usb_charger_flag)
871 val->intval = POWER_SUPPLY_TYPE_USB;
875 case POWER_SUPPLY_PROP_CAPACITY:
877 * need to get the correct percentage value per the
878 * battery characteristics. Approx values for now.
880 if (di->voltage_uV < 2894 || LVL_1) {
883 } else if ((di->voltage_uV < 3451 && di->voltage_uV > 2894)
887 } else if ((di->voltage_uV < 3902 && di->voltage_uV > 3451)
891 } else if ((di->voltage_uV < 3949 && di->voltage_uV > 3902)
895 } else if (di->voltage_uV > 3949)
904 static char *twl4030_bci_supplied_to[] = {
905 "twl4030_bci_battery",
908 static int __init twl4030_bci_battery_probe(struct platform_device *pdev)
910 struct twl4030_bci_platform_data *pdata = pdev->dev.platform_data;
911 struct twl4030_bci_device_info *di;
915 therm_tbl = pdata->battery_tmp_tbl;
917 di = kzalloc(sizeof(*di), GFP_KERNEL);
921 di->dev = &pdev->dev;
922 di->bat.name = "twl4030_bci_battery";
923 di->bat.supplied_to = twl4030_bci_supplied_to;
924 di->bat.num_supplicants = ARRAY_SIZE(twl4030_bci_supplied_to);
925 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
926 di->bat.properties = twl4030_bci_battery_props;
927 di->bat.num_properties = ARRAY_SIZE(twl4030_bci_battery_props);
928 di->bat.get_property = twl4030_bci_battery_get_property;
929 di->bat.external_power_changed =
930 twl4030_bci_battery_external_power_changed;
932 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
934 di->bk_bat.name = "twl4030_bci_bk_battery";
935 di->bk_bat.type = POWER_SUPPLY_TYPE_BATTERY;
936 di->bk_bat.properties = twl4030_bk_bci_battery_props;
937 di->bk_bat.num_properties = ARRAY_SIZE(twl4030_bk_bci_battery_props);
938 di->bk_bat.get_property = twl4030_bk_bci_battery_get_property;
939 di->bk_bat.external_power_changed = NULL;
941 twl4030charger_ac_en(ENABLE);
942 twl4030charger_usb_en(ENABLE);
943 twl4030battery_hw_level_en(ENABLE);
944 twl4030battery_hw_presence_en(ENABLE);
946 platform_set_drvdata(pdev, di);
948 /* settings for temperature sensing */
949 ret = twl4030battery_temp_setup();
951 goto temp_setup_fail;
953 /* enabling GPCH09 for read back battery voltage */
954 ret = twl4030backupbatt_voltage_setup();
956 goto voltage_setup_fail;
958 /* REVISIT do we need to request both IRQs ?? */
960 /* request BCI interruption */
961 ret = request_irq(TWL4030_MODIRQ_BCI, twl4030battery_interrupt,
962 0, pdev->name, NULL);
964 dev_dbg(&pdev->dev, "could not request irq %d, status %d\n",
965 TWL4030_MODIRQ_BCI, ret);
969 irq = platform_get_irq(pdev, 0);
971 /* request Power interruption */
972 ret = request_irq(irq, twl4030charger_interrupt,
976 dev_dbg(&pdev->dev, "could not request irq %d, status %d\n",
981 ret = power_supply_register(&pdev->dev, &di->bat);
983 dev_dbg(&pdev->dev, "failed to register main battery\n");
987 INIT_DELAYED_WORK_DEFERRABLE(&di->twl4030_bci_monitor_work,
988 twl4030_bci_battery_work);
989 schedule_delayed_work(&di->twl4030_bci_monitor_work, 0);
991 ret = power_supply_register(&pdev->dev, &di->bk_bat);
993 dev_dbg(&pdev->dev, "failed to register backup battery\n");
997 INIT_DELAYED_WORK_DEFERRABLE(&di->twl4030_bk_bci_monitor_work,
998 twl4030_bk_bci_battery_work);
999 schedule_delayed_work(&di->twl4030_bk_bci_monitor_work, 500);
1004 power_supply_unregister(&di->bat);
1008 free_irq(TWL4030_MODIRQ_BCI, NULL);
1012 twl4030charger_ac_en(DISABLE);
1013 twl4030charger_usb_en(DISABLE);
1014 twl4030battery_hw_level_en(DISABLE);
1015 twl4030battery_hw_presence_en(DISABLE);
1021 static int __exit twl4030_bci_battery_remove(struct platform_device *pdev)
1023 struct twl4030_bci_device_info *di = platform_get_drvdata(pdev);
1024 int irq = platform_get_irq(pdev, 0);
1026 twl4030charger_ac_en(DISABLE);
1027 twl4030charger_usb_en(DISABLE);
1028 twl4030battery_hw_level_en(DISABLE);
1029 twl4030battery_hw_presence_en(DISABLE);
1031 free_irq(TWL4030_MODIRQ_BCI, NULL);
1034 flush_scheduled_work();
1035 power_supply_unregister(&di->bat);
1036 power_supply_unregister(&di->bk_bat);
1037 platform_set_drvdata(pdev, NULL);
1044 static int twl4030_bci_battery_suspend(struct platform_device *pdev,
1047 struct twl4030_bci_device_info *di = platform_get_drvdata(pdev);
1049 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
1050 cancel_delayed_work(&di->twl4030_bci_monitor_work);
1051 cancel_delayed_work(&di->twl4030_bk_bci_monitor_work);
1055 static int twl4030_bci_battery_resume(struct platform_device *pdev)
1057 struct twl4030_bci_device_info *di = platform_get_drvdata(pdev);
1059 schedule_delayed_work(&di->twl4030_bci_monitor_work, 0);
1060 schedule_delayed_work(&di->twl4030_bk_bci_monitor_work, 50);
1064 #define twl4030_bci_battery_suspend NULL
1065 #define twl4030_bci_battery_resume NULL
1066 #endif /* CONFIG_PM */
1068 static struct platform_driver twl4030_bci_battery_driver = {
1069 .probe = twl4030_bci_battery_probe,
1070 .remove = __exit_p(twl4030_bci_battery_remove),
1071 .suspend = twl4030_bci_battery_suspend,
1072 .resume = twl4030_bci_battery_resume,
1074 .name = "twl4030_bci",
1078 MODULE_LICENSE("GPL");
1079 MODULE_ALIAS("platform:twl4030_bci");
1080 MODULE_AUTHOR("Texas Instruments Inc");
1082 static int __init twl4030_battery_init(void)
1084 return platform_driver_register(&twl4030_bci_battery_driver);
1086 module_init(twl4030_battery_init);
1088 static void __exit twl4030_battery_exit(void)
1090 platform_driver_unregister(&twl4030_bci_battery_driver);
1092 module_exit(twl4030_battery_exit);