Commit | Line | Data |
---|---|---|
1668f811 AM |
1 | /* |
2 | * Copyright (C) ST-Ericsson SA 2012 | |
3 | * | |
4 | * Charging algorithm driver for abx500 variants | |
5 | * | |
6 | * License Terms: GNU General Public License v2 | |
7 | * Authors: | |
8 | * Johan Palsson <johan.palsson@stericsson.com> | |
9 | * Karl Komierowski <karl.komierowski@stericsson.com> | |
10 | * Arun R Murthy <arun.murthy@stericsson.com> | |
11 | */ | |
12 | ||
13 | #include <linux/init.h> | |
14 | #include <linux/module.h> | |
15 | #include <linux/device.h> | |
16 | #include <linux/interrupt.h> | |
17 | #include <linux/delay.h> | |
18 | #include <linux/slab.h> | |
19 | #include <linux/platform_device.h> | |
20 | #include <linux/power_supply.h> | |
21 | #include <linux/completion.h> | |
22 | #include <linux/workqueue.h> | |
23 | #include <linux/kobject.h> | |
a12810ab R |
24 | #include <linux/of.h> |
25 | #include <linux/mfd/core.h> | |
1668f811 AM |
26 | #include <linux/mfd/abx500.h> |
27 | #include <linux/mfd/abx500/ux500_chargalg.h> | |
28 | #include <linux/mfd/abx500/ab8500-bm.h> | |
29 | ||
30 | /* Watchdog kick interval */ | |
31 | #define CHG_WD_INTERVAL (6 * HZ) | |
32 | ||
33 | /* End-of-charge criteria counter */ | |
34 | #define EOC_COND_CNT 10 | |
35 | ||
1668f811 AM |
36 | #define to_abx500_chargalg_device_info(x) container_of((x), \ |
37 | struct abx500_chargalg, chargalg_psy); | |
38 | ||
39 | enum abx500_chargers { | |
40 | NO_CHG, | |
41 | AC_CHG, | |
42 | USB_CHG, | |
43 | }; | |
44 | ||
45 | struct abx500_chargalg_charger_info { | |
46 | enum abx500_chargers conn_chg; | |
47 | enum abx500_chargers prev_conn_chg; | |
48 | enum abx500_chargers online_chg; | |
49 | enum abx500_chargers prev_online_chg; | |
50 | enum abx500_chargers charger_type; | |
51 | bool usb_chg_ok; | |
52 | bool ac_chg_ok; | |
53 | int usb_volt; | |
54 | int usb_curr; | |
55 | int ac_volt; | |
56 | int ac_curr; | |
57 | int usb_vset; | |
58 | int usb_iset; | |
59 | int ac_vset; | |
60 | int ac_iset; | |
61 | }; | |
62 | ||
63 | struct abx500_chargalg_suspension_status { | |
64 | bool suspended_change; | |
65 | bool ac_suspended; | |
66 | bool usb_suspended; | |
67 | }; | |
68 | ||
69 | struct abx500_chargalg_battery_data { | |
70 | int temp; | |
71 | int volt; | |
72 | int avg_curr; | |
73 | int inst_curr; | |
74 | int percent; | |
75 | }; | |
76 | ||
77 | enum abx500_chargalg_states { | |
78 | STATE_HANDHELD_INIT, | |
79 | STATE_HANDHELD, | |
80 | STATE_CHG_NOT_OK_INIT, | |
81 | STATE_CHG_NOT_OK, | |
82 | STATE_HW_TEMP_PROTECT_INIT, | |
83 | STATE_HW_TEMP_PROTECT, | |
84 | STATE_NORMAL_INIT, | |
85 | STATE_NORMAL, | |
86 | STATE_WAIT_FOR_RECHARGE_INIT, | |
87 | STATE_WAIT_FOR_RECHARGE, | |
88 | STATE_MAINTENANCE_A_INIT, | |
89 | STATE_MAINTENANCE_A, | |
90 | STATE_MAINTENANCE_B_INIT, | |
91 | STATE_MAINTENANCE_B, | |
92 | STATE_TEMP_UNDEROVER_INIT, | |
93 | STATE_TEMP_UNDEROVER, | |
94 | STATE_TEMP_LOWHIGH_INIT, | |
95 | STATE_TEMP_LOWHIGH, | |
96 | STATE_SUSPENDED_INIT, | |
97 | STATE_SUSPENDED, | |
98 | STATE_OVV_PROTECT_INIT, | |
99 | STATE_OVV_PROTECT, | |
100 | STATE_SAFETY_TIMER_EXPIRED_INIT, | |
101 | STATE_SAFETY_TIMER_EXPIRED, | |
102 | STATE_BATT_REMOVED_INIT, | |
103 | STATE_BATT_REMOVED, | |
104 | STATE_WD_EXPIRED_INIT, | |
105 | STATE_WD_EXPIRED, | |
106 | }; | |
107 | ||
108 | static const char *states[] = { | |
109 | "HANDHELD_INIT", | |
110 | "HANDHELD", | |
111 | "CHG_NOT_OK_INIT", | |
112 | "CHG_NOT_OK", | |
113 | "HW_TEMP_PROTECT_INIT", | |
114 | "HW_TEMP_PROTECT", | |
115 | "NORMAL_INIT", | |
116 | "NORMAL", | |
117 | "WAIT_FOR_RECHARGE_INIT", | |
118 | "WAIT_FOR_RECHARGE", | |
119 | "MAINTENANCE_A_INIT", | |
120 | "MAINTENANCE_A", | |
121 | "MAINTENANCE_B_INIT", | |
122 | "MAINTENANCE_B", | |
123 | "TEMP_UNDEROVER_INIT", | |
124 | "TEMP_UNDEROVER", | |
125 | "TEMP_LOWHIGH_INIT", | |
126 | "TEMP_LOWHIGH", | |
127 | "SUSPENDED_INIT", | |
128 | "SUSPENDED", | |
129 | "OVV_PROTECT_INIT", | |
130 | "OVV_PROTECT", | |
131 | "SAFETY_TIMER_EXPIRED_INIT", | |
132 | "SAFETY_TIMER_EXPIRED", | |
133 | "BATT_REMOVED_INIT", | |
134 | "BATT_REMOVED", | |
135 | "WD_EXPIRED_INIT", | |
136 | "WD_EXPIRED", | |
137 | }; | |
138 | ||
139 | struct abx500_chargalg_events { | |
140 | bool batt_unknown; | |
141 | bool mainextchnotok; | |
142 | bool batt_ovv; | |
143 | bool batt_rem; | |
144 | bool btemp_underover; | |
145 | bool btemp_lowhigh; | |
146 | bool main_thermal_prot; | |
147 | bool usb_thermal_prot; | |
148 | bool main_ovv; | |
149 | bool vbus_ovv; | |
150 | bool usbchargernotok; | |
151 | bool safety_timer_expired; | |
152 | bool maintenance_timer_expired; | |
153 | bool ac_wd_expired; | |
154 | bool usb_wd_expired; | |
155 | bool ac_cv_active; | |
156 | bool usb_cv_active; | |
157 | bool vbus_collapsed; | |
158 | }; | |
159 | ||
160 | /** | |
161 | * struct abx500_charge_curr_maximization - Charger maximization parameters | |
162 | * @original_iset: the non optimized/maximised charger current | |
163 | * @current_iset: the charging current used at this moment | |
164 | * @test_delta_i: the delta between the current we want to charge and the | |
165 | current that is really going into the battery | |
166 | * @condition_cnt: number of iterations needed before a new charger current | |
167 | is set | |
168 | * @max_current: maximum charger current | |
169 | * @wait_cnt: to avoid too fast current step down in case of charger | |
170 | * voltage collapse, we insert this delay between step | |
171 | * down | |
172 | * @level: tells in how many steps the charging current has been | |
173 | increased | |
174 | */ | |
175 | struct abx500_charge_curr_maximization { | |
176 | int original_iset; | |
177 | int current_iset; | |
178 | int test_delta_i; | |
179 | int condition_cnt; | |
180 | int max_current; | |
181 | int wait_cnt; | |
182 | u8 level; | |
183 | }; | |
184 | ||
185 | enum maxim_ret { | |
186 | MAXIM_RET_NOACTION, | |
187 | MAXIM_RET_CHANGE, | |
188 | MAXIM_RET_IBAT_TOO_HIGH, | |
189 | }; | |
190 | ||
191 | /** | |
192 | * struct abx500_chargalg - abx500 Charging algorithm device information | |
193 | * @dev: pointer to the structure device | |
194 | * @charge_status: battery operating status | |
195 | * @eoc_cnt: counter used to determine end-of_charge | |
1668f811 AM |
196 | * @maintenance_chg: indicate if maintenance charge is active |
197 | * @t_hyst_norm temperature hysteresis when the temperature has been | |
198 | * over or under normal limits | |
199 | * @t_hyst_lowhigh temperature hysteresis when the temperature has been | |
200 | * over or under the high or low limits | |
201 | * @charge_state: current state of the charging algorithm | |
202 | * @ccm charging current maximization parameters | |
203 | * @chg_info: information about connected charger types | |
204 | * @batt_data: data of the battery | |
205 | * @susp_status: current charger suspension status | |
b0284de0 | 206 | * @bm: Platform specific battery management information |
1668f811 AM |
207 | * @chargalg_psy: structure that holds the battery properties exposed by |
208 | * the charging algorithm | |
209 | * @events: structure for information about events triggered | |
210 | * @chargalg_wq: work queue for running the charging algorithm | |
211 | * @chargalg_periodic_work: work to run the charging algorithm periodically | |
212 | * @chargalg_wd_work: work to kick the charger watchdog periodically | |
213 | * @chargalg_work: work to run the charging algorithm instantly | |
214 | * @safety_timer: charging safety timer | |
215 | * @maintenance_timer: maintenance charging timer | |
216 | * @chargalg_kobject: structure of type kobject | |
217 | */ | |
218 | struct abx500_chargalg { | |
219 | struct device *dev; | |
220 | int charge_status; | |
221 | int eoc_cnt; | |
1668f811 AM |
222 | bool maintenance_chg; |
223 | int t_hyst_norm; | |
224 | int t_hyst_lowhigh; | |
225 | enum abx500_chargalg_states charge_state; | |
226 | struct abx500_charge_curr_maximization ccm; | |
227 | struct abx500_chargalg_charger_info chg_info; | |
228 | struct abx500_chargalg_battery_data batt_data; | |
229 | struct abx500_chargalg_suspension_status susp_status; | |
b0284de0 | 230 | struct abx500_bm_data *bm; |
1668f811 AM |
231 | struct power_supply chargalg_psy; |
232 | struct ux500_charger *ac_chg; | |
233 | struct ux500_charger *usb_chg; | |
234 | struct abx500_chargalg_events events; | |
235 | struct workqueue_struct *chargalg_wq; | |
236 | struct delayed_work chargalg_periodic_work; | |
237 | struct delayed_work chargalg_wd_work; | |
238 | struct work_struct chargalg_work; | |
239 | struct timer_list safety_timer; | |
240 | struct timer_list maintenance_timer; | |
241 | struct kobject chargalg_kobject; | |
242 | }; | |
243 | ||
244 | /* Main battery properties */ | |
245 | static enum power_supply_property abx500_chargalg_props[] = { | |
246 | POWER_SUPPLY_PROP_STATUS, | |
247 | POWER_SUPPLY_PROP_HEALTH, | |
248 | }; | |
249 | ||
250 | /** | |
251 | * abx500_chargalg_safety_timer_expired() - Expiration of the safety timer | |
252 | * @data: pointer to the abx500_chargalg structure | |
253 | * | |
254 | * This function gets called when the safety timer for the charger | |
255 | * expires | |
256 | */ | |
257 | static void abx500_chargalg_safety_timer_expired(unsigned long data) | |
258 | { | |
259 | struct abx500_chargalg *di = (struct abx500_chargalg *) data; | |
260 | dev_err(di->dev, "Safety timer expired\n"); | |
261 | di->events.safety_timer_expired = true; | |
262 | ||
263 | /* Trigger execution of the algorithm instantly */ | |
264 | queue_work(di->chargalg_wq, &di->chargalg_work); | |
265 | } | |
266 | ||
267 | /** | |
268 | * abx500_chargalg_maintenance_timer_expired() - Expiration of | |
269 | * the maintenance timer | |
270 | * @i: pointer to the abx500_chargalg structure | |
271 | * | |
272 | * This function gets called when the maintenence timer | |
273 | * expires | |
274 | */ | |
275 | static void abx500_chargalg_maintenance_timer_expired(unsigned long data) | |
276 | { | |
277 | ||
278 | struct abx500_chargalg *di = (struct abx500_chargalg *) data; | |
279 | dev_dbg(di->dev, "Maintenance timer expired\n"); | |
280 | di->events.maintenance_timer_expired = true; | |
281 | ||
282 | /* Trigger execution of the algorithm instantly */ | |
283 | queue_work(di->chargalg_wq, &di->chargalg_work); | |
284 | } | |
285 | ||
286 | /** | |
287 | * abx500_chargalg_state_to() - Change charge state | |
288 | * @di: pointer to the abx500_chargalg structure | |
289 | * | |
290 | * This function gets called when a charge state change should occur | |
291 | */ | |
292 | static void abx500_chargalg_state_to(struct abx500_chargalg *di, | |
293 | enum abx500_chargalg_states state) | |
294 | { | |
295 | dev_dbg(di->dev, | |
296 | "State changed: %s (From state: [%d] %s =to=> [%d] %s )\n", | |
297 | di->charge_state == state ? "NO" : "YES", | |
298 | di->charge_state, | |
299 | states[di->charge_state], | |
300 | state, | |
301 | states[state]); | |
302 | ||
303 | di->charge_state = state; | |
304 | } | |
305 | ||
306 | /** | |
307 | * abx500_chargalg_check_charger_connection() - Check charger connection change | |
308 | * @di: pointer to the abx500_chargalg structure | |
309 | * | |
310 | * This function will check if there is a change in the charger connection | |
311 | * and change charge state accordingly. AC has precedence over USB. | |
312 | */ | |
313 | static int abx500_chargalg_check_charger_connection(struct abx500_chargalg *di) | |
314 | { | |
315 | if (di->chg_info.conn_chg != di->chg_info.prev_conn_chg || | |
316 | di->susp_status.suspended_change) { | |
317 | /* | |
318 | * Charger state changed or suspension | |
319 | * has changed since last update | |
320 | */ | |
321 | if ((di->chg_info.conn_chg & AC_CHG) && | |
322 | !di->susp_status.ac_suspended) { | |
323 | dev_dbg(di->dev, "Charging source is AC\n"); | |
324 | if (di->chg_info.charger_type != AC_CHG) { | |
325 | di->chg_info.charger_type = AC_CHG; | |
326 | abx500_chargalg_state_to(di, STATE_NORMAL_INIT); | |
327 | } | |
328 | } else if ((di->chg_info.conn_chg & USB_CHG) && | |
329 | !di->susp_status.usb_suspended) { | |
330 | dev_dbg(di->dev, "Charging source is USB\n"); | |
331 | di->chg_info.charger_type = USB_CHG; | |
332 | abx500_chargalg_state_to(di, STATE_NORMAL_INIT); | |
333 | } else if (di->chg_info.conn_chg && | |
334 | (di->susp_status.ac_suspended || | |
335 | di->susp_status.usb_suspended)) { | |
336 | dev_dbg(di->dev, "Charging is suspended\n"); | |
337 | di->chg_info.charger_type = NO_CHG; | |
338 | abx500_chargalg_state_to(di, STATE_SUSPENDED_INIT); | |
339 | } else { | |
340 | dev_dbg(di->dev, "Charging source is OFF\n"); | |
341 | di->chg_info.charger_type = NO_CHG; | |
342 | abx500_chargalg_state_to(di, STATE_HANDHELD_INIT); | |
343 | } | |
344 | di->chg_info.prev_conn_chg = di->chg_info.conn_chg; | |
345 | di->susp_status.suspended_change = false; | |
346 | } | |
347 | return di->chg_info.conn_chg; | |
348 | } | |
349 | ||
350 | /** | |
351 | * abx500_chargalg_start_safety_timer() - Start charging safety timer | |
352 | * @di: pointer to the abx500_chargalg structure | |
353 | * | |
354 | * The safety timer is used to avoid overcharging of old or bad batteries. | |
355 | * There are different timers for AC and USB | |
356 | */ | |
357 | static void abx500_chargalg_start_safety_timer(struct abx500_chargalg *di) | |
358 | { | |
359 | unsigned long timer_expiration = 0; | |
360 | ||
361 | switch (di->chg_info.charger_type) { | |
362 | case AC_CHG: | |
363 | timer_expiration = | |
364 | round_jiffies(jiffies + | |
b0284de0 | 365 | (di->bm->main_safety_tmr_h * 3600 * HZ)); |
1668f811 AM |
366 | break; |
367 | ||
368 | case USB_CHG: | |
369 | timer_expiration = | |
370 | round_jiffies(jiffies + | |
b0284de0 | 371 | (di->bm->usb_safety_tmr_h * 3600 * HZ)); |
1668f811 AM |
372 | break; |
373 | ||
374 | default: | |
375 | dev_err(di->dev, "Unknown charger to charge from\n"); | |
376 | break; | |
377 | } | |
378 | ||
379 | di->events.safety_timer_expired = false; | |
380 | di->safety_timer.expires = timer_expiration; | |
381 | if (!timer_pending(&di->safety_timer)) | |
382 | add_timer(&di->safety_timer); | |
383 | else | |
384 | mod_timer(&di->safety_timer, timer_expiration); | |
385 | } | |
386 | ||
387 | /** | |
388 | * abx500_chargalg_stop_safety_timer() - Stop charging safety timer | |
389 | * @di: pointer to the abx500_chargalg structure | |
390 | * | |
391 | * The safety timer is stopped whenever the NORMAL state is exited | |
392 | */ | |
393 | static void abx500_chargalg_stop_safety_timer(struct abx500_chargalg *di) | |
394 | { | |
395 | di->events.safety_timer_expired = false; | |
396 | del_timer(&di->safety_timer); | |
397 | } | |
398 | ||
399 | /** | |
400 | * abx500_chargalg_start_maintenance_timer() - Start charging maintenance timer | |
401 | * @di: pointer to the abx500_chargalg structure | |
402 | * @duration: duration of ther maintenance timer in hours | |
403 | * | |
404 | * The maintenance timer is used to maintain the charge in the battery once | |
405 | * the battery is considered full. These timers are chosen to match the | |
406 | * discharge curve of the battery | |
407 | */ | |
408 | static void abx500_chargalg_start_maintenance_timer(struct abx500_chargalg *di, | |
409 | int duration) | |
410 | { | |
411 | unsigned long timer_expiration; | |
412 | ||
413 | /* Convert from hours to jiffies */ | |
414 | timer_expiration = round_jiffies(jiffies + (duration * 3600 * HZ)); | |
415 | ||
416 | di->events.maintenance_timer_expired = false; | |
417 | di->maintenance_timer.expires = timer_expiration; | |
418 | if (!timer_pending(&di->maintenance_timer)) | |
419 | add_timer(&di->maintenance_timer); | |
420 | else | |
421 | mod_timer(&di->maintenance_timer, timer_expiration); | |
422 | } | |
423 | ||
424 | /** | |
425 | * abx500_chargalg_stop_maintenance_timer() - Stop maintenance timer | |
426 | * @di: pointer to the abx500_chargalg structure | |
427 | * | |
428 | * The maintenance timer is stopped whenever maintenance ends or when another | |
429 | * state is entered | |
430 | */ | |
431 | static void abx500_chargalg_stop_maintenance_timer(struct abx500_chargalg *di) | |
432 | { | |
433 | di->events.maintenance_timer_expired = false; | |
434 | del_timer(&di->maintenance_timer); | |
435 | } | |
436 | ||
437 | /** | |
438 | * abx500_chargalg_kick_watchdog() - Kick charger watchdog | |
439 | * @di: pointer to the abx500_chargalg structure | |
440 | * | |
441 | * The charger watchdog have to be kicked periodically whenever the charger is | |
442 | * on, else the ABB will reset the system | |
443 | */ | |
444 | static int abx500_chargalg_kick_watchdog(struct abx500_chargalg *di) | |
445 | { | |
446 | /* Check if charger exists and kick watchdog if charging */ | |
447 | if (di->ac_chg && di->ac_chg->ops.kick_wd && | |
448 | di->chg_info.online_chg & AC_CHG) | |
449 | return di->ac_chg->ops.kick_wd(di->ac_chg); | |
450 | else if (di->usb_chg && di->usb_chg->ops.kick_wd && | |
451 | di->chg_info.online_chg & USB_CHG) | |
452 | return di->usb_chg->ops.kick_wd(di->usb_chg); | |
453 | ||
454 | return -ENXIO; | |
455 | } | |
456 | ||
457 | /** | |
458 | * abx500_chargalg_ac_en() - Turn on/off the AC charger | |
459 | * @di: pointer to the abx500_chargalg structure | |
460 | * @enable: charger on/off | |
461 | * @vset: requested charger output voltage | |
462 | * @iset: requested charger output current | |
463 | * | |
464 | * The AC charger will be turned on/off with the requested charge voltage and | |
465 | * current | |
466 | */ | |
467 | static int abx500_chargalg_ac_en(struct abx500_chargalg *di, int enable, | |
468 | int vset, int iset) | |
469 | { | |
470 | if (!di->ac_chg || !di->ac_chg->ops.enable) | |
471 | return -ENXIO; | |
472 | ||
473 | /* Select maximum of what both the charger and the battery supports */ | |
474 | if (di->ac_chg->max_out_volt) | |
475 | vset = min(vset, di->ac_chg->max_out_volt); | |
476 | if (di->ac_chg->max_out_curr) | |
477 | iset = min(iset, di->ac_chg->max_out_curr); | |
478 | ||
479 | di->chg_info.ac_iset = iset; | |
480 | di->chg_info.ac_vset = vset; | |
481 | ||
482 | return di->ac_chg->ops.enable(di->ac_chg, enable, vset, iset); | |
483 | } | |
484 | ||
485 | /** | |
486 | * abx500_chargalg_usb_en() - Turn on/off the USB charger | |
487 | * @di: pointer to the abx500_chargalg structure | |
488 | * @enable: charger on/off | |
489 | * @vset: requested charger output voltage | |
490 | * @iset: requested charger output current | |
491 | * | |
492 | * The USB charger will be turned on/off with the requested charge voltage and | |
493 | * current | |
494 | */ | |
495 | static int abx500_chargalg_usb_en(struct abx500_chargalg *di, int enable, | |
496 | int vset, int iset) | |
497 | { | |
498 | if (!di->usb_chg || !di->usb_chg->ops.enable) | |
499 | return -ENXIO; | |
500 | ||
501 | /* Select maximum of what both the charger and the battery supports */ | |
502 | if (di->usb_chg->max_out_volt) | |
503 | vset = min(vset, di->usb_chg->max_out_volt); | |
504 | if (di->usb_chg->max_out_curr) | |
505 | iset = min(iset, di->usb_chg->max_out_curr); | |
506 | ||
507 | di->chg_info.usb_iset = iset; | |
508 | di->chg_info.usb_vset = vset; | |
509 | ||
510 | return di->usb_chg->ops.enable(di->usb_chg, enable, vset, iset); | |
511 | } | |
512 | ||
513 | /** | |
514 | * abx500_chargalg_update_chg_curr() - Update charger current | |
515 | * @di: pointer to the abx500_chargalg structure | |
516 | * @iset: requested charger output current | |
517 | * | |
518 | * The charger output current will be updated for the charger | |
519 | * that is currently in use | |
520 | */ | |
521 | static int abx500_chargalg_update_chg_curr(struct abx500_chargalg *di, | |
522 | int iset) | |
523 | { | |
524 | /* Check if charger exists and update current if charging */ | |
525 | if (di->ac_chg && di->ac_chg->ops.update_curr && | |
526 | di->chg_info.charger_type & AC_CHG) { | |
527 | /* | |
528 | * Select maximum of what both the charger | |
529 | * and the battery supports | |
530 | */ | |
531 | if (di->ac_chg->max_out_curr) | |
532 | iset = min(iset, di->ac_chg->max_out_curr); | |
533 | ||
534 | di->chg_info.ac_iset = iset; | |
535 | ||
536 | return di->ac_chg->ops.update_curr(di->ac_chg, iset); | |
537 | } else if (di->usb_chg && di->usb_chg->ops.update_curr && | |
538 | di->chg_info.charger_type & USB_CHG) { | |
539 | /* | |
540 | * Select maximum of what both the charger | |
541 | * and the battery supports | |
542 | */ | |
543 | if (di->usb_chg->max_out_curr) | |
544 | iset = min(iset, di->usb_chg->max_out_curr); | |
545 | ||
546 | di->chg_info.usb_iset = iset; | |
547 | ||
548 | return di->usb_chg->ops.update_curr(di->usb_chg, iset); | |
549 | } | |
550 | ||
551 | return -ENXIO; | |
552 | } | |
553 | ||
554 | /** | |
555 | * abx500_chargalg_stop_charging() - Stop charging | |
556 | * @di: pointer to the abx500_chargalg structure | |
557 | * | |
558 | * This function is called from any state where charging should be stopped. | |
559 | * All charging is disabled and all status parameters and timers are changed | |
560 | * accordingly | |
561 | */ | |
562 | static void abx500_chargalg_stop_charging(struct abx500_chargalg *di) | |
563 | { | |
564 | abx500_chargalg_ac_en(di, false, 0, 0); | |
565 | abx500_chargalg_usb_en(di, false, 0, 0); | |
566 | abx500_chargalg_stop_safety_timer(di); | |
567 | abx500_chargalg_stop_maintenance_timer(di); | |
568 | di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING; | |
569 | di->maintenance_chg = false; | |
570 | cancel_delayed_work(&di->chargalg_wd_work); | |
571 | power_supply_changed(&di->chargalg_psy); | |
572 | } | |
573 | ||
574 | /** | |
575 | * abx500_chargalg_hold_charging() - Pauses charging | |
576 | * @di: pointer to the abx500_chargalg structure | |
577 | * | |
578 | * This function is called in the case where maintenance charging has been | |
579 | * disabled and instead a battery voltage mode is entered to check when the | |
580 | * battery voltage has reached a certain recharge voltage | |
581 | */ | |
582 | static void abx500_chargalg_hold_charging(struct abx500_chargalg *di) | |
583 | { | |
584 | abx500_chargalg_ac_en(di, false, 0, 0); | |
585 | abx500_chargalg_usb_en(di, false, 0, 0); | |
586 | abx500_chargalg_stop_safety_timer(di); | |
587 | abx500_chargalg_stop_maintenance_timer(di); | |
588 | di->charge_status = POWER_SUPPLY_STATUS_CHARGING; | |
589 | di->maintenance_chg = false; | |
590 | cancel_delayed_work(&di->chargalg_wd_work); | |
591 | power_supply_changed(&di->chargalg_psy); | |
592 | } | |
593 | ||
594 | /** | |
595 | * abx500_chargalg_start_charging() - Start the charger | |
596 | * @di: pointer to the abx500_chargalg structure | |
597 | * @vset: requested charger output voltage | |
598 | * @iset: requested charger output current | |
599 | * | |
600 | * A charger will be enabled depending on the requested charger type that was | |
601 | * detected previously. | |
602 | */ | |
603 | static void abx500_chargalg_start_charging(struct abx500_chargalg *di, | |
604 | int vset, int iset) | |
605 | { | |
3edebfcd LJ |
606 | bool start_chargalg_wd = true; |
607 | ||
1668f811 AM |
608 | switch (di->chg_info.charger_type) { |
609 | case AC_CHG: | |
610 | dev_dbg(di->dev, | |
611 | "AC parameters: Vset %d, Ich %d\n", vset, iset); | |
612 | abx500_chargalg_usb_en(di, false, 0, 0); | |
613 | abx500_chargalg_ac_en(di, true, vset, iset); | |
614 | break; | |
615 | ||
616 | case USB_CHG: | |
617 | dev_dbg(di->dev, | |
618 | "USB parameters: Vset %d, Ich %d\n", vset, iset); | |
619 | abx500_chargalg_ac_en(di, false, 0, 0); | |
620 | abx500_chargalg_usb_en(di, true, vset, iset); | |
621 | break; | |
622 | ||
623 | default: | |
624 | dev_err(di->dev, "Unknown charger to charge from\n"); | |
3edebfcd | 625 | start_chargalg_wd = false; |
1668f811 AM |
626 | break; |
627 | } | |
3edebfcd LJ |
628 | |
629 | if (start_chargalg_wd && !delayed_work_pending(&di->chargalg_wd_work)) | |
630 | queue_delayed_work(di->chargalg_wq, &di->chargalg_wd_work, 0); | |
1668f811 AM |
631 | } |
632 | ||
633 | /** | |
634 | * abx500_chargalg_check_temp() - Check battery temperature ranges | |
635 | * @di: pointer to the abx500_chargalg structure | |
636 | * | |
637 | * The battery temperature is checked against the predefined limits and the | |
638 | * charge state is changed accordingly | |
639 | */ | |
640 | static void abx500_chargalg_check_temp(struct abx500_chargalg *di) | |
641 | { | |
b0284de0 LJ |
642 | if (di->batt_data.temp > (di->bm->temp_low + di->t_hyst_norm) && |
643 | di->batt_data.temp < (di->bm->temp_high - di->t_hyst_norm)) { | |
1668f811 AM |
644 | /* Temp OK! */ |
645 | di->events.btemp_underover = false; | |
646 | di->events.btemp_lowhigh = false; | |
647 | di->t_hyst_norm = 0; | |
648 | di->t_hyst_lowhigh = 0; | |
649 | } else { | |
b0284de0 | 650 | if (((di->batt_data.temp >= di->bm->temp_high) && |
1668f811 | 651 | (di->batt_data.temp < |
b0284de0 | 652 | (di->bm->temp_over - di->t_hyst_lowhigh))) || |
1668f811 | 653 | ((di->batt_data.temp > |
b0284de0 LJ |
654 | (di->bm->temp_under + di->t_hyst_lowhigh)) && |
655 | (di->batt_data.temp <= di->bm->temp_low))) { | |
1668f811 AM |
656 | /* TEMP minor!!!!! */ |
657 | di->events.btemp_underover = false; | |
658 | di->events.btemp_lowhigh = true; | |
b0284de0 | 659 | di->t_hyst_norm = di->bm->temp_hysteresis; |
1668f811 | 660 | di->t_hyst_lowhigh = 0; |
b0284de0 LJ |
661 | } else if (di->batt_data.temp <= di->bm->temp_under || |
662 | di->batt_data.temp >= di->bm->temp_over) { | |
1668f811 AM |
663 | /* TEMP major!!!!! */ |
664 | di->events.btemp_underover = true; | |
665 | di->events.btemp_lowhigh = false; | |
666 | di->t_hyst_norm = 0; | |
b0284de0 | 667 | di->t_hyst_lowhigh = di->bm->temp_hysteresis; |
1668f811 AM |
668 | } else { |
669 | /* Within hysteresis */ | |
670 | dev_dbg(di->dev, "Within hysteresis limit temp: %d " | |
671 | "hyst_lowhigh %d, hyst normal %d\n", | |
672 | di->batt_data.temp, di->t_hyst_lowhigh, | |
673 | di->t_hyst_norm); | |
674 | } | |
675 | } | |
676 | } | |
677 | ||
678 | /** | |
679 | * abx500_chargalg_check_charger_voltage() - Check charger voltage | |
680 | * @di: pointer to the abx500_chargalg structure | |
681 | * | |
682 | * Charger voltage is checked against maximum limit | |
683 | */ | |
684 | static void abx500_chargalg_check_charger_voltage(struct abx500_chargalg *di) | |
685 | { | |
b0284de0 | 686 | if (di->chg_info.usb_volt > di->bm->chg_params->usb_volt_max) |
1668f811 AM |
687 | di->chg_info.usb_chg_ok = false; |
688 | else | |
689 | di->chg_info.usb_chg_ok = true; | |
690 | ||
b0284de0 | 691 | if (di->chg_info.ac_volt > di->bm->chg_params->ac_volt_max) |
1668f811 AM |
692 | di->chg_info.ac_chg_ok = false; |
693 | else | |
694 | di->chg_info.ac_chg_ok = true; | |
695 | ||
696 | } | |
697 | ||
698 | /** | |
699 | * abx500_chargalg_end_of_charge() - Check if end-of-charge criteria is fulfilled | |
700 | * @di: pointer to the abx500_chargalg structure | |
701 | * | |
702 | * End-of-charge criteria is fulfilled when the battery voltage is above a | |
703 | * certain limit and the battery current is below a certain limit for a | |
704 | * predefined number of consecutive seconds. If true, the battery is full | |
705 | */ | |
706 | static void abx500_chargalg_end_of_charge(struct abx500_chargalg *di) | |
707 | { | |
708 | if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING && | |
709 | di->charge_state == STATE_NORMAL && | |
710 | !di->maintenance_chg && (di->batt_data.volt >= | |
b0284de0 | 711 | di->bm->bat_type[di->bm->batt_id].termination_vol || |
1668f811 AM |
712 | di->events.usb_cv_active || di->events.ac_cv_active) && |
713 | di->batt_data.avg_curr < | |
b0284de0 | 714 | di->bm->bat_type[di->bm->batt_id].termination_curr && |
1668f811 AM |
715 | di->batt_data.avg_curr > 0) { |
716 | if (++di->eoc_cnt >= EOC_COND_CNT) { | |
717 | di->eoc_cnt = 0; | |
718 | di->charge_status = POWER_SUPPLY_STATUS_FULL; | |
719 | di->maintenance_chg = true; | |
720 | dev_dbg(di->dev, "EOC reached!\n"); | |
721 | power_supply_changed(&di->chargalg_psy); | |
722 | } else { | |
723 | dev_dbg(di->dev, | |
724 | " EOC limit reached for the %d" | |
725 | " time, out of %d before EOC\n", | |
726 | di->eoc_cnt, | |
727 | EOC_COND_CNT); | |
728 | } | |
729 | } else { | |
730 | di->eoc_cnt = 0; | |
731 | } | |
732 | } | |
733 | ||
734 | static void init_maxim_chg_curr(struct abx500_chargalg *di) | |
735 | { | |
736 | di->ccm.original_iset = | |
b0284de0 | 737 | di->bm->bat_type[di->bm->batt_id].normal_cur_lvl; |
1668f811 | 738 | di->ccm.current_iset = |
b0284de0 LJ |
739 | di->bm->bat_type[di->bm->batt_id].normal_cur_lvl; |
740 | di->ccm.test_delta_i = di->bm->maxi->charger_curr_step; | |
741 | di->ccm.max_current = di->bm->maxi->chg_curr; | |
742 | di->ccm.condition_cnt = di->bm->maxi->wait_cycles; | |
1668f811 AM |
743 | di->ccm.level = 0; |
744 | } | |
745 | ||
746 | /** | |
747 | * abx500_chargalg_chg_curr_maxim - increases the charger current to | |
748 | * compensate for the system load | |
749 | * @di pointer to the abx500_chargalg structure | |
750 | * | |
751 | * This maximization function is used to raise the charger current to get the | |
752 | * battery current as close to the optimal value as possible. The battery | |
753 | * current during charging is affected by the system load | |
754 | */ | |
755 | static enum maxim_ret abx500_chargalg_chg_curr_maxim(struct abx500_chargalg *di) | |
756 | { | |
757 | int delta_i; | |
758 | ||
b0284de0 | 759 | if (!di->bm->maxi->ena_maxi) |
1668f811 AM |
760 | return MAXIM_RET_NOACTION; |
761 | ||
762 | delta_i = di->ccm.original_iset - di->batt_data.inst_curr; | |
763 | ||
764 | if (di->events.vbus_collapsed) { | |
765 | dev_dbg(di->dev, "Charger voltage has collapsed %d\n", | |
766 | di->ccm.wait_cnt); | |
767 | if (di->ccm.wait_cnt == 0) { | |
768 | dev_dbg(di->dev, "lowering current\n"); | |
769 | di->ccm.wait_cnt++; | |
b0284de0 | 770 | di->ccm.condition_cnt = di->bm->maxi->wait_cycles; |
1668f811 AM |
771 | di->ccm.max_current = |
772 | di->ccm.current_iset - di->ccm.test_delta_i; | |
773 | di->ccm.current_iset = di->ccm.max_current; | |
774 | di->ccm.level--; | |
775 | return MAXIM_RET_CHANGE; | |
776 | } else { | |
777 | dev_dbg(di->dev, "waiting\n"); | |
778 | /* Let's go in here twice before lowering curr again */ | |
779 | di->ccm.wait_cnt = (di->ccm.wait_cnt + 1) % 3; | |
780 | return MAXIM_RET_NOACTION; | |
781 | } | |
782 | } | |
783 | ||
784 | di->ccm.wait_cnt = 0; | |
785 | ||
786 | if ((di->batt_data.inst_curr > di->ccm.original_iset)) { | |
787 | dev_dbg(di->dev, " Maximization Ibat (%dmA) too high" | |
788 | " (limit %dmA) (current iset: %dmA)!\n", | |
789 | di->batt_data.inst_curr, di->ccm.original_iset, | |
790 | di->ccm.current_iset); | |
791 | ||
792 | if (di->ccm.current_iset == di->ccm.original_iset) | |
793 | return MAXIM_RET_NOACTION; | |
794 | ||
b0284de0 | 795 | di->ccm.condition_cnt = di->bm->maxi->wait_cycles; |
1668f811 AM |
796 | di->ccm.current_iset = di->ccm.original_iset; |
797 | di->ccm.level = 0; | |
798 | ||
799 | return MAXIM_RET_IBAT_TOO_HIGH; | |
800 | } | |
801 | ||
802 | if (delta_i > di->ccm.test_delta_i && | |
803 | (di->ccm.current_iset + di->ccm.test_delta_i) < | |
804 | di->ccm.max_current) { | |
805 | if (di->ccm.condition_cnt-- == 0) { | |
806 | /* Increse the iset with cco.test_delta_i */ | |
b0284de0 | 807 | di->ccm.condition_cnt = di->bm->maxi->wait_cycles; |
1668f811 AM |
808 | di->ccm.current_iset += di->ccm.test_delta_i; |
809 | di->ccm.level++; | |
810 | dev_dbg(di->dev, " Maximization needed, increase" | |
811 | " with %d mA to %dmA (Optimal ibat: %d)" | |
812 | " Level %d\n", | |
813 | di->ccm.test_delta_i, | |
814 | di->ccm.current_iset, | |
815 | di->ccm.original_iset, | |
816 | di->ccm.level); | |
817 | return MAXIM_RET_CHANGE; | |
818 | } else { | |
819 | return MAXIM_RET_NOACTION; | |
820 | } | |
821 | } else { | |
b0284de0 | 822 | di->ccm.condition_cnt = di->bm->maxi->wait_cycles; |
1668f811 AM |
823 | return MAXIM_RET_NOACTION; |
824 | } | |
825 | } | |
826 | ||
827 | static void handle_maxim_chg_curr(struct abx500_chargalg *di) | |
828 | { | |
829 | enum maxim_ret ret; | |
830 | int result; | |
831 | ||
832 | ret = abx500_chargalg_chg_curr_maxim(di); | |
833 | switch (ret) { | |
834 | case MAXIM_RET_CHANGE: | |
835 | result = abx500_chargalg_update_chg_curr(di, | |
836 | di->ccm.current_iset); | |
837 | if (result) | |
838 | dev_err(di->dev, "failed to set chg curr\n"); | |
839 | break; | |
840 | case MAXIM_RET_IBAT_TOO_HIGH: | |
841 | result = abx500_chargalg_update_chg_curr(di, | |
b0284de0 | 842 | di->bm->bat_type[di->bm->batt_id].normal_cur_lvl); |
1668f811 AM |
843 | if (result) |
844 | dev_err(di->dev, "failed to set chg curr\n"); | |
845 | break; | |
846 | ||
847 | case MAXIM_RET_NOACTION: | |
848 | default: | |
849 | /* Do nothing..*/ | |
850 | break; | |
851 | } | |
852 | } | |
853 | ||
854 | static int abx500_chargalg_get_ext_psy_data(struct device *dev, void *data) | |
855 | { | |
856 | struct power_supply *psy; | |
857 | struct power_supply *ext; | |
858 | struct abx500_chargalg *di; | |
859 | union power_supply_propval ret; | |
860 | int i, j; | |
861 | bool psy_found = false; | |
ea402401 | 862 | bool capacity_updated = false; |
1668f811 AM |
863 | |
864 | psy = (struct power_supply *)data; | |
865 | ext = dev_get_drvdata(dev); | |
866 | di = to_abx500_chargalg_device_info(psy); | |
867 | /* For all psy where the driver name appears in any supplied_to */ | |
868 | for (i = 0; i < ext->num_supplicants; i++) { | |
869 | if (!strcmp(ext->supplied_to[i], psy->name)) | |
870 | psy_found = true; | |
871 | } | |
872 | if (!psy_found) | |
873 | return 0; | |
874 | ||
ea402401 MC |
875 | /* |
876 | * If external is not registering 'POWER_SUPPLY_PROP_CAPACITY' to its | |
877 | * property because of handling that sysfs entry on its own, this is | |
878 | * the place to get the battery capacity. | |
879 | */ | |
880 | if (!ext->get_property(ext, POWER_SUPPLY_PROP_CAPACITY, &ret)) { | |
881 | di->batt_data.percent = ret.intval; | |
882 | capacity_updated = true; | |
883 | } | |
884 | ||
1668f811 AM |
885 | /* Go through all properties for the psy */ |
886 | for (j = 0; j < ext->num_properties; j++) { | |
887 | enum power_supply_property prop; | |
888 | prop = ext->properties[j]; | |
889 | ||
890 | /* Initialize chargers if not already done */ | |
891 | if (!di->ac_chg && | |
892 | ext->type == POWER_SUPPLY_TYPE_MAINS) | |
893 | di->ac_chg = psy_to_ux500_charger(ext); | |
894 | else if (!di->usb_chg && | |
895 | ext->type == POWER_SUPPLY_TYPE_USB) | |
896 | di->usb_chg = psy_to_ux500_charger(ext); | |
897 | ||
898 | if (ext->get_property(ext, prop, &ret)) | |
899 | continue; | |
900 | switch (prop) { | |
901 | case POWER_SUPPLY_PROP_PRESENT: | |
902 | switch (ext->type) { | |
903 | case POWER_SUPPLY_TYPE_BATTERY: | |
904 | /* Battery present */ | |
905 | if (ret.intval) | |
906 | di->events.batt_rem = false; | |
907 | /* Battery removed */ | |
908 | else | |
909 | di->events.batt_rem = true; | |
910 | break; | |
911 | case POWER_SUPPLY_TYPE_MAINS: | |
912 | /* AC disconnected */ | |
913 | if (!ret.intval && | |
914 | (di->chg_info.conn_chg & AC_CHG)) { | |
915 | di->chg_info.prev_conn_chg = | |
916 | di->chg_info.conn_chg; | |
917 | di->chg_info.conn_chg &= ~AC_CHG; | |
918 | } | |
919 | /* AC connected */ | |
920 | else if (ret.intval && | |
921 | !(di->chg_info.conn_chg & AC_CHG)) { | |
922 | di->chg_info.prev_conn_chg = | |
923 | di->chg_info.conn_chg; | |
924 | di->chg_info.conn_chg |= AC_CHG; | |
925 | } | |
926 | break; | |
927 | case POWER_SUPPLY_TYPE_USB: | |
928 | /* USB disconnected */ | |
929 | if (!ret.intval && | |
930 | (di->chg_info.conn_chg & USB_CHG)) { | |
931 | di->chg_info.prev_conn_chg = | |
932 | di->chg_info.conn_chg; | |
933 | di->chg_info.conn_chg &= ~USB_CHG; | |
934 | } | |
935 | /* USB connected */ | |
936 | else if (ret.intval && | |
937 | !(di->chg_info.conn_chg & USB_CHG)) { | |
938 | di->chg_info.prev_conn_chg = | |
939 | di->chg_info.conn_chg; | |
940 | di->chg_info.conn_chg |= USB_CHG; | |
941 | } | |
942 | break; | |
943 | default: | |
944 | break; | |
945 | } | |
946 | break; | |
947 | ||
948 | case POWER_SUPPLY_PROP_ONLINE: | |
949 | switch (ext->type) { | |
950 | case POWER_SUPPLY_TYPE_BATTERY: | |
951 | break; | |
952 | case POWER_SUPPLY_TYPE_MAINS: | |
953 | /* AC offline */ | |
954 | if (!ret.intval && | |
955 | (di->chg_info.online_chg & AC_CHG)) { | |
956 | di->chg_info.prev_online_chg = | |
957 | di->chg_info.online_chg; | |
958 | di->chg_info.online_chg &= ~AC_CHG; | |
959 | } | |
960 | /* AC online */ | |
961 | else if (ret.intval && | |
962 | !(di->chg_info.online_chg & AC_CHG)) { | |
963 | di->chg_info.prev_online_chg = | |
964 | di->chg_info.online_chg; | |
965 | di->chg_info.online_chg |= AC_CHG; | |
966 | queue_delayed_work(di->chargalg_wq, | |
967 | &di->chargalg_wd_work, 0); | |
968 | } | |
969 | break; | |
970 | case POWER_SUPPLY_TYPE_USB: | |
971 | /* USB offline */ | |
972 | if (!ret.intval && | |
973 | (di->chg_info.online_chg & USB_CHG)) { | |
974 | di->chg_info.prev_online_chg = | |
975 | di->chg_info.online_chg; | |
976 | di->chg_info.online_chg &= ~USB_CHG; | |
977 | } | |
978 | /* USB online */ | |
979 | else if (ret.intval && | |
980 | !(di->chg_info.online_chg & USB_CHG)) { | |
981 | di->chg_info.prev_online_chg = | |
982 | di->chg_info.online_chg; | |
983 | di->chg_info.online_chg |= USB_CHG; | |
984 | queue_delayed_work(di->chargalg_wq, | |
985 | &di->chargalg_wd_work, 0); | |
986 | } | |
987 | break; | |
988 | default: | |
989 | break; | |
990 | } | |
991 | break; | |
992 | ||
993 | case POWER_SUPPLY_PROP_HEALTH: | |
994 | switch (ext->type) { | |
995 | case POWER_SUPPLY_TYPE_BATTERY: | |
996 | break; | |
997 | case POWER_SUPPLY_TYPE_MAINS: | |
998 | switch (ret.intval) { | |
999 | case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE: | |
1000 | di->events.mainextchnotok = true; | |
1001 | di->events.main_thermal_prot = false; | |
1002 | di->events.main_ovv = false; | |
1003 | di->events.ac_wd_expired = false; | |
1004 | break; | |
1005 | case POWER_SUPPLY_HEALTH_DEAD: | |
1006 | di->events.ac_wd_expired = true; | |
1007 | di->events.mainextchnotok = false; | |
1008 | di->events.main_ovv = false; | |
1009 | di->events.main_thermal_prot = false; | |
1010 | break; | |
1011 | case POWER_SUPPLY_HEALTH_COLD: | |
1012 | case POWER_SUPPLY_HEALTH_OVERHEAT: | |
1013 | di->events.main_thermal_prot = true; | |
1014 | di->events.mainextchnotok = false; | |
1015 | di->events.main_ovv = false; | |
1016 | di->events.ac_wd_expired = false; | |
1017 | break; | |
1018 | case POWER_SUPPLY_HEALTH_OVERVOLTAGE: | |
1019 | di->events.main_ovv = true; | |
1020 | di->events.mainextchnotok = false; | |
1021 | di->events.main_thermal_prot = false; | |
1022 | di->events.ac_wd_expired = false; | |
1023 | break; | |
1024 | case POWER_SUPPLY_HEALTH_GOOD: | |
1025 | di->events.main_thermal_prot = false; | |
1026 | di->events.mainextchnotok = false; | |
1027 | di->events.main_ovv = false; | |
1028 | di->events.ac_wd_expired = false; | |
1029 | break; | |
1030 | default: | |
1031 | break; | |
1032 | } | |
1033 | break; | |
1034 | ||
1035 | case POWER_SUPPLY_TYPE_USB: | |
1036 | switch (ret.intval) { | |
1037 | case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE: | |
1038 | di->events.usbchargernotok = true; | |
1039 | di->events.usb_thermal_prot = false; | |
1040 | di->events.vbus_ovv = false; | |
1041 | di->events.usb_wd_expired = false; | |
1042 | break; | |
1043 | case POWER_SUPPLY_HEALTH_DEAD: | |
1044 | di->events.usb_wd_expired = true; | |
1045 | di->events.usbchargernotok = false; | |
1046 | di->events.usb_thermal_prot = false; | |
1047 | di->events.vbus_ovv = false; | |
1048 | break; | |
1049 | case POWER_SUPPLY_HEALTH_COLD: | |
1050 | case POWER_SUPPLY_HEALTH_OVERHEAT: | |
1051 | di->events.usb_thermal_prot = true; | |
1052 | di->events.usbchargernotok = false; | |
1053 | di->events.vbus_ovv = false; | |
1054 | di->events.usb_wd_expired = false; | |
1055 | break; | |
1056 | case POWER_SUPPLY_HEALTH_OVERVOLTAGE: | |
1057 | di->events.vbus_ovv = true; | |
1058 | di->events.usbchargernotok = false; | |
1059 | di->events.usb_thermal_prot = false; | |
1060 | di->events.usb_wd_expired = false; | |
1061 | break; | |
1062 | case POWER_SUPPLY_HEALTH_GOOD: | |
1063 | di->events.usbchargernotok = false; | |
1064 | di->events.usb_thermal_prot = false; | |
1065 | di->events.vbus_ovv = false; | |
1066 | di->events.usb_wd_expired = false; | |
1067 | break; | |
1068 | default: | |
1069 | break; | |
1070 | } | |
1071 | default: | |
1072 | break; | |
1073 | } | |
1074 | break; | |
1075 | ||
1076 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: | |
1077 | switch (ext->type) { | |
1078 | case POWER_SUPPLY_TYPE_BATTERY: | |
1079 | di->batt_data.volt = ret.intval / 1000; | |
1080 | break; | |
1081 | case POWER_SUPPLY_TYPE_MAINS: | |
1082 | di->chg_info.ac_volt = ret.intval / 1000; | |
1083 | break; | |
1084 | case POWER_SUPPLY_TYPE_USB: | |
1085 | di->chg_info.usb_volt = ret.intval / 1000; | |
1086 | break; | |
1087 | default: | |
1088 | break; | |
1089 | } | |
1090 | break; | |
1091 | ||
1092 | case POWER_SUPPLY_PROP_VOLTAGE_AVG: | |
1093 | switch (ext->type) { | |
1094 | case POWER_SUPPLY_TYPE_MAINS: | |
1095 | /* AVG is used to indicate when we are | |
1096 | * in CV mode */ | |
1097 | if (ret.intval) | |
1098 | di->events.ac_cv_active = true; | |
1099 | else | |
1100 | di->events.ac_cv_active = false; | |
1101 | ||
1102 | break; | |
1103 | case POWER_SUPPLY_TYPE_USB: | |
1104 | /* AVG is used to indicate when we are | |
1105 | * in CV mode */ | |
1106 | if (ret.intval) | |
1107 | di->events.usb_cv_active = true; | |
1108 | else | |
1109 | di->events.usb_cv_active = false; | |
1110 | ||
1111 | break; | |
1112 | default: | |
1113 | break; | |
1114 | } | |
1115 | break; | |
1116 | ||
1117 | case POWER_SUPPLY_PROP_TECHNOLOGY: | |
1118 | switch (ext->type) { | |
1119 | case POWER_SUPPLY_TYPE_BATTERY: | |
1120 | if (ret.intval) | |
1121 | di->events.batt_unknown = false; | |
1122 | else | |
1123 | di->events.batt_unknown = true; | |
1124 | ||
1125 | break; | |
1126 | default: | |
1127 | break; | |
1128 | } | |
1129 | break; | |
1130 | ||
1131 | case POWER_SUPPLY_PROP_TEMP: | |
1132 | di->batt_data.temp = ret.intval / 10; | |
1133 | break; | |
1134 | ||
1135 | case POWER_SUPPLY_PROP_CURRENT_NOW: | |
1136 | switch (ext->type) { | |
1137 | case POWER_SUPPLY_TYPE_MAINS: | |
1138 | di->chg_info.ac_curr = | |
1139 | ret.intval / 1000; | |
1140 | break; | |
1141 | case POWER_SUPPLY_TYPE_USB: | |
1142 | di->chg_info.usb_curr = | |
1143 | ret.intval / 1000; | |
1144 | break; | |
1145 | case POWER_SUPPLY_TYPE_BATTERY: | |
1146 | di->batt_data.inst_curr = ret.intval / 1000; | |
1147 | break; | |
1148 | default: | |
1149 | break; | |
1150 | } | |
1151 | break; | |
1152 | ||
1153 | case POWER_SUPPLY_PROP_CURRENT_AVG: | |
1154 | switch (ext->type) { | |
1155 | case POWER_SUPPLY_TYPE_BATTERY: | |
1156 | di->batt_data.avg_curr = ret.intval / 1000; | |
1157 | break; | |
1158 | case POWER_SUPPLY_TYPE_USB: | |
1159 | if (ret.intval) | |
1160 | di->events.vbus_collapsed = true; | |
1161 | else | |
1162 | di->events.vbus_collapsed = false; | |
1163 | break; | |
1164 | default: | |
1165 | break; | |
1166 | } | |
1167 | break; | |
1168 | case POWER_SUPPLY_PROP_CAPACITY: | |
ea402401 MC |
1169 | if (!capacity_updated) |
1170 | di->batt_data.percent = ret.intval; | |
1668f811 AM |
1171 | break; |
1172 | default: | |
1173 | break; | |
1174 | } | |
1175 | } | |
1176 | return 0; | |
1177 | } | |
1178 | ||
1179 | /** | |
1180 | * abx500_chargalg_external_power_changed() - callback for power supply changes | |
1181 | * @psy: pointer to the structure power_supply | |
1182 | * | |
1183 | * This function is the entry point of the pointer external_power_changed | |
1184 | * of the structure power_supply. | |
1185 | * This function gets executed when there is a change in any external power | |
1186 | * supply that this driver needs to be notified of. | |
1187 | */ | |
1188 | static void abx500_chargalg_external_power_changed(struct power_supply *psy) | |
1189 | { | |
1190 | struct abx500_chargalg *di = to_abx500_chargalg_device_info(psy); | |
1191 | ||
1192 | /* | |
1193 | * Trigger execution of the algorithm instantly and read | |
1194 | * all power_supply properties there instead | |
1195 | */ | |
1196 | queue_work(di->chargalg_wq, &di->chargalg_work); | |
1197 | } | |
1198 | ||
1199 | /** | |
1200 | * abx500_chargalg_algorithm() - Main function for the algorithm | |
1201 | * @di: pointer to the abx500_chargalg structure | |
1202 | * | |
1203 | * This is the main control function for the charging algorithm. | |
1204 | * It is called periodically or when something happens that will | |
1205 | * trigger a state change | |
1206 | */ | |
1207 | static void abx500_chargalg_algorithm(struct abx500_chargalg *di) | |
1208 | { | |
1209 | int charger_status; | |
1210 | ||
1211 | /* Collect data from all power_supply class devices */ | |
1212 | class_for_each_device(power_supply_class, NULL, | |
1213 | &di->chargalg_psy, abx500_chargalg_get_ext_psy_data); | |
1214 | ||
1215 | abx500_chargalg_end_of_charge(di); | |
1216 | abx500_chargalg_check_temp(di); | |
1217 | abx500_chargalg_check_charger_voltage(di); | |
1218 | ||
1219 | charger_status = abx500_chargalg_check_charger_connection(di); | |
1220 | /* | |
1221 | * First check if we have a charger connected. | |
1222 | * Also we don't allow charging of unknown batteries if configured | |
1223 | * this way | |
1224 | */ | |
1225 | if (!charger_status || | |
b0284de0 | 1226 | (di->events.batt_unknown && !di->bm->chg_unknown_bat)) { |
1668f811 AM |
1227 | if (di->charge_state != STATE_HANDHELD) { |
1228 | di->events.safety_timer_expired = false; | |
1229 | abx500_chargalg_state_to(di, STATE_HANDHELD_INIT); | |
1230 | } | |
1231 | } | |
1232 | ||
1233 | /* If suspended, we should not continue checking the flags */ | |
1234 | else if (di->charge_state == STATE_SUSPENDED_INIT || | |
1235 | di->charge_state == STATE_SUSPENDED) { | |
1236 | /* We don't do anything here, just don,t continue */ | |
1237 | } | |
1238 | ||
1239 | /* Safety timer expiration */ | |
1240 | else if (di->events.safety_timer_expired) { | |
1241 | if (di->charge_state != STATE_SAFETY_TIMER_EXPIRED) | |
1242 | abx500_chargalg_state_to(di, | |
1243 | STATE_SAFETY_TIMER_EXPIRED_INIT); | |
1244 | } | |
1245 | /* | |
1246 | * Check if any interrupts has occured | |
1247 | * that will prevent us from charging | |
1248 | */ | |
1249 | ||
1250 | /* Battery removed */ | |
1251 | else if (di->events.batt_rem) { | |
1252 | if (di->charge_state != STATE_BATT_REMOVED) | |
1253 | abx500_chargalg_state_to(di, STATE_BATT_REMOVED_INIT); | |
1254 | } | |
1255 | /* Main or USB charger not ok. */ | |
1256 | else if (di->events.mainextchnotok || di->events.usbchargernotok) { | |
1257 | /* | |
1258 | * If vbus_collapsed is set, we have to lower the charger | |
1259 | * current, which is done in the normal state below | |
1260 | */ | |
1261 | if (di->charge_state != STATE_CHG_NOT_OK && | |
1262 | !di->events.vbus_collapsed) | |
1263 | abx500_chargalg_state_to(di, STATE_CHG_NOT_OK_INIT); | |
1264 | } | |
1265 | /* VBUS, Main or VBAT OVV. */ | |
1266 | else if (di->events.vbus_ovv || | |
1267 | di->events.main_ovv || | |
1268 | di->events.batt_ovv || | |
1269 | !di->chg_info.usb_chg_ok || | |
1270 | !di->chg_info.ac_chg_ok) { | |
1271 | if (di->charge_state != STATE_OVV_PROTECT) | |
1272 | abx500_chargalg_state_to(di, STATE_OVV_PROTECT_INIT); | |
1273 | } | |
1274 | /* USB Thermal, stop charging */ | |
1275 | else if (di->events.main_thermal_prot || | |
1276 | di->events.usb_thermal_prot) { | |
1277 | if (di->charge_state != STATE_HW_TEMP_PROTECT) | |
1278 | abx500_chargalg_state_to(di, | |
1279 | STATE_HW_TEMP_PROTECT_INIT); | |
1280 | } | |
1281 | /* Battery temp over/under */ | |
1282 | else if (di->events.btemp_underover) { | |
1283 | if (di->charge_state != STATE_TEMP_UNDEROVER) | |
1284 | abx500_chargalg_state_to(di, | |
1285 | STATE_TEMP_UNDEROVER_INIT); | |
1286 | } | |
1287 | /* Watchdog expired */ | |
1288 | else if (di->events.ac_wd_expired || | |
1289 | di->events.usb_wd_expired) { | |
1290 | if (di->charge_state != STATE_WD_EXPIRED) | |
1291 | abx500_chargalg_state_to(di, STATE_WD_EXPIRED_INIT); | |
1292 | } | |
1293 | /* Battery temp high/low */ | |
1294 | else if (di->events.btemp_lowhigh) { | |
1295 | if (di->charge_state != STATE_TEMP_LOWHIGH) | |
1296 | abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH_INIT); | |
1297 | } | |
1298 | ||
1299 | dev_dbg(di->dev, | |
1300 | "[CHARGALG] Vb %d Ib_avg %d Ib_inst %d Tb %d Cap %d Maint %d " | |
1301 | "State %s Active_chg %d Chg_status %d AC %d USB %d " | |
1302 | "AC_online %d USB_online %d AC_CV %d USB_CV %d AC_I %d " | |
1303 | "USB_I %d AC_Vset %d AC_Iset %d USB_Vset %d USB_Iset %d\n", | |
1304 | di->batt_data.volt, | |
1305 | di->batt_data.avg_curr, | |
1306 | di->batt_data.inst_curr, | |
1307 | di->batt_data.temp, | |
1308 | di->batt_data.percent, | |
1309 | di->maintenance_chg, | |
1310 | states[di->charge_state], | |
1311 | di->chg_info.charger_type, | |
1312 | di->charge_status, | |
1313 | di->chg_info.conn_chg & AC_CHG, | |
1314 | di->chg_info.conn_chg & USB_CHG, | |
1315 | di->chg_info.online_chg & AC_CHG, | |
1316 | di->chg_info.online_chg & USB_CHG, | |
1317 | di->events.ac_cv_active, | |
1318 | di->events.usb_cv_active, | |
1319 | di->chg_info.ac_curr, | |
1320 | di->chg_info.usb_curr, | |
1321 | di->chg_info.ac_vset, | |
1322 | di->chg_info.ac_iset, | |
1323 | di->chg_info.usb_vset, | |
1324 | di->chg_info.usb_iset); | |
1325 | ||
1326 | switch (di->charge_state) { | |
1327 | case STATE_HANDHELD_INIT: | |
1328 | abx500_chargalg_stop_charging(di); | |
1329 | di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING; | |
1330 | abx500_chargalg_state_to(di, STATE_HANDHELD); | |
1331 | /* Intentional fallthrough */ | |
1332 | ||
1333 | case STATE_HANDHELD: | |
1334 | break; | |
1335 | ||
1336 | case STATE_SUSPENDED_INIT: | |
1337 | if (di->susp_status.ac_suspended) | |
1338 | abx500_chargalg_ac_en(di, false, 0, 0); | |
1339 | if (di->susp_status.usb_suspended) | |
1340 | abx500_chargalg_usb_en(di, false, 0, 0); | |
1341 | abx500_chargalg_stop_safety_timer(di); | |
1342 | abx500_chargalg_stop_maintenance_timer(di); | |
1343 | di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING; | |
1344 | di->maintenance_chg = false; | |
1345 | abx500_chargalg_state_to(di, STATE_SUSPENDED); | |
1346 | power_supply_changed(&di->chargalg_psy); | |
1347 | /* Intentional fallthrough */ | |
1348 | ||
1349 | case STATE_SUSPENDED: | |
1350 | /* CHARGING is suspended */ | |
1351 | break; | |
1352 | ||
1353 | case STATE_BATT_REMOVED_INIT: | |
1354 | abx500_chargalg_stop_charging(di); | |
1355 | abx500_chargalg_state_to(di, STATE_BATT_REMOVED); | |
1356 | /* Intentional fallthrough */ | |
1357 | ||
1358 | case STATE_BATT_REMOVED: | |
1359 | if (!di->events.batt_rem) | |
1360 | abx500_chargalg_state_to(di, STATE_NORMAL_INIT); | |
1361 | break; | |
1362 | ||
1363 | case STATE_HW_TEMP_PROTECT_INIT: | |
1364 | abx500_chargalg_stop_charging(di); | |
1365 | abx500_chargalg_state_to(di, STATE_HW_TEMP_PROTECT); | |
1366 | /* Intentional fallthrough */ | |
1367 | ||
1368 | case STATE_HW_TEMP_PROTECT: | |
1369 | if (!di->events.main_thermal_prot && | |
1370 | !di->events.usb_thermal_prot) | |
1371 | abx500_chargalg_state_to(di, STATE_NORMAL_INIT); | |
1372 | break; | |
1373 | ||
1374 | case STATE_OVV_PROTECT_INIT: | |
1375 | abx500_chargalg_stop_charging(di); | |
1376 | abx500_chargalg_state_to(di, STATE_OVV_PROTECT); | |
1377 | /* Intentional fallthrough */ | |
1378 | ||
1379 | case STATE_OVV_PROTECT: | |
1380 | if (!di->events.vbus_ovv && | |
1381 | !di->events.main_ovv && | |
1382 | !di->events.batt_ovv && | |
1383 | di->chg_info.usb_chg_ok && | |
1384 | di->chg_info.ac_chg_ok) | |
1385 | abx500_chargalg_state_to(di, STATE_NORMAL_INIT); | |
1386 | break; | |
1387 | ||
1388 | case STATE_CHG_NOT_OK_INIT: | |
1389 | abx500_chargalg_stop_charging(di); | |
1390 | abx500_chargalg_state_to(di, STATE_CHG_NOT_OK); | |
1391 | /* Intentional fallthrough */ | |
1392 | ||
1393 | case STATE_CHG_NOT_OK: | |
1394 | if (!di->events.mainextchnotok && | |
1395 | !di->events.usbchargernotok) | |
1396 | abx500_chargalg_state_to(di, STATE_NORMAL_INIT); | |
1397 | break; | |
1398 | ||
1399 | case STATE_SAFETY_TIMER_EXPIRED_INIT: | |
1400 | abx500_chargalg_stop_charging(di); | |
1401 | abx500_chargalg_state_to(di, STATE_SAFETY_TIMER_EXPIRED); | |
1402 | /* Intentional fallthrough */ | |
1403 | ||
1404 | case STATE_SAFETY_TIMER_EXPIRED: | |
1405 | /* We exit this state when charger is removed */ | |
1406 | break; | |
1407 | ||
1408 | case STATE_NORMAL_INIT: | |
1409 | abx500_chargalg_start_charging(di, | |
b0284de0 LJ |
1410 | di->bm->bat_type[di->bm->batt_id].normal_vol_lvl, |
1411 | di->bm->bat_type[di->bm->batt_id].normal_cur_lvl); | |
1668f811 AM |
1412 | abx500_chargalg_state_to(di, STATE_NORMAL); |
1413 | abx500_chargalg_start_safety_timer(di); | |
1414 | abx500_chargalg_stop_maintenance_timer(di); | |
1415 | init_maxim_chg_curr(di); | |
1416 | di->charge_status = POWER_SUPPLY_STATUS_CHARGING; | |
1417 | di->eoc_cnt = 0; | |
1418 | di->maintenance_chg = false; | |
1419 | power_supply_changed(&di->chargalg_psy); | |
1420 | ||
1421 | break; | |
1422 | ||
1423 | case STATE_NORMAL: | |
1424 | handle_maxim_chg_curr(di); | |
1425 | if (di->charge_status == POWER_SUPPLY_STATUS_FULL && | |
1426 | di->maintenance_chg) { | |
b0284de0 | 1427 | if (di->bm->no_maintenance) |
1668f811 AM |
1428 | abx500_chargalg_state_to(di, |
1429 | STATE_WAIT_FOR_RECHARGE_INIT); | |
1430 | else | |
1431 | abx500_chargalg_state_to(di, | |
1432 | STATE_MAINTENANCE_A_INIT); | |
1433 | } | |
1434 | break; | |
1435 | ||
1436 | /* This state will be used when the maintenance state is disabled */ | |
1437 | case STATE_WAIT_FOR_RECHARGE_INIT: | |
1438 | abx500_chargalg_hold_charging(di); | |
1439 | abx500_chargalg_state_to(di, STATE_WAIT_FOR_RECHARGE); | |
1668f811 AM |
1440 | /* Intentional fallthrough */ |
1441 | ||
1442 | case STATE_WAIT_FOR_RECHARGE: | |
ea402401 MC |
1443 | if (di->batt_data.percent <= |
1444 | di->bm->bat_type[di->bm->batt_id]. | |
1445 | recharge_cap) | |
1446 | abx500_chargalg_state_to(di, STATE_NORMAL_INIT); | |
1668f811 AM |
1447 | break; |
1448 | ||
1449 | case STATE_MAINTENANCE_A_INIT: | |
1450 | abx500_chargalg_stop_safety_timer(di); | |
1451 | abx500_chargalg_start_maintenance_timer(di, | |
b0284de0 LJ |
1452 | di->bm->bat_type[ |
1453 | di->bm->batt_id].maint_a_chg_timer_h); | |
1668f811 | 1454 | abx500_chargalg_start_charging(di, |
b0284de0 LJ |
1455 | di->bm->bat_type[ |
1456 | di->bm->batt_id].maint_a_vol_lvl, | |
1457 | di->bm->bat_type[ | |
1458 | di->bm->batt_id].maint_a_cur_lvl); | |
1668f811 AM |
1459 | abx500_chargalg_state_to(di, STATE_MAINTENANCE_A); |
1460 | power_supply_changed(&di->chargalg_psy); | |
1461 | /* Intentional fallthrough*/ | |
1462 | ||
1463 | case STATE_MAINTENANCE_A: | |
1464 | if (di->events.maintenance_timer_expired) { | |
1465 | abx500_chargalg_stop_maintenance_timer(di); | |
1466 | abx500_chargalg_state_to(di, STATE_MAINTENANCE_B_INIT); | |
1467 | } | |
1468 | break; | |
1469 | ||
1470 | case STATE_MAINTENANCE_B_INIT: | |
1471 | abx500_chargalg_start_maintenance_timer(di, | |
b0284de0 LJ |
1472 | di->bm->bat_type[ |
1473 | di->bm->batt_id].maint_b_chg_timer_h); | |
1668f811 | 1474 | abx500_chargalg_start_charging(di, |
b0284de0 LJ |
1475 | di->bm->bat_type[ |
1476 | di->bm->batt_id].maint_b_vol_lvl, | |
1477 | di->bm->bat_type[ | |
1478 | di->bm->batt_id].maint_b_cur_lvl); | |
1668f811 AM |
1479 | abx500_chargalg_state_to(di, STATE_MAINTENANCE_B); |
1480 | power_supply_changed(&di->chargalg_psy); | |
1481 | /* Intentional fallthrough*/ | |
1482 | ||
1483 | case STATE_MAINTENANCE_B: | |
1484 | if (di->events.maintenance_timer_expired) { | |
1485 | abx500_chargalg_stop_maintenance_timer(di); | |
1486 | abx500_chargalg_state_to(di, STATE_NORMAL_INIT); | |
1487 | } | |
1488 | break; | |
1489 | ||
1490 | case STATE_TEMP_LOWHIGH_INIT: | |
1491 | abx500_chargalg_start_charging(di, | |
b0284de0 LJ |
1492 | di->bm->bat_type[ |
1493 | di->bm->batt_id].low_high_vol_lvl, | |
1494 | di->bm->bat_type[ | |
1495 | di->bm->batt_id].low_high_cur_lvl); | |
1668f811 AM |
1496 | abx500_chargalg_stop_maintenance_timer(di); |
1497 | di->charge_status = POWER_SUPPLY_STATUS_CHARGING; | |
1498 | abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH); | |
1499 | power_supply_changed(&di->chargalg_psy); | |
1500 | /* Intentional fallthrough */ | |
1501 | ||
1502 | case STATE_TEMP_LOWHIGH: | |
1503 | if (!di->events.btemp_lowhigh) | |
1504 | abx500_chargalg_state_to(di, STATE_NORMAL_INIT); | |
1505 | break; | |
1506 | ||
1507 | case STATE_WD_EXPIRED_INIT: | |
1508 | abx500_chargalg_stop_charging(di); | |
1509 | abx500_chargalg_state_to(di, STATE_WD_EXPIRED); | |
1510 | /* Intentional fallthrough */ | |
1511 | ||
1512 | case STATE_WD_EXPIRED: | |
1513 | if (!di->events.ac_wd_expired && | |
1514 | !di->events.usb_wd_expired) | |
1515 | abx500_chargalg_state_to(di, STATE_NORMAL_INIT); | |
1516 | break; | |
1517 | ||
1518 | case STATE_TEMP_UNDEROVER_INIT: | |
1519 | abx500_chargalg_stop_charging(di); | |
1520 | abx500_chargalg_state_to(di, STATE_TEMP_UNDEROVER); | |
1521 | /* Intentional fallthrough */ | |
1522 | ||
1523 | case STATE_TEMP_UNDEROVER: | |
1524 | if (!di->events.btemp_underover) | |
1525 | abx500_chargalg_state_to(di, STATE_NORMAL_INIT); | |
1526 | break; | |
1527 | } | |
1528 | ||
1529 | /* Start charging directly if the new state is a charge state */ | |
1530 | if (di->charge_state == STATE_NORMAL_INIT || | |
1531 | di->charge_state == STATE_MAINTENANCE_A_INIT || | |
1532 | di->charge_state == STATE_MAINTENANCE_B_INIT) | |
1533 | queue_work(di->chargalg_wq, &di->chargalg_work); | |
1534 | } | |
1535 | ||
1536 | /** | |
1537 | * abx500_chargalg_periodic_work() - Periodic work for the algorithm | |
1538 | * @work: pointer to the work_struct structure | |
1539 | * | |
1540 | * Work queue function for the charging algorithm | |
1541 | */ | |
1542 | static void abx500_chargalg_periodic_work(struct work_struct *work) | |
1543 | { | |
1544 | struct abx500_chargalg *di = container_of(work, | |
1545 | struct abx500_chargalg, chargalg_periodic_work.work); | |
1546 | ||
1547 | abx500_chargalg_algorithm(di); | |
1548 | ||
1549 | /* | |
1550 | * If a charger is connected then the battery has to be monitored | |
1551 | * frequently, else the work can be delayed. | |
1552 | */ | |
1553 | if (di->chg_info.conn_chg) | |
1554 | queue_delayed_work(di->chargalg_wq, | |
1555 | &di->chargalg_periodic_work, | |
b0284de0 | 1556 | di->bm->interval_charging * HZ); |
1668f811 AM |
1557 | else |
1558 | queue_delayed_work(di->chargalg_wq, | |
1559 | &di->chargalg_periodic_work, | |
b0284de0 | 1560 | di->bm->interval_not_charging * HZ); |
1668f811 AM |
1561 | } |
1562 | ||
1563 | /** | |
1564 | * abx500_chargalg_wd_work() - periodic work to kick the charger watchdog | |
1565 | * @work: pointer to the work_struct structure | |
1566 | * | |
1567 | * Work queue function for kicking the charger watchdog | |
1568 | */ | |
1569 | static void abx500_chargalg_wd_work(struct work_struct *work) | |
1570 | { | |
1571 | int ret; | |
1572 | struct abx500_chargalg *di = container_of(work, | |
1573 | struct abx500_chargalg, chargalg_wd_work.work); | |
1574 | ||
1575 | dev_dbg(di->dev, "abx500_chargalg_wd_work\n"); | |
1576 | ||
1577 | ret = abx500_chargalg_kick_watchdog(di); | |
1578 | if (ret < 0) | |
1579 | dev_err(di->dev, "failed to kick watchdog\n"); | |
1580 | ||
1581 | queue_delayed_work(di->chargalg_wq, | |
1582 | &di->chargalg_wd_work, CHG_WD_INTERVAL); | |
1583 | } | |
1584 | ||
1585 | /** | |
1586 | * abx500_chargalg_work() - Work to run the charging algorithm instantly | |
1587 | * @work: pointer to the work_struct structure | |
1588 | * | |
1589 | * Work queue function for calling the charging algorithm | |
1590 | */ | |
1591 | static void abx500_chargalg_work(struct work_struct *work) | |
1592 | { | |
1593 | struct abx500_chargalg *di = container_of(work, | |
1594 | struct abx500_chargalg, chargalg_work); | |
1595 | ||
1596 | abx500_chargalg_algorithm(di); | |
1597 | } | |
1598 | ||
1599 | /** | |
1600 | * abx500_chargalg_get_property() - get the chargalg properties | |
1601 | * @psy: pointer to the power_supply structure | |
1602 | * @psp: pointer to the power_supply_property structure | |
1603 | * @val: pointer to the power_supply_propval union | |
1604 | * | |
1605 | * This function gets called when an application tries to get the | |
1606 | * chargalg properties by reading the sysfs files. | |
1607 | * status: charging/discharging/full/unknown | |
1608 | * health: health of the battery | |
1609 | * Returns error code in case of failure else 0 on success | |
1610 | */ | |
1611 | static int abx500_chargalg_get_property(struct power_supply *psy, | |
1612 | enum power_supply_property psp, | |
1613 | union power_supply_propval *val) | |
1614 | { | |
1615 | struct abx500_chargalg *di; | |
1616 | ||
1617 | di = to_abx500_chargalg_device_info(psy); | |
1618 | ||
1619 | switch (psp) { | |
1620 | case POWER_SUPPLY_PROP_STATUS: | |
1621 | val->intval = di->charge_status; | |
1622 | break; | |
1623 | case POWER_SUPPLY_PROP_HEALTH: | |
1624 | if (di->events.batt_ovv) { | |
1625 | val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; | |
1626 | } else if (di->events.btemp_underover) { | |
b0284de0 | 1627 | if (di->batt_data.temp <= di->bm->temp_under) |
1668f811 AM |
1628 | val->intval = POWER_SUPPLY_HEALTH_COLD; |
1629 | else | |
1630 | val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; | |
d80108fc LJ |
1631 | } else if (di->charge_state == STATE_SAFETY_TIMER_EXPIRED || |
1632 | di->charge_state == STATE_SAFETY_TIMER_EXPIRED_INIT) { | |
1633 | val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; | |
1668f811 AM |
1634 | } else { |
1635 | val->intval = POWER_SUPPLY_HEALTH_GOOD; | |
1636 | } | |
1637 | break; | |
1638 | default: | |
1639 | return -EINVAL; | |
1640 | } | |
1641 | return 0; | |
1642 | } | |
1643 | ||
1644 | /* Exposure to the sysfs interface */ | |
1645 | ||
1646 | /** | |
1647 | * abx500_chargalg_sysfs_charger() - sysfs store operations | |
1648 | * @kobj: pointer to the struct kobject | |
1649 | * @attr: pointer to the struct attribute | |
1650 | * @buf: buffer that holds the parameter passed from userspace | |
1651 | * @length: length of the parameter passed | |
1652 | * | |
1653 | * Returns length of the buffer(input taken from user space) on success | |
1654 | * else error code on failure | |
1655 | * The operation to be performed on passing the parameters from the user space. | |
1656 | */ | |
1657 | static ssize_t abx500_chargalg_sysfs_charger(struct kobject *kobj, | |
1658 | struct attribute *attr, const char *buf, size_t length) | |
1659 | { | |
1660 | struct abx500_chargalg *di = container_of(kobj, | |
1661 | struct abx500_chargalg, chargalg_kobject); | |
1662 | long int param; | |
1663 | int ac_usb; | |
1664 | int ret; | |
1665 | char entry = *attr->name; | |
1666 | ||
1667 | switch (entry) { | |
1668 | case 'c': | |
1669 | ret = strict_strtol(buf, 10, ¶m); | |
1670 | if (ret < 0) | |
1671 | return ret; | |
1672 | ||
1673 | ac_usb = param; | |
1674 | switch (ac_usb) { | |
1675 | case 0: | |
1676 | /* Disable charging */ | |
1677 | di->susp_status.ac_suspended = true; | |
1678 | di->susp_status.usb_suspended = true; | |
1679 | di->susp_status.suspended_change = true; | |
1680 | /* Trigger a state change */ | |
1681 | queue_work(di->chargalg_wq, | |
1682 | &di->chargalg_work); | |
1683 | break; | |
1684 | case 1: | |
1685 | /* Enable AC Charging */ | |
1686 | di->susp_status.ac_suspended = false; | |
1687 | di->susp_status.suspended_change = true; | |
1688 | /* Trigger a state change */ | |
1689 | queue_work(di->chargalg_wq, | |
1690 | &di->chargalg_work); | |
1691 | break; | |
1692 | case 2: | |
1693 | /* Enable USB charging */ | |
1694 | di->susp_status.usb_suspended = false; | |
1695 | di->susp_status.suspended_change = true; | |
1696 | /* Trigger a state change */ | |
1697 | queue_work(di->chargalg_wq, | |
1698 | &di->chargalg_work); | |
1699 | break; | |
1700 | default: | |
1701 | dev_info(di->dev, "Wrong input\n" | |
1702 | "Enter 0. Disable AC/USB Charging\n" | |
1703 | "1. Enable AC charging\n" | |
1704 | "2. Enable USB Charging\n"); | |
1705 | }; | |
1706 | break; | |
1707 | }; | |
1708 | return strlen(buf); | |
1709 | } | |
1710 | ||
1711 | static struct attribute abx500_chargalg_en_charger = \ | |
1712 | { | |
1713 | .name = "chargalg", | |
e3455002 | 1714 | .mode = S_IWUSR, |
1668f811 AM |
1715 | }; |
1716 | ||
1717 | static struct attribute *abx500_chargalg_chg[] = { | |
1718 | &abx500_chargalg_en_charger, | |
1719 | NULL | |
1720 | }; | |
1721 | ||
64eb9b02 | 1722 | static const struct sysfs_ops abx500_chargalg_sysfs_ops = { |
1668f811 AM |
1723 | .store = abx500_chargalg_sysfs_charger, |
1724 | }; | |
1725 | ||
1726 | static struct kobj_type abx500_chargalg_ktype = { | |
1727 | .sysfs_ops = &abx500_chargalg_sysfs_ops, | |
1728 | .default_attrs = abx500_chargalg_chg, | |
1729 | }; | |
1730 | ||
1731 | /** | |
1732 | * abx500_chargalg_sysfs_exit() - de-init of sysfs entry | |
1733 | * @di: pointer to the struct abx500_chargalg | |
1734 | * | |
1735 | * This function removes the entry in sysfs. | |
1736 | */ | |
1737 | static void abx500_chargalg_sysfs_exit(struct abx500_chargalg *di) | |
1738 | { | |
1739 | kobject_del(&di->chargalg_kobject); | |
1740 | } | |
1741 | ||
1742 | /** | |
1743 | * abx500_chargalg_sysfs_init() - init of sysfs entry | |
1744 | * @di: pointer to the struct abx500_chargalg | |
1745 | * | |
1746 | * This function adds an entry in sysfs. | |
1747 | * Returns error code in case of failure else 0(on success) | |
1748 | */ | |
1749 | static int abx500_chargalg_sysfs_init(struct abx500_chargalg *di) | |
1750 | { | |
1751 | int ret = 0; | |
1752 | ||
1753 | ret = kobject_init_and_add(&di->chargalg_kobject, | |
1754 | &abx500_chargalg_ktype, | |
1755 | NULL, "abx500_chargalg"); | |
1756 | if (ret < 0) | |
1757 | dev_err(di->dev, "failed to create sysfs entry\n"); | |
1758 | ||
1759 | return ret; | |
1760 | } | |
1761 | /* Exposure to the sysfs interface <<END>> */ | |
1762 | ||
1763 | #if defined(CONFIG_PM) | |
1764 | static int abx500_chargalg_resume(struct platform_device *pdev) | |
1765 | { | |
1766 | struct abx500_chargalg *di = platform_get_drvdata(pdev); | |
1767 | ||
1768 | /* Kick charger watchdog if charging (any charger online) */ | |
1769 | if (di->chg_info.online_chg) | |
1770 | queue_delayed_work(di->chargalg_wq, &di->chargalg_wd_work, 0); | |
1771 | ||
1772 | /* | |
1773 | * Run the charging algorithm directly to be sure we don't | |
1774 | * do it too seldom | |
1775 | */ | |
1776 | queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0); | |
1777 | ||
1778 | return 0; | |
1779 | } | |
1780 | ||
1781 | static int abx500_chargalg_suspend(struct platform_device *pdev, | |
1782 | pm_message_t state) | |
1783 | { | |
1784 | struct abx500_chargalg *di = platform_get_drvdata(pdev); | |
1785 | ||
1786 | if (di->chg_info.online_chg) | |
1787 | cancel_delayed_work_sync(&di->chargalg_wd_work); | |
1788 | ||
1789 | cancel_delayed_work_sync(&di->chargalg_periodic_work); | |
1790 | ||
1791 | return 0; | |
1792 | } | |
1793 | #else | |
1794 | #define abx500_chargalg_suspend NULL | |
1795 | #define abx500_chargalg_resume NULL | |
1796 | #endif | |
1797 | ||
415ec69f | 1798 | static int abx500_chargalg_remove(struct platform_device *pdev) |
1668f811 AM |
1799 | { |
1800 | struct abx500_chargalg *di = platform_get_drvdata(pdev); | |
1801 | ||
1802 | /* sysfs interface to enable/disbale charging from user space */ | |
1803 | abx500_chargalg_sysfs_exit(di); | |
1804 | ||
1805 | /* Delete the work queue */ | |
1806 | destroy_workqueue(di->chargalg_wq); | |
1807 | ||
1808 | flush_scheduled_work(); | |
1809 | power_supply_unregister(&di->chargalg_psy); | |
1810 | platform_set_drvdata(pdev, NULL); | |
1668f811 AM |
1811 | |
1812 | return 0; | |
1813 | } | |
1814 | ||
a12810ab R |
1815 | static char *supply_interface[] = { |
1816 | "ab8500_fg", | |
1817 | }; | |
1818 | ||
c8afa640 | 1819 | static int abx500_chargalg_probe(struct platform_device *pdev) |
1668f811 | 1820 | { |
a12810ab | 1821 | struct device_node *np = pdev->dev.of_node; |
bdc56b44 | 1822 | struct abx500_bm_data *plat = pdev->dev.platform_data; |
a12810ab | 1823 | struct abx500_chargalg *di; |
1668f811 AM |
1824 | int ret = 0; |
1825 | ||
a12810ab R |
1826 | di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL); |
1827 | if (!di) { | |
1828 | dev_err(&pdev->dev, "%s no mem for ab8500_chargalg\n", __func__); | |
1668f811 | 1829 | return -ENOMEM; |
a12810ab | 1830 | } |
bdc56b44 LJ |
1831 | |
1832 | if (!plat) { | |
1833 | dev_err(&pdev->dev, "no battery management data supplied\n"); | |
1834 | return -EINVAL; | |
1835 | } | |
1836 | di->bm = plat; | |
1837 | ||
1838 | if (np) { | |
1839 | ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm); | |
1840 | if (ret) { | |
1841 | dev_err(&pdev->dev, "failed to get battery information\n"); | |
1842 | return ret; | |
a12810ab | 1843 | } |
a12810ab | 1844 | } |
1668f811 AM |
1845 | |
1846 | /* get device struct */ | |
1847 | di->dev = &pdev->dev; | |
1668f811 AM |
1848 | |
1849 | /* chargalg supply */ | |
1850 | di->chargalg_psy.name = "abx500_chargalg"; | |
1851 | di->chargalg_psy.type = POWER_SUPPLY_TYPE_BATTERY; | |
1852 | di->chargalg_psy.properties = abx500_chargalg_props; | |
1853 | di->chargalg_psy.num_properties = ARRAY_SIZE(abx500_chargalg_props); | |
1854 | di->chargalg_psy.get_property = abx500_chargalg_get_property; | |
a12810ab R |
1855 | di->chargalg_psy.supplied_to = supply_interface; |
1856 | di->chargalg_psy.num_supplicants = ARRAY_SIZE(supply_interface), | |
1668f811 AM |
1857 | di->chargalg_psy.external_power_changed = |
1858 | abx500_chargalg_external_power_changed; | |
1859 | ||
1860 | /* Initilialize safety timer */ | |
1861 | init_timer(&di->safety_timer); | |
1862 | di->safety_timer.function = abx500_chargalg_safety_timer_expired; | |
1863 | di->safety_timer.data = (unsigned long) di; | |
1864 | ||
1865 | /* Initilialize maintenance timer */ | |
1866 | init_timer(&di->maintenance_timer); | |
1867 | di->maintenance_timer.function = | |
1868 | abx500_chargalg_maintenance_timer_expired; | |
1869 | di->maintenance_timer.data = (unsigned long) di; | |
1870 | ||
1871 | /* Create a work queue for the chargalg */ | |
1872 | di->chargalg_wq = | |
1873 | create_singlethread_workqueue("abx500_chargalg_wq"); | |
1874 | if (di->chargalg_wq == NULL) { | |
1875 | dev_err(di->dev, "failed to create work queue\n"); | |
a12810ab | 1876 | return -ENOMEM; |
1668f811 AM |
1877 | } |
1878 | ||
1879 | /* Init work for chargalg */ | |
203b42f7 | 1880 | INIT_DEFERRABLE_WORK(&di->chargalg_periodic_work, |
1668f811 | 1881 | abx500_chargalg_periodic_work); |
203b42f7 | 1882 | INIT_DEFERRABLE_WORK(&di->chargalg_wd_work, |
1668f811 AM |
1883 | abx500_chargalg_wd_work); |
1884 | ||
1885 | /* Init work for chargalg */ | |
1886 | INIT_WORK(&di->chargalg_work, abx500_chargalg_work); | |
1887 | ||
1888 | /* To detect charger at startup */ | |
1889 | di->chg_info.prev_conn_chg = -1; | |
1890 | ||
1891 | /* Register chargalg power supply class */ | |
1892 | ret = power_supply_register(di->dev, &di->chargalg_psy); | |
1893 | if (ret) { | |
1894 | dev_err(di->dev, "failed to register chargalg psy\n"); | |
1895 | goto free_chargalg_wq; | |
1896 | } | |
1897 | ||
1898 | platform_set_drvdata(pdev, di); | |
1899 | ||
1900 | /* sysfs interface to enable/disable charging from user space */ | |
1901 | ret = abx500_chargalg_sysfs_init(di); | |
1902 | if (ret) { | |
1903 | dev_err(di->dev, "failed to create sysfs entry\n"); | |
1904 | goto free_psy; | |
1905 | } | |
1906 | ||
1907 | /* Run the charging algorithm */ | |
1908 | queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0); | |
1909 | ||
1910 | dev_info(di->dev, "probe success\n"); | |
1911 | return ret; | |
1912 | ||
1913 | free_psy: | |
1914 | power_supply_unregister(&di->chargalg_psy); | |
1915 | free_chargalg_wq: | |
1916 | destroy_workqueue(di->chargalg_wq); | |
1668f811 AM |
1917 | return ret; |
1918 | } | |
1919 | ||
a12810ab R |
1920 | static const struct of_device_id ab8500_chargalg_match[] = { |
1921 | { .compatible = "stericsson,ab8500-chargalg", }, | |
1922 | { }, | |
1923 | }; | |
1924 | ||
1668f811 AM |
1925 | static struct platform_driver abx500_chargalg_driver = { |
1926 | .probe = abx500_chargalg_probe, | |
28ea73f4 | 1927 | .remove = abx500_chargalg_remove, |
1668f811 AM |
1928 | .suspend = abx500_chargalg_suspend, |
1929 | .resume = abx500_chargalg_resume, | |
1930 | .driver = { | |
a12810ab | 1931 | .name = "ab8500-chargalg", |
1668f811 | 1932 | .owner = THIS_MODULE, |
a12810ab | 1933 | .of_match_table = ab8500_chargalg_match, |
1668f811 AM |
1934 | }, |
1935 | }; | |
1936 | ||
1937 | static int __init abx500_chargalg_init(void) | |
1938 | { | |
1939 | return platform_driver_register(&abx500_chargalg_driver); | |
1940 | } | |
1941 | ||
1942 | static void __exit abx500_chargalg_exit(void) | |
1943 | { | |
1944 | platform_driver_unregister(&abx500_chargalg_driver); | |
1945 | } | |
1946 | ||
1947 | module_init(abx500_chargalg_init); | |
1948 | module_exit(abx500_chargalg_exit); | |
1949 | ||
1950 | MODULE_LICENSE("GPL v2"); | |
1951 | MODULE_AUTHOR("Johan Palsson, Karl Komierowski"); | |
1952 | MODULE_ALIAS("platform:abx500-chargalg"); | |
1953 | MODULE_DESCRIPTION("abx500 battery charging algorithm"); |