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