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