power: remove use of __devinit
[deliverable/linux.git] / drivers / power / charger-manager.c
CommitLineData
3bb3dbbd
DK
1/*
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>
3950c786 25#include <linux/sysfs.h>
3bb3dbbd 26
dfeccb12
CC
27static const char * const default_event_names[] = {
28 [CM_EVENT_UNKNOWN] = "Unknown",
29 [CM_EVENT_BATT_FULL] = "Battery Full",
30 [CM_EVENT_BATT_IN] = "Battery Inserted",
31 [CM_EVENT_BATT_OUT] = "Battery Pulled Out",
32 [CM_EVENT_EXT_PWR_IN_OUT] = "External Power Attach/Detach",
33 [CM_EVENT_CHG_START_STOP] = "Charging Start/Stop",
34 [CM_EVENT_OTHERS] = "Other battery events"
35};
36
3bb3dbbd
DK
37/*
38 * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for
39 * delayed works so that we can run delayed works with CM_JIFFIES_SMALL
40 * without any delays.
41 */
42#define CM_JIFFIES_SMALL (2)
43
44/* If y is valid (> 0) and smaller than x, do x = y */
45#define CM_MIN_VALID(x, y) x = (((y > 0) && ((x) > (y))) ? (y) : (x))
46
47/*
48 * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking
49 * rtc alarm. It should be 2 or larger
50 */
51#define CM_RTC_SMALL (2)
52
53#define UEVENT_BUF_SIZE 32
54
55static LIST_HEAD(cm_list);
56static DEFINE_MUTEX(cm_list_mtx);
57
58/* About in-suspend (suspend-again) monitoring */
59static struct rtc_device *rtc_dev;
60/*
61 * Backup RTC alarm
62 * Save the wakeup alarm before entering suspend-to-RAM
63 */
64static struct rtc_wkalrm rtc_wkalarm_save;
65/* Backup RTC alarm time in terms of seconds since 01-01-1970 00:00:00 */
66static unsigned long rtc_wkalarm_save_time;
67static bool cm_suspended;
68static bool cm_rtc_set;
69static unsigned long cm_suspend_duration_ms;
70
d829dc75
CC
71/* About normal (not suspended) monitoring */
72static unsigned long polling_jiffy = ULONG_MAX; /* ULONG_MAX: no polling */
73static unsigned long next_polling; /* Next appointed polling time */
74static struct workqueue_struct *cm_wq; /* init at driver add */
75static struct delayed_work cm_monitor_work; /* init at driver add */
76
3bb3dbbd
DK
77/* Global charger-manager description */
78static struct charger_global_desc *g_desc; /* init with setup_charger_manager */
79
80/**
81 * is_batt_present - See if the battery presents in place.
82 * @cm: the Charger Manager representing the battery.
83 */
84static bool is_batt_present(struct charger_manager *cm)
85{
86 union power_supply_propval val;
87 bool present = false;
88 int i, ret;
89
90 switch (cm->desc->battery_present) {
d829dc75
CC
91 case CM_BATTERY_PRESENT:
92 present = true;
93 break;
94 case CM_NO_BATTERY:
95 break;
3bb3dbbd
DK
96 case CM_FUEL_GAUGE:
97 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
98 POWER_SUPPLY_PROP_PRESENT, &val);
99 if (ret == 0 && val.intval)
100 present = true;
101 break;
102 case CM_CHARGER_STAT:
103 for (i = 0; cm->charger_stat[i]; i++) {
104 ret = cm->charger_stat[i]->get_property(
105 cm->charger_stat[i],
106 POWER_SUPPLY_PROP_PRESENT, &val);
107 if (ret == 0 && val.intval) {
108 present = true;
109 break;
110 }
111 }
112 break;
113 }
114
115 return present;
116}
117
118/**
119 * is_ext_pwr_online - See if an external power source is attached to charge
120 * @cm: the Charger Manager representing the battery.
121 *
122 * Returns true if at least one of the chargers of the battery has an external
123 * power source attached to charge the battery regardless of whether it is
124 * actually charging or not.
125 */
126static bool is_ext_pwr_online(struct charger_manager *cm)
127{
128 union power_supply_propval val;
129 bool online = false;
130 int i, ret;
131
132 /* If at least one of them has one, it's yes. */
133 for (i = 0; cm->charger_stat[i]; i++) {
134 ret = cm->charger_stat[i]->get_property(
135 cm->charger_stat[i],
136 POWER_SUPPLY_PROP_ONLINE, &val);
137 if (ret == 0 && val.intval) {
138 online = true;
139 break;
140 }
141 }
142
143 return online;
144}
145
ad3d13ee
DK
146/**
147 * get_batt_uV - Get the voltage level of the battery
148 * @cm: the Charger Manager representing the battery.
149 * @uV: the voltage level returned.
150 *
151 * Returns 0 if there is no error.
152 * Returns a negative value on error.
153 */
154static int get_batt_uV(struct charger_manager *cm, int *uV)
155{
156 union power_supply_propval val;
157 int ret;
158
bb2a95c2 159 if (!cm->fuel_gauge)
ad3d13ee
DK
160 return -ENODEV;
161
bb2a95c2
AL
162 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
163 POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
ad3d13ee
DK
164 if (ret)
165 return ret;
166
167 *uV = val.intval;
168 return 0;
169}
170
3bb3dbbd
DK
171/**
172 * is_charging - Returns true if the battery is being charged.
173 * @cm: the Charger Manager representing the battery.
174 */
175static bool is_charging(struct charger_manager *cm)
176{
177 int i, ret;
178 bool charging = false;
179 union power_supply_propval val;
180
181 /* If there is no battery, it cannot be charged */
182 if (!is_batt_present(cm))
183 return false;
184
185 /* If at least one of the charger is charging, return yes */
186 for (i = 0; cm->charger_stat[i]; i++) {
187 /* 1. The charger sholuld not be DISABLED */
188 if (cm->emergency_stop)
189 continue;
190 if (!cm->charger_enabled)
191 continue;
192
193 /* 2. The charger should be online (ext-power) */
194 ret = cm->charger_stat[i]->get_property(
195 cm->charger_stat[i],
196 POWER_SUPPLY_PROP_ONLINE, &val);
197 if (ret) {
198 dev_warn(cm->dev, "Cannot read ONLINE value from %s.\n",
199 cm->desc->psy_charger_stat[i]);
200 continue;
201 }
202 if (val.intval == 0)
203 continue;
204
205 /*
206 * 3. The charger should not be FULL, DISCHARGING,
207 * or NOT_CHARGING.
208 */
209 ret = cm->charger_stat[i]->get_property(
210 cm->charger_stat[i],
211 POWER_SUPPLY_PROP_STATUS, &val);
212 if (ret) {
213 dev_warn(cm->dev, "Cannot read STATUS value from %s.\n",
214 cm->desc->psy_charger_stat[i]);
215 continue;
216 }
217 if (val.intval == POWER_SUPPLY_STATUS_FULL ||
218 val.intval == POWER_SUPPLY_STATUS_DISCHARGING ||
219 val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING)
220 continue;
221
222 /* Then, this is charging. */
223 charging = true;
224 break;
225 }
226
227 return charging;
228}
229
2ed9e9b6
CC
230/**
231 * is_full_charged - Returns true if the battery is fully charged.
232 * @cm: the Charger Manager representing the battery.
233 */
234static bool is_full_charged(struct charger_manager *cm)
235{
236 struct charger_desc *desc = cm->desc;
237 union power_supply_propval val;
238 int ret = 0;
239 int uV;
240
241 /* If there is no battery, it cannot be charged */
242 if (!is_batt_present(cm)) {
243 val.intval = 0;
244 goto out;
245 }
246
247 if (cm->fuel_gauge && desc->fullbatt_full_capacity > 0) {
248 /* Not full if capacity of fuel gauge isn't full */
249 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
250 POWER_SUPPLY_PROP_CHARGE_FULL, &val);
251 if (!ret && val.intval > desc->fullbatt_full_capacity) {
252 val.intval = 1;
253 goto out;
254 }
255 }
256
257 /* Full, if it's over the fullbatt voltage */
258 if (desc->fullbatt_uV > 0) {
259 ret = get_batt_uV(cm, &uV);
260 if (!ret && uV >= desc->fullbatt_uV) {
261 val.intval = 1;
262 goto out;
263 }
264 }
265
266 /* Full, if the capacity is more than fullbatt_soc */
267 if (cm->fuel_gauge && desc->fullbatt_soc > 0) {
268 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
269 POWER_SUPPLY_PROP_CAPACITY, &val);
270 if (!ret && val.intval >= desc->fullbatt_soc) {
271 val.intval = 1;
272 goto out;
273 }
274 }
275
276 val.intval = 0;
277
278out:
279 return val.intval ? true : false;
280}
281
3bb3dbbd
DK
282/**
283 * is_polling_required - Return true if need to continue polling for this CM.
284 * @cm: the Charger Manager representing the battery.
285 */
286static bool is_polling_required(struct charger_manager *cm)
287{
288 switch (cm->desc->polling_mode) {
289 case CM_POLL_DISABLE:
290 return false;
291 case CM_POLL_ALWAYS:
292 return true;
293 case CM_POLL_EXTERNAL_POWER_ONLY:
294 return is_ext_pwr_online(cm);
295 case CM_POLL_CHARGING_ONLY:
296 return is_charging(cm);
297 default:
298 dev_warn(cm->dev, "Incorrect polling_mode (%d)\n",
299 cm->desc->polling_mode);
300 }
301
302 return false;
303}
304
305/**
306 * try_charger_enable - Enable/Disable chargers altogether
307 * @cm: the Charger Manager representing the battery.
308 * @enable: true: enable / false: disable
309 *
310 * Note that Charger Manager keeps the charger enabled regardless whether
311 * the charger is charging or not (because battery is full or no external
312 * power source exists) except when CM needs to disable chargers forcibly
313 * bacause of emergency causes; when the battery is overheated or too cold.
314 */
315static int try_charger_enable(struct charger_manager *cm, bool enable)
316{
317 int err = 0, i;
318 struct charger_desc *desc = cm->desc;
319
320 /* Ignore if it's redundent command */
bb2a95c2 321 if (enable == cm->charger_enabled)
3bb3dbbd
DK
322 return 0;
323
324 if (enable) {
325 if (cm->emergency_stop)
326 return -EAGAIN;
8fcfe088
CC
327
328 /*
329 * Save start time of charging to limit
330 * maximum possible charging time.
331 */
332 cm->charging_start_time = ktime_to_ms(ktime_get());
333 cm->charging_end_time = 0;
334
dbb61fc7 335 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
3950c786
CC
336 if (desc->charger_regulators[i].externally_control)
337 continue;
338
dbb61fc7
CC
339 err = regulator_enable(desc->charger_regulators[i].consumer);
340 if (err < 0) {
341 dev_warn(cm->dev,
342 "Cannot enable %s regulator\n",
343 desc->charger_regulators[i].regulator_name);
344 }
345 }
3bb3dbbd 346 } else {
8fcfe088
CC
347 /*
348 * Save end time of charging to maintain fully charged state
349 * of battery after full-batt.
350 */
351 cm->charging_start_time = 0;
352 cm->charging_end_time = ktime_to_ms(ktime_get());
353
dbb61fc7 354 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
3950c786
CC
355 if (desc->charger_regulators[i].externally_control)
356 continue;
357
dbb61fc7
CC
358 err = regulator_disable(desc->charger_regulators[i].consumer);
359 if (err < 0) {
360 dev_warn(cm->dev,
361 "Cannot disable %s regulator\n",
362 desc->charger_regulators[i].regulator_name);
363 }
364 }
365
3bb3dbbd
DK
366 /*
367 * Abnormal battery state - Stop charging forcibly,
368 * even if charger was enabled at the other places
369 */
3bb3dbbd
DK
370 for (i = 0; i < desc->num_charger_regulators; i++) {
371 if (regulator_is_enabled(
372 desc->charger_regulators[i].consumer)) {
373 regulator_force_disable(
374 desc->charger_regulators[i].consumer);
375 dev_warn(cm->dev,
376 "Disable regulator(%s) forcibly.\n",
bee737bc 377 desc->charger_regulators[i].regulator_name);
3bb3dbbd
DK
378 }
379 }
380 }
381
382 if (!err)
383 cm->charger_enabled = enable;
384
385 return err;
386}
387
d829dc75
CC
388/**
389 * try_charger_restart - Restart charging.
390 * @cm: the Charger Manager representing the battery.
391 *
392 * Restart charging by turning off and on the charger.
393 */
394static int try_charger_restart(struct charger_manager *cm)
395{
396 int err;
397
398 if (cm->emergency_stop)
399 return -EAGAIN;
400
401 err = try_charger_enable(cm, false);
402 if (err)
403 return err;
404
405 return try_charger_enable(cm, true);
406}
407
3bb3dbbd
DK
408/**
409 * uevent_notify - Let users know something has changed.
410 * @cm: the Charger Manager representing the battery.
411 * @event: the event string.
412 *
413 * If @event is null, it implies that uevent_notify is called
414 * by resume function. When called in the resume function, cm_suspended
415 * should be already reset to false in order to let uevent_notify
416 * notify the recent event during the suspend to users. While
417 * suspended, uevent_notify does not notify users, but tracks
418 * events so that uevent_notify can notify users later after resumed.
419 */
420static void uevent_notify(struct charger_manager *cm, const char *event)
421{
422 static char env_str[UEVENT_BUF_SIZE + 1] = "";
423 static char env_str_save[UEVENT_BUF_SIZE + 1] = "";
424
425 if (cm_suspended) {
426 /* Nothing in suspended-event buffer */
427 if (env_str_save[0] == 0) {
428 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
429 return; /* status not changed */
430 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
431 return;
432 }
433
434 if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE))
435 return; /* Duplicated. */
bb2a95c2 436 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
3bb3dbbd
DK
437 return;
438 }
439
440 if (event == NULL) {
441 /* No messages pending */
442 if (!env_str_save[0])
443 return;
444
445 strncpy(env_str, env_str_save, UEVENT_BUF_SIZE);
446 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
447 env_str_save[0] = 0;
448
449 return;
450 }
451
452 /* status not changed */
453 if (!strncmp(env_str, event, UEVENT_BUF_SIZE))
454 return;
455
456 /* save the status and notify the update */
457 strncpy(env_str, event, UEVENT_BUF_SIZE);
458 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE);
459
460 dev_info(cm->dev, event);
461}
462
d829dc75
CC
463/**
464 * fullbatt_vchk - Check voltage drop some times after "FULL" event.
465 * @work: the work_struct appointing the function
466 *
467 * If a user has designated "fullbatt_vchkdrop_ms/uV" values with
468 * charger_desc, Charger Manager checks voltage drop after the battery
469 * "FULL" event. It checks whether the voltage has dropped more than
470 * fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms.
471 */
472static void fullbatt_vchk(struct work_struct *work)
473{
474 struct delayed_work *dwork = to_delayed_work(work);
475 struct charger_manager *cm = container_of(dwork,
476 struct charger_manager, fullbatt_vchk_work);
477 struct charger_desc *desc = cm->desc;
478 int batt_uV, err, diff;
479
480 /* remove the appointment for fullbatt_vchk */
481 cm->fullbatt_vchk_jiffies_at = 0;
482
483 if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
484 return;
485
486 err = get_batt_uV(cm, &batt_uV);
487 if (err) {
488 dev_err(cm->dev, "%s: get_batt_uV error(%d).\n", __func__, err);
489 return;
490 }
491
fd65ee5f 492 diff = desc->fullbatt_uV;
d829dc75
CC
493 diff -= batt_uV;
494
2ed9e9b6 495 dev_info(cm->dev, "VBATT dropped %duV after full-batt.\n", diff);
d829dc75
CC
496
497 if (diff > desc->fullbatt_vchkdrop_uV) {
498 try_charger_restart(cm);
8fcfe088
CC
499 uevent_notify(cm, "Recharging");
500 }
501}
502
503/**
504 * check_charging_duration - Monitor charging/discharging duration
505 * @cm: the Charger Manager representing the battery.
506 *
507 * If whole charging duration exceed 'charging_max_duration_ms',
508 * cm stop charging to prevent overcharge/overheat. If discharging
509 * duration exceed 'discharging _max_duration_ms', charger cable is
510 * attached, after full-batt, cm start charging to maintain fully
511 * charged state for battery.
512 */
513static int check_charging_duration(struct charger_manager *cm)
514{
515 struct charger_desc *desc = cm->desc;
516 u64 curr = ktime_to_ms(ktime_get());
517 u64 duration;
518 int ret = false;
519
520 if (!desc->charging_max_duration_ms &&
521 !desc->discharging_max_duration_ms)
522 return ret;
523
524 if (cm->charger_enabled) {
525 duration = curr - cm->charging_start_time;
526
527 if (duration > desc->charging_max_duration_ms) {
528 dev_info(cm->dev, "Charging duration exceed %lldms",
529 desc->charging_max_duration_ms);
530 uevent_notify(cm, "Discharging");
531 try_charger_enable(cm, false);
532 ret = true;
533 }
534 } else if (is_ext_pwr_online(cm) && !cm->charger_enabled) {
535 duration = curr - cm->charging_end_time;
536
537 if (duration > desc->charging_max_duration_ms &&
538 is_ext_pwr_online(cm)) {
539 dev_info(cm->dev, "DisCharging duration exceed %lldms",
540 desc->discharging_max_duration_ms);
541 uevent_notify(cm, "Recharing");
542 try_charger_enable(cm, true);
543 ret = true;
544 }
d829dc75 545 }
8fcfe088
CC
546
547 return ret;
d829dc75
CC
548}
549
3bb3dbbd
DK
550/**
551 * _cm_monitor - Monitor the temperature and return true for exceptions.
552 * @cm: the Charger Manager representing the battery.
553 *
554 * Returns true if there is an event to notify for the battery.
555 * (True if the status of "emergency_stop" changes)
556 */
557static bool _cm_monitor(struct charger_manager *cm)
558{
559 struct charger_desc *desc = cm->desc;
560 int temp = desc->temperature_out_of_range(&cm->last_temp_mC);
561
562 dev_dbg(cm->dev, "monitoring (%2.2d.%3.3dC)\n",
563 cm->last_temp_mC / 1000, cm->last_temp_mC % 1000);
564
2ed9e9b6
CC
565 /* It has been stopped already */
566 if (temp && cm->emergency_stop)
3bb3dbbd
DK
567 return false;
568
2ed9e9b6
CC
569 /*
570 * Check temperature whether overheat or cold.
571 * If temperature is out of range normal state, stop charging.
572 */
3bb3dbbd
DK
573 if (temp) {
574 cm->emergency_stop = temp;
575 if (!try_charger_enable(cm, false)) {
576 if (temp > 0)
577 uevent_notify(cm, "OVERHEAT");
578 else
579 uevent_notify(cm, "COLD");
580 }
2ed9e9b6 581
8fcfe088
CC
582 /*
583 * Check whole charging duration and discharing duration
584 * after full-batt.
585 */
586 } else if (!cm->emergency_stop && check_charging_duration(cm)) {
587 dev_dbg(cm->dev,
588 "Charging/Discharging duration is out of range");
2ed9e9b6
CC
589 /*
590 * Check dropped voltage of battery. If battery voltage is more
591 * dropped than fullbatt_vchkdrop_uV after fully charged state,
592 * charger-manager have to recharge battery.
593 */
594 } else if (!cm->emergency_stop && is_ext_pwr_online(cm) &&
595 !cm->charger_enabled) {
596 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
597
598 /*
599 * Check whether fully charged state to protect overcharge
600 * if charger-manager is charging for battery.
601 */
602 } else if (!cm->emergency_stop && is_full_charged(cm) &&
603 cm->charger_enabled) {
604 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged.\n");
605 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
606
607 try_charger_enable(cm, false);
608
609 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
3bb3dbbd
DK
610 } else {
611 cm->emergency_stop = 0;
2ed9e9b6
CC
612 if (is_ext_pwr_online(cm)) {
613 if (!try_charger_enable(cm, true))
614 uevent_notify(cm, "CHARGING");
615 }
3bb3dbbd
DK
616 }
617
618 return true;
619}
620
621/**
622 * cm_monitor - Monitor every battery.
623 *
624 * Returns true if there is an event to notify from any of the batteries.
625 * (True if the status of "emergency_stop" changes)
626 */
627static bool cm_monitor(void)
628{
629 bool stop = false;
630 struct charger_manager *cm;
631
632 mutex_lock(&cm_list_mtx);
633
bb2a95c2
AL
634 list_for_each_entry(cm, &cm_list, entry) {
635 if (_cm_monitor(cm))
636 stop = true;
637 }
3bb3dbbd
DK
638
639 mutex_unlock(&cm_list_mtx);
640
641 return stop;
642}
643
d829dc75
CC
644/**
645 * _setup_polling - Setup the next instance of polling.
646 * @work: work_struct of the function _setup_polling.
647 */
648static void _setup_polling(struct work_struct *work)
649{
650 unsigned long min = ULONG_MAX;
651 struct charger_manager *cm;
652 bool keep_polling = false;
653 unsigned long _next_polling;
654
655 mutex_lock(&cm_list_mtx);
656
657 list_for_each_entry(cm, &cm_list, entry) {
658 if (is_polling_required(cm) && cm->desc->polling_interval_ms) {
659 keep_polling = true;
660
661 if (min > cm->desc->polling_interval_ms)
662 min = cm->desc->polling_interval_ms;
663 }
664 }
665
666 polling_jiffy = msecs_to_jiffies(min);
667 if (polling_jiffy <= CM_JIFFIES_SMALL)
668 polling_jiffy = CM_JIFFIES_SMALL + 1;
669
670 if (!keep_polling)
671 polling_jiffy = ULONG_MAX;
672 if (polling_jiffy == ULONG_MAX)
673 goto out;
674
675 WARN(cm_wq == NULL, "charger-manager: workqueue not initialized"
676 ". try it later. %s\n", __func__);
677
678 _next_polling = jiffies + polling_jiffy;
679
680 if (!delayed_work_pending(&cm_monitor_work) ||
681 (delayed_work_pending(&cm_monitor_work) &&
682 time_after(next_polling, _next_polling))) {
d829dc75 683 next_polling = jiffies + polling_jiffy;
41f63c53 684 mod_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy);
d829dc75
CC
685 }
686
687out:
688 mutex_unlock(&cm_list_mtx);
689}
690static DECLARE_WORK(setup_polling, _setup_polling);
691
692/**
693 * cm_monitor_poller - The Monitor / Poller.
694 * @work: work_struct of the function cm_monitor_poller
695 *
696 * During non-suspended state, cm_monitor_poller is used to poll and monitor
697 * the batteries.
698 */
699static void cm_monitor_poller(struct work_struct *work)
700{
701 cm_monitor();
702 schedule_work(&setup_polling);
703}
704
dfeccb12
CC
705/**
706 * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL
707 * @cm: the Charger Manager representing the battery.
708 */
709static void fullbatt_handler(struct charger_manager *cm)
710{
711 struct charger_desc *desc = cm->desc;
712
713 if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
714 goto out;
715
716 if (cm_suspended)
717 device_set_wakeup_capable(cm->dev, true);
718
41f63c53
TH
719 mod_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
720 msecs_to_jiffies(desc->fullbatt_vchkdrop_ms));
dfeccb12
CC
721 cm->fullbatt_vchk_jiffies_at = jiffies + msecs_to_jiffies(
722 desc->fullbatt_vchkdrop_ms);
723
724 if (cm->fullbatt_vchk_jiffies_at == 0)
725 cm->fullbatt_vchk_jiffies_at = 1;
726
727out:
728 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged.\n");
729 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
730}
731
732/**
733 * battout_handler - Event handler for CM_EVENT_BATT_OUT
734 * @cm: the Charger Manager representing the battery.
735 */
736static void battout_handler(struct charger_manager *cm)
737{
738 if (cm_suspended)
739 device_set_wakeup_capable(cm->dev, true);
740
741 if (!is_batt_present(cm)) {
742 dev_emerg(cm->dev, "Battery Pulled Out!\n");
743 uevent_notify(cm, default_event_names[CM_EVENT_BATT_OUT]);
744 } else {
745 uevent_notify(cm, "Battery Reinserted?");
746 }
747}
748
749/**
750 * misc_event_handler - Handler for other evnets
751 * @cm: the Charger Manager representing the battery.
752 * @type: the Charger Manager representing the battery.
753 */
754static void misc_event_handler(struct charger_manager *cm,
755 enum cm_event_types type)
756{
757 if (cm_suspended)
758 device_set_wakeup_capable(cm->dev, true);
759
760 if (!delayed_work_pending(&cm_monitor_work) &&
761 is_polling_required(cm) && cm->desc->polling_interval_ms)
762 schedule_work(&setup_polling);
763 uevent_notify(cm, default_event_names[type]);
764}
765
ad3d13ee
DK
766static int charger_get_property(struct power_supply *psy,
767 enum power_supply_property psp,
768 union power_supply_propval *val)
769{
770 struct charger_manager *cm = container_of(psy,
771 struct charger_manager, charger_psy);
772 struct charger_desc *desc = cm->desc;
df58c04c
AV
773 int ret = 0;
774 int uV;
ad3d13ee
DK
775
776 switch (psp) {
777 case POWER_SUPPLY_PROP_STATUS:
778 if (is_charging(cm))
779 val->intval = POWER_SUPPLY_STATUS_CHARGING;
780 else if (is_ext_pwr_online(cm))
781 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
782 else
783 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
784 break;
785 case POWER_SUPPLY_PROP_HEALTH:
786 if (cm->emergency_stop > 0)
787 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
788 else if (cm->emergency_stop < 0)
789 val->intval = POWER_SUPPLY_HEALTH_COLD;
790 else
791 val->intval = POWER_SUPPLY_HEALTH_GOOD;
792 break;
793 case POWER_SUPPLY_PROP_PRESENT:
794 if (is_batt_present(cm))
795 val->intval = 1;
796 else
797 val->intval = 0;
798 break;
799 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
df58c04c 800 ret = get_batt_uV(cm, &val->intval);
ad3d13ee
DK
801 break;
802 case POWER_SUPPLY_PROP_CURRENT_NOW:
803 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
804 POWER_SUPPLY_PROP_CURRENT_NOW, val);
805 break;
806 case POWER_SUPPLY_PROP_TEMP:
807 /* in thenth of centigrade */
808 if (cm->last_temp_mC == INT_MIN)
809 desc->temperature_out_of_range(&cm->last_temp_mC);
810 val->intval = cm->last_temp_mC / 100;
811 if (!desc->measure_battery_temp)
812 ret = -ENODEV;
813 break;
814 case POWER_SUPPLY_PROP_TEMP_AMBIENT:
815 /* in thenth of centigrade */
816 if (cm->last_temp_mC == INT_MIN)
817 desc->temperature_out_of_range(&cm->last_temp_mC);
818 val->intval = cm->last_temp_mC / 100;
819 if (desc->measure_battery_temp)
820 ret = -ENODEV;
821 break;
822 case POWER_SUPPLY_PROP_CAPACITY:
823 if (!cm->fuel_gauge) {
824 ret = -ENODEV;
825 break;
826 }
827
828 if (!is_batt_present(cm)) {
829 /* There is no battery. Assume 100% */
830 val->intval = 100;
831 break;
832 }
833
834 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
835 POWER_SUPPLY_PROP_CAPACITY, val);
836 if (ret)
837 break;
838
839 if (val->intval > 100) {
840 val->intval = 100;
841 break;
842 }
843 if (val->intval < 0)
844 val->intval = 0;
845
846 /* Do not adjust SOC when charging: voltage is overrated */
847 if (is_charging(cm))
848 break;
849
850 /*
851 * If the capacity value is inconsistent, calibrate it base on
852 * the battery voltage values and the thresholds given as desc
853 */
854 ret = get_batt_uV(cm, &uV);
855 if (ret) {
856 /* Voltage information not available. No calibration */
857 ret = 0;
858 break;
859 }
860
861 if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
862 !is_charging(cm)) {
863 val->intval = 100;
864 break;
865 }
866
867 break;
868 case POWER_SUPPLY_PROP_ONLINE:
869 if (is_ext_pwr_online(cm))
870 val->intval = 1;
871 else
872 val->intval = 0;
873 break;
874 case POWER_SUPPLY_PROP_CHARGE_FULL:
2ed9e9b6 875 if (is_full_charged(cm))
ad3d13ee 876 val->intval = 1;
2ed9e9b6
CC
877 else
878 val->intval = 0;
ad3d13ee
DK
879 ret = 0;
880 break;
881 case POWER_SUPPLY_PROP_CHARGE_NOW:
882 if (is_charging(cm)) {
883 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
884 POWER_SUPPLY_PROP_CHARGE_NOW,
885 val);
886 if (ret) {
887 val->intval = 1;
888 ret = 0;
889 } else {
890 /* If CHARGE_NOW is supplied, use it */
891 val->intval = (val->intval > 0) ?
892 val->intval : 1;
893 }
894 } else {
895 val->intval = 0;
896 }
897 break;
898 default:
899 return -EINVAL;
900 }
901 return ret;
902}
903
904#define NUM_CHARGER_PSY_OPTIONAL (4)
905static enum power_supply_property default_charger_props[] = {
906 /* Guaranteed to provide */
907 POWER_SUPPLY_PROP_STATUS,
908 POWER_SUPPLY_PROP_HEALTH,
909 POWER_SUPPLY_PROP_PRESENT,
910 POWER_SUPPLY_PROP_VOLTAGE_NOW,
911 POWER_SUPPLY_PROP_CAPACITY,
912 POWER_SUPPLY_PROP_ONLINE,
913 POWER_SUPPLY_PROP_CHARGE_FULL,
914 /*
915 * Optional properties are:
916 * POWER_SUPPLY_PROP_CHARGE_NOW,
917 * POWER_SUPPLY_PROP_CURRENT_NOW,
918 * POWER_SUPPLY_PROP_TEMP, and
919 * POWER_SUPPLY_PROP_TEMP_AMBIENT,
920 */
921};
922
923static struct power_supply psy_default = {
924 .name = "battery",
925 .type = POWER_SUPPLY_TYPE_BATTERY,
926 .properties = default_charger_props,
927 .num_properties = ARRAY_SIZE(default_charger_props),
928 .get_property = charger_get_property,
929};
930
3bb3dbbd
DK
931/**
932 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
933 * for suspend_again.
934 *
935 * Returns true if the alarm is set for Charger Manager to use.
936 * Returns false if
937 * cm_setup_timer fails to set an alarm,
938 * cm_setup_timer does not need to set an alarm for Charger Manager,
939 * or an alarm previously configured is to be used.
940 */
941static bool cm_setup_timer(void)
942{
943 struct charger_manager *cm;
944 unsigned int wakeup_ms = UINT_MAX;
945 bool ret = false;
946
947 mutex_lock(&cm_list_mtx);
948
949 list_for_each_entry(cm, &cm_list, entry) {
d829dc75
CC
950 unsigned int fbchk_ms = 0;
951
952 /* fullbatt_vchk is required. setup timer for that */
953 if (cm->fullbatt_vchk_jiffies_at) {
954 fbchk_ms = jiffies_to_msecs(cm->fullbatt_vchk_jiffies_at
955 - jiffies);
956 if (time_is_before_eq_jiffies(
957 cm->fullbatt_vchk_jiffies_at) ||
958 msecs_to_jiffies(fbchk_ms) < CM_JIFFIES_SMALL) {
959 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
960 fbchk_ms = 0;
961 }
962 }
963 CM_MIN_VALID(wakeup_ms, fbchk_ms);
964
3bb3dbbd
DK
965 /* Skip if polling is not required for this CM */
966 if (!is_polling_required(cm) && !cm->emergency_stop)
967 continue;
968 if (cm->desc->polling_interval_ms == 0)
969 continue;
970 CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
971 }
972
973 mutex_unlock(&cm_list_mtx);
974
975 if (wakeup_ms < UINT_MAX && wakeup_ms > 0) {
976 pr_info("Charger Manager wakeup timer: %u ms.\n", wakeup_ms);
977 if (rtc_dev) {
978 struct rtc_wkalrm tmp;
979 unsigned long time, now;
980 unsigned long add = DIV_ROUND_UP(wakeup_ms, 1000);
981
982 /*
983 * Set alarm with the polling interval (wakeup_ms)
984 * except when rtc_wkalarm_save comes first.
985 * However, the alarm time should be NOW +
986 * CM_RTC_SMALL or later.
987 */
988 tmp.enabled = 1;
989 rtc_read_time(rtc_dev, &tmp.time);
990 rtc_tm_to_time(&tmp.time, &now);
991 if (add < CM_RTC_SMALL)
992 add = CM_RTC_SMALL;
993 time = now + add;
994
995 ret = true;
996
997 if (rtc_wkalarm_save.enabled &&
998 rtc_wkalarm_save_time &&
999 rtc_wkalarm_save_time < time) {
1000 if (rtc_wkalarm_save_time < now + CM_RTC_SMALL)
1001 time = now + CM_RTC_SMALL;
1002 else
1003 time = rtc_wkalarm_save_time;
1004
1005 /* The timer is not appointed by CM */
1006 ret = false;
1007 }
1008
1009 pr_info("Waking up after %lu secs.\n",
1010 time - now);
1011
1012 rtc_time_to_tm(time, &tmp.time);
1013 rtc_set_alarm(rtc_dev, &tmp);
1014 cm_suspend_duration_ms += wakeup_ms;
1015 return ret;
1016 }
1017 }
1018
1019 if (rtc_dev)
1020 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1021 return false;
1022}
1023
d829dc75
CC
1024static void _cm_fbchk_in_suspend(struct charger_manager *cm)
1025{
1026 unsigned long jiffy_now = jiffies;
1027
1028 if (!cm->fullbatt_vchk_jiffies_at)
1029 return;
1030
1031 if (g_desc && g_desc->assume_timer_stops_in_suspend)
1032 jiffy_now += msecs_to_jiffies(cm_suspend_duration_ms);
1033
1034 /* Execute now if it's going to be executed not too long after */
1035 jiffy_now += CM_JIFFIES_SMALL;
1036
1037 if (time_after_eq(jiffy_now, cm->fullbatt_vchk_jiffies_at))
1038 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
1039}
1040
3bb3dbbd
DK
1041/**
1042 * cm_suspend_again - Determine whether suspend again or not
1043 *
1044 * Returns true if the system should be suspended again
1045 * Returns false if the system should be woken up
1046 */
1047bool cm_suspend_again(void)
1048{
1049 struct charger_manager *cm;
1050 bool ret = false;
1051
1052 if (!g_desc || !g_desc->rtc_only_wakeup || !g_desc->rtc_only_wakeup() ||
1053 !cm_rtc_set)
1054 return false;
1055
1056 if (cm_monitor())
1057 goto out;
1058
1059 ret = true;
1060 mutex_lock(&cm_list_mtx);
1061 list_for_each_entry(cm, &cm_list, entry) {
d829dc75
CC
1062 _cm_fbchk_in_suspend(cm);
1063
3bb3dbbd 1064 if (cm->status_save_ext_pwr_inserted != is_ext_pwr_online(cm) ||
bb2a95c2 1065 cm->status_save_batt != is_batt_present(cm)) {
3bb3dbbd 1066 ret = false;
bb2a95c2
AL
1067 break;
1068 }
3bb3dbbd
DK
1069 }
1070 mutex_unlock(&cm_list_mtx);
1071
1072 cm_rtc_set = cm_setup_timer();
1073out:
1074 /* It's about the time when the non-CM appointed timer goes off */
1075 if (rtc_wkalarm_save.enabled) {
1076 unsigned long now;
1077 struct rtc_time tmp;
1078
1079 rtc_read_time(rtc_dev, &tmp);
1080 rtc_tm_to_time(&tmp, &now);
1081
1082 if (rtc_wkalarm_save_time &&
1083 now + CM_RTC_SMALL >= rtc_wkalarm_save_time)
1084 return false;
1085 }
1086 return ret;
1087}
1088EXPORT_SYMBOL_GPL(cm_suspend_again);
1089
1090/**
1091 * setup_charger_manager - initialize charger_global_desc data
1092 * @gd: pointer to instance of charger_global_desc
1093 */
1094int setup_charger_manager(struct charger_global_desc *gd)
1095{
1096 if (!gd)
1097 return -EINVAL;
1098
1099 if (rtc_dev)
1100 rtc_class_close(rtc_dev);
1101 rtc_dev = NULL;
1102 g_desc = NULL;
1103
1104 if (!gd->rtc_only_wakeup) {
1105 pr_err("The callback rtc_only_wakeup is not given.\n");
1106 return -EINVAL;
1107 }
1108
1109 if (gd->rtc_name) {
1110 rtc_dev = rtc_class_open(gd->rtc_name);
1111 if (IS_ERR_OR_NULL(rtc_dev)) {
1112 rtc_dev = NULL;
1113 /* Retry at probe. RTC may be not registered yet */
1114 }
1115 } else {
1116 pr_warn("No wakeup timer is given for charger manager."
1117 "In-suspend monitoring won't work.\n");
1118 }
1119
1120 g_desc = gd;
1121 return 0;
1122}
1123EXPORT_SYMBOL_GPL(setup_charger_manager);
1124
bee737bc
CC
1125/**
1126 * charger_extcon_work - enable/diable charger according to the state
1127 * of charger cable
1128 *
1129 * @work: work_struct of the function charger_extcon_work.
1130 */
1131static void charger_extcon_work(struct work_struct *work)
1132{
1133 struct charger_cable *cable =
1134 container_of(work, struct charger_cable, wq);
45cd4fb2
CC
1135 int ret;
1136
1137 if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) {
1138 ret = regulator_set_current_limit(cable->charger->consumer,
1139 cable->min_uA, cable->max_uA);
1140 if (ret < 0) {
1141 pr_err("Cannot set current limit of %s (%s)\n",
1142 cable->charger->regulator_name, cable->name);
1143 return;
1144 }
1145
1146 pr_info("Set current limit of %s : %duA ~ %duA\n",
1147 cable->charger->regulator_name,
1148 cable->min_uA, cable->max_uA);
1149 }
bee737bc
CC
1150
1151 try_charger_enable(cable->cm, cable->attached);
1152}
1153
1154/**
1155 * charger_extcon_notifier - receive the state of charger cable
1156 * when registered cable is attached or detached.
1157 *
1158 * @self: the notifier block of the charger_extcon_notifier.
1159 * @event: the cable state.
1160 * @ptr: the data pointer of notifier block.
1161 */
1162static int charger_extcon_notifier(struct notifier_block *self,
1163 unsigned long event, void *ptr)
1164{
1165 struct charger_cable *cable =
1166 container_of(self, struct charger_cable, nb);
1167
2ed9e9b6
CC
1168 /*
1169 * The newly state of charger cable.
1170 * If cable is attached, cable->attached is true.
1171 */
bee737bc 1172 cable->attached = event;
2ed9e9b6
CC
1173
1174 /*
1175 * Setup monitoring to check battery state
1176 * when charger cable is attached.
1177 */
1178 if (cable->attached && is_polling_required(cable->cm)) {
1179 if (work_pending(&setup_polling))
1180 cancel_work_sync(&setup_polling);
1181 schedule_work(&setup_polling);
1182 }
1183
1184 /*
1185 * Setup work for controlling charger(regulator)
1186 * according to charger cable.
1187 */
bee737bc
CC
1188 schedule_work(&cable->wq);
1189
1190 return NOTIFY_DONE;
1191}
1192
1193/**
1194 * charger_extcon_init - register external connector to use it
1195 * as the charger cable
1196 *
1197 * @cm: the Charger Manager representing the battery.
1198 * @cable: the Charger cable representing the external connector.
1199 */
1200static int charger_extcon_init(struct charger_manager *cm,
1201 struct charger_cable *cable)
1202{
1203 int ret = 0;
1204
1205 /*
1206 * Charger manager use Extcon framework to identify
1207 * the charger cable among various external connector
1208 * cable (e.g., TA, USB, MHL, Dock).
1209 */
1210 INIT_WORK(&cable->wq, charger_extcon_work);
1211 cable->nb.notifier_call = charger_extcon_notifier;
1212 ret = extcon_register_interest(&cable->extcon_dev,
1213 cable->extcon_name, cable->name, &cable->nb);
1214 if (ret < 0) {
1215 pr_info("Cannot register extcon_dev for %s(cable: %s).\n",
1216 cable->extcon_name,
1217 cable->name);
1218 ret = -EINVAL;
1219 }
1220
1221 return ret;
1222}
1223
3950c786
CC
1224/* help function of sysfs node to control charger(regulator) */
1225static ssize_t charger_name_show(struct device *dev,
1226 struct device_attribute *attr, char *buf)
1227{
1228 struct charger_regulator *charger
1229 = container_of(attr, struct charger_regulator, attr_name);
1230
1231 return sprintf(buf, "%s\n", charger->regulator_name);
1232}
1233
1234static ssize_t charger_state_show(struct device *dev,
1235 struct device_attribute *attr, char *buf)
1236{
1237 struct charger_regulator *charger
1238 = container_of(attr, struct charger_regulator, attr_state);
1239 int state = 0;
1240
1241 if (!charger->externally_control)
1242 state = regulator_is_enabled(charger->consumer);
1243
1244 return sprintf(buf, "%s\n", state ? "enabled" : "disabled");
1245}
1246
1247static ssize_t charger_externally_control_show(struct device *dev,
1248 struct device_attribute *attr, char *buf)
1249{
1250 struct charger_regulator *charger = container_of(attr,
1251 struct charger_regulator, attr_externally_control);
1252
1253 return sprintf(buf, "%d\n", charger->externally_control);
1254}
1255
1256static ssize_t charger_externally_control_store(struct device *dev,
1257 struct device_attribute *attr, const char *buf,
1258 size_t count)
1259{
1260 struct charger_regulator *charger
1261 = container_of(attr, struct charger_regulator,
1262 attr_externally_control);
1263 struct charger_manager *cm = charger->cm;
1264 struct charger_desc *desc = cm->desc;
1265 int i;
1266 int ret;
1267 int externally_control;
1268 int chargers_externally_control = 1;
1269
1270 ret = sscanf(buf, "%d", &externally_control);
1271 if (ret == 0) {
1272 ret = -EINVAL;
1273 return ret;
1274 }
1275
1276 if (!externally_control) {
1277 charger->externally_control = 0;
1278 return count;
1279 }
1280
1281 for (i = 0; i < desc->num_charger_regulators; i++) {
1282 if (&desc->charger_regulators[i] != charger &&
1283 !desc->charger_regulators[i].externally_control) {
1284 /*
1285 * At least, one charger is controlled by
1286 * charger-manager
1287 */
1288 chargers_externally_control = 0;
1289 break;
1290 }
1291 }
1292
1293 if (!chargers_externally_control) {
1294 if (cm->charger_enabled) {
1295 try_charger_enable(charger->cm, false);
1296 charger->externally_control = externally_control;
1297 try_charger_enable(charger->cm, true);
1298 } else {
1299 charger->externally_control = externally_control;
1300 }
1301 } else {
1302 dev_warn(cm->dev,
1303 "'%s' regulator should be controlled "
1304 "in charger-manager because charger-manager "
1305 "must need at least one charger for charging\n",
1306 charger->regulator_name);
1307 }
1308
1309 return count;
1310}
1311
3bb3dbbd
DK
1312static int charger_manager_probe(struct platform_device *pdev)
1313{
1314 struct charger_desc *desc = dev_get_platdata(&pdev->dev);
1315 struct charger_manager *cm;
1316 int ret = 0, i = 0;
bee737bc 1317 int j = 0;
3950c786 1318 int chargers_externally_control = 1;
ad3d13ee 1319 union power_supply_propval val;
3bb3dbbd
DK
1320
1321 if (g_desc && !rtc_dev && g_desc->rtc_name) {
1322 rtc_dev = rtc_class_open(g_desc->rtc_name);
1323 if (IS_ERR_OR_NULL(rtc_dev)) {
1324 rtc_dev = NULL;
1325 dev_err(&pdev->dev, "Cannot get RTC %s.\n",
1326 g_desc->rtc_name);
1327 ret = -ENODEV;
1328 goto err_alloc;
1329 }
1330 }
1331
1332 if (!desc) {
1333 dev_err(&pdev->dev, "No platform data (desc) found.\n");
1334 ret = -ENODEV;
1335 goto err_alloc;
1336 }
1337
1338 cm = kzalloc(sizeof(struct charger_manager), GFP_KERNEL);
1339 if (!cm) {
1340 dev_err(&pdev->dev, "Cannot allocate memory.\n");
1341 ret = -ENOMEM;
1342 goto err_alloc;
1343 }
1344
1345 /* Basic Values. Unspecified are Null or 0 */
1346 cm->dev = &pdev->dev;
1347 cm->desc = kzalloc(sizeof(struct charger_desc), GFP_KERNEL);
1348 if (!cm->desc) {
1349 dev_err(&pdev->dev, "Cannot allocate memory.\n");
1350 ret = -ENOMEM;
1351 goto err_alloc_desc;
1352 }
1353 memcpy(cm->desc, desc, sizeof(struct charger_desc));
1354 cm->last_temp_mC = INT_MIN; /* denotes "unmeasured, yet" */
1355
d829dc75
CC
1356 /*
1357 * The following two do not need to be errors.
1358 * Users may intentionally ignore those two features.
1359 */
1360 if (desc->fullbatt_uV == 0) {
1361 dev_info(&pdev->dev, "Ignoring full-battery voltage threshold"
1362 " as it is not supplied.");
1363 }
1364 if (!desc->fullbatt_vchkdrop_ms || !desc->fullbatt_vchkdrop_uV) {
1365 dev_info(&pdev->dev, "Disabling full-battery voltage drop "
1366 "checking mechanism as it is not supplied.");
1367 desc->fullbatt_vchkdrop_ms = 0;
1368 desc->fullbatt_vchkdrop_uV = 0;
1369 }
2ed9e9b6
CC
1370 if (desc->fullbatt_soc == 0) {
1371 dev_info(&pdev->dev, "Ignoring full-battery soc(state of"
1372 " charge) threshold as it is not"
1373 " supplied.");
1374 }
1375 if (desc->fullbatt_full_capacity == 0) {
1376 dev_info(&pdev->dev, "Ignoring full-battery full capacity"
1377 " threshold as it is not supplied.");
1378 }
d829dc75 1379
3bb3dbbd
DK
1380 if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
1381 ret = -EINVAL;
1382 dev_err(&pdev->dev, "charger_regulators undefined.\n");
1383 goto err_no_charger;
1384 }
1385
1386 if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
1387 dev_err(&pdev->dev, "No power supply defined.\n");
1388 ret = -EINVAL;
1389 goto err_no_charger_stat;
1390 }
1391
1392 /* Counting index only */
1393 while (desc->psy_charger_stat[i])
1394 i++;
1395
1396 cm->charger_stat = kzalloc(sizeof(struct power_supply *) * (i + 1),
1397 GFP_KERNEL);
1398 if (!cm->charger_stat) {
1399 ret = -ENOMEM;
1400 goto err_no_charger_stat;
1401 }
1402
1403 for (i = 0; desc->psy_charger_stat[i]; i++) {
1404 cm->charger_stat[i] = power_supply_get_by_name(
1405 desc->psy_charger_stat[i]);
1406 if (!cm->charger_stat[i]) {
1407 dev_err(&pdev->dev, "Cannot find power supply "
1408 "\"%s\"\n",
1409 desc->psy_charger_stat[i]);
1410 ret = -ENODEV;
1411 goto err_chg_stat;
1412 }
1413 }
1414
1415 cm->fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
1416 if (!cm->fuel_gauge) {
1417 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
1418 desc->psy_fuel_gauge);
1419 ret = -ENODEV;
1420 goto err_chg_stat;
1421 }
1422
1423 if (desc->polling_interval_ms == 0 ||
1424 msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) {
1425 dev_err(&pdev->dev, "polling_interval_ms is too small\n");
1426 ret = -EINVAL;
1427 goto err_chg_stat;
1428 }
1429
1430 if (!desc->temperature_out_of_range) {
1431 dev_err(&pdev->dev, "there is no temperature_out_of_range\n");
1432 ret = -EINVAL;
1433 goto err_chg_stat;
1434 }
1435
8fcfe088
CC
1436 if (!desc->charging_max_duration_ms ||
1437 !desc->discharging_max_duration_ms) {
1438 dev_info(&pdev->dev, "Cannot limit charging duration "
1439 "checking mechanism to prevent overcharge/overheat "
1440 "and control discharging duration");
1441 desc->charging_max_duration_ms = 0;
1442 desc->discharging_max_duration_ms = 0;
1443 }
1444
3bb3dbbd
DK
1445 platform_set_drvdata(pdev, cm);
1446
bb2a95c2
AL
1447 memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default));
1448
ad3d13ee 1449 if (!desc->psy_name) {
bb2a95c2 1450 strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
ad3d13ee
DK
1451 } else {
1452 strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
1453 }
1454 cm->charger_psy.name = cm->psy_name_buf;
1455
1456 /* Allocate for psy properties because they may vary */
1457 cm->charger_psy.properties = kzalloc(sizeof(enum power_supply_property)
1458 * (ARRAY_SIZE(default_charger_props) +
1459 NUM_CHARGER_PSY_OPTIONAL),
1460 GFP_KERNEL);
1461 if (!cm->charger_psy.properties) {
1462 dev_err(&pdev->dev, "Cannot allocate for psy properties.\n");
1463 ret = -ENOMEM;
1464 goto err_chg_stat;
1465 }
1466 memcpy(cm->charger_psy.properties, default_charger_props,
1467 sizeof(enum power_supply_property) *
1468 ARRAY_SIZE(default_charger_props));
1469 cm->charger_psy.num_properties = psy_default.num_properties;
1470
1471 /* Find which optional psy-properties are available */
1472 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1473 POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
1474 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1475 POWER_SUPPLY_PROP_CHARGE_NOW;
1476 cm->charger_psy.num_properties++;
1477 }
1478 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1479 POWER_SUPPLY_PROP_CURRENT_NOW,
1480 &val)) {
1481 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1482 POWER_SUPPLY_PROP_CURRENT_NOW;
1483 cm->charger_psy.num_properties++;
1484 }
bb2a95c2 1485
ad3d13ee
DK
1486 if (desc->measure_battery_temp) {
1487 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1488 POWER_SUPPLY_PROP_TEMP;
1489 cm->charger_psy.num_properties++;
bb2a95c2
AL
1490 } else {
1491 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1492 POWER_SUPPLY_PROP_TEMP_AMBIENT;
1493 cm->charger_psy.num_properties++;
ad3d13ee
DK
1494 }
1495
d829dc75
CC
1496 INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk);
1497
ad3d13ee
DK
1498 ret = power_supply_register(NULL, &cm->charger_psy);
1499 if (ret) {
1500 dev_err(&pdev->dev, "Cannot register charger-manager with"
1501 " name \"%s\".\n", cm->charger_psy.name);
1502 goto err_register;
1503 }
1504
bee737bc
CC
1505 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1506 struct charger_regulator *charger
1507 = &desc->charger_regulators[i];
3950c786
CC
1508 char buf[11];
1509 char *str;
bee737bc
CC
1510
1511 charger->consumer = regulator_get(&pdev->dev,
1512 charger->regulator_name);
1513 if (charger->consumer == NULL) {
1514 dev_err(&pdev->dev, "Cannot find charger(%s)n",
1515 charger->regulator_name);
1516 ret = -EINVAL;
1517 goto err_chg_get;
1518 }
3950c786 1519 charger->cm = cm;
bee737bc
CC
1520
1521 for (j = 0 ; j < charger->num_cables ; j++) {
1522 struct charger_cable *cable = &charger->cables[j];
1523
1524 ret = charger_extcon_init(cm, cable);
1525 if (ret < 0) {
1526 dev_err(&pdev->dev, "Cannot find charger(%s)n",
1527 charger->regulator_name);
1528 goto err_extcon;
1529 }
1530 cable->charger = charger;
1531 cable->cm = cm;
1532 }
3950c786
CC
1533
1534 /* Create sysfs entry to control charger(regulator) */
1535 snprintf(buf, 10, "charger.%d", i);
1536 str = kzalloc(sizeof(char) * (strlen(buf) + 1), GFP_KERNEL);
1537 if (!str) {
1538 for (i--; i >= 0; i--) {
1539 charger = &desc->charger_regulators[i];
1540 kfree(charger->attr_g.name);
1541 }
1542 ret = -ENOMEM;
1543
1544 goto err_extcon;
1545 }
1546 strcpy(str, buf);
1547
1548 charger->attrs[0] = &charger->attr_name.attr;
1549 charger->attrs[1] = &charger->attr_state.attr;
1550 charger->attrs[2] = &charger->attr_externally_control.attr;
1551 charger->attrs[3] = NULL;
1552 charger->attr_g.name = str;
1553 charger->attr_g.attrs = charger->attrs;
1554
1555 sysfs_attr_init(&charger->attr_name.attr);
1556 charger->attr_name.attr.name = "name";
1557 charger->attr_name.attr.mode = 0444;
1558 charger->attr_name.show = charger_name_show;
1559
1560 sysfs_attr_init(&charger->attr_state.attr);
1561 charger->attr_state.attr.name = "state";
1562 charger->attr_state.attr.mode = 0444;
1563 charger->attr_state.show = charger_state_show;
1564
1565 sysfs_attr_init(&charger->attr_externally_control.attr);
1566 charger->attr_externally_control.attr.name
1567 = "externally_control";
1568 charger->attr_externally_control.attr.mode = 0644;
1569 charger->attr_externally_control.show
1570 = charger_externally_control_show;
1571 charger->attr_externally_control.store
1572 = charger_externally_control_store;
1573
1574 if (!desc->charger_regulators[i].externally_control ||
1575 !chargers_externally_control) {
1576 chargers_externally_control = 0;
1577 }
1578 dev_info(&pdev->dev, "'%s' regulator's externally_control"
1579 "is %d\n", charger->regulator_name,
1580 charger->externally_control);
1581
1582 ret = sysfs_create_group(&cm->charger_psy.dev->kobj,
1583 &charger->attr_g);
1584 if (ret < 0) {
1585 dev_info(&pdev->dev, "Cannot create sysfs entry"
1586 "of %s regulator\n",
1587 charger->regulator_name);
1588 }
1589 }
1590
1591 if (chargers_externally_control) {
1592 dev_err(&pdev->dev, "Cannot register regulator because "
1593 "charger-manager must need at least "
1594 "one charger for charging battery\n");
1595
1596 ret = -EINVAL;
1597 goto err_chg_enable;
3bb3dbbd
DK
1598 }
1599
1600 ret = try_charger_enable(cm, true);
1601 if (ret) {
1602 dev_err(&pdev->dev, "Cannot enable charger regulators\n");
1603 goto err_chg_enable;
1604 }
1605
1606 /* Add to the list */
1607 mutex_lock(&cm_list_mtx);
1608 list_add(&cm->entry, &cm_list);
1609 mutex_unlock(&cm_list_mtx);
1610
dfeccb12
CC
1611 /*
1612 * Charger-manager is capable of waking up the systme from sleep
1613 * when event is happend through cm_notify_event()
1614 */
1615 device_init_wakeup(&pdev->dev, true);
1616 device_set_wakeup_capable(&pdev->dev, false);
1617
d829dc75
CC
1618 schedule_work(&setup_polling);
1619
3bb3dbbd
DK
1620 return 0;
1621
1622err_chg_enable:
3950c786
CC
1623 for (i = 0; i < desc->num_charger_regulators; i++) {
1624 struct charger_regulator *charger;
1625
1626 charger = &desc->charger_regulators[i];
1627 sysfs_remove_group(&cm->charger_psy.dev->kobj,
1628 &charger->attr_g);
1629 kfree(charger->attr_g.name);
1630 }
bee737bc
CC
1631err_extcon:
1632 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1633 struct charger_regulator *charger
1634 = &desc->charger_regulators[i];
1635 for (j = 0 ; j < charger->num_cables ; j++) {
1636 struct charger_cable *cable = &charger->cables[j];
1637 extcon_unregister_interest(&cable->extcon_dev);
1638 }
1639 }
1640err_chg_get:
1641 for (i = 0 ; i < desc->num_charger_regulators ; i++)
1642 regulator_put(desc->charger_regulators[i].consumer);
1643
ad3d13ee
DK
1644 power_supply_unregister(&cm->charger_psy);
1645err_register:
1646 kfree(cm->charger_psy.properties);
3bb3dbbd
DK
1647err_chg_stat:
1648 kfree(cm->charger_stat);
1649err_no_charger_stat:
1650err_no_charger:
1651 kfree(cm->desc);
1652err_alloc_desc:
1653 kfree(cm);
1654err_alloc:
1655 return ret;
1656}
1657
1658static int __devexit charger_manager_remove(struct platform_device *pdev)
1659{
1660 struct charger_manager *cm = platform_get_drvdata(pdev);
1661 struct charger_desc *desc = cm->desc;
bee737bc
CC
1662 int i = 0;
1663 int j = 0;
3bb3dbbd
DK
1664
1665 /* Remove from the list */
1666 mutex_lock(&cm_list_mtx);
1667 list_del(&cm->entry);
1668 mutex_unlock(&cm_list_mtx);
1669
d829dc75
CC
1670 if (work_pending(&setup_polling))
1671 cancel_work_sync(&setup_polling);
1672 if (delayed_work_pending(&cm_monitor_work))
1673 cancel_delayed_work_sync(&cm_monitor_work);
1674
bee737bc
CC
1675 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1676 struct charger_regulator *charger
1677 = &desc->charger_regulators[i];
1678 for (j = 0 ; j < charger->num_cables ; j++) {
1679 struct charger_cable *cable = &charger->cables[j];
1680 extcon_unregister_interest(&cable->extcon_dev);
1681 }
1682 }
1683
1684 for (i = 0 ; i < desc->num_charger_regulators ; i++)
1685 regulator_put(desc->charger_regulators[i].consumer);
1686
ad3d13ee 1687 power_supply_unregister(&cm->charger_psy);
d829dc75
CC
1688
1689 try_charger_enable(cm, false);
1690
ad3d13ee 1691 kfree(cm->charger_psy.properties);
3bb3dbbd
DK
1692 kfree(cm->charger_stat);
1693 kfree(cm->desc);
1694 kfree(cm);
1695
1696 return 0;
1697}
1698
1bbe24d4 1699static const struct platform_device_id charger_manager_id[] = {
3bb3dbbd
DK
1700 { "charger-manager", 0 },
1701 { },
1702};
1bbe24d4 1703MODULE_DEVICE_TABLE(platform, charger_manager_id);
3bb3dbbd 1704
dfeccb12
CC
1705static int cm_suspend_noirq(struct device *dev)
1706{
1707 int ret = 0;
1708
1709 if (device_may_wakeup(dev)) {
1710 device_set_wakeup_capable(dev, false);
1711 ret = -EAGAIN;
1712 }
1713
1714 return ret;
1715}
1716
3bb3dbbd
DK
1717static int cm_suspend_prepare(struct device *dev)
1718{
bb2a95c2 1719 struct charger_manager *cm = dev_get_drvdata(dev);
3bb3dbbd
DK
1720
1721 if (!cm_suspended) {
1722 if (rtc_dev) {
1723 struct rtc_time tmp;
1724 unsigned long now;
1725
1726 rtc_read_alarm(rtc_dev, &rtc_wkalarm_save);
1727 rtc_read_time(rtc_dev, &tmp);
1728
1729 if (rtc_wkalarm_save.enabled) {
1730 rtc_tm_to_time(&rtc_wkalarm_save.time,
1731 &rtc_wkalarm_save_time);
1732 rtc_tm_to_time(&tmp, &now);
1733 if (now > rtc_wkalarm_save_time)
1734 rtc_wkalarm_save_time = 0;
1735 } else {
1736 rtc_wkalarm_save_time = 0;
1737 }
1738 }
1739 cm_suspended = true;
1740 }
1741
d829dc75
CC
1742 if (delayed_work_pending(&cm->fullbatt_vchk_work))
1743 cancel_delayed_work(&cm->fullbatt_vchk_work);
3bb3dbbd
DK
1744 cm->status_save_ext_pwr_inserted = is_ext_pwr_online(cm);
1745 cm->status_save_batt = is_batt_present(cm);
1746
1747 if (!cm_rtc_set) {
1748 cm_suspend_duration_ms = 0;
1749 cm_rtc_set = cm_setup_timer();
1750 }
1751
1752 return 0;
1753}
1754
1755static void cm_suspend_complete(struct device *dev)
1756{
bb2a95c2 1757 struct charger_manager *cm = dev_get_drvdata(dev);
3bb3dbbd
DK
1758
1759 if (cm_suspended) {
1760 if (rtc_dev) {
1761 struct rtc_wkalrm tmp;
1762
1763 rtc_read_alarm(rtc_dev, &tmp);
1764 rtc_wkalarm_save.pending = tmp.pending;
1765 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1766 }
1767 cm_suspended = false;
1768 cm_rtc_set = false;
1769 }
1770
d829dc75
CC
1771 /* Re-enqueue delayed work (fullbatt_vchk_work) */
1772 if (cm->fullbatt_vchk_jiffies_at) {
1773 unsigned long delay = 0;
1774 unsigned long now = jiffies + CM_JIFFIES_SMALL;
1775
1776 if (time_after_eq(now, cm->fullbatt_vchk_jiffies_at)) {
1777 delay = (unsigned long)((long)now
1778 - (long)(cm->fullbatt_vchk_jiffies_at));
1779 delay = jiffies_to_msecs(delay);
1780 } else {
1781 delay = 0;
1782 }
1783
1784 /*
1785 * Account for cm_suspend_duration_ms if
1786 * assume_timer_stops_in_suspend is active
1787 */
1788 if (g_desc && g_desc->assume_timer_stops_in_suspend) {
1789 if (delay > cm_suspend_duration_ms)
1790 delay -= cm_suspend_duration_ms;
1791 else
1792 delay = 0;
1793 }
1794
1795 queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
1796 msecs_to_jiffies(delay));
1797 }
dfeccb12 1798 device_set_wakeup_capable(cm->dev, false);
3bb3dbbd
DK
1799 uevent_notify(cm, NULL);
1800}
1801
1802static const struct dev_pm_ops charger_manager_pm = {
1803 .prepare = cm_suspend_prepare,
dfeccb12 1804 .suspend_noirq = cm_suspend_noirq,
3bb3dbbd
DK
1805 .complete = cm_suspend_complete,
1806};
1807
1808static struct platform_driver charger_manager_driver = {
1809 .driver = {
1810 .name = "charger-manager",
1811 .owner = THIS_MODULE,
1812 .pm = &charger_manager_pm,
1813 },
1814 .probe = charger_manager_probe,
28ea73f4 1815 .remove = charger_manager_remove,
3bb3dbbd
DK
1816 .id_table = charger_manager_id,
1817};
1818
1819static int __init charger_manager_init(void)
1820{
d829dc75
CC
1821 cm_wq = create_freezable_workqueue("charger_manager");
1822 INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller);
1823
3bb3dbbd
DK
1824 return platform_driver_register(&charger_manager_driver);
1825}
1826late_initcall(charger_manager_init);
1827
1828static void __exit charger_manager_cleanup(void)
1829{
d829dc75
CC
1830 destroy_workqueue(cm_wq);
1831 cm_wq = NULL;
1832
3bb3dbbd
DK
1833 platform_driver_unregister(&charger_manager_driver);
1834}
1835module_exit(charger_manager_cleanup);
1836
dfeccb12
CC
1837/**
1838 * find_power_supply - find the associated power_supply of charger
1839 * @cm: the Charger Manager representing the battery
1840 * @psy: pointer to instance of charger's power_supply
1841 */
1842static bool find_power_supply(struct charger_manager *cm,
1843 struct power_supply *psy)
1844{
1845 int i;
1846 bool found = false;
1847
1848 for (i = 0; cm->charger_stat[i]; i++) {
1849 if (psy == cm->charger_stat[i]) {
1850 found = true;
1851 break;
1852 }
1853 }
1854
1855 return found;
1856}
1857
1858/**
1859 * cm_notify_event - charger driver notify Charger Manager of charger event
1860 * @psy: pointer to instance of charger's power_supply
1861 * @type: type of charger event
1862 * @msg: optional message passed to uevent_notify fuction
1863 */
1864void cm_notify_event(struct power_supply *psy, enum cm_event_types type,
1865 char *msg)
1866{
1867 struct charger_manager *cm;
1868 bool found_power_supply = false;
1869
1870 if (psy == NULL)
1871 return;
1872
1873 mutex_lock(&cm_list_mtx);
1874 list_for_each_entry(cm, &cm_list, entry) {
1875 found_power_supply = find_power_supply(cm, psy);
1876 if (found_power_supply)
1877 break;
1878 }
1879 mutex_unlock(&cm_list_mtx);
1880
1881 if (!found_power_supply)
1882 return;
1883
1884 switch (type) {
1885 case CM_EVENT_BATT_FULL:
1886 fullbatt_handler(cm);
1887 break;
1888 case CM_EVENT_BATT_OUT:
1889 battout_handler(cm);
1890 break;
1891 case CM_EVENT_BATT_IN:
1892 case CM_EVENT_EXT_PWR_IN_OUT ... CM_EVENT_CHG_START_STOP:
1893 misc_event_handler(cm, type);
1894 break;
1895 case CM_EVENT_UNKNOWN:
1896 case CM_EVENT_OTHERS:
1897 uevent_notify(cm, msg ? msg : default_event_names[type]);
1898 break;
1899 default:
1900 dev_err(cm->dev, "%s type not specified.\n", __func__);
1901 break;
1902 }
1903}
1904EXPORT_SYMBOL_GPL(cm_notify_event);
1905
3bb3dbbd
DK
1906MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1907MODULE_DESCRIPTION("Charger Manager");
1908MODULE_LICENSE("GPL");
This page took 0.128915 seconds and 5 git commands to generate.