blob: 7a1177ea238dd5686df5e020438c1b661ccbb14b [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 */
72static struct rtc_device *rtc_dev;
73/*
74 * Backup RTC alarm
75 * Save the wakeup alarm before entering suspend-to-RAM
76 */
77static struct rtc_wkalrm rtc_wkalarm_save;
78/* Backup RTC alarm time in terms of seconds since 01-01-1970 00:00:00 */
79static unsigned long rtc_wkalarm_save_time;
80static bool cm_suspended;
81static bool cm_rtc_set;
82static unsigned long cm_suspend_duration_ms;
83
Chanwoo Choid829dc72012-05-05 06:24:10 -070084/* About normal (not suspended) monitoring */
85static unsigned long polling_jiffy = ULONG_MAX; /* ULONG_MAX: no polling */
86static unsigned long next_polling; /* Next appointed polling time */
87static struct workqueue_struct *cm_wq; /* init at driver add */
88static struct delayed_work cm_monitor_work; /* init at driver add */
89
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090090/* Global charger-manager description */
91static struct charger_global_desc *g_desc; /* init with setup_charger_manager */
92
93/**
94 * is_batt_present - See if the battery presents in place.
95 * @cm: the Charger Manager representing the battery.
96 */
97static bool is_batt_present(struct charger_manager *cm)
98{
99 union power_supply_propval val;
100 bool present = false;
101 int i, ret;
102
103 switch (cm->desc->battery_present) {
Chanwoo Choid829dc72012-05-05 06:24:10 -0700104 case CM_BATTERY_PRESENT:
105 present = true;
106 break;
107 case CM_NO_BATTERY:
108 break;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900109 case CM_FUEL_GAUGE:
110 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
111 POWER_SUPPLY_PROP_PRESENT, &val);
112 if (ret == 0 && val.intval)
113 present = true;
114 break;
115 case CM_CHARGER_STAT:
116 for (i = 0; cm->charger_stat[i]; i++) {
117 ret = cm->charger_stat[i]->get_property(
118 cm->charger_stat[i],
119 POWER_SUPPLY_PROP_PRESENT, &val);
120 if (ret == 0 && val.intval) {
121 present = true;
122 break;
123 }
124 }
125 break;
126 }
127
128 return present;
129}
130
131/**
132 * is_ext_pwr_online - See if an external power source is attached to charge
133 * @cm: the Charger Manager representing the battery.
134 *
135 * Returns true if at least one of the chargers of the battery has an external
136 * power source attached to charge the battery regardless of whether it is
137 * actually charging or not.
138 */
139static bool is_ext_pwr_online(struct charger_manager *cm)
140{
141 union power_supply_propval val;
142 bool online = false;
143 int i, ret;
144
145 /* If at least one of them has one, it's yes. */
146 for (i = 0; cm->charger_stat[i]; i++) {
147 ret = cm->charger_stat[i]->get_property(
148 cm->charger_stat[i],
149 POWER_SUPPLY_PROP_ONLINE, &val);
150 if (ret == 0 && val.intval) {
151 online = true;
152 break;
153 }
154 }
155
156 return online;
157}
158
159/**
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900160 * get_batt_uV - Get the voltage level of the battery
161 * @cm: the Charger Manager representing the battery.
162 * @uV: the voltage level returned.
163 *
164 * Returns 0 if there is no error.
165 * Returns a negative value on error.
166 */
167static int get_batt_uV(struct charger_manager *cm, int *uV)
168{
169 union power_supply_propval val;
170 int ret;
171
Axel Linbb2a95c2012-01-12 12:56:35 +0800172 if (!cm->fuel_gauge)
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900173 return -ENODEV;
174
Axel Linbb2a95c2012-01-12 12:56:35 +0800175 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
176 POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900177 if (ret)
178 return ret;
179
180 *uV = val.intval;
181 return 0;
182}
183
184/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900185 * is_charging - Returns true if the battery is being charged.
186 * @cm: the Charger Manager representing the battery.
187 */
188static bool is_charging(struct charger_manager *cm)
189{
190 int i, ret;
191 bool charging = false;
192 union power_supply_propval val;
193
194 /* If there is no battery, it cannot be charged */
195 if (!is_batt_present(cm))
196 return false;
197
198 /* If at least one of the charger is charging, return yes */
199 for (i = 0; cm->charger_stat[i]; i++) {
200 /* 1. The charger sholuld not be DISABLED */
201 if (cm->emergency_stop)
202 continue;
203 if (!cm->charger_enabled)
204 continue;
205
206 /* 2. The charger should be online (ext-power) */
207 ret = cm->charger_stat[i]->get_property(
208 cm->charger_stat[i],
209 POWER_SUPPLY_PROP_ONLINE, &val);
210 if (ret) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700211 dev_warn(cm->dev, "Cannot read ONLINE value from %s\n",
212 cm->desc->psy_charger_stat[i]);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900213 continue;
214 }
215 if (val.intval == 0)
216 continue;
217
218 /*
219 * 3. The charger should not be FULL, DISCHARGING,
220 * or NOT_CHARGING.
221 */
222 ret = cm->charger_stat[i]->get_property(
223 cm->charger_stat[i],
224 POWER_SUPPLY_PROP_STATUS, &val);
225 if (ret) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700226 dev_warn(cm->dev, "Cannot read STATUS value from %s\n",
227 cm->desc->psy_charger_stat[i]);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900228 continue;
229 }
230 if (val.intval == POWER_SUPPLY_STATUS_FULL ||
231 val.intval == POWER_SUPPLY_STATUS_DISCHARGING ||
232 val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING)
233 continue;
234
235 /* Then, this is charging. */
236 charging = true;
237 break;
238 }
239
240 return charging;
241}
242
243/**
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900244 * is_full_charged - Returns true if the battery is fully charged.
245 * @cm: the Charger Manager representing the battery.
246 */
247static bool is_full_charged(struct charger_manager *cm)
248{
249 struct charger_desc *desc = cm->desc;
250 union power_supply_propval val;
251 int ret = 0;
252 int uV;
253
254 /* If there is no battery, it cannot be charged */
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900255 if (!is_batt_present(cm))
256 return false;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900257
258 if (cm->fuel_gauge && desc->fullbatt_full_capacity > 0) {
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900259 val.intval = 0;
260
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900261 /* Not full if capacity of fuel gauge isn't full */
262 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
263 POWER_SUPPLY_PROP_CHARGE_FULL, &val);
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900264 if (!ret && val.intval > desc->fullbatt_full_capacity)
265 return true;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900266 }
267
268 /* Full, if it's over the fullbatt voltage */
269 if (desc->fullbatt_uV > 0) {
270 ret = get_batt_uV(cm, &uV);
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900271 if (!ret && uV >= desc->fullbatt_uV)
272 return true;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900273 }
274
275 /* Full, if the capacity is more than fullbatt_soc */
276 if (cm->fuel_gauge && desc->fullbatt_soc > 0) {
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900277 val.intval = 0;
278
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900279 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
280 POWER_SUPPLY_PROP_CAPACITY, &val);
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900281 if (!ret && val.intval >= desc->fullbatt_soc)
282 return true;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900283 }
284
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900285 return false;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900286}
287
288/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900289 * is_polling_required - Return true if need to continue polling for this CM.
290 * @cm: the Charger Manager representing the battery.
291 */
292static bool is_polling_required(struct charger_manager *cm)
293{
294 switch (cm->desc->polling_mode) {
295 case CM_POLL_DISABLE:
296 return false;
297 case CM_POLL_ALWAYS:
298 return true;
299 case CM_POLL_EXTERNAL_POWER_ONLY:
300 return is_ext_pwr_online(cm);
301 case CM_POLL_CHARGING_ONLY:
302 return is_charging(cm);
303 default:
304 dev_warn(cm->dev, "Incorrect polling_mode (%d)\n",
Joe Perchese5409cb2013-06-06 18:25:12 -0700305 cm->desc->polling_mode);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900306 }
307
308 return false;
309}
310
311/**
312 * try_charger_enable - Enable/Disable chargers altogether
313 * @cm: the Charger Manager representing the battery.
314 * @enable: true: enable / false: disable
315 *
316 * Note that Charger Manager keeps the charger enabled regardless whether
317 * the charger is charging or not (because battery is full or no external
318 * power source exists) except when CM needs to disable chargers forcibly
319 * bacause of emergency causes; when the battery is overheated or too cold.
320 */
321static int try_charger_enable(struct charger_manager *cm, bool enable)
322{
323 int err = 0, i;
324 struct charger_desc *desc = cm->desc;
325
326 /* Ignore if it's redundent command */
Axel Linbb2a95c2012-01-12 12:56:35 +0800327 if (enable == cm->charger_enabled)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900328 return 0;
329
330 if (enable) {
331 if (cm->emergency_stop)
332 return -EAGAIN;
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700333
334 /*
335 * Save start time of charging to limit
336 * maximum possible charging time.
337 */
338 cm->charging_start_time = ktime_to_ms(ktime_get());
339 cm->charging_end_time = 0;
340
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900341 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
Chanwoo Choi3950c782012-09-21 18:49:37 +0900342 if (desc->charger_regulators[i].externally_control)
343 continue;
344
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900345 err = regulator_enable(desc->charger_regulators[i].consumer);
346 if (err < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700347 dev_warn(cm->dev, "Cannot enable %s regulator\n",
348 desc->charger_regulators[i].regulator_name);
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900349 }
350 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900351 } else {
352 /*
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700353 * Save end time of charging to maintain fully charged state
354 * of battery after full-batt.
355 */
356 cm->charging_start_time = 0;
357 cm->charging_end_time = ktime_to_ms(ktime_get());
358
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900359 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
Chanwoo Choi3950c782012-09-21 18:49:37 +0900360 if (desc->charger_regulators[i].externally_control)
361 continue;
362
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900363 err = regulator_disable(desc->charger_regulators[i].consumer);
364 if (err < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700365 dev_warn(cm->dev, "Cannot disable %s regulator\n",
366 desc->charger_regulators[i].regulator_name);
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900367 }
368 }
369
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900370 /*
371 * Abnormal battery state - Stop charging forcibly,
372 * even if charger was enabled at the other places
373 */
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900374 for (i = 0; i < desc->num_charger_regulators; i++) {
375 if (regulator_is_enabled(
376 desc->charger_regulators[i].consumer)) {
377 regulator_force_disable(
378 desc->charger_regulators[i].consumer);
Joe Perchese5409cb2013-06-06 18:25:12 -0700379 dev_warn(cm->dev, "Disable regulator(%s) forcibly\n",
380 desc->charger_regulators[i].regulator_name);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900381 }
382 }
383 }
384
385 if (!err)
386 cm->charger_enabled = enable;
387
388 return err;
389}
390
391/**
Chanwoo Choid829dc72012-05-05 06:24:10 -0700392 * try_charger_restart - Restart charging.
393 * @cm: the Charger Manager representing the battery.
394 *
395 * Restart charging by turning off and on the charger.
396 */
397static int try_charger_restart(struct charger_manager *cm)
398{
399 int err;
400
401 if (cm->emergency_stop)
402 return -EAGAIN;
403
404 err = try_charger_enable(cm, false);
405 if (err)
406 return err;
407
408 return try_charger_enable(cm, true);
409}
410
411/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900412 * uevent_notify - Let users know something has changed.
413 * @cm: the Charger Manager representing the battery.
414 * @event: the event string.
415 *
416 * If @event is null, it implies that uevent_notify is called
417 * by resume function. When called in the resume function, cm_suspended
418 * should be already reset to false in order to let uevent_notify
419 * notify the recent event during the suspend to users. While
420 * suspended, uevent_notify does not notify users, but tracks
421 * events so that uevent_notify can notify users later after resumed.
422 */
423static void uevent_notify(struct charger_manager *cm, const char *event)
424{
425 static char env_str[UEVENT_BUF_SIZE + 1] = "";
426 static char env_str_save[UEVENT_BUF_SIZE + 1] = "";
427
428 if (cm_suspended) {
429 /* Nothing in suspended-event buffer */
430 if (env_str_save[0] == 0) {
431 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
432 return; /* status not changed */
433 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
434 return;
435 }
436
437 if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE))
438 return; /* Duplicated. */
Axel Linbb2a95c2012-01-12 12:56:35 +0800439 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900440 return;
441 }
442
443 if (event == NULL) {
444 /* No messages pending */
445 if (!env_str_save[0])
446 return;
447
448 strncpy(env_str, env_str_save, UEVENT_BUF_SIZE);
449 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
450 env_str_save[0] = 0;
451
452 return;
453 }
454
455 /* status not changed */
456 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
457 return;
458
459 /* save the status and notify the update */
460 strncpy(env_str, event, UEVENT_BUF_SIZE);
461 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
462
Joe Perchese5409cb2013-06-06 18:25:12 -0700463 dev_info(cm->dev, "%s\n", event);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900464}
465
466/**
Chanwoo Choid829dc72012-05-05 06:24:10 -0700467 * fullbatt_vchk - Check voltage drop some times after "FULL" event.
468 * @work: the work_struct appointing the function
469 *
470 * If a user has designated "fullbatt_vchkdrop_ms/uV" values with
471 * charger_desc, Charger Manager checks voltage drop after the battery
472 * "FULL" event. It checks whether the voltage has dropped more than
473 * fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms.
474 */
475static void fullbatt_vchk(struct work_struct *work)
476{
477 struct delayed_work *dwork = to_delayed_work(work);
478 struct charger_manager *cm = container_of(dwork,
479 struct charger_manager, fullbatt_vchk_work);
480 struct charger_desc *desc = cm->desc;
481 int batt_uV, err, diff;
482
483 /* remove the appointment for fullbatt_vchk */
484 cm->fullbatt_vchk_jiffies_at = 0;
485
486 if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
487 return;
488
489 err = get_batt_uV(cm, &batt_uV);
490 if (err) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700491 dev_err(cm->dev, "%s: get_batt_uV error(%d)\n", __func__, err);
Chanwoo Choid829dc72012-05-05 06:24:10 -0700492 return;
493 }
494
Chanwoo Choif36b9dd2012-11-22 16:53:51 +0900495 diff = desc->fullbatt_uV - batt_uV;
496 if (diff < 0)
497 return;
Chanwoo Choid829dc72012-05-05 06:24:10 -0700498
Joe Perchese5409cb2013-06-06 18:25:12 -0700499 dev_info(cm->dev, "VBATT dropped %duV after full-batt\n", diff);
Chanwoo Choid829dc72012-05-05 06:24:10 -0700500
501 if (diff > desc->fullbatt_vchkdrop_uV) {
502 try_charger_restart(cm);
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700503 uevent_notify(cm, "Recharging");
Chanwoo Choid829dc72012-05-05 06:24:10 -0700504 }
505}
506
507/**
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700508 * check_charging_duration - Monitor charging/discharging duration
509 * @cm: the Charger Manager representing the battery.
510 *
511 * If whole charging duration exceed 'charging_max_duration_ms',
512 * cm stop charging to prevent overcharge/overheat. If discharging
513 * duration exceed 'discharging _max_duration_ms', charger cable is
514 * attached, after full-batt, cm start charging to maintain fully
515 * charged state for battery.
516 */
517static int check_charging_duration(struct charger_manager *cm)
518{
519 struct charger_desc *desc = cm->desc;
520 u64 curr = ktime_to_ms(ktime_get());
521 u64 duration;
522 int ret = false;
523
524 if (!desc->charging_max_duration_ms &&
525 !desc->discharging_max_duration_ms)
526 return ret;
527
528 if (cm->charger_enabled) {
529 duration = curr - cm->charging_start_time;
530
531 if (duration > desc->charging_max_duration_ms) {
Jonghwa Lee856ee612013-12-18 15:42:35 +0900532 dev_info(cm->dev, "Charging duration exceed %ums\n",
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700533 desc->charging_max_duration_ms);
534 uevent_notify(cm, "Discharging");
535 try_charger_enable(cm, false);
536 ret = true;
537 }
538 } else if (is_ext_pwr_online(cm) && !cm->charger_enabled) {
539 duration = curr - cm->charging_end_time;
540
541 if (duration > desc->charging_max_duration_ms &&
542 is_ext_pwr_online(cm)) {
Jonghwa Lee856ee612013-12-18 15:42:35 +0900543 dev_info(cm->dev, "Discharging duration exceed %ums\n",
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700544 desc->discharging_max_duration_ms);
Joe Perchese5409cb2013-06-06 18:25:12 -0700545 uevent_notify(cm, "Recharging");
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700546 try_charger_enable(cm, true);
547 ret = true;
548 }
549 }
550
551 return ret;
552}
553
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900554static int cm_get_battery_temperature(struct charger_manager *cm,
555 int *temp)
556{
557 int ret;
558
559 if (!cm->desc->measure_battery_temp)
560 return -ENODEV;
561
562#ifdef CONFIG_THERMAL
563 ret = thermal_zone_get_temp(cm->tzd_batt, (unsigned long *)temp);
564 if (!ret)
565 /* Calibrate temperature unit */
566 *temp /= 100;
567#else
568 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
569 POWER_SUPPLY_PROP_TEMP,
570 (union power_supply_propval *)temp);
571#endif
572 return ret;
573}
574
575static int cm_check_thermal_status(struct charger_manager *cm)
576{
577 struct charger_desc *desc = cm->desc;
578 int temp, upper_limit, lower_limit;
579 int ret = 0;
580
581 ret = cm_get_battery_temperature(cm, &temp);
582 if (ret) {
583 /* FIXME:
584 * No information of battery temperature might
585 * occur hazadous result. We have to handle it
586 * depending on battery type.
587 */
588 dev_err(cm->dev, "Failed to get battery temperature\n");
589 return 0;
590 }
591
592 upper_limit = desc->temp_max;
593 lower_limit = desc->temp_min;
594
595 if (cm->emergency_stop) {
596 upper_limit -= desc->temp_diff;
597 lower_limit += desc->temp_diff;
598 }
599
600 if (temp > upper_limit)
601 ret = CM_EVENT_BATT_OVERHEAT;
602 else if (temp < lower_limit)
603 ret = CM_EVENT_BATT_COLD;
604
605 return ret;
606}
607
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700608/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900609 * _cm_monitor - Monitor the temperature and return true for exceptions.
610 * @cm: the Charger Manager representing the battery.
611 *
612 * Returns true if there is an event to notify for the battery.
613 * (True if the status of "emergency_stop" changes)
614 */
615static bool _cm_monitor(struct charger_manager *cm)
616{
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900617 int temp_alrt;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900618
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900619 temp_alrt = cm_check_thermal_status(cm);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900620
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900621 /* It has been stopped already */
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900622 if (temp_alrt && cm->emergency_stop)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900623 return false;
624
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900625 /*
626 * Check temperature whether overheat or cold.
627 * If temperature is out of range normal state, stop charging.
628 */
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900629 if (temp_alrt) {
630 cm->emergency_stop = temp_alrt;
631 if (!try_charger_enable(cm, false))
632 uevent_notify(cm, default_event_names[temp_alrt]);
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900633
634 /*
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700635 * Check whole charging duration and discharing duration
636 * after full-batt.
637 */
638 } else if (!cm->emergency_stop && check_charging_duration(cm)) {
639 dev_dbg(cm->dev,
Joe Perchese5409cb2013-06-06 18:25:12 -0700640 "Charging/Discharging duration is out of range\n");
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700641 /*
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900642 * Check dropped voltage of battery. If battery voltage is more
643 * dropped than fullbatt_vchkdrop_uV after fully charged state,
644 * charger-manager have to recharge battery.
645 */
646 } else if (!cm->emergency_stop && is_ext_pwr_online(cm) &&
647 !cm->charger_enabled) {
648 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
649
650 /*
651 * Check whether fully charged state to protect overcharge
652 * if charger-manager is charging for battery.
653 */
654 } else if (!cm->emergency_stop && is_full_charged(cm) &&
655 cm->charger_enabled) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700656 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged\n");
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900657 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
658
659 try_charger_enable(cm, false);
660
661 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900662 } else {
663 cm->emergency_stop = 0;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900664 if (is_ext_pwr_online(cm)) {
665 if (!try_charger_enable(cm, true))
666 uevent_notify(cm, "CHARGING");
667 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900668 }
669
670 return true;
671}
672
673/**
674 * cm_monitor - Monitor every battery.
675 *
676 * Returns true if there is an event to notify from any of the batteries.
677 * (True if the status of "emergency_stop" changes)
678 */
679static bool cm_monitor(void)
680{
681 bool stop = false;
682 struct charger_manager *cm;
683
684 mutex_lock(&cm_list_mtx);
685
Axel Linbb2a95c2012-01-12 12:56:35 +0800686 list_for_each_entry(cm, &cm_list, entry) {
687 if (_cm_monitor(cm))
688 stop = true;
689 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900690
691 mutex_unlock(&cm_list_mtx);
692
693 return stop;
694}
695
Chanwoo Choid829dc72012-05-05 06:24:10 -0700696/**
697 * _setup_polling - Setup the next instance of polling.
698 * @work: work_struct of the function _setup_polling.
699 */
700static void _setup_polling(struct work_struct *work)
701{
702 unsigned long min = ULONG_MAX;
703 struct charger_manager *cm;
704 bool keep_polling = false;
705 unsigned long _next_polling;
706
707 mutex_lock(&cm_list_mtx);
708
709 list_for_each_entry(cm, &cm_list, entry) {
710 if (is_polling_required(cm) && cm->desc->polling_interval_ms) {
711 keep_polling = true;
712
713 if (min > cm->desc->polling_interval_ms)
714 min = cm->desc->polling_interval_ms;
715 }
716 }
717
718 polling_jiffy = msecs_to_jiffies(min);
719 if (polling_jiffy <= CM_JIFFIES_SMALL)
720 polling_jiffy = CM_JIFFIES_SMALL + 1;
721
722 if (!keep_polling)
723 polling_jiffy = ULONG_MAX;
724 if (polling_jiffy == ULONG_MAX)
725 goto out;
726
727 WARN(cm_wq == NULL, "charger-manager: workqueue not initialized"
728 ". try it later. %s\n", __func__);
729
Tejun Heo2fbb5202012-12-21 17:56:51 -0800730 /*
731 * Use mod_delayed_work() iff the next polling interval should
732 * occur before the currently scheduled one. If @cm_monitor_work
733 * isn't active, the end result is the same, so no need to worry
734 * about stale @next_polling.
735 */
Chanwoo Choid829dc72012-05-05 06:24:10 -0700736 _next_polling = jiffies + polling_jiffy;
737
Tejun Heo2fbb5202012-12-21 17:56:51 -0800738 if (time_before(_next_polling, next_polling)) {
Tejun Heo41f63c52012-08-03 10:30:47 -0700739 mod_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy);
Tejun Heo2fbb5202012-12-21 17:56:51 -0800740 next_polling = _next_polling;
741 } else {
742 if (queue_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy))
743 next_polling = _next_polling;
Chanwoo Choid829dc72012-05-05 06:24:10 -0700744 }
Chanwoo Choid829dc72012-05-05 06:24:10 -0700745out:
746 mutex_unlock(&cm_list_mtx);
747}
748static DECLARE_WORK(setup_polling, _setup_polling);
749
750/**
751 * cm_monitor_poller - The Monitor / Poller.
752 * @work: work_struct of the function cm_monitor_poller
753 *
754 * During non-suspended state, cm_monitor_poller is used to poll and monitor
755 * the batteries.
756 */
757static void cm_monitor_poller(struct work_struct *work)
758{
759 cm_monitor();
760 schedule_work(&setup_polling);
761}
762
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700763/**
764 * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL
765 * @cm: the Charger Manager representing the battery.
766 */
767static void fullbatt_handler(struct charger_manager *cm)
768{
769 struct charger_desc *desc = cm->desc;
770
771 if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
772 goto out;
773
774 if (cm_suspended)
775 device_set_wakeup_capable(cm->dev, true);
776
Tejun Heo41f63c52012-08-03 10:30:47 -0700777 mod_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
778 msecs_to_jiffies(desc->fullbatt_vchkdrop_ms));
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700779 cm->fullbatt_vchk_jiffies_at = jiffies + msecs_to_jiffies(
780 desc->fullbatt_vchkdrop_ms);
781
782 if (cm->fullbatt_vchk_jiffies_at == 0)
783 cm->fullbatt_vchk_jiffies_at = 1;
784
785out:
Joe Perchese5409cb2013-06-06 18:25:12 -0700786 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged\n");
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700787 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
788}
789
790/**
791 * battout_handler - Event handler for CM_EVENT_BATT_OUT
792 * @cm: the Charger Manager representing the battery.
793 */
794static void battout_handler(struct charger_manager *cm)
795{
796 if (cm_suspended)
797 device_set_wakeup_capable(cm->dev, true);
798
799 if (!is_batt_present(cm)) {
800 dev_emerg(cm->dev, "Battery Pulled Out!\n");
801 uevent_notify(cm, default_event_names[CM_EVENT_BATT_OUT]);
802 } else {
803 uevent_notify(cm, "Battery Reinserted?");
804 }
805}
806
807/**
808 * misc_event_handler - Handler for other evnets
809 * @cm: the Charger Manager representing the battery.
810 * @type: the Charger Manager representing the battery.
811 */
812static void misc_event_handler(struct charger_manager *cm,
813 enum cm_event_types type)
814{
815 if (cm_suspended)
816 device_set_wakeup_capable(cm->dev, true);
817
Tejun Heo2fbb5202012-12-21 17:56:51 -0800818 if (is_polling_required(cm) && cm->desc->polling_interval_ms)
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700819 schedule_work(&setup_polling);
820 uevent_notify(cm, default_event_names[type]);
821}
822
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900823static int charger_get_property(struct power_supply *psy,
824 enum power_supply_property psp,
825 union power_supply_propval *val)
826{
827 struct charger_manager *cm = container_of(psy,
828 struct charger_manager, charger_psy);
829 struct charger_desc *desc = cm->desc;
Anton Vorontsovdf58c042012-03-15 21:01:28 +0400830 int ret = 0;
831 int uV;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900832
833 switch (psp) {
834 case POWER_SUPPLY_PROP_STATUS:
835 if (is_charging(cm))
836 val->intval = POWER_SUPPLY_STATUS_CHARGING;
837 else if (is_ext_pwr_online(cm))
838 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
839 else
840 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
841 break;
842 case POWER_SUPPLY_PROP_HEALTH:
843 if (cm->emergency_stop > 0)
844 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
845 else if (cm->emergency_stop < 0)
846 val->intval = POWER_SUPPLY_HEALTH_COLD;
847 else
848 val->intval = POWER_SUPPLY_HEALTH_GOOD;
849 break;
850 case POWER_SUPPLY_PROP_PRESENT:
851 if (is_batt_present(cm))
852 val->intval = 1;
853 else
854 val->intval = 0;
855 break;
856 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Anton Vorontsovdf58c042012-03-15 21:01:28 +0400857 ret = get_batt_uV(cm, &val->intval);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900858 break;
859 case POWER_SUPPLY_PROP_CURRENT_NOW:
860 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
861 POWER_SUPPLY_PROP_CURRENT_NOW, val);
862 break;
863 case POWER_SUPPLY_PROP_TEMP:
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900864 case POWER_SUPPLY_PROP_TEMP_AMBIENT:
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900865 return cm_get_battery_temperature(cm, &val->intval);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900866 case POWER_SUPPLY_PROP_CAPACITY:
867 if (!cm->fuel_gauge) {
868 ret = -ENODEV;
869 break;
870 }
871
872 if (!is_batt_present(cm)) {
873 /* There is no battery. Assume 100% */
874 val->intval = 100;
875 break;
876 }
877
878 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
879 POWER_SUPPLY_PROP_CAPACITY, val);
880 if (ret)
881 break;
882
883 if (val->intval > 100) {
884 val->intval = 100;
885 break;
886 }
887 if (val->intval < 0)
888 val->intval = 0;
889
890 /* Do not adjust SOC when charging: voltage is overrated */
891 if (is_charging(cm))
892 break;
893
894 /*
895 * If the capacity value is inconsistent, calibrate it base on
896 * the battery voltage values and the thresholds given as desc
897 */
898 ret = get_batt_uV(cm, &uV);
899 if (ret) {
900 /* Voltage information not available. No calibration */
901 ret = 0;
902 break;
903 }
904
905 if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
906 !is_charging(cm)) {
907 val->intval = 100;
908 break;
909 }
910
911 break;
912 case POWER_SUPPLY_PROP_ONLINE:
913 if (is_ext_pwr_online(cm))
914 val->intval = 1;
915 else
916 val->intval = 0;
917 break;
918 case POWER_SUPPLY_PROP_CHARGE_FULL:
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900919 if (is_full_charged(cm))
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900920 val->intval = 1;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900921 else
922 val->intval = 0;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900923 ret = 0;
924 break;
925 case POWER_SUPPLY_PROP_CHARGE_NOW:
926 if (is_charging(cm)) {
927 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
928 POWER_SUPPLY_PROP_CHARGE_NOW,
929 val);
930 if (ret) {
931 val->intval = 1;
932 ret = 0;
933 } else {
934 /* If CHARGE_NOW is supplied, use it */
935 val->intval = (val->intval > 0) ?
936 val->intval : 1;
937 }
938 } else {
939 val->intval = 0;
940 }
941 break;
942 default:
943 return -EINVAL;
944 }
945 return ret;
946}
947
948#define NUM_CHARGER_PSY_OPTIONAL (4)
949static enum power_supply_property default_charger_props[] = {
950 /* Guaranteed to provide */
951 POWER_SUPPLY_PROP_STATUS,
952 POWER_SUPPLY_PROP_HEALTH,
953 POWER_SUPPLY_PROP_PRESENT,
954 POWER_SUPPLY_PROP_VOLTAGE_NOW,
955 POWER_SUPPLY_PROP_CAPACITY,
956 POWER_SUPPLY_PROP_ONLINE,
957 POWER_SUPPLY_PROP_CHARGE_FULL,
958 /*
959 * Optional properties are:
960 * POWER_SUPPLY_PROP_CHARGE_NOW,
961 * POWER_SUPPLY_PROP_CURRENT_NOW,
962 * POWER_SUPPLY_PROP_TEMP, and
963 * POWER_SUPPLY_PROP_TEMP_AMBIENT,
964 */
965};
966
967static struct power_supply psy_default = {
968 .name = "battery",
969 .type = POWER_SUPPLY_TYPE_BATTERY,
970 .properties = default_charger_props,
971 .num_properties = ARRAY_SIZE(default_charger_props),
972 .get_property = charger_get_property,
Krzysztof Kozlowskiba9c9182014-10-07 17:47:37 +0200973 .no_thermal = true,
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900974};
975
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900976/**
977 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
978 * for suspend_again.
979 *
980 * Returns true if the alarm is set for Charger Manager to use.
981 * Returns false if
982 * cm_setup_timer fails to set an alarm,
983 * cm_setup_timer does not need to set an alarm for Charger Manager,
984 * or an alarm previously configured is to be used.
985 */
986static bool cm_setup_timer(void)
987{
988 struct charger_manager *cm;
989 unsigned int wakeup_ms = UINT_MAX;
990 bool ret = false;
991
992 mutex_lock(&cm_list_mtx);
993
994 list_for_each_entry(cm, &cm_list, entry) {
Chanwoo Choid829dc72012-05-05 06:24:10 -0700995 unsigned int fbchk_ms = 0;
996
997 /* fullbatt_vchk is required. setup timer for that */
998 if (cm->fullbatt_vchk_jiffies_at) {
999 fbchk_ms = jiffies_to_msecs(cm->fullbatt_vchk_jiffies_at
1000 - jiffies);
1001 if (time_is_before_eq_jiffies(
1002 cm->fullbatt_vchk_jiffies_at) ||
1003 msecs_to_jiffies(fbchk_ms) < CM_JIFFIES_SMALL) {
1004 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
1005 fbchk_ms = 0;
1006 }
1007 }
1008 CM_MIN_VALID(wakeup_ms, fbchk_ms);
1009
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001010 /* Skip if polling is not required for this CM */
1011 if (!is_polling_required(cm) && !cm->emergency_stop)
1012 continue;
1013 if (cm->desc->polling_interval_ms == 0)
1014 continue;
1015 CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
1016 }
1017
1018 mutex_unlock(&cm_list_mtx);
1019
1020 if (wakeup_ms < UINT_MAX && wakeup_ms > 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001021 pr_info("Charger Manager wakeup timer: %u ms\n", wakeup_ms);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001022 if (rtc_dev) {
1023 struct rtc_wkalrm tmp;
1024 unsigned long time, now;
1025 unsigned long add = DIV_ROUND_UP(wakeup_ms, 1000);
1026
1027 /*
1028 * Set alarm with the polling interval (wakeup_ms)
1029 * except when rtc_wkalarm_save comes first.
1030 * However, the alarm time should be NOW +
1031 * CM_RTC_SMALL or later.
1032 */
1033 tmp.enabled = 1;
1034 rtc_read_time(rtc_dev, &tmp.time);
1035 rtc_tm_to_time(&tmp.time, &now);
1036 if (add < CM_RTC_SMALL)
1037 add = CM_RTC_SMALL;
1038 time = now + add;
1039
1040 ret = true;
1041
1042 if (rtc_wkalarm_save.enabled &&
1043 rtc_wkalarm_save_time &&
1044 rtc_wkalarm_save_time < time) {
1045 if (rtc_wkalarm_save_time < now + CM_RTC_SMALL)
1046 time = now + CM_RTC_SMALL;
1047 else
1048 time = rtc_wkalarm_save_time;
1049
1050 /* The timer is not appointed by CM */
1051 ret = false;
1052 }
1053
Joe Perchese5409cb2013-06-06 18:25:12 -07001054 pr_info("Waking up after %lu secs\n", time - now);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001055
1056 rtc_time_to_tm(time, &tmp.time);
1057 rtc_set_alarm(rtc_dev, &tmp);
1058 cm_suspend_duration_ms += wakeup_ms;
1059 return ret;
1060 }
1061 }
1062
1063 if (rtc_dev)
1064 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1065 return false;
1066}
1067
Chanwoo Choid829dc72012-05-05 06:24:10 -07001068static void _cm_fbchk_in_suspend(struct charger_manager *cm)
1069{
1070 unsigned long jiffy_now = jiffies;
1071
1072 if (!cm->fullbatt_vchk_jiffies_at)
1073 return;
1074
1075 if (g_desc && g_desc->assume_timer_stops_in_suspend)
1076 jiffy_now += msecs_to_jiffies(cm_suspend_duration_ms);
1077
1078 /* Execute now if it's going to be executed not too long after */
1079 jiffy_now += CM_JIFFIES_SMALL;
1080
1081 if (time_after_eq(jiffy_now, cm->fullbatt_vchk_jiffies_at))
1082 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
1083}
1084
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001085/**
1086 * cm_suspend_again - Determine whether suspend again or not
1087 *
1088 * Returns true if the system should be suspended again
1089 * Returns false if the system should be woken up
1090 */
1091bool cm_suspend_again(void)
1092{
1093 struct charger_manager *cm;
1094 bool ret = false;
1095
1096 if (!g_desc || !g_desc->rtc_only_wakeup || !g_desc->rtc_only_wakeup() ||
1097 !cm_rtc_set)
1098 return false;
1099
1100 if (cm_monitor())
1101 goto out;
1102
1103 ret = true;
1104 mutex_lock(&cm_list_mtx);
1105 list_for_each_entry(cm, &cm_list, entry) {
Chanwoo Choid829dc72012-05-05 06:24:10 -07001106 _cm_fbchk_in_suspend(cm);
1107
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001108 if (cm->status_save_ext_pwr_inserted != is_ext_pwr_online(cm) ||
Axel Linbb2a95c2012-01-12 12:56:35 +08001109 cm->status_save_batt != is_batt_present(cm)) {
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001110 ret = false;
Axel Linbb2a95c2012-01-12 12:56:35 +08001111 break;
1112 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001113 }
1114 mutex_unlock(&cm_list_mtx);
1115
1116 cm_rtc_set = cm_setup_timer();
1117out:
1118 /* It's about the time when the non-CM appointed timer goes off */
1119 if (rtc_wkalarm_save.enabled) {
1120 unsigned long now;
1121 struct rtc_time tmp;
1122
1123 rtc_read_time(rtc_dev, &tmp);
1124 rtc_tm_to_time(&tmp, &now);
1125
1126 if (rtc_wkalarm_save_time &&
1127 now + CM_RTC_SMALL >= rtc_wkalarm_save_time)
1128 return false;
1129 }
1130 return ret;
1131}
1132EXPORT_SYMBOL_GPL(cm_suspend_again);
1133
1134/**
1135 * setup_charger_manager - initialize charger_global_desc data
1136 * @gd: pointer to instance of charger_global_desc
1137 */
1138int setup_charger_manager(struct charger_global_desc *gd)
1139{
1140 if (!gd)
1141 return -EINVAL;
1142
1143 if (rtc_dev)
1144 rtc_class_close(rtc_dev);
1145 rtc_dev = NULL;
1146 g_desc = NULL;
1147
1148 if (!gd->rtc_only_wakeup) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001149 pr_err("The callback rtc_only_wakeup is not given\n");
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001150 return -EINVAL;
1151 }
1152
1153 if (gd->rtc_name) {
1154 rtc_dev = rtc_class_open(gd->rtc_name);
1155 if (IS_ERR_OR_NULL(rtc_dev)) {
1156 rtc_dev = NULL;
1157 /* Retry at probe. RTC may be not registered yet */
1158 }
1159 } else {
Joe Perchese5409cb2013-06-06 18:25:12 -07001160 pr_warn("No wakeup timer is given for charger manager. "
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001161 "In-suspend monitoring won't work.\n");
1162 }
1163
1164 g_desc = gd;
1165 return 0;
1166}
1167EXPORT_SYMBOL_GPL(setup_charger_manager);
1168
Chanwoo Choibee737b2012-07-12 15:03:25 +09001169/**
1170 * charger_extcon_work - enable/diable charger according to the state
1171 * of charger cable
1172 *
1173 * @work: work_struct of the function charger_extcon_work.
1174 */
1175static void charger_extcon_work(struct work_struct *work)
1176{
1177 struct charger_cable *cable =
1178 container_of(work, struct charger_cable, wq);
Chanwoo Choi45cd4fb2012-07-12 15:03:29 +09001179 int ret;
1180
1181 if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) {
1182 ret = regulator_set_current_limit(cable->charger->consumer,
1183 cable->min_uA, cable->max_uA);
1184 if (ret < 0) {
1185 pr_err("Cannot set current limit of %s (%s)\n",
Joe Perchese5409cb2013-06-06 18:25:12 -07001186 cable->charger->regulator_name, cable->name);
Chanwoo Choi45cd4fb2012-07-12 15:03:29 +09001187 return;
1188 }
1189
1190 pr_info("Set current limit of %s : %duA ~ %duA\n",
Joe Perchese5409cb2013-06-06 18:25:12 -07001191 cable->charger->regulator_name,
1192 cable->min_uA, cable->max_uA);
Chanwoo Choi45cd4fb2012-07-12 15:03:29 +09001193 }
Chanwoo Choibee737b2012-07-12 15:03:25 +09001194
1195 try_charger_enable(cable->cm, cable->attached);
1196}
1197
1198/**
1199 * charger_extcon_notifier - receive the state of charger cable
1200 * when registered cable is attached or detached.
1201 *
1202 * @self: the notifier block of the charger_extcon_notifier.
1203 * @event: the cable state.
1204 * @ptr: the data pointer of notifier block.
1205 */
1206static int charger_extcon_notifier(struct notifier_block *self,
1207 unsigned long event, void *ptr)
1208{
1209 struct charger_cable *cable =
1210 container_of(self, struct charger_cable, nb);
1211
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001212 /*
1213 * The newly state of charger cable.
1214 * If cable is attached, cable->attached is true.
1215 */
Chanwoo Choibee737b2012-07-12 15:03:25 +09001216 cable->attached = event;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001217
1218 /*
1219 * Setup monitoring to check battery state
1220 * when charger cable is attached.
1221 */
1222 if (cable->attached && is_polling_required(cable->cm)) {
Tejun Heo2fbb5202012-12-21 17:56:51 -08001223 cancel_work_sync(&setup_polling);
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001224 schedule_work(&setup_polling);
1225 }
1226
1227 /*
1228 * Setup work for controlling charger(regulator)
1229 * according to charger cable.
1230 */
Chanwoo Choibee737b2012-07-12 15:03:25 +09001231 schedule_work(&cable->wq);
1232
1233 return NOTIFY_DONE;
1234}
1235
1236/**
1237 * charger_extcon_init - register external connector to use it
1238 * as the charger cable
1239 *
1240 * @cm: the Charger Manager representing the battery.
1241 * @cable: the Charger cable representing the external connector.
1242 */
1243static int charger_extcon_init(struct charger_manager *cm,
1244 struct charger_cable *cable)
1245{
1246 int ret = 0;
1247
1248 /*
1249 * Charger manager use Extcon framework to identify
1250 * the charger cable among various external connector
1251 * cable (e.g., TA, USB, MHL, Dock).
1252 */
1253 INIT_WORK(&cable->wq, charger_extcon_work);
1254 cable->nb.notifier_call = charger_extcon_notifier;
1255 ret = extcon_register_interest(&cable->extcon_dev,
1256 cable->extcon_name, cable->name, &cable->nb);
1257 if (ret < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001258 pr_info("Cannot register extcon_dev for %s(cable: %s)\n",
1259 cable->extcon_name, cable->name);
Chanwoo Choibee737b2012-07-12 15:03:25 +09001260 ret = -EINVAL;
1261 }
1262
1263 return ret;
1264}
1265
Chanwoo Choi41468a12012-11-22 16:54:26 +09001266/**
1267 * charger_manager_register_extcon - Register extcon device to recevie state
1268 * of charger cable.
1269 * @cm: the Charger Manager representing the battery.
1270 *
1271 * This function support EXTCON(External Connector) subsystem to detect the
1272 * state of charger cables for enabling or disabling charger(regulator) and
1273 * select the charger cable for charging among a number of external cable
1274 * according to policy of H/W board.
1275 */
1276static int charger_manager_register_extcon(struct charger_manager *cm)
1277{
1278 struct charger_desc *desc = cm->desc;
1279 struct charger_regulator *charger;
1280 int ret = 0;
1281 int i;
1282 int j;
1283
1284 for (i = 0; i < desc->num_charger_regulators; i++) {
1285 charger = &desc->charger_regulators[i];
1286
1287 charger->consumer = regulator_get(cm->dev,
1288 charger->regulator_name);
Jonghwa Lee5a6c2202013-06-25 14:18:14 +09001289 if (IS_ERR(charger->consumer)) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001290 dev_err(cm->dev, "Cannot find charger(%s)\n",
1291 charger->regulator_name);
Jonghwa Lee5a6c2202013-06-25 14:18:14 +09001292 return PTR_ERR(charger->consumer);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001293 }
1294 charger->cm = cm;
1295
1296 for (j = 0; j < charger->num_cables; j++) {
1297 struct charger_cable *cable = &charger->cables[j];
1298
1299 ret = charger_extcon_init(cm, cable);
1300 if (ret < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001301 dev_err(cm->dev, "Cannot initialize charger(%s)\n",
1302 charger->regulator_name);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001303 goto err;
1304 }
1305 cable->charger = charger;
1306 cable->cm = cm;
1307 }
1308 }
1309
1310err:
1311 return ret;
1312}
1313
Chanwoo Choi3950c782012-09-21 18:49:37 +09001314/* help function of sysfs node to control charger(regulator) */
1315static ssize_t charger_name_show(struct device *dev,
1316 struct device_attribute *attr, char *buf)
1317{
1318 struct charger_regulator *charger
1319 = container_of(attr, struct charger_regulator, attr_name);
1320
1321 return sprintf(buf, "%s\n", charger->regulator_name);
1322}
1323
1324static ssize_t charger_state_show(struct device *dev,
1325 struct device_attribute *attr, char *buf)
1326{
1327 struct charger_regulator *charger
1328 = container_of(attr, struct charger_regulator, attr_state);
1329 int state = 0;
1330
1331 if (!charger->externally_control)
1332 state = regulator_is_enabled(charger->consumer);
1333
1334 return sprintf(buf, "%s\n", state ? "enabled" : "disabled");
1335}
1336
1337static ssize_t charger_externally_control_show(struct device *dev,
1338 struct device_attribute *attr, char *buf)
1339{
1340 struct charger_regulator *charger = container_of(attr,
1341 struct charger_regulator, attr_externally_control);
1342
1343 return sprintf(buf, "%d\n", charger->externally_control);
1344}
1345
1346static ssize_t charger_externally_control_store(struct device *dev,
1347 struct device_attribute *attr, const char *buf,
1348 size_t count)
1349{
1350 struct charger_regulator *charger
1351 = container_of(attr, struct charger_regulator,
1352 attr_externally_control);
1353 struct charger_manager *cm = charger->cm;
1354 struct charger_desc *desc = cm->desc;
1355 int i;
1356 int ret;
1357 int externally_control;
1358 int chargers_externally_control = 1;
1359
1360 ret = sscanf(buf, "%d", &externally_control);
1361 if (ret == 0) {
1362 ret = -EINVAL;
1363 return ret;
1364 }
1365
1366 if (!externally_control) {
1367 charger->externally_control = 0;
1368 return count;
1369 }
1370
1371 for (i = 0; i < desc->num_charger_regulators; i++) {
1372 if (&desc->charger_regulators[i] != charger &&
Chanwoo Choi41468a12012-11-22 16:54:26 +09001373 !desc->charger_regulators[i].externally_control) {
Chanwoo Choi3950c782012-09-21 18:49:37 +09001374 /*
1375 * At least, one charger is controlled by
1376 * charger-manager
1377 */
1378 chargers_externally_control = 0;
1379 break;
1380 }
1381 }
1382
1383 if (!chargers_externally_control) {
1384 if (cm->charger_enabled) {
1385 try_charger_enable(charger->cm, false);
1386 charger->externally_control = externally_control;
1387 try_charger_enable(charger->cm, true);
1388 } else {
1389 charger->externally_control = externally_control;
1390 }
1391 } else {
1392 dev_warn(cm->dev,
Joe Perchese5409cb2013-06-06 18:25:12 -07001393 "'%s' regulator should be controlled in charger-manager because charger-manager must need at least one charger for charging\n",
1394 charger->regulator_name);
Chanwoo Choi3950c782012-09-21 18:49:37 +09001395 }
1396
1397 return count;
1398}
1399
Chanwoo Choi41468a12012-11-22 16:54:26 +09001400/**
1401 * charger_manager_register_sysfs - Register sysfs entry for each charger
1402 * @cm: the Charger Manager representing the battery.
1403 *
1404 * This function add sysfs entry for charger(regulator) to control charger from
1405 * user-space. If some development board use one more chargers for charging
1406 * but only need one charger on specific case which is dependent on user
1407 * scenario or hardware restrictions, the user enter 1 or 0(zero) to '/sys/
1408 * class/power_supply/battery/charger.[index]/externally_control'. For example,
1409 * if user enter 1 to 'sys/class/power_supply/battery/charger.[index]/
1410 * externally_control, this charger isn't controlled from charger-manager and
1411 * always stay off state of regulator.
1412 */
1413static int charger_manager_register_sysfs(struct charger_manager *cm)
1414{
1415 struct charger_desc *desc = cm->desc;
1416 struct charger_regulator *charger;
1417 int chargers_externally_control = 1;
1418 char buf[11];
1419 char *str;
1420 int ret = 0;
1421 int i;
1422
1423 /* Create sysfs entry to control charger(regulator) */
1424 for (i = 0; i < desc->num_charger_regulators; i++) {
1425 charger = &desc->charger_regulators[i];
1426
1427 snprintf(buf, 10, "charger.%d", i);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001428 str = devm_kzalloc(cm->dev,
1429 sizeof(char) * (strlen(buf) + 1), GFP_KERNEL);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001430 if (!str) {
Chanwoo Choi41468a12012-11-22 16:54:26 +09001431 ret = -ENOMEM;
1432 goto err;
1433 }
1434 strcpy(str, buf);
1435
1436 charger->attrs[0] = &charger->attr_name.attr;
1437 charger->attrs[1] = &charger->attr_state.attr;
1438 charger->attrs[2] = &charger->attr_externally_control.attr;
1439 charger->attrs[3] = NULL;
1440 charger->attr_g.name = str;
1441 charger->attr_g.attrs = charger->attrs;
1442
1443 sysfs_attr_init(&charger->attr_name.attr);
1444 charger->attr_name.attr.name = "name";
1445 charger->attr_name.attr.mode = 0444;
1446 charger->attr_name.show = charger_name_show;
1447
1448 sysfs_attr_init(&charger->attr_state.attr);
1449 charger->attr_state.attr.name = "state";
1450 charger->attr_state.attr.mode = 0444;
1451 charger->attr_state.show = charger_state_show;
1452
1453 sysfs_attr_init(&charger->attr_externally_control.attr);
1454 charger->attr_externally_control.attr.name
1455 = "externally_control";
1456 charger->attr_externally_control.attr.mode = 0644;
1457 charger->attr_externally_control.show
1458 = charger_externally_control_show;
1459 charger->attr_externally_control.store
1460 = charger_externally_control_store;
1461
1462 if (!desc->charger_regulators[i].externally_control ||
1463 !chargers_externally_control)
1464 chargers_externally_control = 0;
1465
Joe Perchese5409cb2013-06-06 18:25:12 -07001466 dev_info(cm->dev, "'%s' regulator's externally_control is %d\n",
1467 charger->regulator_name, charger->externally_control);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001468
1469 ret = sysfs_create_group(&cm->charger_psy.dev->kobj,
1470 &charger->attr_g);
1471 if (ret < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001472 dev_err(cm->dev, "Cannot create sysfs entry of %s regulator\n",
1473 charger->regulator_name);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001474 ret = -EINVAL;
1475 goto err;
1476 }
1477 }
1478
1479 if (chargers_externally_control) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001480 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 +09001481 ret = -EINVAL;
1482 goto err;
1483 }
1484
1485err:
1486 return ret;
1487}
1488
Jonghwa Lee5c49a622013-12-18 15:42:34 +09001489static int cm_init_thermal_data(struct charger_manager *cm)
1490{
1491 struct charger_desc *desc = cm->desc;
1492 union power_supply_propval val;
1493 int ret;
1494
1495 /* Verify whether fuel gauge provides battery temperature */
1496 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
1497 POWER_SUPPLY_PROP_TEMP, &val);
1498
1499 if (!ret) {
1500 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1501 POWER_SUPPLY_PROP_TEMP;
1502 cm->charger_psy.num_properties++;
1503 cm->desc->measure_battery_temp = true;
1504 }
1505#ifdef CONFIG_THERMAL
1506 cm->tzd_batt = cm->fuel_gauge->tzd;
1507
1508 if (ret && desc->thermal_zone) {
1509 cm->tzd_batt =
1510 thermal_zone_get_zone_by_name(desc->thermal_zone);
1511 if (IS_ERR(cm->tzd_batt))
1512 return PTR_ERR(cm->tzd_batt);
1513
1514 /* Use external thermometer */
1515 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1516 POWER_SUPPLY_PROP_TEMP_AMBIENT;
1517 cm->charger_psy.num_properties++;
1518 cm->desc->measure_battery_temp = true;
1519 ret = 0;
1520 }
1521#endif
1522 if (cm->desc->measure_battery_temp) {
1523 /* NOTICE : Default allowable minimum charge temperature is 0 */
1524 if (!desc->temp_max)
1525 desc->temp_max = CM_DEFAULT_CHARGE_TEMP_MAX;
1526 if (!desc->temp_diff)
1527 desc->temp_diff = CM_DEFAULT_RECHARGE_TEMP_DIFF;
1528 }
1529
1530 return ret;
1531}
1532
Jonghwa Lee856ee612013-12-18 15:42:35 +09001533static struct of_device_id charger_manager_match[] = {
1534 {
1535 .compatible = "charger-manager",
1536 },
1537 {},
1538};
1539
Anton Vorontsov434a09f2013-12-23 18:17:05 -08001540static struct charger_desc *of_cm_parse_desc(struct device *dev)
Jonghwa Lee856ee612013-12-18 15:42:35 +09001541{
1542 struct charger_desc *desc;
1543 struct device_node *np = dev->of_node;
1544 u32 poll_mode = CM_POLL_DISABLE;
1545 u32 battery_stat = CM_NO_BATTERY;
1546 int num_chgs = 0;
1547
1548 desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
1549 if (!desc)
1550 return ERR_PTR(-ENOMEM);
1551
1552 of_property_read_string(np, "cm-name", &desc->psy_name);
1553
1554 of_property_read_u32(np, "cm-poll-mode", &poll_mode);
1555 desc->polling_mode = poll_mode;
1556
1557 of_property_read_u32(np, "cm-poll-interval",
1558 &desc->polling_interval_ms);
1559
1560 of_property_read_u32(np, "cm-fullbatt-vchkdrop-ms",
1561 &desc->fullbatt_vchkdrop_ms);
1562 of_property_read_u32(np, "cm-fullbatt-vchkdrop-volt",
1563 &desc->fullbatt_vchkdrop_uV);
1564 of_property_read_u32(np, "cm-fullbatt-voltage", &desc->fullbatt_uV);
1565 of_property_read_u32(np, "cm-fullbatt-soc", &desc->fullbatt_soc);
1566 of_property_read_u32(np, "cm-fullbatt-capacity",
1567 &desc->fullbatt_full_capacity);
1568
1569 of_property_read_u32(np, "cm-battery-stat", &battery_stat);
1570 desc->battery_present = battery_stat;
1571
1572 /* chargers */
1573 of_property_read_u32(np, "cm-num-chargers", &num_chgs);
1574 if (num_chgs) {
1575 /* Allocate empty bin at the tail of array */
1576 desc->psy_charger_stat = devm_kzalloc(dev, sizeof(char *)
1577 * (num_chgs + 1), GFP_KERNEL);
1578 if (desc->psy_charger_stat) {
1579 int i;
1580 for (i = 0; i < num_chgs; i++)
1581 of_property_read_string_index(np, "cm-chargers",
1582 i, &desc->psy_charger_stat[i]);
1583 } else {
1584 return ERR_PTR(-ENOMEM);
1585 }
1586 }
1587
1588 of_property_read_string(np, "cm-fuel-gauge", &desc->psy_fuel_gauge);
1589
1590 of_property_read_string(np, "cm-thermal-zone", &desc->thermal_zone);
1591
1592 of_property_read_u32(np, "cm-battery-cold", &desc->temp_min);
1593 if (of_get_property(np, "cm-battery-cold-in-minus", NULL))
1594 desc->temp_min *= -1;
1595 of_property_read_u32(np, "cm-battery-hot", &desc->temp_max);
1596 of_property_read_u32(np, "cm-battery-temp-diff", &desc->temp_diff);
1597
1598 of_property_read_u32(np, "cm-charging-max",
1599 &desc->charging_max_duration_ms);
1600 of_property_read_u32(np, "cm-discharging-max",
1601 &desc->discharging_max_duration_ms);
1602
1603 /* battery charger regualtors */
1604 desc->num_charger_regulators = of_get_child_count(np);
1605 if (desc->num_charger_regulators) {
1606 struct charger_regulator *chg_regs;
1607 struct device_node *child;
1608
1609 chg_regs = devm_kzalloc(dev, sizeof(*chg_regs)
1610 * desc->num_charger_regulators,
1611 GFP_KERNEL);
1612 if (!chg_regs)
1613 return ERR_PTR(-ENOMEM);
1614
1615 desc->charger_regulators = chg_regs;
1616
1617 for_each_child_of_node(np, child) {
1618 struct charger_cable *cables;
1619 struct device_node *_child;
1620
1621 of_property_read_string(child, "cm-regulator-name",
1622 &chg_regs->regulator_name);
1623
1624 /* charger cables */
1625 chg_regs->num_cables = of_get_child_count(child);
1626 if (chg_regs->num_cables) {
1627 cables = devm_kzalloc(dev, sizeof(*cables)
1628 * chg_regs->num_cables,
1629 GFP_KERNEL);
1630 if (!cables)
1631 return ERR_PTR(-ENOMEM);
1632
1633 chg_regs->cables = cables;
1634
1635 for_each_child_of_node(child, _child) {
1636 of_property_read_string(_child,
1637 "cm-cable-name", &cables->name);
1638 of_property_read_string(_child,
1639 "cm-cable-extcon",
1640 &cables->extcon_name);
1641 of_property_read_u32(_child,
1642 "cm-cable-min",
1643 &cables->min_uA);
1644 of_property_read_u32(_child,
1645 "cm-cable-max",
1646 &cables->max_uA);
1647 cables++;
1648 }
1649 }
1650 chg_regs++;
1651 }
1652 }
1653 return desc;
1654}
1655
1656static inline struct charger_desc *cm_get_drv_data(struct platform_device *pdev)
1657{
1658 if (pdev->dev.of_node)
1659 return of_cm_parse_desc(&pdev->dev);
Jingoo Han86515b72014-08-29 12:45:27 +09001660 return dev_get_platdata(&pdev->dev);
Jonghwa Lee856ee612013-12-18 15:42:35 +09001661}
1662
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001663static int charger_manager_probe(struct platform_device *pdev)
1664{
Jonghwa Lee856ee612013-12-18 15:42:35 +09001665 struct charger_desc *desc = cm_get_drv_data(pdev);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001666 struct charger_manager *cm;
1667 int ret = 0, i = 0;
Chanwoo Choibee737b2012-07-12 15:03:25 +09001668 int j = 0;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001669 union power_supply_propval val;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001670
1671 if (g_desc && !rtc_dev && g_desc->rtc_name) {
1672 rtc_dev = rtc_class_open(g_desc->rtc_name);
1673 if (IS_ERR_OR_NULL(rtc_dev)) {
1674 rtc_dev = NULL;
Joe Perchese5409cb2013-06-06 18:25:12 -07001675 dev_err(&pdev->dev, "Cannot get RTC %s\n",
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001676 g_desc->rtc_name);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001677 return -ENODEV;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001678 }
1679 }
1680
Chanwoo Choic6738d02014-08-26 13:41:38 +09001681 if (IS_ERR(desc)) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001682 dev_err(&pdev->dev, "No platform data (desc) found\n");
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001683 return -ENODEV;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001684 }
1685
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001686 cm = devm_kzalloc(&pdev->dev,
1687 sizeof(struct charger_manager), GFP_KERNEL);
1688 if (!cm)
1689 return -ENOMEM;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001690
1691 /* Basic Values. Unspecified are Null or 0 */
1692 cm->dev = &pdev->dev;
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001693 cm->desc = desc;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001694
Chanwoo Choid829dc72012-05-05 06:24:10 -07001695 /*
1696 * The following two do not need to be errors.
1697 * Users may intentionally ignore those two features.
1698 */
1699 if (desc->fullbatt_uV == 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001700 dev_info(&pdev->dev, "Ignoring full-battery voltage threshold as it is not supplied\n");
Chanwoo Choid829dc72012-05-05 06:24:10 -07001701 }
1702 if (!desc->fullbatt_vchkdrop_ms || !desc->fullbatt_vchkdrop_uV) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001703 dev_info(&pdev->dev, "Disabling full-battery voltage drop checking mechanism as it is not supplied\n");
Chanwoo Choid829dc72012-05-05 06:24:10 -07001704 desc->fullbatt_vchkdrop_ms = 0;
1705 desc->fullbatt_vchkdrop_uV = 0;
1706 }
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001707 if (desc->fullbatt_soc == 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001708 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 +09001709 }
1710 if (desc->fullbatt_full_capacity == 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001711 dev_info(&pdev->dev, "Ignoring full-battery full capacity threshold as it is not supplied\n");
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001712 }
Chanwoo Choid829dc72012-05-05 06:24:10 -07001713
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001714 if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001715 dev_err(&pdev->dev, "charger_regulators undefined\n");
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001716 return -EINVAL;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001717 }
1718
1719 if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001720 dev_err(&pdev->dev, "No power supply defined\n");
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001721 return -EINVAL;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001722 }
1723
Krzysztof Kozlowski661a8882014-09-26 13:27:03 +02001724 if (!desc->psy_fuel_gauge) {
1725 dev_err(&pdev->dev, "No fuel gauge power supply defined\n");
1726 return -EINVAL;
1727 }
1728
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001729 /* Counting index only */
1730 while (desc->psy_charger_stat[i])
1731 i++;
1732
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001733 cm->charger_stat = devm_kzalloc(&pdev->dev,
1734 sizeof(struct power_supply *) * i, GFP_KERNEL);
1735 if (!cm->charger_stat)
1736 return -ENOMEM;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001737
1738 for (i = 0; desc->psy_charger_stat[i]; i++) {
1739 cm->charger_stat[i] = power_supply_get_by_name(
1740 desc->psy_charger_stat[i]);
1741 if (!cm->charger_stat[i]) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001742 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
1743 desc->psy_charger_stat[i]);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001744 return -ENODEV;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001745 }
1746 }
1747
1748 cm->fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
1749 if (!cm->fuel_gauge) {
1750 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
Joe Perchese5409cb2013-06-06 18:25:12 -07001751 desc->psy_fuel_gauge);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001752 return -ENODEV;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001753 }
1754
1755 if (desc->polling_interval_ms == 0 ||
1756 msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) {
1757 dev_err(&pdev->dev, "polling_interval_ms is too small\n");
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001758 return -EINVAL;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001759 }
1760
Chanwoo Choi8fcfe082012-09-20 21:20:05 -07001761 if (!desc->charging_max_duration_ms ||
1762 !desc->discharging_max_duration_ms) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001763 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 -07001764 desc->charging_max_duration_ms = 0;
1765 desc->discharging_max_duration_ms = 0;
1766 }
1767
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001768 platform_set_drvdata(pdev, cm);
1769
Axel Linbb2a95c2012-01-12 12:56:35 +08001770 memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default));
1771
Chanwoo Choi41468a12012-11-22 16:54:26 +09001772 if (!desc->psy_name)
Axel Linbb2a95c2012-01-12 12:56:35 +08001773 strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001774 else
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001775 strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001776 cm->charger_psy.name = cm->psy_name_buf;
1777
1778 /* Allocate for psy properties because they may vary */
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001779 cm->charger_psy.properties = devm_kzalloc(&pdev->dev,
1780 sizeof(enum power_supply_property)
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001781 * (ARRAY_SIZE(default_charger_props) +
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001782 NUM_CHARGER_PSY_OPTIONAL), GFP_KERNEL);
1783 if (!cm->charger_psy.properties)
1784 return -ENOMEM;
1785
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001786 memcpy(cm->charger_psy.properties, default_charger_props,
1787 sizeof(enum power_supply_property) *
1788 ARRAY_SIZE(default_charger_props));
1789 cm->charger_psy.num_properties = psy_default.num_properties;
1790
1791 /* Find which optional psy-properties are available */
1792 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1793 POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
1794 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1795 POWER_SUPPLY_PROP_CHARGE_NOW;
1796 cm->charger_psy.num_properties++;
1797 }
1798 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1799 POWER_SUPPLY_PROP_CURRENT_NOW,
1800 &val)) {
1801 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1802 POWER_SUPPLY_PROP_CURRENT_NOW;
1803 cm->charger_psy.num_properties++;
1804 }
Axel Linbb2a95c2012-01-12 12:56:35 +08001805
Jonghwa Lee5c49a622013-12-18 15:42:34 +09001806 ret = cm_init_thermal_data(cm);
1807 if (ret) {
1808 dev_err(&pdev->dev, "Failed to initialize thermal data\n");
1809 cm->desc->measure_battery_temp = false;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001810 }
1811
Chanwoo Choid829dc72012-05-05 06:24:10 -07001812 INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk);
1813
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001814 ret = power_supply_register(NULL, &cm->charger_psy);
1815 if (ret) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001816 dev_err(&pdev->dev, "Cannot register charger-manager with name \"%s\"\n",
1817 cm->charger_psy.name);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001818 return ret;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001819 }
1820
Chanwoo Choi41468a12012-11-22 16:54:26 +09001821 /* Register extcon device for charger cable */
1822 ret = charger_manager_register_extcon(cm);
1823 if (ret < 0) {
1824 dev_err(&pdev->dev, "Cannot initialize extcon device\n");
1825 goto err_reg_extcon;
Chanwoo Choi3950c782012-09-21 18:49:37 +09001826 }
1827
Chanwoo Choi41468a12012-11-22 16:54:26 +09001828 /* Register sysfs entry for charger(regulator) */
1829 ret = charger_manager_register_sysfs(cm);
1830 if (ret < 0) {
1831 dev_err(&pdev->dev,
1832 "Cannot initialize sysfs entry of regulator\n");
1833 goto err_reg_sysfs;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001834 }
1835
1836 /* Add to the list */
1837 mutex_lock(&cm_list_mtx);
1838 list_add(&cm->entry, &cm_list);
1839 mutex_unlock(&cm_list_mtx);
1840
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001841 /*
1842 * Charger-manager is capable of waking up the systme from sleep
1843 * when event is happend through cm_notify_event()
1844 */
1845 device_init_wakeup(&pdev->dev, true);
1846 device_set_wakeup_capable(&pdev->dev, false);
1847
Chanwoo Choib1022e22014-08-26 13:41:39 +09001848 /*
1849 * Charger-manager have to check the charging state right after
1850 * tialization of charger-manager and then update current charging
1851 * state.
1852 */
1853 cm_monitor();
1854
Chanwoo Choid829dc72012-05-05 06:24:10 -07001855 schedule_work(&setup_polling);
1856
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001857 return 0;
1858
Chanwoo Choi41468a12012-11-22 16:54:26 +09001859err_reg_sysfs:
Chanwoo Choi3950c782012-09-21 18:49:37 +09001860 for (i = 0; i < desc->num_charger_regulators; i++) {
1861 struct charger_regulator *charger;
1862
1863 charger = &desc->charger_regulators[i];
1864 sysfs_remove_group(&cm->charger_psy.dev->kobj,
1865 &charger->attr_g);
Chanwoo Choi3950c782012-09-21 18:49:37 +09001866 }
Chanwoo Choi41468a12012-11-22 16:54:26 +09001867err_reg_extcon:
1868 for (i = 0; i < desc->num_charger_regulators; i++) {
1869 struct charger_regulator *charger;
1870
1871 charger = &desc->charger_regulators[i];
1872 for (j = 0; j < charger->num_cables; j++) {
Chanwoo Choibee737b2012-07-12 15:03:25 +09001873 struct charger_cable *cable = &charger->cables[j];
Jonghwa Lee3cc9d2692013-06-25 14:02:49 +09001874 /* Remove notifier block if only edev exists */
1875 if (cable->extcon_dev.edev)
1876 extcon_unregister_interest(&cable->extcon_dev);
Chanwoo Choibee737b2012-07-12 15:03:25 +09001877 }
Chanwoo Choi41468a12012-11-22 16:54:26 +09001878
Chanwoo Choibee737b2012-07-12 15:03:25 +09001879 regulator_put(desc->charger_regulators[i].consumer);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001880 }
Chanwoo Choibee737b2012-07-12 15:03:25 +09001881
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001882 power_supply_unregister(&cm->charger_psy);
Jonghwa Lee883c10a2013-10-25 11:47:31 +09001883
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001884 return ret;
1885}
1886
Bill Pemberton415ec692012-11-19 13:26:07 -05001887static int charger_manager_remove(struct platform_device *pdev)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001888{
1889 struct charger_manager *cm = platform_get_drvdata(pdev);
1890 struct charger_desc *desc = cm->desc;
Chanwoo Choibee737b2012-07-12 15:03:25 +09001891 int i = 0;
1892 int j = 0;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001893
1894 /* Remove from the list */
1895 mutex_lock(&cm_list_mtx);
1896 list_del(&cm->entry);
1897 mutex_unlock(&cm_list_mtx);
1898
Tejun Heo2fbb5202012-12-21 17:56:51 -08001899 cancel_work_sync(&setup_polling);
1900 cancel_delayed_work_sync(&cm_monitor_work);
Chanwoo Choid829dc72012-05-05 06:24:10 -07001901
Chanwoo Choibee737b2012-07-12 15:03:25 +09001902 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1903 struct charger_regulator *charger
1904 = &desc->charger_regulators[i];
1905 for (j = 0 ; j < charger->num_cables ; j++) {
1906 struct charger_cable *cable = &charger->cables[j];
1907 extcon_unregister_interest(&cable->extcon_dev);
1908 }
1909 }
1910
1911 for (i = 0 ; i < desc->num_charger_regulators ; i++)
1912 regulator_put(desc->charger_regulators[i].consumer);
1913
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001914 power_supply_unregister(&cm->charger_psy);
Chanwoo Choid829dc72012-05-05 06:24:10 -07001915
1916 try_charger_enable(cm, false);
1917
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001918 return 0;
1919}
1920
Axel Lin1bbe24d2012-01-11 17:19:45 +08001921static const struct platform_device_id charger_manager_id[] = {
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001922 { "charger-manager", 0 },
1923 { },
1924};
Axel Lin1bbe24d2012-01-11 17:19:45 +08001925MODULE_DEVICE_TABLE(platform, charger_manager_id);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001926
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001927static int cm_suspend_noirq(struct device *dev)
1928{
1929 int ret = 0;
1930
1931 if (device_may_wakeup(dev)) {
1932 device_set_wakeup_capable(dev, false);
1933 ret = -EAGAIN;
1934 }
1935
1936 return ret;
1937}
1938
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001939static int cm_suspend_prepare(struct device *dev)
1940{
Axel Linbb2a95c2012-01-12 12:56:35 +08001941 struct charger_manager *cm = dev_get_drvdata(dev);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001942
1943 if (!cm_suspended) {
1944 if (rtc_dev) {
1945 struct rtc_time tmp;
1946 unsigned long now;
1947
1948 rtc_read_alarm(rtc_dev, &rtc_wkalarm_save);
1949 rtc_read_time(rtc_dev, &tmp);
1950
1951 if (rtc_wkalarm_save.enabled) {
1952 rtc_tm_to_time(&rtc_wkalarm_save.time,
1953 &rtc_wkalarm_save_time);
1954 rtc_tm_to_time(&tmp, &now);
1955 if (now > rtc_wkalarm_save_time)
1956 rtc_wkalarm_save_time = 0;
1957 } else {
1958 rtc_wkalarm_save_time = 0;
1959 }
1960 }
1961 cm_suspended = true;
1962 }
1963
Tejun Heo2fbb5202012-12-21 17:56:51 -08001964 cancel_delayed_work(&cm->fullbatt_vchk_work);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001965 cm->status_save_ext_pwr_inserted = is_ext_pwr_online(cm);
1966 cm->status_save_batt = is_batt_present(cm);
1967
1968 if (!cm_rtc_set) {
1969 cm_suspend_duration_ms = 0;
1970 cm_rtc_set = cm_setup_timer();
1971 }
1972
1973 return 0;
1974}
1975
1976static void cm_suspend_complete(struct device *dev)
1977{
Axel Linbb2a95c2012-01-12 12:56:35 +08001978 struct charger_manager *cm = dev_get_drvdata(dev);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001979
1980 if (cm_suspended) {
1981 if (rtc_dev) {
1982 struct rtc_wkalrm tmp;
1983
1984 rtc_read_alarm(rtc_dev, &tmp);
1985 rtc_wkalarm_save.pending = tmp.pending;
1986 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1987 }
1988 cm_suspended = false;
1989 cm_rtc_set = false;
1990 }
1991
Chanwoo Choid829dc72012-05-05 06:24:10 -07001992 /* Re-enqueue delayed work (fullbatt_vchk_work) */
1993 if (cm->fullbatt_vchk_jiffies_at) {
1994 unsigned long delay = 0;
1995 unsigned long now = jiffies + CM_JIFFIES_SMALL;
1996
1997 if (time_after_eq(now, cm->fullbatt_vchk_jiffies_at)) {
1998 delay = (unsigned long)((long)now
1999 - (long)(cm->fullbatt_vchk_jiffies_at));
2000 delay = jiffies_to_msecs(delay);
2001 } else {
2002 delay = 0;
2003 }
2004
2005 /*
2006 * Account for cm_suspend_duration_ms if
2007 * assume_timer_stops_in_suspend is active
2008 */
2009 if (g_desc && g_desc->assume_timer_stops_in_suspend) {
2010 if (delay > cm_suspend_duration_ms)
2011 delay -= cm_suspend_duration_ms;
2012 else
2013 delay = 0;
2014 }
2015
2016 queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
2017 msecs_to_jiffies(delay));
2018 }
Chanwoo Choidfeccb12012-05-05 06:26:47 -07002019 device_set_wakeup_capable(cm->dev, false);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09002020 uevent_notify(cm, NULL);
2021}
2022
2023static const struct dev_pm_ops charger_manager_pm = {
2024 .prepare = cm_suspend_prepare,
Chanwoo Choidfeccb12012-05-05 06:26:47 -07002025 .suspend_noirq = cm_suspend_noirq,
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09002026 .complete = cm_suspend_complete,
2027};
2028
2029static struct platform_driver charger_manager_driver = {
2030 .driver = {
2031 .name = "charger-manager",
2032 .owner = THIS_MODULE,
2033 .pm = &charger_manager_pm,
Jonghwa Lee856ee612013-12-18 15:42:35 +09002034 .of_match_table = charger_manager_match,
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09002035 },
2036 .probe = charger_manager_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -05002037 .remove = charger_manager_remove,
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09002038 .id_table = charger_manager_id,
2039};
2040
2041static int __init charger_manager_init(void)
2042{
Chanwoo Choid829dc72012-05-05 06:24:10 -07002043 cm_wq = create_freezable_workqueue("charger_manager");
2044 INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller);
2045
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09002046 return platform_driver_register(&charger_manager_driver);
2047}
2048late_initcall(charger_manager_init);
2049
2050static void __exit charger_manager_cleanup(void)
2051{
Chanwoo Choid829dc72012-05-05 06:24:10 -07002052 destroy_workqueue(cm_wq);
2053 cm_wq = NULL;
2054
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09002055 platform_driver_unregister(&charger_manager_driver);
2056}
2057module_exit(charger_manager_cleanup);
2058
Chanwoo Choidfeccb12012-05-05 06:26:47 -07002059/**
2060 * find_power_supply - find the associated power_supply of charger
2061 * @cm: the Charger Manager representing the battery
2062 * @psy: pointer to instance of charger's power_supply
2063 */
2064static bool find_power_supply(struct charger_manager *cm,
2065 struct power_supply *psy)
2066{
2067 int i;
2068 bool found = false;
2069
2070 for (i = 0; cm->charger_stat[i]; i++) {
2071 if (psy == cm->charger_stat[i]) {
2072 found = true;
2073 break;
2074 }
2075 }
2076
2077 return found;
2078}
2079
2080/**
2081 * cm_notify_event - charger driver notify Charger Manager of charger event
2082 * @psy: pointer to instance of charger's power_supply
2083 * @type: type of charger event
2084 * @msg: optional message passed to uevent_notify fuction
2085 */
2086void cm_notify_event(struct power_supply *psy, enum cm_event_types type,
2087 char *msg)
2088{
2089 struct charger_manager *cm;
2090 bool found_power_supply = false;
2091
2092 if (psy == NULL)
2093 return;
2094
2095 mutex_lock(&cm_list_mtx);
2096 list_for_each_entry(cm, &cm_list, entry) {
2097 found_power_supply = find_power_supply(cm, psy);
2098 if (found_power_supply)
2099 break;
2100 }
2101 mutex_unlock(&cm_list_mtx);
2102
2103 if (!found_power_supply)
2104 return;
2105
2106 switch (type) {
2107 case CM_EVENT_BATT_FULL:
2108 fullbatt_handler(cm);
2109 break;
2110 case CM_EVENT_BATT_OUT:
2111 battout_handler(cm);
2112 break;
2113 case CM_EVENT_BATT_IN:
2114 case CM_EVENT_EXT_PWR_IN_OUT ... CM_EVENT_CHG_START_STOP:
2115 misc_event_handler(cm, type);
2116 break;
2117 case CM_EVENT_UNKNOWN:
2118 case CM_EVENT_OTHERS:
2119 uevent_notify(cm, msg ? msg : default_event_names[type]);
2120 break;
2121 default:
Joe Perchese5409cb2013-06-06 18:25:12 -07002122 dev_err(cm->dev, "%s: type not specified\n", __func__);
Chanwoo Choidfeccb12012-05-05 06:26:47 -07002123 break;
2124 }
2125}
2126EXPORT_SYMBOL_GPL(cm_notify_event);
2127
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09002128MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
2129MODULE_DESCRIPTION("Charger Manager");
2130MODULE_LICENSE("GPL");