blob: 14b0d85318eb59fac2fa9837f85750cfe31b5476 [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
106 ret = psy->get_property(psy,
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900107 POWER_SUPPLY_PROP_PRESENT, &val);
108 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
121 ret = psy->get_property(psy, POWER_SUPPLY_PROP_PRESENT,
122 &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
158 ret = psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &val);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900159 if (ret == 0 && val.intval) {
160 online = true;
161 break;
162 }
163 }
164
165 return online;
166}
167
168/**
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900169 * get_batt_uV - Get the voltage level of the battery
170 * @cm: the Charger Manager representing the battery.
171 * @uV: the voltage level returned.
172 *
173 * Returns 0 if there is no error.
174 * Returns a negative value on error.
175 */
176static int get_batt_uV(struct charger_manager *cm, int *uV)
177{
178 union power_supply_propval val;
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200179 struct power_supply *fuel_gauge;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900180 int ret;
181
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200182 fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
183 if (!fuel_gauge)
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900184 return -ENODEV;
185
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200186 ret = fuel_gauge->get_property(fuel_gauge,
Axel Linbb2a95c2012-01-12 12:56:35 +0800187 POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900188 if (ret)
189 return ret;
190
191 *uV = val.intval;
192 return 0;
193}
194
195/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900196 * is_charging - Returns true if the battery is being charged.
197 * @cm: the Charger Manager representing the battery.
198 */
199static bool is_charging(struct charger_manager *cm)
200{
201 int i, ret;
202 bool charging = false;
Krzysztof Kozlowskicdaf3e12014-10-13 15:34:31 +0200203 struct power_supply *psy;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900204 union power_supply_propval val;
205
206 /* If there is no battery, it cannot be charged */
207 if (!is_batt_present(cm))
208 return false;
209
210 /* If at least one of the charger is charging, return yes */
Krzysztof Kozlowskicdaf3e12014-10-13 15:34:31 +0200211 for (i = 0; cm->desc->psy_charger_stat[i]; i++) {
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900212 /* 1. The charger sholuld not be DISABLED */
213 if (cm->emergency_stop)
214 continue;
215 if (!cm->charger_enabled)
216 continue;
217
Krzysztof Kozlowskicdaf3e12014-10-13 15:34:31 +0200218 psy = power_supply_get_by_name(cm->desc->psy_charger_stat[i]);
219 if (!psy) {
220 dev_err(cm->dev, "Cannot find power supply \"%s\"\n",
221 cm->desc->psy_charger_stat[i]);
222 continue;
223 }
224
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900225 /* 2. The charger should be online (ext-power) */
Krzysztof Kozlowskicdaf3e12014-10-13 15:34:31 +0200226 ret = psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &val);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900227 if (ret) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700228 dev_warn(cm->dev, "Cannot read ONLINE value from %s\n",
229 cm->desc->psy_charger_stat[i]);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900230 continue;
231 }
232 if (val.intval == 0)
233 continue;
234
235 /*
236 * 3. The charger should not be FULL, DISCHARGING,
237 * or NOT_CHARGING.
238 */
Krzysztof Kozlowskicdaf3e12014-10-13 15:34:31 +0200239 ret = psy->get_property(psy, POWER_SUPPLY_PROP_STATUS, &val);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900240 if (ret) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700241 dev_warn(cm->dev, "Cannot read STATUS value from %s\n",
242 cm->desc->psy_charger_stat[i]);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900243 continue;
244 }
245 if (val.intval == POWER_SUPPLY_STATUS_FULL ||
246 val.intval == POWER_SUPPLY_STATUS_DISCHARGING ||
247 val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING)
248 continue;
249
250 /* Then, this is charging. */
251 charging = true;
252 break;
253 }
254
255 return charging;
256}
257
258/**
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900259 * is_full_charged - Returns true if the battery is fully charged.
260 * @cm: the Charger Manager representing the battery.
261 */
262static bool is_full_charged(struct charger_manager *cm)
263{
264 struct charger_desc *desc = cm->desc;
265 union power_supply_propval val;
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200266 struct power_supply *fuel_gauge;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900267 int ret = 0;
268 int uV;
269
270 /* If there is no battery, it cannot be charged */
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900271 if (!is_batt_present(cm))
272 return false;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900273
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200274 fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
275 if (!fuel_gauge)
276 return false;
277
278 if (desc->fullbatt_full_capacity > 0) {
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900279 val.intval = 0;
280
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900281 /* Not full if capacity of fuel gauge isn't full */
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200282 ret = fuel_gauge->get_property(fuel_gauge,
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900283 POWER_SUPPLY_PROP_CHARGE_FULL, &val);
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900284 if (!ret && val.intval > desc->fullbatt_full_capacity)
285 return true;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900286 }
287
288 /* Full, if it's over the fullbatt voltage */
289 if (desc->fullbatt_uV > 0) {
290 ret = get_batt_uV(cm, &uV);
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900291 if (!ret && uV >= desc->fullbatt_uV)
292 return true;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900293 }
294
295 /* Full, if the capacity is more than fullbatt_soc */
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200296 if (desc->fullbatt_soc > 0) {
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900297 val.intval = 0;
298
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200299 ret = fuel_gauge->get_property(fuel_gauge,
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900300 POWER_SUPPLY_PROP_CAPACITY, &val);
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900301 if (!ret && val.intval >= desc->fullbatt_soc)
302 return true;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900303 }
304
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900305 return false;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900306}
307
308/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900309 * is_polling_required - Return true if need to continue polling for this CM.
310 * @cm: the Charger Manager representing the battery.
311 */
312static bool is_polling_required(struct charger_manager *cm)
313{
314 switch (cm->desc->polling_mode) {
315 case CM_POLL_DISABLE:
316 return false;
317 case CM_POLL_ALWAYS:
318 return true;
319 case CM_POLL_EXTERNAL_POWER_ONLY:
320 return is_ext_pwr_online(cm);
321 case CM_POLL_CHARGING_ONLY:
322 return is_charging(cm);
323 default:
324 dev_warn(cm->dev, "Incorrect polling_mode (%d)\n",
Joe Perchese5409cb2013-06-06 18:25:12 -0700325 cm->desc->polling_mode);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900326 }
327
328 return false;
329}
330
331/**
332 * try_charger_enable - Enable/Disable chargers altogether
333 * @cm: the Charger Manager representing the battery.
334 * @enable: true: enable / false: disable
335 *
336 * Note that Charger Manager keeps the charger enabled regardless whether
337 * the charger is charging or not (because battery is full or no external
338 * power source exists) except when CM needs to disable chargers forcibly
339 * bacause of emergency causes; when the battery is overheated or too cold.
340 */
341static int try_charger_enable(struct charger_manager *cm, bool enable)
342{
343 int err = 0, i;
344 struct charger_desc *desc = cm->desc;
345
346 /* Ignore if it's redundent command */
Axel Linbb2a95c2012-01-12 12:56:35 +0800347 if (enable == cm->charger_enabled)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900348 return 0;
349
350 if (enable) {
351 if (cm->emergency_stop)
352 return -EAGAIN;
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700353
354 /*
355 * Save start time of charging to limit
356 * maximum possible charging time.
357 */
358 cm->charging_start_time = ktime_to_ms(ktime_get());
359 cm->charging_end_time = 0;
360
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900361 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
Chanwoo Choi3950c782012-09-21 18:49:37 +0900362 if (desc->charger_regulators[i].externally_control)
363 continue;
364
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900365 err = regulator_enable(desc->charger_regulators[i].consumer);
366 if (err < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700367 dev_warn(cm->dev, "Cannot enable %s regulator\n",
368 desc->charger_regulators[i].regulator_name);
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900369 }
370 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900371 } else {
372 /*
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700373 * Save end time of charging to maintain fully charged state
374 * of battery after full-batt.
375 */
376 cm->charging_start_time = 0;
377 cm->charging_end_time = ktime_to_ms(ktime_get());
378
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900379 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
Chanwoo Choi3950c782012-09-21 18:49:37 +0900380 if (desc->charger_regulators[i].externally_control)
381 continue;
382
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900383 err = regulator_disable(desc->charger_regulators[i].consumer);
384 if (err < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700385 dev_warn(cm->dev, "Cannot disable %s regulator\n",
386 desc->charger_regulators[i].regulator_name);
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900387 }
388 }
389
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900390 /*
391 * Abnormal battery state - Stop charging forcibly,
392 * even if charger was enabled at the other places
393 */
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900394 for (i = 0; i < desc->num_charger_regulators; i++) {
395 if (regulator_is_enabled(
396 desc->charger_regulators[i].consumer)) {
397 regulator_force_disable(
398 desc->charger_regulators[i].consumer);
Joe Perchese5409cb2013-06-06 18:25:12 -0700399 dev_warn(cm->dev, "Disable regulator(%s) forcibly\n",
400 desc->charger_regulators[i].regulator_name);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900401 }
402 }
403 }
404
405 if (!err)
406 cm->charger_enabled = enable;
407
408 return err;
409}
410
411/**
Chanwoo Choid829dc72012-05-05 06:24:10 -0700412 * try_charger_restart - Restart charging.
413 * @cm: the Charger Manager representing the battery.
414 *
415 * Restart charging by turning off and on the charger.
416 */
417static int try_charger_restart(struct charger_manager *cm)
418{
419 int err;
420
421 if (cm->emergency_stop)
422 return -EAGAIN;
423
424 err = try_charger_enable(cm, false);
425 if (err)
426 return err;
427
428 return try_charger_enable(cm, true);
429}
430
431/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900432 * uevent_notify - Let users know something has changed.
433 * @cm: the Charger Manager representing the battery.
434 * @event: the event string.
435 *
436 * If @event is null, it implies that uevent_notify is called
437 * by resume function. When called in the resume function, cm_suspended
438 * should be already reset to false in order to let uevent_notify
439 * notify the recent event during the suspend to users. While
440 * suspended, uevent_notify does not notify users, but tracks
441 * events so that uevent_notify can notify users later after resumed.
442 */
443static void uevent_notify(struct charger_manager *cm, const char *event)
444{
445 static char env_str[UEVENT_BUF_SIZE + 1] = "";
446 static char env_str_save[UEVENT_BUF_SIZE + 1] = "";
447
448 if (cm_suspended) {
449 /* Nothing in suspended-event buffer */
450 if (env_str_save[0] == 0) {
451 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
452 return; /* status not changed */
453 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
454 return;
455 }
456
457 if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE))
458 return; /* Duplicated. */
Axel Linbb2a95c2012-01-12 12:56:35 +0800459 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900460 return;
461 }
462
463 if (event == NULL) {
464 /* No messages pending */
465 if (!env_str_save[0])
466 return;
467
468 strncpy(env_str, env_str_save, UEVENT_BUF_SIZE);
469 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
470 env_str_save[0] = 0;
471
472 return;
473 }
474
475 /* status not changed */
476 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
477 return;
478
479 /* save the status and notify the update */
480 strncpy(env_str, event, UEVENT_BUF_SIZE);
481 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
482
Joe Perchese5409cb2013-06-06 18:25:12 -0700483 dev_info(cm->dev, "%s\n", event);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900484}
485
486/**
Chanwoo Choid829dc72012-05-05 06:24:10 -0700487 * fullbatt_vchk - Check voltage drop some times after "FULL" event.
488 * @work: the work_struct appointing the function
489 *
490 * If a user has designated "fullbatt_vchkdrop_ms/uV" values with
491 * charger_desc, Charger Manager checks voltage drop after the battery
492 * "FULL" event. It checks whether the voltage has dropped more than
493 * fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms.
494 */
495static void fullbatt_vchk(struct work_struct *work)
496{
497 struct delayed_work *dwork = to_delayed_work(work);
498 struct charger_manager *cm = container_of(dwork,
499 struct charger_manager, fullbatt_vchk_work);
500 struct charger_desc *desc = cm->desc;
501 int batt_uV, err, diff;
502
503 /* remove the appointment for fullbatt_vchk */
504 cm->fullbatt_vchk_jiffies_at = 0;
505
506 if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
507 return;
508
509 err = get_batt_uV(cm, &batt_uV);
510 if (err) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700511 dev_err(cm->dev, "%s: get_batt_uV error(%d)\n", __func__, err);
Chanwoo Choid829dc72012-05-05 06:24:10 -0700512 return;
513 }
514
Chanwoo Choif36b9dd2012-11-22 16:53:51 +0900515 diff = desc->fullbatt_uV - batt_uV;
516 if (diff < 0)
517 return;
Chanwoo Choid829dc72012-05-05 06:24:10 -0700518
Joe Perchese5409cb2013-06-06 18:25:12 -0700519 dev_info(cm->dev, "VBATT dropped %duV after full-batt\n", diff);
Chanwoo Choid829dc72012-05-05 06:24:10 -0700520
521 if (diff > desc->fullbatt_vchkdrop_uV) {
522 try_charger_restart(cm);
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700523 uevent_notify(cm, "Recharging");
Chanwoo Choid829dc72012-05-05 06:24:10 -0700524 }
525}
526
527/**
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700528 * check_charging_duration - Monitor charging/discharging duration
529 * @cm: the Charger Manager representing the battery.
530 *
531 * If whole charging duration exceed 'charging_max_duration_ms',
532 * cm stop charging to prevent overcharge/overheat. If discharging
533 * duration exceed 'discharging _max_duration_ms', charger cable is
534 * attached, after full-batt, cm start charging to maintain fully
535 * charged state for battery.
536 */
537static int check_charging_duration(struct charger_manager *cm)
538{
539 struct charger_desc *desc = cm->desc;
540 u64 curr = ktime_to_ms(ktime_get());
541 u64 duration;
542 int ret = false;
543
544 if (!desc->charging_max_duration_ms &&
545 !desc->discharging_max_duration_ms)
546 return ret;
547
548 if (cm->charger_enabled) {
549 duration = curr - cm->charging_start_time;
550
551 if (duration > desc->charging_max_duration_ms) {
Jonghwa Lee856ee612013-12-18 15:42:35 +0900552 dev_info(cm->dev, "Charging duration exceed %ums\n",
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700553 desc->charging_max_duration_ms);
554 uevent_notify(cm, "Discharging");
555 try_charger_enable(cm, false);
556 ret = true;
557 }
558 } else if (is_ext_pwr_online(cm) && !cm->charger_enabled) {
559 duration = curr - cm->charging_end_time;
560
561 if (duration > desc->charging_max_duration_ms &&
562 is_ext_pwr_online(cm)) {
Jonghwa Lee856ee612013-12-18 15:42:35 +0900563 dev_info(cm->dev, "Discharging duration exceed %ums\n",
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700564 desc->discharging_max_duration_ms);
Joe Perchese5409cb2013-06-06 18:25:12 -0700565 uevent_notify(cm, "Recharging");
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700566 try_charger_enable(cm, true);
567 ret = true;
568 }
569 }
570
571 return ret;
572}
573
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200574static int cm_get_battery_temperature_by_psy(struct charger_manager *cm,
575 int *temp)
576{
577 struct power_supply *fuel_gauge;
578
579 fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
580 if (!fuel_gauge)
581 return -ENODEV;
582
583 return fuel_gauge->get_property(fuel_gauge,
584 POWER_SUPPLY_PROP_TEMP,
585 (union power_supply_propval *)temp);
586}
587
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900588static int cm_get_battery_temperature(struct charger_manager *cm,
589 int *temp)
590{
591 int ret;
592
593 if (!cm->desc->measure_battery_temp)
594 return -ENODEV;
595
596#ifdef CONFIG_THERMAL
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200597 if (cm->tzd_batt) {
598 ret = thermal_zone_get_temp(cm->tzd_batt, (unsigned long *)temp);
599 if (!ret)
600 /* Calibrate temperature unit */
601 *temp /= 100;
602 } else
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900603#endif
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200604 {
605 /* if-else continued from CONFIG_THERMAL */
606 ret = cm_get_battery_temperature_by_psy(cm, temp);
607 }
608
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900609 return ret;
610}
611
612static int cm_check_thermal_status(struct charger_manager *cm)
613{
614 struct charger_desc *desc = cm->desc;
615 int temp, upper_limit, lower_limit;
616 int ret = 0;
617
618 ret = cm_get_battery_temperature(cm, &temp);
619 if (ret) {
620 /* FIXME:
621 * No information of battery temperature might
622 * occur hazadous result. We have to handle it
623 * depending on battery type.
624 */
625 dev_err(cm->dev, "Failed to get battery temperature\n");
626 return 0;
627 }
628
629 upper_limit = desc->temp_max;
630 lower_limit = desc->temp_min;
631
632 if (cm->emergency_stop) {
633 upper_limit -= desc->temp_diff;
634 lower_limit += desc->temp_diff;
635 }
636
637 if (temp > upper_limit)
638 ret = CM_EVENT_BATT_OVERHEAT;
639 else if (temp < lower_limit)
640 ret = CM_EVENT_BATT_COLD;
641
642 return ret;
643}
644
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700645/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900646 * _cm_monitor - Monitor the temperature and return true for exceptions.
647 * @cm: the Charger Manager representing the battery.
648 *
649 * Returns true if there is an event to notify for the battery.
650 * (True if the status of "emergency_stop" changes)
651 */
652static bool _cm_monitor(struct charger_manager *cm)
653{
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900654 int temp_alrt;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900655
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900656 temp_alrt = cm_check_thermal_status(cm);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900657
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900658 /* It has been stopped already */
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900659 if (temp_alrt && cm->emergency_stop)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900660 return false;
661
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900662 /*
663 * Check temperature whether overheat or cold.
664 * If temperature is out of range normal state, stop charging.
665 */
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900666 if (temp_alrt) {
667 cm->emergency_stop = temp_alrt;
668 if (!try_charger_enable(cm, false))
669 uevent_notify(cm, default_event_names[temp_alrt]);
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900670
671 /*
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700672 * Check whole charging duration and discharing duration
673 * after full-batt.
674 */
675 } else if (!cm->emergency_stop && check_charging_duration(cm)) {
676 dev_dbg(cm->dev,
Joe Perchese5409cb2013-06-06 18:25:12 -0700677 "Charging/Discharging duration is out of range\n");
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700678 /*
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900679 * Check dropped voltage of battery. If battery voltage is more
680 * dropped than fullbatt_vchkdrop_uV after fully charged state,
681 * charger-manager have to recharge battery.
682 */
683 } else if (!cm->emergency_stop && is_ext_pwr_online(cm) &&
684 !cm->charger_enabled) {
685 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
686
687 /*
688 * Check whether fully charged state to protect overcharge
689 * if charger-manager is charging for battery.
690 */
691 } else if (!cm->emergency_stop && is_full_charged(cm) &&
692 cm->charger_enabled) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700693 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged\n");
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900694 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
695
696 try_charger_enable(cm, false);
697
698 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900699 } else {
700 cm->emergency_stop = 0;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900701 if (is_ext_pwr_online(cm)) {
702 if (!try_charger_enable(cm, true))
703 uevent_notify(cm, "CHARGING");
704 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900705 }
706
707 return true;
708}
709
710/**
711 * cm_monitor - Monitor every battery.
712 *
713 * Returns true if there is an event to notify from any of the batteries.
714 * (True if the status of "emergency_stop" changes)
715 */
716static bool cm_monitor(void)
717{
718 bool stop = false;
719 struct charger_manager *cm;
720
721 mutex_lock(&cm_list_mtx);
722
Axel Linbb2a95c2012-01-12 12:56:35 +0800723 list_for_each_entry(cm, &cm_list, entry) {
724 if (_cm_monitor(cm))
725 stop = true;
726 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900727
728 mutex_unlock(&cm_list_mtx);
729
730 return stop;
731}
732
Chanwoo Choid829dc72012-05-05 06:24:10 -0700733/**
734 * _setup_polling - Setup the next instance of polling.
735 * @work: work_struct of the function _setup_polling.
736 */
737static void _setup_polling(struct work_struct *work)
738{
739 unsigned long min = ULONG_MAX;
740 struct charger_manager *cm;
741 bool keep_polling = false;
742 unsigned long _next_polling;
743
744 mutex_lock(&cm_list_mtx);
745
746 list_for_each_entry(cm, &cm_list, entry) {
747 if (is_polling_required(cm) && cm->desc->polling_interval_ms) {
748 keep_polling = true;
749
750 if (min > cm->desc->polling_interval_ms)
751 min = cm->desc->polling_interval_ms;
752 }
753 }
754
755 polling_jiffy = msecs_to_jiffies(min);
756 if (polling_jiffy <= CM_JIFFIES_SMALL)
757 polling_jiffy = CM_JIFFIES_SMALL + 1;
758
759 if (!keep_polling)
760 polling_jiffy = ULONG_MAX;
761 if (polling_jiffy == ULONG_MAX)
762 goto out;
763
764 WARN(cm_wq == NULL, "charger-manager: workqueue not initialized"
765 ". try it later. %s\n", __func__);
766
Tejun Heo2fbb5202012-12-21 17:56:51 -0800767 /*
768 * Use mod_delayed_work() iff the next polling interval should
769 * occur before the currently scheduled one. If @cm_monitor_work
770 * isn't active, the end result is the same, so no need to worry
771 * about stale @next_polling.
772 */
Chanwoo Choid829dc72012-05-05 06:24:10 -0700773 _next_polling = jiffies + polling_jiffy;
774
Tejun Heo2fbb5202012-12-21 17:56:51 -0800775 if (time_before(_next_polling, next_polling)) {
Tejun Heo41f63c52012-08-03 10:30:47 -0700776 mod_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy);
Tejun Heo2fbb5202012-12-21 17:56:51 -0800777 next_polling = _next_polling;
778 } else {
779 if (queue_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy))
780 next_polling = _next_polling;
Chanwoo Choid829dc72012-05-05 06:24:10 -0700781 }
Chanwoo Choid829dc72012-05-05 06:24:10 -0700782out:
783 mutex_unlock(&cm_list_mtx);
784}
785static DECLARE_WORK(setup_polling, _setup_polling);
786
787/**
788 * cm_monitor_poller - The Monitor / Poller.
789 * @work: work_struct of the function cm_monitor_poller
790 *
791 * During non-suspended state, cm_monitor_poller is used to poll and monitor
792 * the batteries.
793 */
794static void cm_monitor_poller(struct work_struct *work)
795{
796 cm_monitor();
797 schedule_work(&setup_polling);
798}
799
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700800/**
801 * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL
802 * @cm: the Charger Manager representing the battery.
803 */
804static void fullbatt_handler(struct charger_manager *cm)
805{
806 struct charger_desc *desc = cm->desc;
807
808 if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
809 goto out;
810
811 if (cm_suspended)
812 device_set_wakeup_capable(cm->dev, true);
813
Tejun Heo41f63c52012-08-03 10:30:47 -0700814 mod_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
815 msecs_to_jiffies(desc->fullbatt_vchkdrop_ms));
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700816 cm->fullbatt_vchk_jiffies_at = jiffies + msecs_to_jiffies(
817 desc->fullbatt_vchkdrop_ms);
818
819 if (cm->fullbatt_vchk_jiffies_at == 0)
820 cm->fullbatt_vchk_jiffies_at = 1;
821
822out:
Joe Perchese5409cb2013-06-06 18:25:12 -0700823 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged\n");
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700824 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
825}
826
827/**
828 * battout_handler - Event handler for CM_EVENT_BATT_OUT
829 * @cm: the Charger Manager representing the battery.
830 */
831static void battout_handler(struct charger_manager *cm)
832{
833 if (cm_suspended)
834 device_set_wakeup_capable(cm->dev, true);
835
836 if (!is_batt_present(cm)) {
837 dev_emerg(cm->dev, "Battery Pulled Out!\n");
838 uevent_notify(cm, default_event_names[CM_EVENT_BATT_OUT]);
839 } else {
840 uevent_notify(cm, "Battery Reinserted?");
841 }
842}
843
844/**
845 * misc_event_handler - Handler for other evnets
846 * @cm: the Charger Manager representing the battery.
847 * @type: the Charger Manager representing the battery.
848 */
849static void misc_event_handler(struct charger_manager *cm,
850 enum cm_event_types type)
851{
852 if (cm_suspended)
853 device_set_wakeup_capable(cm->dev, true);
854
Tejun Heo2fbb5202012-12-21 17:56:51 -0800855 if (is_polling_required(cm) && cm->desc->polling_interval_ms)
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700856 schedule_work(&setup_polling);
857 uevent_notify(cm, default_event_names[type]);
858}
859
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900860static int charger_get_property(struct power_supply *psy,
861 enum power_supply_property psp,
862 union power_supply_propval *val)
863{
864 struct charger_manager *cm = container_of(psy,
865 struct charger_manager, charger_psy);
866 struct charger_desc *desc = cm->desc;
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200867 struct power_supply *fuel_gauge;
Anton Vorontsovdf58c042012-03-15 21:01:28 +0400868 int ret = 0;
869 int uV;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900870
871 switch (psp) {
872 case POWER_SUPPLY_PROP_STATUS:
873 if (is_charging(cm))
874 val->intval = POWER_SUPPLY_STATUS_CHARGING;
875 else if (is_ext_pwr_online(cm))
876 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
877 else
878 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
879 break;
880 case POWER_SUPPLY_PROP_HEALTH:
881 if (cm->emergency_stop > 0)
882 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
883 else if (cm->emergency_stop < 0)
884 val->intval = POWER_SUPPLY_HEALTH_COLD;
885 else
886 val->intval = POWER_SUPPLY_HEALTH_GOOD;
887 break;
888 case POWER_SUPPLY_PROP_PRESENT:
889 if (is_batt_present(cm))
890 val->intval = 1;
891 else
892 val->intval = 0;
893 break;
894 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Anton Vorontsovdf58c042012-03-15 21:01:28 +0400895 ret = get_batt_uV(cm, &val->intval);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900896 break;
897 case POWER_SUPPLY_PROP_CURRENT_NOW:
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200898 fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
899 if (!fuel_gauge) {
900 ret = -ENODEV;
901 break;
902 }
903 ret = fuel_gauge->get_property(fuel_gauge,
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900904 POWER_SUPPLY_PROP_CURRENT_NOW, val);
905 break;
906 case POWER_SUPPLY_PROP_TEMP:
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900907 case POWER_SUPPLY_PROP_TEMP_AMBIENT:
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900908 return cm_get_battery_temperature(cm, &val->intval);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900909 case POWER_SUPPLY_PROP_CAPACITY:
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200910 fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge);
911 if (!fuel_gauge) {
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900912 ret = -ENODEV;
913 break;
914 }
915
916 if (!is_batt_present(cm)) {
917 /* There is no battery. Assume 100% */
918 val->intval = 100;
919 break;
920 }
921
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200922 ret = fuel_gauge->get_property(fuel_gauge,
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900923 POWER_SUPPLY_PROP_CAPACITY, val);
924 if (ret)
925 break;
926
927 if (val->intval > 100) {
928 val->intval = 100;
929 break;
930 }
931 if (val->intval < 0)
932 val->intval = 0;
933
934 /* Do not adjust SOC when charging: voltage is overrated */
935 if (is_charging(cm))
936 break;
937
938 /*
939 * If the capacity value is inconsistent, calibrate it base on
940 * the battery voltage values and the thresholds given as desc
941 */
942 ret = get_batt_uV(cm, &uV);
943 if (ret) {
944 /* Voltage information not available. No calibration */
945 ret = 0;
946 break;
947 }
948
949 if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
950 !is_charging(cm)) {
951 val->intval = 100;
952 break;
953 }
954
955 break;
956 case POWER_SUPPLY_PROP_ONLINE:
957 if (is_ext_pwr_online(cm))
958 val->intval = 1;
959 else
960 val->intval = 0;
961 break;
962 case POWER_SUPPLY_PROP_CHARGE_FULL:
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900963 if (is_full_charged(cm))
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900964 val->intval = 1;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900965 else
966 val->intval = 0;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900967 ret = 0;
968 break;
969 case POWER_SUPPLY_PROP_CHARGE_NOW:
970 if (is_charging(cm)) {
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +0200971 fuel_gauge = power_supply_get_by_name(
972 cm->desc->psy_fuel_gauge);
973 if (!fuel_gauge) {
974 ret = -ENODEV;
975 break;
976 }
977
978 ret = fuel_gauge->get_property(fuel_gauge,
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900979 POWER_SUPPLY_PROP_CHARGE_NOW,
980 val);
981 if (ret) {
982 val->intval = 1;
983 ret = 0;
984 } else {
985 /* If CHARGE_NOW is supplied, use it */
986 val->intval = (val->intval > 0) ?
987 val->intval : 1;
988 }
989 } else {
990 val->intval = 0;
991 }
992 break;
993 default:
994 return -EINVAL;
995 }
996 return ret;
997}
998
999#define NUM_CHARGER_PSY_OPTIONAL (4)
1000static enum power_supply_property default_charger_props[] = {
1001 /* Guaranteed to provide */
1002 POWER_SUPPLY_PROP_STATUS,
1003 POWER_SUPPLY_PROP_HEALTH,
1004 POWER_SUPPLY_PROP_PRESENT,
1005 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1006 POWER_SUPPLY_PROP_CAPACITY,
1007 POWER_SUPPLY_PROP_ONLINE,
1008 POWER_SUPPLY_PROP_CHARGE_FULL,
1009 /*
1010 * Optional properties are:
1011 * POWER_SUPPLY_PROP_CHARGE_NOW,
1012 * POWER_SUPPLY_PROP_CURRENT_NOW,
1013 * POWER_SUPPLY_PROP_TEMP, and
1014 * POWER_SUPPLY_PROP_TEMP_AMBIENT,
1015 */
1016};
1017
1018static struct power_supply psy_default = {
1019 .name = "battery",
1020 .type = POWER_SUPPLY_TYPE_BATTERY,
1021 .properties = default_charger_props,
1022 .num_properties = ARRAY_SIZE(default_charger_props),
1023 .get_property = charger_get_property,
Krzysztof Kozlowskiba9c9182014-10-07 17:47:37 +02001024 .no_thermal = true,
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001025};
1026
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001027/**
1028 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
1029 * for suspend_again.
1030 *
1031 * Returns true if the alarm is set for Charger Manager to use.
1032 * Returns false if
1033 * cm_setup_timer fails to set an alarm,
1034 * cm_setup_timer does not need to set an alarm for Charger Manager,
1035 * or an alarm previously configured is to be used.
1036 */
1037static bool cm_setup_timer(void)
1038{
1039 struct charger_manager *cm;
1040 unsigned int wakeup_ms = UINT_MAX;
Jonghwa Leec1155c62014-12-19 17:55:13 +09001041 int timer_req = 0;
1042
1043 if (time_after(next_polling, jiffies))
1044 CM_MIN_VALID(wakeup_ms,
1045 jiffies_to_msecs(next_polling - jiffies));
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001046
1047 mutex_lock(&cm_list_mtx);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001048 list_for_each_entry(cm, &cm_list, entry) {
Chanwoo Choid829dc72012-05-05 06:24:10 -07001049 unsigned int fbchk_ms = 0;
1050
1051 /* fullbatt_vchk is required. setup timer for that */
1052 if (cm->fullbatt_vchk_jiffies_at) {
1053 fbchk_ms = jiffies_to_msecs(cm->fullbatt_vchk_jiffies_at
1054 - jiffies);
1055 if (time_is_before_eq_jiffies(
1056 cm->fullbatt_vchk_jiffies_at) ||
1057 msecs_to_jiffies(fbchk_ms) < CM_JIFFIES_SMALL) {
1058 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
1059 fbchk_ms = 0;
1060 }
1061 }
1062 CM_MIN_VALID(wakeup_ms, fbchk_ms);
1063
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001064 /* Skip if polling is not required for this CM */
1065 if (!is_polling_required(cm) && !cm->emergency_stop)
1066 continue;
Jonghwa Leec1155c62014-12-19 17:55:13 +09001067 timer_req++;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001068 if (cm->desc->polling_interval_ms == 0)
1069 continue;
1070 CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
1071 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001072 mutex_unlock(&cm_list_mtx);
1073
Jonghwa Leec1155c62014-12-19 17:55:13 +09001074 if (timer_req && cm_timer) {
1075 ktime_t now, add;
1076
1077 /*
1078 * Set alarm with the polling interval (wakeup_ms)
1079 * The alarm time should be NOW + CM_RTC_SMALL or later.
1080 */
1081 if (wakeup_ms == UINT_MAX ||
1082 wakeup_ms < CM_RTC_SMALL * MSEC_PER_SEC)
1083 wakeup_ms = 2 * CM_RTC_SMALL * MSEC_PER_SEC;
1084
Joe Perchese5409cb2013-06-06 18:25:12 -07001085 pr_info("Charger Manager wakeup timer: %u ms\n", wakeup_ms);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001086
Jonghwa Leec1155c62014-12-19 17:55:13 +09001087 now = ktime_get_boottime();
1088 add = ktime_set(wakeup_ms / MSEC_PER_SEC,
1089 (wakeup_ms % MSEC_PER_SEC) * NSEC_PER_MSEC);
1090 alarm_start(cm_timer, ktime_add(now, add));
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001091
Jonghwa Leec1155c62014-12-19 17:55:13 +09001092 cm_suspend_duration_ms = wakeup_ms;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001093
Jonghwa Leec1155c62014-12-19 17:55:13 +09001094 return true;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001095 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001096 return false;
1097}
1098
Chanwoo Choibee737b2012-07-12 15:03:25 +09001099/**
1100 * charger_extcon_work - enable/diable charger according to the state
1101 * of charger cable
1102 *
1103 * @work: work_struct of the function charger_extcon_work.
1104 */
1105static void charger_extcon_work(struct work_struct *work)
1106{
1107 struct charger_cable *cable =
1108 container_of(work, struct charger_cable, wq);
Chanwoo Choi45cd4fb2012-07-12 15:03:29 +09001109 int ret;
1110
1111 if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) {
1112 ret = regulator_set_current_limit(cable->charger->consumer,
1113 cable->min_uA, cable->max_uA);
1114 if (ret < 0) {
1115 pr_err("Cannot set current limit of %s (%s)\n",
Joe Perchese5409cb2013-06-06 18:25:12 -07001116 cable->charger->regulator_name, cable->name);
Chanwoo Choi45cd4fb2012-07-12 15:03:29 +09001117 return;
1118 }
1119
1120 pr_info("Set current limit of %s : %duA ~ %duA\n",
Joe Perchese5409cb2013-06-06 18:25:12 -07001121 cable->charger->regulator_name,
1122 cable->min_uA, cable->max_uA);
Chanwoo Choi45cd4fb2012-07-12 15:03:29 +09001123 }
Chanwoo Choibee737b2012-07-12 15:03:25 +09001124
1125 try_charger_enable(cable->cm, cable->attached);
1126}
1127
1128/**
1129 * charger_extcon_notifier - receive the state of charger cable
1130 * when registered cable is attached or detached.
1131 *
1132 * @self: the notifier block of the charger_extcon_notifier.
1133 * @event: the cable state.
1134 * @ptr: the data pointer of notifier block.
1135 */
1136static int charger_extcon_notifier(struct notifier_block *self,
1137 unsigned long event, void *ptr)
1138{
1139 struct charger_cable *cable =
1140 container_of(self, struct charger_cable, nb);
1141
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001142 /*
1143 * The newly state of charger cable.
1144 * If cable is attached, cable->attached is true.
1145 */
Chanwoo Choibee737b2012-07-12 15:03:25 +09001146 cable->attached = event;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001147
1148 /*
1149 * Setup monitoring to check battery state
1150 * when charger cable is attached.
1151 */
1152 if (cable->attached && is_polling_required(cable->cm)) {
Tejun Heo2fbb5202012-12-21 17:56:51 -08001153 cancel_work_sync(&setup_polling);
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001154 schedule_work(&setup_polling);
1155 }
1156
1157 /*
1158 * Setup work for controlling charger(regulator)
1159 * according to charger cable.
1160 */
Chanwoo Choibee737b2012-07-12 15:03:25 +09001161 schedule_work(&cable->wq);
1162
1163 return NOTIFY_DONE;
1164}
1165
1166/**
1167 * charger_extcon_init - register external connector to use it
1168 * as the charger cable
1169 *
1170 * @cm: the Charger Manager representing the battery.
1171 * @cable: the Charger cable representing the external connector.
1172 */
1173static int charger_extcon_init(struct charger_manager *cm,
1174 struct charger_cable *cable)
1175{
1176 int ret = 0;
1177
1178 /*
1179 * Charger manager use Extcon framework to identify
1180 * the charger cable among various external connector
1181 * cable (e.g., TA, USB, MHL, Dock).
1182 */
1183 INIT_WORK(&cable->wq, charger_extcon_work);
1184 cable->nb.notifier_call = charger_extcon_notifier;
1185 ret = extcon_register_interest(&cable->extcon_dev,
1186 cable->extcon_name, cable->name, &cable->nb);
1187 if (ret < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001188 pr_info("Cannot register extcon_dev for %s(cable: %s)\n",
1189 cable->extcon_name, cable->name);
Chanwoo Choibee737b2012-07-12 15:03:25 +09001190 ret = -EINVAL;
1191 }
1192
1193 return ret;
1194}
1195
Chanwoo Choi41468a12012-11-22 16:54:26 +09001196/**
1197 * charger_manager_register_extcon - Register extcon device to recevie state
1198 * of charger cable.
1199 * @cm: the Charger Manager representing the battery.
1200 *
1201 * This function support EXTCON(External Connector) subsystem to detect the
1202 * state of charger cables for enabling or disabling charger(regulator) and
1203 * select the charger cable for charging among a number of external cable
1204 * according to policy of H/W board.
1205 */
1206static int charger_manager_register_extcon(struct charger_manager *cm)
1207{
1208 struct charger_desc *desc = cm->desc;
1209 struct charger_regulator *charger;
1210 int ret = 0;
1211 int i;
1212 int j;
1213
1214 for (i = 0; i < desc->num_charger_regulators; i++) {
1215 charger = &desc->charger_regulators[i];
1216
1217 charger->consumer = regulator_get(cm->dev,
1218 charger->regulator_name);
Jonghwa Lee5a6c2202013-06-25 14:18:14 +09001219 if (IS_ERR(charger->consumer)) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001220 dev_err(cm->dev, "Cannot find charger(%s)\n",
1221 charger->regulator_name);
Jonghwa Lee5a6c2202013-06-25 14:18:14 +09001222 return PTR_ERR(charger->consumer);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001223 }
1224 charger->cm = cm;
1225
1226 for (j = 0; j < charger->num_cables; j++) {
1227 struct charger_cable *cable = &charger->cables[j];
1228
1229 ret = charger_extcon_init(cm, cable);
1230 if (ret < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001231 dev_err(cm->dev, "Cannot initialize charger(%s)\n",
1232 charger->regulator_name);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001233 goto err;
1234 }
1235 cable->charger = charger;
1236 cable->cm = cm;
1237 }
1238 }
1239
1240err:
1241 return ret;
1242}
1243
Chanwoo Choi3950c782012-09-21 18:49:37 +09001244/* help function of sysfs node to control charger(regulator) */
1245static ssize_t charger_name_show(struct device *dev,
1246 struct device_attribute *attr, char *buf)
1247{
1248 struct charger_regulator *charger
1249 = container_of(attr, struct charger_regulator, attr_name);
1250
1251 return sprintf(buf, "%s\n", charger->regulator_name);
1252}
1253
1254static ssize_t charger_state_show(struct device *dev,
1255 struct device_attribute *attr, char *buf)
1256{
1257 struct charger_regulator *charger
1258 = container_of(attr, struct charger_regulator, attr_state);
1259 int state = 0;
1260
1261 if (!charger->externally_control)
1262 state = regulator_is_enabled(charger->consumer);
1263
1264 return sprintf(buf, "%s\n", state ? "enabled" : "disabled");
1265}
1266
1267static ssize_t charger_externally_control_show(struct device *dev,
1268 struct device_attribute *attr, char *buf)
1269{
1270 struct charger_regulator *charger = container_of(attr,
1271 struct charger_regulator, attr_externally_control);
1272
1273 return sprintf(buf, "%d\n", charger->externally_control);
1274}
1275
1276static ssize_t charger_externally_control_store(struct device *dev,
1277 struct device_attribute *attr, const char *buf,
1278 size_t count)
1279{
1280 struct charger_regulator *charger
1281 = container_of(attr, struct charger_regulator,
1282 attr_externally_control);
1283 struct charger_manager *cm = charger->cm;
1284 struct charger_desc *desc = cm->desc;
1285 int i;
1286 int ret;
1287 int externally_control;
1288 int chargers_externally_control = 1;
1289
1290 ret = sscanf(buf, "%d", &externally_control);
1291 if (ret == 0) {
1292 ret = -EINVAL;
1293 return ret;
1294 }
1295
1296 if (!externally_control) {
1297 charger->externally_control = 0;
1298 return count;
1299 }
1300
1301 for (i = 0; i < desc->num_charger_regulators; i++) {
1302 if (&desc->charger_regulators[i] != charger &&
Chanwoo Choi41468a12012-11-22 16:54:26 +09001303 !desc->charger_regulators[i].externally_control) {
Chanwoo Choi3950c782012-09-21 18:49:37 +09001304 /*
1305 * At least, one charger is controlled by
1306 * charger-manager
1307 */
1308 chargers_externally_control = 0;
1309 break;
1310 }
1311 }
1312
1313 if (!chargers_externally_control) {
1314 if (cm->charger_enabled) {
1315 try_charger_enable(charger->cm, false);
1316 charger->externally_control = externally_control;
1317 try_charger_enable(charger->cm, true);
1318 } else {
1319 charger->externally_control = externally_control;
1320 }
1321 } else {
1322 dev_warn(cm->dev,
Joe Perchese5409cb2013-06-06 18:25:12 -07001323 "'%s' regulator should be controlled in charger-manager because charger-manager must need at least one charger for charging\n",
1324 charger->regulator_name);
Chanwoo Choi3950c782012-09-21 18:49:37 +09001325 }
1326
1327 return count;
1328}
1329
Chanwoo Choi41468a12012-11-22 16:54:26 +09001330/**
1331 * charger_manager_register_sysfs - Register sysfs entry for each charger
1332 * @cm: the Charger Manager representing the battery.
1333 *
1334 * This function add sysfs entry for charger(regulator) to control charger from
1335 * user-space. If some development board use one more chargers for charging
1336 * but only need one charger on specific case which is dependent on user
1337 * scenario or hardware restrictions, the user enter 1 or 0(zero) to '/sys/
1338 * class/power_supply/battery/charger.[index]/externally_control'. For example,
1339 * if user enter 1 to 'sys/class/power_supply/battery/charger.[index]/
1340 * externally_control, this charger isn't controlled from charger-manager and
1341 * always stay off state of regulator.
1342 */
1343static int charger_manager_register_sysfs(struct charger_manager *cm)
1344{
1345 struct charger_desc *desc = cm->desc;
1346 struct charger_regulator *charger;
1347 int chargers_externally_control = 1;
1348 char buf[11];
1349 char *str;
1350 int ret = 0;
1351 int i;
1352
1353 /* Create sysfs entry to control charger(regulator) */
1354 for (i = 0; i < desc->num_charger_regulators; i++) {
1355 charger = &desc->charger_regulators[i];
1356
1357 snprintf(buf, 10, "charger.%d", i);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001358 str = devm_kzalloc(cm->dev,
1359 sizeof(char) * (strlen(buf) + 1), GFP_KERNEL);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001360 if (!str) {
Chanwoo Choi41468a12012-11-22 16:54:26 +09001361 ret = -ENOMEM;
1362 goto err;
1363 }
1364 strcpy(str, buf);
1365
1366 charger->attrs[0] = &charger->attr_name.attr;
1367 charger->attrs[1] = &charger->attr_state.attr;
1368 charger->attrs[2] = &charger->attr_externally_control.attr;
1369 charger->attrs[3] = NULL;
1370 charger->attr_g.name = str;
1371 charger->attr_g.attrs = charger->attrs;
1372
1373 sysfs_attr_init(&charger->attr_name.attr);
1374 charger->attr_name.attr.name = "name";
1375 charger->attr_name.attr.mode = 0444;
1376 charger->attr_name.show = charger_name_show;
1377
1378 sysfs_attr_init(&charger->attr_state.attr);
1379 charger->attr_state.attr.name = "state";
1380 charger->attr_state.attr.mode = 0444;
1381 charger->attr_state.show = charger_state_show;
1382
1383 sysfs_attr_init(&charger->attr_externally_control.attr);
1384 charger->attr_externally_control.attr.name
1385 = "externally_control";
1386 charger->attr_externally_control.attr.mode = 0644;
1387 charger->attr_externally_control.show
1388 = charger_externally_control_show;
1389 charger->attr_externally_control.store
1390 = charger_externally_control_store;
1391
1392 if (!desc->charger_regulators[i].externally_control ||
1393 !chargers_externally_control)
1394 chargers_externally_control = 0;
1395
Joe Perchese5409cb2013-06-06 18:25:12 -07001396 dev_info(cm->dev, "'%s' regulator's externally_control is %d\n",
1397 charger->regulator_name, charger->externally_control);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001398
1399 ret = sysfs_create_group(&cm->charger_psy.dev->kobj,
1400 &charger->attr_g);
1401 if (ret < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001402 dev_err(cm->dev, "Cannot create sysfs entry of %s regulator\n",
1403 charger->regulator_name);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001404 ret = -EINVAL;
1405 goto err;
1406 }
1407 }
1408
1409 if (chargers_externally_control) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001410 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 +09001411 ret = -EINVAL;
1412 goto err;
1413 }
1414
1415err:
1416 return ret;
1417}
1418
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +02001419static int cm_init_thermal_data(struct charger_manager *cm,
1420 struct power_supply *fuel_gauge)
Jonghwa Lee5c49a622013-12-18 15:42:34 +09001421{
1422 struct charger_desc *desc = cm->desc;
1423 union power_supply_propval val;
1424 int ret;
1425
1426 /* Verify whether fuel gauge provides battery temperature */
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +02001427 ret = fuel_gauge->get_property(fuel_gauge,
Jonghwa Lee5c49a622013-12-18 15:42:34 +09001428 POWER_SUPPLY_PROP_TEMP, &val);
1429
1430 if (!ret) {
1431 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1432 POWER_SUPPLY_PROP_TEMP;
1433 cm->charger_psy.num_properties++;
1434 cm->desc->measure_battery_temp = true;
1435 }
1436#ifdef CONFIG_THERMAL
Jonghwa Lee5c49a622013-12-18 15:42:34 +09001437 if (ret && desc->thermal_zone) {
1438 cm->tzd_batt =
1439 thermal_zone_get_zone_by_name(desc->thermal_zone);
1440 if (IS_ERR(cm->tzd_batt))
1441 return PTR_ERR(cm->tzd_batt);
1442
1443 /* Use external thermometer */
1444 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1445 POWER_SUPPLY_PROP_TEMP_AMBIENT;
1446 cm->charger_psy.num_properties++;
1447 cm->desc->measure_battery_temp = true;
1448 ret = 0;
1449 }
1450#endif
1451 if (cm->desc->measure_battery_temp) {
1452 /* NOTICE : Default allowable minimum charge temperature is 0 */
1453 if (!desc->temp_max)
1454 desc->temp_max = CM_DEFAULT_CHARGE_TEMP_MAX;
1455 if (!desc->temp_diff)
1456 desc->temp_diff = CM_DEFAULT_RECHARGE_TEMP_DIFF;
1457 }
1458
1459 return ret;
1460}
1461
Jonghwa Lee856ee612013-12-18 15:42:35 +09001462static struct of_device_id charger_manager_match[] = {
1463 {
1464 .compatible = "charger-manager",
1465 },
1466 {},
1467};
1468
Anton Vorontsov434a09f2013-12-23 18:17:05 -08001469static struct charger_desc *of_cm_parse_desc(struct device *dev)
Jonghwa Lee856ee612013-12-18 15:42:35 +09001470{
1471 struct charger_desc *desc;
1472 struct device_node *np = dev->of_node;
1473 u32 poll_mode = CM_POLL_DISABLE;
1474 u32 battery_stat = CM_NO_BATTERY;
1475 int num_chgs = 0;
1476
1477 desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
1478 if (!desc)
1479 return ERR_PTR(-ENOMEM);
1480
1481 of_property_read_string(np, "cm-name", &desc->psy_name);
1482
1483 of_property_read_u32(np, "cm-poll-mode", &poll_mode);
1484 desc->polling_mode = poll_mode;
1485
1486 of_property_read_u32(np, "cm-poll-interval",
1487 &desc->polling_interval_ms);
1488
1489 of_property_read_u32(np, "cm-fullbatt-vchkdrop-ms",
1490 &desc->fullbatt_vchkdrop_ms);
1491 of_property_read_u32(np, "cm-fullbatt-vchkdrop-volt",
1492 &desc->fullbatt_vchkdrop_uV);
1493 of_property_read_u32(np, "cm-fullbatt-voltage", &desc->fullbatt_uV);
1494 of_property_read_u32(np, "cm-fullbatt-soc", &desc->fullbatt_soc);
1495 of_property_read_u32(np, "cm-fullbatt-capacity",
1496 &desc->fullbatt_full_capacity);
1497
1498 of_property_read_u32(np, "cm-battery-stat", &battery_stat);
1499 desc->battery_present = battery_stat;
1500
1501 /* chargers */
1502 of_property_read_u32(np, "cm-num-chargers", &num_chgs);
1503 if (num_chgs) {
1504 /* Allocate empty bin at the tail of array */
1505 desc->psy_charger_stat = devm_kzalloc(dev, sizeof(char *)
1506 * (num_chgs + 1), GFP_KERNEL);
1507 if (desc->psy_charger_stat) {
1508 int i;
1509 for (i = 0; i < num_chgs; i++)
1510 of_property_read_string_index(np, "cm-chargers",
1511 i, &desc->psy_charger_stat[i]);
1512 } else {
1513 return ERR_PTR(-ENOMEM);
1514 }
1515 }
1516
1517 of_property_read_string(np, "cm-fuel-gauge", &desc->psy_fuel_gauge);
1518
1519 of_property_read_string(np, "cm-thermal-zone", &desc->thermal_zone);
1520
1521 of_property_read_u32(np, "cm-battery-cold", &desc->temp_min);
1522 if (of_get_property(np, "cm-battery-cold-in-minus", NULL))
1523 desc->temp_min *= -1;
1524 of_property_read_u32(np, "cm-battery-hot", &desc->temp_max);
1525 of_property_read_u32(np, "cm-battery-temp-diff", &desc->temp_diff);
1526
1527 of_property_read_u32(np, "cm-charging-max",
1528 &desc->charging_max_duration_ms);
1529 of_property_read_u32(np, "cm-discharging-max",
1530 &desc->discharging_max_duration_ms);
1531
1532 /* battery charger regualtors */
1533 desc->num_charger_regulators = of_get_child_count(np);
1534 if (desc->num_charger_regulators) {
1535 struct charger_regulator *chg_regs;
1536 struct device_node *child;
1537
1538 chg_regs = devm_kzalloc(dev, sizeof(*chg_regs)
1539 * desc->num_charger_regulators,
1540 GFP_KERNEL);
1541 if (!chg_regs)
1542 return ERR_PTR(-ENOMEM);
1543
1544 desc->charger_regulators = chg_regs;
1545
1546 for_each_child_of_node(np, child) {
1547 struct charger_cable *cables;
1548 struct device_node *_child;
1549
1550 of_property_read_string(child, "cm-regulator-name",
1551 &chg_regs->regulator_name);
1552
1553 /* charger cables */
1554 chg_regs->num_cables = of_get_child_count(child);
1555 if (chg_regs->num_cables) {
1556 cables = devm_kzalloc(dev, sizeof(*cables)
1557 * chg_regs->num_cables,
1558 GFP_KERNEL);
1559 if (!cables)
1560 return ERR_PTR(-ENOMEM);
1561
1562 chg_regs->cables = cables;
1563
1564 for_each_child_of_node(child, _child) {
1565 of_property_read_string(_child,
1566 "cm-cable-name", &cables->name);
1567 of_property_read_string(_child,
1568 "cm-cable-extcon",
1569 &cables->extcon_name);
1570 of_property_read_u32(_child,
1571 "cm-cable-min",
1572 &cables->min_uA);
1573 of_property_read_u32(_child,
1574 "cm-cable-max",
1575 &cables->max_uA);
1576 cables++;
1577 }
1578 }
1579 chg_regs++;
1580 }
1581 }
1582 return desc;
1583}
1584
1585static inline struct charger_desc *cm_get_drv_data(struct platform_device *pdev)
1586{
1587 if (pdev->dev.of_node)
1588 return of_cm_parse_desc(&pdev->dev);
Jingoo Han86515b72014-08-29 12:45:27 +09001589 return dev_get_platdata(&pdev->dev);
Jonghwa Lee856ee612013-12-18 15:42:35 +09001590}
1591
Jonghwa Leec1155c62014-12-19 17:55:13 +09001592static enum alarmtimer_restart cm_timer_func(struct alarm *alarm, ktime_t now)
1593{
1594 cm_timer_set = false;
1595 return ALARMTIMER_NORESTART;
1596}
1597
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001598static int charger_manager_probe(struct platform_device *pdev)
1599{
Jonghwa Lee856ee612013-12-18 15:42:35 +09001600 struct charger_desc *desc = cm_get_drv_data(pdev);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001601 struct charger_manager *cm;
1602 int ret = 0, i = 0;
Chanwoo Choibee737b2012-07-12 15:03:25 +09001603 int j = 0;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001604 union power_supply_propval val;
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +02001605 struct power_supply *fuel_gauge;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001606
Chanwoo Choic6738d02014-08-26 13:41:38 +09001607 if (IS_ERR(desc)) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001608 dev_err(&pdev->dev, "No platform data (desc) found\n");
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001609 return -ENODEV;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001610 }
1611
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001612 cm = devm_kzalloc(&pdev->dev,
1613 sizeof(struct charger_manager), GFP_KERNEL);
1614 if (!cm)
1615 return -ENOMEM;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001616
1617 /* Basic Values. Unspecified are Null or 0 */
1618 cm->dev = &pdev->dev;
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001619 cm->desc = desc;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001620
Jonghwa Leec1155c62014-12-19 17:55:13 +09001621 /* Initialize alarm timer */
1622 if (alarmtimer_get_rtcdev()) {
1623 cm_timer = devm_kzalloc(cm->dev, sizeof(*cm_timer), GFP_KERNEL);
1624 alarm_init(cm_timer, ALARM_BOOTTIME, cm_timer_func);
1625 }
1626
Chanwoo Choid829dc72012-05-05 06:24:10 -07001627 /*
1628 * The following two do not need to be errors.
1629 * Users may intentionally ignore those two features.
1630 */
1631 if (desc->fullbatt_uV == 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001632 dev_info(&pdev->dev, "Ignoring full-battery voltage threshold as it is not supplied\n");
Chanwoo Choid829dc72012-05-05 06:24:10 -07001633 }
1634 if (!desc->fullbatt_vchkdrop_ms || !desc->fullbatt_vchkdrop_uV) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001635 dev_info(&pdev->dev, "Disabling full-battery voltage drop checking mechanism as it is not supplied\n");
Chanwoo Choid829dc72012-05-05 06:24:10 -07001636 desc->fullbatt_vchkdrop_ms = 0;
1637 desc->fullbatt_vchkdrop_uV = 0;
1638 }
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001639 if (desc->fullbatt_soc == 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001640 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 +09001641 }
1642 if (desc->fullbatt_full_capacity == 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001643 dev_info(&pdev->dev, "Ignoring full-battery full capacity threshold as it is not supplied\n");
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001644 }
Chanwoo Choid829dc72012-05-05 06:24:10 -07001645
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001646 if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001647 dev_err(&pdev->dev, "charger_regulators undefined\n");
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001648 return -EINVAL;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001649 }
1650
1651 if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001652 dev_err(&pdev->dev, "No power supply defined\n");
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001653 return -EINVAL;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001654 }
1655
Krzysztof Kozlowski661a8882014-09-26 13:27:03 +02001656 if (!desc->psy_fuel_gauge) {
1657 dev_err(&pdev->dev, "No fuel gauge power supply defined\n");
1658 return -EINVAL;
1659 }
1660
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001661 /* Counting index only */
1662 while (desc->psy_charger_stat[i])
1663 i++;
1664
Krzysztof Kozlowskicdaf3e12014-10-13 15:34:31 +02001665 /* Check if charger's supplies are present at probe */
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001666 for (i = 0; desc->psy_charger_stat[i]; i++) {
Krzysztof Kozlowskicdaf3e12014-10-13 15:34:31 +02001667 struct power_supply *psy;
1668
1669 psy = power_supply_get_by_name(desc->psy_charger_stat[i]);
1670 if (!psy) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001671 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
1672 desc->psy_charger_stat[i]);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001673 return -ENODEV;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001674 }
1675 }
1676
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +02001677 fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
1678 if (!fuel_gauge) {
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001679 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
Joe Perchese5409cb2013-06-06 18:25:12 -07001680 desc->psy_fuel_gauge);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001681 return -ENODEV;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001682 }
1683
1684 if (desc->polling_interval_ms == 0 ||
1685 msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) {
1686 dev_err(&pdev->dev, "polling_interval_ms is too small\n");
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001687 return -EINVAL;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001688 }
1689
Chanwoo Choi8fcfe082012-09-20 21:20:05 -07001690 if (!desc->charging_max_duration_ms ||
1691 !desc->discharging_max_duration_ms) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001692 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 -07001693 desc->charging_max_duration_ms = 0;
1694 desc->discharging_max_duration_ms = 0;
1695 }
1696
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001697 platform_set_drvdata(pdev, cm);
1698
Axel Linbb2a95c2012-01-12 12:56:35 +08001699 memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default));
1700
Chanwoo Choi41468a12012-11-22 16:54:26 +09001701 if (!desc->psy_name)
Axel Linbb2a95c2012-01-12 12:56:35 +08001702 strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001703 else
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001704 strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001705 cm->charger_psy.name = cm->psy_name_buf;
1706
1707 /* Allocate for psy properties because they may vary */
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001708 cm->charger_psy.properties = devm_kzalloc(&pdev->dev,
1709 sizeof(enum power_supply_property)
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001710 * (ARRAY_SIZE(default_charger_props) +
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001711 NUM_CHARGER_PSY_OPTIONAL), GFP_KERNEL);
1712 if (!cm->charger_psy.properties)
1713 return -ENOMEM;
1714
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001715 memcpy(cm->charger_psy.properties, default_charger_props,
1716 sizeof(enum power_supply_property) *
1717 ARRAY_SIZE(default_charger_props));
1718 cm->charger_psy.num_properties = psy_default.num_properties;
1719
1720 /* Find which optional psy-properties are available */
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +02001721 if (!fuel_gauge->get_property(fuel_gauge,
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001722 POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
1723 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1724 POWER_SUPPLY_PROP_CHARGE_NOW;
1725 cm->charger_psy.num_properties++;
1726 }
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +02001727 if (!fuel_gauge->get_property(fuel_gauge,
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001728 POWER_SUPPLY_PROP_CURRENT_NOW,
1729 &val)) {
1730 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1731 POWER_SUPPLY_PROP_CURRENT_NOW;
1732 cm->charger_psy.num_properties++;
1733 }
Axel Linbb2a95c2012-01-12 12:56:35 +08001734
Krzysztof Kozlowskibdbe8142014-10-13 15:34:30 +02001735 ret = cm_init_thermal_data(cm, fuel_gauge);
Jonghwa Lee5c49a622013-12-18 15:42:34 +09001736 if (ret) {
1737 dev_err(&pdev->dev, "Failed to initialize thermal data\n");
1738 cm->desc->measure_battery_temp = false;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001739 }
1740
Chanwoo Choid829dc72012-05-05 06:24:10 -07001741 INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk);
1742
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001743 ret = power_supply_register(NULL, &cm->charger_psy);
1744 if (ret) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001745 dev_err(&pdev->dev, "Cannot register charger-manager with name \"%s\"\n",
1746 cm->charger_psy.name);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001747 return ret;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001748 }
1749
Chanwoo Choi41468a12012-11-22 16:54:26 +09001750 /* Register extcon device for charger cable */
1751 ret = charger_manager_register_extcon(cm);
1752 if (ret < 0) {
1753 dev_err(&pdev->dev, "Cannot initialize extcon device\n");
1754 goto err_reg_extcon;
Chanwoo Choi3950c782012-09-21 18:49:37 +09001755 }
1756
Chanwoo Choi41468a12012-11-22 16:54:26 +09001757 /* Register sysfs entry for charger(regulator) */
1758 ret = charger_manager_register_sysfs(cm);
1759 if (ret < 0) {
1760 dev_err(&pdev->dev,
1761 "Cannot initialize sysfs entry of regulator\n");
1762 goto err_reg_sysfs;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001763 }
1764
1765 /* Add to the list */
1766 mutex_lock(&cm_list_mtx);
1767 list_add(&cm->entry, &cm_list);
1768 mutex_unlock(&cm_list_mtx);
1769
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001770 /*
1771 * Charger-manager is capable of waking up the systme from sleep
1772 * when event is happend through cm_notify_event()
1773 */
1774 device_init_wakeup(&pdev->dev, true);
1775 device_set_wakeup_capable(&pdev->dev, false);
1776
Chanwoo Choib1022e22014-08-26 13:41:39 +09001777 /*
1778 * Charger-manager have to check the charging state right after
1779 * tialization of charger-manager and then update current charging
1780 * state.
1781 */
1782 cm_monitor();
1783
Chanwoo Choid829dc72012-05-05 06:24:10 -07001784 schedule_work(&setup_polling);
1785
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001786 return 0;
1787
Chanwoo Choi41468a12012-11-22 16:54:26 +09001788err_reg_sysfs:
Chanwoo Choi3950c782012-09-21 18:49:37 +09001789 for (i = 0; i < desc->num_charger_regulators; i++) {
1790 struct charger_regulator *charger;
1791
1792 charger = &desc->charger_regulators[i];
1793 sysfs_remove_group(&cm->charger_psy.dev->kobj,
1794 &charger->attr_g);
Chanwoo Choi3950c782012-09-21 18:49:37 +09001795 }
Chanwoo Choi41468a12012-11-22 16:54:26 +09001796err_reg_extcon:
1797 for (i = 0; i < desc->num_charger_regulators; i++) {
1798 struct charger_regulator *charger;
1799
1800 charger = &desc->charger_regulators[i];
1801 for (j = 0; j < charger->num_cables; j++) {
Chanwoo Choibee737b2012-07-12 15:03:25 +09001802 struct charger_cable *cable = &charger->cables[j];
Jonghwa Lee3cc9d2692013-06-25 14:02:49 +09001803 /* Remove notifier block if only edev exists */
1804 if (cable->extcon_dev.edev)
1805 extcon_unregister_interest(&cable->extcon_dev);
Chanwoo Choibee737b2012-07-12 15:03:25 +09001806 }
Chanwoo Choi41468a12012-11-22 16:54:26 +09001807
Chanwoo Choibee737b2012-07-12 15:03:25 +09001808 regulator_put(desc->charger_regulators[i].consumer);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001809 }
Chanwoo Choibee737b2012-07-12 15:03:25 +09001810
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001811 power_supply_unregister(&cm->charger_psy);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001812
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001813 return ret;
1814}
1815
Bill Pemberton415ec692012-11-19 13:26:07 -05001816static int charger_manager_remove(struct platform_device *pdev)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001817{
1818 struct charger_manager *cm = platform_get_drvdata(pdev);
1819 struct charger_desc *desc = cm->desc;
Chanwoo Choibee737b2012-07-12 15:03:25 +09001820 int i = 0;
1821 int j = 0;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001822
1823 /* Remove from the list */
1824 mutex_lock(&cm_list_mtx);
1825 list_del(&cm->entry);
1826 mutex_unlock(&cm_list_mtx);
1827
Tejun Heo2fbb5202012-12-21 17:56:51 -08001828 cancel_work_sync(&setup_polling);
1829 cancel_delayed_work_sync(&cm_monitor_work);
Chanwoo Choid829dc72012-05-05 06:24:10 -07001830
Chanwoo Choibee737b2012-07-12 15:03:25 +09001831 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1832 struct charger_regulator *charger
1833 = &desc->charger_regulators[i];
1834 for (j = 0 ; j < charger->num_cables ; j++) {
1835 struct charger_cable *cable = &charger->cables[j];
1836 extcon_unregister_interest(&cable->extcon_dev);
1837 }
1838 }
1839
1840 for (i = 0 ; i < desc->num_charger_regulators ; i++)
1841 regulator_put(desc->charger_regulators[i].consumer);
1842
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001843 power_supply_unregister(&cm->charger_psy);
Chanwoo Choid829dc72012-05-05 06:24:10 -07001844
1845 try_charger_enable(cm, false);
1846
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001847 return 0;
1848}
1849
Axel Lin1bbe24d2012-01-11 17:19:45 +08001850static const struct platform_device_id charger_manager_id[] = {
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001851 { "charger-manager", 0 },
1852 { },
1853};
Axel Lin1bbe24d2012-01-11 17:19:45 +08001854MODULE_DEVICE_TABLE(platform, charger_manager_id);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001855
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001856static int cm_suspend_noirq(struct device *dev)
1857{
1858 int ret = 0;
1859
1860 if (device_may_wakeup(dev)) {
1861 device_set_wakeup_capable(dev, false);
1862 ret = -EAGAIN;
1863 }
1864
1865 return ret;
1866}
1867
Jonghwa Leec1155c62014-12-19 17:55:13 +09001868static bool cm_need_to_awake(void)
1869{
1870 struct charger_manager *cm;
1871
1872 if (cm_timer)
1873 return false;
1874
1875 mutex_lock(&cm_list_mtx);
1876 list_for_each_entry(cm, &cm_list, entry) {
1877 if (is_charging(cm)) {
1878 mutex_unlock(&cm_list_mtx);
1879 return true;
1880 }
1881 }
1882 mutex_unlock(&cm_list_mtx);
1883
1884 return false;
1885}
1886
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001887static int cm_suspend_prepare(struct device *dev)
1888{
Axel Linbb2a95c2012-01-12 12:56:35 +08001889 struct charger_manager *cm = dev_get_drvdata(dev);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001890
Jonghwa Leec1155c62014-12-19 17:55:13 +09001891 if (cm_need_to_awake())
1892 return -EBUSY;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001893
Jonghwa Leec1155c62014-12-19 17:55:13 +09001894 if (!cm_suspended)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001895 cm_suspended = true;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001896
Jonghwa Leec1155c62014-12-19 17:55:13 +09001897 cm_timer_set = cm_setup_timer();
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001898
Jonghwa Leec1155c62014-12-19 17:55:13 +09001899 if (cm_timer_set) {
1900 cancel_work_sync(&setup_polling);
1901 cancel_delayed_work_sync(&cm_monitor_work);
1902 cancel_delayed_work(&cm->fullbatt_vchk_work);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001903 }
1904
1905 return 0;
1906}
1907
1908static void cm_suspend_complete(struct device *dev)
1909{
Axel Linbb2a95c2012-01-12 12:56:35 +08001910 struct charger_manager *cm = dev_get_drvdata(dev);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001911
Jonghwa Leec1155c62014-12-19 17:55:13 +09001912 if (cm_suspended)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001913 cm_suspended = false;
Jonghwa Leec1155c62014-12-19 17:55:13 +09001914
1915 if (cm_timer_set) {
1916 ktime_t remain;
1917
1918 alarm_cancel(cm_timer);
1919 cm_timer_set = false;
1920 remain = alarm_expires_remaining(cm_timer);
1921 cm_suspend_duration_ms -= ktime_to_ms(remain);
1922 schedule_work(&setup_polling);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001923 }
1924
Jonghwa Leec1155c62014-12-19 17:55:13 +09001925 _cm_monitor(cm);
1926
Chanwoo Choid829dc72012-05-05 06:24:10 -07001927 /* Re-enqueue delayed work (fullbatt_vchk_work) */
1928 if (cm->fullbatt_vchk_jiffies_at) {
1929 unsigned long delay = 0;
1930 unsigned long now = jiffies + CM_JIFFIES_SMALL;
1931
1932 if (time_after_eq(now, cm->fullbatt_vchk_jiffies_at)) {
1933 delay = (unsigned long)((long)now
1934 - (long)(cm->fullbatt_vchk_jiffies_at));
1935 delay = jiffies_to_msecs(delay);
1936 } else {
1937 delay = 0;
1938 }
1939
1940 /*
Jonghwa Leec1155c62014-12-19 17:55:13 +09001941 * Account for cm_suspend_duration_ms with assuming that
1942 * timer stops in suspend.
Chanwoo Choid829dc72012-05-05 06:24:10 -07001943 */
Jonghwa Leec1155c62014-12-19 17:55:13 +09001944 if (delay > cm_suspend_duration_ms)
1945 delay -= cm_suspend_duration_ms;
1946 else
1947 delay = 0;
Chanwoo Choid829dc72012-05-05 06:24:10 -07001948
1949 queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
1950 msecs_to_jiffies(delay));
1951 }
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001952 device_set_wakeup_capable(cm->dev, false);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001953}
1954
1955static const struct dev_pm_ops charger_manager_pm = {
1956 .prepare = cm_suspend_prepare,
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001957 .suspend_noirq = cm_suspend_noirq,
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001958 .complete = cm_suspend_complete,
1959};
1960
1961static struct platform_driver charger_manager_driver = {
1962 .driver = {
1963 .name = "charger-manager",
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001964 .pm = &charger_manager_pm,
Jonghwa Lee856ee612013-12-18 15:42:35 +09001965 .of_match_table = charger_manager_match,
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001966 },
1967 .probe = charger_manager_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -05001968 .remove = charger_manager_remove,
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001969 .id_table = charger_manager_id,
1970};
1971
1972static int __init charger_manager_init(void)
1973{
Chanwoo Choid829dc72012-05-05 06:24:10 -07001974 cm_wq = create_freezable_workqueue("charger_manager");
1975 INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller);
1976
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001977 return platform_driver_register(&charger_manager_driver);
1978}
1979late_initcall(charger_manager_init);
1980
1981static void __exit charger_manager_cleanup(void)
1982{
Chanwoo Choid829dc72012-05-05 06:24:10 -07001983 destroy_workqueue(cm_wq);
1984 cm_wq = NULL;
1985
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001986 platform_driver_unregister(&charger_manager_driver);
1987}
1988module_exit(charger_manager_cleanup);
1989
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001990/**
1991 * find_power_supply - find the associated power_supply of charger
1992 * @cm: the Charger Manager representing the battery
1993 * @psy: pointer to instance of charger's power_supply
1994 */
1995static bool find_power_supply(struct charger_manager *cm,
1996 struct power_supply *psy)
1997{
1998 int i;
1999 bool found = false;
2000
Krzysztof Kozlowskicdaf3e12014-10-13 15:34:31 +02002001 for (i = 0; cm->desc->psy_charger_stat[i]; i++) {
2002 if (!strcmp(psy->name, cm->desc->psy_charger_stat[i])) {
Chanwoo Choidfeccb12012-05-05 06:26:47 -07002003 found = true;
2004 break;
2005 }
2006 }
2007
2008 return found;
2009}
2010
2011/**
2012 * cm_notify_event - charger driver notify Charger Manager of charger event
2013 * @psy: pointer to instance of charger's power_supply
2014 * @type: type of charger event
2015 * @msg: optional message passed to uevent_notify fuction
2016 */
2017void cm_notify_event(struct power_supply *psy, enum cm_event_types type,
2018 char *msg)
2019{
2020 struct charger_manager *cm;
2021 bool found_power_supply = false;
2022
2023 if (psy == NULL)
2024 return;
2025
2026 mutex_lock(&cm_list_mtx);
2027 list_for_each_entry(cm, &cm_list, entry) {
2028 found_power_supply = find_power_supply(cm, psy);
2029 if (found_power_supply)
2030 break;
2031 }
2032 mutex_unlock(&cm_list_mtx);
2033
2034 if (!found_power_supply)
2035 return;
2036
2037 switch (type) {
2038 case CM_EVENT_BATT_FULL:
2039 fullbatt_handler(cm);
2040 break;
2041 case CM_EVENT_BATT_OUT:
2042 battout_handler(cm);
2043 break;
2044 case CM_EVENT_BATT_IN:
2045 case CM_EVENT_EXT_PWR_IN_OUT ... CM_EVENT_CHG_START_STOP:
2046 misc_event_handler(cm, type);
2047 break;
2048 case CM_EVENT_UNKNOWN:
2049 case CM_EVENT_OTHERS:
2050 uevent_notify(cm, msg ? msg : default_event_names[type]);
2051 break;
2052 default:
Joe Perchese5409cb2013-06-06 18:25:12 -07002053 dev_err(cm->dev, "%s: type not specified\n", __func__);
Chanwoo Choidfeccb12012-05-05 06:26:47 -07002054 break;
2055 }
2056}
2057EXPORT_SYMBOL_GPL(cm_notify_event);
2058
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09002059MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
2060MODULE_DESCRIPTION("Charger Manager");
2061MODULE_LICENSE("GPL");