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