blob: 5c47409c68894eceb896e84f5f3cd288f1478337 [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{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100867 struct charger_manager *cm = power_supply_get_drvdata(psy);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900868 struct charger_desc *desc = cm->desc;
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200869 struct power_supply *fuel_gauge;
Anton Vorontsovdf58c042012-03-15 21:01:28 +0400870 int ret = 0;
871 int uV;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900872
873 switch (psp) {
874 case POWER_SUPPLY_PROP_STATUS:
875 if (is_charging(cm))
876 val->intval = POWER_SUPPLY_STATUS_CHARGING;
877 else if (is_ext_pwr_online(cm))
878 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
879 else
880 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
881 break;
882 case POWER_SUPPLY_PROP_HEALTH:
883 if (cm->emergency_stop > 0)
884 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
885 else if (cm->emergency_stop < 0)
886 val->intval = POWER_SUPPLY_HEALTH_COLD;
887 else
888 val->intval = POWER_SUPPLY_HEALTH_GOOD;
889 break;
890 case POWER_SUPPLY_PROP_PRESENT:
891 if (is_batt_present(cm))
892 val->intval = 1;
893 else
894 val->intval = 0;
895 break;
896 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Anton Vorontsovdf58c042012-03-15 21:01:28 +0400897 ret = get_batt_uV(cm, &val->intval);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900898 break;
899 case POWER_SUPPLY_PROP_CURRENT_NOW:
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200900 fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
901 if (!fuel_gauge) {
902 ret = -ENODEV;
903 break;
904 }
Krzysztof Kozlowskib70229b2015-03-12 08:44:10 +0100905 ret = power_supply_get_property(fuel_gauge,
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900906 POWER_SUPPLY_PROP_CURRENT_NOW, val);
907 break;
908 case POWER_SUPPLY_PROP_TEMP:
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900909 case POWER_SUPPLY_PROP_TEMP_AMBIENT:
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900910 return cm_get_battery_temperature(cm, &val->intval);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900911 case POWER_SUPPLY_PROP_CAPACITY:
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200912 fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
913 if (!fuel_gauge) {
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900914 ret = -ENODEV;
915 break;
916 }
917
918 if (!is_batt_present(cm)) {
919 /* There is no battery. Assume 100% */
920 val->intval = 100;
921 break;
922 }
923
Krzysztof Kozlowskib70229b2015-03-12 08:44:10 +0100924 ret = power_supply_get_property(fuel_gauge,
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900925 POWER_SUPPLY_PROP_CAPACITY, val);
926 if (ret)
927 break;
928
929 if (val->intval > 100) {
930 val->intval = 100;
931 break;
932 }
933 if (val->intval < 0)
934 val->intval = 0;
935
936 /* Do not adjust SOC when charging: voltage is overrated */
937 if (is_charging(cm))
938 break;
939
940 /*
941 * If the capacity value is inconsistent, calibrate it base on
942 * the battery voltage values and the thresholds given as desc
943 */
944 ret = get_batt_uV(cm, &uV);
945 if (ret) {
946 /* Voltage information not available. No calibration */
947 ret = 0;
948 break;
949 }
950
951 if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
952 !is_charging(cm)) {
953 val->intval = 100;
954 break;
955 }
956
957 break;
958 case POWER_SUPPLY_PROP_ONLINE:
959 if (is_ext_pwr_online(cm))
960 val->intval = 1;
961 else
962 val->intval = 0;
963 break;
964 case POWER_SUPPLY_PROP_CHARGE_FULL:
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900965 if (is_full_charged(cm))
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900966 val->intval = 1;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900967 else
968 val->intval = 0;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900969 ret = 0;
970 break;
971 case POWER_SUPPLY_PROP_CHARGE_NOW:
972 if (is_charging(cm)) {
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200973 fuel_gauge = power_supply_get_by_name(
974 cm->desc->psy_fuel_gauge);
975 if (!fuel_gauge) {
976 ret = -ENODEV;
977 break;
978 }
979
Krzysztof Kozlowskib70229b2015-03-12 08:44:10 +0100980 ret = power_supply_get_property(fuel_gauge,
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900981 POWER_SUPPLY_PROP_CHARGE_NOW,
982 val);
983 if (ret) {
984 val->intval = 1;
985 ret = 0;
986 } else {
987 /* If CHARGE_NOW is supplied, use it */
988 val->intval = (val->intval > 0) ?
989 val->intval : 1;
990 }
991 } else {
992 val->intval = 0;
993 }
994 break;
995 default:
996 return -EINVAL;
997 }
998 return ret;
999}
1000
1001#define NUM_CHARGER_PSY_OPTIONAL (4)
1002static enum power_supply_property default_charger_props[] = {
1003 /* Guaranteed to provide */
1004 POWER_SUPPLY_PROP_STATUS,
1005 POWER_SUPPLY_PROP_HEALTH,
1006 POWER_SUPPLY_PROP_PRESENT,
1007 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1008 POWER_SUPPLY_PROP_CAPACITY,
1009 POWER_SUPPLY_PROP_ONLINE,
1010 POWER_SUPPLY_PROP_CHARGE_FULL,
1011 /*
1012 * Optional properties are:
1013 * POWER_SUPPLY_PROP_CHARGE_NOW,
1014 * POWER_SUPPLY_PROP_CURRENT_NOW,
1015 * POWER_SUPPLY_PROP_TEMP, and
1016 * POWER_SUPPLY_PROP_TEMP_AMBIENT,
1017 */
1018};
1019
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001020static const struct power_supply_desc psy_default = {
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001021 .name = "battery",
1022 .type = POWER_SUPPLY_TYPE_BATTERY,
1023 .properties = default_charger_props,
1024 .num_properties = ARRAY_SIZE(default_charger_props),
1025 .get_property = charger_get_property,
Krzysztof Kozlowskiba9c9182014-10-07 17:47:37 +02001026 .no_thermal = true,
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001027};
1028
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001029/**
1030 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
1031 * for suspend_again.
1032 *
1033 * Returns true if the alarm is set for Charger Manager to use.
1034 * Returns false if
1035 * cm_setup_timer fails to set an alarm,
1036 * cm_setup_timer does not need to set an alarm for Charger Manager,
1037 * or an alarm previously configured is to be used.
1038 */
1039static bool cm_setup_timer(void)
1040{
1041 struct charger_manager *cm;
1042 unsigned int wakeup_ms = UINT_MAX;
Jonghwa Leec1155c62014-12-19 17:55:13 +09001043 int timer_req = 0;
1044
1045 if (time_after(next_polling, jiffies))
1046 CM_MIN_VALID(wakeup_ms,
1047 jiffies_to_msecs(next_polling - jiffies));
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001048
1049 mutex_lock(&cm_list_mtx);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001050 list_for_each_entry(cm, &cm_list, entry) {
Chanwoo Choid829dc72012-05-05 06:24:10 -07001051 unsigned int fbchk_ms = 0;
1052
1053 /* fullbatt_vchk is required. setup timer for that */
1054 if (cm->fullbatt_vchk_jiffies_at) {
1055 fbchk_ms = jiffies_to_msecs(cm->fullbatt_vchk_jiffies_at
1056 - jiffies);
1057 if (time_is_before_eq_jiffies(
1058 cm->fullbatt_vchk_jiffies_at) ||
1059 msecs_to_jiffies(fbchk_ms) < CM_JIFFIES_SMALL) {
1060 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
1061 fbchk_ms = 0;
1062 }
1063 }
1064 CM_MIN_VALID(wakeup_ms, fbchk_ms);
1065
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001066 /* Skip if polling is not required for this CM */
1067 if (!is_polling_required(cm) && !cm->emergency_stop)
1068 continue;
Jonghwa Leec1155c62014-12-19 17:55:13 +09001069 timer_req++;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001070 if (cm->desc->polling_interval_ms == 0)
1071 continue;
1072 CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
1073 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001074 mutex_unlock(&cm_list_mtx);
1075
Jonghwa Leec1155c62014-12-19 17:55:13 +09001076 if (timer_req && cm_timer) {
1077 ktime_t now, add;
1078
1079 /*
1080 * Set alarm with the polling interval (wakeup_ms)
1081 * The alarm time should be NOW + CM_RTC_SMALL or later.
1082 */
1083 if (wakeup_ms == UINT_MAX ||
1084 wakeup_ms < CM_RTC_SMALL * MSEC_PER_SEC)
1085 wakeup_ms = 2 * CM_RTC_SMALL * MSEC_PER_SEC;
1086
Joe Perchese5409cb2013-06-06 18:25:12 -07001087 pr_info("Charger Manager wakeup timer: %u ms\n", wakeup_ms);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001088
Jonghwa Leec1155c62014-12-19 17:55:13 +09001089 now = ktime_get_boottime();
1090 add = ktime_set(wakeup_ms / MSEC_PER_SEC,
1091 (wakeup_ms % MSEC_PER_SEC) * NSEC_PER_MSEC);
1092 alarm_start(cm_timer, ktime_add(now, add));
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001093
Jonghwa Leec1155c62014-12-19 17:55:13 +09001094 cm_suspend_duration_ms = wakeup_ms;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001095
Jonghwa Leec1155c62014-12-19 17:55:13 +09001096 return true;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001097 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001098 return false;
1099}
1100
Chanwoo Choibee737b2012-07-12 15:03:25 +09001101/**
1102 * charger_extcon_work - enable/diable charger according to the state
1103 * of charger cable
1104 *
1105 * @work: work_struct of the function charger_extcon_work.
1106 */
1107static void charger_extcon_work(struct work_struct *work)
1108{
1109 struct charger_cable *cable =
1110 container_of(work, struct charger_cable, wq);
Chanwoo Choi45cd4fb2012-07-12 15:03:29 +09001111 int ret;
1112
1113 if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) {
1114 ret = regulator_set_current_limit(cable->charger->consumer,
1115 cable->min_uA, cable->max_uA);
1116 if (ret < 0) {
1117 pr_err("Cannot set current limit of %s (%s)\n",
Joe Perchese5409cb2013-06-06 18:25:12 -07001118 cable->charger->regulator_name, cable->name);
Chanwoo Choi45cd4fb2012-07-12 15:03:29 +09001119 return;
1120 }
1121
1122 pr_info("Set current limit of %s : %duA ~ %duA\n",
Joe Perchese5409cb2013-06-06 18:25:12 -07001123 cable->charger->regulator_name,
1124 cable->min_uA, cable->max_uA);
Chanwoo Choi45cd4fb2012-07-12 15:03:29 +09001125 }
Chanwoo Choibee737b2012-07-12 15:03:25 +09001126
1127 try_charger_enable(cable->cm, cable->attached);
1128}
1129
1130/**
1131 * charger_extcon_notifier - receive the state of charger cable
1132 * when registered cable is attached or detached.
1133 *
1134 * @self: the notifier block of the charger_extcon_notifier.
1135 * @event: the cable state.
1136 * @ptr: the data pointer of notifier block.
1137 */
1138static int charger_extcon_notifier(struct notifier_block *self,
1139 unsigned long event, void *ptr)
1140{
1141 struct charger_cable *cable =
1142 container_of(self, struct charger_cable, nb);
1143
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001144 /*
1145 * The newly state of charger cable.
1146 * If cable is attached, cable->attached is true.
1147 */
Chanwoo Choibee737b2012-07-12 15:03:25 +09001148 cable->attached = event;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001149
1150 /*
1151 * Setup monitoring to check battery state
1152 * when charger cable is attached.
1153 */
1154 if (cable->attached && is_polling_required(cable->cm)) {
Tejun Heo2fbb5202012-12-21 17:56:51 -08001155 cancel_work_sync(&setup_polling);
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001156 schedule_work(&setup_polling);
1157 }
1158
1159 /*
1160 * Setup work for controlling charger(regulator)
1161 * according to charger cable.
1162 */
Chanwoo Choibee737b2012-07-12 15:03:25 +09001163 schedule_work(&cable->wq);
1164
1165 return NOTIFY_DONE;
1166}
1167
1168/**
1169 * charger_extcon_init - register external connector to use it
1170 * as the charger cable
1171 *
1172 * @cm: the Charger Manager representing the battery.
1173 * @cable: the Charger cable representing the external connector.
1174 */
1175static int charger_extcon_init(struct charger_manager *cm,
1176 struct charger_cable *cable)
1177{
1178 int ret = 0;
1179
1180 /*
1181 * Charger manager use Extcon framework to identify
1182 * the charger cable among various external connector
1183 * cable (e.g., TA, USB, MHL, Dock).
1184 */
1185 INIT_WORK(&cable->wq, charger_extcon_work);
1186 cable->nb.notifier_call = charger_extcon_notifier;
1187 ret = extcon_register_interest(&cable->extcon_dev,
1188 cable->extcon_name, cable->name, &cable->nb);
1189 if (ret < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001190 pr_info("Cannot register extcon_dev for %s(cable: %s)\n",
1191 cable->extcon_name, cable->name);
Chanwoo Choibee737b2012-07-12 15:03:25 +09001192 ret = -EINVAL;
1193 }
1194
1195 return ret;
1196}
1197
Chanwoo Choi41468a12012-11-22 16:54:26 +09001198/**
1199 * charger_manager_register_extcon - Register extcon device to recevie state
1200 * of charger cable.
1201 * @cm: the Charger Manager representing the battery.
1202 *
1203 * This function support EXTCON(External Connector) subsystem to detect the
1204 * state of charger cables for enabling or disabling charger(regulator) and
1205 * select the charger cable for charging among a number of external cable
1206 * according to policy of H/W board.
1207 */
1208static int charger_manager_register_extcon(struct charger_manager *cm)
1209{
1210 struct charger_desc *desc = cm->desc;
1211 struct charger_regulator *charger;
1212 int ret = 0;
1213 int i;
1214 int j;
1215
1216 for (i = 0; i < desc->num_charger_regulators; i++) {
1217 charger = &desc->charger_regulators[i];
1218
1219 charger->consumer = regulator_get(cm->dev,
1220 charger->regulator_name);
Jonghwa Lee5a6c2202013-06-25 14:18:14 +09001221 if (IS_ERR(charger->consumer)) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001222 dev_err(cm->dev, "Cannot find charger(%s)\n",
1223 charger->regulator_name);
Jonghwa Lee5a6c2202013-06-25 14:18:14 +09001224 return PTR_ERR(charger->consumer);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001225 }
1226 charger->cm = cm;
1227
1228 for (j = 0; j < charger->num_cables; j++) {
1229 struct charger_cable *cable = &charger->cables[j];
1230
1231 ret = charger_extcon_init(cm, cable);
1232 if (ret < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001233 dev_err(cm->dev, "Cannot initialize charger(%s)\n",
1234 charger->regulator_name);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001235 goto err;
1236 }
1237 cable->charger = charger;
1238 cable->cm = cm;
1239 }
1240 }
1241
1242err:
1243 return ret;
1244}
1245
Chanwoo Choi3950c782012-09-21 18:49:37 +09001246/* help function of sysfs node to control charger(regulator) */
1247static ssize_t charger_name_show(struct device *dev,
1248 struct device_attribute *attr, char *buf)
1249{
1250 struct charger_regulator *charger
1251 = container_of(attr, struct charger_regulator, attr_name);
1252
1253 return sprintf(buf, "%s\n", charger->regulator_name);
1254}
1255
1256static ssize_t charger_state_show(struct device *dev,
1257 struct device_attribute *attr, char *buf)
1258{
1259 struct charger_regulator *charger
1260 = container_of(attr, struct charger_regulator, attr_state);
1261 int state = 0;
1262
1263 if (!charger->externally_control)
1264 state = regulator_is_enabled(charger->consumer);
1265
1266 return sprintf(buf, "%s\n", state ? "enabled" : "disabled");
1267}
1268
1269static ssize_t charger_externally_control_show(struct device *dev,
1270 struct device_attribute *attr, char *buf)
1271{
1272 struct charger_regulator *charger = container_of(attr,
1273 struct charger_regulator, attr_externally_control);
1274
1275 return sprintf(buf, "%d\n", charger->externally_control);
1276}
1277
1278static ssize_t charger_externally_control_store(struct device *dev,
1279 struct device_attribute *attr, const char *buf,
1280 size_t count)
1281{
1282 struct charger_regulator *charger
1283 = container_of(attr, struct charger_regulator,
1284 attr_externally_control);
1285 struct charger_manager *cm = charger->cm;
1286 struct charger_desc *desc = cm->desc;
1287 int i;
1288 int ret;
1289 int externally_control;
1290 int chargers_externally_control = 1;
1291
1292 ret = sscanf(buf, "%d", &externally_control);
1293 if (ret == 0) {
1294 ret = -EINVAL;
1295 return ret;
1296 }
1297
1298 if (!externally_control) {
1299 charger->externally_control = 0;
1300 return count;
1301 }
1302
1303 for (i = 0; i < desc->num_charger_regulators; i++) {
1304 if (&desc->charger_regulators[i] != charger &&
Chanwoo Choi41468a12012-11-22 16:54:26 +09001305 !desc->charger_regulators[i].externally_control) {
Chanwoo Choi3950c782012-09-21 18:49:37 +09001306 /*
1307 * At least, one charger is controlled by
1308 * charger-manager
1309 */
1310 chargers_externally_control = 0;
1311 break;
1312 }
1313 }
1314
1315 if (!chargers_externally_control) {
1316 if (cm->charger_enabled) {
1317 try_charger_enable(charger->cm, false);
1318 charger->externally_control = externally_control;
1319 try_charger_enable(charger->cm, true);
1320 } else {
1321 charger->externally_control = externally_control;
1322 }
1323 } else {
1324 dev_warn(cm->dev,
Joe Perchese5409cb2013-06-06 18:25:12 -07001325 "'%s' regulator should be controlled in charger-manager because charger-manager must need at least one charger for charging\n",
1326 charger->regulator_name);
Chanwoo Choi3950c782012-09-21 18:49:37 +09001327 }
1328
1329 return count;
1330}
1331
Chanwoo Choi41468a12012-11-22 16:54:26 +09001332/**
1333 * charger_manager_register_sysfs - Register sysfs entry for each charger
1334 * @cm: the Charger Manager representing the battery.
1335 *
1336 * This function add sysfs entry for charger(regulator) to control charger from
1337 * user-space. If some development board use one more chargers for charging
1338 * but only need one charger on specific case which is dependent on user
1339 * scenario or hardware restrictions, the user enter 1 or 0(zero) to '/sys/
1340 * class/power_supply/battery/charger.[index]/externally_control'. For example,
1341 * if user enter 1 to 'sys/class/power_supply/battery/charger.[index]/
1342 * externally_control, this charger isn't controlled from charger-manager and
1343 * always stay off state of regulator.
1344 */
1345static int charger_manager_register_sysfs(struct charger_manager *cm)
1346{
1347 struct charger_desc *desc = cm->desc;
1348 struct charger_regulator *charger;
1349 int chargers_externally_control = 1;
1350 char buf[11];
1351 char *str;
1352 int ret = 0;
1353 int i;
1354
1355 /* Create sysfs entry to control charger(regulator) */
1356 for (i = 0; i < desc->num_charger_regulators; i++) {
1357 charger = &desc->charger_regulators[i];
1358
1359 snprintf(buf, 10, "charger.%d", i);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001360 str = devm_kzalloc(cm->dev,
1361 sizeof(char) * (strlen(buf) + 1), GFP_KERNEL);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001362 if (!str) {
Chanwoo Choi41468a12012-11-22 16:54:26 +09001363 ret = -ENOMEM;
1364 goto err;
1365 }
1366 strcpy(str, buf);
1367
1368 charger->attrs[0] = &charger->attr_name.attr;
1369 charger->attrs[1] = &charger->attr_state.attr;
1370 charger->attrs[2] = &charger->attr_externally_control.attr;
1371 charger->attrs[3] = NULL;
1372 charger->attr_g.name = str;
1373 charger->attr_g.attrs = charger->attrs;
1374
1375 sysfs_attr_init(&charger->attr_name.attr);
1376 charger->attr_name.attr.name = "name";
1377 charger->attr_name.attr.mode = 0444;
1378 charger->attr_name.show = charger_name_show;
1379
1380 sysfs_attr_init(&charger->attr_state.attr);
1381 charger->attr_state.attr.name = "state";
1382 charger->attr_state.attr.mode = 0444;
1383 charger->attr_state.show = charger_state_show;
1384
1385 sysfs_attr_init(&charger->attr_externally_control.attr);
1386 charger->attr_externally_control.attr.name
1387 = "externally_control";
1388 charger->attr_externally_control.attr.mode = 0644;
1389 charger->attr_externally_control.show
1390 = charger_externally_control_show;
1391 charger->attr_externally_control.store
1392 = charger_externally_control_store;
1393
1394 if (!desc->charger_regulators[i].externally_control ||
1395 !chargers_externally_control)
1396 chargers_externally_control = 0;
1397
Joe Perchese5409cb2013-06-06 18:25:12 -07001398 dev_info(cm->dev, "'%s' regulator's externally_control is %d\n",
1399 charger->regulator_name, charger->externally_control);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001400
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001401 ret = sysfs_create_group(&cm->charger_psy->dev.kobj,
Chanwoo Choi41468a12012-11-22 16:54:26 +09001402 &charger->attr_g);
1403 if (ret < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001404 dev_err(cm->dev, "Cannot create sysfs entry of %s regulator\n",
1405 charger->regulator_name);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001406 ret = -EINVAL;
1407 goto err;
1408 }
1409 }
1410
1411 if (chargers_externally_control) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001412 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 +09001413 ret = -EINVAL;
1414 goto err;
1415 }
1416
1417err:
1418 return ret;
1419}
1420
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +02001421static int cm_init_thermal_data(struct charger_manager *cm,
1422 struct power_supply *fuel_gauge)
Jonghwa Lee5c49a622013-12-18 15:42:34 +09001423{
1424 struct charger_desc *desc = cm->desc;
1425 union power_supply_propval val;
1426 int ret;
1427
1428 /* Verify whether fuel gauge provides battery temperature */
Krzysztof Kozlowskib70229b2015-03-12 08:44:10 +01001429 ret = power_supply_get_property(fuel_gauge,
Jonghwa Lee5c49a622013-12-18 15:42:34 +09001430 POWER_SUPPLY_PROP_TEMP, &val);
1431
1432 if (!ret) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001433 cm->charger_psy_desc.properties[cm->charger_psy_desc.num_properties] =
Jonghwa Lee5c49a622013-12-18 15:42:34 +09001434 POWER_SUPPLY_PROP_TEMP;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001435 cm->charger_psy_desc.num_properties++;
Jonghwa Lee5c49a622013-12-18 15:42:34 +09001436 cm->desc->measure_battery_temp = true;
1437 }
1438#ifdef CONFIG_THERMAL
Jonghwa Lee5c49a622013-12-18 15:42:34 +09001439 if (ret && desc->thermal_zone) {
1440 cm->tzd_batt =
1441 thermal_zone_get_zone_by_name(desc->thermal_zone);
1442 if (IS_ERR(cm->tzd_batt))
1443 return PTR_ERR(cm->tzd_batt);
1444
1445 /* Use external thermometer */
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001446 cm->charger_psy_desc.properties[cm->charger_psy_desc.num_properties] =
Jonghwa Lee5c49a622013-12-18 15:42:34 +09001447 POWER_SUPPLY_PROP_TEMP_AMBIENT;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001448 cm->charger_psy_desc.num_properties++;
Jonghwa Lee5c49a622013-12-18 15:42:34 +09001449 cm->desc->measure_battery_temp = true;
1450 ret = 0;
1451 }
1452#endif
1453 if (cm->desc->measure_battery_temp) {
1454 /* NOTICE : Default allowable minimum charge temperature is 0 */
1455 if (!desc->temp_max)
1456 desc->temp_max = CM_DEFAULT_CHARGE_TEMP_MAX;
1457 if (!desc->temp_diff)
1458 desc->temp_diff = CM_DEFAULT_RECHARGE_TEMP_DIFF;
1459 }
1460
1461 return ret;
1462}
1463
Jonghwa Lee856ee612013-12-18 15:42:35 +09001464static struct of_device_id charger_manager_match[] = {
1465 {
1466 .compatible = "charger-manager",
1467 },
1468 {},
1469};
1470
Anton Vorontsov434a09f2013-12-23 18:17:05 -08001471static struct charger_desc *of_cm_parse_desc(struct device *dev)
Jonghwa Lee856ee612013-12-18 15:42:35 +09001472{
1473 struct charger_desc *desc;
1474 struct device_node *np = dev->of_node;
1475 u32 poll_mode = CM_POLL_DISABLE;
1476 u32 battery_stat = CM_NO_BATTERY;
1477 int num_chgs = 0;
1478
1479 desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
1480 if (!desc)
1481 return ERR_PTR(-ENOMEM);
1482
1483 of_property_read_string(np, "cm-name", &desc->psy_name);
1484
1485 of_property_read_u32(np, "cm-poll-mode", &poll_mode);
1486 desc->polling_mode = poll_mode;
1487
1488 of_property_read_u32(np, "cm-poll-interval",
1489 &desc->polling_interval_ms);
1490
1491 of_property_read_u32(np, "cm-fullbatt-vchkdrop-ms",
1492 &desc->fullbatt_vchkdrop_ms);
1493 of_property_read_u32(np, "cm-fullbatt-vchkdrop-volt",
1494 &desc->fullbatt_vchkdrop_uV);
1495 of_property_read_u32(np, "cm-fullbatt-voltage", &desc->fullbatt_uV);
1496 of_property_read_u32(np, "cm-fullbatt-soc", &desc->fullbatt_soc);
1497 of_property_read_u32(np, "cm-fullbatt-capacity",
1498 &desc->fullbatt_full_capacity);
1499
1500 of_property_read_u32(np, "cm-battery-stat", &battery_stat);
1501 desc->battery_present = battery_stat;
1502
1503 /* chargers */
1504 of_property_read_u32(np, "cm-num-chargers", &num_chgs);
1505 if (num_chgs) {
1506 /* Allocate empty bin at the tail of array */
1507 desc->psy_charger_stat = devm_kzalloc(dev, sizeof(char *)
1508 * (num_chgs + 1), GFP_KERNEL);
1509 if (desc->psy_charger_stat) {
1510 int i;
1511 for (i = 0; i < num_chgs; i++)
1512 of_property_read_string_index(np, "cm-chargers",
1513 i, &desc->psy_charger_stat[i]);
1514 } else {
1515 return ERR_PTR(-ENOMEM);
1516 }
1517 }
1518
1519 of_property_read_string(np, "cm-fuel-gauge", &desc->psy_fuel_gauge);
1520
1521 of_property_read_string(np, "cm-thermal-zone", &desc->thermal_zone);
1522
1523 of_property_read_u32(np, "cm-battery-cold", &desc->temp_min);
1524 if (of_get_property(np, "cm-battery-cold-in-minus", NULL))
1525 desc->temp_min *= -1;
1526 of_property_read_u32(np, "cm-battery-hot", &desc->temp_max);
1527 of_property_read_u32(np, "cm-battery-temp-diff", &desc->temp_diff);
1528
1529 of_property_read_u32(np, "cm-charging-max",
1530 &desc->charging_max_duration_ms);
1531 of_property_read_u32(np, "cm-discharging-max",
1532 &desc->discharging_max_duration_ms);
1533
1534 /* battery charger regualtors */
1535 desc->num_charger_regulators = of_get_child_count(np);
1536 if (desc->num_charger_regulators) {
1537 struct charger_regulator *chg_regs;
1538 struct device_node *child;
1539
1540 chg_regs = devm_kzalloc(dev, sizeof(*chg_regs)
1541 * desc->num_charger_regulators,
1542 GFP_KERNEL);
1543 if (!chg_regs)
1544 return ERR_PTR(-ENOMEM);
1545
1546 desc->charger_regulators = chg_regs;
1547
1548 for_each_child_of_node(np, child) {
1549 struct charger_cable *cables;
1550 struct device_node *_child;
1551
1552 of_property_read_string(child, "cm-regulator-name",
1553 &chg_regs->regulator_name);
1554
1555 /* charger cables */
1556 chg_regs->num_cables = of_get_child_count(child);
1557 if (chg_regs->num_cables) {
1558 cables = devm_kzalloc(dev, sizeof(*cables)
1559 * chg_regs->num_cables,
1560 GFP_KERNEL);
1561 if (!cables)
1562 return ERR_PTR(-ENOMEM);
1563
1564 chg_regs->cables = cables;
1565
1566 for_each_child_of_node(child, _child) {
1567 of_property_read_string(_child,
1568 "cm-cable-name", &cables->name);
1569 of_property_read_string(_child,
1570 "cm-cable-extcon",
1571 &cables->extcon_name);
1572 of_property_read_u32(_child,
1573 "cm-cable-min",
1574 &cables->min_uA);
1575 of_property_read_u32(_child,
1576 "cm-cable-max",
1577 &cables->max_uA);
1578 cables++;
1579 }
1580 }
1581 chg_regs++;
1582 }
1583 }
1584 return desc;
1585}
1586
1587static inline struct charger_desc *cm_get_drv_data(struct platform_device *pdev)
1588{
1589 if (pdev->dev.of_node)
1590 return of_cm_parse_desc(&pdev->dev);
Jingoo Han86515b72014-08-29 12:45:27 +09001591 return dev_get_platdata(&pdev->dev);
Jonghwa Lee856ee612013-12-18 15:42:35 +09001592}
1593
Jonghwa Leec1155c62014-12-19 17:55:13 +09001594static enum alarmtimer_restart cm_timer_func(struct alarm *alarm, ktime_t now)
1595{
1596 cm_timer_set = false;
1597 return ALARMTIMER_NORESTART;
1598}
1599
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001600static int charger_manager_probe(struct platform_device *pdev)
1601{
Jonghwa Lee856ee612013-12-18 15:42:35 +09001602 struct charger_desc *desc = cm_get_drv_data(pdev);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001603 struct charger_manager *cm;
1604 int ret = 0, i = 0;
Chanwoo Choibee737b2012-07-12 15:03:25 +09001605 int j = 0;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001606 union power_supply_propval val;
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +02001607 struct power_supply *fuel_gauge;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001608 struct power_supply_config psy_cfg = {};
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;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001623 psy_cfg.drv_data = cm;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001624
Jonghwa Leec1155c62014-12-19 17:55:13 +09001625 /* Initialize alarm timer */
1626 if (alarmtimer_get_rtcdev()) {
1627 cm_timer = devm_kzalloc(cm->dev, sizeof(*cm_timer), GFP_KERNEL);
1628 alarm_init(cm_timer, ALARM_BOOTTIME, cm_timer_func);
1629 }
1630
Chanwoo Choid829dc72012-05-05 06:24:10 -07001631 /*
1632 * The following two do not need to be errors.
1633 * Users may intentionally ignore those two features.
1634 */
1635 if (desc->fullbatt_uV == 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001636 dev_info(&pdev->dev, "Ignoring full-battery voltage threshold as it is not supplied\n");
Chanwoo Choid829dc72012-05-05 06:24:10 -07001637 }
1638 if (!desc->fullbatt_vchkdrop_ms || !desc->fullbatt_vchkdrop_uV) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001639 dev_info(&pdev->dev, "Disabling full-battery voltage drop checking mechanism as it is not supplied\n");
Chanwoo Choid829dc72012-05-05 06:24:10 -07001640 desc->fullbatt_vchkdrop_ms = 0;
1641 desc->fullbatt_vchkdrop_uV = 0;
1642 }
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001643 if (desc->fullbatt_soc == 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001644 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 +09001645 }
1646 if (desc->fullbatt_full_capacity == 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001647 dev_info(&pdev->dev, "Ignoring full-battery full capacity threshold as it is not supplied\n");
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001648 }
Chanwoo Choid829dc72012-05-05 06:24:10 -07001649
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001650 if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001651 dev_err(&pdev->dev, "charger_regulators undefined\n");
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001652 return -EINVAL;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001653 }
1654
1655 if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001656 dev_err(&pdev->dev, "No power supply defined\n");
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001657 return -EINVAL;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001658 }
1659
Krzysztof Kozlowski661a8882014-09-26 13:27:03 +02001660 if (!desc->psy_fuel_gauge) {
1661 dev_err(&pdev->dev, "No fuel gauge power supply defined\n");
1662 return -EINVAL;
1663 }
1664
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001665 /* Counting index only */
1666 while (desc->psy_charger_stat[i])
1667 i++;
1668
Krzysztof Kozlowskicdaf3e12014-10-13 15:34:31 +02001669 /* Check if charger's supplies are present at probe */
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001670 for (i = 0; desc->psy_charger_stat[i]; i++) {
Krzysztof Kozlowskicdaf3e12014-10-13 15:34:31 +02001671 struct power_supply *psy;
1672
1673 psy = power_supply_get_by_name(desc->psy_charger_stat[i]);
1674 if (!psy) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001675 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
1676 desc->psy_charger_stat[i]);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001677 return -ENODEV;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001678 }
1679 }
1680
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +02001681 fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
1682 if (!fuel_gauge) {
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001683 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
Joe Perchese5409cb2013-06-06 18:25:12 -07001684 desc->psy_fuel_gauge);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001685 return -ENODEV;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001686 }
1687
1688 if (desc->polling_interval_ms == 0 ||
1689 msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) {
1690 dev_err(&pdev->dev, "polling_interval_ms is too small\n");
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001691 return -EINVAL;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001692 }
1693
Chanwoo Choi8fcfe082012-09-20 21:20:05 -07001694 if (!desc->charging_max_duration_ms ||
1695 !desc->discharging_max_duration_ms) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001696 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 -07001697 desc->charging_max_duration_ms = 0;
1698 desc->discharging_max_duration_ms = 0;
1699 }
1700
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001701 platform_set_drvdata(pdev, cm);
1702
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001703 memcpy(&cm->charger_psy_desc, &psy_default, sizeof(psy_default));
Axel Linbb2a95c2012-01-12 12:56:35 +08001704
Chanwoo Choi41468a12012-11-22 16:54:26 +09001705 if (!desc->psy_name)
Axel Linbb2a95c2012-01-12 12:56:35 +08001706 strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001707 else
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001708 strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001709 cm->charger_psy_desc.name = cm->psy_name_buf;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001710
1711 /* Allocate for psy properties because they may vary */
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001712 cm->charger_psy_desc.properties = devm_kzalloc(&pdev->dev,
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001713 sizeof(enum power_supply_property)
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001714 * (ARRAY_SIZE(default_charger_props) +
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001715 NUM_CHARGER_PSY_OPTIONAL), GFP_KERNEL);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001716 if (!cm->charger_psy_desc.properties)
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001717 return -ENOMEM;
1718
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001719 memcpy(cm->charger_psy_desc.properties, default_charger_props,
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001720 sizeof(enum power_supply_property) *
1721 ARRAY_SIZE(default_charger_props));
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001722 cm->charger_psy_desc.num_properties = psy_default.num_properties;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001723
1724 /* Find which optional psy-properties are available */
Krzysztof Kozlowskib70229b2015-03-12 08:44:10 +01001725 if (!power_supply_get_property(fuel_gauge,
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001726 POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001727 cm->charger_psy_desc.properties[cm->charger_psy_desc.num_properties] =
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001728 POWER_SUPPLY_PROP_CHARGE_NOW;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001729 cm->charger_psy_desc.num_properties++;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001730 }
Krzysztof Kozlowskib70229b2015-03-12 08:44:10 +01001731 if (!power_supply_get_property(fuel_gauge,
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001732 POWER_SUPPLY_PROP_CURRENT_NOW,
1733 &val)) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001734 cm->charger_psy_desc.properties[cm->charger_psy_desc.num_properties] =
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001735 POWER_SUPPLY_PROP_CURRENT_NOW;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001736 cm->charger_psy_desc.num_properties++;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001737 }
Axel Linbb2a95c2012-01-12 12:56:35 +08001738
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +02001739 ret = cm_init_thermal_data(cm, fuel_gauge);
Jonghwa Lee5c49a622013-12-18 15:42:34 +09001740 if (ret) {
1741 dev_err(&pdev->dev, "Failed to initialize thermal data\n");
1742 cm->desc->measure_battery_temp = false;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001743 }
1744
Chanwoo Choid829dc72012-05-05 06:24:10 -07001745 INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk);
1746
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001747 cm->charger_psy = power_supply_register(NULL, &cm->charger_psy_desc,
1748 &psy_cfg);
1749 if (IS_ERR(cm->charger_psy)) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001750 dev_err(&pdev->dev, "Cannot register charger-manager with name \"%s\"\n",
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001751 cm->charger_psy->desc->name);
1752 return PTR_ERR(cm->charger_psy);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001753 }
1754
Chanwoo Choi41468a12012-11-22 16:54:26 +09001755 /* Register extcon device for charger cable */
1756 ret = charger_manager_register_extcon(cm);
1757 if (ret < 0) {
1758 dev_err(&pdev->dev, "Cannot initialize extcon device\n");
1759 goto err_reg_extcon;
Chanwoo Choi3950c782012-09-21 18:49:37 +09001760 }
1761
Chanwoo Choi41468a12012-11-22 16:54:26 +09001762 /* Register sysfs entry for charger(regulator) */
1763 ret = charger_manager_register_sysfs(cm);
1764 if (ret < 0) {
1765 dev_err(&pdev->dev,
1766 "Cannot initialize sysfs entry of regulator\n");
1767 goto err_reg_sysfs;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001768 }
1769
1770 /* Add to the list */
1771 mutex_lock(&cm_list_mtx);
1772 list_add(&cm->entry, &cm_list);
1773 mutex_unlock(&cm_list_mtx);
1774
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001775 /*
1776 * Charger-manager is capable of waking up the systme from sleep
1777 * when event is happend through cm_notify_event()
1778 */
1779 device_init_wakeup(&pdev->dev, true);
1780 device_set_wakeup_capable(&pdev->dev, false);
1781
Chanwoo Choib1022e22014-08-26 13:41:39 +09001782 /*
1783 * Charger-manager have to check the charging state right after
1784 * tialization of charger-manager and then update current charging
1785 * state.
1786 */
1787 cm_monitor();
1788
Chanwoo Choid829dc72012-05-05 06:24:10 -07001789 schedule_work(&setup_polling);
1790
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001791 return 0;
1792
Chanwoo Choi41468a12012-11-22 16:54:26 +09001793err_reg_sysfs:
Chanwoo Choi3950c782012-09-21 18:49:37 +09001794 for (i = 0; i < desc->num_charger_regulators; i++) {
1795 struct charger_regulator *charger;
1796
1797 charger = &desc->charger_regulators[i];
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001798 sysfs_remove_group(&cm->charger_psy->dev.kobj,
Chanwoo Choi3950c782012-09-21 18:49:37 +09001799 &charger->attr_g);
Chanwoo Choi3950c782012-09-21 18:49:37 +09001800 }
Chanwoo Choi41468a12012-11-22 16:54:26 +09001801err_reg_extcon:
1802 for (i = 0; i < desc->num_charger_regulators; i++) {
1803 struct charger_regulator *charger;
1804
1805 charger = &desc->charger_regulators[i];
1806 for (j = 0; j < charger->num_cables; j++) {
Chanwoo Choibee737b2012-07-12 15:03:25 +09001807 struct charger_cable *cable = &charger->cables[j];
Jonghwa Lee3cc9d2692013-06-25 14:02:49 +09001808 /* Remove notifier block if only edev exists */
1809 if (cable->extcon_dev.edev)
1810 extcon_unregister_interest(&cable->extcon_dev);
Chanwoo Choibee737b2012-07-12 15:03:25 +09001811 }
Chanwoo Choi41468a12012-11-22 16:54:26 +09001812
Chanwoo Choibee737b2012-07-12 15:03:25 +09001813 regulator_put(desc->charger_regulators[i].consumer);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001814 }
Chanwoo Choibee737b2012-07-12 15:03:25 +09001815
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001816 power_supply_unregister(cm->charger_psy);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001817
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001818 return ret;
1819}
1820
Bill Pemberton415ec692012-11-19 13:26:07 -05001821static int charger_manager_remove(struct platform_device *pdev)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001822{
1823 struct charger_manager *cm = platform_get_drvdata(pdev);
1824 struct charger_desc *desc = cm->desc;
Chanwoo Choibee737b2012-07-12 15:03:25 +09001825 int i = 0;
1826 int j = 0;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001827
1828 /* Remove from the list */
1829 mutex_lock(&cm_list_mtx);
1830 list_del(&cm->entry);
1831 mutex_unlock(&cm_list_mtx);
1832
Tejun Heo2fbb5202012-12-21 17:56:51 -08001833 cancel_work_sync(&setup_polling);
1834 cancel_delayed_work_sync(&cm_monitor_work);
Chanwoo Choid829dc72012-05-05 06:24:10 -07001835
Chanwoo Choibee737b2012-07-12 15:03:25 +09001836 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1837 struct charger_regulator *charger
1838 = &desc->charger_regulators[i];
1839 for (j = 0 ; j < charger->num_cables ; j++) {
1840 struct charger_cable *cable = &charger->cables[j];
1841 extcon_unregister_interest(&cable->extcon_dev);
1842 }
1843 }
1844
1845 for (i = 0 ; i < desc->num_charger_regulators ; i++)
1846 regulator_put(desc->charger_regulators[i].consumer);
1847
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001848 power_supply_unregister(cm->charger_psy);
Chanwoo Choid829dc72012-05-05 06:24:10 -07001849
1850 try_charger_enable(cm, false);
1851
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001852 return 0;
1853}
1854
Axel Lin1bbe24d2012-01-11 17:19:45 +08001855static const struct platform_device_id charger_manager_id[] = {
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001856 { "charger-manager", 0 },
1857 { },
1858};
Axel Lin1bbe24d2012-01-11 17:19:45 +08001859MODULE_DEVICE_TABLE(platform, charger_manager_id);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001860
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001861static int cm_suspend_noirq(struct device *dev)
1862{
1863 int ret = 0;
1864
1865 if (device_may_wakeup(dev)) {
1866 device_set_wakeup_capable(dev, false);
1867 ret = -EAGAIN;
1868 }
1869
1870 return ret;
1871}
1872
Jonghwa Leec1155c62014-12-19 17:55:13 +09001873static bool cm_need_to_awake(void)
1874{
1875 struct charger_manager *cm;
1876
1877 if (cm_timer)
1878 return false;
1879
1880 mutex_lock(&cm_list_mtx);
1881 list_for_each_entry(cm, &cm_list, entry) {
1882 if (is_charging(cm)) {
1883 mutex_unlock(&cm_list_mtx);
1884 return true;
1885 }
1886 }
1887 mutex_unlock(&cm_list_mtx);
1888
1889 return false;
1890}
1891
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001892static int cm_suspend_prepare(struct device *dev)
1893{
Axel Linbb2a95c2012-01-12 12:56:35 +08001894 struct charger_manager *cm = dev_get_drvdata(dev);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001895
Jonghwa Leec1155c62014-12-19 17:55:13 +09001896 if (cm_need_to_awake())
1897 return -EBUSY;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001898
Jonghwa Leec1155c62014-12-19 17:55:13 +09001899 if (!cm_suspended)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001900 cm_suspended = true;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001901
Jonghwa Leec1155c62014-12-19 17:55:13 +09001902 cm_timer_set = cm_setup_timer();
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001903
Jonghwa Leec1155c62014-12-19 17:55:13 +09001904 if (cm_timer_set) {
1905 cancel_work_sync(&setup_polling);
1906 cancel_delayed_work_sync(&cm_monitor_work);
1907 cancel_delayed_work(&cm->fullbatt_vchk_work);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001908 }
1909
1910 return 0;
1911}
1912
1913static void cm_suspend_complete(struct device *dev)
1914{
Axel Linbb2a95c2012-01-12 12:56:35 +08001915 struct charger_manager *cm = dev_get_drvdata(dev);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001916
Jonghwa Leec1155c62014-12-19 17:55:13 +09001917 if (cm_suspended)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001918 cm_suspended = false;
Jonghwa Leec1155c62014-12-19 17:55:13 +09001919
1920 if (cm_timer_set) {
1921 ktime_t remain;
1922
1923 alarm_cancel(cm_timer);
1924 cm_timer_set = false;
1925 remain = alarm_expires_remaining(cm_timer);
1926 cm_suspend_duration_ms -= ktime_to_ms(remain);
1927 schedule_work(&setup_polling);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001928 }
1929
Jonghwa Leec1155c62014-12-19 17:55:13 +09001930 _cm_monitor(cm);
1931
Chanwoo Choid829dc72012-05-05 06:24:10 -07001932 /* Re-enqueue delayed work (fullbatt_vchk_work) */
1933 if (cm->fullbatt_vchk_jiffies_at) {
1934 unsigned long delay = 0;
1935 unsigned long now = jiffies + CM_JIFFIES_SMALL;
1936
1937 if (time_after_eq(now, cm->fullbatt_vchk_jiffies_at)) {
1938 delay = (unsigned long)((long)now
1939 - (long)(cm->fullbatt_vchk_jiffies_at));
1940 delay = jiffies_to_msecs(delay);
1941 } else {
1942 delay = 0;
1943 }
1944
1945 /*
Jonghwa Leec1155c62014-12-19 17:55:13 +09001946 * Account for cm_suspend_duration_ms with assuming that
1947 * timer stops in suspend.
Chanwoo Choid829dc72012-05-05 06:24:10 -07001948 */
Jonghwa Leec1155c62014-12-19 17:55:13 +09001949 if (delay > cm_suspend_duration_ms)
1950 delay -= cm_suspend_duration_ms;
1951 else
1952 delay = 0;
Chanwoo Choid829dc72012-05-05 06:24:10 -07001953
1954 queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
1955 msecs_to_jiffies(delay));
1956 }
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001957 device_set_wakeup_capable(cm->dev, false);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001958}
1959
1960static const struct dev_pm_ops charger_manager_pm = {
1961 .prepare = cm_suspend_prepare,
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001962 .suspend_noirq = cm_suspend_noirq,
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001963 .complete = cm_suspend_complete,
1964};
1965
1966static struct platform_driver charger_manager_driver = {
1967 .driver = {
1968 .name = "charger-manager",
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001969 .pm = &charger_manager_pm,
Jonghwa Lee856ee612013-12-18 15:42:35 +09001970 .of_match_table = charger_manager_match,
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001971 },
1972 .probe = charger_manager_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -05001973 .remove = charger_manager_remove,
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001974 .id_table = charger_manager_id,
1975};
1976
1977static int __init charger_manager_init(void)
1978{
Chanwoo Choid829dc72012-05-05 06:24:10 -07001979 cm_wq = create_freezable_workqueue("charger_manager");
1980 INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller);
1981
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001982 return platform_driver_register(&charger_manager_driver);
1983}
1984late_initcall(charger_manager_init);
1985
1986static void __exit charger_manager_cleanup(void)
1987{
Chanwoo Choid829dc72012-05-05 06:24:10 -07001988 destroy_workqueue(cm_wq);
1989 cm_wq = NULL;
1990
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001991 platform_driver_unregister(&charger_manager_driver);
1992}
1993module_exit(charger_manager_cleanup);
1994
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001995/**
1996 * find_power_supply - find the associated power_supply of charger
1997 * @cm: the Charger Manager representing the battery
1998 * @psy: pointer to instance of charger's power_supply
1999 */
2000static bool find_power_supply(struct charger_manager *cm,
2001 struct power_supply *psy)
2002{
2003 int i;
2004 bool found = false;
2005
Krzysztof Kozlowskicdaf3e12014-10-13 15:34:31 +02002006 for (i = 0; cm->desc->psy_charger_stat[i]; i++) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002007 if (!strcmp(psy->desc->name, cm->desc->psy_charger_stat[i])) {
Chanwoo Choidfeccb12012-05-05 06:26:47 -07002008 found = true;
2009 break;
2010 }
2011 }
2012
2013 return found;
2014}
2015
2016/**
2017 * cm_notify_event - charger driver notify Charger Manager of charger event
2018 * @psy: pointer to instance of charger's power_supply
2019 * @type: type of charger event
2020 * @msg: optional message passed to uevent_notify fuction
2021 */
2022void cm_notify_event(struct power_supply *psy, enum cm_event_types type,
2023 char *msg)
2024{
2025 struct charger_manager *cm;
2026 bool found_power_supply = false;
2027
2028 if (psy == NULL)
2029 return;
2030
2031 mutex_lock(&cm_list_mtx);
2032 list_for_each_entry(cm, &cm_list, entry) {
2033 found_power_supply = find_power_supply(cm, psy);
2034 if (found_power_supply)
2035 break;
2036 }
2037 mutex_unlock(&cm_list_mtx);
2038
2039 if (!found_power_supply)
2040 return;
2041
2042 switch (type) {
2043 case CM_EVENT_BATT_FULL:
2044 fullbatt_handler(cm);
2045 break;
2046 case CM_EVENT_BATT_OUT:
2047 battout_handler(cm);
2048 break;
2049 case CM_EVENT_BATT_IN:
2050 case CM_EVENT_EXT_PWR_IN_OUT ... CM_EVENT_CHG_START_STOP:
2051 misc_event_handler(cm, type);
2052 break;
2053 case CM_EVENT_UNKNOWN:
2054 case CM_EVENT_OTHERS:
2055 uevent_notify(cm, msg ? msg : default_event_names[type]);
2056 break;
2057 default:
Joe Perchese5409cb2013-06-06 18:25:12 -07002058 dev_err(cm->dev, "%s: type not specified\n", __func__);
Chanwoo Choidfeccb12012-05-05 06:26:47 -07002059 break;
2060 }
2061}
2062EXPORT_SYMBOL_GPL(cm_notify_event);
2063
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09002064MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
2065MODULE_DESCRIPTION("Charger Manager");
2066MODULE_LICENSE("GPL");