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>
30 /* Watchdog kick interval */
31 #define CHG_WD_INTERVAL (6 * HZ)
33 /* End-of-charge criteria counter */
34 #define EOC_COND_CNT 10
36 #define to_abx500_chargalg_device_info(x) container_of((x), \
37 struct abx500_chargalg, chargalg_psy);
39 enum abx500_chargers {
45 struct abx500_chargalg_charger_info {
46 enum abx500_chargers conn_chg;
47 enum abx500_chargers prev_conn_chg;
48 enum abx500_chargers online_chg;
49 enum abx500_chargers prev_online_chg;
50 enum abx500_chargers charger_type;
63 struct abx500_chargalg_suspension_status {
64 bool suspended_change;
69 struct abx500_chargalg_battery_data {
77 enum abx500_chargalg_states {
80 STATE_CHG_NOT_OK_INIT,
82 STATE_HW_TEMP_PROTECT_INIT,
83 STATE_HW_TEMP_PROTECT,
86 STATE_WAIT_FOR_RECHARGE_INIT,
87 STATE_WAIT_FOR_RECHARGE,
88 STATE_MAINTENANCE_A_INIT,
90 STATE_MAINTENANCE_B_INIT,
92 STATE_TEMP_UNDEROVER_INIT,
94 STATE_TEMP_LOWHIGH_INIT,
98 STATE_OVV_PROTECT_INIT,
100 STATE_SAFETY_TIMER_EXPIRED_INIT,
101 STATE_SAFETY_TIMER_EXPIRED,
102 STATE_BATT_REMOVED_INIT,
104 STATE_WD_EXPIRED_INIT,
108 static const char *states[] = {
113 "HW_TEMP_PROTECT_INIT",
117 "WAIT_FOR_RECHARGE_INIT",
119 "MAINTENANCE_A_INIT",
121 "MAINTENANCE_B_INIT",
123 "TEMP_UNDEROVER_INIT",
131 "SAFETY_TIMER_EXPIRED_INIT",
132 "SAFETY_TIMER_EXPIRED",
139 struct abx500_chargalg_events {
144 bool btemp_underover;
146 bool main_thermal_prot;
147 bool usb_thermal_prot;
150 bool usbchargernotok;
151 bool safety_timer_expired;
152 bool maintenance_timer_expired;
161 * struct abx500_charge_curr_maximization - Charger maximization parameters
162 * @original_iset: the non optimized/maximised charger current
163 * @current_iset: the charging current used at this moment
164 * @test_delta_i: the delta between the current we want to charge and the
165 current that is really going into the battery
166 * @condition_cnt: number of iterations needed before a new charger current
168 * @max_current: maximum charger current
169 * @wait_cnt: to avoid too fast current step down in case of charger
170 * voltage collapse, we insert this delay between step
172 * @level: tells in how many steps the charging current has been
175 struct abx500_charge_curr_maximization {
188 MAXIM_RET_IBAT_TOO_HIGH,
192 * struct abx500_chargalg - abx500 Charging algorithm device information
193 * @dev: pointer to the structure device
194 * @charge_status: battery operating status
195 * @eoc_cnt: counter used to determine end-of_charge
196 * @maintenance_chg: indicate if maintenance charge is active
197 * @t_hyst_norm temperature hysteresis when the temperature has been
198 * over or under normal limits
199 * @t_hyst_lowhigh temperature hysteresis when the temperature has been
200 * over or under the high or low limits
201 * @charge_state: current state of the charging algorithm
202 * @ccm charging current maximization parameters
203 * @chg_info: information about connected charger types
204 * @batt_data: data of the battery
205 * @susp_status: current charger suspension status
206 * @bm: Platform specific battery management information
207 * @chargalg_psy: structure that holds the battery properties exposed by
208 * the charging algorithm
209 * @events: structure for information about events triggered
210 * @chargalg_wq: work queue for running the charging algorithm
211 * @chargalg_periodic_work: work to run the charging algorithm periodically
212 * @chargalg_wd_work: work to kick the charger watchdog periodically
213 * @chargalg_work: work to run the charging algorithm instantly
214 * @safety_timer: charging safety timer
215 * @maintenance_timer: maintenance charging timer
216 * @chargalg_kobject: structure of type kobject
218 struct abx500_chargalg {
222 bool maintenance_chg;
225 enum abx500_chargalg_states charge_state;
226 struct abx500_charge_curr_maximization ccm;
227 struct abx500_chargalg_charger_info chg_info;
228 struct abx500_chargalg_battery_data batt_data;
229 struct abx500_chargalg_suspension_status susp_status;
230 struct abx500_bm_data *bm;
231 struct power_supply chargalg_psy;
232 struct ux500_charger *ac_chg;
233 struct ux500_charger *usb_chg;
234 struct abx500_chargalg_events events;
235 struct workqueue_struct *chargalg_wq;
236 struct delayed_work chargalg_periodic_work;
237 struct delayed_work chargalg_wd_work;
238 struct work_struct chargalg_work;
239 struct timer_list safety_timer;
240 struct timer_list maintenance_timer;
241 struct kobject chargalg_kobject;
244 /* Main battery properties */
245 static enum power_supply_property abx500_chargalg_props[] = {
246 POWER_SUPPLY_PROP_STATUS,
247 POWER_SUPPLY_PROP_HEALTH,
251 * abx500_chargalg_safety_timer_expired() - Expiration of the safety timer
252 * @data: pointer to the abx500_chargalg structure
254 * This function gets called when the safety timer for the charger
257 static void abx500_chargalg_safety_timer_expired(unsigned long data)
259 struct abx500_chargalg *di = (struct abx500_chargalg *) data;
260 dev_err(di->dev, "Safety timer expired\n");
261 di->events.safety_timer_expired = true;
263 /* Trigger execution of the algorithm instantly */
264 queue_work(di->chargalg_wq, &di->chargalg_work);
268 * abx500_chargalg_maintenance_timer_expired() - Expiration of
269 * the maintenance timer
270 * @i: pointer to the abx500_chargalg structure
272 * This function gets called when the maintenence timer
275 static void abx500_chargalg_maintenance_timer_expired(unsigned long data)
278 struct abx500_chargalg *di = (struct abx500_chargalg *) data;
279 dev_dbg(di->dev, "Maintenance timer expired\n");
280 di->events.maintenance_timer_expired = true;
282 /* Trigger execution of the algorithm instantly */
283 queue_work(di->chargalg_wq, &di->chargalg_work);
287 * abx500_chargalg_state_to() - Change charge state
288 * @di: pointer to the abx500_chargalg structure
290 * This function gets called when a charge state change should occur
292 static void abx500_chargalg_state_to(struct abx500_chargalg *di,
293 enum abx500_chargalg_states state)
296 "State changed: %s (From state: [%d] %s =to=> [%d] %s )\n",
297 di->charge_state == state ? "NO" : "YES",
299 states[di->charge_state],
303 di->charge_state = state;
307 * abx500_chargalg_check_charger_connection() - Check charger connection change
308 * @di: pointer to the abx500_chargalg structure
310 * This function will check if there is a change in the charger connection
311 * and change charge state accordingly. AC has precedence over USB.
313 static int abx500_chargalg_check_charger_connection(struct abx500_chargalg *di)
315 if (di->chg_info.conn_chg != di->chg_info.prev_conn_chg ||
316 di->susp_status.suspended_change) {
318 * Charger state changed or suspension
319 * has changed since last update
321 if ((di->chg_info.conn_chg & AC_CHG) &&
322 !di->susp_status.ac_suspended) {
323 dev_dbg(di->dev, "Charging source is AC\n");
324 if (di->chg_info.charger_type != AC_CHG) {
325 di->chg_info.charger_type = AC_CHG;
326 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
328 } else if ((di->chg_info.conn_chg & USB_CHG) &&
329 !di->susp_status.usb_suspended) {
330 dev_dbg(di->dev, "Charging source is USB\n");
331 di->chg_info.charger_type = USB_CHG;
332 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
333 } else if (di->chg_info.conn_chg &&
334 (di->susp_status.ac_suspended ||
335 di->susp_status.usb_suspended)) {
336 dev_dbg(di->dev, "Charging is suspended\n");
337 di->chg_info.charger_type = NO_CHG;
338 abx500_chargalg_state_to(di, STATE_SUSPENDED_INIT);
340 dev_dbg(di->dev, "Charging source is OFF\n");
341 di->chg_info.charger_type = NO_CHG;
342 abx500_chargalg_state_to(di, STATE_HANDHELD_INIT);
344 di->chg_info.prev_conn_chg = di->chg_info.conn_chg;
345 di->susp_status.suspended_change = false;
347 return di->chg_info.conn_chg;
351 * abx500_chargalg_start_safety_timer() - Start charging safety timer
352 * @di: pointer to the abx500_chargalg structure
354 * The safety timer is used to avoid overcharging of old or bad batteries.
355 * There are different timers for AC and USB
357 static void abx500_chargalg_start_safety_timer(struct abx500_chargalg *di)
359 unsigned long timer_expiration = 0;
361 switch (di->chg_info.charger_type) {
364 round_jiffies(jiffies +
365 (di->bm->main_safety_tmr_h * 3600 * HZ));
370 round_jiffies(jiffies +
371 (di->bm->usb_safety_tmr_h * 3600 * HZ));
375 dev_err(di->dev, "Unknown charger to charge from\n");
379 di->events.safety_timer_expired = false;
380 di->safety_timer.expires = timer_expiration;
381 if (!timer_pending(&di->safety_timer))
382 add_timer(&di->safety_timer);
384 mod_timer(&di->safety_timer, timer_expiration);
388 * abx500_chargalg_stop_safety_timer() - Stop charging safety timer
389 * @di: pointer to the abx500_chargalg structure
391 * The safety timer is stopped whenever the NORMAL state is exited
393 static void abx500_chargalg_stop_safety_timer(struct abx500_chargalg *di)
395 di->events.safety_timer_expired = false;
396 del_timer(&di->safety_timer);
400 * abx500_chargalg_start_maintenance_timer() - Start charging maintenance timer
401 * @di: pointer to the abx500_chargalg structure
402 * @duration: duration of ther maintenance timer in hours
404 * The maintenance timer is used to maintain the charge in the battery once
405 * the battery is considered full. These timers are chosen to match the
406 * discharge curve of the battery
408 static void abx500_chargalg_start_maintenance_timer(struct abx500_chargalg *di,
411 unsigned long timer_expiration;
413 /* Convert from hours to jiffies */
414 timer_expiration = round_jiffies(jiffies + (duration * 3600 * HZ));
416 di->events.maintenance_timer_expired = false;
417 di->maintenance_timer.expires = timer_expiration;
418 if (!timer_pending(&di->maintenance_timer))
419 add_timer(&di->maintenance_timer);
421 mod_timer(&di->maintenance_timer, timer_expiration);
425 * abx500_chargalg_stop_maintenance_timer() - Stop maintenance timer
426 * @di: pointer to the abx500_chargalg structure
428 * The maintenance timer is stopped whenever maintenance ends or when another
431 static void abx500_chargalg_stop_maintenance_timer(struct abx500_chargalg *di)
433 di->events.maintenance_timer_expired = false;
434 del_timer(&di->maintenance_timer);
438 * abx500_chargalg_kick_watchdog() - Kick charger watchdog
439 * @di: pointer to the abx500_chargalg structure
441 * The charger watchdog have to be kicked periodically whenever the charger is
442 * on, else the ABB will reset the system
444 static int abx500_chargalg_kick_watchdog(struct abx500_chargalg *di)
446 /* Check if charger exists and kick watchdog if charging */
447 if (di->ac_chg && di->ac_chg->ops.kick_wd &&
448 di->chg_info.online_chg & AC_CHG) {
450 * If AB charger watchdog expired, pm2xxx charging
451 * gets disabled. To be safe, kick both AB charger watchdog
452 * and pm2xxx watchdog.
454 if (di->ac_chg->external &&
455 di->usb_chg && di->usb_chg->ops.kick_wd)
456 di->usb_chg->ops.kick_wd(di->usb_chg);
458 return di->ac_chg->ops.kick_wd(di->ac_chg);
460 else if (di->usb_chg && di->usb_chg->ops.kick_wd &&
461 di->chg_info.online_chg & USB_CHG)
462 return di->usb_chg->ops.kick_wd(di->usb_chg);
468 * abx500_chargalg_ac_en() - Turn on/off the AC charger
469 * @di: pointer to the abx500_chargalg structure
470 * @enable: charger on/off
471 * @vset: requested charger output voltage
472 * @iset: requested charger output current
474 * The AC charger will be turned on/off with the requested charge voltage and
477 static int abx500_chargalg_ac_en(struct abx500_chargalg *di, int enable,
480 if (!di->ac_chg || !di->ac_chg->ops.enable)
483 /* Select maximum of what both the charger and the battery supports */
484 if (di->ac_chg->max_out_volt)
485 vset = min(vset, di->ac_chg->max_out_volt);
486 if (di->ac_chg->max_out_curr)
487 iset = min(iset, di->ac_chg->max_out_curr);
489 di->chg_info.ac_iset = iset;
490 di->chg_info.ac_vset = vset;
492 return di->ac_chg->ops.enable(di->ac_chg, enable, vset, iset);
496 * abx500_chargalg_usb_en() - Turn on/off the USB charger
497 * @di: pointer to the abx500_chargalg structure
498 * @enable: charger on/off
499 * @vset: requested charger output voltage
500 * @iset: requested charger output current
502 * The USB charger will be turned on/off with the requested charge voltage and
505 static int abx500_chargalg_usb_en(struct abx500_chargalg *di, int enable,
508 if (!di->usb_chg || !di->usb_chg->ops.enable)
511 /* Select maximum of what both the charger and the battery supports */
512 if (di->usb_chg->max_out_volt)
513 vset = min(vset, di->usb_chg->max_out_volt);
514 if (di->usb_chg->max_out_curr)
515 iset = min(iset, di->usb_chg->max_out_curr);
517 di->chg_info.usb_iset = iset;
518 di->chg_info.usb_vset = vset;
520 return di->usb_chg->ops.enable(di->usb_chg, enable, vset, iset);
524 * abx500_chargalg_update_chg_curr() - Update charger current
525 * @di: pointer to the abx500_chargalg structure
526 * @iset: requested charger output current
528 * The charger output current will be updated for the charger
529 * that is currently in use
531 static int abx500_chargalg_update_chg_curr(struct abx500_chargalg *di,
534 /* Check if charger exists and update current if charging */
535 if (di->ac_chg && di->ac_chg->ops.update_curr &&
536 di->chg_info.charger_type & AC_CHG) {
538 * Select maximum of what both the charger
539 * and the battery supports
541 if (di->ac_chg->max_out_curr)
542 iset = min(iset, di->ac_chg->max_out_curr);
544 di->chg_info.ac_iset = iset;
546 return di->ac_chg->ops.update_curr(di->ac_chg, iset);
547 } else if (di->usb_chg && di->usb_chg->ops.update_curr &&
548 di->chg_info.charger_type & USB_CHG) {
550 * Select maximum of what both the charger
551 * and the battery supports
553 if (di->usb_chg->max_out_curr)
554 iset = min(iset, di->usb_chg->max_out_curr);
556 di->chg_info.usb_iset = iset;
558 return di->usb_chg->ops.update_curr(di->usb_chg, iset);
565 * abx500_chargalg_stop_charging() - Stop charging
566 * @di: pointer to the abx500_chargalg structure
568 * This function is called from any state where charging should be stopped.
569 * All charging is disabled and all status parameters and timers are changed
572 static void abx500_chargalg_stop_charging(struct abx500_chargalg *di)
574 abx500_chargalg_ac_en(di, false, 0, 0);
575 abx500_chargalg_usb_en(di, false, 0, 0);
576 abx500_chargalg_stop_safety_timer(di);
577 abx500_chargalg_stop_maintenance_timer(di);
578 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
579 di->maintenance_chg = false;
580 cancel_delayed_work(&di->chargalg_wd_work);
581 power_supply_changed(&di->chargalg_psy);
585 * abx500_chargalg_hold_charging() - Pauses charging
586 * @di: pointer to the abx500_chargalg structure
588 * This function is called in the case where maintenance charging has been
589 * disabled and instead a battery voltage mode is entered to check when the
590 * battery voltage has reached a certain recharge voltage
592 static void abx500_chargalg_hold_charging(struct abx500_chargalg *di)
594 abx500_chargalg_ac_en(di, false, 0, 0);
595 abx500_chargalg_usb_en(di, false, 0, 0);
596 abx500_chargalg_stop_safety_timer(di);
597 abx500_chargalg_stop_maintenance_timer(di);
598 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
599 di->maintenance_chg = false;
600 cancel_delayed_work(&di->chargalg_wd_work);
601 power_supply_changed(&di->chargalg_psy);
605 * abx500_chargalg_start_charging() - Start the charger
606 * @di: pointer to the abx500_chargalg structure
607 * @vset: requested charger output voltage
608 * @iset: requested charger output current
610 * A charger will be enabled depending on the requested charger type that was
611 * detected previously.
613 static void abx500_chargalg_start_charging(struct abx500_chargalg *di,
616 bool start_chargalg_wd = true;
618 switch (di->chg_info.charger_type) {
621 "AC parameters: Vset %d, Ich %d\n", vset, iset);
622 abx500_chargalg_usb_en(di, false, 0, 0);
623 abx500_chargalg_ac_en(di, true, vset, iset);
628 "USB parameters: Vset %d, Ich %d\n", vset, iset);
629 abx500_chargalg_ac_en(di, false, 0, 0);
630 abx500_chargalg_usb_en(di, true, vset, iset);
634 dev_err(di->dev, "Unknown charger to charge from\n");
635 start_chargalg_wd = false;
639 if (start_chargalg_wd && !delayed_work_pending(&di->chargalg_wd_work))
640 queue_delayed_work(di->chargalg_wq, &di->chargalg_wd_work, 0);
644 * abx500_chargalg_check_temp() - Check battery temperature ranges
645 * @di: pointer to the abx500_chargalg structure
647 * The battery temperature is checked against the predefined limits and the
648 * charge state is changed accordingly
650 static void abx500_chargalg_check_temp(struct abx500_chargalg *di)
652 if (di->batt_data.temp > (di->bm->temp_low + di->t_hyst_norm) &&
653 di->batt_data.temp < (di->bm->temp_high - di->t_hyst_norm)) {
655 di->events.btemp_underover = false;
656 di->events.btemp_lowhigh = false;
658 di->t_hyst_lowhigh = 0;
660 if (((di->batt_data.temp >= di->bm->temp_high) &&
661 (di->batt_data.temp <
662 (di->bm->temp_over - di->t_hyst_lowhigh))) ||
663 ((di->batt_data.temp >
664 (di->bm->temp_under + di->t_hyst_lowhigh)) &&
665 (di->batt_data.temp <= di->bm->temp_low))) {
666 /* TEMP minor!!!!! */
667 di->events.btemp_underover = false;
668 di->events.btemp_lowhigh = true;
669 di->t_hyst_norm = di->bm->temp_hysteresis;
670 di->t_hyst_lowhigh = 0;
671 } else if (di->batt_data.temp <= di->bm->temp_under ||
672 di->batt_data.temp >= di->bm->temp_over) {
673 /* TEMP major!!!!! */
674 di->events.btemp_underover = true;
675 di->events.btemp_lowhigh = false;
677 di->t_hyst_lowhigh = di->bm->temp_hysteresis;
679 /* Within hysteresis */
680 dev_dbg(di->dev, "Within hysteresis limit temp: %d "
681 "hyst_lowhigh %d, hyst normal %d\n",
682 di->batt_data.temp, di->t_hyst_lowhigh,
689 * abx500_chargalg_check_charger_voltage() - Check charger voltage
690 * @di: pointer to the abx500_chargalg structure
692 * Charger voltage is checked against maximum limit
694 static void abx500_chargalg_check_charger_voltage(struct abx500_chargalg *di)
696 if (di->chg_info.usb_volt > di->bm->chg_params->usb_volt_max)
697 di->chg_info.usb_chg_ok = false;
699 di->chg_info.usb_chg_ok = true;
701 if (di->chg_info.ac_volt > di->bm->chg_params->ac_volt_max)
702 di->chg_info.ac_chg_ok = false;
704 di->chg_info.ac_chg_ok = true;
709 * abx500_chargalg_end_of_charge() - Check if end-of-charge criteria is fulfilled
710 * @di: pointer to the abx500_chargalg structure
712 * End-of-charge criteria is fulfilled when the battery voltage is above a
713 * certain limit and the battery current is below a certain limit for a
714 * predefined number of consecutive seconds. If true, the battery is full
716 static void abx500_chargalg_end_of_charge(struct abx500_chargalg *di)
718 if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING &&
719 di->charge_state == STATE_NORMAL &&
720 !di->maintenance_chg && (di->batt_data.volt >=
721 di->bm->bat_type[di->bm->batt_id].termination_vol ||
722 di->events.usb_cv_active || di->events.ac_cv_active) &&
723 di->batt_data.avg_curr <
724 di->bm->bat_type[di->bm->batt_id].termination_curr &&
725 di->batt_data.avg_curr > 0) {
726 if (++di->eoc_cnt >= EOC_COND_CNT) {
728 di->charge_status = POWER_SUPPLY_STATUS_FULL;
729 di->maintenance_chg = true;
730 dev_dbg(di->dev, "EOC reached!\n");
731 power_supply_changed(&di->chargalg_psy);
734 " EOC limit reached for the %d"
735 " time, out of %d before EOC\n",
744 static void init_maxim_chg_curr(struct abx500_chargalg *di)
746 di->ccm.original_iset =
747 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl;
748 di->ccm.current_iset =
749 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl;
750 di->ccm.test_delta_i = di->bm->maxi->charger_curr_step;
751 di->ccm.max_current = di->bm->maxi->chg_curr;
752 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
757 * abx500_chargalg_chg_curr_maxim - increases the charger current to
758 * compensate for the system load
759 * @di pointer to the abx500_chargalg structure
761 * This maximization function is used to raise the charger current to get the
762 * battery current as close to the optimal value as possible. The battery
763 * current during charging is affected by the system load
765 static enum maxim_ret abx500_chargalg_chg_curr_maxim(struct abx500_chargalg *di)
769 if (!di->bm->maxi->ena_maxi)
770 return MAXIM_RET_NOACTION;
772 delta_i = di->ccm.original_iset - di->batt_data.inst_curr;
774 if (di->events.vbus_collapsed) {
775 dev_dbg(di->dev, "Charger voltage has collapsed %d\n",
777 if (di->ccm.wait_cnt == 0) {
778 dev_dbg(di->dev, "lowering current\n");
780 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
781 di->ccm.max_current =
782 di->ccm.current_iset - di->ccm.test_delta_i;
783 di->ccm.current_iset = di->ccm.max_current;
785 return MAXIM_RET_CHANGE;
787 dev_dbg(di->dev, "waiting\n");
788 /* Let's go in here twice before lowering curr again */
789 di->ccm.wait_cnt = (di->ccm.wait_cnt + 1) % 3;
790 return MAXIM_RET_NOACTION;
794 di->ccm.wait_cnt = 0;
796 if ((di->batt_data.inst_curr > di->ccm.original_iset)) {
797 dev_dbg(di->dev, " Maximization Ibat (%dmA) too high"
798 " (limit %dmA) (current iset: %dmA)!\n",
799 di->batt_data.inst_curr, di->ccm.original_iset,
800 di->ccm.current_iset);
802 if (di->ccm.current_iset == di->ccm.original_iset)
803 return MAXIM_RET_NOACTION;
805 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
806 di->ccm.current_iset = di->ccm.original_iset;
809 return MAXIM_RET_IBAT_TOO_HIGH;
812 if (delta_i > di->ccm.test_delta_i &&
813 (di->ccm.current_iset + di->ccm.test_delta_i) <
814 di->ccm.max_current) {
815 if (di->ccm.condition_cnt-- == 0) {
816 /* Increse the iset with cco.test_delta_i */
817 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
818 di->ccm.current_iset += di->ccm.test_delta_i;
820 dev_dbg(di->dev, " Maximization needed, increase"
821 " with %d mA to %dmA (Optimal ibat: %d)"
823 di->ccm.test_delta_i,
824 di->ccm.current_iset,
825 di->ccm.original_iset,
827 return MAXIM_RET_CHANGE;
829 return MAXIM_RET_NOACTION;
832 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
833 return MAXIM_RET_NOACTION;
837 static void handle_maxim_chg_curr(struct abx500_chargalg *di)
842 ret = abx500_chargalg_chg_curr_maxim(di);
844 case MAXIM_RET_CHANGE:
845 result = abx500_chargalg_update_chg_curr(di,
846 di->ccm.current_iset);
848 dev_err(di->dev, "failed to set chg curr\n");
850 case MAXIM_RET_IBAT_TOO_HIGH:
851 result = abx500_chargalg_update_chg_curr(di,
852 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
854 dev_err(di->dev, "failed to set chg curr\n");
857 case MAXIM_RET_NOACTION:
864 static int abx500_chargalg_get_ext_psy_data(struct device *dev, void *data)
866 struct power_supply *psy;
867 struct power_supply *ext;
868 struct abx500_chargalg *di;
869 union power_supply_propval ret;
871 bool psy_found = false;
872 bool capacity_updated = false;
874 psy = (struct power_supply *)data;
875 ext = dev_get_drvdata(dev);
876 di = to_abx500_chargalg_device_info(psy);
877 /* For all psy where the driver name appears in any supplied_to */
878 for (i = 0; i < ext->num_supplicants; i++) {
879 if (!strcmp(ext->supplied_to[i], psy->name))
886 * If external is not registering 'POWER_SUPPLY_PROP_CAPACITY' to its
887 * property because of handling that sysfs entry on its own, this is
888 * the place to get the battery capacity.
890 if (!ext->get_property(ext, POWER_SUPPLY_PROP_CAPACITY, &ret)) {
891 di->batt_data.percent = ret.intval;
892 capacity_updated = true;
895 /* Go through all properties for the psy */
896 for (j = 0; j < ext->num_properties; j++) {
897 enum power_supply_property prop;
898 prop = ext->properties[j];
900 /* Initialize chargers if not already done */
902 ext->type == POWER_SUPPLY_TYPE_MAINS)
903 di->ac_chg = psy_to_ux500_charger(ext);
904 else if (!di->usb_chg &&
905 ext->type == POWER_SUPPLY_TYPE_USB)
906 di->usb_chg = psy_to_ux500_charger(ext);
908 if (ext->get_property(ext, prop, &ret))
911 case POWER_SUPPLY_PROP_PRESENT:
913 case POWER_SUPPLY_TYPE_BATTERY:
914 /* Battery present */
916 di->events.batt_rem = false;
917 /* Battery removed */
919 di->events.batt_rem = true;
921 case POWER_SUPPLY_TYPE_MAINS:
922 /* AC disconnected */
924 (di->chg_info.conn_chg & AC_CHG)) {
925 di->chg_info.prev_conn_chg =
926 di->chg_info.conn_chg;
927 di->chg_info.conn_chg &= ~AC_CHG;
930 else if (ret.intval &&
931 !(di->chg_info.conn_chg & AC_CHG)) {
932 di->chg_info.prev_conn_chg =
933 di->chg_info.conn_chg;
934 di->chg_info.conn_chg |= AC_CHG;
937 case POWER_SUPPLY_TYPE_USB:
938 /* USB disconnected */
940 (di->chg_info.conn_chg & USB_CHG)) {
941 di->chg_info.prev_conn_chg =
942 di->chg_info.conn_chg;
943 di->chg_info.conn_chg &= ~USB_CHG;
946 else if (ret.intval &&
947 !(di->chg_info.conn_chg & USB_CHG)) {
948 di->chg_info.prev_conn_chg =
949 di->chg_info.conn_chg;
950 di->chg_info.conn_chg |= USB_CHG;
958 case POWER_SUPPLY_PROP_ONLINE:
960 case POWER_SUPPLY_TYPE_BATTERY:
962 case POWER_SUPPLY_TYPE_MAINS:
965 (di->chg_info.online_chg & AC_CHG)) {
966 di->chg_info.prev_online_chg =
967 di->chg_info.online_chg;
968 di->chg_info.online_chg &= ~AC_CHG;
971 else if (ret.intval &&
972 !(di->chg_info.online_chg & AC_CHG)) {
973 di->chg_info.prev_online_chg =
974 di->chg_info.online_chg;
975 di->chg_info.online_chg |= AC_CHG;
976 queue_delayed_work(di->chargalg_wq,
977 &di->chargalg_wd_work, 0);
980 case POWER_SUPPLY_TYPE_USB:
983 (di->chg_info.online_chg & USB_CHG)) {
984 di->chg_info.prev_online_chg =
985 di->chg_info.online_chg;
986 di->chg_info.online_chg &= ~USB_CHG;
989 else if (ret.intval &&
990 !(di->chg_info.online_chg & USB_CHG)) {
991 di->chg_info.prev_online_chg =
992 di->chg_info.online_chg;
993 di->chg_info.online_chg |= USB_CHG;
994 queue_delayed_work(di->chargalg_wq,
995 &di->chargalg_wd_work, 0);
1003 case POWER_SUPPLY_PROP_HEALTH:
1004 switch (ext->type) {
1005 case POWER_SUPPLY_TYPE_BATTERY:
1007 case POWER_SUPPLY_TYPE_MAINS:
1008 switch (ret.intval) {
1009 case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
1010 di->events.mainextchnotok = true;
1011 di->events.main_thermal_prot = false;
1012 di->events.main_ovv = false;
1013 di->events.ac_wd_expired = false;
1015 case POWER_SUPPLY_HEALTH_DEAD:
1016 di->events.ac_wd_expired = true;
1017 di->events.mainextchnotok = false;
1018 di->events.main_ovv = false;
1019 di->events.main_thermal_prot = false;
1021 case POWER_SUPPLY_HEALTH_COLD:
1022 case POWER_SUPPLY_HEALTH_OVERHEAT:
1023 di->events.main_thermal_prot = true;
1024 di->events.mainextchnotok = false;
1025 di->events.main_ovv = false;
1026 di->events.ac_wd_expired = false;
1028 case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
1029 di->events.main_ovv = true;
1030 di->events.mainextchnotok = false;
1031 di->events.main_thermal_prot = false;
1032 di->events.ac_wd_expired = false;
1034 case POWER_SUPPLY_HEALTH_GOOD:
1035 di->events.main_thermal_prot = false;
1036 di->events.mainextchnotok = false;
1037 di->events.main_ovv = false;
1038 di->events.ac_wd_expired = false;
1045 case POWER_SUPPLY_TYPE_USB:
1046 switch (ret.intval) {
1047 case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
1048 di->events.usbchargernotok = true;
1049 di->events.usb_thermal_prot = false;
1050 di->events.vbus_ovv = false;
1051 di->events.usb_wd_expired = false;
1053 case POWER_SUPPLY_HEALTH_DEAD:
1054 di->events.usb_wd_expired = true;
1055 di->events.usbchargernotok = false;
1056 di->events.usb_thermal_prot = false;
1057 di->events.vbus_ovv = false;
1059 case POWER_SUPPLY_HEALTH_COLD:
1060 case POWER_SUPPLY_HEALTH_OVERHEAT:
1061 di->events.usb_thermal_prot = true;
1062 di->events.usbchargernotok = false;
1063 di->events.vbus_ovv = false;
1064 di->events.usb_wd_expired = false;
1066 case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
1067 di->events.vbus_ovv = true;
1068 di->events.usbchargernotok = false;
1069 di->events.usb_thermal_prot = false;
1070 di->events.usb_wd_expired = false;
1072 case POWER_SUPPLY_HEALTH_GOOD:
1073 di->events.usbchargernotok = false;
1074 di->events.usb_thermal_prot = false;
1075 di->events.vbus_ovv = false;
1076 di->events.usb_wd_expired = false;
1086 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1087 switch (ext->type) {
1088 case POWER_SUPPLY_TYPE_BATTERY:
1089 di->batt_data.volt = ret.intval / 1000;
1091 case POWER_SUPPLY_TYPE_MAINS:
1092 di->chg_info.ac_volt = ret.intval / 1000;
1094 case POWER_SUPPLY_TYPE_USB:
1095 di->chg_info.usb_volt = ret.intval / 1000;
1102 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
1103 switch (ext->type) {
1104 case POWER_SUPPLY_TYPE_MAINS:
1105 /* AVG is used to indicate when we are
1108 di->events.ac_cv_active = true;
1110 di->events.ac_cv_active = false;
1113 case POWER_SUPPLY_TYPE_USB:
1114 /* AVG is used to indicate when we are
1117 di->events.usb_cv_active = true;
1119 di->events.usb_cv_active = false;
1127 case POWER_SUPPLY_PROP_TECHNOLOGY:
1128 switch (ext->type) {
1129 case POWER_SUPPLY_TYPE_BATTERY:
1131 di->events.batt_unknown = false;
1133 di->events.batt_unknown = true;
1141 case POWER_SUPPLY_PROP_TEMP:
1142 di->batt_data.temp = ret.intval / 10;
1145 case POWER_SUPPLY_PROP_CURRENT_NOW:
1146 switch (ext->type) {
1147 case POWER_SUPPLY_TYPE_MAINS:
1148 di->chg_info.ac_curr =
1151 case POWER_SUPPLY_TYPE_USB:
1152 di->chg_info.usb_curr =
1155 case POWER_SUPPLY_TYPE_BATTERY:
1156 di->batt_data.inst_curr = ret.intval / 1000;
1163 case POWER_SUPPLY_PROP_CURRENT_AVG:
1164 switch (ext->type) {
1165 case POWER_SUPPLY_TYPE_BATTERY:
1166 di->batt_data.avg_curr = ret.intval / 1000;
1168 case POWER_SUPPLY_TYPE_USB:
1170 di->events.vbus_collapsed = true;
1172 di->events.vbus_collapsed = false;
1178 case POWER_SUPPLY_PROP_CAPACITY:
1179 if (!capacity_updated)
1180 di->batt_data.percent = ret.intval;
1190 * abx500_chargalg_external_power_changed() - callback for power supply changes
1191 * @psy: pointer to the structure power_supply
1193 * This function is the entry point of the pointer external_power_changed
1194 * of the structure power_supply.
1195 * This function gets executed when there is a change in any external power
1196 * supply that this driver needs to be notified of.
1198 static void abx500_chargalg_external_power_changed(struct power_supply *psy)
1200 struct abx500_chargalg *di = to_abx500_chargalg_device_info(psy);
1203 * Trigger execution of the algorithm instantly and read
1204 * all power_supply properties there instead
1206 queue_work(di->chargalg_wq, &di->chargalg_work);
1210 * abx500_chargalg_algorithm() - Main function for the algorithm
1211 * @di: pointer to the abx500_chargalg structure
1213 * This is the main control function for the charging algorithm.
1214 * It is called periodically or when something happens that will
1215 * trigger a state change
1217 static void abx500_chargalg_algorithm(struct abx500_chargalg *di)
1221 /* Collect data from all power_supply class devices */
1222 class_for_each_device(power_supply_class, NULL,
1223 &di->chargalg_psy, abx500_chargalg_get_ext_psy_data);
1225 abx500_chargalg_end_of_charge(di);
1226 abx500_chargalg_check_temp(di);
1227 abx500_chargalg_check_charger_voltage(di);
1229 charger_status = abx500_chargalg_check_charger_connection(di);
1231 * First check if we have a charger connected.
1232 * Also we don't allow charging of unknown batteries if configured
1235 if (!charger_status ||
1236 (di->events.batt_unknown && !di->bm->chg_unknown_bat)) {
1237 if (di->charge_state != STATE_HANDHELD) {
1238 di->events.safety_timer_expired = false;
1239 abx500_chargalg_state_to(di, STATE_HANDHELD_INIT);
1243 /* If suspended, we should not continue checking the flags */
1244 else if (di->charge_state == STATE_SUSPENDED_INIT ||
1245 di->charge_state == STATE_SUSPENDED) {
1246 /* We don't do anything here, just don,t continue */
1249 /* Safety timer expiration */
1250 else if (di->events.safety_timer_expired) {
1251 if (di->charge_state != STATE_SAFETY_TIMER_EXPIRED)
1252 abx500_chargalg_state_to(di,
1253 STATE_SAFETY_TIMER_EXPIRED_INIT);
1256 * Check if any interrupts has occured
1257 * that will prevent us from charging
1260 /* Battery removed */
1261 else if (di->events.batt_rem) {
1262 if (di->charge_state != STATE_BATT_REMOVED)
1263 abx500_chargalg_state_to(di, STATE_BATT_REMOVED_INIT);
1265 /* Main or USB charger not ok. */
1266 else if (di->events.mainextchnotok || di->events.usbchargernotok) {
1268 * If vbus_collapsed is set, we have to lower the charger
1269 * current, which is done in the normal state below
1271 if (di->charge_state != STATE_CHG_NOT_OK &&
1272 !di->events.vbus_collapsed)
1273 abx500_chargalg_state_to(di, STATE_CHG_NOT_OK_INIT);
1275 /* VBUS, Main or VBAT OVV. */
1276 else if (di->events.vbus_ovv ||
1277 di->events.main_ovv ||
1278 di->events.batt_ovv ||
1279 !di->chg_info.usb_chg_ok ||
1280 !di->chg_info.ac_chg_ok) {
1281 if (di->charge_state != STATE_OVV_PROTECT)
1282 abx500_chargalg_state_to(di, STATE_OVV_PROTECT_INIT);
1284 /* USB Thermal, stop charging */
1285 else if (di->events.main_thermal_prot ||
1286 di->events.usb_thermal_prot) {
1287 if (di->charge_state != STATE_HW_TEMP_PROTECT)
1288 abx500_chargalg_state_to(di,
1289 STATE_HW_TEMP_PROTECT_INIT);
1291 /* Battery temp over/under */
1292 else if (di->events.btemp_underover) {
1293 if (di->charge_state != STATE_TEMP_UNDEROVER)
1294 abx500_chargalg_state_to(di,
1295 STATE_TEMP_UNDEROVER_INIT);
1297 /* Watchdog expired */
1298 else if (di->events.ac_wd_expired ||
1299 di->events.usb_wd_expired) {
1300 if (di->charge_state != STATE_WD_EXPIRED)
1301 abx500_chargalg_state_to(di, STATE_WD_EXPIRED_INIT);
1303 /* Battery temp high/low */
1304 else if (di->events.btemp_lowhigh) {
1305 if (di->charge_state != STATE_TEMP_LOWHIGH)
1306 abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH_INIT);
1310 "[CHARGALG] Vb %d Ib_avg %d Ib_inst %d Tb %d Cap %d Maint %d "
1311 "State %s Active_chg %d Chg_status %d AC %d USB %d "
1312 "AC_online %d USB_online %d AC_CV %d USB_CV %d AC_I %d "
1313 "USB_I %d AC_Vset %d AC_Iset %d USB_Vset %d USB_Iset %d\n",
1315 di->batt_data.avg_curr,
1316 di->batt_data.inst_curr,
1318 di->batt_data.percent,
1319 di->maintenance_chg,
1320 states[di->charge_state],
1321 di->chg_info.charger_type,
1323 di->chg_info.conn_chg & AC_CHG,
1324 di->chg_info.conn_chg & USB_CHG,
1325 di->chg_info.online_chg & AC_CHG,
1326 di->chg_info.online_chg & USB_CHG,
1327 di->events.ac_cv_active,
1328 di->events.usb_cv_active,
1329 di->chg_info.ac_curr,
1330 di->chg_info.usb_curr,
1331 di->chg_info.ac_vset,
1332 di->chg_info.ac_iset,
1333 di->chg_info.usb_vset,
1334 di->chg_info.usb_iset);
1336 switch (di->charge_state) {
1337 case STATE_HANDHELD_INIT:
1338 abx500_chargalg_stop_charging(di);
1339 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
1340 abx500_chargalg_state_to(di, STATE_HANDHELD);
1341 /* Intentional fallthrough */
1343 case STATE_HANDHELD:
1346 case STATE_SUSPENDED_INIT:
1347 if (di->susp_status.ac_suspended)
1348 abx500_chargalg_ac_en(di, false, 0, 0);
1349 if (di->susp_status.usb_suspended)
1350 abx500_chargalg_usb_en(di, false, 0, 0);
1351 abx500_chargalg_stop_safety_timer(di);
1352 abx500_chargalg_stop_maintenance_timer(di);
1353 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1354 di->maintenance_chg = false;
1355 abx500_chargalg_state_to(di, STATE_SUSPENDED);
1356 power_supply_changed(&di->chargalg_psy);
1357 /* Intentional fallthrough */
1359 case STATE_SUSPENDED:
1360 /* CHARGING is suspended */
1363 case STATE_BATT_REMOVED_INIT:
1364 abx500_chargalg_stop_charging(di);
1365 abx500_chargalg_state_to(di, STATE_BATT_REMOVED);
1366 /* Intentional fallthrough */
1368 case STATE_BATT_REMOVED:
1369 if (!di->events.batt_rem)
1370 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1373 case STATE_HW_TEMP_PROTECT_INIT:
1374 abx500_chargalg_stop_charging(di);
1375 abx500_chargalg_state_to(di, STATE_HW_TEMP_PROTECT);
1376 /* Intentional fallthrough */
1378 case STATE_HW_TEMP_PROTECT:
1379 if (!di->events.main_thermal_prot &&
1380 !di->events.usb_thermal_prot)
1381 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1384 case STATE_OVV_PROTECT_INIT:
1385 abx500_chargalg_stop_charging(di);
1386 abx500_chargalg_state_to(di, STATE_OVV_PROTECT);
1387 /* Intentional fallthrough */
1389 case STATE_OVV_PROTECT:
1390 if (!di->events.vbus_ovv &&
1391 !di->events.main_ovv &&
1392 !di->events.batt_ovv &&
1393 di->chg_info.usb_chg_ok &&
1394 di->chg_info.ac_chg_ok)
1395 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1398 case STATE_CHG_NOT_OK_INIT:
1399 abx500_chargalg_stop_charging(di);
1400 abx500_chargalg_state_to(di, STATE_CHG_NOT_OK);
1401 /* Intentional fallthrough */
1403 case STATE_CHG_NOT_OK:
1404 if (!di->events.mainextchnotok &&
1405 !di->events.usbchargernotok)
1406 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1409 case STATE_SAFETY_TIMER_EXPIRED_INIT:
1410 abx500_chargalg_stop_charging(di);
1411 abx500_chargalg_state_to(di, STATE_SAFETY_TIMER_EXPIRED);
1412 /* Intentional fallthrough */
1414 case STATE_SAFETY_TIMER_EXPIRED:
1415 /* We exit this state when charger is removed */
1418 case STATE_NORMAL_INIT:
1419 abx500_chargalg_start_charging(di,
1420 di->bm->bat_type[di->bm->batt_id].normal_vol_lvl,
1421 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
1422 abx500_chargalg_state_to(di, STATE_NORMAL);
1423 abx500_chargalg_start_safety_timer(di);
1424 abx500_chargalg_stop_maintenance_timer(di);
1425 init_maxim_chg_curr(di);
1426 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
1428 di->maintenance_chg = false;
1429 power_supply_changed(&di->chargalg_psy);
1434 handle_maxim_chg_curr(di);
1435 if (di->charge_status == POWER_SUPPLY_STATUS_FULL &&
1436 di->maintenance_chg) {
1437 if (di->bm->no_maintenance)
1438 abx500_chargalg_state_to(di,
1439 STATE_WAIT_FOR_RECHARGE_INIT);
1441 abx500_chargalg_state_to(di,
1442 STATE_MAINTENANCE_A_INIT);
1446 /* This state will be used when the maintenance state is disabled */
1447 case STATE_WAIT_FOR_RECHARGE_INIT:
1448 abx500_chargalg_hold_charging(di);
1449 abx500_chargalg_state_to(di, STATE_WAIT_FOR_RECHARGE);
1450 /* Intentional fallthrough */
1452 case STATE_WAIT_FOR_RECHARGE:
1453 if (di->batt_data.percent <=
1454 di->bm->bat_type[di->bm->batt_id].
1456 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1459 case STATE_MAINTENANCE_A_INIT:
1460 abx500_chargalg_stop_safety_timer(di);
1461 abx500_chargalg_start_maintenance_timer(di,
1463 di->bm->batt_id].maint_a_chg_timer_h);
1464 abx500_chargalg_start_charging(di,
1466 di->bm->batt_id].maint_a_vol_lvl,
1468 di->bm->batt_id].maint_a_cur_lvl);
1469 abx500_chargalg_state_to(di, STATE_MAINTENANCE_A);
1470 power_supply_changed(&di->chargalg_psy);
1471 /* Intentional fallthrough*/
1473 case STATE_MAINTENANCE_A:
1474 if (di->events.maintenance_timer_expired) {
1475 abx500_chargalg_stop_maintenance_timer(di);
1476 abx500_chargalg_state_to(di, STATE_MAINTENANCE_B_INIT);
1480 case STATE_MAINTENANCE_B_INIT:
1481 abx500_chargalg_start_maintenance_timer(di,
1483 di->bm->batt_id].maint_b_chg_timer_h);
1484 abx500_chargalg_start_charging(di,
1486 di->bm->batt_id].maint_b_vol_lvl,
1488 di->bm->batt_id].maint_b_cur_lvl);
1489 abx500_chargalg_state_to(di, STATE_MAINTENANCE_B);
1490 power_supply_changed(&di->chargalg_psy);
1491 /* Intentional fallthrough*/
1493 case STATE_MAINTENANCE_B:
1494 if (di->events.maintenance_timer_expired) {
1495 abx500_chargalg_stop_maintenance_timer(di);
1496 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1500 case STATE_TEMP_LOWHIGH_INIT:
1501 abx500_chargalg_start_charging(di,
1503 di->bm->batt_id].low_high_vol_lvl,
1505 di->bm->batt_id].low_high_cur_lvl);
1506 abx500_chargalg_stop_maintenance_timer(di);
1507 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
1508 abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH);
1509 power_supply_changed(&di->chargalg_psy);
1510 /* Intentional fallthrough */
1512 case STATE_TEMP_LOWHIGH:
1513 if (!di->events.btemp_lowhigh)
1514 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1517 case STATE_WD_EXPIRED_INIT:
1518 abx500_chargalg_stop_charging(di);
1519 abx500_chargalg_state_to(di, STATE_WD_EXPIRED);
1520 /* Intentional fallthrough */
1522 case STATE_WD_EXPIRED:
1523 if (!di->events.ac_wd_expired &&
1524 !di->events.usb_wd_expired)
1525 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1528 case STATE_TEMP_UNDEROVER_INIT:
1529 abx500_chargalg_stop_charging(di);
1530 abx500_chargalg_state_to(di, STATE_TEMP_UNDEROVER);
1531 /* Intentional fallthrough */
1533 case STATE_TEMP_UNDEROVER:
1534 if (!di->events.btemp_underover)
1535 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1539 /* Start charging directly if the new state is a charge state */
1540 if (di->charge_state == STATE_NORMAL_INIT ||
1541 di->charge_state == STATE_MAINTENANCE_A_INIT ||
1542 di->charge_state == STATE_MAINTENANCE_B_INIT)
1543 queue_work(di->chargalg_wq, &di->chargalg_work);
1547 * abx500_chargalg_periodic_work() - Periodic work for the algorithm
1548 * @work: pointer to the work_struct structure
1550 * Work queue function for the charging algorithm
1552 static void abx500_chargalg_periodic_work(struct work_struct *work)
1554 struct abx500_chargalg *di = container_of(work,
1555 struct abx500_chargalg, chargalg_periodic_work.work);
1557 abx500_chargalg_algorithm(di);
1560 * If a charger is connected then the battery has to be monitored
1561 * frequently, else the work can be delayed.
1563 if (di->chg_info.conn_chg)
1564 queue_delayed_work(di->chargalg_wq,
1565 &di->chargalg_periodic_work,
1566 di->bm->interval_charging * HZ);
1568 queue_delayed_work(di->chargalg_wq,
1569 &di->chargalg_periodic_work,
1570 di->bm->interval_not_charging * HZ);
1574 * abx500_chargalg_wd_work() - periodic work to kick the charger watchdog
1575 * @work: pointer to the work_struct structure
1577 * Work queue function for kicking the charger watchdog
1579 static void abx500_chargalg_wd_work(struct work_struct *work)
1582 struct abx500_chargalg *di = container_of(work,
1583 struct abx500_chargalg, chargalg_wd_work.work);
1585 dev_dbg(di->dev, "abx500_chargalg_wd_work\n");
1587 ret = abx500_chargalg_kick_watchdog(di);
1589 dev_err(di->dev, "failed to kick watchdog\n");
1591 queue_delayed_work(di->chargalg_wq,
1592 &di->chargalg_wd_work, CHG_WD_INTERVAL);
1596 * abx500_chargalg_work() - Work to run the charging algorithm instantly
1597 * @work: pointer to the work_struct structure
1599 * Work queue function for calling the charging algorithm
1601 static void abx500_chargalg_work(struct work_struct *work)
1603 struct abx500_chargalg *di = container_of(work,
1604 struct abx500_chargalg, chargalg_work);
1606 abx500_chargalg_algorithm(di);
1610 * abx500_chargalg_get_property() - get the chargalg properties
1611 * @psy: pointer to the power_supply structure
1612 * @psp: pointer to the power_supply_property structure
1613 * @val: pointer to the power_supply_propval union
1615 * This function gets called when an application tries to get the
1616 * chargalg properties by reading the sysfs files.
1617 * status: charging/discharging/full/unknown
1618 * health: health of the battery
1619 * Returns error code in case of failure else 0 on success
1621 static int abx500_chargalg_get_property(struct power_supply *psy,
1622 enum power_supply_property psp,
1623 union power_supply_propval *val)
1625 struct abx500_chargalg *di;
1627 di = to_abx500_chargalg_device_info(psy);
1630 case POWER_SUPPLY_PROP_STATUS:
1631 val->intval = di->charge_status;
1633 case POWER_SUPPLY_PROP_HEALTH:
1634 if (di->events.batt_ovv) {
1635 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
1636 } else if (di->events.btemp_underover) {
1637 if (di->batt_data.temp <= di->bm->temp_under)
1638 val->intval = POWER_SUPPLY_HEALTH_COLD;
1640 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
1641 } else if (di->charge_state == STATE_SAFETY_TIMER_EXPIRED ||
1642 di->charge_state == STATE_SAFETY_TIMER_EXPIRED_INIT) {
1643 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
1645 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1654 /* Exposure to the sysfs interface */
1657 * abx500_chargalg_sysfs_charger() - sysfs store operations
1658 * @kobj: pointer to the struct kobject
1659 * @attr: pointer to the struct attribute
1660 * @buf: buffer that holds the parameter passed from userspace
1661 * @length: length of the parameter passed
1663 * Returns length of the buffer(input taken from user space) on success
1664 * else error code on failure
1665 * The operation to be performed on passing the parameters from the user space.
1667 static ssize_t abx500_chargalg_sysfs_charger(struct kobject *kobj,
1668 struct attribute *attr, const char *buf, size_t length)
1670 struct abx500_chargalg *di = container_of(kobj,
1671 struct abx500_chargalg, chargalg_kobject);
1675 char entry = *attr->name;
1679 ret = strict_strtol(buf, 10, ¶m);
1686 /* Disable charging */
1687 di->susp_status.ac_suspended = true;
1688 di->susp_status.usb_suspended = true;
1689 di->susp_status.suspended_change = true;
1690 /* Trigger a state change */
1691 queue_work(di->chargalg_wq,
1692 &di->chargalg_work);
1695 /* Enable AC Charging */
1696 di->susp_status.ac_suspended = false;
1697 di->susp_status.suspended_change = true;
1698 /* Trigger a state change */
1699 queue_work(di->chargalg_wq,
1700 &di->chargalg_work);
1703 /* Enable USB charging */
1704 di->susp_status.usb_suspended = false;
1705 di->susp_status.suspended_change = true;
1706 /* Trigger a state change */
1707 queue_work(di->chargalg_wq,
1708 &di->chargalg_work);
1711 dev_info(di->dev, "Wrong input\n"
1712 "Enter 0. Disable AC/USB Charging\n"
1713 "1. Enable AC charging\n"
1714 "2. Enable USB Charging\n");
1721 static struct attribute abx500_chargalg_en_charger = \
1727 static struct attribute *abx500_chargalg_chg[] = {
1728 &abx500_chargalg_en_charger,
1732 static const struct sysfs_ops abx500_chargalg_sysfs_ops = {
1733 .store = abx500_chargalg_sysfs_charger,
1736 static struct kobj_type abx500_chargalg_ktype = {
1737 .sysfs_ops = &abx500_chargalg_sysfs_ops,
1738 .default_attrs = abx500_chargalg_chg,
1742 * abx500_chargalg_sysfs_exit() - de-init of sysfs entry
1743 * @di: pointer to the struct abx500_chargalg
1745 * This function removes the entry in sysfs.
1747 static void abx500_chargalg_sysfs_exit(struct abx500_chargalg *di)
1749 kobject_del(&di->chargalg_kobject);
1753 * abx500_chargalg_sysfs_init() - init of sysfs entry
1754 * @di: pointer to the struct abx500_chargalg
1756 * This function adds an entry in sysfs.
1757 * Returns error code in case of failure else 0(on success)
1759 static int abx500_chargalg_sysfs_init(struct abx500_chargalg *di)
1763 ret = kobject_init_and_add(&di->chargalg_kobject,
1764 &abx500_chargalg_ktype,
1765 NULL, "abx500_chargalg");
1767 dev_err(di->dev, "failed to create sysfs entry\n");
1771 /* Exposure to the sysfs interface <<END>> */
1773 #if defined(CONFIG_PM)
1774 static int abx500_chargalg_resume(struct platform_device *pdev)
1776 struct abx500_chargalg *di = platform_get_drvdata(pdev);
1778 /* Kick charger watchdog if charging (any charger online) */
1779 if (di->chg_info.online_chg)
1780 queue_delayed_work(di->chargalg_wq, &di->chargalg_wd_work, 0);
1783 * Run the charging algorithm directly to be sure we don't
1786 queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
1791 static int abx500_chargalg_suspend(struct platform_device *pdev,
1794 struct abx500_chargalg *di = platform_get_drvdata(pdev);
1796 if (di->chg_info.online_chg)
1797 cancel_delayed_work_sync(&di->chargalg_wd_work);
1799 cancel_delayed_work_sync(&di->chargalg_periodic_work);
1804 #define abx500_chargalg_suspend NULL
1805 #define abx500_chargalg_resume NULL
1808 static int abx500_chargalg_remove(struct platform_device *pdev)
1810 struct abx500_chargalg *di = platform_get_drvdata(pdev);
1812 /* sysfs interface to enable/disbale charging from user space */
1813 abx500_chargalg_sysfs_exit(di);
1815 /* Delete the work queue */
1816 destroy_workqueue(di->chargalg_wq);
1818 flush_scheduled_work();
1819 power_supply_unregister(&di->chargalg_psy);
1820 platform_set_drvdata(pdev, NULL);
1825 static char *supply_interface[] = {
1829 static int abx500_chargalg_probe(struct platform_device *pdev)
1831 struct device_node *np = pdev->dev.of_node;
1832 struct abx500_bm_data *plat = pdev->dev.platform_data;
1833 struct abx500_chargalg *di;
1836 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
1838 dev_err(&pdev->dev, "%s no mem for ab8500_chargalg\n", __func__);
1843 dev_err(&pdev->dev, "no battery management data supplied\n");
1849 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
1851 dev_err(&pdev->dev, "failed to get battery information\n");
1856 /* get device struct */
1857 di->dev = &pdev->dev;
1859 /* chargalg supply */
1860 di->chargalg_psy.name = "abx500_chargalg";
1861 di->chargalg_psy.type = POWER_SUPPLY_TYPE_BATTERY;
1862 di->chargalg_psy.properties = abx500_chargalg_props;
1863 di->chargalg_psy.num_properties = ARRAY_SIZE(abx500_chargalg_props);
1864 di->chargalg_psy.get_property = abx500_chargalg_get_property;
1865 di->chargalg_psy.supplied_to = supply_interface;
1866 di->chargalg_psy.num_supplicants = ARRAY_SIZE(supply_interface),
1867 di->chargalg_psy.external_power_changed =
1868 abx500_chargalg_external_power_changed;
1870 /* Initilialize safety timer */
1871 init_timer(&di->safety_timer);
1872 di->safety_timer.function = abx500_chargalg_safety_timer_expired;
1873 di->safety_timer.data = (unsigned long) di;
1875 /* Initilialize maintenance timer */
1876 init_timer(&di->maintenance_timer);
1877 di->maintenance_timer.function =
1878 abx500_chargalg_maintenance_timer_expired;
1879 di->maintenance_timer.data = (unsigned long) di;
1881 /* Create a work queue for the chargalg */
1883 create_singlethread_workqueue("abx500_chargalg_wq");
1884 if (di->chargalg_wq == NULL) {
1885 dev_err(di->dev, "failed to create work queue\n");
1889 /* Init work for chargalg */
1890 INIT_DEFERRABLE_WORK(&di->chargalg_periodic_work,
1891 abx500_chargalg_periodic_work);
1892 INIT_DEFERRABLE_WORK(&di->chargalg_wd_work,
1893 abx500_chargalg_wd_work);
1895 /* Init work for chargalg */
1896 INIT_WORK(&di->chargalg_work, abx500_chargalg_work);
1898 /* To detect charger at startup */
1899 di->chg_info.prev_conn_chg = -1;
1901 /* Register chargalg power supply class */
1902 ret = power_supply_register(di->dev, &di->chargalg_psy);
1904 dev_err(di->dev, "failed to register chargalg psy\n");
1905 goto free_chargalg_wq;
1908 platform_set_drvdata(pdev, di);
1910 /* sysfs interface to enable/disable charging from user space */
1911 ret = abx500_chargalg_sysfs_init(di);
1913 dev_err(di->dev, "failed to create sysfs entry\n");
1917 /* Run the charging algorithm */
1918 queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
1920 dev_info(di->dev, "probe success\n");
1924 power_supply_unregister(&di->chargalg_psy);
1926 destroy_workqueue(di->chargalg_wq);
1930 static const struct of_device_id ab8500_chargalg_match[] = {
1931 { .compatible = "stericsson,ab8500-chargalg", },
1935 static struct platform_driver abx500_chargalg_driver = {
1936 .probe = abx500_chargalg_probe,
1937 .remove = abx500_chargalg_remove,
1938 .suspend = abx500_chargalg_suspend,
1939 .resume = abx500_chargalg_resume,
1941 .name = "ab8500-chargalg",
1942 .owner = THIS_MODULE,
1943 .of_match_table = ab8500_chargalg_match,
1947 static int __init abx500_chargalg_init(void)
1949 return platform_driver_register(&abx500_chargalg_driver);
1952 static void __exit abx500_chargalg_exit(void)
1954 platform_driver_unregister(&abx500_chargalg_driver);
1957 module_init(abx500_chargalg_init);
1958 module_exit(abx500_chargalg_exit);
1960 MODULE_LICENSE("GPL v2");
1961 MODULE_AUTHOR("Johan Palsson, Karl Komierowski");
1962 MODULE_ALIAS("platform:abx500-chargalg");
1963 MODULE_DESCRIPTION("abx500 battery charging algorithm");