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