Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
charger-manager.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 Samsung Electronics Co., Ltd.
3  * MyungJoo Ham <[email protected]>
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>
25 #include <linux/sysfs.h>
26 
27 static const char * const default_event_names[] = {
28  [CM_EVENT_UNKNOWN] = "Unknown",
29  [CM_EVENT_BATT_FULL] = "Battery Full",
30  [CM_EVENT_BATT_IN] = "Battery Inserted",
31  [CM_EVENT_BATT_OUT] = "Battery Pulled Out",
32  [CM_EVENT_EXT_PWR_IN_OUT] = "External Power Attach/Detach",
33  [CM_EVENT_CHG_START_STOP] = "Charging Start/Stop",
34  [CM_EVENT_OTHERS] = "Other battery events"
35 };
36 
37 /*
38  * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for
39  * delayed works so that we can run delayed works with CM_JIFFIES_SMALL
40  * without any delays.
41  */
42 #define CM_JIFFIES_SMALL (2)
43 
44 /* If y is valid (> 0) and smaller than x, do x = y */
45 #define CM_MIN_VALID(x, y) x = (((y > 0) && ((x) > (y))) ? (y) : (x))
46 
47 /*
48  * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking
49  * rtc alarm. It should be 2 or larger
50  */
51 #define CM_RTC_SMALL (2)
52 
53 #define UEVENT_BUF_SIZE 32
54 
55 static LIST_HEAD(cm_list);
56 static DEFINE_MUTEX(cm_list_mtx);
57 
58 /* About in-suspend (suspend-again) monitoring */
59 static struct rtc_device *rtc_dev;
60 /*
61  * Backup RTC alarm
62  * Save the wakeup alarm before entering suspend-to-RAM
63  */
64 static struct rtc_wkalrm rtc_wkalarm_save;
65 /* Backup RTC alarm time in terms of seconds since 01-01-1970 00:00:00 */
66 static unsigned long rtc_wkalarm_save_time;
67 static bool cm_suspended;
68 static bool cm_rtc_set;
69 static unsigned long cm_suspend_duration_ms;
70 
71 /* About normal (not suspended) monitoring */
72 static unsigned long polling_jiffy = ULONG_MAX; /* ULONG_MAX: no polling */
73 static unsigned long next_polling; /* Next appointed polling time */
74 static struct workqueue_struct *cm_wq; /* init at driver add */
75 static struct delayed_work cm_monitor_work; /* init at driver add */
76 
77 /* Global charger-manager description */
78 static struct charger_global_desc *g_desc; /* init with setup_charger_manager */
79 
84 static bool is_batt_present(struct charger_manager *cm)
85 {
87  bool present = false;
88  int i, ret;
89 
90  switch (cm->desc->battery_present) {
91  case CM_BATTERY_PRESENT:
92  present = true;
93  break;
94  case CM_NO_BATTERY:
95  break;
96  case CM_FUEL_GAUGE:
97  ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
99  if (ret == 0 && val.intval)
100  present = true;
101  break;
102  case CM_CHARGER_STAT:
103  for (i = 0; cm->charger_stat[i]; i++) {
104  ret = cm->charger_stat[i]->get_property(
105  cm->charger_stat[i],
107  if (ret == 0 && val.intval) {
108  present = true;
109  break;
110  }
111  }
112  break;
113  }
114 
115  return present;
116 }
117 
126 static bool is_ext_pwr_online(struct charger_manager *cm)
127 {
129  bool online = false;
130  int i, ret;
131 
132  /* If at least one of them has one, it's yes. */
133  for (i = 0; cm->charger_stat[i]; i++) {
134  ret = cm->charger_stat[i]->get_property(
135  cm->charger_stat[i],
137  if (ret == 0 && val.intval) {
138  online = true;
139  break;
140  }
141  }
142 
143  return online;
144 }
145 
154 static int get_batt_uV(struct charger_manager *cm, int *uV)
155 {
157  int ret;
158 
159  if (!cm->fuel_gauge)
160  return -ENODEV;
161 
162  ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
164  if (ret)
165  return ret;
166 
167  *uV = val.intval;
168  return 0;
169 }
170 
175 static bool is_charging(struct charger_manager *cm)
176 {
177  int i, ret;
178  bool charging = false;
180 
181  /* If there is no battery, it cannot be charged */
182  if (!is_batt_present(cm))
183  return false;
184 
185  /* If at least one of the charger is charging, return yes */
186  for (i = 0; cm->charger_stat[i]; i++) {
187  /* 1. The charger sholuld not be DISABLED */
188  if (cm->emergency_stop)
189  continue;
190  if (!cm->charger_enabled)
191  continue;
192 
193  /* 2. The charger should be online (ext-power) */
194  ret = cm->charger_stat[i]->get_property(
195  cm->charger_stat[i],
197  if (ret) {
198  dev_warn(cm->dev, "Cannot read ONLINE value from %s.\n",
199  cm->desc->psy_charger_stat[i]);
200  continue;
201  }
202  if (val.intval == 0)
203  continue;
204 
205  /*
206  * 3. The charger should not be FULL, DISCHARGING,
207  * or NOT_CHARGING.
208  */
209  ret = cm->charger_stat[i]->get_property(
210  cm->charger_stat[i],
212  if (ret) {
213  dev_warn(cm->dev, "Cannot read STATUS value from %s.\n",
214  cm->desc->psy_charger_stat[i]);
215  continue;
216  }
217  if (val.intval == POWER_SUPPLY_STATUS_FULL ||
220  continue;
221 
222  /* Then, this is charging. */
223  charging = true;
224  break;
225  }
226 
227  return charging;
228 }
229 
234 static bool is_full_charged(struct charger_manager *cm)
235 {
236  struct charger_desc *desc = cm->desc;
238  int ret = 0;
239  int uV;
240 
241  /* If there is no battery, it cannot be charged */
242  if (!is_batt_present(cm)) {
243  val.intval = 0;
244  goto out;
245  }
246 
247  if (cm->fuel_gauge && desc->fullbatt_full_capacity > 0) {
248  /* Not full if capacity of fuel gauge isn't full */
249  ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
251  if (!ret && val.intval > desc->fullbatt_full_capacity) {
252  val.intval = 1;
253  goto out;
254  }
255  }
256 
257  /* Full, if it's over the fullbatt voltage */
258  if (desc->fullbatt_uV > 0) {
259  ret = get_batt_uV(cm, &uV);
260  if (!ret && uV >= desc->fullbatt_uV) {
261  val.intval = 1;
262  goto out;
263  }
264  }
265 
266  /* Full, if the capacity is more than fullbatt_soc */
267  if (cm->fuel_gauge && desc->fullbatt_soc > 0) {
268  ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
270  if (!ret && val.intval >= desc->fullbatt_soc) {
271  val.intval = 1;
272  goto out;
273  }
274  }
275 
276  val.intval = 0;
277 
278 out:
279  return val.intval ? true : false;
280 }
281 
286 static bool is_polling_required(struct charger_manager *cm)
287 {
288  switch (cm->desc->polling_mode) {
289  case CM_POLL_DISABLE:
290  return false;
291  case CM_POLL_ALWAYS:
292  return true;
294  return is_ext_pwr_online(cm);
296  return is_charging(cm);
297  default:
298  dev_warn(cm->dev, "Incorrect polling_mode (%d)\n",
299  cm->desc->polling_mode);
300  }
301 
302  return false;
303 }
304 
315 static int try_charger_enable(struct charger_manager *cm, bool enable)
316 {
317  int err = 0, i;
318  struct charger_desc *desc = cm->desc;
319 
320  /* Ignore if it's redundent command */
321  if (enable == cm->charger_enabled)
322  return 0;
323 
324  if (enable) {
325  if (cm->emergency_stop)
326  return -EAGAIN;
327 
328  /*
329  * Save start time of charging to limit
330  * maximum possible charging time.
331  */
332  cm->charging_start_time = ktime_to_ms(ktime_get());
333  cm->charging_end_time = 0;
334 
335  for (i = 0 ; i < desc->num_charger_regulators ; i++) {
336  if (desc->charger_regulators[i].externally_control)
337  continue;
338 
339  err = regulator_enable(desc->charger_regulators[i].consumer);
340  if (err < 0) {
341  dev_warn(cm->dev,
342  "Cannot enable %s regulator\n",
343  desc->charger_regulators[i].regulator_name);
344  }
345  }
346  } else {
347  /*
348  * Save end time of charging to maintain fully charged state
349  * of battery after full-batt.
350  */
351  cm->charging_start_time = 0;
352  cm->charging_end_time = ktime_to_ms(ktime_get());
353 
354  for (i = 0 ; i < desc->num_charger_regulators ; i++) {
355  if (desc->charger_regulators[i].externally_control)
356  continue;
357 
358  err = regulator_disable(desc->charger_regulators[i].consumer);
359  if (err < 0) {
360  dev_warn(cm->dev,
361  "Cannot disable %s regulator\n",
362  desc->charger_regulators[i].regulator_name);
363  }
364  }
365 
366  /*
367  * Abnormal battery state - Stop charging forcibly,
368  * even if charger was enabled at the other places
369  */
370  for (i = 0; i < desc->num_charger_regulators; i++) {
372  desc->charger_regulators[i].consumer)) {
374  desc->charger_regulators[i].consumer);
375  dev_warn(cm->dev,
376  "Disable regulator(%s) forcibly.\n",
377  desc->charger_regulators[i].regulator_name);
378  }
379  }
380  }
381 
382  if (!err)
383  cm->charger_enabled = enable;
384 
385  return err;
386 }
387 
394 static int try_charger_restart(struct charger_manager *cm)
395 {
396  int err;
397 
398  if (cm->emergency_stop)
399  return -EAGAIN;
400 
401  err = try_charger_enable(cm, false);
402  if (err)
403  return err;
404 
405  return try_charger_enable(cm, true);
406 }
407 
420 static void uevent_notify(struct charger_manager *cm, const char *event)
421 {
422  static char env_str[UEVENT_BUF_SIZE + 1] = "";
423  static char env_str_save[UEVENT_BUF_SIZE + 1] = "";
424 
425  if (cm_suspended) {
426  /* Nothing in suspended-event buffer */
427  if (env_str_save[0] == 0) {
428  if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
429  return; /* status not changed */
430  strncpy(env_str_save, event, UEVENT_BUF_SIZE);
431  return;
432  }
433 
434  if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE))
435  return; /* Duplicated. */
436  strncpy(env_str_save, event, UEVENT_BUF_SIZE);
437  return;
438  }
439 
440  if (event == NULL) {
441  /* No messages pending */
442  if (!env_str_save[0])
443  return;
444 
445  strncpy(env_str, env_str_save, UEVENT_BUF_SIZE);
446  kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
447  env_str_save[0] = 0;
448 
449  return;
450  }
451 
452  /* status not changed */
453  if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
454  return;
455 
456  /* save the status and notify the update */
457  strncpy(env_str, event, UEVENT_BUF_SIZE);
458  kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
459 
460  dev_info(cm->dev, event);
461 }
462 
472 static void fullbatt_vchk(struct work_struct *work)
473 {
474  struct delayed_work *dwork = to_delayed_work(work);
475  struct charger_manager *cm = container_of(dwork,
477  struct charger_desc *desc = cm->desc;
478  int batt_uV, err, diff;
479 
480  /* remove the appointment for fullbatt_vchk */
481  cm->fullbatt_vchk_jiffies_at = 0;
482 
483  if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
484  return;
485 
486  err = get_batt_uV(cm, &batt_uV);
487  if (err) {
488  dev_err(cm->dev, "%s: get_batt_uV error(%d).\n", __func__, err);
489  return;
490  }
491 
492  diff = desc->fullbatt_uV;
493  diff -= batt_uV;
494 
495  dev_info(cm->dev, "VBATT dropped %duV after full-batt.\n", diff);
496 
497  if (diff > desc->fullbatt_vchkdrop_uV) {
498  try_charger_restart(cm);
499  uevent_notify(cm, "Recharging");
500  }
501 }
502 
513 static int check_charging_duration(struct charger_manager *cm)
514 {
515  struct charger_desc *desc = cm->desc;
516  u64 curr = ktime_to_ms(ktime_get());
517  u64 duration;
518  int ret = false;
519 
520  if (!desc->charging_max_duration_ms &&
522  return ret;
523 
524  if (cm->charger_enabled) {
525  duration = curr - cm->charging_start_time;
526 
527  if (duration > desc->charging_max_duration_ms) {
528  dev_info(cm->dev, "Charging duration exceed %lldms",
530  uevent_notify(cm, "Discharging");
531  try_charger_enable(cm, false);
532  ret = true;
533  }
534  } else if (is_ext_pwr_online(cm) && !cm->charger_enabled) {
535  duration = curr - cm->charging_end_time;
536 
537  if (duration > desc->charging_max_duration_ms &&
538  is_ext_pwr_online(cm)) {
539  dev_info(cm->dev, "DisCharging duration exceed %lldms",
541  uevent_notify(cm, "Recharing");
542  try_charger_enable(cm, true);
543  ret = true;
544  }
545  }
546 
547  return ret;
548 }
549 
557 static bool _cm_monitor(struct charger_manager *cm)
558 {
559  struct charger_desc *desc = cm->desc;
560  int temp = desc->temperature_out_of_range(&cm->last_temp_mC);
561 
562  dev_dbg(cm->dev, "monitoring (%2.2d.%3.3dC)\n",
563  cm->last_temp_mC / 1000, cm->last_temp_mC % 1000);
564 
565  /* It has been stopped already */
566  if (temp && cm->emergency_stop)
567  return false;
568 
569  /*
570  * Check temperature whether overheat or cold.
571  * If temperature is out of range normal state, stop charging.
572  */
573  if (temp) {
574  cm->emergency_stop = temp;
575  if (!try_charger_enable(cm, false)) {
576  if (temp > 0)
577  uevent_notify(cm, "OVERHEAT");
578  else
579  uevent_notify(cm, "COLD");
580  }
581 
582  /*
583  * Check whole charging duration and discharing duration
584  * after full-batt.
585  */
586  } else if (!cm->emergency_stop && check_charging_duration(cm)) {
587  dev_dbg(cm->dev,
588  "Charging/Discharging duration is out of range");
589  /*
590  * Check dropped voltage of battery. If battery voltage is more
591  * dropped than fullbatt_vchkdrop_uV after fully charged state,
592  * charger-manager have to recharge battery.
593  */
594  } else if (!cm->emergency_stop && is_ext_pwr_online(cm) &&
595  !cm->charger_enabled) {
596  fullbatt_vchk(&cm->fullbatt_vchk_work.work);
597 
598  /*
599  * Check whether fully charged state to protect overcharge
600  * if charger-manager is charging for battery.
601  */
602  } else if (!cm->emergency_stop && is_full_charged(cm) &&
603  cm->charger_enabled) {
604  dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged.\n");
605  uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
606 
607  try_charger_enable(cm, false);
608 
609  fullbatt_vchk(&cm->fullbatt_vchk_work.work);
610  } else {
611  cm->emergency_stop = 0;
612  if (is_ext_pwr_online(cm)) {
613  if (!try_charger_enable(cm, true))
614  uevent_notify(cm, "CHARGING");
615  }
616  }
617 
618  return true;
619 }
620 
627 static bool cm_monitor(void)
628 {
629  bool stop = false;
630  struct charger_manager *cm;
631 
632  mutex_lock(&cm_list_mtx);
633 
634  list_for_each_entry(cm, &cm_list, entry) {
635  if (_cm_monitor(cm))
636  stop = true;
637  }
638 
639  mutex_unlock(&cm_list_mtx);
640 
641  return stop;
642 }
643 
648 static void _setup_polling(struct work_struct *work)
649 {
650  unsigned long min = ULONG_MAX;
651  struct charger_manager *cm;
652  bool keep_polling = false;
653  unsigned long _next_polling;
654 
655  mutex_lock(&cm_list_mtx);
656 
657  list_for_each_entry(cm, &cm_list, entry) {
658  if (is_polling_required(cm) && cm->desc->polling_interval_ms) {
659  keep_polling = true;
660 
661  if (min > cm->desc->polling_interval_ms)
662  min = cm->desc->polling_interval_ms;
663  }
664  }
665 
666  polling_jiffy = msecs_to_jiffies(min);
667  if (polling_jiffy <= CM_JIFFIES_SMALL)
668  polling_jiffy = CM_JIFFIES_SMALL + 1;
669 
670  if (!keep_polling)
671  polling_jiffy = ULONG_MAX;
672  if (polling_jiffy == ULONG_MAX)
673  goto out;
674 
675  WARN(cm_wq == NULL, "charger-manager: workqueue not initialized"
676  ". try it later. %s\n", __func__);
677 
678  _next_polling = jiffies + polling_jiffy;
679 
680  if (!delayed_work_pending(&cm_monitor_work) ||
681  (delayed_work_pending(&cm_monitor_work) &&
682  time_after(next_polling, _next_polling))) {
683  next_polling = jiffies + polling_jiffy;
684  mod_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy);
685  }
686 
687 out:
688  mutex_unlock(&cm_list_mtx);
689 }
690 static DECLARE_WORK(setup_polling, _setup_polling);
691 
699 static void cm_monitor_poller(struct work_struct *work)
700 {
701  cm_monitor();
702  schedule_work(&setup_polling);
703 }
704 
709 static void fullbatt_handler(struct charger_manager *cm)
710 {
711  struct charger_desc *desc = cm->desc;
712 
713  if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
714  goto out;
715 
716  if (cm_suspended)
717  device_set_wakeup_capable(cm->dev, true);
718 
722  desc->fullbatt_vchkdrop_ms);
723 
724  if (cm->fullbatt_vchk_jiffies_at == 0)
725  cm->fullbatt_vchk_jiffies_at = 1;
726 
727 out:
728  dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged.\n");
729  uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
730 }
731 
736 static void battout_handler(struct charger_manager *cm)
737 {
738  if (cm_suspended)
739  device_set_wakeup_capable(cm->dev, true);
740 
741  if (!is_batt_present(cm)) {
742  dev_emerg(cm->dev, "Battery Pulled Out!\n");
743  uevent_notify(cm, default_event_names[CM_EVENT_BATT_OUT]);
744  } else {
745  uevent_notify(cm, "Battery Reinserted?");
746  }
747 }
748 
754 static void misc_event_handler(struct charger_manager *cm,
755  enum cm_event_types type)
756 {
757  if (cm_suspended)
758  device_set_wakeup_capable(cm->dev, true);
759 
760  if (!delayed_work_pending(&cm_monitor_work) &&
761  is_polling_required(cm) && cm->desc->polling_interval_ms)
762  schedule_work(&setup_polling);
763  uevent_notify(cm, default_event_names[type]);
764 }
765 
766 static int charger_get_property(struct power_supply *psy,
767  enum power_supply_property psp,
768  union power_supply_propval *val)
769 {
770  struct charger_manager *cm = container_of(psy,
771  struct charger_manager, charger_psy);
772  struct charger_desc *desc = cm->desc;
773  int ret = 0;
774  int uV;
775 
776  switch (psp) {
778  if (is_charging(cm))
780  else if (is_ext_pwr_online(cm))
782  else
784  break;
786  if (cm->emergency_stop > 0)
788  else if (cm->emergency_stop < 0)
790  else
792  break;
794  if (is_batt_present(cm))
795  val->intval = 1;
796  else
797  val->intval = 0;
798  break;
800  ret = get_batt_uV(cm, &val->intval);
801  break;
803  ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
805  break;
807  /* in thenth of centigrade */
808  if (cm->last_temp_mC == INT_MIN)
810  val->intval = cm->last_temp_mC / 100;
811  if (!desc->measure_battery_temp)
812  ret = -ENODEV;
813  break;
815  /* in thenth of centigrade */
816  if (cm->last_temp_mC == INT_MIN)
818  val->intval = cm->last_temp_mC / 100;
819  if (desc->measure_battery_temp)
820  ret = -ENODEV;
821  break;
823  if (!cm->fuel_gauge) {
824  ret = -ENODEV;
825  break;
826  }
827 
828  if (!is_batt_present(cm)) {
829  /* There is no battery. Assume 100% */
830  val->intval = 100;
831  break;
832  }
833 
834  ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
836  if (ret)
837  break;
838 
839  if (val->intval > 100) {
840  val->intval = 100;
841  break;
842  }
843  if (val->intval < 0)
844  val->intval = 0;
845 
846  /* Do not adjust SOC when charging: voltage is overrated */
847  if (is_charging(cm))
848  break;
849 
850  /*
851  * If the capacity value is inconsistent, calibrate it base on
852  * the battery voltage values and the thresholds given as desc
853  */
854  ret = get_batt_uV(cm, &uV);
855  if (ret) {
856  /* Voltage information not available. No calibration */
857  ret = 0;
858  break;
859  }
860 
861  if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
862  !is_charging(cm)) {
863  val->intval = 100;
864  break;
865  }
866 
867  break;
869  if (is_ext_pwr_online(cm))
870  val->intval = 1;
871  else
872  val->intval = 0;
873  break;
875  if (is_full_charged(cm))
876  val->intval = 1;
877  else
878  val->intval = 0;
879  ret = 0;
880  break;
882  if (is_charging(cm)) {
883  ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
885  val);
886  if (ret) {
887  val->intval = 1;
888  ret = 0;
889  } else {
890  /* If CHARGE_NOW is supplied, use it */
891  val->intval = (val->intval > 0) ?
892  val->intval : 1;
893  }
894  } else {
895  val->intval = 0;
896  }
897  break;
898  default:
899  return -EINVAL;
900  }
901  return ret;
902 }
903 
904 #define NUM_CHARGER_PSY_OPTIONAL (4)
905 static enum power_supply_property default_charger_props[] = {
906  /* Guaranteed to provide */
914  /*
915  * Optional properties are:
916  * POWER_SUPPLY_PROP_CHARGE_NOW,
917  * POWER_SUPPLY_PROP_CURRENT_NOW,
918  * POWER_SUPPLY_PROP_TEMP, and
919  * POWER_SUPPLY_PROP_TEMP_AMBIENT,
920  */
921 };
922 
923 static struct power_supply psy_default = {
924  .name = "battery",
926  .properties = default_charger_props,
927  .num_properties = ARRAY_SIZE(default_charger_props),
928  .get_property = charger_get_property,
929 };
930 
941 static bool cm_setup_timer(void)
942 {
943  struct charger_manager *cm;
944  unsigned int wakeup_ms = UINT_MAX;
945  bool ret = false;
946 
947  mutex_lock(&cm_list_mtx);
948 
949  list_for_each_entry(cm, &cm_list, entry) {
950  unsigned int fbchk_ms = 0;
951 
952  /* fullbatt_vchk is required. setup timer for that */
953  if (cm->fullbatt_vchk_jiffies_at) {
955  - jiffies);
958  msecs_to_jiffies(fbchk_ms) < CM_JIFFIES_SMALL) {
959  fullbatt_vchk(&cm->fullbatt_vchk_work.work);
960  fbchk_ms = 0;
961  }
962  }
963  CM_MIN_VALID(wakeup_ms, fbchk_ms);
964 
965  /* Skip if polling is not required for this CM */
966  if (!is_polling_required(cm) && !cm->emergency_stop)
967  continue;
968  if (cm->desc->polling_interval_ms == 0)
969  continue;
970  CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
971  }
972 
973  mutex_unlock(&cm_list_mtx);
974 
975  if (wakeup_ms < UINT_MAX && wakeup_ms > 0) {
976  pr_info("Charger Manager wakeup timer: %u ms.\n", wakeup_ms);
977  if (rtc_dev) {
978  struct rtc_wkalrm tmp;
979  unsigned long time, now;
980  unsigned long add = DIV_ROUND_UP(wakeup_ms, 1000);
981 
982  /*
983  * Set alarm with the polling interval (wakeup_ms)
984  * except when rtc_wkalarm_save comes first.
985  * However, the alarm time should be NOW +
986  * CM_RTC_SMALL or later.
987  */
988  tmp.enabled = 1;
989  rtc_read_time(rtc_dev, &tmp.time);
990  rtc_tm_to_time(&tmp.time, &now);
991  if (add < CM_RTC_SMALL)
992  add = CM_RTC_SMALL;
993  time = now + add;
994 
995  ret = true;
996 
997  if (rtc_wkalarm_save.enabled &&
998  rtc_wkalarm_save_time &&
999  rtc_wkalarm_save_time < time) {
1000  if (rtc_wkalarm_save_time < now + CM_RTC_SMALL)
1001  time = now + CM_RTC_SMALL;
1002  else
1003  time = rtc_wkalarm_save_time;
1004 
1005  /* The timer is not appointed by CM */
1006  ret = false;
1007  }
1008 
1009  pr_info("Waking up after %lu secs.\n",
1010  time - now);
1011 
1012  rtc_time_to_tm(time, &tmp.time);
1013  rtc_set_alarm(rtc_dev, &tmp);
1014  cm_suspend_duration_ms += wakeup_ms;
1015  return ret;
1016  }
1017  }
1018 
1019  if (rtc_dev)
1020  rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1021  return false;
1022 }
1023 
1024 static void _cm_fbchk_in_suspend(struct charger_manager *cm)
1025 {
1026  unsigned long jiffy_now = jiffies;
1027 
1028  if (!cm->fullbatt_vchk_jiffies_at)
1029  return;
1030 
1031  if (g_desc && g_desc->assume_timer_stops_in_suspend)
1032  jiffy_now += msecs_to_jiffies(cm_suspend_duration_ms);
1033 
1034  /* Execute now if it's going to be executed not too long after */
1035  jiffy_now += CM_JIFFIES_SMALL;
1036 
1037  if (time_after_eq(jiffy_now, cm->fullbatt_vchk_jiffies_at))
1038  fullbatt_vchk(&cm->fullbatt_vchk_work.work);
1039 }
1040 
1048 {
1049  struct charger_manager *cm;
1050  bool ret = false;
1051 
1052  if (!g_desc || !g_desc->rtc_only_wakeup || !g_desc->rtc_only_wakeup() ||
1053  !cm_rtc_set)
1054  return false;
1055 
1056  if (cm_monitor())
1057  goto out;
1058 
1059  ret = true;
1060  mutex_lock(&cm_list_mtx);
1061  list_for_each_entry(cm, &cm_list, entry) {
1062  _cm_fbchk_in_suspend(cm);
1063 
1064  if (cm->status_save_ext_pwr_inserted != is_ext_pwr_online(cm) ||
1065  cm->status_save_batt != is_batt_present(cm)) {
1066  ret = false;
1067  break;
1068  }
1069  }
1070  mutex_unlock(&cm_list_mtx);
1071 
1072  cm_rtc_set = cm_setup_timer();
1073 out:
1074  /* It's about the time when the non-CM appointed timer goes off */
1075  if (rtc_wkalarm_save.enabled) {
1076  unsigned long now;
1077  struct rtc_time tmp;
1078 
1079  rtc_read_time(rtc_dev, &tmp);
1080  rtc_tm_to_time(&tmp, &now);
1081 
1082  if (rtc_wkalarm_save_time &&
1083  now + CM_RTC_SMALL >= rtc_wkalarm_save_time)
1084  return false;
1085  }
1086  return ret;
1087 }
1089 
1095 {
1096  if (!gd)
1097  return -EINVAL;
1098 
1099  if (rtc_dev)
1100  rtc_class_close(rtc_dev);
1101  rtc_dev = NULL;
1102  g_desc = NULL;
1103 
1104  if (!gd->rtc_only_wakeup) {
1105  pr_err("The callback rtc_only_wakeup is not given.\n");
1106  return -EINVAL;
1107  }
1108 
1109  if (gd->rtc_name) {
1110  rtc_dev = rtc_class_open(gd->rtc_name);
1111  if (IS_ERR_OR_NULL(rtc_dev)) {
1112  rtc_dev = NULL;
1113  /* Retry at probe. RTC may be not registered yet */
1114  }
1115  } else {
1116  pr_warn("No wakeup timer is given for charger manager."
1117  "In-suspend monitoring won't work.\n");
1118  }
1119 
1120  g_desc = gd;
1121  return 0;
1122 }
1124 
1131 static void charger_extcon_work(struct work_struct *work)
1132 {
1133  struct charger_cable *cable =
1134  container_of(work, struct charger_cable, wq);
1135  int ret;
1136 
1137  if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) {
1138  ret = regulator_set_current_limit(cable->charger->consumer,
1139  cable->min_uA, cable->max_uA);
1140  if (ret < 0) {
1141  pr_err("Cannot set current limit of %s (%s)\n",
1142  cable->charger->regulator_name, cable->name);
1143  return;
1144  }
1145 
1146  pr_info("Set current limit of %s : %duA ~ %duA\n",
1147  cable->charger->regulator_name,
1148  cable->min_uA, cable->max_uA);
1149  }
1150 
1151  try_charger_enable(cable->cm, cable->attached);
1152 }
1153 
1162 static int charger_extcon_notifier(struct notifier_block *self,
1163  unsigned long event, void *ptr)
1164 {
1165  struct charger_cable *cable =
1166  container_of(self, struct charger_cable, nb);
1167 
1168  /*
1169  * The newly state of charger cable.
1170  * If cable is attached, cable->attached is true.
1171  */
1172  cable->attached = event;
1173 
1174  /*
1175  * Setup monitoring to check battery state
1176  * when charger cable is attached.
1177  */
1178  if (cable->attached && is_polling_required(cable->cm)) {
1179  if (work_pending(&setup_polling))
1180  cancel_work_sync(&setup_polling);
1181  schedule_work(&setup_polling);
1182  }
1183 
1184  /*
1185  * Setup work for controlling charger(regulator)
1186  * according to charger cable.
1187  */
1188  schedule_work(&cable->wq);
1189 
1190  return NOTIFY_DONE;
1191 }
1192 
1200 static int charger_extcon_init(struct charger_manager *cm,
1201  struct charger_cable *cable)
1202 {
1203  int ret = 0;
1204 
1205  /*
1206  * Charger manager use Extcon framework to identify
1207  * the charger cable among various external connector
1208  * cable (e.g., TA, USB, MHL, Dock).
1209  */
1210  INIT_WORK(&cable->wq, charger_extcon_work);
1211  cable->nb.notifier_call = charger_extcon_notifier;
1212  ret = extcon_register_interest(&cable->extcon_dev,
1213  cable->extcon_name, cable->name, &cable->nb);
1214  if (ret < 0) {
1215  pr_info("Cannot register extcon_dev for %s(cable: %s).\n",
1216  cable->extcon_name,
1217  cable->name);
1218  ret = -EINVAL;
1219  }
1220 
1221  return ret;
1222 }
1223 
1224 /* help function of sysfs node to control charger(regulator) */
1225 static ssize_t charger_name_show(struct device *dev,
1226  struct device_attribute *attr, char *buf)
1227 {
1228  struct charger_regulator *charger
1229  = container_of(attr, struct charger_regulator, attr_name);
1230 
1231  return sprintf(buf, "%s\n", charger->regulator_name);
1232 }
1233 
1234 static ssize_t charger_state_show(struct device *dev,
1235  struct device_attribute *attr, char *buf)
1236 {
1237  struct charger_regulator *charger
1238  = container_of(attr, struct charger_regulator, attr_state);
1239  int state = 0;
1240 
1241  if (!charger->externally_control)
1242  state = regulator_is_enabled(charger->consumer);
1243 
1244  return sprintf(buf, "%s\n", state ? "enabled" : "disabled");
1245 }
1246 
1247 static ssize_t charger_externally_control_show(struct device *dev,
1248  struct device_attribute *attr, char *buf)
1249 {
1250  struct charger_regulator *charger = container_of(attr,
1252 
1253  return sprintf(buf, "%d\n", charger->externally_control);
1254 }
1255 
1256 static ssize_t charger_externally_control_store(struct device *dev,
1257  struct device_attribute *attr, const char *buf,
1258  size_t count)
1259 {
1260  struct charger_regulator *charger
1261  = container_of(attr, struct charger_regulator,
1263  struct charger_manager *cm = charger->cm;
1264  struct charger_desc *desc = cm->desc;
1265  int i;
1266  int ret;
1267  int externally_control;
1268  int chargers_externally_control = 1;
1269 
1270  ret = sscanf(buf, "%d", &externally_control);
1271  if (ret == 0) {
1272  ret = -EINVAL;
1273  return ret;
1274  }
1275 
1276  if (!externally_control) {
1277  charger->externally_control = 0;
1278  return count;
1279  }
1280 
1281  for (i = 0; i < desc->num_charger_regulators; i++) {
1282  if (&desc->charger_regulators[i] != charger &&
1283  !desc->charger_regulators[i].externally_control) {
1284  /*
1285  * At least, one charger is controlled by
1286  * charger-manager
1287  */
1288  chargers_externally_control = 0;
1289  break;
1290  }
1291  }
1292 
1293  if (!chargers_externally_control) {
1294  if (cm->charger_enabled) {
1295  try_charger_enable(charger->cm, false);
1296  charger->externally_control = externally_control;
1297  try_charger_enable(charger->cm, true);
1298  } else {
1299  charger->externally_control = externally_control;
1300  }
1301  } else {
1302  dev_warn(cm->dev,
1303  "'%s' regulator should be controlled "
1304  "in charger-manager because charger-manager "
1305  "must need at least one charger for charging\n",
1306  charger->regulator_name);
1307  }
1308 
1309  return count;
1310 }
1311 
1312 static int charger_manager_probe(struct platform_device *pdev)
1313 {
1314  struct charger_desc *desc = dev_get_platdata(&pdev->dev);
1315  struct charger_manager *cm;
1316  int ret = 0, i = 0;
1317  int j = 0;
1318  int chargers_externally_control = 1;
1319  union power_supply_propval val;
1320 
1321  if (g_desc && !rtc_dev && g_desc->rtc_name) {
1322  rtc_dev = rtc_class_open(g_desc->rtc_name);
1323  if (IS_ERR_OR_NULL(rtc_dev)) {
1324  rtc_dev = NULL;
1325  dev_err(&pdev->dev, "Cannot get RTC %s.\n",
1326  g_desc->rtc_name);
1327  ret = -ENODEV;
1328  goto err_alloc;
1329  }
1330  }
1331 
1332  if (!desc) {
1333  dev_err(&pdev->dev, "No platform data (desc) found.\n");
1334  ret = -ENODEV;
1335  goto err_alloc;
1336  }
1337 
1338  cm = kzalloc(sizeof(struct charger_manager), GFP_KERNEL);
1339  if (!cm) {
1340  dev_err(&pdev->dev, "Cannot allocate memory.\n");
1341  ret = -ENOMEM;
1342  goto err_alloc;
1343  }
1344 
1345  /* Basic Values. Unspecified are Null or 0 */
1346  cm->dev = &pdev->dev;
1347  cm->desc = kzalloc(sizeof(struct charger_desc), GFP_KERNEL);
1348  if (!cm->desc) {
1349  dev_err(&pdev->dev, "Cannot allocate memory.\n");
1350  ret = -ENOMEM;
1351  goto err_alloc_desc;
1352  }
1353  memcpy(cm->desc, desc, sizeof(struct charger_desc));
1354  cm->last_temp_mC = INT_MIN; /* denotes "unmeasured, yet" */
1355 
1356  /*
1357  * The following two do not need to be errors.
1358  * Users may intentionally ignore those two features.
1359  */
1360  if (desc->fullbatt_uV == 0) {
1361  dev_info(&pdev->dev, "Ignoring full-battery voltage threshold"
1362  " as it is not supplied.");
1363  }
1364  if (!desc->fullbatt_vchkdrop_ms || !desc->fullbatt_vchkdrop_uV) {
1365  dev_info(&pdev->dev, "Disabling full-battery voltage drop "
1366  "checking mechanism as it is not supplied.");
1367  desc->fullbatt_vchkdrop_ms = 0;
1368  desc->fullbatt_vchkdrop_uV = 0;
1369  }
1370  if (desc->fullbatt_soc == 0) {
1371  dev_info(&pdev->dev, "Ignoring full-battery soc(state of"
1372  " charge) threshold as it is not"
1373  " supplied.");
1374  }
1375  if (desc->fullbatt_full_capacity == 0) {
1376  dev_info(&pdev->dev, "Ignoring full-battery full capacity"
1377  " threshold as it is not supplied.");
1378  }
1379 
1380  if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
1381  ret = -EINVAL;
1382  dev_err(&pdev->dev, "charger_regulators undefined.\n");
1383  goto err_no_charger;
1384  }
1385 
1386  if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
1387  dev_err(&pdev->dev, "No power supply defined.\n");
1388  ret = -EINVAL;
1389  goto err_no_charger_stat;
1390  }
1391 
1392  /* Counting index only */
1393  while (desc->psy_charger_stat[i])
1394  i++;
1395 
1396  cm->charger_stat = kzalloc(sizeof(struct power_supply *) * (i + 1),
1397  GFP_KERNEL);
1398  if (!cm->charger_stat) {
1399  ret = -ENOMEM;
1400  goto err_no_charger_stat;
1401  }
1402 
1403  for (i = 0; desc->psy_charger_stat[i]; i++) {
1405  desc->psy_charger_stat[i]);
1406  if (!cm->charger_stat[i]) {
1407  dev_err(&pdev->dev, "Cannot find power supply "
1408  "\"%s\"\n",
1409  desc->psy_charger_stat[i]);
1410  ret = -ENODEV;
1411  goto err_chg_stat;
1412  }
1413  }
1414 
1416  if (!cm->fuel_gauge) {
1417  dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
1418  desc->psy_fuel_gauge);
1419  ret = -ENODEV;
1420  goto err_chg_stat;
1421  }
1422 
1423  if (desc->polling_interval_ms == 0 ||
1425  dev_err(&pdev->dev, "polling_interval_ms is too small\n");
1426  ret = -EINVAL;
1427  goto err_chg_stat;
1428  }
1429 
1430  if (!desc->temperature_out_of_range) {
1431  dev_err(&pdev->dev, "there is no temperature_out_of_range\n");
1432  ret = -EINVAL;
1433  goto err_chg_stat;
1434  }
1435 
1436  if (!desc->charging_max_duration_ms ||
1437  !desc->discharging_max_duration_ms) {
1438  dev_info(&pdev->dev, "Cannot limit charging duration "
1439  "checking mechanism to prevent overcharge/overheat "
1440  "and control discharging duration");
1441  desc->charging_max_duration_ms = 0;
1442  desc->discharging_max_duration_ms = 0;
1443  }
1444 
1445  platform_set_drvdata(pdev, cm);
1446 
1447  memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default));
1448 
1449  if (!desc->psy_name) {
1450  strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
1451  } else {
1453  }
1454  cm->charger_psy.name = cm->psy_name_buf;
1455 
1456  /* Allocate for psy properties because they may vary */
1457  cm->charger_psy.properties = kzalloc(sizeof(enum power_supply_property)
1458  * (ARRAY_SIZE(default_charger_props) +
1460  GFP_KERNEL);
1461  if (!cm->charger_psy.properties) {
1462  dev_err(&pdev->dev, "Cannot allocate for psy properties.\n");
1463  ret = -ENOMEM;
1464  goto err_chg_stat;
1465  }
1466  memcpy(cm->charger_psy.properties, default_charger_props,
1467  sizeof(enum power_supply_property) *
1468  ARRAY_SIZE(default_charger_props));
1469  cm->charger_psy.num_properties = psy_default.num_properties;
1470 
1471  /* Find which optional psy-properties are available */
1472  if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1474  cm->charger_psy.properties[cm->charger_psy.num_properties] =
1476  cm->charger_psy.num_properties++;
1477  }
1478  if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1480  &val)) {
1481  cm->charger_psy.properties[cm->charger_psy.num_properties] =
1483  cm->charger_psy.num_properties++;
1484  }
1485 
1486  if (desc->measure_battery_temp) {
1487  cm->charger_psy.properties[cm->charger_psy.num_properties] =
1489  cm->charger_psy.num_properties++;
1490  } else {
1491  cm->charger_psy.properties[cm->charger_psy.num_properties] =
1493  cm->charger_psy.num_properties++;
1494  }
1495 
1496  INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk);
1497 
1498  ret = power_supply_register(NULL, &cm->charger_psy);
1499  if (ret) {
1500  dev_err(&pdev->dev, "Cannot register charger-manager with"
1501  " name \"%s\".\n", cm->charger_psy.name);
1502  goto err_register;
1503  }
1504 
1505  for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1506  struct charger_regulator *charger
1507  = &desc->charger_regulators[i];
1508  char buf[11];
1509  char *str;
1510 
1511  charger->consumer = regulator_get(&pdev->dev,
1512  charger->regulator_name);
1513  if (charger->consumer == NULL) {
1514  dev_err(&pdev->dev, "Cannot find charger(%s)n",
1515  charger->regulator_name);
1516  ret = -EINVAL;
1517  goto err_chg_get;
1518  }
1519  charger->cm = cm;
1520 
1521  for (j = 0 ; j < charger->num_cables ; j++) {
1522  struct charger_cable *cable = &charger->cables[j];
1523 
1524  ret = charger_extcon_init(cm, cable);
1525  if (ret < 0) {
1526  dev_err(&pdev->dev, "Cannot find charger(%s)n",
1527  charger->regulator_name);
1528  goto err_extcon;
1529  }
1530  cable->charger = charger;
1531  cable->cm = cm;
1532  }
1533 
1534  /* Create sysfs entry to control charger(regulator) */
1535  snprintf(buf, 10, "charger.%d", i);
1536  str = kzalloc(sizeof(char) * (strlen(buf) + 1), GFP_KERNEL);
1537  if (!str) {
1538  for (i--; i >= 0; i--) {
1539  charger = &desc->charger_regulators[i];
1540  kfree(charger->attr_g.name);
1541  }
1542  ret = -ENOMEM;
1543 
1544  goto err_extcon;
1545  }
1546  strcpy(str, buf);
1547 
1548  charger->attrs[0] = &charger->attr_name.attr;
1549  charger->attrs[1] = &charger->attr_state.attr;
1550  charger->attrs[2] = &charger->attr_externally_control.attr;
1551  charger->attrs[3] = NULL;
1552  charger->attr_g.name = str;
1553  charger->attr_g.attrs = charger->attrs;
1554 
1555  sysfs_attr_init(&charger->attr_name.attr);
1556  charger->attr_name.attr.name = "name";
1557  charger->attr_name.attr.mode = 0444;
1558  charger->attr_name.show = charger_name_show;
1559 
1560  sysfs_attr_init(&charger->attr_state.attr);
1561  charger->attr_state.attr.name = "state";
1562  charger->attr_state.attr.mode = 0444;
1563  charger->attr_state.show = charger_state_show;
1564 
1565  sysfs_attr_init(&charger->attr_externally_control.attr);
1566  charger->attr_externally_control.attr.name
1567  = "externally_control";
1568  charger->attr_externally_control.attr.mode = 0644;
1569  charger->attr_externally_control.show
1570  = charger_externally_control_show;
1571  charger->attr_externally_control.store
1572  = charger_externally_control_store;
1573 
1574  if (!desc->charger_regulators[i].externally_control ||
1575  !chargers_externally_control) {
1576  chargers_externally_control = 0;
1577  }
1578  dev_info(&pdev->dev, "'%s' regulator's externally_control"
1579  "is %d\n", charger->regulator_name,
1580  charger->externally_control);
1581 
1582  ret = sysfs_create_group(&cm->charger_psy.dev->kobj,
1583  &charger->attr_g);
1584  if (ret < 0) {
1585  dev_info(&pdev->dev, "Cannot create sysfs entry"
1586  "of %s regulator\n",
1587  charger->regulator_name);
1588  }
1589  }
1590 
1591  if (chargers_externally_control) {
1592  dev_err(&pdev->dev, "Cannot register regulator because "
1593  "charger-manager must need at least "
1594  "one charger for charging battery\n");
1595 
1596  ret = -EINVAL;
1597  goto err_chg_enable;
1598  }
1599 
1600  ret = try_charger_enable(cm, true);
1601  if (ret) {
1602  dev_err(&pdev->dev, "Cannot enable charger regulators\n");
1603  goto err_chg_enable;
1604  }
1605 
1606  /* Add to the list */
1607  mutex_lock(&cm_list_mtx);
1608  list_add(&cm->entry, &cm_list);
1609  mutex_unlock(&cm_list_mtx);
1610 
1611  /*
1612  * Charger-manager is capable of waking up the systme from sleep
1613  * when event is happend through cm_notify_event()
1614  */
1615  device_init_wakeup(&pdev->dev, true);
1616  device_set_wakeup_capable(&pdev->dev, false);
1617 
1618  schedule_work(&setup_polling);
1619 
1620  return 0;
1621 
1622 err_chg_enable:
1623  for (i = 0; i < desc->num_charger_regulators; i++) {
1624  struct charger_regulator *charger;
1625 
1626  charger = &desc->charger_regulators[i];
1627  sysfs_remove_group(&cm->charger_psy.dev->kobj,
1628  &charger->attr_g);
1629  kfree(charger->attr_g.name);
1630  }
1631 err_extcon:
1632  for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1633  struct charger_regulator *charger
1634  = &desc->charger_regulators[i];
1635  for (j = 0 ; j < charger->num_cables ; j++) {
1636  struct charger_cable *cable = &charger->cables[j];
1638  }
1639  }
1640 err_chg_get:
1641  for (i = 0 ; i < desc->num_charger_regulators ; i++)
1642  regulator_put(desc->charger_regulators[i].consumer);
1643 
1645 err_register:
1646  kfree(cm->charger_psy.properties);
1647 err_chg_stat:
1648  kfree(cm->charger_stat);
1649 err_no_charger_stat:
1650 err_no_charger:
1651  kfree(cm->desc);
1652 err_alloc_desc:
1653  kfree(cm);
1654 err_alloc:
1655  return ret;
1656 }
1657 
1658 static int __devexit charger_manager_remove(struct platform_device *pdev)
1659 {
1660  struct charger_manager *cm = platform_get_drvdata(pdev);
1661  struct charger_desc *desc = cm->desc;
1662  int i = 0;
1663  int j = 0;
1664 
1665  /* Remove from the list */
1666  mutex_lock(&cm_list_mtx);
1667  list_del(&cm->entry);
1668  mutex_unlock(&cm_list_mtx);
1669 
1670  if (work_pending(&setup_polling))
1671  cancel_work_sync(&setup_polling);
1672  if (delayed_work_pending(&cm_monitor_work))
1673  cancel_delayed_work_sync(&cm_monitor_work);
1674 
1675  for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1676  struct charger_regulator *charger
1677  = &desc->charger_regulators[i];
1678  for (j = 0 ; j < charger->num_cables ; j++) {
1679  struct charger_cable *cable = &charger->cables[j];
1681  }
1682  }
1683 
1684  for (i = 0 ; i < desc->num_charger_regulators ; i++)
1685  regulator_put(desc->charger_regulators[i].consumer);
1686 
1688 
1689  try_charger_enable(cm, false);
1690 
1691  kfree(cm->charger_psy.properties);
1692  kfree(cm->charger_stat);
1693  kfree(cm->desc);
1694  kfree(cm);
1695 
1696  return 0;
1697 }
1698 
1699 static const struct platform_device_id charger_manager_id[] = {
1700  { "charger-manager", 0 },
1701  { },
1702 };
1703 MODULE_DEVICE_TABLE(platform, charger_manager_id);
1704 
1705 static int cm_suspend_noirq(struct device *dev)
1706 {
1707  int ret = 0;
1708 
1709  if (device_may_wakeup(dev)) {
1710  device_set_wakeup_capable(dev, false);
1711  ret = -EAGAIN;
1712  }
1713 
1714  return ret;
1715 }
1716 
1717 static int cm_suspend_prepare(struct device *dev)
1718 {
1719  struct charger_manager *cm = dev_get_drvdata(dev);
1720 
1721  if (!cm_suspended) {
1722  if (rtc_dev) {
1723  struct rtc_time tmp;
1724  unsigned long now;
1725 
1726  rtc_read_alarm(rtc_dev, &rtc_wkalarm_save);
1727  rtc_read_time(rtc_dev, &tmp);
1728 
1729  if (rtc_wkalarm_save.enabled) {
1730  rtc_tm_to_time(&rtc_wkalarm_save.time,
1731  &rtc_wkalarm_save_time);
1732  rtc_tm_to_time(&tmp, &now);
1733  if (now > rtc_wkalarm_save_time)
1734  rtc_wkalarm_save_time = 0;
1735  } else {
1736  rtc_wkalarm_save_time = 0;
1737  }
1738  }
1739  cm_suspended = true;
1740  }
1741 
1744  cm->status_save_ext_pwr_inserted = is_ext_pwr_online(cm);
1745  cm->status_save_batt = is_batt_present(cm);
1746 
1747  if (!cm_rtc_set) {
1748  cm_suspend_duration_ms = 0;
1749  cm_rtc_set = cm_setup_timer();
1750  }
1751 
1752  return 0;
1753 }
1754 
1755 static void cm_suspend_complete(struct device *dev)
1756 {
1757  struct charger_manager *cm = dev_get_drvdata(dev);
1758 
1759  if (cm_suspended) {
1760  if (rtc_dev) {
1761  struct rtc_wkalrm tmp;
1762 
1763  rtc_read_alarm(rtc_dev, &tmp);
1764  rtc_wkalarm_save.pending = tmp.pending;
1765  rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1766  }
1767  cm_suspended = false;
1768  cm_rtc_set = false;
1769  }
1770 
1771  /* Re-enqueue delayed work (fullbatt_vchk_work) */
1772  if (cm->fullbatt_vchk_jiffies_at) {
1773  unsigned long delay = 0;
1774  unsigned long now = jiffies + CM_JIFFIES_SMALL;
1775 
1776  if (time_after_eq(now, cm->fullbatt_vchk_jiffies_at)) {
1777  delay = (unsigned long)((long)now
1778  - (long)(cm->fullbatt_vchk_jiffies_at));
1779  delay = jiffies_to_msecs(delay);
1780  } else {
1781  delay = 0;
1782  }
1783 
1784  /*
1785  * Account for cm_suspend_duration_ms if
1786  * assume_timer_stops_in_suspend is active
1787  */
1788  if (g_desc && g_desc->assume_timer_stops_in_suspend) {
1789  if (delay > cm_suspend_duration_ms)
1790  delay -= cm_suspend_duration_ms;
1791  else
1792  delay = 0;
1793  }
1794 
1796  msecs_to_jiffies(delay));
1797  }
1798  device_set_wakeup_capable(cm->dev, false);
1799  uevent_notify(cm, NULL);
1800 }
1801 
1802 static const struct dev_pm_ops charger_manager_pm = {
1803  .prepare = cm_suspend_prepare,
1804  .suspend_noirq = cm_suspend_noirq,
1805  .complete = cm_suspend_complete,
1806 };
1807 
1808 static struct platform_driver charger_manager_driver = {
1809  .driver = {
1810  .name = "charger-manager",
1811  .owner = THIS_MODULE,
1812  .pm = &charger_manager_pm,
1813  },
1814  .probe = charger_manager_probe,
1815  .remove = __devexit_p(charger_manager_remove),
1816  .id_table = charger_manager_id,
1817 };
1818 
1819 static int __init charger_manager_init(void)
1820 {
1821  cm_wq = create_freezable_workqueue("charger_manager");
1822  INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller);
1823 
1824  return platform_driver_register(&charger_manager_driver);
1825 }
1826 late_initcall(charger_manager_init);
1827 
1828 static void __exit charger_manager_cleanup(void)
1829 {
1830  destroy_workqueue(cm_wq);
1831  cm_wq = NULL;
1832 
1833  platform_driver_unregister(&charger_manager_driver);
1834 }
1835 module_exit(charger_manager_cleanup);
1836 
1842 static bool find_power_supply(struct charger_manager *cm,
1843  struct power_supply *psy)
1844 {
1845  int i;
1846  bool found = false;
1847 
1848  for (i = 0; cm->charger_stat[i]; i++) {
1849  if (psy == cm->charger_stat[i]) {
1850  found = true;
1851  break;
1852  }
1853  }
1854 
1855  return found;
1856 }
1857 
1864 void cm_notify_event(struct power_supply *psy, enum cm_event_types type,
1865  char *msg)
1866 {
1867  struct charger_manager *cm;
1868  bool found_power_supply = false;
1869 
1870  if (psy == NULL)
1871  return;
1872 
1873  mutex_lock(&cm_list_mtx);
1874  list_for_each_entry(cm, &cm_list, entry) {
1875  found_power_supply = find_power_supply(cm, psy);
1876  if (found_power_supply)
1877  break;
1878  }
1879  mutex_unlock(&cm_list_mtx);
1880 
1881  if (!found_power_supply)
1882  return;
1883 
1884  switch (type) {
1885  case CM_EVENT_BATT_FULL:
1886  fullbatt_handler(cm);
1887  break;
1888  case CM_EVENT_BATT_OUT:
1889  battout_handler(cm);
1890  break;
1891  case CM_EVENT_BATT_IN:
1893  misc_event_handler(cm, type);
1894  break;
1895  case CM_EVENT_UNKNOWN:
1896  case CM_EVENT_OTHERS:
1897  uevent_notify(cm, msg ? msg : default_event_names[type]);
1898  break;
1899  default:
1900  dev_err(cm->dev, "%s type not specified.\n", __func__);
1901  break;
1902  }
1903 }
1905 
1906 MODULE_AUTHOR("MyungJoo Ham <[email protected]>");
1907 MODULE_DESCRIPTION("Charger Manager");
1908 MODULE_LICENSE("GPL");