2 * Nokia RX-51 battery driver
4 * Copyright (C) 2012 Pali Rohár <pali.rohar@gmail.com>
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.
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.
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.
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>
28 struct rx51_device_info
{
30 struct power_supply bat
;
34 * Read ADCIN channel value, code copied from maemo kernel
36 static int rx51_battery_read_adc(int channel
)
38 struct twl4030_madc_request req
;
40 req
.channels
= 1 << channel
;
42 req
.method
= TWL4030_MADC_SW1
;
44 req
.type
= TWL4030_MADC_WAIT
;
46 if (twl4030_madc_conversion(&req
) <= 0)
49 return req
.rbuf
[channel
];
53 * Read ADCIN channel 12 (voltage) and convert RAW value to micro voltage
54 * This conversion formula was extracted from maemo program bsi-read
56 static int rx51_battery_read_voltage(struct rx51_device_info
*di
)
58 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);
149 return 1280 * (1200 * capacity
)/(1024 - capacity
);
153 * Return power_supply property
155 static int rx51_battery_get_property(struct power_supply
*psy
,
156 enum power_supply_property psp
,
157 union power_supply_propval
*val
)
159 struct rx51_device_info
*di
= container_of((psy
),
160 struct rx51_device_info
, bat
);
163 case POWER_SUPPLY_PROP_TECHNOLOGY
:
164 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
166 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
167 val
->intval
= 4200000;
169 case POWER_SUPPLY_PROP_PRESENT
:
170 val
->intval
= rx51_battery_read_voltage(di
) ? 1 : 0;
172 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
173 val
->intval
= rx51_battery_read_voltage(di
);
175 case POWER_SUPPLY_PROP_TEMP
:
176 val
->intval
= rx51_battery_read_temperature(di
);
178 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
179 val
->intval
= rx51_battery_read_capacity(di
);
185 if (val
->intval
== INT_MAX
|| val
->intval
== INT_MIN
)
191 static enum power_supply_property rx51_battery_props
[] = {
192 POWER_SUPPLY_PROP_TECHNOLOGY
,
193 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
,
194 POWER_SUPPLY_PROP_PRESENT
,
195 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
196 POWER_SUPPLY_PROP_TEMP
,
197 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
200 static int __devinit
rx51_battery_probe(struct platform_device
*pdev
)
202 struct rx51_device_info
*di
;
205 di
= kzalloc(sizeof(*di
), GFP_KERNEL
);
209 platform_set_drvdata(pdev
, di
);
211 di
->bat
.name
= dev_name(&pdev
->dev
);
212 di
->bat
.type
= POWER_SUPPLY_TYPE_BATTERY
;
213 di
->bat
.properties
= rx51_battery_props
;
214 di
->bat
.num_properties
= ARRAY_SIZE(rx51_battery_props
);
215 di
->bat
.get_property
= rx51_battery_get_property
;
217 ret
= power_supply_register(di
->dev
, &di
->bat
);
219 platform_set_drvdata(pdev
, NULL
);
227 static int __devexit
rx51_battery_remove(struct platform_device
*pdev
)
229 struct rx51_device_info
*di
= platform_get_drvdata(pdev
);
231 power_supply_unregister(&di
->bat
);
232 platform_set_drvdata(pdev
, NULL
);
238 static struct platform_driver rx51_battery_driver
= {
239 .probe
= rx51_battery_probe
,
240 .remove
= __devexit_p(rx51_battery_remove
),
242 .name
= "rx51-battery",
243 .owner
= THIS_MODULE
,
246 module_platform_driver(rx51_battery_driver
);
248 MODULE_ALIAS("platform:rx51-battery");
249 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
250 MODULE_DESCRIPTION("Nokia RX-51 battery driver");
251 MODULE_LICENSE("GPL");