Merge tag 'for-linus-v3.6-rc1' of git://oss.sgi.com/xfs/xfs
[pandora-kernel.git] / drivers / power / charger-manager.c
1 /*
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 static const char * const default_event_names[] = {
27         [CM_EVENT_UNKNOWN] = "Unknown",
28         [CM_EVENT_BATT_FULL] = "Battery Full",
29         [CM_EVENT_BATT_IN] = "Battery Inserted",
30         [CM_EVENT_BATT_OUT] = "Battery Pulled Out",
31         [CM_EVENT_EXT_PWR_IN_OUT] = "External Power Attach/Detach",
32         [CM_EVENT_CHG_START_STOP] = "Charging Start/Stop",
33         [CM_EVENT_OTHERS] = "Other battery events"
34 };
35
36 /*
37  * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for
38  * delayed works so that we can run delayed works with CM_JIFFIES_SMALL
39  * without any delays.
40  */
41 #define CM_JIFFIES_SMALL        (2)
42
43 /* If y is valid (> 0) and smaller than x, do x = y */
44 #define CM_MIN_VALID(x, y)      x = (((y > 0) && ((x) > (y))) ? (y) : (x))
45
46 /*
47  * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking
48  * rtc alarm. It should be 2 or larger
49  */
50 #define CM_RTC_SMALL            (2)
51
52 #define UEVENT_BUF_SIZE         32
53
54 static LIST_HEAD(cm_list);
55 static DEFINE_MUTEX(cm_list_mtx);
56
57 /* About in-suspend (suspend-again) monitoring */
58 static struct rtc_device *rtc_dev;
59 /*
60  * Backup RTC alarm
61  * Save the wakeup alarm before entering suspend-to-RAM
62  */
63 static struct rtc_wkalrm rtc_wkalarm_save;
64 /* Backup RTC alarm time in terms of seconds since 01-01-1970 00:00:00 */
65 static unsigned long rtc_wkalarm_save_time;
66 static bool cm_suspended;
67 static bool cm_rtc_set;
68 static unsigned long cm_suspend_duration_ms;
69
70 /* About normal (not suspended) monitoring */
71 static unsigned long polling_jiffy = ULONG_MAX; /* ULONG_MAX: no polling */
72 static unsigned long next_polling; /* Next appointed polling time */
73 static struct workqueue_struct *cm_wq; /* init at driver add */
74 static struct delayed_work cm_monitor_work; /* init at driver add */
75
76 /* Global charger-manager description */
77 static struct charger_global_desc *g_desc; /* init with setup_charger_manager */
78
79 /**
80  * is_batt_present - See if the battery presents in place.
81  * @cm: the Charger Manager representing the battery.
82  */
83 static bool is_batt_present(struct charger_manager *cm)
84 {
85         union power_supply_propval val;
86         bool present = false;
87         int i, ret;
88
89         switch (cm->desc->battery_present) {
90         case CM_BATTERY_PRESENT:
91                 present = true;
92                 break;
93         case CM_NO_BATTERY:
94                 break;
95         case CM_FUEL_GAUGE:
96                 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
97                                 POWER_SUPPLY_PROP_PRESENT, &val);
98                 if (ret == 0 && val.intval)
99                         present = true;
100                 break;
101         case CM_CHARGER_STAT:
102                 for (i = 0; cm->charger_stat[i]; i++) {
103                         ret = cm->charger_stat[i]->get_property(
104                                         cm->charger_stat[i],
105                                         POWER_SUPPLY_PROP_PRESENT, &val);
106                         if (ret == 0 && val.intval) {
107                                 present = true;
108                                 break;
109                         }
110                 }
111                 break;
112         }
113
114         return present;
115 }
116
117 /**
118  * is_ext_pwr_online - See if an external power source is attached to charge
119  * @cm: the Charger Manager representing the battery.
120  *
121  * Returns true if at least one of the chargers of the battery has an external
122  * power source attached to charge the battery regardless of whether it is
123  * actually charging or not.
124  */
125 static bool is_ext_pwr_online(struct charger_manager *cm)
126 {
127         union power_supply_propval val;
128         bool online = false;
129         int i, ret;
130
131         /* If at least one of them has one, it's yes. */
132         for (i = 0; cm->charger_stat[i]; i++) {
133                 ret = cm->charger_stat[i]->get_property(
134                                 cm->charger_stat[i],
135                                 POWER_SUPPLY_PROP_ONLINE, &val);
136                 if (ret == 0 && val.intval) {
137                         online = true;
138                         break;
139                 }
140         }
141
142         return online;
143 }
144
145 /**
146  * get_batt_uV - Get the voltage level of the battery
147  * @cm: the Charger Manager representing the battery.
148  * @uV: the voltage level returned.
149  *
150  * Returns 0 if there is no error.
151  * Returns a negative value on error.
152  */
153 static int get_batt_uV(struct charger_manager *cm, int *uV)
154 {
155         union power_supply_propval val;
156         int ret;
157
158         if (!cm->fuel_gauge)
159                 return -ENODEV;
160
161         ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
162                                 POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
163         if (ret)
164                 return ret;
165
166         *uV = val.intval;
167         return 0;
168 }
169
170 /**
171  * is_charging - Returns true if the battery is being charged.
172  * @cm: the Charger Manager representing the battery.
173  */
174 static bool is_charging(struct charger_manager *cm)
175 {
176         int i, ret;
177         bool charging = false;
178         union power_supply_propval val;
179
180         /* If there is no battery, it cannot be charged */
181         if (!is_batt_present(cm))
182                 return false;
183
184         /* If at least one of the charger is charging, return yes */
185         for (i = 0; cm->charger_stat[i]; i++) {
186                 /* 1. The charger sholuld not be DISABLED */
187                 if (cm->emergency_stop)
188                         continue;
189                 if (!cm->charger_enabled)
190                         continue;
191
192                 /* 2. The charger should be online (ext-power) */
193                 ret = cm->charger_stat[i]->get_property(
194                                 cm->charger_stat[i],
195                                 POWER_SUPPLY_PROP_ONLINE, &val);
196                 if (ret) {
197                         dev_warn(cm->dev, "Cannot read ONLINE value from %s.\n",
198                                         cm->desc->psy_charger_stat[i]);
199                         continue;
200                 }
201                 if (val.intval == 0)
202                         continue;
203
204                 /*
205                  * 3. The charger should not be FULL, DISCHARGING,
206                  * or NOT_CHARGING.
207                  */
208                 ret = cm->charger_stat[i]->get_property(
209                                 cm->charger_stat[i],
210                                 POWER_SUPPLY_PROP_STATUS, &val);
211                 if (ret) {
212                         dev_warn(cm->dev, "Cannot read STATUS value from %s.\n",
213                                         cm->desc->psy_charger_stat[i]);
214                         continue;
215                 }
216                 if (val.intval == POWER_SUPPLY_STATUS_FULL ||
217                                 val.intval == POWER_SUPPLY_STATUS_DISCHARGING ||
218                                 val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING)
219                         continue;
220
221                 /* Then, this is charging. */
222                 charging = true;
223                 break;
224         }
225
226         return charging;
227 }
228
229 /**
230  * is_polling_required - Return true if need to continue polling for this CM.
231  * @cm: the Charger Manager representing the battery.
232  */
233 static bool is_polling_required(struct charger_manager *cm)
234 {
235         switch (cm->desc->polling_mode) {
236         case CM_POLL_DISABLE:
237                 return false;
238         case CM_POLL_ALWAYS:
239                 return true;
240         case CM_POLL_EXTERNAL_POWER_ONLY:
241                 return is_ext_pwr_online(cm);
242         case CM_POLL_CHARGING_ONLY:
243                 return is_charging(cm);
244         default:
245                 dev_warn(cm->dev, "Incorrect polling_mode (%d)\n",
246                         cm->desc->polling_mode);
247         }
248
249         return false;
250 }
251
252 /**
253  * try_charger_enable - Enable/Disable chargers altogether
254  * @cm: the Charger Manager representing the battery.
255  * @enable: true: enable / false: disable
256  *
257  * Note that Charger Manager keeps the charger enabled regardless whether
258  * the charger is charging or not (because battery is full or no external
259  * power source exists) except when CM needs to disable chargers forcibly
260  * bacause of emergency causes; when the battery is overheated or too cold.
261  */
262 static int try_charger_enable(struct charger_manager *cm, bool enable)
263 {
264         int err = 0, i;
265         struct charger_desc *desc = cm->desc;
266
267         /* Ignore if it's redundent command */
268         if (enable == cm->charger_enabled)
269                 return 0;
270
271         if (enable) {
272                 if (cm->emergency_stop)
273                         return -EAGAIN;
274                 err = regulator_bulk_enable(desc->num_charger_regulators,
275                                         desc->charger_regulators);
276         } else {
277                 /*
278                  * Abnormal battery state - Stop charging forcibly,
279                  * even if charger was enabled at the other places
280                  */
281                 err = regulator_bulk_disable(desc->num_charger_regulators,
282                                         desc->charger_regulators);
283
284                 for (i = 0; i < desc->num_charger_regulators; i++) {
285                         if (regulator_is_enabled(
286                                     desc->charger_regulators[i].consumer)) {
287                                 regulator_force_disable(
288                                         desc->charger_regulators[i].consumer);
289                                 dev_warn(cm->dev,
290                                         "Disable regulator(%s) forcibly.\n",
291                                         desc->charger_regulators[i].supply);
292                         }
293                 }
294         }
295
296         if (!err)
297                 cm->charger_enabled = enable;
298
299         return err;
300 }
301
302 /**
303  * try_charger_restart - Restart charging.
304  * @cm: the Charger Manager representing the battery.
305  *
306  * Restart charging by turning off and on the charger.
307  */
308 static int try_charger_restart(struct charger_manager *cm)
309 {
310         int err;
311
312         if (cm->emergency_stop)
313                 return -EAGAIN;
314
315         err = try_charger_enable(cm, false);
316         if (err)
317                 return err;
318
319         return try_charger_enable(cm, true);
320 }
321
322 /**
323  * uevent_notify - Let users know something has changed.
324  * @cm: the Charger Manager representing the battery.
325  * @event: the event string.
326  *
327  * If @event is null, it implies that uevent_notify is called
328  * by resume function. When called in the resume function, cm_suspended
329  * should be already reset to false in order to let uevent_notify
330  * notify the recent event during the suspend to users. While
331  * suspended, uevent_notify does not notify users, but tracks
332  * events so that uevent_notify can notify users later after resumed.
333  */
334 static void uevent_notify(struct charger_manager *cm, const char *event)
335 {
336         static char env_str[UEVENT_BUF_SIZE + 1] = "";
337         static char env_str_save[UEVENT_BUF_SIZE + 1] = "";
338
339         if (cm_suspended) {
340                 /* Nothing in suspended-event buffer */
341                 if (env_str_save[0] == 0) {
342                         if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
343                                 return; /* status not changed */
344                         strncpy(env_str_save, event, UEVENT_BUF_SIZE);
345                         return;
346                 }
347
348                 if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE))
349                         return; /* Duplicated. */
350                 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
351                 return;
352         }
353
354         if (event == NULL) {
355                 /* No messages pending */
356                 if (!env_str_save[0])
357                         return;
358
359                 strncpy(env_str, env_str_save, UEVENT_BUF_SIZE);
360                 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
361                 env_str_save[0] = 0;
362
363                 return;
364         }
365
366         /* status not changed */
367         if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
368                 return;
369
370         /* save the status and notify the update */
371         strncpy(env_str, event, UEVENT_BUF_SIZE);
372         kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
373
374         dev_info(cm->dev, event);
375 }
376
377 /**
378  * fullbatt_vchk - Check voltage drop some times after "FULL" event.
379  * @work: the work_struct appointing the function
380  *
381  * If a user has designated "fullbatt_vchkdrop_ms/uV" values with
382  * charger_desc, Charger Manager checks voltage drop after the battery
383  * "FULL" event. It checks whether the voltage has dropped more than
384  * fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms.
385  */
386 static void fullbatt_vchk(struct work_struct *work)
387 {
388         struct delayed_work *dwork = to_delayed_work(work);
389         struct charger_manager *cm = container_of(dwork,
390                         struct charger_manager, fullbatt_vchk_work);
391         struct charger_desc *desc = cm->desc;
392         int batt_uV, err, diff;
393
394         /* remove the appointment for fullbatt_vchk */
395         cm->fullbatt_vchk_jiffies_at = 0;
396
397         if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
398                 return;
399
400         err = get_batt_uV(cm, &batt_uV);
401         if (err) {
402                 dev_err(cm->dev, "%s: get_batt_uV error(%d).\n", __func__, err);
403                 return;
404         }
405
406         diff = cm->fullbatt_vchk_uV;
407         diff -= batt_uV;
408
409         dev_dbg(cm->dev, "VBATT dropped %duV after full-batt.\n", diff);
410
411         if (diff > desc->fullbatt_vchkdrop_uV) {
412                 try_charger_restart(cm);
413                 uevent_notify(cm, "Recharge");
414         }
415 }
416
417 /**
418  * _cm_monitor - Monitor the temperature and return true for exceptions.
419  * @cm: the Charger Manager representing the battery.
420  *
421  * Returns true if there is an event to notify for the battery.
422  * (True if the status of "emergency_stop" changes)
423  */
424 static bool _cm_monitor(struct charger_manager *cm)
425 {
426         struct charger_desc *desc = cm->desc;
427         int temp = desc->temperature_out_of_range(&cm->last_temp_mC);
428
429         dev_dbg(cm->dev, "monitoring (%2.2d.%3.3dC)\n",
430                 cm->last_temp_mC / 1000, cm->last_temp_mC % 1000);
431
432         /* It has been stopped or charging already */
433         if (!!temp == !!cm->emergency_stop)
434                 return false;
435
436         if (temp) {
437                 cm->emergency_stop = temp;
438                 if (!try_charger_enable(cm, false)) {
439                         if (temp > 0)
440                                 uevent_notify(cm, "OVERHEAT");
441                         else
442                                 uevent_notify(cm, "COLD");
443                 }
444         } else {
445                 cm->emergency_stop = 0;
446                 if (!try_charger_enable(cm, true))
447                         uevent_notify(cm, "CHARGING");
448         }
449
450         return true;
451 }
452
453 /**
454  * cm_monitor - Monitor every battery.
455  *
456  * Returns true if there is an event to notify from any of the batteries.
457  * (True if the status of "emergency_stop" changes)
458  */
459 static bool cm_monitor(void)
460 {
461         bool stop = false;
462         struct charger_manager *cm;
463
464         mutex_lock(&cm_list_mtx);
465
466         list_for_each_entry(cm, &cm_list, entry) {
467                 if (_cm_monitor(cm))
468                         stop = true;
469         }
470
471         mutex_unlock(&cm_list_mtx);
472
473         return stop;
474 }
475
476 /**
477  * _setup_polling - Setup the next instance of polling.
478  * @work: work_struct of the function _setup_polling.
479  */
480 static void _setup_polling(struct work_struct *work)
481 {
482         unsigned long min = ULONG_MAX;
483         struct charger_manager *cm;
484         bool keep_polling = false;
485         unsigned long _next_polling;
486
487         mutex_lock(&cm_list_mtx);
488
489         list_for_each_entry(cm, &cm_list, entry) {
490                 if (is_polling_required(cm) && cm->desc->polling_interval_ms) {
491                         keep_polling = true;
492
493                         if (min > cm->desc->polling_interval_ms)
494                                 min = cm->desc->polling_interval_ms;
495                 }
496         }
497
498         polling_jiffy = msecs_to_jiffies(min);
499         if (polling_jiffy <= CM_JIFFIES_SMALL)
500                 polling_jiffy = CM_JIFFIES_SMALL + 1;
501
502         if (!keep_polling)
503                 polling_jiffy = ULONG_MAX;
504         if (polling_jiffy == ULONG_MAX)
505                 goto out;
506
507         WARN(cm_wq == NULL, "charger-manager: workqueue not initialized"
508                             ". try it later. %s\n", __func__);
509
510         _next_polling = jiffies + polling_jiffy;
511
512         if (!delayed_work_pending(&cm_monitor_work) ||
513             (delayed_work_pending(&cm_monitor_work) &&
514              time_after(next_polling, _next_polling))) {
515                 cancel_delayed_work_sync(&cm_monitor_work);
516                 next_polling = jiffies + polling_jiffy;
517                 queue_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy);
518         }
519
520 out:
521         mutex_unlock(&cm_list_mtx);
522 }
523 static DECLARE_WORK(setup_polling, _setup_polling);
524
525 /**
526  * cm_monitor_poller - The Monitor / Poller.
527  * @work: work_struct of the function cm_monitor_poller
528  *
529  * During non-suspended state, cm_monitor_poller is used to poll and monitor
530  * the batteries.
531  */
532 static void cm_monitor_poller(struct work_struct *work)
533 {
534         cm_monitor();
535         schedule_work(&setup_polling);
536 }
537
538 /**
539  * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL
540  * @cm: the Charger Manager representing the battery.
541  */
542 static void fullbatt_handler(struct charger_manager *cm)
543 {
544         struct charger_desc *desc = cm->desc;
545
546         if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
547                 goto out;
548
549         if (cm_suspended)
550                 device_set_wakeup_capable(cm->dev, true);
551
552         if (delayed_work_pending(&cm->fullbatt_vchk_work))
553                 cancel_delayed_work(&cm->fullbatt_vchk_work);
554         queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
555                            msecs_to_jiffies(desc->fullbatt_vchkdrop_ms));
556         cm->fullbatt_vchk_jiffies_at = jiffies + msecs_to_jiffies(
557                                        desc->fullbatt_vchkdrop_ms);
558
559         if (cm->fullbatt_vchk_jiffies_at == 0)
560                 cm->fullbatt_vchk_jiffies_at = 1;
561
562 out:
563         dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged.\n");
564         uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
565 }
566
567 /**
568  * battout_handler - Event handler for CM_EVENT_BATT_OUT
569  * @cm: the Charger Manager representing the battery.
570  */
571 static void battout_handler(struct charger_manager *cm)
572 {
573         if (cm_suspended)
574                 device_set_wakeup_capable(cm->dev, true);
575
576         if (!is_batt_present(cm)) {
577                 dev_emerg(cm->dev, "Battery Pulled Out!\n");
578                 uevent_notify(cm, default_event_names[CM_EVENT_BATT_OUT]);
579         } else {
580                 uevent_notify(cm, "Battery Reinserted?");
581         }
582 }
583
584 /**
585  * misc_event_handler - Handler for other evnets
586  * @cm: the Charger Manager representing the battery.
587  * @type: the Charger Manager representing the battery.
588  */
589 static void misc_event_handler(struct charger_manager *cm,
590                         enum cm_event_types type)
591 {
592         if (cm_suspended)
593                 device_set_wakeup_capable(cm->dev, true);
594
595         if (!delayed_work_pending(&cm_monitor_work) &&
596             is_polling_required(cm) && cm->desc->polling_interval_ms)
597                 schedule_work(&setup_polling);
598         uevent_notify(cm, default_event_names[type]);
599 }
600
601 static int charger_get_property(struct power_supply *psy,
602                 enum power_supply_property psp,
603                 union power_supply_propval *val)
604 {
605         struct charger_manager *cm = container_of(psy,
606                         struct charger_manager, charger_psy);
607         struct charger_desc *desc = cm->desc;
608         int ret = 0;
609         int uV;
610
611         switch (psp) {
612         case POWER_SUPPLY_PROP_STATUS:
613                 if (is_charging(cm))
614                         val->intval = POWER_SUPPLY_STATUS_CHARGING;
615                 else if (is_ext_pwr_online(cm))
616                         val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
617                 else
618                         val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
619                 break;
620         case POWER_SUPPLY_PROP_HEALTH:
621                 if (cm->emergency_stop > 0)
622                         val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
623                 else if (cm->emergency_stop < 0)
624                         val->intval = POWER_SUPPLY_HEALTH_COLD;
625                 else
626                         val->intval = POWER_SUPPLY_HEALTH_GOOD;
627                 break;
628         case POWER_SUPPLY_PROP_PRESENT:
629                 if (is_batt_present(cm))
630                         val->intval = 1;
631                 else
632                         val->intval = 0;
633                 break;
634         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
635                 ret = get_batt_uV(cm, &val->intval);
636                 break;
637         case POWER_SUPPLY_PROP_CURRENT_NOW:
638                 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
639                                 POWER_SUPPLY_PROP_CURRENT_NOW, val);
640                 break;
641         case POWER_SUPPLY_PROP_TEMP:
642                 /* in thenth of centigrade */
643                 if (cm->last_temp_mC == INT_MIN)
644                         desc->temperature_out_of_range(&cm->last_temp_mC);
645                 val->intval = cm->last_temp_mC / 100;
646                 if (!desc->measure_battery_temp)
647                         ret = -ENODEV;
648                 break;
649         case POWER_SUPPLY_PROP_TEMP_AMBIENT:
650                 /* in thenth of centigrade */
651                 if (cm->last_temp_mC == INT_MIN)
652                         desc->temperature_out_of_range(&cm->last_temp_mC);
653                 val->intval = cm->last_temp_mC / 100;
654                 if (desc->measure_battery_temp)
655                         ret = -ENODEV;
656                 break;
657         case POWER_SUPPLY_PROP_CAPACITY:
658                 if (!cm->fuel_gauge) {
659                         ret = -ENODEV;
660                         break;
661                 }
662
663                 if (!is_batt_present(cm)) {
664                         /* There is no battery. Assume 100% */
665                         val->intval = 100;
666                         break;
667                 }
668
669                 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
670                                         POWER_SUPPLY_PROP_CAPACITY, val);
671                 if (ret)
672                         break;
673
674                 if (val->intval > 100) {
675                         val->intval = 100;
676                         break;
677                 }
678                 if (val->intval < 0)
679                         val->intval = 0;
680
681                 /* Do not adjust SOC when charging: voltage is overrated */
682                 if (is_charging(cm))
683                         break;
684
685                 /*
686                  * If the capacity value is inconsistent, calibrate it base on
687                  * the battery voltage values and the thresholds given as desc
688                  */
689                 ret = get_batt_uV(cm, &uV);
690                 if (ret) {
691                         /* Voltage information not available. No calibration */
692                         ret = 0;
693                         break;
694                 }
695
696                 if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
697                     !is_charging(cm)) {
698                         val->intval = 100;
699                         break;
700                 }
701
702                 break;
703         case POWER_SUPPLY_PROP_ONLINE:
704                 if (is_ext_pwr_online(cm))
705                         val->intval = 1;
706                 else
707                         val->intval = 0;
708                 break;
709         case POWER_SUPPLY_PROP_CHARGE_FULL:
710                 if (cm->fuel_gauge) {
711                         if (cm->fuel_gauge->get_property(cm->fuel_gauge,
712                             POWER_SUPPLY_PROP_CHARGE_FULL, val) == 0)
713                                 break;
714                 }
715
716                 if (is_ext_pwr_online(cm)) {
717                         /* Not full if it's charging. */
718                         if (is_charging(cm)) {
719                                 val->intval = 0;
720                                 break;
721                         }
722                         /*
723                          * Full if it's powered but not charging andi
724                          * not forced stop by emergency
725                          */
726                         if (!cm->emergency_stop) {
727                                 val->intval = 1;
728                                 break;
729                         }
730                 }
731
732                 /* Full if it's over the fullbatt voltage */
733                 ret = get_batt_uV(cm, &uV);
734                 if (!ret && desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
735                     !is_charging(cm)) {
736                         val->intval = 1;
737                         break;
738                 }
739
740                 /* Full if the cap is 100 */
741                 if (cm->fuel_gauge) {
742                         ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
743                                         POWER_SUPPLY_PROP_CAPACITY, val);
744                         if (!ret && val->intval >= 100 && !is_charging(cm)) {
745                                 val->intval = 1;
746                                 break;
747                         }
748                 }
749
750                 val->intval = 0;
751                 ret = 0;
752                 break;
753         case POWER_SUPPLY_PROP_CHARGE_NOW:
754                 if (is_charging(cm)) {
755                         ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
756                                                 POWER_SUPPLY_PROP_CHARGE_NOW,
757                                                 val);
758                         if (ret) {
759                                 val->intval = 1;
760                                 ret = 0;
761                         } else {
762                                 /* If CHARGE_NOW is supplied, use it */
763                                 val->intval = (val->intval > 0) ?
764                                                 val->intval : 1;
765                         }
766                 } else {
767                         val->intval = 0;
768                 }
769                 break;
770         default:
771                 return -EINVAL;
772         }
773         return ret;
774 }
775
776 #define NUM_CHARGER_PSY_OPTIONAL        (4)
777 static enum power_supply_property default_charger_props[] = {
778         /* Guaranteed to provide */
779         POWER_SUPPLY_PROP_STATUS,
780         POWER_SUPPLY_PROP_HEALTH,
781         POWER_SUPPLY_PROP_PRESENT,
782         POWER_SUPPLY_PROP_VOLTAGE_NOW,
783         POWER_SUPPLY_PROP_CAPACITY,
784         POWER_SUPPLY_PROP_ONLINE,
785         POWER_SUPPLY_PROP_CHARGE_FULL,
786         /*
787          * Optional properties are:
788          * POWER_SUPPLY_PROP_CHARGE_NOW,
789          * POWER_SUPPLY_PROP_CURRENT_NOW,
790          * POWER_SUPPLY_PROP_TEMP, and
791          * POWER_SUPPLY_PROP_TEMP_AMBIENT,
792          */
793 };
794
795 static struct power_supply psy_default = {
796         .name = "battery",
797         .type = POWER_SUPPLY_TYPE_BATTERY,
798         .properties = default_charger_props,
799         .num_properties = ARRAY_SIZE(default_charger_props),
800         .get_property = charger_get_property,
801 };
802
803 /**
804  * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
805  *                  for suspend_again.
806  *
807  * Returns true if the alarm is set for Charger Manager to use.
808  * Returns false if
809  *      cm_setup_timer fails to set an alarm,
810  *      cm_setup_timer does not need to set an alarm for Charger Manager,
811  *      or an alarm previously configured is to be used.
812  */
813 static bool cm_setup_timer(void)
814 {
815         struct charger_manager *cm;
816         unsigned int wakeup_ms = UINT_MAX;
817         bool ret = false;
818
819         mutex_lock(&cm_list_mtx);
820
821         list_for_each_entry(cm, &cm_list, entry) {
822                 unsigned int fbchk_ms = 0;
823
824                 /* fullbatt_vchk is required. setup timer for that */
825                 if (cm->fullbatt_vchk_jiffies_at) {
826                         fbchk_ms = jiffies_to_msecs(cm->fullbatt_vchk_jiffies_at
827                                                     - jiffies);
828                         if (time_is_before_eq_jiffies(
829                                 cm->fullbatt_vchk_jiffies_at) ||
830                                 msecs_to_jiffies(fbchk_ms) < CM_JIFFIES_SMALL) {
831                                 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
832                                 fbchk_ms = 0;
833                         }
834                 }
835                 CM_MIN_VALID(wakeup_ms, fbchk_ms);
836
837                 /* Skip if polling is not required for this CM */
838                 if (!is_polling_required(cm) && !cm->emergency_stop)
839                         continue;
840                 if (cm->desc->polling_interval_ms == 0)
841                         continue;
842                 CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
843         }
844
845         mutex_unlock(&cm_list_mtx);
846
847         if (wakeup_ms < UINT_MAX && wakeup_ms > 0) {
848                 pr_info("Charger Manager wakeup timer: %u ms.\n", wakeup_ms);
849                 if (rtc_dev) {
850                         struct rtc_wkalrm tmp;
851                         unsigned long time, now;
852                         unsigned long add = DIV_ROUND_UP(wakeup_ms, 1000);
853
854                         /*
855                          * Set alarm with the polling interval (wakeup_ms)
856                          * except when rtc_wkalarm_save comes first.
857                          * However, the alarm time should be NOW +
858                          * CM_RTC_SMALL or later.
859                          */
860                         tmp.enabled = 1;
861                         rtc_read_time(rtc_dev, &tmp.time);
862                         rtc_tm_to_time(&tmp.time, &now);
863                         if (add < CM_RTC_SMALL)
864                                 add = CM_RTC_SMALL;
865                         time = now + add;
866
867                         ret = true;
868
869                         if (rtc_wkalarm_save.enabled &&
870                             rtc_wkalarm_save_time &&
871                             rtc_wkalarm_save_time < time) {
872                                 if (rtc_wkalarm_save_time < now + CM_RTC_SMALL)
873                                         time = now + CM_RTC_SMALL;
874                                 else
875                                         time = rtc_wkalarm_save_time;
876
877                                 /* The timer is not appointed by CM */
878                                 ret = false;
879                         }
880
881                         pr_info("Waking up after %lu secs.\n",
882                                         time - now);
883
884                         rtc_time_to_tm(time, &tmp.time);
885                         rtc_set_alarm(rtc_dev, &tmp);
886                         cm_suspend_duration_ms += wakeup_ms;
887                         return ret;
888                 }
889         }
890
891         if (rtc_dev)
892                 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
893         return false;
894 }
895
896 static void _cm_fbchk_in_suspend(struct charger_manager *cm)
897 {
898         unsigned long jiffy_now = jiffies;
899
900         if (!cm->fullbatt_vchk_jiffies_at)
901                 return;
902
903         if (g_desc && g_desc->assume_timer_stops_in_suspend)
904                 jiffy_now += msecs_to_jiffies(cm_suspend_duration_ms);
905
906         /* Execute now if it's going to be executed not too long after */
907         jiffy_now += CM_JIFFIES_SMALL;
908
909         if (time_after_eq(jiffy_now, cm->fullbatt_vchk_jiffies_at))
910                 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
911 }
912
913 /**
914  * cm_suspend_again - Determine whether suspend again or not
915  *
916  * Returns true if the system should be suspended again
917  * Returns false if the system should be woken up
918  */
919 bool cm_suspend_again(void)
920 {
921         struct charger_manager *cm;
922         bool ret = false;
923
924         if (!g_desc || !g_desc->rtc_only_wakeup || !g_desc->rtc_only_wakeup() ||
925             !cm_rtc_set)
926                 return false;
927
928         if (cm_monitor())
929                 goto out;
930
931         ret = true;
932         mutex_lock(&cm_list_mtx);
933         list_for_each_entry(cm, &cm_list, entry) {
934                 _cm_fbchk_in_suspend(cm);
935
936                 if (cm->status_save_ext_pwr_inserted != is_ext_pwr_online(cm) ||
937                     cm->status_save_batt != is_batt_present(cm)) {
938                         ret = false;
939                         break;
940                 }
941         }
942         mutex_unlock(&cm_list_mtx);
943
944         cm_rtc_set = cm_setup_timer();
945 out:
946         /* It's about the time when the non-CM appointed timer goes off */
947         if (rtc_wkalarm_save.enabled) {
948                 unsigned long now;
949                 struct rtc_time tmp;
950
951                 rtc_read_time(rtc_dev, &tmp);
952                 rtc_tm_to_time(&tmp, &now);
953
954                 if (rtc_wkalarm_save_time &&
955                     now + CM_RTC_SMALL >= rtc_wkalarm_save_time)
956                         return false;
957         }
958         return ret;
959 }
960 EXPORT_SYMBOL_GPL(cm_suspend_again);
961
962 /**
963  * setup_charger_manager - initialize charger_global_desc data
964  * @gd: pointer to instance of charger_global_desc
965  */
966 int setup_charger_manager(struct charger_global_desc *gd)
967 {
968         if (!gd)
969                 return -EINVAL;
970
971         if (rtc_dev)
972                 rtc_class_close(rtc_dev);
973         rtc_dev = NULL;
974         g_desc = NULL;
975
976         if (!gd->rtc_only_wakeup) {
977                 pr_err("The callback rtc_only_wakeup is not given.\n");
978                 return -EINVAL;
979         }
980
981         if (gd->rtc_name) {
982                 rtc_dev = rtc_class_open(gd->rtc_name);
983                 if (IS_ERR_OR_NULL(rtc_dev)) {
984                         rtc_dev = NULL;
985                         /* Retry at probe. RTC may be not registered yet */
986                 }
987         } else {
988                 pr_warn("No wakeup timer is given for charger manager."
989                         "In-suspend monitoring won't work.\n");
990         }
991
992         g_desc = gd;
993         return 0;
994 }
995 EXPORT_SYMBOL_GPL(setup_charger_manager);
996
997 static int charger_manager_probe(struct platform_device *pdev)
998 {
999         struct charger_desc *desc = dev_get_platdata(&pdev->dev);
1000         struct charger_manager *cm;
1001         int ret = 0, i = 0;
1002         union power_supply_propval val;
1003
1004         if (g_desc && !rtc_dev && g_desc->rtc_name) {
1005                 rtc_dev = rtc_class_open(g_desc->rtc_name);
1006                 if (IS_ERR_OR_NULL(rtc_dev)) {
1007                         rtc_dev = NULL;
1008                         dev_err(&pdev->dev, "Cannot get RTC %s.\n",
1009                                 g_desc->rtc_name);
1010                         ret = -ENODEV;
1011                         goto err_alloc;
1012                 }
1013         }
1014
1015         if (!desc) {
1016                 dev_err(&pdev->dev, "No platform data (desc) found.\n");
1017                 ret = -ENODEV;
1018                 goto err_alloc;
1019         }
1020
1021         cm = kzalloc(sizeof(struct charger_manager), GFP_KERNEL);
1022         if (!cm) {
1023                 dev_err(&pdev->dev, "Cannot allocate memory.\n");
1024                 ret = -ENOMEM;
1025                 goto err_alloc;
1026         }
1027
1028         /* Basic Values. Unspecified are Null or 0 */
1029         cm->dev = &pdev->dev;
1030         cm->desc = kzalloc(sizeof(struct charger_desc), GFP_KERNEL);
1031         if (!cm->desc) {
1032                 dev_err(&pdev->dev, "Cannot allocate memory.\n");
1033                 ret = -ENOMEM;
1034                 goto err_alloc_desc;
1035         }
1036         memcpy(cm->desc, desc, sizeof(struct charger_desc));
1037         cm->last_temp_mC = INT_MIN; /* denotes "unmeasured, yet" */
1038
1039         /*
1040          * The following two do not need to be errors.
1041          * Users may intentionally ignore those two features.
1042          */
1043         if (desc->fullbatt_uV == 0) {
1044                 dev_info(&pdev->dev, "Ignoring full-battery voltage threshold"
1045                                         " as it is not supplied.");
1046         }
1047         if (!desc->fullbatt_vchkdrop_ms || !desc->fullbatt_vchkdrop_uV) {
1048                 dev_info(&pdev->dev, "Disabling full-battery voltage drop "
1049                                 "checking mechanism as it is not supplied.");
1050                 desc->fullbatt_vchkdrop_ms = 0;
1051                 desc->fullbatt_vchkdrop_uV = 0;
1052         }
1053
1054         if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
1055                 ret = -EINVAL;
1056                 dev_err(&pdev->dev, "charger_regulators undefined.\n");
1057                 goto err_no_charger;
1058         }
1059
1060         if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
1061                 dev_err(&pdev->dev, "No power supply defined.\n");
1062                 ret = -EINVAL;
1063                 goto err_no_charger_stat;
1064         }
1065
1066         /* Counting index only */
1067         while (desc->psy_charger_stat[i])
1068                 i++;
1069
1070         cm->charger_stat = kzalloc(sizeof(struct power_supply *) * (i + 1),
1071                                    GFP_KERNEL);
1072         if (!cm->charger_stat) {
1073                 ret = -ENOMEM;
1074                 goto err_no_charger_stat;
1075         }
1076
1077         for (i = 0; desc->psy_charger_stat[i]; i++) {
1078                 cm->charger_stat[i] = power_supply_get_by_name(
1079                                         desc->psy_charger_stat[i]);
1080                 if (!cm->charger_stat[i]) {
1081                         dev_err(&pdev->dev, "Cannot find power supply "
1082                                         "\"%s\"\n",
1083                                         desc->psy_charger_stat[i]);
1084                         ret = -ENODEV;
1085                         goto err_chg_stat;
1086                 }
1087         }
1088
1089         cm->fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
1090         if (!cm->fuel_gauge) {
1091                 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
1092                                 desc->psy_fuel_gauge);
1093                 ret = -ENODEV;
1094                 goto err_chg_stat;
1095         }
1096
1097         if (desc->polling_interval_ms == 0 ||
1098             msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) {
1099                 dev_err(&pdev->dev, "polling_interval_ms is too small\n");
1100                 ret = -EINVAL;
1101                 goto err_chg_stat;
1102         }
1103
1104         if (!desc->temperature_out_of_range) {
1105                 dev_err(&pdev->dev, "there is no temperature_out_of_range\n");
1106                 ret = -EINVAL;
1107                 goto err_chg_stat;
1108         }
1109
1110         platform_set_drvdata(pdev, cm);
1111
1112         memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default));
1113
1114         if (!desc->psy_name) {
1115                 strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
1116         } else {
1117                 strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
1118         }
1119         cm->charger_psy.name = cm->psy_name_buf;
1120
1121         /* Allocate for psy properties because they may vary */
1122         cm->charger_psy.properties = kzalloc(sizeof(enum power_supply_property)
1123                                 * (ARRAY_SIZE(default_charger_props) +
1124                                 NUM_CHARGER_PSY_OPTIONAL),
1125                                 GFP_KERNEL);
1126         if (!cm->charger_psy.properties) {
1127                 dev_err(&pdev->dev, "Cannot allocate for psy properties.\n");
1128                 ret = -ENOMEM;
1129                 goto err_chg_stat;
1130         }
1131         memcpy(cm->charger_psy.properties, default_charger_props,
1132                 sizeof(enum power_supply_property) *
1133                 ARRAY_SIZE(default_charger_props));
1134         cm->charger_psy.num_properties = psy_default.num_properties;
1135
1136         /* Find which optional psy-properties are available */
1137         if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1138                                           POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
1139                 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1140                                 POWER_SUPPLY_PROP_CHARGE_NOW;
1141                 cm->charger_psy.num_properties++;
1142         }
1143         if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1144                                           POWER_SUPPLY_PROP_CURRENT_NOW,
1145                                           &val)) {
1146                 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1147                                 POWER_SUPPLY_PROP_CURRENT_NOW;
1148                 cm->charger_psy.num_properties++;
1149         }
1150
1151         if (desc->measure_battery_temp) {
1152                 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1153                                 POWER_SUPPLY_PROP_TEMP;
1154                 cm->charger_psy.num_properties++;
1155         } else {
1156                 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1157                                 POWER_SUPPLY_PROP_TEMP_AMBIENT;
1158                 cm->charger_psy.num_properties++;
1159         }
1160
1161         INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk);
1162
1163         ret = power_supply_register(NULL, &cm->charger_psy);
1164         if (ret) {
1165                 dev_err(&pdev->dev, "Cannot register charger-manager with"
1166                                 " name \"%s\".\n", cm->charger_psy.name);
1167                 goto err_register;
1168         }
1169
1170         ret = regulator_bulk_get(&pdev->dev, desc->num_charger_regulators,
1171                                  desc->charger_regulators);
1172         if (ret) {
1173                 dev_err(&pdev->dev, "Cannot get charger regulators.\n");
1174                 goto err_bulk_get;
1175         }
1176
1177         ret = try_charger_enable(cm, true);
1178         if (ret) {
1179                 dev_err(&pdev->dev, "Cannot enable charger regulators\n");
1180                 goto err_chg_enable;
1181         }
1182
1183         /* Add to the list */
1184         mutex_lock(&cm_list_mtx);
1185         list_add(&cm->entry, &cm_list);
1186         mutex_unlock(&cm_list_mtx);
1187
1188         /*
1189          * Charger-manager is capable of waking up the systme from sleep
1190          * when event is happend through cm_notify_event()
1191          */
1192         device_init_wakeup(&pdev->dev, true);
1193         device_set_wakeup_capable(&pdev->dev, false);
1194
1195         schedule_work(&setup_polling);
1196
1197         return 0;
1198
1199 err_chg_enable:
1200         regulator_bulk_free(desc->num_charger_regulators,
1201                             desc->charger_regulators);
1202 err_bulk_get:
1203         power_supply_unregister(&cm->charger_psy);
1204 err_register:
1205         kfree(cm->charger_psy.properties);
1206 err_chg_stat:
1207         kfree(cm->charger_stat);
1208 err_no_charger_stat:
1209 err_no_charger:
1210         kfree(cm->desc);
1211 err_alloc_desc:
1212         kfree(cm);
1213 err_alloc:
1214         return ret;
1215 }
1216
1217 static int __devexit charger_manager_remove(struct platform_device *pdev)
1218 {
1219         struct charger_manager *cm = platform_get_drvdata(pdev);
1220         struct charger_desc *desc = cm->desc;
1221
1222         /* Remove from the list */
1223         mutex_lock(&cm_list_mtx);
1224         list_del(&cm->entry);
1225         mutex_unlock(&cm_list_mtx);
1226
1227         if (work_pending(&setup_polling))
1228                 cancel_work_sync(&setup_polling);
1229         if (delayed_work_pending(&cm_monitor_work))
1230                 cancel_delayed_work_sync(&cm_monitor_work);
1231
1232         regulator_bulk_free(desc->num_charger_regulators,
1233                             desc->charger_regulators);
1234         power_supply_unregister(&cm->charger_psy);
1235
1236         try_charger_enable(cm, false);
1237
1238         kfree(cm->charger_psy.properties);
1239         kfree(cm->charger_stat);
1240         kfree(cm->desc);
1241         kfree(cm);
1242
1243         return 0;
1244 }
1245
1246 static const struct platform_device_id charger_manager_id[] = {
1247         { "charger-manager", 0 },
1248         { },
1249 };
1250 MODULE_DEVICE_TABLE(platform, charger_manager_id);
1251
1252 static int cm_suspend_noirq(struct device *dev)
1253 {
1254         int ret = 0;
1255
1256         if (device_may_wakeup(dev)) {
1257                 device_set_wakeup_capable(dev, false);
1258                 ret = -EAGAIN;
1259         }
1260
1261         return ret;
1262 }
1263
1264 static int cm_suspend_prepare(struct device *dev)
1265 {
1266         struct charger_manager *cm = dev_get_drvdata(dev);
1267
1268         if (!cm_suspended) {
1269                 if (rtc_dev) {
1270                         struct rtc_time tmp;
1271                         unsigned long now;
1272
1273                         rtc_read_alarm(rtc_dev, &rtc_wkalarm_save);
1274                         rtc_read_time(rtc_dev, &tmp);
1275
1276                         if (rtc_wkalarm_save.enabled) {
1277                                 rtc_tm_to_time(&rtc_wkalarm_save.time,
1278                                                &rtc_wkalarm_save_time);
1279                                 rtc_tm_to_time(&tmp, &now);
1280                                 if (now > rtc_wkalarm_save_time)
1281                                         rtc_wkalarm_save_time = 0;
1282                         } else {
1283                                 rtc_wkalarm_save_time = 0;
1284                         }
1285                 }
1286                 cm_suspended = true;
1287         }
1288
1289         if (delayed_work_pending(&cm->fullbatt_vchk_work))
1290                 cancel_delayed_work(&cm->fullbatt_vchk_work);
1291         cm->status_save_ext_pwr_inserted = is_ext_pwr_online(cm);
1292         cm->status_save_batt = is_batt_present(cm);
1293
1294         if (!cm_rtc_set) {
1295                 cm_suspend_duration_ms = 0;
1296                 cm_rtc_set = cm_setup_timer();
1297         }
1298
1299         return 0;
1300 }
1301
1302 static void cm_suspend_complete(struct device *dev)
1303 {
1304         struct charger_manager *cm = dev_get_drvdata(dev);
1305
1306         if (cm_suspended) {
1307                 if (rtc_dev) {
1308                         struct rtc_wkalrm tmp;
1309
1310                         rtc_read_alarm(rtc_dev, &tmp);
1311                         rtc_wkalarm_save.pending = tmp.pending;
1312                         rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1313                 }
1314                 cm_suspended = false;
1315                 cm_rtc_set = false;
1316         }
1317
1318         /* Re-enqueue delayed work (fullbatt_vchk_work) */
1319         if (cm->fullbatt_vchk_jiffies_at) {
1320                 unsigned long delay = 0;
1321                 unsigned long now = jiffies + CM_JIFFIES_SMALL;
1322
1323                 if (time_after_eq(now, cm->fullbatt_vchk_jiffies_at)) {
1324                         delay = (unsigned long)((long)now
1325                                 - (long)(cm->fullbatt_vchk_jiffies_at));
1326                         delay = jiffies_to_msecs(delay);
1327                 } else {
1328                         delay = 0;
1329                 }
1330
1331                 /*
1332                  * Account for cm_suspend_duration_ms if
1333                  * assume_timer_stops_in_suspend is active
1334                  */
1335                 if (g_desc && g_desc->assume_timer_stops_in_suspend) {
1336                         if (delay > cm_suspend_duration_ms)
1337                                 delay -= cm_suspend_duration_ms;
1338                         else
1339                                 delay = 0;
1340                 }
1341
1342                 queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
1343                                    msecs_to_jiffies(delay));
1344         }
1345         device_set_wakeup_capable(cm->dev, false);
1346         uevent_notify(cm, NULL);
1347 }
1348
1349 static const struct dev_pm_ops charger_manager_pm = {
1350         .prepare        = cm_suspend_prepare,
1351         .suspend_noirq  = cm_suspend_noirq,
1352         .complete       = cm_suspend_complete,
1353 };
1354
1355 static struct platform_driver charger_manager_driver = {
1356         .driver = {
1357                 .name = "charger-manager",
1358                 .owner = THIS_MODULE,
1359                 .pm = &charger_manager_pm,
1360         },
1361         .probe = charger_manager_probe,
1362         .remove = __devexit_p(charger_manager_remove),
1363         .id_table = charger_manager_id,
1364 };
1365
1366 static int __init charger_manager_init(void)
1367 {
1368         cm_wq = create_freezable_workqueue("charger_manager");
1369         INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller);
1370
1371         return platform_driver_register(&charger_manager_driver);
1372 }
1373 late_initcall(charger_manager_init);
1374
1375 static void __exit charger_manager_cleanup(void)
1376 {
1377         destroy_workqueue(cm_wq);
1378         cm_wq = NULL;
1379
1380         platform_driver_unregister(&charger_manager_driver);
1381 }
1382 module_exit(charger_manager_cleanup);
1383
1384 /**
1385  * find_power_supply - find the associated power_supply of charger
1386  * @cm: the Charger Manager representing the battery
1387  * @psy: pointer to instance of charger's power_supply
1388  */
1389 static bool find_power_supply(struct charger_manager *cm,
1390                         struct power_supply *psy)
1391 {
1392         int i;
1393         bool found = false;
1394
1395         for (i = 0; cm->charger_stat[i]; i++) {
1396                 if (psy == cm->charger_stat[i]) {
1397                         found = true;
1398                         break;
1399                 }
1400         }
1401
1402         return found;
1403 }
1404
1405 /**
1406  * cm_notify_event - charger driver notify Charger Manager of charger event
1407  * @psy: pointer to instance of charger's power_supply
1408  * @type: type of charger event
1409  * @msg: optional message passed to uevent_notify fuction
1410  */
1411 void cm_notify_event(struct power_supply *psy, enum cm_event_types type,
1412                      char *msg)
1413 {
1414         struct charger_manager *cm;
1415         bool found_power_supply = false;
1416
1417         if (psy == NULL)
1418                 return;
1419
1420         mutex_lock(&cm_list_mtx);
1421         list_for_each_entry(cm, &cm_list, entry) {
1422                 found_power_supply = find_power_supply(cm, psy);
1423                 if (found_power_supply)
1424                         break;
1425         }
1426         mutex_unlock(&cm_list_mtx);
1427
1428         if (!found_power_supply)
1429                 return;
1430
1431         switch (type) {
1432         case CM_EVENT_BATT_FULL:
1433                 fullbatt_handler(cm);
1434                 break;
1435         case CM_EVENT_BATT_OUT:
1436                 battout_handler(cm);
1437                 break;
1438         case CM_EVENT_BATT_IN:
1439         case CM_EVENT_EXT_PWR_IN_OUT ... CM_EVENT_CHG_START_STOP:
1440                 misc_event_handler(cm, type);
1441                 break;
1442         case CM_EVENT_UNKNOWN:
1443         case CM_EVENT_OTHERS:
1444                 uevent_notify(cm, msg ? msg : default_event_names[type]);
1445                 break;
1446         default:
1447                 dev_err(cm->dev, "%s type not specified.\n", __func__);
1448                 break;
1449         }
1450 }
1451 EXPORT_SYMBOL_GPL(cm_notify_event);
1452
1453 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1454 MODULE_DESCRIPTION("Charger Manager");
1455 MODULE_LICENSE("GPL");