2 +++ kernel-power/drivers/power/rx51_battery.c
5 + rx51_battery.c - Nokia RX-51 battery driver
6 + Copyright (C) 2012 Pali Rohár <pali.rohar@gmail.com>
8 + This program is free software; you can redistribute it and/or modify
9 + it under the terms of the GNU General Public License as published by
10 + the Free Software Foundation; either version 2 of the License, or
11 + (at your option) any later version.
13 + This program is distributed in the hope that it will be useful,
14 + but WITHOUT ANY WARRANTY; without even the implied warranty of
15 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 + GNU General Public License for more details.
18 + You should have received a copy of the GNU General Public License along
19 + with this program; if not, write to the Free Software Foundation, Inc.,
20 + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 +#include <linux/module.h>
24 +#include <linux/param.h>
25 +#include <linux/platform_device.h>
26 +#include <linux/power_supply.h>
27 +#include <linux/slab.h>
28 +#include <linux/i2c/twl4030-madc.h>
30 +struct rx51_device_info {
32 + struct power_supply bat;
36 + Read ADCIN channel value, code copied from maemo kernel
38 +static int rx51_battery_read_adc(int channel)
40 + struct twl4030_madc_request req;
42 + req.channels = (1 << channel);
44 + req.method = TWL4030_MADC_SW1;
46 + req.type = TWL4030_MADC_WAIT;
48 + if (twl4030_madc_conversion(&req) > 0)
49 + return req.rbuf[channel];
55 + Read ADCIN channel 12 (voltage) and convert RAW value to micro voltage
56 + This conversion formula was extracted from maemo program bsi-read
58 +static int rx51_battery_read_voltage(struct rx51_device_info *di)
60 + int voltage = rx51_battery_read_adc(12);
63 + return 1000 * (10000 * voltage / 1705);
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
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
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
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
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,
103 + Read ADCIN channel 0 (battery temp) and convert value to tenths of Celsius
104 + Use Temperature look-up tables for conversation
106 +static int rx51_battery_read_temperature(struct rx51_device_info *di)
109 + int max = ARRAY_SIZE(rx51_temp_table2)-1;
110 + int raw = rx51_battery_read_adc(0);
112 + /* Zero and negative values are undefined */
116 + /* ADC channels are 10 bit, higher value are undefined */
117 + if (raw >= (1 << 10))
120 + /* First check for temperature in first direct table */
121 + if (raw < ARRAY_SIZE(rx51_temp_table1))
122 + return rx51_temp_table1[raw] * 100;
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)
129 + else if (rx51_temp_table2[mid] > raw)
131 + if (rx51_temp_table2[mid] == raw)
135 + return (rx51_temp_table2_first - min) * 100;
139 + Read ADCIN channel 4 (BSI) and convert RAW value to micro Ah
140 + This conversion formula was extracted from maemo program bsi-read
142 +static int rx51_battery_read_capacity(struct rx51_device_info *di)
144 + int capacity = rx51_battery_read_adc(4);
147 + return 1280 * (1200 * capacity)/(1024 - capacity);
151 + Return power_supply property
153 +static int rx51_battery_get_property(struct power_supply *psy,
154 + enum power_supply_property psp,
155 + union power_supply_propval *val)
157 + struct rx51_device_info *di = container_of((psy),
158 + struct rx51_device_info, bat);
161 + case POWER_SUPPLY_PROP_TECHNOLOGY:
162 + val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
164 + case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
165 + val->intval = 4200000;
167 + case POWER_SUPPLY_PROP_PRESENT:
168 + val->intval = rx51_battery_read_voltage(di) ? 1 : 0;
170 + case POWER_SUPPLY_PROP_VOLTAGE_NOW:
171 + val->intval = rx51_battery_read_voltage(di);
173 + case POWER_SUPPLY_PROP_TEMP:
174 + val->intval = rx51_battery_read_temperature(di);
176 + case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
177 + val->intval = rx51_battery_read_capacity(di);
183 + if (val->intval == INT_MAX || val->intval == INT_MIN)
189 +static enum power_supply_property rx51_battery_props[] = {
190 + POWER_SUPPLY_PROP_TECHNOLOGY,
191 + POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
192 + POWER_SUPPLY_PROP_PRESENT,
193 + POWER_SUPPLY_PROP_VOLTAGE_NOW,
194 + POWER_SUPPLY_PROP_TEMP,
195 + POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
198 +static int __devinit rx51_battery_probe(struct platform_device *pdev)
200 + struct rx51_device_info *di;
203 + di = kzalloc(sizeof(*di), GFP_KERNEL);
207 + platform_set_drvdata(pdev, di);
209 + di->bat.name = dev_name(&pdev->dev);
210 + di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
211 + di->bat.properties = rx51_battery_props;
212 + di->bat.num_properties = ARRAY_SIZE(rx51_battery_props);
213 + di->bat.get_property = rx51_battery_get_property;
215 + ret = power_supply_register(di->dev, &di->bat);
217 + platform_set_drvdata(pdev, NULL);
225 +static int __devexit rx51_battery_remove(struct platform_device *pdev)
227 + struct rx51_device_info *di = platform_get_drvdata(pdev);
229 + power_supply_unregister(&di->bat);
230 + platform_set_drvdata(pdev, NULL);
236 +static struct platform_driver rx51_battery_driver = {
237 + .probe = rx51_battery_probe,
238 + .remove = __devexit_p(rx51_battery_remove),
240 + .name = "rx51-battery",
241 + .owner = THIS_MODULE,
245 +static int __init rx51_battery_init(void)
247 + return platform_driver_register(&rx51_battery_driver);
249 +module_init(rx51_battery_init);
251 +static void __exit rx51_battery_exit(void)
253 + platform_driver_unregister(&rx51_battery_driver);
255 +module_exit(rx51_battery_exit);
257 +MODULE_ALIAS("platform:rx51-battery");
258 +MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
259 +MODULE_DESCRIPTION("Nokia RX-51 battery driver");
260 +MODULE_LICENSE("GPL");