Merge branch 'i2c-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jdelvar...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / power / charger-manager.c
blob0378d019efae4454395485571e273ce45c8f6abf
1 /*
2 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
3 * MyungJoo Ham <myungjoo.ham@samsung.com>
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.
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 **/
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>
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.
31 #define CM_JIFFIES_SMALL (2)
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))
37 * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking
38 * rtc alarm. It should be 2 or larger
40 #define CM_RTC_SMALL (2)
42 #define UEVENT_BUF_SIZE 32
44 static LIST_HEAD(cm_list);
45 static DEFINE_MUTEX(cm_list_mtx);
47 /* About in-suspend (suspend-again) monitoring */
48 static struct rtc_device *rtc_dev;
50 * Backup RTC alarm
51 * Save the wakeup alarm before entering suspend-to-RAM
53 static struct rtc_wkalrm rtc_wkalarm_save;
54 /* Backup RTC alarm time in terms of seconds since 01-01-1970 00:00:00 */
55 static unsigned long rtc_wkalarm_save_time;
56 static bool cm_suspended;
57 static bool cm_rtc_set;
58 static unsigned long cm_suspend_duration_ms;
60 /* Global charger-manager description */
61 static struct charger_global_desc *g_desc; /* init with setup_charger_manager */
63 /**
64 * is_batt_present - See if the battery presents in place.
65 * @cm: the Charger Manager representing the battery.
67 static bool is_batt_present(struct charger_manager *cm)
69 union power_supply_propval val;
70 bool present = false;
71 int i, ret;
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;
90 break;
93 return present;
96 /**
97 * is_ext_pwr_online - See if an external power source is attached to charge
98 * @cm: the Charger Manager representing the battery.
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.
104 static bool is_ext_pwr_online(struct charger_manager *cm)
106 union power_supply_propval val;
107 bool online = false;
108 int i, ret;
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;
121 return online;
125 * get_batt_uV - Get the voltage level of the battery
126 * @cm: the Charger Manager representing the battery.
127 * @uV: the voltage level returned.
129 * Returns 0 if there is no error.
130 * Returns a negative value on error.
132 static int get_batt_uV(struct charger_manager *cm, int *uV)
134 union power_supply_propval val;
135 int ret;
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;
143 if (ret)
144 return ret;
146 *uV = val.intval;
147 return 0;
151 * is_charging - Returns true if the battery is being charged.
152 * @cm: the Charger Manager representing the battery.
154 static bool is_charging(struct charger_manager *cm)
156 int i, ret;
157 bool charging = false;
158 union power_supply_propval val;
160 /* If there is no battery, it cannot be charged */
161 if (!is_batt_present(cm))
162 return false;
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;
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;
181 if (val.intval == 0)
182 continue;
185 * 3. The charger should not be FULL, DISCHARGING,
186 * or NOT_CHARGING.
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;
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;
201 /* Then, this is charging. */
202 charging = true;
203 break;
206 return charging;
210 * is_polling_required - Return true if need to continue polling for this CM.
211 * @cm: the Charger Manager representing the battery.
213 static bool is_polling_required(struct charger_manager *cm)
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);
229 return false;
233 * try_charger_enable - Enable/Disable chargers altogether
234 * @cm: the Charger Manager representing the battery.
235 * @enable: true: enable / false: disable
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.
242 static int try_charger_enable(struct charger_manager *cm, bool enable)
244 int err = 0, i;
245 struct charger_desc *desc = cm->desc;
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;
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 {
260 * Abnormal battery state - Stop charging forcibly,
261 * even if charger was enabled at the other places
263 err = regulator_bulk_disable(desc->num_charger_regulators,
264 desc->charger_regulators);
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);
278 if (!err)
279 cm->charger_enabled = enable;
281 return err;
285 * uevent_notify - Let users know something has changed.
286 * @cm: the Charger Manager representing the battery.
287 * @event: the event string.
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.
296 static void uevent_notify(struct charger_manager *cm, const char *event)
298 static char env_str[UEVENT_BUF_SIZE + 1] = "";
299 static char env_str_save[UEVENT_BUF_SIZE + 1] = "";
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;
310 if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE))
311 return; /* Duplicated. */
312 else
313 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
315 return;
318 if (event == NULL) {
319 /* No messages pending */
320 if (!env_str_save[0])
321 return;
323 strncpy(env_str, env_str_save, UEVENT_BUF_SIZE);
324 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
325 env_str_save[0] = 0;
327 return;
330 /* status not changed */
331 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
332 return;
334 /* save the status and notify the update */
335 strncpy(env_str, event, UEVENT_BUF_SIZE);
336 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
338 dev_info(cm->dev, event);
342 * _cm_monitor - Monitor the temperature and return true for exceptions.
343 * @cm: the Charger Manager representing the battery.
345 * Returns true if there is an event to notify for the battery.
346 * (True if the status of "emergency_stop" changes)
348 static bool _cm_monitor(struct charger_manager *cm)
350 struct charger_desc *desc = cm->desc;
351 int temp = desc->temperature_out_of_range(&cm->last_temp_mC);
353 dev_dbg(cm->dev, "monitoring (%2.2d.%3.3dC)\n",
354 cm->last_temp_mC / 1000, cm->last_temp_mC % 1000);
356 /* It has been stopped or charging already */
357 if (!!temp == !!cm->emergency_stop)
358 return false;
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");
368 } else {
369 cm->emergency_stop = 0;
370 if (!try_charger_enable(cm, true))
371 uevent_notify(cm, "CHARGING");
374 return true;
378 * cm_monitor - Monitor every battery.
380 * Returns true if there is an event to notify from any of the batteries.
381 * (True if the status of "emergency_stop" changes)
383 static bool cm_monitor(void)
385 bool stop = false;
386 struct charger_manager *cm;
388 mutex_lock(&cm_list_mtx);
390 list_for_each_entry(cm, &cm_list, entry)
391 stop = stop || _cm_monitor(cm);
393 mutex_unlock(&cm_list_mtx);
395 return stop;
398 static int charger_get_property(struct power_supply *psy,
399 enum power_supply_property psp,
400 union power_supply_propval *val)
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;
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;
460 if (!is_batt_present(cm)) {
461 /* There is no battery. Assume 100% */
462 val->intval = 100;
463 break;
466 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
467 POWER_SUPPLY_PROP_CAPACITY, val);
468 if (ret)
469 break;
471 if (val->intval > 100) {
472 val->intval = 100;
473 break;
475 if (val->intval < 0)
476 val->intval = 0;
478 /* Do not adjust SOC when charging: voltage is overrated */
479 if (is_charging(cm))
480 break;
483 * If the capacity value is inconsistent, calibrate it base on
484 * the battery voltage values and the thresholds given as desc
486 ret = get_batt_uV(cm, &uV);
487 if (ret) {
488 /* Voltage information not available. No calibration */
489 ret = 0;
490 break;
493 if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
494 !is_charging(cm)) {
495 val->intval = 100;
496 break;
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;
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;
520 * Full if it's powered but not charging andi
521 * not forced stop by emergency
523 if (!cm->emergency_stop) {
524 val->intval = 1;
525 break;
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;
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;
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;
563 } else {
564 val->intval = 0;
566 break;
567 default:
568 return -EINVAL;
570 return ret;
573 #define NUM_CHARGER_PSY_OPTIONAL (4)
574 static 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,
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,
592 static 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,
601 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
602 * for suspend_again.
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.
610 static bool cm_setup_timer(void)
612 struct charger_manager *cm;
613 unsigned int wakeup_ms = UINT_MAX;
614 bool ret = false;
616 mutex_lock(&cm_list_mtx);
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);
627 mutex_unlock(&cm_list_mtx);
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);
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.
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;
649 ret = true;
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;
659 /* The timer is not appointed by CM */
660 ret = false;
663 pr_info("Waking up after %lu secs.\n",
664 time - now);
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;
673 if (rtc_dev)
674 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
675 return false;
679 * cm_suspend_again - Determine whether suspend again or not
681 * Returns true if the system should be suspended again
682 * Returns false if the system should be woken up
684 bool cm_suspend_again(void)
686 struct charger_manager *cm;
687 bool ret = false;
689 if (!g_desc || !g_desc->rtc_only_wakeup || !g_desc->rtc_only_wakeup() ||
690 !cm_rtc_set)
691 return false;
693 if (cm_monitor())
694 goto out;
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;
703 mutex_unlock(&cm_list_mtx);
705 cm_rtc_set = cm_setup_timer();
706 out:
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;
712 rtc_read_time(rtc_dev, &tmp);
713 rtc_tm_to_time(&tmp, &now);
715 if (rtc_wkalarm_save_time &&
716 now + CM_RTC_SMALL >= rtc_wkalarm_save_time)
717 return false;
719 return ret;
721 EXPORT_SYMBOL_GPL(cm_suspend_again);
724 * setup_charger_manager - initialize charger_global_desc data
725 * @gd: pointer to instance of charger_global_desc
727 int setup_charger_manager(struct charger_global_desc *gd)
729 if (!gd)
730 return -EINVAL;
732 if (rtc_dev)
733 rtc_class_close(rtc_dev);
734 rtc_dev = NULL;
735 g_desc = NULL;
737 if (!gd->rtc_only_wakeup) {
738 pr_err("The callback rtc_only_wakeup is not given.\n");
739 return -EINVAL;
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 */
748 } else {
749 pr_warn("No wakeup timer is given for charger manager."
750 "In-suspend monitoring won't work.\n");
753 g_desc = gd;
754 return 0;
756 EXPORT_SYMBOL_GPL(setup_charger_manager);
758 static int charger_manager_probe(struct platform_device *pdev)
760 struct charger_desc *desc = dev_get_platdata(&pdev->dev);
761 struct charger_manager *cm;
762 int ret = 0, i = 0;
763 union power_supply_propval val;
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;
776 if (!desc) {
777 dev_err(&pdev->dev, "No platform data (desc) found.\n");
778 ret = -ENODEV;
779 goto err_alloc;
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;
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;
797 memcpy(cm->desc, desc, sizeof(struct charger_desc));
798 cm->last_temp_mC = INT_MIN; /* denotes "unmeasured, yet" */
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;
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;
812 /* Counting index only */
813 while (desc->psy_charger_stat[i])
814 i++;
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;
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;
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;
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;
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;
856 platform_set_drvdata(pdev, cm);
858 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);
866 cm->charger_psy.name = cm->psy_name_buf;
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;
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;
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++;
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++;
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++;
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++;
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;
915 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");
919 goto err_bulk_get;
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;
928 /* Add to the list */
929 mutex_lock(&cm_list_mtx);
930 list_add(&cm->entry, &cm_list);
931 mutex_unlock(&cm_list_mtx);
933 return 0;
935 err_chg_enable:
936 if (desc->charger_regulators)
937 regulator_bulk_free(desc->num_charger_regulators,
938 desc->charger_regulators);
939 err_bulk_get:
940 power_supply_unregister(&cm->charger_psy);
941 err_register:
942 kfree(cm->charger_psy.properties);
943 err_chg_stat:
944 kfree(cm->charger_stat);
945 err_no_charger_stat:
946 err_no_charger:
947 kfree(cm->desc);
948 err_alloc_desc:
949 kfree(cm);
950 err_alloc:
951 return ret;
954 static int __devexit charger_manager_remove(struct platform_device *pdev)
956 struct charger_manager *cm = platform_get_drvdata(pdev);
957 struct charger_desc *desc = cm->desc;
959 /* Remove from the list */
960 mutex_lock(&cm_list_mtx);
961 list_del(&cm->entry);
962 mutex_unlock(&cm_list_mtx);
964 if (desc->charger_regulators)
965 regulator_bulk_free(desc->num_charger_regulators,
966 desc->charger_regulators);
968 power_supply_unregister(&cm->charger_psy);
969 kfree(cm->charger_psy.properties);
970 kfree(cm->charger_stat);
971 kfree(cm->desc);
972 kfree(cm);
974 return 0;
977 const struct platform_device_id charger_manager_id[] = {
978 { "charger-manager", 0 },
979 { },
982 static int cm_suspend_prepare(struct device *dev)
984 struct platform_device *pdev = container_of(dev, struct platform_device,
985 dev);
986 struct charger_manager *cm = platform_get_drvdata(pdev);
988 if (!cm_suspended) {
989 if (rtc_dev) {
990 struct rtc_time tmp;
991 unsigned long now;
993 rtc_read_alarm(rtc_dev, &rtc_wkalarm_save);
994 rtc_read_time(rtc_dev, &tmp);
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;
1006 cm_suspended = true;
1009 cm->status_save_ext_pwr_inserted = is_ext_pwr_online(cm);
1010 cm->status_save_batt = is_batt_present(cm);
1012 if (!cm_rtc_set) {
1013 cm_suspend_duration_ms = 0;
1014 cm_rtc_set = cm_setup_timer();
1017 return 0;
1020 static void cm_suspend_complete(struct device *dev)
1022 struct platform_device *pdev = container_of(dev, struct platform_device,
1023 dev);
1024 struct charger_manager *cm = platform_get_drvdata(pdev);
1026 if (cm_suspended) {
1027 if (rtc_dev) {
1028 struct rtc_wkalrm tmp;
1030 rtc_read_alarm(rtc_dev, &tmp);
1031 rtc_wkalarm_save.pending = tmp.pending;
1032 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1034 cm_suspended = false;
1035 cm_rtc_set = false;
1038 uevent_notify(cm, NULL);
1041 static const struct dev_pm_ops charger_manager_pm = {
1042 .prepare = cm_suspend_prepare,
1043 .complete = cm_suspend_complete,
1046 static struct platform_driver charger_manager_driver = {
1047 .driver = {
1048 .name = "charger-manager",
1049 .owner = THIS_MODULE,
1050 .pm = &charger_manager_pm,
1052 .probe = charger_manager_probe,
1053 .remove = __devexit_p(charger_manager_remove),
1054 .id_table = charger_manager_id,
1057 static int __init charger_manager_init(void)
1059 return platform_driver_register(&charger_manager_driver);
1061 late_initcall(charger_manager_init);
1063 static void __exit charger_manager_cleanup(void)
1065 platform_driver_unregister(&charger_manager_driver);
1067 module_exit(charger_manager_cleanup);
1069 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1070 MODULE_DESCRIPTION("Charger Manager");
1071 MODULE_LICENSE("GPL");
1072 MODULE_ALIAS("charger-manager");