blob: 029732d830908c4c3d13a4026b2d5b581a87f8e4 [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
15#include <linux/io.h>
16#include <linux/module.h>
17#include <linux/irq.h>
18#include <linux/interrupt.h>
19#include <linux/rtc.h>
20#include <linux/slab.h>
21#include <linux/workqueue.h>
22#include <linux/platform_device.h>
23#include <linux/power/charger-manager.h>
24#include <linux/regulator/consumer.h>
Chanwoo Choi3950c782012-09-21 18:49:37 +090025#include <linux/sysfs.h>
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090026
Chanwoo Choidfeccb12012-05-05 06:26:47 -070027static const char * const default_event_names[] = {
28 [CM_EVENT_UNKNOWN] = "Unknown",
29 [CM_EVENT_BATT_FULL] = "Battery Full",
30 [CM_EVENT_BATT_IN] = "Battery Inserted",
31 [CM_EVENT_BATT_OUT] = "Battery Pulled Out",
32 [CM_EVENT_EXT_PWR_IN_OUT] = "External Power Attach/Detach",
33 [CM_EVENT_CHG_START_STOP] = "Charging Start/Stop",
34 [CM_EVENT_OTHERS] = "Other battery events"
35};
36
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090037/*
38 * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for
39 * delayed works so that we can run delayed works with CM_JIFFIES_SMALL
40 * without any delays.
41 */
42#define CM_JIFFIES_SMALL (2)
43
44/* If y is valid (> 0) and smaller than x, do x = y */
45#define CM_MIN_VALID(x, y) x = (((y > 0) && ((x) > (y))) ? (y) : (x))
46
47/*
48 * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking
49 * rtc alarm. It should be 2 or larger
50 */
51#define CM_RTC_SMALL (2)
52
53#define UEVENT_BUF_SIZE 32
54
55static LIST_HEAD(cm_list);
56static DEFINE_MUTEX(cm_list_mtx);
57
58/* About in-suspend (suspend-again) monitoring */
59static struct rtc_device *rtc_dev;
60/*
61 * Backup RTC alarm
62 * Save the wakeup alarm before entering suspend-to-RAM
63 */
64static struct rtc_wkalrm rtc_wkalarm_save;
65/* Backup RTC alarm time in terms of seconds since 01-01-1970 00:00:00 */
66static unsigned long rtc_wkalarm_save_time;
67static bool cm_suspended;
68static bool cm_rtc_set;
69static unsigned long cm_suspend_duration_ms;
70
Chanwoo Choid829dc72012-05-05 06:24:10 -070071/* About normal (not suspended) monitoring */
72static unsigned long polling_jiffy = ULONG_MAX; /* ULONG_MAX: no polling */
73static unsigned long next_polling; /* Next appointed polling time */
74static struct workqueue_struct *cm_wq; /* init at driver add */
75static struct delayed_work cm_monitor_work; /* init at driver add */
76
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090077/* Global charger-manager description */
78static struct charger_global_desc *g_desc; /* init with setup_charger_manager */
79
80/**
81 * is_batt_present - See if the battery presents in place.
82 * @cm: the Charger Manager representing the battery.
83 */
84static bool is_batt_present(struct charger_manager *cm)
85{
86 union power_supply_propval val;
87 bool present = false;
88 int i, ret;
89
90 switch (cm->desc->battery_present) {
Chanwoo Choid829dc72012-05-05 06:24:10 -070091 case CM_BATTERY_PRESENT:
92 present = true;
93 break;
94 case CM_NO_BATTERY:
95 break;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090096 case CM_FUEL_GAUGE:
97 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
98 POWER_SUPPLY_PROP_PRESENT, &val);
99 if (ret == 0 && val.intval)
100 present = true;
101 break;
102 case CM_CHARGER_STAT:
103 for (i = 0; cm->charger_stat[i]; i++) {
104 ret = cm->charger_stat[i]->get_property(
105 cm->charger_stat[i],
106 POWER_SUPPLY_PROP_PRESENT, &val);
107 if (ret == 0 && val.intval) {
108 present = true;
109 break;
110 }
111 }
112 break;
113 }
114
115 return present;
116}
117
118/**
119 * is_ext_pwr_online - See if an external power source is attached to charge
120 * @cm: the Charger Manager representing the battery.
121 *
122 * Returns true if at least one of the chargers of the battery has an external
123 * power source attached to charge the battery regardless of whether it is
124 * actually charging or not.
125 */
126static bool is_ext_pwr_online(struct charger_manager *cm)
127{
128 union power_supply_propval val;
129 bool online = false;
130 int i, ret;
131
132 /* If at least one of them has one, it's yes. */
133 for (i = 0; cm->charger_stat[i]; i++) {
134 ret = cm->charger_stat[i]->get_property(
135 cm->charger_stat[i],
136 POWER_SUPPLY_PROP_ONLINE, &val);
137 if (ret == 0 && val.intval) {
138 online = true;
139 break;
140 }
141 }
142
143 return online;
144}
145
146/**
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900147 * get_batt_uV - Get the voltage level of the battery
148 * @cm: the Charger Manager representing the battery.
149 * @uV: the voltage level returned.
150 *
151 * Returns 0 if there is no error.
152 * Returns a negative value on error.
153 */
154static int get_batt_uV(struct charger_manager *cm, int *uV)
155{
156 union power_supply_propval val;
157 int ret;
158
Axel Linbb2a95c2012-01-12 12:56:35 +0800159 if (!cm->fuel_gauge)
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900160 return -ENODEV;
161
Axel Linbb2a95c2012-01-12 12:56:35 +0800162 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
163 POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900164 if (ret)
165 return ret;
166
167 *uV = val.intval;
168 return 0;
169}
170
171/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900172 * is_charging - Returns true if the battery is being charged.
173 * @cm: the Charger Manager representing the battery.
174 */
175static bool is_charging(struct charger_manager *cm)
176{
177 int i, ret;
178 bool charging = false;
179 union power_supply_propval val;
180
181 /* If there is no battery, it cannot be charged */
182 if (!is_batt_present(cm))
183 return false;
184
185 /* If at least one of the charger is charging, return yes */
186 for (i = 0; cm->charger_stat[i]; i++) {
187 /* 1. The charger sholuld not be DISABLED */
188 if (cm->emergency_stop)
189 continue;
190 if (!cm->charger_enabled)
191 continue;
192
193 /* 2. The charger should be online (ext-power) */
194 ret = cm->charger_stat[i]->get_property(
195 cm->charger_stat[i],
196 POWER_SUPPLY_PROP_ONLINE, &val);
197 if (ret) {
198 dev_warn(cm->dev, "Cannot read ONLINE value from %s.\n",
199 cm->desc->psy_charger_stat[i]);
200 continue;
201 }
202 if (val.intval == 0)
203 continue;
204
205 /*
206 * 3. The charger should not be FULL, DISCHARGING,
207 * or NOT_CHARGING.
208 */
209 ret = cm->charger_stat[i]->get_property(
210 cm->charger_stat[i],
211 POWER_SUPPLY_PROP_STATUS, &val);
212 if (ret) {
213 dev_warn(cm->dev, "Cannot read STATUS value from %s.\n",
214 cm->desc->psy_charger_stat[i]);
215 continue;
216 }
217 if (val.intval == POWER_SUPPLY_STATUS_FULL ||
218 val.intval == POWER_SUPPLY_STATUS_DISCHARGING ||
219 val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING)
220 continue;
221
222 /* Then, this is charging. */
223 charging = true;
224 break;
225 }
226
227 return charging;
228}
229
230/**
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900231 * is_full_charged - Returns true if the battery is fully charged.
232 * @cm: the Charger Manager representing the battery.
233 */
234static bool is_full_charged(struct charger_manager *cm)
235{
236 struct charger_desc *desc = cm->desc;
237 union power_supply_propval val;
238 int ret = 0;
239 int uV;
240
241 /* If there is no battery, it cannot be charged */
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900242 if (!is_batt_present(cm))
243 return false;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900244
245 if (cm->fuel_gauge && desc->fullbatt_full_capacity > 0) {
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900246 val.intval = 0;
247
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900248 /* Not full if capacity of fuel gauge isn't full */
249 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
250 POWER_SUPPLY_PROP_CHARGE_FULL, &val);
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900251 if (!ret && val.intval > desc->fullbatt_full_capacity)
252 return true;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900253 }
254
255 /* Full, if it's over the fullbatt voltage */
256 if (desc->fullbatt_uV > 0) {
257 ret = get_batt_uV(cm, &uV);
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900258 if (!ret && uV >= desc->fullbatt_uV)
259 return true;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900260 }
261
262 /* Full, if the capacity is more than fullbatt_soc */
263 if (cm->fuel_gauge && desc->fullbatt_soc > 0) {
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900264 val.intval = 0;
265
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900266 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
267 POWER_SUPPLY_PROP_CAPACITY, &val);
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900268 if (!ret && val.intval >= desc->fullbatt_soc)
269 return true;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900270 }
271
Chanwoo Choi0fa11db2012-11-22 16:53:46 +0900272 return false;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900273}
274
275/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900276 * is_polling_required - Return true if need to continue polling for this CM.
277 * @cm: the Charger Manager representing the battery.
278 */
279static bool is_polling_required(struct charger_manager *cm)
280{
281 switch (cm->desc->polling_mode) {
282 case CM_POLL_DISABLE:
283 return false;
284 case CM_POLL_ALWAYS:
285 return true;
286 case CM_POLL_EXTERNAL_POWER_ONLY:
287 return is_ext_pwr_online(cm);
288 case CM_POLL_CHARGING_ONLY:
289 return is_charging(cm);
290 default:
291 dev_warn(cm->dev, "Incorrect polling_mode (%d)\n",
292 cm->desc->polling_mode);
293 }
294
295 return false;
296}
297
298/**
299 * try_charger_enable - Enable/Disable chargers altogether
300 * @cm: the Charger Manager representing the battery.
301 * @enable: true: enable / false: disable
302 *
303 * Note that Charger Manager keeps the charger enabled regardless whether
304 * the charger is charging or not (because battery is full or no external
305 * power source exists) except when CM needs to disable chargers forcibly
306 * bacause of emergency causes; when the battery is overheated or too cold.
307 */
308static int try_charger_enable(struct charger_manager *cm, bool enable)
309{
310 int err = 0, i;
311 struct charger_desc *desc = cm->desc;
312
313 /* Ignore if it's redundent command */
Axel Linbb2a95c2012-01-12 12:56:35 +0800314 if (enable == cm->charger_enabled)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900315 return 0;
316
317 if (enable) {
318 if (cm->emergency_stop)
319 return -EAGAIN;
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700320
321 /*
322 * Save start time of charging to limit
323 * maximum possible charging time.
324 */
325 cm->charging_start_time = ktime_to_ms(ktime_get());
326 cm->charging_end_time = 0;
327
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900328 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
Chanwoo Choi3950c782012-09-21 18:49:37 +0900329 if (desc->charger_regulators[i].externally_control)
330 continue;
331
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900332 err = regulator_enable(desc->charger_regulators[i].consumer);
333 if (err < 0) {
334 dev_warn(cm->dev,
335 "Cannot enable %s regulator\n",
336 desc->charger_regulators[i].regulator_name);
337 }
338 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900339 } else {
340 /*
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700341 * Save end time of charging to maintain fully charged state
342 * of battery after full-batt.
343 */
344 cm->charging_start_time = 0;
345 cm->charging_end_time = ktime_to_ms(ktime_get());
346
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900347 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
Chanwoo Choi3950c782012-09-21 18:49:37 +0900348 if (desc->charger_regulators[i].externally_control)
349 continue;
350
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900351 err = regulator_disable(desc->charger_regulators[i].consumer);
352 if (err < 0) {
353 dev_warn(cm->dev,
354 "Cannot disable %s regulator\n",
355 desc->charger_regulators[i].regulator_name);
356 }
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);
368 dev_warn(cm->dev,
369 "Disable regulator(%s) forcibly.\n",
Chanwoo Choibee737b2012-07-12 15:03:25 +0900370 desc->charger_regulators[i].regulator_name);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900371 }
372 }
373 }
374
375 if (!err)
376 cm->charger_enabled = enable;
377
378 return err;
379}
380
381/**
Chanwoo Choid829dc72012-05-05 06:24:10 -0700382 * try_charger_restart - Restart charging.
383 * @cm: the Charger Manager representing the battery.
384 *
385 * Restart charging by turning off and on the charger.
386 */
387static int try_charger_restart(struct charger_manager *cm)
388{
389 int err;
390
391 if (cm->emergency_stop)
392 return -EAGAIN;
393
394 err = try_charger_enable(cm, false);
395 if (err)
396 return err;
397
398 return try_charger_enable(cm, true);
399}
400
401/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900402 * uevent_notify - Let users know something has changed.
403 * @cm: the Charger Manager representing the battery.
404 * @event: the event string.
405 *
406 * If @event is null, it implies that uevent_notify is called
407 * by resume function. When called in the resume function, cm_suspended
408 * should be already reset to false in order to let uevent_notify
409 * notify the recent event during the suspend to users. While
410 * suspended, uevent_notify does not notify users, but tracks
411 * events so that uevent_notify can notify users later after resumed.
412 */
413static void uevent_notify(struct charger_manager *cm, const char *event)
414{
415 static char env_str[UEVENT_BUF_SIZE + 1] = "";
416 static char env_str_save[UEVENT_BUF_SIZE + 1] = "";
417
418 if (cm_suspended) {
419 /* Nothing in suspended-event buffer */
420 if (env_str_save[0] == 0) {
421 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
422 return; /* status not changed */
423 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
424 return;
425 }
426
427 if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE))
428 return; /* Duplicated. */
Axel Linbb2a95c2012-01-12 12:56:35 +0800429 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900430 return;
431 }
432
433 if (event == NULL) {
434 /* No messages pending */
435 if (!env_str_save[0])
436 return;
437
438 strncpy(env_str, env_str_save, UEVENT_BUF_SIZE);
439 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
440 env_str_save[0] = 0;
441
442 return;
443 }
444
445 /* status not changed */
446 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
447 return;
448
449 /* save the status and notify the update */
450 strncpy(env_str, event, UEVENT_BUF_SIZE);
451 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
452
453 dev_info(cm->dev, event);
454}
455
456/**
Chanwoo Choid829dc72012-05-05 06:24:10 -0700457 * fullbatt_vchk - Check voltage drop some times after "FULL" event.
458 * @work: the work_struct appointing the function
459 *
460 * If a user has designated "fullbatt_vchkdrop_ms/uV" values with
461 * charger_desc, Charger Manager checks voltage drop after the battery
462 * "FULL" event. It checks whether the voltage has dropped more than
463 * fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms.
464 */
465static void fullbatt_vchk(struct work_struct *work)
466{
467 struct delayed_work *dwork = to_delayed_work(work);
468 struct charger_manager *cm = container_of(dwork,
469 struct charger_manager, fullbatt_vchk_work);
470 struct charger_desc *desc = cm->desc;
471 int batt_uV, err, diff;
472
473 /* remove the appointment for fullbatt_vchk */
474 cm->fullbatt_vchk_jiffies_at = 0;
475
476 if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
477 return;
478
479 err = get_batt_uV(cm, &batt_uV);
480 if (err) {
481 dev_err(cm->dev, "%s: get_batt_uV error(%d).\n", __func__, err);
482 return;
483 }
484
Chanwoo Choif36b9dd2012-11-22 16:53:51 +0900485 diff = desc->fullbatt_uV - batt_uV;
486 if (diff < 0)
487 return;
Chanwoo Choid829dc72012-05-05 06:24:10 -0700488
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900489 dev_info(cm->dev, "VBATT dropped %duV after full-batt.\n", diff);
Chanwoo Choid829dc72012-05-05 06:24:10 -0700490
491 if (diff > desc->fullbatt_vchkdrop_uV) {
492 try_charger_restart(cm);
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700493 uevent_notify(cm, "Recharging");
Chanwoo Choid829dc72012-05-05 06:24:10 -0700494 }
495}
496
497/**
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700498 * check_charging_duration - Monitor charging/discharging duration
499 * @cm: the Charger Manager representing the battery.
500 *
501 * If whole charging duration exceed 'charging_max_duration_ms',
502 * cm stop charging to prevent overcharge/overheat. If discharging
503 * duration exceed 'discharging _max_duration_ms', charger cable is
504 * attached, after full-batt, cm start charging to maintain fully
505 * charged state for battery.
506 */
507static int check_charging_duration(struct charger_manager *cm)
508{
509 struct charger_desc *desc = cm->desc;
510 u64 curr = ktime_to_ms(ktime_get());
511 u64 duration;
512 int ret = false;
513
514 if (!desc->charging_max_duration_ms &&
515 !desc->discharging_max_duration_ms)
516 return ret;
517
518 if (cm->charger_enabled) {
519 duration = curr - cm->charging_start_time;
520
521 if (duration > desc->charging_max_duration_ms) {
522 dev_info(cm->dev, "Charging duration exceed %lldms",
523 desc->charging_max_duration_ms);
524 uevent_notify(cm, "Discharging");
525 try_charger_enable(cm, false);
526 ret = true;
527 }
528 } else if (is_ext_pwr_online(cm) && !cm->charger_enabled) {
529 duration = curr - cm->charging_end_time;
530
531 if (duration > desc->charging_max_duration_ms &&
532 is_ext_pwr_online(cm)) {
533 dev_info(cm->dev, "DisCharging duration exceed %lldms",
534 desc->discharging_max_duration_ms);
535 uevent_notify(cm, "Recharing");
536 try_charger_enable(cm, true);
537 ret = true;
538 }
539 }
540
541 return ret;
542}
543
544/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900545 * _cm_monitor - Monitor the temperature and return true for exceptions.
546 * @cm: the Charger Manager representing the battery.
547 *
548 * Returns true if there is an event to notify for the battery.
549 * (True if the status of "emergency_stop" changes)
550 */
551static bool _cm_monitor(struct charger_manager *cm)
552{
553 struct charger_desc *desc = cm->desc;
554 int temp = desc->temperature_out_of_range(&cm->last_temp_mC);
555
556 dev_dbg(cm->dev, "monitoring (%2.2d.%3.3dC)\n",
557 cm->last_temp_mC / 1000, cm->last_temp_mC % 1000);
558
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900559 /* It has been stopped already */
560 if (temp && cm->emergency_stop)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900561 return false;
562
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900563 /*
564 * Check temperature whether overheat or cold.
565 * If temperature is out of range normal state, stop charging.
566 */
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900567 if (temp) {
568 cm->emergency_stop = temp;
569 if (!try_charger_enable(cm, false)) {
570 if (temp > 0)
571 uevent_notify(cm, "OVERHEAT");
572 else
573 uevent_notify(cm, "COLD");
574 }
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900575
576 /*
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700577 * Check whole charging duration and discharing duration
578 * after full-batt.
579 */
580 } else if (!cm->emergency_stop && check_charging_duration(cm)) {
581 dev_dbg(cm->dev,
582 "Charging/Discharging duration is out of range");
583 /*
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900584 * Check dropped voltage of battery. If battery voltage is more
585 * dropped than fullbatt_vchkdrop_uV after fully charged state,
586 * charger-manager have to recharge battery.
587 */
588 } else if (!cm->emergency_stop && is_ext_pwr_online(cm) &&
589 !cm->charger_enabled) {
590 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
591
592 /*
593 * Check whether fully charged state to protect overcharge
594 * if charger-manager is charging for battery.
595 */
596 } else if (!cm->emergency_stop && is_full_charged(cm) &&
597 cm->charger_enabled) {
598 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged.\n");
599 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
600
601 try_charger_enable(cm, false);
602
603 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900604 } else {
605 cm->emergency_stop = 0;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900606 if (is_ext_pwr_online(cm)) {
607 if (!try_charger_enable(cm, true))
608 uevent_notify(cm, "CHARGING");
609 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900610 }
611
612 return true;
613}
614
615/**
616 * cm_monitor - Monitor every battery.
617 *
618 * Returns true if there is an event to notify from any of the batteries.
619 * (True if the status of "emergency_stop" changes)
620 */
621static bool cm_monitor(void)
622{
623 bool stop = false;
624 struct charger_manager *cm;
625
626 mutex_lock(&cm_list_mtx);
627
Axel Linbb2a95c2012-01-12 12:56:35 +0800628 list_for_each_entry(cm, &cm_list, entry) {
629 if (_cm_monitor(cm))
630 stop = true;
631 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900632
633 mutex_unlock(&cm_list_mtx);
634
635 return stop;
636}
637
Chanwoo Choid829dc72012-05-05 06:24:10 -0700638/**
639 * _setup_polling - Setup the next instance of polling.
640 * @work: work_struct of the function _setup_polling.
641 */
642static void _setup_polling(struct work_struct *work)
643{
644 unsigned long min = ULONG_MAX;
645 struct charger_manager *cm;
646 bool keep_polling = false;
647 unsigned long _next_polling;
648
649 mutex_lock(&cm_list_mtx);
650
651 list_for_each_entry(cm, &cm_list, entry) {
652 if (is_polling_required(cm) && cm->desc->polling_interval_ms) {
653 keep_polling = true;
654
655 if (min > cm->desc->polling_interval_ms)
656 min = cm->desc->polling_interval_ms;
657 }
658 }
659
660 polling_jiffy = msecs_to_jiffies(min);
661 if (polling_jiffy <= CM_JIFFIES_SMALL)
662 polling_jiffy = CM_JIFFIES_SMALL + 1;
663
664 if (!keep_polling)
665 polling_jiffy = ULONG_MAX;
666 if (polling_jiffy == ULONG_MAX)
667 goto out;
668
669 WARN(cm_wq == NULL, "charger-manager: workqueue not initialized"
670 ". try it later. %s\n", __func__);
671
672 _next_polling = jiffies + polling_jiffy;
673
674 if (!delayed_work_pending(&cm_monitor_work) ||
675 (delayed_work_pending(&cm_monitor_work) &&
676 time_after(next_polling, _next_polling))) {
Chanwoo Choid829dc72012-05-05 06:24:10 -0700677 next_polling = jiffies + polling_jiffy;
Tejun Heo41f63c52012-08-03 10:30:47 -0700678 mod_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy);
Chanwoo Choid829dc72012-05-05 06:24:10 -0700679 }
680
681out:
682 mutex_unlock(&cm_list_mtx);
683}
684static DECLARE_WORK(setup_polling, _setup_polling);
685
686/**
687 * cm_monitor_poller - The Monitor / Poller.
688 * @work: work_struct of the function cm_monitor_poller
689 *
690 * During non-suspended state, cm_monitor_poller is used to poll and monitor
691 * the batteries.
692 */
693static void cm_monitor_poller(struct work_struct *work)
694{
695 cm_monitor();
696 schedule_work(&setup_polling);
697}
698
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700699/**
700 * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL
701 * @cm: the Charger Manager representing the battery.
702 */
703static void fullbatt_handler(struct charger_manager *cm)
704{
705 struct charger_desc *desc = cm->desc;
706
707 if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
708 goto out;
709
710 if (cm_suspended)
711 device_set_wakeup_capable(cm->dev, true);
712
Tejun Heo41f63c52012-08-03 10:30:47 -0700713 mod_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
714 msecs_to_jiffies(desc->fullbatt_vchkdrop_ms));
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700715 cm->fullbatt_vchk_jiffies_at = jiffies + msecs_to_jiffies(
716 desc->fullbatt_vchkdrop_ms);
717
718 if (cm->fullbatt_vchk_jiffies_at == 0)
719 cm->fullbatt_vchk_jiffies_at = 1;
720
721out:
722 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged.\n");
723 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
724}
725
726/**
727 * battout_handler - Event handler for CM_EVENT_BATT_OUT
728 * @cm: the Charger Manager representing the battery.
729 */
730static void battout_handler(struct charger_manager *cm)
731{
732 if (cm_suspended)
733 device_set_wakeup_capable(cm->dev, true);
734
735 if (!is_batt_present(cm)) {
736 dev_emerg(cm->dev, "Battery Pulled Out!\n");
737 uevent_notify(cm, default_event_names[CM_EVENT_BATT_OUT]);
738 } else {
739 uevent_notify(cm, "Battery Reinserted?");
740 }
741}
742
743/**
744 * misc_event_handler - Handler for other evnets
745 * @cm: the Charger Manager representing the battery.
746 * @type: the Charger Manager representing the battery.
747 */
748static void misc_event_handler(struct charger_manager *cm,
749 enum cm_event_types type)
750{
751 if (cm_suspended)
752 device_set_wakeup_capable(cm->dev, true);
753
754 if (!delayed_work_pending(&cm_monitor_work) &&
755 is_polling_required(cm) && cm->desc->polling_interval_ms)
756 schedule_work(&setup_polling);
757 uevent_notify(cm, default_event_names[type]);
758}
759
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900760static int charger_get_property(struct power_supply *psy,
761 enum power_supply_property psp,
762 union power_supply_propval *val)
763{
764 struct charger_manager *cm = container_of(psy,
765 struct charger_manager, charger_psy);
766 struct charger_desc *desc = cm->desc;
Anton Vorontsovdf58c042012-03-15 21:01:28 +0400767 int ret = 0;
768 int uV;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900769
770 switch (psp) {
771 case POWER_SUPPLY_PROP_STATUS:
772 if (is_charging(cm))
773 val->intval = POWER_SUPPLY_STATUS_CHARGING;
774 else if (is_ext_pwr_online(cm))
775 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
776 else
777 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
778 break;
779 case POWER_SUPPLY_PROP_HEALTH:
780 if (cm->emergency_stop > 0)
781 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
782 else if (cm->emergency_stop < 0)
783 val->intval = POWER_SUPPLY_HEALTH_COLD;
784 else
785 val->intval = POWER_SUPPLY_HEALTH_GOOD;
786 break;
787 case POWER_SUPPLY_PROP_PRESENT:
788 if (is_batt_present(cm))
789 val->intval = 1;
790 else
791 val->intval = 0;
792 break;
793 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Anton Vorontsovdf58c042012-03-15 21:01:28 +0400794 ret = get_batt_uV(cm, &val->intval);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900795 break;
796 case POWER_SUPPLY_PROP_CURRENT_NOW:
797 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
798 POWER_SUPPLY_PROP_CURRENT_NOW, val);
799 break;
800 case POWER_SUPPLY_PROP_TEMP:
801 /* in thenth of centigrade */
802 if (cm->last_temp_mC == INT_MIN)
803 desc->temperature_out_of_range(&cm->last_temp_mC);
804 val->intval = cm->last_temp_mC / 100;
805 if (!desc->measure_battery_temp)
806 ret = -ENODEV;
807 break;
808 case POWER_SUPPLY_PROP_TEMP_AMBIENT:
809 /* in thenth of centigrade */
810 if (cm->last_temp_mC == INT_MIN)
811 desc->temperature_out_of_range(&cm->last_temp_mC);
812 val->intval = cm->last_temp_mC / 100;
813 if (desc->measure_battery_temp)
814 ret = -ENODEV;
815 break;
816 case POWER_SUPPLY_PROP_CAPACITY:
817 if (!cm->fuel_gauge) {
818 ret = -ENODEV;
819 break;
820 }
821
822 if (!is_batt_present(cm)) {
823 /* There is no battery. Assume 100% */
824 val->intval = 100;
825 break;
826 }
827
828 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
829 POWER_SUPPLY_PROP_CAPACITY, val);
830 if (ret)
831 break;
832
833 if (val->intval > 100) {
834 val->intval = 100;
835 break;
836 }
837 if (val->intval < 0)
838 val->intval = 0;
839
840 /* Do not adjust SOC when charging: voltage is overrated */
841 if (is_charging(cm))
842 break;
843
844 /*
845 * If the capacity value is inconsistent, calibrate it base on
846 * the battery voltage values and the thresholds given as desc
847 */
848 ret = get_batt_uV(cm, &uV);
849 if (ret) {
850 /* Voltage information not available. No calibration */
851 ret = 0;
852 break;
853 }
854
855 if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
856 !is_charging(cm)) {
857 val->intval = 100;
858 break;
859 }
860
861 break;
862 case POWER_SUPPLY_PROP_ONLINE:
863 if (is_ext_pwr_online(cm))
864 val->intval = 1;
865 else
866 val->intval = 0;
867 break;
868 case POWER_SUPPLY_PROP_CHARGE_FULL:
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900869 if (is_full_charged(cm))
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900870 val->intval = 1;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900871 else
872 val->intval = 0;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900873 ret = 0;
874 break;
875 case POWER_SUPPLY_PROP_CHARGE_NOW:
876 if (is_charging(cm)) {
877 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
878 POWER_SUPPLY_PROP_CHARGE_NOW,
879 val);
880 if (ret) {
881 val->intval = 1;
882 ret = 0;
883 } else {
884 /* If CHARGE_NOW is supplied, use it */
885 val->intval = (val->intval > 0) ?
886 val->intval : 1;
887 }
888 } else {
889 val->intval = 0;
890 }
891 break;
892 default:
893 return -EINVAL;
894 }
895 return ret;
896}
897
898#define NUM_CHARGER_PSY_OPTIONAL (4)
899static enum power_supply_property default_charger_props[] = {
900 /* Guaranteed to provide */
901 POWER_SUPPLY_PROP_STATUS,
902 POWER_SUPPLY_PROP_HEALTH,
903 POWER_SUPPLY_PROP_PRESENT,
904 POWER_SUPPLY_PROP_VOLTAGE_NOW,
905 POWER_SUPPLY_PROP_CAPACITY,
906 POWER_SUPPLY_PROP_ONLINE,
907 POWER_SUPPLY_PROP_CHARGE_FULL,
908 /*
909 * Optional properties are:
910 * POWER_SUPPLY_PROP_CHARGE_NOW,
911 * POWER_SUPPLY_PROP_CURRENT_NOW,
912 * POWER_SUPPLY_PROP_TEMP, and
913 * POWER_SUPPLY_PROP_TEMP_AMBIENT,
914 */
915};
916
917static struct power_supply psy_default = {
918 .name = "battery",
919 .type = POWER_SUPPLY_TYPE_BATTERY,
920 .properties = default_charger_props,
921 .num_properties = ARRAY_SIZE(default_charger_props),
922 .get_property = charger_get_property,
923};
924
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900925/**
926 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
927 * for suspend_again.
928 *
929 * Returns true if the alarm is set for Charger Manager to use.
930 * Returns false if
931 * cm_setup_timer fails to set an alarm,
932 * cm_setup_timer does not need to set an alarm for Charger Manager,
933 * or an alarm previously configured is to be used.
934 */
935static bool cm_setup_timer(void)
936{
937 struct charger_manager *cm;
938 unsigned int wakeup_ms = UINT_MAX;
939 bool ret = false;
940
941 mutex_lock(&cm_list_mtx);
942
943 list_for_each_entry(cm, &cm_list, entry) {
Chanwoo Choid829dc72012-05-05 06:24:10 -0700944 unsigned int fbchk_ms = 0;
945
946 /* fullbatt_vchk is required. setup timer for that */
947 if (cm->fullbatt_vchk_jiffies_at) {
948 fbchk_ms = jiffies_to_msecs(cm->fullbatt_vchk_jiffies_at
949 - jiffies);
950 if (time_is_before_eq_jiffies(
951 cm->fullbatt_vchk_jiffies_at) ||
952 msecs_to_jiffies(fbchk_ms) < CM_JIFFIES_SMALL) {
953 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
954 fbchk_ms = 0;
955 }
956 }
957 CM_MIN_VALID(wakeup_ms, fbchk_ms);
958
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900959 /* Skip if polling is not required for this CM */
960 if (!is_polling_required(cm) && !cm->emergency_stop)
961 continue;
962 if (cm->desc->polling_interval_ms == 0)
963 continue;
964 CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
965 }
966
967 mutex_unlock(&cm_list_mtx);
968
969 if (wakeup_ms < UINT_MAX && wakeup_ms > 0) {
970 pr_info("Charger Manager wakeup timer: %u ms.\n", wakeup_ms);
971 if (rtc_dev) {
972 struct rtc_wkalrm tmp;
973 unsigned long time, now;
974 unsigned long add = DIV_ROUND_UP(wakeup_ms, 1000);
975
976 /*
977 * Set alarm with the polling interval (wakeup_ms)
978 * except when rtc_wkalarm_save comes first.
979 * However, the alarm time should be NOW +
980 * CM_RTC_SMALL or later.
981 */
982 tmp.enabled = 1;
983 rtc_read_time(rtc_dev, &tmp.time);
984 rtc_tm_to_time(&tmp.time, &now);
985 if (add < CM_RTC_SMALL)
986 add = CM_RTC_SMALL;
987 time = now + add;
988
989 ret = true;
990
991 if (rtc_wkalarm_save.enabled &&
992 rtc_wkalarm_save_time &&
993 rtc_wkalarm_save_time < time) {
994 if (rtc_wkalarm_save_time < now + CM_RTC_SMALL)
995 time = now + CM_RTC_SMALL;
996 else
997 time = rtc_wkalarm_save_time;
998
999 /* The timer is not appointed by CM */
1000 ret = false;
1001 }
1002
1003 pr_info("Waking up after %lu secs.\n",
1004 time - now);
1005
1006 rtc_time_to_tm(time, &tmp.time);
1007 rtc_set_alarm(rtc_dev, &tmp);
1008 cm_suspend_duration_ms += wakeup_ms;
1009 return ret;
1010 }
1011 }
1012
1013 if (rtc_dev)
1014 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1015 return false;
1016}
1017
Chanwoo Choid829dc72012-05-05 06:24:10 -07001018static void _cm_fbchk_in_suspend(struct charger_manager *cm)
1019{
1020 unsigned long jiffy_now = jiffies;
1021
1022 if (!cm->fullbatt_vchk_jiffies_at)
1023 return;
1024
1025 if (g_desc && g_desc->assume_timer_stops_in_suspend)
1026 jiffy_now += msecs_to_jiffies(cm_suspend_duration_ms);
1027
1028 /* Execute now if it's going to be executed not too long after */
1029 jiffy_now += CM_JIFFIES_SMALL;
1030
1031 if (time_after_eq(jiffy_now, cm->fullbatt_vchk_jiffies_at))
1032 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
1033}
1034
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001035/**
1036 * cm_suspend_again - Determine whether suspend again or not
1037 *
1038 * Returns true if the system should be suspended again
1039 * Returns false if the system should be woken up
1040 */
1041bool cm_suspend_again(void)
1042{
1043 struct charger_manager *cm;
1044 bool ret = false;
1045
1046 if (!g_desc || !g_desc->rtc_only_wakeup || !g_desc->rtc_only_wakeup() ||
1047 !cm_rtc_set)
1048 return false;
1049
1050 if (cm_monitor())
1051 goto out;
1052
1053 ret = true;
1054 mutex_lock(&cm_list_mtx);
1055 list_for_each_entry(cm, &cm_list, entry) {
Chanwoo Choid829dc72012-05-05 06:24:10 -07001056 _cm_fbchk_in_suspend(cm);
1057
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001058 if (cm->status_save_ext_pwr_inserted != is_ext_pwr_online(cm) ||
Axel Linbb2a95c2012-01-12 12:56:35 +08001059 cm->status_save_batt != is_batt_present(cm)) {
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001060 ret = false;
Axel Linbb2a95c2012-01-12 12:56:35 +08001061 break;
1062 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001063 }
1064 mutex_unlock(&cm_list_mtx);
1065
1066 cm_rtc_set = cm_setup_timer();
1067out:
1068 /* It's about the time when the non-CM appointed timer goes off */
1069 if (rtc_wkalarm_save.enabled) {
1070 unsigned long now;
1071 struct rtc_time tmp;
1072
1073 rtc_read_time(rtc_dev, &tmp);
1074 rtc_tm_to_time(&tmp, &now);
1075
1076 if (rtc_wkalarm_save_time &&
1077 now + CM_RTC_SMALL >= rtc_wkalarm_save_time)
1078 return false;
1079 }
1080 return ret;
1081}
1082EXPORT_SYMBOL_GPL(cm_suspend_again);
1083
1084/**
1085 * setup_charger_manager - initialize charger_global_desc data
1086 * @gd: pointer to instance of charger_global_desc
1087 */
1088int setup_charger_manager(struct charger_global_desc *gd)
1089{
1090 if (!gd)
1091 return -EINVAL;
1092
1093 if (rtc_dev)
1094 rtc_class_close(rtc_dev);
1095 rtc_dev = NULL;
1096 g_desc = NULL;
1097
1098 if (!gd->rtc_only_wakeup) {
1099 pr_err("The callback rtc_only_wakeup is not given.\n");
1100 return -EINVAL;
1101 }
1102
1103 if (gd->rtc_name) {
1104 rtc_dev = rtc_class_open(gd->rtc_name);
1105 if (IS_ERR_OR_NULL(rtc_dev)) {
1106 rtc_dev = NULL;
1107 /* Retry at probe. RTC may be not registered yet */
1108 }
1109 } else {
1110 pr_warn("No wakeup timer is given for charger manager."
1111 "In-suspend monitoring won't work.\n");
1112 }
1113
1114 g_desc = gd;
1115 return 0;
1116}
1117EXPORT_SYMBOL_GPL(setup_charger_manager);
1118
Chanwoo Choibee737b2012-07-12 15:03:25 +09001119/**
1120 * charger_extcon_work - enable/diable charger according to the state
1121 * of charger cable
1122 *
1123 * @work: work_struct of the function charger_extcon_work.
1124 */
1125static void charger_extcon_work(struct work_struct *work)
1126{
1127 struct charger_cable *cable =
1128 container_of(work, struct charger_cable, wq);
Chanwoo Choi45cd4fb2012-07-12 15:03:29 +09001129 int ret;
1130
1131 if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) {
1132 ret = regulator_set_current_limit(cable->charger->consumer,
1133 cable->min_uA, cable->max_uA);
1134 if (ret < 0) {
1135 pr_err("Cannot set current limit of %s (%s)\n",
1136 cable->charger->regulator_name, cable->name);
1137 return;
1138 }
1139
1140 pr_info("Set current limit of %s : %duA ~ %duA\n",
1141 cable->charger->regulator_name,
1142 cable->min_uA, cable->max_uA);
1143 }
Chanwoo Choibee737b2012-07-12 15:03:25 +09001144
1145 try_charger_enable(cable->cm, cable->attached);
1146}
1147
1148/**
1149 * charger_extcon_notifier - receive the state of charger cable
1150 * when registered cable is attached or detached.
1151 *
1152 * @self: the notifier block of the charger_extcon_notifier.
1153 * @event: the cable state.
1154 * @ptr: the data pointer of notifier block.
1155 */
1156static int charger_extcon_notifier(struct notifier_block *self,
1157 unsigned long event, void *ptr)
1158{
1159 struct charger_cable *cable =
1160 container_of(self, struct charger_cable, nb);
1161
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001162 /*
1163 * The newly state of charger cable.
1164 * If cable is attached, cable->attached is true.
1165 */
Chanwoo Choibee737b2012-07-12 15:03:25 +09001166 cable->attached = event;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001167
1168 /*
1169 * Setup monitoring to check battery state
1170 * when charger cable is attached.
1171 */
1172 if (cable->attached && is_polling_required(cable->cm)) {
1173 if (work_pending(&setup_polling))
1174 cancel_work_sync(&setup_polling);
1175 schedule_work(&setup_polling);
1176 }
1177
1178 /*
1179 * Setup work for controlling charger(regulator)
1180 * according to charger cable.
1181 */
Chanwoo Choibee737b2012-07-12 15:03:25 +09001182 schedule_work(&cable->wq);
1183
1184 return NOTIFY_DONE;
1185}
1186
1187/**
1188 * charger_extcon_init - register external connector to use it
1189 * as the charger cable
1190 *
1191 * @cm: the Charger Manager representing the battery.
1192 * @cable: the Charger cable representing the external connector.
1193 */
1194static int charger_extcon_init(struct charger_manager *cm,
1195 struct charger_cable *cable)
1196{
1197 int ret = 0;
1198
1199 /*
1200 * Charger manager use Extcon framework to identify
1201 * the charger cable among various external connector
1202 * cable (e.g., TA, USB, MHL, Dock).
1203 */
1204 INIT_WORK(&cable->wq, charger_extcon_work);
1205 cable->nb.notifier_call = charger_extcon_notifier;
1206 ret = extcon_register_interest(&cable->extcon_dev,
1207 cable->extcon_name, cable->name, &cable->nb);
1208 if (ret < 0) {
1209 pr_info("Cannot register extcon_dev for %s(cable: %s).\n",
1210 cable->extcon_name,
1211 cable->name);
1212 ret = -EINVAL;
1213 }
1214
1215 return ret;
1216}
1217
Chanwoo Choi41468a12012-11-22 16:54:26 +09001218/**
1219 * charger_manager_register_extcon - Register extcon device to recevie state
1220 * of charger cable.
1221 * @cm: the Charger Manager representing the battery.
1222 *
1223 * This function support EXTCON(External Connector) subsystem to detect the
1224 * state of charger cables for enabling or disabling charger(regulator) and
1225 * select the charger cable for charging among a number of external cable
1226 * according to policy of H/W board.
1227 */
1228static int charger_manager_register_extcon(struct charger_manager *cm)
1229{
1230 struct charger_desc *desc = cm->desc;
1231 struct charger_regulator *charger;
1232 int ret = 0;
1233 int i;
1234 int j;
1235
1236 for (i = 0; i < desc->num_charger_regulators; i++) {
1237 charger = &desc->charger_regulators[i];
1238
1239 charger->consumer = regulator_get(cm->dev,
1240 charger->regulator_name);
1241 if (charger->consumer == NULL) {
1242 dev_err(cm->dev, "Cannot find charger(%s)n",
1243 charger->regulator_name);
1244 ret = -EINVAL;
1245 goto err;
1246 }
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) {
1254 dev_err(cm->dev, "Cannot initialize charger(%s)n",
1255 charger->regulator_name);
1256 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,
1346 "'%s' regulator should be controlled "
1347 "in charger-manager because charger-manager "
1348 "must need at least one charger for charging\n",
1349 charger->regulator_name);
1350 }
1351
1352 return count;
1353}
1354
Chanwoo Choi41468a12012-11-22 16:54:26 +09001355/**
1356 * charger_manager_register_sysfs - Register sysfs entry for each charger
1357 * @cm: the Charger Manager representing the battery.
1358 *
1359 * This function add sysfs entry for charger(regulator) to control charger from
1360 * user-space. If some development board use one more chargers for charging
1361 * but only need one charger on specific case which is dependent on user
1362 * scenario or hardware restrictions, the user enter 1 or 0(zero) to '/sys/
1363 * class/power_supply/battery/charger.[index]/externally_control'. For example,
1364 * if user enter 1 to 'sys/class/power_supply/battery/charger.[index]/
1365 * externally_control, this charger isn't controlled from charger-manager and
1366 * always stay off state of regulator.
1367 */
1368static int charger_manager_register_sysfs(struct charger_manager *cm)
1369{
1370 struct charger_desc *desc = cm->desc;
1371 struct charger_regulator *charger;
1372 int chargers_externally_control = 1;
1373 char buf[11];
1374 char *str;
1375 int ret = 0;
1376 int i;
1377
1378 /* Create sysfs entry to control charger(regulator) */
1379 for (i = 0; i < desc->num_charger_regulators; i++) {
1380 charger = &desc->charger_regulators[i];
1381
1382 snprintf(buf, 10, "charger.%d", i);
1383 str = kzalloc(sizeof(char) * (strlen(buf) + 1), GFP_KERNEL);
1384 if (!str) {
1385 dev_err(cm->dev, "Cannot allocate memory: %s\n",
1386 charger->regulator_name);
1387 ret = -ENOMEM;
1388 goto err;
1389 }
1390 strcpy(str, buf);
1391
1392 charger->attrs[0] = &charger->attr_name.attr;
1393 charger->attrs[1] = &charger->attr_state.attr;
1394 charger->attrs[2] = &charger->attr_externally_control.attr;
1395 charger->attrs[3] = NULL;
1396 charger->attr_g.name = str;
1397 charger->attr_g.attrs = charger->attrs;
1398
1399 sysfs_attr_init(&charger->attr_name.attr);
1400 charger->attr_name.attr.name = "name";
1401 charger->attr_name.attr.mode = 0444;
1402 charger->attr_name.show = charger_name_show;
1403
1404 sysfs_attr_init(&charger->attr_state.attr);
1405 charger->attr_state.attr.name = "state";
1406 charger->attr_state.attr.mode = 0444;
1407 charger->attr_state.show = charger_state_show;
1408
1409 sysfs_attr_init(&charger->attr_externally_control.attr);
1410 charger->attr_externally_control.attr.name
1411 = "externally_control";
1412 charger->attr_externally_control.attr.mode = 0644;
1413 charger->attr_externally_control.show
1414 = charger_externally_control_show;
1415 charger->attr_externally_control.store
1416 = charger_externally_control_store;
1417
1418 if (!desc->charger_regulators[i].externally_control ||
1419 !chargers_externally_control)
1420 chargers_externally_control = 0;
1421
1422 dev_info(cm->dev, "'%s' regulator's externally_control"
1423 "is %d\n", charger->regulator_name,
1424 charger->externally_control);
1425
1426 ret = sysfs_create_group(&cm->charger_psy.dev->kobj,
1427 &charger->attr_g);
1428 if (ret < 0) {
1429 dev_err(cm->dev, "Cannot create sysfs entry"
1430 "of %s regulator\n",
1431 charger->regulator_name);
1432 ret = -EINVAL;
1433 goto err;
1434 }
1435 }
1436
1437 if (chargers_externally_control) {
1438 dev_err(cm->dev, "Cannot register regulator because "
1439 "charger-manager must need at least "
1440 "one charger for charging battery\n");
1441
1442 ret = -EINVAL;
1443 goto err;
1444 }
1445
1446err:
1447 return ret;
1448}
1449
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001450static int charger_manager_probe(struct platform_device *pdev)
1451{
1452 struct charger_desc *desc = dev_get_platdata(&pdev->dev);
1453 struct charger_manager *cm;
1454 int ret = 0, i = 0;
Chanwoo Choibee737b2012-07-12 15:03:25 +09001455 int j = 0;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001456 union power_supply_propval val;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001457
1458 if (g_desc && !rtc_dev && g_desc->rtc_name) {
1459 rtc_dev = rtc_class_open(g_desc->rtc_name);
1460 if (IS_ERR_OR_NULL(rtc_dev)) {
1461 rtc_dev = NULL;
1462 dev_err(&pdev->dev, "Cannot get RTC %s.\n",
1463 g_desc->rtc_name);
1464 ret = -ENODEV;
1465 goto err_alloc;
1466 }
1467 }
1468
1469 if (!desc) {
1470 dev_err(&pdev->dev, "No platform data (desc) found.\n");
1471 ret = -ENODEV;
1472 goto err_alloc;
1473 }
1474
1475 cm = kzalloc(sizeof(struct charger_manager), GFP_KERNEL);
1476 if (!cm) {
1477 dev_err(&pdev->dev, "Cannot allocate memory.\n");
1478 ret = -ENOMEM;
1479 goto err_alloc;
1480 }
1481
1482 /* Basic Values. Unspecified are Null or 0 */
1483 cm->dev = &pdev->dev;
1484 cm->desc = kzalloc(sizeof(struct charger_desc), GFP_KERNEL);
1485 if (!cm->desc) {
1486 dev_err(&pdev->dev, "Cannot allocate memory.\n");
1487 ret = -ENOMEM;
1488 goto err_alloc_desc;
1489 }
1490 memcpy(cm->desc, desc, sizeof(struct charger_desc));
1491 cm->last_temp_mC = INT_MIN; /* denotes "unmeasured, yet" */
1492
Chanwoo Choid829dc72012-05-05 06:24:10 -07001493 /*
1494 * The following two do not need to be errors.
1495 * Users may intentionally ignore those two features.
1496 */
1497 if (desc->fullbatt_uV == 0) {
1498 dev_info(&pdev->dev, "Ignoring full-battery voltage threshold"
1499 " as it is not supplied.");
1500 }
1501 if (!desc->fullbatt_vchkdrop_ms || !desc->fullbatt_vchkdrop_uV) {
1502 dev_info(&pdev->dev, "Disabling full-battery voltage drop "
1503 "checking mechanism as it is not supplied.");
1504 desc->fullbatt_vchkdrop_ms = 0;
1505 desc->fullbatt_vchkdrop_uV = 0;
1506 }
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001507 if (desc->fullbatt_soc == 0) {
1508 dev_info(&pdev->dev, "Ignoring full-battery soc(state of"
1509 " charge) threshold as it is not"
1510 " supplied.");
1511 }
1512 if (desc->fullbatt_full_capacity == 0) {
1513 dev_info(&pdev->dev, "Ignoring full-battery full capacity"
1514 " threshold as it is not supplied.");
1515 }
Chanwoo Choid829dc72012-05-05 06:24:10 -07001516
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001517 if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
1518 ret = -EINVAL;
1519 dev_err(&pdev->dev, "charger_regulators undefined.\n");
1520 goto err_no_charger;
1521 }
1522
1523 if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
1524 dev_err(&pdev->dev, "No power supply defined.\n");
1525 ret = -EINVAL;
1526 goto err_no_charger_stat;
1527 }
1528
1529 /* Counting index only */
1530 while (desc->psy_charger_stat[i])
1531 i++;
1532
1533 cm->charger_stat = kzalloc(sizeof(struct power_supply *) * (i + 1),
1534 GFP_KERNEL);
1535 if (!cm->charger_stat) {
1536 ret = -ENOMEM;
1537 goto err_no_charger_stat;
1538 }
1539
1540 for (i = 0; desc->psy_charger_stat[i]; i++) {
1541 cm->charger_stat[i] = power_supply_get_by_name(
1542 desc->psy_charger_stat[i]);
1543 if (!cm->charger_stat[i]) {
1544 dev_err(&pdev->dev, "Cannot find power supply "
1545 "\"%s\"\n",
1546 desc->psy_charger_stat[i]);
1547 ret = -ENODEV;
1548 goto err_chg_stat;
1549 }
1550 }
1551
1552 cm->fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
1553 if (!cm->fuel_gauge) {
1554 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
1555 desc->psy_fuel_gauge);
1556 ret = -ENODEV;
1557 goto err_chg_stat;
1558 }
1559
1560 if (desc->polling_interval_ms == 0 ||
1561 msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) {
1562 dev_err(&pdev->dev, "polling_interval_ms is too small\n");
1563 ret = -EINVAL;
1564 goto err_chg_stat;
1565 }
1566
1567 if (!desc->temperature_out_of_range) {
1568 dev_err(&pdev->dev, "there is no temperature_out_of_range\n");
1569 ret = -EINVAL;
1570 goto err_chg_stat;
1571 }
1572
Chanwoo Choi8fcfe082012-09-20 21:20:05 -07001573 if (!desc->charging_max_duration_ms ||
1574 !desc->discharging_max_duration_ms) {
1575 dev_info(&pdev->dev, "Cannot limit charging duration "
1576 "checking mechanism to prevent overcharge/overheat "
1577 "and control discharging duration");
1578 desc->charging_max_duration_ms = 0;
1579 desc->discharging_max_duration_ms = 0;
1580 }
1581
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001582 platform_set_drvdata(pdev, cm);
1583
Axel Linbb2a95c2012-01-12 12:56:35 +08001584 memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default));
1585
Chanwoo Choi41468a12012-11-22 16:54:26 +09001586 if (!desc->psy_name)
Axel Linbb2a95c2012-01-12 12:56:35 +08001587 strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001588 else
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001589 strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001590 cm->charger_psy.name = cm->psy_name_buf;
1591
1592 /* Allocate for psy properties because they may vary */
1593 cm->charger_psy.properties = kzalloc(sizeof(enum power_supply_property)
1594 * (ARRAY_SIZE(default_charger_props) +
1595 NUM_CHARGER_PSY_OPTIONAL),
1596 GFP_KERNEL);
1597 if (!cm->charger_psy.properties) {
1598 dev_err(&pdev->dev, "Cannot allocate for psy properties.\n");
1599 ret = -ENOMEM;
1600 goto err_chg_stat;
1601 }
1602 memcpy(cm->charger_psy.properties, default_charger_props,
1603 sizeof(enum power_supply_property) *
1604 ARRAY_SIZE(default_charger_props));
1605 cm->charger_psy.num_properties = psy_default.num_properties;
1606
1607 /* Find which optional psy-properties are available */
1608 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1609 POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
1610 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1611 POWER_SUPPLY_PROP_CHARGE_NOW;
1612 cm->charger_psy.num_properties++;
1613 }
1614 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1615 POWER_SUPPLY_PROP_CURRENT_NOW,
1616 &val)) {
1617 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1618 POWER_SUPPLY_PROP_CURRENT_NOW;
1619 cm->charger_psy.num_properties++;
1620 }
Axel Linbb2a95c2012-01-12 12:56:35 +08001621
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001622 if (desc->measure_battery_temp) {
1623 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1624 POWER_SUPPLY_PROP_TEMP;
1625 cm->charger_psy.num_properties++;
Axel Linbb2a95c2012-01-12 12:56:35 +08001626 } else {
1627 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1628 POWER_SUPPLY_PROP_TEMP_AMBIENT;
1629 cm->charger_psy.num_properties++;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001630 }
1631
Chanwoo Choid829dc72012-05-05 06:24:10 -07001632 INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk);
1633
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001634 ret = power_supply_register(NULL, &cm->charger_psy);
1635 if (ret) {
1636 dev_err(&pdev->dev, "Cannot register charger-manager with"
1637 " name \"%s\".\n", cm->charger_psy.name);
1638 goto err_register;
1639 }
1640
Chanwoo Choi41468a12012-11-22 16:54:26 +09001641 /* Register extcon device for charger cable */
1642 ret = charger_manager_register_extcon(cm);
1643 if (ret < 0) {
1644 dev_err(&pdev->dev, "Cannot initialize extcon device\n");
1645 goto err_reg_extcon;
Chanwoo Choi3950c782012-09-21 18:49:37 +09001646 }
1647
Chanwoo Choi41468a12012-11-22 16:54:26 +09001648 /* Register sysfs entry for charger(regulator) */
1649 ret = charger_manager_register_sysfs(cm);
1650 if (ret < 0) {
1651 dev_err(&pdev->dev,
1652 "Cannot initialize sysfs entry of regulator\n");
1653 goto err_reg_sysfs;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001654 }
1655
1656 /* Add to the list */
1657 mutex_lock(&cm_list_mtx);
1658 list_add(&cm->entry, &cm_list);
1659 mutex_unlock(&cm_list_mtx);
1660
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001661 /*
1662 * Charger-manager is capable of waking up the systme from sleep
1663 * when event is happend through cm_notify_event()
1664 */
1665 device_init_wakeup(&pdev->dev, true);
1666 device_set_wakeup_capable(&pdev->dev, false);
1667
Chanwoo Choid829dc72012-05-05 06:24:10 -07001668 schedule_work(&setup_polling);
1669
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001670 return 0;
1671
Chanwoo Choi41468a12012-11-22 16:54:26 +09001672err_reg_sysfs:
Chanwoo Choi3950c782012-09-21 18:49:37 +09001673 for (i = 0; i < desc->num_charger_regulators; i++) {
1674 struct charger_regulator *charger;
1675
1676 charger = &desc->charger_regulators[i];
1677 sysfs_remove_group(&cm->charger_psy.dev->kobj,
1678 &charger->attr_g);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001679
Chanwoo Choi3950c782012-09-21 18:49:37 +09001680 kfree(charger->attr_g.name);
1681 }
Chanwoo Choi41468a12012-11-22 16:54:26 +09001682err_reg_extcon:
1683 for (i = 0; i < desc->num_charger_regulators; i++) {
1684 struct charger_regulator *charger;
1685
1686 charger = &desc->charger_regulators[i];
1687 for (j = 0; j < charger->num_cables; j++) {
Chanwoo Choibee737b2012-07-12 15:03:25 +09001688 struct charger_cable *cable = &charger->cables[j];
1689 extcon_unregister_interest(&cable->extcon_dev);
1690 }
Chanwoo Choi41468a12012-11-22 16:54:26 +09001691
Chanwoo Choibee737b2012-07-12 15:03:25 +09001692 regulator_put(desc->charger_regulators[i].consumer);
Chanwoo Choi41468a12012-11-22 16:54:26 +09001693 }
Chanwoo Choibee737b2012-07-12 15:03:25 +09001694
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001695 power_supply_unregister(&cm->charger_psy);
1696err_register:
1697 kfree(cm->charger_psy.properties);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001698err_chg_stat:
1699 kfree(cm->charger_stat);
1700err_no_charger_stat:
1701err_no_charger:
1702 kfree(cm->desc);
1703err_alloc_desc:
1704 kfree(cm);
1705err_alloc:
1706 return ret;
1707}
1708
Bill Pemberton415ec692012-11-19 13:26:07 -05001709static int charger_manager_remove(struct platform_device *pdev)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001710{
1711 struct charger_manager *cm = platform_get_drvdata(pdev);
1712 struct charger_desc *desc = cm->desc;
Chanwoo Choibee737b2012-07-12 15:03:25 +09001713 int i = 0;
1714 int j = 0;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001715
1716 /* Remove from the list */
1717 mutex_lock(&cm_list_mtx);
1718 list_del(&cm->entry);
1719 mutex_unlock(&cm_list_mtx);
1720
Chanwoo Choid829dc72012-05-05 06:24:10 -07001721 if (work_pending(&setup_polling))
1722 cancel_work_sync(&setup_polling);
1723 if (delayed_work_pending(&cm_monitor_work))
1724 cancel_delayed_work_sync(&cm_monitor_work);
1725
Chanwoo Choibee737b2012-07-12 15:03:25 +09001726 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1727 struct charger_regulator *charger
1728 = &desc->charger_regulators[i];
1729 for (j = 0 ; j < charger->num_cables ; j++) {
1730 struct charger_cable *cable = &charger->cables[j];
1731 extcon_unregister_interest(&cable->extcon_dev);
1732 }
1733 }
1734
1735 for (i = 0 ; i < desc->num_charger_regulators ; i++)
1736 regulator_put(desc->charger_regulators[i].consumer);
1737
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001738 power_supply_unregister(&cm->charger_psy);
Chanwoo Choid829dc72012-05-05 06:24:10 -07001739
1740 try_charger_enable(cm, false);
1741
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001742 kfree(cm->charger_psy.properties);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001743 kfree(cm->charger_stat);
1744 kfree(cm->desc);
1745 kfree(cm);
1746
1747 return 0;
1748}
1749
Axel Lin1bbe24d2012-01-11 17:19:45 +08001750static const struct platform_device_id charger_manager_id[] = {
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001751 { "charger-manager", 0 },
1752 { },
1753};
Axel Lin1bbe24d2012-01-11 17:19:45 +08001754MODULE_DEVICE_TABLE(platform, charger_manager_id);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001755
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001756static int cm_suspend_noirq(struct device *dev)
1757{
1758 int ret = 0;
1759
1760 if (device_may_wakeup(dev)) {
1761 device_set_wakeup_capable(dev, false);
1762 ret = -EAGAIN;
1763 }
1764
1765 return ret;
1766}
1767
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001768static int cm_suspend_prepare(struct device *dev)
1769{
Axel Linbb2a95c2012-01-12 12:56:35 +08001770 struct charger_manager *cm = dev_get_drvdata(dev);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001771
1772 if (!cm_suspended) {
1773 if (rtc_dev) {
1774 struct rtc_time tmp;
1775 unsigned long now;
1776
1777 rtc_read_alarm(rtc_dev, &rtc_wkalarm_save);
1778 rtc_read_time(rtc_dev, &tmp);
1779
1780 if (rtc_wkalarm_save.enabled) {
1781 rtc_tm_to_time(&rtc_wkalarm_save.time,
1782 &rtc_wkalarm_save_time);
1783 rtc_tm_to_time(&tmp, &now);
1784 if (now > rtc_wkalarm_save_time)
1785 rtc_wkalarm_save_time = 0;
1786 } else {
1787 rtc_wkalarm_save_time = 0;
1788 }
1789 }
1790 cm_suspended = true;
1791 }
1792
Chanwoo Choid829dc72012-05-05 06:24:10 -07001793 if (delayed_work_pending(&cm->fullbatt_vchk_work))
1794 cancel_delayed_work(&cm->fullbatt_vchk_work);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001795 cm->status_save_ext_pwr_inserted = is_ext_pwr_online(cm);
1796 cm->status_save_batt = is_batt_present(cm);
1797
1798 if (!cm_rtc_set) {
1799 cm_suspend_duration_ms = 0;
1800 cm_rtc_set = cm_setup_timer();
1801 }
1802
1803 return 0;
1804}
1805
1806static void cm_suspend_complete(struct device *dev)
1807{
Axel Linbb2a95c2012-01-12 12:56:35 +08001808 struct charger_manager *cm = dev_get_drvdata(dev);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001809
1810 if (cm_suspended) {
1811 if (rtc_dev) {
1812 struct rtc_wkalrm tmp;
1813
1814 rtc_read_alarm(rtc_dev, &tmp);
1815 rtc_wkalarm_save.pending = tmp.pending;
1816 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1817 }
1818 cm_suspended = false;
1819 cm_rtc_set = false;
1820 }
1821
Chanwoo Choid829dc72012-05-05 06:24:10 -07001822 /* Re-enqueue delayed work (fullbatt_vchk_work) */
1823 if (cm->fullbatt_vchk_jiffies_at) {
1824 unsigned long delay = 0;
1825 unsigned long now = jiffies + CM_JIFFIES_SMALL;
1826
1827 if (time_after_eq(now, cm->fullbatt_vchk_jiffies_at)) {
1828 delay = (unsigned long)((long)now
1829 - (long)(cm->fullbatt_vchk_jiffies_at));
1830 delay = jiffies_to_msecs(delay);
1831 } else {
1832 delay = 0;
1833 }
1834
1835 /*
1836 * Account for cm_suspend_duration_ms if
1837 * assume_timer_stops_in_suspend is active
1838 */
1839 if (g_desc && g_desc->assume_timer_stops_in_suspend) {
1840 if (delay > cm_suspend_duration_ms)
1841 delay -= cm_suspend_duration_ms;
1842 else
1843 delay = 0;
1844 }
1845
1846 queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
1847 msecs_to_jiffies(delay));
1848 }
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001849 device_set_wakeup_capable(cm->dev, false);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001850 uevent_notify(cm, NULL);
1851}
1852
1853static const struct dev_pm_ops charger_manager_pm = {
1854 .prepare = cm_suspend_prepare,
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001855 .suspend_noirq = cm_suspend_noirq,
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001856 .complete = cm_suspend_complete,
1857};
1858
1859static struct platform_driver charger_manager_driver = {
1860 .driver = {
1861 .name = "charger-manager",
1862 .owner = THIS_MODULE,
1863 .pm = &charger_manager_pm,
1864 },
1865 .probe = charger_manager_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -05001866 .remove = charger_manager_remove,
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001867 .id_table = charger_manager_id,
1868};
1869
1870static int __init charger_manager_init(void)
1871{
Chanwoo Choid829dc72012-05-05 06:24:10 -07001872 cm_wq = create_freezable_workqueue("charger_manager");
1873 INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller);
1874
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001875 return platform_driver_register(&charger_manager_driver);
1876}
1877late_initcall(charger_manager_init);
1878
1879static void __exit charger_manager_cleanup(void)
1880{
Chanwoo Choid829dc72012-05-05 06:24:10 -07001881 destroy_workqueue(cm_wq);
1882 cm_wq = NULL;
1883
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001884 platform_driver_unregister(&charger_manager_driver);
1885}
1886module_exit(charger_manager_cleanup);
1887
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001888/**
1889 * find_power_supply - find the associated power_supply of charger
1890 * @cm: the Charger Manager representing the battery
1891 * @psy: pointer to instance of charger's power_supply
1892 */
1893static bool find_power_supply(struct charger_manager *cm,
1894 struct power_supply *psy)
1895{
1896 int i;
1897 bool found = false;
1898
1899 for (i = 0; cm->charger_stat[i]; i++) {
1900 if (psy == cm->charger_stat[i]) {
1901 found = true;
1902 break;
1903 }
1904 }
1905
1906 return found;
1907}
1908
1909/**
1910 * cm_notify_event - charger driver notify Charger Manager of charger event
1911 * @psy: pointer to instance of charger's power_supply
1912 * @type: type of charger event
1913 * @msg: optional message passed to uevent_notify fuction
1914 */
1915void cm_notify_event(struct power_supply *psy, enum cm_event_types type,
1916 char *msg)
1917{
1918 struct charger_manager *cm;
1919 bool found_power_supply = false;
1920
1921 if (psy == NULL)
1922 return;
1923
1924 mutex_lock(&cm_list_mtx);
1925 list_for_each_entry(cm, &cm_list, entry) {
1926 found_power_supply = find_power_supply(cm, psy);
1927 if (found_power_supply)
1928 break;
1929 }
1930 mutex_unlock(&cm_list_mtx);
1931
1932 if (!found_power_supply)
1933 return;
1934
1935 switch (type) {
1936 case CM_EVENT_BATT_FULL:
1937 fullbatt_handler(cm);
1938 break;
1939 case CM_EVENT_BATT_OUT:
1940 battout_handler(cm);
1941 break;
1942 case CM_EVENT_BATT_IN:
1943 case CM_EVENT_EXT_PWR_IN_OUT ... CM_EVENT_CHG_START_STOP:
1944 misc_event_handler(cm, type);
1945 break;
1946 case CM_EVENT_UNKNOWN:
1947 case CM_EVENT_OTHERS:
1948 uevent_notify(cm, msg ? msg : default_event_names[type]);
1949 break;
1950 default:
1951 dev_err(cm->dev, "%s type not specified.\n", __func__);
1952 break;
1953 }
1954}
1955EXPORT_SYMBOL_GPL(cm_notify_event);
1956
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001957MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1958MODULE_DESCRIPTION("Charger Manager");
1959MODULE_LICENSE("GPL");