power_supply: Change ownership from driver to core
[linux-2.6/btrfs-unstable.git] / drivers / power / twl4030_madc_battery.c
blob36d3a0e229fab52f4e4d2bd4112c65aa992e098b
1 /*
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;
44 int val;
46 req.channels = index;
47 req.method = TWL4030_MADC_SW2;
48 req.type = TWL4030_MADC_WAIT;
49 req.do_avg = 0;
50 req.raw = false;
51 req.func_cb = NULL;
53 val = twl4030_madc_conversion(&req);
54 if (val < 0)
55 return val;
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,
81 int volt)
83 struct twl4030_madc_bat_calibration *calibration;
84 int i, res = 0;
86 /* choose charging curve */
87 if (twl4030_madc_bat_get_charging_status())
88 calibration = bat->pdata->charging;
89 else
90 calibration = bat->pdata->discharging;
92 if (volt > calibration[0].voltage) {
93 res = calibration[0].level;
94 } else {
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);
105 break;
109 return res;
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 = power_supply_get_drvdata(psy);
118 switch (psp) {
119 case POWER_SUPPLY_PROP_STATUS:
120 if (twl4030_madc_bat_voltscale(bat,
121 twl4030_madc_bat_get_voltage()) > 95)
122 val->intval = POWER_SUPPLY_STATUS_FULL;
123 else {
124 if (twl4030_madc_bat_get_charging_status())
125 val->intval = POWER_SUPPLY_STATUS_CHARGING;
126 else
127 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
129 break;
130 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
131 val->intval = twl4030_madc_bat_get_voltage() * 1000;
132 break;
133 case POWER_SUPPLY_PROP_TECHNOLOGY:
134 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
135 break;
136 case POWER_SUPPLY_PROP_CURRENT_NOW:
137 val->intval = twl4030_madc_bat_get_current();
138 break;
139 case POWER_SUPPLY_PROP_PRESENT:
140 /* assume battery is always present */
141 val->intval = 1;
142 break;
143 case POWER_SUPPLY_PROP_CHARGE_NOW: {
144 int percent = twl4030_madc_bat_voltscale(bat,
145 twl4030_madc_bat_get_voltage());
146 val->intval = (percent * bat->pdata->capacity) / 100;
147 break;
149 case POWER_SUPPLY_PROP_CAPACITY:
150 val->intval = twl4030_madc_bat_voltscale(bat,
151 twl4030_madc_bat_get_voltage());
152 break;
153 case POWER_SUPPLY_PROP_CHARGE_FULL:
154 val->intval = bat->pdata->capacity;
155 break;
156 case POWER_SUPPLY_PROP_TEMP:
157 val->intval = twl4030_madc_bat_get_temp();
158 break;
159 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: {
160 int percent = twl4030_madc_bat_voltscale(bat,
161 twl4030_madc_bat_get_voltage());
162 /* in mAh */
163 int chg = (percent * (bat->pdata->capacity/1000))/100;
165 /* assume discharge with 400 mA (ca. 1.5W) */
166 val->intval = (3600l * chg) / 400;
167 break;
169 default:
170 return -EINVAL;
173 return 0;
176 static void twl4030_madc_bat_ext_changed(struct power_supply *psy)
178 power_supply_changed(psy);
181 static const struct power_supply_desc twl4030_madc_bat_desc = {
182 .name = "twl4030_battery",
183 .type = POWER_SUPPLY_TYPE_BATTERY,
184 .properties = twl4030_madc_bat_props,
185 .num_properties = ARRAY_SIZE(twl4030_madc_bat_props),
186 .get_property = twl4030_madc_bat_get_property,
187 .external_power_changed = twl4030_madc_bat_ext_changed,
191 static int twl4030_cmp(const void *a, const void *b)
193 return ((struct twl4030_madc_bat_calibration *)b)->voltage -
194 ((struct twl4030_madc_bat_calibration *)a)->voltage;
197 static int twl4030_madc_battery_probe(struct platform_device *pdev)
199 struct twl4030_madc_battery *twl4030_madc_bat;
200 struct twl4030_madc_bat_platform_data *pdata = pdev->dev.platform_data;
201 struct power_supply_config psy_cfg = {};
202 int ret = 0;
204 twl4030_madc_bat = kzalloc(sizeof(*twl4030_madc_bat), GFP_KERNEL);
205 if (!twl4030_madc_bat)
206 return -ENOMEM;
208 /* sort charging and discharging calibration data */
209 sort(pdata->charging, pdata->charging_size,
210 sizeof(struct twl4030_madc_bat_calibration),
211 twl4030_cmp, NULL);
212 sort(pdata->discharging, pdata->discharging_size,
213 sizeof(struct twl4030_madc_bat_calibration),
214 twl4030_cmp, NULL);
216 twl4030_madc_bat->pdata = pdata;
217 platform_set_drvdata(pdev, twl4030_madc_bat);
218 psy_cfg.drv_data = twl4030_madc_bat;
219 twl4030_madc_bat->psy = power_supply_register(&pdev->dev,
220 &twl4030_madc_bat_desc,
221 &psy_cfg);
222 if (IS_ERR(twl4030_madc_bat->psy)) {
223 ret = PTR_ERR(twl4030_madc_bat->psy);
224 kfree(twl4030_madc_bat);
227 return ret;
230 static int twl4030_madc_battery_remove(struct platform_device *pdev)
232 struct twl4030_madc_battery *bat = platform_get_drvdata(pdev);
234 power_supply_unregister(bat->psy);
235 kfree(bat);
237 return 0;
240 static struct platform_driver twl4030_madc_battery_driver = {
241 .driver = {
242 .name = "twl4030_madc_battery",
244 .probe = twl4030_madc_battery_probe,
245 .remove = twl4030_madc_battery_remove,
247 module_platform_driver(twl4030_madc_battery_driver);
249 MODULE_LICENSE("GPL");
250 MODULE_AUTHOR("Lukas Märdian <lukas@goldelico.com>");
251 MODULE_DESCRIPTION("twl4030_madc battery driver");