1 /*
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/iio/consumer.h>
27 #include <linux/of.h>
28 
29 struct rx51_device_info {
30 	struct device *dev;
31 	struct power_supply *bat;
32 	struct power_supply_desc bat_desc;
33 	struct iio_channel *channel_temp;
34 	struct iio_channel *channel_bsi;
35 	struct iio_channel *channel_vbat;
36 };
37 
38 /*
39  * Read ADCIN channel value, code copied from maemo kernel
40  */
rx51_battery_read_adc(struct iio_channel * channel)41 static int rx51_battery_read_adc(struct iio_channel *channel)
42 {
43 	int val, err;
44 	err = iio_read_channel_average_raw(channel, &val);
45 	if (err < 0)
46 		return err;
47 	return val;
48 }
49 
50 /*
51  * Read ADCIN channel 12 (voltage) and convert RAW value to micro voltage
52  * This conversion formula was extracted from maemo program bsi-read
53  */
rx51_battery_read_voltage(struct rx51_device_info * di)54 static int rx51_battery_read_voltage(struct rx51_device_info *di)
55 {
56 	int voltage = rx51_battery_read_adc(di->channel_vbat);
57 
58 	if (voltage < 0) {
59 		dev_err(di->dev, "Could not read ADC: %d\n", voltage);
60 		return voltage;
61 	}
62 
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 #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,
99 	937, 993, 1024
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  */
rx51_battery_read_temperature(struct rx51_device_info * di)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(di->channel_temp);
111 
112 	if (raw < 0)
113 		dev_err(di->dev, "Could not read ADC: %d\n", raw);
114 
115 	/* Zero and negative values are undefined */
116 	if (raw <= 0)
117 		return INT_MAX;
118 
119 	/* ADC channels are 10 bit, higher value are undefined */
120 	if (raw >= (1 << 10))
121 		return INT_MIN;
122 
123 	/* First check for temperature in first direct table */
124 	if (raw < ARRAY_SIZE(rx51_temp_table1))
125 		return rx51_temp_table1[raw] * 10;
126 
127 	/* Binary search RAW value in second inverse table */
128 	while (max - min > 1) {
129 		int mid = (max + min) / 2;
130 		if (rx51_temp_table2[mid] <= raw)
131 			min = mid;
132 		else if (rx51_temp_table2[mid] > raw)
133 			max = mid;
134 		if (rx51_temp_table2[mid] == raw)
135 			break;
136 	}
137 
138 	return (rx51_temp_table2_first - min) * 10;
139 }
140 
141 /*
142  * Read ADCIN channel 4 (BSI) and convert RAW value to micro Ah
143  * This conversion formula was extracted from maemo program bsi-read
144  */
rx51_battery_read_capacity(struct rx51_device_info * di)145 static int rx51_battery_read_capacity(struct rx51_device_info *di)
146 {
147 	int capacity = rx51_battery_read_adc(di->channel_bsi);
148 
149 	if (capacity < 0) {
150 		dev_err(di->dev, "Could not read ADC: %d\n", capacity);
151 		return capacity;
152 	}
153 
154 	return 1280 * (1200 * capacity)/(1024 - capacity);
155 }
156 
157 /*
158  * Return power_supply property
159  */
rx51_battery_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)160 static int rx51_battery_get_property(struct power_supply *psy,
161 					enum power_supply_property psp,
162 					union power_supply_propval *val)
163 {
164 	struct rx51_device_info *di = power_supply_get_drvdata(psy);
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 
rx51_battery_probe(struct platform_device * pdev)204 static int rx51_battery_probe(struct platform_device *pdev)
205 {
206 	struct power_supply_config psy_cfg = {};
207 	struct rx51_device_info *di;
208 	int ret;
209 
210 	di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
211 	if (!di)
212 		return -ENOMEM;
213 
214 	platform_set_drvdata(pdev, di);
215 
216 	di->dev = &pdev->dev;
217 	di->bat_desc.name = "rx51-battery";
218 	di->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
219 	di->bat_desc.properties = rx51_battery_props;
220 	di->bat_desc.num_properties = ARRAY_SIZE(rx51_battery_props);
221 	di->bat_desc.get_property = rx51_battery_get_property;
222 
223 	psy_cfg.drv_data = di;
224 
225 	di->channel_temp = iio_channel_get(di->dev, "temp");
226 	if (IS_ERR(di->channel_temp)) {
227 		ret = PTR_ERR(di->channel_temp);
228 		goto error;
229 	}
230 
231 	di->channel_bsi  = iio_channel_get(di->dev, "bsi");
232 	if (IS_ERR(di->channel_bsi)) {
233 		ret = PTR_ERR(di->channel_bsi);
234 		goto error_channel_temp;
235 	}
236 
237 	di->channel_vbat = iio_channel_get(di->dev, "vbat");
238 	if (IS_ERR(di->channel_vbat)) {
239 		ret = PTR_ERR(di->channel_vbat);
240 		goto error_channel_bsi;
241 	}
242 
243 	di->bat = power_supply_register(di->dev, &di->bat_desc, &psy_cfg);
244 	if (IS_ERR(di->bat)) {
245 		ret = PTR_ERR(di->bat);
246 		goto error_channel_vbat;
247 	}
248 
249 	return 0;
250 
251 error_channel_vbat:
252 	iio_channel_release(di->channel_vbat);
253 error_channel_bsi:
254 	iio_channel_release(di->channel_bsi);
255 error_channel_temp:
256 	iio_channel_release(di->channel_temp);
257 error:
258 
259 	return ret;
260 }
261 
rx51_battery_remove(struct platform_device * pdev)262 static int rx51_battery_remove(struct platform_device *pdev)
263 {
264 	struct rx51_device_info *di = platform_get_drvdata(pdev);
265 
266 	power_supply_unregister(di->bat);
267 
268 	iio_channel_release(di->channel_vbat);
269 	iio_channel_release(di->channel_bsi);
270 	iio_channel_release(di->channel_temp);
271 
272 	return 0;
273 }
274 
275 #ifdef CONFIG_OF
276 static const struct of_device_id n900_battery_of_match[] = {
277 	{.compatible = "nokia,n900-battery", },
278 	{ },
279 };
280 MODULE_DEVICE_TABLE(of, n900_battery_of_match);
281 #endif
282 
283 static struct platform_driver rx51_battery_driver = {
284 	.probe = rx51_battery_probe,
285 	.remove = rx51_battery_remove,
286 	.driver = {
287 		.name = "rx51-battery",
288 		.of_match_table = of_match_ptr(n900_battery_of_match),
289 	},
290 };
291 module_platform_driver(rx51_battery_driver);
292 
293 MODULE_ALIAS("platform:rx51-battery");
294 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
295 MODULE_DESCRIPTION("Nokia RX-51 battery driver");
296 MODULE_LICENSE("GPL");
297