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 /* Boot with automatic charge */
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/device.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/platform_device.h>
27 #include <linux/i2c/twl4030.h>
28 #include <linux/power_supply.h>
29 #include <linux/i2c/twl4030-madc.h>
31 #define T2_BATTERY_VOLT 0x04
32 #define T2_BATTERY_TEMP 0x06
33 #define T2_BATTERY_CUR 0x08
35 /* charger constants */
37 #define AC_PW_CONN 0x01
38 #define USB_PW_CONN 0x02
40 /* TWL4030_MODULE_USB */
41 #define REG_POWER_CTRL 0x0AC
43 #define REG_PHY_CLK_CTRL 0x0FE
44 #define REG_PHY_CLK_CTRL_STS 0x0FF
45 #define PHY_DPLL_CLK 0x01
47 #define REG_BCICTL1 0x023
48 #define REG_BCICTL2 0x024
53 /* Boot BCI flag bits */
54 #define BCIAUTOWEN 0x020
55 #define CONFIG_DONE 0x010
57 #define BCIAUTOUSB 0x002
58 #define BCIAUTOAC 0x001
59 #define BCIMSTAT_MASK 0x03F
61 /* Boot BCI register */
62 #define REG_BOOT_BCI 0x007
63 #define REG_CTRL1 0x00
64 #define REG_SW1SELECT_MSB 0x07
65 #define SW1_CH9_SEL 0x02
66 #define REG_CTRL_SW1 0x012
67 #define SW1_TRIGGER 0x020
69 #define REG_GPCH9 0x049
70 #define REG_STS_HW_CONDITIONS 0x0F
71 #define STS_VBUS 0x080
73 #define REG_BCIMSTATEC 0x02
74 #define REG_BCIMFSTS4 0x010
75 #define REG_BCIMFSTS2 0x00E
76 #define REG_BCIMFSTS3 0x00F
77 #define REG_BCIMFSTS1 0x001
78 #define USBFASTMCHG 0x004
79 #define BATSTSPCHG 0x004
80 #define BATSTSMCHG 0x040
85 #define REG_BB_CFG 0x012
89 #define REG_GPBR1 0x0c
90 #define MADC_HFCLK_EN 0x80
91 #define DEFAULT_MADC_CLK_EN 0x10
93 /* Power supply charge interrupt */
94 #define REG_PWR_ISR1 0x00
95 #define REG_PWR_IMR1 0x01
96 #define REG_PWR_EDR1 0x05
97 #define REG_PWR_SIH_CTRL 0x007
99 #define USB_PRES 0x004
100 #define CHG_PRES 0x002
102 #define USB_PRES_RISING 0x020
103 #define USB_PRES_FALLING 0x010
104 #define CHG_PRES_RISING 0x008
105 #define CHG_PRES_FALLING 0x004
106 #define AC_STATEC 0x20
109 /* interrupt status registers */
110 #define REG_BCIISR1A 0x0
111 #define REG_BCIISR2A 0x01
113 /* Interrupt flags bits BCIISR1 */
114 #define BATSTS_ISR1 0x080
115 #define VBATLVL_ISR1 0x001
117 /* Interrupt mask registers for int1*/
118 #define REG_BCIIMR1A 0x002
119 #define REG_BCIIMR2A 0x003
121 /* Interrupt masks for BCIIMR1 */
122 #define BATSTS_IMR1 0x080
123 #define VBATLVL_IMR1 0x001
125 /* Interrupt edge detection register */
126 #define REG_BCIEDR1 0x00A
127 #define REG_BCIEDR2 0x00B
128 #define REG_BCIEDR3 0x00C
131 #define BATSTS_EDRRISIN 0x080
132 #define BATSTS_EDRFALLING 0x040
135 #define VBATLVL_EDRRISIN 0x02
138 #define REG_BCIIREF1 0x027
139 #define REG_BCIIREF2 0x028
142 #define REG_BCIMFTH1 0x016
145 #define KEY_IIREF 0xE7
146 #define KEY_FTH1 0xD2
147 #define REG_BCIMFKEY 0x011
149 /* Step size and prescaler ratio */
150 #define TEMP_STEP_SIZE 147
151 #define TEMP_PSR_R 100
153 #define VOLT_STEP_SIZE 588
154 #define VOLT_PSR_R 100
156 #define CURR_STEP_SIZE 147
157 #define CURR_PSR_R1 44
158 #define CURR_PSR_R2 80
160 #define BK_VOLT_STEP_SIZE 441
161 #define BK_VOLT_PSR_R 100
166 struct twl4030_bci_device_info {
169 unsigned long update_time;
177 struct power_supply bat;
178 struct power_supply bk_bat;
179 struct delayed_work twl4030_bci_monitor_work;
180 struct delayed_work twl4030_bk_bci_monitor_work;
182 struct twl4030_bci_platform_data *pdata;
185 static int usb_charger_flag;
186 static int LVL_1, LVL_2, LVL_3, LVL_4;
188 static int read_bci_val(u8 reg_1);
189 static inline int clear_n_set(u8 mod_no, u8 clear, u8 set, u8 reg);
190 static int twl4030charger_presence(void);
193 * Report and clear the charger presence event.
195 static inline int twl4030charger_presence_evt(void)
198 u8 chg_sts, set = 0, clear = 0;
200 /* read charger power supply status */
201 ret = twl4030_i2c_read_u8(TWL4030_MODULE_PM_MASTER, &chg_sts,
202 REG_STS_HW_CONDITIONS);
206 if (chg_sts & STS_CHG) { /* If the AC charger have been connected */
207 /* configuring falling edge detection for CHG_PRES */
208 set = CHG_PRES_FALLING;
209 clear = CHG_PRES_RISING;
210 } else { /* If the AC charger have been disconnected */
211 /* configuring rising edge detection for CHG_PRES */
212 set = CHG_PRES_RISING;
213 clear = CHG_PRES_FALLING;
216 /* Update the interrupt edge detection register */
217 clear_n_set(TWL4030_MODULE_INT, clear, set, REG_PWR_EDR1);
223 * Interrupt service routine
225 * Attends to TWL 4030 power module interruptions events, specifically
226 * USB_PRES (USB charger presence) CHG_PRES (AC charger presence) events
229 static irqreturn_t twl4030charger_interrupt(int irq, void *_di)
231 struct twl4030_bci_device_info *di = _di;
233 #ifdef CONFIG_LOCKDEP
234 /* WORKAROUND for lockdep forcing IRQF_DISABLED on us, which
235 * we don't want and can't tolerate. Although it might be
236 * friendlier not to borrow this thread context...
241 twl4030charger_presence_evt();
242 power_supply_changed(&di->bat);
248 * This function handles the twl4030 battery presence interrupt
250 static int twl4030battery_presence_evt(void)
253 u8 batstsmchg, batstspchg;
255 /* check for the battery presence in main charge*/
256 ret = twl4030_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE,
257 &batstsmchg, REG_BCIMFSTS3);
261 /* check for the battery presence in precharge */
262 ret = twl4030_i2c_read_u8(TWL4030_MODULE_PRECHARGE,
263 &batstspchg, REG_BCIMFSTS1);
268 * REVISIT: Physically inserting/removing the batt
269 * does not seem to generate an int on 3430ES2 SDP.
271 if ((batstspchg & BATSTSPCHG) || (batstsmchg & BATSTSMCHG)) {
272 /* In case of the battery insertion event */
273 ret = clear_n_set(TWL4030_MODULE_INTERRUPTS, BATSTS_EDRRISIN,
274 BATSTS_EDRFALLING, REG_BCIEDR2);
278 /* In case of the battery removal event */
279 ret = clear_n_set(TWL4030_MODULE_INTERRUPTS, BATSTS_EDRFALLING,
280 BATSTS_EDRRISIN, REG_BCIEDR2);
289 * This function handles the twl4030 battery voltage level interrupt.
291 static int twl4030battery_level_evt(void)
296 /* checking for threshold event */
297 ret = twl4030_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE,
298 &mfst, REG_BCIMFSTS2);
302 /* REVISIT could use a bitmap */
303 if (mfst & VBATOV4) {
308 } else if (mfst & VBATOV3) {
313 } else if (mfst & VBATOV2) {
329 * Interrupt service routine
331 * Attends to BCI interruptions events,
332 * specifically BATSTS (battery connection and removal)
333 * VBATOV (main battery voltage threshold) events
336 static irqreturn_t twl4030battery_interrupt(int irq, void *_di)
338 u8 isr1a_val, isr2a_val, clear_2a, clear_1a;
341 #ifdef CONFIG_LOCKDEP
342 /* WORKAROUND for lockdep forcing IRQF_DISABLED on us, which
343 * we don't want and can't tolerate. Although it might be
344 * friendlier not to borrow this thread context...
349 ret = twl4030_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &isr1a_val,
354 ret = twl4030_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &isr2a_val,
359 clear_2a = (isr2a_val & VBATLVL_ISR1) ? (VBATLVL_ISR1) : 0;
360 clear_1a = (isr1a_val & BATSTS_ISR1) ? (BATSTS_ISR1) : 0;
362 /* cleaning BCI interrupt status flags */
363 ret = twl4030_i2c_write_u8(TWL4030_MODULE_INTERRUPTS,
364 clear_1a , REG_BCIISR1A);
368 ret = twl4030_i2c_write_u8(TWL4030_MODULE_INTERRUPTS,
369 clear_2a , REG_BCIISR2A);
373 /* battery connetion or removal event */
374 if (isr1a_val & BATSTS_ISR1)
375 twl4030battery_presence_evt();
376 /* battery voltage threshold event*/
377 else if (isr2a_val & VBATLVL_ISR1)
378 twl4030battery_level_evt();
386 * Enable/Disable hardware battery level event notifications.
388 static int twl4030battery_hw_level_en(int enable)
393 /* unmask VBATOV interrupt for INT1 */
394 ret = clear_n_set(TWL4030_MODULE_INTERRUPTS, VBATLVL_IMR1,
399 /* configuring interrupt edge detection for VBATOv */
400 ret = clear_n_set(TWL4030_MODULE_INTERRUPTS, 0,
401 VBATLVL_EDRRISIN, REG_BCIEDR3);
405 /* mask VBATOV interrupt for INT1 */
406 ret = clear_n_set(TWL4030_MODULE_INTERRUPTS, 0,
407 VBATLVL_IMR1, REG_BCIIMR2A);
416 * Enable/disable hardware battery presence event notifications.
418 static int twl4030battery_hw_presence_en(int enable)
423 /* unmask BATSTS interrupt for INT1 */
424 ret = clear_n_set(TWL4030_MODULE_INTERRUPTS, BATSTS_IMR1,
429 /* configuring interrupt edge for BATSTS */
430 ret = clear_n_set(TWL4030_MODULE_INTERRUPTS, 0,
431 BATSTS_EDRRISIN | BATSTS_EDRFALLING, REG_BCIEDR2);
435 /* mask BATSTS interrupt for INT1 */
436 ret = clear_n_set(TWL4030_MODULE_INTERRUPTS, 0,
437 BATSTS_IMR1, REG_BCIIMR1A);
446 * Enable/Disable AC Charge funtionality.
448 static int twl4030charger_ac_en(int enable, int automatic)
453 /* forcing the field BCIAUTOAC (BOOT_BCI[0) to 1 */
455 ret = clear_n_set(TWL4030_MODULE_PM_MASTER, BCIAUTOAC | CVENAC,
456 (CONFIG_DONE | BCIAUTOWEN),
459 ret = clear_n_set(TWL4030_MODULE_PM_MASTER, 0,
460 (CONFIG_DONE | BCIAUTOWEN | BCIAUTOAC | CVENAC),
466 /* forcing the field BCIAUTOAC (BOOT_BCI[0) to 0*/
467 ret = clear_n_set(TWL4030_MODULE_PM_MASTER, BCIAUTOAC,
468 (CONFIG_DONE | BCIAUTOWEN),
478 * Enable/Disable USB Charge funtionality.
480 int twl4030charger_usb_en(int enable)
484 unsigned long timeout;
487 /* Check for USB charger conneted */
488 ret = twl4030charger_presence();
492 if (!(ret & USB_PW_CONN))
495 /* forcing the field BCIAUTOUSB (BOOT_BCI[1]) to 1 */
496 ret = clear_n_set(TWL4030_MODULE_PM_MASTER, 0,
497 (CONFIG_DONE | BCIAUTOWEN | BCIAUTOUSB),
502 ret = clear_n_set(TWL4030_MODULE_USB, 0, PHY_DPLL_CLK,
508 timeout = jiffies + msecs_to_jiffies(50);
510 while ((!(value & PHY_DPLL_CLK)) &&
511 time_before(jiffies, timeout)) {
513 ret = twl4030_i2c_read_u8(TWL4030_MODULE_USB, &value,
514 REG_PHY_CLK_CTRL_STS);
519 /* OTG_EN (POWER_CTRL[5]) to 1 */
520 ret = clear_n_set(TWL4030_MODULE_USB, 0, OTG_EN,
527 /* forcing USBFASTMCHG(BCIMFSTS4[2]) to 1 */
528 ret = clear_n_set(TWL4030_MODULE_MAIN_CHARGE, 0,
529 USBFASTMCHG, REG_BCIMFSTS4);
533 twl4030charger_presence();
534 ret = clear_n_set(TWL4030_MODULE_PM_MASTER, BCIAUTOUSB,
535 (CONFIG_DONE | BCIAUTOWEN), REG_BOOT_BCI);
544 * Return battery temperature
547 static int twl4030battery_temperature(struct twl4030_bci_device_info *di)
550 int temp, curr, volt, res, ret;
552 /* Is a temperature table specified? */
553 if (!di->pdata->tblsize)
556 /* Getting and calculating the thermistor voltage */
557 ret = read_bci_val(T2_BATTERY_TEMP);
561 volt = (ret * TEMP_STEP_SIZE) / TEMP_PSR_R;
563 /* Getting and calculating the supply current in micro ampers */
564 ret = twl4030_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE, &val,
569 curr = ((val & ITHSENS) + 1) * 10;
571 /* Getting and calculating the thermistor resistance in ohms*/
572 res = volt * 1000 / curr;
574 /*calculating temperature*/
575 for (temp = 58; temp >= 0; temp--) {
576 int actual = di->pdata->battery_tmp_tbl[temp];
577 if ((actual - res) >= 0)
581 /* Negative temperature */
595 * Return battery voltage
598 static int twl4030battery_voltage(void)
600 int volt = read_bci_val(T2_BATTERY_VOLT);
602 return (volt * VOLT_STEP_SIZE) / VOLT_PSR_R;
606 * Return the battery current
609 static int twl4030battery_current(void)
611 int ret, curr = read_bci_val(T2_BATTERY_CUR);
614 ret = twl4030_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE, &val,
619 if (val & CGAIN) /* slope of 0.44 mV/mA */
620 return (curr * CURR_STEP_SIZE) / CURR_PSR_R1;
621 else /* slope of 0.88 mV/mA */
622 return (curr * CURR_STEP_SIZE) / CURR_PSR_R2;
626 * Return the battery backup voltage
629 static int twl4030backupbatt_voltage(void)
631 struct twl4030_madc_request req;
634 req.channels = (1 << 9);
636 req.method = TWL4030_MADC_SW1;
639 twl4030_madc_conversion(&req);
640 temp = (u16)req.rbuf[9];
642 return (temp * BK_VOLT_STEP_SIZE) / BK_VOLT_PSR_R;
646 * Returns an integer value, that means,
647 * NO_PW_CONN no power supply is connected
648 * AC_PW_CONN if the AC power supply is connected
649 * USB_PW_CONN if the USB power supply is connected
650 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
654 static int twl4030charger_presence(void)
659 ret = twl4030_i2c_read_u8(TWL4030_MODULE_PM_MASTER, &hwsts,
660 REG_STS_HW_CONDITIONS);
662 pr_err("twl4030_bci: error reading STS_HW_CONDITIONS\n");
666 ret = (hwsts & STS_CHG) ? AC_PW_CONN : NO_PW_CONN;
667 ret += (hwsts & STS_VBUS) ? USB_PW_CONN : NO_PW_CONN;
669 if (ret & USB_PW_CONN)
670 usb_charger_flag = 1;
672 usb_charger_flag = 0;
679 * Returns the main charge FSM status
682 static int twl4030bci_status(void)
687 ret = twl4030_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE,
688 &status, REG_BCIMSTATEC);
690 pr_err("twl4030_bci: error reading BCIMSTATEC\n");
695 printk("BCI DEBUG: BCIMSTATEC Charge state is 0x%x\n", status);
697 return (int) (status & BCIMSTAT_MASK);
700 static int read_bci_val(u8 reg)
706 ret = twl4030_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE, &val,
711 temp = ((int)(val & 0x03)) << 8;
714 ret = twl4030_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE, &val,
723 * Settup the twl4030 BCI module to enable backup
726 static int twl4030backupbatt_voltage_setup(void)
730 /* Starting backup batery charge */
731 ret = clear_n_set(TWL4030_MODULE_PM_RECEIVER, 0, BBCHEN,
740 * Settup the twl4030 BCI module to measure battery
743 static int twl4030battery_temp_setup(void)
750 /* Enabling thermistor current */
751 ret = clear_n_set(TWL4030_MODULE_MAIN_CHARGE, 0, 0x1B,
757 twl4030_i2c_read_u8(TWL4030_MODULE_PM_MASTER, &ret, REG_BOOT_BCI);
758 printk("BCI DEBUG: BOOT_BCI Value is 0x%x\n", ret);
760 twl4030_i2c_read_u8(TWL4030_MODULE_PM_MASTER, &ret, REG_STS_HW_CONDITIONS);
761 printk("BCI DEBUG: STS_HW_CONDITIONS Value is 0x%x\n", ret);
763 twl4030_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE, &ret, REG_BCICTL1);
764 printk("BCI DEBUG: BCICTL1 Value is 0x%x\n", ret);
766 twl4030_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE, &ret, REG_BCICTL2);
767 printk("BCI DEBUG: BCICTL2 Value is 0x%x\n", ret);
769 twl4030_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE, &ret, 0x0);
770 printk("BCI DEBUG: BCIMDEN Value is 0x%x\n", ret);
772 twl4030_i2c_read_u8(TWL4030_MODULE_INTBR, &ret, REG_GPBR1);
773 printk("BCI DEBUG: GPBR1 Value is 0x%x\n", ret);
775 for(i = 0x0; i <= 0x32; i++)
777 twl4030_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE, &ret, i);
778 printk("BCI DEBUG: BCI 0x%x Value is 0x%x\n", i, ret);
786 * Sets and clears bits on an given register on a given module
788 static inline int clear_n_set(u8 mod_no, u8 clear, u8 set, u8 reg)
793 /* Gets the initial register value */
794 ret = twl4030_i2c_read_u8(mod_no, &val, reg);
797 /* Clearing all those bits to clear */
800 /* Setting all those bits to set */
803 /* Update the register */
804 ret = twl4030_i2c_write_u8(mod_no, val, reg);
811 static enum power_supply_property twl4030_bci_battery_props[] = {
812 POWER_SUPPLY_PROP_STATUS,
813 POWER_SUPPLY_PROP_ONLINE,
814 POWER_SUPPLY_PROP_VOLTAGE_NOW,
815 POWER_SUPPLY_PROP_CURRENT_NOW,
816 POWER_SUPPLY_PROP_CAPACITY,
817 POWER_SUPPLY_PROP_TEMP,
820 static enum power_supply_property twl4030_bk_bci_battery_props[] = {
821 POWER_SUPPLY_PROP_VOLTAGE_NOW,
825 twl4030_bk_bci_battery_read_status(struct twl4030_bci_device_info *di)
827 di->bk_voltage_uV = twl4030backupbatt_voltage();
830 static void twl4030_bk_bci_battery_work(struct work_struct *work)
832 struct twl4030_bci_device_info *di = container_of(work,
833 struct twl4030_bci_device_info,
834 twl4030_bk_bci_monitor_work.work);
836 if(!di->pdata->no_backup_battery)
837 twl4030_bk_bci_battery_read_status(di);
838 schedule_delayed_work(&di->twl4030_bk_bci_monitor_work, 500);
841 static void twl4030_bci_battery_read_status(struct twl4030_bci_device_info *di)
843 di->temp_C = twl4030battery_temperature(di);
844 di->voltage_uV = twl4030battery_voltage();
845 di->current_uA = twl4030battery_current();
849 twl4030_bci_battery_update_status(struct twl4030_bci_device_info *di)
851 twl4030_bci_battery_read_status(di);
852 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
854 if (power_supply_am_i_supplied(&di->bat))
855 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
857 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
860 static void twl4030_bci_battery_work(struct work_struct *work)
862 struct twl4030_bci_device_info *di = container_of(work,
863 struct twl4030_bci_device_info, twl4030_bci_monitor_work.work);
865 twl4030_bci_battery_update_status(di);
866 schedule_delayed_work(&di->twl4030_bci_monitor_work, 100);
870 #define to_twl4030_bci_device_info(x) container_of((x), \
871 struct twl4030_bci_device_info, bat);
873 static void twl4030_bci_battery_external_power_changed(struct power_supply *psy)
875 struct twl4030_bci_device_info *di = to_twl4030_bci_device_info(psy);
877 cancel_delayed_work(&di->twl4030_bci_monitor_work);
878 schedule_delayed_work(&di->twl4030_bci_monitor_work, 0);
881 #define to_twl4030_bk_bci_device_info(x) container_of((x), \
882 struct twl4030_bci_device_info, bk_bat);
885 show_charge_current(struct device *dev, struct device_attribute *attr, char *buf)
888 int ret = read_bci_val(REG_BCIIREF1) & 0x1FF;
889 twl4030_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE, &ctl, REG_BCICTL1);
896 twl4030battery_temp_setup();
899 return sprintf(buf, "%d\n", ret);
903 set_charge_current(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
905 unsigned long newCurrent;
908 ret = strict_strtoul(buf, 10, &newCurrent);
912 ret = twl4030_i2c_write_u8(TWL4030_MODULE_MAIN_CHARGE, KEY_IIREF, REG_BCIMFKEY);
916 ret = twl4030_i2c_write_u8(TWL4030_MODULE_MAIN_CHARGE, newCurrent & 0xff, REG_BCIIREF1);
920 ret = twl4030_i2c_write_u8(TWL4030_MODULE_MAIN_CHARGE, KEY_IIREF, REG_BCIMFKEY);
924 ret = twl4030_i2c_write_u8(TWL4030_MODULE_MAIN_CHARGE, (newCurrent >> 8) & 0x1, REG_BCIIREF2);
928 /* Set software-controlled charge */
929 twl4030charger_ac_en(ENABLE, 0);
931 /* Set CGAIN = 0 or 1 */
932 if(newCurrent > 511) {
935 /* Set CGAIN = 1 -- need to wait until automatic charge turns off */
937 clear_n_set(TWL4030_MODULE_MAIN_CHARGE, 0, CGAIN | 0x1B, REG_BCICTL1);
938 twl4030_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE, &tmp, REG_BCICTL1);
947 /* Set CGAIN = 0 -- need to wait until automatic charge turns off */
949 clear_n_set(TWL4030_MODULE_MAIN_CHARGE, CGAIN, 0x1B, REG_BCICTL1);
950 twl4030_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE, &tmp, REG_BCICTL1);
952 ret = !(tmp & CGAIN);
958 /* Set automatic charge (CGAIN = 0/1 persists) */
959 twl4030charger_ac_en(ENABLE, 1);
963 static DEVICE_ATTR(charge_current, S_IRUGO | S_IWUSR, show_charge_current, set_charge_current);
965 static int twl4030_bk_bci_battery_get_property(struct power_supply *psy,
966 enum power_supply_property psp,
967 union power_supply_propval *val)
969 struct twl4030_bci_device_info *di = to_twl4030_bk_bci_device_info(psy);
972 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
973 val->intval = di->bk_voltage_uV;
982 static int twl4030_bci_battery_get_property(struct power_supply *psy,
983 enum power_supply_property psp,
984 union power_supply_propval *val)
986 struct twl4030_bci_device_info *di;
989 di = to_twl4030_bci_device_info(psy);
992 case POWER_SUPPLY_PROP_STATUS:
993 val->intval = di->charge_status;
1000 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1001 val->intval = di->voltage_uV;
1003 case POWER_SUPPLY_PROP_CURRENT_NOW:
1004 val->intval = di->current_uA;
1006 case POWER_SUPPLY_PROP_TEMP:
1007 val->intval = di->temp_C;
1009 case POWER_SUPPLY_PROP_ONLINE:
1010 status = twl4030bci_status();
1011 if ((status & AC_STATEC) == AC_STATEC)
1012 val->intval = POWER_SUPPLY_TYPE_MAINS;
1013 else if (usb_charger_flag)
1014 val->intval = POWER_SUPPLY_TYPE_USB;
1018 case POWER_SUPPLY_PROP_CAPACITY:
1020 * need to get the correct percentage value per the
1021 * battery characteristics. Approx values for now.
1023 if (di->voltage_uV < 2894 || LVL_1) {
1026 } else if ((di->voltage_uV < 3451 && di->voltage_uV > 2894)
1030 } else if ((di->voltage_uV < 3902 && di->voltage_uV > 3451)
1034 } else if ((di->voltage_uV < 3949 && di->voltage_uV > 3902)
1038 } else if (di->voltage_uV > 3949)
1047 static char *twl4030_bci_supplied_to[] = {
1048 "twl4030_bci_battery",
1051 static int __init twl4030_bci_battery_probe(struct platform_device *pdev)
1053 struct twl4030_bci_platform_data *pdata = pdev->dev.platform_data;
1054 struct twl4030_bci_device_info *di;
1058 di = kzalloc(sizeof(*di), GFP_KERNEL);
1062 di->dev = &pdev->dev;
1063 di->bat.name = "twl4030_bci_battery";
1064 di->bat.supplied_to = twl4030_bci_supplied_to;
1065 di->bat.num_supplicants = ARRAY_SIZE(twl4030_bci_supplied_to);
1066 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
1067 di->bat.properties = twl4030_bci_battery_props;
1068 di->bat.num_properties = ARRAY_SIZE(twl4030_bci_battery_props);
1069 di->bat.get_property = twl4030_bci_battery_get_property;
1070 di->bat.external_power_changed =
1071 twl4030_bci_battery_external_power_changed;
1073 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
1075 di->bk_bat.name = "twl4030_bci_bk_battery";
1076 di->bk_bat.type = POWER_SUPPLY_TYPE_BATTERY;
1077 di->bk_bat.properties = twl4030_bk_bci_battery_props;
1078 di->bk_bat.num_properties = ARRAY_SIZE(twl4030_bk_bci_battery_props);
1079 di->bk_bat.get_property = twl4030_bk_bci_battery_get_property;
1080 di->bk_bat.external_power_changed = NULL;
1084 twl4030_i2c_write_u8(TWL4030_MODULE_INTBR, MADC_HFCLK_EN | DEFAULT_MADC_CLK_EN, REG_GPBR1);
1086 twl4030charger_ac_en(ENABLE, CHARGE_MODE);
1087 twl4030charger_usb_en(ENABLE);
1088 twl4030battery_hw_level_en(ENABLE);
1089 twl4030battery_hw_presence_en(ENABLE);
1091 platform_set_drvdata(pdev, di);
1093 /* settings for temperature sensing */
1094 ret = twl4030battery_temp_setup();
1096 goto temp_setup_fail;
1098 /* enabling GPCH09 for read back battery voltage */
1099 if(!di->pdata->no_backup_battery)
1101 ret = twl4030backupbatt_voltage_setup();
1103 goto voltage_setup_fail;
1106 /* REVISIT do we need to request both IRQs ?? */
1108 /* request BCI interruption */
1109 ret = request_irq(TWL4030_MODIRQ_BCI, twl4030battery_interrupt,
1110 0, pdev->name, NULL);
1112 dev_dbg(&pdev->dev, "could not request irq %d, status %d\n",
1113 TWL4030_MODIRQ_BCI, ret);
1117 irq = platform_get_irq(pdev, 0);
1119 /* request Power interruption */
1120 ret = request_irq(irq, twl4030charger_interrupt,
1124 dev_dbg(&pdev->dev, "could not request irq %d, status %d\n",
1129 ret = power_supply_register(&pdev->dev, &di->bat);
1131 dev_dbg(&pdev->dev, "failed to register main battery\n");
1135 INIT_DELAYED_WORK_DEFERRABLE(&di->twl4030_bci_monitor_work,
1136 twl4030_bci_battery_work);
1137 schedule_delayed_work(&di->twl4030_bci_monitor_work, 0);
1139 if(!pdata->no_backup_battery)
1141 ret = power_supply_register(&pdev->dev, &di->bk_bat);
1143 dev_dbg(&pdev->dev, "failed to register backup battery\n");
1144 goto bk_batt_failed;
1148 ret = device_create_file(di->bat.dev, &dev_attr_charge_current);
1150 dev_err(&pdev->dev, "failed to create sysfs entries\n");
1151 goto bk_batt_failed;
1154 INIT_DELAYED_WORK_DEFERRABLE(&di->twl4030_bk_bci_monitor_work,
1155 twl4030_bk_bci_battery_work);
1156 schedule_delayed_work(&di->twl4030_bk_bci_monitor_work, 500);
1161 if(!pdata->no_backup_battery)
1162 power_supply_unregister(&di->bat);
1166 free_irq(TWL4030_MODIRQ_BCI, NULL);
1170 twl4030charger_ac_en(DISABLE, CHARGE_MODE);
1171 twl4030charger_usb_en(DISABLE);
1172 twl4030battery_hw_level_en(DISABLE);
1173 twl4030battery_hw_presence_en(DISABLE);
1179 static int __exit twl4030_bci_battery_remove(struct platform_device *pdev)
1181 struct twl4030_bci_device_info *di = platform_get_drvdata(pdev);
1182 int irq = platform_get_irq(pdev, 0);
1184 twl4030charger_ac_en(DISABLE, CHARGE_MODE);
1185 twl4030charger_usb_en(DISABLE);
1186 twl4030battery_hw_level_en(DISABLE);
1187 twl4030battery_hw_presence_en(DISABLE);
1189 free_irq(TWL4030_MODIRQ_BCI, NULL);
1192 flush_scheduled_work();
1193 power_supply_unregister(&di->bat);
1194 power_supply_unregister(&di->bk_bat);
1195 platform_set_drvdata(pdev, NULL);
1202 static int twl4030_bci_battery_suspend(struct platform_device *pdev,
1205 struct twl4030_bci_device_info *di = platform_get_drvdata(pdev);
1207 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
1208 cancel_delayed_work(&di->twl4030_bci_monitor_work);
1209 cancel_delayed_work(&di->twl4030_bk_bci_monitor_work);
1213 static int twl4030_bci_battery_resume(struct platform_device *pdev)
1215 struct twl4030_bci_device_info *di = platform_get_drvdata(pdev);
1217 schedule_delayed_work(&di->twl4030_bci_monitor_work, 0);
1218 schedule_delayed_work(&di->twl4030_bk_bci_monitor_work, 50);
1222 #define twl4030_bci_battery_suspend NULL
1223 #define twl4030_bci_battery_resume NULL
1224 #endif /* CONFIG_PM */
1226 static struct platform_driver twl4030_bci_battery_driver = {
1227 .probe = twl4030_bci_battery_probe,
1228 .remove = __exit_p(twl4030_bci_battery_remove),
1229 .suspend = twl4030_bci_battery_suspend,
1230 .resume = twl4030_bci_battery_resume,
1232 .name = "twl4030_bci",
1236 MODULE_LICENSE("GPL");
1237 MODULE_ALIAS("platform:twl4030_bci");
1238 MODULE_AUTHOR("Texas Instruments Inc");
1240 static int __init twl4030_battery_init(void)
1242 return platform_driver_register(&twl4030_bci_battery_driver);
1244 module_init(twl4030_battery_init);
1246 static void __exit twl4030_battery_exit(void)
1248 platform_driver_unregister(&twl4030_bci_battery_driver);
1250 module_exit(twl4030_battery_exit);