blob: e30e847600bb69c71519b7e1325286830e504c53 [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>
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090028
Chanwoo Choidfeccb12012-05-05 06:26:47 -070029static const char * const default_event_names[] = {
30 [CM_EVENT_UNKNOWN] = "Unknown",
31 [CM_EVENT_BATT_FULL] = "Battery Full",
32 [CM_EVENT_BATT_IN] = "Battery Inserted",
33 [CM_EVENT_BATT_OUT] = "Battery Pulled Out",
34 [CM_EVENT_EXT_PWR_IN_OUT] = "External Power Attach/Detach",
35 [CM_EVENT_CHG_START_STOP] = "Charging Start/Stop",
36 [CM_EVENT_OTHERS] = "Other battery events"
37};
38
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090039/*
40 * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for
41 * delayed works so that we can run delayed works with CM_JIFFIES_SMALL
42 * without any delays.
43 */
44#define CM_JIFFIES_SMALL (2)
45
46/* If y is valid (> 0) and smaller than x, do x = y */
47#define CM_MIN_VALID(x, y) x = (((y > 0) && ((x) > (y))) ? (y) : (x))
48
49/*
50 * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking
51 * rtc alarm. It should be 2 or larger
52 */
53#define CM_RTC_SMALL (2)
54
55#define UEVENT_BUF_SIZE 32
56
57static LIST_HEAD(cm_list);
58static DEFINE_MUTEX(cm_list_mtx);
59
60/* About in-suspend (suspend-again) monitoring */
61static struct rtc_device *rtc_dev;
62/*
63 * Backup RTC alarm
64 * Save the wakeup alarm before entering suspend-to-RAM
65 */
66static struct rtc_wkalrm rtc_wkalarm_save;
67/* Backup RTC alarm time in terms of seconds since 01-01-1970 00:00:00 */
68static unsigned long rtc_wkalarm_save_time;
69static bool cm_suspended;
70static bool cm_rtc_set;
71static unsigned long cm_suspend_duration_ms;
72
Chanwoo Choid829dc72012-05-05 06:24:10 -070073/* About normal (not suspended) monitoring */
74static unsigned long polling_jiffy = ULONG_MAX; /* ULONG_MAX: no polling */
75static unsigned long next_polling; /* Next appointed polling time */
76static struct workqueue_struct *cm_wq; /* init at driver add */
77static struct delayed_work cm_monitor_work; /* init at driver add */
78
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090079/* Global charger-manager description */
80static struct charger_global_desc *g_desc; /* init with setup_charger_manager */
81
82/**
83 * is_batt_present - See if the battery presents in place.
84 * @cm: the Charger Manager representing the battery.
85 */
86static bool is_batt_present(struct charger_manager *cm)
87{
88 union power_supply_propval val;
89 bool present = false;
90 int i, ret;
91
92 switch (cm->desc->battery_present) {
Chanwoo Choid829dc72012-05-05 06:24:10 -070093 case CM_BATTERY_PRESENT:
94 present = true;
95 break;
96 case CM_NO_BATTERY:
97 break;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090098 case CM_FUEL_GAUGE:
99 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
100 POWER_SUPPLY_PROP_PRESENT, &val);
101 if (ret == 0 && val.intval)
102 present = true;
103 break;
104 case CM_CHARGER_STAT:
105 for (i = 0; cm->charger_stat[i]; i++) {
106 ret = cm->charger_stat[i]->get_property(
107 cm->charger_stat[i],
108 POWER_SUPPLY_PROP_PRESENT, &val);
109 if (ret == 0 && val.intval) {
110 present = true;
111 break;
112 }
113 }
114 break;
115 }
116
117 return present;
118}
119
120/**
121 * is_ext_pwr_online - See if an external power source is attached to charge
122 * @cm: the Charger Manager representing the battery.
123 *
124 * Returns true if at least one of the chargers of the battery has an external
125 * power source attached to charge the battery regardless of whether it is
126 * actually charging or not.
127 */
128static bool is_ext_pwr_online(struct charger_manager *cm)
129{
130 union power_supply_propval val;
131 bool online = false;
132 int i, ret;
133
134 /* If at least one of them has one, it's yes. */
135 for (i = 0; cm->charger_stat[i]; i++) {
136 ret = cm->charger_stat[i]->get_property(
137 cm->charger_stat[i],
138 POWER_SUPPLY_PROP_ONLINE, &val);
139 if (ret == 0 && val.intval) {
140 online = true;
141 break;
142 }
143 }
144
145 return online;
146}
147
148/**
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900149 * get_batt_uV - Get the voltage level of the battery
150 * @cm: the Charger Manager representing the battery.
151 * @uV: the voltage level returned.
152 *
153 * Returns 0 if there is no error.
154 * Returns a negative value on error.
155 */
156static int get_batt_uV(struct charger_manager *cm, int *uV)
157{
158 union power_supply_propval val;
159 int ret;
160
Axel Linbb2a95c2012-01-12 12:56:35 +0800161 if (!cm->fuel_gauge)
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900162 return -ENODEV;
163
Axel Linbb2a95c2012-01-12 12:56:35 +0800164 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
165 POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900166 if (ret)
167 return ret;
168
169 *uV = val.intval;
170 return 0;
171}
172
173/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900174 * is_charging - Returns true if the battery is being charged.
175 * @cm: the Charger Manager representing the battery.
176 */
177static bool is_charging(struct charger_manager *cm)
178{
179 int i, ret;
180 bool charging = false;
181 union power_supply_propval val;
182
183 /* If there is no battery, it cannot be charged */
184 if (!is_batt_present(cm))
185 return false;
186
187 /* If at least one of the charger is charging, return yes */
188 for (i = 0; cm->charger_stat[i]; i++) {
189 /* 1. The charger sholuld not be DISABLED */
190 if (cm->emergency_stop)
191 continue;
192 if (!cm->charger_enabled)
193 continue;
194
195 /* 2. The charger should be online (ext-power) */
196 ret = cm->charger_stat[i]->get_property(
197 cm->charger_stat[i],
198 POWER_SUPPLY_PROP_ONLINE, &val);
199 if (ret) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700200 dev_warn(cm->dev, "Cannot read ONLINE value from %s\n",
201 cm->desc->psy_charger_stat[i]);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900202 continue;
203 }
204 if (val.intval == 0)
205 continue;
206
207 /*
208 * 3. The charger should not be FULL, DISCHARGING,
209 * or NOT_CHARGING.
210 */
211 ret = cm->charger_stat[i]->get_property(
212 cm->charger_stat[i],
213 POWER_SUPPLY_PROP_STATUS, &val);
214 if (ret) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700215 dev_warn(cm->dev, "Cannot read STATUS value from %s\n",
216 cm->desc->psy_charger_stat[i]);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900217 continue;
218 }
219 if (val.intval == POWER_SUPPLY_STATUS_FULL ||
220 val.intval == POWER_SUPPLY_STATUS_DISCHARGING ||
221 val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING)
222 continue;
223
224 /* Then, this is charging. */
225 charging = true;
226 break;
227 }
228
229 return charging;
230}
231
232/**
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900233 * is_full_charged - Returns true if the battery is fully charged.
234 * @cm: the Charger Manager representing the battery.
235 */
236static bool is_full_charged(struct charger_manager *cm)
237{
238 struct charger_desc *desc = cm->desc;
239 union power_supply_propval val;
240 int ret = 0;
241 int uV;
242
243 /* If there is no battery, it cannot be charged */
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900244 if (!is_batt_present(cm))
245 return false;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900246
247 if (cm->fuel_gauge && desc->fullbatt_full_capacity > 0) {
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900248 val.intval = 0;
249
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900250 /* Not full if capacity of fuel gauge isn't full */
251 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
252 POWER_SUPPLY_PROP_CHARGE_FULL, &val);
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900253 if (!ret && val.intval > desc->fullbatt_full_capacity)
254 return true;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900255 }
256
257 /* Full, if it's over the fullbatt voltage */
258 if (desc->fullbatt_uV > 0) {
259 ret = get_batt_uV(cm, &uV);
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900260 if (!ret && uV >= desc->fullbatt_uV)
261 return true;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900262 }
263
264 /* Full, if the capacity is more than fullbatt_soc */
265 if (cm->fuel_gauge && desc->fullbatt_soc > 0) {
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900266 val.intval = 0;
267
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900268 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
269 POWER_SUPPLY_PROP_CAPACITY, &val);
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900270 if (!ret && val.intval >= desc->fullbatt_soc)
271 return true;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900272 }
273
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900274 return false;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900275}
276
277/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900278 * is_polling_required - Return true if need to continue polling for this CM.
279 * @cm: the Charger Manager representing the battery.
280 */
281static bool is_polling_required(struct charger_manager *cm)
282{
283 switch (cm->desc->polling_mode) {
284 case CM_POLL_DISABLE:
285 return false;
286 case CM_POLL_ALWAYS:
287 return true;
288 case CM_POLL_EXTERNAL_POWER_ONLY:
289 return is_ext_pwr_online(cm);
290 case CM_POLL_CHARGING_ONLY:
291 return is_charging(cm);
292 default:
293 dev_warn(cm->dev, "Incorrect polling_mode (%d)\n",
Joe Perchese5409cb2013-06-06 18:25:12 -0700294 cm->desc->polling_mode);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900295 }
296
297 return false;
298}
299
300/**
301 * try_charger_enable - Enable/Disable chargers altogether
302 * @cm: the Charger Manager representing the battery.
303 * @enable: true: enable / false: disable
304 *
305 * Note that Charger Manager keeps the charger enabled regardless whether
306 * the charger is charging or not (because battery is full or no external
307 * power source exists) except when CM needs to disable chargers forcibly
308 * bacause of emergency causes; when the battery is overheated or too cold.
309 */
310static int try_charger_enable(struct charger_manager *cm, bool enable)
311{
312 int err = 0, i;
313 struct charger_desc *desc = cm->desc;
314
315 /* Ignore if it's redundent command */
Axel Linbb2a95c2012-01-12 12:56:35 +0800316 if (enable == cm->charger_enabled)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900317 return 0;
318
319 if (enable) {
320 if (cm->emergency_stop)
321 return -EAGAIN;
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700322
323 /*
324 * Save start time of charging to limit
325 * maximum possible charging time.
326 */
327 cm->charging_start_time = ktime_to_ms(ktime_get());
328 cm->charging_end_time = 0;
329
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900330 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
Chanwoo Choi3950c782012-09-21 18:49:37 +0900331 if (desc->charger_regulators[i].externally_control)
332 continue;
333
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900334 err = regulator_enable(desc->charger_regulators[i].consumer);
335 if (err < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700336 dev_warn(cm->dev, "Cannot enable %s regulator\n",
337 desc->charger_regulators[i].regulator_name);
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900338 }
339 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900340 } else {
341 /*
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700342 * Save end time of charging to maintain fully charged state
343 * of battery after full-batt.
344 */
345 cm->charging_start_time = 0;
346 cm->charging_end_time = ktime_to_ms(ktime_get());
347
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900348 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
Chanwoo Choi3950c782012-09-21 18:49:37 +0900349 if (desc->charger_regulators[i].externally_control)
350 continue;
351
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900352 err = regulator_disable(desc->charger_regulators[i].consumer);
353 if (err < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700354 dev_warn(cm->dev, "Cannot disable %s regulator\n",
355 desc->charger_regulators[i].regulator_name);
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900356 }
357 }
358
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900359 /*
360 * Abnormal battery state - Stop charging forcibly,
361 * even if charger was enabled at the other places
362 */
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900363 for (i = 0; i < desc->num_charger_regulators; i++) {
364 if (regulator_is_enabled(
365 desc->charger_regulators[i].consumer)) {
366 regulator_force_disable(
367 desc->charger_regulators[i].consumer);
Joe Perchese5409cb2013-06-06 18:25:12 -0700368 dev_warn(cm->dev, "Disable regulator(%s) forcibly\n",
369 desc->charger_regulators[i].regulator_name);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900370 }
371 }
372 }
373
374 if (!err)
375 cm->charger_enabled = enable;
376
377 return err;
378}
379
380/**
Chanwoo Choid829dc72012-05-05 06:24:10 -0700381 * try_charger_restart - Restart charging.
382 * @cm: the Charger Manager representing the battery.
383 *
384 * Restart charging by turning off and on the charger.
385 */
386static int try_charger_restart(struct charger_manager *cm)
387{
388 int err;
389
390 if (cm->emergency_stop)
391 return -EAGAIN;
392
393 err = try_charger_enable(cm, false);
394 if (err)
395 return err;
396
397 return try_charger_enable(cm, true);
398}
399
400/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900401 * uevent_notify - Let users know something has changed.
402 * @cm: the Charger Manager representing the battery.
403 * @event: the event string.
404 *
405 * If @event is null, it implies that uevent_notify is called
406 * by resume function. When called in the resume function, cm_suspended
407 * should be already reset to false in order to let uevent_notify
408 * notify the recent event during the suspend to users. While
409 * suspended, uevent_notify does not notify users, but tracks
410 * events so that uevent_notify can notify users later after resumed.
411 */
412static void uevent_notify(struct charger_manager *cm, const char *event)
413{
414 static char env_str[UEVENT_BUF_SIZE + 1] = "";
415 static char env_str_save[UEVENT_BUF_SIZE + 1] = "";
416
417 if (cm_suspended) {
418 /* Nothing in suspended-event buffer */
419 if (env_str_save[0] == 0) {
420 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
421 return; /* status not changed */
422 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
423 return;
424 }
425
426 if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE))
427 return; /* Duplicated. */
Axel Linbb2a95c2012-01-12 12:56:35 +0800428 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900429 return;
430 }
431
432 if (event == NULL) {
433 /* No messages pending */
434 if (!env_str_save[0])
435 return;
436
437 strncpy(env_str, env_str_save, UEVENT_BUF_SIZE);
438 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
439 env_str_save[0] = 0;
440
441 return;
442 }
443
444 /* status not changed */
445 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
446 return;
447
448 /* save the status and notify the update */
449 strncpy(env_str, event, UEVENT_BUF_SIZE);
450 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
451
Joe Perchese5409cb2013-06-06 18:25:12 -0700452 dev_info(cm->dev, "%s\n", event);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900453}
454
455/**
Chanwoo Choid829dc72012-05-05 06:24:10 -0700456 * fullbatt_vchk - Check voltage drop some times after "FULL" event.
457 * @work: the work_struct appointing the function
458 *
459 * If a user has designated "fullbatt_vchkdrop_ms/uV" values with
460 * charger_desc, Charger Manager checks voltage drop after the battery
461 * "FULL" event. It checks whether the voltage has dropped more than
462 * fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms.
463 */
464static void fullbatt_vchk(struct work_struct *work)
465{
466 struct delayed_work *dwork = to_delayed_work(work);
467 struct charger_manager *cm = container_of(dwork,
468 struct charger_manager, fullbatt_vchk_work);
469 struct charger_desc *desc = cm->desc;
470 int batt_uV, err, diff;
471
472 /* remove the appointment for fullbatt_vchk */
473 cm->fullbatt_vchk_jiffies_at = 0;
474
475 if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
476 return;
477
478 err = get_batt_uV(cm, &batt_uV);
479 if (err) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700480 dev_err(cm->dev, "%s: get_batt_uV error(%d)\n", __func__, err);
Chanwoo Choid829dc72012-05-05 06:24:10 -0700481 return;
482 }
483
Chanwoo Choif36b9dd2012-11-22 16:53:51 +0900484 diff = desc->fullbatt_uV - batt_uV;
485 if (diff < 0)
486 return;
Chanwoo Choid829dc72012-05-05 06:24:10 -0700487
Joe Perchese5409cb2013-06-06 18:25:12 -0700488 dev_info(cm->dev, "VBATT dropped %duV after full-batt\n", diff);
Chanwoo Choid829dc72012-05-05 06:24:10 -0700489
490 if (diff > desc->fullbatt_vchkdrop_uV) {
491 try_charger_restart(cm);
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700492 uevent_notify(cm, "Recharging");
Chanwoo Choid829dc72012-05-05 06:24:10 -0700493 }
494}
495
496/**
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700497 * check_charging_duration - Monitor charging/discharging duration
498 * @cm: the Charger Manager representing the battery.
499 *
500 * If whole charging duration exceed 'charging_max_duration_ms',
501 * cm stop charging to prevent overcharge/overheat. If discharging
502 * duration exceed 'discharging _max_duration_ms', charger cable is
503 * attached, after full-batt, cm start charging to maintain fully
504 * charged state for battery.
505 */
506static int check_charging_duration(struct charger_manager *cm)
507{
508 struct charger_desc *desc = cm->desc;
509 u64 curr = ktime_to_ms(ktime_get());
510 u64 duration;
511 int ret = false;
512
513 if (!desc->charging_max_duration_ms &&
514 !desc->discharging_max_duration_ms)
515 return ret;
516
517 if (cm->charger_enabled) {
518 duration = curr - cm->charging_start_time;
519
520 if (duration > desc->charging_max_duration_ms) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700521 dev_info(cm->dev, "Charging duration exceed %lldms\n",
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700522 desc->charging_max_duration_ms);
523 uevent_notify(cm, "Discharging");
524 try_charger_enable(cm, false);
525 ret = true;
526 }
527 } else if (is_ext_pwr_online(cm) && !cm->charger_enabled) {
528 duration = curr - cm->charging_end_time;
529
530 if (duration > desc->charging_max_duration_ms &&
531 is_ext_pwr_online(cm)) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700532 dev_info(cm->dev, "Discharging duration exceed %lldms\n",
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700533 desc->discharging_max_duration_ms);
Joe Perchese5409cb2013-06-06 18:25:12 -0700534 uevent_notify(cm, "Recharging");
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700535 try_charger_enable(cm, true);
536 ret = true;
537 }
538 }
539
540 return ret;
541}
542
543/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900544 * _cm_monitor - Monitor the temperature and return true for exceptions.
545 * @cm: the Charger Manager representing the battery.
546 *
547 * Returns true if there is an event to notify for the battery.
548 * (True if the status of "emergency_stop" changes)
549 */
550static bool _cm_monitor(struct charger_manager *cm)
551{
552 struct charger_desc *desc = cm->desc;
553 int temp = desc->temperature_out_of_range(&cm->last_temp_mC);
554
555 dev_dbg(cm->dev, "monitoring (%2.2d.%3.3dC)\n",
556 cm->last_temp_mC / 1000, cm->last_temp_mC % 1000);
557
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900558 /* It has been stopped already */
559 if (temp && cm->emergency_stop)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900560 return false;
561
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900562 /*
563 * Check temperature whether overheat or cold.
564 * If temperature is out of range normal state, stop charging.
565 */
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900566 if (temp) {
567 cm->emergency_stop = temp;
568 if (!try_charger_enable(cm, false)) {
569 if (temp > 0)
570 uevent_notify(cm, "OVERHEAT");
571 else
572 uevent_notify(cm, "COLD");
573 }
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900574
575 /*
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700576 * Check whole charging duration and discharing duration
577 * after full-batt.
578 */
579 } else if (!cm->emergency_stop && check_charging_duration(cm)) {
580 dev_dbg(cm->dev,
Joe Perchese5409cb2013-06-06 18:25:12 -0700581 "Charging/Discharging duration is out of range\n");
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700582 /*
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900583 * Check dropped voltage of battery. If battery voltage is more
584 * dropped than fullbatt_vchkdrop_uV after fully charged state,
585 * charger-manager have to recharge battery.
586 */
587 } else if (!cm->emergency_stop && is_ext_pwr_online(cm) &&
588 !cm->charger_enabled) {
589 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
590
591 /*
592 * Check whether fully charged state to protect overcharge
593 * if charger-manager is charging for battery.
594 */
595 } else if (!cm->emergency_stop && is_full_charged(cm) &&
596 cm->charger_enabled) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700597 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged\n");
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900598 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
599
600 try_charger_enable(cm, false);
601
602 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900603 } else {
604 cm->emergency_stop = 0;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900605 if (is_ext_pwr_online(cm)) {
606 if (!try_charger_enable(cm, true))
607 uevent_notify(cm, "CHARGING");
608 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900609 }
610
611 return true;
612}
613
614/**
615 * cm_monitor - Monitor every battery.
616 *
617 * Returns true if there is an event to notify from any of the batteries.
618 * (True if the status of "emergency_stop" changes)
619 */
620static bool cm_monitor(void)
621{
622 bool stop = false;
623 struct charger_manager *cm;
624
625 mutex_lock(&cm_list_mtx);
626
Axel Linbb2a95c2012-01-12 12:56:35 +0800627 list_for_each_entry(cm, &cm_list, entry) {
628 if (_cm_monitor(cm))
629 stop = true;
630 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900631
632 mutex_unlock(&cm_list_mtx);
633
634 return stop;
635}
636
Chanwoo Choid829dc72012-05-05 06:24:10 -0700637/**
638 * _setup_polling - Setup the next instance of polling.
639 * @work: work_struct of the function _setup_polling.
640 */
641static void _setup_polling(struct work_struct *work)
642{
643 unsigned long min = ULONG_MAX;
644 struct charger_manager *cm;
645 bool keep_polling = false;
646 unsigned long _next_polling;
647
648 mutex_lock(&cm_list_mtx);
649
650 list_for_each_entry(cm, &cm_list, entry) {
651 if (is_polling_required(cm) && cm->desc->polling_interval_ms) {
652 keep_polling = true;
653
654 if (min > cm->desc->polling_interval_ms)
655 min = cm->desc->polling_interval_ms;
656 }
657 }
658
659 polling_jiffy = msecs_to_jiffies(min);
660 if (polling_jiffy <= CM_JIFFIES_SMALL)
661 polling_jiffy = CM_JIFFIES_SMALL + 1;
662
663 if (!keep_polling)
664 polling_jiffy = ULONG_MAX;
665 if (polling_jiffy == ULONG_MAX)
666 goto out;
667
668 WARN(cm_wq == NULL, "charger-manager: workqueue not initialized"
669 ". try it later. %s\n", __func__);
670
Tejun Heo2fbb5202012-12-21 17:56:51 -0800671 /*
672 * Use mod_delayed_work() iff the next polling interval should
673 * occur before the currently scheduled one. If @cm_monitor_work
674 * isn't active, the end result is the same, so no need to worry
675 * about stale @next_polling.
676 */
Chanwoo Choid829dc72012-05-05 06:24:10 -0700677 _next_polling = jiffies + polling_jiffy;
678
Tejun Heo2fbb5202012-12-21 17:56:51 -0800679 if (time_before(_next_polling, next_polling)) {
Tejun Heo41f63c52012-08-03 10:30:47 -0700680 mod_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy);
Tejun Heo2fbb5202012-12-21 17:56:51 -0800681 next_polling = _next_polling;
682 } else {
683 if (queue_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy))
684 next_polling = _next_polling;
Chanwoo Choid829dc72012-05-05 06:24:10 -0700685 }
Chanwoo Choid829dc72012-05-05 06:24:10 -0700686out:
687 mutex_unlock(&cm_list_mtx);
688}
689static DECLARE_WORK(setup_polling, _setup_polling);
690
691/**
692 * cm_monitor_poller - The Monitor / Poller.
693 * @work: work_struct of the function cm_monitor_poller
694 *
695 * During non-suspended state, cm_monitor_poller is used to poll and monitor
696 * the batteries.
697 */
698static void cm_monitor_poller(struct work_struct *work)
699{
700 cm_monitor();
701 schedule_work(&setup_polling);
702}
703
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700704/**
705 * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL
706 * @cm: the Charger Manager representing the battery.
707 */
708static void fullbatt_handler(struct charger_manager *cm)
709{
710 struct charger_desc *desc = cm->desc;
711
712 if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
713 goto out;
714
715 if (cm_suspended)
716 device_set_wakeup_capable(cm->dev, true);
717
Tejun Heo41f63c52012-08-03 10:30:47 -0700718 mod_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
719 msecs_to_jiffies(desc->fullbatt_vchkdrop_ms));
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700720 cm->fullbatt_vchk_jiffies_at = jiffies + msecs_to_jiffies(
721 desc->fullbatt_vchkdrop_ms);
722
723 if (cm->fullbatt_vchk_jiffies_at == 0)
724 cm->fullbatt_vchk_jiffies_at = 1;
725
726out:
Joe Perchese5409cb2013-06-06 18:25:12 -0700727 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged\n");
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700728 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
729}
730
731/**
732 * battout_handler - Event handler for CM_EVENT_BATT_OUT
733 * @cm: the Charger Manager representing the battery.
734 */
735static void battout_handler(struct charger_manager *cm)
736{
737 if (cm_suspended)
738 device_set_wakeup_capable(cm->dev, true);
739
740 if (!is_batt_present(cm)) {
741 dev_emerg(cm->dev, "Battery Pulled Out!\n");
742 uevent_notify(cm, default_event_names[CM_EVENT_BATT_OUT]);
743 } else {
744 uevent_notify(cm, "Battery Reinserted?");
745 }
746}
747
748/**
749 * misc_event_handler - Handler for other evnets
750 * @cm: the Charger Manager representing the battery.
751 * @type: the Charger Manager representing the battery.
752 */
753static void misc_event_handler(struct charger_manager *cm,
754 enum cm_event_types type)
755{
756 if (cm_suspended)
757 device_set_wakeup_capable(cm->dev, true);
758
Tejun Heo2fbb5202012-12-21 17:56:51 -0800759 if (is_polling_required(cm) && cm->desc->polling_interval_ms)
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700760 schedule_work(&setup_polling);
761 uevent_notify(cm, default_event_names[type]);
762}
763
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900764static int charger_get_property(struct power_supply *psy,
765 enum power_supply_property psp,
766 union power_supply_propval *val)
767{
768 struct charger_manager *cm = container_of(psy,
769 struct charger_manager, charger_psy);
770 struct charger_desc *desc = cm->desc;
Anton Vorontsovdf58c042012-03-15 21:01:28 +0400771 int ret = 0;
772 int uV;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900773
774 switch (psp) {
775 case POWER_SUPPLY_PROP_STATUS:
776 if (is_charging(cm))
777 val->intval = POWER_SUPPLY_STATUS_CHARGING;
778 else if (is_ext_pwr_online(cm))
779 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
780 else
781 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
782 break;
783 case POWER_SUPPLY_PROP_HEALTH:
784 if (cm->emergency_stop > 0)
785 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
786 else if (cm->emergency_stop < 0)
787 val->intval = POWER_SUPPLY_HEALTH_COLD;
788 else
789 val->intval = POWER_SUPPLY_HEALTH_GOOD;
790 break;
791 case POWER_SUPPLY_PROP_PRESENT:
792 if (is_batt_present(cm))
793 val->intval = 1;
794 else
795 val->intval = 0;
796 break;
797 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Anton Vorontsovdf58c042012-03-15 21:01:28 +0400798 ret = get_batt_uV(cm, &val->intval);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900799 break;
800 case POWER_SUPPLY_PROP_CURRENT_NOW:
801 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
802 POWER_SUPPLY_PROP_CURRENT_NOW, val);
803 break;
804 case POWER_SUPPLY_PROP_TEMP:
805 /* in thenth of centigrade */
806 if (cm->last_temp_mC == INT_MIN)
807 desc->temperature_out_of_range(&cm->last_temp_mC);
808 val->intval = cm->last_temp_mC / 100;
809 if (!desc->measure_battery_temp)
810 ret = -ENODEV;
811 break;
812 case POWER_SUPPLY_PROP_TEMP_AMBIENT:
813 /* in thenth of centigrade */
814 if (cm->last_temp_mC == INT_MIN)
815 desc->temperature_out_of_range(&cm->last_temp_mC);
816 val->intval = cm->last_temp_mC / 100;
817 if (desc->measure_battery_temp)
818 ret = -ENODEV;
819 break;
820 case POWER_SUPPLY_PROP_CAPACITY:
821 if (!cm->fuel_gauge) {
822 ret = -ENODEV;
823 break;
824 }
825
826 if (!is_batt_present(cm)) {
827 /* There is no battery. Assume 100% */
828 val->intval = 100;
829 break;
830 }
831
832 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
833 POWER_SUPPLY_PROP_CAPACITY, val);
834 if (ret)
835 break;
836
837 if (val->intval > 100) {
838 val->intval = 100;
839 break;
840 }
841 if (val->intval < 0)
842 val->intval = 0;
843
844 /* Do not adjust SOC when charging: voltage is overrated */
845 if (is_charging(cm))
846 break;
847
848 /*
849 * If the capacity value is inconsistent, calibrate it base on
850 * the battery voltage values and the thresholds given as desc
851 */
852 ret = get_batt_uV(cm, &uV);
853 if (ret) {
854 /* Voltage information not available. No calibration */
855 ret = 0;
856 break;
857 }
858
859 if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
860 !is_charging(cm)) {
861 val->intval = 100;
862 break;
863 }
864
865 break;
866 case POWER_SUPPLY_PROP_ONLINE:
867 if (is_ext_pwr_online(cm))
868 val->intval = 1;
869 else
870 val->intval = 0;
871 break;
872 case POWER_SUPPLY_PROP_CHARGE_FULL:
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900873 if (is_full_charged(cm))
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900874 val->intval = 1;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900875 else
876 val->intval = 0;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900877 ret = 0;
878 break;
879 case POWER_SUPPLY_PROP_CHARGE_NOW:
880 if (is_charging(cm)) {
881 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
882 POWER_SUPPLY_PROP_CHARGE_NOW,
883 val);
884 if (ret) {
885 val->intval = 1;
886 ret = 0;
887 } else {
888 /* If CHARGE_NOW is supplied, use it */
889 val->intval = (val->intval > 0) ?
890 val->intval : 1;
891 }
892 } else {
893 val->intval = 0;
894 }
895 break;
896 default:
897 return -EINVAL;
898 }
899 return ret;
900}
901
902#define NUM_CHARGER_PSY_OPTIONAL (4)
903static enum power_supply_property default_charger_props[] = {
904 /* Guaranteed to provide */
905 POWER_SUPPLY_PROP_STATUS,
906 POWER_SUPPLY_PROP_HEALTH,
907 POWER_SUPPLY_PROP_PRESENT,
908 POWER_SUPPLY_PROP_VOLTAGE_NOW,
909 POWER_SUPPLY_PROP_CAPACITY,
910 POWER_SUPPLY_PROP_ONLINE,
911 POWER_SUPPLY_PROP_CHARGE_FULL,
912 /*
913 * Optional properties are:
914 * POWER_SUPPLY_PROP_CHARGE_NOW,
915 * POWER_SUPPLY_PROP_CURRENT_NOW,
916 * POWER_SUPPLY_PROP_TEMP, and
917 * POWER_SUPPLY_PROP_TEMP_AMBIENT,
918 */
919};
920
921static struct power_supply psy_default = {
922 .name = "battery",
923 .type = POWER_SUPPLY_TYPE_BATTERY,
924 .properties = default_charger_props,
925 .num_properties = ARRAY_SIZE(default_charger_props),
926 .get_property = charger_get_property,
927};
928
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900929/**
930 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
931 * for suspend_again.
932 *
933 * Returns true if the alarm is set for Charger Manager to use.
934 * Returns false if
935 * cm_setup_timer fails to set an alarm,
936 * cm_setup_timer does not need to set an alarm for Charger Manager,
937 * or an alarm previously configured is to be used.
938 */
939static bool cm_setup_timer(void)
940{
941 struct charger_manager *cm;
942 unsigned int wakeup_ms = UINT_MAX;
943 bool ret = false;
944
945 mutex_lock(&cm_list_mtx);
946
947 list_for_each_entry(cm, &cm_list, entry) {
Chanwoo Choid829dc72012-05-05 06:24:10 -0700948 unsigned int fbchk_ms = 0;
949
950 /* fullbatt_vchk is required. setup timer for that */
951 if (cm->fullbatt_vchk_jiffies_at) {
952 fbchk_ms = jiffies_to_msecs(cm->fullbatt_vchk_jiffies_at
953 - jiffies);
954 if (time_is_before_eq_jiffies(
955 cm->fullbatt_vchk_jiffies_at) ||
956 msecs_to_jiffies(fbchk_ms) < CM_JIFFIES_SMALL) {
957 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
958 fbchk_ms = 0;
959 }
960 }
961 CM_MIN_VALID(wakeup_ms, fbchk_ms);
962
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900963 /* Skip if polling is not required for this CM */
964 if (!is_polling_required(cm) && !cm->emergency_stop)
965 continue;
966 if (cm->desc->polling_interval_ms == 0)
967 continue;
968 CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
969 }
970
971 mutex_unlock(&cm_list_mtx);
972
973 if (wakeup_ms < UINT_MAX && wakeup_ms > 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -0700974 pr_info("Charger Manager wakeup timer: %u ms\n", wakeup_ms);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900975 if (rtc_dev) {
976 struct rtc_wkalrm tmp;
977 unsigned long time, now;
978 unsigned long add = DIV_ROUND_UP(wakeup_ms, 1000);
979
980 /*
981 * Set alarm with the polling interval (wakeup_ms)
982 * except when rtc_wkalarm_save comes first.
983 * However, the alarm time should be NOW +
984 * CM_RTC_SMALL or later.
985 */
986 tmp.enabled = 1;
987 rtc_read_time(rtc_dev, &tmp.time);
988 rtc_tm_to_time(&tmp.time, &now);
989 if (add < CM_RTC_SMALL)
990 add = CM_RTC_SMALL;
991 time = now + add;
992
993 ret = true;
994
995 if (rtc_wkalarm_save.enabled &&
996 rtc_wkalarm_save_time &&
997 rtc_wkalarm_save_time < time) {
998 if (rtc_wkalarm_save_time < now + CM_RTC_SMALL)
999 time = now + CM_RTC_SMALL;
1000 else
1001 time = rtc_wkalarm_save_time;
1002
1003 /* The timer is not appointed by CM */
1004 ret = false;
1005 }
1006
Joe Perchese5409cb2013-06-06 18:25:12 -07001007 pr_info("Waking up after %lu secs\n", time - now);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001008
1009 rtc_time_to_tm(time, &tmp.time);
1010 rtc_set_alarm(rtc_dev, &tmp);
1011 cm_suspend_duration_ms += wakeup_ms;
1012 return ret;
1013 }
1014 }
1015
1016 if (rtc_dev)
1017 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1018 return false;
1019}
1020
Chanwoo Choid829dc72012-05-05 06:24:10 -07001021static void _cm_fbchk_in_suspend(struct charger_manager *cm)
1022{
1023 unsigned long jiffy_now = jiffies;
1024
1025 if (!cm->fullbatt_vchk_jiffies_at)
1026 return;
1027
1028 if (g_desc && g_desc->assume_timer_stops_in_suspend)
1029 jiffy_now += msecs_to_jiffies(cm_suspend_duration_ms);
1030
1031 /* Execute now if it's going to be executed not too long after */
1032 jiffy_now += CM_JIFFIES_SMALL;
1033
1034 if (time_after_eq(jiffy_now, cm->fullbatt_vchk_jiffies_at))
1035 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
1036}
1037
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001038/**
1039 * cm_suspend_again - Determine whether suspend again or not
1040 *
1041 * Returns true if the system should be suspended again
1042 * Returns false if the system should be woken up
1043 */
1044bool cm_suspend_again(void)
1045{
1046 struct charger_manager *cm;
1047 bool ret = false;
1048
1049 if (!g_desc || !g_desc->rtc_only_wakeup || !g_desc->rtc_only_wakeup() ||
1050 !cm_rtc_set)
1051 return false;
1052
1053 if (cm_monitor())
1054 goto out;
1055
1056 ret = true;
1057 mutex_lock(&cm_list_mtx);
1058 list_for_each_entry(cm, &cm_list, entry) {
Chanwoo Choid829dc72012-05-05 06:24:10 -07001059 _cm_fbchk_in_suspend(cm);
1060
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001061 if (cm->status_save_ext_pwr_inserted != is_ext_pwr_online(cm) ||
Axel Linbb2a95c2012-01-12 12:56:35 +08001062 cm->status_save_batt != is_batt_present(cm)) {
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001063 ret = false;
Axel Linbb2a95c2012-01-12 12:56:35 +08001064 break;
1065 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001066 }
1067 mutex_unlock(&cm_list_mtx);
1068
1069 cm_rtc_set = cm_setup_timer();
1070out:
1071 /* It's about the time when the non-CM appointed timer goes off */
1072 if (rtc_wkalarm_save.enabled) {
1073 unsigned long now;
1074 struct rtc_time tmp;
1075
1076 rtc_read_time(rtc_dev, &tmp);
1077 rtc_tm_to_time(&tmp, &now);
1078
1079 if (rtc_wkalarm_save_time &&
1080 now + CM_RTC_SMALL >= rtc_wkalarm_save_time)
1081 return false;
1082 }
1083 return ret;
1084}
1085EXPORT_SYMBOL_GPL(cm_suspend_again);
1086
1087/**
1088 * setup_charger_manager - initialize charger_global_desc data
1089 * @gd: pointer to instance of charger_global_desc
1090 */
1091int setup_charger_manager(struct charger_global_desc *gd)
1092{
1093 if (!gd)
1094 return -EINVAL;
1095
1096 if (rtc_dev)
1097 rtc_class_close(rtc_dev);
1098 rtc_dev = NULL;
1099 g_desc = NULL;
1100
1101 if (!gd->rtc_only_wakeup) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001102 pr_err("The callback rtc_only_wakeup is not given\n");
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001103 return -EINVAL;
1104 }
1105
1106 if (gd->rtc_name) {
1107 rtc_dev = rtc_class_open(gd->rtc_name);
1108 if (IS_ERR_OR_NULL(rtc_dev)) {
1109 rtc_dev = NULL;
1110 /* Retry at probe. RTC may be not registered yet */
1111 }
1112 } else {
Joe Perchese5409cb2013-06-06 18:25:12 -07001113 pr_warn("No wakeup timer is given for charger manager. "
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001114 "In-suspend monitoring won't work.\n");
1115 }
1116
1117 g_desc = gd;
1118 return 0;
1119}
1120EXPORT_SYMBOL_GPL(setup_charger_manager);
1121
Chanwoo Choibee737b2012-07-12 15:03:25 +09001122/**
1123 * charger_extcon_work - enable/diable charger according to the state
1124 * of charger cable
1125 *
1126 * @work: work_struct of the function charger_extcon_work.
1127 */
1128static void charger_extcon_work(struct work_struct *work)
1129{
1130 struct charger_cable *cable =
1131 container_of(work, struct charger_cable, wq);
Chanwoo Choi45cd4fb2012-07-12 15:03:29 +09001132 int ret;
1133
1134 if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) {
1135 ret = regulator_set_current_limit(cable->charger->consumer,
1136 cable->min_uA, cable->max_uA);
1137 if (ret < 0) {
1138 pr_err("Cannot set current limit of %s (%s)\n",
Joe Perchese5409cb2013-06-06 18:25:12 -07001139 cable->charger->regulator_name, cable->name);
Chanwoo Choi45cd4fb2012-07-12 15:03:29 +09001140 return;
1141 }
1142
1143 pr_info("Set current limit of %s : %duA ~ %duA\n",
Joe Perchese5409cb2013-06-06 18:25:12 -07001144 cable->charger->regulator_name,
1145 cable->min_uA, cable->max_uA);
Chanwoo Choi45cd4fb2012-07-12 15:03:29 +09001146 }
Chanwoo Choibee737b2012-07-12 15:03:25 +09001147
1148 try_charger_enable(cable->cm, cable->attached);
1149}
1150
1151/**
1152 * charger_extcon_notifier - receive the state of charger cable
1153 * when registered cable is attached or detached.
1154 *
1155 * @self: the notifier block of the charger_extcon_notifier.
1156 * @event: the cable state.
1157 * @ptr: the data pointer of notifier block.
1158 */
1159static int charger_extcon_notifier(struct notifier_block *self,
1160 unsigned long event, void *ptr)
1161{
1162 struct charger_cable *cable =
1163 container_of(self, struct charger_cable, nb);
1164
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001165 /*
1166 * The newly state of charger cable.
1167 * If cable is attached, cable->attached is true.
1168 */
Chanwoo Choibee737b2012-07-12 15:03:25 +09001169 cable->attached = event;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001170
1171 /*
1172 * Setup monitoring to check battery state
1173 * when charger cable is attached.
1174 */
1175 if (cable->attached && is_polling_required(cable->cm)) {
Tejun Heo2fbb5202012-12-21 17:56:51 -08001176 cancel_work_sync(&setup_polling);
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001177 schedule_work(&setup_polling);
1178 }
1179
1180 /*
1181 * Setup work for controlling charger(regulator)
1182 * according to charger cable.
1183 */
Chanwoo Choibee737b2012-07-12 15:03:25 +09001184 schedule_work(&cable->wq);
1185
1186 return NOTIFY_DONE;
1187}
1188
1189/**
1190 * charger_extcon_init - register external connector to use it
1191 * as the charger cable
1192 *
1193 * @cm: the Charger Manager representing the battery.
1194 * @cable: the Charger cable representing the external connector.
1195 */
1196static int charger_extcon_init(struct charger_manager *cm,
1197 struct charger_cable *cable)
1198{
1199 int ret = 0;
1200
1201 /*
1202 * Charger manager use Extcon framework to identify
1203 * the charger cable among various external connector
1204 * cable (e.g., TA, USB, MHL, Dock).
1205 */
1206 INIT_WORK(&cable->wq, charger_extcon_work);
1207 cable->nb.notifier_call = charger_extcon_notifier;
1208 ret = extcon_register_interest(&cable->extcon_dev,
1209 cable->extcon_name, cable->name, &cable->nb);
1210 if (ret < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001211 pr_info("Cannot register extcon_dev for %s(cable: %s)\n",
1212 cable->extcon_name, cable->name);
Chanwoo Choibee737b2012-07-12 15:03:25 +09001213 ret = -EINVAL;
1214 }
1215
1216 return ret;
1217}
1218
Chanwoo Choi41468a12012-11-22 16:54:26 +09001219/**
1220 * charger_manager_register_extcon - Register extcon device to recevie state
1221 * of charger cable.
1222 * @cm: the Charger Manager representing the battery.
1223 *
1224 * This function support EXTCON(External Connector) subsystem to detect the
1225 * state of charger cables for enabling or disabling charger(regulator) and
1226 * select the charger cable for charging among a number of external cable
1227 * according to policy of H/W board.
1228 */
1229static int charger_manager_register_extcon(struct charger_manager *cm)
1230{
1231 struct charger_desc *desc = cm->desc;
1232 struct charger_regulator *charger;
1233 int ret = 0;
1234 int i;
1235 int j;
1236
1237 for (i = 0; i < desc->num_charger_regulators; i++) {
1238 charger = &desc->charger_regulators[i];
1239
1240 charger->consumer = regulator_get(cm->dev,
1241 charger->regulator_name);
Jonghwa Lee5a6c2202013-06-25 14:18:14 +09001242 if (IS_ERR(charger->consumer)) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001243 dev_err(cm->dev, "Cannot find charger(%s)\n",
1244 charger->regulator_name);
Jonghwa Lee5a6c2202013-06-25 14:18:14 +09001245 return PTR_ERR(charger->consumer);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001246 }
1247 charger->cm = cm;
1248
1249 for (j = 0; j < charger->num_cables; j++) {
1250 struct charger_cable *cable = &charger->cables[j];
1251
1252 ret = charger_extcon_init(cm, cable);
1253 if (ret < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001254 dev_err(cm->dev, "Cannot initialize charger(%s)\n",
1255 charger->regulator_name);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001256 goto err;
1257 }
1258 cable->charger = charger;
1259 cable->cm = cm;
1260 }
1261 }
1262
1263err:
1264 return ret;
1265}
1266
Chanwoo Choi3950c782012-09-21 18:49:37 +09001267/* help function of sysfs node to control charger(regulator) */
1268static ssize_t charger_name_show(struct device *dev,
1269 struct device_attribute *attr, char *buf)
1270{
1271 struct charger_regulator *charger
1272 = container_of(attr, struct charger_regulator, attr_name);
1273
1274 return sprintf(buf, "%s\n", charger->regulator_name);
1275}
1276
1277static ssize_t charger_state_show(struct device *dev,
1278 struct device_attribute *attr, char *buf)
1279{
1280 struct charger_regulator *charger
1281 = container_of(attr, struct charger_regulator, attr_state);
1282 int state = 0;
1283
1284 if (!charger->externally_control)
1285 state = regulator_is_enabled(charger->consumer);
1286
1287 return sprintf(buf, "%s\n", state ? "enabled" : "disabled");
1288}
1289
1290static ssize_t charger_externally_control_show(struct device *dev,
1291 struct device_attribute *attr, char *buf)
1292{
1293 struct charger_regulator *charger = container_of(attr,
1294 struct charger_regulator, attr_externally_control);
1295
1296 return sprintf(buf, "%d\n", charger->externally_control);
1297}
1298
1299static ssize_t charger_externally_control_store(struct device *dev,
1300 struct device_attribute *attr, const char *buf,
1301 size_t count)
1302{
1303 struct charger_regulator *charger
1304 = container_of(attr, struct charger_regulator,
1305 attr_externally_control);
1306 struct charger_manager *cm = charger->cm;
1307 struct charger_desc *desc = cm->desc;
1308 int i;
1309 int ret;
1310 int externally_control;
1311 int chargers_externally_control = 1;
1312
1313 ret = sscanf(buf, "%d", &externally_control);
1314 if (ret == 0) {
1315 ret = -EINVAL;
1316 return ret;
1317 }
1318
1319 if (!externally_control) {
1320 charger->externally_control = 0;
1321 return count;
1322 }
1323
1324 for (i = 0; i < desc->num_charger_regulators; i++) {
1325 if (&desc->charger_regulators[i] != charger &&
Chanwoo Choi41468a12012-11-22 16:54:26 +09001326 !desc->charger_regulators[i].externally_control) {
Chanwoo Choi3950c782012-09-21 18:49:37 +09001327 /*
1328 * At least, one charger is controlled by
1329 * charger-manager
1330 */
1331 chargers_externally_control = 0;
1332 break;
1333 }
1334 }
1335
1336 if (!chargers_externally_control) {
1337 if (cm->charger_enabled) {
1338 try_charger_enable(charger->cm, false);
1339 charger->externally_control = externally_control;
1340 try_charger_enable(charger->cm, true);
1341 } else {
1342 charger->externally_control = externally_control;
1343 }
1344 } else {
1345 dev_warn(cm->dev,
Joe Perchese5409cb2013-06-06 18:25:12 -07001346 "'%s' regulator should be controlled in charger-manager because charger-manager must need at least one charger for charging\n",
1347 charger->regulator_name);
Chanwoo Choi3950c782012-09-21 18:49:37 +09001348 }
1349
1350 return count;
1351}
1352
Chanwoo Choi41468a12012-11-22 16:54:26 +09001353/**
1354 * charger_manager_register_sysfs - Register sysfs entry for each charger
1355 * @cm: the Charger Manager representing the battery.
1356 *
1357 * This function add sysfs entry for charger(regulator) to control charger from
1358 * user-space. If some development board use one more chargers for charging
1359 * but only need one charger on specific case which is dependent on user
1360 * scenario or hardware restrictions, the user enter 1 or 0(zero) to '/sys/
1361 * class/power_supply/battery/charger.[index]/externally_control'. For example,
1362 * if user enter 1 to 'sys/class/power_supply/battery/charger.[index]/
1363 * externally_control, this charger isn't controlled from charger-manager and
1364 * always stay off state of regulator.
1365 */
1366static int charger_manager_register_sysfs(struct charger_manager *cm)
1367{
1368 struct charger_desc *desc = cm->desc;
1369 struct charger_regulator *charger;
1370 int chargers_externally_control = 1;
1371 char buf[11];
1372 char *str;
1373 int ret = 0;
1374 int i;
1375
1376 /* Create sysfs entry to control charger(regulator) */
1377 for (i = 0; i < desc->num_charger_regulators; i++) {
1378 charger = &desc->charger_regulators[i];
1379
1380 snprintf(buf, 10, "charger.%d", i);
1381 str = kzalloc(sizeof(char) * (strlen(buf) + 1), GFP_KERNEL);
1382 if (!str) {
Chanwoo Choi41468a12012-11-22 16:54:26 +09001383 ret = -ENOMEM;
1384 goto err;
1385 }
1386 strcpy(str, buf);
1387
1388 charger->attrs[0] = &charger->attr_name.attr;
1389 charger->attrs[1] = &charger->attr_state.attr;
1390 charger->attrs[2] = &charger->attr_externally_control.attr;
1391 charger->attrs[3] = NULL;
1392 charger->attr_g.name = str;
1393 charger->attr_g.attrs = charger->attrs;
1394
1395 sysfs_attr_init(&charger->attr_name.attr);
1396 charger->attr_name.attr.name = "name";
1397 charger->attr_name.attr.mode = 0444;
1398 charger->attr_name.show = charger_name_show;
1399
1400 sysfs_attr_init(&charger->attr_state.attr);
1401 charger->attr_state.attr.name = "state";
1402 charger->attr_state.attr.mode = 0444;
1403 charger->attr_state.show = charger_state_show;
1404
1405 sysfs_attr_init(&charger->attr_externally_control.attr);
1406 charger->attr_externally_control.attr.name
1407 = "externally_control";
1408 charger->attr_externally_control.attr.mode = 0644;
1409 charger->attr_externally_control.show
1410 = charger_externally_control_show;
1411 charger->attr_externally_control.store
1412 = charger_externally_control_store;
1413
1414 if (!desc->charger_regulators[i].externally_control ||
1415 !chargers_externally_control)
1416 chargers_externally_control = 0;
1417
Joe Perchese5409cb2013-06-06 18:25:12 -07001418 dev_info(cm->dev, "'%s' regulator's externally_control is %d\n",
1419 charger->regulator_name, charger->externally_control);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001420
1421 ret = sysfs_create_group(&cm->charger_psy.dev->kobj,
1422 &charger->attr_g);
1423 if (ret < 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001424 dev_err(cm->dev, "Cannot create sysfs entry of %s regulator\n",
1425 charger->regulator_name);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001426 ret = -EINVAL;
1427 goto err;
1428 }
1429 }
1430
1431 if (chargers_externally_control) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001432 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 +09001433 ret = -EINVAL;
1434 goto err;
1435 }
1436
1437err:
1438 return ret;
1439}
1440
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001441static int charger_manager_probe(struct platform_device *pdev)
1442{
1443 struct charger_desc *desc = dev_get_platdata(&pdev->dev);
1444 struct charger_manager *cm;
1445 int ret = 0, i = 0;
Chanwoo Choibee737b2012-07-12 15:03:25 +09001446 int j = 0;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001447 union power_supply_propval val;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001448
1449 if (g_desc && !rtc_dev && g_desc->rtc_name) {
1450 rtc_dev = rtc_class_open(g_desc->rtc_name);
1451 if (IS_ERR_OR_NULL(rtc_dev)) {
1452 rtc_dev = NULL;
Joe Perchese5409cb2013-06-06 18:25:12 -07001453 dev_err(&pdev->dev, "Cannot get RTC %s\n",
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001454 g_desc->rtc_name);
1455 ret = -ENODEV;
1456 goto err_alloc;
1457 }
1458 }
1459
1460 if (!desc) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001461 dev_err(&pdev->dev, "No platform data (desc) found\n");
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001462 ret = -ENODEV;
1463 goto err_alloc;
1464 }
1465
1466 cm = kzalloc(sizeof(struct charger_manager), GFP_KERNEL);
1467 if (!cm) {
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001468 ret = -ENOMEM;
1469 goto err_alloc;
1470 }
1471
1472 /* Basic Values. Unspecified are Null or 0 */
1473 cm->dev = &pdev->dev;
Andrei Epuref2c6a5f2013-03-27 23:48:21 +02001474 cm->desc = kmemdup(desc, sizeof(struct charger_desc), GFP_KERNEL);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001475 if (!cm->desc) {
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001476 ret = -ENOMEM;
1477 goto err_alloc_desc;
1478 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001479 cm->last_temp_mC = INT_MIN; /* denotes "unmeasured, yet" */
1480
Chanwoo Choid829dc72012-05-05 06:24:10 -07001481 /*
1482 * The following two do not need to be errors.
1483 * Users may intentionally ignore those two features.
1484 */
1485 if (desc->fullbatt_uV == 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001486 dev_info(&pdev->dev, "Ignoring full-battery voltage threshold as it is not supplied\n");
Chanwoo Choid829dc72012-05-05 06:24:10 -07001487 }
1488 if (!desc->fullbatt_vchkdrop_ms || !desc->fullbatt_vchkdrop_uV) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001489 dev_info(&pdev->dev, "Disabling full-battery voltage drop checking mechanism as it is not supplied\n");
Chanwoo Choid829dc72012-05-05 06:24:10 -07001490 desc->fullbatt_vchkdrop_ms = 0;
1491 desc->fullbatt_vchkdrop_uV = 0;
1492 }
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001493 if (desc->fullbatt_soc == 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001494 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 +09001495 }
1496 if (desc->fullbatt_full_capacity == 0) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001497 dev_info(&pdev->dev, "Ignoring full-battery full capacity threshold as it is not supplied\n");
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001498 }
Chanwoo Choid829dc72012-05-05 06:24:10 -07001499
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001500 if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
1501 ret = -EINVAL;
Joe Perchese5409cb2013-06-06 18:25:12 -07001502 dev_err(&pdev->dev, "charger_regulators undefined\n");
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001503 goto err_no_charger;
1504 }
1505
1506 if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001507 dev_err(&pdev->dev, "No power supply defined\n");
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001508 ret = -EINVAL;
1509 goto err_no_charger_stat;
1510 }
1511
1512 /* Counting index only */
1513 while (desc->psy_charger_stat[i])
1514 i++;
1515
1516 cm->charger_stat = kzalloc(sizeof(struct power_supply *) * (i + 1),
1517 GFP_KERNEL);
1518 if (!cm->charger_stat) {
1519 ret = -ENOMEM;
1520 goto err_no_charger_stat;
1521 }
1522
1523 for (i = 0; desc->psy_charger_stat[i]; i++) {
1524 cm->charger_stat[i] = power_supply_get_by_name(
1525 desc->psy_charger_stat[i]);
1526 if (!cm->charger_stat[i]) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001527 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
1528 desc->psy_charger_stat[i]);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001529 ret = -ENODEV;
1530 goto err_chg_stat;
1531 }
1532 }
1533
1534 cm->fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
1535 if (!cm->fuel_gauge) {
1536 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
Joe Perchese5409cb2013-06-06 18:25:12 -07001537 desc->psy_fuel_gauge);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001538 ret = -ENODEV;
1539 goto err_chg_stat;
1540 }
1541
1542 if (desc->polling_interval_ms == 0 ||
1543 msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) {
1544 dev_err(&pdev->dev, "polling_interval_ms is too small\n");
1545 ret = -EINVAL;
1546 goto err_chg_stat;
1547 }
1548
1549 if (!desc->temperature_out_of_range) {
1550 dev_err(&pdev->dev, "there is no temperature_out_of_range\n");
1551 ret = -EINVAL;
1552 goto err_chg_stat;
1553 }
1554
Chanwoo Choi8fcfe082012-09-20 21:20:05 -07001555 if (!desc->charging_max_duration_ms ||
1556 !desc->discharging_max_duration_ms) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001557 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 -07001558 desc->charging_max_duration_ms = 0;
1559 desc->discharging_max_duration_ms = 0;
1560 }
1561
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001562 platform_set_drvdata(pdev, cm);
1563
Axel Linbb2a95c2012-01-12 12:56:35 +08001564 memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default));
1565
Chanwoo Choi41468a12012-11-22 16:54:26 +09001566 if (!desc->psy_name)
Axel Linbb2a95c2012-01-12 12:56:35 +08001567 strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001568 else
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001569 strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001570 cm->charger_psy.name = cm->psy_name_buf;
1571
1572 /* Allocate for psy properties because they may vary */
1573 cm->charger_psy.properties = kzalloc(sizeof(enum power_supply_property)
1574 * (ARRAY_SIZE(default_charger_props) +
1575 NUM_CHARGER_PSY_OPTIONAL),
1576 GFP_KERNEL);
1577 if (!cm->charger_psy.properties) {
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001578 ret = -ENOMEM;
1579 goto err_chg_stat;
1580 }
1581 memcpy(cm->charger_psy.properties, default_charger_props,
1582 sizeof(enum power_supply_property) *
1583 ARRAY_SIZE(default_charger_props));
1584 cm->charger_psy.num_properties = psy_default.num_properties;
1585
1586 /* Find which optional psy-properties are available */
1587 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1588 POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
1589 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1590 POWER_SUPPLY_PROP_CHARGE_NOW;
1591 cm->charger_psy.num_properties++;
1592 }
1593 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1594 POWER_SUPPLY_PROP_CURRENT_NOW,
1595 &val)) {
1596 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1597 POWER_SUPPLY_PROP_CURRENT_NOW;
1598 cm->charger_psy.num_properties++;
1599 }
Axel Linbb2a95c2012-01-12 12:56:35 +08001600
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001601 if (desc->measure_battery_temp) {
1602 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1603 POWER_SUPPLY_PROP_TEMP;
1604 cm->charger_psy.num_properties++;
Axel Linbb2a95c2012-01-12 12:56:35 +08001605 } else {
1606 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1607 POWER_SUPPLY_PROP_TEMP_AMBIENT;
1608 cm->charger_psy.num_properties++;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001609 }
1610
Chanwoo Choid829dc72012-05-05 06:24:10 -07001611 INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk);
1612
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001613 ret = power_supply_register(NULL, &cm->charger_psy);
1614 if (ret) {
Joe Perchese5409cb2013-06-06 18:25:12 -07001615 dev_err(&pdev->dev, "Cannot register charger-manager with name \"%s\"\n",
1616 cm->charger_psy.name);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001617 goto err_register;
1618 }
1619
Chanwoo Choi41468a12012-11-22 16:54:26 +09001620 /* Register extcon device for charger cable */
1621 ret = charger_manager_register_extcon(cm);
1622 if (ret < 0) {
1623 dev_err(&pdev->dev, "Cannot initialize extcon device\n");
1624 goto err_reg_extcon;
Chanwoo Choi3950c782012-09-21 18:49:37 +09001625 }
1626
Chanwoo Choi41468a12012-11-22 16:54:26 +09001627 /* Register sysfs entry for charger(regulator) */
1628 ret = charger_manager_register_sysfs(cm);
1629 if (ret < 0) {
1630 dev_err(&pdev->dev,
1631 "Cannot initialize sysfs entry of regulator\n");
1632 goto err_reg_sysfs;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001633 }
1634
1635 /* Add to the list */
1636 mutex_lock(&cm_list_mtx);
1637 list_add(&cm->entry, &cm_list);
1638 mutex_unlock(&cm_list_mtx);
1639
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001640 /*
1641 * Charger-manager is capable of waking up the systme from sleep
1642 * when event is happend through cm_notify_event()
1643 */
1644 device_init_wakeup(&pdev->dev, true);
1645 device_set_wakeup_capable(&pdev->dev, false);
1646
Chanwoo Choid829dc72012-05-05 06:24:10 -07001647 schedule_work(&setup_polling);
1648
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001649 return 0;
1650
Chanwoo Choi41468a12012-11-22 16:54:26 +09001651err_reg_sysfs:
Chanwoo Choi3950c782012-09-21 18:49:37 +09001652 for (i = 0; i < desc->num_charger_regulators; i++) {
1653 struct charger_regulator *charger;
1654
1655 charger = &desc->charger_regulators[i];
1656 sysfs_remove_group(&cm->charger_psy.dev->kobj,
1657 &charger->attr_g);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001658
Chanwoo Choi3950c782012-09-21 18:49:37 +09001659 kfree(charger->attr_g.name);
1660 }
Chanwoo Choi41468a12012-11-22 16:54:26 +09001661err_reg_extcon:
1662 for (i = 0; i < desc->num_charger_regulators; i++) {
1663 struct charger_regulator *charger;
1664
1665 charger = &desc->charger_regulators[i];
1666 for (j = 0; j < charger->num_cables; j++) {
Chanwoo Choibee737b2012-07-12 15:03:25 +09001667 struct charger_cable *cable = &charger->cables[j];
Jonghwa Lee3cc9d2692013-06-25 14:02:49 +09001668 /* Remove notifier block if only edev exists */
1669 if (cable->extcon_dev.edev)
1670 extcon_unregister_interest(&cable->extcon_dev);
Chanwoo Choibee737b2012-07-12 15:03:25 +09001671 }
Chanwoo Choi41468a12012-11-22 16:54:26 +09001672
Chanwoo Choibee737b2012-07-12 15:03:25 +09001673 regulator_put(desc->charger_regulators[i].consumer);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001674 }
Chanwoo Choibee737b2012-07-12 15:03:25 +09001675
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001676 power_supply_unregister(&cm->charger_psy);
1677err_register:
1678 kfree(cm->charger_psy.properties);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001679err_chg_stat:
1680 kfree(cm->charger_stat);
1681err_no_charger_stat:
1682err_no_charger:
1683 kfree(cm->desc);
1684err_alloc_desc:
1685 kfree(cm);
1686err_alloc:
1687 return ret;
1688}
1689
Bill Pemberton415ec692012-11-19 13:26:07 -05001690static int charger_manager_remove(struct platform_device *pdev)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001691{
1692 struct charger_manager *cm = platform_get_drvdata(pdev);
1693 struct charger_desc *desc = cm->desc;
Chanwoo Choibee737b2012-07-12 15:03:25 +09001694 int i = 0;
1695 int j = 0;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001696
1697 /* Remove from the list */
1698 mutex_lock(&cm_list_mtx);
1699 list_del(&cm->entry);
1700 mutex_unlock(&cm_list_mtx);
1701
Tejun Heo2fbb5202012-12-21 17:56:51 -08001702 cancel_work_sync(&setup_polling);
1703 cancel_delayed_work_sync(&cm_monitor_work);
Chanwoo Choid829dc72012-05-05 06:24:10 -07001704
Chanwoo Choibee737b2012-07-12 15:03:25 +09001705 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1706 struct charger_regulator *charger
1707 = &desc->charger_regulators[i];
1708 for (j = 0 ; j < charger->num_cables ; j++) {
1709 struct charger_cable *cable = &charger->cables[j];
1710 extcon_unregister_interest(&cable->extcon_dev);
1711 }
1712 }
1713
1714 for (i = 0 ; i < desc->num_charger_regulators ; i++)
1715 regulator_put(desc->charger_regulators[i].consumer);
1716
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001717 power_supply_unregister(&cm->charger_psy);
Chanwoo Choid829dc72012-05-05 06:24:10 -07001718
1719 try_charger_enable(cm, false);
1720
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001721 kfree(cm->charger_psy.properties);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001722 kfree(cm->charger_stat);
1723 kfree(cm->desc);
1724 kfree(cm);
1725
1726 return 0;
1727}
1728
Axel Lin1bbe24d2012-01-11 17:19:45 +08001729static const struct platform_device_id charger_manager_id[] = {
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001730 { "charger-manager", 0 },
1731 { },
1732};
Axel Lin1bbe24d2012-01-11 17:19:45 +08001733MODULE_DEVICE_TABLE(platform, charger_manager_id);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001734
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001735static int cm_suspend_noirq(struct device *dev)
1736{
1737 int ret = 0;
1738
1739 if (device_may_wakeup(dev)) {
1740 device_set_wakeup_capable(dev, false);
1741 ret = -EAGAIN;
1742 }
1743
1744 return ret;
1745}
1746
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001747static int cm_suspend_prepare(struct device *dev)
1748{
Axel Linbb2a95c2012-01-12 12:56:35 +08001749 struct charger_manager *cm = dev_get_drvdata(dev);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001750
1751 if (!cm_suspended) {
1752 if (rtc_dev) {
1753 struct rtc_time tmp;
1754 unsigned long now;
1755
1756 rtc_read_alarm(rtc_dev, &rtc_wkalarm_save);
1757 rtc_read_time(rtc_dev, &tmp);
1758
1759 if (rtc_wkalarm_save.enabled) {
1760 rtc_tm_to_time(&rtc_wkalarm_save.time,
1761 &rtc_wkalarm_save_time);
1762 rtc_tm_to_time(&tmp, &now);
1763 if (now > rtc_wkalarm_save_time)
1764 rtc_wkalarm_save_time = 0;
1765 } else {
1766 rtc_wkalarm_save_time = 0;
1767 }
1768 }
1769 cm_suspended = true;
1770 }
1771
Tejun Heo2fbb5202012-12-21 17:56:51 -08001772 cancel_delayed_work(&cm->fullbatt_vchk_work);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001773 cm->status_save_ext_pwr_inserted = is_ext_pwr_online(cm);
1774 cm->status_save_batt = is_batt_present(cm);
1775
1776 if (!cm_rtc_set) {
1777 cm_suspend_duration_ms = 0;
1778 cm_rtc_set = cm_setup_timer();
1779 }
1780
1781 return 0;
1782}
1783
1784static void cm_suspend_complete(struct device *dev)
1785{
Axel Linbb2a95c2012-01-12 12:56:35 +08001786 struct charger_manager *cm = dev_get_drvdata(dev);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001787
1788 if (cm_suspended) {
1789 if (rtc_dev) {
1790 struct rtc_wkalrm tmp;
1791
1792 rtc_read_alarm(rtc_dev, &tmp);
1793 rtc_wkalarm_save.pending = tmp.pending;
1794 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1795 }
1796 cm_suspended = false;
1797 cm_rtc_set = false;
1798 }
1799
Chanwoo Choid829dc72012-05-05 06:24:10 -07001800 /* Re-enqueue delayed work (fullbatt_vchk_work) */
1801 if (cm->fullbatt_vchk_jiffies_at) {
1802 unsigned long delay = 0;
1803 unsigned long now = jiffies + CM_JIFFIES_SMALL;
1804
1805 if (time_after_eq(now, cm->fullbatt_vchk_jiffies_at)) {
1806 delay = (unsigned long)((long)now
1807 - (long)(cm->fullbatt_vchk_jiffies_at));
1808 delay = jiffies_to_msecs(delay);
1809 } else {
1810 delay = 0;
1811 }
1812
1813 /*
1814 * Account for cm_suspend_duration_ms if
1815 * assume_timer_stops_in_suspend is active
1816 */
1817 if (g_desc && g_desc->assume_timer_stops_in_suspend) {
1818 if (delay > cm_suspend_duration_ms)
1819 delay -= cm_suspend_duration_ms;
1820 else
1821 delay = 0;
1822 }
1823
1824 queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
1825 msecs_to_jiffies(delay));
1826 }
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001827 device_set_wakeup_capable(cm->dev, false);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001828 uevent_notify(cm, NULL);
1829}
1830
1831static const struct dev_pm_ops charger_manager_pm = {
1832 .prepare = cm_suspend_prepare,
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001833 .suspend_noirq = cm_suspend_noirq,
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001834 .complete = cm_suspend_complete,
1835};
1836
1837static struct platform_driver charger_manager_driver = {
1838 .driver = {
1839 .name = "charger-manager",
1840 .owner = THIS_MODULE,
1841 .pm = &charger_manager_pm,
1842 },
1843 .probe = charger_manager_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -05001844 .remove = charger_manager_remove,
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001845 .id_table = charger_manager_id,
1846};
1847
1848static int __init charger_manager_init(void)
1849{
Chanwoo Choid829dc72012-05-05 06:24:10 -07001850 cm_wq = create_freezable_workqueue("charger_manager");
1851 INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller);
1852
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001853 return platform_driver_register(&charger_manager_driver);
1854}
1855late_initcall(charger_manager_init);
1856
1857static void __exit charger_manager_cleanup(void)
1858{
Chanwoo Choid829dc72012-05-05 06:24:10 -07001859 destroy_workqueue(cm_wq);
1860 cm_wq = NULL;
1861
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001862 platform_driver_unregister(&charger_manager_driver);
1863}
1864module_exit(charger_manager_cleanup);
1865
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001866/**
1867 * find_power_supply - find the associated power_supply of charger
1868 * @cm: the Charger Manager representing the battery
1869 * @psy: pointer to instance of charger's power_supply
1870 */
1871static bool find_power_supply(struct charger_manager *cm,
1872 struct power_supply *psy)
1873{
1874 int i;
1875 bool found = false;
1876
1877 for (i = 0; cm->charger_stat[i]; i++) {
1878 if (psy == cm->charger_stat[i]) {
1879 found = true;
1880 break;
1881 }
1882 }
1883
1884 return found;
1885}
1886
1887/**
1888 * cm_notify_event - charger driver notify Charger Manager of charger event
1889 * @psy: pointer to instance of charger's power_supply
1890 * @type: type of charger event
1891 * @msg: optional message passed to uevent_notify fuction
1892 */
1893void cm_notify_event(struct power_supply *psy, enum cm_event_types type,
1894 char *msg)
1895{
1896 struct charger_manager *cm;
1897 bool found_power_supply = false;
1898
1899 if (psy == NULL)
1900 return;
1901
1902 mutex_lock(&cm_list_mtx);
1903 list_for_each_entry(cm, &cm_list, entry) {
1904 found_power_supply = find_power_supply(cm, psy);
1905 if (found_power_supply)
1906 break;
1907 }
1908 mutex_unlock(&cm_list_mtx);
1909
1910 if (!found_power_supply)
1911 return;
1912
1913 switch (type) {
1914 case CM_EVENT_BATT_FULL:
1915 fullbatt_handler(cm);
1916 break;
1917 case CM_EVENT_BATT_OUT:
1918 battout_handler(cm);
1919 break;
1920 case CM_EVENT_BATT_IN:
1921 case CM_EVENT_EXT_PWR_IN_OUT ... CM_EVENT_CHG_START_STOP:
1922 misc_event_handler(cm, type);
1923 break;
1924 case CM_EVENT_UNKNOWN:
1925 case CM_EVENT_OTHERS:
1926 uevent_notify(cm, msg ? msg : default_event_names[type]);
1927 break;
1928 default:
Joe Perchese5409cb2013-06-06 18:25:12 -07001929 dev_err(cm->dev, "%s: type not specified\n", __func__);
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001930 break;
1931 }
1932}
1933EXPORT_SYMBOL_GPL(cm_notify_event);
1934
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001935MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1936MODULE_DESCRIPTION("Charger Manager");
1937MODULE_LICENSE("GPL");