Commit | Line | Data |
---|---|---|
04930baf PR |
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> | |
57da5e86 SR |
27 | #include <linux/iio/consumer.h> |
28 | #include <linux/of.h> | |
8e2747f0 | 29 | |
04930baf PR |
30 | struct rx51_device_info { |
31 | struct device *dev; | |
297d716f KK |
32 | struct power_supply *bat; |
33 | struct power_supply_desc bat_desc; | |
57da5e86 SR |
34 | struct iio_channel *channel_temp; |
35 | struct iio_channel *channel_bsi; | |
36 | struct iio_channel *channel_vbat; | |
04930baf PR |
37 | }; |
38 | ||
39 | /* | |
40 | * Read ADCIN channel value, code copied from maemo kernel | |
41 | */ | |
57da5e86 | 42 | static int rx51_battery_read_adc(struct iio_channel *channel) |
04930baf | 43 | { |
57da5e86 SR |
44 | int val, err; |
45 | err = iio_read_channel_average_raw(channel, &val); | |
46 | if (err < 0) | |
47 | return err; | |
48 | return val; | |
04930baf PR |
49 | } |
50 | ||
51 | /* | |
52 | * Read ADCIN channel 12 (voltage) and convert RAW value to micro voltage | |
53 | * This conversion formula was extracted from maemo program bsi-read | |
54 | */ | |
55 | static int rx51_battery_read_voltage(struct rx51_device_info *di) | |
56 | { | |
57da5e86 | 57 | int voltage = rx51_battery_read_adc(di->channel_vbat); |
04930baf | 58 | |
57da5e86 SR |
59 | if (voltage < 0) { |
60 | dev_err(di->dev, "Could not read ADC: %d\n", voltage); | |
04930baf | 61 | return voltage; |
57da5e86 | 62 | } |
04930baf PR |
63 | |
64 | return 1000 * (10000 * voltage / 1705); | |
65 | } | |
66 | ||
67 | /* | |
68 | * Temperature look-up tables | |
69 | * TEMP = (1/(t1 + 1/298) - 273.15) | |
70 | * Where t1 = (1/B) * ln((RAW_ADC_U * 2.5)/(R * I * 255)) | |
71 | * Formula is based on experimental data, RX-51 CAL data, maemo program bme | |
72 | * and formula from da9052 driver with values R = 100, B = 3380, I = 0.00671 | |
73 | */ | |
74 | ||
75 | /* | |
76 | * Table1 (temperature for first 25 RAW values) | |
77 | * Usage: TEMP = rx51_temp_table1[RAW] | |
78 | * RAW is between 1 and 24 | |
79 | * TEMP is between 201 C and 55 C | |
80 | */ | |
81 | static u8 rx51_temp_table1[] = { | |
82 | 255, 201, 159, 138, 124, 114, 106, 99, 94, 89, 85, 82, 78, 75, | |
83 | 73, 70, 68, 66, 64, 62, 61, 59, 57, 56, 55 | |
84 | }; | |
85 | ||
86 | /* | |
87 | * Table2 (lowest RAW value for temperature) | |
88 | * Usage: RAW = rx51_temp_table2[TEMP-rx51_temp_table2_first] | |
89 | * TEMP is between 53 C and -32 C | |
90 | * RAW is between 25 and 993 | |
91 | */ | |
92 | #define rx51_temp_table2_first 53 | |
93 | static u16 rx51_temp_table2[] = { | |
94 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 39, | |
95 | 40, 41, 43, 44, 46, 48, 49, 51, 53, 55, 57, 59, 61, 64, | |
96 | 66, 69, 71, 74, 77, 80, 83, 86, 90, 94, 97, 101, 106, 110, | |
97 | 115, 119, 125, 130, 136, 141, 148, 154, 161, 168, 176, 184, 202, 211, | |
98 | 221, 231, 242, 254, 266, 279, 293, 308, 323, 340, 357, 375, 395, 415, | |
99 | 437, 460, 485, 511, 539, 568, 600, 633, 669, 706, 747, 790, 836, 885, | |
100 | 937, 993, 1024 | |
101 | }; | |
102 | ||
103 | /* | |
104 | * Read ADCIN channel 0 (battery temp) and convert value to tenths of Celsius | |
105 | * Use Temperature look-up tables for conversation | |
106 | */ | |
107 | static int rx51_battery_read_temperature(struct rx51_device_info *di) | |
108 | { | |
109 | int min = 0; | |
110 | int max = ARRAY_SIZE(rx51_temp_table2) - 1; | |
57da5e86 SR |
111 | int raw = rx51_battery_read_adc(di->channel_temp); |
112 | ||
113 | if (raw < 0) | |
114 | dev_err(di->dev, "Could not read ADC: %d\n", raw); | |
04930baf PR |
115 | |
116 | /* Zero and negative values are undefined */ | |
117 | if (raw <= 0) | |
118 | return INT_MAX; | |
119 | ||
120 | /* ADC channels are 10 bit, higher value are undefined */ | |
121 | if (raw >= (1 << 10)) | |
122 | return INT_MIN; | |
123 | ||
124 | /* First check for temperature in first direct table */ | |
125 | if (raw < ARRAY_SIZE(rx51_temp_table1)) | |
59584857 | 126 | return rx51_temp_table1[raw] * 10; |
04930baf PR |
127 | |
128 | /* Binary search RAW value in second inverse table */ | |
129 | while (max - min > 1) { | |
130 | int mid = (max + min) / 2; | |
131 | if (rx51_temp_table2[mid] <= raw) | |
132 | min = mid; | |
133 | else if (rx51_temp_table2[mid] > raw) | |
134 | max = mid; | |
135 | if (rx51_temp_table2[mid] == raw) | |
136 | break; | |
137 | } | |
138 | ||
59584857 | 139 | return (rx51_temp_table2_first - min) * 10; |
04930baf PR |
140 | } |
141 | ||
142 | /* | |
143 | * Read ADCIN channel 4 (BSI) and convert RAW value to micro Ah | |
144 | * This conversion formula was extracted from maemo program bsi-read | |
145 | */ | |
146 | static int rx51_battery_read_capacity(struct rx51_device_info *di) | |
147 | { | |
57da5e86 | 148 | int capacity = rx51_battery_read_adc(di->channel_bsi); |
04930baf | 149 | |
57da5e86 SR |
150 | if (capacity < 0) { |
151 | dev_err(di->dev, "Could not read ADC: %d\n", capacity); | |
04930baf | 152 | return capacity; |
57da5e86 | 153 | } |
04930baf PR |
154 | |
155 | return 1280 * (1200 * capacity)/(1024 - capacity); | |
156 | } | |
157 | ||
158 | /* | |
159 | * Return power_supply property | |
160 | */ | |
161 | static int rx51_battery_get_property(struct power_supply *psy, | |
162 | enum power_supply_property psp, | |
163 | union power_supply_propval *val) | |
164 | { | |
297d716f | 165 | struct rx51_device_info *di = power_supply_get_drvdata(psy); |
04930baf PR |
166 | |
167 | switch (psp) { | |
168 | case POWER_SUPPLY_PROP_TECHNOLOGY: | |
169 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; | |
170 | break; | |
171 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: | |
172 | val->intval = 4200000; | |
173 | break; | |
174 | case POWER_SUPPLY_PROP_PRESENT: | |
175 | val->intval = rx51_battery_read_voltage(di) ? 1 : 0; | |
176 | break; | |
177 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: | |
178 | val->intval = rx51_battery_read_voltage(di); | |
179 | break; | |
180 | case POWER_SUPPLY_PROP_TEMP: | |
181 | val->intval = rx51_battery_read_temperature(di); | |
182 | break; | |
183 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: | |
184 | val->intval = rx51_battery_read_capacity(di); | |
185 | break; | |
186 | default: | |
187 | return -EINVAL; | |
188 | } | |
189 | ||
190 | if (val->intval == INT_MAX || val->intval == INT_MIN) | |
191 | return -EINVAL; | |
192 | ||
193 | return 0; | |
194 | } | |
195 | ||
196 | static enum power_supply_property rx51_battery_props[] = { | |
197 | POWER_SUPPLY_PROP_TECHNOLOGY, | |
198 | POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, | |
199 | POWER_SUPPLY_PROP_PRESENT, | |
200 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | |
201 | POWER_SUPPLY_PROP_TEMP, | |
202 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, | |
203 | }; | |
204 | ||
6d2cea4f | 205 | static int rx51_battery_probe(struct platform_device *pdev) |
04930baf | 206 | { |
297d716f | 207 | struct power_supply_config psy_cfg = {}; |
04930baf PR |
208 | struct rx51_device_info *di; |
209 | int ret; | |
210 | ||
b852ac51 | 211 | di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL); |
04930baf PR |
212 | if (!di) |
213 | return -ENOMEM; | |
214 | ||
215 | platform_set_drvdata(pdev, di); | |
216 | ||
57da5e86 | 217 | di->dev = &pdev->dev; |
b5e57863 | 218 | di->bat_desc.name = "rx51-battery"; |
297d716f KK |
219 | di->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY; |
220 | di->bat_desc.properties = rx51_battery_props; | |
221 | di->bat_desc.num_properties = ARRAY_SIZE(rx51_battery_props); | |
222 | di->bat_desc.get_property = rx51_battery_get_property; | |
223 | ||
224 | psy_cfg.drv_data = di; | |
04930baf | 225 | |
57da5e86 SR |
226 | di->channel_temp = iio_channel_get(di->dev, "temp"); |
227 | if (IS_ERR(di->channel_temp)) { | |
228 | ret = PTR_ERR(di->channel_temp); | |
229 | goto error; | |
230 | } | |
231 | ||
232 | di->channel_bsi = iio_channel_get(di->dev, "bsi"); | |
233 | if (IS_ERR(di->channel_bsi)) { | |
234 | ret = PTR_ERR(di->channel_bsi); | |
235 | goto error_channel_temp; | |
236 | } | |
237 | ||
238 | di->channel_vbat = iio_channel_get(di->dev, "vbat"); | |
239 | if (IS_ERR(di->channel_vbat)) { | |
240 | ret = PTR_ERR(di->channel_vbat); | |
241 | goto error_channel_bsi; | |
242 | } | |
243 | ||
297d716f KK |
244 | di->bat = power_supply_register(di->dev, &di->bat_desc, &psy_cfg); |
245 | if (IS_ERR(di->bat)) { | |
246 | ret = PTR_ERR(di->bat); | |
57da5e86 | 247 | goto error_channel_vbat; |
297d716f | 248 | } |
04930baf PR |
249 | |
250 | return 0; | |
57da5e86 SR |
251 | |
252 | error_channel_vbat: | |
253 | iio_channel_release(di->channel_vbat); | |
254 | error_channel_bsi: | |
255 | iio_channel_release(di->channel_bsi); | |
256 | error_channel_temp: | |
257 | iio_channel_release(di->channel_temp); | |
258 | error: | |
259 | ||
260 | return ret; | |
04930baf PR |
261 | } |
262 | ||
6d2cea4f | 263 | static int rx51_battery_remove(struct platform_device *pdev) |
04930baf PR |
264 | { |
265 | struct rx51_device_info *di = platform_get_drvdata(pdev); | |
266 | ||
297d716f | 267 | power_supply_unregister(di->bat); |
04930baf | 268 | |
57da5e86 SR |
269 | iio_channel_release(di->channel_vbat); |
270 | iio_channel_release(di->channel_bsi); | |
271 | iio_channel_release(di->channel_temp); | |
272 | ||
04930baf PR |
273 | return 0; |
274 | } | |
275 | ||
57da5e86 SR |
276 | #ifdef CONFIG_OF |
277 | static const struct of_device_id n900_battery_of_match[] = { | |
278 | {.compatible = "nokia,n900-battery", }, | |
279 | { }, | |
280 | }; | |
281 | MODULE_DEVICE_TABLE(of, n900_battery_of_match); | |
282 | #endif | |
283 | ||
04930baf PR |
284 | static struct platform_driver rx51_battery_driver = { |
285 | .probe = rx51_battery_probe, | |
6d2cea4f | 286 | .remove = rx51_battery_remove, |
04930baf PR |
287 | .driver = { |
288 | .name = "rx51-battery", | |
57da5e86 | 289 | .of_match_table = of_match_ptr(n900_battery_of_match), |
04930baf PR |
290 | }, |
291 | }; | |
292 | module_platform_driver(rx51_battery_driver); | |
293 | ||
294 | MODULE_ALIAS("platform:rx51-battery"); | |
295 | MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>"); | |
296 | MODULE_DESCRIPTION("Nokia RX-51 battery driver"); | |
297 | MODULE_LICENSE("GPL"); |