ds2760_battery.c revision 02d0d2758821c38b2601d34dac544140af09e651
1/*
2 * Driver for batteries with DS2760 chips inside.
3 *
4 * Copyright © 2007 Anton Vorontsov
5 *	       2004-2007 Matt Reimer
6 *	       2004 Szabolcs Gyurko
7 *
8 * Use consistent with the GNU GPL is permitted,
9 * provided that this copyright notice is
10 * preserved in its entirety in all copies and derived works.
11 *
12 * Author:  Anton Vorontsov <cbou@mail.ru>
13 *	    February 2007
14 *
15 *	    Matt Reimer <mreimer@vpop.net>
16 *	    April 2004, 2005, 2007
17 *
18 *	    Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>
19 *	    September 2004
20 */
21
22#include <linux/module.h>
23#include <linux/param.h>
24#include <linux/jiffies.h>
25#include <linux/workqueue.h>
26#include <linux/pm.h>
27#include <linux/platform_device.h>
28#include <linux/power_supply.h>
29
30#include "../w1/w1.h"
31#include "../w1/slaves/w1_ds2760.h"
32
33struct ds2760_device_info {
34	struct device *dev;
35
36	/* DS2760 data, valid after calling ds2760_battery_read_status() */
37	unsigned long update_time;	/* jiffies when data read */
38	char raw[DS2760_DATA_SIZE];	/* raw DS2760 data */
39	int voltage_raw;		/* units of 4.88 mV */
40	int voltage_uV;			/* units of µV */
41	int current_raw;		/* units of 0.625 mA */
42	int current_uA;			/* units of µA */
43	int accum_current_raw;		/* units of 0.25 mAh */
44	int accum_current_uAh;		/* units of µAh */
45	int temp_raw;			/* units of 0.125 °C */
46	int temp_C;			/* units of 0.1 °C */
47	int rated_capacity;		/* units of µAh */
48	int rem_capacity;		/* percentage */
49	int full_active_uAh;		/* units of µAh */
50	int empty_uAh;			/* units of µAh */
51	int life_sec;			/* units of seconds */
52	int charge_status;		/* POWER_SUPPLY_STATUS_* */
53
54	int full_counter;
55	struct power_supply bat;
56	struct device *w1_dev;
57	struct workqueue_struct *monitor_wqueue;
58	struct delayed_work monitor_work;
59};
60
61static unsigned int cache_time = 1000;
62module_param(cache_time, uint, 0644);
63MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
64
65static unsigned int pmod_enabled;
66module_param(pmod_enabled, bool, 0644);
67MODULE_PARM_DESC(pmod_enabled, "PMOD enable bit");
68
69static unsigned int rated_capacity;
70module_param(rated_capacity, uint, 0644);
71MODULE_PARM_DESC(rated_capacity, "rated battery capacity, 10*mAh or index");
72
73static unsigned int current_accum;
74module_param(current_accum, uint, 0644);
75MODULE_PARM_DESC(current_accum, "current accumulator value");
76
77/* Some batteries have their rated capacity stored a N * 10 mAh, while
78 * others use an index into this table. */
79static int rated_capacities[] = {
80	0,
81	920,	/* Samsung */
82	920,	/* BYD */
83	920,	/* Lishen */
84	920,	/* NEC */
85	1440,	/* Samsung */
86	1440,	/* BYD */
87	1440,	/* Lishen */
88	1440,	/* NEC */
89	2880,	/* Samsung */
90	2880,	/* BYD */
91	2880,	/* Lishen */
92	2880	/* NEC */
93};
94
95/* array is level at temps 0°C, 10°C, 20°C, 30°C, 40°C
96 * temp is in Celsius */
97static int battery_interpolate(int array[], int temp)
98{
99	int index, dt;
100
101	if (temp <= 0)
102		return array[0];
103	if (temp >= 40)
104		return array[4];
105
106	index = temp / 10;
107	dt    = temp % 10;
108
109	return array[index] + (((array[index + 1] - array[index]) * dt) / 10);
110}
111
112static int ds2760_battery_read_status(struct ds2760_device_info *di)
113{
114	int ret, i, start, count, scale[5];
115
116	if (di->update_time && time_before(jiffies, di->update_time +
117					   msecs_to_jiffies(cache_time)))
118		return 0;
119
120	/* The first time we read the entire contents of SRAM/EEPROM,
121	 * but after that we just read the interesting bits that change. */
122	if (di->update_time == 0) {
123		start = 0;
124		count = DS2760_DATA_SIZE;
125	} else {
126		start = DS2760_VOLTAGE_MSB;
127		count = DS2760_TEMP_LSB - start + 1;
128	}
129
130	ret = w1_ds2760_read(di->w1_dev, di->raw + start, start, count);
131	if (ret != count) {
132		dev_warn(di->dev, "call to w1_ds2760_read failed (0x%p)\n",
133			 di->w1_dev);
134		return 1;
135	}
136
137	di->update_time = jiffies;
138
139	/* DS2760 reports voltage in units of 4.88mV, but the battery class
140	 * reports in units of uV, so convert by multiplying by 4880. */
141	di->voltage_raw = (di->raw[DS2760_VOLTAGE_MSB] << 3) |
142			  (di->raw[DS2760_VOLTAGE_LSB] >> 5);
143	di->voltage_uV = di->voltage_raw * 4880;
144
145	/* DS2760 reports current in signed units of 0.625mA, but the battery
146	 * class reports in units of µA, so convert by multiplying by 625. */
147	di->current_raw =
148	    (((signed char)di->raw[DS2760_CURRENT_MSB]) << 5) |
149			  (di->raw[DS2760_CURRENT_LSB] >> 3);
150	di->current_uA = di->current_raw * 625;
151
152	/* DS2760 reports accumulated current in signed units of 0.25mAh. */
153	di->accum_current_raw =
154	    (((signed char)di->raw[DS2760_CURRENT_ACCUM_MSB]) << 8) |
155			   di->raw[DS2760_CURRENT_ACCUM_LSB];
156	di->accum_current_uAh = di->accum_current_raw * 250;
157
158	/* DS2760 reports temperature in signed units of 0.125°C, but the
159	 * battery class reports in units of 1/10 °C, so we convert by
160	 * multiplying by .125 * 10 = 1.25. */
161	di->temp_raw = (((signed char)di->raw[DS2760_TEMP_MSB]) << 3) |
162				     (di->raw[DS2760_TEMP_LSB] >> 5);
163	di->temp_C = di->temp_raw + (di->temp_raw / 4);
164
165	/* At least some battery monitors (e.g. HP iPAQ) store the battery's
166	 * maximum rated capacity. */
167	if (di->raw[DS2760_RATED_CAPACITY] < ARRAY_SIZE(rated_capacities))
168		di->rated_capacity = rated_capacities[
169			(unsigned int)di->raw[DS2760_RATED_CAPACITY]];
170	else
171		di->rated_capacity = di->raw[DS2760_RATED_CAPACITY] * 10;
172
173	di->rated_capacity *= 1000; /* convert to µAh */
174
175	/* Calculate the full level at the present temperature. */
176	di->full_active_uAh = di->raw[DS2760_ACTIVE_FULL] << 8 |
177			      di->raw[DS2760_ACTIVE_FULL + 1];
178
179	/* If the full_active_uAh value is not given, fall back to the rated
180	 * capacity. This is likely to happen when chips are not part of the
181	 * battery pack and is therefore not bootstrapped. */
182	if (di->full_active_uAh == 0)
183		di->full_active_uAh = di->rated_capacity / 1000L;
184
185	scale[0] = di->full_active_uAh;
186	for (i = 1; i < 5; i++)
187		scale[i] = scale[i - 1] + di->raw[DS2760_ACTIVE_FULL + 2 + i];
188
189	di->full_active_uAh = battery_interpolate(scale, di->temp_C / 10);
190	di->full_active_uAh *= 1000; /* convert to µAh */
191
192	/* Calculate the empty level at the present temperature. */
193	scale[4] = di->raw[DS2760_ACTIVE_EMPTY + 4];
194	for (i = 3; i >= 0; i--)
195		scale[i] = scale[i + 1] + di->raw[DS2760_ACTIVE_EMPTY + i];
196
197	di->empty_uAh = battery_interpolate(scale, di->temp_C / 10);
198	di->empty_uAh *= 1000; /* convert to µAh */
199
200	if (di->full_active_uAh == di->empty_uAh)
201		di->rem_capacity = 0;
202	else
203		/* From Maxim Application Note 131: remaining capacity =
204		 * ((ICA - Empty Value) / (Full Value - Empty Value)) x 100% */
205		di->rem_capacity = ((di->accum_current_uAh - di->empty_uAh) * 100L) /
206				    (di->full_active_uAh - di->empty_uAh);
207
208	if (di->rem_capacity < 0)
209		di->rem_capacity = 0;
210	if (di->rem_capacity > 100)
211		di->rem_capacity = 100;
212
213	if (di->current_uA)
214		di->life_sec = -((di->accum_current_uAh - di->empty_uAh) *
215				 3600L) / di->current_uA;
216	else
217		di->life_sec = 0;
218
219	return 0;
220}
221
222static void ds2760_battery_set_current_accum(struct ds2760_device_info *di,
223					     unsigned int acr_val)
224{
225	unsigned char acr[2];
226
227	/* acr is in units of 0.25 mAh */
228	acr_val *= 4L;
229	acr_val /= 1000;
230
231	acr[0] = acr_val >> 8;
232	acr[1] = acr_val & 0xff;
233
234	if (w1_ds2760_write(di->w1_dev, acr, DS2760_CURRENT_ACCUM_MSB, 2) < 2)
235		dev_warn(di->dev, "ACR write failed\n");
236}
237
238static void ds2760_battery_update_status(struct ds2760_device_info *di)
239{
240	int old_charge_status = di->charge_status;
241
242	ds2760_battery_read_status(di);
243
244	if (di->charge_status == POWER_SUPPLY_STATUS_UNKNOWN)
245		di->full_counter = 0;
246
247	if (power_supply_am_i_supplied(&di->bat)) {
248		if (di->current_uA > 10000) {
249			di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
250			di->full_counter = 0;
251		} else if (di->current_uA < -5000) {
252			if (di->charge_status != POWER_SUPPLY_STATUS_NOT_CHARGING)
253				dev_notice(di->dev, "not enough power to "
254					   "charge\n");
255			di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
256			di->full_counter = 0;
257		} else if (di->current_uA < 10000 &&
258			    di->charge_status != POWER_SUPPLY_STATUS_FULL) {
259
260			/* Don't consider the battery to be full unless
261			 * we've seen the current < 10 mA at least two
262			 * consecutive times. */
263
264			di->full_counter++;
265
266			if (di->full_counter < 2) {
267				di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
268			} else {
269				di->charge_status = POWER_SUPPLY_STATUS_FULL;
270				ds2760_battery_set_current_accum(di,
271						di->full_active_uAh);
272			}
273		}
274	} else {
275		di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
276		di->full_counter = 0;
277	}
278
279	if (di->charge_status != old_charge_status)
280		power_supply_changed(&di->bat);
281}
282
283static void ds2760_battery_write_status(struct ds2760_device_info *di,
284					char status)
285{
286	if (status == di->raw[DS2760_STATUS_REG])
287		return;
288
289	w1_ds2760_write(di->w1_dev, &status, DS2760_STATUS_WRITE_REG, 1);
290	w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
291	w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
292}
293
294static void ds2760_battery_write_rated_capacity(struct ds2760_device_info *di,
295						unsigned char rated_capacity)
296{
297	if (rated_capacity == di->raw[DS2760_RATED_CAPACITY])
298		return;
299
300	w1_ds2760_write(di->w1_dev, &rated_capacity, DS2760_RATED_CAPACITY, 1);
301	w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
302	w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
303}
304
305static void ds2760_battery_work(struct work_struct *work)
306{
307	struct ds2760_device_info *di = container_of(work,
308		struct ds2760_device_info, monitor_work.work);
309	const int interval = HZ * 60;
310
311	dev_dbg(di->dev, "%s\n", __func__);
312
313	ds2760_battery_update_status(di);
314	queue_delayed_work(di->monitor_wqueue, &di->monitor_work, interval);
315}
316
317#define to_ds2760_device_info(x) container_of((x), struct ds2760_device_info, \
318					      bat);
319
320static void ds2760_battery_external_power_changed(struct power_supply *psy)
321{
322	struct ds2760_device_info *di = to_ds2760_device_info(psy);
323
324	dev_dbg(di->dev, "%s\n", __func__);
325
326	cancel_delayed_work(&di->monitor_work);
327	queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ/10);
328}
329
330static int ds2760_battery_get_property(struct power_supply *psy,
331				       enum power_supply_property psp,
332				       union power_supply_propval *val)
333{
334	struct ds2760_device_info *di = to_ds2760_device_info(psy);
335
336	switch (psp) {
337	case POWER_SUPPLY_PROP_STATUS:
338		val->intval = di->charge_status;
339		return 0;
340	default:
341		break;
342	}
343
344	ds2760_battery_read_status(di);
345
346	switch (psp) {
347	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
348		val->intval = di->voltage_uV;
349		break;
350	case POWER_SUPPLY_PROP_CURRENT_NOW:
351		val->intval = di->current_uA;
352		break;
353	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
354		val->intval = di->rated_capacity;
355		break;
356	case POWER_SUPPLY_PROP_CHARGE_FULL:
357		val->intval = di->full_active_uAh;
358		break;
359	case POWER_SUPPLY_PROP_CHARGE_EMPTY:
360		val->intval = di->empty_uAh;
361		break;
362	case POWER_SUPPLY_PROP_CHARGE_NOW:
363		val->intval = di->accum_current_uAh;
364		break;
365	case POWER_SUPPLY_PROP_TEMP:
366		val->intval = di->temp_C;
367		break;
368	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
369		val->intval = di->life_sec;
370		break;
371	case POWER_SUPPLY_PROP_CAPACITY:
372		val->intval = di->rem_capacity;
373		break;
374	default:
375		return -EINVAL;
376	}
377
378	return 0;
379}
380
381static enum power_supply_property ds2760_battery_props[] = {
382	POWER_SUPPLY_PROP_STATUS,
383	POWER_SUPPLY_PROP_VOLTAGE_NOW,
384	POWER_SUPPLY_PROP_CURRENT_NOW,
385	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
386	POWER_SUPPLY_PROP_CHARGE_FULL,
387	POWER_SUPPLY_PROP_CHARGE_EMPTY,
388	POWER_SUPPLY_PROP_CHARGE_NOW,
389	POWER_SUPPLY_PROP_TEMP,
390	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
391	POWER_SUPPLY_PROP_CAPACITY,
392};
393
394static int ds2760_battery_probe(struct platform_device *pdev)
395{
396	char status;
397	int retval = 0;
398	struct ds2760_device_info *di;
399
400	di = kzalloc(sizeof(*di), GFP_KERNEL);
401	if (!di) {
402		retval = -ENOMEM;
403		goto di_alloc_failed;
404	}
405
406	platform_set_drvdata(pdev, di);
407
408	di->dev			= &pdev->dev;
409	di->w1_dev		= pdev->dev.parent;
410	di->bat.name		= dev_name(&pdev->dev);
411	di->bat.type		= POWER_SUPPLY_TYPE_BATTERY;
412	di->bat.properties	= ds2760_battery_props;
413	di->bat.num_properties	= ARRAY_SIZE(ds2760_battery_props);
414	di->bat.get_property	= ds2760_battery_get_property;
415	di->bat.external_power_changed =
416				  ds2760_battery_external_power_changed;
417
418	di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
419
420	/* enable sleep mode feature */
421	ds2760_battery_read_status(di);
422	status = di->raw[DS2760_STATUS_REG];
423	if (pmod_enabled)
424		status |= DS2760_STATUS_PMOD;
425	else
426		status &= ~DS2760_STATUS_PMOD;
427
428	ds2760_battery_write_status(di, status);
429
430	/* set rated capacity from module param */
431	if (rated_capacity)
432		ds2760_battery_write_rated_capacity(di, rated_capacity);
433
434	/* set current accumulator if given as parameter.
435	 * this should only be done for bootstrapping the value */
436	if (current_accum)
437		ds2760_battery_set_current_accum(di, current_accum);
438
439	retval = power_supply_register(&pdev->dev, &di->bat);
440	if (retval) {
441		dev_err(di->dev, "failed to register battery\n");
442		goto batt_failed;
443	}
444
445	INIT_DELAYED_WORK(&di->monitor_work, ds2760_battery_work);
446	di->monitor_wqueue = create_singlethread_workqueue(dev_name(&pdev->dev));
447	if (!di->monitor_wqueue) {
448		retval = -ESRCH;
449		goto workqueue_failed;
450	}
451	queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ * 1);
452
453	goto success;
454
455workqueue_failed:
456	power_supply_unregister(&di->bat);
457batt_failed:
458	kfree(di);
459di_alloc_failed:
460success:
461	return retval;
462}
463
464static int ds2760_battery_remove(struct platform_device *pdev)
465{
466	struct ds2760_device_info *di = platform_get_drvdata(pdev);
467
468	cancel_rearming_delayed_workqueue(di->monitor_wqueue,
469					  &di->monitor_work);
470	destroy_workqueue(di->monitor_wqueue);
471	power_supply_unregister(&di->bat);
472
473	return 0;
474}
475
476#ifdef CONFIG_PM
477
478static int ds2760_battery_suspend(struct platform_device *pdev,
479				  pm_message_t state)
480{
481	struct ds2760_device_info *di = platform_get_drvdata(pdev);
482
483	di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
484
485	return 0;
486}
487
488static int ds2760_battery_resume(struct platform_device *pdev)
489{
490	struct ds2760_device_info *di = platform_get_drvdata(pdev);
491
492	di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
493	power_supply_changed(&di->bat);
494
495	cancel_delayed_work(&di->monitor_work);
496	queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ);
497
498	return 0;
499}
500
501#else
502
503#define ds2760_battery_suspend NULL
504#define ds2760_battery_resume NULL
505
506#endif /* CONFIG_PM */
507
508MODULE_ALIAS("platform:ds2760-battery");
509
510static struct platform_driver ds2760_battery_driver = {
511	.driver = {
512		.name = "ds2760-battery",
513	},
514	.probe	  = ds2760_battery_probe,
515	.remove   = ds2760_battery_remove,
516	.suspend  = ds2760_battery_suspend,
517	.resume	  = ds2760_battery_resume,
518};
519
520static int __init ds2760_battery_init(void)
521{
522	return platform_driver_register(&ds2760_battery_driver);
523}
524
525static void __exit ds2760_battery_exit(void)
526{
527	platform_driver_unregister(&ds2760_battery_driver);
528}
529
530module_init(ds2760_battery_init);
531module_exit(ds2760_battery_exit);
532
533MODULE_LICENSE("GPL");
534MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>, "
535	      "Matt Reimer <mreimer@vpop.net>, "
536	      "Anton Vorontsov <cbou@mail.ru>");
537MODULE_DESCRIPTION("ds2760 battery driver");
538