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