charger-manager: Fix bug related to checking fully charged state of battery
[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 */
0fa11dbc
CC
242 if (!is_batt_present(cm))
243 return false;
2ed9e9b6
CC
244
245 if (cm->fuel_gauge && desc->fullbatt_full_capacity > 0) {
0fa11dbc
CC
246 val.intval = 0;
247
2ed9e9b6
CC
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);
0fa11dbc
CC
251 if (!ret && val.intval > desc->fullbatt_full_capacity)
252 return true;
2ed9e9b6
CC
253 }
254
255 /* Full, if it's over the fullbatt voltage */
256 if (desc->fullbatt_uV > 0) {
257 ret = get_batt_uV(cm, &uV);
0fa11dbc
CC
258 if (!ret && uV >= desc->fullbatt_uV)
259 return true;
2ed9e9b6
CC
260 }
261
262 /* Full, if the capacity is more than fullbatt_soc */
263 if (cm->fuel_gauge && desc->fullbatt_soc > 0) {
0fa11dbc
CC
264 val.intval = 0;
265
2ed9e9b6
CC
266 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
267 POWER_SUPPLY_PROP_CAPACITY, &val);
0fa11dbc
CC
268 if (!ret && val.intval >= desc->fullbatt_soc)
269 return true;
2ed9e9b6
CC
270 }
271
0fa11dbc 272 return false;
2ed9e9b6
CC
273}
274
3bb3dbbd
DK
275/**
276 * is_polling_required - Return true if need to continue polling for this CM.
277 * @cm: the Charger Manager representing the battery.
278 */
279static bool is_polling_required(struct charger_manager *cm)
280{
281 switch (cm->desc->polling_mode) {
282 case CM_POLL_DISABLE:
283 return false;
284 case CM_POLL_ALWAYS:
285 return true;
286 case CM_POLL_EXTERNAL_POWER_ONLY:
287 return is_ext_pwr_online(cm);
288 case CM_POLL_CHARGING_ONLY:
289 return is_charging(cm);
290 default:
291 dev_warn(cm->dev, "Incorrect polling_mode (%d)\n",
292 cm->desc->polling_mode);
293 }
294
295 return false;
296}
297
298/**
299 * try_charger_enable - Enable/Disable chargers altogether
300 * @cm: the Charger Manager representing the battery.
301 * @enable: true: enable / false: disable
302 *
303 * Note that Charger Manager keeps the charger enabled regardless whether
304 * the charger is charging or not (because battery is full or no external
305 * power source exists) except when CM needs to disable chargers forcibly
306 * bacause of emergency causes; when the battery is overheated or too cold.
307 */
308static int try_charger_enable(struct charger_manager *cm, bool enable)
309{
310 int err = 0, i;
311 struct charger_desc *desc = cm->desc;
312
313 /* Ignore if it's redundent command */
bb2a95c2 314 if (enable == cm->charger_enabled)
3bb3dbbd
DK
315 return 0;
316
317 if (enable) {
318 if (cm->emergency_stop)
319 return -EAGAIN;
8fcfe088
CC
320
321 /*
322 * Save start time of charging to limit
323 * maximum possible charging time.
324 */
325 cm->charging_start_time = ktime_to_ms(ktime_get());
326 cm->charging_end_time = 0;
327
dbb61fc7 328 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
3950c786
CC
329 if (desc->charger_regulators[i].externally_control)
330 continue;
331
dbb61fc7
CC
332 err = regulator_enable(desc->charger_regulators[i].consumer);
333 if (err < 0) {
334 dev_warn(cm->dev,
335 "Cannot enable %s regulator\n",
336 desc->charger_regulators[i].regulator_name);
337 }
338 }
3bb3dbbd 339 } else {
8fcfe088
CC
340 /*
341 * Save end time of charging to maintain fully charged state
342 * of battery after full-batt.
343 */
344 cm->charging_start_time = 0;
345 cm->charging_end_time = ktime_to_ms(ktime_get());
346
dbb61fc7 347 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
3950c786
CC
348 if (desc->charger_regulators[i].externally_control)
349 continue;
350
dbb61fc7
CC
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
3bb3dbbd
DK
359 /*
360 * Abnormal battery state - Stop charging forcibly,
361 * even if charger was enabled at the other places
362 */
3bb3dbbd
DK
363 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",
bee737bc 370 desc->charger_regulators[i].regulator_name);
3bb3dbbd
DK
371 }
372 }
373 }
374
375 if (!err)
376 cm->charger_enabled = enable;
377
378 return err;
379}
380
d829dc75
CC
381/**
382 * 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
3bb3dbbd
DK
401/**
402 * 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. */
bb2a95c2 429 strncpy(env_str_save, event, UEVENT_BUF_SIZE);
3bb3dbbd
DK
430 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
d829dc75
CC
456/**
457 * 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
fd65ee5f 485 diff = desc->fullbatt_uV;
d829dc75
CC
486 diff -= batt_uV;
487
2ed9e9b6 488 dev_info(cm->dev, "VBATT dropped %duV after full-batt.\n", diff);
d829dc75
CC
489
490 if (diff > desc->fullbatt_vchkdrop_uV) {
491 try_charger_restart(cm);
8fcfe088
CC
492 uevent_notify(cm, "Recharging");
493 }
494}
495
496/**
497 * 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 }
d829dc75 538 }
8fcfe088
CC
539
540 return ret;
d829dc75
CC
541}
542
3bb3dbbd
DK
543/**
544 * _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
2ed9e9b6
CC
558 /* It has been stopped already */
559 if (temp && cm->emergency_stop)
3bb3dbbd
DK
560 return false;
561
2ed9e9b6
CC
562 /*
563 * Check temperature whether overheat or cold.
564 * If temperature is out of range normal state, stop charging.
565 */
3bb3dbbd
DK
566 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 }
2ed9e9b6 574
8fcfe088
CC
575 /*
576 * 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");
2ed9e9b6
CC
582 /*
583 * 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);
3bb3dbbd
DK
603 } else {
604 cm->emergency_stop = 0;
2ed9e9b6
CC
605 if (is_ext_pwr_online(cm)) {
606 if (!try_charger_enable(cm, true))
607 uevent_notify(cm, "CHARGING");
608 }
3bb3dbbd
DK
609 }
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
bb2a95c2
AL
627 list_for_each_entry(cm, &cm_list, entry) {
628 if (_cm_monitor(cm))
629 stop = true;
630 }
3bb3dbbd
DK
631
632 mutex_unlock(&cm_list_mtx);
633
634 return stop;
635}
636
d829dc75
CC
637/**
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))) {
d829dc75 676 next_polling = jiffies + polling_jiffy;
41f63c53 677 mod_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy);
d829dc75
CC
678 }
679
680out:
681 mutex_unlock(&cm_list_mtx);
682}
683static DECLARE_WORK(setup_polling, _setup_polling);
684
685/**
686 * cm_monitor_poller - The Monitor / Poller.
687 * @work: work_struct of the function cm_monitor_poller
688 *
689 * During non-suspended state, cm_monitor_poller is used to poll and monitor
690 * the batteries.
691 */
692static void cm_monitor_poller(struct work_struct *work)
693{
694 cm_monitor();
695 schedule_work(&setup_polling);
696}
697
dfeccb12
CC
698/**
699 * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL
700 * @cm: the Charger Manager representing the battery.
701 */
702static void fullbatt_handler(struct charger_manager *cm)
703{
704 struct charger_desc *desc = cm->desc;
705
706 if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms)
707 goto out;
708
709 if (cm_suspended)
710 device_set_wakeup_capable(cm->dev, true);
711
41f63c53
TH
712 mod_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
713 msecs_to_jiffies(desc->fullbatt_vchkdrop_ms));
dfeccb12
CC
714 cm->fullbatt_vchk_jiffies_at = jiffies + msecs_to_jiffies(
715 desc->fullbatt_vchkdrop_ms);
716
717 if (cm->fullbatt_vchk_jiffies_at == 0)
718 cm->fullbatt_vchk_jiffies_at = 1;
719
720out:
721 dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged.\n");
722 uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]);
723}
724
725/**
726 * battout_handler - Event handler for CM_EVENT_BATT_OUT
727 * @cm: the Charger Manager representing the battery.
728 */
729static void battout_handler(struct charger_manager *cm)
730{
731 if (cm_suspended)
732 device_set_wakeup_capable(cm->dev, true);
733
734 if (!is_batt_present(cm)) {
735 dev_emerg(cm->dev, "Battery Pulled Out!\n");
736 uevent_notify(cm, default_event_names[CM_EVENT_BATT_OUT]);
737 } else {
738 uevent_notify(cm, "Battery Reinserted?");
739 }
740}
741
742/**
743 * misc_event_handler - Handler for other evnets
744 * @cm: the Charger Manager representing the battery.
745 * @type: the Charger Manager representing the battery.
746 */
747static void misc_event_handler(struct charger_manager *cm,
748 enum cm_event_types type)
749{
750 if (cm_suspended)
751 device_set_wakeup_capable(cm->dev, true);
752
753 if (!delayed_work_pending(&cm_monitor_work) &&
754 is_polling_required(cm) && cm->desc->polling_interval_ms)
755 schedule_work(&setup_polling);
756 uevent_notify(cm, default_event_names[type]);
757}
758
ad3d13ee
DK
759static int charger_get_property(struct power_supply *psy,
760 enum power_supply_property psp,
761 union power_supply_propval *val)
762{
763 struct charger_manager *cm = container_of(psy,
764 struct charger_manager, charger_psy);
765 struct charger_desc *desc = cm->desc;
df58c04c
AV
766 int ret = 0;
767 int uV;
ad3d13ee
DK
768
769 switch (psp) {
770 case POWER_SUPPLY_PROP_STATUS:
771 if (is_charging(cm))
772 val->intval = POWER_SUPPLY_STATUS_CHARGING;
773 else if (is_ext_pwr_online(cm))
774 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
775 else
776 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
777 break;
778 case POWER_SUPPLY_PROP_HEALTH:
779 if (cm->emergency_stop > 0)
780 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
781 else if (cm->emergency_stop < 0)
782 val->intval = POWER_SUPPLY_HEALTH_COLD;
783 else
784 val->intval = POWER_SUPPLY_HEALTH_GOOD;
785 break;
786 case POWER_SUPPLY_PROP_PRESENT:
787 if (is_batt_present(cm))
788 val->intval = 1;
789 else
790 val->intval = 0;
791 break;
792 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
df58c04c 793 ret = get_batt_uV(cm, &val->intval);
ad3d13ee
DK
794 break;
795 case POWER_SUPPLY_PROP_CURRENT_NOW:
796 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
797 POWER_SUPPLY_PROP_CURRENT_NOW, val);
798 break;
799 case POWER_SUPPLY_PROP_TEMP:
800 /* in thenth of centigrade */
801 if (cm->last_temp_mC == INT_MIN)
802 desc->temperature_out_of_range(&cm->last_temp_mC);
803 val->intval = cm->last_temp_mC / 100;
804 if (!desc->measure_battery_temp)
805 ret = -ENODEV;
806 break;
807 case POWER_SUPPLY_PROP_TEMP_AMBIENT:
808 /* in thenth of centigrade */
809 if (cm->last_temp_mC == INT_MIN)
810 desc->temperature_out_of_range(&cm->last_temp_mC);
811 val->intval = cm->last_temp_mC / 100;
812 if (desc->measure_battery_temp)
813 ret = -ENODEV;
814 break;
815 case POWER_SUPPLY_PROP_CAPACITY:
816 if (!cm->fuel_gauge) {
817 ret = -ENODEV;
818 break;
819 }
820
821 if (!is_batt_present(cm)) {
822 /* There is no battery. Assume 100% */
823 val->intval = 100;
824 break;
825 }
826
827 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
828 POWER_SUPPLY_PROP_CAPACITY, val);
829 if (ret)
830 break;
831
832 if (val->intval > 100) {
833 val->intval = 100;
834 break;
835 }
836 if (val->intval < 0)
837 val->intval = 0;
838
839 /* Do not adjust SOC when charging: voltage is overrated */
840 if (is_charging(cm))
841 break;
842
843 /*
844 * If the capacity value is inconsistent, calibrate it base on
845 * the battery voltage values and the thresholds given as desc
846 */
847 ret = get_batt_uV(cm, &uV);
848 if (ret) {
849 /* Voltage information not available. No calibration */
850 ret = 0;
851 break;
852 }
853
854 if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV &&
855 !is_charging(cm)) {
856 val->intval = 100;
857 break;
858 }
859
860 break;
861 case POWER_SUPPLY_PROP_ONLINE:
862 if (is_ext_pwr_online(cm))
863 val->intval = 1;
864 else
865 val->intval = 0;
866 break;
867 case POWER_SUPPLY_PROP_CHARGE_FULL:
2ed9e9b6 868 if (is_full_charged(cm))
ad3d13ee 869 val->intval = 1;
2ed9e9b6
CC
870 else
871 val->intval = 0;
ad3d13ee
DK
872 ret = 0;
873 break;
874 case POWER_SUPPLY_PROP_CHARGE_NOW:
875 if (is_charging(cm)) {
876 ret = cm->fuel_gauge->get_property(cm->fuel_gauge,
877 POWER_SUPPLY_PROP_CHARGE_NOW,
878 val);
879 if (ret) {
880 val->intval = 1;
881 ret = 0;
882 } else {
883 /* If CHARGE_NOW is supplied, use it */
884 val->intval = (val->intval > 0) ?
885 val->intval : 1;
886 }
887 } else {
888 val->intval = 0;
889 }
890 break;
891 default:
892 return -EINVAL;
893 }
894 return ret;
895}
896
897#define NUM_CHARGER_PSY_OPTIONAL (4)
898static enum power_supply_property default_charger_props[] = {
899 /* Guaranteed to provide */
900 POWER_SUPPLY_PROP_STATUS,
901 POWER_SUPPLY_PROP_HEALTH,
902 POWER_SUPPLY_PROP_PRESENT,
903 POWER_SUPPLY_PROP_VOLTAGE_NOW,
904 POWER_SUPPLY_PROP_CAPACITY,
905 POWER_SUPPLY_PROP_ONLINE,
906 POWER_SUPPLY_PROP_CHARGE_FULL,
907 /*
908 * Optional properties are:
909 * POWER_SUPPLY_PROP_CHARGE_NOW,
910 * POWER_SUPPLY_PROP_CURRENT_NOW,
911 * POWER_SUPPLY_PROP_TEMP, and
912 * POWER_SUPPLY_PROP_TEMP_AMBIENT,
913 */
914};
915
916static struct power_supply psy_default = {
917 .name = "battery",
918 .type = POWER_SUPPLY_TYPE_BATTERY,
919 .properties = default_charger_props,
920 .num_properties = ARRAY_SIZE(default_charger_props),
921 .get_property = charger_get_property,
922};
923
3bb3dbbd
DK
924/**
925 * cm_setup_timer - For in-suspend monitoring setup wakeup alarm
926 * for suspend_again.
927 *
928 * Returns true if the alarm is set for Charger Manager to use.
929 * Returns false if
930 * cm_setup_timer fails to set an alarm,
931 * cm_setup_timer does not need to set an alarm for Charger Manager,
932 * or an alarm previously configured is to be used.
933 */
934static bool cm_setup_timer(void)
935{
936 struct charger_manager *cm;
937 unsigned int wakeup_ms = UINT_MAX;
938 bool ret = false;
939
940 mutex_lock(&cm_list_mtx);
941
942 list_for_each_entry(cm, &cm_list, entry) {
d829dc75
CC
943 unsigned int fbchk_ms = 0;
944
945 /* fullbatt_vchk is required. setup timer for that */
946 if (cm->fullbatt_vchk_jiffies_at) {
947 fbchk_ms = jiffies_to_msecs(cm->fullbatt_vchk_jiffies_at
948 - jiffies);
949 if (time_is_before_eq_jiffies(
950 cm->fullbatt_vchk_jiffies_at) ||
951 msecs_to_jiffies(fbchk_ms) < CM_JIFFIES_SMALL) {
952 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
953 fbchk_ms = 0;
954 }
955 }
956 CM_MIN_VALID(wakeup_ms, fbchk_ms);
957
3bb3dbbd
DK
958 /* Skip if polling is not required for this CM */
959 if (!is_polling_required(cm) && !cm->emergency_stop)
960 continue;
961 if (cm->desc->polling_interval_ms == 0)
962 continue;
963 CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms);
964 }
965
966 mutex_unlock(&cm_list_mtx);
967
968 if (wakeup_ms < UINT_MAX && wakeup_ms > 0) {
969 pr_info("Charger Manager wakeup timer: %u ms.\n", wakeup_ms);
970 if (rtc_dev) {
971 struct rtc_wkalrm tmp;
972 unsigned long time, now;
973 unsigned long add = DIV_ROUND_UP(wakeup_ms, 1000);
974
975 /*
976 * Set alarm with the polling interval (wakeup_ms)
977 * except when rtc_wkalarm_save comes first.
978 * However, the alarm time should be NOW +
979 * CM_RTC_SMALL or later.
980 */
981 tmp.enabled = 1;
982 rtc_read_time(rtc_dev, &tmp.time);
983 rtc_tm_to_time(&tmp.time, &now);
984 if (add < CM_RTC_SMALL)
985 add = CM_RTC_SMALL;
986 time = now + add;
987
988 ret = true;
989
990 if (rtc_wkalarm_save.enabled &&
991 rtc_wkalarm_save_time &&
992 rtc_wkalarm_save_time < time) {
993 if (rtc_wkalarm_save_time < now + CM_RTC_SMALL)
994 time = now + CM_RTC_SMALL;
995 else
996 time = rtc_wkalarm_save_time;
997
998 /* The timer is not appointed by CM */
999 ret = false;
1000 }
1001
1002 pr_info("Waking up after %lu secs.\n",
1003 time - now);
1004
1005 rtc_time_to_tm(time, &tmp.time);
1006 rtc_set_alarm(rtc_dev, &tmp);
1007 cm_suspend_duration_ms += wakeup_ms;
1008 return ret;
1009 }
1010 }
1011
1012 if (rtc_dev)
1013 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1014 return false;
1015}
1016
d829dc75
CC
1017static void _cm_fbchk_in_suspend(struct charger_manager *cm)
1018{
1019 unsigned long jiffy_now = jiffies;
1020
1021 if (!cm->fullbatt_vchk_jiffies_at)
1022 return;
1023
1024 if (g_desc && g_desc->assume_timer_stops_in_suspend)
1025 jiffy_now += msecs_to_jiffies(cm_suspend_duration_ms);
1026
1027 /* Execute now if it's going to be executed not too long after */
1028 jiffy_now += CM_JIFFIES_SMALL;
1029
1030 if (time_after_eq(jiffy_now, cm->fullbatt_vchk_jiffies_at))
1031 fullbatt_vchk(&cm->fullbatt_vchk_work.work);
1032}
1033
3bb3dbbd
DK
1034/**
1035 * cm_suspend_again - Determine whether suspend again or not
1036 *
1037 * Returns true if the system should be suspended again
1038 * Returns false if the system should be woken up
1039 */
1040bool cm_suspend_again(void)
1041{
1042 struct charger_manager *cm;
1043 bool ret = false;
1044
1045 if (!g_desc || !g_desc->rtc_only_wakeup || !g_desc->rtc_only_wakeup() ||
1046 !cm_rtc_set)
1047 return false;
1048
1049 if (cm_monitor())
1050 goto out;
1051
1052 ret = true;
1053 mutex_lock(&cm_list_mtx);
1054 list_for_each_entry(cm, &cm_list, entry) {
d829dc75
CC
1055 _cm_fbchk_in_suspend(cm);
1056
3bb3dbbd 1057 if (cm->status_save_ext_pwr_inserted != is_ext_pwr_online(cm) ||
bb2a95c2 1058 cm->status_save_batt != is_batt_present(cm)) {
3bb3dbbd 1059 ret = false;
bb2a95c2
AL
1060 break;
1061 }
3bb3dbbd
DK
1062 }
1063 mutex_unlock(&cm_list_mtx);
1064
1065 cm_rtc_set = cm_setup_timer();
1066out:
1067 /* It's about the time when the non-CM appointed timer goes off */
1068 if (rtc_wkalarm_save.enabled) {
1069 unsigned long now;
1070 struct rtc_time tmp;
1071
1072 rtc_read_time(rtc_dev, &tmp);
1073 rtc_tm_to_time(&tmp, &now);
1074
1075 if (rtc_wkalarm_save_time &&
1076 now + CM_RTC_SMALL >= rtc_wkalarm_save_time)
1077 return false;
1078 }
1079 return ret;
1080}
1081EXPORT_SYMBOL_GPL(cm_suspend_again);
1082
1083/**
1084 * setup_charger_manager - initialize charger_global_desc data
1085 * @gd: pointer to instance of charger_global_desc
1086 */
1087int setup_charger_manager(struct charger_global_desc *gd)
1088{
1089 if (!gd)
1090 return -EINVAL;
1091
1092 if (rtc_dev)
1093 rtc_class_close(rtc_dev);
1094 rtc_dev = NULL;
1095 g_desc = NULL;
1096
1097 if (!gd->rtc_only_wakeup) {
1098 pr_err("The callback rtc_only_wakeup is not given.\n");
1099 return -EINVAL;
1100 }
1101
1102 if (gd->rtc_name) {
1103 rtc_dev = rtc_class_open(gd->rtc_name);
1104 if (IS_ERR_OR_NULL(rtc_dev)) {
1105 rtc_dev = NULL;
1106 /* Retry at probe. RTC may be not registered yet */
1107 }
1108 } else {
1109 pr_warn("No wakeup timer is given for charger manager."
1110 "In-suspend monitoring won't work.\n");
1111 }
1112
1113 g_desc = gd;
1114 return 0;
1115}
1116EXPORT_SYMBOL_GPL(setup_charger_manager);
1117
bee737bc
CC
1118/**
1119 * charger_extcon_work - enable/diable charger according to the state
1120 * of charger cable
1121 *
1122 * @work: work_struct of the function charger_extcon_work.
1123 */
1124static void charger_extcon_work(struct work_struct *work)
1125{
1126 struct charger_cable *cable =
1127 container_of(work, struct charger_cable, wq);
45cd4fb2
CC
1128 int ret;
1129
1130 if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) {
1131 ret = regulator_set_current_limit(cable->charger->consumer,
1132 cable->min_uA, cable->max_uA);
1133 if (ret < 0) {
1134 pr_err("Cannot set current limit of %s (%s)\n",
1135 cable->charger->regulator_name, cable->name);
1136 return;
1137 }
1138
1139 pr_info("Set current limit of %s : %duA ~ %duA\n",
1140 cable->charger->regulator_name,
1141 cable->min_uA, cable->max_uA);
1142 }
bee737bc
CC
1143
1144 try_charger_enable(cable->cm, cable->attached);
1145}
1146
1147/**
1148 * charger_extcon_notifier - receive the state of charger cable
1149 * when registered cable is attached or detached.
1150 *
1151 * @self: the notifier block of the charger_extcon_notifier.
1152 * @event: the cable state.
1153 * @ptr: the data pointer of notifier block.
1154 */
1155static int charger_extcon_notifier(struct notifier_block *self,
1156 unsigned long event, void *ptr)
1157{
1158 struct charger_cable *cable =
1159 container_of(self, struct charger_cable, nb);
1160
2ed9e9b6
CC
1161 /*
1162 * The newly state of charger cable.
1163 * If cable is attached, cable->attached is true.
1164 */
bee737bc 1165 cable->attached = event;
2ed9e9b6
CC
1166
1167 /*
1168 * Setup monitoring to check battery state
1169 * when charger cable is attached.
1170 */
1171 if (cable->attached && is_polling_required(cable->cm)) {
1172 if (work_pending(&setup_polling))
1173 cancel_work_sync(&setup_polling);
1174 schedule_work(&setup_polling);
1175 }
1176
1177 /*
1178 * Setup work for controlling charger(regulator)
1179 * according to charger cable.
1180 */
bee737bc
CC
1181 schedule_work(&cable->wq);
1182
1183 return NOTIFY_DONE;
1184}
1185
1186/**
1187 * charger_extcon_init - register external connector to use it
1188 * as the charger cable
1189 *
1190 * @cm: the Charger Manager representing the battery.
1191 * @cable: the Charger cable representing the external connector.
1192 */
1193static int charger_extcon_init(struct charger_manager *cm,
1194 struct charger_cable *cable)
1195{
1196 int ret = 0;
1197
1198 /*
1199 * Charger manager use Extcon framework to identify
1200 * the charger cable among various external connector
1201 * cable (e.g., TA, USB, MHL, Dock).
1202 */
1203 INIT_WORK(&cable->wq, charger_extcon_work);
1204 cable->nb.notifier_call = charger_extcon_notifier;
1205 ret = extcon_register_interest(&cable->extcon_dev,
1206 cable->extcon_name, cable->name, &cable->nb);
1207 if (ret < 0) {
1208 pr_info("Cannot register extcon_dev for %s(cable: %s).\n",
1209 cable->extcon_name,
1210 cable->name);
1211 ret = -EINVAL;
1212 }
1213
1214 return ret;
1215}
1216
3950c786
CC
1217/* help function of sysfs node to control charger(regulator) */
1218static ssize_t charger_name_show(struct device *dev,
1219 struct device_attribute *attr, char *buf)
1220{
1221 struct charger_regulator *charger
1222 = container_of(attr, struct charger_regulator, attr_name);
1223
1224 return sprintf(buf, "%s\n", charger->regulator_name);
1225}
1226
1227static ssize_t charger_state_show(struct device *dev,
1228 struct device_attribute *attr, char *buf)
1229{
1230 struct charger_regulator *charger
1231 = container_of(attr, struct charger_regulator, attr_state);
1232 int state = 0;
1233
1234 if (!charger->externally_control)
1235 state = regulator_is_enabled(charger->consumer);
1236
1237 return sprintf(buf, "%s\n", state ? "enabled" : "disabled");
1238}
1239
1240static ssize_t charger_externally_control_show(struct device *dev,
1241 struct device_attribute *attr, char *buf)
1242{
1243 struct charger_regulator *charger = container_of(attr,
1244 struct charger_regulator, attr_externally_control);
1245
1246 return sprintf(buf, "%d\n", charger->externally_control);
1247}
1248
1249static ssize_t charger_externally_control_store(struct device *dev,
1250 struct device_attribute *attr, const char *buf,
1251 size_t count)
1252{
1253 struct charger_regulator *charger
1254 = container_of(attr, struct charger_regulator,
1255 attr_externally_control);
1256 struct charger_manager *cm = charger->cm;
1257 struct charger_desc *desc = cm->desc;
1258 int i;
1259 int ret;
1260 int externally_control;
1261 int chargers_externally_control = 1;
1262
1263 ret = sscanf(buf, "%d", &externally_control);
1264 if (ret == 0) {
1265 ret = -EINVAL;
1266 return ret;
1267 }
1268
1269 if (!externally_control) {
1270 charger->externally_control = 0;
1271 return count;
1272 }
1273
1274 for (i = 0; i < desc->num_charger_regulators; i++) {
1275 if (&desc->charger_regulators[i] != charger &&
1276 !desc->charger_regulators[i].externally_control) {
1277 /*
1278 * At least, one charger is controlled by
1279 * charger-manager
1280 */
1281 chargers_externally_control = 0;
1282 break;
1283 }
1284 }
1285
1286 if (!chargers_externally_control) {
1287 if (cm->charger_enabled) {
1288 try_charger_enable(charger->cm, false);
1289 charger->externally_control = externally_control;
1290 try_charger_enable(charger->cm, true);
1291 } else {
1292 charger->externally_control = externally_control;
1293 }
1294 } else {
1295 dev_warn(cm->dev,
1296 "'%s' regulator should be controlled "
1297 "in charger-manager because charger-manager "
1298 "must need at least one charger for charging\n",
1299 charger->regulator_name);
1300 }
1301
1302 return count;
1303}
1304
3bb3dbbd
DK
1305static int charger_manager_probe(struct platform_device *pdev)
1306{
1307 struct charger_desc *desc = dev_get_platdata(&pdev->dev);
1308 struct charger_manager *cm;
1309 int ret = 0, i = 0;
bee737bc 1310 int j = 0;
3950c786 1311 int chargers_externally_control = 1;
ad3d13ee 1312 union power_supply_propval val;
3bb3dbbd
DK
1313
1314 if (g_desc && !rtc_dev && g_desc->rtc_name) {
1315 rtc_dev = rtc_class_open(g_desc->rtc_name);
1316 if (IS_ERR_OR_NULL(rtc_dev)) {
1317 rtc_dev = NULL;
1318 dev_err(&pdev->dev, "Cannot get RTC %s.\n",
1319 g_desc->rtc_name);
1320 ret = -ENODEV;
1321 goto err_alloc;
1322 }
1323 }
1324
1325 if (!desc) {
1326 dev_err(&pdev->dev, "No platform data (desc) found.\n");
1327 ret = -ENODEV;
1328 goto err_alloc;
1329 }
1330
1331 cm = kzalloc(sizeof(struct charger_manager), GFP_KERNEL);
1332 if (!cm) {
1333 dev_err(&pdev->dev, "Cannot allocate memory.\n");
1334 ret = -ENOMEM;
1335 goto err_alloc;
1336 }
1337
1338 /* Basic Values. Unspecified are Null or 0 */
1339 cm->dev = &pdev->dev;
1340 cm->desc = kzalloc(sizeof(struct charger_desc), GFP_KERNEL);
1341 if (!cm->desc) {
1342 dev_err(&pdev->dev, "Cannot allocate memory.\n");
1343 ret = -ENOMEM;
1344 goto err_alloc_desc;
1345 }
1346 memcpy(cm->desc, desc, sizeof(struct charger_desc));
1347 cm->last_temp_mC = INT_MIN; /* denotes "unmeasured, yet" */
1348
d829dc75
CC
1349 /*
1350 * The following two do not need to be errors.
1351 * Users may intentionally ignore those two features.
1352 */
1353 if (desc->fullbatt_uV == 0) {
1354 dev_info(&pdev->dev, "Ignoring full-battery voltage threshold"
1355 " as it is not supplied.");
1356 }
1357 if (!desc->fullbatt_vchkdrop_ms || !desc->fullbatt_vchkdrop_uV) {
1358 dev_info(&pdev->dev, "Disabling full-battery voltage drop "
1359 "checking mechanism as it is not supplied.");
1360 desc->fullbatt_vchkdrop_ms = 0;
1361 desc->fullbatt_vchkdrop_uV = 0;
1362 }
2ed9e9b6
CC
1363 if (desc->fullbatt_soc == 0) {
1364 dev_info(&pdev->dev, "Ignoring full-battery soc(state of"
1365 " charge) threshold as it is not"
1366 " supplied.");
1367 }
1368 if (desc->fullbatt_full_capacity == 0) {
1369 dev_info(&pdev->dev, "Ignoring full-battery full capacity"
1370 " threshold as it is not supplied.");
1371 }
d829dc75 1372
3bb3dbbd
DK
1373 if (!desc->charger_regulators || desc->num_charger_regulators < 1) {
1374 ret = -EINVAL;
1375 dev_err(&pdev->dev, "charger_regulators undefined.\n");
1376 goto err_no_charger;
1377 }
1378
1379 if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) {
1380 dev_err(&pdev->dev, "No power supply defined.\n");
1381 ret = -EINVAL;
1382 goto err_no_charger_stat;
1383 }
1384
1385 /* Counting index only */
1386 while (desc->psy_charger_stat[i])
1387 i++;
1388
1389 cm->charger_stat = kzalloc(sizeof(struct power_supply *) * (i + 1),
1390 GFP_KERNEL);
1391 if (!cm->charger_stat) {
1392 ret = -ENOMEM;
1393 goto err_no_charger_stat;
1394 }
1395
1396 for (i = 0; desc->psy_charger_stat[i]; i++) {
1397 cm->charger_stat[i] = power_supply_get_by_name(
1398 desc->psy_charger_stat[i]);
1399 if (!cm->charger_stat[i]) {
1400 dev_err(&pdev->dev, "Cannot find power supply "
1401 "\"%s\"\n",
1402 desc->psy_charger_stat[i]);
1403 ret = -ENODEV;
1404 goto err_chg_stat;
1405 }
1406 }
1407
1408 cm->fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge);
1409 if (!cm->fuel_gauge) {
1410 dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n",
1411 desc->psy_fuel_gauge);
1412 ret = -ENODEV;
1413 goto err_chg_stat;
1414 }
1415
1416 if (desc->polling_interval_ms == 0 ||
1417 msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) {
1418 dev_err(&pdev->dev, "polling_interval_ms is too small\n");
1419 ret = -EINVAL;
1420 goto err_chg_stat;
1421 }
1422
1423 if (!desc->temperature_out_of_range) {
1424 dev_err(&pdev->dev, "there is no temperature_out_of_range\n");
1425 ret = -EINVAL;
1426 goto err_chg_stat;
1427 }
1428
8fcfe088
CC
1429 if (!desc->charging_max_duration_ms ||
1430 !desc->discharging_max_duration_ms) {
1431 dev_info(&pdev->dev, "Cannot limit charging duration "
1432 "checking mechanism to prevent overcharge/overheat "
1433 "and control discharging duration");
1434 desc->charging_max_duration_ms = 0;
1435 desc->discharging_max_duration_ms = 0;
1436 }
1437
3bb3dbbd
DK
1438 platform_set_drvdata(pdev, cm);
1439
bb2a95c2
AL
1440 memcpy(&cm->charger_psy, &psy_default, sizeof(psy_default));
1441
ad3d13ee 1442 if (!desc->psy_name) {
bb2a95c2 1443 strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX);
ad3d13ee
DK
1444 } else {
1445 strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX);
1446 }
1447 cm->charger_psy.name = cm->psy_name_buf;
1448
1449 /* Allocate for psy properties because they may vary */
1450 cm->charger_psy.properties = kzalloc(sizeof(enum power_supply_property)
1451 * (ARRAY_SIZE(default_charger_props) +
1452 NUM_CHARGER_PSY_OPTIONAL),
1453 GFP_KERNEL);
1454 if (!cm->charger_psy.properties) {
1455 dev_err(&pdev->dev, "Cannot allocate for psy properties.\n");
1456 ret = -ENOMEM;
1457 goto err_chg_stat;
1458 }
1459 memcpy(cm->charger_psy.properties, default_charger_props,
1460 sizeof(enum power_supply_property) *
1461 ARRAY_SIZE(default_charger_props));
1462 cm->charger_psy.num_properties = psy_default.num_properties;
1463
1464 /* Find which optional psy-properties are available */
1465 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1466 POWER_SUPPLY_PROP_CHARGE_NOW, &val)) {
1467 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1468 POWER_SUPPLY_PROP_CHARGE_NOW;
1469 cm->charger_psy.num_properties++;
1470 }
1471 if (!cm->fuel_gauge->get_property(cm->fuel_gauge,
1472 POWER_SUPPLY_PROP_CURRENT_NOW,
1473 &val)) {
1474 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1475 POWER_SUPPLY_PROP_CURRENT_NOW;
1476 cm->charger_psy.num_properties++;
1477 }
bb2a95c2 1478
ad3d13ee
DK
1479 if (desc->measure_battery_temp) {
1480 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1481 POWER_SUPPLY_PROP_TEMP;
1482 cm->charger_psy.num_properties++;
bb2a95c2
AL
1483 } else {
1484 cm->charger_psy.properties[cm->charger_psy.num_properties] =
1485 POWER_SUPPLY_PROP_TEMP_AMBIENT;
1486 cm->charger_psy.num_properties++;
ad3d13ee
DK
1487 }
1488
d829dc75
CC
1489 INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk);
1490
ad3d13ee
DK
1491 ret = power_supply_register(NULL, &cm->charger_psy);
1492 if (ret) {
1493 dev_err(&pdev->dev, "Cannot register charger-manager with"
1494 " name \"%s\".\n", cm->charger_psy.name);
1495 goto err_register;
1496 }
1497
bee737bc
CC
1498 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1499 struct charger_regulator *charger
1500 = &desc->charger_regulators[i];
3950c786
CC
1501 char buf[11];
1502 char *str;
bee737bc
CC
1503
1504 charger->consumer = regulator_get(&pdev->dev,
1505 charger->regulator_name);
1506 if (charger->consumer == NULL) {
1507 dev_err(&pdev->dev, "Cannot find charger(%s)n",
1508 charger->regulator_name);
1509 ret = -EINVAL;
1510 goto err_chg_get;
1511 }
3950c786 1512 charger->cm = cm;
bee737bc
CC
1513
1514 for (j = 0 ; j < charger->num_cables ; j++) {
1515 struct charger_cable *cable = &charger->cables[j];
1516
1517 ret = charger_extcon_init(cm, cable);
1518 if (ret < 0) {
1519 dev_err(&pdev->dev, "Cannot find charger(%s)n",
1520 charger->regulator_name);
1521 goto err_extcon;
1522 }
1523 cable->charger = charger;
1524 cable->cm = cm;
1525 }
3950c786
CC
1526
1527 /* Create sysfs entry to control charger(regulator) */
1528 snprintf(buf, 10, "charger.%d", i);
1529 str = kzalloc(sizeof(char) * (strlen(buf) + 1), GFP_KERNEL);
1530 if (!str) {
1531 for (i--; i >= 0; i--) {
1532 charger = &desc->charger_regulators[i];
1533 kfree(charger->attr_g.name);
1534 }
1535 ret = -ENOMEM;
1536
1537 goto err_extcon;
1538 }
1539 strcpy(str, buf);
1540
1541 charger->attrs[0] = &charger->attr_name.attr;
1542 charger->attrs[1] = &charger->attr_state.attr;
1543 charger->attrs[2] = &charger->attr_externally_control.attr;
1544 charger->attrs[3] = NULL;
1545 charger->attr_g.name = str;
1546 charger->attr_g.attrs = charger->attrs;
1547
1548 sysfs_attr_init(&charger->attr_name.attr);
1549 charger->attr_name.attr.name = "name";
1550 charger->attr_name.attr.mode = 0444;
1551 charger->attr_name.show = charger_name_show;
1552
1553 sysfs_attr_init(&charger->attr_state.attr);
1554 charger->attr_state.attr.name = "state";
1555 charger->attr_state.attr.mode = 0444;
1556 charger->attr_state.show = charger_state_show;
1557
1558 sysfs_attr_init(&charger->attr_externally_control.attr);
1559 charger->attr_externally_control.attr.name
1560 = "externally_control";
1561 charger->attr_externally_control.attr.mode = 0644;
1562 charger->attr_externally_control.show
1563 = charger_externally_control_show;
1564 charger->attr_externally_control.store
1565 = charger_externally_control_store;
1566
1567 if (!desc->charger_regulators[i].externally_control ||
1568 !chargers_externally_control) {
1569 chargers_externally_control = 0;
1570 }
1571 dev_info(&pdev->dev, "'%s' regulator's externally_control"
1572 "is %d\n", charger->regulator_name,
1573 charger->externally_control);
1574
1575 ret = sysfs_create_group(&cm->charger_psy.dev->kobj,
1576 &charger->attr_g);
1577 if (ret < 0) {
1578 dev_info(&pdev->dev, "Cannot create sysfs entry"
1579 "of %s regulator\n",
1580 charger->regulator_name);
1581 }
1582 }
1583
1584 if (chargers_externally_control) {
1585 dev_err(&pdev->dev, "Cannot register regulator because "
1586 "charger-manager must need at least "
1587 "one charger for charging battery\n");
1588
1589 ret = -EINVAL;
1590 goto err_chg_enable;
3bb3dbbd
DK
1591 }
1592
1593 ret = try_charger_enable(cm, true);
1594 if (ret) {
1595 dev_err(&pdev->dev, "Cannot enable charger regulators\n");
1596 goto err_chg_enable;
1597 }
1598
1599 /* Add to the list */
1600 mutex_lock(&cm_list_mtx);
1601 list_add(&cm->entry, &cm_list);
1602 mutex_unlock(&cm_list_mtx);
1603
dfeccb12
CC
1604 /*
1605 * Charger-manager is capable of waking up the systme from sleep
1606 * when event is happend through cm_notify_event()
1607 */
1608 device_init_wakeup(&pdev->dev, true);
1609 device_set_wakeup_capable(&pdev->dev, false);
1610
d829dc75
CC
1611 schedule_work(&setup_polling);
1612
3bb3dbbd
DK
1613 return 0;
1614
1615err_chg_enable:
3950c786
CC
1616 for (i = 0; i < desc->num_charger_regulators; i++) {
1617 struct charger_regulator *charger;
1618
1619 charger = &desc->charger_regulators[i];
1620 sysfs_remove_group(&cm->charger_psy.dev->kobj,
1621 &charger->attr_g);
1622 kfree(charger->attr_g.name);
1623 }
bee737bc
CC
1624err_extcon:
1625 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1626 struct charger_regulator *charger
1627 = &desc->charger_regulators[i];
1628 for (j = 0 ; j < charger->num_cables ; j++) {
1629 struct charger_cable *cable = &charger->cables[j];
1630 extcon_unregister_interest(&cable->extcon_dev);
1631 }
1632 }
1633err_chg_get:
1634 for (i = 0 ; i < desc->num_charger_regulators ; i++)
1635 regulator_put(desc->charger_regulators[i].consumer);
1636
ad3d13ee
DK
1637 power_supply_unregister(&cm->charger_psy);
1638err_register:
1639 kfree(cm->charger_psy.properties);
3bb3dbbd
DK
1640err_chg_stat:
1641 kfree(cm->charger_stat);
1642err_no_charger_stat:
1643err_no_charger:
1644 kfree(cm->desc);
1645err_alloc_desc:
1646 kfree(cm);
1647err_alloc:
1648 return ret;
1649}
1650
415ec69f 1651static int charger_manager_remove(struct platform_device *pdev)
3bb3dbbd
DK
1652{
1653 struct charger_manager *cm = platform_get_drvdata(pdev);
1654 struct charger_desc *desc = cm->desc;
bee737bc
CC
1655 int i = 0;
1656 int j = 0;
3bb3dbbd
DK
1657
1658 /* Remove from the list */
1659 mutex_lock(&cm_list_mtx);
1660 list_del(&cm->entry);
1661 mutex_unlock(&cm_list_mtx);
1662
d829dc75
CC
1663 if (work_pending(&setup_polling))
1664 cancel_work_sync(&setup_polling);
1665 if (delayed_work_pending(&cm_monitor_work))
1666 cancel_delayed_work_sync(&cm_monitor_work);
1667
bee737bc
CC
1668 for (i = 0 ; i < desc->num_charger_regulators ; i++) {
1669 struct charger_regulator *charger
1670 = &desc->charger_regulators[i];
1671 for (j = 0 ; j < charger->num_cables ; j++) {
1672 struct charger_cable *cable = &charger->cables[j];
1673 extcon_unregister_interest(&cable->extcon_dev);
1674 }
1675 }
1676
1677 for (i = 0 ; i < desc->num_charger_regulators ; i++)
1678 regulator_put(desc->charger_regulators[i].consumer);
1679
ad3d13ee 1680 power_supply_unregister(&cm->charger_psy);
d829dc75
CC
1681
1682 try_charger_enable(cm, false);
1683
ad3d13ee 1684 kfree(cm->charger_psy.properties);
3bb3dbbd
DK
1685 kfree(cm->charger_stat);
1686 kfree(cm->desc);
1687 kfree(cm);
1688
1689 return 0;
1690}
1691
1bbe24d4 1692static const struct platform_device_id charger_manager_id[] = {
3bb3dbbd
DK
1693 { "charger-manager", 0 },
1694 { },
1695};
1bbe24d4 1696MODULE_DEVICE_TABLE(platform, charger_manager_id);
3bb3dbbd 1697
dfeccb12
CC
1698static int cm_suspend_noirq(struct device *dev)
1699{
1700 int ret = 0;
1701
1702 if (device_may_wakeup(dev)) {
1703 device_set_wakeup_capable(dev, false);
1704 ret = -EAGAIN;
1705 }
1706
1707 return ret;
1708}
1709
3bb3dbbd
DK
1710static int cm_suspend_prepare(struct device *dev)
1711{
bb2a95c2 1712 struct charger_manager *cm = dev_get_drvdata(dev);
3bb3dbbd
DK
1713
1714 if (!cm_suspended) {
1715 if (rtc_dev) {
1716 struct rtc_time tmp;
1717 unsigned long now;
1718
1719 rtc_read_alarm(rtc_dev, &rtc_wkalarm_save);
1720 rtc_read_time(rtc_dev, &tmp);
1721
1722 if (rtc_wkalarm_save.enabled) {
1723 rtc_tm_to_time(&rtc_wkalarm_save.time,
1724 &rtc_wkalarm_save_time);
1725 rtc_tm_to_time(&tmp, &now);
1726 if (now > rtc_wkalarm_save_time)
1727 rtc_wkalarm_save_time = 0;
1728 } else {
1729 rtc_wkalarm_save_time = 0;
1730 }
1731 }
1732 cm_suspended = true;
1733 }
1734
d829dc75
CC
1735 if (delayed_work_pending(&cm->fullbatt_vchk_work))
1736 cancel_delayed_work(&cm->fullbatt_vchk_work);
3bb3dbbd
DK
1737 cm->status_save_ext_pwr_inserted = is_ext_pwr_online(cm);
1738 cm->status_save_batt = is_batt_present(cm);
1739
1740 if (!cm_rtc_set) {
1741 cm_suspend_duration_ms = 0;
1742 cm_rtc_set = cm_setup_timer();
1743 }
1744
1745 return 0;
1746}
1747
1748static void cm_suspend_complete(struct device *dev)
1749{
bb2a95c2 1750 struct charger_manager *cm = dev_get_drvdata(dev);
3bb3dbbd
DK
1751
1752 if (cm_suspended) {
1753 if (rtc_dev) {
1754 struct rtc_wkalrm tmp;
1755
1756 rtc_read_alarm(rtc_dev, &tmp);
1757 rtc_wkalarm_save.pending = tmp.pending;
1758 rtc_set_alarm(rtc_dev, &rtc_wkalarm_save);
1759 }
1760 cm_suspended = false;
1761 cm_rtc_set = false;
1762 }
1763
d829dc75
CC
1764 /* Re-enqueue delayed work (fullbatt_vchk_work) */
1765 if (cm->fullbatt_vchk_jiffies_at) {
1766 unsigned long delay = 0;
1767 unsigned long now = jiffies + CM_JIFFIES_SMALL;
1768
1769 if (time_after_eq(now, cm->fullbatt_vchk_jiffies_at)) {
1770 delay = (unsigned long)((long)now
1771 - (long)(cm->fullbatt_vchk_jiffies_at));
1772 delay = jiffies_to_msecs(delay);
1773 } else {
1774 delay = 0;
1775 }
1776
1777 /*
1778 * Account for cm_suspend_duration_ms if
1779 * assume_timer_stops_in_suspend is active
1780 */
1781 if (g_desc && g_desc->assume_timer_stops_in_suspend) {
1782 if (delay > cm_suspend_duration_ms)
1783 delay -= cm_suspend_duration_ms;
1784 else
1785 delay = 0;
1786 }
1787
1788 queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work,
1789 msecs_to_jiffies(delay));
1790 }
dfeccb12 1791 device_set_wakeup_capable(cm->dev, false);
3bb3dbbd
DK
1792 uevent_notify(cm, NULL);
1793}
1794
1795static const struct dev_pm_ops charger_manager_pm = {
1796 .prepare = cm_suspend_prepare,
dfeccb12 1797 .suspend_noirq = cm_suspend_noirq,
3bb3dbbd
DK
1798 .complete = cm_suspend_complete,
1799};
1800
1801static struct platform_driver charger_manager_driver = {
1802 .driver = {
1803 .name = "charger-manager",
1804 .owner = THIS_MODULE,
1805 .pm = &charger_manager_pm,
1806 },
1807 .probe = charger_manager_probe,
28ea73f4 1808 .remove = charger_manager_remove,
3bb3dbbd
DK
1809 .id_table = charger_manager_id,
1810};
1811
1812static int __init charger_manager_init(void)
1813{
d829dc75
CC
1814 cm_wq = create_freezable_workqueue("charger_manager");
1815 INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller);
1816
3bb3dbbd
DK
1817 return platform_driver_register(&charger_manager_driver);
1818}
1819late_initcall(charger_manager_init);
1820
1821static void __exit charger_manager_cleanup(void)
1822{
d829dc75
CC
1823 destroy_workqueue(cm_wq);
1824 cm_wq = NULL;
1825
3bb3dbbd
DK
1826 platform_driver_unregister(&charger_manager_driver);
1827}
1828module_exit(charger_manager_cleanup);
1829
dfeccb12
CC
1830/**
1831 * find_power_supply - find the associated power_supply of charger
1832 * @cm: the Charger Manager representing the battery
1833 * @psy: pointer to instance of charger's power_supply
1834 */
1835static bool find_power_supply(struct charger_manager *cm,
1836 struct power_supply *psy)
1837{
1838 int i;
1839 bool found = false;
1840
1841 for (i = 0; cm->charger_stat[i]; i++) {
1842 if (psy == cm->charger_stat[i]) {
1843 found = true;
1844 break;
1845 }
1846 }
1847
1848 return found;
1849}
1850
1851/**
1852 * cm_notify_event - charger driver notify Charger Manager of charger event
1853 * @psy: pointer to instance of charger's power_supply
1854 * @type: type of charger event
1855 * @msg: optional message passed to uevent_notify fuction
1856 */
1857void cm_notify_event(struct power_supply *psy, enum cm_event_types type,
1858 char *msg)
1859{
1860 struct charger_manager *cm;
1861 bool found_power_supply = false;
1862
1863 if (psy == NULL)
1864 return;
1865
1866 mutex_lock(&cm_list_mtx);
1867 list_for_each_entry(cm, &cm_list, entry) {
1868 found_power_supply = find_power_supply(cm, psy);
1869 if (found_power_supply)
1870 break;
1871 }
1872 mutex_unlock(&cm_list_mtx);
1873
1874 if (!found_power_supply)
1875 return;
1876
1877 switch (type) {
1878 case CM_EVENT_BATT_FULL:
1879 fullbatt_handler(cm);
1880 break;
1881 case CM_EVENT_BATT_OUT:
1882 battout_handler(cm);
1883 break;
1884 case CM_EVENT_BATT_IN:
1885 case CM_EVENT_EXT_PWR_IN_OUT ... CM_EVENT_CHG_START_STOP:
1886 misc_event_handler(cm, type);
1887 break;
1888 case CM_EVENT_UNKNOWN:
1889 case CM_EVENT_OTHERS:
1890 uevent_notify(cm, msg ? msg : default_event_names[type]);
1891 break;
1892 default:
1893 dev_err(cm->dev, "%s type not specified.\n", __func__);
1894 break;
1895 }
1896}
1897EXPORT_SYMBOL_GPL(cm_notify_event);
1898
3bb3dbbd
DK
1899MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1900MODULE_DESCRIPTION("Charger Manager");
1901MODULE_LICENSE("GPL");
This page took 0.143851 seconds and 5 git commands to generate.