blob: e92ec55ced91723abfdf7f112b1b6eb259ef626f [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 Choi8fcfe082012-09-20 21:20:05 -0700326
327 /*
328 * Save start time of charging to limit
329 * maximum possible charging time.
330 */
331 cm->charging_start_time = ktime_to_ms(ktime_get());
332 cm->charging_end_time = 0;
333
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900334 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
335 err = regulator_enable(desc->charger_regulators[i].consumer);
336 if (err < 0) {
337 dev_warn(cm->dev,
338 "Cannot enable %s regulator\n",
339 desc->charger_regulators[i].regulator_name);
340 }
341 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900342 } else {
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700343 /*
344 * Save end time of charging to maintain fully charged state
345 * of battery after full-batt.
346 */
347 cm->charging_start_time = 0;
348 cm->charging_end_time = ktime_to_ms(ktime_get());
349
Chanwoo Choidbb61fc2012-07-27 14:01:34 +0900350 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
351 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 Choifd65ee52012-07-27 14:01:37 +0900485 diff = desc->fullbatt_uV;
Chanwoo Choid829dc72012-05-05 06:24:10 -0700486 diff -= batt_uV;
487
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900488 dev_info(cm->dev, "VBATT dropped %duV after full-batt.\n", diff);
Chanwoo Choid829dc72012-05-05 06:24:10 -0700489
490 if (diff > desc->fullbatt_vchkdrop_uV) {
491 try_charger_restart(cm);
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700492 uevent_notify(cm, "Recharging");
Chanwoo Choid829dc72012-05-05 06:24:10 -0700493 }
494}
495
496/**
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700497 * check_charging_duration - Monitor charging/discharging duration
498 * @cm: the Charger Manager representing the battery.
499 *
500 * If whole charging duration exceed 'charging_max_duration_ms',
501 * cm stop charging to prevent overcharge/overheat. If discharging
502 * duration exceed 'discharging _max_duration_ms', charger cable is
503 * attached, after full-batt, cm start charging to maintain fully
504 * charged state for battery.
505 */
506static int check_charging_duration(struct charger_manager *cm)
507{
508 struct charger_desc *desc = cm->desc;
509 u64 curr = ktime_to_ms(ktime_get());
510 u64 duration;
511 int ret = false;
512
513 if (!desc->charging_max_duration_ms &&
514 !desc->discharging_max_duration_ms)
515 return ret;
516
517 if (cm->charger_enabled) {
518 duration = curr - cm->charging_start_time;
519
520 if (duration > desc->charging_max_duration_ms) {
521 dev_info(cm->dev, "Charging duration exceed %lldms",
522 desc->charging_max_duration_ms);
523 uevent_notify(cm, "Discharging");
524 try_charger_enable(cm, false);
525 ret = true;
526 }
527 } else if (is_ext_pwr_online(cm) && !cm->charger_enabled) {
528 duration = curr - cm->charging_end_time;
529
530 if (duration > desc->charging_max_duration_ms &&
531 is_ext_pwr_online(cm)) {
532 dev_info(cm->dev, "DisCharging duration exceed %lldms",
533 desc->discharging_max_duration_ms);
534 uevent_notify(cm, "Recharing");
535 try_charger_enable(cm, true);
536 ret = true;
537 }
538 }
539
540 return ret;
541}
542
543/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900544 * _cm_monitor - Monitor the temperature and return true for exceptions.
545 * @cm: the Charger Manager representing the battery.
546 *
547 * Returns true if there is an event to notify for the battery.
548 * (True if the status of "emergency_stop" changes)
549 */
550static bool _cm_monitor(struct charger_manager *cm)
551{
552 struct charger_desc *desc = cm->desc;
553 int temp = desc->temperature_out_of_range(&cm->last_temp_mC);
554
555 dev_dbg(cm->dev, "monitoring (%2.2d.%3.3dC)\n",
556 cm->last_temp_mC / 1000, cm->last_temp_mC % 1000);
557
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900558 /* It has been stopped already */
559 if (temp && cm->emergency_stop)
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900560 return false;
561
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900562 /*
563 * Check temperature whether overheat or cold.
564 * If temperature is out of range normal state, stop charging.
565 */
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900566 if (temp) {
567 cm->emergency_stop = temp;
568 if (!try_charger_enable(cm, false)) {
569 if (temp > 0)
570 uevent_notify(cm, "OVERHEAT");
571 else
572 uevent_notify(cm, "COLD");
573 }
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900574
575 /*
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700576 * Check whole charging duration and discharing duration
577 * after full-batt.
578 */
579 } else if (!cm->emergency_stop && check_charging_duration(cm)) {
580 dev_dbg(cm->dev,
581 "Charging/Discharging duration is out of range");
582 /*
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900583 * Check dropped voltage of battery. If battery voltage is more
584 * dropped than fullbatt_vchkdrop_uV after fully charged state,
585 * charger-manager have to recharge battery.
586 */
587 } else if (!cm->emergency_stop && is_ext_pwr_online(cm) &&
588 !cm->charger_enabled) {
589 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
590
591 /*
592 * Check whether fully charged state to protect overcharge
593 * if charger-manager is charging for battery.
594 */
595 } else if (!cm->emergency_stop && is_full_charged(cm) &&
596 cm->charger_enabled) {
597 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged.\n");
598 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
599
600 try_charger_enable(cm, false);
601
602 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900603 } else {
604 cm->emergency_stop = 0;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900605 if (is_ext_pwr_online(cm)) {
606 if (!try_charger_enable(cm, true))
607 uevent_notify(cm, "CHARGING");
608 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900609 }
610
611 return true;
612}
613
614/**
615 * cm_monitor - Monitor every battery.
616 *
617 * Returns true if there is an event to notify from any of the batteries.
618 * (True if the status of "emergency_stop" changes)
619 */
620static bool cm_monitor(void)
621{
622 bool stop = false;
623 struct charger_manager *cm;
624
625 mutex_lock(&cm_list_mtx);
626
Axel Linbb2a95c2012-01-12 12:56:35 +0800627 list_for_each_entry(cm, &cm_list, entry) {
628 if (_cm_monitor(cm))
629 stop = true;
630 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900631
632 mutex_unlock(&cm_list_mtx);
633
634 return stop;
635}
636
Chanwoo Choid829dc72012-05-05 06:24:10 -0700637/**
638 * _setup_polling - Setup the next instance of polling.
639 * @work: work_struct of the function _setup_polling.
640 */
641static void _setup_polling(struct work_struct *work)
642{
643 unsigned long min = ULONG_MAX;
644 struct charger_manager *cm;
645 bool keep_polling = false;
646 unsigned long _next_polling;
647
648 mutex_lock(&cm_list_mtx);
649
650 list_for_each_entry(cm, &cm_list, entry) {
651 if (is_polling_required(cm) && cm->desc->polling_interval_ms) {
652 keep_polling = true;
653
654 if (min > cm->desc->polling_interval_ms)
655 min = cm->desc->polling_interval_ms;
656 }
657 }
658
659 polling_jiffy = msecs_to_jiffies(min);
660 if (polling_jiffy <= CM_JIFFIES_SMALL)
661 polling_jiffy = CM_JIFFIES_SMALL + 1;
662
663 if (!keep_polling)
664 polling_jiffy = ULONG_MAX;
665 if (polling_jiffy == ULONG_MAX)
666 goto out;
667
668 WARN(cm_wq == NULL, "charger-manager: workqueue not initialized"
669 ". try it later. %s\n", __func__);
670
671 _next_polling = jiffies + polling_jiffy;
672
673 if (!delayed_work_pending(&cm_monitor_work) ||
674 (delayed_work_pending(&cm_monitor_work) &&
675 time_after(next_polling, _next_polling))) {
676 cancel_delayed_work_sync(&cm_monitor_work);
677 next_polling = jiffies + polling_jiffy;
678 queue_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy);
679 }
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
713 if (delayed_work_pending(&cm->fullbatt_vchk_work))
714 cancel_delayed_work(&cm->fullbatt_vchk_work);
715 queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
716 msecs_to_jiffies(desc->fullbatt_vchkdrop_ms));
717 cm->fullbatt_vchk_jiffies_at = jiffies + msecs_to_jiffies(
718 desc->fullbatt_vchkdrop_ms);
719
720 if (cm->fullbatt_vchk_jiffies_at == 0)
721 cm->fullbatt_vchk_jiffies_at = 1;
722
723out:
724 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged.\n");
725 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
726}
727
728/**
729 * battout_handler - Event handler for CM_EVENT_BATT_OUT
730 * @cm: the Charger Manager representing the battery.
731 */
732static void battout_handler(struct charger_manager *cm)
733{
734 if (cm_suspended)
735 device_set_wakeup_capable(cm->dev, true);
736
737 if (!is_batt_present(cm)) {
738 dev_emerg(cm->dev, "Battery Pulled Out!\n");
739 uevent_notify(cm, default_event_names[CM_EVENT_BATT_OUT]);
740 } else {
741 uevent_notify(cm, "Battery Reinserted?");
742 }
743}
744
745/**
746 * misc_event_handler - Handler for other evnets
747 * @cm: the Charger Manager representing the battery.
748 * @type: the Charger Manager representing the battery.
749 */
750static void misc_event_handler(struct charger_manager *cm,
751 enum cm_event_types type)
752{
753 if (cm_suspended)
754 device_set_wakeup_capable(cm->dev, true);
755
756 if (!delayed_work_pending(&cm_monitor_work) &&
757 is_polling_required(cm) && cm->desc->polling_interval_ms)
758 schedule_work(&setup_polling);
759 uevent_notify(cm, default_event_names[type]);
760}
761
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900762static int charger_get_property(struct power_supply *psy,
763 enum power_supply_property psp,
764 union power_supply_propval *val)
765{
766 struct charger_manager *cm = container_of(psy,
767 struct charger_manager, charger_psy);
768 struct charger_desc *desc = cm->desc;
Anton Vorontsovdf58c042012-03-15 21:01:28 +0400769 int ret = 0;
770 int uV;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900771
772 switch (psp) {
773 case POWER_SUPPLY_PROP_STATUS:
774 if (is_charging(cm))
775 val->intval = POWER_SUPPLY_STATUS_CHARGING;
776 else if (is_ext_pwr_online(cm))
777 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
778 else
779 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
780 break;
781 case POWER_SUPPLY_PROP_HEALTH:
782 if (cm->emergency_stop > 0)
783 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
784 else if (cm->emergency_stop < 0)
785 val->intval = POWER_SUPPLY_HEALTH_COLD;
786 else
787 val->intval = POWER_SUPPLY_HEALTH_GOOD;
788 break;
789 case POWER_SUPPLY_PROP_PRESENT:
790 if (is_batt_present(cm))
791 val->intval = 1;
792 else
793 val->intval = 0;
794 break;
795 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Anton Vorontsovdf58c042012-03-15 21:01:28 +0400796 ret = get_batt_uV(cm, &val->intval);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900797 break;
798 case POWER_SUPPLY_PROP_CURRENT_NOW:
799 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
800 POWER_SUPPLY_PROP_CURRENT_NOW, val);
801 break;
802 case POWER_SUPPLY_PROP_TEMP:
803 /* in thenth of centigrade */
804 if (cm->last_temp_mC == INT_MIN)
805 desc->temperature_out_of_range(&cm->last_temp_mC);
806 val->intval = cm->last_temp_mC / 100;
807 if (!desc->measure_battery_temp)
808 ret = -ENODEV;
809 break;
810 case POWER_SUPPLY_PROP_TEMP_AMBIENT:
811 /* in thenth of centigrade */
812 if (cm->last_temp_mC == INT_MIN)
813 desc->temperature_out_of_range(&cm->last_temp_mC);
814 val->intval = cm->last_temp_mC / 100;
815 if (desc->measure_battery_temp)
816 ret = -ENODEV;
817 break;
818 case POWER_SUPPLY_PROP_CAPACITY:
819 if (!cm->fuel_gauge) {
820 ret = -ENODEV;
821 break;
822 }
823
824 if (!is_batt_present(cm)) {
825 /* There is no battery. Assume 100% */
826 val->intval = 100;
827 break;
828 }
829
830 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
831 POWER_SUPPLY_PROP_CAPACITY, val);
832 if (ret)
833 break;
834
835 if (val->intval > 100) {
836 val->intval = 100;
837 break;
838 }
839 if (val->intval < 0)
840 val->intval = 0;
841
842 /* Do not adjust SOC when charging: voltage is overrated */
843 if (is_charging(cm))
844 break;
845
846 /*
847 * If the capacity value is inconsistent, calibrate it base on
848 * the battery voltage values and the thresholds given as desc
849 */
850 ret = get_batt_uV(cm, &uV);
851 if (ret) {
852 /* Voltage information not available. No calibration */
853 ret = 0;
854 break;
855 }
856
857 if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
858 !is_charging(cm)) {
859 val->intval = 100;
860 break;
861 }
862
863 break;
864 case POWER_SUPPLY_PROP_ONLINE:
865 if (is_ext_pwr_online(cm))
866 val->intval = 1;
867 else
868 val->intval = 0;
869 break;
870 case POWER_SUPPLY_PROP_CHARGE_FULL:
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900871 if (is_full_charged(cm))
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900872 val->intval = 1;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900873 else
874 val->intval = 0;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900875 ret = 0;
876 break;
877 case POWER_SUPPLY_PROP_CHARGE_NOW:
878 if (is_charging(cm)) {
879 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
880 POWER_SUPPLY_PROP_CHARGE_NOW,
881 val);
882 if (ret) {
883 val->intval = 1;
884 ret = 0;
885 } else {
886 /* If CHARGE_NOW is supplied, use it */
887 val->intval = (val->intval > 0) ?
888 val->intval : 1;
889 }
890 } else {
891 val->intval = 0;
892 }
893 break;
894 default:
895 return -EINVAL;
896 }
897 return ret;
898}
899
900#define NUM_CHARGER_PSY_OPTIONAL (4)
901static enum power_supply_property default_charger_props[] = {
902 /* Guaranteed to provide */
903 POWER_SUPPLY_PROP_STATUS,
904 POWER_SUPPLY_PROP_HEALTH,
905 POWER_SUPPLY_PROP_PRESENT,
906 POWER_SUPPLY_PROP_VOLTAGE_NOW,
907 POWER_SUPPLY_PROP_CAPACITY,
908 POWER_SUPPLY_PROP_ONLINE,
909 POWER_SUPPLY_PROP_CHARGE_FULL,
910 /*
911 * Optional properties are:
912 * POWER_SUPPLY_PROP_CHARGE_NOW,
913 * POWER_SUPPLY_PROP_CURRENT_NOW,
914 * POWER_SUPPLY_PROP_TEMP, and
915 * POWER_SUPPLY_PROP_TEMP_AMBIENT,
916 */
917};
918
919static struct power_supply psy_default = {
920 .name = "battery",
921 .type = POWER_SUPPLY_TYPE_BATTERY,
922 .properties = default_charger_props,
923 .num_properties = ARRAY_SIZE(default_charger_props),
924 .get_property = charger_get_property,
925};
926
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900927/**
928 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
929 * for suspend_again.
930 *
931 * Returns true if the alarm is set for Charger Manager to use.
932 * Returns false if
933 * cm_setup_timer fails to set an alarm,
934 * cm_setup_timer does not need to set an alarm for Charger Manager,
935 * or an alarm previously configured is to be used.
936 */
937static bool cm_setup_timer(void)
938{
939 struct charger_manager *cm;
940 unsigned int wakeup_ms = UINT_MAX;
941 bool ret = false;
942
943 mutex_lock(&cm_list_mtx);
944
945 list_for_each_entry(cm, &cm_list, entry) {
Chanwoo Choid829dc72012-05-05 06:24:10 -0700946 unsigned int fbchk_ms = 0;
947
948 /* fullbatt_vchk is required. setup timer for that */
949 if (cm->fullbatt_vchk_jiffies_at) {
950 fbchk_ms = jiffies_to_msecs(cm->fullbatt_vchk_jiffies_at
951 - jiffies);
952 if (time_is_before_eq_jiffies(
953 cm->fullbatt_vchk_jiffies_at) ||
954 msecs_to_jiffies(fbchk_ms) < CM_JIFFIES_SMALL) {
955 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
956 fbchk_ms = 0;
957 }
958 }
959 CM_MIN_VALID(wakeup_ms, fbchk_ms);
960
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900961 /* Skip if polling is not required for this CM */
962 if (!is_polling_required(cm) && !cm->emergency_stop)
963 continue;
964 if (cm->desc->polling_interval_ms == 0)
965 continue;
966 CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
967 }
968
969 mutex_unlock(&cm_list_mtx);
970
971 if (wakeup_ms < UINT_MAX && wakeup_ms > 0) {
972 pr_info("Charger Manager wakeup timer: %u ms.\n", wakeup_ms);
973 if (rtc_dev) {
974 struct rtc_wkalrm tmp;
975 unsigned long time, now;
976 unsigned long add = DIV_ROUND_UP(wakeup_ms, 1000);
977
978 /*
979 * Set alarm with the polling interval (wakeup_ms)
980 * except when rtc_wkalarm_save comes first.
981 * However, the alarm time should be NOW +
982 * CM_RTC_SMALL or later.
983 */
984 tmp.enabled = 1;
985 rtc_read_time(rtc_dev, &tmp.time);
986 rtc_tm_to_time(&tmp.time, &now);
987 if (add < CM_RTC_SMALL)
988 add = CM_RTC_SMALL;
989 time = now + add;
990
991 ret = true;
992
993 if (rtc_wkalarm_save.enabled &&
994 rtc_wkalarm_save_time &&
995 rtc_wkalarm_save_time < time) {
996 if (rtc_wkalarm_save_time < now + CM_RTC_SMALL)
997 time = now + CM_RTC_SMALL;
998 else
999 time = rtc_wkalarm_save_time;
1000
1001 /* The timer is not appointed by CM */
1002 ret = false;
1003 }
1004
1005 pr_info("Waking up after %lu secs.\n",
1006 time - now);
1007
1008 rtc_time_to_tm(time, &tmp.time);
1009 rtc_set_alarm(rtc_dev, &tmp);
1010 cm_suspend_duration_ms += wakeup_ms;
1011 return ret;
1012 }
1013 }
1014
1015 if (rtc_dev)
1016 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1017 return false;
1018}
1019
Chanwoo Choid829dc72012-05-05 06:24:10 -07001020static void _cm_fbchk_in_suspend(struct charger_manager *cm)
1021{
1022 unsigned long jiffy_now = jiffies;
1023
1024 if (!cm->fullbatt_vchk_jiffies_at)
1025 return;
1026
1027 if (g_desc && g_desc->assume_timer_stops_in_suspend)
1028 jiffy_now += msecs_to_jiffies(cm_suspend_duration_ms);
1029
1030 /* Execute now if it's going to be executed not too long after */
1031 jiffy_now += CM_JIFFIES_SMALL;
1032
1033 if (time_after_eq(jiffy_now, cm->fullbatt_vchk_jiffies_at))
1034 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
1035}
1036
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001037/**
1038 * cm_suspend_again - Determine whether suspend again or not
1039 *
1040 * Returns true if the system should be suspended again
1041 * Returns false if the system should be woken up
1042 */
1043bool cm_suspend_again(void)
1044{
1045 struct charger_manager *cm;
1046 bool ret = false;
1047
1048 if (!g_desc || !g_desc->rtc_only_wakeup || !g_desc->rtc_only_wakeup() ||
1049 !cm_rtc_set)
1050 return false;
1051
1052 if (cm_monitor())
1053 goto out;
1054
1055 ret = true;
1056 mutex_lock(&cm_list_mtx);
1057 list_for_each_entry(cm, &cm_list, entry) {
Chanwoo Choid829dc72012-05-05 06:24:10 -07001058 _cm_fbchk_in_suspend(cm);
1059
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001060 if (cm->status_save_ext_pwr_inserted != is_ext_pwr_online(cm) ||
Axel Linbb2a95c2012-01-12 12:56:35 +08001061 cm->status_save_batt != is_batt_present(cm)) {
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001062 ret = false;
Axel Linbb2a95c2012-01-12 12:56:35 +08001063 break;
1064 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001065 }
1066 mutex_unlock(&cm_list_mtx);
1067
1068 cm_rtc_set = cm_setup_timer();
1069out:
1070 /* It's about the time when the non-CM appointed timer goes off */
1071 if (rtc_wkalarm_save.enabled) {
1072 unsigned long now;
1073 struct rtc_time tmp;
1074
1075 rtc_read_time(rtc_dev, &tmp);
1076 rtc_tm_to_time(&tmp, &now);
1077
1078 if (rtc_wkalarm_save_time &&
1079 now + CM_RTC_SMALL >= rtc_wkalarm_save_time)
1080 return false;
1081 }
1082 return ret;
1083}
1084EXPORT_SYMBOL_GPL(cm_suspend_again);
1085
1086/**
1087 * setup_charger_manager - initialize charger_global_desc data
1088 * @gd: pointer to instance of charger_global_desc
1089 */
1090int setup_charger_manager(struct charger_global_desc *gd)
1091{
1092 if (!gd)
1093 return -EINVAL;
1094
1095 if (rtc_dev)
1096 rtc_class_close(rtc_dev);
1097 rtc_dev = NULL;
1098 g_desc = NULL;
1099
1100 if (!gd->rtc_only_wakeup) {
1101 pr_err("The callback rtc_only_wakeup is not given.\n");
1102 return -EINVAL;
1103 }
1104
1105 if (gd->rtc_name) {
1106 rtc_dev = rtc_class_open(gd->rtc_name);
1107 if (IS_ERR_OR_NULL(rtc_dev)) {
1108 rtc_dev = NULL;
1109 /* Retry at probe. RTC may be not registered yet */
1110 }
1111 } else {
1112 pr_warn("No wakeup timer is given for charger manager."
1113 "In-suspend monitoring won't work.\n");
1114 }
1115
1116 g_desc = gd;
1117 return 0;
1118}
1119EXPORT_SYMBOL_GPL(setup_charger_manager);
1120
Chanwoo Choibee737b2012-07-12 15:03:25 +09001121/**
1122 * charger_extcon_work - enable/diable charger according to the state
1123 * of charger cable
1124 *
1125 * @work: work_struct of the function charger_extcon_work.
1126 */
1127static void charger_extcon_work(struct work_struct *work)
1128{
1129 struct charger_cable *cable =
1130 container_of(work, struct charger_cable, wq);
Chanwoo Choi45cd4fb2012-07-12 15:03:29 +09001131 int ret;
1132
1133 if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) {
1134 ret = regulator_set_current_limit(cable->charger->consumer,
1135 cable->min_uA, cable->max_uA);
1136 if (ret < 0) {
1137 pr_err("Cannot set current limit of %s (%s)\n",
1138 cable->charger->regulator_name, cable->name);
1139 return;
1140 }
1141
1142 pr_info("Set current limit of %s : %duA ~ %duA\n",
1143 cable->charger->regulator_name,
1144 cable->min_uA, cable->max_uA);
1145 }
Chanwoo Choibee737b2012-07-12 15:03:25 +09001146
1147 try_charger_enable(cable->cm, cable->attached);
1148}
1149
1150/**
1151 * charger_extcon_notifier - receive the state of charger cable
1152 * when registered cable is attached or detached.
1153 *
1154 * @self: the notifier block of the charger_extcon_notifier.
1155 * @event: the cable state.
1156 * @ptr: the data pointer of notifier block.
1157 */
1158static int charger_extcon_notifier(struct notifier_block *self,
1159 unsigned long event, void *ptr)
1160{
1161 struct charger_cable *cable =
1162 container_of(self, struct charger_cable, nb);
1163
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001164 /*
1165 * The newly state of charger cable.
1166 * If cable is attached, cable->attached is true.
1167 */
Chanwoo Choibee737b2012-07-12 15:03:25 +09001168 cable->attached = event;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001169
1170 /*
1171 * Setup monitoring to check battery state
1172 * when charger cable is attached.
1173 */
1174 if (cable->attached && is_polling_required(cable->cm)) {
1175 if (work_pending(&setup_polling))
1176 cancel_work_sync(&setup_polling);
1177 schedule_work(&setup_polling);
1178 }
1179
1180 /*
1181 * Setup work for controlling charger(regulator)
1182 * according to charger cable.
1183 */
Chanwoo Choibee737b2012-07-12 15:03:25 +09001184 schedule_work(&cable->wq);
1185
1186 return NOTIFY_DONE;
1187}
1188
1189/**
1190 * charger_extcon_init - register external connector to use it
1191 * as the charger cable
1192 *
1193 * @cm: the Charger Manager representing the battery.
1194 * @cable: the Charger cable representing the external connector.
1195 */
1196static int charger_extcon_init(struct charger_manager *cm,
1197 struct charger_cable *cable)
1198{
1199 int ret = 0;
1200
1201 /*
1202 * Charger manager use Extcon framework to identify
1203 * the charger cable among various external connector
1204 * cable (e.g., TA, USB, MHL, Dock).
1205 */
1206 INIT_WORK(&cable->wq, charger_extcon_work);
1207 cable->nb.notifier_call = charger_extcon_notifier;
1208 ret = extcon_register_interest(&cable->extcon_dev,
1209 cable->extcon_name, cable->name, &cable->nb);
1210 if (ret < 0) {
1211 pr_info("Cannot register extcon_dev for %s(cable: %s).\n",
1212 cable->extcon_name,
1213 cable->name);
1214 ret = -EINVAL;
1215 }
1216
1217 return ret;
1218}
1219
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001220static int charger_manager_probe(struct platform_device *pdev)
1221{
1222 struct charger_desc *desc = dev_get_platdata(&pdev->dev);
1223 struct charger_manager *cm;
1224 int ret = 0, i = 0;
Chanwoo Choibee737b2012-07-12 15:03:25 +09001225 int j = 0;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001226 union power_supply_propval val;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001227
1228 if (g_desc && !rtc_dev && g_desc->rtc_name) {
1229 rtc_dev = rtc_class_open(g_desc->rtc_name);
1230 if (IS_ERR_OR_NULL(rtc_dev)) {
1231 rtc_dev = NULL;
1232 dev_err(&pdev->dev, "Cannot get RTC %s.\n",
1233 g_desc->rtc_name);
1234 ret = -ENODEV;
1235 goto err_alloc;
1236 }
1237 }
1238
1239 if (!desc) {
1240 dev_err(&pdev->dev, "No platform data (desc) found.\n");
1241 ret = -ENODEV;
1242 goto err_alloc;
1243 }
1244
1245 cm = kzalloc(sizeof(struct charger_manager), GFP_KERNEL);
1246 if (!cm) {
1247 dev_err(&pdev->dev, "Cannot allocate memory.\n");
1248 ret = -ENOMEM;
1249 goto err_alloc;
1250 }
1251
1252 /* Basic Values. Unspecified are Null or 0 */
1253 cm->dev = &pdev->dev;
1254 cm->desc = kzalloc(sizeof(struct charger_desc), GFP_KERNEL);
1255 if (!cm->desc) {
1256 dev_err(&pdev->dev, "Cannot allocate memory.\n");
1257 ret = -ENOMEM;
1258 goto err_alloc_desc;
1259 }
1260 memcpy(cm->desc, desc, sizeof(struct charger_desc));
1261 cm->last_temp_mC = INT_MIN; /* denotes "unmeasured, yet" */
1262
Chanwoo Choid829dc72012-05-05 06:24:10 -07001263 /*
1264 * The following two do not need to be errors.
1265 * Users may intentionally ignore those two features.
1266 */
1267 if (desc->fullbatt_uV == 0) {
1268 dev_info(&pdev->dev, "Ignoring full-battery voltage threshold"
1269 " as it is not supplied.");
1270 }
1271 if (!desc->fullbatt_vchkdrop_ms || !desc->fullbatt_vchkdrop_uV) {
1272 dev_info(&pdev->dev, "Disabling full-battery voltage drop "
1273 "checking mechanism as it is not supplied.");
1274 desc->fullbatt_vchkdrop_ms = 0;
1275 desc->fullbatt_vchkdrop_uV = 0;
1276 }
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +09001277 if (desc->fullbatt_soc == 0) {
1278 dev_info(&pdev->dev, "Ignoring full-battery soc(state of"
1279 " charge) threshold as it is not"
1280 " supplied.");
1281 }
1282 if (desc->fullbatt_full_capacity == 0) {
1283 dev_info(&pdev->dev, "Ignoring full-battery full capacity"
1284 " threshold as it is not supplied.");
1285 }
Chanwoo Choid829dc72012-05-05 06:24:10 -07001286
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001287 if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
1288 ret = -EINVAL;
1289 dev_err(&pdev->dev, "charger_regulators undefined.\n");
1290 goto err_no_charger;
1291 }
1292
1293 if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
1294 dev_err(&pdev->dev, "No power supply defined.\n");
1295 ret = -EINVAL;
1296 goto err_no_charger_stat;
1297 }
1298
1299 /* Counting index only */
1300 while (desc->psy_charger_stat[i])
1301 i++;
1302
1303 cm->charger_stat = kzalloc(sizeof(struct power_supply *) * (i + 1),
1304 GFP_KERNEL);
1305 if (!cm->charger_stat) {
1306 ret = -ENOMEM;
1307 goto err_no_charger_stat;
1308 }
1309
1310 for (i = 0; desc->psy_charger_stat[i]; i++) {
1311 cm->charger_stat[i] = power_supply_get_by_name(
1312 desc->psy_charger_stat[i]);
1313 if (!cm->charger_stat[i]) {
1314 dev_err(&pdev->dev, "Cannot find power supply "
1315 "\"%s\"\n",
1316 desc->psy_charger_stat[i]);
1317 ret = -ENODEV;
1318 goto err_chg_stat;
1319 }
1320 }
1321
1322 cm->fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
1323 if (!cm->fuel_gauge) {
1324 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
1325 desc->psy_fuel_gauge);
1326 ret = -ENODEV;
1327 goto err_chg_stat;
1328 }
1329
1330 if (desc->polling_interval_ms == 0 ||
1331 msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) {
1332 dev_err(&pdev->dev, "polling_interval_ms is too small\n");
1333 ret = -EINVAL;
1334 goto err_chg_stat;
1335 }
1336
1337 if (!desc->temperature_out_of_range) {
1338 dev_err(&pdev->dev, "there is no temperature_out_of_range\n");
1339 ret = -EINVAL;
1340 goto err_chg_stat;
1341 }
1342
Chanwoo Choi8fcfe082012-09-20 21:20:05 -07001343 if (!desc->charging_max_duration_ms ||
1344 !desc->discharging_max_duration_ms) {
1345 dev_info(&pdev->dev, "Cannot limit charging duration "
1346 "checking mechanism to prevent overcharge/overheat "
1347 "and control discharging duration");
1348 desc->charging_max_duration_ms = 0;
1349 desc->discharging_max_duration_ms = 0;
1350 }
1351
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001352 platform_set_drvdata(pdev, cm);
1353
Axel Linbb2a95c2012-01-12 12:56:35 +08001354 memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default));
1355
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001356 if (!desc->psy_name) {
Axel Linbb2a95c2012-01-12 12:56:35 +08001357 strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001358 } else {
1359 strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
1360 }
1361 cm->charger_psy.name = cm->psy_name_buf;
1362
1363 /* Allocate for psy properties because they may vary */
1364 cm->charger_psy.properties = kzalloc(sizeof(enum power_supply_property)
1365 * (ARRAY_SIZE(default_charger_props) +
1366 NUM_CHARGER_PSY_OPTIONAL),
1367 GFP_KERNEL);
1368 if (!cm->charger_psy.properties) {
1369 dev_err(&pdev->dev, "Cannot allocate for psy properties.\n");
1370 ret = -ENOMEM;
1371 goto err_chg_stat;
1372 }
1373 memcpy(cm->charger_psy.properties, default_charger_props,
1374 sizeof(enum power_supply_property) *
1375 ARRAY_SIZE(default_charger_props));
1376 cm->charger_psy.num_properties = psy_default.num_properties;
1377
1378 /* Find which optional psy-properties are available */
1379 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1380 POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
1381 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1382 POWER_SUPPLY_PROP_CHARGE_NOW;
1383 cm->charger_psy.num_properties++;
1384 }
1385 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1386 POWER_SUPPLY_PROP_CURRENT_NOW,
1387 &val)) {
1388 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1389 POWER_SUPPLY_PROP_CURRENT_NOW;
1390 cm->charger_psy.num_properties++;
1391 }
Axel Linbb2a95c2012-01-12 12:56:35 +08001392
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001393 if (desc->measure_battery_temp) {
1394 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1395 POWER_SUPPLY_PROP_TEMP;
1396 cm->charger_psy.num_properties++;
Axel Linbb2a95c2012-01-12 12:56:35 +08001397 } else {
1398 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1399 POWER_SUPPLY_PROP_TEMP_AMBIENT;
1400 cm->charger_psy.num_properties++;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001401 }
1402
Chanwoo Choid829dc72012-05-05 06:24:10 -07001403 INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk);
1404
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001405 ret = power_supply_register(NULL, &cm->charger_psy);
1406 if (ret) {
1407 dev_err(&pdev->dev, "Cannot register charger-manager with"
1408 " name \"%s\".\n", cm->charger_psy.name);
1409 goto err_register;
1410 }
1411
Chanwoo Choibee737b2012-07-12 15:03:25 +09001412 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1413 struct charger_regulator *charger
1414 = &desc->charger_regulators[i];
1415
1416 charger->consumer = regulator_get(&pdev->dev,
1417 charger->regulator_name);
1418 if (charger->consumer == NULL) {
1419 dev_err(&pdev->dev, "Cannot find charger(%s)n",
1420 charger->regulator_name);
1421 ret = -EINVAL;
1422 goto err_chg_get;
1423 }
1424
1425 for (j = 0 ; j < charger->num_cables ; j++) {
1426 struct charger_cable *cable = &charger->cables[j];
1427
1428 ret = charger_extcon_init(cm, cable);
1429 if (ret < 0) {
1430 dev_err(&pdev->dev, "Cannot find charger(%s)n",
1431 charger->regulator_name);
1432 goto err_extcon;
1433 }
1434 cable->charger = charger;
1435 cable->cm = cm;
1436 }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001437 }
1438
1439 ret = try_charger_enable(cm, true);
1440 if (ret) {
1441 dev_err(&pdev->dev, "Cannot enable charger regulators\n");
1442 goto err_chg_enable;
1443 }
1444
1445 /* Add to the list */
1446 mutex_lock(&cm_list_mtx);
1447 list_add(&cm->entry, &cm_list);
1448 mutex_unlock(&cm_list_mtx);
1449
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001450 /*
1451 * Charger-manager is capable of waking up the systme from sleep
1452 * when event is happend through cm_notify_event()
1453 */
1454 device_init_wakeup(&pdev->dev, true);
1455 device_set_wakeup_capable(&pdev->dev, false);
1456
Chanwoo Choid829dc72012-05-05 06:24:10 -07001457 schedule_work(&setup_polling);
1458
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001459 return 0;
1460
1461err_chg_enable:
Chanwoo Choibee737b2012-07-12 15:03:25 +09001462err_extcon:
1463 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1464 struct charger_regulator *charger
1465 = &desc->charger_regulators[i];
1466 for (j = 0 ; j < charger->num_cables ; j++) {
1467 struct charger_cable *cable = &charger->cables[j];
1468 extcon_unregister_interest(&cable->extcon_dev);
1469 }
1470 }
1471err_chg_get:
1472 for (i = 0 ; i < desc->num_charger_regulators ; i++)
1473 regulator_put(desc->charger_regulators[i].consumer);
1474
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001475 power_supply_unregister(&cm->charger_psy);
1476err_register:
1477 kfree(cm->charger_psy.properties);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001478err_chg_stat:
1479 kfree(cm->charger_stat);
1480err_no_charger_stat:
1481err_no_charger:
1482 kfree(cm->desc);
1483err_alloc_desc:
1484 kfree(cm);
1485err_alloc:
1486 return ret;
1487}
1488
1489static int __devexit charger_manager_remove(struct platform_device *pdev)
1490{
1491 struct charger_manager *cm = platform_get_drvdata(pdev);
1492 struct charger_desc *desc = cm->desc;
Chanwoo Choibee737b2012-07-12 15:03:25 +09001493 int i = 0;
1494 int j = 0;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001495
1496 /* Remove from the list */
1497 mutex_lock(&cm_list_mtx);
1498 list_del(&cm->entry);
1499 mutex_unlock(&cm_list_mtx);
1500
Chanwoo Choid829dc72012-05-05 06:24:10 -07001501 if (work_pending(&setup_polling))
1502 cancel_work_sync(&setup_polling);
1503 if (delayed_work_pending(&cm_monitor_work))
1504 cancel_delayed_work_sync(&cm_monitor_work);
1505
Chanwoo Choibee737b2012-07-12 15:03:25 +09001506 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1507 struct charger_regulator *charger
1508 = &desc->charger_regulators[i];
1509 for (j = 0 ; j < charger->num_cables ; j++) {
1510 struct charger_cable *cable = &charger->cables[j];
1511 extcon_unregister_interest(&cable->extcon_dev);
1512 }
1513 }
1514
1515 for (i = 0 ; i < desc->num_charger_regulators ; i++)
1516 regulator_put(desc->charger_regulators[i].consumer);
1517
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001518 power_supply_unregister(&cm->charger_psy);
Chanwoo Choid829dc72012-05-05 06:24:10 -07001519
1520 try_charger_enable(cm, false);
1521
Donggeun Kimad3d13ee2011-12-27 18:47:49 +09001522 kfree(cm->charger_psy.properties);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001523 kfree(cm->charger_stat);
1524 kfree(cm->desc);
1525 kfree(cm);
1526
1527 return 0;
1528}
1529
Axel Lin1bbe24d2012-01-11 17:19:45 +08001530static const struct platform_device_id charger_manager_id[] = {
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001531 { "charger-manager", 0 },
1532 { },
1533};
Axel Lin1bbe24d2012-01-11 17:19:45 +08001534MODULE_DEVICE_TABLE(platform, charger_manager_id);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001535
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001536static int cm_suspend_noirq(struct device *dev)
1537{
1538 int ret = 0;
1539
1540 if (device_may_wakeup(dev)) {
1541 device_set_wakeup_capable(dev, false);
1542 ret = -EAGAIN;
1543 }
1544
1545 return ret;
1546}
1547
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001548static int cm_suspend_prepare(struct device *dev)
1549{
Axel Linbb2a95c2012-01-12 12:56:35 +08001550 struct charger_manager *cm = dev_get_drvdata(dev);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001551
1552 if (!cm_suspended) {
1553 if (rtc_dev) {
1554 struct rtc_time tmp;
1555 unsigned long now;
1556
1557 rtc_read_alarm(rtc_dev, &rtc_wkalarm_save);
1558 rtc_read_time(rtc_dev, &tmp);
1559
1560 if (rtc_wkalarm_save.enabled) {
1561 rtc_tm_to_time(&rtc_wkalarm_save.time,
1562 &rtc_wkalarm_save_time);
1563 rtc_tm_to_time(&tmp, &now);
1564 if (now > rtc_wkalarm_save_time)
1565 rtc_wkalarm_save_time = 0;
1566 } else {
1567 rtc_wkalarm_save_time = 0;
1568 }
1569 }
1570 cm_suspended = true;
1571 }
1572
Chanwoo Choid829dc72012-05-05 06:24:10 -07001573 if (delayed_work_pending(&cm->fullbatt_vchk_work))
1574 cancel_delayed_work(&cm->fullbatt_vchk_work);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001575 cm->status_save_ext_pwr_inserted = is_ext_pwr_online(cm);
1576 cm->status_save_batt = is_batt_present(cm);
1577
1578 if (!cm_rtc_set) {
1579 cm_suspend_duration_ms = 0;
1580 cm_rtc_set = cm_setup_timer();
1581 }
1582
1583 return 0;
1584}
1585
1586static void cm_suspend_complete(struct device *dev)
1587{
Axel Linbb2a95c2012-01-12 12:56:35 +08001588 struct charger_manager *cm = dev_get_drvdata(dev);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001589
1590 if (cm_suspended) {
1591 if (rtc_dev) {
1592 struct rtc_wkalrm tmp;
1593
1594 rtc_read_alarm(rtc_dev, &tmp);
1595 rtc_wkalarm_save.pending = tmp.pending;
1596 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1597 }
1598 cm_suspended = false;
1599 cm_rtc_set = false;
1600 }
1601
Chanwoo Choid829dc72012-05-05 06:24:10 -07001602 /* Re-enqueue delayed work (fullbatt_vchk_work) */
1603 if (cm->fullbatt_vchk_jiffies_at) {
1604 unsigned long delay = 0;
1605 unsigned long now = jiffies + CM_JIFFIES_SMALL;
1606
1607 if (time_after_eq(now, cm->fullbatt_vchk_jiffies_at)) {
1608 delay = (unsigned long)((long)now
1609 - (long)(cm->fullbatt_vchk_jiffies_at));
1610 delay = jiffies_to_msecs(delay);
1611 } else {
1612 delay = 0;
1613 }
1614
1615 /*
1616 * Account for cm_suspend_duration_ms if
1617 * assume_timer_stops_in_suspend is active
1618 */
1619 if (g_desc && g_desc->assume_timer_stops_in_suspend) {
1620 if (delay > cm_suspend_duration_ms)
1621 delay -= cm_suspend_duration_ms;
1622 else
1623 delay = 0;
1624 }
1625
1626 queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
1627 msecs_to_jiffies(delay));
1628 }
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001629 device_set_wakeup_capable(cm->dev, false);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001630 uevent_notify(cm, NULL);
1631}
1632
1633static const struct dev_pm_ops charger_manager_pm = {
1634 .prepare = cm_suspend_prepare,
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001635 .suspend_noirq = cm_suspend_noirq,
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001636 .complete = cm_suspend_complete,
1637};
1638
1639static struct platform_driver charger_manager_driver = {
1640 .driver = {
1641 .name = "charger-manager",
1642 .owner = THIS_MODULE,
1643 .pm = &charger_manager_pm,
1644 },
1645 .probe = charger_manager_probe,
1646 .remove = __devexit_p(charger_manager_remove),
1647 .id_table = charger_manager_id,
1648};
1649
1650static int __init charger_manager_init(void)
1651{
Chanwoo Choid829dc72012-05-05 06:24:10 -07001652 cm_wq = create_freezable_workqueue("charger_manager");
1653 INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller);
1654
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001655 return platform_driver_register(&charger_manager_driver);
1656}
1657late_initcall(charger_manager_init);
1658
1659static void __exit charger_manager_cleanup(void)
1660{
Chanwoo Choid829dc72012-05-05 06:24:10 -07001661 destroy_workqueue(cm_wq);
1662 cm_wq = NULL;
1663
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001664 platform_driver_unregister(&charger_manager_driver);
1665}
1666module_exit(charger_manager_cleanup);
1667
Chanwoo Choidfeccb12012-05-05 06:26:47 -07001668/**
1669 * find_power_supply - find the associated power_supply of charger
1670 * @cm: the Charger Manager representing the battery
1671 * @psy: pointer to instance of charger's power_supply
1672 */
1673static bool find_power_supply(struct charger_manager *cm,
1674 struct power_supply *psy)
1675{
1676 int i;
1677 bool found = false;
1678
1679 for (i = 0; cm->charger_stat[i]; i++) {
1680 if (psy == cm->charger_stat[i]) {
1681 found = true;
1682 break;
1683 }
1684 }
1685
1686 return found;
1687}
1688
1689/**
1690 * cm_notify_event - charger driver notify Charger Manager of charger event
1691 * @psy: pointer to instance of charger's power_supply
1692 * @type: type of charger event
1693 * @msg: optional message passed to uevent_notify fuction
1694 */
1695void cm_notify_event(struct power_supply *psy, enum cm_event_types type,
1696 char *msg)
1697{
1698 struct charger_manager *cm;
1699 bool found_power_supply = false;
1700
1701 if (psy == NULL)
1702 return;
1703
1704 mutex_lock(&cm_list_mtx);
1705 list_for_each_entry(cm, &cm_list, entry) {
1706 found_power_supply = find_power_supply(cm, psy);
1707 if (found_power_supply)
1708 break;
1709 }
1710 mutex_unlock(&cm_list_mtx);
1711
1712 if (!found_power_supply)
1713 return;
1714
1715 switch (type) {
1716 case CM_EVENT_BATT_FULL:
1717 fullbatt_handler(cm);
1718 break;
1719 case CM_EVENT_BATT_OUT:
1720 battout_handler(cm);
1721 break;
1722 case CM_EVENT_BATT_IN:
1723 case CM_EVENT_EXT_PWR_IN_OUT ... CM_EVENT_CHG_START_STOP:
1724 misc_event_handler(cm, type);
1725 break;
1726 case CM_EVENT_UNKNOWN:
1727 case CM_EVENT_OTHERS:
1728 uevent_notify(cm, msg ? msg : default_event_names[type]);
1729 break;
1730 default:
1731 dev_err(cm->dev, "%s type not specified.\n", __func__);
1732 break;
1733 }
1734}
1735EXPORT_SYMBOL_GPL(cm_notify_event);
1736
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001737MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1738MODULE_DESCRIPTION("Charger Manager");
1739MODULE_LICENSE("GPL");