Merge tag 'for-v4.21' of git://git.kernel.org/pub/scm/linux/kernel/git/sre/linux...
[sfrench/cifs-2.6.git] / drivers / power / supply / 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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
17 #include <linux/io.h>
18 #include <linux/module.h>
19 #include <linux/irq.h>
20 #include <linux/interrupt.h>
21 #include <linux/rtc.h>
22 #include <linux/slab.h>
23 #include <linux/workqueue.h>
24 #include <linux/platform_device.h>
25 #include <linux/power/charger-manager.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/sysfs.h>
28 #include <linux/of.h>
29 #include <linux/thermal.h>
30
31 /*
32  * Default termperature threshold for charging.
33  * Every temperature units are in tenth of centigrade.
34  */
35 #define CM_DEFAULT_RECHARGE_TEMP_DIFF   50
36 #define CM_DEFAULT_CHARGE_TEMP_MAX      500
37
38 static const char * const default_event_names[] = {
39         [CM_EVENT_UNKNOWN] = "Unknown",
40         [CM_EVENT_BATT_FULL] = "Battery Full",
41         [CM_EVENT_BATT_IN] = "Battery Inserted",
42         [CM_EVENT_BATT_OUT] = "Battery Pulled Out",
43         [CM_EVENT_BATT_OVERHEAT] = "Battery Overheat",
44         [CM_EVENT_BATT_COLD] = "Battery Cold",
45         [CM_EVENT_EXT_PWR_IN_OUT] = "External Power Attach/Detach",
46         [CM_EVENT_CHG_START_STOP] = "Charging Start/Stop",
47         [CM_EVENT_OTHERS] = "Other battery events"
48 };
49
50 /*
51  * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for
52  * delayed works so that we can run delayed works with CM_JIFFIES_SMALL
53  * without any delays.
54  */
55 #define CM_JIFFIES_SMALL        (2)
56
57 /* If y is valid (> 0) and smaller than x, do x = y */
58 #define CM_MIN_VALID(x, y)      x = (((y > 0) && ((x) > (y))) ? (y) : (x))
59
60 /*
61  * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking
62  * rtc alarm. It should be 2 or larger
63  */
64 #define CM_RTC_SMALL            (2)
65
66 #define UEVENT_BUF_SIZE         32
67
68 static LIST_HEAD(cm_list);
69 static DEFINE_MUTEX(cm_list_mtx);
70
71 /* About in-suspend (suspend-again) monitoring */
72 static struct alarm *cm_timer;
73
74 static bool cm_suspended;
75 static bool cm_timer_set;
76 static unsigned long cm_suspend_duration_ms;
77
78 /* About normal (not suspended) monitoring */
79 static unsigned long polling_jiffy = ULONG_MAX; /* ULONG_MAX: no polling */
80 static unsigned long next_polling; /* Next appointed polling time */
81 static struct workqueue_struct *cm_wq; /* init at driver add */
82 static struct delayed_work cm_monitor_work; /* init at driver add */
83
84 /**
85  * is_batt_present - See if the battery presents in place.
86  * @cm: the Charger Manager representing the battery.
87  */
88 static bool is_batt_present(struct charger_manager *cm)
89 {
90         union power_supply_propval val;
91         struct power_supply *psy;
92         bool present = false;
93         int i, ret;
94
95         switch (cm->desc->battery_present) {
96         case CM_BATTERY_PRESENT:
97                 present = true;
98                 break;
99         case CM_NO_BATTERY:
100                 break;
101         case CM_FUEL_GAUGE:
102                 psy = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
103                 if (!psy)
104                         break;
105
106                 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_PRESENT,
107                                 &val);
108                 if (ret == 0 && val.intval)
109                         present = true;
110                 power_supply_put(psy);
111                 break;
112         case CM_CHARGER_STAT:
113                 for (i = 0; cm->desc->psy_charger_stat[i]; i++) {
114                         psy = power_supply_get_by_name(
115                                         cm->desc->psy_charger_stat[i]);
116                         if (!psy) {
117                                 dev_err(cm->dev, "Cannot find power supply \"%s\"\n",
118                                         cm->desc->psy_charger_stat[i]);
119                                 continue;
120                         }
121
122                         ret = power_supply_get_property(psy,
123                                 POWER_SUPPLY_PROP_PRESENT, &val);
124                         power_supply_put(psy);
125                         if (ret == 0 && val.intval) {
126                                 present = true;
127                                 break;
128                         }
129                 }
130                 break;
131         }
132
133         return present;
134 }
135
136 /**
137  * is_ext_pwr_online - See if an external power source is attached to charge
138  * @cm: the Charger Manager representing the battery.
139  *
140  * Returns true if at least one of the chargers of the battery has an external
141  * power source attached to charge the battery regardless of whether it is
142  * actually charging or not.
143  */
144 static bool is_ext_pwr_online(struct charger_manager *cm)
145 {
146         union power_supply_propval val;
147         struct power_supply *psy;
148         bool online = false;
149         int i, ret;
150
151         /* If at least one of them has one, it's yes. */
152         for (i = 0; cm->desc->psy_charger_stat[i]; i++) {
153                 psy = power_supply_get_by_name(cm->desc->psy_charger_stat[i]);
154                 if (!psy) {
155                         dev_err(cm->dev, "Cannot find power supply \"%s\"\n",
156                                         cm->desc->psy_charger_stat[i]);
157                         continue;
158                 }
159
160                 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE,
161                                 &val);
162                 power_supply_put(psy);
163                 if (ret == 0 && val.intval) {
164                         online = true;
165                         break;
166                 }
167         }
168
169         return online;
170 }
171
172 /**
173  * get_batt_uV - Get the voltage level of the battery
174  * @cm: the Charger Manager representing the battery.
175  * @uV: the voltage level returned.
176  *
177  * Returns 0 if there is no error.
178  * Returns a negative value on error.
179  */
180 static int get_batt_uV(struct charger_manager *cm, int *uV)
181 {
182         union power_supply_propval val;
183         struct power_supply *fuel_gauge;
184         int ret;
185
186         fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
187         if (!fuel_gauge)
188                 return -ENODEV;
189
190         ret = power_supply_get_property(fuel_gauge,
191                                 POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
192         power_supply_put(fuel_gauge);
193         if (ret)
194                 return ret;
195
196         *uV = val.intval;
197         return 0;
198 }
199
200 /**
201  * is_charging - Returns true if the battery is being charged.
202  * @cm: the Charger Manager representing the battery.
203  */
204 static bool is_charging(struct charger_manager *cm)
205 {
206         int i, ret;
207         bool charging = false;
208         struct power_supply *psy;
209         union power_supply_propval val;
210
211         /* If there is no battery, it cannot be charged */
212         if (!is_batt_present(cm))
213                 return false;
214
215         /* If at least one of the charger is charging, return yes */
216         for (i = 0; cm->desc->psy_charger_stat[i]; i++) {
217                 /* 1. The charger sholuld not be DISABLED */
218                 if (cm->emergency_stop)
219                         continue;
220                 if (!cm->charger_enabled)
221                         continue;
222
223                 psy = power_supply_get_by_name(cm->desc->psy_charger_stat[i]);
224                 if (!psy) {
225                         dev_err(cm->dev, "Cannot find power supply \"%s\"\n",
226                                         cm->desc->psy_charger_stat[i]);
227                         continue;
228                 }
229
230                 /* 2. The charger should be online (ext-power) */
231                 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE,
232                                 &val);
233                 if (ret) {
234                         dev_warn(cm->dev, "Cannot read ONLINE value from %s\n",
235                                  cm->desc->psy_charger_stat[i]);
236                         power_supply_put(psy);
237                         continue;
238                 }
239                 if (val.intval == 0) {
240                         power_supply_put(psy);
241                         continue;
242                 }
243
244                 /*
245                  * 3. The charger should not be FULL, DISCHARGING,
246                  * or NOT_CHARGING.
247                  */
248                 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_STATUS,
249                                 &val);
250                 power_supply_put(psy);
251                 if (ret) {
252                         dev_warn(cm->dev, "Cannot read STATUS value from %s\n",
253                                  cm->desc->psy_charger_stat[i]);
254                         continue;
255                 }
256                 if (val.intval == POWER_SUPPLY_STATUS_FULL ||
257                                 val.intval == POWER_SUPPLY_STATUS_DISCHARGING ||
258                                 val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING)
259                         continue;
260
261                 /* Then, this is charging. */
262                 charging = true;
263                 break;
264         }
265
266         return charging;
267 }
268
269 /**
270  * is_full_charged - Returns true if the battery is fully charged.
271  * @cm: the Charger Manager representing the battery.
272  */
273 static bool is_full_charged(struct charger_manager *cm)
274 {
275         struct charger_desc *desc = cm->desc;
276         union power_supply_propval val;
277         struct power_supply *fuel_gauge;
278         bool is_full = false;
279         int ret = 0;
280         int uV;
281
282         /* If there is no battery, it cannot be charged */
283         if (!is_batt_present(cm))
284                 return false;
285
286         fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
287         if (!fuel_gauge)
288                 return false;
289
290         if (desc->fullbatt_full_capacity > 0) {
291                 val.intval = 0;
292
293                 /* Not full if capacity of fuel gauge isn't full */
294                 ret = power_supply_get_property(fuel_gauge,
295                                 POWER_SUPPLY_PROP_CHARGE_FULL, &val);
296                 if (!ret && val.intval > desc->fullbatt_full_capacity) {
297                         is_full = true;
298                         goto out;
299                 }
300         }
301
302         /* Full, if it's over the fullbatt voltage */
303         if (desc->fullbatt_uV > 0) {
304                 ret = get_batt_uV(cm, &uV);
305                 if (!ret && uV >= desc->fullbatt_uV) {
306                         is_full = true;
307                         goto out;
308                 }
309         }
310
311         /* Full, if the capacity is more than fullbatt_soc */
312         if (desc->fullbatt_soc > 0) {
313                 val.intval = 0;
314
315                 ret = power_supply_get_property(fuel_gauge,
316                                 POWER_SUPPLY_PROP_CAPACITY, &val);
317                 if (!ret && val.intval >= desc->fullbatt_soc) {
318                         is_full = true;
319                         goto out;
320                 }
321         }
322
323 out:
324         power_supply_put(fuel_gauge);
325         return is_full;
326 }
327
328 /**
329  * is_polling_required - Return true if need to continue polling for this CM.
330  * @cm: the Charger Manager representing the battery.
331  */
332 static bool is_polling_required(struct charger_manager *cm)
333 {
334         switch (cm->desc->polling_mode) {
335         case CM_POLL_DISABLE:
336                 return false;
337         case CM_POLL_ALWAYS:
338                 return true;
339         case CM_POLL_EXTERNAL_POWER_ONLY:
340                 return is_ext_pwr_online(cm);
341         case CM_POLL_CHARGING_ONLY:
342                 return is_charging(cm);
343         default:
344                 dev_warn(cm->dev, "Incorrect polling_mode (%d)\n",
345                          cm->desc->polling_mode);
346         }
347
348         return false;
349 }
350
351 /**
352  * try_charger_enable - Enable/Disable chargers altogether
353  * @cm: the Charger Manager representing the battery.
354  * @enable: true: enable / false: disable
355  *
356  * Note that Charger Manager keeps the charger enabled regardless whether
357  * the charger is charging or not (because battery is full or no external
358  * power source exists) except when CM needs to disable chargers forcibly
359  * bacause of emergency causes; when the battery is overheated or too cold.
360  */
361 static int try_charger_enable(struct charger_manager *cm, bool enable)
362 {
363         int err = 0, i;
364         struct charger_desc *desc = cm->desc;
365
366         /* Ignore if it's redundant command */
367         if (enable == cm->charger_enabled)
368                 return 0;
369
370         if (enable) {
371                 if (cm->emergency_stop)
372                         return -EAGAIN;
373
374                 /*
375                  * Save start time of charging to limit
376                  * maximum possible charging time.
377                  */
378                 cm->charging_start_time = ktime_to_ms(ktime_get());
379                 cm->charging_end_time = 0;
380
381                 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
382                         if (desc->charger_regulators[i].externally_control)
383                                 continue;
384
385                         err = regulator_enable(desc->charger_regulators[i].consumer);
386                         if (err < 0) {
387                                 dev_warn(cm->dev, "Cannot enable %s regulator\n",
388                                          desc->charger_regulators[i].regulator_name);
389                         }
390                 }
391         } else {
392                 /*
393                  * Save end time of charging to maintain fully charged state
394                  * of battery after full-batt.
395                  */
396                 cm->charging_start_time = 0;
397                 cm->charging_end_time = ktime_to_ms(ktime_get());
398
399                 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
400                         if (desc->charger_regulators[i].externally_control)
401                                 continue;
402
403                         err = regulator_disable(desc->charger_regulators[i].consumer);
404                         if (err < 0) {
405                                 dev_warn(cm->dev, "Cannot disable %s regulator\n",
406                                          desc->charger_regulators[i].regulator_name);
407                         }
408                 }
409
410                 /*
411                  * Abnormal battery state - Stop charging forcibly,
412                  * even if charger was enabled at the other places
413                  */
414                 for (i = 0; i < desc->num_charger_regulators; i++) {
415                         if (regulator_is_enabled(
416                                     desc->charger_regulators[i].consumer)) {
417                                 regulator_force_disable(
418                                         desc->charger_regulators[i].consumer);
419                                 dev_warn(cm->dev, "Disable regulator(%s) forcibly\n",
420                                          desc->charger_regulators[i].regulator_name);
421                         }
422                 }
423         }
424
425         if (!err)
426                 cm->charger_enabled = enable;
427
428         return err;
429 }
430
431 /**
432  * try_charger_restart - Restart charging.
433  * @cm: the Charger Manager representing the battery.
434  *
435  * Restart charging by turning off and on the charger.
436  */
437 static int try_charger_restart(struct charger_manager *cm)
438 {
439         int err;
440
441         if (cm->emergency_stop)
442                 return -EAGAIN;
443
444         err = try_charger_enable(cm, false);
445         if (err)
446                 return err;
447
448         return try_charger_enable(cm, true);
449 }
450
451 /**
452  * uevent_notify - Let users know something has changed.
453  * @cm: the Charger Manager representing the battery.
454  * @event: the event string.
455  *
456  * If @event is null, it implies that uevent_notify is called
457  * by resume function. When called in the resume function, cm_suspended
458  * should be already reset to false in order to let uevent_notify
459  * notify the recent event during the suspend to users. While
460  * suspended, uevent_notify does not notify users, but tracks
461  * events so that uevent_notify can notify users later after resumed.
462  */
463 static void uevent_notify(struct charger_manager *cm, const char *event)
464 {
465         static char env_str[UEVENT_BUF_SIZE + 1] = "";
466         static char env_str_save[UEVENT_BUF_SIZE + 1] = "";
467
468         if (cm_suspended) {
469                 /* Nothing in suspended-event buffer */
470                 if (env_str_save[0] == 0) {
471                         if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
472                                 return; /* status not changed */
473                         strncpy(env_str_save, event, UEVENT_BUF_SIZE);
474                         return;
475                 }
476
477                 if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE))
478                         return; /* Duplicated. */
479                 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
480                 return;
481         }
482
483         if (event == NULL) {
484                 /* No messages pending */
485                 if (!env_str_save[0])
486                         return;
487
488                 strncpy(env_str, env_str_save, UEVENT_BUF_SIZE);
489                 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
490                 env_str_save[0] = 0;
491
492                 return;
493         }
494
495         /* status not changed */
496         if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
497                 return;
498
499         /* save the status and notify the update */
500         strncpy(env_str, event, UEVENT_BUF_SIZE);
501         kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
502
503         dev_info(cm->dev, "%s\n", event);
504 }
505
506 /**
507  * fullbatt_vchk - Check voltage drop some times after "FULL" event.
508  * @work: the work_struct appointing the function
509  *
510  * If a user has designated "fullbatt_vchkdrop_ms/uV" values with
511  * charger_desc, Charger Manager checks voltage drop after the battery
512  * "FULL" event. It checks whether the voltage has dropped more than
513  * fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms.
514  */
515 static void fullbatt_vchk(struct work_struct *work)
516 {
517         struct delayed_work *dwork = to_delayed_work(work);
518         struct charger_manager *cm = container_of(dwork,
519                         struct charger_manager, fullbatt_vchk_work);
520         struct charger_desc *desc = cm->desc;
521         int batt_uV, err, diff;
522
523         /* remove the appointment for fullbatt_vchk */
524         cm->fullbatt_vchk_jiffies_at = 0;
525
526         if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
527                 return;
528
529         err = get_batt_uV(cm, &batt_uV);
530         if (err) {
531                 dev_err(cm->dev, "%s: get_batt_uV error(%d)\n", __func__, err);
532                 return;
533         }
534
535         diff = desc->fullbatt_uV - batt_uV;
536         if (diff < 0)
537                 return;
538
539         dev_info(cm->dev, "VBATT dropped %duV after full-batt\n", diff);
540
541         if (diff > desc->fullbatt_vchkdrop_uV) {
542                 try_charger_restart(cm);
543                 uevent_notify(cm, "Recharging");
544         }
545 }
546
547 /**
548  * check_charging_duration - Monitor charging/discharging duration
549  * @cm: the Charger Manager representing the battery.
550  *
551  * If whole charging duration exceed 'charging_max_duration_ms',
552  * cm stop charging to prevent overcharge/overheat. If discharging
553  * duration exceed 'discharging _max_duration_ms', charger cable is
554  * attached, after full-batt, cm start charging to maintain fully
555  * charged state for battery.
556  */
557 static int check_charging_duration(struct charger_manager *cm)
558 {
559         struct charger_desc *desc = cm->desc;
560         u64 curr = ktime_to_ms(ktime_get());
561         u64 duration;
562         int ret = false;
563
564         if (!desc->charging_max_duration_ms &&
565                         !desc->discharging_max_duration_ms)
566                 return ret;
567
568         if (cm->charger_enabled) {
569                 duration = curr - cm->charging_start_time;
570
571                 if (duration > desc->charging_max_duration_ms) {
572                         dev_info(cm->dev, "Charging duration exceed %ums\n",
573                                  desc->charging_max_duration_ms);
574                         uevent_notify(cm, "Discharging");
575                         try_charger_enable(cm, false);
576                         ret = true;
577                 }
578         } else if (is_ext_pwr_online(cm) && !cm->charger_enabled) {
579                 duration = curr - cm->charging_end_time;
580
581                 if (duration > desc->discharging_max_duration_ms &&
582                                 is_ext_pwr_online(cm)) {
583                         dev_info(cm->dev, "Discharging duration exceed %ums\n",
584                                  desc->discharging_max_duration_ms);
585                         uevent_notify(cm, "Recharging");
586                         try_charger_enable(cm, true);
587                         ret = true;
588                 }
589         }
590
591         return ret;
592 }
593
594 static int cm_get_battery_temperature_by_psy(struct charger_manager *cm,
595                                         int *temp)
596 {
597         struct power_supply *fuel_gauge;
598         int ret;
599
600         fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
601         if (!fuel_gauge)
602                 return -ENODEV;
603
604         ret = power_supply_get_property(fuel_gauge,
605                                 POWER_SUPPLY_PROP_TEMP,
606                                 (union power_supply_propval *)temp);
607         power_supply_put(fuel_gauge);
608
609         return ret;
610 }
611
612 static int cm_get_battery_temperature(struct charger_manager *cm,
613                                         int *temp)
614 {
615         int ret;
616
617         if (!cm->desc->measure_battery_temp)
618                 return -ENODEV;
619
620 #ifdef CONFIG_THERMAL
621         if (cm->tzd_batt) {
622                 ret = thermal_zone_get_temp(cm->tzd_batt, temp);
623                 if (!ret)
624                         /* Calibrate temperature unit */
625                         *temp /= 100;
626         } else
627 #endif
628         {
629                 /* if-else continued from CONFIG_THERMAL */
630                 ret = cm_get_battery_temperature_by_psy(cm, temp);
631         }
632
633         return ret;
634 }
635
636 static int cm_check_thermal_status(struct charger_manager *cm)
637 {
638         struct charger_desc *desc = cm->desc;
639         int temp, upper_limit, lower_limit;
640         int ret = 0;
641
642         ret = cm_get_battery_temperature(cm, &temp);
643         if (ret) {
644                 /* FIXME:
645                  * No information of battery temperature might
646                  * occur hazadous result. We have to handle it
647                  * depending on battery type.
648                  */
649                 dev_err(cm->dev, "Failed to get battery temperature\n");
650                 return 0;
651         }
652
653         upper_limit = desc->temp_max;
654         lower_limit = desc->temp_min;
655
656         if (cm->emergency_stop) {
657                 upper_limit -= desc->temp_diff;
658                 lower_limit += desc->temp_diff;
659         }
660
661         if (temp > upper_limit)
662                 ret = CM_EVENT_BATT_OVERHEAT;
663         else if (temp < lower_limit)
664                 ret = CM_EVENT_BATT_COLD;
665
666         return ret;
667 }
668
669 /**
670  * _cm_monitor - Monitor the temperature and return true for exceptions.
671  * @cm: the Charger Manager representing the battery.
672  *
673  * Returns true if there is an event to notify for the battery.
674  * (True if the status of "emergency_stop" changes)
675  */
676 static bool _cm_monitor(struct charger_manager *cm)
677 {
678         int temp_alrt;
679
680         temp_alrt = cm_check_thermal_status(cm);
681
682         /* It has been stopped already */
683         if (temp_alrt && cm->emergency_stop)
684                 return false;
685
686         /*
687          * Check temperature whether overheat or cold.
688          * If temperature is out of range normal state, stop charging.
689          */
690         if (temp_alrt) {
691                 cm->emergency_stop = temp_alrt;
692                 if (!try_charger_enable(cm, false))
693                         uevent_notify(cm, default_event_names[temp_alrt]);
694
695         /*
696          * Check whole charging duration and discharing duration
697          * after full-batt.
698          */
699         } else if (!cm->emergency_stop && check_charging_duration(cm)) {
700                 dev_dbg(cm->dev,
701                         "Charging/Discharging duration is out of range\n");
702         /*
703          * Check dropped voltage of battery. If battery voltage is more
704          * dropped than fullbatt_vchkdrop_uV after fully charged state,
705          * charger-manager have to recharge battery.
706          */
707         } else if (!cm->emergency_stop && is_ext_pwr_online(cm) &&
708                         !cm->charger_enabled) {
709                 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
710
711         /*
712          * Check whether fully charged state to protect overcharge
713          * if charger-manager is charging for battery.
714          */
715         } else if (!cm->emergency_stop && is_full_charged(cm) &&
716                         cm->charger_enabled) {
717                 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged\n");
718                 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
719
720                 try_charger_enable(cm, false);
721
722                 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
723         } else {
724                 cm->emergency_stop = 0;
725                 if (is_ext_pwr_online(cm)) {
726                         if (!try_charger_enable(cm, true))
727                                 uevent_notify(cm, "CHARGING");
728                 }
729         }
730
731         return true;
732 }
733
734 /**
735  * cm_monitor - Monitor every battery.
736  *
737  * Returns true if there is an event to notify from any of the batteries.
738  * (True if the status of "emergency_stop" changes)
739  */
740 static bool cm_monitor(void)
741 {
742         bool stop = false;
743         struct charger_manager *cm;
744
745         mutex_lock(&cm_list_mtx);
746
747         list_for_each_entry(cm, &cm_list, entry) {
748                 if (_cm_monitor(cm))
749                         stop = true;
750         }
751
752         mutex_unlock(&cm_list_mtx);
753
754         return stop;
755 }
756
757 /**
758  * _setup_polling - Setup the next instance of polling.
759  * @work: work_struct of the function _setup_polling.
760  */
761 static void _setup_polling(struct work_struct *work)
762 {
763         unsigned long min = ULONG_MAX;
764         struct charger_manager *cm;
765         bool keep_polling = false;
766         unsigned long _next_polling;
767
768         mutex_lock(&cm_list_mtx);
769
770         list_for_each_entry(cm, &cm_list, entry) {
771                 if (is_polling_required(cm) && cm->desc->polling_interval_ms) {
772                         keep_polling = true;
773
774                         if (min > cm->desc->polling_interval_ms)
775                                 min = cm->desc->polling_interval_ms;
776                 }
777         }
778
779         polling_jiffy = msecs_to_jiffies(min);
780         if (polling_jiffy <= CM_JIFFIES_SMALL)
781                 polling_jiffy = CM_JIFFIES_SMALL + 1;
782
783         if (!keep_polling)
784                 polling_jiffy = ULONG_MAX;
785         if (polling_jiffy == ULONG_MAX)
786                 goto out;
787
788         WARN(cm_wq == NULL, "charger-manager: workqueue not initialized"
789                             ". try it later. %s\n", __func__);
790
791         /*
792          * Use mod_delayed_work() iff the next polling interval should
793          * occur before the currently scheduled one.  If @cm_monitor_work
794          * isn't active, the end result is the same, so no need to worry
795          * about stale @next_polling.
796          */
797         _next_polling = jiffies + polling_jiffy;
798
799         if (time_before(_next_polling, next_polling)) {
800                 mod_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy);
801                 next_polling = _next_polling;
802         } else {
803                 if (queue_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy))
804                         next_polling = _next_polling;
805         }
806 out:
807         mutex_unlock(&cm_list_mtx);
808 }
809 static DECLARE_WORK(setup_polling, _setup_polling);
810
811 /**
812  * cm_monitor_poller - The Monitor / Poller.
813  * @work: work_struct of the function cm_monitor_poller
814  *
815  * During non-suspended state, cm_monitor_poller is used to poll and monitor
816  * the batteries.
817  */
818 static void cm_monitor_poller(struct work_struct *work)
819 {
820         cm_monitor();
821         schedule_work(&setup_polling);
822 }
823
824 /**
825  * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL
826  * @cm: the Charger Manager representing the battery.
827  */
828 static void fullbatt_handler(struct charger_manager *cm)
829 {
830         struct charger_desc *desc = cm->desc;
831
832         if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
833                 goto out;
834
835         if (cm_suspended)
836                 device_set_wakeup_capable(cm->dev, true);
837
838         mod_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
839                          msecs_to_jiffies(desc->fullbatt_vchkdrop_ms));
840         cm->fullbatt_vchk_jiffies_at = jiffies + msecs_to_jiffies(
841                                        desc->fullbatt_vchkdrop_ms);
842
843         if (cm->fullbatt_vchk_jiffies_at == 0)
844                 cm->fullbatt_vchk_jiffies_at = 1;
845
846 out:
847         dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged\n");
848         uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
849 }
850
851 /**
852  * battout_handler - Event handler for CM_EVENT_BATT_OUT
853  * @cm: the Charger Manager representing the battery.
854  */
855 static void battout_handler(struct charger_manager *cm)
856 {
857         if (cm_suspended)
858                 device_set_wakeup_capable(cm->dev, true);
859
860         if (!is_batt_present(cm)) {
861                 dev_emerg(cm->dev, "Battery Pulled Out!\n");
862                 uevent_notify(cm, default_event_names[CM_EVENT_BATT_OUT]);
863         } else {
864                 uevent_notify(cm, "Battery Reinserted?");
865         }
866 }
867
868 /**
869  * misc_event_handler - Handler for other evnets
870  * @cm: the Charger Manager representing the battery.
871  * @type: the Charger Manager representing the battery.
872  */
873 static void misc_event_handler(struct charger_manager *cm,
874                         enum cm_event_types type)
875 {
876         if (cm_suspended)
877                 device_set_wakeup_capable(cm->dev, true);
878
879         if (is_polling_required(cm) && cm->desc->polling_interval_ms)
880                 schedule_work(&setup_polling);
881         uevent_notify(cm, default_event_names[type]);
882 }
883
884 static int charger_get_property(struct power_supply *psy,
885                 enum power_supply_property psp,
886                 union power_supply_propval *val)
887 {
888         struct charger_manager *cm = power_supply_get_drvdata(psy);
889         struct charger_desc *desc = cm->desc;
890         struct power_supply *fuel_gauge = NULL;
891         int ret = 0;
892         int uV;
893
894         switch (psp) {
895         case POWER_SUPPLY_PROP_STATUS:
896                 if (is_charging(cm))
897                         val->intval = POWER_SUPPLY_STATUS_CHARGING;
898                 else if (is_ext_pwr_online(cm))
899                         val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
900                 else
901                         val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
902                 break;
903         case POWER_SUPPLY_PROP_HEALTH:
904                 if (cm->emergency_stop > 0)
905                         val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
906                 else if (cm->emergency_stop < 0)
907                         val->intval = POWER_SUPPLY_HEALTH_COLD;
908                 else
909                         val->intval = POWER_SUPPLY_HEALTH_GOOD;
910                 break;
911         case POWER_SUPPLY_PROP_PRESENT:
912                 if (is_batt_present(cm))
913                         val->intval = 1;
914                 else
915                         val->intval = 0;
916                 break;
917         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
918                 ret = get_batt_uV(cm, &val->intval);
919                 break;
920         case POWER_SUPPLY_PROP_CURRENT_NOW:
921                 fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
922                 if (!fuel_gauge) {
923                         ret = -ENODEV;
924                         break;
925                 }
926                 ret = power_supply_get_property(fuel_gauge,
927                                 POWER_SUPPLY_PROP_CURRENT_NOW, val);
928                 break;
929         case POWER_SUPPLY_PROP_TEMP:
930         case POWER_SUPPLY_PROP_TEMP_AMBIENT:
931                 return cm_get_battery_temperature(cm, &val->intval);
932         case POWER_SUPPLY_PROP_CAPACITY:
933                 if (!is_batt_present(cm)) {
934                         /* There is no battery. Assume 100% */
935                         val->intval = 100;
936                         break;
937                 }
938
939                 fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
940                 if (!fuel_gauge) {
941                         ret = -ENODEV;
942                         break;
943                 }
944
945                 ret = power_supply_get_property(fuel_gauge,
946                                         POWER_SUPPLY_PROP_CAPACITY, val);
947                 if (ret)
948                         break;
949
950                 if (val->intval > 100) {
951                         val->intval = 100;
952                         break;
953                 }
954                 if (val->intval < 0)
955                         val->intval = 0;
956
957                 /* Do not adjust SOC when charging: voltage is overrated */
958                 if (is_charging(cm))
959                         break;
960
961                 /*
962                  * If the capacity value is inconsistent, calibrate it base on
963                  * the battery voltage values and the thresholds given as desc
964                  */
965                 ret = get_batt_uV(cm, &uV);
966                 if (ret) {
967                         /* Voltage information not available. No calibration */
968                         ret = 0;
969                         break;
970                 }
971
972                 if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
973                     !is_charging(cm)) {
974                         val->intval = 100;
975                         break;
976                 }
977
978                 break;
979         case POWER_SUPPLY_PROP_ONLINE:
980                 if (is_ext_pwr_online(cm))
981                         val->intval = 1;
982                 else
983                         val->intval = 0;
984                 break;
985         case POWER_SUPPLY_PROP_CHARGE_FULL:
986                 if (is_full_charged(cm))
987                         val->intval = 1;
988                 else
989                         val->intval = 0;
990                 ret = 0;
991                 break;
992         case POWER_SUPPLY_PROP_CHARGE_NOW:
993                 if (is_charging(cm)) {
994                         fuel_gauge = power_supply_get_by_name(
995                                         cm->desc->psy_fuel_gauge);
996                         if (!fuel_gauge) {
997                                 ret = -ENODEV;
998                                 break;
999                         }
1000
1001                         ret = power_supply_get_property(fuel_gauge,
1002                                                 POWER_SUPPLY_PROP_CHARGE_NOW,
1003                                                 val);
1004                         if (ret) {
1005                                 val->intval = 1;
1006                                 ret = 0;
1007                         } else {
1008                                 /* If CHARGE_NOW is supplied, use it */
1009                                 val->intval = (val->intval > 0) ?
1010                                                 val->intval : 1;
1011                         }
1012                 } else {
1013                         val->intval = 0;
1014                 }
1015                 break;
1016         default:
1017                 return -EINVAL;
1018         }
1019         if (fuel_gauge)
1020                 power_supply_put(fuel_gauge);
1021         return ret;
1022 }
1023
1024 #define NUM_CHARGER_PSY_OPTIONAL        (4)
1025 static enum power_supply_property default_charger_props[] = {
1026         /* Guaranteed to provide */
1027         POWER_SUPPLY_PROP_STATUS,
1028         POWER_SUPPLY_PROP_HEALTH,
1029         POWER_SUPPLY_PROP_PRESENT,
1030         POWER_SUPPLY_PROP_VOLTAGE_NOW,
1031         POWER_SUPPLY_PROP_CAPACITY,
1032         POWER_SUPPLY_PROP_ONLINE,
1033         POWER_SUPPLY_PROP_CHARGE_FULL,
1034         /*
1035          * Optional properties are:
1036          * POWER_SUPPLY_PROP_CHARGE_NOW,
1037          * POWER_SUPPLY_PROP_CURRENT_NOW,
1038          * POWER_SUPPLY_PROP_TEMP, and
1039          * POWER_SUPPLY_PROP_TEMP_AMBIENT,
1040          */
1041 };
1042
1043 static const struct power_supply_desc psy_default = {
1044         .name = "battery",
1045         .type = POWER_SUPPLY_TYPE_BATTERY,
1046         .properties = default_charger_props,
1047         .num_properties = ARRAY_SIZE(default_charger_props),
1048         .get_property = charger_get_property,
1049         .no_thermal = true,
1050 };
1051
1052 /**
1053  * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
1054  *                  for suspend_again.
1055  *
1056  * Returns true if the alarm is set for Charger Manager to use.
1057  * Returns false if
1058  *      cm_setup_timer fails to set an alarm,
1059  *      cm_setup_timer does not need to set an alarm for Charger Manager,
1060  *      or an alarm previously configured is to be used.
1061  */
1062 static bool cm_setup_timer(void)
1063 {
1064         struct charger_manager *cm;
1065         unsigned int wakeup_ms = UINT_MAX;
1066         int timer_req = 0;
1067
1068         if (time_after(next_polling, jiffies))
1069                 CM_MIN_VALID(wakeup_ms,
1070                         jiffies_to_msecs(next_polling - jiffies));
1071
1072         mutex_lock(&cm_list_mtx);
1073         list_for_each_entry(cm, &cm_list, entry) {
1074                 unsigned int fbchk_ms = 0;
1075
1076                 /* fullbatt_vchk is required. setup timer for that */
1077                 if (cm->fullbatt_vchk_jiffies_at) {
1078                         fbchk_ms = jiffies_to_msecs(cm->fullbatt_vchk_jiffies_at
1079                                                     - jiffies);
1080                         if (time_is_before_eq_jiffies(
1081                                 cm->fullbatt_vchk_jiffies_at) ||
1082                                 msecs_to_jiffies(fbchk_ms) < CM_JIFFIES_SMALL) {
1083                                 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
1084                                 fbchk_ms = 0;
1085                         }
1086                 }
1087                 CM_MIN_VALID(wakeup_ms, fbchk_ms);
1088
1089                 /* Skip if polling is not required for this CM */
1090                 if (!is_polling_required(cm) && !cm->emergency_stop)
1091                         continue;
1092                 timer_req++;
1093                 if (cm->desc->polling_interval_ms == 0)
1094                         continue;
1095                 CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
1096         }
1097         mutex_unlock(&cm_list_mtx);
1098
1099         if (timer_req && cm_timer) {
1100                 ktime_t now, add;
1101
1102                 /*
1103                  * Set alarm with the polling interval (wakeup_ms)
1104                  * The alarm time should be NOW + CM_RTC_SMALL or later.
1105                  */
1106                 if (wakeup_ms == UINT_MAX ||
1107                         wakeup_ms < CM_RTC_SMALL * MSEC_PER_SEC)
1108                         wakeup_ms = 2 * CM_RTC_SMALL * MSEC_PER_SEC;
1109
1110                 pr_info("Charger Manager wakeup timer: %u ms\n", wakeup_ms);
1111
1112                 now = ktime_get_boottime();
1113                 add = ktime_set(wakeup_ms / MSEC_PER_SEC,
1114                                 (wakeup_ms % MSEC_PER_SEC) * NSEC_PER_MSEC);
1115                 alarm_start(cm_timer, ktime_add(now, add));
1116
1117                 cm_suspend_duration_ms = wakeup_ms;
1118
1119                 return true;
1120         }
1121         return false;
1122 }
1123
1124 /**
1125  * charger_extcon_work - enable/diable charger according to the state
1126  *                      of charger cable
1127  *
1128  * @work: work_struct of the function charger_extcon_work.
1129  */
1130 static void charger_extcon_work(struct work_struct *work)
1131 {
1132         struct charger_cable *cable =
1133                         container_of(work, struct charger_cable, wq);
1134         int ret;
1135
1136         if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) {
1137                 ret = regulator_set_current_limit(cable->charger->consumer,
1138                                         cable->min_uA, cable->max_uA);
1139                 if (ret < 0) {
1140                         pr_err("Cannot set current limit of %s (%s)\n",
1141                                cable->charger->regulator_name, cable->name);
1142                         return;
1143                 }
1144
1145                 pr_info("Set current limit of %s : %duA ~ %duA\n",
1146                         cable->charger->regulator_name,
1147                         cable->min_uA, cable->max_uA);
1148         }
1149
1150         try_charger_enable(cable->cm, cable->attached);
1151 }
1152
1153 /**
1154  * charger_extcon_notifier - receive the state of charger cable
1155  *                      when registered cable is attached or detached.
1156  *
1157  * @self: the notifier block of the charger_extcon_notifier.
1158  * @event: the cable state.
1159  * @ptr: the data pointer of notifier block.
1160  */
1161 static int charger_extcon_notifier(struct notifier_block *self,
1162                         unsigned long event, void *ptr)
1163 {
1164         struct charger_cable *cable =
1165                 container_of(self, struct charger_cable, nb);
1166
1167         /*
1168          * The newly state of charger cable.
1169          * If cable is attached, cable->attached is true.
1170          */
1171         cable->attached = event;
1172
1173         /*
1174          * Setup monitoring to check battery state
1175          * when charger cable is attached.
1176          */
1177         if (cable->attached && is_polling_required(cable->cm)) {
1178                 cancel_work_sync(&setup_polling);
1179                 schedule_work(&setup_polling);
1180         }
1181
1182         /*
1183          * Setup work for controlling charger(regulator)
1184          * according to charger cable.
1185          */
1186         schedule_work(&cable->wq);
1187
1188         return NOTIFY_DONE;
1189 }
1190
1191 /**
1192  * charger_extcon_init - register external connector to use it
1193  *                      as the charger cable
1194  *
1195  * @cm: the Charger Manager representing the battery.
1196  * @cable: the Charger cable representing the external connector.
1197  */
1198 static int charger_extcon_init(struct charger_manager *cm,
1199                 struct charger_cable *cable)
1200 {
1201         int ret;
1202
1203         /*
1204          * Charger manager use Extcon framework to identify
1205          * the charger cable among various external connector
1206          * cable (e.g., TA, USB, MHL, Dock).
1207          */
1208         INIT_WORK(&cable->wq, charger_extcon_work);
1209         cable->nb.notifier_call = charger_extcon_notifier;
1210         ret = extcon_register_interest(&cable->extcon_dev,
1211                         cable->extcon_name, cable->name, &cable->nb);
1212         if (ret < 0) {
1213                 pr_info("Cannot register extcon_dev for %s(cable: %s)\n",
1214                         cable->extcon_name, cable->name);
1215         }
1216
1217         return ret;
1218 }
1219
1220 /**
1221  * charger_manager_register_extcon - Register extcon device to recevie state
1222  *                                   of charger cable.
1223  * @cm: the Charger Manager representing the battery.
1224  *
1225  * This function support EXTCON(External Connector) subsystem to detect the
1226  * state of charger cables for enabling or disabling charger(regulator) and
1227  * select the charger cable for charging among a number of external cable
1228  * according to policy of H/W board.
1229  */
1230 static int charger_manager_register_extcon(struct charger_manager *cm)
1231 {
1232         struct charger_desc *desc = cm->desc;
1233         struct charger_regulator *charger;
1234         int ret;
1235         int i;
1236         int j;
1237
1238         for (i = 0; i < desc->num_charger_regulators; i++) {
1239                 charger = &desc->charger_regulators[i];
1240
1241                 charger->consumer = regulator_get(cm->dev,
1242                                         charger->regulator_name);
1243                 if (IS_ERR(charger->consumer)) {
1244                         dev_err(cm->dev, "Cannot find charger(%s)\n",
1245                                 charger->regulator_name);
1246                         return PTR_ERR(charger->consumer);
1247                 }
1248                 charger->cm = cm;
1249
1250                 for (j = 0; j < charger->num_cables; j++) {
1251                         struct charger_cable *cable = &charger->cables[j];
1252
1253                         ret = charger_extcon_init(cm, cable);
1254                         if (ret < 0) {
1255                                 dev_err(cm->dev, "Cannot initialize charger(%s)\n",
1256                                         charger->regulator_name);
1257                                 return ret;
1258                         }
1259                         cable->charger = charger;
1260                         cable->cm = cm;
1261                 }
1262         }
1263
1264         return 0;
1265 }
1266
1267 /* help function of sysfs node to control charger(regulator) */
1268 static ssize_t charger_name_show(struct device *dev,
1269                                 struct device_attribute *attr, char *buf)
1270 {
1271         struct charger_regulator *charger
1272                 = container_of(attr, struct charger_regulator, attr_name);
1273
1274         return sprintf(buf, "%s\n", charger->regulator_name);
1275 }
1276
1277 static ssize_t charger_state_show(struct device *dev,
1278                                 struct device_attribute *attr, char *buf)
1279 {
1280         struct charger_regulator *charger
1281                 = container_of(attr, struct charger_regulator, attr_state);
1282         int state = 0;
1283
1284         if (!charger->externally_control)
1285                 state = regulator_is_enabled(charger->consumer);
1286
1287         return sprintf(buf, "%s\n", state ? "enabled" : "disabled");
1288 }
1289
1290 static ssize_t charger_externally_control_show(struct device *dev,
1291                                 struct device_attribute *attr, char *buf)
1292 {
1293         struct charger_regulator *charger = container_of(attr,
1294                         struct charger_regulator, attr_externally_control);
1295
1296         return sprintf(buf, "%d\n", charger->externally_control);
1297 }
1298
1299 static ssize_t charger_externally_control_store(struct device *dev,
1300                                 struct device_attribute *attr, const char *buf,
1301                                 size_t count)
1302 {
1303         struct charger_regulator *charger
1304                 = container_of(attr, struct charger_regulator,
1305                                         attr_externally_control);
1306         struct charger_manager *cm = charger->cm;
1307         struct charger_desc *desc = cm->desc;
1308         int i;
1309         int ret;
1310         int externally_control;
1311         int chargers_externally_control = 1;
1312
1313         ret = sscanf(buf, "%d", &externally_control);
1314         if (ret == 0) {
1315                 ret = -EINVAL;
1316                 return ret;
1317         }
1318
1319         if (!externally_control) {
1320                 charger->externally_control = 0;
1321                 return count;
1322         }
1323
1324         for (i = 0; i < desc->num_charger_regulators; i++) {
1325                 if (&desc->charger_regulators[i] != charger &&
1326                         !desc->charger_regulators[i].externally_control) {
1327                         /*
1328                          * At least, one charger is controlled by
1329                          * charger-manager
1330                          */
1331                         chargers_externally_control = 0;
1332                         break;
1333                 }
1334         }
1335
1336         if (!chargers_externally_control) {
1337                 if (cm->charger_enabled) {
1338                         try_charger_enable(charger->cm, false);
1339                         charger->externally_control = externally_control;
1340                         try_charger_enable(charger->cm, true);
1341                 } else {
1342                         charger->externally_control = externally_control;
1343                 }
1344         } else {
1345                 dev_warn(cm->dev,
1346                          "'%s' regulator should be controlled in charger-manager because charger-manager must need at least one charger for charging\n",
1347                          charger->regulator_name);
1348         }
1349
1350         return count;
1351 }
1352
1353 /**
1354  * charger_manager_prepare_sysfs - Prepare sysfs entry for each charger
1355  * @cm: the Charger Manager representing the battery.
1356  *
1357  * This function add sysfs entry for charger(regulator) to control charger from
1358  * user-space. If some development board use one more chargers for charging
1359  * but only need one charger on specific case which is dependent on user
1360  * scenario or hardware restrictions, the user enter 1 or 0(zero) to '/sys/
1361  * class/power_supply/battery/charger.[index]/externally_control'. For example,
1362  * if user enter 1 to 'sys/class/power_supply/battery/charger.[index]/
1363  * externally_control, this charger isn't controlled from charger-manager and
1364  * always stay off state of regulator.
1365  */
1366 static int charger_manager_prepare_sysfs(struct charger_manager *cm)
1367 {
1368         struct charger_desc *desc = cm->desc;
1369         struct charger_regulator *charger;
1370         int chargers_externally_control = 1;
1371         char *name;
1372         int i;
1373
1374         /* Create sysfs entry to control charger(regulator) */
1375         for (i = 0; i < desc->num_charger_regulators; i++) {
1376                 charger = &desc->charger_regulators[i];
1377
1378                 name = devm_kasprintf(cm->dev, GFP_KERNEL, "charger.%d", i);
1379                 if (!name)
1380                         return -ENOMEM;
1381
1382                 charger->attrs[0] = &charger->attr_name.attr;
1383                 charger->attrs[1] = &charger->attr_state.attr;
1384                 charger->attrs[2] = &charger->attr_externally_control.attr;
1385                 charger->attrs[3] = NULL;
1386
1387                 charger->attr_grp.name = name;
1388                 charger->attr_grp.attrs = charger->attrs;
1389                 desc->sysfs_groups[i] = &charger->attr_grp;
1390
1391                 sysfs_attr_init(&charger->attr_name.attr);
1392                 charger->attr_name.attr.name = "name";
1393                 charger->attr_name.attr.mode = 0444;
1394                 charger->attr_name.show = charger_name_show;
1395
1396                 sysfs_attr_init(&charger->attr_state.attr);
1397                 charger->attr_state.attr.name = "state";
1398                 charger->attr_state.attr.mode = 0444;
1399                 charger->attr_state.show = charger_state_show;
1400
1401                 sysfs_attr_init(&charger->attr_externally_control.attr);
1402                 charger->attr_externally_control.attr.name
1403                                 = "externally_control";
1404                 charger->attr_externally_control.attr.mode = 0644;
1405                 charger->attr_externally_control.show
1406                                 = charger_externally_control_show;
1407                 charger->attr_externally_control.store
1408                                 = charger_externally_control_store;
1409
1410                 if (!desc->charger_regulators[i].externally_control ||
1411                                 !chargers_externally_control)
1412                         chargers_externally_control = 0;
1413
1414                 dev_info(cm->dev, "'%s' regulator's externally_control is %d\n",
1415                          charger->regulator_name, charger->externally_control);
1416         }
1417
1418         if (chargers_externally_control) {
1419                 dev_err(cm->dev, "Cannot register regulator because charger-manager must need at least one charger for charging battery\n");
1420                 return -EINVAL;
1421         }
1422
1423         return 0;
1424 }
1425
1426 static int cm_init_thermal_data(struct charger_manager *cm,
1427                 struct power_supply *fuel_gauge)
1428 {
1429         struct charger_desc *desc = cm->desc;
1430         union power_supply_propval val;
1431         int ret;
1432
1433         /* Verify whether fuel gauge provides battery temperature */
1434         ret = power_supply_get_property(fuel_gauge,
1435                                         POWER_SUPPLY_PROP_TEMP, &val);
1436
1437         if (!ret) {
1438                 cm->charger_psy_desc.properties[cm->charger_psy_desc.num_properties] =
1439                                 POWER_SUPPLY_PROP_TEMP;
1440                 cm->charger_psy_desc.num_properties++;
1441                 cm->desc->measure_battery_temp = true;
1442         }
1443 #ifdef CONFIG_THERMAL
1444         if (ret && desc->thermal_zone) {
1445                 cm->tzd_batt =
1446                         thermal_zone_get_zone_by_name(desc->thermal_zone);
1447                 if (IS_ERR(cm->tzd_batt))
1448                         return PTR_ERR(cm->tzd_batt);
1449
1450                 /* Use external thermometer */
1451                 cm->charger_psy_desc.properties[cm->charger_psy_desc.num_properties] =
1452                                 POWER_SUPPLY_PROP_TEMP_AMBIENT;
1453                 cm->charger_psy_desc.num_properties++;
1454                 cm->desc->measure_battery_temp = true;
1455                 ret = 0;
1456         }
1457 #endif
1458         if (cm->desc->measure_battery_temp) {
1459                 /* NOTICE : Default allowable minimum charge temperature is 0 */
1460                 if (!desc->temp_max)
1461                         desc->temp_max = CM_DEFAULT_CHARGE_TEMP_MAX;
1462                 if (!desc->temp_diff)
1463                         desc->temp_diff = CM_DEFAULT_RECHARGE_TEMP_DIFF;
1464         }
1465
1466         return ret;
1467 }
1468
1469 static const struct of_device_id charger_manager_match[] = {
1470         {
1471                 .compatible = "charger-manager",
1472         },
1473         {},
1474 };
1475
1476 static struct charger_desc *of_cm_parse_desc(struct device *dev)
1477 {
1478         struct charger_desc *desc;
1479         struct device_node *np = dev->of_node;
1480         u32 poll_mode = CM_POLL_DISABLE;
1481         u32 battery_stat = CM_NO_BATTERY;
1482         int num_chgs = 0;
1483
1484         desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
1485         if (!desc)
1486                 return ERR_PTR(-ENOMEM);
1487
1488         of_property_read_string(np, "cm-name", &desc->psy_name);
1489
1490         of_property_read_u32(np, "cm-poll-mode", &poll_mode);
1491         desc->polling_mode = poll_mode;
1492
1493         of_property_read_u32(np, "cm-poll-interval",
1494                                 &desc->polling_interval_ms);
1495
1496         of_property_read_u32(np, "cm-fullbatt-vchkdrop-ms",
1497                                         &desc->fullbatt_vchkdrop_ms);
1498         of_property_read_u32(np, "cm-fullbatt-vchkdrop-volt",
1499                                         &desc->fullbatt_vchkdrop_uV);
1500         of_property_read_u32(np, "cm-fullbatt-voltage", &desc->fullbatt_uV);
1501         of_property_read_u32(np, "cm-fullbatt-soc", &desc->fullbatt_soc);
1502         of_property_read_u32(np, "cm-fullbatt-capacity",
1503                                         &desc->fullbatt_full_capacity);
1504
1505         of_property_read_u32(np, "cm-battery-stat", &battery_stat);
1506         desc->battery_present = battery_stat;
1507
1508         /* chargers */
1509         of_property_read_u32(np, "cm-num-chargers", &num_chgs);
1510         if (num_chgs) {
1511                 int i;
1512
1513                 /* Allocate empty bin at the tail of array */
1514                 desc->psy_charger_stat = devm_kcalloc(dev,
1515                                                       num_chgs + 1,
1516                                                       sizeof(char *),
1517                                                       GFP_KERNEL);
1518                 if (!desc->psy_charger_stat)
1519                         return ERR_PTR(-ENOMEM);
1520
1521                 for (i = 0; i < num_chgs; i++)
1522                         of_property_read_string_index(np, "cm-chargers",
1523                                                       i, &desc->psy_charger_stat[i]);
1524         }
1525
1526         of_property_read_string(np, "cm-fuel-gauge", &desc->psy_fuel_gauge);
1527
1528         of_property_read_string(np, "cm-thermal-zone", &desc->thermal_zone);
1529
1530         of_property_read_u32(np, "cm-battery-cold", &desc->temp_min);
1531         if (of_get_property(np, "cm-battery-cold-in-minus", NULL))
1532                 desc->temp_min *= -1;
1533         of_property_read_u32(np, "cm-battery-hot", &desc->temp_max);
1534         of_property_read_u32(np, "cm-battery-temp-diff", &desc->temp_diff);
1535
1536         of_property_read_u32(np, "cm-charging-max",
1537                                 &desc->charging_max_duration_ms);
1538         of_property_read_u32(np, "cm-discharging-max",
1539                                 &desc->discharging_max_duration_ms);
1540
1541         /* battery charger regualtors */
1542         desc->num_charger_regulators = of_get_child_count(np);
1543         if (desc->num_charger_regulators) {
1544                 struct charger_regulator *chg_regs;
1545                 struct device_node *child;
1546
1547                 chg_regs = devm_kcalloc(dev,
1548                                         desc->num_charger_regulators,
1549                                         sizeof(*chg_regs),
1550                                         GFP_KERNEL);
1551                 if (!chg_regs)
1552                         return ERR_PTR(-ENOMEM);
1553
1554                 desc->charger_regulators = chg_regs;
1555
1556                 desc->sysfs_groups = devm_kcalloc(dev,
1557                                         desc->num_charger_regulators + 1,
1558                                         sizeof(*desc->sysfs_groups),
1559                                         GFP_KERNEL);
1560                 if (!desc->sysfs_groups)
1561                         return ERR_PTR(-ENOMEM);
1562
1563                 for_each_child_of_node(np, child) {
1564                         struct charger_cable *cables;
1565                         struct device_node *_child;
1566
1567                         of_property_read_string(child, "cm-regulator-name",
1568                                         &chg_regs->regulator_name);
1569
1570                         /* charger cables */
1571                         chg_regs->num_cables = of_get_child_count(child);
1572                         if (chg_regs->num_cables) {
1573                                 cables = devm_kcalloc(dev,
1574                                                       chg_regs->num_cables,
1575                                                       sizeof(*cables),
1576                                                       GFP_KERNEL);
1577                                 if (!cables) {
1578                                         of_node_put(child);
1579                                         return ERR_PTR(-ENOMEM);
1580                                 }
1581
1582                                 chg_regs->cables = cables;
1583
1584                                 for_each_child_of_node(child, _child) {
1585                                         of_property_read_string(_child,
1586                                         "cm-cable-name", &cables->name);
1587                                         of_property_read_string(_child,
1588                                         "cm-cable-extcon",
1589                                         &cables->extcon_name);
1590                                         of_property_read_u32(_child,
1591                                         "cm-cable-min",
1592                                         &cables->min_uA);
1593                                         of_property_read_u32(_child,
1594                                         "cm-cable-max",
1595                                         &cables->max_uA);
1596                                         cables++;
1597                                 }
1598                         }
1599                         chg_regs++;
1600                 }
1601         }
1602         return desc;
1603 }
1604
1605 static inline struct charger_desc *cm_get_drv_data(struct platform_device *pdev)
1606 {
1607         if (pdev->dev.of_node)
1608                 return of_cm_parse_desc(&pdev->dev);
1609         return dev_get_platdata(&pdev->dev);
1610 }
1611
1612 static enum alarmtimer_restart cm_timer_func(struct alarm *alarm, ktime_t now)
1613 {
1614         cm_timer_set = false;
1615         return ALARMTIMER_NORESTART;
1616 }
1617
1618 static int charger_manager_probe(struct platform_device *pdev)
1619 {
1620         struct charger_desc *desc = cm_get_drv_data(pdev);
1621         struct charger_manager *cm;
1622         int ret, i = 0;
1623         int j = 0;
1624         union power_supply_propval val;
1625         struct power_supply *fuel_gauge;
1626         struct power_supply_config psy_cfg = {};
1627
1628         if (IS_ERR(desc)) {
1629                 dev_err(&pdev->dev, "No platform data (desc) found\n");
1630                 return PTR_ERR(desc);
1631         }
1632
1633         cm = devm_kzalloc(&pdev->dev, sizeof(*cm), GFP_KERNEL);
1634         if (!cm)
1635                 return -ENOMEM;
1636
1637         /* Basic Values. Unspecified are Null or 0 */
1638         cm->dev = &pdev->dev;
1639         cm->desc = desc;
1640         psy_cfg.drv_data = cm;
1641
1642         /* Initialize alarm timer */
1643         if (alarmtimer_get_rtcdev()) {
1644                 cm_timer = devm_kzalloc(cm->dev, sizeof(*cm_timer), GFP_KERNEL);
1645                 if (!cm_timer)
1646                         return -ENOMEM;
1647                 alarm_init(cm_timer, ALARM_BOOTTIME, cm_timer_func);
1648         }
1649
1650         /*
1651          * Some of the following do not need to be errors.
1652          * Users may intentionally ignore those features.
1653          */
1654         if (desc->fullbatt_uV == 0) {
1655                 dev_info(&pdev->dev, "Ignoring full-battery voltage threshold as it is not supplied\n");
1656         }
1657         if (!desc->fullbatt_vchkdrop_ms || !desc->fullbatt_vchkdrop_uV) {
1658                 dev_info(&pdev->dev, "Disabling full-battery voltage drop checking mechanism as it is not supplied\n");
1659                 desc->fullbatt_vchkdrop_ms = 0;
1660                 desc->fullbatt_vchkdrop_uV = 0;
1661         }
1662         if (desc->fullbatt_soc == 0) {
1663                 dev_info(&pdev->dev, "Ignoring full-battery soc(state of charge) threshold as it is not supplied\n");
1664         }
1665         if (desc->fullbatt_full_capacity == 0) {
1666                 dev_info(&pdev->dev, "Ignoring full-battery full capacity threshold as it is not supplied\n");
1667         }
1668
1669         if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
1670                 dev_err(&pdev->dev, "charger_regulators undefined\n");
1671                 return -EINVAL;
1672         }
1673
1674         if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
1675                 dev_err(&pdev->dev, "No power supply defined\n");
1676                 return -EINVAL;
1677         }
1678
1679         if (!desc->psy_fuel_gauge) {
1680                 dev_err(&pdev->dev, "No fuel gauge power supply defined\n");
1681                 return -EINVAL;
1682         }
1683
1684         /* Check if charger's supplies are present at probe */
1685         for (i = 0; desc->psy_charger_stat[i]; i++) {
1686                 struct power_supply *psy;
1687
1688                 psy = power_supply_get_by_name(desc->psy_charger_stat[i]);
1689                 if (!psy) {
1690                         dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
1691                                 desc->psy_charger_stat[i]);
1692                         return -ENODEV;
1693                 }
1694                 power_supply_put(psy);
1695         }
1696
1697         if (cm->desc->polling_mode != CM_POLL_DISABLE &&
1698             (desc->polling_interval_ms == 0 ||
1699              msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL)) {
1700                 dev_err(&pdev->dev, "polling_interval_ms is too small\n");
1701                 return -EINVAL;
1702         }
1703
1704         if (!desc->charging_max_duration_ms ||
1705                         !desc->discharging_max_duration_ms) {
1706                 dev_info(&pdev->dev, "Cannot limit charging duration checking mechanism to prevent overcharge/overheat and control discharging duration\n");
1707                 desc->charging_max_duration_ms = 0;
1708                 desc->discharging_max_duration_ms = 0;
1709         }
1710
1711         platform_set_drvdata(pdev, cm);
1712
1713         memcpy(&cm->charger_psy_desc, &psy_default, sizeof(psy_default));
1714
1715         if (!desc->psy_name)
1716                 strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
1717         else
1718                 strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
1719         cm->charger_psy_desc.name = cm->psy_name_buf;
1720
1721         /* Allocate for psy properties because they may vary */
1722         cm->charger_psy_desc.properties =
1723                 devm_kcalloc(&pdev->dev,
1724                              ARRAY_SIZE(default_charger_props) +
1725                                 NUM_CHARGER_PSY_OPTIONAL,
1726                              sizeof(enum power_supply_property), GFP_KERNEL);
1727         if (!cm->charger_psy_desc.properties)
1728                 return -ENOMEM;
1729
1730         memcpy(cm->charger_psy_desc.properties, default_charger_props,
1731                 sizeof(enum power_supply_property) *
1732                 ARRAY_SIZE(default_charger_props));
1733         cm->charger_psy_desc.num_properties = psy_default.num_properties;
1734
1735         /* Find which optional psy-properties are available */
1736         fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
1737         if (!fuel_gauge) {
1738                 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
1739                         desc->psy_fuel_gauge);
1740                 return -ENODEV;
1741         }
1742         if (!power_supply_get_property(fuel_gauge,
1743                                           POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
1744                 cm->charger_psy_desc.properties[cm->charger_psy_desc.num_properties] =
1745                                 POWER_SUPPLY_PROP_CHARGE_NOW;
1746                 cm->charger_psy_desc.num_properties++;
1747         }
1748         if (!power_supply_get_property(fuel_gauge,
1749                                           POWER_SUPPLY_PROP_CURRENT_NOW,
1750                                           &val)) {
1751                 cm->charger_psy_desc.properties[cm->charger_psy_desc.num_properties] =
1752                                 POWER_SUPPLY_PROP_CURRENT_NOW;
1753                 cm->charger_psy_desc.num_properties++;
1754         }
1755
1756         ret = cm_init_thermal_data(cm, fuel_gauge);
1757         if (ret) {
1758                 dev_err(&pdev->dev, "Failed to initialize thermal data\n");
1759                 cm->desc->measure_battery_temp = false;
1760         }
1761         power_supply_put(fuel_gauge);
1762
1763         INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk);
1764
1765         /* Register sysfs entry for charger(regulator) */
1766         ret = charger_manager_prepare_sysfs(cm);
1767         if (ret < 0) {
1768                 dev_err(&pdev->dev,
1769                         "Cannot prepare sysfs entry of regulators\n");
1770                 return ret;
1771         }
1772         psy_cfg.attr_grp = desc->sysfs_groups;
1773
1774         cm->charger_psy = power_supply_register(&pdev->dev,
1775                                                 &cm->charger_psy_desc,
1776                                                 &psy_cfg);
1777         if (IS_ERR(cm->charger_psy)) {
1778                 dev_err(&pdev->dev, "Cannot register charger-manager with name \"%s\"\n",
1779                         cm->charger_psy_desc.name);
1780                 return PTR_ERR(cm->charger_psy);
1781         }
1782
1783         /* Register extcon device for charger cable */
1784         ret = charger_manager_register_extcon(cm);
1785         if (ret < 0) {
1786                 dev_err(&pdev->dev, "Cannot initialize extcon device\n");
1787                 goto err_reg_extcon;
1788         }
1789
1790         /* Add to the list */
1791         mutex_lock(&cm_list_mtx);
1792         list_add(&cm->entry, &cm_list);
1793         mutex_unlock(&cm_list_mtx);
1794
1795         /*
1796          * Charger-manager is capable of waking up the systme from sleep
1797          * when event is happened through cm_notify_event()
1798          */
1799         device_init_wakeup(&pdev->dev, true);
1800         device_set_wakeup_capable(&pdev->dev, false);
1801
1802         /*
1803          * Charger-manager have to check the charging state right after
1804          * tialization of charger-manager and then update current charging
1805          * state.
1806          */
1807         cm_monitor();
1808
1809         schedule_work(&setup_polling);
1810
1811         return 0;
1812
1813 err_reg_extcon:
1814         for (i = 0; i < desc->num_charger_regulators; i++) {
1815                 struct charger_regulator *charger;
1816
1817                 charger = &desc->charger_regulators[i];
1818                 for (j = 0; j < charger->num_cables; j++) {
1819                         struct charger_cable *cable = &charger->cables[j];
1820                         /* Remove notifier block if only edev exists */
1821                         if (cable->extcon_dev.edev)
1822                                 extcon_unregister_interest(&cable->extcon_dev);
1823                 }
1824
1825                 regulator_put(desc->charger_regulators[i].consumer);
1826         }
1827
1828         power_supply_unregister(cm->charger_psy);
1829
1830         return ret;
1831 }
1832
1833 static int charger_manager_remove(struct platform_device *pdev)
1834 {
1835         struct charger_manager *cm = platform_get_drvdata(pdev);
1836         struct charger_desc *desc = cm->desc;
1837         int i = 0;
1838         int j = 0;
1839
1840         /* Remove from the list */
1841         mutex_lock(&cm_list_mtx);
1842         list_del(&cm->entry);
1843         mutex_unlock(&cm_list_mtx);
1844
1845         cancel_work_sync(&setup_polling);
1846         cancel_delayed_work_sync(&cm_monitor_work);
1847
1848         for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1849                 struct charger_regulator *charger
1850                                 = &desc->charger_regulators[i];
1851                 for (j = 0 ; j < charger->num_cables ; j++) {
1852                         struct charger_cable *cable = &charger->cables[j];
1853                         extcon_unregister_interest(&cable->extcon_dev);
1854                 }
1855         }
1856
1857         for (i = 0 ; i < desc->num_charger_regulators ; i++)
1858                 regulator_put(desc->charger_regulators[i].consumer);
1859
1860         power_supply_unregister(cm->charger_psy);
1861
1862         try_charger_enable(cm, false);
1863
1864         return 0;
1865 }
1866
1867 static const struct platform_device_id charger_manager_id[] = {
1868         { "charger-manager", 0 },
1869         { },
1870 };
1871 MODULE_DEVICE_TABLE(platform, charger_manager_id);
1872
1873 static int cm_suspend_noirq(struct device *dev)
1874 {
1875         if (device_may_wakeup(dev)) {
1876                 device_set_wakeup_capable(dev, false);
1877                 return -EAGAIN;
1878         }
1879
1880         return 0;
1881 }
1882
1883 static bool cm_need_to_awake(void)
1884 {
1885         struct charger_manager *cm;
1886
1887         if (cm_timer)
1888                 return false;
1889
1890         mutex_lock(&cm_list_mtx);
1891         list_for_each_entry(cm, &cm_list, entry) {
1892                 if (is_charging(cm)) {
1893                         mutex_unlock(&cm_list_mtx);
1894                         return true;
1895                 }
1896         }
1897         mutex_unlock(&cm_list_mtx);
1898
1899         return false;
1900 }
1901
1902 static int cm_suspend_prepare(struct device *dev)
1903 {
1904         struct charger_manager *cm = dev_get_drvdata(dev);
1905
1906         if (cm_need_to_awake())
1907                 return -EBUSY;
1908
1909         if (!cm_suspended)
1910                 cm_suspended = true;
1911
1912         cm_timer_set = cm_setup_timer();
1913
1914         if (cm_timer_set) {
1915                 cancel_work_sync(&setup_polling);
1916                 cancel_delayed_work_sync(&cm_monitor_work);
1917                 cancel_delayed_work(&cm->fullbatt_vchk_work);
1918         }
1919
1920         return 0;
1921 }
1922
1923 static void cm_suspend_complete(struct device *dev)
1924 {
1925         struct charger_manager *cm = dev_get_drvdata(dev);
1926
1927         if (cm_suspended)
1928                 cm_suspended = false;
1929
1930         if (cm_timer_set) {
1931                 ktime_t remain;
1932
1933                 alarm_cancel(cm_timer);
1934                 cm_timer_set = false;
1935                 remain = alarm_expires_remaining(cm_timer);
1936                 cm_suspend_duration_ms -= ktime_to_ms(remain);
1937                 schedule_work(&setup_polling);
1938         }
1939
1940         _cm_monitor(cm);
1941
1942         /* Re-enqueue delayed work (fullbatt_vchk_work) */
1943         if (cm->fullbatt_vchk_jiffies_at) {
1944                 unsigned long delay = 0;
1945                 unsigned long now = jiffies + CM_JIFFIES_SMALL;
1946
1947                 if (time_after_eq(now, cm->fullbatt_vchk_jiffies_at)) {
1948                         delay = (unsigned long)((long)now
1949                                 - (long)(cm->fullbatt_vchk_jiffies_at));
1950                         delay = jiffies_to_msecs(delay);
1951                 } else {
1952                         delay = 0;
1953                 }
1954
1955                 /*
1956                  * Account for cm_suspend_duration_ms with assuming that
1957                  * timer stops in suspend.
1958                  */
1959                 if (delay > cm_suspend_duration_ms)
1960                         delay -= cm_suspend_duration_ms;
1961                 else
1962                         delay = 0;
1963
1964                 queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
1965                                    msecs_to_jiffies(delay));
1966         }
1967         device_set_wakeup_capable(cm->dev, false);
1968 }
1969
1970 static const struct dev_pm_ops charger_manager_pm = {
1971         .prepare        = cm_suspend_prepare,
1972         .suspend_noirq  = cm_suspend_noirq,
1973         .complete       = cm_suspend_complete,
1974 };
1975
1976 static struct platform_driver charger_manager_driver = {
1977         .driver = {
1978                 .name = "charger-manager",
1979                 .pm = &charger_manager_pm,
1980                 .of_match_table = charger_manager_match,
1981         },
1982         .probe = charger_manager_probe,
1983         .remove = charger_manager_remove,
1984         .id_table = charger_manager_id,
1985 };
1986
1987 static int __init charger_manager_init(void)
1988 {
1989         cm_wq = create_freezable_workqueue("charger_manager");
1990         INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller);
1991
1992         return platform_driver_register(&charger_manager_driver);
1993 }
1994 late_initcall(charger_manager_init);
1995
1996 static void __exit charger_manager_cleanup(void)
1997 {
1998         destroy_workqueue(cm_wq);
1999         cm_wq = NULL;
2000
2001         platform_driver_unregister(&charger_manager_driver);
2002 }
2003 module_exit(charger_manager_cleanup);
2004
2005 /**
2006  * cm_notify_event - charger driver notify Charger Manager of charger event
2007  * @psy: pointer to instance of charger's power_supply
2008  * @type: type of charger event
2009  * @msg: optional message passed to uevent_notify function
2010  */
2011 void cm_notify_event(struct power_supply *psy, enum cm_event_types type,
2012                      char *msg)
2013 {
2014         struct charger_manager *cm;
2015         bool found_power_supply = false;
2016
2017         if (psy == NULL)
2018                 return;
2019
2020         mutex_lock(&cm_list_mtx);
2021         list_for_each_entry(cm, &cm_list, entry) {
2022                 if (match_string(cm->desc->psy_charger_stat, -1,
2023                                  psy->desc->name) >= 0) {
2024                         found_power_supply = true;
2025                         break;
2026                 }
2027         }
2028         mutex_unlock(&cm_list_mtx);
2029
2030         if (!found_power_supply)
2031                 return;
2032
2033         switch (type) {
2034         case CM_EVENT_BATT_FULL:
2035                 fullbatt_handler(cm);
2036                 break;
2037         case CM_EVENT_BATT_OUT:
2038                 battout_handler(cm);
2039                 break;
2040         case CM_EVENT_BATT_IN:
2041         case CM_EVENT_EXT_PWR_IN_OUT ... CM_EVENT_CHG_START_STOP:
2042                 misc_event_handler(cm, type);
2043                 break;
2044         case CM_EVENT_UNKNOWN:
2045         case CM_EVENT_OTHERS:
2046                 uevent_notify(cm, msg ? msg : default_event_names[type]);
2047                 break;
2048         default:
2049                 dev_err(cm->dev, "%s: type not specified\n", __func__);
2050                 break;
2051         }
2052 }
2053 EXPORT_SYMBOL_GPL(cm_notify_event);
2054
2055 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
2056 MODULE_DESCRIPTION("Charger Manager");
2057 MODULE_LICENSE("GPL");