2 * Copyright (C) ST-Ericsson SA 2012
3 * Copyright (c) 2012 Sony Mobile Communications AB
5 * Charging algorithm driver for abx500 variants
7 * License Terms: GNU General Public License v2
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/hrtimer.h>
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>
28 #include <linux/mfd/core.h>
29 #include <linux/mfd/abx500.h>
30 #include <linux/mfd/abx500/ab8500.h>
31 #include <linux/mfd/abx500/ux500_chargalg.h>
32 #include <linux/mfd/abx500/ab8500-bm.h>
33 #include <linux/notifier.h>
35 /* Watchdog kick interval */
36 #define CHG_WD_INTERVAL (6 * HZ)
38 /* End-of-charge criteria counter */
39 #define EOC_COND_CNT 10
41 /* One hour expressed in seconds */
42 #define ONE_HOUR_IN_SECONDS 3600
44 /* Five minutes expressed in seconds */
45 #define FIVE_MINUTES_IN_SECONDS 300
47 /* Plus margin for the low battery threshold */
48 #define BAT_PLUS_MARGIN (100)
50 #define CHARGALG_CURR_STEP_LOW 0
51 #define CHARGALG_CURR_STEP_HIGH 100
53 #define to_abx500_chargalg_device_info(x) container_of((x), \
54 struct abx500_chargalg, chargalg_psy);
56 enum abx500_chargers {
62 struct abx500_chargalg_charger_info {
63 enum abx500_chargers conn_chg;
64 enum abx500_chargers prev_conn_chg;
65 enum abx500_chargers online_chg;
66 enum abx500_chargers prev_online_chg;
67 enum abx500_chargers charger_type;
80 struct abx500_chargalg_suspension_status {
81 bool suspended_change;
86 struct abx500_chargalg_current_step_status {
87 bool curr_step_change;
91 struct abx500_chargalg_battery_data {
99 enum abx500_chargalg_states {
102 STATE_CHG_NOT_OK_INIT,
104 STATE_HW_TEMP_PROTECT_INIT,
105 STATE_HW_TEMP_PROTECT,
107 STATE_USB_PP_PRE_CHARGE,
109 STATE_WAIT_FOR_RECHARGE_INIT,
110 STATE_WAIT_FOR_RECHARGE,
111 STATE_MAINTENANCE_A_INIT,
113 STATE_MAINTENANCE_B_INIT,
115 STATE_TEMP_UNDEROVER_INIT,
116 STATE_TEMP_UNDEROVER,
117 STATE_TEMP_LOWHIGH_INIT,
119 STATE_SUSPENDED_INIT,
121 STATE_OVV_PROTECT_INIT,
123 STATE_SAFETY_TIMER_EXPIRED_INIT,
124 STATE_SAFETY_TIMER_EXPIRED,
125 STATE_BATT_REMOVED_INIT,
127 STATE_WD_EXPIRED_INIT,
131 static const char *states[] = {
136 "HW_TEMP_PROTECT_INIT",
141 "WAIT_FOR_RECHARGE_INIT",
143 "MAINTENANCE_A_INIT",
145 "MAINTENANCE_B_INIT",
147 "TEMP_UNDEROVER_INIT",
155 "SAFETY_TIMER_EXPIRED_INIT",
156 "SAFETY_TIMER_EXPIRED",
163 struct abx500_chargalg_events {
168 bool btemp_underover;
170 bool main_thermal_prot;
171 bool usb_thermal_prot;
174 bool usbchargernotok;
175 bool safety_timer_expired;
176 bool maintenance_timer_expired;
185 * struct abx500_charge_curr_maximization - Charger maximization parameters
186 * @original_iset: the non optimized/maximised charger current
187 * @current_iset: the charging current used at this moment
188 * @test_delta_i: the delta between the current we want to charge and the
189 current that is really going into the battery
190 * @condition_cnt: number of iterations needed before a new charger current
192 * @max_current: maximum charger current
193 * @wait_cnt: to avoid too fast current step down in case of charger
194 * voltage collapse, we insert this delay between step
196 * @level: tells in how many steps the charging current has been
199 struct abx500_charge_curr_maximization {
212 MAXIM_RET_IBAT_TOO_HIGH,
216 * struct abx500_chargalg - abx500 Charging algorithm device information
217 * @dev: pointer to the structure device
218 * @charge_status: battery operating status
219 * @eoc_cnt: counter used to determine end-of_charge
220 * @maintenance_chg: indicate if maintenance charge is active
221 * @t_hyst_norm temperature hysteresis when the temperature has been
222 * over or under normal limits
223 * @t_hyst_lowhigh temperature hysteresis when the temperature has been
224 * over or under the high or low limits
225 * @charge_state: current state of the charging algorithm
226 * @ccm charging current maximization parameters
227 * @chg_info: information about connected charger types
228 * @batt_data: data of the battery
229 * @susp_status: current charger suspension status
230 * @bm: Platform specific battery management information
231 * @curr_status: Current step status for over-current protection
232 * @parent: pointer to the struct abx500
233 * @chargalg_psy: structure that holds the battery properties exposed by
234 * the charging algorithm
235 * @events: structure for information about events triggered
236 * @chargalg_wq: work queue for running the charging algorithm
237 * @chargalg_periodic_work: work to run the charging algorithm periodically
238 * @chargalg_wd_work: work to kick the charger watchdog periodically
239 * @chargalg_work: work to run the charging algorithm instantly
240 * @safety_timer: charging safety timer
241 * @maintenance_timer: maintenance charging timer
242 * @chargalg_kobject: structure of type kobject
244 struct abx500_chargalg {
248 bool maintenance_chg;
251 enum abx500_chargalg_states charge_state;
252 struct abx500_charge_curr_maximization ccm;
253 struct abx500_chargalg_charger_info chg_info;
254 struct abx500_chargalg_battery_data batt_data;
255 struct abx500_chargalg_suspension_status susp_status;
256 struct ab8500 *parent;
257 struct abx500_chargalg_current_step_status curr_status;
258 struct abx500_bm_data *bm;
259 struct power_supply chargalg_psy;
260 struct ux500_charger *ac_chg;
261 struct ux500_charger *usb_chg;
262 struct abx500_chargalg_events events;
263 struct workqueue_struct *chargalg_wq;
264 struct delayed_work chargalg_periodic_work;
265 struct delayed_work chargalg_wd_work;
266 struct work_struct chargalg_work;
267 struct hrtimer safety_timer;
268 struct hrtimer maintenance_timer;
269 struct kobject chargalg_kobject;
272 /*External charger prepare notifier*/
273 BLOCKING_NOTIFIER_HEAD(charger_notifier_list);
275 /* Main battery properties */
276 static enum power_supply_property abx500_chargalg_props[] = {
277 POWER_SUPPLY_PROP_STATUS,
278 POWER_SUPPLY_PROP_HEALTH,
281 struct abx500_chargalg_sysfs_entry {
282 struct attribute attr;
283 ssize_t (*show)(struct abx500_chargalg *, char *);
284 ssize_t (*store)(struct abx500_chargalg *, const char *, size_t);
288 * abx500_chargalg_safety_timer_expired() - Expiration of the safety timer
289 * @timer: pointer to the hrtimer structure
291 * This function gets called when the safety timer for the charger
294 static enum hrtimer_restart
295 abx500_chargalg_safety_timer_expired(struct hrtimer *timer)
297 struct abx500_chargalg *di = container_of(timer, struct abx500_chargalg,
299 dev_err(di->dev, "Safety timer expired\n");
300 di->events.safety_timer_expired = true;
302 /* Trigger execution of the algorithm instantly */
303 queue_work(di->chargalg_wq, &di->chargalg_work);
305 return HRTIMER_NORESTART;
309 * abx500_chargalg_maintenance_timer_expired() - Expiration of
310 * the maintenance timer
311 * @timer: pointer to the timer structure
313 * This function gets called when the maintenence timer
316 static enum hrtimer_restart
317 abx500_chargalg_maintenance_timer_expired(struct hrtimer *timer)
320 struct abx500_chargalg *di = container_of(timer, struct abx500_chargalg,
323 dev_dbg(di->dev, "Maintenance timer expired\n");
324 di->events.maintenance_timer_expired = true;
326 /* Trigger execution of the algorithm instantly */
327 queue_work(di->chargalg_wq, &di->chargalg_work);
329 return HRTIMER_NORESTART;
333 * abx500_chargalg_state_to() - Change charge state
334 * @di: pointer to the abx500_chargalg structure
336 * This function gets called when a charge state change should occur
338 static void abx500_chargalg_state_to(struct abx500_chargalg *di,
339 enum abx500_chargalg_states state)
342 "State changed: %s (From state: [%d] %s =to=> [%d] %s )\n",
343 di->charge_state == state ? "NO" : "YES",
345 states[di->charge_state],
349 di->charge_state = state;
352 static int abx500_chargalg_check_charger_enable(struct abx500_chargalg *di)
354 switch (di->charge_state) {
356 case STATE_MAINTENANCE_A:
357 case STATE_MAINTENANCE_B:
363 if (di->chg_info.charger_type & USB_CHG) {
364 return di->usb_chg->ops.check_enable(di->usb_chg,
365 di->bm->bat_type[di->bm->batt_id].normal_vol_lvl,
366 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
367 } else if ((di->chg_info.charger_type & AC_CHG) &&
368 !(di->ac_chg->external)) {
369 return di->ac_chg->ops.check_enable(di->ac_chg,
370 di->bm->bat_type[di->bm->batt_id].normal_vol_lvl,
371 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
377 * abx500_chargalg_check_charger_connection() - Check charger connection change
378 * @di: pointer to the abx500_chargalg structure
380 * This function will check if there is a change in the charger connection
381 * and change charge state accordingly. AC has precedence over USB.
383 static int abx500_chargalg_check_charger_connection(struct abx500_chargalg *di)
385 if (di->chg_info.conn_chg != di->chg_info.prev_conn_chg ||
386 di->susp_status.suspended_change) {
388 * Charger state changed or suspension
389 * has changed since last update
391 if ((di->chg_info.conn_chg & AC_CHG) &&
392 !di->susp_status.ac_suspended) {
393 dev_dbg(di->dev, "Charging source is AC\n");
394 if (di->chg_info.charger_type != AC_CHG) {
395 di->chg_info.charger_type = AC_CHG;
396 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
398 } else if ((di->chg_info.conn_chg & USB_CHG) &&
399 !di->susp_status.usb_suspended) {
400 dev_dbg(di->dev, "Charging source is USB\n");
401 di->chg_info.charger_type = USB_CHG;
402 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
403 } else if (di->chg_info.conn_chg &&
404 (di->susp_status.ac_suspended ||
405 di->susp_status.usb_suspended)) {
406 dev_dbg(di->dev, "Charging is suspended\n");
407 di->chg_info.charger_type = NO_CHG;
408 abx500_chargalg_state_to(di, STATE_SUSPENDED_INIT);
410 dev_dbg(di->dev, "Charging source is OFF\n");
411 di->chg_info.charger_type = NO_CHG;
412 abx500_chargalg_state_to(di, STATE_HANDHELD_INIT);
414 di->chg_info.prev_conn_chg = di->chg_info.conn_chg;
415 di->susp_status.suspended_change = false;
417 return di->chg_info.conn_chg;
421 * abx500_chargalg_check_current_step_status() - Check charging current
423 * @di: pointer to the abx500_chargalg structure
425 * This function will check if there is a change in the charging current step
426 * and change charge state accordingly.
428 static void abx500_chargalg_check_current_step_status
429 (struct abx500_chargalg *di)
431 if (di->curr_status.curr_step_change)
432 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
433 di->curr_status.curr_step_change = false;
437 * abx500_chargalg_start_safety_timer() - Start charging safety timer
438 * @di: pointer to the abx500_chargalg structure
440 * The safety timer is used to avoid overcharging of old or bad batteries.
441 * There are different timers for AC and USB
443 static void abx500_chargalg_start_safety_timer(struct abx500_chargalg *di)
445 /* Charger-dependent expiration time in hours*/
446 int timer_expiration = 0;
448 switch (di->chg_info.charger_type) {
450 timer_expiration = di->bm->main_safety_tmr_h;
454 timer_expiration = di->bm->usb_safety_tmr_h;
458 dev_err(di->dev, "Unknown charger to charge from\n");
462 di->events.safety_timer_expired = false;
463 hrtimer_set_expires_range(&di->safety_timer,
464 ktime_set(timer_expiration * ONE_HOUR_IN_SECONDS, 0),
465 ktime_set(FIVE_MINUTES_IN_SECONDS, 0));
466 hrtimer_start_expires(&di->safety_timer, HRTIMER_MODE_REL);
470 * abx500_chargalg_stop_safety_timer() - Stop charging safety timer
471 * @di: pointer to the abx500_chargalg structure
473 * The safety timer is stopped whenever the NORMAL state is exited
475 static void abx500_chargalg_stop_safety_timer(struct abx500_chargalg *di)
477 if (hrtimer_try_to_cancel(&di->safety_timer) >= 0)
478 di->events.safety_timer_expired = false;
482 * abx500_chargalg_start_maintenance_timer() - Start charging maintenance timer
483 * @di: pointer to the abx500_chargalg structure
484 * @duration: duration of ther maintenance timer in hours
486 * The maintenance timer is used to maintain the charge in the battery once
487 * the battery is considered full. These timers are chosen to match the
488 * discharge curve of the battery
490 static void abx500_chargalg_start_maintenance_timer(struct abx500_chargalg *di,
493 hrtimer_set_expires_range(&di->maintenance_timer,
494 ktime_set(duration * ONE_HOUR_IN_SECONDS, 0),
495 ktime_set(FIVE_MINUTES_IN_SECONDS, 0));
496 di->events.maintenance_timer_expired = false;
497 hrtimer_start_expires(&di->maintenance_timer, HRTIMER_MODE_REL);
501 * abx500_chargalg_stop_maintenance_timer() - Stop maintenance timer
502 * @di: pointer to the abx500_chargalg structure
504 * The maintenance timer is stopped whenever maintenance ends or when another
507 static void abx500_chargalg_stop_maintenance_timer(struct abx500_chargalg *di)
509 if (hrtimer_try_to_cancel(&di->maintenance_timer) >= 0)
510 di->events.maintenance_timer_expired = false;
514 * abx500_chargalg_kick_watchdog() - Kick charger watchdog
515 * @di: pointer to the abx500_chargalg structure
517 * The charger watchdog have to be kicked periodically whenever the charger is
518 * on, else the ABB will reset the system
520 static int abx500_chargalg_kick_watchdog(struct abx500_chargalg *di)
522 /* Check if charger exists and kick watchdog if charging */
523 if (di->ac_chg && di->ac_chg->ops.kick_wd &&
524 di->chg_info.online_chg & AC_CHG) {
526 * If AB charger watchdog expired, pm2xxx charging
527 * gets disabled. To be safe, kick both AB charger watchdog
528 * and pm2xxx watchdog.
530 if (di->ac_chg->external &&
531 di->usb_chg && di->usb_chg->ops.kick_wd)
532 di->usb_chg->ops.kick_wd(di->usb_chg);
534 return di->ac_chg->ops.kick_wd(di->ac_chg);
536 else if (di->usb_chg && di->usb_chg->ops.kick_wd &&
537 di->chg_info.online_chg & USB_CHG)
538 return di->usb_chg->ops.kick_wd(di->usb_chg);
544 * abx500_chargalg_ac_en() - Turn on/off the AC charger
545 * @di: pointer to the abx500_chargalg structure
546 * @enable: charger on/off
547 * @vset: requested charger output voltage
548 * @iset: requested charger output current
550 * The AC charger will be turned on/off with the requested charge voltage and
553 static int abx500_chargalg_ac_en(struct abx500_chargalg *di, int enable,
556 static int abx500_chargalg_ex_ac_enable_toggle;
558 if (!di->ac_chg || !di->ac_chg->ops.enable)
561 /* Select maximum of what both the charger and the battery supports */
562 if (di->ac_chg->max_out_volt)
563 vset = min(vset, di->ac_chg->max_out_volt);
564 if (di->ac_chg->max_out_curr)
565 iset = min(iset, di->ac_chg->max_out_curr);
567 di->chg_info.ac_iset = iset;
568 di->chg_info.ac_vset = vset;
570 /* Enable external charger */
571 if (enable && di->ac_chg->external &&
572 !abx500_chargalg_ex_ac_enable_toggle) {
573 blocking_notifier_call_chain(&charger_notifier_list,
575 abx500_chargalg_ex_ac_enable_toggle++;
578 return di->ac_chg->ops.enable(di->ac_chg, enable, vset, iset);
582 * abx500_chargalg_usb_en() - Turn on/off the USB charger
583 * @di: pointer to the abx500_chargalg structure
584 * @enable: charger on/off
585 * @vset: requested charger output voltage
586 * @iset: requested charger output current
588 * The USB charger will be turned on/off with the requested charge voltage and
591 static int abx500_chargalg_usb_en(struct abx500_chargalg *di, int enable,
594 if (!di->usb_chg || !di->usb_chg->ops.enable)
597 /* Select maximum of what both the charger and the battery supports */
598 if (di->usb_chg->max_out_volt)
599 vset = min(vset, di->usb_chg->max_out_volt);
600 if (di->usb_chg->max_out_curr)
601 iset = min(iset, di->usb_chg->max_out_curr);
603 di->chg_info.usb_iset = iset;
604 di->chg_info.usb_vset = vset;
606 return di->usb_chg->ops.enable(di->usb_chg, enable, vset, iset);
610 * ab8540_chargalg_usb_pp_en() - Enable/ disable USB power path
611 * @di: pointer to the abx500_chargalg structure
612 * @enable: power path enable/disable
614 * The USB power path will be enable/ disable
616 static int ab8540_chargalg_usb_pp_en(struct abx500_chargalg *di, bool enable)
618 if (!di->usb_chg || !di->usb_chg->ops.pp_enable)
621 return di->usb_chg->ops.pp_enable(di->usb_chg, enable);
625 * ab8540_chargalg_usb_pre_chg_en() - Enable/ disable USB pre-charge
626 * @di: pointer to the abx500_chargalg structure
627 * @enable: USB pre-charge enable/disable
629 * The USB USB pre-charge will be enable/ disable
631 static int ab8540_chargalg_usb_pre_chg_en(struct abx500_chargalg *di,
634 if (!di->usb_chg || !di->usb_chg->ops.pre_chg_enable)
637 return di->usb_chg->ops.pre_chg_enable(di->usb_chg, enable);
641 * abx500_chargalg_update_chg_curr() - Update charger current
642 * @di: pointer to the abx500_chargalg structure
643 * @iset: requested charger output current
645 * The charger output current will be updated for the charger
646 * that is currently in use
648 static int abx500_chargalg_update_chg_curr(struct abx500_chargalg *di,
651 /* Check if charger exists and update current if charging */
652 if (di->ac_chg && di->ac_chg->ops.update_curr &&
653 di->chg_info.charger_type & AC_CHG) {
655 * Select maximum of what both the charger
656 * and the battery supports
658 if (di->ac_chg->max_out_curr)
659 iset = min(iset, di->ac_chg->max_out_curr);
661 di->chg_info.ac_iset = iset;
663 return di->ac_chg->ops.update_curr(di->ac_chg, iset);
664 } else if (di->usb_chg && di->usb_chg->ops.update_curr &&
665 di->chg_info.charger_type & USB_CHG) {
667 * Select maximum of what both the charger
668 * and the battery supports
670 if (di->usb_chg->max_out_curr)
671 iset = min(iset, di->usb_chg->max_out_curr);
673 di->chg_info.usb_iset = iset;
675 return di->usb_chg->ops.update_curr(di->usb_chg, iset);
682 * abx500_chargalg_stop_charging() - Stop charging
683 * @di: pointer to the abx500_chargalg structure
685 * This function is called from any state where charging should be stopped.
686 * All charging is disabled and all status parameters and timers are changed
689 static void abx500_chargalg_stop_charging(struct abx500_chargalg *di)
691 abx500_chargalg_ac_en(di, false, 0, 0);
692 abx500_chargalg_usb_en(di, false, 0, 0);
693 abx500_chargalg_stop_safety_timer(di);
694 abx500_chargalg_stop_maintenance_timer(di);
695 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
696 di->maintenance_chg = false;
697 cancel_delayed_work(&di->chargalg_wd_work);
698 power_supply_changed(&di->chargalg_psy);
702 * abx500_chargalg_hold_charging() - Pauses charging
703 * @di: pointer to the abx500_chargalg structure
705 * This function is called in the case where maintenance charging has been
706 * disabled and instead a battery voltage mode is entered to check when the
707 * battery voltage has reached a certain recharge voltage
709 static void abx500_chargalg_hold_charging(struct abx500_chargalg *di)
711 abx500_chargalg_ac_en(di, false, 0, 0);
712 abx500_chargalg_usb_en(di, false, 0, 0);
713 abx500_chargalg_stop_safety_timer(di);
714 abx500_chargalg_stop_maintenance_timer(di);
715 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
716 di->maintenance_chg = false;
717 cancel_delayed_work(&di->chargalg_wd_work);
718 power_supply_changed(&di->chargalg_psy);
722 * abx500_chargalg_start_charging() - Start the charger
723 * @di: pointer to the abx500_chargalg structure
724 * @vset: requested charger output voltage
725 * @iset: requested charger output current
727 * A charger will be enabled depending on the requested charger type that was
728 * detected previously.
730 static void abx500_chargalg_start_charging(struct abx500_chargalg *di,
733 switch (di->chg_info.charger_type) {
736 "AC parameters: Vset %d, Ich %d\n", vset, iset);
737 abx500_chargalg_usb_en(di, false, 0, 0);
738 abx500_chargalg_ac_en(di, true, vset, iset);
743 "USB parameters: Vset %d, Ich %d\n", vset, iset);
744 abx500_chargalg_ac_en(di, false, 0, 0);
745 abx500_chargalg_usb_en(di, true, vset, iset);
749 dev_err(di->dev, "Unknown charger to charge from\n");
755 * abx500_chargalg_check_temp() - Check battery temperature ranges
756 * @di: pointer to the abx500_chargalg structure
758 * The battery temperature is checked against the predefined limits and the
759 * charge state is changed accordingly
761 static void abx500_chargalg_check_temp(struct abx500_chargalg *di)
763 if (di->batt_data.temp > (di->bm->temp_low + di->t_hyst_norm) &&
764 di->batt_data.temp < (di->bm->temp_high - di->t_hyst_norm)) {
766 di->events.btemp_underover = false;
767 di->events.btemp_lowhigh = false;
769 di->t_hyst_lowhigh = 0;
771 if (((di->batt_data.temp >= di->bm->temp_high) &&
772 (di->batt_data.temp <
773 (di->bm->temp_over - di->t_hyst_lowhigh))) ||
774 ((di->batt_data.temp >
775 (di->bm->temp_under + di->t_hyst_lowhigh)) &&
776 (di->batt_data.temp <= di->bm->temp_low))) {
777 /* TEMP minor!!!!! */
778 di->events.btemp_underover = false;
779 di->events.btemp_lowhigh = true;
780 di->t_hyst_norm = di->bm->temp_hysteresis;
781 di->t_hyst_lowhigh = 0;
782 } else if (di->batt_data.temp <= di->bm->temp_under ||
783 di->batt_data.temp >= di->bm->temp_over) {
784 /* TEMP major!!!!! */
785 di->events.btemp_underover = true;
786 di->events.btemp_lowhigh = false;
788 di->t_hyst_lowhigh = di->bm->temp_hysteresis;
790 /* Within hysteresis */
791 dev_dbg(di->dev, "Within hysteresis limit temp: %d "
792 "hyst_lowhigh %d, hyst normal %d\n",
793 di->batt_data.temp, di->t_hyst_lowhigh,
800 * abx500_chargalg_check_charger_voltage() - Check charger voltage
801 * @di: pointer to the abx500_chargalg structure
803 * Charger voltage is checked against maximum limit
805 static void abx500_chargalg_check_charger_voltage(struct abx500_chargalg *di)
807 if (di->chg_info.usb_volt > di->bm->chg_params->usb_volt_max)
808 di->chg_info.usb_chg_ok = false;
810 di->chg_info.usb_chg_ok = true;
812 if (di->chg_info.ac_volt > di->bm->chg_params->ac_volt_max)
813 di->chg_info.ac_chg_ok = false;
815 di->chg_info.ac_chg_ok = true;
820 * abx500_chargalg_end_of_charge() - Check if end-of-charge criteria is fulfilled
821 * @di: pointer to the abx500_chargalg structure
823 * End-of-charge criteria is fulfilled when the battery voltage is above a
824 * certain limit and the battery current is below a certain limit for a
825 * predefined number of consecutive seconds. If true, the battery is full
827 static void abx500_chargalg_end_of_charge(struct abx500_chargalg *di)
829 if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING &&
830 di->charge_state == STATE_NORMAL &&
831 !di->maintenance_chg && (di->batt_data.volt >=
832 di->bm->bat_type[di->bm->batt_id].termination_vol ||
833 di->events.usb_cv_active || di->events.ac_cv_active) &&
834 di->batt_data.avg_curr <
835 di->bm->bat_type[di->bm->batt_id].termination_curr &&
836 di->batt_data.avg_curr > 0) {
837 if (++di->eoc_cnt >= EOC_COND_CNT) {
839 if ((di->chg_info.charger_type & USB_CHG) &&
840 (di->usb_chg->power_path))
841 ab8540_chargalg_usb_pp_en(di, true);
842 di->charge_status = POWER_SUPPLY_STATUS_FULL;
843 di->maintenance_chg = true;
844 dev_dbg(di->dev, "EOC reached!\n");
845 power_supply_changed(&di->chargalg_psy);
848 " EOC limit reached for the %d"
849 " time, out of %d before EOC\n",
858 static void init_maxim_chg_curr(struct abx500_chargalg *di)
860 di->ccm.original_iset =
861 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl;
862 di->ccm.current_iset =
863 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl;
864 di->ccm.test_delta_i = di->bm->maxi->charger_curr_step;
865 di->ccm.max_current = di->bm->maxi->chg_curr;
866 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
871 * abx500_chargalg_chg_curr_maxim - increases the charger current to
872 * compensate for the system load
873 * @di pointer to the abx500_chargalg structure
875 * This maximization function is used to raise the charger current to get the
876 * battery current as close to the optimal value as possible. The battery
877 * current during charging is affected by the system load
879 static enum maxim_ret abx500_chargalg_chg_curr_maxim(struct abx500_chargalg *di)
883 if (!di->bm->maxi->ena_maxi)
884 return MAXIM_RET_NOACTION;
886 delta_i = di->ccm.original_iset - di->batt_data.inst_curr;
888 if (di->events.vbus_collapsed) {
889 dev_dbg(di->dev, "Charger voltage has collapsed %d\n",
891 if (di->ccm.wait_cnt == 0) {
892 dev_dbg(di->dev, "lowering current\n");
894 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
895 di->ccm.max_current =
896 di->ccm.current_iset - di->ccm.test_delta_i;
897 di->ccm.current_iset = di->ccm.max_current;
899 return MAXIM_RET_CHANGE;
901 dev_dbg(di->dev, "waiting\n");
902 /* Let's go in here twice before lowering curr again */
903 di->ccm.wait_cnt = (di->ccm.wait_cnt + 1) % 3;
904 return MAXIM_RET_NOACTION;
908 di->ccm.wait_cnt = 0;
910 if ((di->batt_data.inst_curr > di->ccm.original_iset)) {
911 dev_dbg(di->dev, " Maximization Ibat (%dmA) too high"
912 " (limit %dmA) (current iset: %dmA)!\n",
913 di->batt_data.inst_curr, di->ccm.original_iset,
914 di->ccm.current_iset);
916 if (di->ccm.current_iset == di->ccm.original_iset)
917 return MAXIM_RET_NOACTION;
919 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
920 di->ccm.current_iset = di->ccm.original_iset;
923 return MAXIM_RET_IBAT_TOO_HIGH;
926 if (delta_i > di->ccm.test_delta_i &&
927 (di->ccm.current_iset + di->ccm.test_delta_i) <
928 di->ccm.max_current) {
929 if (di->ccm.condition_cnt-- == 0) {
930 /* Increse the iset with cco.test_delta_i */
931 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
932 di->ccm.current_iset += di->ccm.test_delta_i;
934 dev_dbg(di->dev, " Maximization needed, increase"
935 " with %d mA to %dmA (Optimal ibat: %d)"
937 di->ccm.test_delta_i,
938 di->ccm.current_iset,
939 di->ccm.original_iset,
941 return MAXIM_RET_CHANGE;
943 return MAXIM_RET_NOACTION;
946 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
947 return MAXIM_RET_NOACTION;
951 static void handle_maxim_chg_curr(struct abx500_chargalg *di)
956 ret = abx500_chargalg_chg_curr_maxim(di);
958 case MAXIM_RET_CHANGE:
959 result = abx500_chargalg_update_chg_curr(di,
960 di->ccm.current_iset);
962 dev_err(di->dev, "failed to set chg curr\n");
964 case MAXIM_RET_IBAT_TOO_HIGH:
965 result = abx500_chargalg_update_chg_curr(di,
966 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
968 dev_err(di->dev, "failed to set chg curr\n");
971 case MAXIM_RET_NOACTION:
978 static int abx500_chargalg_get_ext_psy_data(struct device *dev, void *data)
980 struct power_supply *psy;
981 struct power_supply *ext;
982 struct abx500_chargalg *di;
983 union power_supply_propval ret;
985 bool psy_found = false;
986 bool capacity_updated = false;
988 psy = (struct power_supply *)data;
989 ext = dev_get_drvdata(dev);
990 di = to_abx500_chargalg_device_info(psy);
991 /* For all psy where the driver name appears in any supplied_to */
992 for (i = 0; i < ext->num_supplicants; i++) {
993 if (!strcmp(ext->supplied_to[i], psy->name))
1000 * If external is not registering 'POWER_SUPPLY_PROP_CAPACITY' to its
1001 * property because of handling that sysfs entry on its own, this is
1002 * the place to get the battery capacity.
1004 if (!ext->get_property(ext, POWER_SUPPLY_PROP_CAPACITY, &ret)) {
1005 di->batt_data.percent = ret.intval;
1006 capacity_updated = true;
1009 /* Go through all properties for the psy */
1010 for (j = 0; j < ext->num_properties; j++) {
1011 enum power_supply_property prop;
1012 prop = ext->properties[j];
1014 /* Initialize chargers if not already done */
1016 ext->type == POWER_SUPPLY_TYPE_MAINS)
1017 di->ac_chg = psy_to_ux500_charger(ext);
1018 else if (!di->usb_chg &&
1019 ext->type == POWER_SUPPLY_TYPE_USB)
1020 di->usb_chg = psy_to_ux500_charger(ext);
1022 if (ext->get_property(ext, prop, &ret))
1025 case POWER_SUPPLY_PROP_PRESENT:
1026 switch (ext->type) {
1027 case POWER_SUPPLY_TYPE_BATTERY:
1028 /* Battery present */
1030 di->events.batt_rem = false;
1031 /* Battery removed */
1033 di->events.batt_rem = true;
1035 case POWER_SUPPLY_TYPE_MAINS:
1036 /* AC disconnected */
1038 (di->chg_info.conn_chg & AC_CHG)) {
1039 di->chg_info.prev_conn_chg =
1040 di->chg_info.conn_chg;
1041 di->chg_info.conn_chg &= ~AC_CHG;
1044 else if (ret.intval &&
1045 !(di->chg_info.conn_chg & AC_CHG)) {
1046 di->chg_info.prev_conn_chg =
1047 di->chg_info.conn_chg;
1048 di->chg_info.conn_chg |= AC_CHG;
1051 case POWER_SUPPLY_TYPE_USB:
1052 /* USB disconnected */
1054 (di->chg_info.conn_chg & USB_CHG)) {
1055 di->chg_info.prev_conn_chg =
1056 di->chg_info.conn_chg;
1057 di->chg_info.conn_chg &= ~USB_CHG;
1060 else if (ret.intval &&
1061 !(di->chg_info.conn_chg & USB_CHG)) {
1062 di->chg_info.prev_conn_chg =
1063 di->chg_info.conn_chg;
1064 di->chg_info.conn_chg |= USB_CHG;
1072 case POWER_SUPPLY_PROP_ONLINE:
1073 switch (ext->type) {
1074 case POWER_SUPPLY_TYPE_BATTERY:
1076 case POWER_SUPPLY_TYPE_MAINS:
1079 (di->chg_info.online_chg & AC_CHG)) {
1080 di->chg_info.prev_online_chg =
1081 di->chg_info.online_chg;
1082 di->chg_info.online_chg &= ~AC_CHG;
1085 else if (ret.intval &&
1086 !(di->chg_info.online_chg & AC_CHG)) {
1087 di->chg_info.prev_online_chg =
1088 di->chg_info.online_chg;
1089 di->chg_info.online_chg |= AC_CHG;
1090 queue_delayed_work(di->chargalg_wq,
1091 &di->chargalg_wd_work, 0);
1094 case POWER_SUPPLY_TYPE_USB:
1097 (di->chg_info.online_chg & USB_CHG)) {
1098 di->chg_info.prev_online_chg =
1099 di->chg_info.online_chg;
1100 di->chg_info.online_chg &= ~USB_CHG;
1103 else if (ret.intval &&
1104 !(di->chg_info.online_chg & USB_CHG)) {
1105 di->chg_info.prev_online_chg =
1106 di->chg_info.online_chg;
1107 di->chg_info.online_chg |= USB_CHG;
1108 queue_delayed_work(di->chargalg_wq,
1109 &di->chargalg_wd_work, 0);
1117 case POWER_SUPPLY_PROP_HEALTH:
1118 switch (ext->type) {
1119 case POWER_SUPPLY_TYPE_BATTERY:
1121 case POWER_SUPPLY_TYPE_MAINS:
1122 switch (ret.intval) {
1123 case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
1124 di->events.mainextchnotok = true;
1125 di->events.main_thermal_prot = false;
1126 di->events.main_ovv = false;
1127 di->events.ac_wd_expired = false;
1129 case POWER_SUPPLY_HEALTH_DEAD:
1130 di->events.ac_wd_expired = true;
1131 di->events.mainextchnotok = false;
1132 di->events.main_ovv = false;
1133 di->events.main_thermal_prot = false;
1135 case POWER_SUPPLY_HEALTH_COLD:
1136 case POWER_SUPPLY_HEALTH_OVERHEAT:
1137 di->events.main_thermal_prot = true;
1138 di->events.mainextchnotok = false;
1139 di->events.main_ovv = false;
1140 di->events.ac_wd_expired = false;
1142 case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
1143 di->events.main_ovv = true;
1144 di->events.mainextchnotok = false;
1145 di->events.main_thermal_prot = false;
1146 di->events.ac_wd_expired = false;
1148 case POWER_SUPPLY_HEALTH_GOOD:
1149 di->events.main_thermal_prot = false;
1150 di->events.mainextchnotok = false;
1151 di->events.main_ovv = false;
1152 di->events.ac_wd_expired = false;
1159 case POWER_SUPPLY_TYPE_USB:
1160 switch (ret.intval) {
1161 case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
1162 di->events.usbchargernotok = true;
1163 di->events.usb_thermal_prot = false;
1164 di->events.vbus_ovv = false;
1165 di->events.usb_wd_expired = false;
1167 case POWER_SUPPLY_HEALTH_DEAD:
1168 di->events.usb_wd_expired = true;
1169 di->events.usbchargernotok = false;
1170 di->events.usb_thermal_prot = false;
1171 di->events.vbus_ovv = false;
1173 case POWER_SUPPLY_HEALTH_COLD:
1174 case POWER_SUPPLY_HEALTH_OVERHEAT:
1175 di->events.usb_thermal_prot = true;
1176 di->events.usbchargernotok = false;
1177 di->events.vbus_ovv = false;
1178 di->events.usb_wd_expired = false;
1180 case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
1181 di->events.vbus_ovv = true;
1182 di->events.usbchargernotok = false;
1183 di->events.usb_thermal_prot = false;
1184 di->events.usb_wd_expired = false;
1186 case POWER_SUPPLY_HEALTH_GOOD:
1187 di->events.usbchargernotok = false;
1188 di->events.usb_thermal_prot = false;
1189 di->events.vbus_ovv = false;
1190 di->events.usb_wd_expired = false;
1200 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1201 switch (ext->type) {
1202 case POWER_SUPPLY_TYPE_BATTERY:
1203 di->batt_data.volt = ret.intval / 1000;
1205 case POWER_SUPPLY_TYPE_MAINS:
1206 di->chg_info.ac_volt = ret.intval / 1000;
1208 case POWER_SUPPLY_TYPE_USB:
1209 di->chg_info.usb_volt = ret.intval / 1000;
1216 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
1217 switch (ext->type) {
1218 case POWER_SUPPLY_TYPE_MAINS:
1219 /* AVG is used to indicate when we are
1222 di->events.ac_cv_active = true;
1224 di->events.ac_cv_active = false;
1227 case POWER_SUPPLY_TYPE_USB:
1228 /* AVG is used to indicate when we are
1231 di->events.usb_cv_active = true;
1233 di->events.usb_cv_active = false;
1241 case POWER_SUPPLY_PROP_TECHNOLOGY:
1242 switch (ext->type) {
1243 case POWER_SUPPLY_TYPE_BATTERY:
1245 di->events.batt_unknown = false;
1247 di->events.batt_unknown = true;
1255 case POWER_SUPPLY_PROP_TEMP:
1256 di->batt_data.temp = ret.intval / 10;
1259 case POWER_SUPPLY_PROP_CURRENT_NOW:
1260 switch (ext->type) {
1261 case POWER_SUPPLY_TYPE_MAINS:
1262 di->chg_info.ac_curr =
1265 case POWER_SUPPLY_TYPE_USB:
1266 di->chg_info.usb_curr =
1269 case POWER_SUPPLY_TYPE_BATTERY:
1270 di->batt_data.inst_curr = ret.intval / 1000;
1277 case POWER_SUPPLY_PROP_CURRENT_AVG:
1278 switch (ext->type) {
1279 case POWER_SUPPLY_TYPE_BATTERY:
1280 di->batt_data.avg_curr = ret.intval / 1000;
1282 case POWER_SUPPLY_TYPE_USB:
1284 di->events.vbus_collapsed = true;
1286 di->events.vbus_collapsed = false;
1292 case POWER_SUPPLY_PROP_CAPACITY:
1293 if (!capacity_updated)
1294 di->batt_data.percent = ret.intval;
1304 * abx500_chargalg_external_power_changed() - callback for power supply changes
1305 * @psy: pointer to the structure power_supply
1307 * This function is the entry point of the pointer external_power_changed
1308 * of the structure power_supply.
1309 * This function gets executed when there is a change in any external power
1310 * supply that this driver needs to be notified of.
1312 static void abx500_chargalg_external_power_changed(struct power_supply *psy)
1314 struct abx500_chargalg *di = to_abx500_chargalg_device_info(psy);
1317 * Trigger execution of the algorithm instantly and read
1318 * all power_supply properties there instead
1320 queue_work(di->chargalg_wq, &di->chargalg_work);
1324 * abx500_chargalg_algorithm() - Main function for the algorithm
1325 * @di: pointer to the abx500_chargalg structure
1327 * This is the main control function for the charging algorithm.
1328 * It is called periodically or when something happens that will
1329 * trigger a state change
1331 static void abx500_chargalg_algorithm(struct abx500_chargalg *di)
1337 /* Collect data from all power_supply class devices */
1338 class_for_each_device(power_supply_class, NULL,
1339 &di->chargalg_psy, abx500_chargalg_get_ext_psy_data);
1341 abx500_chargalg_end_of_charge(di);
1342 abx500_chargalg_check_temp(di);
1343 abx500_chargalg_check_charger_voltage(di);
1345 charger_status = abx500_chargalg_check_charger_connection(di);
1346 abx500_chargalg_check_current_step_status(di);
1348 if (is_ab8500(di->parent)) {
1349 ret = abx500_chargalg_check_charger_enable(di);
1351 dev_err(di->dev, "Checking charger is enabled error"
1352 ": Returned Value %d\n", ret);
1356 * First check if we have a charger connected.
1357 * Also we don't allow charging of unknown batteries if configured
1360 if (!charger_status ||
1361 (di->events.batt_unknown && !di->bm->chg_unknown_bat)) {
1362 if (di->charge_state != STATE_HANDHELD) {
1363 di->events.safety_timer_expired = false;
1364 abx500_chargalg_state_to(di, STATE_HANDHELD_INIT);
1368 /* If suspended, we should not continue checking the flags */
1369 else if (di->charge_state == STATE_SUSPENDED_INIT ||
1370 di->charge_state == STATE_SUSPENDED) {
1371 /* We don't do anything here, just don,t continue */
1374 /* Safety timer expiration */
1375 else if (di->events.safety_timer_expired) {
1376 if (di->charge_state != STATE_SAFETY_TIMER_EXPIRED)
1377 abx500_chargalg_state_to(di,
1378 STATE_SAFETY_TIMER_EXPIRED_INIT);
1381 * Check if any interrupts has occured
1382 * that will prevent us from charging
1385 /* Battery removed */
1386 else if (di->events.batt_rem) {
1387 if (di->charge_state != STATE_BATT_REMOVED)
1388 abx500_chargalg_state_to(di, STATE_BATT_REMOVED_INIT);
1390 /* Main or USB charger not ok. */
1391 else if (di->events.mainextchnotok || di->events.usbchargernotok) {
1393 * If vbus_collapsed is set, we have to lower the charger
1394 * current, which is done in the normal state below
1396 if (di->charge_state != STATE_CHG_NOT_OK &&
1397 !di->events.vbus_collapsed)
1398 abx500_chargalg_state_to(di, STATE_CHG_NOT_OK_INIT);
1400 /* VBUS, Main or VBAT OVV. */
1401 else if (di->events.vbus_ovv ||
1402 di->events.main_ovv ||
1403 di->events.batt_ovv ||
1404 !di->chg_info.usb_chg_ok ||
1405 !di->chg_info.ac_chg_ok) {
1406 if (di->charge_state != STATE_OVV_PROTECT)
1407 abx500_chargalg_state_to(di, STATE_OVV_PROTECT_INIT);
1409 /* USB Thermal, stop charging */
1410 else if (di->events.main_thermal_prot ||
1411 di->events.usb_thermal_prot) {
1412 if (di->charge_state != STATE_HW_TEMP_PROTECT)
1413 abx500_chargalg_state_to(di,
1414 STATE_HW_TEMP_PROTECT_INIT);
1416 /* Battery temp over/under */
1417 else if (di->events.btemp_underover) {
1418 if (di->charge_state != STATE_TEMP_UNDEROVER)
1419 abx500_chargalg_state_to(di,
1420 STATE_TEMP_UNDEROVER_INIT);
1422 /* Watchdog expired */
1423 else if (di->events.ac_wd_expired ||
1424 di->events.usb_wd_expired) {
1425 if (di->charge_state != STATE_WD_EXPIRED)
1426 abx500_chargalg_state_to(di, STATE_WD_EXPIRED_INIT);
1428 /* Battery temp high/low */
1429 else if (di->events.btemp_lowhigh) {
1430 if (di->charge_state != STATE_TEMP_LOWHIGH)
1431 abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH_INIT);
1435 "[CHARGALG] Vb %d Ib_avg %d Ib_inst %d Tb %d Cap %d Maint %d "
1436 "State %s Active_chg %d Chg_status %d AC %d USB %d "
1437 "AC_online %d USB_online %d AC_CV %d USB_CV %d AC_I %d "
1438 "USB_I %d AC_Vset %d AC_Iset %d USB_Vset %d USB_Iset %d\n",
1440 di->batt_data.avg_curr,
1441 di->batt_data.inst_curr,
1443 di->batt_data.percent,
1444 di->maintenance_chg,
1445 states[di->charge_state],
1446 di->chg_info.charger_type,
1448 di->chg_info.conn_chg & AC_CHG,
1449 di->chg_info.conn_chg & USB_CHG,
1450 di->chg_info.online_chg & AC_CHG,
1451 di->chg_info.online_chg & USB_CHG,
1452 di->events.ac_cv_active,
1453 di->events.usb_cv_active,
1454 di->chg_info.ac_curr,
1455 di->chg_info.usb_curr,
1456 di->chg_info.ac_vset,
1457 di->chg_info.ac_iset,
1458 di->chg_info.usb_vset,
1459 di->chg_info.usb_iset);
1461 switch (di->charge_state) {
1462 case STATE_HANDHELD_INIT:
1463 abx500_chargalg_stop_charging(di);
1464 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
1465 abx500_chargalg_state_to(di, STATE_HANDHELD);
1466 /* Intentional fallthrough */
1468 case STATE_HANDHELD:
1471 case STATE_SUSPENDED_INIT:
1472 if (di->susp_status.ac_suspended)
1473 abx500_chargalg_ac_en(di, false, 0, 0);
1474 if (di->susp_status.usb_suspended)
1475 abx500_chargalg_usb_en(di, false, 0, 0);
1476 abx500_chargalg_stop_safety_timer(di);
1477 abx500_chargalg_stop_maintenance_timer(di);
1478 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1479 di->maintenance_chg = false;
1480 abx500_chargalg_state_to(di, STATE_SUSPENDED);
1481 power_supply_changed(&di->chargalg_psy);
1482 /* Intentional fallthrough */
1484 case STATE_SUSPENDED:
1485 /* CHARGING is suspended */
1488 case STATE_BATT_REMOVED_INIT:
1489 abx500_chargalg_stop_charging(di);
1490 abx500_chargalg_state_to(di, STATE_BATT_REMOVED);
1491 /* Intentional fallthrough */
1493 case STATE_BATT_REMOVED:
1494 if (!di->events.batt_rem)
1495 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1498 case STATE_HW_TEMP_PROTECT_INIT:
1499 abx500_chargalg_stop_charging(di);
1500 abx500_chargalg_state_to(di, STATE_HW_TEMP_PROTECT);
1501 /* Intentional fallthrough */
1503 case STATE_HW_TEMP_PROTECT:
1504 if (!di->events.main_thermal_prot &&
1505 !di->events.usb_thermal_prot)
1506 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1509 case STATE_OVV_PROTECT_INIT:
1510 abx500_chargalg_stop_charging(di);
1511 abx500_chargalg_state_to(di, STATE_OVV_PROTECT);
1512 /* Intentional fallthrough */
1514 case STATE_OVV_PROTECT:
1515 if (!di->events.vbus_ovv &&
1516 !di->events.main_ovv &&
1517 !di->events.batt_ovv &&
1518 di->chg_info.usb_chg_ok &&
1519 di->chg_info.ac_chg_ok)
1520 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1523 case STATE_CHG_NOT_OK_INIT:
1524 abx500_chargalg_stop_charging(di);
1525 abx500_chargalg_state_to(di, STATE_CHG_NOT_OK);
1526 /* Intentional fallthrough */
1528 case STATE_CHG_NOT_OK:
1529 if (!di->events.mainextchnotok &&
1530 !di->events.usbchargernotok)
1531 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1534 case STATE_SAFETY_TIMER_EXPIRED_INIT:
1535 abx500_chargalg_stop_charging(di);
1536 abx500_chargalg_state_to(di, STATE_SAFETY_TIMER_EXPIRED);
1537 /* Intentional fallthrough */
1539 case STATE_SAFETY_TIMER_EXPIRED:
1540 /* We exit this state when charger is removed */
1543 case STATE_NORMAL_INIT:
1544 if ((di->chg_info.charger_type & USB_CHG) &&
1545 di->usb_chg->power_path) {
1546 if (di->batt_data.volt >
1547 (di->bm->fg_params->lowbat_threshold +
1549 ab8540_chargalg_usb_pre_chg_en(di, false);
1550 ab8540_chargalg_usb_pp_en(di, false);
1552 ab8540_chargalg_usb_pp_en(di, true);
1553 ab8540_chargalg_usb_pre_chg_en(di, true);
1554 abx500_chargalg_state_to(di,
1555 STATE_USB_PP_PRE_CHARGE);
1560 if (di->curr_status.curr_step == CHARGALG_CURR_STEP_LOW)
1561 abx500_chargalg_stop_charging(di);
1563 curr_step_lvl = di->bm->bat_type[
1564 di->bm->batt_id].normal_cur_lvl
1565 * di->curr_status.curr_step
1566 / CHARGALG_CURR_STEP_HIGH;
1567 abx500_chargalg_start_charging(di,
1568 di->bm->bat_type[di->bm->batt_id]
1569 .normal_vol_lvl, curr_step_lvl);
1572 abx500_chargalg_state_to(di, STATE_NORMAL);
1573 abx500_chargalg_start_safety_timer(di);
1574 abx500_chargalg_stop_maintenance_timer(di);
1575 init_maxim_chg_curr(di);
1576 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
1578 di->maintenance_chg = false;
1579 power_supply_changed(&di->chargalg_psy);
1583 case STATE_USB_PP_PRE_CHARGE:
1584 if (di->batt_data.volt >
1585 (di->bm->fg_params->lowbat_threshold +
1587 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1591 handle_maxim_chg_curr(di);
1592 if (di->charge_status == POWER_SUPPLY_STATUS_FULL &&
1593 di->maintenance_chg) {
1594 if (di->bm->no_maintenance)
1595 abx500_chargalg_state_to(di,
1596 STATE_WAIT_FOR_RECHARGE_INIT);
1598 abx500_chargalg_state_to(di,
1599 STATE_MAINTENANCE_A_INIT);
1603 /* This state will be used when the maintenance state is disabled */
1604 case STATE_WAIT_FOR_RECHARGE_INIT:
1605 abx500_chargalg_hold_charging(di);
1606 abx500_chargalg_state_to(di, STATE_WAIT_FOR_RECHARGE);
1607 /* Intentional fallthrough */
1609 case STATE_WAIT_FOR_RECHARGE:
1610 if (di->batt_data.percent <=
1611 di->bm->bat_type[di->bm->batt_id].
1613 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1616 case STATE_MAINTENANCE_A_INIT:
1617 abx500_chargalg_stop_safety_timer(di);
1618 abx500_chargalg_start_maintenance_timer(di,
1620 di->bm->batt_id].maint_a_chg_timer_h);
1621 abx500_chargalg_start_charging(di,
1623 di->bm->batt_id].maint_a_vol_lvl,
1625 di->bm->batt_id].maint_a_cur_lvl);
1626 abx500_chargalg_state_to(di, STATE_MAINTENANCE_A);
1627 power_supply_changed(&di->chargalg_psy);
1628 /* Intentional fallthrough*/
1630 case STATE_MAINTENANCE_A:
1631 if (di->events.maintenance_timer_expired) {
1632 abx500_chargalg_stop_maintenance_timer(di);
1633 abx500_chargalg_state_to(di, STATE_MAINTENANCE_B_INIT);
1637 case STATE_MAINTENANCE_B_INIT:
1638 abx500_chargalg_start_maintenance_timer(di,
1640 di->bm->batt_id].maint_b_chg_timer_h);
1641 abx500_chargalg_start_charging(di,
1643 di->bm->batt_id].maint_b_vol_lvl,
1645 di->bm->batt_id].maint_b_cur_lvl);
1646 abx500_chargalg_state_to(di, STATE_MAINTENANCE_B);
1647 power_supply_changed(&di->chargalg_psy);
1648 /* Intentional fallthrough*/
1650 case STATE_MAINTENANCE_B:
1651 if (di->events.maintenance_timer_expired) {
1652 abx500_chargalg_stop_maintenance_timer(di);
1653 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1657 case STATE_TEMP_LOWHIGH_INIT:
1658 abx500_chargalg_start_charging(di,
1660 di->bm->batt_id].low_high_vol_lvl,
1662 di->bm->batt_id].low_high_cur_lvl);
1663 abx500_chargalg_stop_maintenance_timer(di);
1664 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
1665 abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH);
1666 power_supply_changed(&di->chargalg_psy);
1667 /* Intentional fallthrough */
1669 case STATE_TEMP_LOWHIGH:
1670 if (!di->events.btemp_lowhigh)
1671 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1674 case STATE_WD_EXPIRED_INIT:
1675 abx500_chargalg_stop_charging(di);
1676 abx500_chargalg_state_to(di, STATE_WD_EXPIRED);
1677 /* Intentional fallthrough */
1679 case STATE_WD_EXPIRED:
1680 if (!di->events.ac_wd_expired &&
1681 !di->events.usb_wd_expired)
1682 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1685 case STATE_TEMP_UNDEROVER_INIT:
1686 abx500_chargalg_stop_charging(di);
1687 abx500_chargalg_state_to(di, STATE_TEMP_UNDEROVER);
1688 /* Intentional fallthrough */
1690 case STATE_TEMP_UNDEROVER:
1691 if (!di->events.btemp_underover)
1692 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1696 /* Start charging directly if the new state is a charge state */
1697 if (di->charge_state == STATE_NORMAL_INIT ||
1698 di->charge_state == STATE_MAINTENANCE_A_INIT ||
1699 di->charge_state == STATE_MAINTENANCE_B_INIT)
1700 queue_work(di->chargalg_wq, &di->chargalg_work);
1704 * abx500_chargalg_periodic_work() - Periodic work for the algorithm
1705 * @work: pointer to the work_struct structure
1707 * Work queue function for the charging algorithm
1709 static void abx500_chargalg_periodic_work(struct work_struct *work)
1711 struct abx500_chargalg *di = container_of(work,
1712 struct abx500_chargalg, chargalg_periodic_work.work);
1714 abx500_chargalg_algorithm(di);
1717 * If a charger is connected then the battery has to be monitored
1718 * frequently, else the work can be delayed.
1720 if (di->chg_info.conn_chg)
1721 queue_delayed_work(di->chargalg_wq,
1722 &di->chargalg_periodic_work,
1723 di->bm->interval_charging * HZ);
1725 queue_delayed_work(di->chargalg_wq,
1726 &di->chargalg_periodic_work,
1727 di->bm->interval_not_charging * HZ);
1731 * abx500_chargalg_wd_work() - periodic work to kick the charger watchdog
1732 * @work: pointer to the work_struct structure
1734 * Work queue function for kicking the charger watchdog
1736 static void abx500_chargalg_wd_work(struct work_struct *work)
1739 struct abx500_chargalg *di = container_of(work,
1740 struct abx500_chargalg, chargalg_wd_work.work);
1742 dev_dbg(di->dev, "abx500_chargalg_wd_work\n");
1744 ret = abx500_chargalg_kick_watchdog(di);
1746 dev_err(di->dev, "failed to kick watchdog\n");
1748 queue_delayed_work(di->chargalg_wq,
1749 &di->chargalg_wd_work, CHG_WD_INTERVAL);
1753 * abx500_chargalg_work() - Work to run the charging algorithm instantly
1754 * @work: pointer to the work_struct structure
1756 * Work queue function for calling the charging algorithm
1758 static void abx500_chargalg_work(struct work_struct *work)
1760 struct abx500_chargalg *di = container_of(work,
1761 struct abx500_chargalg, chargalg_work);
1763 abx500_chargalg_algorithm(di);
1767 * abx500_chargalg_get_property() - get the chargalg properties
1768 * @psy: pointer to the power_supply structure
1769 * @psp: pointer to the power_supply_property structure
1770 * @val: pointer to the power_supply_propval union
1772 * This function gets called when an application tries to get the
1773 * chargalg properties by reading the sysfs files.
1774 * status: charging/discharging/full/unknown
1775 * health: health of the battery
1776 * Returns error code in case of failure else 0 on success
1778 static int abx500_chargalg_get_property(struct power_supply *psy,
1779 enum power_supply_property psp,
1780 union power_supply_propval *val)
1782 struct abx500_chargalg *di;
1784 di = to_abx500_chargalg_device_info(psy);
1787 case POWER_SUPPLY_PROP_STATUS:
1788 val->intval = di->charge_status;
1790 case POWER_SUPPLY_PROP_HEALTH:
1791 if (di->events.batt_ovv) {
1792 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
1793 } else if (di->events.btemp_underover) {
1794 if (di->batt_data.temp <= di->bm->temp_under)
1795 val->intval = POWER_SUPPLY_HEALTH_COLD;
1797 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
1798 } else if (di->charge_state == STATE_SAFETY_TIMER_EXPIRED ||
1799 di->charge_state == STATE_SAFETY_TIMER_EXPIRED_INIT) {
1800 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
1802 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1811 /* Exposure to the sysfs interface */
1813 static ssize_t abx500_chargalg_curr_step_show(struct abx500_chargalg *di,
1816 return sprintf(buf, "%d\n", di->curr_status.curr_step);
1819 static ssize_t abx500_chargalg_curr_step_store(struct abx500_chargalg *di,
1820 const char *buf, size_t length)
1825 ret = kstrtol(buf, 10, ¶m);
1829 di->curr_status.curr_step = param;
1830 if (di->curr_status.curr_step >= CHARGALG_CURR_STEP_LOW &&
1831 di->curr_status.curr_step <= CHARGALG_CURR_STEP_HIGH) {
1832 di->curr_status.curr_step_change = true;
1833 queue_work(di->chargalg_wq, &di->chargalg_work);
1835 dev_info(di->dev, "Wrong current step\n"
1836 "Enter 0. Disable AC/USB Charging\n"
1837 "1--100. Set AC/USB charging current step\n"
1838 "100. Enable AC/USB Charging\n");
1844 static ssize_t abx500_chargalg_en_show(struct abx500_chargalg *di,
1847 return sprintf(buf, "%d\n",
1848 di->susp_status.ac_suspended &&
1849 di->susp_status.usb_suspended);
1852 static ssize_t abx500_chargalg_en_store(struct abx500_chargalg *di,
1853 const char *buf, size_t length)
1859 ret = kstrtol(buf, 10, ¶m);
1866 /* Disable charging */
1867 di->susp_status.ac_suspended = true;
1868 di->susp_status.usb_suspended = true;
1869 di->susp_status.suspended_change = true;
1870 /* Trigger a state change */
1871 queue_work(di->chargalg_wq,
1872 &di->chargalg_work);
1875 /* Enable AC Charging */
1876 di->susp_status.ac_suspended = false;
1877 di->susp_status.suspended_change = true;
1878 /* Trigger a state change */
1879 queue_work(di->chargalg_wq,
1880 &di->chargalg_work);
1883 /* Enable USB charging */
1884 di->susp_status.usb_suspended = false;
1885 di->susp_status.suspended_change = true;
1886 /* Trigger a state change */
1887 queue_work(di->chargalg_wq,
1888 &di->chargalg_work);
1891 dev_info(di->dev, "Wrong input\n"
1892 "Enter 0. Disable AC/USB Charging\n"
1893 "1. Enable AC charging\n"
1894 "2. Enable USB Charging\n");
1899 static struct abx500_chargalg_sysfs_entry abx500_chargalg_en_charger =
1900 __ATTR(chargalg, 0644, abx500_chargalg_en_show,
1901 abx500_chargalg_en_store);
1903 static struct abx500_chargalg_sysfs_entry abx500_chargalg_curr_step =
1904 __ATTR(chargalg_curr_step, 0644, abx500_chargalg_curr_step_show,
1905 abx500_chargalg_curr_step_store);
1907 static ssize_t abx500_chargalg_sysfs_show(struct kobject *kobj,
1908 struct attribute *attr, char *buf)
1910 struct abx500_chargalg_sysfs_entry *entry = container_of(attr,
1911 struct abx500_chargalg_sysfs_entry, attr);
1913 struct abx500_chargalg *di = container_of(kobj,
1914 struct abx500_chargalg, chargalg_kobject);
1919 return entry->show(di, buf);
1922 static ssize_t abx500_chargalg_sysfs_charger(struct kobject *kobj,
1923 struct attribute *attr, const char *buf, size_t length)
1925 struct abx500_chargalg_sysfs_entry *entry = container_of(attr,
1926 struct abx500_chargalg_sysfs_entry, attr);
1928 struct abx500_chargalg *di = container_of(kobj,
1929 struct abx500_chargalg, chargalg_kobject);
1934 return entry->store(di, buf, length);
1937 static struct attribute *abx500_chargalg_chg[] = {
1938 &abx500_chargalg_en_charger.attr,
1939 &abx500_chargalg_curr_step.attr,
1943 static const struct sysfs_ops abx500_chargalg_sysfs_ops = {
1944 .show = abx500_chargalg_sysfs_show,
1945 .store = abx500_chargalg_sysfs_charger,
1948 static struct kobj_type abx500_chargalg_ktype = {
1949 .sysfs_ops = &abx500_chargalg_sysfs_ops,
1950 .default_attrs = abx500_chargalg_chg,
1954 * abx500_chargalg_sysfs_exit() - de-init of sysfs entry
1955 * @di: pointer to the struct abx500_chargalg
1957 * This function removes the entry in sysfs.
1959 static void abx500_chargalg_sysfs_exit(struct abx500_chargalg *di)
1961 kobject_del(&di->chargalg_kobject);
1965 * abx500_chargalg_sysfs_init() - init of sysfs entry
1966 * @di: pointer to the struct abx500_chargalg
1968 * This function adds an entry in sysfs.
1969 * Returns error code in case of failure else 0(on success)
1971 static int abx500_chargalg_sysfs_init(struct abx500_chargalg *di)
1975 ret = kobject_init_and_add(&di->chargalg_kobject,
1976 &abx500_chargalg_ktype,
1977 NULL, "abx500_chargalg");
1979 dev_err(di->dev, "failed to create sysfs entry\n");
1983 /* Exposure to the sysfs interface <<END>> */
1985 #if defined(CONFIG_PM)
1986 static int abx500_chargalg_resume(struct platform_device *pdev)
1988 struct abx500_chargalg *di = platform_get_drvdata(pdev);
1990 /* Kick charger watchdog if charging (any charger online) */
1991 if (di->chg_info.online_chg)
1992 queue_delayed_work(di->chargalg_wq, &di->chargalg_wd_work, 0);
1995 * Run the charging algorithm directly to be sure we don't
1998 queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
2003 static int abx500_chargalg_suspend(struct platform_device *pdev,
2006 struct abx500_chargalg *di = platform_get_drvdata(pdev);
2008 if (di->chg_info.online_chg)
2009 cancel_delayed_work_sync(&di->chargalg_wd_work);
2011 cancel_delayed_work_sync(&di->chargalg_periodic_work);
2016 #define abx500_chargalg_suspend NULL
2017 #define abx500_chargalg_resume NULL
2020 static int abx500_chargalg_remove(struct platform_device *pdev)
2022 struct abx500_chargalg *di = platform_get_drvdata(pdev);
2024 /* sysfs interface to enable/disbale charging from user space */
2025 abx500_chargalg_sysfs_exit(di);
2027 hrtimer_cancel(&di->safety_timer);
2028 hrtimer_cancel(&di->maintenance_timer);
2030 cancel_delayed_work_sync(&di->chargalg_periodic_work);
2031 cancel_delayed_work_sync(&di->chargalg_wd_work);
2032 cancel_work_sync(&di->chargalg_work);
2034 /* Delete the work queue */
2035 destroy_workqueue(di->chargalg_wq);
2037 power_supply_unregister(&di->chargalg_psy);
2042 static char *supply_interface[] = {
2046 static int abx500_chargalg_probe(struct platform_device *pdev)
2048 struct device_node *np = pdev->dev.of_node;
2049 struct abx500_bm_data *plat = pdev->dev.platform_data;
2050 struct abx500_chargalg *di;
2053 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
2055 dev_err(&pdev->dev, "%s no mem for ab8500_chargalg\n", __func__);
2060 dev_err(&pdev->dev, "no battery management data supplied\n");
2066 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
2068 dev_err(&pdev->dev, "failed to get battery information\n");
2073 /* get device struct and parent */
2074 di->dev = &pdev->dev;
2075 di->parent = dev_get_drvdata(pdev->dev.parent);
2077 /* chargalg supply */
2078 di->chargalg_psy.name = "abx500_chargalg";
2079 di->chargalg_psy.type = POWER_SUPPLY_TYPE_BATTERY;
2080 di->chargalg_psy.properties = abx500_chargalg_props;
2081 di->chargalg_psy.num_properties = ARRAY_SIZE(abx500_chargalg_props);
2082 di->chargalg_psy.get_property = abx500_chargalg_get_property;
2083 di->chargalg_psy.supplied_to = supply_interface;
2084 di->chargalg_psy.num_supplicants = ARRAY_SIZE(supply_interface),
2085 di->chargalg_psy.external_power_changed =
2086 abx500_chargalg_external_power_changed;
2088 /* Initilialize safety timer */
2089 hrtimer_init(&di->safety_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
2090 di->safety_timer.function = abx500_chargalg_safety_timer_expired;
2092 /* Initilialize maintenance timer */
2093 hrtimer_init(&di->maintenance_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
2094 di->maintenance_timer.function =
2095 abx500_chargalg_maintenance_timer_expired;
2097 /* Create a work queue for the chargalg */
2099 create_singlethread_workqueue("abx500_chargalg_wq");
2100 if (di->chargalg_wq == NULL) {
2101 dev_err(di->dev, "failed to create work queue\n");
2105 /* Init work for chargalg */
2106 INIT_DEFERRABLE_WORK(&di->chargalg_periodic_work,
2107 abx500_chargalg_periodic_work);
2108 INIT_DEFERRABLE_WORK(&di->chargalg_wd_work,
2109 abx500_chargalg_wd_work);
2111 /* Init work for chargalg */
2112 INIT_WORK(&di->chargalg_work, abx500_chargalg_work);
2114 /* To detect charger at startup */
2115 di->chg_info.prev_conn_chg = -1;
2117 /* Register chargalg power supply class */
2118 ret = power_supply_register(di->dev, &di->chargalg_psy);
2120 dev_err(di->dev, "failed to register chargalg psy\n");
2121 goto free_chargalg_wq;
2124 platform_set_drvdata(pdev, di);
2126 /* sysfs interface to enable/disable charging from user space */
2127 ret = abx500_chargalg_sysfs_init(di);
2129 dev_err(di->dev, "failed to create sysfs entry\n");
2132 di->curr_status.curr_step = CHARGALG_CURR_STEP_HIGH;
2134 /* Run the charging algorithm */
2135 queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
2137 dev_info(di->dev, "probe success\n");
2141 power_supply_unregister(&di->chargalg_psy);
2143 destroy_workqueue(di->chargalg_wq);
2147 static const struct of_device_id ab8500_chargalg_match[] = {
2148 { .compatible = "stericsson,ab8500-chargalg", },
2152 static struct platform_driver abx500_chargalg_driver = {
2153 .probe = abx500_chargalg_probe,
2154 .remove = abx500_chargalg_remove,
2155 .suspend = abx500_chargalg_suspend,
2156 .resume = abx500_chargalg_resume,
2158 .name = "ab8500-chargalg",
2159 .owner = THIS_MODULE,
2160 .of_match_table = ab8500_chargalg_match,
2164 module_platform_driver(abx500_chargalg_driver);
2166 MODULE_LICENSE("GPL v2");
2167 MODULE_AUTHOR("Johan Palsson, Karl Komierowski");
2168 MODULE_ALIAS("platform:abx500-chargalg");
2169 MODULE_DESCRIPTION("abx500 battery charging algorithm");