1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson SA 2012
4 * Copyright (c) 2012 Sony Mobile Communications AB
6 * Charging algorithm driver for abx500 variants
9 * Johan Palsson <johan.palsson@stericsson.com>
10 * Karl Komierowski <karl.komierowski@stericsson.com>
11 * Arun R Murthy <arun.murthy@stericsson.com>
12 * Author: Imre Sunyi <imre.sunyi@sonymobile.com>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/device.h>
18 #include <linux/component.h>
19 #include <linux/hrtimer.h>
20 #include <linux/interrupt.h>
21 #include <linux/delay.h>
22 #include <linux/slab.h>
23 #include <linux/platform_device.h>
24 #include <linux/power_supply.h>
25 #include <linux/completion.h>
26 #include <linux/workqueue.h>
27 #include <linux/kobject.h>
29 #include <linux/mfd/core.h>
30 #include <linux/mfd/abx500.h>
31 #include <linux/mfd/abx500/ab8500.h>
32 #include <linux/notifier.h>
34 #include "ab8500-bm.h"
35 #include "ab8500-chargalg.h"
37 /* Watchdog kick interval */
38 #define CHG_WD_INTERVAL (6 * HZ)
40 /* End-of-charge criteria counter */
41 #define EOC_COND_CNT 10
43 /* One hour expressed in seconds */
44 #define ONE_HOUR_IN_SECONDS 3600
46 /* Five minutes expressed in seconds */
47 #define FIVE_MINUTES_IN_SECONDS 300
49 #define CHARGALG_CURR_STEP_LOW 0
50 #define CHARGALG_CURR_STEP_HIGH 100
52 enum abx500_chargers {
58 struct abx500_chargalg_charger_info {
59 enum abx500_chargers conn_chg;
60 enum abx500_chargers prev_conn_chg;
61 enum abx500_chargers online_chg;
62 enum abx500_chargers prev_online_chg;
63 enum abx500_chargers charger_type;
76 struct abx500_chargalg_suspension_status {
77 bool suspended_change;
82 struct abx500_chargalg_current_step_status {
83 bool curr_step_change;
87 struct abx500_chargalg_battery_data {
95 enum abx500_chargalg_states {
98 STATE_CHG_NOT_OK_INIT,
100 STATE_HW_TEMP_PROTECT_INIT,
101 STATE_HW_TEMP_PROTECT,
104 STATE_WAIT_FOR_RECHARGE_INIT,
105 STATE_WAIT_FOR_RECHARGE,
106 STATE_MAINTENANCE_A_INIT,
108 STATE_MAINTENANCE_B_INIT,
110 STATE_TEMP_UNDEROVER_INIT,
111 STATE_TEMP_UNDEROVER,
112 STATE_TEMP_LOWHIGH_INIT,
114 STATE_SUSPENDED_INIT,
116 STATE_OVV_PROTECT_INIT,
118 STATE_SAFETY_TIMER_EXPIRED_INIT,
119 STATE_SAFETY_TIMER_EXPIRED,
120 STATE_BATT_REMOVED_INIT,
122 STATE_WD_EXPIRED_INIT,
126 static const char *states[] = {
131 "HW_TEMP_PROTECT_INIT",
135 "WAIT_FOR_RECHARGE_INIT",
137 "MAINTENANCE_A_INIT",
139 "MAINTENANCE_B_INIT",
141 "TEMP_UNDEROVER_INIT",
149 "SAFETY_TIMER_EXPIRED_INIT",
150 "SAFETY_TIMER_EXPIRED",
157 struct abx500_chargalg_events {
162 bool btemp_underover;
164 bool main_thermal_prot;
165 bool usb_thermal_prot;
168 bool usbchargernotok;
169 bool safety_timer_expired;
170 bool maintenance_timer_expired;
179 * struct abx500_charge_curr_maximization - Charger maximization parameters
180 * @original_iset: the non optimized/maximised charger current
181 * @current_iset: the charging current used at this moment
182 * @test_delta_i: the delta between the current we want to charge and the
183 current that is really going into the battery
184 * @condition_cnt: number of iterations needed before a new charger current
186 * @max_current: maximum charger current
187 * @wait_cnt: to avoid too fast current step down in case of charger
188 * voltage collapse, we insert this delay between step
190 * @level: tells in how many steps the charging current has been
193 struct abx500_charge_curr_maximization {
206 MAXIM_RET_IBAT_TOO_HIGH,
210 * struct abx500_chargalg - abx500 Charging algorithm device information
211 * @dev: pointer to the structure device
212 * @charge_status: battery operating status
213 * @eoc_cnt: counter used to determine end-of_charge
214 * @maintenance_chg: indicate if maintenance charge is active
215 * @t_hyst_norm temperature hysteresis when the temperature has been
216 * over or under normal limits
217 * @t_hyst_lowhigh temperature hysteresis when the temperature has been
218 * over or under the high or low limits
219 * @charge_state: current state of the charging algorithm
220 * @ccm charging current maximization parameters
221 * @chg_info: information about connected charger types
222 * @batt_data: data of the battery
223 * @susp_status: current charger suspension status
224 * @bm: Platform specific battery management information
225 * @curr_status: Current step status for over-current protection
226 * @parent: pointer to the struct abx500
227 * @chargalg_psy: structure that holds the battery properties exposed by
228 * the charging algorithm
229 * @events: structure for information about events triggered
230 * @chargalg_wq: work queue for running the charging algorithm
231 * @chargalg_periodic_work: work to run the charging algorithm periodically
232 * @chargalg_wd_work: work to kick the charger watchdog periodically
233 * @chargalg_work: work to run the charging algorithm instantly
234 * @safety_timer: charging safety timer
235 * @maintenance_timer: maintenance charging timer
236 * @chargalg_kobject: structure of type kobject
238 struct abx500_chargalg {
242 bool maintenance_chg;
245 enum abx500_chargalg_states charge_state;
246 struct abx500_charge_curr_maximization ccm;
247 struct abx500_chargalg_charger_info chg_info;
248 struct abx500_chargalg_battery_data batt_data;
249 struct abx500_chargalg_suspension_status susp_status;
250 struct ab8500 *parent;
251 struct abx500_chargalg_current_step_status curr_status;
252 struct abx500_bm_data *bm;
253 struct power_supply *chargalg_psy;
254 struct ux500_charger *ac_chg;
255 struct ux500_charger *usb_chg;
256 struct abx500_chargalg_events events;
257 struct workqueue_struct *chargalg_wq;
258 struct delayed_work chargalg_periodic_work;
259 struct delayed_work chargalg_wd_work;
260 struct work_struct chargalg_work;
261 struct hrtimer safety_timer;
262 struct hrtimer maintenance_timer;
263 struct kobject chargalg_kobject;
266 /*External charger prepare notifier*/
267 BLOCKING_NOTIFIER_HEAD(charger_notifier_list);
269 /* Main battery properties */
270 static enum power_supply_property abx500_chargalg_props[] = {
271 POWER_SUPPLY_PROP_STATUS,
272 POWER_SUPPLY_PROP_HEALTH,
275 struct abx500_chargalg_sysfs_entry {
276 struct attribute attr;
277 ssize_t (*show)(struct abx500_chargalg *, char *);
278 ssize_t (*store)(struct abx500_chargalg *, const char *, size_t);
282 * abx500_chargalg_safety_timer_expired() - Expiration of the safety timer
283 * @timer: pointer to the hrtimer structure
285 * This function gets called when the safety timer for the charger
288 static enum hrtimer_restart
289 abx500_chargalg_safety_timer_expired(struct hrtimer *timer)
291 struct abx500_chargalg *di = container_of(timer, struct abx500_chargalg,
293 dev_err(di->dev, "Safety timer expired\n");
294 di->events.safety_timer_expired = true;
296 /* Trigger execution of the algorithm instantly */
297 queue_work(di->chargalg_wq, &di->chargalg_work);
299 return HRTIMER_NORESTART;
303 * abx500_chargalg_maintenance_timer_expired() - Expiration of
304 * the maintenance timer
305 * @timer: pointer to the timer structure
307 * This function gets called when the maintenence timer
310 static enum hrtimer_restart
311 abx500_chargalg_maintenance_timer_expired(struct hrtimer *timer)
314 struct abx500_chargalg *di = container_of(timer, struct abx500_chargalg,
317 dev_dbg(di->dev, "Maintenance timer expired\n");
318 di->events.maintenance_timer_expired = true;
320 /* Trigger execution of the algorithm instantly */
321 queue_work(di->chargalg_wq, &di->chargalg_work);
323 return HRTIMER_NORESTART;
327 * abx500_chargalg_state_to() - Change charge state
328 * @di: pointer to the abx500_chargalg structure
330 * This function gets called when a charge state change should occur
332 static void abx500_chargalg_state_to(struct abx500_chargalg *di,
333 enum abx500_chargalg_states state)
336 "State changed: %s (From state: [%d] %s =to=> [%d] %s )\n",
337 di->charge_state == state ? "NO" : "YES",
339 states[di->charge_state],
343 di->charge_state = state;
346 static int abx500_chargalg_check_charger_enable(struct abx500_chargalg *di)
348 switch (di->charge_state) {
350 case STATE_MAINTENANCE_A:
351 case STATE_MAINTENANCE_B:
357 if (di->chg_info.charger_type & USB_CHG) {
358 return di->usb_chg->ops.check_enable(di->usb_chg,
359 di->bm->bat_type[di->bm->batt_id].normal_vol_lvl,
360 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
361 } else if ((di->chg_info.charger_type & AC_CHG) &&
362 !(di->ac_chg->external)) {
363 return di->ac_chg->ops.check_enable(di->ac_chg,
364 di->bm->bat_type[di->bm->batt_id].normal_vol_lvl,
365 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
371 * abx500_chargalg_check_charger_connection() - Check charger connection change
372 * @di: pointer to the abx500_chargalg structure
374 * This function will check if there is a change in the charger connection
375 * and change charge state accordingly. AC has precedence over USB.
377 static int abx500_chargalg_check_charger_connection(struct abx500_chargalg *di)
379 if (di->chg_info.conn_chg != di->chg_info.prev_conn_chg ||
380 di->susp_status.suspended_change) {
382 * Charger state changed or suspension
383 * has changed since last update
385 if ((di->chg_info.conn_chg & AC_CHG) &&
386 !di->susp_status.ac_suspended) {
387 dev_dbg(di->dev, "Charging source is AC\n");
388 if (di->chg_info.charger_type != AC_CHG) {
389 di->chg_info.charger_type = AC_CHG;
390 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
392 } else if ((di->chg_info.conn_chg & USB_CHG) &&
393 !di->susp_status.usb_suspended) {
394 dev_dbg(di->dev, "Charging source is USB\n");
395 di->chg_info.charger_type = USB_CHG;
396 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
397 } else if (di->chg_info.conn_chg &&
398 (di->susp_status.ac_suspended ||
399 di->susp_status.usb_suspended)) {
400 dev_dbg(di->dev, "Charging is suspended\n");
401 di->chg_info.charger_type = NO_CHG;
402 abx500_chargalg_state_to(di, STATE_SUSPENDED_INIT);
404 dev_dbg(di->dev, "Charging source is OFF\n");
405 di->chg_info.charger_type = NO_CHG;
406 abx500_chargalg_state_to(di, STATE_HANDHELD_INIT);
408 di->chg_info.prev_conn_chg = di->chg_info.conn_chg;
409 di->susp_status.suspended_change = false;
411 return di->chg_info.conn_chg;
415 * abx500_chargalg_check_current_step_status() - Check charging current
417 * @di: pointer to the abx500_chargalg structure
419 * This function will check if there is a change in the charging current step
420 * and change charge state accordingly.
422 static void abx500_chargalg_check_current_step_status
423 (struct abx500_chargalg *di)
425 if (di->curr_status.curr_step_change)
426 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
427 di->curr_status.curr_step_change = false;
431 * abx500_chargalg_start_safety_timer() - Start charging safety timer
432 * @di: pointer to the abx500_chargalg structure
434 * The safety timer is used to avoid overcharging of old or bad batteries.
435 * There are different timers for AC and USB
437 static void abx500_chargalg_start_safety_timer(struct abx500_chargalg *di)
439 /* Charger-dependent expiration time in hours*/
440 int timer_expiration = 0;
442 switch (di->chg_info.charger_type) {
444 timer_expiration = di->bm->main_safety_tmr_h;
448 timer_expiration = di->bm->usb_safety_tmr_h;
452 dev_err(di->dev, "Unknown charger to charge from\n");
456 di->events.safety_timer_expired = false;
457 hrtimer_set_expires_range(&di->safety_timer,
458 ktime_set(timer_expiration * ONE_HOUR_IN_SECONDS, 0),
459 ktime_set(FIVE_MINUTES_IN_SECONDS, 0));
460 hrtimer_start_expires(&di->safety_timer, HRTIMER_MODE_REL);
464 * abx500_chargalg_stop_safety_timer() - Stop charging safety timer
465 * @di: pointer to the abx500_chargalg structure
467 * The safety timer is stopped whenever the NORMAL state is exited
469 static void abx500_chargalg_stop_safety_timer(struct abx500_chargalg *di)
471 if (hrtimer_try_to_cancel(&di->safety_timer) >= 0)
472 di->events.safety_timer_expired = false;
476 * abx500_chargalg_start_maintenance_timer() - Start charging maintenance timer
477 * @di: pointer to the abx500_chargalg structure
478 * @duration: duration of ther maintenance timer in hours
480 * The maintenance timer is used to maintain the charge in the battery once
481 * the battery is considered full. These timers are chosen to match the
482 * discharge curve of the battery
484 static void abx500_chargalg_start_maintenance_timer(struct abx500_chargalg *di,
487 hrtimer_set_expires_range(&di->maintenance_timer,
488 ktime_set(duration * ONE_HOUR_IN_SECONDS, 0),
489 ktime_set(FIVE_MINUTES_IN_SECONDS, 0));
490 di->events.maintenance_timer_expired = false;
491 hrtimer_start_expires(&di->maintenance_timer, HRTIMER_MODE_REL);
495 * abx500_chargalg_stop_maintenance_timer() - Stop maintenance timer
496 * @di: pointer to the abx500_chargalg structure
498 * The maintenance timer is stopped whenever maintenance ends or when another
501 static void abx500_chargalg_stop_maintenance_timer(struct abx500_chargalg *di)
503 if (hrtimer_try_to_cancel(&di->maintenance_timer) >= 0)
504 di->events.maintenance_timer_expired = false;
508 * abx500_chargalg_kick_watchdog() - Kick charger watchdog
509 * @di: pointer to the abx500_chargalg structure
511 * The charger watchdog have to be kicked periodically whenever the charger is
512 * on, else the ABB will reset the system
514 static int abx500_chargalg_kick_watchdog(struct abx500_chargalg *di)
516 /* Check if charger exists and kick watchdog if charging */
517 if (di->ac_chg && di->ac_chg->ops.kick_wd &&
518 di->chg_info.online_chg & AC_CHG) {
520 * If AB charger watchdog expired, pm2xxx charging
521 * gets disabled. To be safe, kick both AB charger watchdog
522 * and pm2xxx watchdog.
524 if (di->ac_chg->external &&
525 di->usb_chg && di->usb_chg->ops.kick_wd)
526 di->usb_chg->ops.kick_wd(di->usb_chg);
528 return di->ac_chg->ops.kick_wd(di->ac_chg);
530 else if (di->usb_chg && di->usb_chg->ops.kick_wd &&
531 di->chg_info.online_chg & USB_CHG)
532 return di->usb_chg->ops.kick_wd(di->usb_chg);
538 * abx500_chargalg_ac_en() - Turn on/off the AC charger
539 * @di: pointer to the abx500_chargalg structure
540 * @enable: charger on/off
541 * @vset: requested charger output voltage
542 * @iset: requested charger output current
544 * The AC charger will be turned on/off with the requested charge voltage and
547 static int abx500_chargalg_ac_en(struct abx500_chargalg *di, int enable,
550 static int abx500_chargalg_ex_ac_enable_toggle;
552 if (!di->ac_chg || !di->ac_chg->ops.enable)
555 /* Select maximum of what both the charger and the battery supports */
556 if (di->ac_chg->max_out_volt)
557 vset = min(vset, di->ac_chg->max_out_volt);
558 if (di->ac_chg->max_out_curr)
559 iset = min(iset, di->ac_chg->max_out_curr);
561 di->chg_info.ac_iset = iset;
562 di->chg_info.ac_vset = vset;
564 /* Enable external charger */
565 if (enable && di->ac_chg->external &&
566 !abx500_chargalg_ex_ac_enable_toggle) {
567 blocking_notifier_call_chain(&charger_notifier_list,
569 abx500_chargalg_ex_ac_enable_toggle++;
572 return di->ac_chg->ops.enable(di->ac_chg, enable, vset, iset);
576 * abx500_chargalg_usb_en() - Turn on/off the USB charger
577 * @di: pointer to the abx500_chargalg structure
578 * @enable: charger on/off
579 * @vset: requested charger output voltage
580 * @iset: requested charger output current
582 * The USB charger will be turned on/off with the requested charge voltage and
585 static int abx500_chargalg_usb_en(struct abx500_chargalg *di, int enable,
588 if (!di->usb_chg || !di->usb_chg->ops.enable)
591 /* Select maximum of what both the charger and the battery supports */
592 if (di->usb_chg->max_out_volt)
593 vset = min(vset, di->usb_chg->max_out_volt);
594 if (di->usb_chg->max_out_curr)
595 iset = min(iset, di->usb_chg->max_out_curr);
597 di->chg_info.usb_iset = iset;
598 di->chg_info.usb_vset = vset;
600 return di->usb_chg->ops.enable(di->usb_chg, enable, vset, iset);
604 * abx500_chargalg_update_chg_curr() - Update charger current
605 * @di: pointer to the abx500_chargalg structure
606 * @iset: requested charger output current
608 * The charger output current will be updated for the charger
609 * that is currently in use
611 static int abx500_chargalg_update_chg_curr(struct abx500_chargalg *di,
614 /* Check if charger exists and update current if charging */
615 if (di->ac_chg && di->ac_chg->ops.update_curr &&
616 di->chg_info.charger_type & AC_CHG) {
618 * Select maximum of what both the charger
619 * and the battery supports
621 if (di->ac_chg->max_out_curr)
622 iset = min(iset, di->ac_chg->max_out_curr);
624 di->chg_info.ac_iset = iset;
626 return di->ac_chg->ops.update_curr(di->ac_chg, iset);
627 } else if (di->usb_chg && di->usb_chg->ops.update_curr &&
628 di->chg_info.charger_type & USB_CHG) {
630 * Select maximum of what both the charger
631 * and the battery supports
633 if (di->usb_chg->max_out_curr)
634 iset = min(iset, di->usb_chg->max_out_curr);
636 di->chg_info.usb_iset = iset;
638 return di->usb_chg->ops.update_curr(di->usb_chg, iset);
645 * abx500_chargalg_stop_charging() - Stop charging
646 * @di: pointer to the abx500_chargalg structure
648 * This function is called from any state where charging should be stopped.
649 * All charging is disabled and all status parameters and timers are changed
652 static void abx500_chargalg_stop_charging(struct abx500_chargalg *di)
654 abx500_chargalg_ac_en(di, false, 0, 0);
655 abx500_chargalg_usb_en(di, false, 0, 0);
656 abx500_chargalg_stop_safety_timer(di);
657 abx500_chargalg_stop_maintenance_timer(di);
658 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
659 di->maintenance_chg = false;
660 cancel_delayed_work(&di->chargalg_wd_work);
661 power_supply_changed(di->chargalg_psy);
665 * abx500_chargalg_hold_charging() - Pauses charging
666 * @di: pointer to the abx500_chargalg structure
668 * This function is called in the case where maintenance charging has been
669 * disabled and instead a battery voltage mode is entered to check when the
670 * battery voltage has reached a certain recharge voltage
672 static void abx500_chargalg_hold_charging(struct abx500_chargalg *di)
674 abx500_chargalg_ac_en(di, false, 0, 0);
675 abx500_chargalg_usb_en(di, false, 0, 0);
676 abx500_chargalg_stop_safety_timer(di);
677 abx500_chargalg_stop_maintenance_timer(di);
678 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
679 di->maintenance_chg = false;
680 cancel_delayed_work(&di->chargalg_wd_work);
681 power_supply_changed(di->chargalg_psy);
685 * abx500_chargalg_start_charging() - Start the charger
686 * @di: pointer to the abx500_chargalg structure
687 * @vset: requested charger output voltage
688 * @iset: requested charger output current
690 * A charger will be enabled depending on the requested charger type that was
691 * detected previously.
693 static void abx500_chargalg_start_charging(struct abx500_chargalg *di,
696 switch (di->chg_info.charger_type) {
699 "AC parameters: Vset %d, Ich %d\n", vset, iset);
700 abx500_chargalg_usb_en(di, false, 0, 0);
701 abx500_chargalg_ac_en(di, true, vset, iset);
706 "USB parameters: Vset %d, Ich %d\n", vset, iset);
707 abx500_chargalg_ac_en(di, false, 0, 0);
708 abx500_chargalg_usb_en(di, true, vset, iset);
712 dev_err(di->dev, "Unknown charger to charge from\n");
718 * abx500_chargalg_check_temp() - Check battery temperature ranges
719 * @di: pointer to the abx500_chargalg structure
721 * The battery temperature is checked against the predefined limits and the
722 * charge state is changed accordingly
724 static void abx500_chargalg_check_temp(struct abx500_chargalg *di)
726 if (di->batt_data.temp > (di->bm->temp_low + di->t_hyst_norm) &&
727 di->batt_data.temp < (di->bm->temp_high - di->t_hyst_norm)) {
729 di->events.btemp_underover = false;
730 di->events.btemp_lowhigh = false;
732 di->t_hyst_lowhigh = 0;
734 if (((di->batt_data.temp >= di->bm->temp_high) &&
735 (di->batt_data.temp <
736 (di->bm->temp_over - di->t_hyst_lowhigh))) ||
737 ((di->batt_data.temp >
738 (di->bm->temp_under + di->t_hyst_lowhigh)) &&
739 (di->batt_data.temp <= di->bm->temp_low))) {
740 /* TEMP minor!!!!! */
741 di->events.btemp_underover = false;
742 di->events.btemp_lowhigh = true;
743 di->t_hyst_norm = di->bm->temp_hysteresis;
744 di->t_hyst_lowhigh = 0;
745 } else if (di->batt_data.temp <= di->bm->temp_under ||
746 di->batt_data.temp >= di->bm->temp_over) {
747 /* TEMP major!!!!! */
748 di->events.btemp_underover = true;
749 di->events.btemp_lowhigh = false;
751 di->t_hyst_lowhigh = di->bm->temp_hysteresis;
753 /* Within hysteresis */
754 dev_dbg(di->dev, "Within hysteresis limit temp: %d "
755 "hyst_lowhigh %d, hyst normal %d\n",
756 di->batt_data.temp, di->t_hyst_lowhigh,
763 * abx500_chargalg_check_charger_voltage() - Check charger voltage
764 * @di: pointer to the abx500_chargalg structure
766 * Charger voltage is checked against maximum limit
768 static void abx500_chargalg_check_charger_voltage(struct abx500_chargalg *di)
770 if (di->chg_info.usb_volt > di->bm->chg_params->usb_volt_max)
771 di->chg_info.usb_chg_ok = false;
773 di->chg_info.usb_chg_ok = true;
775 if (di->chg_info.ac_volt > di->bm->chg_params->ac_volt_max)
776 di->chg_info.ac_chg_ok = false;
778 di->chg_info.ac_chg_ok = true;
783 * abx500_chargalg_end_of_charge() - Check if end-of-charge criteria is fulfilled
784 * @di: pointer to the abx500_chargalg structure
786 * End-of-charge criteria is fulfilled when the battery voltage is above a
787 * certain limit and the battery current is below a certain limit for a
788 * predefined number of consecutive seconds. If true, the battery is full
790 static void abx500_chargalg_end_of_charge(struct abx500_chargalg *di)
792 if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING &&
793 di->charge_state == STATE_NORMAL &&
794 !di->maintenance_chg && (di->batt_data.volt >=
795 di->bm->bat_type[di->bm->batt_id].termination_vol ||
796 di->events.usb_cv_active || di->events.ac_cv_active) &&
797 di->batt_data.avg_curr <
798 di->bm->bat_type[di->bm->batt_id].termination_curr &&
799 di->batt_data.avg_curr > 0) {
800 if (++di->eoc_cnt >= EOC_COND_CNT) {
802 di->charge_status = POWER_SUPPLY_STATUS_FULL;
803 di->maintenance_chg = true;
804 dev_dbg(di->dev, "EOC reached!\n");
805 power_supply_changed(di->chargalg_psy);
808 " EOC limit reached for the %d"
809 " time, out of %d before EOC\n",
818 static void init_maxim_chg_curr(struct abx500_chargalg *di)
820 di->ccm.original_iset =
821 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl;
822 di->ccm.current_iset =
823 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl;
824 di->ccm.test_delta_i = di->bm->maxi->charger_curr_step;
825 di->ccm.max_current = di->bm->maxi->chg_curr;
826 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
831 * abx500_chargalg_chg_curr_maxim - increases the charger current to
832 * compensate for the system load
833 * @di pointer to the abx500_chargalg structure
835 * This maximization function is used to raise the charger current to get the
836 * battery current as close to the optimal value as possible. The battery
837 * current during charging is affected by the system load
839 static enum maxim_ret abx500_chargalg_chg_curr_maxim(struct abx500_chargalg *di)
843 if (!di->bm->maxi->ena_maxi)
844 return MAXIM_RET_NOACTION;
846 delta_i = di->ccm.original_iset - di->batt_data.inst_curr;
848 if (di->events.vbus_collapsed) {
849 dev_dbg(di->dev, "Charger voltage has collapsed %d\n",
851 if (di->ccm.wait_cnt == 0) {
852 dev_dbg(di->dev, "lowering current\n");
854 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
855 di->ccm.max_current =
856 di->ccm.current_iset - di->ccm.test_delta_i;
857 di->ccm.current_iset = di->ccm.max_current;
859 return MAXIM_RET_CHANGE;
861 dev_dbg(di->dev, "waiting\n");
862 /* Let's go in here twice before lowering curr again */
863 di->ccm.wait_cnt = (di->ccm.wait_cnt + 1) % 3;
864 return MAXIM_RET_NOACTION;
868 di->ccm.wait_cnt = 0;
870 if ((di->batt_data.inst_curr > di->ccm.original_iset)) {
871 dev_dbg(di->dev, " Maximization Ibat (%dmA) too high"
872 " (limit %dmA) (current iset: %dmA)!\n",
873 di->batt_data.inst_curr, di->ccm.original_iset,
874 di->ccm.current_iset);
876 if (di->ccm.current_iset == di->ccm.original_iset)
877 return MAXIM_RET_NOACTION;
879 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
880 di->ccm.current_iset = di->ccm.original_iset;
883 return MAXIM_RET_IBAT_TOO_HIGH;
886 if (delta_i > di->ccm.test_delta_i &&
887 (di->ccm.current_iset + di->ccm.test_delta_i) <
888 di->ccm.max_current) {
889 if (di->ccm.condition_cnt-- == 0) {
890 /* Increse the iset with cco.test_delta_i */
891 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
892 di->ccm.current_iset += di->ccm.test_delta_i;
894 dev_dbg(di->dev, " Maximization needed, increase"
895 " with %d mA to %dmA (Optimal ibat: %d)"
897 di->ccm.test_delta_i,
898 di->ccm.current_iset,
899 di->ccm.original_iset,
901 return MAXIM_RET_CHANGE;
903 return MAXIM_RET_NOACTION;
906 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
907 return MAXIM_RET_NOACTION;
911 static void handle_maxim_chg_curr(struct abx500_chargalg *di)
916 ret = abx500_chargalg_chg_curr_maxim(di);
918 case MAXIM_RET_CHANGE:
919 result = abx500_chargalg_update_chg_curr(di,
920 di->ccm.current_iset);
922 dev_err(di->dev, "failed to set chg curr\n");
924 case MAXIM_RET_IBAT_TOO_HIGH:
925 result = abx500_chargalg_update_chg_curr(di,
926 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
928 dev_err(di->dev, "failed to set chg curr\n");
931 case MAXIM_RET_NOACTION:
938 static int abx500_chargalg_get_ext_psy_data(struct device *dev, void *data)
940 struct power_supply *psy;
941 struct power_supply *ext = dev_get_drvdata(dev);
942 const char **supplicants = (const char **)ext->supplied_to;
943 struct abx500_chargalg *di;
944 union power_supply_propval ret;
946 bool capacity_updated = false;
948 psy = (struct power_supply *)data;
949 di = power_supply_get_drvdata(psy);
950 /* For all psy where the driver name appears in any supplied_to */
951 j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
956 * If external is not registering 'POWER_SUPPLY_PROP_CAPACITY' to its
957 * property because of handling that sysfs entry on its own, this is
958 * the place to get the battery capacity.
960 if (!power_supply_get_property(ext, POWER_SUPPLY_PROP_CAPACITY, &ret)) {
961 di->batt_data.percent = ret.intval;
962 capacity_updated = true;
965 /* Go through all properties for the psy */
966 for (j = 0; j < ext->desc->num_properties; j++) {
967 enum power_supply_property prop;
968 prop = ext->desc->properties[j];
971 * Initialize chargers if not already done.
972 * The ab8500_charger*/
974 ext->desc->type == POWER_SUPPLY_TYPE_MAINS)
975 di->ac_chg = psy_to_ux500_charger(ext);
976 else if (!di->usb_chg &&
977 ext->desc->type == POWER_SUPPLY_TYPE_USB)
978 di->usb_chg = psy_to_ux500_charger(ext);
980 if (power_supply_get_property(ext, prop, &ret))
983 case POWER_SUPPLY_PROP_PRESENT:
984 switch (ext->desc->type) {
985 case POWER_SUPPLY_TYPE_BATTERY:
986 /* Battery present */
988 di->events.batt_rem = false;
989 /* Battery removed */
991 di->events.batt_rem = true;
993 case POWER_SUPPLY_TYPE_MAINS:
994 /* AC disconnected */
996 (di->chg_info.conn_chg & AC_CHG)) {
997 di->chg_info.prev_conn_chg =
998 di->chg_info.conn_chg;
999 di->chg_info.conn_chg &= ~AC_CHG;
1002 else if (ret.intval &&
1003 !(di->chg_info.conn_chg & AC_CHG)) {
1004 di->chg_info.prev_conn_chg =
1005 di->chg_info.conn_chg;
1006 di->chg_info.conn_chg |= AC_CHG;
1009 case POWER_SUPPLY_TYPE_USB:
1010 /* USB disconnected */
1012 (di->chg_info.conn_chg & USB_CHG)) {
1013 di->chg_info.prev_conn_chg =
1014 di->chg_info.conn_chg;
1015 di->chg_info.conn_chg &= ~USB_CHG;
1018 else if (ret.intval &&
1019 !(di->chg_info.conn_chg & USB_CHG)) {
1020 di->chg_info.prev_conn_chg =
1021 di->chg_info.conn_chg;
1022 di->chg_info.conn_chg |= USB_CHG;
1030 case POWER_SUPPLY_PROP_ONLINE:
1031 switch (ext->desc->type) {
1032 case POWER_SUPPLY_TYPE_BATTERY:
1034 case POWER_SUPPLY_TYPE_MAINS:
1037 (di->chg_info.online_chg & AC_CHG)) {
1038 di->chg_info.prev_online_chg =
1039 di->chg_info.online_chg;
1040 di->chg_info.online_chg &= ~AC_CHG;
1043 else if (ret.intval &&
1044 !(di->chg_info.online_chg & AC_CHG)) {
1045 di->chg_info.prev_online_chg =
1046 di->chg_info.online_chg;
1047 di->chg_info.online_chg |= AC_CHG;
1048 queue_delayed_work(di->chargalg_wq,
1049 &di->chargalg_wd_work, 0);
1052 case POWER_SUPPLY_TYPE_USB:
1055 (di->chg_info.online_chg & USB_CHG)) {
1056 di->chg_info.prev_online_chg =
1057 di->chg_info.online_chg;
1058 di->chg_info.online_chg &= ~USB_CHG;
1061 else if (ret.intval &&
1062 !(di->chg_info.online_chg & USB_CHG)) {
1063 di->chg_info.prev_online_chg =
1064 di->chg_info.online_chg;
1065 di->chg_info.online_chg |= USB_CHG;
1066 queue_delayed_work(di->chargalg_wq,
1067 &di->chargalg_wd_work, 0);
1075 case POWER_SUPPLY_PROP_HEALTH:
1076 switch (ext->desc->type) {
1077 case POWER_SUPPLY_TYPE_BATTERY:
1079 case POWER_SUPPLY_TYPE_MAINS:
1080 switch (ret.intval) {
1081 case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
1082 di->events.mainextchnotok = true;
1083 di->events.main_thermal_prot = false;
1084 di->events.main_ovv = false;
1085 di->events.ac_wd_expired = false;
1087 case POWER_SUPPLY_HEALTH_DEAD:
1088 di->events.ac_wd_expired = true;
1089 di->events.mainextchnotok = false;
1090 di->events.main_ovv = false;
1091 di->events.main_thermal_prot = false;
1093 case POWER_SUPPLY_HEALTH_COLD:
1094 case POWER_SUPPLY_HEALTH_OVERHEAT:
1095 di->events.main_thermal_prot = true;
1096 di->events.mainextchnotok = false;
1097 di->events.main_ovv = false;
1098 di->events.ac_wd_expired = false;
1100 case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
1101 di->events.main_ovv = true;
1102 di->events.mainextchnotok = false;
1103 di->events.main_thermal_prot = false;
1104 di->events.ac_wd_expired = false;
1106 case POWER_SUPPLY_HEALTH_GOOD:
1107 di->events.main_thermal_prot = false;
1108 di->events.mainextchnotok = false;
1109 di->events.main_ovv = false;
1110 di->events.ac_wd_expired = false;
1117 case POWER_SUPPLY_TYPE_USB:
1118 switch (ret.intval) {
1119 case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
1120 di->events.usbchargernotok = true;
1121 di->events.usb_thermal_prot = false;
1122 di->events.vbus_ovv = false;
1123 di->events.usb_wd_expired = false;
1125 case POWER_SUPPLY_HEALTH_DEAD:
1126 di->events.usb_wd_expired = true;
1127 di->events.usbchargernotok = false;
1128 di->events.usb_thermal_prot = false;
1129 di->events.vbus_ovv = false;
1131 case POWER_SUPPLY_HEALTH_COLD:
1132 case POWER_SUPPLY_HEALTH_OVERHEAT:
1133 di->events.usb_thermal_prot = true;
1134 di->events.usbchargernotok = false;
1135 di->events.vbus_ovv = false;
1136 di->events.usb_wd_expired = false;
1138 case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
1139 di->events.vbus_ovv = true;
1140 di->events.usbchargernotok = false;
1141 di->events.usb_thermal_prot = false;
1142 di->events.usb_wd_expired = false;
1144 case POWER_SUPPLY_HEALTH_GOOD:
1145 di->events.usbchargernotok = false;
1146 di->events.usb_thermal_prot = false;
1147 di->events.vbus_ovv = false;
1148 di->events.usb_wd_expired = false;
1159 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1160 switch (ext->desc->type) {
1161 case POWER_SUPPLY_TYPE_BATTERY:
1162 di->batt_data.volt = ret.intval / 1000;
1164 case POWER_SUPPLY_TYPE_MAINS:
1165 di->chg_info.ac_volt = ret.intval / 1000;
1167 case POWER_SUPPLY_TYPE_USB:
1168 di->chg_info.usb_volt = ret.intval / 1000;
1175 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
1176 switch (ext->desc->type) {
1177 case POWER_SUPPLY_TYPE_MAINS:
1178 /* AVG is used to indicate when we are
1181 di->events.ac_cv_active = true;
1183 di->events.ac_cv_active = false;
1186 case POWER_SUPPLY_TYPE_USB:
1187 /* AVG is used to indicate when we are
1190 di->events.usb_cv_active = true;
1192 di->events.usb_cv_active = false;
1200 case POWER_SUPPLY_PROP_TECHNOLOGY:
1201 switch (ext->desc->type) {
1202 case POWER_SUPPLY_TYPE_BATTERY:
1204 di->events.batt_unknown = false;
1206 di->events.batt_unknown = true;
1214 case POWER_SUPPLY_PROP_TEMP:
1215 di->batt_data.temp = ret.intval / 10;
1218 case POWER_SUPPLY_PROP_CURRENT_NOW:
1219 switch (ext->desc->type) {
1220 case POWER_SUPPLY_TYPE_MAINS:
1221 di->chg_info.ac_curr =
1224 case POWER_SUPPLY_TYPE_USB:
1225 di->chg_info.usb_curr =
1228 case POWER_SUPPLY_TYPE_BATTERY:
1229 di->batt_data.inst_curr = ret.intval / 1000;
1236 case POWER_SUPPLY_PROP_CURRENT_AVG:
1237 switch (ext->desc->type) {
1238 case POWER_SUPPLY_TYPE_BATTERY:
1239 di->batt_data.avg_curr = ret.intval / 1000;
1241 case POWER_SUPPLY_TYPE_USB:
1243 di->events.vbus_collapsed = true;
1245 di->events.vbus_collapsed = false;
1251 case POWER_SUPPLY_PROP_CAPACITY:
1252 if (!capacity_updated)
1253 di->batt_data.percent = ret.intval;
1263 * abx500_chargalg_external_power_changed() - callback for power supply changes
1264 * @psy: pointer to the structure power_supply
1266 * This function is the entry point of the pointer external_power_changed
1267 * of the structure power_supply.
1268 * This function gets executed when there is a change in any external power
1269 * supply that this driver needs to be notified of.
1271 static void abx500_chargalg_external_power_changed(struct power_supply *psy)
1273 struct abx500_chargalg *di = power_supply_get_drvdata(psy);
1276 * Trigger execution of the algorithm instantly and read
1277 * all power_supply properties there instead
1279 queue_work(di->chargalg_wq, &di->chargalg_work);
1283 * abx500_chargalg_algorithm() - Main function for the algorithm
1284 * @di: pointer to the abx500_chargalg structure
1286 * This is the main control function for the charging algorithm.
1287 * It is called periodically or when something happens that will
1288 * trigger a state change
1290 static void abx500_chargalg_algorithm(struct abx500_chargalg *di)
1296 /* Collect data from all power_supply class devices */
1297 class_for_each_device(power_supply_class, NULL,
1298 di->chargalg_psy, abx500_chargalg_get_ext_psy_data);
1300 abx500_chargalg_end_of_charge(di);
1301 abx500_chargalg_check_temp(di);
1302 abx500_chargalg_check_charger_voltage(di);
1304 charger_status = abx500_chargalg_check_charger_connection(di);
1305 abx500_chargalg_check_current_step_status(di);
1307 if (is_ab8500(di->parent)) {
1308 ret = abx500_chargalg_check_charger_enable(di);
1310 dev_err(di->dev, "Checking charger is enabled error"
1311 ": Returned Value %d\n", ret);
1315 * First check if we have a charger connected.
1316 * Also we don't allow charging of unknown batteries if configured
1319 if (!charger_status ||
1320 (di->events.batt_unknown && !di->bm->chg_unknown_bat)) {
1321 if (di->charge_state != STATE_HANDHELD) {
1322 di->events.safety_timer_expired = false;
1323 abx500_chargalg_state_to(di, STATE_HANDHELD_INIT);
1327 /* If suspended, we should not continue checking the flags */
1328 else if (di->charge_state == STATE_SUSPENDED_INIT ||
1329 di->charge_state == STATE_SUSPENDED) {
1330 /* We don't do anything here, just don,t continue */
1333 /* Safety timer expiration */
1334 else if (di->events.safety_timer_expired) {
1335 if (di->charge_state != STATE_SAFETY_TIMER_EXPIRED)
1336 abx500_chargalg_state_to(di,
1337 STATE_SAFETY_TIMER_EXPIRED_INIT);
1340 * Check if any interrupts has occured
1341 * that will prevent us from charging
1344 /* Battery removed */
1345 else if (di->events.batt_rem) {
1346 if (di->charge_state != STATE_BATT_REMOVED)
1347 abx500_chargalg_state_to(di, STATE_BATT_REMOVED_INIT);
1349 /* Main or USB charger not ok. */
1350 else if (di->events.mainextchnotok || di->events.usbchargernotok) {
1352 * If vbus_collapsed is set, we have to lower the charger
1353 * current, which is done in the normal state below
1355 if (di->charge_state != STATE_CHG_NOT_OK &&
1356 !di->events.vbus_collapsed)
1357 abx500_chargalg_state_to(di, STATE_CHG_NOT_OK_INIT);
1359 /* VBUS, Main or VBAT OVV. */
1360 else if (di->events.vbus_ovv ||
1361 di->events.main_ovv ||
1362 di->events.batt_ovv ||
1363 !di->chg_info.usb_chg_ok ||
1364 !di->chg_info.ac_chg_ok) {
1365 if (di->charge_state != STATE_OVV_PROTECT)
1366 abx500_chargalg_state_to(di, STATE_OVV_PROTECT_INIT);
1368 /* USB Thermal, stop charging */
1369 else if (di->events.main_thermal_prot ||
1370 di->events.usb_thermal_prot) {
1371 if (di->charge_state != STATE_HW_TEMP_PROTECT)
1372 abx500_chargalg_state_to(di,
1373 STATE_HW_TEMP_PROTECT_INIT);
1375 /* Battery temp over/under */
1376 else if (di->events.btemp_underover) {
1377 if (di->charge_state != STATE_TEMP_UNDEROVER)
1378 abx500_chargalg_state_to(di,
1379 STATE_TEMP_UNDEROVER_INIT);
1381 /* Watchdog expired */
1382 else if (di->events.ac_wd_expired ||
1383 di->events.usb_wd_expired) {
1384 if (di->charge_state != STATE_WD_EXPIRED)
1385 abx500_chargalg_state_to(di, STATE_WD_EXPIRED_INIT);
1387 /* Battery temp high/low */
1388 else if (di->events.btemp_lowhigh) {
1389 if (di->charge_state != STATE_TEMP_LOWHIGH)
1390 abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH_INIT);
1394 "[CHARGALG] Vb %d Ib_avg %d Ib_inst %d Tb %d Cap %d Maint %d "
1395 "State %s Active_chg %d Chg_status %d AC %d USB %d "
1396 "AC_online %d USB_online %d AC_CV %d USB_CV %d AC_I %d "
1397 "USB_I %d AC_Vset %d AC_Iset %d USB_Vset %d USB_Iset %d\n",
1399 di->batt_data.avg_curr,
1400 di->batt_data.inst_curr,
1402 di->batt_data.percent,
1403 di->maintenance_chg,
1404 states[di->charge_state],
1405 di->chg_info.charger_type,
1407 di->chg_info.conn_chg & AC_CHG,
1408 di->chg_info.conn_chg & USB_CHG,
1409 di->chg_info.online_chg & AC_CHG,
1410 di->chg_info.online_chg & USB_CHG,
1411 di->events.ac_cv_active,
1412 di->events.usb_cv_active,
1413 di->chg_info.ac_curr,
1414 di->chg_info.usb_curr,
1415 di->chg_info.ac_vset,
1416 di->chg_info.ac_iset,
1417 di->chg_info.usb_vset,
1418 di->chg_info.usb_iset);
1420 switch (di->charge_state) {
1421 case STATE_HANDHELD_INIT:
1422 abx500_chargalg_stop_charging(di);
1423 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
1424 abx500_chargalg_state_to(di, STATE_HANDHELD);
1427 case STATE_HANDHELD:
1430 case STATE_SUSPENDED_INIT:
1431 if (di->susp_status.ac_suspended)
1432 abx500_chargalg_ac_en(di, false, 0, 0);
1433 if (di->susp_status.usb_suspended)
1434 abx500_chargalg_usb_en(di, false, 0, 0);
1435 abx500_chargalg_stop_safety_timer(di);
1436 abx500_chargalg_stop_maintenance_timer(di);
1437 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1438 di->maintenance_chg = false;
1439 abx500_chargalg_state_to(di, STATE_SUSPENDED);
1440 power_supply_changed(di->chargalg_psy);
1443 case STATE_SUSPENDED:
1444 /* CHARGING is suspended */
1447 case STATE_BATT_REMOVED_INIT:
1448 abx500_chargalg_stop_charging(di);
1449 abx500_chargalg_state_to(di, STATE_BATT_REMOVED);
1452 case STATE_BATT_REMOVED:
1453 if (!di->events.batt_rem)
1454 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1457 case STATE_HW_TEMP_PROTECT_INIT:
1458 abx500_chargalg_stop_charging(di);
1459 abx500_chargalg_state_to(di, STATE_HW_TEMP_PROTECT);
1462 case STATE_HW_TEMP_PROTECT:
1463 if (!di->events.main_thermal_prot &&
1464 !di->events.usb_thermal_prot)
1465 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1468 case STATE_OVV_PROTECT_INIT:
1469 abx500_chargalg_stop_charging(di);
1470 abx500_chargalg_state_to(di, STATE_OVV_PROTECT);
1473 case STATE_OVV_PROTECT:
1474 if (!di->events.vbus_ovv &&
1475 !di->events.main_ovv &&
1476 !di->events.batt_ovv &&
1477 di->chg_info.usb_chg_ok &&
1478 di->chg_info.ac_chg_ok)
1479 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1482 case STATE_CHG_NOT_OK_INIT:
1483 abx500_chargalg_stop_charging(di);
1484 abx500_chargalg_state_to(di, STATE_CHG_NOT_OK);
1487 case STATE_CHG_NOT_OK:
1488 if (!di->events.mainextchnotok &&
1489 !di->events.usbchargernotok)
1490 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1493 case STATE_SAFETY_TIMER_EXPIRED_INIT:
1494 abx500_chargalg_stop_charging(di);
1495 abx500_chargalg_state_to(di, STATE_SAFETY_TIMER_EXPIRED);
1498 case STATE_SAFETY_TIMER_EXPIRED:
1499 /* We exit this state when charger is removed */
1502 case STATE_NORMAL_INIT:
1503 if (di->curr_status.curr_step == CHARGALG_CURR_STEP_LOW)
1504 abx500_chargalg_stop_charging(di);
1506 curr_step_lvl = di->bm->bat_type[
1507 di->bm->batt_id].normal_cur_lvl
1508 * di->curr_status.curr_step
1509 / CHARGALG_CURR_STEP_HIGH;
1510 abx500_chargalg_start_charging(di,
1511 di->bm->bat_type[di->bm->batt_id]
1512 .normal_vol_lvl, curr_step_lvl);
1515 abx500_chargalg_state_to(di, STATE_NORMAL);
1516 abx500_chargalg_start_safety_timer(di);
1517 abx500_chargalg_stop_maintenance_timer(di);
1518 init_maxim_chg_curr(di);
1519 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
1521 di->maintenance_chg = false;
1522 power_supply_changed(di->chargalg_psy);
1527 handle_maxim_chg_curr(di);
1528 if (di->charge_status == POWER_SUPPLY_STATUS_FULL &&
1529 di->maintenance_chg) {
1530 if (di->bm->no_maintenance)
1531 abx500_chargalg_state_to(di,
1532 STATE_WAIT_FOR_RECHARGE_INIT);
1534 abx500_chargalg_state_to(di,
1535 STATE_MAINTENANCE_A_INIT);
1539 /* This state will be used when the maintenance state is disabled */
1540 case STATE_WAIT_FOR_RECHARGE_INIT:
1541 abx500_chargalg_hold_charging(di);
1542 abx500_chargalg_state_to(di, STATE_WAIT_FOR_RECHARGE);
1545 case STATE_WAIT_FOR_RECHARGE:
1546 if (di->batt_data.percent <=
1547 di->bm->bat_type[di->bm->batt_id].
1549 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1552 case STATE_MAINTENANCE_A_INIT:
1553 abx500_chargalg_stop_safety_timer(di);
1554 abx500_chargalg_start_maintenance_timer(di,
1556 di->bm->batt_id].maint_a_chg_timer_h);
1557 abx500_chargalg_start_charging(di,
1559 di->bm->batt_id].maint_a_vol_lvl,
1561 di->bm->batt_id].maint_a_cur_lvl);
1562 abx500_chargalg_state_to(di, STATE_MAINTENANCE_A);
1563 power_supply_changed(di->chargalg_psy);
1566 case STATE_MAINTENANCE_A:
1567 if (di->events.maintenance_timer_expired) {
1568 abx500_chargalg_stop_maintenance_timer(di);
1569 abx500_chargalg_state_to(di, STATE_MAINTENANCE_B_INIT);
1573 case STATE_MAINTENANCE_B_INIT:
1574 abx500_chargalg_start_maintenance_timer(di,
1576 di->bm->batt_id].maint_b_chg_timer_h);
1577 abx500_chargalg_start_charging(di,
1579 di->bm->batt_id].maint_b_vol_lvl,
1581 di->bm->batt_id].maint_b_cur_lvl);
1582 abx500_chargalg_state_to(di, STATE_MAINTENANCE_B);
1583 power_supply_changed(di->chargalg_psy);
1586 case STATE_MAINTENANCE_B:
1587 if (di->events.maintenance_timer_expired) {
1588 abx500_chargalg_stop_maintenance_timer(di);
1589 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1593 case STATE_TEMP_LOWHIGH_INIT:
1594 abx500_chargalg_start_charging(di,
1596 di->bm->batt_id].low_high_vol_lvl,
1598 di->bm->batt_id].low_high_cur_lvl);
1599 abx500_chargalg_stop_maintenance_timer(di);
1600 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
1601 abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH);
1602 power_supply_changed(di->chargalg_psy);
1605 case STATE_TEMP_LOWHIGH:
1606 if (!di->events.btemp_lowhigh)
1607 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1610 case STATE_WD_EXPIRED_INIT:
1611 abx500_chargalg_stop_charging(di);
1612 abx500_chargalg_state_to(di, STATE_WD_EXPIRED);
1615 case STATE_WD_EXPIRED:
1616 if (!di->events.ac_wd_expired &&
1617 !di->events.usb_wd_expired)
1618 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1621 case STATE_TEMP_UNDEROVER_INIT:
1622 abx500_chargalg_stop_charging(di);
1623 abx500_chargalg_state_to(di, STATE_TEMP_UNDEROVER);
1626 case STATE_TEMP_UNDEROVER:
1627 if (!di->events.btemp_underover)
1628 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1632 /* Start charging directly if the new state is a charge state */
1633 if (di->charge_state == STATE_NORMAL_INIT ||
1634 di->charge_state == STATE_MAINTENANCE_A_INIT ||
1635 di->charge_state == STATE_MAINTENANCE_B_INIT)
1636 queue_work(di->chargalg_wq, &di->chargalg_work);
1640 * abx500_chargalg_periodic_work() - Periodic work for the algorithm
1641 * @work: pointer to the work_struct structure
1643 * Work queue function for the charging algorithm
1645 static void abx500_chargalg_periodic_work(struct work_struct *work)
1647 struct abx500_chargalg *di = container_of(work,
1648 struct abx500_chargalg, chargalg_periodic_work.work);
1650 abx500_chargalg_algorithm(di);
1653 * If a charger is connected then the battery has to be monitored
1654 * frequently, else the work can be delayed.
1656 if (di->chg_info.conn_chg)
1657 queue_delayed_work(di->chargalg_wq,
1658 &di->chargalg_periodic_work,
1659 di->bm->interval_charging * HZ);
1661 queue_delayed_work(di->chargalg_wq,
1662 &di->chargalg_periodic_work,
1663 di->bm->interval_not_charging * HZ);
1667 * abx500_chargalg_wd_work() - periodic work to kick the charger watchdog
1668 * @work: pointer to the work_struct structure
1670 * Work queue function for kicking the charger watchdog
1672 static void abx500_chargalg_wd_work(struct work_struct *work)
1675 struct abx500_chargalg *di = container_of(work,
1676 struct abx500_chargalg, chargalg_wd_work.work);
1678 dev_dbg(di->dev, "abx500_chargalg_wd_work\n");
1680 ret = abx500_chargalg_kick_watchdog(di);
1682 dev_err(di->dev, "failed to kick watchdog\n");
1684 queue_delayed_work(di->chargalg_wq,
1685 &di->chargalg_wd_work, CHG_WD_INTERVAL);
1689 * abx500_chargalg_work() - Work to run the charging algorithm instantly
1690 * @work: pointer to the work_struct structure
1692 * Work queue function for calling the charging algorithm
1694 static void abx500_chargalg_work(struct work_struct *work)
1696 struct abx500_chargalg *di = container_of(work,
1697 struct abx500_chargalg, chargalg_work);
1699 abx500_chargalg_algorithm(di);
1703 * abx500_chargalg_get_property() - get the chargalg properties
1704 * @psy: pointer to the power_supply structure
1705 * @psp: pointer to the power_supply_property structure
1706 * @val: pointer to the power_supply_propval union
1708 * This function gets called when an application tries to get the
1709 * chargalg properties by reading the sysfs files.
1710 * status: charging/discharging/full/unknown
1711 * health: health of the battery
1712 * Returns error code in case of failure else 0 on success
1714 static int abx500_chargalg_get_property(struct power_supply *psy,
1715 enum power_supply_property psp,
1716 union power_supply_propval *val)
1718 struct abx500_chargalg *di = power_supply_get_drvdata(psy);
1721 case POWER_SUPPLY_PROP_STATUS:
1722 val->intval = di->charge_status;
1724 case POWER_SUPPLY_PROP_HEALTH:
1725 if (di->events.batt_ovv) {
1726 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
1727 } else if (di->events.btemp_underover) {
1728 if (di->batt_data.temp <= di->bm->temp_under)
1729 val->intval = POWER_SUPPLY_HEALTH_COLD;
1731 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
1732 } else if (di->charge_state == STATE_SAFETY_TIMER_EXPIRED ||
1733 di->charge_state == STATE_SAFETY_TIMER_EXPIRED_INIT) {
1734 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
1736 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1745 /* Exposure to the sysfs interface */
1747 static ssize_t abx500_chargalg_curr_step_show(struct abx500_chargalg *di,
1750 return sprintf(buf, "%d\n", di->curr_status.curr_step);
1753 static ssize_t abx500_chargalg_curr_step_store(struct abx500_chargalg *di,
1754 const char *buf, size_t length)
1759 ret = kstrtol(buf, 10, ¶m);
1763 di->curr_status.curr_step = param;
1764 if (di->curr_status.curr_step >= CHARGALG_CURR_STEP_LOW &&
1765 di->curr_status.curr_step <= CHARGALG_CURR_STEP_HIGH) {
1766 di->curr_status.curr_step_change = true;
1767 queue_work(di->chargalg_wq, &di->chargalg_work);
1769 dev_info(di->dev, "Wrong current step\n"
1770 "Enter 0. Disable AC/USB Charging\n"
1771 "1--100. Set AC/USB charging current step\n"
1772 "100. Enable AC/USB Charging\n");
1778 static ssize_t abx500_chargalg_en_show(struct abx500_chargalg *di,
1781 return sprintf(buf, "%d\n",
1782 di->susp_status.ac_suspended &&
1783 di->susp_status.usb_suspended);
1786 static ssize_t abx500_chargalg_en_store(struct abx500_chargalg *di,
1787 const char *buf, size_t length)
1793 ret = kstrtol(buf, 10, ¶m);
1800 /* Disable charging */
1801 di->susp_status.ac_suspended = true;
1802 di->susp_status.usb_suspended = true;
1803 di->susp_status.suspended_change = true;
1804 /* Trigger a state change */
1805 queue_work(di->chargalg_wq,
1806 &di->chargalg_work);
1809 /* Enable AC Charging */
1810 di->susp_status.ac_suspended = false;
1811 di->susp_status.suspended_change = true;
1812 /* Trigger a state change */
1813 queue_work(di->chargalg_wq,
1814 &di->chargalg_work);
1817 /* Enable USB charging */
1818 di->susp_status.usb_suspended = false;
1819 di->susp_status.suspended_change = true;
1820 /* Trigger a state change */
1821 queue_work(di->chargalg_wq,
1822 &di->chargalg_work);
1825 dev_info(di->dev, "Wrong input\n"
1826 "Enter 0. Disable AC/USB Charging\n"
1827 "1. Enable AC charging\n"
1828 "2. Enable USB Charging\n");
1833 static struct abx500_chargalg_sysfs_entry abx500_chargalg_en_charger =
1834 __ATTR(chargalg, 0644, abx500_chargalg_en_show,
1835 abx500_chargalg_en_store);
1837 static struct abx500_chargalg_sysfs_entry abx500_chargalg_curr_step =
1838 __ATTR(chargalg_curr_step, 0644, abx500_chargalg_curr_step_show,
1839 abx500_chargalg_curr_step_store);
1841 static ssize_t abx500_chargalg_sysfs_show(struct kobject *kobj,
1842 struct attribute *attr, char *buf)
1844 struct abx500_chargalg_sysfs_entry *entry = container_of(attr,
1845 struct abx500_chargalg_sysfs_entry, attr);
1847 struct abx500_chargalg *di = container_of(kobj,
1848 struct abx500_chargalg, chargalg_kobject);
1853 return entry->show(di, buf);
1856 static ssize_t abx500_chargalg_sysfs_charger(struct kobject *kobj,
1857 struct attribute *attr, const char *buf, size_t length)
1859 struct abx500_chargalg_sysfs_entry *entry = container_of(attr,
1860 struct abx500_chargalg_sysfs_entry, attr);
1862 struct abx500_chargalg *di = container_of(kobj,
1863 struct abx500_chargalg, chargalg_kobject);
1868 return entry->store(di, buf, length);
1871 static struct attribute *abx500_chargalg_chg[] = {
1872 &abx500_chargalg_en_charger.attr,
1873 &abx500_chargalg_curr_step.attr,
1877 static const struct sysfs_ops abx500_chargalg_sysfs_ops = {
1878 .show = abx500_chargalg_sysfs_show,
1879 .store = abx500_chargalg_sysfs_charger,
1882 static struct kobj_type abx500_chargalg_ktype = {
1883 .sysfs_ops = &abx500_chargalg_sysfs_ops,
1884 .default_attrs = abx500_chargalg_chg,
1888 * abx500_chargalg_sysfs_exit() - de-init of sysfs entry
1889 * @di: pointer to the struct abx500_chargalg
1891 * This function removes the entry in sysfs.
1893 static void abx500_chargalg_sysfs_exit(struct abx500_chargalg *di)
1895 kobject_del(&di->chargalg_kobject);
1899 * abx500_chargalg_sysfs_init() - init of sysfs entry
1900 * @di: pointer to the struct abx500_chargalg
1902 * This function adds an entry in sysfs.
1903 * Returns error code in case of failure else 0(on success)
1905 static int abx500_chargalg_sysfs_init(struct abx500_chargalg *di)
1909 ret = kobject_init_and_add(&di->chargalg_kobject,
1910 &abx500_chargalg_ktype,
1911 NULL, "abx500_chargalg");
1913 dev_err(di->dev, "failed to create sysfs entry\n");
1917 /* Exposure to the sysfs interface <<END>> */
1919 static int __maybe_unused abx500_chargalg_resume(struct device *dev)
1921 struct abx500_chargalg *di = dev_get_drvdata(dev);
1923 /* Kick charger watchdog if charging (any charger online) */
1924 if (di->chg_info.online_chg)
1925 queue_delayed_work(di->chargalg_wq, &di->chargalg_wd_work, 0);
1928 * Run the charging algorithm directly to be sure we don't
1931 queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
1936 static int __maybe_unused abx500_chargalg_suspend(struct device *dev)
1938 struct abx500_chargalg *di = dev_get_drvdata(dev);
1940 if (di->chg_info.online_chg)
1941 cancel_delayed_work_sync(&di->chargalg_wd_work);
1943 cancel_delayed_work_sync(&di->chargalg_periodic_work);
1948 static char *supply_interface[] = {
1952 static const struct power_supply_desc abx500_chargalg_desc = {
1953 .name = "abx500_chargalg",
1954 .type = POWER_SUPPLY_TYPE_BATTERY,
1955 .properties = abx500_chargalg_props,
1956 .num_properties = ARRAY_SIZE(abx500_chargalg_props),
1957 .get_property = abx500_chargalg_get_property,
1958 .external_power_changed = abx500_chargalg_external_power_changed,
1961 static int abx500_chargalg_bind(struct device *dev, struct device *master,
1964 struct abx500_chargalg *di = dev_get_drvdata(dev);
1966 /* Create a work queue for the chargalg */
1967 di->chargalg_wq = alloc_ordered_workqueue("abx500_chargalg_wq",
1969 if (di->chargalg_wq == NULL) {
1970 dev_err(di->dev, "failed to create work queue\n");
1974 /* Run the charging algorithm */
1975 queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
1980 static void abx500_chargalg_unbind(struct device *dev, struct device *master,
1983 struct abx500_chargalg *di = dev_get_drvdata(dev);
1985 /* Stop all timers and work */
1986 hrtimer_cancel(&di->safety_timer);
1987 hrtimer_cancel(&di->maintenance_timer);
1989 cancel_delayed_work_sync(&di->chargalg_periodic_work);
1990 cancel_delayed_work_sync(&di->chargalg_wd_work);
1991 cancel_work_sync(&di->chargalg_work);
1993 /* Delete the work queue */
1994 destroy_workqueue(di->chargalg_wq);
1995 flush_scheduled_work();
1998 static const struct component_ops abx500_chargalg_component_ops = {
1999 .bind = abx500_chargalg_bind,
2000 .unbind = abx500_chargalg_unbind,
2003 static int abx500_chargalg_probe(struct platform_device *pdev)
2005 struct device *dev = &pdev->dev;
2006 struct power_supply_config psy_cfg = {};
2007 struct abx500_chargalg *di;
2010 di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL);
2014 di->bm = &ab8500_bm_data;
2016 /* get device struct and parent */
2018 di->parent = dev_get_drvdata(pdev->dev.parent);
2020 psy_cfg.supplied_to = supply_interface;
2021 psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
2022 psy_cfg.drv_data = di;
2024 /* Initilialize safety timer */
2025 hrtimer_init(&di->safety_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
2026 di->safety_timer.function = abx500_chargalg_safety_timer_expired;
2028 /* Initilialize maintenance timer */
2029 hrtimer_init(&di->maintenance_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
2030 di->maintenance_timer.function =
2031 abx500_chargalg_maintenance_timer_expired;
2033 /* Init work for chargalg */
2034 INIT_DEFERRABLE_WORK(&di->chargalg_periodic_work,
2035 abx500_chargalg_periodic_work);
2036 INIT_DEFERRABLE_WORK(&di->chargalg_wd_work,
2037 abx500_chargalg_wd_work);
2039 /* Init work for chargalg */
2040 INIT_WORK(&di->chargalg_work, abx500_chargalg_work);
2042 /* To detect charger at startup */
2043 di->chg_info.prev_conn_chg = -1;
2045 /* Register chargalg power supply class */
2046 di->chargalg_psy = devm_power_supply_register(di->dev,
2047 &abx500_chargalg_desc,
2049 if (IS_ERR(di->chargalg_psy)) {
2050 dev_err(di->dev, "failed to register chargalg psy\n");
2051 return PTR_ERR(di->chargalg_psy);
2054 platform_set_drvdata(pdev, di);
2056 /* sysfs interface to enable/disable charging from user space */
2057 ret = abx500_chargalg_sysfs_init(di);
2059 dev_err(di->dev, "failed to create sysfs entry\n");
2062 di->curr_status.curr_step = CHARGALG_CURR_STEP_HIGH;
2064 dev_info(di->dev, "probe success\n");
2065 return component_add(dev, &abx500_chargalg_component_ops);
2068 static int abx500_chargalg_remove(struct platform_device *pdev)
2070 struct abx500_chargalg *di = platform_get_drvdata(pdev);
2072 component_del(&pdev->dev, &abx500_chargalg_component_ops);
2074 /* sysfs interface to enable/disable charging from user space */
2075 abx500_chargalg_sysfs_exit(di);
2080 static SIMPLE_DEV_PM_OPS(abx500_chargalg_pm_ops, abx500_chargalg_suspend, abx500_chargalg_resume);
2082 static const struct of_device_id ab8500_chargalg_match[] = {
2083 { .compatible = "stericsson,ab8500-chargalg", },
2087 struct platform_driver abx500_chargalg_driver = {
2088 .probe = abx500_chargalg_probe,
2089 .remove = abx500_chargalg_remove,
2091 .name = "ab8500-chargalg",
2092 .of_match_table = ab8500_chargalg_match,
2093 .pm = &abx500_chargalg_pm_ops,
2096 MODULE_LICENSE("GPL v2");
2097 MODULE_AUTHOR("Johan Palsson, Karl Komierowski");
2098 MODULE_ALIAS("platform:abx500-chargalg");
2099 MODULE_DESCRIPTION("abx500 battery charging algorithm");