temp revert rk change
[firefly-linux-kernel-4.4.55.git] / drivers / power / ds2781_battery.c
1 /*
2  * Copyright (C) 2010 Motorola, Inc.
3  *
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.
7  *
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.
12  *
13  * Based on ds2784_battery.c which is:
14  * Copyright (C) 2009 HTC Corporation
15  * Copyright (C) 2009 Google, Inc.
16  */
17
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>
30 #include <linux/pm.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>
37
38 #include "../w1/w1.h"
39 #include "../w1/slaves/w1_ds2781.h"
40
41 extern int is_ac_charging(void);
42 extern int is_ac_charge_complete(void);
43
44 struct battery_status {
45         int timestamp;
46
47         int voltage_uV;         /* units of uV */
48         int current_uA;         /* units of uA */
49         int current_avg_uA;
50         int charge_uAh;
51
52         short temp_C;           /* units of 0.1 C */
53
54         u8 percentage;          /* battery percentage */
55         u8 age_scalar;          /* converted to percent */
56         u8 charge_source;
57         u8 status_reg;
58         u8 battery_full;        /* battery full (don't charge) */
59
60         u8 cooldown;            /* was overtemp */
61 };
62
63
64 #define TEMP_HOT        450 /* 45.0 degrees Celcius */
65
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.
70  */
71 #define THRES_PERCENT   3
72 #define THRES_BATT      7500000
73 #define THRES_CHRG      7750000
74
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.
78  */
79 #define THRES_100_RESET 8320000
80
81 #define BATTERY_LOG_MAX 1024
82 #define BATTERY_LOG_MASK (BATTERY_LOG_MAX - 1)
83
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
87  */
88 #define FAST_POLL       (1 * 60)
89 #define SLOW_POLL       (20 * 60)
90
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;
95
96 static int battery_log_en;
97 module_param(battery_log_en, int, S_IRUGO | S_IWUSR | S_IWGRP);
98
99 void battery_log_status(struct battery_status *s)
100 {
101         unsigned n;
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);
110 }
111
112 static const char *battery_source[2] = { "none", "  ac" };
113
114 static int battery_log_print(struct seq_file *sf, void *private)
115 {
116         unsigned n;
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,
125                            s->percentage,
126                            battery_source[s->charge_source],
127                            s->status_reg, s->battery_full);
128         }
129         mutex_unlock(&battery_log_lock);
130         return 0;
131 }
132
133
134 struct ds2781_device_info {
135         struct device *dev;
136
137         /* DS2781 data, valid after calling ds2781_battery_read_status() */
138         char raw[DS2781_DATA_SIZE];     /* raw DS2781 data */
139
140         struct battery_status status;
141         struct mutex status_lock;
142
143         struct power_supply bat;
144         struct device *w1_dev;
145         struct workqueue_struct *monitor_wqueue;
146         struct work_struct monitor_work;
147         struct alarm alarm;
148         struct wake_lock work_wake_lock;
149
150         u8 slow_poll;
151         ktime_t last_poll;
152 };
153
154 #define psy_to_dev_info(x) container_of((x), struct ds2781_device_info, bat)
155
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,
168 };
169
170 #define to_ds2781_device_info(x) container_of((x), struct ds2781_device_info, \
171                                               bat);
172
173 static void ds2781_parse_data(u8 *raw, struct battery_status *s)
174 {
175         short n;
176
177         /* Get status reg */
178         s->status_reg = raw[DS2781_REG_STATUS];
179
180         /* Get Level */
181         s->percentage = raw[DS2781_REG_RARC];
182
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);
186
187         s->voltage_uV = n * 9760;
188
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];
193
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];
197
198         /* Get Temperature:
199          * Unit=0.125 degree C,therefore, give up LSB ,
200          * just caculate MSB for temperature only.
201          */
202         n = (((signed char)raw[DS2781_REG_TEMP_MSB]) << 3) |
203                 (raw[DS2781_REG_TEMP_LSB] >> 5);
204
205         s->temp_C = n + (n / 4);
206
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;
210
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;
214 }
215
216 static int ds2781_battery_read_status(struct ds2781_device_info *di)
217 {
218         int ret;
219         int start;
220         int count;
221         char first_time;
222         struct battery_status *s = &di->status;
223
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;
229                 first_time = 1;
230         } else {
231                 start = DS2781_REG_STATUS;
232                 count = DS2781_REG_AGE_SCALAR - start + 1;
233                 first_time = 0;
234         }
235
236         ret = w1_ds2781_read(di->w1_dev, di->raw + start, start, count);
237         if (ret != count) {
238                 dev_warn(di->dev, "call to w1_ds2781_read failed (0x%p)\n",
239                          di->w1_dev);
240                 return 1;
241         }
242
243         ds2781_parse_data(di->raw, s);
244
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");
249
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);
255
256                 /* Give time for registers to update, then read again */
257                 msleep(450);
258                 return ds2781_battery_read_status(di);
259         }
260
261         return 0;
262 }
263
264 static int battery_get_property(struct power_supply *psy,
265                                 enum power_supply_property psp,
266                                 union power_supply_propval *val)
267 {
268         struct ds2781_device_info *di = psy_to_dev_info(psy);
269         int retval = 0;
270
271         mutex_lock(&di->status_lock);
272
273         switch (psp) {
274         case POWER_SUPPLY_PROP_STATUS:
275                 if (is_ac_charging()) {
276                         if (di->status.battery_full)
277                                 val->intval = POWER_SUPPLY_STATUS_FULL;
278                         else
279                                 val->intval = POWER_SUPPLY_STATUS_CHARGING;
280                 } else
281                         val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
282                 break;
283         case POWER_SUPPLY_PROP_HEALTH:
284                 if (di->status.temp_C >= TEMP_HOT)
285                         val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
286                 else
287                         val->intval = POWER_SUPPLY_HEALTH_GOOD;
288                 break;
289         case POWER_SUPPLY_PROP_PRESENT:
290                 val->intval = 1;
291                 break;
292         case POWER_SUPPLY_PROP_TECHNOLOGY:
293                 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
294                 break;
295         case POWER_SUPPLY_PROP_CYCLE_COUNT:
296                 val->intval = di->status.age_scalar;
297                 break;
298         case POWER_SUPPLY_PROP_CAPACITY:
299                 if (di->status.battery_full)
300                         val->intval = 100;
301                 else
302                         val->intval = di->status.percentage;
303                 break;
304         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
305                 val->intval = di->status.voltage_uV;
306                 break;
307         case POWER_SUPPLY_PROP_TEMP:
308                 val->intval = di->status.temp_C;
309                 break;
310         case POWER_SUPPLY_PROP_CURRENT_NOW:
311                 val->intval = di->status.current_uA;
312                 break;
313         case POWER_SUPPLY_PROP_CURRENT_AVG:
314                 val->intval = di->status.current_avg_uA;
315                 break;
316         case POWER_SUPPLY_PROP_CHARGE_COUNTER:
317                 val->intval = di->status.charge_uAh;
318                 break;
319         default:
320                 retval = -EINVAL;
321         }
322
323         mutex_unlock(&di->status_lock);
324
325         return retval;
326 }
327
328 static void ds2781_battery_update_status(struct ds2781_device_info *di)
329 {
330         u8 last_level;
331         last_level = di->status.percentage;
332
333         mutex_lock(&di->status_lock);
334         ds2781_battery_read_status(di);
335
336         if (battery_log_en)
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);
345
346         if ((last_level != di->status.percentage) ||
347             (di->status.temp_C >= TEMP_HOT))
348                 power_supply_changed(&di->bat);
349 }
350
351 static void ds2781_program_alarm(struct ds2781_device_info *di, int seconds)
352 {
353         ktime_t low_interval = ktime_set(seconds - 10, 0);
354         ktime_t slack = ktime_set(20, 0);
355         ktime_t next;
356
357         next = ktime_add(di->last_poll, low_interval);
358
359         alarm_cancel(&di->alarm);
360         alarm_start_range(&di->alarm, next, ktime_add(next, slack));
361 }
362
363 static void ds2781_battery_work(struct work_struct *work)
364 {
365         struct ds2781_device_info *di =
366                 container_of(work, struct ds2781_device_info, monitor_work);
367         struct timespec ts;
368
369         ds2781_battery_update_status(di);
370
371         di->last_poll = alarm_get_elapsed_realtime();
372
373         ts = ktime_to_timespec(di->last_poll);
374         di->status.timestamp = ts.tv_sec;
375
376         if (battery_log_en)
377                 battery_log_status(&di->status);
378
379         ds2781_program_alarm(di, FAST_POLL);
380         wake_unlock(&di->work_wake_lock);
381 }
382
383 static void ds2781_battery_alarm(struct alarm *alarm)
384 {
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);
389 }
390
391 static void ds2781_reset_if_necessary(struct ds2781_device_info *di)
392 {
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");
399
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);
407         }
408 }
409
410 static void battery_ext_power_changed(struct power_supply *psy)
411 {
412         struct ds2781_device_info *di;
413         int got_power;
414
415         di = psy_to_dev_info(psy);
416         got_power = power_supply_am_i_supplied(psy);
417
418         mutex_lock(&di->status_lock);
419         if (got_power) {
420                 di->status.charge_source = 1;
421                 if (is_ac_charge_complete()) {
422                         di->status.battery_full = 1;
423                         ds2781_reset_if_necessary(di);
424                 }
425         } else {
426                 di->status.charge_source = 0;
427                 di->status.battery_full = 0;
428         }
429         mutex_unlock(&di->status_lock);
430
431         power_supply_changed(psy);
432 }
433
434 static int ds2781_battery_probe(struct platform_device *pdev)
435 {
436         int rc;
437         struct ds2781_device_info *di;
438
439         di = kzalloc(sizeof(*di), GFP_KERNEL);
440         if (!di)
441                 return -ENOMEM;
442
443         platform_set_drvdata(pdev, di);
444
445         di->dev = &pdev->dev;
446         di->w1_dev = pdev->dev.parent;
447         mutex_init(&di->status_lock);
448
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;
455
456         rc = power_supply_register(&pdev->dev, &di->bat);
457         if (rc)
458                 goto fail_register;
459
460         INIT_WORK(&di->monitor_work, ds2781_battery_work);
461         di->monitor_wqueue = create_freezeable_workqueue(dev_name(&pdev->dev));
462
463         /* init to something sane */
464         di->last_poll = alarm_get_elapsed_realtime();
465
466         if (!di->monitor_wqueue) {
467                 rc = -ESRCH;
468                 goto fail_workqueue;
469         }
470         wake_lock_init(&di->work_wake_lock, WAKE_LOCK_SUSPEND,
471                         "ds2781-battery");
472         alarm_init(&di->alarm, ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP,
473                         ds2781_battery_alarm);
474         wake_lock(&di->work_wake_lock);
475
476         /* Check for charger since it could have been detected already. */
477         battery_ext_power_changed(&di->bat);
478
479         queue_work(di->monitor_wqueue, &di->monitor_work);
480         return 0;
481
482 fail_workqueue:
483         power_supply_unregister(&di->bat);
484 fail_register:
485         kfree(di);
486         return rc;
487 }
488
489 static int ds2781_suspend(struct device *dev)
490 {
491         struct ds2781_device_info *di = dev_get_drvdata(dev);
492
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);
497                 di->slow_poll = 1;
498         }
499         return 0;
500 }
501
502 static int ds2781_resume(struct device *dev)
503 {
504         struct ds2781_device_info *di = dev_get_drvdata(dev);
505
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
510          * we suspend again.
511          */
512         if (di->slow_poll) {
513                 ds2781_program_alarm(di, FAST_POLL);
514                 di->slow_poll = 0;
515         }
516         return 0;
517 }
518
519 static struct dev_pm_ops ds2781_pm_ops = {
520         .suspend        = ds2781_suspend,
521         .resume         = ds2781_resume,
522 };
523
524 static struct platform_driver ds2781_battery_driver = {
525         .driver = {
526                 .name = "ds2781-battery",
527                 .pm = &ds2781_pm_ops,
528         },
529         .probe    = ds2781_battery_probe,
530 };
531
532 static int battery_log_open(struct inode *inode, struct file *file)
533 {
534         return single_open(file, battery_log_print, NULL);
535 }
536
537 static struct file_operations battery_log_fops = {
538         .open = battery_log_open,
539         .read = seq_read,
540         .llseek = seq_lseek,
541         .release = single_release,
542 };
543
544 static int __init ds2781_battery_init(void)
545 {
546         debugfs_create_file("battery_log", 0444, NULL, NULL, &battery_log_fops);
547         return platform_driver_register(&ds2781_battery_driver);
548 }
549
550 module_init(ds2781_battery_init);
551
552 MODULE_LICENSE("GPL");
553 MODULE_AUTHOR("Motorola");
554 MODULE_DESCRIPTION("ds2781 battery driver");