blob: a5b86b60d244200327150115cc44bbb25b837fcd [file] [log] [blame]
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001/*
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
Joe Perchese5409cb2013-06-06 18:25:12 -070015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090017#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>
Chanwoo Choi3950c782012-09-21 18:49:37 +090027#include <linux/sysfs.h>
Jonghwa Lee856ee612013-12-18 15:42:35 +090028#include <linux/of.h>
Jonghwa Lee5c49a622013-12-18 15:42:34 +090029#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
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090037
Chanwoo Choidfeccb12012-05-05 06:26:47 -070038static 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",
Jonghwa Lee5c49a622013-12-18 15:42:34 +090043 [CM_EVENT_BATT_OVERHEAT] = "Battery Overheat",
44 [CM_EVENT_BATT_COLD] = "Battery Cold",
Chanwoo Choidfeccb12012-05-05 06:26:47 -070045 [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
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090050/*
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
68static LIST_HEAD(cm_list);
69static DEFINE_MUTEX(cm_list_mtx);
70
71/* About in-suspend (suspend-again) monitoring */
Jonghwa Leec1155c62014-12-19 17:55:13 +090072static struct alarm *cm_timer;
73
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090074static bool cm_suspended;
Jonghwa Leec1155c62014-12-19 17:55:13 +090075static bool cm_timer_set;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090076static unsigned long cm_suspend_duration_ms;
77
Chanwoo Choid829dc72012-05-05 06:24:10 -070078/* About normal (not suspended) monitoring */
79static unsigned long polling_jiffy = ULONG_MAX; /* ULONG_MAX: no polling */
80static unsigned long next_polling; /* Next appointed polling time */
81static struct workqueue_struct *cm_wq; /* init at driver add */
82static struct delayed_work cm_monitor_work; /* init at driver add */
83
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090084/**
85 * is_batt_present - See if the battery presents in place.
86 * @cm: the Charger Manager representing the battery.
87 */
88static bool is_batt_present(struct charger_manager *cm)
89{
90 union power_supply_propval val;
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +020091 struct power_supply *psy;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090092 bool present = false;
93 int i, ret;
94
95 switch (cm->desc->battery_present) {
Chanwoo Choid829dc72012-05-05 06:24:10 -070096 case CM_BATTERY_PRESENT:
97 present = true;
98 break;
99 case CM_NO_BATTERY:
100 break;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900101 case CM_FUEL_GAUGE:
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200102 psy = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
103 if (!psy)
104 break;
105
Krzysztof Kozlowskib70229b2015-03-12 08:44:10 +0100106 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_PRESENT,
107 &val);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900108 if (ret == 0 && val.intval)
109 present = true;
110 break;
111 case CM_CHARGER_STAT:
Krzysztof Kozlowskicdaf3e12014-10-13 15:34:31 +0200112 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
Krzysztof Kozlowskib70229b2015-03-12 08:44:10 +0100121 ret = power_supply_get_property(psy,
122 POWER_SUPPLY_PROP_PRESENT, &val);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900123 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 */
142static bool is_ext_pwr_online(struct charger_manager *cm)
143{
144 union power_supply_propval val;
Krzysztof Kozlowskicdaf3e12014-10-13 15:34:31 +0200145 struct power_supply *psy;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900146 bool online = false;
147 int i, ret;
148
149 /* If at least one of them has one, it's yes. */
Krzysztof Kozlowskicdaf3e12014-10-13 15:34:31 +0200150 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
Krzysztof Kozlowskib70229b2015-03-12 08:44:10 +0100158 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE,
159 &val);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900160 if (ret == 0 && val.intval) {
161 online = true;
162 break;
163 }
164 }
165
166 return online;
167}
168
169/**
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900170 * get_batt_uV - Get the voltage level of the battery
171 * @cm: the Charger Manager representing the battery.
172 * @uV: the voltage level returned.
173 *
174 * Returns 0 if there is no error.
175 * Returns a negative value on error.
176 */
177static int get_batt_uV(struct charger_manager *cm, int *uV)
178{
179 union power_supply_propval val;
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200180 struct power_supply *fuel_gauge;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900181 int ret;
182
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200183 fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
184 if (!fuel_gauge)
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900185 return -ENODEV;
186
Krzysztof Kozlowskib70229b2015-03-12 08:44:10 +0100187 ret = power_supply_get_property(fuel_gauge,
Axel Linbb2a95c2012-01-12 12:56:35 +0800188 POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900189 if (ret)
190 return ret;
191
192 *uV = val.intval;
193 return 0;
194}
195
196/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900197 * is_charging - Returns true if the battery is being charged.
198 * @cm: the Charger Manager representing the battery.
199 */
200static bool is_charging(struct charger_manager *cm)
201{
202 int i, ret;
203 bool charging = false;
Krzysztof Kozlowskicdaf3e12014-10-13 15:34:31 +0200204 struct power_supply *psy;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900205 union power_supply_propval val;
206
207 /* If there is no battery, it cannot be charged */
208 if (!is_batt_present(cm))
209 return false;
210
211 /* If at least one of the charger is charging, return yes */
Krzysztof Kozlowskicdaf3e12014-10-13 15:34:31 +0200212 for (i = 0; cm->desc->psy_charger_stat[i]; i++) {
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900213 /* 1. The charger sholuld not be DISABLED */
214 if (cm->emergency_stop)
215 continue;
216 if (!cm->charger_enabled)
217 continue;
218
Krzysztof Kozlowskicdaf3e12014-10-13 15:34:31 +0200219 psy = power_supply_get_by_name(cm->desc->psy_charger_stat[i]);
220 if (!psy) {
221 dev_err(cm->dev, "Cannot find power supply \"%s\"\n",
222 cm->desc->psy_charger_stat[i]);
223 continue;
224 }
225
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900226 /* 2. The charger should be online (ext-power) */
Krzysztof Kozlowskib70229b2015-03-12 08:44:10 +0100227 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE,
228 &val);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900229 if (ret) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700230 dev_warn(cm->dev, "Cannot read ONLINE value from %s\n",
231 cm->desc->psy_charger_stat[i]);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900232 continue;
233 }
234 if (val.intval == 0)
235 continue;
236
237 /*
238 * 3. The charger should not be FULL, DISCHARGING,
239 * or NOT_CHARGING.
240 */
Krzysztof Kozlowskib70229b2015-03-12 08:44:10 +0100241 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_STATUS,
242 &val);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900243 if (ret) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700244 dev_warn(cm->dev, "Cannot read STATUS value from %s\n",
245 cm->desc->psy_charger_stat[i]);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900246 continue;
247 }
248 if (val.intval == POWER_SUPPLY_STATUS_FULL ||
249 val.intval == POWER_SUPPLY_STATUS_DISCHARGING ||
250 val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING)
251 continue;
252
253 /* Then, this is charging. */
254 charging = true;
255 break;
256 }
257
258 return charging;
259}
260
261/**
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900262 * is_full_charged - Returns true if the battery is fully charged.
263 * @cm: the Charger Manager representing the battery.
264 */
265static bool is_full_charged(struct charger_manager *cm)
266{
267 struct charger_desc *desc = cm->desc;
268 union power_supply_propval val;
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200269 struct power_supply *fuel_gauge;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900270 int ret = 0;
271 int uV;
272
273 /* If there is no battery, it cannot be charged */
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900274 if (!is_batt_present(cm))
275 return false;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900276
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200277 fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
278 if (!fuel_gauge)
279 return false;
280
281 if (desc->fullbatt_full_capacity > 0) {
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900282 val.intval = 0;
283
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900284 /* Not full if capacity of fuel gauge isn't full */
Krzysztof Kozlowskib70229b2015-03-12 08:44:10 +0100285 ret = power_supply_get_property(fuel_gauge,
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900286 POWER_SUPPLY_PROP_CHARGE_FULL, &val);
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900287 if (!ret && val.intval > desc->fullbatt_full_capacity)
288 return true;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900289 }
290
291 /* Full, if it's over the fullbatt voltage */
292 if (desc->fullbatt_uV > 0) {
293 ret = get_batt_uV(cm, &uV);
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900294 if (!ret && uV >= desc->fullbatt_uV)
295 return true;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900296 }
297
298 /* Full, if the capacity is more than fullbatt_soc */
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200299 if (desc->fullbatt_soc > 0) {
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900300 val.intval = 0;
301
Krzysztof Kozlowskib70229b2015-03-12 08:44:10 +0100302 ret = power_supply_get_property(fuel_gauge,
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900303 POWER_SUPPLY_PROP_CAPACITY, &val);
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900304 if (!ret && val.intval >= desc->fullbatt_soc)
305 return true;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900306 }
307
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900308 return false;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900309}
310
311/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900312 * is_polling_required - Return true if need to continue polling for this CM.
313 * @cm: the Charger Manager representing the battery.
314 */
315static bool is_polling_required(struct charger_manager *cm)
316{
317 switch (cm->desc->polling_mode) {
318 case CM_POLL_DISABLE:
319 return false;
320 case CM_POLL_ALWAYS:
321 return true;
322 case CM_POLL_EXTERNAL_POWER_ONLY:
323 return is_ext_pwr_online(cm);
324 case CM_POLL_CHARGING_ONLY:
325 return is_charging(cm);
326 default:
327 dev_warn(cm->dev, "Incorrect polling_mode (%d)\n",
Joe Perchese5409cb2013-06-06 18:25:12 -0700328 cm->desc->polling_mode);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900329 }
330
331 return false;
332}
333
334/**
335 * try_charger_enable - Enable/Disable chargers altogether
336 * @cm: the Charger Manager representing the battery.
337 * @enable: true: enable / false: disable
338 *
339 * Note that Charger Manager keeps the charger enabled regardless whether
340 * the charger is charging or not (because battery is full or no external
341 * power source exists) except when CM needs to disable chargers forcibly
342 * bacause of emergency causes; when the battery is overheated or too cold.
343 */
344static int try_charger_enable(struct charger_manager *cm, bool enable)
345{
346 int err = 0, i;
347 struct charger_desc *desc = cm->desc;
348
349 /* Ignore if it's redundent command */
Axel Linbb2a95c2012-01-12 12:56:35 +0800350 if (enable == cm->charger_enabled)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900351 return 0;
352
353 if (enable) {
354 if (cm->emergency_stop)
355 return -EAGAIN;
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700356
357 /*
358 * Save start time of charging to limit
359 * maximum possible charging time.
360 */
361 cm->charging_start_time = ktime_to_ms(ktime_get());
362 cm->charging_end_time = 0;
363
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900364 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
Chanwoo Choi3950c782012-09-21 18:49:37 +0900365 if (desc->charger_regulators[i].externally_control)
366 continue;
367
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900368 err = regulator_enable(desc->charger_regulators[i].consumer);
369 if (err < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700370 dev_warn(cm->dev, "Cannot enable %s regulator\n",
371 desc->charger_regulators[i].regulator_name);
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900372 }
373 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900374 } else {
375 /*
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700376 * Save end time of charging to maintain fully charged state
377 * of battery after full-batt.
378 */
379 cm->charging_start_time = 0;
380 cm->charging_end_time = ktime_to_ms(ktime_get());
381
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900382 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
Chanwoo Choi3950c782012-09-21 18:49:37 +0900383 if (desc->charger_regulators[i].externally_control)
384 continue;
385
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900386 err = regulator_disable(desc->charger_regulators[i].consumer);
387 if (err < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700388 dev_warn(cm->dev, "Cannot disable %s regulator\n",
389 desc->charger_regulators[i].regulator_name);
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900390 }
391 }
392
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900393 /*
394 * Abnormal battery state - Stop charging forcibly,
395 * even if charger was enabled at the other places
396 */
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900397 for (i = 0; i < desc->num_charger_regulators; i++) {
398 if (regulator_is_enabled(
399 desc->charger_regulators[i].consumer)) {
400 regulator_force_disable(
401 desc->charger_regulators[i].consumer);
Joe Perchese5409cb2013-06-06 18:25:12 -0700402 dev_warn(cm->dev, "Disable regulator(%s) forcibly\n",
403 desc->charger_regulators[i].regulator_name);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900404 }
405 }
406 }
407
408 if (!err)
409 cm->charger_enabled = enable;
410
411 return err;
412}
413
414/**
Chanwoo Choid829dc72012-05-05 06:24:10 -0700415 * try_charger_restart - Restart charging.
416 * @cm: the Charger Manager representing the battery.
417 *
418 * Restart charging by turning off and on the charger.
419 */
420static int try_charger_restart(struct charger_manager *cm)
421{
422 int err;
423
424 if (cm->emergency_stop)
425 return -EAGAIN;
426
427 err = try_charger_enable(cm, false);
428 if (err)
429 return err;
430
431 return try_charger_enable(cm, true);
432}
433
434/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900435 * uevent_notify - Let users know something has changed.
436 * @cm: the Charger Manager representing the battery.
437 * @event: the event string.
438 *
439 * If @event is null, it implies that uevent_notify is called
440 * by resume function. When called in the resume function, cm_suspended
441 * should be already reset to false in order to let uevent_notify
442 * notify the recent event during the suspend to users. While
443 * suspended, uevent_notify does not notify users, but tracks
444 * events so that uevent_notify can notify users later after resumed.
445 */
446static void uevent_notify(struct charger_manager *cm, const char *event)
447{
448 static char env_str[UEVENT_BUF_SIZE + 1] = "";
449 static char env_str_save[UEVENT_BUF_SIZE + 1] = "";
450
451 if (cm_suspended) {
452 /* Nothing in suspended-event buffer */
453 if (env_str_save[0] == 0) {
454 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
455 return; /* status not changed */
456 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
457 return;
458 }
459
460 if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE))
461 return; /* Duplicated. */
Axel Linbb2a95c2012-01-12 12:56:35 +0800462 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900463 return;
464 }
465
466 if (event == NULL) {
467 /* No messages pending */
468 if (!env_str_save[0])
469 return;
470
471 strncpy(env_str, env_str_save, UEVENT_BUF_SIZE);
472 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
473 env_str_save[0] = 0;
474
475 return;
476 }
477
478 /* status not changed */
479 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
480 return;
481
482 /* save the status and notify the update */
483 strncpy(env_str, event, UEVENT_BUF_SIZE);
484 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
485
Joe Perchese5409cb2013-06-06 18:25:12 -0700486 dev_info(cm->dev, "%s\n", event);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900487}
488
489/**
Chanwoo Choid829dc72012-05-05 06:24:10 -0700490 * fullbatt_vchk - Check voltage drop some times after "FULL" event.
491 * @work: the work_struct appointing the function
492 *
493 * If a user has designated "fullbatt_vchkdrop_ms/uV" values with
494 * charger_desc, Charger Manager checks voltage drop after the battery
495 * "FULL" event. It checks whether the voltage has dropped more than
496 * fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms.
497 */
498static void fullbatt_vchk(struct work_struct *work)
499{
500 struct delayed_work *dwork = to_delayed_work(work);
501 struct charger_manager *cm = container_of(dwork,
502 struct charger_manager, fullbatt_vchk_work);
503 struct charger_desc *desc = cm->desc;
504 int batt_uV, err, diff;
505
506 /* remove the appointment for fullbatt_vchk */
507 cm->fullbatt_vchk_jiffies_at = 0;
508
509 if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
510 return;
511
512 err = get_batt_uV(cm, &batt_uV);
513 if (err) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700514 dev_err(cm->dev, "%s: get_batt_uV error(%d)\n", __func__, err);
Chanwoo Choid829dc72012-05-05 06:24:10 -0700515 return;
516 }
517
Chanwoo Choif36b9dd2012-11-22 16:53:51 +0900518 diff = desc->fullbatt_uV - batt_uV;
519 if (diff < 0)
520 return;
Chanwoo Choid829dc72012-05-05 06:24:10 -0700521
Joe Perchese5409cb2013-06-06 18:25:12 -0700522 dev_info(cm->dev, "VBATT dropped %duV after full-batt\n", diff);
Chanwoo Choid829dc72012-05-05 06:24:10 -0700523
524 if (diff > desc->fullbatt_vchkdrop_uV) {
525 try_charger_restart(cm);
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700526 uevent_notify(cm, "Recharging");
Chanwoo Choid829dc72012-05-05 06:24:10 -0700527 }
528}
529
530/**
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700531 * check_charging_duration - Monitor charging/discharging duration
532 * @cm: the Charger Manager representing the battery.
533 *
534 * If whole charging duration exceed 'charging_max_duration_ms',
535 * cm stop charging to prevent overcharge/overheat. If discharging
536 * duration exceed 'discharging _max_duration_ms', charger cable is
537 * attached, after full-batt, cm start charging to maintain fully
538 * charged state for battery.
539 */
540static int check_charging_duration(struct charger_manager *cm)
541{
542 struct charger_desc *desc = cm->desc;
543 u64 curr = ktime_to_ms(ktime_get());
544 u64 duration;
545 int ret = false;
546
547 if (!desc->charging_max_duration_ms &&
548 !desc->discharging_max_duration_ms)
549 return ret;
550
551 if (cm->charger_enabled) {
552 duration = curr - cm->charging_start_time;
553
554 if (duration > desc->charging_max_duration_ms) {
Jonghwa Lee856ee612013-12-18 15:42:35 +0900555 dev_info(cm->dev, "Charging duration exceed %ums\n",
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700556 desc->charging_max_duration_ms);
557 uevent_notify(cm, "Discharging");
558 try_charger_enable(cm, false);
559 ret = true;
560 }
561 } else if (is_ext_pwr_online(cm) && !cm->charger_enabled) {
562 duration = curr - cm->charging_end_time;
563
564 if (duration > desc->charging_max_duration_ms &&
565 is_ext_pwr_online(cm)) {
Jonghwa Lee856ee612013-12-18 15:42:35 +0900566 dev_info(cm->dev, "Discharging duration exceed %ums\n",
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700567 desc->discharging_max_duration_ms);
Joe Perchese5409cb2013-06-06 18:25:12 -0700568 uevent_notify(cm, "Recharging");
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700569 try_charger_enable(cm, true);
570 ret = true;
571 }
572 }
573
574 return ret;
575}
576
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200577static int cm_get_battery_temperature_by_psy(struct charger_manager *cm,
578 int *temp)
579{
580 struct power_supply *fuel_gauge;
581
582 fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
583 if (!fuel_gauge)
584 return -ENODEV;
585
Krzysztof Kozlowskib70229b2015-03-12 08:44:10 +0100586 return power_supply_get_property(fuel_gauge,
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200587 POWER_SUPPLY_PROP_TEMP,
588 (union power_supply_propval *)temp);
589}
590
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900591static int cm_get_battery_temperature(struct charger_manager *cm,
592 int *temp)
593{
594 int ret;
595
596 if (!cm->desc->measure_battery_temp)
597 return -ENODEV;
598
599#ifdef CONFIG_THERMAL
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200600 if (cm->tzd_batt) {
601 ret = thermal_zone_get_temp(cm->tzd_batt, (unsigned long *)temp);
602 if (!ret)
603 /* Calibrate temperature unit */
604 *temp /= 100;
605 } else
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900606#endif
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200607 {
608 /* if-else continued from CONFIG_THERMAL */
609 ret = cm_get_battery_temperature_by_psy(cm, temp);
610 }
611
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900612 return ret;
613}
614
615static int cm_check_thermal_status(struct charger_manager *cm)
616{
617 struct charger_desc *desc = cm->desc;
618 int temp, upper_limit, lower_limit;
619 int ret = 0;
620
621 ret = cm_get_battery_temperature(cm, &temp);
622 if (ret) {
623 /* FIXME:
624 * No information of battery temperature might
625 * occur hazadous result. We have to handle it
626 * depending on battery type.
627 */
628 dev_err(cm->dev, "Failed to get battery temperature\n");
629 return 0;
630 }
631
632 upper_limit = desc->temp_max;
633 lower_limit = desc->temp_min;
634
635 if (cm->emergency_stop) {
636 upper_limit -= desc->temp_diff;
637 lower_limit += desc->temp_diff;
638 }
639
640 if (temp > upper_limit)
641 ret = CM_EVENT_BATT_OVERHEAT;
642 else if (temp < lower_limit)
643 ret = CM_EVENT_BATT_COLD;
644
645 return ret;
646}
647
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700648/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900649 * _cm_monitor - Monitor the temperature and return true for exceptions.
650 * @cm: the Charger Manager representing the battery.
651 *
652 * Returns true if there is an event to notify for the battery.
653 * (True if the status of "emergency_stop" changes)
654 */
655static bool _cm_monitor(struct charger_manager *cm)
656{
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900657 int temp_alrt;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900658
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900659 temp_alrt = cm_check_thermal_status(cm);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900660
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900661 /* It has been stopped already */
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900662 if (temp_alrt && cm->emergency_stop)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900663 return false;
664
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900665 /*
666 * Check temperature whether overheat or cold.
667 * If temperature is out of range normal state, stop charging.
668 */
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900669 if (temp_alrt) {
670 cm->emergency_stop = temp_alrt;
671 if (!try_charger_enable(cm, false))
672 uevent_notify(cm, default_event_names[temp_alrt]);
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900673
674 /*
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700675 * Check whole charging duration and discharing duration
676 * after full-batt.
677 */
678 } else if (!cm->emergency_stop && check_charging_duration(cm)) {
679 dev_dbg(cm->dev,
Joe Perchese5409cb2013-06-06 18:25:12 -0700680 "Charging/Discharging duration is out of range\n");
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700681 /*
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900682 * Check dropped voltage of battery. If battery voltage is more
683 * dropped than fullbatt_vchkdrop_uV after fully charged state,
684 * charger-manager have to recharge battery.
685 */
686 } else if (!cm->emergency_stop && is_ext_pwr_online(cm) &&
687 !cm->charger_enabled) {
688 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
689
690 /*
691 * Check whether fully charged state to protect overcharge
692 * if charger-manager is charging for battery.
693 */
694 } else if (!cm->emergency_stop && is_full_charged(cm) &&
695 cm->charger_enabled) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700696 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged\n");
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900697 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
698
699 try_charger_enable(cm, false);
700
701 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900702 } else {
703 cm->emergency_stop = 0;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900704 if (is_ext_pwr_online(cm)) {
705 if (!try_charger_enable(cm, true))
706 uevent_notify(cm, "CHARGING");
707 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900708 }
709
710 return true;
711}
712
713/**
714 * cm_monitor - Monitor every battery.
715 *
716 * Returns true if there is an event to notify from any of the batteries.
717 * (True if the status of "emergency_stop" changes)
718 */
719static bool cm_monitor(void)
720{
721 bool stop = false;
722 struct charger_manager *cm;
723
724 mutex_lock(&cm_list_mtx);
725
Axel Linbb2a95c2012-01-12 12:56:35 +0800726 list_for_each_entry(cm, &cm_list, entry) {
727 if (_cm_monitor(cm))
728 stop = true;
729 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900730
731 mutex_unlock(&cm_list_mtx);
732
733 return stop;
734}
735
Chanwoo Choid829dc72012-05-05 06:24:10 -0700736/**
737 * _setup_polling - Setup the next instance of polling.
738 * @work: work_struct of the function _setup_polling.
739 */
740static void _setup_polling(struct work_struct *work)
741{
742 unsigned long min = ULONG_MAX;
743 struct charger_manager *cm;
744 bool keep_polling = false;
745 unsigned long _next_polling;
746
747 mutex_lock(&cm_list_mtx);
748
749 list_for_each_entry(cm, &cm_list, entry) {
750 if (is_polling_required(cm) && cm->desc->polling_interval_ms) {
751 keep_polling = true;
752
753 if (min > cm->desc->polling_interval_ms)
754 min = cm->desc->polling_interval_ms;
755 }
756 }
757
758 polling_jiffy = msecs_to_jiffies(min);
759 if (polling_jiffy <= CM_JIFFIES_SMALL)
760 polling_jiffy = CM_JIFFIES_SMALL + 1;
761
762 if (!keep_polling)
763 polling_jiffy = ULONG_MAX;
764 if (polling_jiffy == ULONG_MAX)
765 goto out;
766
767 WARN(cm_wq == NULL, "charger-manager: workqueue not initialized"
768 ". try it later. %s\n", __func__);
769
Tejun Heo2fbb5202012-12-21 17:56:51 -0800770 /*
771 * Use mod_delayed_work() iff the next polling interval should
772 * occur before the currently scheduled one. If @cm_monitor_work
773 * isn't active, the end result is the same, so no need to worry
774 * about stale @next_polling.
775 */
Chanwoo Choid829dc72012-05-05 06:24:10 -0700776 _next_polling = jiffies + polling_jiffy;
777
Tejun Heo2fbb5202012-12-21 17:56:51 -0800778 if (time_before(_next_polling, next_polling)) {
Tejun Heo41f63c52012-08-03 10:30:47 -0700779 mod_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy);
Tejun Heo2fbb5202012-12-21 17:56:51 -0800780 next_polling = _next_polling;
781 } else {
782 if (queue_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy))
783 next_polling = _next_polling;
Chanwoo Choid829dc72012-05-05 06:24:10 -0700784 }
Chanwoo Choid829dc72012-05-05 06:24:10 -0700785out:
786 mutex_unlock(&cm_list_mtx);
787}
788static DECLARE_WORK(setup_polling, _setup_polling);
789
790/**
791 * cm_monitor_poller - The Monitor / Poller.
792 * @work: work_struct of the function cm_monitor_poller
793 *
794 * During non-suspended state, cm_monitor_poller is used to poll and monitor
795 * the batteries.
796 */
797static void cm_monitor_poller(struct work_struct *work)
798{
799 cm_monitor();
800 schedule_work(&setup_polling);
801}
802
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700803/**
804 * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL
805 * @cm: the Charger Manager representing the battery.
806 */
807static void fullbatt_handler(struct charger_manager *cm)
808{
809 struct charger_desc *desc = cm->desc;
810
811 if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
812 goto out;
813
814 if (cm_suspended)
815 device_set_wakeup_capable(cm->dev, true);
816
Tejun Heo41f63c52012-08-03 10:30:47 -0700817 mod_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
818 msecs_to_jiffies(desc->fullbatt_vchkdrop_ms));
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700819 cm->fullbatt_vchk_jiffies_at = jiffies + msecs_to_jiffies(
820 desc->fullbatt_vchkdrop_ms);
821
822 if (cm->fullbatt_vchk_jiffies_at == 0)
823 cm->fullbatt_vchk_jiffies_at = 1;
824
825out:
Joe Perchese5409cb2013-06-06 18:25:12 -0700826 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged\n");
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700827 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
828}
829
830/**
831 * battout_handler - Event handler for CM_EVENT_BATT_OUT
832 * @cm: the Charger Manager representing the battery.
833 */
834static void battout_handler(struct charger_manager *cm)
835{
836 if (cm_suspended)
837 device_set_wakeup_capable(cm->dev, true);
838
839 if (!is_batt_present(cm)) {
840 dev_emerg(cm->dev, "Battery Pulled Out!\n");
841 uevent_notify(cm, default_event_names[CM_EVENT_BATT_OUT]);
842 } else {
843 uevent_notify(cm, "Battery Reinserted?");
844 }
845}
846
847/**
848 * misc_event_handler - Handler for other evnets
849 * @cm: the Charger Manager representing the battery.
850 * @type: the Charger Manager representing the battery.
851 */
852static void misc_event_handler(struct charger_manager *cm,
853 enum cm_event_types type)
854{
855 if (cm_suspended)
856 device_set_wakeup_capable(cm->dev, true);
857
Tejun Heo2fbb5202012-12-21 17:56:51 -0800858 if (is_polling_required(cm) && cm->desc->polling_interval_ms)
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700859 schedule_work(&setup_polling);
860 uevent_notify(cm, default_event_names[type]);
861}
862
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900863static int charger_get_property(struct power_supply *psy,
864 enum power_supply_property psp,
865 union power_supply_propval *val)
866{
867 struct charger_manager *cm = container_of(psy,
868 struct charger_manager, charger_psy);
869 struct charger_desc *desc = cm->desc;
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200870 struct power_supply *fuel_gauge;
Anton Vorontsovdf58c042012-03-15 21:01:28 +0400871 int ret = 0;
872 int uV;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900873
874 switch (psp) {
875 case POWER_SUPPLY_PROP_STATUS:
876 if (is_charging(cm))
877 val->intval = POWER_SUPPLY_STATUS_CHARGING;
878 else if (is_ext_pwr_online(cm))
879 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
880 else
881 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
882 break;
883 case POWER_SUPPLY_PROP_HEALTH:
884 if (cm->emergency_stop > 0)
885 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
886 else if (cm->emergency_stop < 0)
887 val->intval = POWER_SUPPLY_HEALTH_COLD;
888 else
889 val->intval = POWER_SUPPLY_HEALTH_GOOD;
890 break;
891 case POWER_SUPPLY_PROP_PRESENT:
892 if (is_batt_present(cm))
893 val->intval = 1;
894 else
895 val->intval = 0;
896 break;
897 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Anton Vorontsovdf58c042012-03-15 21:01:28 +0400898 ret = get_batt_uV(cm, &val->intval);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900899 break;
900 case POWER_SUPPLY_PROP_CURRENT_NOW:
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200901 fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
902 if (!fuel_gauge) {
903 ret = -ENODEV;
904 break;
905 }
Krzysztof Kozlowskib70229b2015-03-12 08:44:10 +0100906 ret = power_supply_get_property(fuel_gauge,
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900907 POWER_SUPPLY_PROP_CURRENT_NOW, val);
908 break;
909 case POWER_SUPPLY_PROP_TEMP:
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900910 case POWER_SUPPLY_PROP_TEMP_AMBIENT:
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900911 return cm_get_battery_temperature(cm, &val->intval);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900912 case POWER_SUPPLY_PROP_CAPACITY:
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200913 fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
914 if (!fuel_gauge) {
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900915 ret = -ENODEV;
916 break;
917 }
918
919 if (!is_batt_present(cm)) {
920 /* There is no battery. Assume 100% */
921 val->intval = 100;
922 break;
923 }
924
Krzysztof Kozlowskib70229b2015-03-12 08:44:10 +0100925 ret = power_supply_get_property(fuel_gauge,
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900926 POWER_SUPPLY_PROP_CAPACITY, val);
927 if (ret)
928 break;
929
930 if (val->intval > 100) {
931 val->intval = 100;
932 break;
933 }
934 if (val->intval < 0)
935 val->intval = 0;
936
937 /* Do not adjust SOC when charging: voltage is overrated */
938 if (is_charging(cm))
939 break;
940
941 /*
942 * If the capacity value is inconsistent, calibrate it base on
943 * the battery voltage values and the thresholds given as desc
944 */
945 ret = get_batt_uV(cm, &uV);
946 if (ret) {
947 /* Voltage information not available. No calibration */
948 ret = 0;
949 break;
950 }
951
952 if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
953 !is_charging(cm)) {
954 val->intval = 100;
955 break;
956 }
957
958 break;
959 case POWER_SUPPLY_PROP_ONLINE:
960 if (is_ext_pwr_online(cm))
961 val->intval = 1;
962 else
963 val->intval = 0;
964 break;
965 case POWER_SUPPLY_PROP_CHARGE_FULL:
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900966 if (is_full_charged(cm))
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900967 val->intval = 1;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900968 else
969 val->intval = 0;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900970 ret = 0;
971 break;
972 case POWER_SUPPLY_PROP_CHARGE_NOW:
973 if (is_charging(cm)) {
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200974 fuel_gauge = power_supply_get_by_name(
975 cm->desc->psy_fuel_gauge);
976 if (!fuel_gauge) {
977 ret = -ENODEV;
978 break;
979 }
980
Krzysztof Kozlowskib70229b2015-03-12 08:44:10 +0100981 ret = power_supply_get_property(fuel_gauge,
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900982 POWER_SUPPLY_PROP_CHARGE_NOW,
983 val);
984 if (ret) {
985 val->intval = 1;
986 ret = 0;
987 } else {
988 /* If CHARGE_NOW is supplied, use it */
989 val->intval = (val->intval > 0) ?
990 val->intval : 1;
991 }
992 } else {
993 val->intval = 0;
994 }
995 break;
996 default:
997 return -EINVAL;
998 }
999 return ret;
1000}
1001
1002#define NUM_CHARGER_PSY_OPTIONAL (4)
1003static enum power_supply_property default_charger_props[] = {
1004 /* Guaranteed to provide */
1005 POWER_SUPPLY_PROP_STATUS,
1006 POWER_SUPPLY_PROP_HEALTH,
1007 POWER_SUPPLY_PROP_PRESENT,
1008 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1009 POWER_SUPPLY_PROP_CAPACITY,
1010 POWER_SUPPLY_PROP_ONLINE,
1011 POWER_SUPPLY_PROP_CHARGE_FULL,
1012 /*
1013 * Optional properties are:
1014 * POWER_SUPPLY_PROP_CHARGE_NOW,
1015 * POWER_SUPPLY_PROP_CURRENT_NOW,
1016 * POWER_SUPPLY_PROP_TEMP, and
1017 * POWER_SUPPLY_PROP_TEMP_AMBIENT,
1018 */
1019};
1020
1021static struct power_supply psy_default = {
1022 .name = "battery",
1023 .type = POWER_SUPPLY_TYPE_BATTERY,
1024 .properties = default_charger_props,
1025 .num_properties = ARRAY_SIZE(default_charger_props),
1026 .get_property = charger_get_property,
Krzysztof Kozlowskiba9c9182014-10-07 17:47:37 +02001027 .no_thermal = true,
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001028};
1029
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001030/**
1031 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
1032 * for suspend_again.
1033 *
1034 * Returns true if the alarm is set for Charger Manager to use.
1035 * Returns false if
1036 * cm_setup_timer fails to set an alarm,
1037 * cm_setup_timer does not need to set an alarm for Charger Manager,
1038 * or an alarm previously configured is to be used.
1039 */
1040static bool cm_setup_timer(void)
1041{
1042 struct charger_manager *cm;
1043 unsigned int wakeup_ms = UINT_MAX;
Jonghwa Leec1155c62014-12-19 17:55:13 +09001044 int timer_req = 0;
1045
1046 if (time_after(next_polling, jiffies))
1047 CM_MIN_VALID(wakeup_ms,
1048 jiffies_to_msecs(next_polling - jiffies));
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001049
1050 mutex_lock(&cm_list_mtx);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001051 list_for_each_entry(cm, &cm_list, entry) {
Chanwoo Choid829dc72012-05-05 06:24:10 -07001052 unsigned int fbchk_ms = 0;
1053
1054 /* fullbatt_vchk is required. setup timer for that */
1055 if (cm->fullbatt_vchk_jiffies_at) {
1056 fbchk_ms = jiffies_to_msecs(cm->fullbatt_vchk_jiffies_at
1057 - jiffies);
1058 if (time_is_before_eq_jiffies(
1059 cm->fullbatt_vchk_jiffies_at) ||
1060 msecs_to_jiffies(fbchk_ms) < CM_JIFFIES_SMALL) {
1061 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
1062 fbchk_ms = 0;
1063 }
1064 }
1065 CM_MIN_VALID(wakeup_ms, fbchk_ms);
1066
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001067 /* Skip if polling is not required for this CM */
1068 if (!is_polling_required(cm) && !cm->emergency_stop)
1069 continue;
Jonghwa Leec1155c62014-12-19 17:55:13 +09001070 timer_req++;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001071 if (cm->desc->polling_interval_ms == 0)
1072 continue;
1073 CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
1074 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001075 mutex_unlock(&cm_list_mtx);
1076
Jonghwa Leec1155c62014-12-19 17:55:13 +09001077 if (timer_req && cm_timer) {
1078 ktime_t now, add;
1079
1080 /*
1081 * Set alarm with the polling interval (wakeup_ms)
1082 * The alarm time should be NOW + CM_RTC_SMALL or later.
1083 */
1084 if (wakeup_ms == UINT_MAX ||
1085 wakeup_ms < CM_RTC_SMALL * MSEC_PER_SEC)
1086 wakeup_ms = 2 * CM_RTC_SMALL * MSEC_PER_SEC;
1087
Joe Perchese5409cb2013-06-06 18:25:12 -07001088 pr_info("Charger Manager wakeup timer: %u ms\n", wakeup_ms);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001089
Jonghwa Leec1155c62014-12-19 17:55:13 +09001090 now = ktime_get_boottime();
1091 add = ktime_set(wakeup_ms / MSEC_PER_SEC,
1092 (wakeup_ms % MSEC_PER_SEC) * NSEC_PER_MSEC);
1093 alarm_start(cm_timer, ktime_add(now, add));
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001094
Jonghwa Leec1155c62014-12-19 17:55:13 +09001095 cm_suspend_duration_ms = wakeup_ms;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001096
Jonghwa Leec1155c62014-12-19 17:55:13 +09001097 return true;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001098 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001099 return false;
1100}
1101
Chanwoo Choibee737b2012-07-12 15:03:25 +09001102/**
1103 * charger_extcon_work - enable/diable charger according to the state
1104 * of charger cable
1105 *
1106 * @work: work_struct of the function charger_extcon_work.
1107 */
1108static void charger_extcon_work(struct work_struct *work)
1109{
1110 struct charger_cable *cable =
1111 container_of(work, struct charger_cable, wq);
Chanwoo Choi45cd4fb2012-07-12 15:03:29 +09001112 int ret;
1113
1114 if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) {
1115 ret = regulator_set_current_limit(cable->charger->consumer,
1116 cable->min_uA, cable->max_uA);
1117 if (ret < 0) {
1118 pr_err("Cannot set current limit of %s (%s)\n",
Joe Perchese5409cb2013-06-06 18:25:12 -07001119 cable->charger->regulator_name, cable->name);
Chanwoo Choi45cd4fb2012-07-12 15:03:29 +09001120 return;
1121 }
1122
1123 pr_info("Set current limit of %s : %duA ~ %duA\n",
Joe Perchese5409cb2013-06-06 18:25:12 -07001124 cable->charger->regulator_name,
1125 cable->min_uA, cable->max_uA);
Chanwoo Choi45cd4fb2012-07-12 15:03:29 +09001126 }
Chanwoo Choibee737b2012-07-12 15:03:25 +09001127
1128 try_charger_enable(cable->cm, cable->attached);
1129}
1130
1131/**
1132 * charger_extcon_notifier - receive the state of charger cable
1133 * when registered cable is attached or detached.
1134 *
1135 * @self: the notifier block of the charger_extcon_notifier.
1136 * @event: the cable state.
1137 * @ptr: the data pointer of notifier block.
1138 */
1139static int charger_extcon_notifier(struct notifier_block *self,
1140 unsigned long event, void *ptr)
1141{
1142 struct charger_cable *cable =
1143 container_of(self, struct charger_cable, nb);
1144
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001145 /*
1146 * The newly state of charger cable.
1147 * If cable is attached, cable->attached is true.
1148 */
Chanwoo Choibee737b2012-07-12 15:03:25 +09001149 cable->attached = event;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001150
1151 /*
1152 * Setup monitoring to check battery state
1153 * when charger cable is attached.
1154 */
1155 if (cable->attached && is_polling_required(cable->cm)) {
Tejun Heo2fbb5202012-12-21 17:56:51 -08001156 cancel_work_sync(&setup_polling);
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001157 schedule_work(&setup_polling);
1158 }
1159
1160 /*
1161 * Setup work for controlling charger(regulator)
1162 * according to charger cable.
1163 */
Chanwoo Choibee737b2012-07-12 15:03:25 +09001164 schedule_work(&cable->wq);
1165
1166 return NOTIFY_DONE;
1167}
1168
1169/**
1170 * charger_extcon_init - register external connector to use it
1171 * as the charger cable
1172 *
1173 * @cm: the Charger Manager representing the battery.
1174 * @cable: the Charger cable representing the external connector.
1175 */
1176static int charger_extcon_init(struct charger_manager *cm,
1177 struct charger_cable *cable)
1178{
1179 int ret = 0;
1180
1181 /*
1182 * Charger manager use Extcon framework to identify
1183 * the charger cable among various external connector
1184 * cable (e.g., TA, USB, MHL, Dock).
1185 */
1186 INIT_WORK(&cable->wq, charger_extcon_work);
1187 cable->nb.notifier_call = charger_extcon_notifier;
1188 ret = extcon_register_interest(&cable->extcon_dev,
1189 cable->extcon_name, cable->name, &cable->nb);
1190 if (ret < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001191 pr_info("Cannot register extcon_dev for %s(cable: %s)\n",
1192 cable->extcon_name, cable->name);
Chanwoo Choibee737b2012-07-12 15:03:25 +09001193 ret = -EINVAL;
1194 }
1195
1196 return ret;
1197}
1198
Chanwoo Choi41468a12012-11-22 16:54:26 +09001199/**
1200 * charger_manager_register_extcon - Register extcon device to recevie state
1201 * of charger cable.
1202 * @cm: the Charger Manager representing the battery.
1203 *
1204 * This function support EXTCON(External Connector) subsystem to detect the
1205 * state of charger cables for enabling or disabling charger(regulator) and
1206 * select the charger cable for charging among a number of external cable
1207 * according to policy of H/W board.
1208 */
1209static int charger_manager_register_extcon(struct charger_manager *cm)
1210{
1211 struct charger_desc *desc = cm->desc;
1212 struct charger_regulator *charger;
1213 int ret = 0;
1214 int i;
1215 int j;
1216
1217 for (i = 0; i < desc->num_charger_regulators; i++) {
1218 charger = &desc->charger_regulators[i];
1219
1220 charger->consumer = regulator_get(cm->dev,
1221 charger->regulator_name);
Jonghwa Lee5a6c2202013-06-25 14:18:14 +09001222 if (IS_ERR(charger->consumer)) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001223 dev_err(cm->dev, "Cannot find charger(%s)\n",
1224 charger->regulator_name);
Jonghwa Lee5a6c2202013-06-25 14:18:14 +09001225 return PTR_ERR(charger->consumer);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001226 }
1227 charger->cm = cm;
1228
1229 for (j = 0; j < charger->num_cables; j++) {
1230 struct charger_cable *cable = &charger->cables[j];
1231
1232 ret = charger_extcon_init(cm, cable);
1233 if (ret < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001234 dev_err(cm->dev, "Cannot initialize charger(%s)\n",
1235 charger->regulator_name);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001236 goto err;
1237 }
1238 cable->charger = charger;
1239 cable->cm = cm;
1240 }
1241 }
1242
1243err:
1244 return ret;
1245}
1246
Chanwoo Choi3950c782012-09-21 18:49:37 +09001247/* help function of sysfs node to control charger(regulator) */
1248static ssize_t charger_name_show(struct device *dev,
1249 struct device_attribute *attr, char *buf)
1250{
1251 struct charger_regulator *charger
1252 = container_of(attr, struct charger_regulator, attr_name);
1253
1254 return sprintf(buf, "%s\n", charger->regulator_name);
1255}
1256
1257static ssize_t charger_state_show(struct device *dev,
1258 struct device_attribute *attr, char *buf)
1259{
1260 struct charger_regulator *charger
1261 = container_of(attr, struct charger_regulator, attr_state);
1262 int state = 0;
1263
1264 if (!charger->externally_control)
1265 state = regulator_is_enabled(charger->consumer);
1266
1267 return sprintf(buf, "%s\n", state ? "enabled" : "disabled");
1268}
1269
1270static ssize_t charger_externally_control_show(struct device *dev,
1271 struct device_attribute *attr, char *buf)
1272{
1273 struct charger_regulator *charger = container_of(attr,
1274 struct charger_regulator, attr_externally_control);
1275
1276 return sprintf(buf, "%d\n", charger->externally_control);
1277}
1278
1279static ssize_t charger_externally_control_store(struct device *dev,
1280 struct device_attribute *attr, const char *buf,
1281 size_t count)
1282{
1283 struct charger_regulator *charger
1284 = container_of(attr, struct charger_regulator,
1285 attr_externally_control);
1286 struct charger_manager *cm = charger->cm;
1287 struct charger_desc *desc = cm->desc;
1288 int i;
1289 int ret;
1290 int externally_control;
1291 int chargers_externally_control = 1;
1292
1293 ret = sscanf(buf, "%d", &externally_control);
1294 if (ret == 0) {
1295 ret = -EINVAL;
1296 return ret;
1297 }
1298
1299 if (!externally_control) {
1300 charger->externally_control = 0;
1301 return count;
1302 }
1303
1304 for (i = 0; i < desc->num_charger_regulators; i++) {
1305 if (&desc->charger_regulators[i] != charger &&
Chanwoo Choi41468a12012-11-22 16:54:26 +09001306 !desc->charger_regulators[i].externally_control) {
Chanwoo Choi3950c782012-09-21 18:49:37 +09001307 /*
1308 * At least, one charger is controlled by
1309 * charger-manager
1310 */
1311 chargers_externally_control = 0;
1312 break;
1313 }
1314 }
1315
1316 if (!chargers_externally_control) {
1317 if (cm->charger_enabled) {
1318 try_charger_enable(charger->cm, false);
1319 charger->externally_control = externally_control;
1320 try_charger_enable(charger->cm, true);
1321 } else {
1322 charger->externally_control = externally_control;
1323 }
1324 } else {
1325 dev_warn(cm->dev,
Joe Perchese5409cb2013-06-06 18:25:12 -07001326 "'%s' regulator should be controlled in charger-manager because charger-manager must need at least one charger for charging\n",
1327 charger->regulator_name);
Chanwoo Choi3950c782012-09-21 18:49:37 +09001328 }
1329
1330 return count;
1331}
1332
Chanwoo Choi41468a12012-11-22 16:54:26 +09001333/**
1334 * charger_manager_register_sysfs - Register sysfs entry for each charger
1335 * @cm: the Charger Manager representing the battery.
1336 *
1337 * This function add sysfs entry for charger(regulator) to control charger from
1338 * user-space. If some development board use one more chargers for charging
1339 * but only need one charger on specific case which is dependent on user
1340 * scenario or hardware restrictions, the user enter 1 or 0(zero) to '/sys/
1341 * class/power_supply/battery/charger.[index]/externally_control'. For example,
1342 * if user enter 1 to 'sys/class/power_supply/battery/charger.[index]/
1343 * externally_control, this charger isn't controlled from charger-manager and
1344 * always stay off state of regulator.
1345 */
1346static int charger_manager_register_sysfs(struct charger_manager *cm)
1347{
1348 struct charger_desc *desc = cm->desc;
1349 struct charger_regulator *charger;
1350 int chargers_externally_control = 1;
1351 char buf[11];
1352 char *str;
1353 int ret = 0;
1354 int i;
1355
1356 /* Create sysfs entry to control charger(regulator) */
1357 for (i = 0; i < desc->num_charger_regulators; i++) {
1358 charger = &desc->charger_regulators[i];
1359
1360 snprintf(buf, 10, "charger.%d", i);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001361 str = devm_kzalloc(cm->dev,
1362 sizeof(char) * (strlen(buf) + 1), GFP_KERNEL);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001363 if (!str) {
Chanwoo Choi41468a12012-11-22 16:54:26 +09001364 ret = -ENOMEM;
1365 goto err;
1366 }
1367 strcpy(str, buf);
1368
1369 charger->attrs[0] = &charger->attr_name.attr;
1370 charger->attrs[1] = &charger->attr_state.attr;
1371 charger->attrs[2] = &charger->attr_externally_control.attr;
1372 charger->attrs[3] = NULL;
1373 charger->attr_g.name = str;
1374 charger->attr_g.attrs = charger->attrs;
1375
1376 sysfs_attr_init(&charger->attr_name.attr);
1377 charger->attr_name.attr.name = "name";
1378 charger->attr_name.attr.mode = 0444;
1379 charger->attr_name.show = charger_name_show;
1380
1381 sysfs_attr_init(&charger->attr_state.attr);
1382 charger->attr_state.attr.name = "state";
1383 charger->attr_state.attr.mode = 0444;
1384 charger->attr_state.show = charger_state_show;
1385
1386 sysfs_attr_init(&charger->attr_externally_control.attr);
1387 charger->attr_externally_control.attr.name
1388 = "externally_control";
1389 charger->attr_externally_control.attr.mode = 0644;
1390 charger->attr_externally_control.show
1391 = charger_externally_control_show;
1392 charger->attr_externally_control.store
1393 = charger_externally_control_store;
1394
1395 if (!desc->charger_regulators[i].externally_control ||
1396 !chargers_externally_control)
1397 chargers_externally_control = 0;
1398
Joe Perchese5409cb2013-06-06 18:25:12 -07001399 dev_info(cm->dev, "'%s' regulator's externally_control is %d\n",
1400 charger->regulator_name, charger->externally_control);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001401
1402 ret = sysfs_create_group(&cm->charger_psy.dev->kobj,
1403 &charger->attr_g);
1404 if (ret < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001405 dev_err(cm->dev, "Cannot create sysfs entry of %s regulator\n",
1406 charger->regulator_name);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001407 ret = -EINVAL;
1408 goto err;
1409 }
1410 }
1411
1412 if (chargers_externally_control) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001413 dev_err(cm->dev, "Cannot register regulator because charger-manager must need at least one charger for charging battery\n");
Chanwoo Choi41468a12012-11-22 16:54:26 +09001414 ret = -EINVAL;
1415 goto err;
1416 }
1417
1418err:
1419 return ret;
1420}
1421
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +02001422static int cm_init_thermal_data(struct charger_manager *cm,
1423 struct power_supply *fuel_gauge)
Jonghwa Lee5c49a622013-12-18 15:42:34 +09001424{
1425 struct charger_desc *desc = cm->desc;
1426 union power_supply_propval val;
1427 int ret;
1428
1429 /* Verify whether fuel gauge provides battery temperature */
Krzysztof Kozlowskib70229b2015-03-12 08:44:10 +01001430 ret = power_supply_get_property(fuel_gauge,
Jonghwa Lee5c49a622013-12-18 15:42:34 +09001431 POWER_SUPPLY_PROP_TEMP, &val);
1432
1433 if (!ret) {
1434 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1435 POWER_SUPPLY_PROP_TEMP;
1436 cm->charger_psy.num_properties++;
1437 cm->desc->measure_battery_temp = true;
1438 }
1439#ifdef CONFIG_THERMAL
Jonghwa Lee5c49a622013-12-18 15:42:34 +09001440 if (ret && desc->thermal_zone) {
1441 cm->tzd_batt =
1442 thermal_zone_get_zone_by_name(desc->thermal_zone);
1443 if (IS_ERR(cm->tzd_batt))
1444 return PTR_ERR(cm->tzd_batt);
1445
1446 /* Use external thermometer */
1447 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1448 POWER_SUPPLY_PROP_TEMP_AMBIENT;
1449 cm->charger_psy.num_properties++;
1450 cm->desc->measure_battery_temp = true;
1451 ret = 0;
1452 }
1453#endif
1454 if (cm->desc->measure_battery_temp) {
1455 /* NOTICE : Default allowable minimum charge temperature is 0 */
1456 if (!desc->temp_max)
1457 desc->temp_max = CM_DEFAULT_CHARGE_TEMP_MAX;
1458 if (!desc->temp_diff)
1459 desc->temp_diff = CM_DEFAULT_RECHARGE_TEMP_DIFF;
1460 }
1461
1462 return ret;
1463}
1464
Jonghwa Lee856ee612013-12-18 15:42:35 +09001465static struct of_device_id charger_manager_match[] = {
1466 {
1467 .compatible = "charger-manager",
1468 },
1469 {},
1470};
1471
Anton Vorontsov434a09f2013-12-23 18:17:05 -08001472static struct charger_desc *of_cm_parse_desc(struct device *dev)
Jonghwa Lee856ee612013-12-18 15:42:35 +09001473{
1474 struct charger_desc *desc;
1475 struct device_node *np = dev->of_node;
1476 u32 poll_mode = CM_POLL_DISABLE;
1477 u32 battery_stat = CM_NO_BATTERY;
1478 int num_chgs = 0;
1479
1480 desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
1481 if (!desc)
1482 return ERR_PTR(-ENOMEM);
1483
1484 of_property_read_string(np, "cm-name", &desc->psy_name);
1485
1486 of_property_read_u32(np, "cm-poll-mode", &poll_mode);
1487 desc->polling_mode = poll_mode;
1488
1489 of_property_read_u32(np, "cm-poll-interval",
1490 &desc->polling_interval_ms);
1491
1492 of_property_read_u32(np, "cm-fullbatt-vchkdrop-ms",
1493 &desc->fullbatt_vchkdrop_ms);
1494 of_property_read_u32(np, "cm-fullbatt-vchkdrop-volt",
1495 &desc->fullbatt_vchkdrop_uV);
1496 of_property_read_u32(np, "cm-fullbatt-voltage", &desc->fullbatt_uV);
1497 of_property_read_u32(np, "cm-fullbatt-soc", &desc->fullbatt_soc);
1498 of_property_read_u32(np, "cm-fullbatt-capacity",
1499 &desc->fullbatt_full_capacity);
1500
1501 of_property_read_u32(np, "cm-battery-stat", &battery_stat);
1502 desc->battery_present = battery_stat;
1503
1504 /* chargers */
1505 of_property_read_u32(np, "cm-num-chargers", &num_chgs);
1506 if (num_chgs) {
1507 /* Allocate empty bin at the tail of array */
1508 desc->psy_charger_stat = devm_kzalloc(dev, sizeof(char *)
1509 * (num_chgs + 1), GFP_KERNEL);
1510 if (desc->psy_charger_stat) {
1511 int i;
1512 for (i = 0; i < num_chgs; i++)
1513 of_property_read_string_index(np, "cm-chargers",
1514 i, &desc->psy_charger_stat[i]);
1515 } else {
1516 return ERR_PTR(-ENOMEM);
1517 }
1518 }
1519
1520 of_property_read_string(np, "cm-fuel-gauge", &desc->psy_fuel_gauge);
1521
1522 of_property_read_string(np, "cm-thermal-zone", &desc->thermal_zone);
1523
1524 of_property_read_u32(np, "cm-battery-cold", &desc->temp_min);
1525 if (of_get_property(np, "cm-battery-cold-in-minus", NULL))
1526 desc->temp_min *= -1;
1527 of_property_read_u32(np, "cm-battery-hot", &desc->temp_max);
1528 of_property_read_u32(np, "cm-battery-temp-diff", &desc->temp_diff);
1529
1530 of_property_read_u32(np, "cm-charging-max",
1531 &desc->charging_max_duration_ms);
1532 of_property_read_u32(np, "cm-discharging-max",
1533 &desc->discharging_max_duration_ms);
1534
1535 /* battery charger regualtors */
1536 desc->num_charger_regulators = of_get_child_count(np);
1537 if (desc->num_charger_regulators) {
1538 struct charger_regulator *chg_regs;
1539 struct device_node *child;
1540
1541 chg_regs = devm_kzalloc(dev, sizeof(*chg_regs)
1542 * desc->num_charger_regulators,
1543 GFP_KERNEL);
1544 if (!chg_regs)
1545 return ERR_PTR(-ENOMEM);
1546
1547 desc->charger_regulators = chg_regs;
1548
1549 for_each_child_of_node(np, child) {
1550 struct charger_cable *cables;
1551 struct device_node *_child;
1552
1553 of_property_read_string(child, "cm-regulator-name",
1554 &chg_regs->regulator_name);
1555
1556 /* charger cables */
1557 chg_regs->num_cables = of_get_child_count(child);
1558 if (chg_regs->num_cables) {
1559 cables = devm_kzalloc(dev, sizeof(*cables)
1560 * chg_regs->num_cables,
1561 GFP_KERNEL);
1562 if (!cables)
1563 return ERR_PTR(-ENOMEM);
1564
1565 chg_regs->cables = cables;
1566
1567 for_each_child_of_node(child, _child) {
1568 of_property_read_string(_child,
1569 "cm-cable-name", &cables->name);
1570 of_property_read_string(_child,
1571 "cm-cable-extcon",
1572 &cables->extcon_name);
1573 of_property_read_u32(_child,
1574 "cm-cable-min",
1575 &cables->min_uA);
1576 of_property_read_u32(_child,
1577 "cm-cable-max",
1578 &cables->max_uA);
1579 cables++;
1580 }
1581 }
1582 chg_regs++;
1583 }
1584 }
1585 return desc;
1586}
1587
1588static inline struct charger_desc *cm_get_drv_data(struct platform_device *pdev)
1589{
1590 if (pdev->dev.of_node)
1591 return of_cm_parse_desc(&pdev->dev);
Jingoo Han86515b72014-08-29 12:45:27 +09001592 return dev_get_platdata(&pdev->dev);
Jonghwa Lee856ee612013-12-18 15:42:35 +09001593}
1594
Jonghwa Leec1155c62014-12-19 17:55:13 +09001595static enum alarmtimer_restart cm_timer_func(struct alarm *alarm, ktime_t now)
1596{
1597 cm_timer_set = false;
1598 return ALARMTIMER_NORESTART;
1599}
1600
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001601static int charger_manager_probe(struct platform_device *pdev)
1602{
Jonghwa Lee856ee612013-12-18 15:42:35 +09001603 struct charger_desc *desc = cm_get_drv_data(pdev);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001604 struct charger_manager *cm;
1605 int ret = 0, i = 0;
Chanwoo Choibee737b2012-07-12 15:03:25 +09001606 int j = 0;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001607 union power_supply_propval val;
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +02001608 struct power_supply *fuel_gauge;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001609
Chanwoo Choic6738d02014-08-26 13:41:38 +09001610 if (IS_ERR(desc)) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001611 dev_err(&pdev->dev, "No platform data (desc) found\n");
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001612 return -ENODEV;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001613 }
1614
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001615 cm = devm_kzalloc(&pdev->dev,
1616 sizeof(struct charger_manager), GFP_KERNEL);
1617 if (!cm)
1618 return -ENOMEM;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001619
1620 /* Basic Values. Unspecified are Null or 0 */
1621 cm->dev = &pdev->dev;
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001622 cm->desc = desc;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001623
Jonghwa Leec1155c62014-12-19 17:55:13 +09001624 /* Initialize alarm timer */
1625 if (alarmtimer_get_rtcdev()) {
1626 cm_timer = devm_kzalloc(cm->dev, sizeof(*cm_timer), GFP_KERNEL);
1627 alarm_init(cm_timer, ALARM_BOOTTIME, cm_timer_func);
1628 }
1629
Chanwoo Choid829dc72012-05-05 06:24:10 -07001630 /*
1631 * The following two do not need to be errors.
1632 * Users may intentionally ignore those two features.
1633 */
1634 if (desc->fullbatt_uV == 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001635 dev_info(&pdev->dev, "Ignoring full-battery voltage threshold as it is not supplied\n");
Chanwoo Choid829dc72012-05-05 06:24:10 -07001636 }
1637 if (!desc->fullbatt_vchkdrop_ms || !desc->fullbatt_vchkdrop_uV) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001638 dev_info(&pdev->dev, "Disabling full-battery voltage drop checking mechanism as it is not supplied\n");
Chanwoo Choid829dc72012-05-05 06:24:10 -07001639 desc->fullbatt_vchkdrop_ms = 0;
1640 desc->fullbatt_vchkdrop_uV = 0;
1641 }
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001642 if (desc->fullbatt_soc == 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001643 dev_info(&pdev->dev, "Ignoring full-battery soc(state of charge) threshold as it is not supplied\n");
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001644 }
1645 if (desc->fullbatt_full_capacity == 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001646 dev_info(&pdev->dev, "Ignoring full-battery full capacity threshold as it is not supplied\n");
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001647 }
Chanwoo Choid829dc72012-05-05 06:24:10 -07001648
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001649 if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001650 dev_err(&pdev->dev, "charger_regulators undefined\n");
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001651 return -EINVAL;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001652 }
1653
1654 if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001655 dev_err(&pdev->dev, "No power supply defined\n");
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001656 return -EINVAL;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001657 }
1658
Krzysztof Kozlowski661a8882014-09-26 13:27:03 +02001659 if (!desc->psy_fuel_gauge) {
1660 dev_err(&pdev->dev, "No fuel gauge power supply defined\n");
1661 return -EINVAL;
1662 }
1663
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001664 /* Counting index only */
1665 while (desc->psy_charger_stat[i])
1666 i++;
1667
Krzysztof Kozlowskicdaf3e12014-10-13 15:34:31 +02001668 /* Check if charger's supplies are present at probe */
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001669 for (i = 0; desc->psy_charger_stat[i]; i++) {
Krzysztof Kozlowskicdaf3e12014-10-13 15:34:31 +02001670 struct power_supply *psy;
1671
1672 psy = power_supply_get_by_name(desc->psy_charger_stat[i]);
1673 if (!psy) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001674 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
1675 desc->psy_charger_stat[i]);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001676 return -ENODEV;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001677 }
1678 }
1679
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +02001680 fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
1681 if (!fuel_gauge) {
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001682 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
Joe Perchese5409cb2013-06-06 18:25:12 -07001683 desc->psy_fuel_gauge);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001684 return -ENODEV;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001685 }
1686
1687 if (desc->polling_interval_ms == 0 ||
1688 msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) {
1689 dev_err(&pdev->dev, "polling_interval_ms is too small\n");
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001690 return -EINVAL;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001691 }
1692
Chanwoo Choi8fcfe082012-09-20 21:20:05 -07001693 if (!desc->charging_max_duration_ms ||
1694 !desc->discharging_max_duration_ms) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001695 dev_info(&pdev->dev, "Cannot limit charging duration checking mechanism to prevent overcharge/overheat and control discharging duration\n");
Chanwoo Choi8fcfe082012-09-20 21:20:05 -07001696 desc->charging_max_duration_ms = 0;
1697 desc->discharging_max_duration_ms = 0;
1698 }
1699
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001700 platform_set_drvdata(pdev, cm);
1701
Axel Linbb2a95c2012-01-12 12:56:35 +08001702 memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default));
1703
Chanwoo Choi41468a12012-11-22 16:54:26 +09001704 if (!desc->psy_name)
Axel Linbb2a95c2012-01-12 12:56:35 +08001705 strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001706 else
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001707 strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001708 cm->charger_psy.name = cm->psy_name_buf;
1709
1710 /* Allocate for psy properties because they may vary */
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001711 cm->charger_psy.properties = devm_kzalloc(&pdev->dev,
1712 sizeof(enum power_supply_property)
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001713 * (ARRAY_SIZE(default_charger_props) +
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001714 NUM_CHARGER_PSY_OPTIONAL), GFP_KERNEL);
1715 if (!cm->charger_psy.properties)
1716 return -ENOMEM;
1717
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001718 memcpy(cm->charger_psy.properties, default_charger_props,
1719 sizeof(enum power_supply_property) *
1720 ARRAY_SIZE(default_charger_props));
1721 cm->charger_psy.num_properties = psy_default.num_properties;
1722
1723 /* Find which optional psy-properties are available */
Krzysztof Kozlowskib70229b2015-03-12 08:44:10 +01001724 if (!power_supply_get_property(fuel_gauge,
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001725 POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
1726 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1727 POWER_SUPPLY_PROP_CHARGE_NOW;
1728 cm->charger_psy.num_properties++;
1729 }
Krzysztof Kozlowskib70229b2015-03-12 08:44:10 +01001730 if (!power_supply_get_property(fuel_gauge,
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001731 POWER_SUPPLY_PROP_CURRENT_NOW,
1732 &val)) {
1733 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1734 POWER_SUPPLY_PROP_CURRENT_NOW;
1735 cm->charger_psy.num_properties++;
1736 }
Axel Linbb2a95c2012-01-12 12:56:35 +08001737
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +02001738 ret = cm_init_thermal_data(cm, fuel_gauge);
Jonghwa Lee5c49a622013-12-18 15:42:34 +09001739 if (ret) {
1740 dev_err(&pdev->dev, "Failed to initialize thermal data\n");
1741 cm->desc->measure_battery_temp = false;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001742 }
1743
Chanwoo Choid829dc72012-05-05 06:24:10 -07001744 INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk);
1745
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +01001746 ret = power_supply_register(NULL, &cm->charger_psy, NULL);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001747 if (ret) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001748 dev_err(&pdev->dev, "Cannot register charger-manager with name \"%s\"\n",
1749 cm->charger_psy.name);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001750 return ret;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001751 }
1752
Chanwoo Choi41468a12012-11-22 16:54:26 +09001753 /* Register extcon device for charger cable */
1754 ret = charger_manager_register_extcon(cm);
1755 if (ret < 0) {
1756 dev_err(&pdev->dev, "Cannot initialize extcon device\n");
1757 goto err_reg_extcon;
Chanwoo Choi3950c782012-09-21 18:49:37 +09001758 }
1759
Chanwoo Choi41468a12012-11-22 16:54:26 +09001760 /* Register sysfs entry for charger(regulator) */
1761 ret = charger_manager_register_sysfs(cm);
1762 if (ret < 0) {
1763 dev_err(&pdev->dev,
1764 "Cannot initialize sysfs entry of regulator\n");
1765 goto err_reg_sysfs;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001766 }
1767
1768 /* Add to the list */
1769 mutex_lock(&cm_list_mtx);
1770 list_add(&cm->entry, &cm_list);
1771 mutex_unlock(&cm_list_mtx);
1772
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001773 /*
1774 * Charger-manager is capable of waking up the systme from sleep
1775 * when event is happend through cm_notify_event()
1776 */
1777 device_init_wakeup(&pdev->dev, true);
1778 device_set_wakeup_capable(&pdev->dev, false);
1779
Chanwoo Choib1022e22014-08-26 13:41:39 +09001780 /*
1781 * Charger-manager have to check the charging state right after
1782 * tialization of charger-manager and then update current charging
1783 * state.
1784 */
1785 cm_monitor();
1786
Chanwoo Choid829dc72012-05-05 06:24:10 -07001787 schedule_work(&setup_polling);
1788
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001789 return 0;
1790
Chanwoo Choi41468a12012-11-22 16:54:26 +09001791err_reg_sysfs:
Chanwoo Choi3950c782012-09-21 18:49:37 +09001792 for (i = 0; i < desc->num_charger_regulators; i++) {
1793 struct charger_regulator *charger;
1794
1795 charger = &desc->charger_regulators[i];
1796 sysfs_remove_group(&cm->charger_psy.dev->kobj,
1797 &charger->attr_g);
Chanwoo Choi3950c782012-09-21 18:49:37 +09001798 }
Chanwoo Choi41468a12012-11-22 16:54:26 +09001799err_reg_extcon:
1800 for (i = 0; i < desc->num_charger_regulators; i++) {
1801 struct charger_regulator *charger;
1802
1803 charger = &desc->charger_regulators[i];
1804 for (j = 0; j < charger->num_cables; j++) {
Chanwoo Choibee737b2012-07-12 15:03:25 +09001805 struct charger_cable *cable = &charger->cables[j];
Jonghwa Lee3cc9d2692013-06-25 14:02:49 +09001806 /* Remove notifier block if only edev exists */
1807 if (cable->extcon_dev.edev)
1808 extcon_unregister_interest(&cable->extcon_dev);
Chanwoo Choibee737b2012-07-12 15:03:25 +09001809 }
Chanwoo Choi41468a12012-11-22 16:54:26 +09001810
Chanwoo Choibee737b2012-07-12 15:03:25 +09001811 regulator_put(desc->charger_regulators[i].consumer);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001812 }
Chanwoo Choibee737b2012-07-12 15:03:25 +09001813
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001814 power_supply_unregister(&cm->charger_psy);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001815
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001816 return ret;
1817}
1818
Bill Pemberton415ec692012-11-19 13:26:07 -05001819static int charger_manager_remove(struct platform_device *pdev)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001820{
1821 struct charger_manager *cm = platform_get_drvdata(pdev);
1822 struct charger_desc *desc = cm->desc;
Chanwoo Choibee737b2012-07-12 15:03:25 +09001823 int i = 0;
1824 int j = 0;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001825
1826 /* Remove from the list */
1827 mutex_lock(&cm_list_mtx);
1828 list_del(&cm->entry);
1829 mutex_unlock(&cm_list_mtx);
1830
Tejun Heo2fbb5202012-12-21 17:56:51 -08001831 cancel_work_sync(&setup_polling);
1832 cancel_delayed_work_sync(&cm_monitor_work);
Chanwoo Choid829dc72012-05-05 06:24:10 -07001833
Chanwoo Choibee737b2012-07-12 15:03:25 +09001834 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1835 struct charger_regulator *charger
1836 = &desc->charger_regulators[i];
1837 for (j = 0 ; j < charger->num_cables ; j++) {
1838 struct charger_cable *cable = &charger->cables[j];
1839 extcon_unregister_interest(&cable->extcon_dev);
1840 }
1841 }
1842
1843 for (i = 0 ; i < desc->num_charger_regulators ; i++)
1844 regulator_put(desc->charger_regulators[i].consumer);
1845
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001846 power_supply_unregister(&cm->charger_psy);
Chanwoo Choid829dc72012-05-05 06:24:10 -07001847
1848 try_charger_enable(cm, false);
1849
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001850 return 0;
1851}
1852
Axel Lin1bbe24d2012-01-11 17:19:45 +08001853static const struct platform_device_id charger_manager_id[] = {
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001854 { "charger-manager", 0 },
1855 { },
1856};
Axel Lin1bbe24d2012-01-11 17:19:45 +08001857MODULE_DEVICE_TABLE(platform, charger_manager_id);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001858
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001859static int cm_suspend_noirq(struct device *dev)
1860{
1861 int ret = 0;
1862
1863 if (device_may_wakeup(dev)) {
1864 device_set_wakeup_capable(dev, false);
1865 ret = -EAGAIN;
1866 }
1867
1868 return ret;
1869}
1870
Jonghwa Leec1155c62014-12-19 17:55:13 +09001871static bool cm_need_to_awake(void)
1872{
1873 struct charger_manager *cm;
1874
1875 if (cm_timer)
1876 return false;
1877
1878 mutex_lock(&cm_list_mtx);
1879 list_for_each_entry(cm, &cm_list, entry) {
1880 if (is_charging(cm)) {
1881 mutex_unlock(&cm_list_mtx);
1882 return true;
1883 }
1884 }
1885 mutex_unlock(&cm_list_mtx);
1886
1887 return false;
1888}
1889
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001890static int cm_suspend_prepare(struct device *dev)
1891{
Axel Linbb2a95c2012-01-12 12:56:35 +08001892 struct charger_manager *cm = dev_get_drvdata(dev);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001893
Jonghwa Leec1155c62014-12-19 17:55:13 +09001894 if (cm_need_to_awake())
1895 return -EBUSY;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001896
Jonghwa Leec1155c62014-12-19 17:55:13 +09001897 if (!cm_suspended)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001898 cm_suspended = true;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001899
Jonghwa Leec1155c62014-12-19 17:55:13 +09001900 cm_timer_set = cm_setup_timer();
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001901
Jonghwa Leec1155c62014-12-19 17:55:13 +09001902 if (cm_timer_set) {
1903 cancel_work_sync(&setup_polling);
1904 cancel_delayed_work_sync(&cm_monitor_work);
1905 cancel_delayed_work(&cm->fullbatt_vchk_work);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001906 }
1907
1908 return 0;
1909}
1910
1911static void cm_suspend_complete(struct device *dev)
1912{
Axel Linbb2a95c2012-01-12 12:56:35 +08001913 struct charger_manager *cm = dev_get_drvdata(dev);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001914
Jonghwa Leec1155c62014-12-19 17:55:13 +09001915 if (cm_suspended)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001916 cm_suspended = false;
Jonghwa Leec1155c62014-12-19 17:55:13 +09001917
1918 if (cm_timer_set) {
1919 ktime_t remain;
1920
1921 alarm_cancel(cm_timer);
1922 cm_timer_set = false;
1923 remain = alarm_expires_remaining(cm_timer);
1924 cm_suspend_duration_ms -= ktime_to_ms(remain);
1925 schedule_work(&setup_polling);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001926 }
1927
Jonghwa Leec1155c62014-12-19 17:55:13 +09001928 _cm_monitor(cm);
1929
Chanwoo Choid829dc72012-05-05 06:24:10 -07001930 /* Re-enqueue delayed work (fullbatt_vchk_work) */
1931 if (cm->fullbatt_vchk_jiffies_at) {
1932 unsigned long delay = 0;
1933 unsigned long now = jiffies + CM_JIFFIES_SMALL;
1934
1935 if (time_after_eq(now, cm->fullbatt_vchk_jiffies_at)) {
1936 delay = (unsigned long)((long)now
1937 - (long)(cm->fullbatt_vchk_jiffies_at));
1938 delay = jiffies_to_msecs(delay);
1939 } else {
1940 delay = 0;
1941 }
1942
1943 /*
Jonghwa Leec1155c62014-12-19 17:55:13 +09001944 * Account for cm_suspend_duration_ms with assuming that
1945 * timer stops in suspend.
Chanwoo Choid829dc72012-05-05 06:24:10 -07001946 */
Jonghwa Leec1155c62014-12-19 17:55:13 +09001947 if (delay > cm_suspend_duration_ms)
1948 delay -= cm_suspend_duration_ms;
1949 else
1950 delay = 0;
Chanwoo Choid829dc72012-05-05 06:24:10 -07001951
1952 queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
1953 msecs_to_jiffies(delay));
1954 }
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001955 device_set_wakeup_capable(cm->dev, false);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001956}
1957
1958static const struct dev_pm_ops charger_manager_pm = {
1959 .prepare = cm_suspend_prepare,
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001960 .suspend_noirq = cm_suspend_noirq,
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001961 .complete = cm_suspend_complete,
1962};
1963
1964static struct platform_driver charger_manager_driver = {
1965 .driver = {
1966 .name = "charger-manager",
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001967 .pm = &charger_manager_pm,
Jonghwa Lee856ee612013-12-18 15:42:35 +09001968 .of_match_table = charger_manager_match,
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001969 },
1970 .probe = charger_manager_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -05001971 .remove = charger_manager_remove,
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001972 .id_table = charger_manager_id,
1973};
1974
1975static int __init charger_manager_init(void)
1976{
Chanwoo Choid829dc72012-05-05 06:24:10 -07001977 cm_wq = create_freezable_workqueue("charger_manager");
1978 INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller);
1979
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001980 return platform_driver_register(&charger_manager_driver);
1981}
1982late_initcall(charger_manager_init);
1983
1984static void __exit charger_manager_cleanup(void)
1985{
Chanwoo Choid829dc72012-05-05 06:24:10 -07001986 destroy_workqueue(cm_wq);
1987 cm_wq = NULL;
1988
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001989 platform_driver_unregister(&charger_manager_driver);
1990}
1991module_exit(charger_manager_cleanup);
1992
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001993/**
1994 * find_power_supply - find the associated power_supply of charger
1995 * @cm: the Charger Manager representing the battery
1996 * @psy: pointer to instance of charger's power_supply
1997 */
1998static bool find_power_supply(struct charger_manager *cm,
1999 struct power_supply *psy)
2000{
2001 int i;
2002 bool found = false;
2003
Krzysztof Kozlowskicdaf3e12014-10-13 15:34:31 +02002004 for (i = 0; cm->desc->psy_charger_stat[i]; i++) {
2005 if (!strcmp(psy->name, cm->desc->psy_charger_stat[i])) {
Chanwoo Choidfeccb12012-05-05 06:26:47 -07002006 found = true;
2007 break;
2008 }
2009 }
2010
2011 return found;
2012}
2013
2014/**
2015 * cm_notify_event - charger driver notify Charger Manager of charger event
2016 * @psy: pointer to instance of charger's power_supply
2017 * @type: type of charger event
2018 * @msg: optional message passed to uevent_notify fuction
2019 */
2020void cm_notify_event(struct power_supply *psy, enum cm_event_types type,
2021 char *msg)
2022{
2023 struct charger_manager *cm;
2024 bool found_power_supply = false;
2025
2026 if (psy == NULL)
2027 return;
2028
2029 mutex_lock(&cm_list_mtx);
2030 list_for_each_entry(cm, &cm_list, entry) {
2031 found_power_supply = find_power_supply(cm, psy);
2032 if (found_power_supply)
2033 break;
2034 }
2035 mutex_unlock(&cm_list_mtx);
2036
2037 if (!found_power_supply)
2038 return;
2039
2040 switch (type) {
2041 case CM_EVENT_BATT_FULL:
2042 fullbatt_handler(cm);
2043 break;
2044 case CM_EVENT_BATT_OUT:
2045 battout_handler(cm);
2046 break;
2047 case CM_EVENT_BATT_IN:
2048 case CM_EVENT_EXT_PWR_IN_OUT ... CM_EVENT_CHG_START_STOP:
2049 misc_event_handler(cm, type);
2050 break;
2051 case CM_EVENT_UNKNOWN:
2052 case CM_EVENT_OTHERS:
2053 uevent_notify(cm, msg ? msg : default_event_names[type]);
2054 break;
2055 default:
Joe Perchese5409cb2013-06-06 18:25:12 -07002056 dev_err(cm->dev, "%s: type not specified\n", __func__);
Chanwoo Choidfeccb12012-05-05 06:26:47 -07002057 break;
2058 }
2059}
2060EXPORT_SYMBOL_GPL(cm_notify_event);
2061
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09002062MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
2063MODULE_DESCRIPTION("Charger Manager");
2064MODULE_LICENSE("GPL");