blob: 0378d019efae4454395485571e273ce45c8f6abf [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
137 if (cm->fuel_gauge)
138 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
139 POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
140 else
141 return -ENODEV;
142
143 if (ret)
144 return ret;
145
146 *uV = val.intval;
147 return 0;
148}
149
150/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900151 * is_charging - Returns true if the battery is being charged.
152 * @cm: the Charger Manager representing the battery.
153 */
154static bool is_charging(struct charger_manager *cm)
155{
156 int i, ret;
157 bool charging = false;
158 union power_supply_propval val;
159
160 /* If there is no battery, it cannot be charged */
161 if (!is_batt_present(cm))
162 return false;
163
164 /* If at least one of the charger is charging, return yes */
165 for (i = 0; cm->charger_stat[i]; i++) {
166 /* 1. The charger sholuld not be DISABLED */
167 if (cm->emergency_stop)
168 continue;
169 if (!cm->charger_enabled)
170 continue;
171
172 /* 2. The charger should be online (ext-power) */
173 ret = cm->charger_stat[i]->get_property(
174 cm->charger_stat[i],
175 POWER_SUPPLY_PROP_ONLINE, &val);
176 if (ret) {
177 dev_warn(cm->dev, "Cannot read ONLINE value from %s.\n",
178 cm->desc->psy_charger_stat[i]);
179 continue;
180 }
181 if (val.intval == 0)
182 continue;
183
184 /*
185 * 3. The charger should not be FULL, DISCHARGING,
186 * or NOT_CHARGING.
187 */
188 ret = cm->charger_stat[i]->get_property(
189 cm->charger_stat[i],
190 POWER_SUPPLY_PROP_STATUS, &val);
191 if (ret) {
192 dev_warn(cm->dev, "Cannot read STATUS value from %s.\n",
193 cm->desc->psy_charger_stat[i]);
194 continue;
195 }
196 if (val.intval == POWER_SUPPLY_STATUS_FULL ||
197 val.intval == POWER_SUPPLY_STATUS_DISCHARGING ||
198 val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING)
199 continue;
200
201 /* Then, this is charging. */
202 charging = true;
203 break;
204 }
205
206 return charging;
207}
208
209/**
210 * is_polling_required - Return true if need to continue polling for this CM.
211 * @cm: the Charger Manager representing the battery.
212 */
213static bool is_polling_required(struct charger_manager *cm)
214{
215 switch (cm->desc->polling_mode) {
216 case CM_POLL_DISABLE:
217 return false;
218 case CM_POLL_ALWAYS:
219 return true;
220 case CM_POLL_EXTERNAL_POWER_ONLY:
221 return is_ext_pwr_online(cm);
222 case CM_POLL_CHARGING_ONLY:
223 return is_charging(cm);
224 default:
225 dev_warn(cm->dev, "Incorrect polling_mode (%d)\n",
226 cm->desc->polling_mode);
227 }
228
229 return false;
230}
231
232/**
233 * try_charger_enable - Enable/Disable chargers altogether
234 * @cm: the Charger Manager representing the battery.
235 * @enable: true: enable / false: disable
236 *
237 * Note that Charger Manager keeps the charger enabled regardless whether
238 * the charger is charging or not (because battery is full or no external
239 * power source exists) except when CM needs to disable chargers forcibly
240 * bacause of emergency causes; when the battery is overheated or too cold.
241 */
242static int try_charger_enable(struct charger_manager *cm, bool enable)
243{
244 int err = 0, i;
245 struct charger_desc *desc = cm->desc;
246
247 /* Ignore if it's redundent command */
248 if (enable && cm->charger_enabled)
249 return 0;
250 if (!enable && !cm->charger_enabled)
251 return 0;
252
253 if (enable) {
254 if (cm->emergency_stop)
255 return -EAGAIN;
256 err = regulator_bulk_enable(desc->num_charger_regulators,
257 desc->charger_regulators);
258 } else {
259 /*
260 * Abnormal battery state - Stop charging forcibly,
261 * even if charger was enabled at the other places
262 */
263 err = regulator_bulk_disable(desc->num_charger_regulators,
264 desc->charger_regulators);
265
266 for (i = 0; i < desc->num_charger_regulators; i++) {
267 if (regulator_is_enabled(
268 desc->charger_regulators[i].consumer)) {
269 regulator_force_disable(
270 desc->charger_regulators[i].consumer);
271 dev_warn(cm->dev,
272 "Disable regulator(%s) forcibly.\n",
273 desc->charger_regulators[i].supply);
274 }
275 }
276 }
277
278 if (!err)
279 cm->charger_enabled = enable;
280
281 return err;
282}
283
284/**
285 * uevent_notify - Let users know something has changed.
286 * @cm: the Charger Manager representing the battery.
287 * @event: the event string.
288 *
289 * If @event is null, it implies that uevent_notify is called
290 * by resume function. When called in the resume function, cm_suspended
291 * should be already reset to false in order to let uevent_notify
292 * notify the recent event during the suspend to users. While
293 * suspended, uevent_notify does not notify users, but tracks
294 * events so that uevent_notify can notify users later after resumed.
295 */
296static void uevent_notify(struct charger_manager *cm, const char *event)
297{
298 static char env_str[UEVENT_BUF_SIZE + 1] = "";
299 static char env_str_save[UEVENT_BUF_SIZE + 1] = "";
300
301 if (cm_suspended) {
302 /* Nothing in suspended-event buffer */
303 if (env_str_save[0] == 0) {
304 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
305 return; /* status not changed */
306 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
307 return;
308 }
309
310 if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE))
311 return; /* Duplicated. */
312 else
313 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
314
315 return;
316 }
317
318 if (event == NULL) {
319 /* No messages pending */
320 if (!env_str_save[0])
321 return;
322
323 strncpy(env_str, env_str_save, UEVENT_BUF_SIZE);
324 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
325 env_str_save[0] = 0;
326
327 return;
328 }
329
330 /* status not changed */
331 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
332 return;
333
334 /* save the status and notify the update */
335 strncpy(env_str, event, UEVENT_BUF_SIZE);
336 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
337
338 dev_info(cm->dev, event);
339}
340
341/**
342 * _cm_monitor - Monitor the temperature and return true for exceptions.
343 * @cm: the Charger Manager representing the battery.
344 *
345 * Returns true if there is an event to notify for the battery.
346 * (True if the status of "emergency_stop" changes)
347 */
348static bool _cm_monitor(struct charger_manager *cm)
349{
350 struct charger_desc *desc = cm->desc;
351 int temp = desc->temperature_out_of_range(&cm->last_temp_mC);
352
353 dev_dbg(cm->dev, "monitoring (%2.2d.%3.3dC)\n",
354 cm->last_temp_mC / 1000, cm->last_temp_mC % 1000);
355
356 /* It has been stopped or charging already */
357 if (!!temp == !!cm->emergency_stop)
358 return false;
359
360 if (temp) {
361 cm->emergency_stop = temp;
362 if (!try_charger_enable(cm, false)) {
363 if (temp > 0)
364 uevent_notify(cm, "OVERHEAT");
365 else
366 uevent_notify(cm, "COLD");
367 }
368 } else {
369 cm->emergency_stop = 0;
370 if (!try_charger_enable(cm, true))
371 uevent_notify(cm, "CHARGING");
372 }
373
374 return true;
375}
376
377/**
378 * cm_monitor - Monitor every battery.
379 *
380 * Returns true if there is an event to notify from any of the batteries.
381 * (True if the status of "emergency_stop" changes)
382 */
383static bool cm_monitor(void)
384{
385 bool stop = false;
386 struct charger_manager *cm;
387
388 mutex_lock(&cm_list_mtx);
389
390 list_for_each_entry(cm, &cm_list, entry)
391 stop = stop || _cm_monitor(cm);
392
393 mutex_unlock(&cm_list_mtx);
394
395 return stop;
396}
397
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900398static int charger_get_property(struct power_supply *psy,
399 enum power_supply_property psp,
400 union power_supply_propval *val)
401{
402 struct charger_manager *cm = container_of(psy,
403 struct charger_manager, charger_psy);
404 struct charger_desc *desc = cm->desc;
405 int i, ret = 0, uV;
406
407 switch (psp) {
408 case POWER_SUPPLY_PROP_STATUS:
409 if (is_charging(cm))
410 val->intval = POWER_SUPPLY_STATUS_CHARGING;
411 else if (is_ext_pwr_online(cm))
412 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
413 else
414 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
415 break;
416 case POWER_SUPPLY_PROP_HEALTH:
417 if (cm->emergency_stop > 0)
418 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
419 else if (cm->emergency_stop < 0)
420 val->intval = POWER_SUPPLY_HEALTH_COLD;
421 else
422 val->intval = POWER_SUPPLY_HEALTH_GOOD;
423 break;
424 case POWER_SUPPLY_PROP_PRESENT:
425 if (is_batt_present(cm))
426 val->intval = 1;
427 else
428 val->intval = 0;
429 break;
430 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
431 ret = get_batt_uV(cm, &i);
432 val->intval = i;
433 break;
434 case POWER_SUPPLY_PROP_CURRENT_NOW:
435 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
436 POWER_SUPPLY_PROP_CURRENT_NOW, val);
437 break;
438 case POWER_SUPPLY_PROP_TEMP:
439 /* in thenth of centigrade */
440 if (cm->last_temp_mC == INT_MIN)
441 desc->temperature_out_of_range(&cm->last_temp_mC);
442 val->intval = cm->last_temp_mC / 100;
443 if (!desc->measure_battery_temp)
444 ret = -ENODEV;
445 break;
446 case POWER_SUPPLY_PROP_TEMP_AMBIENT:
447 /* in thenth of centigrade */
448 if (cm->last_temp_mC == INT_MIN)
449 desc->temperature_out_of_range(&cm->last_temp_mC);
450 val->intval = cm->last_temp_mC / 100;
451 if (desc->measure_battery_temp)
452 ret = -ENODEV;
453 break;
454 case POWER_SUPPLY_PROP_CAPACITY:
455 if (!cm->fuel_gauge) {
456 ret = -ENODEV;
457 break;
458 }
459
460 if (!is_batt_present(cm)) {
461 /* There is no battery. Assume 100% */
462 val->intval = 100;
463 break;
464 }
465
466 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
467 POWER_SUPPLY_PROP_CAPACITY, val);
468 if (ret)
469 break;
470
471 if (val->intval > 100) {
472 val->intval = 100;
473 break;
474 }
475 if (val->intval < 0)
476 val->intval = 0;
477
478 /* Do not adjust SOC when charging: voltage is overrated */
479 if (is_charging(cm))
480 break;
481
482 /*
483 * If the capacity value is inconsistent, calibrate it base on
484 * the battery voltage values and the thresholds given as desc
485 */
486 ret = get_batt_uV(cm, &uV);
487 if (ret) {
488 /* Voltage information not available. No calibration */
489 ret = 0;
490 break;
491 }
492
493 if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
494 !is_charging(cm)) {
495 val->intval = 100;
496 break;
497 }
498
499 break;
500 case POWER_SUPPLY_PROP_ONLINE:
501 if (is_ext_pwr_online(cm))
502 val->intval = 1;
503 else
504 val->intval = 0;
505 break;
506 case POWER_SUPPLY_PROP_CHARGE_FULL:
507 if (cm->fuel_gauge) {
508 if (cm->fuel_gauge->get_property(cm->fuel_gauge,
509 POWER_SUPPLY_PROP_CHARGE_FULL, val) == 0)
510 break;
511 }
512
513 if (is_ext_pwr_online(cm)) {
514 /* Not full if it's charging. */
515 if (is_charging(cm)) {
516 val->intval = 0;
517 break;
518 }
519 /*
520 * Full if it's powered but not charging andi
521 * not forced stop by emergency
522 */
523 if (!cm->emergency_stop) {
524 val->intval = 1;
525 break;
526 }
527 }
528
529 /* Full if it's over the fullbatt voltage */
530 ret = get_batt_uV(cm, &uV);
531 if (!ret && desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
532 !is_charging(cm)) {
533 val->intval = 1;
534 break;
535 }
536
537 /* Full if the cap is 100 */
538 if (cm->fuel_gauge) {
539 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
540 POWER_SUPPLY_PROP_CAPACITY, val);
541 if (!ret && val->intval >= 100 && !is_charging(cm)) {
542 val->intval = 1;
543 break;
544 }
545 }
546
547 val->intval = 0;
548 ret = 0;
549 break;
550 case POWER_SUPPLY_PROP_CHARGE_NOW:
551 if (is_charging(cm)) {
552 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
553 POWER_SUPPLY_PROP_CHARGE_NOW,
554 val);
555 if (ret) {
556 val->intval = 1;
557 ret = 0;
558 } else {
559 /* If CHARGE_NOW is supplied, use it */
560 val->intval = (val->intval > 0) ?
561 val->intval : 1;
562 }
563 } else {
564 val->intval = 0;
565 }
566 break;
567 default:
568 return -EINVAL;
569 }
570 return ret;
571}
572
573#define NUM_CHARGER_PSY_OPTIONAL (4)
574static enum power_supply_property default_charger_props[] = {
575 /* Guaranteed to provide */
576 POWER_SUPPLY_PROP_STATUS,
577 POWER_SUPPLY_PROP_HEALTH,
578 POWER_SUPPLY_PROP_PRESENT,
579 POWER_SUPPLY_PROP_VOLTAGE_NOW,
580 POWER_SUPPLY_PROP_CAPACITY,
581 POWER_SUPPLY_PROP_ONLINE,
582 POWER_SUPPLY_PROP_CHARGE_FULL,
583 /*
584 * Optional properties are:
585 * POWER_SUPPLY_PROP_CHARGE_NOW,
586 * POWER_SUPPLY_PROP_CURRENT_NOW,
587 * POWER_SUPPLY_PROP_TEMP, and
588 * POWER_SUPPLY_PROP_TEMP_AMBIENT,
589 */
590};
591
592static struct power_supply psy_default = {
593 .name = "battery",
594 .type = POWER_SUPPLY_TYPE_BATTERY,
595 .properties = default_charger_props,
596 .num_properties = ARRAY_SIZE(default_charger_props),
597 .get_property = charger_get_property,
598};
599
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900600/**
601 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
602 * for suspend_again.
603 *
604 * Returns true if the alarm is set for Charger Manager to use.
605 * Returns false if
606 * cm_setup_timer fails to set an alarm,
607 * cm_setup_timer does not need to set an alarm for Charger Manager,
608 * or an alarm previously configured is to be used.
609 */
610static bool cm_setup_timer(void)
611{
612 struct charger_manager *cm;
613 unsigned int wakeup_ms = UINT_MAX;
614 bool ret = false;
615
616 mutex_lock(&cm_list_mtx);
617
618 list_for_each_entry(cm, &cm_list, entry) {
619 /* Skip if polling is not required for this CM */
620 if (!is_polling_required(cm) && !cm->emergency_stop)
621 continue;
622 if (cm->desc->polling_interval_ms == 0)
623 continue;
624 CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
625 }
626
627 mutex_unlock(&cm_list_mtx);
628
629 if (wakeup_ms < UINT_MAX && wakeup_ms > 0) {
630 pr_info("Charger Manager wakeup timer: %u ms.\n", wakeup_ms);
631 if (rtc_dev) {
632 struct rtc_wkalrm tmp;
633 unsigned long time, now;
634 unsigned long add = DIV_ROUND_UP(wakeup_ms, 1000);
635
636 /*
637 * Set alarm with the polling interval (wakeup_ms)
638 * except when rtc_wkalarm_save comes first.
639 * However, the alarm time should be NOW +
640 * CM_RTC_SMALL or later.
641 */
642 tmp.enabled = 1;
643 rtc_read_time(rtc_dev, &tmp.time);
644 rtc_tm_to_time(&tmp.time, &now);
645 if (add < CM_RTC_SMALL)
646 add = CM_RTC_SMALL;
647 time = now + add;
648
649 ret = true;
650
651 if (rtc_wkalarm_save.enabled &&
652 rtc_wkalarm_save_time &&
653 rtc_wkalarm_save_time < time) {
654 if (rtc_wkalarm_save_time < now + CM_RTC_SMALL)
655 time = now + CM_RTC_SMALL;
656 else
657 time = rtc_wkalarm_save_time;
658
659 /* The timer is not appointed by CM */
660 ret = false;
661 }
662
663 pr_info("Waking up after %lu secs.\n",
664 time - now);
665
666 rtc_time_to_tm(time, &tmp.time);
667 rtc_set_alarm(rtc_dev, &tmp);
668 cm_suspend_duration_ms += wakeup_ms;
669 return ret;
670 }
671 }
672
673 if (rtc_dev)
674 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
675 return false;
676}
677
678/**
679 * cm_suspend_again - Determine whether suspend again or not
680 *
681 * Returns true if the system should be suspended again
682 * Returns false if the system should be woken up
683 */
684bool cm_suspend_again(void)
685{
686 struct charger_manager *cm;
687 bool ret = false;
688
689 if (!g_desc || !g_desc->rtc_only_wakeup || !g_desc->rtc_only_wakeup() ||
690 !cm_rtc_set)
691 return false;
692
693 if (cm_monitor())
694 goto out;
695
696 ret = true;
697 mutex_lock(&cm_list_mtx);
698 list_for_each_entry(cm, &cm_list, entry) {
699 if (cm->status_save_ext_pwr_inserted != is_ext_pwr_online(cm) ||
700 cm->status_save_batt != is_batt_present(cm))
701 ret = false;
702 }
703 mutex_unlock(&cm_list_mtx);
704
705 cm_rtc_set = cm_setup_timer();
706out:
707 /* It's about the time when the non-CM appointed timer goes off */
708 if (rtc_wkalarm_save.enabled) {
709 unsigned long now;
710 struct rtc_time tmp;
711
712 rtc_read_time(rtc_dev, &tmp);
713 rtc_tm_to_time(&tmp, &now);
714
715 if (rtc_wkalarm_save_time &&
716 now + CM_RTC_SMALL >= rtc_wkalarm_save_time)
717 return false;
718 }
719 return ret;
720}
721EXPORT_SYMBOL_GPL(cm_suspend_again);
722
723/**
724 * setup_charger_manager - initialize charger_global_desc data
725 * @gd: pointer to instance of charger_global_desc
726 */
727int setup_charger_manager(struct charger_global_desc *gd)
728{
729 if (!gd)
730 return -EINVAL;
731
732 if (rtc_dev)
733 rtc_class_close(rtc_dev);
734 rtc_dev = NULL;
735 g_desc = NULL;
736
737 if (!gd->rtc_only_wakeup) {
738 pr_err("The callback rtc_only_wakeup is not given.\n");
739 return -EINVAL;
740 }
741
742 if (gd->rtc_name) {
743 rtc_dev = rtc_class_open(gd->rtc_name);
744 if (IS_ERR_OR_NULL(rtc_dev)) {
745 rtc_dev = NULL;
746 /* Retry at probe. RTC may be not registered yet */
747 }
748 } else {
749 pr_warn("No wakeup timer is given for charger manager."
750 "In-suspend monitoring won't work.\n");
751 }
752
753 g_desc = gd;
754 return 0;
755}
756EXPORT_SYMBOL_GPL(setup_charger_manager);
757
758static int charger_manager_probe(struct platform_device *pdev)
759{
760 struct charger_desc *desc = dev_get_platdata(&pdev->dev);
761 struct charger_manager *cm;
762 int ret = 0, i = 0;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900763 union power_supply_propval val;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900764
765 if (g_desc && !rtc_dev && g_desc->rtc_name) {
766 rtc_dev = rtc_class_open(g_desc->rtc_name);
767 if (IS_ERR_OR_NULL(rtc_dev)) {
768 rtc_dev = NULL;
769 dev_err(&pdev->dev, "Cannot get RTC %s.\n",
770 g_desc->rtc_name);
771 ret = -ENODEV;
772 goto err_alloc;
773 }
774 }
775
776 if (!desc) {
777 dev_err(&pdev->dev, "No platform data (desc) found.\n");
778 ret = -ENODEV;
779 goto err_alloc;
780 }
781
782 cm = kzalloc(sizeof(struct charger_manager), GFP_KERNEL);
783 if (!cm) {
784 dev_err(&pdev->dev, "Cannot allocate memory.\n");
785 ret = -ENOMEM;
786 goto err_alloc;
787 }
788
789 /* Basic Values. Unspecified are Null or 0 */
790 cm->dev = &pdev->dev;
791 cm->desc = kzalloc(sizeof(struct charger_desc), GFP_KERNEL);
792 if (!cm->desc) {
793 dev_err(&pdev->dev, "Cannot allocate memory.\n");
794 ret = -ENOMEM;
795 goto err_alloc_desc;
796 }
797 memcpy(cm->desc, desc, sizeof(struct charger_desc));
798 cm->last_temp_mC = INT_MIN; /* denotes "unmeasured, yet" */
799
800 if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
801 ret = -EINVAL;
802 dev_err(&pdev->dev, "charger_regulators undefined.\n");
803 goto err_no_charger;
804 }
805
806 if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
807 dev_err(&pdev->dev, "No power supply defined.\n");
808 ret = -EINVAL;
809 goto err_no_charger_stat;
810 }
811
812 /* Counting index only */
813 while (desc->psy_charger_stat[i])
814 i++;
815
816 cm->charger_stat = kzalloc(sizeof(struct power_supply *) * (i + 1),
817 GFP_KERNEL);
818 if (!cm->charger_stat) {
819 ret = -ENOMEM;
820 goto err_no_charger_stat;
821 }
822
823 for (i = 0; desc->psy_charger_stat[i]; i++) {
824 cm->charger_stat[i] = power_supply_get_by_name(
825 desc->psy_charger_stat[i]);
826 if (!cm->charger_stat[i]) {
827 dev_err(&pdev->dev, "Cannot find power supply "
828 "\"%s\"\n",
829 desc->psy_charger_stat[i]);
830 ret = -ENODEV;
831 goto err_chg_stat;
832 }
833 }
834
835 cm->fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
836 if (!cm->fuel_gauge) {
837 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
838 desc->psy_fuel_gauge);
839 ret = -ENODEV;
840 goto err_chg_stat;
841 }
842
843 if (desc->polling_interval_ms == 0 ||
844 msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) {
845 dev_err(&pdev->dev, "polling_interval_ms is too small\n");
846 ret = -EINVAL;
847 goto err_chg_stat;
848 }
849
850 if (!desc->temperature_out_of_range) {
851 dev_err(&pdev->dev, "there is no temperature_out_of_range\n");
852 ret = -EINVAL;
853 goto err_chg_stat;
854 }
855
856 platform_set_drvdata(pdev, cm);
857
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900858 memcpy(&cm->charger_psy, &psy_default,
859 sizeof(psy_default));
860 if (!desc->psy_name) {
861 strncpy(cm->psy_name_buf, psy_default.name,
862 PSY_NAME_MAX);
863 } else {
864 strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
865 }
866 cm->charger_psy.name = cm->psy_name_buf;
867
868 /* Allocate for psy properties because they may vary */
869 cm->charger_psy.properties = kzalloc(sizeof(enum power_supply_property)
870 * (ARRAY_SIZE(default_charger_props) +
871 NUM_CHARGER_PSY_OPTIONAL),
872 GFP_KERNEL);
873 if (!cm->charger_psy.properties) {
874 dev_err(&pdev->dev, "Cannot allocate for psy properties.\n");
875 ret = -ENOMEM;
876 goto err_chg_stat;
877 }
878 memcpy(cm->charger_psy.properties, default_charger_props,
879 sizeof(enum power_supply_property) *
880 ARRAY_SIZE(default_charger_props));
881 cm->charger_psy.num_properties = psy_default.num_properties;
882
883 /* Find which optional psy-properties are available */
884 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
885 POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
886 cm->charger_psy.properties[cm->charger_psy.num_properties] =
887 POWER_SUPPLY_PROP_CHARGE_NOW;
888 cm->charger_psy.num_properties++;
889 }
890 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
891 POWER_SUPPLY_PROP_CURRENT_NOW,
892 &val)) {
893 cm->charger_psy.properties[cm->charger_psy.num_properties] =
894 POWER_SUPPLY_PROP_CURRENT_NOW;
895 cm->charger_psy.num_properties++;
896 }
897 if (!desc->measure_battery_temp) {
898 cm->charger_psy.properties[cm->charger_psy.num_properties] =
899 POWER_SUPPLY_PROP_TEMP_AMBIENT;
900 cm->charger_psy.num_properties++;
901 }
902 if (desc->measure_battery_temp) {
903 cm->charger_psy.properties[cm->charger_psy.num_properties] =
904 POWER_SUPPLY_PROP_TEMP;
905 cm->charger_psy.num_properties++;
906 }
907
908 ret = power_supply_register(NULL, &cm->charger_psy);
909 if (ret) {
910 dev_err(&pdev->dev, "Cannot register charger-manager with"
911 " name \"%s\".\n", cm->charger_psy.name);
912 goto err_register;
913 }
914
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900915 ret = regulator_bulk_get(&pdev->dev, desc->num_charger_regulators,
916 desc->charger_regulators);
917 if (ret) {
918 dev_err(&pdev->dev, "Cannot get charger regulators.\n");
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900919 goto err_bulk_get;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900920 }
921
922 ret = try_charger_enable(cm, true);
923 if (ret) {
924 dev_err(&pdev->dev, "Cannot enable charger regulators\n");
925 goto err_chg_enable;
926 }
927
928 /* Add to the list */
929 mutex_lock(&cm_list_mtx);
930 list_add(&cm->entry, &cm_list);
931 mutex_unlock(&cm_list_mtx);
932
933 return 0;
934
935err_chg_enable:
936 if (desc->charger_regulators)
937 regulator_bulk_free(desc->num_charger_regulators,
938 desc->charger_regulators);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900939err_bulk_get:
940 power_supply_unregister(&cm->charger_psy);
941err_register:
942 kfree(cm->charger_psy.properties);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900943err_chg_stat:
944 kfree(cm->charger_stat);
945err_no_charger_stat:
946err_no_charger:
947 kfree(cm->desc);
948err_alloc_desc:
949 kfree(cm);
950err_alloc:
951 return ret;
952}
953
954static int __devexit charger_manager_remove(struct platform_device *pdev)
955{
956 struct charger_manager *cm = platform_get_drvdata(pdev);
957 struct charger_desc *desc = cm->desc;
958
959 /* Remove from the list */
960 mutex_lock(&cm_list_mtx);
961 list_del(&cm->entry);
962 mutex_unlock(&cm_list_mtx);
963
964 if (desc->charger_regulators)
965 regulator_bulk_free(desc->num_charger_regulators,
966 desc->charger_regulators);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900967
968 power_supply_unregister(&cm->charger_psy);
969 kfree(cm->charger_psy.properties);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900970 kfree(cm->charger_stat);
971 kfree(cm->desc);
972 kfree(cm);
973
974 return 0;
975}
976
977const struct platform_device_id charger_manager_id[] = {
978 { "charger-manager", 0 },
979 { },
980};
981
982static int cm_suspend_prepare(struct device *dev)
983{
984 struct platform_device *pdev = container_of(dev, struct platform_device,
985 dev);
986 struct charger_manager *cm = platform_get_drvdata(pdev);
987
988 if (!cm_suspended) {
989 if (rtc_dev) {
990 struct rtc_time tmp;
991 unsigned long now;
992
993 rtc_read_alarm(rtc_dev, &rtc_wkalarm_save);
994 rtc_read_time(rtc_dev, &tmp);
995
996 if (rtc_wkalarm_save.enabled) {
997 rtc_tm_to_time(&rtc_wkalarm_save.time,
998 &rtc_wkalarm_save_time);
999 rtc_tm_to_time(&tmp, &now);
1000 if (now > rtc_wkalarm_save_time)
1001 rtc_wkalarm_save_time = 0;
1002 } else {
1003 rtc_wkalarm_save_time = 0;
1004 }
1005 }
1006 cm_suspended = true;
1007 }
1008
1009 cm->status_save_ext_pwr_inserted = is_ext_pwr_online(cm);
1010 cm->status_save_batt = is_batt_present(cm);
1011
1012 if (!cm_rtc_set) {
1013 cm_suspend_duration_ms = 0;
1014 cm_rtc_set = cm_setup_timer();
1015 }
1016
1017 return 0;
1018}
1019
1020static void cm_suspend_complete(struct device *dev)
1021{
1022 struct platform_device *pdev = container_of(dev, struct platform_device,
1023 dev);
1024 struct charger_manager *cm = platform_get_drvdata(pdev);
1025
1026 if (cm_suspended) {
1027 if (rtc_dev) {
1028 struct rtc_wkalrm tmp;
1029
1030 rtc_read_alarm(rtc_dev, &tmp);
1031 rtc_wkalarm_save.pending = tmp.pending;
1032 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1033 }
1034 cm_suspended = false;
1035 cm_rtc_set = false;
1036 }
1037
1038 uevent_notify(cm, NULL);
1039}
1040
1041static const struct dev_pm_ops charger_manager_pm = {
1042 .prepare = cm_suspend_prepare,
1043 .complete = cm_suspend_complete,
1044};
1045
1046static struct platform_driver charger_manager_driver = {
1047 .driver = {
1048 .name = "charger-manager",
1049 .owner = THIS_MODULE,
1050 .pm = &charger_manager_pm,
1051 },
1052 .probe = charger_manager_probe,
1053 .remove = __devexit_p(charger_manager_remove),
1054 .id_table = charger_manager_id,
1055};
1056
1057static int __init charger_manager_init(void)
1058{
1059 return platform_driver_register(&charger_manager_driver);
1060}
1061late_initcall(charger_manager_init);
1062
1063static void __exit charger_manager_cleanup(void)
1064{
1065 platform_driver_unregister(&charger_manager_driver);
1066}
1067module_exit(charger_manager_cleanup);
1068
1069MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1070MODULE_DESCRIPTION("Charger Manager");
1071MODULE_LICENSE("GPL");
1072MODULE_ALIAS("charger-manager");