blob: 8a6288d87056ef40f92538ce30b30eded9763a9a [file] [log] [blame]
Pali Rohár04930ba2012-10-31 10:48:40 +01001/*
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
28struct 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 */
36static 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;
Pali Rohár8059c1c2013-02-15 23:56:50 +010045 req.raw = true;
Pali Rohár04930ba2012-10-31 10:48:40 +010046
47 if (twl4030_madc_conversion(&req) <= 0)
48 return -ENODATA;
49
50 return req.rbuf[channel];
51}
52
53/*
54 * Read ADCIN channel 12 (voltage) and convert RAW value to micro voltage
55 * This conversion formula was extracted from maemo program bsi-read
56 */
57static int rx51_battery_read_voltage(struct rx51_device_info *di)
58{
59 int voltage = rx51_battery_read_adc(12);
60
61 if (voltage < 0)
62 return voltage;
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 */
81static 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
93static 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 */
107static int rx51_battery_read_temperature(struct rx51_device_info *di)
108{
109 int min = 0;
110 int max = ARRAY_SIZE(rx51_temp_table2) - 1;
111 int raw = rx51_battery_read_adc(0);
112
113 /* Zero and negative values are undefined */
114 if (raw <= 0)
115 return INT_MAX;
116
117 /* ADC channels are 10 bit, higher value are undefined */
118 if (raw >= (1 << 10))
119 return INT_MIN;
120
121 /* First check for temperature in first direct table */
122 if (raw < ARRAY_SIZE(rx51_temp_table1))
Pali Rohár59584852013-03-28 17:42:23 +0100123 return rx51_temp_table1[raw] * 10;
Pali Rohár04930ba2012-10-31 10:48:40 +0100124
125 /* Binary search RAW value in second inverse table */
126 while (max - min > 1) {
127 int mid = (max + min) / 2;
128 if (rx51_temp_table2[mid] <= raw)
129 min = mid;
130 else if (rx51_temp_table2[mid] > raw)
131 max = mid;
132 if (rx51_temp_table2[mid] == raw)
133 break;
134 }
135
Pali Rohár59584852013-03-28 17:42:23 +0100136 return (rx51_temp_table2_first - min) * 10;
Pali Rohár04930ba2012-10-31 10:48:40 +0100137}
138
139/*
140 * Read ADCIN channel 4 (BSI) and convert RAW value to micro Ah
141 * This conversion formula was extracted from maemo program bsi-read
142 */
143static int rx51_battery_read_capacity(struct rx51_device_info *di)
144{
145 int capacity = rx51_battery_read_adc(4);
146
147 if (capacity < 0)
148 return capacity;
149
150 return 1280 * (1200 * capacity)/(1024 - capacity);
151}
152
153/*
154 * Return power_supply property
155 */
156static int rx51_battery_get_property(struct power_supply *psy,
157 enum power_supply_property psp,
158 union power_supply_propval *val)
159{
160 struct rx51_device_info *di = container_of((psy),
161 struct rx51_device_info, bat);
162
163 switch (psp) {
164 case POWER_SUPPLY_PROP_TECHNOLOGY:
165 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
166 break;
167 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
168 val->intval = 4200000;
169 break;
170 case POWER_SUPPLY_PROP_PRESENT:
171 val->intval = rx51_battery_read_voltage(di) ? 1 : 0;
172 break;
173 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
174 val->intval = rx51_battery_read_voltage(di);
175 break;
176 case POWER_SUPPLY_PROP_TEMP:
177 val->intval = rx51_battery_read_temperature(di);
178 break;
179 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
180 val->intval = rx51_battery_read_capacity(di);
181 break;
182 default:
183 return -EINVAL;
184 }
185
186 if (val->intval == INT_MAX || val->intval == INT_MIN)
187 return -EINVAL;
188
189 return 0;
190}
191
192static enum power_supply_property rx51_battery_props[] = {
193 POWER_SUPPLY_PROP_TECHNOLOGY,
194 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
195 POWER_SUPPLY_PROP_PRESENT,
196 POWER_SUPPLY_PROP_VOLTAGE_NOW,
197 POWER_SUPPLY_PROP_TEMP,
198 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
199};
200
Greg Kroah-Hartman6d2cea42012-12-21 15:04:54 -0800201static int rx51_battery_probe(struct platform_device *pdev)
Pali Rohár04930ba2012-10-31 10:48:40 +0100202{
203 struct rx51_device_info *di;
204 int ret;
205
Jingoo Hanb852ac52013-03-11 15:35:58 +0900206 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
Pali Rohár04930ba2012-10-31 10:48:40 +0100207 if (!di)
208 return -ENOMEM;
209
210 platform_set_drvdata(pdev, di);
211
212 di->bat.name = dev_name(&pdev->dev);
213 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
214 di->bat.properties = rx51_battery_props;
215 di->bat.num_properties = ARRAY_SIZE(rx51_battery_props);
216 di->bat.get_property = rx51_battery_get_property;
217
218 ret = power_supply_register(di->dev, &di->bat);
Jingoo Hanf70d7392013-05-06 13:23:45 +0900219 if (ret)
Pali Rohár04930ba2012-10-31 10:48:40 +0100220 return ret;
Pali Rohár04930ba2012-10-31 10:48:40 +0100221
222 return 0;
223}
224
Greg Kroah-Hartman6d2cea42012-12-21 15:04:54 -0800225static int rx51_battery_remove(struct platform_device *pdev)
Pali Rohár04930ba2012-10-31 10:48:40 +0100226{
227 struct rx51_device_info *di = platform_get_drvdata(pdev);
228
229 power_supply_unregister(&di->bat);
Pali Rohár04930ba2012-10-31 10:48:40 +0100230
231 return 0;
232}
233
234static struct platform_driver rx51_battery_driver = {
235 .probe = rx51_battery_probe,
Greg Kroah-Hartman6d2cea42012-12-21 15:04:54 -0800236 .remove = rx51_battery_remove,
Pali Rohár04930ba2012-10-31 10:48:40 +0100237 .driver = {
238 .name = "rx51-battery",
239 .owner = THIS_MODULE,
240 },
241};
242module_platform_driver(rx51_battery_driver);
243
244MODULE_ALIAS("platform:rx51-battery");
245MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
246MODULE_DESCRIPTION("Nokia RX-51 battery driver");
247MODULE_LICENSE("GPL");