blob: c701f5e6f327f05b5d47054631cbd4f025c14c12 [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>
25
Chanwoo Choidfeccb12012-05-05 06:26:47 -070026static const char * const default_event_names[] = {
27 [CM_EVENT_UNKNOWN] = "Unknown",
28 [CM_EVENT_BATT_FULL] = "Battery Full",
29 [CM_EVENT_BATT_IN] = "Battery Inserted",
30 [CM_EVENT_BATT_OUT] = "Battery Pulled Out",
31 [CM_EVENT_EXT_PWR_IN_OUT] = "External Power Attach/Detach",
32 [CM_EVENT_CHG_START_STOP] = "Charging Start/Stop",
33 [CM_EVENT_OTHERS] = "Other battery events"
34};
35
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090036/*
37 * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for
38 * delayed works so that we can run delayed works with CM_JIFFIES_SMALL
39 * without any delays.
40 */
41#define CM_JIFFIES_SMALL (2)
42
43/* If y is valid (> 0) and smaller than x, do x = y */
44#define CM_MIN_VALID(x, y) x = (((y > 0) && ((x) > (y))) ? (y) : (x))
45
46/*
47 * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking
48 * rtc alarm. It should be 2 or larger
49 */
50#define CM_RTC_SMALL (2)
51
52#define UEVENT_BUF_SIZE 32
53
54static LIST_HEAD(cm_list);
55static DEFINE_MUTEX(cm_list_mtx);
56
57/* About in-suspend (suspend-again) monitoring */
58static struct rtc_device *rtc_dev;
59/*
60 * Backup RTC alarm
61 * Save the wakeup alarm before entering suspend-to-RAM
62 */
63static struct rtc_wkalrm rtc_wkalarm_save;
64/* Backup RTC alarm time in terms of seconds since 01-01-1970 00:00:00 */
65static unsigned long rtc_wkalarm_save_time;
66static bool cm_suspended;
67static bool cm_rtc_set;
68static unsigned long cm_suspend_duration_ms;
69
Chanwoo Choid829dc72012-05-05 06:24:10 -070070/* About normal (not suspended) monitoring */
71static unsigned long polling_jiffy = ULONG_MAX; /* ULONG_MAX: no polling */
72static unsigned long next_polling; /* Next appointed polling time */
73static struct workqueue_struct *cm_wq; /* init at driver add */
74static struct delayed_work cm_monitor_work; /* init at driver add */
75
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090076/* Global charger-manager description */
77static struct charger_global_desc *g_desc; /* init with setup_charger_manager */
78
79/**
80 * is_batt_present - See if the battery presents in place.
81 * @cm: the Charger Manager representing the battery.
82 */
83static bool is_batt_present(struct charger_manager *cm)
84{
85 union power_supply_propval val;
86 bool present = false;
87 int i, ret;
88
89 switch (cm->desc->battery_present) {
Chanwoo Choid829dc72012-05-05 06:24:10 -070090 case CM_BATTERY_PRESENT:
91 present = true;
92 break;
93 case CM_NO_BATTERY:
94 break;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090095 case CM_FUEL_GAUGE:
96 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
97 POWER_SUPPLY_PROP_PRESENT, &val);
98 if (ret == 0 && val.intval)
99 present = true;
100 break;
101 case CM_CHARGER_STAT:
102 for (i = 0; cm->charger_stat[i]; i++) {
103 ret = cm->charger_stat[i]->get_property(
104 cm->charger_stat[i],
105 POWER_SUPPLY_PROP_PRESENT, &val);
106 if (ret == 0 && val.intval) {
107 present = true;
108 break;
109 }
110 }
111 break;
112 }
113
114 return present;
115}
116
117/**
118 * is_ext_pwr_online - See if an external power source is attached to charge
119 * @cm: the Charger Manager representing the battery.
120 *
121 * Returns true if at least one of the chargers of the battery has an external
122 * power source attached to charge the battery regardless of whether it is
123 * actually charging or not.
124 */
125static bool is_ext_pwr_online(struct charger_manager *cm)
126{
127 union power_supply_propval val;
128 bool online = false;
129 int i, ret;
130
131 /* If at least one of them has one, it's yes. */
132 for (i = 0; cm->charger_stat[i]; i++) {
133 ret = cm->charger_stat[i]->get_property(
134 cm->charger_stat[i],
135 POWER_SUPPLY_PROP_ONLINE, &val);
136 if (ret == 0 && val.intval) {
137 online = true;
138 break;
139 }
140 }
141
142 return online;
143}
144
145/**
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900146 * get_batt_uV - Get the voltage level of the battery
147 * @cm: the Charger Manager representing the battery.
148 * @uV: the voltage level returned.
149 *
150 * Returns 0 if there is no error.
151 * Returns a negative value on error.
152 */
153static int get_batt_uV(struct charger_manager *cm, int *uV)
154{
155 union power_supply_propval val;
156 int ret;
157
Axel Linbb2a95c2012-01-12 12:56:35 +0800158 if (!cm->fuel_gauge)
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900159 return -ENODEV;
160
Axel Linbb2a95c2012-01-12 12:56:35 +0800161 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
162 POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900163 if (ret)
164 return ret;
165
166 *uV = val.intval;
167 return 0;
168}
169
170/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900171 * is_charging - Returns true if the battery is being charged.
172 * @cm: the Charger Manager representing the battery.
173 */
174static bool is_charging(struct charger_manager *cm)
175{
176 int i, ret;
177 bool charging = false;
178 union power_supply_propval val;
179
180 /* If there is no battery, it cannot be charged */
181 if (!is_batt_present(cm))
182 return false;
183
184 /* If at least one of the charger is charging, return yes */
185 for (i = 0; cm->charger_stat[i]; i++) {
186 /* 1. The charger sholuld not be DISABLED */
187 if (cm->emergency_stop)
188 continue;
189 if (!cm->charger_enabled)
190 continue;
191
192 /* 2. The charger should be online (ext-power) */
193 ret = cm->charger_stat[i]->get_property(
194 cm->charger_stat[i],
195 POWER_SUPPLY_PROP_ONLINE, &val);
196 if (ret) {
197 dev_warn(cm->dev, "Cannot read ONLINE value from %s.\n",
198 cm->desc->psy_charger_stat[i]);
199 continue;
200 }
201 if (val.intval == 0)
202 continue;
203
204 /*
205 * 3. The charger should not be FULL, DISCHARGING,
206 * or NOT_CHARGING.
207 */
208 ret = cm->charger_stat[i]->get_property(
209 cm->charger_stat[i],
210 POWER_SUPPLY_PROP_STATUS, &val);
211 if (ret) {
212 dev_warn(cm->dev, "Cannot read STATUS value from %s.\n",
213 cm->desc->psy_charger_stat[i]);
214 continue;
215 }
216 if (val.intval == POWER_SUPPLY_STATUS_FULL ||
217 val.intval == POWER_SUPPLY_STATUS_DISCHARGING ||
218 val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING)
219 continue;
220
221 /* Then, this is charging. */
222 charging = true;
223 break;
224 }
225
226 return charging;
227}
228
229/**
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900230 * is_full_charged - Returns true if the battery is fully charged.
231 * @cm: the Charger Manager representing the battery.
232 */
233static bool is_full_charged(struct charger_manager *cm)
234{
235 struct charger_desc *desc = cm->desc;
236 union power_supply_propval val;
237 int ret = 0;
238 int uV;
239
240 /* If there is no battery, it cannot be charged */
241 if (!is_batt_present(cm)) {
242 val.intval = 0;
243 goto out;
244 }
245
246 if (cm->fuel_gauge && desc->fullbatt_full_capacity > 0) {
247 /* Not full if capacity of fuel gauge isn't full */
248 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
249 POWER_SUPPLY_PROP_CHARGE_FULL, &val);
250 if (!ret && val.intval > desc->fullbatt_full_capacity) {
251 val.intval = 1;
252 goto out;
253 }
254 }
255
256 /* Full, if it's over the fullbatt voltage */
257 if (desc->fullbatt_uV > 0) {
258 ret = get_batt_uV(cm, &uV);
259 if (!ret && uV >= desc->fullbatt_uV) {
260 val.intval = 1;
261 goto out;
262 }
263 }
264
265 /* Full, if the capacity is more than fullbatt_soc */
266 if (cm->fuel_gauge && desc->fullbatt_soc > 0) {
267 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
268 POWER_SUPPLY_PROP_CAPACITY, &val);
269 if (!ret && val.intval >= desc->fullbatt_soc) {
270 val.intval = 1;
271 goto out;
272 }
273 }
274
275 val.intval = 0;
276
277out:
278 return val.intval ? true : false;
279}
280
281/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900282 * is_polling_required - Return true if need to continue polling for this CM.
283 * @cm: the Charger Manager representing the battery.
284 */
285static bool is_polling_required(struct charger_manager *cm)
286{
287 switch (cm->desc->polling_mode) {
288 case CM_POLL_DISABLE:
289 return false;
290 case CM_POLL_ALWAYS:
291 return true;
292 case CM_POLL_EXTERNAL_POWER_ONLY:
293 return is_ext_pwr_online(cm);
294 case CM_POLL_CHARGING_ONLY:
295 return is_charging(cm);
296 default:
297 dev_warn(cm->dev, "Incorrect polling_mode (%d)\n",
298 cm->desc->polling_mode);
299 }
300
301 return false;
302}
303
304/**
305 * try_charger_enable - Enable/Disable chargers altogether
306 * @cm: the Charger Manager representing the battery.
307 * @enable: true: enable / false: disable
308 *
309 * Note that Charger Manager keeps the charger enabled regardless whether
310 * the charger is charging or not (because battery is full or no external
311 * power source exists) except when CM needs to disable chargers forcibly
312 * bacause of emergency causes; when the battery is overheated or too cold.
313 */
314static int try_charger_enable(struct charger_manager *cm, bool enable)
315{
316 int err = 0, i;
317 struct charger_desc *desc = cm->desc;
318
319 /* Ignore if it's redundent command */
Axel Linbb2a95c2012-01-12 12:56:35 +0800320 if (enable == cm->charger_enabled)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900321 return 0;
322
323 if (enable) {
324 if (cm->emergency_stop)
325 return -EAGAIN;
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900326 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
327 err = regulator_enable(desc->charger_regulators[i].consumer);
328 if (err < 0) {
329 dev_warn(cm->dev,
330 "Cannot enable %s regulator\n",
331 desc->charger_regulators[i].regulator_name);
332 }
333 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900334 } else {
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900335 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
336 err = regulator_disable(desc->charger_regulators[i].consumer);
337 if (err < 0) {
338 dev_warn(cm->dev,
339 "Cannot disable %s regulator\n",
340 desc->charger_regulators[i].regulator_name);
341 }
342 }
343
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900344 /*
345 * Abnormal battery state - Stop charging forcibly,
346 * even if charger was enabled at the other places
347 */
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900348 for (i = 0; i < desc->num_charger_regulators; i++) {
349 if (regulator_is_enabled(
350 desc->charger_regulators[i].consumer)) {
351 regulator_force_disable(
352 desc->charger_regulators[i].consumer);
353 dev_warn(cm->dev,
354 "Disable regulator(%s) forcibly.\n",
Chanwoo Choibee737b2012-07-12 15:03:25 +0900355 desc->charger_regulators[i].regulator_name);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900356 }
357 }
358 }
359
360 if (!err)
361 cm->charger_enabled = enable;
362
363 return err;
364}
365
366/**
Chanwoo Choid829dc72012-05-05 06:24:10 -0700367 * try_charger_restart - Restart charging.
368 * @cm: the Charger Manager representing the battery.
369 *
370 * Restart charging by turning off and on the charger.
371 */
372static int try_charger_restart(struct charger_manager *cm)
373{
374 int err;
375
376 if (cm->emergency_stop)
377 return -EAGAIN;
378
379 err = try_charger_enable(cm, false);
380 if (err)
381 return err;
382
383 return try_charger_enable(cm, true);
384}
385
386/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900387 * uevent_notify - Let users know something has changed.
388 * @cm: the Charger Manager representing the battery.
389 * @event: the event string.
390 *
391 * If @event is null, it implies that uevent_notify is called
392 * by resume function. When called in the resume function, cm_suspended
393 * should be already reset to false in order to let uevent_notify
394 * notify the recent event during the suspend to users. While
395 * suspended, uevent_notify does not notify users, but tracks
396 * events so that uevent_notify can notify users later after resumed.
397 */
398static void uevent_notify(struct charger_manager *cm, const char *event)
399{
400 static char env_str[UEVENT_BUF_SIZE + 1] = "";
401 static char env_str_save[UEVENT_BUF_SIZE + 1] = "";
402
403 if (cm_suspended) {
404 /* Nothing in suspended-event buffer */
405 if (env_str_save[0] == 0) {
406 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
407 return; /* status not changed */
408 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
409 return;
410 }
411
412 if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE))
413 return; /* Duplicated. */
Axel Linbb2a95c2012-01-12 12:56:35 +0800414 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900415 return;
416 }
417
418 if (event == NULL) {
419 /* No messages pending */
420 if (!env_str_save[0])
421 return;
422
423 strncpy(env_str, env_str_save, UEVENT_BUF_SIZE);
424 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
425 env_str_save[0] = 0;
426
427 return;
428 }
429
430 /* status not changed */
431 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
432 return;
433
434 /* save the status and notify the update */
435 strncpy(env_str, event, UEVENT_BUF_SIZE);
436 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
437
438 dev_info(cm->dev, event);
439}
440
441/**
Chanwoo Choid829dc72012-05-05 06:24:10 -0700442 * fullbatt_vchk - Check voltage drop some times after "FULL" event.
443 * @work: the work_struct appointing the function
444 *
445 * If a user has designated "fullbatt_vchkdrop_ms/uV" values with
446 * charger_desc, Charger Manager checks voltage drop after the battery
447 * "FULL" event. It checks whether the voltage has dropped more than
448 * fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms.
449 */
450static void fullbatt_vchk(struct work_struct *work)
451{
452 struct delayed_work *dwork = to_delayed_work(work);
453 struct charger_manager *cm = container_of(dwork,
454 struct charger_manager, fullbatt_vchk_work);
455 struct charger_desc *desc = cm->desc;
456 int batt_uV, err, diff;
457
458 /* remove the appointment for fullbatt_vchk */
459 cm->fullbatt_vchk_jiffies_at = 0;
460
461 if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
462 return;
463
464 err = get_batt_uV(cm, &batt_uV);
465 if (err) {
466 dev_err(cm->dev, "%s: get_batt_uV error(%d).\n", __func__, err);
467 return;
468 }
469
Chanwoo Choifd65ee52012-07-27 14:01:37 +0900470 diff = desc->fullbatt_uV;
Chanwoo Choid829dc72012-05-05 06:24:10 -0700471 diff -= batt_uV;
472
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900473 dev_info(cm->dev, "VBATT dropped %duV after full-batt.\n", diff);
Chanwoo Choid829dc72012-05-05 06:24:10 -0700474
475 if (diff > desc->fullbatt_vchkdrop_uV) {
476 try_charger_restart(cm);
477 uevent_notify(cm, "Recharge");
478 }
479}
480
481/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900482 * _cm_monitor - Monitor the temperature and return true for exceptions.
483 * @cm: the Charger Manager representing the battery.
484 *
485 * Returns true if there is an event to notify for the battery.
486 * (True if the status of "emergency_stop" changes)
487 */
488static bool _cm_monitor(struct charger_manager *cm)
489{
490 struct charger_desc *desc = cm->desc;
491 int temp = desc->temperature_out_of_range(&cm->last_temp_mC);
492
493 dev_dbg(cm->dev, "monitoring (%2.2d.%3.3dC)\n",
494 cm->last_temp_mC / 1000, cm->last_temp_mC % 1000);
495
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900496 /* It has been stopped already */
497 if (temp && cm->emergency_stop)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900498 return false;
499
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900500 /*
501 * Check temperature whether overheat or cold.
502 * If temperature is out of range normal state, stop charging.
503 */
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900504 if (temp) {
505 cm->emergency_stop = temp;
506 if (!try_charger_enable(cm, false)) {
507 if (temp > 0)
508 uevent_notify(cm, "OVERHEAT");
509 else
510 uevent_notify(cm, "COLD");
511 }
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900512
513 /*
514 * Check dropped voltage of battery. If battery voltage is more
515 * dropped than fullbatt_vchkdrop_uV after fully charged state,
516 * charger-manager have to recharge battery.
517 */
518 } else if (!cm->emergency_stop && is_ext_pwr_online(cm) &&
519 !cm->charger_enabled) {
520 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
521
522 /*
523 * Check whether fully charged state to protect overcharge
524 * if charger-manager is charging for battery.
525 */
526 } else if (!cm->emergency_stop && is_full_charged(cm) &&
527 cm->charger_enabled) {
528 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged.\n");
529 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
530
531 try_charger_enable(cm, false);
532
533 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900534 } else {
535 cm->emergency_stop = 0;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900536 if (is_ext_pwr_online(cm)) {
537 if (!try_charger_enable(cm, true))
538 uevent_notify(cm, "CHARGING");
539 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900540 }
541
542 return true;
543}
544
545/**
546 * cm_monitor - Monitor every battery.
547 *
548 * Returns true if there is an event to notify from any of the batteries.
549 * (True if the status of "emergency_stop" changes)
550 */
551static bool cm_monitor(void)
552{
553 bool stop = false;
554 struct charger_manager *cm;
555
556 mutex_lock(&cm_list_mtx);
557
Axel Linbb2a95c2012-01-12 12:56:35 +0800558 list_for_each_entry(cm, &cm_list, entry) {
559 if (_cm_monitor(cm))
560 stop = true;
561 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900562
563 mutex_unlock(&cm_list_mtx);
564
565 return stop;
566}
567
Chanwoo Choid829dc72012-05-05 06:24:10 -0700568/**
569 * _setup_polling - Setup the next instance of polling.
570 * @work: work_struct of the function _setup_polling.
571 */
572static void _setup_polling(struct work_struct *work)
573{
574 unsigned long min = ULONG_MAX;
575 struct charger_manager *cm;
576 bool keep_polling = false;
577 unsigned long _next_polling;
578
579 mutex_lock(&cm_list_mtx);
580
581 list_for_each_entry(cm, &cm_list, entry) {
582 if (is_polling_required(cm) && cm->desc->polling_interval_ms) {
583 keep_polling = true;
584
585 if (min > cm->desc->polling_interval_ms)
586 min = cm->desc->polling_interval_ms;
587 }
588 }
589
590 polling_jiffy = msecs_to_jiffies(min);
591 if (polling_jiffy <= CM_JIFFIES_SMALL)
592 polling_jiffy = CM_JIFFIES_SMALL + 1;
593
594 if (!keep_polling)
595 polling_jiffy = ULONG_MAX;
596 if (polling_jiffy == ULONG_MAX)
597 goto out;
598
599 WARN(cm_wq == NULL, "charger-manager: workqueue not initialized"
600 ". try it later. %s\n", __func__);
601
602 _next_polling = jiffies + polling_jiffy;
603
604 if (!delayed_work_pending(&cm_monitor_work) ||
605 (delayed_work_pending(&cm_monitor_work) &&
606 time_after(next_polling, _next_polling))) {
607 cancel_delayed_work_sync(&cm_monitor_work);
608 next_polling = jiffies + polling_jiffy;
609 queue_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy);
610 }
611
612out:
613 mutex_unlock(&cm_list_mtx);
614}
615static DECLARE_WORK(setup_polling, _setup_polling);
616
617/**
618 * cm_monitor_poller - The Monitor / Poller.
619 * @work: work_struct of the function cm_monitor_poller
620 *
621 * During non-suspended state, cm_monitor_poller is used to poll and monitor
622 * the batteries.
623 */
624static void cm_monitor_poller(struct work_struct *work)
625{
626 cm_monitor();
627 schedule_work(&setup_polling);
628}
629
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700630/**
631 * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL
632 * @cm: the Charger Manager representing the battery.
633 */
634static void fullbatt_handler(struct charger_manager *cm)
635{
636 struct charger_desc *desc = cm->desc;
637
638 if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
639 goto out;
640
641 if (cm_suspended)
642 device_set_wakeup_capable(cm->dev, true);
643
644 if (delayed_work_pending(&cm->fullbatt_vchk_work))
645 cancel_delayed_work(&cm->fullbatt_vchk_work);
646 queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
647 msecs_to_jiffies(desc->fullbatt_vchkdrop_ms));
648 cm->fullbatt_vchk_jiffies_at = jiffies + msecs_to_jiffies(
649 desc->fullbatt_vchkdrop_ms);
650
651 if (cm->fullbatt_vchk_jiffies_at == 0)
652 cm->fullbatt_vchk_jiffies_at = 1;
653
654out:
655 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged.\n");
656 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
657}
658
659/**
660 * battout_handler - Event handler for CM_EVENT_BATT_OUT
661 * @cm: the Charger Manager representing the battery.
662 */
663static void battout_handler(struct charger_manager *cm)
664{
665 if (cm_suspended)
666 device_set_wakeup_capable(cm->dev, true);
667
668 if (!is_batt_present(cm)) {
669 dev_emerg(cm->dev, "Battery Pulled Out!\n");
670 uevent_notify(cm, default_event_names[CM_EVENT_BATT_OUT]);
671 } else {
672 uevent_notify(cm, "Battery Reinserted?");
673 }
674}
675
676/**
677 * misc_event_handler - Handler for other evnets
678 * @cm: the Charger Manager representing the battery.
679 * @type: the Charger Manager representing the battery.
680 */
681static void misc_event_handler(struct charger_manager *cm,
682 enum cm_event_types type)
683{
684 if (cm_suspended)
685 device_set_wakeup_capable(cm->dev, true);
686
687 if (!delayed_work_pending(&cm_monitor_work) &&
688 is_polling_required(cm) && cm->desc->polling_interval_ms)
689 schedule_work(&setup_polling);
690 uevent_notify(cm, default_event_names[type]);
691}
692
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900693static int charger_get_property(struct power_supply *psy,
694 enum power_supply_property psp,
695 union power_supply_propval *val)
696{
697 struct charger_manager *cm = container_of(psy,
698 struct charger_manager, charger_psy);
699 struct charger_desc *desc = cm->desc;
Anton Vorontsovdf58c042012-03-15 21:01:28 +0400700 int ret = 0;
701 int uV;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900702
703 switch (psp) {
704 case POWER_SUPPLY_PROP_STATUS:
705 if (is_charging(cm))
706 val->intval = POWER_SUPPLY_STATUS_CHARGING;
707 else if (is_ext_pwr_online(cm))
708 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
709 else
710 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
711 break;
712 case POWER_SUPPLY_PROP_HEALTH:
713 if (cm->emergency_stop > 0)
714 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
715 else if (cm->emergency_stop < 0)
716 val->intval = POWER_SUPPLY_HEALTH_COLD;
717 else
718 val->intval = POWER_SUPPLY_HEALTH_GOOD;
719 break;
720 case POWER_SUPPLY_PROP_PRESENT:
721 if (is_batt_present(cm))
722 val->intval = 1;
723 else
724 val->intval = 0;
725 break;
726 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Anton Vorontsovdf58c042012-03-15 21:01:28 +0400727 ret = get_batt_uV(cm, &val->intval);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900728 break;
729 case POWER_SUPPLY_PROP_CURRENT_NOW:
730 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
731 POWER_SUPPLY_PROP_CURRENT_NOW, val);
732 break;
733 case POWER_SUPPLY_PROP_TEMP:
734 /* in thenth of centigrade */
735 if (cm->last_temp_mC == INT_MIN)
736 desc->temperature_out_of_range(&cm->last_temp_mC);
737 val->intval = cm->last_temp_mC / 100;
738 if (!desc->measure_battery_temp)
739 ret = -ENODEV;
740 break;
741 case POWER_SUPPLY_PROP_TEMP_AMBIENT:
742 /* in thenth of centigrade */
743 if (cm->last_temp_mC == INT_MIN)
744 desc->temperature_out_of_range(&cm->last_temp_mC);
745 val->intval = cm->last_temp_mC / 100;
746 if (desc->measure_battery_temp)
747 ret = -ENODEV;
748 break;
749 case POWER_SUPPLY_PROP_CAPACITY:
750 if (!cm->fuel_gauge) {
751 ret = -ENODEV;
752 break;
753 }
754
755 if (!is_batt_present(cm)) {
756 /* There is no battery. Assume 100% */
757 val->intval = 100;
758 break;
759 }
760
761 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
762 POWER_SUPPLY_PROP_CAPACITY, val);
763 if (ret)
764 break;
765
766 if (val->intval > 100) {
767 val->intval = 100;
768 break;
769 }
770 if (val->intval < 0)
771 val->intval = 0;
772
773 /* Do not adjust SOC when charging: voltage is overrated */
774 if (is_charging(cm))
775 break;
776
777 /*
778 * If the capacity value is inconsistent, calibrate it base on
779 * the battery voltage values and the thresholds given as desc
780 */
781 ret = get_batt_uV(cm, &uV);
782 if (ret) {
783 /* Voltage information not available. No calibration */
784 ret = 0;
785 break;
786 }
787
788 if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
789 !is_charging(cm)) {
790 val->intval = 100;
791 break;
792 }
793
794 break;
795 case POWER_SUPPLY_PROP_ONLINE:
796 if (is_ext_pwr_online(cm))
797 val->intval = 1;
798 else
799 val->intval = 0;
800 break;
801 case POWER_SUPPLY_PROP_CHARGE_FULL:
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900802 if (is_full_charged(cm))
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900803 val->intval = 1;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900804 else
805 val->intval = 0;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900806 ret = 0;
807 break;
808 case POWER_SUPPLY_PROP_CHARGE_NOW:
809 if (is_charging(cm)) {
810 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
811 POWER_SUPPLY_PROP_CHARGE_NOW,
812 val);
813 if (ret) {
814 val->intval = 1;
815 ret = 0;
816 } else {
817 /* If CHARGE_NOW is supplied, use it */
818 val->intval = (val->intval > 0) ?
819 val->intval : 1;
820 }
821 } else {
822 val->intval = 0;
823 }
824 break;
825 default:
826 return -EINVAL;
827 }
828 return ret;
829}
830
831#define NUM_CHARGER_PSY_OPTIONAL (4)
832static enum power_supply_property default_charger_props[] = {
833 /* Guaranteed to provide */
834 POWER_SUPPLY_PROP_STATUS,
835 POWER_SUPPLY_PROP_HEALTH,
836 POWER_SUPPLY_PROP_PRESENT,
837 POWER_SUPPLY_PROP_VOLTAGE_NOW,
838 POWER_SUPPLY_PROP_CAPACITY,
839 POWER_SUPPLY_PROP_ONLINE,
840 POWER_SUPPLY_PROP_CHARGE_FULL,
841 /*
842 * Optional properties are:
843 * POWER_SUPPLY_PROP_CHARGE_NOW,
844 * POWER_SUPPLY_PROP_CURRENT_NOW,
845 * POWER_SUPPLY_PROP_TEMP, and
846 * POWER_SUPPLY_PROP_TEMP_AMBIENT,
847 */
848};
849
850static struct power_supply psy_default = {
851 .name = "battery",
852 .type = POWER_SUPPLY_TYPE_BATTERY,
853 .properties = default_charger_props,
854 .num_properties = ARRAY_SIZE(default_charger_props),
855 .get_property = charger_get_property,
856};
857
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900858/**
859 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
860 * for suspend_again.
861 *
862 * Returns true if the alarm is set for Charger Manager to use.
863 * Returns false if
864 * cm_setup_timer fails to set an alarm,
865 * cm_setup_timer does not need to set an alarm for Charger Manager,
866 * or an alarm previously configured is to be used.
867 */
868static bool cm_setup_timer(void)
869{
870 struct charger_manager *cm;
871 unsigned int wakeup_ms = UINT_MAX;
872 bool ret = false;
873
874 mutex_lock(&cm_list_mtx);
875
876 list_for_each_entry(cm, &cm_list, entry) {
Chanwoo Choid829dc72012-05-05 06:24:10 -0700877 unsigned int fbchk_ms = 0;
878
879 /* fullbatt_vchk is required. setup timer for that */
880 if (cm->fullbatt_vchk_jiffies_at) {
881 fbchk_ms = jiffies_to_msecs(cm->fullbatt_vchk_jiffies_at
882 - jiffies);
883 if (time_is_before_eq_jiffies(
884 cm->fullbatt_vchk_jiffies_at) ||
885 msecs_to_jiffies(fbchk_ms) < CM_JIFFIES_SMALL) {
886 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
887 fbchk_ms = 0;
888 }
889 }
890 CM_MIN_VALID(wakeup_ms, fbchk_ms);
891
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900892 /* Skip if polling is not required for this CM */
893 if (!is_polling_required(cm) && !cm->emergency_stop)
894 continue;
895 if (cm->desc->polling_interval_ms == 0)
896 continue;
897 CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
898 }
899
900 mutex_unlock(&cm_list_mtx);
901
902 if (wakeup_ms < UINT_MAX && wakeup_ms > 0) {
903 pr_info("Charger Manager wakeup timer: %u ms.\n", wakeup_ms);
904 if (rtc_dev) {
905 struct rtc_wkalrm tmp;
906 unsigned long time, now;
907 unsigned long add = DIV_ROUND_UP(wakeup_ms, 1000);
908
909 /*
910 * Set alarm with the polling interval (wakeup_ms)
911 * except when rtc_wkalarm_save comes first.
912 * However, the alarm time should be NOW +
913 * CM_RTC_SMALL or later.
914 */
915 tmp.enabled = 1;
916 rtc_read_time(rtc_dev, &tmp.time);
917 rtc_tm_to_time(&tmp.time, &now);
918 if (add < CM_RTC_SMALL)
919 add = CM_RTC_SMALL;
920 time = now + add;
921
922 ret = true;
923
924 if (rtc_wkalarm_save.enabled &&
925 rtc_wkalarm_save_time &&
926 rtc_wkalarm_save_time < time) {
927 if (rtc_wkalarm_save_time < now + CM_RTC_SMALL)
928 time = now + CM_RTC_SMALL;
929 else
930 time = rtc_wkalarm_save_time;
931
932 /* The timer is not appointed by CM */
933 ret = false;
934 }
935
936 pr_info("Waking up after %lu secs.\n",
937 time - now);
938
939 rtc_time_to_tm(time, &tmp.time);
940 rtc_set_alarm(rtc_dev, &tmp);
941 cm_suspend_duration_ms += wakeup_ms;
942 return ret;
943 }
944 }
945
946 if (rtc_dev)
947 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
948 return false;
949}
950
Chanwoo Choid829dc72012-05-05 06:24:10 -0700951static void _cm_fbchk_in_suspend(struct charger_manager *cm)
952{
953 unsigned long jiffy_now = jiffies;
954
955 if (!cm->fullbatt_vchk_jiffies_at)
956 return;
957
958 if (g_desc && g_desc->assume_timer_stops_in_suspend)
959 jiffy_now += msecs_to_jiffies(cm_suspend_duration_ms);
960
961 /* Execute now if it's going to be executed not too long after */
962 jiffy_now += CM_JIFFIES_SMALL;
963
964 if (time_after_eq(jiffy_now, cm->fullbatt_vchk_jiffies_at))
965 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
966}
967
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900968/**
969 * cm_suspend_again - Determine whether suspend again or not
970 *
971 * Returns true if the system should be suspended again
972 * Returns false if the system should be woken up
973 */
974bool cm_suspend_again(void)
975{
976 struct charger_manager *cm;
977 bool ret = false;
978
979 if (!g_desc || !g_desc->rtc_only_wakeup || !g_desc->rtc_only_wakeup() ||
980 !cm_rtc_set)
981 return false;
982
983 if (cm_monitor())
984 goto out;
985
986 ret = true;
987 mutex_lock(&cm_list_mtx);
988 list_for_each_entry(cm, &cm_list, entry) {
Chanwoo Choid829dc72012-05-05 06:24:10 -0700989 _cm_fbchk_in_suspend(cm);
990
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900991 if (cm->status_save_ext_pwr_inserted != is_ext_pwr_online(cm) ||
Axel Linbb2a95c2012-01-12 12:56:35 +0800992 cm->status_save_batt != is_batt_present(cm)) {
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900993 ret = false;
Axel Linbb2a95c2012-01-12 12:56:35 +0800994 break;
995 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900996 }
997 mutex_unlock(&cm_list_mtx);
998
999 cm_rtc_set = cm_setup_timer();
1000out:
1001 /* It's about the time when the non-CM appointed timer goes off */
1002 if (rtc_wkalarm_save.enabled) {
1003 unsigned long now;
1004 struct rtc_time tmp;
1005
1006 rtc_read_time(rtc_dev, &tmp);
1007 rtc_tm_to_time(&tmp, &now);
1008
1009 if (rtc_wkalarm_save_time &&
1010 now + CM_RTC_SMALL >= rtc_wkalarm_save_time)
1011 return false;
1012 }
1013 return ret;
1014}
1015EXPORT_SYMBOL_GPL(cm_suspend_again);
1016
1017/**
1018 * setup_charger_manager - initialize charger_global_desc data
1019 * @gd: pointer to instance of charger_global_desc
1020 */
1021int setup_charger_manager(struct charger_global_desc *gd)
1022{
1023 if (!gd)
1024 return -EINVAL;
1025
1026 if (rtc_dev)
1027 rtc_class_close(rtc_dev);
1028 rtc_dev = NULL;
1029 g_desc = NULL;
1030
1031 if (!gd->rtc_only_wakeup) {
1032 pr_err("The callback rtc_only_wakeup is not given.\n");
1033 return -EINVAL;
1034 }
1035
1036 if (gd->rtc_name) {
1037 rtc_dev = rtc_class_open(gd->rtc_name);
1038 if (IS_ERR_OR_NULL(rtc_dev)) {
1039 rtc_dev = NULL;
1040 /* Retry at probe. RTC may be not registered yet */
1041 }
1042 } else {
1043 pr_warn("No wakeup timer is given for charger manager."
1044 "In-suspend monitoring won't work.\n");
1045 }
1046
1047 g_desc = gd;
1048 return 0;
1049}
1050EXPORT_SYMBOL_GPL(setup_charger_manager);
1051
Chanwoo Choibee737b2012-07-12 15:03:25 +09001052/**
1053 * charger_extcon_work - enable/diable charger according to the state
1054 * of charger cable
1055 *
1056 * @work: work_struct of the function charger_extcon_work.
1057 */
1058static void charger_extcon_work(struct work_struct *work)
1059{
1060 struct charger_cable *cable =
1061 container_of(work, struct charger_cable, wq);
Chanwoo Choi45cd4fb2012-07-12 15:03:29 +09001062 int ret;
1063
1064 if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) {
1065 ret = regulator_set_current_limit(cable->charger->consumer,
1066 cable->min_uA, cable->max_uA);
1067 if (ret < 0) {
1068 pr_err("Cannot set current limit of %s (%s)\n",
1069 cable->charger->regulator_name, cable->name);
1070 return;
1071 }
1072
1073 pr_info("Set current limit of %s : %duA ~ %duA\n",
1074 cable->charger->regulator_name,
1075 cable->min_uA, cable->max_uA);
1076 }
Chanwoo Choibee737b2012-07-12 15:03:25 +09001077
1078 try_charger_enable(cable->cm, cable->attached);
1079}
1080
1081/**
1082 * charger_extcon_notifier - receive the state of charger cable
1083 * when registered cable is attached or detached.
1084 *
1085 * @self: the notifier block of the charger_extcon_notifier.
1086 * @event: the cable state.
1087 * @ptr: the data pointer of notifier block.
1088 */
1089static int charger_extcon_notifier(struct notifier_block *self,
1090 unsigned long event, void *ptr)
1091{
1092 struct charger_cable *cable =
1093 container_of(self, struct charger_cable, nb);
1094
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001095 /*
1096 * The newly state of charger cable.
1097 * If cable is attached, cable->attached is true.
1098 */
Chanwoo Choibee737b2012-07-12 15:03:25 +09001099 cable->attached = event;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001100
1101 /*
1102 * Setup monitoring to check battery state
1103 * when charger cable is attached.
1104 */
1105 if (cable->attached && is_polling_required(cable->cm)) {
1106 if (work_pending(&setup_polling))
1107 cancel_work_sync(&setup_polling);
1108 schedule_work(&setup_polling);
1109 }
1110
1111 /*
1112 * Setup work for controlling charger(regulator)
1113 * according to charger cable.
1114 */
Chanwoo Choibee737b2012-07-12 15:03:25 +09001115 schedule_work(&cable->wq);
1116
1117 return NOTIFY_DONE;
1118}
1119
1120/**
1121 * charger_extcon_init - register external connector to use it
1122 * as the charger cable
1123 *
1124 * @cm: the Charger Manager representing the battery.
1125 * @cable: the Charger cable representing the external connector.
1126 */
1127static int charger_extcon_init(struct charger_manager *cm,
1128 struct charger_cable *cable)
1129{
1130 int ret = 0;
1131
1132 /*
1133 * Charger manager use Extcon framework to identify
1134 * the charger cable among various external connector
1135 * cable (e.g., TA, USB, MHL, Dock).
1136 */
1137 INIT_WORK(&cable->wq, charger_extcon_work);
1138 cable->nb.notifier_call = charger_extcon_notifier;
1139 ret = extcon_register_interest(&cable->extcon_dev,
1140 cable->extcon_name, cable->name, &cable->nb);
1141 if (ret < 0) {
1142 pr_info("Cannot register extcon_dev for %s(cable: %s).\n",
1143 cable->extcon_name,
1144 cable->name);
1145 ret = -EINVAL;
1146 }
1147
1148 return ret;
1149}
1150
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001151static int charger_manager_probe(struct platform_device *pdev)
1152{
1153 struct charger_desc *desc = dev_get_platdata(&pdev->dev);
1154 struct charger_manager *cm;
1155 int ret = 0, i = 0;
Chanwoo Choibee737b2012-07-12 15:03:25 +09001156 int j = 0;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001157 union power_supply_propval val;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001158
1159 if (g_desc && !rtc_dev && g_desc->rtc_name) {
1160 rtc_dev = rtc_class_open(g_desc->rtc_name);
1161 if (IS_ERR_OR_NULL(rtc_dev)) {
1162 rtc_dev = NULL;
1163 dev_err(&pdev->dev, "Cannot get RTC %s.\n",
1164 g_desc->rtc_name);
1165 ret = -ENODEV;
1166 goto err_alloc;
1167 }
1168 }
1169
1170 if (!desc) {
1171 dev_err(&pdev->dev, "No platform data (desc) found.\n");
1172 ret = -ENODEV;
1173 goto err_alloc;
1174 }
1175
1176 cm = kzalloc(sizeof(struct charger_manager), GFP_KERNEL);
1177 if (!cm) {
1178 dev_err(&pdev->dev, "Cannot allocate memory.\n");
1179 ret = -ENOMEM;
1180 goto err_alloc;
1181 }
1182
1183 /* Basic Values. Unspecified are Null or 0 */
1184 cm->dev = &pdev->dev;
1185 cm->desc = kzalloc(sizeof(struct charger_desc), GFP_KERNEL);
1186 if (!cm->desc) {
1187 dev_err(&pdev->dev, "Cannot allocate memory.\n");
1188 ret = -ENOMEM;
1189 goto err_alloc_desc;
1190 }
1191 memcpy(cm->desc, desc, sizeof(struct charger_desc));
1192 cm->last_temp_mC = INT_MIN; /* denotes "unmeasured, yet" */
1193
Chanwoo Choid829dc72012-05-05 06:24:10 -07001194 /*
1195 * The following two do not need to be errors.
1196 * Users may intentionally ignore those two features.
1197 */
1198 if (desc->fullbatt_uV == 0) {
1199 dev_info(&pdev->dev, "Ignoring full-battery voltage threshold"
1200 " as it is not supplied.");
1201 }
1202 if (!desc->fullbatt_vchkdrop_ms || !desc->fullbatt_vchkdrop_uV) {
1203 dev_info(&pdev->dev, "Disabling full-battery voltage drop "
1204 "checking mechanism as it is not supplied.");
1205 desc->fullbatt_vchkdrop_ms = 0;
1206 desc->fullbatt_vchkdrop_uV = 0;
1207 }
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001208 if (desc->fullbatt_soc == 0) {
1209 dev_info(&pdev->dev, "Ignoring full-battery soc(state of"
1210 " charge) threshold as it is not"
1211 " supplied.");
1212 }
1213 if (desc->fullbatt_full_capacity == 0) {
1214 dev_info(&pdev->dev, "Ignoring full-battery full capacity"
1215 " threshold as it is not supplied.");
1216 }
Chanwoo Choid829dc72012-05-05 06:24:10 -07001217
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001218 if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
1219 ret = -EINVAL;
1220 dev_err(&pdev->dev, "charger_regulators undefined.\n");
1221 goto err_no_charger;
1222 }
1223
1224 if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
1225 dev_err(&pdev->dev, "No power supply defined.\n");
1226 ret = -EINVAL;
1227 goto err_no_charger_stat;
1228 }
1229
1230 /* Counting index only */
1231 while (desc->psy_charger_stat[i])
1232 i++;
1233
1234 cm->charger_stat = kzalloc(sizeof(struct power_supply *) * (i + 1),
1235 GFP_KERNEL);
1236 if (!cm->charger_stat) {
1237 ret = -ENOMEM;
1238 goto err_no_charger_stat;
1239 }
1240
1241 for (i = 0; desc->psy_charger_stat[i]; i++) {
1242 cm->charger_stat[i] = power_supply_get_by_name(
1243 desc->psy_charger_stat[i]);
1244 if (!cm->charger_stat[i]) {
1245 dev_err(&pdev->dev, "Cannot find power supply "
1246 "\"%s\"\n",
1247 desc->psy_charger_stat[i]);
1248 ret = -ENODEV;
1249 goto err_chg_stat;
1250 }
1251 }
1252
1253 cm->fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
1254 if (!cm->fuel_gauge) {
1255 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
1256 desc->psy_fuel_gauge);
1257 ret = -ENODEV;
1258 goto err_chg_stat;
1259 }
1260
1261 if (desc->polling_interval_ms == 0 ||
1262 msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) {
1263 dev_err(&pdev->dev, "polling_interval_ms is too small\n");
1264 ret = -EINVAL;
1265 goto err_chg_stat;
1266 }
1267
1268 if (!desc->temperature_out_of_range) {
1269 dev_err(&pdev->dev, "there is no temperature_out_of_range\n");
1270 ret = -EINVAL;
1271 goto err_chg_stat;
1272 }
1273
1274 platform_set_drvdata(pdev, cm);
1275
Axel Linbb2a95c2012-01-12 12:56:35 +08001276 memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default));
1277
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001278 if (!desc->psy_name) {
Axel Linbb2a95c2012-01-12 12:56:35 +08001279 strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001280 } else {
1281 strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
1282 }
1283 cm->charger_psy.name = cm->psy_name_buf;
1284
1285 /* Allocate for psy properties because they may vary */
1286 cm->charger_psy.properties = kzalloc(sizeof(enum power_supply_property)
1287 * (ARRAY_SIZE(default_charger_props) +
1288 NUM_CHARGER_PSY_OPTIONAL),
1289 GFP_KERNEL);
1290 if (!cm->charger_psy.properties) {
1291 dev_err(&pdev->dev, "Cannot allocate for psy properties.\n");
1292 ret = -ENOMEM;
1293 goto err_chg_stat;
1294 }
1295 memcpy(cm->charger_psy.properties, default_charger_props,
1296 sizeof(enum power_supply_property) *
1297 ARRAY_SIZE(default_charger_props));
1298 cm->charger_psy.num_properties = psy_default.num_properties;
1299
1300 /* Find which optional psy-properties are available */
1301 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1302 POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
1303 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1304 POWER_SUPPLY_PROP_CHARGE_NOW;
1305 cm->charger_psy.num_properties++;
1306 }
1307 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1308 POWER_SUPPLY_PROP_CURRENT_NOW,
1309 &val)) {
1310 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1311 POWER_SUPPLY_PROP_CURRENT_NOW;
1312 cm->charger_psy.num_properties++;
1313 }
Axel Linbb2a95c2012-01-12 12:56:35 +08001314
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001315 if (desc->measure_battery_temp) {
1316 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1317 POWER_SUPPLY_PROP_TEMP;
1318 cm->charger_psy.num_properties++;
Axel Linbb2a95c2012-01-12 12:56:35 +08001319 } else {
1320 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1321 POWER_SUPPLY_PROP_TEMP_AMBIENT;
1322 cm->charger_psy.num_properties++;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001323 }
1324
Chanwoo Choid829dc72012-05-05 06:24:10 -07001325 INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk);
1326
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001327 ret = power_supply_register(NULL, &cm->charger_psy);
1328 if (ret) {
1329 dev_err(&pdev->dev, "Cannot register charger-manager with"
1330 " name \"%s\".\n", cm->charger_psy.name);
1331 goto err_register;
1332 }
1333
Chanwoo Choibee737b2012-07-12 15:03:25 +09001334 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1335 struct charger_regulator *charger
1336 = &desc->charger_regulators[i];
1337
1338 charger->consumer = regulator_get(&pdev->dev,
1339 charger->regulator_name);
1340 if (charger->consumer == NULL) {
1341 dev_err(&pdev->dev, "Cannot find charger(%s)n",
1342 charger->regulator_name);
1343 ret = -EINVAL;
1344 goto err_chg_get;
1345 }
1346
1347 for (j = 0 ; j < charger->num_cables ; j++) {
1348 struct charger_cable *cable = &charger->cables[j];
1349
1350 ret = charger_extcon_init(cm, cable);
1351 if (ret < 0) {
1352 dev_err(&pdev->dev, "Cannot find charger(%s)n",
1353 charger->regulator_name);
1354 goto err_extcon;
1355 }
1356 cable->charger = charger;
1357 cable->cm = cm;
1358 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001359 }
1360
1361 ret = try_charger_enable(cm, true);
1362 if (ret) {
1363 dev_err(&pdev->dev, "Cannot enable charger regulators\n");
1364 goto err_chg_enable;
1365 }
1366
1367 /* Add to the list */
1368 mutex_lock(&cm_list_mtx);
1369 list_add(&cm->entry, &cm_list);
1370 mutex_unlock(&cm_list_mtx);
1371
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001372 /*
1373 * Charger-manager is capable of waking up the systme from sleep
1374 * when event is happend through cm_notify_event()
1375 */
1376 device_init_wakeup(&pdev->dev, true);
1377 device_set_wakeup_capable(&pdev->dev, false);
1378
Chanwoo Choid829dc72012-05-05 06:24:10 -07001379 schedule_work(&setup_polling);
1380
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001381 return 0;
1382
1383err_chg_enable:
Chanwoo Choibee737b2012-07-12 15:03:25 +09001384err_extcon:
1385 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1386 struct charger_regulator *charger
1387 = &desc->charger_regulators[i];
1388 for (j = 0 ; j < charger->num_cables ; j++) {
1389 struct charger_cable *cable = &charger->cables[j];
1390 extcon_unregister_interest(&cable->extcon_dev);
1391 }
1392 }
1393err_chg_get:
1394 for (i = 0 ; i < desc->num_charger_regulators ; i++)
1395 regulator_put(desc->charger_regulators[i].consumer);
1396
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001397 power_supply_unregister(&cm->charger_psy);
1398err_register:
1399 kfree(cm->charger_psy.properties);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001400err_chg_stat:
1401 kfree(cm->charger_stat);
1402err_no_charger_stat:
1403err_no_charger:
1404 kfree(cm->desc);
1405err_alloc_desc:
1406 kfree(cm);
1407err_alloc:
1408 return ret;
1409}
1410
1411static int __devexit charger_manager_remove(struct platform_device *pdev)
1412{
1413 struct charger_manager *cm = platform_get_drvdata(pdev);
1414 struct charger_desc *desc = cm->desc;
Chanwoo Choibee737b2012-07-12 15:03:25 +09001415 int i = 0;
1416 int j = 0;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001417
1418 /* Remove from the list */
1419 mutex_lock(&cm_list_mtx);
1420 list_del(&cm->entry);
1421 mutex_unlock(&cm_list_mtx);
1422
Chanwoo Choid829dc72012-05-05 06:24:10 -07001423 if (work_pending(&setup_polling))
1424 cancel_work_sync(&setup_polling);
1425 if (delayed_work_pending(&cm_monitor_work))
1426 cancel_delayed_work_sync(&cm_monitor_work);
1427
Chanwoo Choibee737b2012-07-12 15:03:25 +09001428 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1429 struct charger_regulator *charger
1430 = &desc->charger_regulators[i];
1431 for (j = 0 ; j < charger->num_cables ; j++) {
1432 struct charger_cable *cable = &charger->cables[j];
1433 extcon_unregister_interest(&cable->extcon_dev);
1434 }
1435 }
1436
1437 for (i = 0 ; i < desc->num_charger_regulators ; i++)
1438 regulator_put(desc->charger_regulators[i].consumer);
1439
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001440 power_supply_unregister(&cm->charger_psy);
Chanwoo Choid829dc72012-05-05 06:24:10 -07001441
1442 try_charger_enable(cm, false);
1443
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001444 kfree(cm->charger_psy.properties);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001445 kfree(cm->charger_stat);
1446 kfree(cm->desc);
1447 kfree(cm);
1448
1449 return 0;
1450}
1451
Axel Lin1bbe24d2012-01-11 17:19:45 +08001452static const struct platform_device_id charger_manager_id[] = {
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001453 { "charger-manager", 0 },
1454 { },
1455};
Axel Lin1bbe24d2012-01-11 17:19:45 +08001456MODULE_DEVICE_TABLE(platform, charger_manager_id);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001457
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001458static int cm_suspend_noirq(struct device *dev)
1459{
1460 int ret = 0;
1461
1462 if (device_may_wakeup(dev)) {
1463 device_set_wakeup_capable(dev, false);
1464 ret = -EAGAIN;
1465 }
1466
1467 return ret;
1468}
1469
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001470static int cm_suspend_prepare(struct device *dev)
1471{
Axel Linbb2a95c2012-01-12 12:56:35 +08001472 struct charger_manager *cm = dev_get_drvdata(dev);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001473
1474 if (!cm_suspended) {
1475 if (rtc_dev) {
1476 struct rtc_time tmp;
1477 unsigned long now;
1478
1479 rtc_read_alarm(rtc_dev, &rtc_wkalarm_save);
1480 rtc_read_time(rtc_dev, &tmp);
1481
1482 if (rtc_wkalarm_save.enabled) {
1483 rtc_tm_to_time(&rtc_wkalarm_save.time,
1484 &rtc_wkalarm_save_time);
1485 rtc_tm_to_time(&tmp, &now);
1486 if (now > rtc_wkalarm_save_time)
1487 rtc_wkalarm_save_time = 0;
1488 } else {
1489 rtc_wkalarm_save_time = 0;
1490 }
1491 }
1492 cm_suspended = true;
1493 }
1494
Chanwoo Choid829dc72012-05-05 06:24:10 -07001495 if (delayed_work_pending(&cm->fullbatt_vchk_work))
1496 cancel_delayed_work(&cm->fullbatt_vchk_work);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001497 cm->status_save_ext_pwr_inserted = is_ext_pwr_online(cm);
1498 cm->status_save_batt = is_batt_present(cm);
1499
1500 if (!cm_rtc_set) {
1501 cm_suspend_duration_ms = 0;
1502 cm_rtc_set = cm_setup_timer();
1503 }
1504
1505 return 0;
1506}
1507
1508static void cm_suspend_complete(struct device *dev)
1509{
Axel Linbb2a95c2012-01-12 12:56:35 +08001510 struct charger_manager *cm = dev_get_drvdata(dev);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001511
1512 if (cm_suspended) {
1513 if (rtc_dev) {
1514 struct rtc_wkalrm tmp;
1515
1516 rtc_read_alarm(rtc_dev, &tmp);
1517 rtc_wkalarm_save.pending = tmp.pending;
1518 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1519 }
1520 cm_suspended = false;
1521 cm_rtc_set = false;
1522 }
1523
Chanwoo Choid829dc72012-05-05 06:24:10 -07001524 /* Re-enqueue delayed work (fullbatt_vchk_work) */
1525 if (cm->fullbatt_vchk_jiffies_at) {
1526 unsigned long delay = 0;
1527 unsigned long now = jiffies + CM_JIFFIES_SMALL;
1528
1529 if (time_after_eq(now, cm->fullbatt_vchk_jiffies_at)) {
1530 delay = (unsigned long)((long)now
1531 - (long)(cm->fullbatt_vchk_jiffies_at));
1532 delay = jiffies_to_msecs(delay);
1533 } else {
1534 delay = 0;
1535 }
1536
1537 /*
1538 * Account for cm_suspend_duration_ms if
1539 * assume_timer_stops_in_suspend is active
1540 */
1541 if (g_desc && g_desc->assume_timer_stops_in_suspend) {
1542 if (delay > cm_suspend_duration_ms)
1543 delay -= cm_suspend_duration_ms;
1544 else
1545 delay = 0;
1546 }
1547
1548 queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
1549 msecs_to_jiffies(delay));
1550 }
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001551 device_set_wakeup_capable(cm->dev, false);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001552 uevent_notify(cm, NULL);
1553}
1554
1555static const struct dev_pm_ops charger_manager_pm = {
1556 .prepare = cm_suspend_prepare,
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001557 .suspend_noirq = cm_suspend_noirq,
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001558 .complete = cm_suspend_complete,
1559};
1560
1561static struct platform_driver charger_manager_driver = {
1562 .driver = {
1563 .name = "charger-manager",
1564 .owner = THIS_MODULE,
1565 .pm = &charger_manager_pm,
1566 },
1567 .probe = charger_manager_probe,
1568 .remove = __devexit_p(charger_manager_remove),
1569 .id_table = charger_manager_id,
1570};
1571
1572static int __init charger_manager_init(void)
1573{
Chanwoo Choid829dc72012-05-05 06:24:10 -07001574 cm_wq = create_freezable_workqueue("charger_manager");
1575 INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller);
1576
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001577 return platform_driver_register(&charger_manager_driver);
1578}
1579late_initcall(charger_manager_init);
1580
1581static void __exit charger_manager_cleanup(void)
1582{
Chanwoo Choid829dc72012-05-05 06:24:10 -07001583 destroy_workqueue(cm_wq);
1584 cm_wq = NULL;
1585
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001586 platform_driver_unregister(&charger_manager_driver);
1587}
1588module_exit(charger_manager_cleanup);
1589
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001590/**
1591 * find_power_supply - find the associated power_supply of charger
1592 * @cm: the Charger Manager representing the battery
1593 * @psy: pointer to instance of charger's power_supply
1594 */
1595static bool find_power_supply(struct charger_manager *cm,
1596 struct power_supply *psy)
1597{
1598 int i;
1599 bool found = false;
1600
1601 for (i = 0; cm->charger_stat[i]; i++) {
1602 if (psy == cm->charger_stat[i]) {
1603 found = true;
1604 break;
1605 }
1606 }
1607
1608 return found;
1609}
1610
1611/**
1612 * cm_notify_event - charger driver notify Charger Manager of charger event
1613 * @psy: pointer to instance of charger's power_supply
1614 * @type: type of charger event
1615 * @msg: optional message passed to uevent_notify fuction
1616 */
1617void cm_notify_event(struct power_supply *psy, enum cm_event_types type,
1618 char *msg)
1619{
1620 struct charger_manager *cm;
1621 bool found_power_supply = false;
1622
1623 if (psy == NULL)
1624 return;
1625
1626 mutex_lock(&cm_list_mtx);
1627 list_for_each_entry(cm, &cm_list, entry) {
1628 found_power_supply = find_power_supply(cm, psy);
1629 if (found_power_supply)
1630 break;
1631 }
1632 mutex_unlock(&cm_list_mtx);
1633
1634 if (!found_power_supply)
1635 return;
1636
1637 switch (type) {
1638 case CM_EVENT_BATT_FULL:
1639 fullbatt_handler(cm);
1640 break;
1641 case CM_EVENT_BATT_OUT:
1642 battout_handler(cm);
1643 break;
1644 case CM_EVENT_BATT_IN:
1645 case CM_EVENT_EXT_PWR_IN_OUT ... CM_EVENT_CHG_START_STOP:
1646 misc_event_handler(cm, type);
1647 break;
1648 case CM_EVENT_UNKNOWN:
1649 case CM_EVENT_OTHERS:
1650 uevent_notify(cm, msg ? msg : default_event_names[type]);
1651 break;
1652 default:
1653 dev_err(cm->dev, "%s type not specified.\n", __func__);
1654 break;
1655 }
1656}
1657EXPORT_SYMBOL_GPL(cm_notify_event);
1658
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001659MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1660MODULE_DESCRIPTION("Charger Manager");
1661MODULE_LICENSE("GPL");