Version:  2.0.40 2.2.26 2.4.37 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0

Linux/drivers/power/charger-manager.c

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us