blob: 9eca9f1ff0eae2e5b381e503f035ceb446340e66 [file] [log] [blame]
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001/*
2 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
3 * MyungJoo Ham <myungjoo.ham@samsung.com>
4 *
5 * This driver enables to monitor battery health and control charger
6 * during suspend-to-mem.
7 * Charger manager depends on other devices. register this later than
8 * the depending devices.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13**/
14
15#include <linux/io.h>
16#include <linux/module.h>
17#include <linux/irq.h>
18#include <linux/interrupt.h>
19#include <linux/rtc.h>
20#include <linux/slab.h>
21#include <linux/workqueue.h>
22#include <linux/platform_device.h>
23#include <linux/power/charger-manager.h>
24#include <linux/regulator/consumer.h>
25
26/*
27 * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for
28 * delayed works so that we can run delayed works with CM_JIFFIES_SMALL
29 * without any delays.
30 */
31#define CM_JIFFIES_SMALL (2)
32
33/* If y is valid (> 0) and smaller than x, do x = y */
34#define CM_MIN_VALID(x, y) x = (((y > 0) && ((x) > (y))) ? (y) : (x))
35
36/*
37 * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking
38 * rtc alarm. It should be 2 or larger
39 */
40#define CM_RTC_SMALL (2)
41
42#define UEVENT_BUF_SIZE 32
43
44static LIST_HEAD(cm_list);
45static DEFINE_MUTEX(cm_list_mtx);
46
47/* About in-suspend (suspend-again) monitoring */
48static struct rtc_device *rtc_dev;
49/*
50 * Backup RTC alarm
51 * Save the wakeup alarm before entering suspend-to-RAM
52 */
53static struct rtc_wkalrm rtc_wkalarm_save;
54/* Backup RTC alarm time in terms of seconds since 01-01-1970 00:00:00 */
55static unsigned long rtc_wkalarm_save_time;
56static bool cm_suspended;
57static bool cm_rtc_set;
58static unsigned long cm_suspend_duration_ms;
59
60/* Global charger-manager description */
61static struct charger_global_desc *g_desc; /* init with setup_charger_manager */
62
63/**
64 * is_batt_present - See if the battery presents in place.
65 * @cm: the Charger Manager representing the battery.
66 */
67static bool is_batt_present(struct charger_manager *cm)
68{
69 union power_supply_propval val;
70 bool present = false;
71 int i, ret;
72
73 switch (cm->desc->battery_present) {
74 case CM_FUEL_GAUGE:
75 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
76 POWER_SUPPLY_PROP_PRESENT, &val);
77 if (ret == 0 && val.intval)
78 present = true;
79 break;
80 case CM_CHARGER_STAT:
81 for (i = 0; cm->charger_stat[i]; i++) {
82 ret = cm->charger_stat[i]->get_property(
83 cm->charger_stat[i],
84 POWER_SUPPLY_PROP_PRESENT, &val);
85 if (ret == 0 && val.intval) {
86 present = true;
87 break;
88 }
89 }
90 break;
91 }
92
93 return present;
94}
95
96/**
97 * is_ext_pwr_online - See if an external power source is attached to charge
98 * @cm: the Charger Manager representing the battery.
99 *
100 * Returns true if at least one of the chargers of the battery has an external
101 * power source attached to charge the battery regardless of whether it is
102 * actually charging or not.
103 */
104static bool is_ext_pwr_online(struct charger_manager *cm)
105{
106 union power_supply_propval val;
107 bool online = false;
108 int i, ret;
109
110 /* If at least one of them has one, it's yes. */
111 for (i = 0; cm->charger_stat[i]; i++) {
112 ret = cm->charger_stat[i]->get_property(
113 cm->charger_stat[i],
114 POWER_SUPPLY_PROP_ONLINE, &val);
115 if (ret == 0 && val.intval) {
116 online = true;
117 break;
118 }
119 }
120
121 return online;
122}
123
124/**
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900125 * get_batt_uV - Get the voltage level of the battery
126 * @cm: the Charger Manager representing the battery.
127 * @uV: the voltage level returned.
128 *
129 * Returns 0 if there is no error.
130 * Returns a negative value on error.
131 */
132static int get_batt_uV(struct charger_manager *cm, int *uV)
133{
134 union power_supply_propval val;
135 int ret;
136
Axel Linbb2a95c2012-01-12 12:56:35 +0800137 if (!cm->fuel_gauge)
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900138 return -ENODEV;
139
Axel Linbb2a95c2012-01-12 12:56:35 +0800140 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
141 POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900142 if (ret)
143 return ret;
144
145 *uV = val.intval;
146 return 0;
147}
148
149/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900150 * is_charging - Returns true if the battery is being charged.
151 * @cm: the Charger Manager representing the battery.
152 */
153static bool is_charging(struct charger_manager *cm)
154{
155 int i, ret;
156 bool charging = false;
157 union power_supply_propval val;
158
159 /* If there is no battery, it cannot be charged */
160 if (!is_batt_present(cm))
161 return false;
162
163 /* If at least one of the charger is charging, return yes */
164 for (i = 0; cm->charger_stat[i]; i++) {
165 /* 1. The charger sholuld not be DISABLED */
166 if (cm->emergency_stop)
167 continue;
168 if (!cm->charger_enabled)
169 continue;
170
171 /* 2. The charger should be online (ext-power) */
172 ret = cm->charger_stat[i]->get_property(
173 cm->charger_stat[i],
174 POWER_SUPPLY_PROP_ONLINE, &val);
175 if (ret) {
176 dev_warn(cm->dev, "Cannot read ONLINE value from %s.\n",
177 cm->desc->psy_charger_stat[i]);
178 continue;
179 }
180 if (val.intval == 0)
181 continue;
182
183 /*
184 * 3. The charger should not be FULL, DISCHARGING,
185 * or NOT_CHARGING.
186 */
187 ret = cm->charger_stat[i]->get_property(
188 cm->charger_stat[i],
189 POWER_SUPPLY_PROP_STATUS, &val);
190 if (ret) {
191 dev_warn(cm->dev, "Cannot read STATUS value from %s.\n",
192 cm->desc->psy_charger_stat[i]);
193 continue;
194 }
195 if (val.intval == POWER_SUPPLY_STATUS_FULL ||
196 val.intval == POWER_SUPPLY_STATUS_DISCHARGING ||
197 val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING)
198 continue;
199
200 /* Then, this is charging. */
201 charging = true;
202 break;
203 }
204
205 return charging;
206}
207
208/**
209 * is_polling_required - Return true if need to continue polling for this CM.
210 * @cm: the Charger Manager representing the battery.
211 */
212static bool is_polling_required(struct charger_manager *cm)
213{
214 switch (cm->desc->polling_mode) {
215 case CM_POLL_DISABLE:
216 return false;
217 case CM_POLL_ALWAYS:
218 return true;
219 case CM_POLL_EXTERNAL_POWER_ONLY:
220 return is_ext_pwr_online(cm);
221 case CM_POLL_CHARGING_ONLY:
222 return is_charging(cm);
223 default:
224 dev_warn(cm->dev, "Incorrect polling_mode (%d)\n",
225 cm->desc->polling_mode);
226 }
227
228 return false;
229}
230
231/**
232 * try_charger_enable - Enable/Disable chargers altogether
233 * @cm: the Charger Manager representing the battery.
234 * @enable: true: enable / false: disable
235 *
236 * Note that Charger Manager keeps the charger enabled regardless whether
237 * the charger is charging or not (because battery is full or no external
238 * power source exists) except when CM needs to disable chargers forcibly
239 * bacause of emergency causes; when the battery is overheated or too cold.
240 */
241static int try_charger_enable(struct charger_manager *cm, bool enable)
242{
243 int err = 0, i;
244 struct charger_desc *desc = cm->desc;
245
246 /* Ignore if it's redundent command */
Axel Linbb2a95c2012-01-12 12:56:35 +0800247 if (enable == cm->charger_enabled)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900248 return 0;
249
250 if (enable) {
251 if (cm->emergency_stop)
252 return -EAGAIN;
253 err = regulator_bulk_enable(desc->num_charger_regulators,
254 desc->charger_regulators);
255 } else {
256 /*
257 * Abnormal battery state - Stop charging forcibly,
258 * even if charger was enabled at the other places
259 */
260 err = regulator_bulk_disable(desc->num_charger_regulators,
261 desc->charger_regulators);
262
263 for (i = 0; i < desc->num_charger_regulators; i++) {
264 if (regulator_is_enabled(
265 desc->charger_regulators[i].consumer)) {
266 regulator_force_disable(
267 desc->charger_regulators[i].consumer);
268 dev_warn(cm->dev,
269 "Disable regulator(%s) forcibly.\n",
270 desc->charger_regulators[i].supply);
271 }
272 }
273 }
274
275 if (!err)
276 cm->charger_enabled = enable;
277
278 return err;
279}
280
281/**
282 * uevent_notify - Let users know something has changed.
283 * @cm: the Charger Manager representing the battery.
284 * @event: the event string.
285 *
286 * If @event is null, it implies that uevent_notify is called
287 * by resume function. When called in the resume function, cm_suspended
288 * should be already reset to false in order to let uevent_notify
289 * notify the recent event during the suspend to users. While
290 * suspended, uevent_notify does not notify users, but tracks
291 * events so that uevent_notify can notify users later after resumed.
292 */
293static void uevent_notify(struct charger_manager *cm, const char *event)
294{
295 static char env_str[UEVENT_BUF_SIZE + 1] = "";
296 static char env_str_save[UEVENT_BUF_SIZE + 1] = "";
297
298 if (cm_suspended) {
299 /* Nothing in suspended-event buffer */
300 if (env_str_save[0] == 0) {
301 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
302 return; /* status not changed */
303 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
304 return;
305 }
306
307 if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE))
308 return; /* Duplicated. */
Axel Linbb2a95c2012-01-12 12:56:35 +0800309 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900310 return;
311 }
312
313 if (event == NULL) {
314 /* No messages pending */
315 if (!env_str_save[0])
316 return;
317
318 strncpy(env_str, env_str_save, UEVENT_BUF_SIZE);
319 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
320 env_str_save[0] = 0;
321
322 return;
323 }
324
325 /* status not changed */
326 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
327 return;
328
329 /* save the status and notify the update */
330 strncpy(env_str, event, UEVENT_BUF_SIZE);
331 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
332
333 dev_info(cm->dev, event);
334}
335
336/**
337 * _cm_monitor - Monitor the temperature and return true for exceptions.
338 * @cm: the Charger Manager representing the battery.
339 *
340 * Returns true if there is an event to notify for the battery.
341 * (True if the status of "emergency_stop" changes)
342 */
343static bool _cm_monitor(struct charger_manager *cm)
344{
345 struct charger_desc *desc = cm->desc;
346 int temp = desc->temperature_out_of_range(&cm->last_temp_mC);
347
348 dev_dbg(cm->dev, "monitoring (%2.2d.%3.3dC)\n",
349 cm->last_temp_mC / 1000, cm->last_temp_mC % 1000);
350
351 /* It has been stopped or charging already */
352 if (!!temp == !!cm->emergency_stop)
353 return false;
354
355 if (temp) {
356 cm->emergency_stop = temp;
357 if (!try_charger_enable(cm, false)) {
358 if (temp > 0)
359 uevent_notify(cm, "OVERHEAT");
360 else
361 uevent_notify(cm, "COLD");
362 }
363 } else {
364 cm->emergency_stop = 0;
365 if (!try_charger_enable(cm, true))
366 uevent_notify(cm, "CHARGING");
367 }
368
369 return true;
370}
371
372/**
373 * cm_monitor - Monitor every battery.
374 *
375 * Returns true if there is an event to notify from any of the batteries.
376 * (True if the status of "emergency_stop" changes)
377 */
378static bool cm_monitor(void)
379{
380 bool stop = false;
381 struct charger_manager *cm;
382
383 mutex_lock(&cm_list_mtx);
384
Axel Linbb2a95c2012-01-12 12:56:35 +0800385 list_for_each_entry(cm, &cm_list, entry) {
386 if (_cm_monitor(cm))
387 stop = true;
388 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900389
390 mutex_unlock(&cm_list_mtx);
391
392 return stop;
393}
394
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900395static int charger_get_property(struct power_supply *psy,
396 enum power_supply_property psp,
397 union power_supply_propval *val)
398{
399 struct charger_manager *cm = container_of(psy,
400 struct charger_manager, charger_psy);
401 struct charger_desc *desc = cm->desc;
Anton Vorontsovdf58c042012-03-15 21:01:28 +0400402 int ret = 0;
403 int uV;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900404
405 switch (psp) {
406 case POWER_SUPPLY_PROP_STATUS:
407 if (is_charging(cm))
408 val->intval = POWER_SUPPLY_STATUS_CHARGING;
409 else if (is_ext_pwr_online(cm))
410 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
411 else
412 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
413 break;
414 case POWER_SUPPLY_PROP_HEALTH:
415 if (cm->emergency_stop > 0)
416 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
417 else if (cm->emergency_stop < 0)
418 val->intval = POWER_SUPPLY_HEALTH_COLD;
419 else
420 val->intval = POWER_SUPPLY_HEALTH_GOOD;
421 break;
422 case POWER_SUPPLY_PROP_PRESENT:
423 if (is_batt_present(cm))
424 val->intval = 1;
425 else
426 val->intval = 0;
427 break;
428 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Anton Vorontsovdf58c042012-03-15 21:01:28 +0400429 ret = get_batt_uV(cm, &val->intval);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900430 break;
431 case POWER_SUPPLY_PROP_CURRENT_NOW:
432 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
433 POWER_SUPPLY_PROP_CURRENT_NOW, val);
434 break;
435 case POWER_SUPPLY_PROP_TEMP:
436 /* in thenth of centigrade */
437 if (cm->last_temp_mC == INT_MIN)
438 desc->temperature_out_of_range(&cm->last_temp_mC);
439 val->intval = cm->last_temp_mC / 100;
440 if (!desc->measure_battery_temp)
441 ret = -ENODEV;
442 break;
443 case POWER_SUPPLY_PROP_TEMP_AMBIENT:
444 /* in thenth of centigrade */
445 if (cm->last_temp_mC == INT_MIN)
446 desc->temperature_out_of_range(&cm->last_temp_mC);
447 val->intval = cm->last_temp_mC / 100;
448 if (desc->measure_battery_temp)
449 ret = -ENODEV;
450 break;
451 case POWER_SUPPLY_PROP_CAPACITY:
452 if (!cm->fuel_gauge) {
453 ret = -ENODEV;
454 break;
455 }
456
457 if (!is_batt_present(cm)) {
458 /* There is no battery. Assume 100% */
459 val->intval = 100;
460 break;
461 }
462
463 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
464 POWER_SUPPLY_PROP_CAPACITY, val);
465 if (ret)
466 break;
467
468 if (val->intval > 100) {
469 val->intval = 100;
470 break;
471 }
472 if (val->intval < 0)
473 val->intval = 0;
474
475 /* Do not adjust SOC when charging: voltage is overrated */
476 if (is_charging(cm))
477 break;
478
479 /*
480 * If the capacity value is inconsistent, calibrate it base on
481 * the battery voltage values and the thresholds given as desc
482 */
483 ret = get_batt_uV(cm, &uV);
484 if (ret) {
485 /* Voltage information not available. No calibration */
486 ret = 0;
487 break;
488 }
489
490 if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
491 !is_charging(cm)) {
492 val->intval = 100;
493 break;
494 }
495
496 break;
497 case POWER_SUPPLY_PROP_ONLINE:
498 if (is_ext_pwr_online(cm))
499 val->intval = 1;
500 else
501 val->intval = 0;
502 break;
503 case POWER_SUPPLY_PROP_CHARGE_FULL:
504 if (cm->fuel_gauge) {
505 if (cm->fuel_gauge->get_property(cm->fuel_gauge,
506 POWER_SUPPLY_PROP_CHARGE_FULL, val) == 0)
507 break;
508 }
509
510 if (is_ext_pwr_online(cm)) {
511 /* Not full if it's charging. */
512 if (is_charging(cm)) {
513 val->intval = 0;
514 break;
515 }
516 /*
517 * Full if it's powered but not charging andi
518 * not forced stop by emergency
519 */
520 if (!cm->emergency_stop) {
521 val->intval = 1;
522 break;
523 }
524 }
525
526 /* Full if it's over the fullbatt voltage */
527 ret = get_batt_uV(cm, &uV);
528 if (!ret && desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
529 !is_charging(cm)) {
530 val->intval = 1;
531 break;
532 }
533
534 /* Full if the cap is 100 */
535 if (cm->fuel_gauge) {
536 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
537 POWER_SUPPLY_PROP_CAPACITY, val);
538 if (!ret && val->intval >= 100 && !is_charging(cm)) {
539 val->intval = 1;
540 break;
541 }
542 }
543
544 val->intval = 0;
545 ret = 0;
546 break;
547 case POWER_SUPPLY_PROP_CHARGE_NOW:
548 if (is_charging(cm)) {
549 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
550 POWER_SUPPLY_PROP_CHARGE_NOW,
551 val);
552 if (ret) {
553 val->intval = 1;
554 ret = 0;
555 } else {
556 /* If CHARGE_NOW is supplied, use it */
557 val->intval = (val->intval > 0) ?
558 val->intval : 1;
559 }
560 } else {
561 val->intval = 0;
562 }
563 break;
564 default:
565 return -EINVAL;
566 }
567 return ret;
568}
569
570#define NUM_CHARGER_PSY_OPTIONAL (4)
571static enum power_supply_property default_charger_props[] = {
572 /* Guaranteed to provide */
573 POWER_SUPPLY_PROP_STATUS,
574 POWER_SUPPLY_PROP_HEALTH,
575 POWER_SUPPLY_PROP_PRESENT,
576 POWER_SUPPLY_PROP_VOLTAGE_NOW,
577 POWER_SUPPLY_PROP_CAPACITY,
578 POWER_SUPPLY_PROP_ONLINE,
579 POWER_SUPPLY_PROP_CHARGE_FULL,
580 /*
581 * Optional properties are:
582 * POWER_SUPPLY_PROP_CHARGE_NOW,
583 * POWER_SUPPLY_PROP_CURRENT_NOW,
584 * POWER_SUPPLY_PROP_TEMP, and
585 * POWER_SUPPLY_PROP_TEMP_AMBIENT,
586 */
587};
588
589static struct power_supply psy_default = {
590 .name = "battery",
591 .type = POWER_SUPPLY_TYPE_BATTERY,
592 .properties = default_charger_props,
593 .num_properties = ARRAY_SIZE(default_charger_props),
594 .get_property = charger_get_property,
595};
596
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900597/**
598 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
599 * for suspend_again.
600 *
601 * Returns true if the alarm is set for Charger Manager to use.
602 * Returns false if
603 * cm_setup_timer fails to set an alarm,
604 * cm_setup_timer does not need to set an alarm for Charger Manager,
605 * or an alarm previously configured is to be used.
606 */
607static bool cm_setup_timer(void)
608{
609 struct charger_manager *cm;
610 unsigned int wakeup_ms = UINT_MAX;
611 bool ret = false;
612
613 mutex_lock(&cm_list_mtx);
614
615 list_for_each_entry(cm, &cm_list, entry) {
616 /* Skip if polling is not required for this CM */
617 if (!is_polling_required(cm) && !cm->emergency_stop)
618 continue;
619 if (cm->desc->polling_interval_ms == 0)
620 continue;
621 CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
622 }
623
624 mutex_unlock(&cm_list_mtx);
625
626 if (wakeup_ms < UINT_MAX && wakeup_ms > 0) {
627 pr_info("Charger Manager wakeup timer: %u ms.\n", wakeup_ms);
628 if (rtc_dev) {
629 struct rtc_wkalrm tmp;
630 unsigned long time, now;
631 unsigned long add = DIV_ROUND_UP(wakeup_ms, 1000);
632
633 /*
634 * Set alarm with the polling interval (wakeup_ms)
635 * except when rtc_wkalarm_save comes first.
636 * However, the alarm time should be NOW +
637 * CM_RTC_SMALL or later.
638 */
639 tmp.enabled = 1;
640 rtc_read_time(rtc_dev, &tmp.time);
641 rtc_tm_to_time(&tmp.time, &now);
642 if (add < CM_RTC_SMALL)
643 add = CM_RTC_SMALL;
644 time = now + add;
645
646 ret = true;
647
648 if (rtc_wkalarm_save.enabled &&
649 rtc_wkalarm_save_time &&
650 rtc_wkalarm_save_time < time) {
651 if (rtc_wkalarm_save_time < now + CM_RTC_SMALL)
652 time = now + CM_RTC_SMALL;
653 else
654 time = rtc_wkalarm_save_time;
655
656 /* The timer is not appointed by CM */
657 ret = false;
658 }
659
660 pr_info("Waking up after %lu secs.\n",
661 time - now);
662
663 rtc_time_to_tm(time, &tmp.time);
664 rtc_set_alarm(rtc_dev, &tmp);
665 cm_suspend_duration_ms += wakeup_ms;
666 return ret;
667 }
668 }
669
670 if (rtc_dev)
671 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
672 return false;
673}
674
675/**
676 * cm_suspend_again - Determine whether suspend again or not
677 *
678 * Returns true if the system should be suspended again
679 * Returns false if the system should be woken up
680 */
681bool cm_suspend_again(void)
682{
683 struct charger_manager *cm;
684 bool ret = false;
685
686 if (!g_desc || !g_desc->rtc_only_wakeup || !g_desc->rtc_only_wakeup() ||
687 !cm_rtc_set)
688 return false;
689
690 if (cm_monitor())
691 goto out;
692
693 ret = true;
694 mutex_lock(&cm_list_mtx);
695 list_for_each_entry(cm, &cm_list, entry) {
696 if (cm->status_save_ext_pwr_inserted != is_ext_pwr_online(cm) ||
Axel Linbb2a95c2012-01-12 12:56:35 +0800697 cm->status_save_batt != is_batt_present(cm)) {
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900698 ret = false;
Axel Linbb2a95c2012-01-12 12:56:35 +0800699 break;
700 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900701 }
702 mutex_unlock(&cm_list_mtx);
703
704 cm_rtc_set = cm_setup_timer();
705out:
706 /* It's about the time when the non-CM appointed timer goes off */
707 if (rtc_wkalarm_save.enabled) {
708 unsigned long now;
709 struct rtc_time tmp;
710
711 rtc_read_time(rtc_dev, &tmp);
712 rtc_tm_to_time(&tmp, &now);
713
714 if (rtc_wkalarm_save_time &&
715 now + CM_RTC_SMALL >= rtc_wkalarm_save_time)
716 return false;
717 }
718 return ret;
719}
720EXPORT_SYMBOL_GPL(cm_suspend_again);
721
722/**
723 * setup_charger_manager - initialize charger_global_desc data
724 * @gd: pointer to instance of charger_global_desc
725 */
726int setup_charger_manager(struct charger_global_desc *gd)
727{
728 if (!gd)
729 return -EINVAL;
730
731 if (rtc_dev)
732 rtc_class_close(rtc_dev);
733 rtc_dev = NULL;
734 g_desc = NULL;
735
736 if (!gd->rtc_only_wakeup) {
737 pr_err("The callback rtc_only_wakeup is not given.\n");
738 return -EINVAL;
739 }
740
741 if (gd->rtc_name) {
742 rtc_dev = rtc_class_open(gd->rtc_name);
743 if (IS_ERR_OR_NULL(rtc_dev)) {
744 rtc_dev = NULL;
745 /* Retry at probe. RTC may be not registered yet */
746 }
747 } else {
748 pr_warn("No wakeup timer is given for charger manager."
749 "In-suspend monitoring won't work.\n");
750 }
751
752 g_desc = gd;
753 return 0;
754}
755EXPORT_SYMBOL_GPL(setup_charger_manager);
756
757static int charger_manager_probe(struct platform_device *pdev)
758{
759 struct charger_desc *desc = dev_get_platdata(&pdev->dev);
760 struct charger_manager *cm;
761 int ret = 0, i = 0;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900762 union power_supply_propval val;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900763
764 if (g_desc && !rtc_dev && g_desc->rtc_name) {
765 rtc_dev = rtc_class_open(g_desc->rtc_name);
766 if (IS_ERR_OR_NULL(rtc_dev)) {
767 rtc_dev = NULL;
768 dev_err(&pdev->dev, "Cannot get RTC %s.\n",
769 g_desc->rtc_name);
770 ret = -ENODEV;
771 goto err_alloc;
772 }
773 }
774
775 if (!desc) {
776 dev_err(&pdev->dev, "No platform data (desc) found.\n");
777 ret = -ENODEV;
778 goto err_alloc;
779 }
780
781 cm = kzalloc(sizeof(struct charger_manager), GFP_KERNEL);
782 if (!cm) {
783 dev_err(&pdev->dev, "Cannot allocate memory.\n");
784 ret = -ENOMEM;
785 goto err_alloc;
786 }
787
788 /* Basic Values. Unspecified are Null or 0 */
789 cm->dev = &pdev->dev;
790 cm->desc = kzalloc(sizeof(struct charger_desc), GFP_KERNEL);
791 if (!cm->desc) {
792 dev_err(&pdev->dev, "Cannot allocate memory.\n");
793 ret = -ENOMEM;
794 goto err_alloc_desc;
795 }
796 memcpy(cm->desc, desc, sizeof(struct charger_desc));
797 cm->last_temp_mC = INT_MIN; /* denotes "unmeasured, yet" */
798
799 if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
800 ret = -EINVAL;
801 dev_err(&pdev->dev, "charger_regulators undefined.\n");
802 goto err_no_charger;
803 }
804
805 if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
806 dev_err(&pdev->dev, "No power supply defined.\n");
807 ret = -EINVAL;
808 goto err_no_charger_stat;
809 }
810
811 /* Counting index only */
812 while (desc->psy_charger_stat[i])
813 i++;
814
815 cm->charger_stat = kzalloc(sizeof(struct power_supply *) * (i + 1),
816 GFP_KERNEL);
817 if (!cm->charger_stat) {
818 ret = -ENOMEM;
819 goto err_no_charger_stat;
820 }
821
822 for (i = 0; desc->psy_charger_stat[i]; i++) {
823 cm->charger_stat[i] = power_supply_get_by_name(
824 desc->psy_charger_stat[i]);
825 if (!cm->charger_stat[i]) {
826 dev_err(&pdev->dev, "Cannot find power supply "
827 "\"%s\"\n",
828 desc->psy_charger_stat[i]);
829 ret = -ENODEV;
830 goto err_chg_stat;
831 }
832 }
833
834 cm->fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
835 if (!cm->fuel_gauge) {
836 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
837 desc->psy_fuel_gauge);
838 ret = -ENODEV;
839 goto err_chg_stat;
840 }
841
842 if (desc->polling_interval_ms == 0 ||
843 msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) {
844 dev_err(&pdev->dev, "polling_interval_ms is too small\n");
845 ret = -EINVAL;
846 goto err_chg_stat;
847 }
848
849 if (!desc->temperature_out_of_range) {
850 dev_err(&pdev->dev, "there is no temperature_out_of_range\n");
851 ret = -EINVAL;
852 goto err_chg_stat;
853 }
854
855 platform_set_drvdata(pdev, cm);
856
Axel Linbb2a95c2012-01-12 12:56:35 +0800857 memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default));
858
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900859 if (!desc->psy_name) {
Axel Linbb2a95c2012-01-12 12:56:35 +0800860 strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900861 } else {
862 strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
863 }
864 cm->charger_psy.name = cm->psy_name_buf;
865
866 /* Allocate for psy properties because they may vary */
867 cm->charger_psy.properties = kzalloc(sizeof(enum power_supply_property)
868 * (ARRAY_SIZE(default_charger_props) +
869 NUM_CHARGER_PSY_OPTIONAL),
870 GFP_KERNEL);
871 if (!cm->charger_psy.properties) {
872 dev_err(&pdev->dev, "Cannot allocate for psy properties.\n");
873 ret = -ENOMEM;
874 goto err_chg_stat;
875 }
876 memcpy(cm->charger_psy.properties, default_charger_props,
877 sizeof(enum power_supply_property) *
878 ARRAY_SIZE(default_charger_props));
879 cm->charger_psy.num_properties = psy_default.num_properties;
880
881 /* Find which optional psy-properties are available */
882 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
883 POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
884 cm->charger_psy.properties[cm->charger_psy.num_properties] =
885 POWER_SUPPLY_PROP_CHARGE_NOW;
886 cm->charger_psy.num_properties++;
887 }
888 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
889 POWER_SUPPLY_PROP_CURRENT_NOW,
890 &val)) {
891 cm->charger_psy.properties[cm->charger_psy.num_properties] =
892 POWER_SUPPLY_PROP_CURRENT_NOW;
893 cm->charger_psy.num_properties++;
894 }
Axel Linbb2a95c2012-01-12 12:56:35 +0800895
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900896 if (desc->measure_battery_temp) {
897 cm->charger_psy.properties[cm->charger_psy.num_properties] =
898 POWER_SUPPLY_PROP_TEMP;
899 cm->charger_psy.num_properties++;
Axel Linbb2a95c2012-01-12 12:56:35 +0800900 } else {
901 cm->charger_psy.properties[cm->charger_psy.num_properties] =
902 POWER_SUPPLY_PROP_TEMP_AMBIENT;
903 cm->charger_psy.num_properties++;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900904 }
905
906 ret = power_supply_register(NULL, &cm->charger_psy);
907 if (ret) {
908 dev_err(&pdev->dev, "Cannot register charger-manager with"
909 " name \"%s\".\n", cm->charger_psy.name);
910 goto err_register;
911 }
912
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900913 ret = regulator_bulk_get(&pdev->dev, desc->num_charger_regulators,
914 desc->charger_regulators);
915 if (ret) {
916 dev_err(&pdev->dev, "Cannot get charger regulators.\n");
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900917 goto err_bulk_get;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900918 }
919
920 ret = try_charger_enable(cm, true);
921 if (ret) {
922 dev_err(&pdev->dev, "Cannot enable charger regulators\n");
923 goto err_chg_enable;
924 }
925
926 /* Add to the list */
927 mutex_lock(&cm_list_mtx);
928 list_add(&cm->entry, &cm_list);
929 mutex_unlock(&cm_list_mtx);
930
931 return 0;
932
933err_chg_enable:
Axel Linbb2a95c2012-01-12 12:56:35 +0800934 regulator_bulk_free(desc->num_charger_regulators,
935 desc->charger_regulators);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900936err_bulk_get:
937 power_supply_unregister(&cm->charger_psy);
938err_register:
939 kfree(cm->charger_psy.properties);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900940err_chg_stat:
941 kfree(cm->charger_stat);
942err_no_charger_stat:
943err_no_charger:
944 kfree(cm->desc);
945err_alloc_desc:
946 kfree(cm);
947err_alloc:
948 return ret;
949}
950
951static int __devexit charger_manager_remove(struct platform_device *pdev)
952{
953 struct charger_manager *cm = platform_get_drvdata(pdev);
954 struct charger_desc *desc = cm->desc;
955
956 /* Remove from the list */
957 mutex_lock(&cm_list_mtx);
958 list_del(&cm->entry);
959 mutex_unlock(&cm_list_mtx);
960
Axel Linbb2a95c2012-01-12 12:56:35 +0800961 regulator_bulk_free(desc->num_charger_regulators,
962 desc->charger_regulators);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900963 power_supply_unregister(&cm->charger_psy);
964 kfree(cm->charger_psy.properties);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900965 kfree(cm->charger_stat);
966 kfree(cm->desc);
967 kfree(cm);
968
969 return 0;
970}
971
Axel Lin1bbe24d2012-01-11 17:19:45 +0800972static const struct platform_device_id charger_manager_id[] = {
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900973 { "charger-manager", 0 },
974 { },
975};
Axel Lin1bbe24d2012-01-11 17:19:45 +0800976MODULE_DEVICE_TABLE(platform, charger_manager_id);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900977
978static int cm_suspend_prepare(struct device *dev)
979{
Axel Linbb2a95c2012-01-12 12:56:35 +0800980 struct charger_manager *cm = dev_get_drvdata(dev);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900981
982 if (!cm_suspended) {
983 if (rtc_dev) {
984 struct rtc_time tmp;
985 unsigned long now;
986
987 rtc_read_alarm(rtc_dev, &rtc_wkalarm_save);
988 rtc_read_time(rtc_dev, &tmp);
989
990 if (rtc_wkalarm_save.enabled) {
991 rtc_tm_to_time(&rtc_wkalarm_save.time,
992 &rtc_wkalarm_save_time);
993 rtc_tm_to_time(&tmp, &now);
994 if (now > rtc_wkalarm_save_time)
995 rtc_wkalarm_save_time = 0;
996 } else {
997 rtc_wkalarm_save_time = 0;
998 }
999 }
1000 cm_suspended = true;
1001 }
1002
1003 cm->status_save_ext_pwr_inserted = is_ext_pwr_online(cm);
1004 cm->status_save_batt = is_batt_present(cm);
1005
1006 if (!cm_rtc_set) {
1007 cm_suspend_duration_ms = 0;
1008 cm_rtc_set = cm_setup_timer();
1009 }
1010
1011 return 0;
1012}
1013
1014static void cm_suspend_complete(struct device *dev)
1015{
Axel Linbb2a95c2012-01-12 12:56:35 +08001016 struct charger_manager *cm = dev_get_drvdata(dev);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001017
1018 if (cm_suspended) {
1019 if (rtc_dev) {
1020 struct rtc_wkalrm tmp;
1021
1022 rtc_read_alarm(rtc_dev, &tmp);
1023 rtc_wkalarm_save.pending = tmp.pending;
1024 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1025 }
1026 cm_suspended = false;
1027 cm_rtc_set = false;
1028 }
1029
1030 uevent_notify(cm, NULL);
1031}
1032
1033static const struct dev_pm_ops charger_manager_pm = {
1034 .prepare = cm_suspend_prepare,
1035 .complete = cm_suspend_complete,
1036};
1037
1038static struct platform_driver charger_manager_driver = {
1039 .driver = {
1040 .name = "charger-manager",
1041 .owner = THIS_MODULE,
1042 .pm = &charger_manager_pm,
1043 },
1044 .probe = charger_manager_probe,
1045 .remove = __devexit_p(charger_manager_remove),
1046 .id_table = charger_manager_id,
1047};
1048
1049static int __init charger_manager_init(void)
1050{
1051 return platform_driver_register(&charger_manager_driver);
1052}
1053late_initcall(charger_manager_init);
1054
1055static void __exit charger_manager_cleanup(void)
1056{
1057 platform_driver_unregister(&charger_manager_driver);
1058}
1059module_exit(charger_manager_cleanup);
1060
1061MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1062MODULE_DESCRIPTION("Charger Manager");
1063MODULE_LICENSE("GPL");