blob: 6b68d158e3c12dd0e9ceb4ad5dcbbe1a79d817c7 [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 Lee5c49a622013-12-18 15:42:34 +090028#include <linux/thermal.h>
29
30/*
31 * Default termperature threshold for charging.
32 * Every temperature units are in tenth of centigrade.
33 */
34#define CM_DEFAULT_RECHARGE_TEMP_DIFF 50
35#define CM_DEFAULT_CHARGE_TEMP_MAX 500
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090036
Chanwoo Choidfeccb12012-05-05 06:26:47 -070037static const char * const default_event_names[] = {
38 [CM_EVENT_UNKNOWN] = "Unknown",
39 [CM_EVENT_BATT_FULL] = "Battery Full",
40 [CM_EVENT_BATT_IN] = "Battery Inserted",
41 [CM_EVENT_BATT_OUT] = "Battery Pulled Out",
Jonghwa Lee5c49a622013-12-18 15:42:34 +090042 [CM_EVENT_BATT_OVERHEAT] = "Battery Overheat",
43 [CM_EVENT_BATT_COLD] = "Battery Cold",
Chanwoo Choidfeccb12012-05-05 06:26:47 -070044 [CM_EVENT_EXT_PWR_IN_OUT] = "External Power Attach/Detach",
45 [CM_EVENT_CHG_START_STOP] = "Charging Start/Stop",
46 [CM_EVENT_OTHERS] = "Other battery events"
47};
48
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090049/*
50 * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for
51 * delayed works so that we can run delayed works with CM_JIFFIES_SMALL
52 * without any delays.
53 */
54#define CM_JIFFIES_SMALL (2)
55
56/* If y is valid (> 0) and smaller than x, do x = y */
57#define CM_MIN_VALID(x, y) x = (((y > 0) && ((x) > (y))) ? (y) : (x))
58
59/*
60 * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking
61 * rtc alarm. It should be 2 or larger
62 */
63#define CM_RTC_SMALL (2)
64
65#define UEVENT_BUF_SIZE 32
66
67static LIST_HEAD(cm_list);
68static DEFINE_MUTEX(cm_list_mtx);
69
70/* About in-suspend (suspend-again) monitoring */
71static struct rtc_device *rtc_dev;
72/*
73 * Backup RTC alarm
74 * Save the wakeup alarm before entering suspend-to-RAM
75 */
76static struct rtc_wkalrm rtc_wkalarm_save;
77/* Backup RTC alarm time in terms of seconds since 01-01-1970 00:00:00 */
78static unsigned long rtc_wkalarm_save_time;
79static bool cm_suspended;
80static bool cm_rtc_set;
81static unsigned long cm_suspend_duration_ms;
82
Chanwoo Choid829dc72012-05-05 06:24:10 -070083/* About normal (not suspended) monitoring */
84static unsigned long polling_jiffy = ULONG_MAX; /* ULONG_MAX: no polling */
85static unsigned long next_polling; /* Next appointed polling time */
86static struct workqueue_struct *cm_wq; /* init at driver add */
87static struct delayed_work cm_monitor_work; /* init at driver add */
88
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090089/* Global charger-manager description */
90static struct charger_global_desc *g_desc; /* init with setup_charger_manager */
91
92/**
93 * is_batt_present - See if the battery presents in place.
94 * @cm: the Charger Manager representing the battery.
95 */
96static bool is_batt_present(struct charger_manager *cm)
97{
98 union power_supply_propval val;
99 bool present = false;
100 int i, ret;
101
102 switch (cm->desc->battery_present) {
Chanwoo Choid829dc72012-05-05 06:24:10 -0700103 case CM_BATTERY_PRESENT:
104 present = true;
105 break;
106 case CM_NO_BATTERY:
107 break;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900108 case CM_FUEL_GAUGE:
109 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
110 POWER_SUPPLY_PROP_PRESENT, &val);
111 if (ret == 0 && val.intval)
112 present = true;
113 break;
114 case CM_CHARGER_STAT:
115 for (i = 0; cm->charger_stat[i]; i++) {
116 ret = cm->charger_stat[i]->get_property(
117 cm->charger_stat[i],
118 POWER_SUPPLY_PROP_PRESENT, &val);
119 if (ret == 0 && val.intval) {
120 present = true;
121 break;
122 }
123 }
124 break;
125 }
126
127 return present;
128}
129
130/**
131 * is_ext_pwr_online - See if an external power source is attached to charge
132 * @cm: the Charger Manager representing the battery.
133 *
134 * Returns true if at least one of the chargers of the battery has an external
135 * power source attached to charge the battery regardless of whether it is
136 * actually charging or not.
137 */
138static bool is_ext_pwr_online(struct charger_manager *cm)
139{
140 union power_supply_propval val;
141 bool online = false;
142 int i, ret;
143
144 /* If at least one of them has one, it's yes. */
145 for (i = 0; cm->charger_stat[i]; i++) {
146 ret = cm->charger_stat[i]->get_property(
147 cm->charger_stat[i],
148 POWER_SUPPLY_PROP_ONLINE, &val);
149 if (ret == 0 && val.intval) {
150 online = true;
151 break;
152 }
153 }
154
155 return online;
156}
157
158/**
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900159 * get_batt_uV - Get the voltage level of the battery
160 * @cm: the Charger Manager representing the battery.
161 * @uV: the voltage level returned.
162 *
163 * Returns 0 if there is no error.
164 * Returns a negative value on error.
165 */
166static int get_batt_uV(struct charger_manager *cm, int *uV)
167{
168 union power_supply_propval val;
169 int ret;
170
Axel Linbb2a95c2012-01-12 12:56:35 +0800171 if (!cm->fuel_gauge)
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900172 return -ENODEV;
173
Axel Linbb2a95c2012-01-12 12:56:35 +0800174 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
175 POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900176 if (ret)
177 return ret;
178
179 *uV = val.intval;
180 return 0;
181}
182
183/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900184 * is_charging - Returns true if the battery is being charged.
185 * @cm: the Charger Manager representing the battery.
186 */
187static bool is_charging(struct charger_manager *cm)
188{
189 int i, ret;
190 bool charging = false;
191 union power_supply_propval val;
192
193 /* If there is no battery, it cannot be charged */
194 if (!is_batt_present(cm))
195 return false;
196
197 /* If at least one of the charger is charging, return yes */
198 for (i = 0; cm->charger_stat[i]; i++) {
199 /* 1. The charger sholuld not be DISABLED */
200 if (cm->emergency_stop)
201 continue;
202 if (!cm->charger_enabled)
203 continue;
204
205 /* 2. The charger should be online (ext-power) */
206 ret = cm->charger_stat[i]->get_property(
207 cm->charger_stat[i],
208 POWER_SUPPLY_PROP_ONLINE, &val);
209 if (ret) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700210 dev_warn(cm->dev, "Cannot read ONLINE value from %s\n",
211 cm->desc->psy_charger_stat[i]);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900212 continue;
213 }
214 if (val.intval == 0)
215 continue;
216
217 /*
218 * 3. The charger should not be FULL, DISCHARGING,
219 * or NOT_CHARGING.
220 */
221 ret = cm->charger_stat[i]->get_property(
222 cm->charger_stat[i],
223 POWER_SUPPLY_PROP_STATUS, &val);
224 if (ret) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700225 dev_warn(cm->dev, "Cannot read STATUS value from %s\n",
226 cm->desc->psy_charger_stat[i]);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900227 continue;
228 }
229 if (val.intval == POWER_SUPPLY_STATUS_FULL ||
230 val.intval == POWER_SUPPLY_STATUS_DISCHARGING ||
231 val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING)
232 continue;
233
234 /* Then, this is charging. */
235 charging = true;
236 break;
237 }
238
239 return charging;
240}
241
242/**
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900243 * is_full_charged - Returns true if the battery is fully charged.
244 * @cm: the Charger Manager representing the battery.
245 */
246static bool is_full_charged(struct charger_manager *cm)
247{
248 struct charger_desc *desc = cm->desc;
249 union power_supply_propval val;
250 int ret = 0;
251 int uV;
252
253 /* If there is no battery, it cannot be charged */
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900254 if (!is_batt_present(cm))
255 return false;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900256
257 if (cm->fuel_gauge && desc->fullbatt_full_capacity > 0) {
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900258 val.intval = 0;
259
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900260 /* Not full if capacity of fuel gauge isn't full */
261 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
262 POWER_SUPPLY_PROP_CHARGE_FULL, &val);
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900263 if (!ret && val.intval > desc->fullbatt_full_capacity)
264 return true;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900265 }
266
267 /* Full, if it's over the fullbatt voltage */
268 if (desc->fullbatt_uV > 0) {
269 ret = get_batt_uV(cm, &uV);
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900270 if (!ret && uV >= desc->fullbatt_uV)
271 return true;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900272 }
273
274 /* Full, if the capacity is more than fullbatt_soc */
275 if (cm->fuel_gauge && desc->fullbatt_soc > 0) {
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900276 val.intval = 0;
277
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900278 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
279 POWER_SUPPLY_PROP_CAPACITY, &val);
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900280 if (!ret && val.intval >= desc->fullbatt_soc)
281 return true;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900282 }
283
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900284 return false;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900285}
286
287/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900288 * is_polling_required - Return true if need to continue polling for this CM.
289 * @cm: the Charger Manager representing the battery.
290 */
291static bool is_polling_required(struct charger_manager *cm)
292{
293 switch (cm->desc->polling_mode) {
294 case CM_POLL_DISABLE:
295 return false;
296 case CM_POLL_ALWAYS:
297 return true;
298 case CM_POLL_EXTERNAL_POWER_ONLY:
299 return is_ext_pwr_online(cm);
300 case CM_POLL_CHARGING_ONLY:
301 return is_charging(cm);
302 default:
303 dev_warn(cm->dev, "Incorrect polling_mode (%d)\n",
Joe Perchese5409cb2013-06-06 18:25:12 -0700304 cm->desc->polling_mode);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900305 }
306
307 return false;
308}
309
310/**
311 * try_charger_enable - Enable/Disable chargers altogether
312 * @cm: the Charger Manager representing the battery.
313 * @enable: true: enable / false: disable
314 *
315 * Note that Charger Manager keeps the charger enabled regardless whether
316 * the charger is charging or not (because battery is full or no external
317 * power source exists) except when CM needs to disable chargers forcibly
318 * bacause of emergency causes; when the battery is overheated or too cold.
319 */
320static int try_charger_enable(struct charger_manager *cm, bool enable)
321{
322 int err = 0, i;
323 struct charger_desc *desc = cm->desc;
324
325 /* Ignore if it's redundent command */
Axel Linbb2a95c2012-01-12 12:56:35 +0800326 if (enable == cm->charger_enabled)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900327 return 0;
328
329 if (enable) {
330 if (cm->emergency_stop)
331 return -EAGAIN;
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700332
333 /*
334 * Save start time of charging to limit
335 * maximum possible charging time.
336 */
337 cm->charging_start_time = ktime_to_ms(ktime_get());
338 cm->charging_end_time = 0;
339
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900340 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
Chanwoo Choi3950c782012-09-21 18:49:37 +0900341 if (desc->charger_regulators[i].externally_control)
342 continue;
343
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900344 err = regulator_enable(desc->charger_regulators[i].consumer);
345 if (err < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700346 dev_warn(cm->dev, "Cannot enable %s regulator\n",
347 desc->charger_regulators[i].regulator_name);
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900348 }
349 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900350 } else {
351 /*
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700352 * Save end time of charging to maintain fully charged state
353 * of battery after full-batt.
354 */
355 cm->charging_start_time = 0;
356 cm->charging_end_time = ktime_to_ms(ktime_get());
357
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900358 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
Chanwoo Choi3950c782012-09-21 18:49:37 +0900359 if (desc->charger_regulators[i].externally_control)
360 continue;
361
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900362 err = regulator_disable(desc->charger_regulators[i].consumer);
363 if (err < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700364 dev_warn(cm->dev, "Cannot disable %s regulator\n",
365 desc->charger_regulators[i].regulator_name);
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900366 }
367 }
368
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900369 /*
370 * Abnormal battery state - Stop charging forcibly,
371 * even if charger was enabled at the other places
372 */
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900373 for (i = 0; i < desc->num_charger_regulators; i++) {
374 if (regulator_is_enabled(
375 desc->charger_regulators[i].consumer)) {
376 regulator_force_disable(
377 desc->charger_regulators[i].consumer);
Joe Perchese5409cb2013-06-06 18:25:12 -0700378 dev_warn(cm->dev, "Disable regulator(%s) forcibly\n",
379 desc->charger_regulators[i].regulator_name);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900380 }
381 }
382 }
383
384 if (!err)
385 cm->charger_enabled = enable;
386
387 return err;
388}
389
390/**
Chanwoo Choid829dc72012-05-05 06:24:10 -0700391 * try_charger_restart - Restart charging.
392 * @cm: the Charger Manager representing the battery.
393 *
394 * Restart charging by turning off and on the charger.
395 */
396static int try_charger_restart(struct charger_manager *cm)
397{
398 int err;
399
400 if (cm->emergency_stop)
401 return -EAGAIN;
402
403 err = try_charger_enable(cm, false);
404 if (err)
405 return err;
406
407 return try_charger_enable(cm, true);
408}
409
410/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900411 * uevent_notify - Let users know something has changed.
412 * @cm: the Charger Manager representing the battery.
413 * @event: the event string.
414 *
415 * If @event is null, it implies that uevent_notify is called
416 * by resume function. When called in the resume function, cm_suspended
417 * should be already reset to false in order to let uevent_notify
418 * notify the recent event during the suspend to users. While
419 * suspended, uevent_notify does not notify users, but tracks
420 * events so that uevent_notify can notify users later after resumed.
421 */
422static void uevent_notify(struct charger_manager *cm, const char *event)
423{
424 static char env_str[UEVENT_BUF_SIZE + 1] = "";
425 static char env_str_save[UEVENT_BUF_SIZE + 1] = "";
426
427 if (cm_suspended) {
428 /* Nothing in suspended-event buffer */
429 if (env_str_save[0] == 0) {
430 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
431 return; /* status not changed */
432 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
433 return;
434 }
435
436 if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE))
437 return; /* Duplicated. */
Axel Linbb2a95c2012-01-12 12:56:35 +0800438 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900439 return;
440 }
441
442 if (event == NULL) {
443 /* No messages pending */
444 if (!env_str_save[0])
445 return;
446
447 strncpy(env_str, env_str_save, UEVENT_BUF_SIZE);
448 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
449 env_str_save[0] = 0;
450
451 return;
452 }
453
454 /* status not changed */
455 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
456 return;
457
458 /* save the status and notify the update */
459 strncpy(env_str, event, UEVENT_BUF_SIZE);
460 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
461
Joe Perchese5409cb2013-06-06 18:25:12 -0700462 dev_info(cm->dev, "%s\n", event);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900463}
464
465/**
Chanwoo Choid829dc72012-05-05 06:24:10 -0700466 * fullbatt_vchk - Check voltage drop some times after "FULL" event.
467 * @work: the work_struct appointing the function
468 *
469 * If a user has designated "fullbatt_vchkdrop_ms/uV" values with
470 * charger_desc, Charger Manager checks voltage drop after the battery
471 * "FULL" event. It checks whether the voltage has dropped more than
472 * fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms.
473 */
474static void fullbatt_vchk(struct work_struct *work)
475{
476 struct delayed_work *dwork = to_delayed_work(work);
477 struct charger_manager *cm = container_of(dwork,
478 struct charger_manager, fullbatt_vchk_work);
479 struct charger_desc *desc = cm->desc;
480 int batt_uV, err, diff;
481
482 /* remove the appointment for fullbatt_vchk */
483 cm->fullbatt_vchk_jiffies_at = 0;
484
485 if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
486 return;
487
488 err = get_batt_uV(cm, &batt_uV);
489 if (err) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700490 dev_err(cm->dev, "%s: get_batt_uV error(%d)\n", __func__, err);
Chanwoo Choid829dc72012-05-05 06:24:10 -0700491 return;
492 }
493
Chanwoo Choif36b9dd2012-11-22 16:53:51 +0900494 diff = desc->fullbatt_uV - batt_uV;
495 if (diff < 0)
496 return;
Chanwoo Choid829dc72012-05-05 06:24:10 -0700497
Joe Perchese5409cb2013-06-06 18:25:12 -0700498 dev_info(cm->dev, "VBATT dropped %duV after full-batt\n", diff);
Chanwoo Choid829dc72012-05-05 06:24:10 -0700499
500 if (diff > desc->fullbatt_vchkdrop_uV) {
501 try_charger_restart(cm);
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700502 uevent_notify(cm, "Recharging");
Chanwoo Choid829dc72012-05-05 06:24:10 -0700503 }
504}
505
506/**
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700507 * check_charging_duration - Monitor charging/discharging duration
508 * @cm: the Charger Manager representing the battery.
509 *
510 * If whole charging duration exceed 'charging_max_duration_ms',
511 * cm stop charging to prevent overcharge/overheat. If discharging
512 * duration exceed 'discharging _max_duration_ms', charger cable is
513 * attached, after full-batt, cm start charging to maintain fully
514 * charged state for battery.
515 */
516static int check_charging_duration(struct charger_manager *cm)
517{
518 struct charger_desc *desc = cm->desc;
519 u64 curr = ktime_to_ms(ktime_get());
520 u64 duration;
521 int ret = false;
522
523 if (!desc->charging_max_duration_ms &&
524 !desc->discharging_max_duration_ms)
525 return ret;
526
527 if (cm->charger_enabled) {
528 duration = curr - cm->charging_start_time;
529
530 if (duration > desc->charging_max_duration_ms) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700531 dev_info(cm->dev, "Charging duration exceed %lldms\n",
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700532 desc->charging_max_duration_ms);
533 uevent_notify(cm, "Discharging");
534 try_charger_enable(cm, false);
535 ret = true;
536 }
537 } else if (is_ext_pwr_online(cm) && !cm->charger_enabled) {
538 duration = curr - cm->charging_end_time;
539
540 if (duration > desc->charging_max_duration_ms &&
541 is_ext_pwr_online(cm)) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700542 dev_info(cm->dev, "Discharging duration exceed %lldms\n",
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700543 desc->discharging_max_duration_ms);
Joe Perchese5409cb2013-06-06 18:25:12 -0700544 uevent_notify(cm, "Recharging");
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700545 try_charger_enable(cm, true);
546 ret = true;
547 }
548 }
549
550 return ret;
551}
552
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900553static int cm_get_battery_temperature(struct charger_manager *cm,
554 int *temp)
555{
556 int ret;
557
558 if (!cm->desc->measure_battery_temp)
559 return -ENODEV;
560
561#ifdef CONFIG_THERMAL
562 ret = thermal_zone_get_temp(cm->tzd_batt, (unsigned long *)temp);
563 if (!ret)
564 /* Calibrate temperature unit */
565 *temp /= 100;
566#else
567 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
568 POWER_SUPPLY_PROP_TEMP,
569 (union power_supply_propval *)temp);
570#endif
571 return ret;
572}
573
574static int cm_check_thermal_status(struct charger_manager *cm)
575{
576 struct charger_desc *desc = cm->desc;
577 int temp, upper_limit, lower_limit;
578 int ret = 0;
579
580 ret = cm_get_battery_temperature(cm, &temp);
581 if (ret) {
582 /* FIXME:
583 * No information of battery temperature might
584 * occur hazadous result. We have to handle it
585 * depending on battery type.
586 */
587 dev_err(cm->dev, "Failed to get battery temperature\n");
588 return 0;
589 }
590
591 upper_limit = desc->temp_max;
592 lower_limit = desc->temp_min;
593
594 if (cm->emergency_stop) {
595 upper_limit -= desc->temp_diff;
596 lower_limit += desc->temp_diff;
597 }
598
599 if (temp > upper_limit)
600 ret = CM_EVENT_BATT_OVERHEAT;
601 else if (temp < lower_limit)
602 ret = CM_EVENT_BATT_COLD;
603
604 return ret;
605}
606
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700607/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900608 * _cm_monitor - Monitor the temperature and return true for exceptions.
609 * @cm: the Charger Manager representing the battery.
610 *
611 * Returns true if there is an event to notify for the battery.
612 * (True if the status of "emergency_stop" changes)
613 */
614static bool _cm_monitor(struct charger_manager *cm)
615{
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900616 int temp_alrt;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900617
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900618 temp_alrt = cm_check_thermal_status(cm);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900619
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900620 /* It has been stopped already */
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900621 if (temp_alrt && cm->emergency_stop)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900622 return false;
623
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900624 /*
625 * Check temperature whether overheat or cold.
626 * If temperature is out of range normal state, stop charging.
627 */
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900628 if (temp_alrt) {
629 cm->emergency_stop = temp_alrt;
630 if (!try_charger_enable(cm, false))
631 uevent_notify(cm, default_event_names[temp_alrt]);
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900632
633 /*
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700634 * Check whole charging duration and discharing duration
635 * after full-batt.
636 */
637 } else if (!cm->emergency_stop && check_charging_duration(cm)) {
638 dev_dbg(cm->dev,
Joe Perchese5409cb2013-06-06 18:25:12 -0700639 "Charging/Discharging duration is out of range\n");
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700640 /*
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900641 * Check dropped voltage of battery. If battery voltage is more
642 * dropped than fullbatt_vchkdrop_uV after fully charged state,
643 * charger-manager have to recharge battery.
644 */
645 } else if (!cm->emergency_stop && is_ext_pwr_online(cm) &&
646 !cm->charger_enabled) {
647 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
648
649 /*
650 * Check whether fully charged state to protect overcharge
651 * if charger-manager is charging for battery.
652 */
653 } else if (!cm->emergency_stop && is_full_charged(cm) &&
654 cm->charger_enabled) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700655 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged\n");
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900656 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
657
658 try_charger_enable(cm, false);
659
660 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900661 } else {
662 cm->emergency_stop = 0;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900663 if (is_ext_pwr_online(cm)) {
664 if (!try_charger_enable(cm, true))
665 uevent_notify(cm, "CHARGING");
666 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900667 }
668
669 return true;
670}
671
672/**
673 * cm_monitor - Monitor every battery.
674 *
675 * Returns true if there is an event to notify from any of the batteries.
676 * (True if the status of "emergency_stop" changes)
677 */
678static bool cm_monitor(void)
679{
680 bool stop = false;
681 struct charger_manager *cm;
682
683 mutex_lock(&cm_list_mtx);
684
Axel Linbb2a95c2012-01-12 12:56:35 +0800685 list_for_each_entry(cm, &cm_list, entry) {
686 if (_cm_monitor(cm))
687 stop = true;
688 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900689
690 mutex_unlock(&cm_list_mtx);
691
692 return stop;
693}
694
Chanwoo Choid829dc72012-05-05 06:24:10 -0700695/**
696 * _setup_polling - Setup the next instance of polling.
697 * @work: work_struct of the function _setup_polling.
698 */
699static void _setup_polling(struct work_struct *work)
700{
701 unsigned long min = ULONG_MAX;
702 struct charger_manager *cm;
703 bool keep_polling = false;
704 unsigned long _next_polling;
705
706 mutex_lock(&cm_list_mtx);
707
708 list_for_each_entry(cm, &cm_list, entry) {
709 if (is_polling_required(cm) && cm->desc->polling_interval_ms) {
710 keep_polling = true;
711
712 if (min > cm->desc->polling_interval_ms)
713 min = cm->desc->polling_interval_ms;
714 }
715 }
716
717 polling_jiffy = msecs_to_jiffies(min);
718 if (polling_jiffy <= CM_JIFFIES_SMALL)
719 polling_jiffy = CM_JIFFIES_SMALL + 1;
720
721 if (!keep_polling)
722 polling_jiffy = ULONG_MAX;
723 if (polling_jiffy == ULONG_MAX)
724 goto out;
725
726 WARN(cm_wq == NULL, "charger-manager: workqueue not initialized"
727 ". try it later. %s\n", __func__);
728
Tejun Heo2fbb5202012-12-21 17:56:51 -0800729 /*
730 * Use mod_delayed_work() iff the next polling interval should
731 * occur before the currently scheduled one. If @cm_monitor_work
732 * isn't active, the end result is the same, so no need to worry
733 * about stale @next_polling.
734 */
Chanwoo Choid829dc72012-05-05 06:24:10 -0700735 _next_polling = jiffies + polling_jiffy;
736
Tejun Heo2fbb5202012-12-21 17:56:51 -0800737 if (time_before(_next_polling, next_polling)) {
Tejun Heo41f63c52012-08-03 10:30:47 -0700738 mod_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy);
Tejun Heo2fbb5202012-12-21 17:56:51 -0800739 next_polling = _next_polling;
740 } else {
741 if (queue_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy))
742 next_polling = _next_polling;
Chanwoo Choid829dc72012-05-05 06:24:10 -0700743 }
Chanwoo Choid829dc72012-05-05 06:24:10 -0700744out:
745 mutex_unlock(&cm_list_mtx);
746}
747static DECLARE_WORK(setup_polling, _setup_polling);
748
749/**
750 * cm_monitor_poller - The Monitor / Poller.
751 * @work: work_struct of the function cm_monitor_poller
752 *
753 * During non-suspended state, cm_monitor_poller is used to poll and monitor
754 * the batteries.
755 */
756static void cm_monitor_poller(struct work_struct *work)
757{
758 cm_monitor();
759 schedule_work(&setup_polling);
760}
761
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700762/**
763 * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL
764 * @cm: the Charger Manager representing the battery.
765 */
766static void fullbatt_handler(struct charger_manager *cm)
767{
768 struct charger_desc *desc = cm->desc;
769
770 if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
771 goto out;
772
773 if (cm_suspended)
774 device_set_wakeup_capable(cm->dev, true);
775
Tejun Heo41f63c52012-08-03 10:30:47 -0700776 mod_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
777 msecs_to_jiffies(desc->fullbatt_vchkdrop_ms));
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700778 cm->fullbatt_vchk_jiffies_at = jiffies + msecs_to_jiffies(
779 desc->fullbatt_vchkdrop_ms);
780
781 if (cm->fullbatt_vchk_jiffies_at == 0)
782 cm->fullbatt_vchk_jiffies_at = 1;
783
784out:
Joe Perchese5409cb2013-06-06 18:25:12 -0700785 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged\n");
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700786 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
787}
788
789/**
790 * battout_handler - Event handler for CM_EVENT_BATT_OUT
791 * @cm: the Charger Manager representing the battery.
792 */
793static void battout_handler(struct charger_manager *cm)
794{
795 if (cm_suspended)
796 device_set_wakeup_capable(cm->dev, true);
797
798 if (!is_batt_present(cm)) {
799 dev_emerg(cm->dev, "Battery Pulled Out!\n");
800 uevent_notify(cm, default_event_names[CM_EVENT_BATT_OUT]);
801 } else {
802 uevent_notify(cm, "Battery Reinserted?");
803 }
804}
805
806/**
807 * misc_event_handler - Handler for other evnets
808 * @cm: the Charger Manager representing the battery.
809 * @type: the Charger Manager representing the battery.
810 */
811static void misc_event_handler(struct charger_manager *cm,
812 enum cm_event_types type)
813{
814 if (cm_suspended)
815 device_set_wakeup_capable(cm->dev, true);
816
Tejun Heo2fbb5202012-12-21 17:56:51 -0800817 if (is_polling_required(cm) && cm->desc->polling_interval_ms)
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700818 schedule_work(&setup_polling);
819 uevent_notify(cm, default_event_names[type]);
820}
821
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900822static int charger_get_property(struct power_supply *psy,
823 enum power_supply_property psp,
824 union power_supply_propval *val)
825{
826 struct charger_manager *cm = container_of(psy,
827 struct charger_manager, charger_psy);
828 struct charger_desc *desc = cm->desc;
Anton Vorontsovdf58c042012-03-15 21:01:28 +0400829 int ret = 0;
830 int uV;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900831
832 switch (psp) {
833 case POWER_SUPPLY_PROP_STATUS:
834 if (is_charging(cm))
835 val->intval = POWER_SUPPLY_STATUS_CHARGING;
836 else if (is_ext_pwr_online(cm))
837 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
838 else
839 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
840 break;
841 case POWER_SUPPLY_PROP_HEALTH:
842 if (cm->emergency_stop > 0)
843 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
844 else if (cm->emergency_stop < 0)
845 val->intval = POWER_SUPPLY_HEALTH_COLD;
846 else
847 val->intval = POWER_SUPPLY_HEALTH_GOOD;
848 break;
849 case POWER_SUPPLY_PROP_PRESENT:
850 if (is_batt_present(cm))
851 val->intval = 1;
852 else
853 val->intval = 0;
854 break;
855 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Anton Vorontsovdf58c042012-03-15 21:01:28 +0400856 ret = get_batt_uV(cm, &val->intval);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900857 break;
858 case POWER_SUPPLY_PROP_CURRENT_NOW:
859 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
860 POWER_SUPPLY_PROP_CURRENT_NOW, val);
861 break;
862 case POWER_SUPPLY_PROP_TEMP:
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900863 case POWER_SUPPLY_PROP_TEMP_AMBIENT:
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900864 return cm_get_battery_temperature(cm, &val->intval);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900865 case POWER_SUPPLY_PROP_CAPACITY:
866 if (!cm->fuel_gauge) {
867 ret = -ENODEV;
868 break;
869 }
870
871 if (!is_batt_present(cm)) {
872 /* There is no battery. Assume 100% */
873 val->intval = 100;
874 break;
875 }
876
877 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
878 POWER_SUPPLY_PROP_CAPACITY, val);
879 if (ret)
880 break;
881
882 if (val->intval > 100) {
883 val->intval = 100;
884 break;
885 }
886 if (val->intval < 0)
887 val->intval = 0;
888
889 /* Do not adjust SOC when charging: voltage is overrated */
890 if (is_charging(cm))
891 break;
892
893 /*
894 * If the capacity value is inconsistent, calibrate it base on
895 * the battery voltage values and the thresholds given as desc
896 */
897 ret = get_batt_uV(cm, &uV);
898 if (ret) {
899 /* Voltage information not available. No calibration */
900 ret = 0;
901 break;
902 }
903
904 if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
905 !is_charging(cm)) {
906 val->intval = 100;
907 break;
908 }
909
910 break;
911 case POWER_SUPPLY_PROP_ONLINE:
912 if (is_ext_pwr_online(cm))
913 val->intval = 1;
914 else
915 val->intval = 0;
916 break;
917 case POWER_SUPPLY_PROP_CHARGE_FULL:
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900918 if (is_full_charged(cm))
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900919 val->intval = 1;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900920 else
921 val->intval = 0;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900922 ret = 0;
923 break;
924 case POWER_SUPPLY_PROP_CHARGE_NOW:
925 if (is_charging(cm)) {
926 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
927 POWER_SUPPLY_PROP_CHARGE_NOW,
928 val);
929 if (ret) {
930 val->intval = 1;
931 ret = 0;
932 } else {
933 /* If CHARGE_NOW is supplied, use it */
934 val->intval = (val->intval > 0) ?
935 val->intval : 1;
936 }
937 } else {
938 val->intval = 0;
939 }
940 break;
941 default:
942 return -EINVAL;
943 }
944 return ret;
945}
946
947#define NUM_CHARGER_PSY_OPTIONAL (4)
948static enum power_supply_property default_charger_props[] = {
949 /* Guaranteed to provide */
950 POWER_SUPPLY_PROP_STATUS,
951 POWER_SUPPLY_PROP_HEALTH,
952 POWER_SUPPLY_PROP_PRESENT,
953 POWER_SUPPLY_PROP_VOLTAGE_NOW,
954 POWER_SUPPLY_PROP_CAPACITY,
955 POWER_SUPPLY_PROP_ONLINE,
956 POWER_SUPPLY_PROP_CHARGE_FULL,
957 /*
958 * Optional properties are:
959 * POWER_SUPPLY_PROP_CHARGE_NOW,
960 * POWER_SUPPLY_PROP_CURRENT_NOW,
961 * POWER_SUPPLY_PROP_TEMP, and
962 * POWER_SUPPLY_PROP_TEMP_AMBIENT,
963 */
964};
965
966static struct power_supply psy_default = {
967 .name = "battery",
968 .type = POWER_SUPPLY_TYPE_BATTERY,
969 .properties = default_charger_props,
970 .num_properties = ARRAY_SIZE(default_charger_props),
971 .get_property = charger_get_property,
972};
973
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900974/**
975 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
976 * for suspend_again.
977 *
978 * Returns true if the alarm is set for Charger Manager to use.
979 * Returns false if
980 * cm_setup_timer fails to set an alarm,
981 * cm_setup_timer does not need to set an alarm for Charger Manager,
982 * or an alarm previously configured is to be used.
983 */
984static bool cm_setup_timer(void)
985{
986 struct charger_manager *cm;
987 unsigned int wakeup_ms = UINT_MAX;
988 bool ret = false;
989
990 mutex_lock(&cm_list_mtx);
991
992 list_for_each_entry(cm, &cm_list, entry) {
Chanwoo Choid829dc72012-05-05 06:24:10 -0700993 unsigned int fbchk_ms = 0;
994
995 /* fullbatt_vchk is required. setup timer for that */
996 if (cm->fullbatt_vchk_jiffies_at) {
997 fbchk_ms = jiffies_to_msecs(cm->fullbatt_vchk_jiffies_at
998 - jiffies);
999 if (time_is_before_eq_jiffies(
1000 cm->fullbatt_vchk_jiffies_at) ||
1001 msecs_to_jiffies(fbchk_ms) < CM_JIFFIES_SMALL) {
1002 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
1003 fbchk_ms = 0;
1004 }
1005 }
1006 CM_MIN_VALID(wakeup_ms, fbchk_ms);
1007
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001008 /* Skip if polling is not required for this CM */
1009 if (!is_polling_required(cm) && !cm->emergency_stop)
1010 continue;
1011 if (cm->desc->polling_interval_ms == 0)
1012 continue;
1013 CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
1014 }
1015
1016 mutex_unlock(&cm_list_mtx);
1017
1018 if (wakeup_ms < UINT_MAX && wakeup_ms > 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001019 pr_info("Charger Manager wakeup timer: %u ms\n", wakeup_ms);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001020 if (rtc_dev) {
1021 struct rtc_wkalrm tmp;
1022 unsigned long time, now;
1023 unsigned long add = DIV_ROUND_UP(wakeup_ms, 1000);
1024
1025 /*
1026 * Set alarm with the polling interval (wakeup_ms)
1027 * except when rtc_wkalarm_save comes first.
1028 * However, the alarm time should be NOW +
1029 * CM_RTC_SMALL or later.
1030 */
1031 tmp.enabled = 1;
1032 rtc_read_time(rtc_dev, &tmp.time);
1033 rtc_tm_to_time(&tmp.time, &now);
1034 if (add < CM_RTC_SMALL)
1035 add = CM_RTC_SMALL;
1036 time = now + add;
1037
1038 ret = true;
1039
1040 if (rtc_wkalarm_save.enabled &&
1041 rtc_wkalarm_save_time &&
1042 rtc_wkalarm_save_time < time) {
1043 if (rtc_wkalarm_save_time < now + CM_RTC_SMALL)
1044 time = now + CM_RTC_SMALL;
1045 else
1046 time = rtc_wkalarm_save_time;
1047
1048 /* The timer is not appointed by CM */
1049 ret = false;
1050 }
1051
Joe Perchese5409cb2013-06-06 18:25:12 -07001052 pr_info("Waking up after %lu secs\n", time - now);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001053
1054 rtc_time_to_tm(time, &tmp.time);
1055 rtc_set_alarm(rtc_dev, &tmp);
1056 cm_suspend_duration_ms += wakeup_ms;
1057 return ret;
1058 }
1059 }
1060
1061 if (rtc_dev)
1062 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1063 return false;
1064}
1065
Chanwoo Choid829dc72012-05-05 06:24:10 -07001066static void _cm_fbchk_in_suspend(struct charger_manager *cm)
1067{
1068 unsigned long jiffy_now = jiffies;
1069
1070 if (!cm->fullbatt_vchk_jiffies_at)
1071 return;
1072
1073 if (g_desc && g_desc->assume_timer_stops_in_suspend)
1074 jiffy_now += msecs_to_jiffies(cm_suspend_duration_ms);
1075
1076 /* Execute now if it's going to be executed not too long after */
1077 jiffy_now += CM_JIFFIES_SMALL;
1078
1079 if (time_after_eq(jiffy_now, cm->fullbatt_vchk_jiffies_at))
1080 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
1081}
1082
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001083/**
1084 * cm_suspend_again - Determine whether suspend again or not
1085 *
1086 * Returns true if the system should be suspended again
1087 * Returns false if the system should be woken up
1088 */
1089bool cm_suspend_again(void)
1090{
1091 struct charger_manager *cm;
1092 bool ret = false;
1093
1094 if (!g_desc || !g_desc->rtc_only_wakeup || !g_desc->rtc_only_wakeup() ||
1095 !cm_rtc_set)
1096 return false;
1097
1098 if (cm_monitor())
1099 goto out;
1100
1101 ret = true;
1102 mutex_lock(&cm_list_mtx);
1103 list_for_each_entry(cm, &cm_list, entry) {
Chanwoo Choid829dc72012-05-05 06:24:10 -07001104 _cm_fbchk_in_suspend(cm);
1105
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001106 if (cm->status_save_ext_pwr_inserted != is_ext_pwr_online(cm) ||
Axel Linbb2a95c2012-01-12 12:56:35 +08001107 cm->status_save_batt != is_batt_present(cm)) {
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001108 ret = false;
Axel Linbb2a95c2012-01-12 12:56:35 +08001109 break;
1110 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001111 }
1112 mutex_unlock(&cm_list_mtx);
1113
1114 cm_rtc_set = cm_setup_timer();
1115out:
1116 /* It's about the time when the non-CM appointed timer goes off */
1117 if (rtc_wkalarm_save.enabled) {
1118 unsigned long now;
1119 struct rtc_time tmp;
1120
1121 rtc_read_time(rtc_dev, &tmp);
1122 rtc_tm_to_time(&tmp, &now);
1123
1124 if (rtc_wkalarm_save_time &&
1125 now + CM_RTC_SMALL >= rtc_wkalarm_save_time)
1126 return false;
1127 }
1128 return ret;
1129}
1130EXPORT_SYMBOL_GPL(cm_suspend_again);
1131
1132/**
1133 * setup_charger_manager - initialize charger_global_desc data
1134 * @gd: pointer to instance of charger_global_desc
1135 */
1136int setup_charger_manager(struct charger_global_desc *gd)
1137{
1138 if (!gd)
1139 return -EINVAL;
1140
1141 if (rtc_dev)
1142 rtc_class_close(rtc_dev);
1143 rtc_dev = NULL;
1144 g_desc = NULL;
1145
1146 if (!gd->rtc_only_wakeup) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001147 pr_err("The callback rtc_only_wakeup is not given\n");
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001148 return -EINVAL;
1149 }
1150
1151 if (gd->rtc_name) {
1152 rtc_dev = rtc_class_open(gd->rtc_name);
1153 if (IS_ERR_OR_NULL(rtc_dev)) {
1154 rtc_dev = NULL;
1155 /* Retry at probe. RTC may be not registered yet */
1156 }
1157 } else {
Joe Perchese5409cb2013-06-06 18:25:12 -07001158 pr_warn("No wakeup timer is given for charger manager. "
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001159 "In-suspend monitoring won't work.\n");
1160 }
1161
1162 g_desc = gd;
1163 return 0;
1164}
1165EXPORT_SYMBOL_GPL(setup_charger_manager);
1166
Chanwoo Choibee737b2012-07-12 15:03:25 +09001167/**
1168 * charger_extcon_work - enable/diable charger according to the state
1169 * of charger cable
1170 *
1171 * @work: work_struct of the function charger_extcon_work.
1172 */
1173static void charger_extcon_work(struct work_struct *work)
1174{
1175 struct charger_cable *cable =
1176 container_of(work, struct charger_cable, wq);
Chanwoo Choi45cd4fb2012-07-12 15:03:29 +09001177 int ret;
1178
1179 if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) {
1180 ret = regulator_set_current_limit(cable->charger->consumer,
1181 cable->min_uA, cable->max_uA);
1182 if (ret < 0) {
1183 pr_err("Cannot set current limit of %s (%s)\n",
Joe Perchese5409cb2013-06-06 18:25:12 -07001184 cable->charger->regulator_name, cable->name);
Chanwoo Choi45cd4fb2012-07-12 15:03:29 +09001185 return;
1186 }
1187
1188 pr_info("Set current limit of %s : %duA ~ %duA\n",
Joe Perchese5409cb2013-06-06 18:25:12 -07001189 cable->charger->regulator_name,
1190 cable->min_uA, cable->max_uA);
Chanwoo Choi45cd4fb2012-07-12 15:03:29 +09001191 }
Chanwoo Choibee737b2012-07-12 15:03:25 +09001192
1193 try_charger_enable(cable->cm, cable->attached);
1194}
1195
1196/**
1197 * charger_extcon_notifier - receive the state of charger cable
1198 * when registered cable is attached or detached.
1199 *
1200 * @self: the notifier block of the charger_extcon_notifier.
1201 * @event: the cable state.
1202 * @ptr: the data pointer of notifier block.
1203 */
1204static int charger_extcon_notifier(struct notifier_block *self,
1205 unsigned long event, void *ptr)
1206{
1207 struct charger_cable *cable =
1208 container_of(self, struct charger_cable, nb);
1209
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001210 /*
1211 * The newly state of charger cable.
1212 * If cable is attached, cable->attached is true.
1213 */
Chanwoo Choibee737b2012-07-12 15:03:25 +09001214 cable->attached = event;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001215
1216 /*
1217 * Setup monitoring to check battery state
1218 * when charger cable is attached.
1219 */
1220 if (cable->attached && is_polling_required(cable->cm)) {
Tejun Heo2fbb5202012-12-21 17:56:51 -08001221 cancel_work_sync(&setup_polling);
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001222 schedule_work(&setup_polling);
1223 }
1224
1225 /*
1226 * Setup work for controlling charger(regulator)
1227 * according to charger cable.
1228 */
Chanwoo Choibee737b2012-07-12 15:03:25 +09001229 schedule_work(&cable->wq);
1230
1231 return NOTIFY_DONE;
1232}
1233
1234/**
1235 * charger_extcon_init - register external connector to use it
1236 * as the charger cable
1237 *
1238 * @cm: the Charger Manager representing the battery.
1239 * @cable: the Charger cable representing the external connector.
1240 */
1241static int charger_extcon_init(struct charger_manager *cm,
1242 struct charger_cable *cable)
1243{
1244 int ret = 0;
1245
1246 /*
1247 * Charger manager use Extcon framework to identify
1248 * the charger cable among various external connector
1249 * cable (e.g., TA, USB, MHL, Dock).
1250 */
1251 INIT_WORK(&cable->wq, charger_extcon_work);
1252 cable->nb.notifier_call = charger_extcon_notifier;
1253 ret = extcon_register_interest(&cable->extcon_dev,
1254 cable->extcon_name, cable->name, &cable->nb);
1255 if (ret < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001256 pr_info("Cannot register extcon_dev for %s(cable: %s)\n",
1257 cable->extcon_name, cable->name);
Chanwoo Choibee737b2012-07-12 15:03:25 +09001258 ret = -EINVAL;
1259 }
1260
1261 return ret;
1262}
1263
Chanwoo Choi41468a12012-11-22 16:54:26 +09001264/**
1265 * charger_manager_register_extcon - Register extcon device to recevie state
1266 * of charger cable.
1267 * @cm: the Charger Manager representing the battery.
1268 *
1269 * This function support EXTCON(External Connector) subsystem to detect the
1270 * state of charger cables for enabling or disabling charger(regulator) and
1271 * select the charger cable for charging among a number of external cable
1272 * according to policy of H/W board.
1273 */
1274static int charger_manager_register_extcon(struct charger_manager *cm)
1275{
1276 struct charger_desc *desc = cm->desc;
1277 struct charger_regulator *charger;
1278 int ret = 0;
1279 int i;
1280 int j;
1281
1282 for (i = 0; i < desc->num_charger_regulators; i++) {
1283 charger = &desc->charger_regulators[i];
1284
1285 charger->consumer = regulator_get(cm->dev,
1286 charger->regulator_name);
Jonghwa Lee5a6c2202013-06-25 14:18:14 +09001287 if (IS_ERR(charger->consumer)) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001288 dev_err(cm->dev, "Cannot find charger(%s)\n",
1289 charger->regulator_name);
Jonghwa Lee5a6c2202013-06-25 14:18:14 +09001290 return PTR_ERR(charger->consumer);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001291 }
1292 charger->cm = cm;
1293
1294 for (j = 0; j < charger->num_cables; j++) {
1295 struct charger_cable *cable = &charger->cables[j];
1296
1297 ret = charger_extcon_init(cm, cable);
1298 if (ret < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001299 dev_err(cm->dev, "Cannot initialize charger(%s)\n",
1300 charger->regulator_name);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001301 goto err;
1302 }
1303 cable->charger = charger;
1304 cable->cm = cm;
1305 }
1306 }
1307
1308err:
1309 return ret;
1310}
1311
Chanwoo Choi3950c782012-09-21 18:49:37 +09001312/* help function of sysfs node to control charger(regulator) */
1313static ssize_t charger_name_show(struct device *dev,
1314 struct device_attribute *attr, char *buf)
1315{
1316 struct charger_regulator *charger
1317 = container_of(attr, struct charger_regulator, attr_name);
1318
1319 return sprintf(buf, "%s\n", charger->regulator_name);
1320}
1321
1322static ssize_t charger_state_show(struct device *dev,
1323 struct device_attribute *attr, char *buf)
1324{
1325 struct charger_regulator *charger
1326 = container_of(attr, struct charger_regulator, attr_state);
1327 int state = 0;
1328
1329 if (!charger->externally_control)
1330 state = regulator_is_enabled(charger->consumer);
1331
1332 return sprintf(buf, "%s\n", state ? "enabled" : "disabled");
1333}
1334
1335static ssize_t charger_externally_control_show(struct device *dev,
1336 struct device_attribute *attr, char *buf)
1337{
1338 struct charger_regulator *charger = container_of(attr,
1339 struct charger_regulator, attr_externally_control);
1340
1341 return sprintf(buf, "%d\n", charger->externally_control);
1342}
1343
1344static ssize_t charger_externally_control_store(struct device *dev,
1345 struct device_attribute *attr, const char *buf,
1346 size_t count)
1347{
1348 struct charger_regulator *charger
1349 = container_of(attr, struct charger_regulator,
1350 attr_externally_control);
1351 struct charger_manager *cm = charger->cm;
1352 struct charger_desc *desc = cm->desc;
1353 int i;
1354 int ret;
1355 int externally_control;
1356 int chargers_externally_control = 1;
1357
1358 ret = sscanf(buf, "%d", &externally_control);
1359 if (ret == 0) {
1360 ret = -EINVAL;
1361 return ret;
1362 }
1363
1364 if (!externally_control) {
1365 charger->externally_control = 0;
1366 return count;
1367 }
1368
1369 for (i = 0; i < desc->num_charger_regulators; i++) {
1370 if (&desc->charger_regulators[i] != charger &&
Chanwoo Choi41468a12012-11-22 16:54:26 +09001371 !desc->charger_regulators[i].externally_control) {
Chanwoo Choi3950c782012-09-21 18:49:37 +09001372 /*
1373 * At least, one charger is controlled by
1374 * charger-manager
1375 */
1376 chargers_externally_control = 0;
1377 break;
1378 }
1379 }
1380
1381 if (!chargers_externally_control) {
1382 if (cm->charger_enabled) {
1383 try_charger_enable(charger->cm, false);
1384 charger->externally_control = externally_control;
1385 try_charger_enable(charger->cm, true);
1386 } else {
1387 charger->externally_control = externally_control;
1388 }
1389 } else {
1390 dev_warn(cm->dev,
Joe Perchese5409cb2013-06-06 18:25:12 -07001391 "'%s' regulator should be controlled in charger-manager because charger-manager must need at least one charger for charging\n",
1392 charger->regulator_name);
Chanwoo Choi3950c782012-09-21 18:49:37 +09001393 }
1394
1395 return count;
1396}
1397
Chanwoo Choi41468a12012-11-22 16:54:26 +09001398/**
1399 * charger_manager_register_sysfs - Register sysfs entry for each charger
1400 * @cm: the Charger Manager representing the battery.
1401 *
1402 * This function add sysfs entry for charger(regulator) to control charger from
1403 * user-space. If some development board use one more chargers for charging
1404 * but only need one charger on specific case which is dependent on user
1405 * scenario or hardware restrictions, the user enter 1 or 0(zero) to '/sys/
1406 * class/power_supply/battery/charger.[index]/externally_control'. For example,
1407 * if user enter 1 to 'sys/class/power_supply/battery/charger.[index]/
1408 * externally_control, this charger isn't controlled from charger-manager and
1409 * always stay off state of regulator.
1410 */
1411static int charger_manager_register_sysfs(struct charger_manager *cm)
1412{
1413 struct charger_desc *desc = cm->desc;
1414 struct charger_regulator *charger;
1415 int chargers_externally_control = 1;
1416 char buf[11];
1417 char *str;
1418 int ret = 0;
1419 int i;
1420
1421 /* Create sysfs entry to control charger(regulator) */
1422 for (i = 0; i < desc->num_charger_regulators; i++) {
1423 charger = &desc->charger_regulators[i];
1424
1425 snprintf(buf, 10, "charger.%d", i);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001426 str = devm_kzalloc(cm->dev,
1427 sizeof(char) * (strlen(buf) + 1), GFP_KERNEL);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001428 if (!str) {
Chanwoo Choi41468a12012-11-22 16:54:26 +09001429 ret = -ENOMEM;
1430 goto err;
1431 }
1432 strcpy(str, buf);
1433
1434 charger->attrs[0] = &charger->attr_name.attr;
1435 charger->attrs[1] = &charger->attr_state.attr;
1436 charger->attrs[2] = &charger->attr_externally_control.attr;
1437 charger->attrs[3] = NULL;
1438 charger->attr_g.name = str;
1439 charger->attr_g.attrs = charger->attrs;
1440
1441 sysfs_attr_init(&charger->attr_name.attr);
1442 charger->attr_name.attr.name = "name";
1443 charger->attr_name.attr.mode = 0444;
1444 charger->attr_name.show = charger_name_show;
1445
1446 sysfs_attr_init(&charger->attr_state.attr);
1447 charger->attr_state.attr.name = "state";
1448 charger->attr_state.attr.mode = 0444;
1449 charger->attr_state.show = charger_state_show;
1450
1451 sysfs_attr_init(&charger->attr_externally_control.attr);
1452 charger->attr_externally_control.attr.name
1453 = "externally_control";
1454 charger->attr_externally_control.attr.mode = 0644;
1455 charger->attr_externally_control.show
1456 = charger_externally_control_show;
1457 charger->attr_externally_control.store
1458 = charger_externally_control_store;
1459
1460 if (!desc->charger_regulators[i].externally_control ||
1461 !chargers_externally_control)
1462 chargers_externally_control = 0;
1463
Joe Perchese5409cb2013-06-06 18:25:12 -07001464 dev_info(cm->dev, "'%s' regulator's externally_control is %d\n",
1465 charger->regulator_name, charger->externally_control);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001466
1467 ret = sysfs_create_group(&cm->charger_psy.dev->kobj,
1468 &charger->attr_g);
1469 if (ret < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001470 dev_err(cm->dev, "Cannot create sysfs entry of %s regulator\n",
1471 charger->regulator_name);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001472 ret = -EINVAL;
1473 goto err;
1474 }
1475 }
1476
1477 if (chargers_externally_control) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001478 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 +09001479 ret = -EINVAL;
1480 goto err;
1481 }
1482
1483err:
1484 return ret;
1485}
1486
Jonghwa Lee5c49a622013-12-18 15:42:34 +09001487static int cm_init_thermal_data(struct charger_manager *cm)
1488{
1489 struct charger_desc *desc = cm->desc;
1490 union power_supply_propval val;
1491 int ret;
1492
1493 /* Verify whether fuel gauge provides battery temperature */
1494 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
1495 POWER_SUPPLY_PROP_TEMP, &val);
1496
1497 if (!ret) {
1498 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1499 POWER_SUPPLY_PROP_TEMP;
1500 cm->charger_psy.num_properties++;
1501 cm->desc->measure_battery_temp = true;
1502 }
1503#ifdef CONFIG_THERMAL
1504 cm->tzd_batt = cm->fuel_gauge->tzd;
1505
1506 if (ret && desc->thermal_zone) {
1507 cm->tzd_batt =
1508 thermal_zone_get_zone_by_name(desc->thermal_zone);
1509 if (IS_ERR(cm->tzd_batt))
1510 return PTR_ERR(cm->tzd_batt);
1511
1512 /* Use external thermometer */
1513 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1514 POWER_SUPPLY_PROP_TEMP_AMBIENT;
1515 cm->charger_psy.num_properties++;
1516 cm->desc->measure_battery_temp = true;
1517 ret = 0;
1518 }
1519#endif
1520 if (cm->desc->measure_battery_temp) {
1521 /* NOTICE : Default allowable minimum charge temperature is 0 */
1522 if (!desc->temp_max)
1523 desc->temp_max = CM_DEFAULT_CHARGE_TEMP_MAX;
1524 if (!desc->temp_diff)
1525 desc->temp_diff = CM_DEFAULT_RECHARGE_TEMP_DIFF;
1526 }
1527
1528 return ret;
1529}
1530
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001531static int charger_manager_probe(struct platform_device *pdev)
1532{
1533 struct charger_desc *desc = dev_get_platdata(&pdev->dev);
1534 struct charger_manager *cm;
1535 int ret = 0, i = 0;
Chanwoo Choibee737b2012-07-12 15:03:25 +09001536 int j = 0;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001537 union power_supply_propval val;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001538
1539 if (g_desc && !rtc_dev && g_desc->rtc_name) {
1540 rtc_dev = rtc_class_open(g_desc->rtc_name);
1541 if (IS_ERR_OR_NULL(rtc_dev)) {
1542 rtc_dev = NULL;
Joe Perchese5409cb2013-06-06 18:25:12 -07001543 dev_err(&pdev->dev, "Cannot get RTC %s\n",
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001544 g_desc->rtc_name);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001545 return -ENODEV;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001546 }
1547 }
1548
1549 if (!desc) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001550 dev_err(&pdev->dev, "No platform data (desc) found\n");
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001551 return -ENODEV;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001552 }
1553
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001554 cm = devm_kzalloc(&pdev->dev,
1555 sizeof(struct charger_manager), GFP_KERNEL);
1556 if (!cm)
1557 return -ENOMEM;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001558
1559 /* Basic Values. Unspecified are Null or 0 */
1560 cm->dev = &pdev->dev;
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001561 cm->desc = desc;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001562
Chanwoo Choid829dc72012-05-05 06:24:10 -07001563 /*
1564 * The following two do not need to be errors.
1565 * Users may intentionally ignore those two features.
1566 */
1567 if (desc->fullbatt_uV == 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001568 dev_info(&pdev->dev, "Ignoring full-battery voltage threshold as it is not supplied\n");
Chanwoo Choid829dc72012-05-05 06:24:10 -07001569 }
1570 if (!desc->fullbatt_vchkdrop_ms || !desc->fullbatt_vchkdrop_uV) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001571 dev_info(&pdev->dev, "Disabling full-battery voltage drop checking mechanism as it is not supplied\n");
Chanwoo Choid829dc72012-05-05 06:24:10 -07001572 desc->fullbatt_vchkdrop_ms = 0;
1573 desc->fullbatt_vchkdrop_uV = 0;
1574 }
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001575 if (desc->fullbatt_soc == 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001576 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 +09001577 }
1578 if (desc->fullbatt_full_capacity == 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001579 dev_info(&pdev->dev, "Ignoring full-battery full capacity threshold as it is not supplied\n");
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001580 }
Chanwoo Choid829dc72012-05-05 06:24:10 -07001581
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001582 if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001583 dev_err(&pdev->dev, "charger_regulators undefined\n");
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001584 return -EINVAL;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001585 }
1586
1587 if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001588 dev_err(&pdev->dev, "No power supply defined\n");
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001589 return -EINVAL;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001590 }
1591
1592 /* Counting index only */
1593 while (desc->psy_charger_stat[i])
1594 i++;
1595
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001596 cm->charger_stat = devm_kzalloc(&pdev->dev,
1597 sizeof(struct power_supply *) * i, GFP_KERNEL);
1598 if (!cm->charger_stat)
1599 return -ENOMEM;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001600
1601 for (i = 0; desc->psy_charger_stat[i]; i++) {
1602 cm->charger_stat[i] = power_supply_get_by_name(
1603 desc->psy_charger_stat[i]);
1604 if (!cm->charger_stat[i]) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001605 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
1606 desc->psy_charger_stat[i]);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001607 return -ENODEV;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001608 }
1609 }
1610
1611 cm->fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
1612 if (!cm->fuel_gauge) {
1613 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
Joe Perchese5409cb2013-06-06 18:25:12 -07001614 desc->psy_fuel_gauge);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001615 return -ENODEV;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001616 }
1617
1618 if (desc->polling_interval_ms == 0 ||
1619 msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) {
1620 dev_err(&pdev->dev, "polling_interval_ms is too small\n");
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001621 return -EINVAL;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001622 }
1623
Chanwoo Choi8fcfe082012-09-20 21:20:05 -07001624 if (!desc->charging_max_duration_ms ||
1625 !desc->discharging_max_duration_ms) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001626 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 -07001627 desc->charging_max_duration_ms = 0;
1628 desc->discharging_max_duration_ms = 0;
1629 }
1630
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001631 platform_set_drvdata(pdev, cm);
1632
Axel Linbb2a95c2012-01-12 12:56:35 +08001633 memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default));
1634
Chanwoo Choi41468a12012-11-22 16:54:26 +09001635 if (!desc->psy_name)
Axel Linbb2a95c2012-01-12 12:56:35 +08001636 strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001637 else
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001638 strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001639 cm->charger_psy.name = cm->psy_name_buf;
1640
1641 /* Allocate for psy properties because they may vary */
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001642 cm->charger_psy.properties = devm_kzalloc(&pdev->dev,
1643 sizeof(enum power_supply_property)
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001644 * (ARRAY_SIZE(default_charger_props) +
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001645 NUM_CHARGER_PSY_OPTIONAL), GFP_KERNEL);
1646 if (!cm->charger_psy.properties)
1647 return -ENOMEM;
1648
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001649 memcpy(cm->charger_psy.properties, default_charger_props,
1650 sizeof(enum power_supply_property) *
1651 ARRAY_SIZE(default_charger_props));
1652 cm->charger_psy.num_properties = psy_default.num_properties;
1653
1654 /* Find which optional psy-properties are available */
1655 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1656 POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
1657 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1658 POWER_SUPPLY_PROP_CHARGE_NOW;
1659 cm->charger_psy.num_properties++;
1660 }
1661 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1662 POWER_SUPPLY_PROP_CURRENT_NOW,
1663 &val)) {
1664 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1665 POWER_SUPPLY_PROP_CURRENT_NOW;
1666 cm->charger_psy.num_properties++;
1667 }
Axel Linbb2a95c2012-01-12 12:56:35 +08001668
Jonghwa Lee5c49a622013-12-18 15:42:34 +09001669 ret = cm_init_thermal_data(cm);
1670 if (ret) {
1671 dev_err(&pdev->dev, "Failed to initialize thermal data\n");
1672 cm->desc->measure_battery_temp = false;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001673 }
1674
Chanwoo Choid829dc72012-05-05 06:24:10 -07001675 INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk);
1676
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001677 ret = power_supply_register(NULL, &cm->charger_psy);
1678 if (ret) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001679 dev_err(&pdev->dev, "Cannot register charger-manager with name \"%s\"\n",
1680 cm->charger_psy.name);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001681 return ret;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001682 }
1683
Chanwoo Choi41468a12012-11-22 16:54:26 +09001684 /* Register extcon device for charger cable */
1685 ret = charger_manager_register_extcon(cm);
1686 if (ret < 0) {
1687 dev_err(&pdev->dev, "Cannot initialize extcon device\n");
1688 goto err_reg_extcon;
Chanwoo Choi3950c782012-09-21 18:49:37 +09001689 }
1690
Chanwoo Choi41468a12012-11-22 16:54:26 +09001691 /* Register sysfs entry for charger(regulator) */
1692 ret = charger_manager_register_sysfs(cm);
1693 if (ret < 0) {
1694 dev_err(&pdev->dev,
1695 "Cannot initialize sysfs entry of regulator\n");
1696 goto err_reg_sysfs;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001697 }
1698
1699 /* Add to the list */
1700 mutex_lock(&cm_list_mtx);
1701 list_add(&cm->entry, &cm_list);
1702 mutex_unlock(&cm_list_mtx);
1703
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001704 /*
1705 * Charger-manager is capable of waking up the systme from sleep
1706 * when event is happend through cm_notify_event()
1707 */
1708 device_init_wakeup(&pdev->dev, true);
1709 device_set_wakeup_capable(&pdev->dev, false);
1710
Chanwoo Choid829dc72012-05-05 06:24:10 -07001711 schedule_work(&setup_polling);
1712
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001713 return 0;
1714
Chanwoo Choi41468a12012-11-22 16:54:26 +09001715err_reg_sysfs:
Chanwoo Choi3950c782012-09-21 18:49:37 +09001716 for (i = 0; i < desc->num_charger_regulators; i++) {
1717 struct charger_regulator *charger;
1718
1719 charger = &desc->charger_regulators[i];
1720 sysfs_remove_group(&cm->charger_psy.dev->kobj,
1721 &charger->attr_g);
Chanwoo Choi3950c782012-09-21 18:49:37 +09001722 }
Chanwoo Choi41468a12012-11-22 16:54:26 +09001723err_reg_extcon:
1724 for (i = 0; i < desc->num_charger_regulators; i++) {
1725 struct charger_regulator *charger;
1726
1727 charger = &desc->charger_regulators[i];
1728 for (j = 0; j < charger->num_cables; j++) {
Chanwoo Choibee737b2012-07-12 15:03:25 +09001729 struct charger_cable *cable = &charger->cables[j];
Jonghwa Lee3cc9d2692013-06-25 14:02:49 +09001730 /* Remove notifier block if only edev exists */
1731 if (cable->extcon_dev.edev)
1732 extcon_unregister_interest(&cable->extcon_dev);
Chanwoo Choibee737b2012-07-12 15:03:25 +09001733 }
Chanwoo Choi41468a12012-11-22 16:54:26 +09001734
Chanwoo Choibee737b2012-07-12 15:03:25 +09001735 regulator_put(desc->charger_regulators[i].consumer);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001736 }
Chanwoo Choibee737b2012-07-12 15:03:25 +09001737
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001738 power_supply_unregister(&cm->charger_psy);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001739
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001740 return ret;
1741}
1742
Bill Pemberton415ec692012-11-19 13:26:07 -05001743static int charger_manager_remove(struct platform_device *pdev)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001744{
1745 struct charger_manager *cm = platform_get_drvdata(pdev);
1746 struct charger_desc *desc = cm->desc;
Chanwoo Choibee737b2012-07-12 15:03:25 +09001747 int i = 0;
1748 int j = 0;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001749
1750 /* Remove from the list */
1751 mutex_lock(&cm_list_mtx);
1752 list_del(&cm->entry);
1753 mutex_unlock(&cm_list_mtx);
1754
Tejun Heo2fbb5202012-12-21 17:56:51 -08001755 cancel_work_sync(&setup_polling);
1756 cancel_delayed_work_sync(&cm_monitor_work);
Chanwoo Choid829dc72012-05-05 06:24:10 -07001757
Chanwoo Choibee737b2012-07-12 15:03:25 +09001758 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1759 struct charger_regulator *charger
1760 = &desc->charger_regulators[i];
1761 for (j = 0 ; j < charger->num_cables ; j++) {
1762 struct charger_cable *cable = &charger->cables[j];
1763 extcon_unregister_interest(&cable->extcon_dev);
1764 }
1765 }
1766
1767 for (i = 0 ; i < desc->num_charger_regulators ; i++)
1768 regulator_put(desc->charger_regulators[i].consumer);
1769
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001770 power_supply_unregister(&cm->charger_psy);
Chanwoo Choid829dc72012-05-05 06:24:10 -07001771
1772 try_charger_enable(cm, false);
1773
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001774 return 0;
1775}
1776
Axel Lin1bbe24d2012-01-11 17:19:45 +08001777static const struct platform_device_id charger_manager_id[] = {
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001778 { "charger-manager", 0 },
1779 { },
1780};
Axel Lin1bbe24d2012-01-11 17:19:45 +08001781MODULE_DEVICE_TABLE(platform, charger_manager_id);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001782
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001783static int cm_suspend_noirq(struct device *dev)
1784{
1785 int ret = 0;
1786
1787 if (device_may_wakeup(dev)) {
1788 device_set_wakeup_capable(dev, false);
1789 ret = -EAGAIN;
1790 }
1791
1792 return ret;
1793}
1794
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001795static int cm_suspend_prepare(struct device *dev)
1796{
Axel Linbb2a95c2012-01-12 12:56:35 +08001797 struct charger_manager *cm = dev_get_drvdata(dev);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001798
1799 if (!cm_suspended) {
1800 if (rtc_dev) {
1801 struct rtc_time tmp;
1802 unsigned long now;
1803
1804 rtc_read_alarm(rtc_dev, &rtc_wkalarm_save);
1805 rtc_read_time(rtc_dev, &tmp);
1806
1807 if (rtc_wkalarm_save.enabled) {
1808 rtc_tm_to_time(&rtc_wkalarm_save.time,
1809 &rtc_wkalarm_save_time);
1810 rtc_tm_to_time(&tmp, &now);
1811 if (now > rtc_wkalarm_save_time)
1812 rtc_wkalarm_save_time = 0;
1813 } else {
1814 rtc_wkalarm_save_time = 0;
1815 }
1816 }
1817 cm_suspended = true;
1818 }
1819
Tejun Heo2fbb5202012-12-21 17:56:51 -08001820 cancel_delayed_work(&cm->fullbatt_vchk_work);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001821 cm->status_save_ext_pwr_inserted = is_ext_pwr_online(cm);
1822 cm->status_save_batt = is_batt_present(cm);
1823
1824 if (!cm_rtc_set) {
1825 cm_suspend_duration_ms = 0;
1826 cm_rtc_set = cm_setup_timer();
1827 }
1828
1829 return 0;
1830}
1831
1832static void cm_suspend_complete(struct device *dev)
1833{
Axel Linbb2a95c2012-01-12 12:56:35 +08001834 struct charger_manager *cm = dev_get_drvdata(dev);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001835
1836 if (cm_suspended) {
1837 if (rtc_dev) {
1838 struct rtc_wkalrm tmp;
1839
1840 rtc_read_alarm(rtc_dev, &tmp);
1841 rtc_wkalarm_save.pending = tmp.pending;
1842 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1843 }
1844 cm_suspended = false;
1845 cm_rtc_set = false;
1846 }
1847
Chanwoo Choid829dc72012-05-05 06:24:10 -07001848 /* Re-enqueue delayed work (fullbatt_vchk_work) */
1849 if (cm->fullbatt_vchk_jiffies_at) {
1850 unsigned long delay = 0;
1851 unsigned long now = jiffies + CM_JIFFIES_SMALL;
1852
1853 if (time_after_eq(now, cm->fullbatt_vchk_jiffies_at)) {
1854 delay = (unsigned long)((long)now
1855 - (long)(cm->fullbatt_vchk_jiffies_at));
1856 delay = jiffies_to_msecs(delay);
1857 } else {
1858 delay = 0;
1859 }
1860
1861 /*
1862 * Account for cm_suspend_duration_ms if
1863 * assume_timer_stops_in_suspend is active
1864 */
1865 if (g_desc && g_desc->assume_timer_stops_in_suspend) {
1866 if (delay > cm_suspend_duration_ms)
1867 delay -= cm_suspend_duration_ms;
1868 else
1869 delay = 0;
1870 }
1871
1872 queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
1873 msecs_to_jiffies(delay));
1874 }
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001875 device_set_wakeup_capable(cm->dev, false);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001876 uevent_notify(cm, NULL);
1877}
1878
1879static const struct dev_pm_ops charger_manager_pm = {
1880 .prepare = cm_suspend_prepare,
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001881 .suspend_noirq = cm_suspend_noirq,
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001882 .complete = cm_suspend_complete,
1883};
1884
1885static struct platform_driver charger_manager_driver = {
1886 .driver = {
1887 .name = "charger-manager",
1888 .owner = THIS_MODULE,
1889 .pm = &charger_manager_pm,
1890 },
1891 .probe = charger_manager_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -05001892 .remove = charger_manager_remove,
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001893 .id_table = charger_manager_id,
1894};
1895
1896static int __init charger_manager_init(void)
1897{
Chanwoo Choid829dc72012-05-05 06:24:10 -07001898 cm_wq = create_freezable_workqueue("charger_manager");
1899 INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller);
1900
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001901 return platform_driver_register(&charger_manager_driver);
1902}
1903late_initcall(charger_manager_init);
1904
1905static void __exit charger_manager_cleanup(void)
1906{
Chanwoo Choid829dc72012-05-05 06:24:10 -07001907 destroy_workqueue(cm_wq);
1908 cm_wq = NULL;
1909
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001910 platform_driver_unregister(&charger_manager_driver);
1911}
1912module_exit(charger_manager_cleanup);
1913
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001914/**
1915 * find_power_supply - find the associated power_supply of charger
1916 * @cm: the Charger Manager representing the battery
1917 * @psy: pointer to instance of charger's power_supply
1918 */
1919static bool find_power_supply(struct charger_manager *cm,
1920 struct power_supply *psy)
1921{
1922 int i;
1923 bool found = false;
1924
1925 for (i = 0; cm->charger_stat[i]; i++) {
1926 if (psy == cm->charger_stat[i]) {
1927 found = true;
1928 break;
1929 }
1930 }
1931
1932 return found;
1933}
1934
1935/**
1936 * cm_notify_event - charger driver notify Charger Manager of charger event
1937 * @psy: pointer to instance of charger's power_supply
1938 * @type: type of charger event
1939 * @msg: optional message passed to uevent_notify fuction
1940 */
1941void cm_notify_event(struct power_supply *psy, enum cm_event_types type,
1942 char *msg)
1943{
1944 struct charger_manager *cm;
1945 bool found_power_supply = false;
1946
1947 if (psy == NULL)
1948 return;
1949
1950 mutex_lock(&cm_list_mtx);
1951 list_for_each_entry(cm, &cm_list, entry) {
1952 found_power_supply = find_power_supply(cm, psy);
1953 if (found_power_supply)
1954 break;
1955 }
1956 mutex_unlock(&cm_list_mtx);
1957
1958 if (!found_power_supply)
1959 return;
1960
1961 switch (type) {
1962 case CM_EVENT_BATT_FULL:
1963 fullbatt_handler(cm);
1964 break;
1965 case CM_EVENT_BATT_OUT:
1966 battout_handler(cm);
1967 break;
1968 case CM_EVENT_BATT_IN:
1969 case CM_EVENT_EXT_PWR_IN_OUT ... CM_EVENT_CHG_START_STOP:
1970 misc_event_handler(cm, type);
1971 break;
1972 case CM_EVENT_UNKNOWN:
1973 case CM_EVENT_OTHERS:
1974 uevent_notify(cm, msg ? msg : default_event_names[type]);
1975 break;
1976 default:
Joe Perchese5409cb2013-06-06 18:25:12 -07001977 dev_err(cm->dev, "%s: type not specified\n", __func__);
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001978 break;
1979 }
1980}
1981EXPORT_SYMBOL_GPL(cm_notify_event);
1982
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001983MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1984MODULE_DESCRIPTION("Charger Manager");
1985MODULE_LICENSE("GPL");