Update rx51_battery.patch: Extended temperature tables
[kernel-power] / kernel-power-2.6.28 / debian / patches / rx51_battery.patch
1 --- /dev/null
2 +++ kernel-power/drivers/power/rx51_battery.c
3 @@ -0,0 +1,263 @@
4 +/*
5 +    rx51_battery.c - Nokia RX-51 battery driver
6 +    Copyright (C) 2012  Pali Rohár <pali.rohar@gmail.com>
7 +
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.
12 +
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.
17 +
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.
21 +*/
22 +
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>
29 +
30 +struct rx51_device_info {
31 +       struct device *dev;
32 +       struct power_supply bat;
33 +};
34 +
35 +/*
36 +   Read ADCIN channel value, code copied from maemo kernel
37 + */
38 +static int rx51_battery_read_adc(int channel)
39 +{
40 +       struct twl4030_madc_request req;
41 +
42 +       req.channels = (1 << channel);
43 +       req.do_avg = 1;
44 +       req.method = TWL4030_MADC_SW1;
45 +       req.func_cb = NULL;
46 +       req.type = TWL4030_MADC_WAIT;
47 +
48 +       if (twl4030_madc_conversion(&req) > 0)
49 +               return req.rbuf[channel];
50 +       else
51 +               return -ENODATA;
52 +}
53 +
54 +/*
55 +   Read ADCIN channel 12 (voltage) and convert RAW value to micro voltage
56 +   This conversion formula was extracted from maemo program bsi-read
57 + */
58 +static int rx51_battery_read_voltage(struct rx51_device_info *di)
59 +{
60 +       int voltage = rx51_battery_read_adc(12);
61 +       if (voltage < 0)
62 +               return voltage;
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 +static u8 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
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];
123 +
124 +       /* Possible correct values which are not in tables */
125 +       if (raw < rx51_temp_table2[min])
126 +               return rx51_temp_table2_first-min;
127 +       if (raw > rx51_temp_table2[max])
128 +               return rx51_temp_table2_first-max;
129 +
130 +       /* Binary search RAW value in second inverse table */
131 +       while (max-min > 1) {
132 +               int mid = (max+min)/2;
133 +               if (rx51_temp_table2[mid] <= raw)
134 +                       min = mid;
135 +               else if (rx51_temp_table2[mid] > raw)
136 +                       max = mid;
137 +               if (rx51_temp_table2[mid] == raw)
138 +                       break;
139 +       }
140 +
141 +       return (rx51_temp_table2_first - min) * 100;
142 +}
143 +
144 +/*
145 +   Read ADCIN channel 4 (BSI) and convert RAW value to micro Ah
146 +   This conversion formula was extracted from maemo program bsi-read
147 + */
148 +static int rx51_battery_read_capacity(struct rx51_device_info *di)
149 +{
150 +       int capacity = rx51_battery_read_adc(4);
151 +       if (capacity < 0)
152 +               return capacity;
153 +       return 1280 * (1200 * capacity)/(1024 - capacity);
154 +}
155 +
156 +/*
157 +   Return power_supply property
158 + */
159 +static int rx51_battery_get_property(struct power_supply *psy,
160 +                                       enum power_supply_property psp,
161 +                                       union power_supply_propval *val)
162 +{
163 +       struct rx51_device_info *di = container_of((psy),
164 +                               struct rx51_device_info, bat);
165 +
166 +       switch (psp) {
167 +       case POWER_SUPPLY_PROP_TECHNOLOGY:
168 +               val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
169 +               break;
170 +       case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
171 +               val->intval = 4200000;
172 +               break;
173 +       case POWER_SUPPLY_PROP_PRESENT:
174 +               val->intval = rx51_battery_read_voltage(di) ? 1 : 0;
175 +               break;
176 +       case POWER_SUPPLY_PROP_VOLTAGE_NOW:
177 +               val->intval = rx51_battery_read_voltage(di);
178 +               break;
179 +       case POWER_SUPPLY_PROP_TEMP:
180 +               val->intval = rx51_battery_read_temperature(di);
181 +               break;
182 +       case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
183 +               val->intval = rx51_battery_read_capacity(di);
184 +               break;
185 +       default:
186 +               return -EINVAL;
187 +       }
188 +
189 +       if (val->intval == INT_MAX || val->intval == INT_MIN)
190 +               return -EINVAL;
191 +
192 +       return 0;
193 +}
194 +
195 +static enum power_supply_property rx51_battery_props[] = {
196 +       POWER_SUPPLY_PROP_TECHNOLOGY,
197 +       POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
198 +       POWER_SUPPLY_PROP_PRESENT,
199 +       POWER_SUPPLY_PROP_VOLTAGE_NOW,
200 +       POWER_SUPPLY_PROP_TEMP,
201 +       POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
202 +};
203 +
204 +static int __devinit rx51_battery_probe(struct platform_device *pdev)
205 +{
206 +       struct rx51_device_info *di;
207 +       int ret;
208 +
209 +       di = kzalloc(sizeof(*di), GFP_KERNEL);
210 +       if (!di)
211 +               return -ENOMEM;
212 +
213 +       platform_set_drvdata(pdev, di);
214 +
215 +       di->bat.name = dev_name(&pdev->dev);
216 +       di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
217 +       di->bat.properties = rx51_battery_props;
218 +       di->bat.num_properties = ARRAY_SIZE(rx51_battery_props);
219 +       di->bat.get_property = rx51_battery_get_property;
220 +
221 +       ret = power_supply_register(di->dev, &di->bat);
222 +       if (ret) {
223 +               platform_set_drvdata(pdev, NULL);
224 +               kfree(di);
225 +               return ret;
226 +       }
227 +
228 +       return 0;
229 +}
230 +
231 +static int __devexit rx51_battery_remove(struct platform_device *pdev)
232 +{
233 +       struct rx51_device_info *di = platform_get_drvdata(pdev);
234 +
235 +       power_supply_unregister(&di->bat);
236 +       platform_set_drvdata(pdev, NULL);
237 +       kfree(di);
238 +
239 +       return 0;
240 +}
241 +
242 +static struct platform_driver rx51_battery_driver = {
243 +       .probe = rx51_battery_probe,
244 +       .remove = __devexit_p(rx51_battery_remove),
245 +       .driver = {
246 +               .name = "rx51-battery",
247 +               .owner = THIS_MODULE,
248 +       },
249 +};
250 +
251 +static int __init rx51_battery_init(void)
252 +{
253 +       return platform_driver_register(&rx51_battery_driver);
254 +}
255 +module_init(rx51_battery_init);
256 +
257 +static void __exit rx51_battery_exit(void)
258 +{
259 +       platform_driver_unregister(&rx51_battery_driver);
260 +}
261 +module_exit(rx51_battery_exit);
262 +
263 +MODULE_ALIAS("platform:rx51-battery");
264 +MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
265 +MODULE_DESCRIPTION("Nokia RX-51 battery driver");
266 +MODULE_LICENSE("GPL");