diff options
author | Pali Rohár <pali.rohar@gmail.com> | 2012-10-31 05:48:40 -0400 |
---|---|---|
committer | Anton Vorontsov <anton.vorontsov@linaro.org> | 2012-11-18 18:11:33 -0500 |
commit | 04930baf28f08cd275e4afbf5a335b42335705df (patch) | |
tree | 2dfe81a39d7b656362fa090aabd4e3463da2f3b8 /drivers/power/rx51_battery.c | |
parent | a879f19fe7decb9a53d13a63edd2eced8c2895a2 (diff) |
power_supply: Add new Nokia RX-51 (N900) power supply battery driver
This driver exporting battery design capacity, temperature and voltage for
battery in Nokia RX-51. This driver is needed for open source battery
management on Nokia RX-51 (N900).
Signed-off-by: Pali Rohár <pali.rohar@gmail.com>
Signed-off-by: Anton Vorontsov <anton.vorontsov@linaro.org>
Diffstat (limited to 'drivers/power/rx51_battery.c')
-rw-r--r-- | drivers/power/rx51_battery.c | 251 |
1 files changed, 251 insertions, 0 deletions
diff --git a/drivers/power/rx51_battery.c b/drivers/power/rx51_battery.c new file mode 100644 index 000000000000..ca49d6c0ee9d --- /dev/null +++ b/drivers/power/rx51_battery.c | |||
@@ -0,0 +1,251 @@ | |||
1 | /* | ||
2 | * Nokia RX-51 battery driver | ||
3 | * | ||
4 | * Copyright (C) 2012 Pali Rohár <pali.rohar@gmail.com> | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation; either version 2 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License along | ||
17 | * with this program; if not, write to the Free Software Foundation, Inc., | ||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
19 | */ | ||
20 | |||
21 | #include <linux/module.h> | ||
22 | #include <linux/param.h> | ||
23 | #include <linux/platform_device.h> | ||
24 | #include <linux/power_supply.h> | ||
25 | #include <linux/slab.h> | ||
26 | #include <linux/i2c/twl4030-madc.h> | ||
27 | |||
28 | struct rx51_device_info { | ||
29 | struct device *dev; | ||
30 | struct power_supply bat; | ||
31 | }; | ||
32 | |||
33 | /* | ||
34 | * Read ADCIN channel value, code copied from maemo kernel | ||
35 | */ | ||
36 | static int rx51_battery_read_adc(int channel) | ||
37 | { | ||
38 | struct twl4030_madc_request req; | ||
39 | |||
40 | req.channels = 1 << channel; | ||
41 | req.do_avg = 1; | ||
42 | req.method = TWL4030_MADC_SW1; | ||
43 | req.func_cb = NULL; | ||
44 | req.type = TWL4030_MADC_WAIT; | ||
45 | |||
46 | if (twl4030_madc_conversion(&req) <= 0) | ||
47 | return -ENODATA; | ||
48 | |||
49 | return req.rbuf[channel]; | ||
50 | } | ||
51 | |||
52 | /* | ||
53 | * Read ADCIN channel 12 (voltage) and convert RAW value to micro voltage | ||
54 | * This conversion formula was extracted from maemo program bsi-read | ||
55 | */ | ||
56 | static int rx51_battery_read_voltage(struct rx51_device_info *di) | ||
57 | { | ||
58 | int voltage = rx51_battery_read_adc(12); | ||
59 | |||
60 | if (voltage < 0) | ||
61 | return voltage; | ||
62 | |||
63 | return 1000 * (10000 * voltage / 1705); | ||
64 | } | ||
65 | |||
66 | /* | ||
67 | * Temperature look-up tables | ||
68 | * TEMP = (1/(t1 + 1/298) - 273.15) | ||
69 | * Where t1 = (1/B) * ln((RAW_ADC_U * 2.5)/(R * I * 255)) | ||
70 | * Formula is based on experimental data, RX-51 CAL data, maemo program bme | ||
71 | * and formula from da9052 driver with values R = 100, B = 3380, I = 0.00671 | ||
72 | */ | ||
73 | |||
74 | /* | ||
75 | * Table1 (temperature for first 25 RAW values) | ||
76 | * Usage: TEMP = rx51_temp_table1[RAW] | ||
77 | * RAW is between 1 and 24 | ||
78 | * TEMP is between 201 C and 55 C | ||
79 | */ | ||
80 | static u8 rx51_temp_table1[] = { | ||
81 | 255, 201, 159, 138, 124, 114, 106, 99, 94, 89, 85, 82, 78, 75, | ||
82 | 73, 70, 68, 66, 64, 62, 61, 59, 57, 56, 55 | ||
83 | }; | ||
84 | |||
85 | /* | ||
86 | * Table2 (lowest RAW value for temperature) | ||
87 | * Usage: RAW = rx51_temp_table2[TEMP-rx51_temp_table2_first] | ||
88 | * TEMP is between 53 C and -32 C | ||
89 | * RAW is between 25 and 993 | ||
90 | */ | ||
91 | #define rx51_temp_table2_first 53 | ||
92 | static u16 rx51_temp_table2[] = { | ||
93 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 39, | ||
94 | 40, 41, 43, 44, 46, 48, 49, 51, 53, 55, 57, 59, 61, 64, | ||
95 | 66, 69, 71, 74, 77, 80, 83, 86, 90, 94, 97, 101, 106, 110, | ||
96 | 115, 119, 125, 130, 136, 141, 148, 154, 161, 168, 176, 184, 202, 211, | ||
97 | 221, 231, 242, 254, 266, 279, 293, 308, 323, 340, 357, 375, 395, 415, | ||
98 | 437, 460, 485, 511, 539, 568, 600, 633, 669, 706, 747, 790, 836, 885, | ||
99 | 937, 993, 1024 | ||
100 | }; | ||
101 | |||
102 | /* | ||
103 | * Read ADCIN channel 0 (battery temp) and convert value to tenths of Celsius | ||
104 | * Use Temperature look-up tables for conversation | ||
105 | */ | ||
106 | static int rx51_battery_read_temperature(struct rx51_device_info *di) | ||
107 | { | ||
108 | int min = 0; | ||
109 | int max = ARRAY_SIZE(rx51_temp_table2) - 1; | ||
110 | int raw = rx51_battery_read_adc(0); | ||
111 | |||
112 | /* Zero and negative values are undefined */ | ||
113 | if (raw <= 0) | ||
114 | return INT_MAX; | ||
115 | |||
116 | /* ADC channels are 10 bit, higher value are undefined */ | ||
117 | if (raw >= (1 << 10)) | ||
118 | return INT_MIN; | ||
119 | |||
120 | /* First check for temperature in first direct table */ | ||
121 | if (raw < ARRAY_SIZE(rx51_temp_table1)) | ||
122 | return rx51_temp_table1[raw] * 100; | ||
123 | |||
124 | /* Binary search RAW value in second inverse table */ | ||
125 | while (max - min > 1) { | ||
126 | int mid = (max + min) / 2; | ||
127 | if (rx51_temp_table2[mid] <= raw) | ||
128 | min = mid; | ||
129 | else if (rx51_temp_table2[mid] > raw) | ||
130 | max = mid; | ||
131 | if (rx51_temp_table2[mid] == raw) | ||
132 | break; | ||
133 | } | ||
134 | |||
135 | return (rx51_temp_table2_first - min) * 100; | ||
136 | } | ||
137 | |||
138 | /* | ||
139 | * Read ADCIN channel 4 (BSI) and convert RAW value to micro Ah | ||
140 | * This conversion formula was extracted from maemo program bsi-read | ||
141 | */ | ||
142 | static int rx51_battery_read_capacity(struct rx51_device_info *di) | ||
143 | { | ||
144 | int capacity = rx51_battery_read_adc(4); | ||
145 | |||
146 | if (capacity < 0) | ||
147 | return capacity; | ||
148 | |||
149 | return 1280 * (1200 * capacity)/(1024 - capacity); | ||
150 | } | ||
151 | |||
152 | /* | ||
153 | * Return power_supply property | ||
154 | */ | ||
155 | static int rx51_battery_get_property(struct power_supply *psy, | ||
156 | enum power_supply_property psp, | ||
157 | union power_supply_propval *val) | ||
158 | { | ||
159 | struct rx51_device_info *di = container_of((psy), | ||
160 | struct rx51_device_info, bat); | ||
161 | |||
162 | switch (psp) { | ||
163 | case POWER_SUPPLY_PROP_TECHNOLOGY: | ||
164 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; | ||
165 | break; | ||
166 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: | ||
167 | val->intval = 4200000; | ||
168 | break; | ||
169 | case POWER_SUPPLY_PROP_PRESENT: | ||
170 | val->intval = rx51_battery_read_voltage(di) ? 1 : 0; | ||
171 | break; | ||
172 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: | ||
173 | val->intval = rx51_battery_read_voltage(di); | ||
174 | break; | ||
175 | case POWER_SUPPLY_PROP_TEMP: | ||
176 | val->intval = rx51_battery_read_temperature(di); | ||
177 | break; | ||
178 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: | ||
179 | val->intval = rx51_battery_read_capacity(di); | ||
180 | break; | ||
181 | default: | ||
182 | return -EINVAL; | ||
183 | } | ||
184 | |||
185 | if (val->intval == INT_MAX || val->intval == INT_MIN) | ||
186 | return -EINVAL; | ||
187 | |||
188 | return 0; | ||
189 | } | ||
190 | |||
191 | static enum power_supply_property rx51_battery_props[] = { | ||
192 | POWER_SUPPLY_PROP_TECHNOLOGY, | ||
193 | POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, | ||
194 | POWER_SUPPLY_PROP_PRESENT, | ||
195 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | ||
196 | POWER_SUPPLY_PROP_TEMP, | ||
197 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, | ||
198 | }; | ||
199 | |||
200 | static int __devinit rx51_battery_probe(struct platform_device *pdev) | ||
201 | { | ||
202 | struct rx51_device_info *di; | ||
203 | int ret; | ||
204 | |||
205 | di = kzalloc(sizeof(*di), GFP_KERNEL); | ||
206 | if (!di) | ||
207 | return -ENOMEM; | ||
208 | |||
209 | platform_set_drvdata(pdev, di); | ||
210 | |||
211 | di->bat.name = dev_name(&pdev->dev); | ||
212 | di->bat.type = POWER_SUPPLY_TYPE_BATTERY; | ||
213 | di->bat.properties = rx51_battery_props; | ||
214 | di->bat.num_properties = ARRAY_SIZE(rx51_battery_props); | ||
215 | di->bat.get_property = rx51_battery_get_property; | ||
216 | |||
217 | ret = power_supply_register(di->dev, &di->bat); | ||
218 | if (ret) { | ||
219 | platform_set_drvdata(pdev, NULL); | ||
220 | kfree(di); | ||
221 | return ret; | ||
222 | } | ||
223 | |||
224 | return 0; | ||
225 | } | ||
226 | |||
227 | static int __devexit rx51_battery_remove(struct platform_device *pdev) | ||
228 | { | ||
229 | struct rx51_device_info *di = platform_get_drvdata(pdev); | ||
230 | |||
231 | power_supply_unregister(&di->bat); | ||
232 | platform_set_drvdata(pdev, NULL); | ||
233 | kfree(di); | ||
234 | |||
235 | return 0; | ||
236 | } | ||
237 | |||
238 | static struct platform_driver rx51_battery_driver = { | ||
239 | .probe = rx51_battery_probe, | ||
240 | .remove = __devexit_p(rx51_battery_remove), | ||
241 | .driver = { | ||
242 | .name = "rx51-battery", | ||
243 | .owner = THIS_MODULE, | ||
244 | }, | ||
245 | }; | ||
246 | module_platform_driver(rx51_battery_driver); | ||
247 | |||
248 | MODULE_ALIAS("platform:rx51-battery"); | ||
249 | MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>"); | ||
250 | MODULE_DESCRIPTION("Nokia RX-51 battery driver"); | ||
251 | MODULE_LICENSE("GPL"); | ||