2 * Dumb driver for LiIon batteries using TWL4030 madc.
4 * Copyright 2013 Golden Delicious Computers
5 * Lukas Märdian <lukas@goldelico.com>
7 * Based on dumb driver for gta01 battery
8 * Copyright 2009 Openmoko, Inc
9 * Balaji Rao <balajirrao@openmoko.org>
12 #include <linux/module.h>
13 #include <linux/param.h>
14 #include <linux/delay.h>
15 #include <linux/workqueue.h>
16 #include <linux/platform_device.h>
17 #include <linux/power_supply.h>
18 #include <linux/slab.h>
19 #include <linux/sort.h>
20 #include <linux/i2c/twl4030-madc.h>
21 #include <linux/power/twl4030_madc_battery.h>
23 struct twl4030_madc_battery
{
24 struct power_supply psy
;
25 struct twl4030_madc_bat_platform_data
*pdata
;
28 static enum power_supply_property twl4030_madc_bat_props
[] = {
29 POWER_SUPPLY_PROP_PRESENT
,
30 POWER_SUPPLY_PROP_STATUS
,
31 POWER_SUPPLY_PROP_TECHNOLOGY
,
32 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
33 POWER_SUPPLY_PROP_CURRENT_NOW
,
34 POWER_SUPPLY_PROP_CAPACITY
,
35 POWER_SUPPLY_PROP_CHARGE_FULL
,
36 POWER_SUPPLY_PROP_CHARGE_NOW
,
37 POWER_SUPPLY_PROP_TEMP
,
38 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
41 static int madc_read(int index
)
43 struct twl4030_madc_request req
;
47 req
.method
= TWL4030_MADC_SW2
;
48 req
.type
= TWL4030_MADC_WAIT
;
53 val
= twl4030_madc_conversion(&req
);
57 return req
.rbuf
[ffs(index
) - 1];
60 static int twl4030_madc_bat_get_charging_status(void)
62 return (madc_read(TWL4030_MADC_ICHG
) > 0) ? 1 : 0;
65 static int twl4030_madc_bat_get_voltage(void)
67 return madc_read(TWL4030_MADC_VBAT
);
70 static int twl4030_madc_bat_get_current(void)
72 return madc_read(TWL4030_MADC_ICHG
) * 1000;
75 static int twl4030_madc_bat_get_temp(void)
77 return madc_read(TWL4030_MADC_BTEMP
) * 10;
80 static int twl4030_madc_bat_voltscale(struct twl4030_madc_battery
*bat
,
83 struct twl4030_madc_bat_calibration
*calibration
;
86 /* choose charging curve */
87 if (twl4030_madc_bat_get_charging_status())
88 calibration
= bat
->pdata
->charging
;
90 calibration
= bat
->pdata
->discharging
;
92 if (volt
> calibration
[0].voltage
) {
93 res
= calibration
[0].level
;
95 for (i
= 0; calibration
[i
+1].voltage
>= 0; i
++) {
96 if (volt
<= calibration
[i
].voltage
&&
97 volt
>= calibration
[i
+1].voltage
) {
98 /* interval found - interpolate within range */
99 res
= calibration
[i
].level
-
100 ((calibration
[i
].voltage
- volt
) *
101 (calibration
[i
].level
-
102 calibration
[i
+1].level
)) /
103 (calibration
[i
].voltage
-
104 calibration
[i
+1].voltage
);
112 static int twl4030_madc_bat_get_property(struct power_supply
*psy
,
113 enum power_supply_property psp
,
114 union power_supply_propval
*val
)
116 struct twl4030_madc_battery
*bat
= container_of(psy
,
117 struct twl4030_madc_battery
, psy
);
120 case POWER_SUPPLY_PROP_STATUS
:
121 if (twl4030_madc_bat_voltscale(bat
,
122 twl4030_madc_bat_get_voltage()) > 95)
123 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
125 if (twl4030_madc_bat_get_charging_status())
126 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
128 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
131 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
132 val
->intval
= twl4030_madc_bat_get_voltage() * 1000;
134 case POWER_SUPPLY_PROP_TECHNOLOGY
:
135 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
137 case POWER_SUPPLY_PROP_CURRENT_NOW
:
138 val
->intval
= twl4030_madc_bat_get_current();
140 case POWER_SUPPLY_PROP_PRESENT
:
141 /* assume battery is always present */
144 case POWER_SUPPLY_PROP_CHARGE_NOW
: {
145 int percent
= twl4030_madc_bat_voltscale(bat
,
146 twl4030_madc_bat_get_voltage());
147 val
->intval
= (percent
* bat
->pdata
->capacity
) / 100;
150 case POWER_SUPPLY_PROP_CAPACITY
:
151 val
->intval
= twl4030_madc_bat_voltscale(bat
,
152 twl4030_madc_bat_get_voltage());
154 case POWER_SUPPLY_PROP_CHARGE_FULL
:
155 val
->intval
= bat
->pdata
->capacity
;
157 case POWER_SUPPLY_PROP_TEMP
:
158 val
->intval
= twl4030_madc_bat_get_temp();
160 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
: {
161 int percent
= twl4030_madc_bat_voltscale(bat
,
162 twl4030_madc_bat_get_voltage());
164 int chg
= (percent
* (bat
->pdata
->capacity
/1000))/100;
166 /* assume discharge with 400 mA (ca. 1.5W) */
167 val
->intval
= (3600l * chg
) / 400;
177 static void twl4030_madc_bat_ext_changed(struct power_supply
*psy
)
179 struct twl4030_madc_battery
*bat
= container_of(psy
,
180 struct twl4030_madc_battery
, psy
);
182 power_supply_changed(&bat
->psy
);
185 static int twl4030_cmp(const void *a
, const void *b
)
187 return ((struct twl4030_madc_bat_calibration
*)b
)->voltage
-
188 ((struct twl4030_madc_bat_calibration
*)a
)->voltage
;
191 static int twl4030_madc_battery_probe(struct platform_device
*pdev
)
193 struct twl4030_madc_battery
*twl4030_madc_bat
;
194 struct twl4030_madc_bat_platform_data
*pdata
= pdev
->dev
.platform_data
;
196 twl4030_madc_bat
= kzalloc(sizeof(*twl4030_madc_bat
), GFP_KERNEL
);
197 if (!twl4030_madc_bat
)
200 twl4030_madc_bat
->psy
.name
= "twl4030_battery";
201 twl4030_madc_bat
->psy
.type
= POWER_SUPPLY_TYPE_BATTERY
;
202 twl4030_madc_bat
->psy
.properties
= twl4030_madc_bat_props
;
203 twl4030_madc_bat
->psy
.num_properties
=
204 ARRAY_SIZE(twl4030_madc_bat_props
);
205 twl4030_madc_bat
->psy
.get_property
= twl4030_madc_bat_get_property
;
206 twl4030_madc_bat
->psy
.external_power_changed
=
207 twl4030_madc_bat_ext_changed
;
209 /* sort charging and discharging calibration data */
210 sort(pdata
->charging
, pdata
->charging_size
,
211 sizeof(struct twl4030_madc_bat_calibration
),
213 sort(pdata
->discharging
, pdata
->discharging_size
,
214 sizeof(struct twl4030_madc_bat_calibration
),
217 twl4030_madc_bat
->pdata
= pdata
;
218 platform_set_drvdata(pdev
, twl4030_madc_bat
);
219 power_supply_register(&pdev
->dev
, &twl4030_madc_bat
->psy
);
224 static int twl4030_madc_battery_remove(struct platform_device
*pdev
)
226 struct twl4030_madc_battery
*bat
= platform_get_drvdata(pdev
);
228 power_supply_unregister(&bat
->psy
);
234 static struct platform_driver twl4030_madc_battery_driver
= {
236 .name
= "twl4030_madc_battery",
238 .probe
= twl4030_madc_battery_probe
,
239 .remove
= twl4030_madc_battery_remove
,
241 module_platform_driver(twl4030_madc_battery_driver
);
243 MODULE_LICENSE("GPL");
244 MODULE_AUTHOR("Lukas Märdian <lukas@goldelico.com>");
245 MODULE_DESCRIPTION("twl4030_madc battery driver");