abd116632efa96c7e21592de7f67e236b50e8884
[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,216 @@
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 +/* Read ADCIN channel value, code copied from maemo kernel */
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 (u16)req.rbuf[channel];
48 +       else
49 +               return -ENODATA;
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 +static int rx51_battery_read_voltage(struct rx51_device_info *di)
55 +{
56 +       int voltage = rx51_battery_read_adc(12);
57 +       if (voltage < 0)
58 +               return voltage;
59 +       return 1000 * (10000 * voltage / 1705);
60 +}
61 +
62 +/* Conversation table based on experimental data */
63 +/* Usage: rx51_temp_table[rx51_temp_first - Celsius value] = lowest RAW value */
64 +static int rx51_temp_first = 48;
65 +static int rx51_temp_table[] = {
66 +        30,  31,  32,  33,  34,  35,  36,  38,  39,  40,  42,  43,  45,  47,
67 +        48,  50,  51,  53,  55,  57,  59,  61,  63,  66,  69,  72,  74,  76,
68 +        80,  83,  86,  90,  93,  96, 100, 105, 109, 114, 119, 124, 130, 136,
69 +       142, 148, 155, 161, 166, 177, 184, 193, 202, 213, 222, 230, 243, 255,
70 +       268, 280
71 +};
72 +
73 +/* Read ADCIN channel 0 (battery temp) and convert value to tenths of Celsius */
74 +/* Try to find lowest temperature value for raw value in table */
75 +static int rx51_battery_read_temperature(struct rx51_device_info *di)
76 +{
77 +       int min = 0;
78 +       int max = ARRAY_SIZE(rx51_temp_table)-1;
79 +       int temperature = rx51_battery_read_adc(0);
80 +
81 +       if (temperature < 0)
82 +               return temperature;
83 +       if (temperature < rx51_temp_table[min])
84 +               return rx51_temp_first-min+1;
85 +       if (temperature > rx51_temp_table[max])
86 +               return rx51_temp_first-max-1;
87 +
88 +       while (max-min > 1) {
89 +               int mid = (max+min)/2;
90 +               if (rx51_temp_table[mid] <= temperature)
91 +                       min = mid;
92 +               else if (rx51_temp_table[mid] > temperature)
93 +                       max = mid;
94 +               if (rx51_temp_table[mid] == temperature)
95 +                       break;
96 +       }
97 +
98 +       return (rx51_temp_first - min) * 100;
99 +}
100 +
101 +/* Read ADCIN channel 4 (BSI) and convert RAW value to micro Ah */
102 +/* This conversion formula was extracted from maemo program bsi-read */
103 +static int rx51_battery_read_capacity(struct rx51_device_info *di)
104 +{
105 +       int capacity = rx51_battery_read_adc(4);
106 +       if (capacity < 0)
107 +               return capacity;
108 +       return 1280 * (1200 * capacity)/(1024 - capacity);
109 +}
110 +
111 +/* Return power_supply property */
112 +static int rx51_battery_get_property(struct power_supply *psy,
113 +                                       enum power_supply_property psp,
114 +                                       union power_supply_propval *val)
115 +{
116 +       struct rx51_device_info *di = container_of((psy),
117 +                               struct rx51_device_info, bat);
118 +
119 +       switch (psp) {
120 +       case POWER_SUPPLY_PROP_TECHNOLOGY:
121 +               val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
122 +               break;
123 +       case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
124 +               val->intval = 4200000;
125 +               break;
126 +       case POWER_SUPPLY_PROP_PRESENT:
127 +               val->intval = rx51_battery_read_voltage(di) ? 1 : 0;
128 +               break;
129 +       case POWER_SUPPLY_PROP_VOLTAGE_NOW:
130 +               val->intval = rx51_battery_read_voltage(di);
131 +               break;
132 +       case POWER_SUPPLY_PROP_TEMP:
133 +               val->intval = rx51_battery_read_temperature(di);
134 +               break;
135 +       case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
136 +               val->intval = rx51_battery_read_capacity(di);
137 +               break;
138 +       default:
139 +               return -EINVAL;
140 +       }
141 +
142 +       if (val->intval < 0)
143 +               return val->intval;
144 +
145 +       return 0;
146 +}
147 +
148 +static enum power_supply_property rx51_battery_props[] = {
149 +       POWER_SUPPLY_PROP_TECHNOLOGY,
150 +       POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
151 +       POWER_SUPPLY_PROP_PRESENT,
152 +       POWER_SUPPLY_PROP_VOLTAGE_NOW,
153 +       POWER_SUPPLY_PROP_TEMP,
154 +       POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
155 +};
156 +
157 +static int __devinit rx51_battery_probe(struct platform_device *pdev)
158 +{
159 +       struct rx51_device_info *di;
160 +       int ret;
161 +
162 +       di = kzalloc(sizeof(*di), GFP_KERNEL);
163 +       if (!di)
164 +               return -ENOMEM;
165 +
166 +       platform_set_drvdata(pdev, di);
167 +
168 +       di->bat.name = dev_name(&pdev->dev);
169 +       di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
170 +       di->bat.properties = rx51_battery_props;
171 +       di->bat.num_properties = ARRAY_SIZE(rx51_battery_props);
172 +       di->bat.get_property = rx51_battery_get_property;
173 +
174 +       ret = power_supply_register(di->dev, &di->bat);
175 +       if (ret) {
176 +               platform_set_drvdata(pdev, NULL);
177 +               kfree(di);
178 +               return ret;
179 +       }
180 +
181 +       return 0;
182 +}
183 +
184 +static int __devexit rx51_battery_remove(struct platform_device *pdev)
185 +{
186 +       struct rx51_device_info *di = platform_get_drvdata(pdev);
187 +
188 +       power_supply_unregister(&di->bat);
189 +       platform_set_drvdata(pdev, NULL);
190 +       kfree(di);
191 +
192 +       return 0;
193 +}
194 +
195 +static struct platform_driver rx51_battery_driver = {
196 +       .probe = rx51_battery_probe,
197 +       .remove = __devexit_p(rx51_battery_remove),
198 +       .driver = {
199 +               .name = "rx51-battery",
200 +               .owner = THIS_MODULE,
201 +       },
202 +};
203 +
204 +static int __init rx51_battery_init(void)
205 +{
206 +       return platform_driver_register(&rx51_battery_driver);
207 +}
208 +module_init(rx51_battery_init);
209 +
210 +static void __exit rx51_battery_exit(void)
211 +{
212 +       platform_driver_unregister(&rx51_battery_driver);
213 +}
214 +module_exit(rx51_battery_exit);
215 +
216 +MODULE_ALIAS("platform:rx51-battery");
217 +MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
218 +MODULE_DESCRIPTION("Nokia RX-51 battery driver");
219 +MODULE_LICENSE("GPL");