2 * Copyright (C) ST-Ericsson SA 2012
4 * Charging algorithm driver for abx500 variants
6 * License Terms: GNU General Public License v2
8 * Johan Palsson <johan.palsson@stericsson.com>
9 * Karl Komierowski <karl.komierowski@stericsson.com>
10 * Arun R Murthy <arun.murthy@stericsson.com>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/interrupt.h>
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <linux/platform_device.h>
20 #include <linux/power_supply.h>
21 #include <linux/completion.h>
22 #include <linux/workqueue.h>
23 #include <linux/kobject.h>
25 #include <linux/mfd/core.h>
26 #include <linux/mfd/abx500.h>
27 #include <linux/mfd/abx500/ux500_chargalg.h>
28 #include <linux/mfd/abx500/ab8500-bm.h>
29 #include <linux/notifier.h>
31 /* Watchdog kick interval */
32 #define CHG_WD_INTERVAL (6 * HZ)
34 /* End-of-charge criteria counter */
35 #define EOC_COND_CNT 10
37 #define to_abx500_chargalg_device_info(x) container_of((x), \
38 struct abx500_chargalg, chargalg_psy);
40 enum abx500_chargers {
46 struct abx500_chargalg_charger_info {
47 enum abx500_chargers conn_chg;
48 enum abx500_chargers prev_conn_chg;
49 enum abx500_chargers online_chg;
50 enum abx500_chargers prev_online_chg;
51 enum abx500_chargers charger_type;
64 struct abx500_chargalg_suspension_status {
65 bool suspended_change;
70 struct abx500_chargalg_battery_data {
78 enum abx500_chargalg_states {
81 STATE_CHG_NOT_OK_INIT,
83 STATE_HW_TEMP_PROTECT_INIT,
84 STATE_HW_TEMP_PROTECT,
87 STATE_WAIT_FOR_RECHARGE_INIT,
88 STATE_WAIT_FOR_RECHARGE,
89 STATE_MAINTENANCE_A_INIT,
91 STATE_MAINTENANCE_B_INIT,
93 STATE_TEMP_UNDEROVER_INIT,
95 STATE_TEMP_LOWHIGH_INIT,
99 STATE_OVV_PROTECT_INIT,
101 STATE_SAFETY_TIMER_EXPIRED_INIT,
102 STATE_SAFETY_TIMER_EXPIRED,
103 STATE_BATT_REMOVED_INIT,
105 STATE_WD_EXPIRED_INIT,
109 static const char *states[] = {
114 "HW_TEMP_PROTECT_INIT",
118 "WAIT_FOR_RECHARGE_INIT",
120 "MAINTENANCE_A_INIT",
122 "MAINTENANCE_B_INIT",
124 "TEMP_UNDEROVER_INIT",
132 "SAFETY_TIMER_EXPIRED_INIT",
133 "SAFETY_TIMER_EXPIRED",
140 struct abx500_chargalg_events {
145 bool btemp_underover;
147 bool main_thermal_prot;
148 bool usb_thermal_prot;
151 bool usbchargernotok;
152 bool safety_timer_expired;
153 bool maintenance_timer_expired;
162 * struct abx500_charge_curr_maximization - Charger maximization parameters
163 * @original_iset: the non optimized/maximised charger current
164 * @current_iset: the charging current used at this moment
165 * @test_delta_i: the delta between the current we want to charge and the
166 current that is really going into the battery
167 * @condition_cnt: number of iterations needed before a new charger current
169 * @max_current: maximum charger current
170 * @wait_cnt: to avoid too fast current step down in case of charger
171 * voltage collapse, we insert this delay between step
173 * @level: tells in how many steps the charging current has been
176 struct abx500_charge_curr_maximization {
189 MAXIM_RET_IBAT_TOO_HIGH,
193 * struct abx500_chargalg - abx500 Charging algorithm device information
194 * @dev: pointer to the structure device
195 * @charge_status: battery operating status
196 * @eoc_cnt: counter used to determine end-of_charge
197 * @maintenance_chg: indicate if maintenance charge is active
198 * @t_hyst_norm temperature hysteresis when the temperature has been
199 * over or under normal limits
200 * @t_hyst_lowhigh temperature hysteresis when the temperature has been
201 * over or under the high or low limits
202 * @charge_state: current state of the charging algorithm
203 * @ccm charging current maximization parameters
204 * @chg_info: information about connected charger types
205 * @batt_data: data of the battery
206 * @susp_status: current charger suspension status
207 * @bm: Platform specific battery management information
208 * @parent: pointer to the struct abx500
209 * @chargalg_psy: structure that holds the battery properties exposed by
210 * the charging algorithm
211 * @events: structure for information about events triggered
212 * @chargalg_wq: work queue for running the charging algorithm
213 * @chargalg_periodic_work: work to run the charging algorithm periodically
214 * @chargalg_wd_work: work to kick the charger watchdog periodically
215 * @chargalg_work: work to run the charging algorithm instantly
216 * @safety_timer: charging safety timer
217 * @maintenance_timer: maintenance charging timer
218 * @chargalg_kobject: structure of type kobject
220 struct abx500_chargalg {
224 bool maintenance_chg;
227 enum abx500_chargalg_states charge_state;
228 struct abx500_charge_curr_maximization ccm;
229 struct abx500_chargalg_charger_info chg_info;
230 struct abx500_chargalg_battery_data batt_data;
231 struct abx500_chargalg_suspension_status susp_status;
232 struct ab8500 *parent;
233 struct abx500_bm_data *bm;
234 struct power_supply chargalg_psy;
235 struct ux500_charger *ac_chg;
236 struct ux500_charger *usb_chg;
237 struct abx500_chargalg_events events;
238 struct workqueue_struct *chargalg_wq;
239 struct delayed_work chargalg_periodic_work;
240 struct delayed_work chargalg_wd_work;
241 struct work_struct chargalg_work;
242 struct timer_list safety_timer;
243 struct timer_list maintenance_timer;
244 struct kobject chargalg_kobject;
247 /*External charger prepare notifier*/
248 BLOCKING_NOTIFIER_HEAD(charger_notifier_list);
250 /* Main battery properties */
251 static enum power_supply_property abx500_chargalg_props[] = {
252 POWER_SUPPLY_PROP_STATUS,
253 POWER_SUPPLY_PROP_HEALTH,
257 * abx500_chargalg_safety_timer_expired() - Expiration of the safety timer
258 * @data: pointer to the abx500_chargalg structure
260 * This function gets called when the safety timer for the charger
263 static void abx500_chargalg_safety_timer_expired(unsigned long data)
265 struct abx500_chargalg *di = (struct abx500_chargalg *) data;
266 dev_err(di->dev, "Safety timer expired\n");
267 di->events.safety_timer_expired = true;
269 /* Trigger execution of the algorithm instantly */
270 queue_work(di->chargalg_wq, &di->chargalg_work);
274 * abx500_chargalg_maintenance_timer_expired() - Expiration of
275 * the maintenance timer
276 * @i: pointer to the abx500_chargalg structure
278 * This function gets called when the maintenence timer
281 static void abx500_chargalg_maintenance_timer_expired(unsigned long data)
284 struct abx500_chargalg *di = (struct abx500_chargalg *) data;
285 dev_dbg(di->dev, "Maintenance timer expired\n");
286 di->events.maintenance_timer_expired = true;
288 /* Trigger execution of the algorithm instantly */
289 queue_work(di->chargalg_wq, &di->chargalg_work);
293 * abx500_chargalg_state_to() - Change charge state
294 * @di: pointer to the abx500_chargalg structure
296 * This function gets called when a charge state change should occur
298 static void abx500_chargalg_state_to(struct abx500_chargalg *di,
299 enum abx500_chargalg_states state)
302 "State changed: %s (From state: [%d] %s =to=> [%d] %s )\n",
303 di->charge_state == state ? "NO" : "YES",
305 states[di->charge_state],
309 di->charge_state = state;
312 static int abx500_chargalg_check_charger_enable(struct abx500_chargalg *di)
314 switch (di->charge_state) {
316 case STATE_MAINTENANCE_A:
317 case STATE_MAINTENANCE_B:
323 if (di->chg_info.charger_type & USB_CHG) {
324 return di->usb_chg->ops.check_enable(di->usb_chg,
325 di->bm->bat_type[di->bm->batt_id].normal_vol_lvl,
326 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
327 } else if ((di->chg_info.charger_type & AC_CHG) &&
328 !(di->ac_chg->external)) {
329 return di->ac_chg->ops.check_enable(di->ac_chg,
330 di->bm->bat_type[di->bm->batt_id].normal_vol_lvl,
331 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
337 * abx500_chargalg_check_charger_connection() - Check charger connection change
338 * @di: pointer to the abx500_chargalg structure
340 * This function will check if there is a change in the charger connection
341 * and change charge state accordingly. AC has precedence over USB.
343 static int abx500_chargalg_check_charger_connection(struct abx500_chargalg *di)
345 if (di->chg_info.conn_chg != di->chg_info.prev_conn_chg ||
346 di->susp_status.suspended_change) {
348 * Charger state changed or suspension
349 * has changed since last update
351 if ((di->chg_info.conn_chg & AC_CHG) &&
352 !di->susp_status.ac_suspended) {
353 dev_dbg(di->dev, "Charging source is AC\n");
354 if (di->chg_info.charger_type != AC_CHG) {
355 di->chg_info.charger_type = AC_CHG;
356 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
358 } else if ((di->chg_info.conn_chg & USB_CHG) &&
359 !di->susp_status.usb_suspended) {
360 dev_dbg(di->dev, "Charging source is USB\n");
361 di->chg_info.charger_type = USB_CHG;
362 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
363 } else if (di->chg_info.conn_chg &&
364 (di->susp_status.ac_suspended ||
365 di->susp_status.usb_suspended)) {
366 dev_dbg(di->dev, "Charging is suspended\n");
367 di->chg_info.charger_type = NO_CHG;
368 abx500_chargalg_state_to(di, STATE_SUSPENDED_INIT);
370 dev_dbg(di->dev, "Charging source is OFF\n");
371 di->chg_info.charger_type = NO_CHG;
372 abx500_chargalg_state_to(di, STATE_HANDHELD_INIT);
374 di->chg_info.prev_conn_chg = di->chg_info.conn_chg;
375 di->susp_status.suspended_change = false;
377 return di->chg_info.conn_chg;
381 * abx500_chargalg_start_safety_timer() - Start charging safety timer
382 * @di: pointer to the abx500_chargalg structure
384 * The safety timer is used to avoid overcharging of old or bad batteries.
385 * There are different timers for AC and USB
387 static void abx500_chargalg_start_safety_timer(struct abx500_chargalg *di)
389 unsigned long timer_expiration = 0;
391 switch (di->chg_info.charger_type) {
394 round_jiffies(jiffies +
395 (di->bm->main_safety_tmr_h * 3600 * HZ));
400 round_jiffies(jiffies +
401 (di->bm->usb_safety_tmr_h * 3600 * HZ));
405 dev_err(di->dev, "Unknown charger to charge from\n");
409 di->events.safety_timer_expired = false;
410 di->safety_timer.expires = timer_expiration;
411 if (!timer_pending(&di->safety_timer))
412 add_timer(&di->safety_timer);
414 mod_timer(&di->safety_timer, timer_expiration);
418 * abx500_chargalg_stop_safety_timer() - Stop charging safety timer
419 * @di: pointer to the abx500_chargalg structure
421 * The safety timer is stopped whenever the NORMAL state is exited
423 static void abx500_chargalg_stop_safety_timer(struct abx500_chargalg *di)
425 di->events.safety_timer_expired = false;
426 del_timer(&di->safety_timer);
430 * abx500_chargalg_start_maintenance_timer() - Start charging maintenance timer
431 * @di: pointer to the abx500_chargalg structure
432 * @duration: duration of ther maintenance timer in hours
434 * The maintenance timer is used to maintain the charge in the battery once
435 * the battery is considered full. These timers are chosen to match the
436 * discharge curve of the battery
438 static void abx500_chargalg_start_maintenance_timer(struct abx500_chargalg *di,
441 unsigned long timer_expiration;
443 /* Convert from hours to jiffies */
444 timer_expiration = round_jiffies(jiffies + (duration * 3600 * HZ));
446 di->events.maintenance_timer_expired = false;
447 di->maintenance_timer.expires = timer_expiration;
448 if (!timer_pending(&di->maintenance_timer))
449 add_timer(&di->maintenance_timer);
451 mod_timer(&di->maintenance_timer, timer_expiration);
455 * abx500_chargalg_stop_maintenance_timer() - Stop maintenance timer
456 * @di: pointer to the abx500_chargalg structure
458 * The maintenance timer is stopped whenever maintenance ends or when another
461 static void abx500_chargalg_stop_maintenance_timer(struct abx500_chargalg *di)
463 di->events.maintenance_timer_expired = false;
464 del_timer(&di->maintenance_timer);
468 * abx500_chargalg_kick_watchdog() - Kick charger watchdog
469 * @di: pointer to the abx500_chargalg structure
471 * The charger watchdog have to be kicked periodically whenever the charger is
472 * on, else the ABB will reset the system
474 static int abx500_chargalg_kick_watchdog(struct abx500_chargalg *di)
476 /* Check if charger exists and kick watchdog if charging */
477 if (di->ac_chg && di->ac_chg->ops.kick_wd &&
478 di->chg_info.online_chg & AC_CHG) {
480 * If AB charger watchdog expired, pm2xxx charging
481 * gets disabled. To be safe, kick both AB charger watchdog
482 * and pm2xxx watchdog.
484 if (di->ac_chg->external &&
485 di->usb_chg && di->usb_chg->ops.kick_wd)
486 di->usb_chg->ops.kick_wd(di->usb_chg);
488 return di->ac_chg->ops.kick_wd(di->ac_chg);
490 else if (di->usb_chg && di->usb_chg->ops.kick_wd &&
491 di->chg_info.online_chg & USB_CHG)
492 return di->usb_chg->ops.kick_wd(di->usb_chg);
498 * abx500_chargalg_ac_en() - Turn on/off the AC charger
499 * @di: pointer to the abx500_chargalg structure
500 * @enable: charger on/off
501 * @vset: requested charger output voltage
502 * @iset: requested charger output current
504 * The AC charger will be turned on/off with the requested charge voltage and
507 static int abx500_chargalg_ac_en(struct abx500_chargalg *di, int enable,
510 static int abx500_chargalg_ex_ac_enable_toggle;
512 if (!di->ac_chg || !di->ac_chg->ops.enable)
515 /* Select maximum of what both the charger and the battery supports */
516 if (di->ac_chg->max_out_volt)
517 vset = min(vset, di->ac_chg->max_out_volt);
518 if (di->ac_chg->max_out_curr)
519 iset = min(iset, di->ac_chg->max_out_curr);
521 di->chg_info.ac_iset = iset;
522 di->chg_info.ac_vset = vset;
524 /* Enable external charger */
525 if (enable && di->ac_chg->external &&
526 !abx500_chargalg_ex_ac_enable_toggle) {
527 blocking_notifier_call_chain(&charger_notifier_list,
529 abx500_chargalg_ex_ac_enable_toggle++;
532 return di->ac_chg->ops.enable(di->ac_chg, enable, vset, iset);
536 * abx500_chargalg_usb_en() - Turn on/off the USB charger
537 * @di: pointer to the abx500_chargalg structure
538 * @enable: charger on/off
539 * @vset: requested charger output voltage
540 * @iset: requested charger output current
542 * The USB charger will be turned on/off with the requested charge voltage and
545 static int abx500_chargalg_usb_en(struct abx500_chargalg *di, int enable,
548 if (!di->usb_chg || !di->usb_chg->ops.enable)
551 /* Select maximum of what both the charger and the battery supports */
552 if (di->usb_chg->max_out_volt)
553 vset = min(vset, di->usb_chg->max_out_volt);
554 if (di->usb_chg->max_out_curr)
555 iset = min(iset, di->usb_chg->max_out_curr);
557 di->chg_info.usb_iset = iset;
558 di->chg_info.usb_vset = vset;
560 return di->usb_chg->ops.enable(di->usb_chg, enable, vset, iset);
564 * abx500_chargalg_update_chg_curr() - Update charger current
565 * @di: pointer to the abx500_chargalg structure
566 * @iset: requested charger output current
568 * The charger output current will be updated for the charger
569 * that is currently in use
571 static int abx500_chargalg_update_chg_curr(struct abx500_chargalg *di,
574 /* Check if charger exists and update current if charging */
575 if (di->ac_chg && di->ac_chg->ops.update_curr &&
576 di->chg_info.charger_type & AC_CHG) {
578 * Select maximum of what both the charger
579 * and the battery supports
581 if (di->ac_chg->max_out_curr)
582 iset = min(iset, di->ac_chg->max_out_curr);
584 di->chg_info.ac_iset = iset;
586 return di->ac_chg->ops.update_curr(di->ac_chg, iset);
587 } else if (di->usb_chg && di->usb_chg->ops.update_curr &&
588 di->chg_info.charger_type & USB_CHG) {
590 * Select maximum of what both the charger
591 * and the battery supports
593 if (di->usb_chg->max_out_curr)
594 iset = min(iset, di->usb_chg->max_out_curr);
596 di->chg_info.usb_iset = iset;
598 return di->usb_chg->ops.update_curr(di->usb_chg, iset);
605 * abx500_chargalg_stop_charging() - Stop charging
606 * @di: pointer to the abx500_chargalg structure
608 * This function is called from any state where charging should be stopped.
609 * All charging is disabled and all status parameters and timers are changed
612 static void abx500_chargalg_stop_charging(struct abx500_chargalg *di)
614 abx500_chargalg_ac_en(di, false, 0, 0);
615 abx500_chargalg_usb_en(di, false, 0, 0);
616 abx500_chargalg_stop_safety_timer(di);
617 abx500_chargalg_stop_maintenance_timer(di);
618 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
619 di->maintenance_chg = false;
620 cancel_delayed_work(&di->chargalg_wd_work);
621 power_supply_changed(&di->chargalg_psy);
625 * abx500_chargalg_hold_charging() - Pauses charging
626 * @di: pointer to the abx500_chargalg structure
628 * This function is called in the case where maintenance charging has been
629 * disabled and instead a battery voltage mode is entered to check when the
630 * battery voltage has reached a certain recharge voltage
632 static void abx500_chargalg_hold_charging(struct abx500_chargalg *di)
634 abx500_chargalg_ac_en(di, false, 0, 0);
635 abx500_chargalg_usb_en(di, false, 0, 0);
636 abx500_chargalg_stop_safety_timer(di);
637 abx500_chargalg_stop_maintenance_timer(di);
638 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
639 di->maintenance_chg = false;
640 cancel_delayed_work(&di->chargalg_wd_work);
641 power_supply_changed(&di->chargalg_psy);
645 * abx500_chargalg_start_charging() - Start the charger
646 * @di: pointer to the abx500_chargalg structure
647 * @vset: requested charger output voltage
648 * @iset: requested charger output current
650 * A charger will be enabled depending on the requested charger type that was
651 * detected previously.
653 static void abx500_chargalg_start_charging(struct abx500_chargalg *di,
656 bool start_chargalg_wd = true;
658 switch (di->chg_info.charger_type) {
661 "AC parameters: Vset %d, Ich %d\n", vset, iset);
662 abx500_chargalg_usb_en(di, false, 0, 0);
663 abx500_chargalg_ac_en(di, true, vset, iset);
668 "USB parameters: Vset %d, Ich %d\n", vset, iset);
669 abx500_chargalg_ac_en(di, false, 0, 0);
670 abx500_chargalg_usb_en(di, true, vset, iset);
674 dev_err(di->dev, "Unknown charger to charge from\n");
675 start_chargalg_wd = false;
679 if (start_chargalg_wd && !delayed_work_pending(&di->chargalg_wd_work))
680 queue_delayed_work(di->chargalg_wq, &di->chargalg_wd_work, 0);
684 * abx500_chargalg_check_temp() - Check battery temperature ranges
685 * @di: pointer to the abx500_chargalg structure
687 * The battery temperature is checked against the predefined limits and the
688 * charge state is changed accordingly
690 static void abx500_chargalg_check_temp(struct abx500_chargalg *di)
692 if (di->batt_data.temp > (di->bm->temp_low + di->t_hyst_norm) &&
693 di->batt_data.temp < (di->bm->temp_high - di->t_hyst_norm)) {
695 di->events.btemp_underover = false;
696 di->events.btemp_lowhigh = false;
698 di->t_hyst_lowhigh = 0;
700 if (((di->batt_data.temp >= di->bm->temp_high) &&
701 (di->batt_data.temp <
702 (di->bm->temp_over - di->t_hyst_lowhigh))) ||
703 ((di->batt_data.temp >
704 (di->bm->temp_under + di->t_hyst_lowhigh)) &&
705 (di->batt_data.temp <= di->bm->temp_low))) {
706 /* TEMP minor!!!!! */
707 di->events.btemp_underover = false;
708 di->events.btemp_lowhigh = true;
709 di->t_hyst_norm = di->bm->temp_hysteresis;
710 di->t_hyst_lowhigh = 0;
711 } else if (di->batt_data.temp <= di->bm->temp_under ||
712 di->batt_data.temp >= di->bm->temp_over) {
713 /* TEMP major!!!!! */
714 di->events.btemp_underover = true;
715 di->events.btemp_lowhigh = false;
717 di->t_hyst_lowhigh = di->bm->temp_hysteresis;
719 /* Within hysteresis */
720 dev_dbg(di->dev, "Within hysteresis limit temp: %d "
721 "hyst_lowhigh %d, hyst normal %d\n",
722 di->batt_data.temp, di->t_hyst_lowhigh,
729 * abx500_chargalg_check_charger_voltage() - Check charger voltage
730 * @di: pointer to the abx500_chargalg structure
732 * Charger voltage is checked against maximum limit
734 static void abx500_chargalg_check_charger_voltage(struct abx500_chargalg *di)
736 if (di->chg_info.usb_volt > di->bm->chg_params->usb_volt_max)
737 di->chg_info.usb_chg_ok = false;
739 di->chg_info.usb_chg_ok = true;
741 if (di->chg_info.ac_volt > di->bm->chg_params->ac_volt_max)
742 di->chg_info.ac_chg_ok = false;
744 di->chg_info.ac_chg_ok = true;
749 * abx500_chargalg_end_of_charge() - Check if end-of-charge criteria is fulfilled
750 * @di: pointer to the abx500_chargalg structure
752 * End-of-charge criteria is fulfilled when the battery voltage is above a
753 * certain limit and the battery current is below a certain limit for a
754 * predefined number of consecutive seconds. If true, the battery is full
756 static void abx500_chargalg_end_of_charge(struct abx500_chargalg *di)
758 if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING &&
759 di->charge_state == STATE_NORMAL &&
760 !di->maintenance_chg && (di->batt_data.volt >=
761 di->bm->bat_type[di->bm->batt_id].termination_vol ||
762 di->events.usb_cv_active || di->events.ac_cv_active) &&
763 di->batt_data.avg_curr <
764 di->bm->bat_type[di->bm->batt_id].termination_curr &&
765 di->batt_data.avg_curr > 0) {
766 if (++di->eoc_cnt >= EOC_COND_CNT) {
768 di->charge_status = POWER_SUPPLY_STATUS_FULL;
769 di->maintenance_chg = true;
770 dev_dbg(di->dev, "EOC reached!\n");
771 power_supply_changed(&di->chargalg_psy);
774 " EOC limit reached for the %d"
775 " time, out of %d before EOC\n",
784 static void init_maxim_chg_curr(struct abx500_chargalg *di)
786 di->ccm.original_iset =
787 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl;
788 di->ccm.current_iset =
789 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl;
790 di->ccm.test_delta_i = di->bm->maxi->charger_curr_step;
791 di->ccm.max_current = di->bm->maxi->chg_curr;
792 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
797 * abx500_chargalg_chg_curr_maxim - increases the charger current to
798 * compensate for the system load
799 * @di pointer to the abx500_chargalg structure
801 * This maximization function is used to raise the charger current to get the
802 * battery current as close to the optimal value as possible. The battery
803 * current during charging is affected by the system load
805 static enum maxim_ret abx500_chargalg_chg_curr_maxim(struct abx500_chargalg *di)
809 if (!di->bm->maxi->ena_maxi)
810 return MAXIM_RET_NOACTION;
812 delta_i = di->ccm.original_iset - di->batt_data.inst_curr;
814 if (di->events.vbus_collapsed) {
815 dev_dbg(di->dev, "Charger voltage has collapsed %d\n",
817 if (di->ccm.wait_cnt == 0) {
818 dev_dbg(di->dev, "lowering current\n");
820 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
821 di->ccm.max_current =
822 di->ccm.current_iset - di->ccm.test_delta_i;
823 di->ccm.current_iset = di->ccm.max_current;
825 return MAXIM_RET_CHANGE;
827 dev_dbg(di->dev, "waiting\n");
828 /* Let's go in here twice before lowering curr again */
829 di->ccm.wait_cnt = (di->ccm.wait_cnt + 1) % 3;
830 return MAXIM_RET_NOACTION;
834 di->ccm.wait_cnt = 0;
836 if ((di->batt_data.inst_curr > di->ccm.original_iset)) {
837 dev_dbg(di->dev, " Maximization Ibat (%dmA) too high"
838 " (limit %dmA) (current iset: %dmA)!\n",
839 di->batt_data.inst_curr, di->ccm.original_iset,
840 di->ccm.current_iset);
842 if (di->ccm.current_iset == di->ccm.original_iset)
843 return MAXIM_RET_NOACTION;
845 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
846 di->ccm.current_iset = di->ccm.original_iset;
849 return MAXIM_RET_IBAT_TOO_HIGH;
852 if (delta_i > di->ccm.test_delta_i &&
853 (di->ccm.current_iset + di->ccm.test_delta_i) <
854 di->ccm.max_current) {
855 if (di->ccm.condition_cnt-- == 0) {
856 /* Increse the iset with cco.test_delta_i */
857 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
858 di->ccm.current_iset += di->ccm.test_delta_i;
860 dev_dbg(di->dev, " Maximization needed, increase"
861 " with %d mA to %dmA (Optimal ibat: %d)"
863 di->ccm.test_delta_i,
864 di->ccm.current_iset,
865 di->ccm.original_iset,
867 return MAXIM_RET_CHANGE;
869 return MAXIM_RET_NOACTION;
872 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
873 return MAXIM_RET_NOACTION;
877 static void handle_maxim_chg_curr(struct abx500_chargalg *di)
882 ret = abx500_chargalg_chg_curr_maxim(di);
884 case MAXIM_RET_CHANGE:
885 result = abx500_chargalg_update_chg_curr(di,
886 di->ccm.current_iset);
888 dev_err(di->dev, "failed to set chg curr\n");
890 case MAXIM_RET_IBAT_TOO_HIGH:
891 result = abx500_chargalg_update_chg_curr(di,
892 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
894 dev_err(di->dev, "failed to set chg curr\n");
897 case MAXIM_RET_NOACTION:
904 static int abx500_chargalg_get_ext_psy_data(struct device *dev, void *data)
906 struct power_supply *psy;
907 struct power_supply *ext;
908 struct abx500_chargalg *di;
909 union power_supply_propval ret;
911 bool psy_found = false;
912 bool capacity_updated = false;
914 psy = (struct power_supply *)data;
915 ext = dev_get_drvdata(dev);
916 di = to_abx500_chargalg_device_info(psy);
917 /* For all psy where the driver name appears in any supplied_to */
918 for (i = 0; i < ext->num_supplicants; i++) {
919 if (!strcmp(ext->supplied_to[i], psy->name))
926 * If external is not registering 'POWER_SUPPLY_PROP_CAPACITY' to its
927 * property because of handling that sysfs entry on its own, this is
928 * the place to get the battery capacity.
930 if (!ext->get_property(ext, POWER_SUPPLY_PROP_CAPACITY, &ret)) {
931 di->batt_data.percent = ret.intval;
932 capacity_updated = true;
935 /* Go through all properties for the psy */
936 for (j = 0; j < ext->num_properties; j++) {
937 enum power_supply_property prop;
938 prop = ext->properties[j];
940 /* Initialize chargers if not already done */
942 ext->type == POWER_SUPPLY_TYPE_MAINS)
943 di->ac_chg = psy_to_ux500_charger(ext);
944 else if (!di->usb_chg &&
945 ext->type == POWER_SUPPLY_TYPE_USB)
946 di->usb_chg = psy_to_ux500_charger(ext);
948 if (ext->get_property(ext, prop, &ret))
951 case POWER_SUPPLY_PROP_PRESENT:
953 case POWER_SUPPLY_TYPE_BATTERY:
954 /* Battery present */
956 di->events.batt_rem = false;
957 /* Battery removed */
959 di->events.batt_rem = true;
961 case POWER_SUPPLY_TYPE_MAINS:
962 /* AC disconnected */
964 (di->chg_info.conn_chg & AC_CHG)) {
965 di->chg_info.prev_conn_chg =
966 di->chg_info.conn_chg;
967 di->chg_info.conn_chg &= ~AC_CHG;
970 else if (ret.intval &&
971 !(di->chg_info.conn_chg & AC_CHG)) {
972 di->chg_info.prev_conn_chg =
973 di->chg_info.conn_chg;
974 di->chg_info.conn_chg |= AC_CHG;
977 case POWER_SUPPLY_TYPE_USB:
978 /* USB disconnected */
980 (di->chg_info.conn_chg & USB_CHG)) {
981 di->chg_info.prev_conn_chg =
982 di->chg_info.conn_chg;
983 di->chg_info.conn_chg &= ~USB_CHG;
986 else if (ret.intval &&
987 !(di->chg_info.conn_chg & USB_CHG)) {
988 di->chg_info.prev_conn_chg =
989 di->chg_info.conn_chg;
990 di->chg_info.conn_chg |= USB_CHG;
998 case POWER_SUPPLY_PROP_ONLINE:
1000 case POWER_SUPPLY_TYPE_BATTERY:
1002 case POWER_SUPPLY_TYPE_MAINS:
1005 (di->chg_info.online_chg & AC_CHG)) {
1006 di->chg_info.prev_online_chg =
1007 di->chg_info.online_chg;
1008 di->chg_info.online_chg &= ~AC_CHG;
1011 else if (ret.intval &&
1012 !(di->chg_info.online_chg & AC_CHG)) {
1013 di->chg_info.prev_online_chg =
1014 di->chg_info.online_chg;
1015 di->chg_info.online_chg |= AC_CHG;
1016 queue_delayed_work(di->chargalg_wq,
1017 &di->chargalg_wd_work, 0);
1020 case POWER_SUPPLY_TYPE_USB:
1023 (di->chg_info.online_chg & USB_CHG)) {
1024 di->chg_info.prev_online_chg =
1025 di->chg_info.online_chg;
1026 di->chg_info.online_chg &= ~USB_CHG;
1029 else if (ret.intval &&
1030 !(di->chg_info.online_chg & USB_CHG)) {
1031 di->chg_info.prev_online_chg =
1032 di->chg_info.online_chg;
1033 di->chg_info.online_chg |= USB_CHG;
1034 queue_delayed_work(di->chargalg_wq,
1035 &di->chargalg_wd_work, 0);
1043 case POWER_SUPPLY_PROP_HEALTH:
1044 switch (ext->type) {
1045 case POWER_SUPPLY_TYPE_BATTERY:
1047 case POWER_SUPPLY_TYPE_MAINS:
1048 switch (ret.intval) {
1049 case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
1050 di->events.mainextchnotok = true;
1051 di->events.main_thermal_prot = false;
1052 di->events.main_ovv = false;
1053 di->events.ac_wd_expired = false;
1055 case POWER_SUPPLY_HEALTH_DEAD:
1056 di->events.ac_wd_expired = true;
1057 di->events.mainextchnotok = false;
1058 di->events.main_ovv = false;
1059 di->events.main_thermal_prot = false;
1061 case POWER_SUPPLY_HEALTH_COLD:
1062 case POWER_SUPPLY_HEALTH_OVERHEAT:
1063 di->events.main_thermal_prot = true;
1064 di->events.mainextchnotok = false;
1065 di->events.main_ovv = false;
1066 di->events.ac_wd_expired = false;
1068 case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
1069 di->events.main_ovv = true;
1070 di->events.mainextchnotok = false;
1071 di->events.main_thermal_prot = false;
1072 di->events.ac_wd_expired = false;
1074 case POWER_SUPPLY_HEALTH_GOOD:
1075 di->events.main_thermal_prot = false;
1076 di->events.mainextchnotok = false;
1077 di->events.main_ovv = false;
1078 di->events.ac_wd_expired = false;
1085 case POWER_SUPPLY_TYPE_USB:
1086 switch (ret.intval) {
1087 case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
1088 di->events.usbchargernotok = true;
1089 di->events.usb_thermal_prot = false;
1090 di->events.vbus_ovv = false;
1091 di->events.usb_wd_expired = false;
1093 case POWER_SUPPLY_HEALTH_DEAD:
1094 di->events.usb_wd_expired = true;
1095 di->events.usbchargernotok = false;
1096 di->events.usb_thermal_prot = false;
1097 di->events.vbus_ovv = false;
1099 case POWER_SUPPLY_HEALTH_COLD:
1100 case POWER_SUPPLY_HEALTH_OVERHEAT:
1101 di->events.usb_thermal_prot = true;
1102 di->events.usbchargernotok = false;
1103 di->events.vbus_ovv = false;
1104 di->events.usb_wd_expired = false;
1106 case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
1107 di->events.vbus_ovv = true;
1108 di->events.usbchargernotok = false;
1109 di->events.usb_thermal_prot = false;
1110 di->events.usb_wd_expired = false;
1112 case POWER_SUPPLY_HEALTH_GOOD:
1113 di->events.usbchargernotok = false;
1114 di->events.usb_thermal_prot = false;
1115 di->events.vbus_ovv = false;
1116 di->events.usb_wd_expired = false;
1126 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1127 switch (ext->type) {
1128 case POWER_SUPPLY_TYPE_BATTERY:
1129 di->batt_data.volt = ret.intval / 1000;
1131 case POWER_SUPPLY_TYPE_MAINS:
1132 di->chg_info.ac_volt = ret.intval / 1000;
1134 case POWER_SUPPLY_TYPE_USB:
1135 di->chg_info.usb_volt = ret.intval / 1000;
1142 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
1143 switch (ext->type) {
1144 case POWER_SUPPLY_TYPE_MAINS:
1145 /* AVG is used to indicate when we are
1148 di->events.ac_cv_active = true;
1150 di->events.ac_cv_active = false;
1153 case POWER_SUPPLY_TYPE_USB:
1154 /* AVG is used to indicate when we are
1157 di->events.usb_cv_active = true;
1159 di->events.usb_cv_active = false;
1167 case POWER_SUPPLY_PROP_TECHNOLOGY:
1168 switch (ext->type) {
1169 case POWER_SUPPLY_TYPE_BATTERY:
1171 di->events.batt_unknown = false;
1173 di->events.batt_unknown = true;
1181 case POWER_SUPPLY_PROP_TEMP:
1182 di->batt_data.temp = ret.intval / 10;
1185 case POWER_SUPPLY_PROP_CURRENT_NOW:
1186 switch (ext->type) {
1187 case POWER_SUPPLY_TYPE_MAINS:
1188 di->chg_info.ac_curr =
1191 case POWER_SUPPLY_TYPE_USB:
1192 di->chg_info.usb_curr =
1195 case POWER_SUPPLY_TYPE_BATTERY:
1196 di->batt_data.inst_curr = ret.intval / 1000;
1203 case POWER_SUPPLY_PROP_CURRENT_AVG:
1204 switch (ext->type) {
1205 case POWER_SUPPLY_TYPE_BATTERY:
1206 di->batt_data.avg_curr = ret.intval / 1000;
1208 case POWER_SUPPLY_TYPE_USB:
1210 di->events.vbus_collapsed = true;
1212 di->events.vbus_collapsed = false;
1218 case POWER_SUPPLY_PROP_CAPACITY:
1219 if (!capacity_updated)
1220 di->batt_data.percent = ret.intval;
1230 * abx500_chargalg_external_power_changed() - callback for power supply changes
1231 * @psy: pointer to the structure power_supply
1233 * This function is the entry point of the pointer external_power_changed
1234 * of the structure power_supply.
1235 * This function gets executed when there is a change in any external power
1236 * supply that this driver needs to be notified of.
1238 static void abx500_chargalg_external_power_changed(struct power_supply *psy)
1240 struct abx500_chargalg *di = to_abx500_chargalg_device_info(psy);
1243 * Trigger execution of the algorithm instantly and read
1244 * all power_supply properties there instead
1246 queue_work(di->chargalg_wq, &di->chargalg_work);
1250 * abx500_chargalg_algorithm() - Main function for the algorithm
1251 * @di: pointer to the abx500_chargalg structure
1253 * This is the main control function for the charging algorithm.
1254 * It is called periodically or when something happens that will
1255 * trigger a state change
1257 static void abx500_chargalg_algorithm(struct abx500_chargalg *di)
1262 /* Collect data from all power_supply class devices */
1263 class_for_each_device(power_supply_class, NULL,
1264 &di->chargalg_psy, abx500_chargalg_get_ext_psy_data);
1266 abx500_chargalg_end_of_charge(di);
1267 abx500_chargalg_check_temp(di);
1268 abx500_chargalg_check_charger_voltage(di);
1270 charger_status = abx500_chargalg_check_charger_connection(di);
1272 if (is_ab8500(di->parent)) {
1273 ret = abx500_chargalg_check_charger_enable(di);
1275 dev_err(di->dev, "Checking charger is enabled error"
1276 ": Returned Value %d\n", ret);
1280 * First check if we have a charger connected.
1281 * Also we don't allow charging of unknown batteries if configured
1284 if (!charger_status ||
1285 (di->events.batt_unknown && !di->bm->chg_unknown_bat)) {
1286 if (di->charge_state != STATE_HANDHELD) {
1287 di->events.safety_timer_expired = false;
1288 abx500_chargalg_state_to(di, STATE_HANDHELD_INIT);
1292 /* If suspended, we should not continue checking the flags */
1293 else if (di->charge_state == STATE_SUSPENDED_INIT ||
1294 di->charge_state == STATE_SUSPENDED) {
1295 /* We don't do anything here, just don,t continue */
1298 /* Safety timer expiration */
1299 else if (di->events.safety_timer_expired) {
1300 if (di->charge_state != STATE_SAFETY_TIMER_EXPIRED)
1301 abx500_chargalg_state_to(di,
1302 STATE_SAFETY_TIMER_EXPIRED_INIT);
1305 * Check if any interrupts has occured
1306 * that will prevent us from charging
1309 /* Battery removed */
1310 else if (di->events.batt_rem) {
1311 if (di->charge_state != STATE_BATT_REMOVED)
1312 abx500_chargalg_state_to(di, STATE_BATT_REMOVED_INIT);
1314 /* Main or USB charger not ok. */
1315 else if (di->events.mainextchnotok || di->events.usbchargernotok) {
1317 * If vbus_collapsed is set, we have to lower the charger
1318 * current, which is done in the normal state below
1320 if (di->charge_state != STATE_CHG_NOT_OK &&
1321 !di->events.vbus_collapsed)
1322 abx500_chargalg_state_to(di, STATE_CHG_NOT_OK_INIT);
1324 /* VBUS, Main or VBAT OVV. */
1325 else if (di->events.vbus_ovv ||
1326 di->events.main_ovv ||
1327 di->events.batt_ovv ||
1328 !di->chg_info.usb_chg_ok ||
1329 !di->chg_info.ac_chg_ok) {
1330 if (di->charge_state != STATE_OVV_PROTECT)
1331 abx500_chargalg_state_to(di, STATE_OVV_PROTECT_INIT);
1333 /* USB Thermal, stop charging */
1334 else if (di->events.main_thermal_prot ||
1335 di->events.usb_thermal_prot) {
1336 if (di->charge_state != STATE_HW_TEMP_PROTECT)
1337 abx500_chargalg_state_to(di,
1338 STATE_HW_TEMP_PROTECT_INIT);
1340 /* Battery temp over/under */
1341 else if (di->events.btemp_underover) {
1342 if (di->charge_state != STATE_TEMP_UNDEROVER)
1343 abx500_chargalg_state_to(di,
1344 STATE_TEMP_UNDEROVER_INIT);
1346 /* Watchdog expired */
1347 else if (di->events.ac_wd_expired ||
1348 di->events.usb_wd_expired) {
1349 if (di->charge_state != STATE_WD_EXPIRED)
1350 abx500_chargalg_state_to(di, STATE_WD_EXPIRED_INIT);
1352 /* Battery temp high/low */
1353 else if (di->events.btemp_lowhigh) {
1354 if (di->charge_state != STATE_TEMP_LOWHIGH)
1355 abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH_INIT);
1359 "[CHARGALG] Vb %d Ib_avg %d Ib_inst %d Tb %d Cap %d Maint %d "
1360 "State %s Active_chg %d Chg_status %d AC %d USB %d "
1361 "AC_online %d USB_online %d AC_CV %d USB_CV %d AC_I %d "
1362 "USB_I %d AC_Vset %d AC_Iset %d USB_Vset %d USB_Iset %d\n",
1364 di->batt_data.avg_curr,
1365 di->batt_data.inst_curr,
1367 di->batt_data.percent,
1368 di->maintenance_chg,
1369 states[di->charge_state],
1370 di->chg_info.charger_type,
1372 di->chg_info.conn_chg & AC_CHG,
1373 di->chg_info.conn_chg & USB_CHG,
1374 di->chg_info.online_chg & AC_CHG,
1375 di->chg_info.online_chg & USB_CHG,
1376 di->events.ac_cv_active,
1377 di->events.usb_cv_active,
1378 di->chg_info.ac_curr,
1379 di->chg_info.usb_curr,
1380 di->chg_info.ac_vset,
1381 di->chg_info.ac_iset,
1382 di->chg_info.usb_vset,
1383 di->chg_info.usb_iset);
1385 switch (di->charge_state) {
1386 case STATE_HANDHELD_INIT:
1387 abx500_chargalg_stop_charging(di);
1388 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
1389 abx500_chargalg_state_to(di, STATE_HANDHELD);
1390 /* Intentional fallthrough */
1392 case STATE_HANDHELD:
1395 case STATE_SUSPENDED_INIT:
1396 if (di->susp_status.ac_suspended)
1397 abx500_chargalg_ac_en(di, false, 0, 0);
1398 if (di->susp_status.usb_suspended)
1399 abx500_chargalg_usb_en(di, false, 0, 0);
1400 abx500_chargalg_stop_safety_timer(di);
1401 abx500_chargalg_stop_maintenance_timer(di);
1402 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1403 di->maintenance_chg = false;
1404 abx500_chargalg_state_to(di, STATE_SUSPENDED);
1405 power_supply_changed(&di->chargalg_psy);
1406 /* Intentional fallthrough */
1408 case STATE_SUSPENDED:
1409 /* CHARGING is suspended */
1412 case STATE_BATT_REMOVED_INIT:
1413 abx500_chargalg_stop_charging(di);
1414 abx500_chargalg_state_to(di, STATE_BATT_REMOVED);
1415 /* Intentional fallthrough */
1417 case STATE_BATT_REMOVED:
1418 if (!di->events.batt_rem)
1419 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1422 case STATE_HW_TEMP_PROTECT_INIT:
1423 abx500_chargalg_stop_charging(di);
1424 abx500_chargalg_state_to(di, STATE_HW_TEMP_PROTECT);
1425 /* Intentional fallthrough */
1427 case STATE_HW_TEMP_PROTECT:
1428 if (!di->events.main_thermal_prot &&
1429 !di->events.usb_thermal_prot)
1430 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1433 case STATE_OVV_PROTECT_INIT:
1434 abx500_chargalg_stop_charging(di);
1435 abx500_chargalg_state_to(di, STATE_OVV_PROTECT);
1436 /* Intentional fallthrough */
1438 case STATE_OVV_PROTECT:
1439 if (!di->events.vbus_ovv &&
1440 !di->events.main_ovv &&
1441 !di->events.batt_ovv &&
1442 di->chg_info.usb_chg_ok &&
1443 di->chg_info.ac_chg_ok)
1444 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1447 case STATE_CHG_NOT_OK_INIT:
1448 abx500_chargalg_stop_charging(di);
1449 abx500_chargalg_state_to(di, STATE_CHG_NOT_OK);
1450 /* Intentional fallthrough */
1452 case STATE_CHG_NOT_OK:
1453 if (!di->events.mainextchnotok &&
1454 !di->events.usbchargernotok)
1455 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1458 case STATE_SAFETY_TIMER_EXPIRED_INIT:
1459 abx500_chargalg_stop_charging(di);
1460 abx500_chargalg_state_to(di, STATE_SAFETY_TIMER_EXPIRED);
1461 /* Intentional fallthrough */
1463 case STATE_SAFETY_TIMER_EXPIRED:
1464 /* We exit this state when charger is removed */
1467 case STATE_NORMAL_INIT:
1468 abx500_chargalg_start_charging(di,
1469 di->bm->bat_type[di->bm->batt_id].normal_vol_lvl,
1470 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
1471 abx500_chargalg_state_to(di, STATE_NORMAL);
1472 abx500_chargalg_start_safety_timer(di);
1473 abx500_chargalg_stop_maintenance_timer(di);
1474 init_maxim_chg_curr(di);
1475 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
1477 di->maintenance_chg = false;
1478 power_supply_changed(&di->chargalg_psy);
1483 handle_maxim_chg_curr(di);
1484 if (di->charge_status == POWER_SUPPLY_STATUS_FULL &&
1485 di->maintenance_chg) {
1486 if (di->bm->no_maintenance)
1487 abx500_chargalg_state_to(di,
1488 STATE_WAIT_FOR_RECHARGE_INIT);
1490 abx500_chargalg_state_to(di,
1491 STATE_MAINTENANCE_A_INIT);
1495 /* This state will be used when the maintenance state is disabled */
1496 case STATE_WAIT_FOR_RECHARGE_INIT:
1497 abx500_chargalg_hold_charging(di);
1498 abx500_chargalg_state_to(di, STATE_WAIT_FOR_RECHARGE);
1499 /* Intentional fallthrough */
1501 case STATE_WAIT_FOR_RECHARGE:
1502 if (di->batt_data.percent <=
1503 di->bm->bat_type[di->bm->batt_id].
1505 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1508 case STATE_MAINTENANCE_A_INIT:
1509 abx500_chargalg_stop_safety_timer(di);
1510 abx500_chargalg_start_maintenance_timer(di,
1512 di->bm->batt_id].maint_a_chg_timer_h);
1513 abx500_chargalg_start_charging(di,
1515 di->bm->batt_id].maint_a_vol_lvl,
1517 di->bm->batt_id].maint_a_cur_lvl);
1518 abx500_chargalg_state_to(di, STATE_MAINTENANCE_A);
1519 power_supply_changed(&di->chargalg_psy);
1520 /* Intentional fallthrough*/
1522 case STATE_MAINTENANCE_A:
1523 if (di->events.maintenance_timer_expired) {
1524 abx500_chargalg_stop_maintenance_timer(di);
1525 abx500_chargalg_state_to(di, STATE_MAINTENANCE_B_INIT);
1529 case STATE_MAINTENANCE_B_INIT:
1530 abx500_chargalg_start_maintenance_timer(di,
1532 di->bm->batt_id].maint_b_chg_timer_h);
1533 abx500_chargalg_start_charging(di,
1535 di->bm->batt_id].maint_b_vol_lvl,
1537 di->bm->batt_id].maint_b_cur_lvl);
1538 abx500_chargalg_state_to(di, STATE_MAINTENANCE_B);
1539 power_supply_changed(&di->chargalg_psy);
1540 /* Intentional fallthrough*/
1542 case STATE_MAINTENANCE_B:
1543 if (di->events.maintenance_timer_expired) {
1544 abx500_chargalg_stop_maintenance_timer(di);
1545 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1549 case STATE_TEMP_LOWHIGH_INIT:
1550 abx500_chargalg_start_charging(di,
1552 di->bm->batt_id].low_high_vol_lvl,
1554 di->bm->batt_id].low_high_cur_lvl);
1555 abx500_chargalg_stop_maintenance_timer(di);
1556 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
1557 abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH);
1558 power_supply_changed(&di->chargalg_psy);
1559 /* Intentional fallthrough */
1561 case STATE_TEMP_LOWHIGH:
1562 if (!di->events.btemp_lowhigh)
1563 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1566 case STATE_WD_EXPIRED_INIT:
1567 abx500_chargalg_stop_charging(di);
1568 abx500_chargalg_state_to(di, STATE_WD_EXPIRED);
1569 /* Intentional fallthrough */
1571 case STATE_WD_EXPIRED:
1572 if (!di->events.ac_wd_expired &&
1573 !di->events.usb_wd_expired)
1574 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1577 case STATE_TEMP_UNDEROVER_INIT:
1578 abx500_chargalg_stop_charging(di);
1579 abx500_chargalg_state_to(di, STATE_TEMP_UNDEROVER);
1580 /* Intentional fallthrough */
1582 case STATE_TEMP_UNDEROVER:
1583 if (!di->events.btemp_underover)
1584 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1588 /* Start charging directly if the new state is a charge state */
1589 if (di->charge_state == STATE_NORMAL_INIT ||
1590 di->charge_state == STATE_MAINTENANCE_A_INIT ||
1591 di->charge_state == STATE_MAINTENANCE_B_INIT)
1592 queue_work(di->chargalg_wq, &di->chargalg_work);
1596 * abx500_chargalg_periodic_work() - Periodic work for the algorithm
1597 * @work: pointer to the work_struct structure
1599 * Work queue function for the charging algorithm
1601 static void abx500_chargalg_periodic_work(struct work_struct *work)
1603 struct abx500_chargalg *di = container_of(work,
1604 struct abx500_chargalg, chargalg_periodic_work.work);
1606 abx500_chargalg_algorithm(di);
1609 * If a charger is connected then the battery has to be monitored
1610 * frequently, else the work can be delayed.
1612 if (di->chg_info.conn_chg)
1613 queue_delayed_work(di->chargalg_wq,
1614 &di->chargalg_periodic_work,
1615 di->bm->interval_charging * HZ);
1617 queue_delayed_work(di->chargalg_wq,
1618 &di->chargalg_periodic_work,
1619 di->bm->interval_not_charging * HZ);
1623 * abx500_chargalg_wd_work() - periodic work to kick the charger watchdog
1624 * @work: pointer to the work_struct structure
1626 * Work queue function for kicking the charger watchdog
1628 static void abx500_chargalg_wd_work(struct work_struct *work)
1631 struct abx500_chargalg *di = container_of(work,
1632 struct abx500_chargalg, chargalg_wd_work.work);
1634 dev_dbg(di->dev, "abx500_chargalg_wd_work\n");
1636 ret = abx500_chargalg_kick_watchdog(di);
1638 dev_err(di->dev, "failed to kick watchdog\n");
1640 queue_delayed_work(di->chargalg_wq,
1641 &di->chargalg_wd_work, CHG_WD_INTERVAL);
1645 * abx500_chargalg_work() - Work to run the charging algorithm instantly
1646 * @work: pointer to the work_struct structure
1648 * Work queue function for calling the charging algorithm
1650 static void abx500_chargalg_work(struct work_struct *work)
1652 struct abx500_chargalg *di = container_of(work,
1653 struct abx500_chargalg, chargalg_work);
1655 abx500_chargalg_algorithm(di);
1659 * abx500_chargalg_get_property() - get the chargalg properties
1660 * @psy: pointer to the power_supply structure
1661 * @psp: pointer to the power_supply_property structure
1662 * @val: pointer to the power_supply_propval union
1664 * This function gets called when an application tries to get the
1665 * chargalg properties by reading the sysfs files.
1666 * status: charging/discharging/full/unknown
1667 * health: health of the battery
1668 * Returns error code in case of failure else 0 on success
1670 static int abx500_chargalg_get_property(struct power_supply *psy,
1671 enum power_supply_property psp,
1672 union power_supply_propval *val)
1674 struct abx500_chargalg *di;
1676 di = to_abx500_chargalg_device_info(psy);
1679 case POWER_SUPPLY_PROP_STATUS:
1680 val->intval = di->charge_status;
1682 case POWER_SUPPLY_PROP_HEALTH:
1683 if (di->events.batt_ovv) {
1684 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
1685 } else if (di->events.btemp_underover) {
1686 if (di->batt_data.temp <= di->bm->temp_under)
1687 val->intval = POWER_SUPPLY_HEALTH_COLD;
1689 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
1690 } else if (di->charge_state == STATE_SAFETY_TIMER_EXPIRED ||
1691 di->charge_state == STATE_SAFETY_TIMER_EXPIRED_INIT) {
1692 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
1694 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1703 /* Exposure to the sysfs interface */
1706 * abx500_chargalg_sysfs_show() - sysfs show operations
1707 * @kobj: pointer to the struct kobject
1708 * @attr: pointer to the struct attribute
1709 * @buf: buffer that holds the parameter to send to userspace
1711 * Returns a buffer to be displayed in user space
1713 static ssize_t abx500_chargalg_sysfs_show(struct kobject *kobj,
1714 struct attribute *attr, char *buf)
1716 struct abx500_chargalg *di = container_of(kobj,
1717 struct abx500_chargalg, chargalg_kobject);
1719 return sprintf(buf, "%d\n",
1720 di->susp_status.ac_suspended &&
1721 di->susp_status.usb_suspended);
1725 * abx500_chargalg_sysfs_charger() - sysfs store operations
1726 * @kobj: pointer to the struct kobject
1727 * @attr: pointer to the struct attribute
1728 * @buf: buffer that holds the parameter passed from userspace
1729 * @length: length of the parameter passed
1731 * Returns length of the buffer(input taken from user space) on success
1732 * else error code on failure
1733 * The operation to be performed on passing the parameters from the user space.
1735 static ssize_t abx500_chargalg_sysfs_charger(struct kobject *kobj,
1736 struct attribute *attr, const char *buf, size_t length)
1738 struct abx500_chargalg *di = container_of(kobj,
1739 struct abx500_chargalg, chargalg_kobject);
1743 char entry = *attr->name;
1747 ret = strict_strtol(buf, 10, ¶m);
1754 /* Disable charging */
1755 di->susp_status.ac_suspended = true;
1756 di->susp_status.usb_suspended = true;
1757 di->susp_status.suspended_change = true;
1758 /* Trigger a state change */
1759 queue_work(di->chargalg_wq,
1760 &di->chargalg_work);
1763 /* Enable AC Charging */
1764 di->susp_status.ac_suspended = false;
1765 di->susp_status.suspended_change = true;
1766 /* Trigger a state change */
1767 queue_work(di->chargalg_wq,
1768 &di->chargalg_work);
1771 /* Enable USB charging */
1772 di->susp_status.usb_suspended = false;
1773 di->susp_status.suspended_change = true;
1774 /* Trigger a state change */
1775 queue_work(di->chargalg_wq,
1776 &di->chargalg_work);
1779 dev_info(di->dev, "Wrong input\n"
1780 "Enter 0. Disable AC/USB Charging\n"
1781 "1. Enable AC charging\n"
1782 "2. Enable USB Charging\n");
1789 static struct attribute abx500_chargalg_en_charger = \
1792 .mode = S_IRUGO | S_IWUSR,
1795 static struct attribute *abx500_chargalg_chg[] = {
1796 &abx500_chargalg_en_charger,
1800 static const struct sysfs_ops abx500_chargalg_sysfs_ops = {
1801 .show = abx500_chargalg_sysfs_show,
1802 .store = abx500_chargalg_sysfs_charger,
1805 static struct kobj_type abx500_chargalg_ktype = {
1806 .sysfs_ops = &abx500_chargalg_sysfs_ops,
1807 .default_attrs = abx500_chargalg_chg,
1811 * abx500_chargalg_sysfs_exit() - de-init of sysfs entry
1812 * @di: pointer to the struct abx500_chargalg
1814 * This function removes the entry in sysfs.
1816 static void abx500_chargalg_sysfs_exit(struct abx500_chargalg *di)
1818 kobject_del(&di->chargalg_kobject);
1822 * abx500_chargalg_sysfs_init() - init of sysfs entry
1823 * @di: pointer to the struct abx500_chargalg
1825 * This function adds an entry in sysfs.
1826 * Returns error code in case of failure else 0(on success)
1828 static int abx500_chargalg_sysfs_init(struct abx500_chargalg *di)
1832 ret = kobject_init_and_add(&di->chargalg_kobject,
1833 &abx500_chargalg_ktype,
1834 NULL, "abx500_chargalg");
1836 dev_err(di->dev, "failed to create sysfs entry\n");
1840 /* Exposure to the sysfs interface <<END>> */
1842 #if defined(CONFIG_PM)
1843 static int abx500_chargalg_resume(struct platform_device *pdev)
1845 struct abx500_chargalg *di = platform_get_drvdata(pdev);
1847 /* Kick charger watchdog if charging (any charger online) */
1848 if (di->chg_info.online_chg)
1849 queue_delayed_work(di->chargalg_wq, &di->chargalg_wd_work, 0);
1852 * Run the charging algorithm directly to be sure we don't
1855 queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
1860 static int abx500_chargalg_suspend(struct platform_device *pdev,
1863 struct abx500_chargalg *di = platform_get_drvdata(pdev);
1865 if (di->chg_info.online_chg)
1866 cancel_delayed_work_sync(&di->chargalg_wd_work);
1868 cancel_delayed_work_sync(&di->chargalg_periodic_work);
1873 #define abx500_chargalg_suspend NULL
1874 #define abx500_chargalg_resume NULL
1877 static int abx500_chargalg_remove(struct platform_device *pdev)
1879 struct abx500_chargalg *di = platform_get_drvdata(pdev);
1881 /* sysfs interface to enable/disbale charging from user space */
1882 abx500_chargalg_sysfs_exit(di);
1884 /* Delete the work queue */
1885 destroy_workqueue(di->chargalg_wq);
1887 flush_scheduled_work();
1888 power_supply_unregister(&di->chargalg_psy);
1889 platform_set_drvdata(pdev, NULL);
1894 static char *supply_interface[] = {
1898 static int abx500_chargalg_probe(struct platform_device *pdev)
1900 struct device_node *np = pdev->dev.of_node;
1901 struct abx500_bm_data *plat = pdev->dev.platform_data;
1902 struct abx500_chargalg *di;
1905 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
1907 dev_err(&pdev->dev, "%s no mem for ab8500_chargalg\n", __func__);
1912 dev_err(&pdev->dev, "no battery management data supplied\n");
1918 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
1920 dev_err(&pdev->dev, "failed to get battery information\n");
1925 /* get device struct and parent */
1926 di->dev = &pdev->dev;
1927 di->parent = dev_get_drvdata(pdev->dev.parent);
1929 /* chargalg supply */
1930 di->chargalg_psy.name = "abx500_chargalg";
1931 di->chargalg_psy.type = POWER_SUPPLY_TYPE_BATTERY;
1932 di->chargalg_psy.properties = abx500_chargalg_props;
1933 di->chargalg_psy.num_properties = ARRAY_SIZE(abx500_chargalg_props);
1934 di->chargalg_psy.get_property = abx500_chargalg_get_property;
1935 di->chargalg_psy.supplied_to = supply_interface;
1936 di->chargalg_psy.num_supplicants = ARRAY_SIZE(supply_interface),
1937 di->chargalg_psy.external_power_changed =
1938 abx500_chargalg_external_power_changed;
1940 /* Initilialize safety timer */
1941 init_timer(&di->safety_timer);
1942 di->safety_timer.function = abx500_chargalg_safety_timer_expired;
1943 di->safety_timer.data = (unsigned long) di;
1945 /* Initilialize maintenance timer */
1946 init_timer(&di->maintenance_timer);
1947 di->maintenance_timer.function =
1948 abx500_chargalg_maintenance_timer_expired;
1949 di->maintenance_timer.data = (unsigned long) di;
1951 /* Create a work queue for the chargalg */
1953 create_singlethread_workqueue("abx500_chargalg_wq");
1954 if (di->chargalg_wq == NULL) {
1955 dev_err(di->dev, "failed to create work queue\n");
1959 /* Init work for chargalg */
1960 INIT_DEFERRABLE_WORK(&di->chargalg_periodic_work,
1961 abx500_chargalg_periodic_work);
1962 INIT_DEFERRABLE_WORK(&di->chargalg_wd_work,
1963 abx500_chargalg_wd_work);
1965 /* Init work for chargalg */
1966 INIT_WORK(&di->chargalg_work, abx500_chargalg_work);
1968 /* To detect charger at startup */
1969 di->chg_info.prev_conn_chg = -1;
1971 /* Register chargalg power supply class */
1972 ret = power_supply_register(di->dev, &di->chargalg_psy);
1974 dev_err(di->dev, "failed to register chargalg psy\n");
1975 goto free_chargalg_wq;
1978 platform_set_drvdata(pdev, di);
1980 /* sysfs interface to enable/disable charging from user space */
1981 ret = abx500_chargalg_sysfs_init(di);
1983 dev_err(di->dev, "failed to create sysfs entry\n");
1987 /* Run the charging algorithm */
1988 queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
1990 dev_info(di->dev, "probe success\n");
1994 power_supply_unregister(&di->chargalg_psy);
1996 destroy_workqueue(di->chargalg_wq);
2000 static const struct of_device_id ab8500_chargalg_match[] = {
2001 { .compatible = "stericsson,ab8500-chargalg", },
2005 static struct platform_driver abx500_chargalg_driver = {
2006 .probe = abx500_chargalg_probe,
2007 .remove = abx500_chargalg_remove,
2008 .suspend = abx500_chargalg_suspend,
2009 .resume = abx500_chargalg_resume,
2011 .name = "ab8500-chargalg",
2012 .owner = THIS_MODULE,
2013 .of_match_table = ab8500_chargalg_match,
2017 static int __init abx500_chargalg_init(void)
2019 return platform_driver_register(&abx500_chargalg_driver);
2022 static void __exit abx500_chargalg_exit(void)
2024 platform_driver_unregister(&abx500_chargalg_driver);
2027 module_init(abx500_chargalg_init);
2028 module_exit(abx500_chargalg_exit);
2030 MODULE_LICENSE("GPL v2");
2031 MODULE_AUTHOR("Johan Palsson, Karl Komierowski");
2032 MODULE_ALIAS("platform:abx500-chargalg");
2033 MODULE_DESCRIPTION("abx500 battery charging algorithm");