2 * Copyright (C) 2010 Motorola, Inc.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * Based on ds2784_battery.c which is:
14 * Copyright (C) 2009 HTC Corporation
15 * Copyright (C) 2009 Google, Inc.
18 #include <linux/android_alarm.h>
19 #include <linux/debugfs.h>
20 #include <linux/delay.h>
21 #include <linux/err.h>
22 #include <linux/gpio.h>
23 #include <linux/init.h>
24 #include <linux/jiffies.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/mutex.h>
28 #include <linux/param.h>
29 #include <linux/platform_device.h>
31 #include <linux/power_supply.h>
32 #include <linux/seq_file.h>
33 #include <linux/slab.h>
34 #include <linux/spinlock.h>
35 #include <linux/wakelock.h>
36 #include <linux/workqueue.h>
39 #include "../w1/slaves/w1_ds2781.h"
41 extern int is_ac_charging(void);
42 extern int is_ac_charge_complete(void);
44 struct battery_status {
47 int voltage_uV; /* units of uV */
48 int current_uA; /* units of uA */
52 short temp_C; /* units of 0.1 C */
54 u8 percentage; /* battery percentage */
55 u8 age_scalar; /* converted to percent */
58 u8 battery_full; /* battery full (don't charge) */
60 u8 cooldown; /* was overtemp */
64 #define TEMP_HOT 450 /* 45.0 degrees Celcius */
66 /* On power up, a sanity check is performed on the reported capacity. If the
67 * capacity is less than THRES_PERCENT, but the battery voltage is above
68 * THRES_BATT (when battery powered) or THRES_CHRG (when on a charger), the
69 * ACR will be reset to a reasonable value.
71 #define THRES_PERCENT 3
72 #define THRES_BATT 7500000
73 #define THRES_CHRG 7750000
75 /* When charge complete is reached, a sanity check is performed on the reported
76 * capacity. If the capacity is less than 100, but the battery voltage is above
77 * THRES_100_RESET, the ACR will be reset to full capacity.
79 #define THRES_100_RESET 8320000
81 #define BATTERY_LOG_MAX 1024
82 #define BATTERY_LOG_MASK (BATTERY_LOG_MAX - 1)
84 /* When we're awake or running on wall power, sample the battery
85 * gauge every FAST_POLL seconds. If we're asleep and on battery
86 * power, sample every SLOW_POLL seconds
88 #define FAST_POLL (1 * 60)
89 #define SLOW_POLL (20 * 60)
91 static DEFINE_MUTEX(battery_log_lock);
92 static struct battery_status battery_log[BATTERY_LOG_MAX];
93 static unsigned battery_log_head;
94 static unsigned battery_log_tail;
96 static int battery_log_en;
97 module_param(battery_log_en, int, S_IRUGO | S_IWUSR | S_IWGRP);
99 void battery_log_status(struct battery_status *s)
102 mutex_lock(&battery_log_lock);
103 n = battery_log_head;
104 memcpy(battery_log + n, s, sizeof(struct battery_status));
105 n = (n + 1) & BATTERY_LOG_MASK;
106 if (n == battery_log_tail)
107 battery_log_tail = (battery_log_tail + 1) & BATTERY_LOG_MASK;
108 battery_log_head = n;
109 mutex_unlock(&battery_log_lock);
112 static const char *battery_source[2] = { "none", " ac" };
114 static int battery_log_print(struct seq_file *sf, void *private)
117 mutex_lock(&battery_log_lock);
118 seq_printf(sf, "timestamp mV mA avg mA uAh dC %% src reg full\n");
119 for (n = battery_log_tail; n != battery_log_head; n = (n + 1) & BATTERY_LOG_MASK) {
120 struct battery_status *s = battery_log + n;
121 seq_printf(sf, "%9d %5d %6d %6d %8d %4d %3d %s 0x%02x %d\n",
122 s->timestamp, s->voltage_uV / 1000,
123 s->current_uA / 1000, s->current_avg_uA / 1000,
124 s->charge_uAh, s->temp_C,
126 battery_source[s->charge_source],
127 s->status_reg, s->battery_full);
129 mutex_unlock(&battery_log_lock);
134 struct ds2781_device_info {
137 /* DS2781 data, valid after calling ds2781_battery_read_status() */
138 char raw[DS2781_DATA_SIZE]; /* raw DS2781 data */
140 struct battery_status status;
141 struct mutex status_lock;
143 struct power_supply bat;
144 struct device *w1_dev;
145 struct workqueue_struct *monitor_wqueue;
146 struct work_struct monitor_work;
148 struct wake_lock work_wake_lock;
154 #define psy_to_dev_info(x) container_of((x), struct ds2781_device_info, bat)
156 static enum power_supply_property battery_properties[] = {
157 POWER_SUPPLY_PROP_STATUS,
158 POWER_SUPPLY_PROP_HEALTH,
159 POWER_SUPPLY_PROP_PRESENT,
160 POWER_SUPPLY_PROP_TECHNOLOGY,
161 POWER_SUPPLY_PROP_CYCLE_COUNT,
162 POWER_SUPPLY_PROP_CAPACITY,
163 POWER_SUPPLY_PROP_VOLTAGE_NOW,
164 POWER_SUPPLY_PROP_TEMP,
165 POWER_SUPPLY_PROP_CURRENT_NOW,
166 POWER_SUPPLY_PROP_CURRENT_AVG,
167 POWER_SUPPLY_PROP_CHARGE_COUNTER,
170 #define to_ds2781_device_info(x) container_of((x), struct ds2781_device_info, \
173 static void ds2781_parse_data(u8 *raw, struct battery_status *s)
178 s->status_reg = raw[DS2781_REG_STATUS];
181 s->percentage = raw[DS2781_REG_RARC];
183 /* Get Voltage: Unit=9.76mV, range is 0V to 9.9902V */
184 n = (((raw[DS2781_REG_VOLT_MSB] << 8) |
185 (raw[DS2781_REG_VOLT_LSB])) >> 5);
187 s->voltage_uV = n * 9760;
189 /* Get Current: Unit= 1.5625uV x Rsnsp */
190 n = ((raw[DS2781_REG_CURR_MSB]) << 8) |
191 raw[DS2781_REG_CURR_LSB];
192 s->current_uA = ((n * 15625) / 10000) * raw[DS2781_REG_RSNSP];
194 n = ((raw[DS2781_REG_AVG_CURR_MSB]) << 8) |
195 raw[DS2781_REG_AVG_CURR_LSB];
196 s->current_avg_uA = ((n * 15625) / 10000) * raw[DS2781_REG_RSNSP];
199 * Unit=0.125 degree C,therefore, give up LSB ,
200 * just caculate MSB for temperature only.
202 n = (((signed char)raw[DS2781_REG_TEMP_MSB]) << 3) |
203 (raw[DS2781_REG_TEMP_LSB] >> 5);
205 s->temp_C = n + (n / 4);
207 /* RAAC is in units of 1.6mAh */
208 s->charge_uAh = ((raw[DS2781_REG_RAAC_MSB] << 8) |
209 raw[DS2781_REG_RAAC_LSB]) * 1600;
211 /* Get Age: Unit=0.78125%, range is 49.2% to 100% */
212 n = raw[DS2781_REG_AGE_SCALAR];
213 s->age_scalar = (n * 78125) / 100000;
216 static int ds2781_battery_read_status(struct ds2781_device_info *di)
222 struct battery_status *s = &di->status;
224 /* The first time we read the entire contents of SRAM/EEPROM,
225 * but after that we just read the interesting bits that change. */
226 if (di->raw[DS2781_REG_RSNSP] == 0x00) {
227 start = DS2781_REG_STATUS;
228 count = DS2781_DATA_SIZE - start;
231 start = DS2781_REG_STATUS;
232 count = DS2781_REG_AGE_SCALAR - start + 1;
236 ret = w1_ds2781_read(di->w1_dev, di->raw + start, start, count);
238 dev_warn(di->dev, "call to w1_ds2781_read failed (0x%p)\n",
243 ds2781_parse_data(di->raw, s);
245 if (first_time && (s->percentage < THRES_PERCENT) &&
246 (s->voltage_uV > (s->charge_source ? THRES_CHRG : THRES_BATT))) {
247 dev_err(di->dev, "Battery capacity is obviously wrong\n");
248 dev_err(di->dev, "Reset ACR registers to ~1300mAh\n");
250 di->raw[DS2781_REG_ACCUMULATE_CURR_MSB] = 0x08;
251 di->raw[DS2781_REG_ACCUMULATE_CURR_LSB] = 0x25;
252 w1_ds2781_write(di->w1_dev,
253 di->raw + DS2781_REG_ACCUMULATE_CURR_MSB,
254 DS2781_REG_ACCUMULATE_CURR_MSB, 2);
256 /* Give time for registers to update, then read again */
258 return ds2781_battery_read_status(di);
264 static int battery_get_property(struct power_supply *psy,
265 enum power_supply_property psp,
266 union power_supply_propval *val)
268 struct ds2781_device_info *di = psy_to_dev_info(psy);
271 mutex_lock(&di->status_lock);
274 case POWER_SUPPLY_PROP_STATUS:
275 if (is_ac_charging()) {
276 if (di->status.battery_full)
277 val->intval = POWER_SUPPLY_STATUS_FULL;
279 val->intval = POWER_SUPPLY_STATUS_CHARGING;
281 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
283 case POWER_SUPPLY_PROP_HEALTH:
284 if (di->status.temp_C >= TEMP_HOT)
285 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
287 val->intval = POWER_SUPPLY_HEALTH_GOOD;
289 case POWER_SUPPLY_PROP_PRESENT:
292 case POWER_SUPPLY_PROP_TECHNOLOGY:
293 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
295 case POWER_SUPPLY_PROP_CYCLE_COUNT:
296 val->intval = di->status.age_scalar;
298 case POWER_SUPPLY_PROP_CAPACITY:
299 if (di->status.battery_full)
302 val->intval = di->status.percentage;
304 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
305 val->intval = di->status.voltage_uV;
307 case POWER_SUPPLY_PROP_TEMP:
308 val->intval = di->status.temp_C;
310 case POWER_SUPPLY_PROP_CURRENT_NOW:
311 val->intval = di->status.current_uA;
313 case POWER_SUPPLY_PROP_CURRENT_AVG:
314 val->intval = di->status.current_avg_uA;
316 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
317 val->intval = di->status.charge_uAh;
323 mutex_unlock(&di->status_lock);
328 static void ds2781_battery_update_status(struct ds2781_device_info *di)
331 last_level = di->status.percentage;
333 mutex_lock(&di->status_lock);
334 ds2781_battery_read_status(di);
337 pr_info("batt: %3d%%, %d mV, %d mA (%d avg), %d.%d C, %d mAh\n",
338 di->status.percentage,
339 di->status.voltage_uV / 1000,
340 di->status.current_uA / 1000,
341 di->status.current_avg_uA / 1000,
342 di->status.temp_C / 10, di->status.temp_C % 10,
343 di->status.charge_uAh / 1000);
344 mutex_unlock(&di->status_lock);
346 if ((last_level != di->status.percentage) ||
347 (di->status.temp_C >= TEMP_HOT))
348 power_supply_changed(&di->bat);
351 static void ds2781_program_alarm(struct ds2781_device_info *di, int seconds)
353 ktime_t low_interval = ktime_set(seconds - 10, 0);
354 ktime_t slack = ktime_set(20, 0);
357 next = ktime_add(di->last_poll, low_interval);
359 alarm_cancel(&di->alarm);
360 alarm_start_range(&di->alarm, next, ktime_add(next, slack));
363 static void ds2781_battery_work(struct work_struct *work)
365 struct ds2781_device_info *di =
366 container_of(work, struct ds2781_device_info, monitor_work);
369 ds2781_battery_update_status(di);
371 di->last_poll = alarm_get_elapsed_realtime();
373 ts = ktime_to_timespec(di->last_poll);
374 di->status.timestamp = ts.tv_sec;
377 battery_log_status(&di->status);
379 ds2781_program_alarm(di, FAST_POLL);
380 wake_unlock(&di->work_wake_lock);
383 static void ds2781_battery_alarm(struct alarm *alarm)
385 struct ds2781_device_info *di =
386 container_of(alarm, struct ds2781_device_info, alarm);
387 wake_lock(&di->work_wake_lock);
388 queue_work(di->monitor_wqueue, &di->monitor_work);
391 static void ds2781_reset_if_necessary(struct ds2781_device_info *di)
393 /* If we have read from the DS2781 and the percentage is not 100%,
394 * the ACR should be reset. */
395 if (di->raw[DS2781_REG_RSNSP] && (di->status.percentage < 100) &&
396 (di->status.voltage_uV > THRES_100_RESET)) {
397 dev_err(di->dev, "Charge complete before 100 percent.\n");
398 dev_err(di->dev, "Resetting ACR registers to Full 40 value.\n");
400 di->raw[DS2781_REG_ACCUMULATE_CURR_MSB] =
401 di->raw[DS2781_REG_FULL_40_MSB];
402 di->raw[DS2781_REG_ACCUMULATE_CURR_LSB] =
403 di->raw[DS2781_REG_FULL_40_LSB];
404 w1_ds2781_write(di->w1_dev,
405 di->raw + DS2781_REG_ACCUMULATE_CURR_MSB,
406 DS2781_REG_ACCUMULATE_CURR_MSB, 2);
410 static void battery_ext_power_changed(struct power_supply *psy)
412 struct ds2781_device_info *di;
415 di = psy_to_dev_info(psy);
416 got_power = power_supply_am_i_supplied(psy);
418 mutex_lock(&di->status_lock);
420 di->status.charge_source = 1;
421 if (is_ac_charge_complete()) {
422 di->status.battery_full = 1;
423 ds2781_reset_if_necessary(di);
426 di->status.charge_source = 0;
427 di->status.battery_full = 0;
429 mutex_unlock(&di->status_lock);
431 power_supply_changed(psy);
434 static int ds2781_battery_probe(struct platform_device *pdev)
437 struct ds2781_device_info *di;
439 di = kzalloc(sizeof(*di), GFP_KERNEL);
443 platform_set_drvdata(pdev, di);
445 di->dev = &pdev->dev;
446 di->w1_dev = pdev->dev.parent;
447 mutex_init(&di->status_lock);
449 di->bat.name = "battery";
450 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
451 di->bat.properties = battery_properties;
452 di->bat.num_properties = ARRAY_SIZE(battery_properties);
453 di->bat.external_power_changed = battery_ext_power_changed;
454 di->bat.get_property = battery_get_property;
456 rc = power_supply_register(&pdev->dev, &di->bat);
460 INIT_WORK(&di->monitor_work, ds2781_battery_work);
461 di->monitor_wqueue = create_freezeable_workqueue(dev_name(&pdev->dev));
463 /* init to something sane */
464 di->last_poll = alarm_get_elapsed_realtime();
466 if (!di->monitor_wqueue) {
470 wake_lock_init(&di->work_wake_lock, WAKE_LOCK_SUSPEND,
472 alarm_init(&di->alarm, ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP,
473 ds2781_battery_alarm);
474 wake_lock(&di->work_wake_lock);
476 /* Check for charger since it could have been detected already. */
477 battery_ext_power_changed(&di->bat);
479 queue_work(di->monitor_wqueue, &di->monitor_work);
483 power_supply_unregister(&di->bat);
489 static int ds2781_suspend(struct device *dev)
491 struct ds2781_device_info *di = dev_get_drvdata(dev);
493 /* If on battery, reduce update rate until next resume. */
494 if ((!di->status.charge_source) &&
495 (di->status.temp_C < TEMP_HOT)) {
496 ds2781_program_alarm(di, SLOW_POLL);
502 static int ds2781_resume(struct device *dev)
504 struct ds2781_device_info *di = dev_get_drvdata(dev);
506 /* We might be on a slow sample cycle. If we're
507 * resuming we should resample the battery state
508 * if it's been over a minute since we last did
509 * so, and move back to sampling every minute until
513 ds2781_program_alarm(di, FAST_POLL);
519 static struct dev_pm_ops ds2781_pm_ops = {
520 .suspend = ds2781_suspend,
521 .resume = ds2781_resume,
524 static struct platform_driver ds2781_battery_driver = {
526 .name = "ds2781-battery",
527 .pm = &ds2781_pm_ops,
529 .probe = ds2781_battery_probe,
532 static int battery_log_open(struct inode *inode, struct file *file)
534 return single_open(file, battery_log_print, NULL);
537 static struct file_operations battery_log_fops = {
538 .open = battery_log_open,
541 .release = single_release,
544 static int __init ds2781_battery_init(void)
546 debugfs_create_file("battery_log", 0444, NULL, NULL, &battery_log_fops);
547 return platform_driver_register(&ds2781_battery_driver);
550 module_init(ds2781_battery_init);
552 MODULE_LICENSE("GPL");
553 MODULE_AUTHOR("Motorola");
554 MODULE_DESCRIPTION("ds2781 battery driver");