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 * @parent: pointer to the struct abx500
208 * @chargalg_psy: structure that holds the battery properties exposed by
209 * the charging algorithm
210 * @events: structure for information about events triggered
211 * @chargalg_wq: work queue for running the charging algorithm
212 * @chargalg_periodic_work: work to run the charging algorithm periodically
213 * @chargalg_wd_work: work to kick the charger watchdog periodically
214 * @chargalg_work: work to run the charging algorithm instantly
215 * @safety_timer: charging safety timer
216 * @maintenance_timer: maintenance charging timer
217 * @chargalg_kobject: structure of type kobject
219 struct abx500_chargalg {
223 bool maintenance_chg;
226 enum abx500_chargalg_states charge_state;
227 struct abx500_charge_curr_maximization ccm;
228 struct abx500_chargalg_charger_info chg_info;
229 struct abx500_chargalg_battery_data batt_data;
230 struct abx500_chargalg_suspension_status susp_status;
231 struct ab8500 *parent;
232 struct abx500_bm_data *bm;
233 struct power_supply chargalg_psy;
234 struct ux500_charger *ac_chg;
235 struct ux500_charger *usb_chg;
236 struct abx500_chargalg_events events;
237 struct workqueue_struct *chargalg_wq;
238 struct delayed_work chargalg_periodic_work;
239 struct delayed_work chargalg_wd_work;
240 struct work_struct chargalg_work;
241 struct timer_list safety_timer;
242 struct timer_list maintenance_timer;
243 struct kobject chargalg_kobject;
246 /* Main battery properties */
247 static enum power_supply_property abx500_chargalg_props[] = {
248 POWER_SUPPLY_PROP_STATUS,
249 POWER_SUPPLY_PROP_HEALTH,
253 * abx500_chargalg_safety_timer_expired() - Expiration of the safety timer
254 * @data: pointer to the abx500_chargalg structure
256 * This function gets called when the safety timer for the charger
259 static void abx500_chargalg_safety_timer_expired(unsigned long data)
261 struct abx500_chargalg *di = (struct abx500_chargalg *) data;
262 dev_err(di->dev, "Safety timer expired\n");
263 di->events.safety_timer_expired = true;
265 /* Trigger execution of the algorithm instantly */
266 queue_work(di->chargalg_wq, &di->chargalg_work);
270 * abx500_chargalg_maintenance_timer_expired() - Expiration of
271 * the maintenance timer
272 * @i: pointer to the abx500_chargalg structure
274 * This function gets called when the maintenence timer
277 static void abx500_chargalg_maintenance_timer_expired(unsigned long data)
280 struct abx500_chargalg *di = (struct abx500_chargalg *) data;
281 dev_dbg(di->dev, "Maintenance timer expired\n");
282 di->events.maintenance_timer_expired = true;
284 /* Trigger execution of the algorithm instantly */
285 queue_work(di->chargalg_wq, &di->chargalg_work);
289 * abx500_chargalg_state_to() - Change charge state
290 * @di: pointer to the abx500_chargalg structure
292 * This function gets called when a charge state change should occur
294 static void abx500_chargalg_state_to(struct abx500_chargalg *di,
295 enum abx500_chargalg_states state)
298 "State changed: %s (From state: [%d] %s =to=> [%d] %s )\n",
299 di->charge_state == state ? "NO" : "YES",
301 states[di->charge_state],
305 di->charge_state = state;
308 static int abx500_chargalg_check_charger_enable(struct abx500_chargalg *di)
310 switch (di->charge_state) {
312 case STATE_MAINTENANCE_A:
313 case STATE_MAINTENANCE_B:
319 if (di->chg_info.charger_type & USB_CHG) {
320 return di->usb_chg->ops.check_enable(di->usb_chg,
321 di->bm->bat_type[di->bm->batt_id].normal_vol_lvl,
322 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
323 } else if ((di->chg_info.charger_type & AC_CHG) &&
324 !(di->ac_chg->external)) {
325 return di->ac_chg->ops.check_enable(di->ac_chg,
326 di->bm->bat_type[di->bm->batt_id].normal_vol_lvl,
327 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
333 * abx500_chargalg_check_charger_connection() - Check charger connection change
334 * @di: pointer to the abx500_chargalg structure
336 * This function will check if there is a change in the charger connection
337 * and change charge state accordingly. AC has precedence over USB.
339 static int abx500_chargalg_check_charger_connection(struct abx500_chargalg *di)
341 if (di->chg_info.conn_chg != di->chg_info.prev_conn_chg ||
342 di->susp_status.suspended_change) {
344 * Charger state changed or suspension
345 * has changed since last update
347 if ((di->chg_info.conn_chg & AC_CHG) &&
348 !di->susp_status.ac_suspended) {
349 dev_dbg(di->dev, "Charging source is AC\n");
350 if (di->chg_info.charger_type != AC_CHG) {
351 di->chg_info.charger_type = AC_CHG;
352 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
354 } else if ((di->chg_info.conn_chg & USB_CHG) &&
355 !di->susp_status.usb_suspended) {
356 dev_dbg(di->dev, "Charging source is USB\n");
357 di->chg_info.charger_type = USB_CHG;
358 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
359 } else if (di->chg_info.conn_chg &&
360 (di->susp_status.ac_suspended ||
361 di->susp_status.usb_suspended)) {
362 dev_dbg(di->dev, "Charging is suspended\n");
363 di->chg_info.charger_type = NO_CHG;
364 abx500_chargalg_state_to(di, STATE_SUSPENDED_INIT);
366 dev_dbg(di->dev, "Charging source is OFF\n");
367 di->chg_info.charger_type = NO_CHG;
368 abx500_chargalg_state_to(di, STATE_HANDHELD_INIT);
370 di->chg_info.prev_conn_chg = di->chg_info.conn_chg;
371 di->susp_status.suspended_change = false;
373 return di->chg_info.conn_chg;
377 * abx500_chargalg_start_safety_timer() - Start charging safety timer
378 * @di: pointer to the abx500_chargalg structure
380 * The safety timer is used to avoid overcharging of old or bad batteries.
381 * There are different timers for AC and USB
383 static void abx500_chargalg_start_safety_timer(struct abx500_chargalg *di)
385 unsigned long timer_expiration = 0;
387 switch (di->chg_info.charger_type) {
390 round_jiffies(jiffies +
391 (di->bm->main_safety_tmr_h * 3600 * HZ));
396 round_jiffies(jiffies +
397 (di->bm->usb_safety_tmr_h * 3600 * HZ));
401 dev_err(di->dev, "Unknown charger to charge from\n");
405 di->events.safety_timer_expired = false;
406 di->safety_timer.expires = timer_expiration;
407 if (!timer_pending(&di->safety_timer))
408 add_timer(&di->safety_timer);
410 mod_timer(&di->safety_timer, timer_expiration);
414 * abx500_chargalg_stop_safety_timer() - Stop charging safety timer
415 * @di: pointer to the abx500_chargalg structure
417 * The safety timer is stopped whenever the NORMAL state is exited
419 static void abx500_chargalg_stop_safety_timer(struct abx500_chargalg *di)
421 di->events.safety_timer_expired = false;
422 del_timer(&di->safety_timer);
426 * abx500_chargalg_start_maintenance_timer() - Start charging maintenance timer
427 * @di: pointer to the abx500_chargalg structure
428 * @duration: duration of ther maintenance timer in hours
430 * The maintenance timer is used to maintain the charge in the battery once
431 * the battery is considered full. These timers are chosen to match the
432 * discharge curve of the battery
434 static void abx500_chargalg_start_maintenance_timer(struct abx500_chargalg *di,
437 unsigned long timer_expiration;
439 /* Convert from hours to jiffies */
440 timer_expiration = round_jiffies(jiffies + (duration * 3600 * HZ));
442 di->events.maintenance_timer_expired = false;
443 di->maintenance_timer.expires = timer_expiration;
444 if (!timer_pending(&di->maintenance_timer))
445 add_timer(&di->maintenance_timer);
447 mod_timer(&di->maintenance_timer, timer_expiration);
451 * abx500_chargalg_stop_maintenance_timer() - Stop maintenance timer
452 * @di: pointer to the abx500_chargalg structure
454 * The maintenance timer is stopped whenever maintenance ends or when another
457 static void abx500_chargalg_stop_maintenance_timer(struct abx500_chargalg *di)
459 di->events.maintenance_timer_expired = false;
460 del_timer(&di->maintenance_timer);
464 * abx500_chargalg_kick_watchdog() - Kick charger watchdog
465 * @di: pointer to the abx500_chargalg structure
467 * The charger watchdog have to be kicked periodically whenever the charger is
468 * on, else the ABB will reset the system
470 static int abx500_chargalg_kick_watchdog(struct abx500_chargalg *di)
472 /* Check if charger exists and kick watchdog if charging */
473 if (di->ac_chg && di->ac_chg->ops.kick_wd &&
474 di->chg_info.online_chg & AC_CHG) {
476 * If AB charger watchdog expired, pm2xxx charging
477 * gets disabled. To be safe, kick both AB charger watchdog
478 * and pm2xxx watchdog.
480 if (di->ac_chg->external &&
481 di->usb_chg && di->usb_chg->ops.kick_wd)
482 di->usb_chg->ops.kick_wd(di->usb_chg);
484 return di->ac_chg->ops.kick_wd(di->ac_chg);
486 else if (di->usb_chg && di->usb_chg->ops.kick_wd &&
487 di->chg_info.online_chg & USB_CHG)
488 return di->usb_chg->ops.kick_wd(di->usb_chg);
494 * abx500_chargalg_ac_en() - Turn on/off the AC charger
495 * @di: pointer to the abx500_chargalg structure
496 * @enable: charger on/off
497 * @vset: requested charger output voltage
498 * @iset: requested charger output current
500 * The AC charger will be turned on/off with the requested charge voltage and
503 static int abx500_chargalg_ac_en(struct abx500_chargalg *di, int enable,
506 if (!di->ac_chg || !di->ac_chg->ops.enable)
509 /* Select maximum of what both the charger and the battery supports */
510 if (di->ac_chg->max_out_volt)
511 vset = min(vset, di->ac_chg->max_out_volt);
512 if (di->ac_chg->max_out_curr)
513 iset = min(iset, di->ac_chg->max_out_curr);
515 di->chg_info.ac_iset = iset;
516 di->chg_info.ac_vset = vset;
518 return di->ac_chg->ops.enable(di->ac_chg, enable, vset, iset);
522 * abx500_chargalg_usb_en() - Turn on/off the USB charger
523 * @di: pointer to the abx500_chargalg structure
524 * @enable: charger on/off
525 * @vset: requested charger output voltage
526 * @iset: requested charger output current
528 * The USB charger will be turned on/off with the requested charge voltage and
531 static int abx500_chargalg_usb_en(struct abx500_chargalg *di, int enable,
534 if (!di->usb_chg || !di->usb_chg->ops.enable)
537 /* Select maximum of what both the charger and the battery supports */
538 if (di->usb_chg->max_out_volt)
539 vset = min(vset, di->usb_chg->max_out_volt);
540 if (di->usb_chg->max_out_curr)
541 iset = min(iset, di->usb_chg->max_out_curr);
543 di->chg_info.usb_iset = iset;
544 di->chg_info.usb_vset = vset;
546 return di->usb_chg->ops.enable(di->usb_chg, enable, vset, iset);
550 * abx500_chargalg_update_chg_curr() - Update charger current
551 * @di: pointer to the abx500_chargalg structure
552 * @iset: requested charger output current
554 * The charger output current will be updated for the charger
555 * that is currently in use
557 static int abx500_chargalg_update_chg_curr(struct abx500_chargalg *di,
560 /* Check if charger exists and update current if charging */
561 if (di->ac_chg && di->ac_chg->ops.update_curr &&
562 di->chg_info.charger_type & AC_CHG) {
564 * Select maximum of what both the charger
565 * and the battery supports
567 if (di->ac_chg->max_out_curr)
568 iset = min(iset, di->ac_chg->max_out_curr);
570 di->chg_info.ac_iset = iset;
572 return di->ac_chg->ops.update_curr(di->ac_chg, iset);
573 } else if (di->usb_chg && di->usb_chg->ops.update_curr &&
574 di->chg_info.charger_type & USB_CHG) {
576 * Select maximum of what both the charger
577 * and the battery supports
579 if (di->usb_chg->max_out_curr)
580 iset = min(iset, di->usb_chg->max_out_curr);
582 di->chg_info.usb_iset = iset;
584 return di->usb_chg->ops.update_curr(di->usb_chg, iset);
591 * abx500_chargalg_stop_charging() - Stop charging
592 * @di: pointer to the abx500_chargalg structure
594 * This function is called from any state where charging should be stopped.
595 * All charging is disabled and all status parameters and timers are changed
598 static void abx500_chargalg_stop_charging(struct abx500_chargalg *di)
600 abx500_chargalg_ac_en(di, false, 0, 0);
601 abx500_chargalg_usb_en(di, false, 0, 0);
602 abx500_chargalg_stop_safety_timer(di);
603 abx500_chargalg_stop_maintenance_timer(di);
604 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
605 di->maintenance_chg = false;
606 cancel_delayed_work(&di->chargalg_wd_work);
607 power_supply_changed(&di->chargalg_psy);
611 * abx500_chargalg_hold_charging() - Pauses charging
612 * @di: pointer to the abx500_chargalg structure
614 * This function is called in the case where maintenance charging has been
615 * disabled and instead a battery voltage mode is entered to check when the
616 * battery voltage has reached a certain recharge voltage
618 static void abx500_chargalg_hold_charging(struct abx500_chargalg *di)
620 abx500_chargalg_ac_en(di, false, 0, 0);
621 abx500_chargalg_usb_en(di, false, 0, 0);
622 abx500_chargalg_stop_safety_timer(di);
623 abx500_chargalg_stop_maintenance_timer(di);
624 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
625 di->maintenance_chg = false;
626 cancel_delayed_work(&di->chargalg_wd_work);
627 power_supply_changed(&di->chargalg_psy);
631 * abx500_chargalg_start_charging() - Start the charger
632 * @di: pointer to the abx500_chargalg structure
633 * @vset: requested charger output voltage
634 * @iset: requested charger output current
636 * A charger will be enabled depending on the requested charger type that was
637 * detected previously.
639 static void abx500_chargalg_start_charging(struct abx500_chargalg *di,
642 bool start_chargalg_wd = true;
644 switch (di->chg_info.charger_type) {
647 "AC parameters: Vset %d, Ich %d\n", vset, iset);
648 abx500_chargalg_usb_en(di, false, 0, 0);
649 abx500_chargalg_ac_en(di, true, vset, iset);
654 "USB parameters: Vset %d, Ich %d\n", vset, iset);
655 abx500_chargalg_ac_en(di, false, 0, 0);
656 abx500_chargalg_usb_en(di, true, vset, iset);
660 dev_err(di->dev, "Unknown charger to charge from\n");
661 start_chargalg_wd = false;
665 if (start_chargalg_wd && !delayed_work_pending(&di->chargalg_wd_work))
666 queue_delayed_work(di->chargalg_wq, &di->chargalg_wd_work, 0);
670 * abx500_chargalg_check_temp() - Check battery temperature ranges
671 * @di: pointer to the abx500_chargalg structure
673 * The battery temperature is checked against the predefined limits and the
674 * charge state is changed accordingly
676 static void abx500_chargalg_check_temp(struct abx500_chargalg *di)
678 if (di->batt_data.temp > (di->bm->temp_low + di->t_hyst_norm) &&
679 di->batt_data.temp < (di->bm->temp_high - di->t_hyst_norm)) {
681 di->events.btemp_underover = false;
682 di->events.btemp_lowhigh = false;
684 di->t_hyst_lowhigh = 0;
686 if (((di->batt_data.temp >= di->bm->temp_high) &&
687 (di->batt_data.temp <
688 (di->bm->temp_over - di->t_hyst_lowhigh))) ||
689 ((di->batt_data.temp >
690 (di->bm->temp_under + di->t_hyst_lowhigh)) &&
691 (di->batt_data.temp <= di->bm->temp_low))) {
692 /* TEMP minor!!!!! */
693 di->events.btemp_underover = false;
694 di->events.btemp_lowhigh = true;
695 di->t_hyst_norm = di->bm->temp_hysteresis;
696 di->t_hyst_lowhigh = 0;
697 } else if (di->batt_data.temp <= di->bm->temp_under ||
698 di->batt_data.temp >= di->bm->temp_over) {
699 /* TEMP major!!!!! */
700 di->events.btemp_underover = true;
701 di->events.btemp_lowhigh = false;
703 di->t_hyst_lowhigh = di->bm->temp_hysteresis;
705 /* Within hysteresis */
706 dev_dbg(di->dev, "Within hysteresis limit temp: %d "
707 "hyst_lowhigh %d, hyst normal %d\n",
708 di->batt_data.temp, di->t_hyst_lowhigh,
715 * abx500_chargalg_check_charger_voltage() - Check charger voltage
716 * @di: pointer to the abx500_chargalg structure
718 * Charger voltage is checked against maximum limit
720 static void abx500_chargalg_check_charger_voltage(struct abx500_chargalg *di)
722 if (di->chg_info.usb_volt > di->bm->chg_params->usb_volt_max)
723 di->chg_info.usb_chg_ok = false;
725 di->chg_info.usb_chg_ok = true;
727 if (di->chg_info.ac_volt > di->bm->chg_params->ac_volt_max)
728 di->chg_info.ac_chg_ok = false;
730 di->chg_info.ac_chg_ok = true;
735 * abx500_chargalg_end_of_charge() - Check if end-of-charge criteria is fulfilled
736 * @di: pointer to the abx500_chargalg structure
738 * End-of-charge criteria is fulfilled when the battery voltage is above a
739 * certain limit and the battery current is below a certain limit for a
740 * predefined number of consecutive seconds. If true, the battery is full
742 static void abx500_chargalg_end_of_charge(struct abx500_chargalg *di)
744 if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING &&
745 di->charge_state == STATE_NORMAL &&
746 !di->maintenance_chg && (di->batt_data.volt >=
747 di->bm->bat_type[di->bm->batt_id].termination_vol ||
748 di->events.usb_cv_active || di->events.ac_cv_active) &&
749 di->batt_data.avg_curr <
750 di->bm->bat_type[di->bm->batt_id].termination_curr &&
751 di->batt_data.avg_curr > 0) {
752 if (++di->eoc_cnt >= EOC_COND_CNT) {
754 di->charge_status = POWER_SUPPLY_STATUS_FULL;
755 di->maintenance_chg = true;
756 dev_dbg(di->dev, "EOC reached!\n");
757 power_supply_changed(&di->chargalg_psy);
760 " EOC limit reached for the %d"
761 " time, out of %d before EOC\n",
770 static void init_maxim_chg_curr(struct abx500_chargalg *di)
772 di->ccm.original_iset =
773 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl;
774 di->ccm.current_iset =
775 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl;
776 di->ccm.test_delta_i = di->bm->maxi->charger_curr_step;
777 di->ccm.max_current = di->bm->maxi->chg_curr;
778 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
783 * abx500_chargalg_chg_curr_maxim - increases the charger current to
784 * compensate for the system load
785 * @di pointer to the abx500_chargalg structure
787 * This maximization function is used to raise the charger current to get the
788 * battery current as close to the optimal value as possible. The battery
789 * current during charging is affected by the system load
791 static enum maxim_ret abx500_chargalg_chg_curr_maxim(struct abx500_chargalg *di)
795 if (!di->bm->maxi->ena_maxi)
796 return MAXIM_RET_NOACTION;
798 delta_i = di->ccm.original_iset - di->batt_data.inst_curr;
800 if (di->events.vbus_collapsed) {
801 dev_dbg(di->dev, "Charger voltage has collapsed %d\n",
803 if (di->ccm.wait_cnt == 0) {
804 dev_dbg(di->dev, "lowering current\n");
806 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
807 di->ccm.max_current =
808 di->ccm.current_iset - di->ccm.test_delta_i;
809 di->ccm.current_iset = di->ccm.max_current;
811 return MAXIM_RET_CHANGE;
813 dev_dbg(di->dev, "waiting\n");
814 /* Let's go in here twice before lowering curr again */
815 di->ccm.wait_cnt = (di->ccm.wait_cnt + 1) % 3;
816 return MAXIM_RET_NOACTION;
820 di->ccm.wait_cnt = 0;
822 if ((di->batt_data.inst_curr > di->ccm.original_iset)) {
823 dev_dbg(di->dev, " Maximization Ibat (%dmA) too high"
824 " (limit %dmA) (current iset: %dmA)!\n",
825 di->batt_data.inst_curr, di->ccm.original_iset,
826 di->ccm.current_iset);
828 if (di->ccm.current_iset == di->ccm.original_iset)
829 return MAXIM_RET_NOACTION;
831 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
832 di->ccm.current_iset = di->ccm.original_iset;
835 return MAXIM_RET_IBAT_TOO_HIGH;
838 if (delta_i > di->ccm.test_delta_i &&
839 (di->ccm.current_iset + di->ccm.test_delta_i) <
840 di->ccm.max_current) {
841 if (di->ccm.condition_cnt-- == 0) {
842 /* Increse the iset with cco.test_delta_i */
843 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
844 di->ccm.current_iset += di->ccm.test_delta_i;
846 dev_dbg(di->dev, " Maximization needed, increase"
847 " with %d mA to %dmA (Optimal ibat: %d)"
849 di->ccm.test_delta_i,
850 di->ccm.current_iset,
851 di->ccm.original_iset,
853 return MAXIM_RET_CHANGE;
855 return MAXIM_RET_NOACTION;
858 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
859 return MAXIM_RET_NOACTION;
863 static void handle_maxim_chg_curr(struct abx500_chargalg *di)
868 ret = abx500_chargalg_chg_curr_maxim(di);
870 case MAXIM_RET_CHANGE:
871 result = abx500_chargalg_update_chg_curr(di,
872 di->ccm.current_iset);
874 dev_err(di->dev, "failed to set chg curr\n");
876 case MAXIM_RET_IBAT_TOO_HIGH:
877 result = abx500_chargalg_update_chg_curr(di,
878 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
880 dev_err(di->dev, "failed to set chg curr\n");
883 case MAXIM_RET_NOACTION:
890 static int abx500_chargalg_get_ext_psy_data(struct device *dev, void *data)
892 struct power_supply *psy;
893 struct power_supply *ext;
894 struct abx500_chargalg *di;
895 union power_supply_propval ret;
897 bool psy_found = false;
898 bool capacity_updated = false;
900 psy = (struct power_supply *)data;
901 ext = dev_get_drvdata(dev);
902 di = to_abx500_chargalg_device_info(psy);
903 /* For all psy where the driver name appears in any supplied_to */
904 for (i = 0; i < ext->num_supplicants; i++) {
905 if (!strcmp(ext->supplied_to[i], psy->name))
912 * If external is not registering 'POWER_SUPPLY_PROP_CAPACITY' to its
913 * property because of handling that sysfs entry on its own, this is
914 * the place to get the battery capacity.
916 if (!ext->get_property(ext, POWER_SUPPLY_PROP_CAPACITY, &ret)) {
917 di->batt_data.percent = ret.intval;
918 capacity_updated = true;
921 /* Go through all properties for the psy */
922 for (j = 0; j < ext->num_properties; j++) {
923 enum power_supply_property prop;
924 prop = ext->properties[j];
926 /* Initialize chargers if not already done */
928 ext->type == POWER_SUPPLY_TYPE_MAINS)
929 di->ac_chg = psy_to_ux500_charger(ext);
930 else if (!di->usb_chg &&
931 ext->type == POWER_SUPPLY_TYPE_USB)
932 di->usb_chg = psy_to_ux500_charger(ext);
934 if (ext->get_property(ext, prop, &ret))
937 case POWER_SUPPLY_PROP_PRESENT:
939 case POWER_SUPPLY_TYPE_BATTERY:
940 /* Battery present */
942 di->events.batt_rem = false;
943 /* Battery removed */
945 di->events.batt_rem = true;
947 case POWER_SUPPLY_TYPE_MAINS:
948 /* AC disconnected */
950 (di->chg_info.conn_chg & AC_CHG)) {
951 di->chg_info.prev_conn_chg =
952 di->chg_info.conn_chg;
953 di->chg_info.conn_chg &= ~AC_CHG;
956 else if (ret.intval &&
957 !(di->chg_info.conn_chg & AC_CHG)) {
958 di->chg_info.prev_conn_chg =
959 di->chg_info.conn_chg;
960 di->chg_info.conn_chg |= AC_CHG;
963 case POWER_SUPPLY_TYPE_USB:
964 /* USB disconnected */
966 (di->chg_info.conn_chg & USB_CHG)) {
967 di->chg_info.prev_conn_chg =
968 di->chg_info.conn_chg;
969 di->chg_info.conn_chg &= ~USB_CHG;
972 else if (ret.intval &&
973 !(di->chg_info.conn_chg & USB_CHG)) {
974 di->chg_info.prev_conn_chg =
975 di->chg_info.conn_chg;
976 di->chg_info.conn_chg |= USB_CHG;
984 case POWER_SUPPLY_PROP_ONLINE:
986 case POWER_SUPPLY_TYPE_BATTERY:
988 case POWER_SUPPLY_TYPE_MAINS:
991 (di->chg_info.online_chg & AC_CHG)) {
992 di->chg_info.prev_online_chg =
993 di->chg_info.online_chg;
994 di->chg_info.online_chg &= ~AC_CHG;
997 else if (ret.intval &&
998 !(di->chg_info.online_chg & AC_CHG)) {
999 di->chg_info.prev_online_chg =
1000 di->chg_info.online_chg;
1001 di->chg_info.online_chg |= AC_CHG;
1002 queue_delayed_work(di->chargalg_wq,
1003 &di->chargalg_wd_work, 0);
1006 case POWER_SUPPLY_TYPE_USB:
1009 (di->chg_info.online_chg & USB_CHG)) {
1010 di->chg_info.prev_online_chg =
1011 di->chg_info.online_chg;
1012 di->chg_info.online_chg &= ~USB_CHG;
1015 else if (ret.intval &&
1016 !(di->chg_info.online_chg & USB_CHG)) {
1017 di->chg_info.prev_online_chg =
1018 di->chg_info.online_chg;
1019 di->chg_info.online_chg |= USB_CHG;
1020 queue_delayed_work(di->chargalg_wq,
1021 &di->chargalg_wd_work, 0);
1029 case POWER_SUPPLY_PROP_HEALTH:
1030 switch (ext->type) {
1031 case POWER_SUPPLY_TYPE_BATTERY:
1033 case POWER_SUPPLY_TYPE_MAINS:
1034 switch (ret.intval) {
1035 case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
1036 di->events.mainextchnotok = true;
1037 di->events.main_thermal_prot = false;
1038 di->events.main_ovv = false;
1039 di->events.ac_wd_expired = false;
1041 case POWER_SUPPLY_HEALTH_DEAD:
1042 di->events.ac_wd_expired = true;
1043 di->events.mainextchnotok = false;
1044 di->events.main_ovv = false;
1045 di->events.main_thermal_prot = false;
1047 case POWER_SUPPLY_HEALTH_COLD:
1048 case POWER_SUPPLY_HEALTH_OVERHEAT:
1049 di->events.main_thermal_prot = true;
1050 di->events.mainextchnotok = false;
1051 di->events.main_ovv = false;
1052 di->events.ac_wd_expired = false;
1054 case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
1055 di->events.main_ovv = true;
1056 di->events.mainextchnotok = false;
1057 di->events.main_thermal_prot = false;
1058 di->events.ac_wd_expired = false;
1060 case POWER_SUPPLY_HEALTH_GOOD:
1061 di->events.main_thermal_prot = false;
1062 di->events.mainextchnotok = false;
1063 di->events.main_ovv = false;
1064 di->events.ac_wd_expired = false;
1071 case POWER_SUPPLY_TYPE_USB:
1072 switch (ret.intval) {
1073 case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
1074 di->events.usbchargernotok = true;
1075 di->events.usb_thermal_prot = false;
1076 di->events.vbus_ovv = false;
1077 di->events.usb_wd_expired = false;
1079 case POWER_SUPPLY_HEALTH_DEAD:
1080 di->events.usb_wd_expired = true;
1081 di->events.usbchargernotok = false;
1082 di->events.usb_thermal_prot = false;
1083 di->events.vbus_ovv = false;
1085 case POWER_SUPPLY_HEALTH_COLD:
1086 case POWER_SUPPLY_HEALTH_OVERHEAT:
1087 di->events.usb_thermal_prot = true;
1088 di->events.usbchargernotok = false;
1089 di->events.vbus_ovv = false;
1090 di->events.usb_wd_expired = false;
1092 case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
1093 di->events.vbus_ovv = true;
1094 di->events.usbchargernotok = false;
1095 di->events.usb_thermal_prot = false;
1096 di->events.usb_wd_expired = false;
1098 case POWER_SUPPLY_HEALTH_GOOD:
1099 di->events.usbchargernotok = false;
1100 di->events.usb_thermal_prot = false;
1101 di->events.vbus_ovv = false;
1102 di->events.usb_wd_expired = false;
1112 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1113 switch (ext->type) {
1114 case POWER_SUPPLY_TYPE_BATTERY:
1115 di->batt_data.volt = ret.intval / 1000;
1117 case POWER_SUPPLY_TYPE_MAINS:
1118 di->chg_info.ac_volt = ret.intval / 1000;
1120 case POWER_SUPPLY_TYPE_USB:
1121 di->chg_info.usb_volt = ret.intval / 1000;
1128 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
1129 switch (ext->type) {
1130 case POWER_SUPPLY_TYPE_MAINS:
1131 /* AVG is used to indicate when we are
1134 di->events.ac_cv_active = true;
1136 di->events.ac_cv_active = false;
1139 case POWER_SUPPLY_TYPE_USB:
1140 /* AVG is used to indicate when we are
1143 di->events.usb_cv_active = true;
1145 di->events.usb_cv_active = false;
1153 case POWER_SUPPLY_PROP_TECHNOLOGY:
1154 switch (ext->type) {
1155 case POWER_SUPPLY_TYPE_BATTERY:
1157 di->events.batt_unknown = false;
1159 di->events.batt_unknown = true;
1167 case POWER_SUPPLY_PROP_TEMP:
1168 di->batt_data.temp = ret.intval / 10;
1171 case POWER_SUPPLY_PROP_CURRENT_NOW:
1172 switch (ext->type) {
1173 case POWER_SUPPLY_TYPE_MAINS:
1174 di->chg_info.ac_curr =
1177 case POWER_SUPPLY_TYPE_USB:
1178 di->chg_info.usb_curr =
1181 case POWER_SUPPLY_TYPE_BATTERY:
1182 di->batt_data.inst_curr = ret.intval / 1000;
1189 case POWER_SUPPLY_PROP_CURRENT_AVG:
1190 switch (ext->type) {
1191 case POWER_SUPPLY_TYPE_BATTERY:
1192 di->batt_data.avg_curr = ret.intval / 1000;
1194 case POWER_SUPPLY_TYPE_USB:
1196 di->events.vbus_collapsed = true;
1198 di->events.vbus_collapsed = false;
1204 case POWER_SUPPLY_PROP_CAPACITY:
1205 if (!capacity_updated)
1206 di->batt_data.percent = ret.intval;
1216 * abx500_chargalg_external_power_changed() - callback for power supply changes
1217 * @psy: pointer to the structure power_supply
1219 * This function is the entry point of the pointer external_power_changed
1220 * of the structure power_supply.
1221 * This function gets executed when there is a change in any external power
1222 * supply that this driver needs to be notified of.
1224 static void abx500_chargalg_external_power_changed(struct power_supply *psy)
1226 struct abx500_chargalg *di = to_abx500_chargalg_device_info(psy);
1229 * Trigger execution of the algorithm instantly and read
1230 * all power_supply properties there instead
1232 queue_work(di->chargalg_wq, &di->chargalg_work);
1236 * abx500_chargalg_algorithm() - Main function for the algorithm
1237 * @di: pointer to the abx500_chargalg structure
1239 * This is the main control function for the charging algorithm.
1240 * It is called periodically or when something happens that will
1241 * trigger a state change
1243 static void abx500_chargalg_algorithm(struct abx500_chargalg *di)
1248 /* Collect data from all power_supply class devices */
1249 class_for_each_device(power_supply_class, NULL,
1250 &di->chargalg_psy, abx500_chargalg_get_ext_psy_data);
1252 abx500_chargalg_end_of_charge(di);
1253 abx500_chargalg_check_temp(di);
1254 abx500_chargalg_check_charger_voltage(di);
1256 charger_status = abx500_chargalg_check_charger_connection(di);
1258 if (is_ab8500(di->parent)) {
1259 ret = abx500_chargalg_check_charger_enable(di);
1261 dev_err(di->dev, "Checking charger is enabled error"
1262 ": Returned Value %d\n", ret);
1266 * First check if we have a charger connected.
1267 * Also we don't allow charging of unknown batteries if configured
1270 if (!charger_status ||
1271 (di->events.batt_unknown && !di->bm->chg_unknown_bat)) {
1272 if (di->charge_state != STATE_HANDHELD) {
1273 di->events.safety_timer_expired = false;
1274 abx500_chargalg_state_to(di, STATE_HANDHELD_INIT);
1278 /* If suspended, we should not continue checking the flags */
1279 else if (di->charge_state == STATE_SUSPENDED_INIT ||
1280 di->charge_state == STATE_SUSPENDED) {
1281 /* We don't do anything here, just don,t continue */
1284 /* Safety timer expiration */
1285 else if (di->events.safety_timer_expired) {
1286 if (di->charge_state != STATE_SAFETY_TIMER_EXPIRED)
1287 abx500_chargalg_state_to(di,
1288 STATE_SAFETY_TIMER_EXPIRED_INIT);
1291 * Check if any interrupts has occured
1292 * that will prevent us from charging
1295 /* Battery removed */
1296 else if (di->events.batt_rem) {
1297 if (di->charge_state != STATE_BATT_REMOVED)
1298 abx500_chargalg_state_to(di, STATE_BATT_REMOVED_INIT);
1300 /* Main or USB charger not ok. */
1301 else if (di->events.mainextchnotok || di->events.usbchargernotok) {
1303 * If vbus_collapsed is set, we have to lower the charger
1304 * current, which is done in the normal state below
1306 if (di->charge_state != STATE_CHG_NOT_OK &&
1307 !di->events.vbus_collapsed)
1308 abx500_chargalg_state_to(di, STATE_CHG_NOT_OK_INIT);
1310 /* VBUS, Main or VBAT OVV. */
1311 else if (di->events.vbus_ovv ||
1312 di->events.main_ovv ||
1313 di->events.batt_ovv ||
1314 !di->chg_info.usb_chg_ok ||
1315 !di->chg_info.ac_chg_ok) {
1316 if (di->charge_state != STATE_OVV_PROTECT)
1317 abx500_chargalg_state_to(di, STATE_OVV_PROTECT_INIT);
1319 /* USB Thermal, stop charging */
1320 else if (di->events.main_thermal_prot ||
1321 di->events.usb_thermal_prot) {
1322 if (di->charge_state != STATE_HW_TEMP_PROTECT)
1323 abx500_chargalg_state_to(di,
1324 STATE_HW_TEMP_PROTECT_INIT);
1326 /* Battery temp over/under */
1327 else if (di->events.btemp_underover) {
1328 if (di->charge_state != STATE_TEMP_UNDEROVER)
1329 abx500_chargalg_state_to(di,
1330 STATE_TEMP_UNDEROVER_INIT);
1332 /* Watchdog expired */
1333 else if (di->events.ac_wd_expired ||
1334 di->events.usb_wd_expired) {
1335 if (di->charge_state != STATE_WD_EXPIRED)
1336 abx500_chargalg_state_to(di, STATE_WD_EXPIRED_INIT);
1338 /* Battery temp high/low */
1339 else if (di->events.btemp_lowhigh) {
1340 if (di->charge_state != STATE_TEMP_LOWHIGH)
1341 abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH_INIT);
1345 "[CHARGALG] Vb %d Ib_avg %d Ib_inst %d Tb %d Cap %d Maint %d "
1346 "State %s Active_chg %d Chg_status %d AC %d USB %d "
1347 "AC_online %d USB_online %d AC_CV %d USB_CV %d AC_I %d "
1348 "USB_I %d AC_Vset %d AC_Iset %d USB_Vset %d USB_Iset %d\n",
1350 di->batt_data.avg_curr,
1351 di->batt_data.inst_curr,
1353 di->batt_data.percent,
1354 di->maintenance_chg,
1355 states[di->charge_state],
1356 di->chg_info.charger_type,
1358 di->chg_info.conn_chg & AC_CHG,
1359 di->chg_info.conn_chg & USB_CHG,
1360 di->chg_info.online_chg & AC_CHG,
1361 di->chg_info.online_chg & USB_CHG,
1362 di->events.ac_cv_active,
1363 di->events.usb_cv_active,
1364 di->chg_info.ac_curr,
1365 di->chg_info.usb_curr,
1366 di->chg_info.ac_vset,
1367 di->chg_info.ac_iset,
1368 di->chg_info.usb_vset,
1369 di->chg_info.usb_iset);
1371 switch (di->charge_state) {
1372 case STATE_HANDHELD_INIT:
1373 abx500_chargalg_stop_charging(di);
1374 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
1375 abx500_chargalg_state_to(di, STATE_HANDHELD);
1376 /* Intentional fallthrough */
1378 case STATE_HANDHELD:
1381 case STATE_SUSPENDED_INIT:
1382 if (di->susp_status.ac_suspended)
1383 abx500_chargalg_ac_en(di, false, 0, 0);
1384 if (di->susp_status.usb_suspended)
1385 abx500_chargalg_usb_en(di, false, 0, 0);
1386 abx500_chargalg_stop_safety_timer(di);
1387 abx500_chargalg_stop_maintenance_timer(di);
1388 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1389 di->maintenance_chg = false;
1390 abx500_chargalg_state_to(di, STATE_SUSPENDED);
1391 power_supply_changed(&di->chargalg_psy);
1392 /* Intentional fallthrough */
1394 case STATE_SUSPENDED:
1395 /* CHARGING is suspended */
1398 case STATE_BATT_REMOVED_INIT:
1399 abx500_chargalg_stop_charging(di);
1400 abx500_chargalg_state_to(di, STATE_BATT_REMOVED);
1401 /* Intentional fallthrough */
1403 case STATE_BATT_REMOVED:
1404 if (!di->events.batt_rem)
1405 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1408 case STATE_HW_TEMP_PROTECT_INIT:
1409 abx500_chargalg_stop_charging(di);
1410 abx500_chargalg_state_to(di, STATE_HW_TEMP_PROTECT);
1411 /* Intentional fallthrough */
1413 case STATE_HW_TEMP_PROTECT:
1414 if (!di->events.main_thermal_prot &&
1415 !di->events.usb_thermal_prot)
1416 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1419 case STATE_OVV_PROTECT_INIT:
1420 abx500_chargalg_stop_charging(di);
1421 abx500_chargalg_state_to(di, STATE_OVV_PROTECT);
1422 /* Intentional fallthrough */
1424 case STATE_OVV_PROTECT:
1425 if (!di->events.vbus_ovv &&
1426 !di->events.main_ovv &&
1427 !di->events.batt_ovv &&
1428 di->chg_info.usb_chg_ok &&
1429 di->chg_info.ac_chg_ok)
1430 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1433 case STATE_CHG_NOT_OK_INIT:
1434 abx500_chargalg_stop_charging(di);
1435 abx500_chargalg_state_to(di, STATE_CHG_NOT_OK);
1436 /* Intentional fallthrough */
1438 case STATE_CHG_NOT_OK:
1439 if (!di->events.mainextchnotok &&
1440 !di->events.usbchargernotok)
1441 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1444 case STATE_SAFETY_TIMER_EXPIRED_INIT:
1445 abx500_chargalg_stop_charging(di);
1446 abx500_chargalg_state_to(di, STATE_SAFETY_TIMER_EXPIRED);
1447 /* Intentional fallthrough */
1449 case STATE_SAFETY_TIMER_EXPIRED:
1450 /* We exit this state when charger is removed */
1453 case STATE_NORMAL_INIT:
1454 abx500_chargalg_start_charging(di,
1455 di->bm->bat_type[di->bm->batt_id].normal_vol_lvl,
1456 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
1457 abx500_chargalg_state_to(di, STATE_NORMAL);
1458 abx500_chargalg_start_safety_timer(di);
1459 abx500_chargalg_stop_maintenance_timer(di);
1460 init_maxim_chg_curr(di);
1461 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
1463 di->maintenance_chg = false;
1464 power_supply_changed(&di->chargalg_psy);
1469 handle_maxim_chg_curr(di);
1470 if (di->charge_status == POWER_SUPPLY_STATUS_FULL &&
1471 di->maintenance_chg) {
1472 if (di->bm->no_maintenance)
1473 abx500_chargalg_state_to(di,
1474 STATE_WAIT_FOR_RECHARGE_INIT);
1476 abx500_chargalg_state_to(di,
1477 STATE_MAINTENANCE_A_INIT);
1481 /* This state will be used when the maintenance state is disabled */
1482 case STATE_WAIT_FOR_RECHARGE_INIT:
1483 abx500_chargalg_hold_charging(di);
1484 abx500_chargalg_state_to(di, STATE_WAIT_FOR_RECHARGE);
1485 /* Intentional fallthrough */
1487 case STATE_WAIT_FOR_RECHARGE:
1488 if (di->batt_data.percent <=
1489 di->bm->bat_type[di->bm->batt_id].
1491 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1494 case STATE_MAINTENANCE_A_INIT:
1495 abx500_chargalg_stop_safety_timer(di);
1496 abx500_chargalg_start_maintenance_timer(di,
1498 di->bm->batt_id].maint_a_chg_timer_h);
1499 abx500_chargalg_start_charging(di,
1501 di->bm->batt_id].maint_a_vol_lvl,
1503 di->bm->batt_id].maint_a_cur_lvl);
1504 abx500_chargalg_state_to(di, STATE_MAINTENANCE_A);
1505 power_supply_changed(&di->chargalg_psy);
1506 /* Intentional fallthrough*/
1508 case STATE_MAINTENANCE_A:
1509 if (di->events.maintenance_timer_expired) {
1510 abx500_chargalg_stop_maintenance_timer(di);
1511 abx500_chargalg_state_to(di, STATE_MAINTENANCE_B_INIT);
1515 case STATE_MAINTENANCE_B_INIT:
1516 abx500_chargalg_start_maintenance_timer(di,
1518 di->bm->batt_id].maint_b_chg_timer_h);
1519 abx500_chargalg_start_charging(di,
1521 di->bm->batt_id].maint_b_vol_lvl,
1523 di->bm->batt_id].maint_b_cur_lvl);
1524 abx500_chargalg_state_to(di, STATE_MAINTENANCE_B);
1525 power_supply_changed(&di->chargalg_psy);
1526 /* Intentional fallthrough*/
1528 case STATE_MAINTENANCE_B:
1529 if (di->events.maintenance_timer_expired) {
1530 abx500_chargalg_stop_maintenance_timer(di);
1531 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1535 case STATE_TEMP_LOWHIGH_INIT:
1536 abx500_chargalg_start_charging(di,
1538 di->bm->batt_id].low_high_vol_lvl,
1540 di->bm->batt_id].low_high_cur_lvl);
1541 abx500_chargalg_stop_maintenance_timer(di);
1542 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
1543 abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH);
1544 power_supply_changed(&di->chargalg_psy);
1545 /* Intentional fallthrough */
1547 case STATE_TEMP_LOWHIGH:
1548 if (!di->events.btemp_lowhigh)
1549 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1552 case STATE_WD_EXPIRED_INIT:
1553 abx500_chargalg_stop_charging(di);
1554 abx500_chargalg_state_to(di, STATE_WD_EXPIRED);
1555 /* Intentional fallthrough */
1557 case STATE_WD_EXPIRED:
1558 if (!di->events.ac_wd_expired &&
1559 !di->events.usb_wd_expired)
1560 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1563 case STATE_TEMP_UNDEROVER_INIT:
1564 abx500_chargalg_stop_charging(di);
1565 abx500_chargalg_state_to(di, STATE_TEMP_UNDEROVER);
1566 /* Intentional fallthrough */
1568 case STATE_TEMP_UNDEROVER:
1569 if (!di->events.btemp_underover)
1570 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1574 /* Start charging directly if the new state is a charge state */
1575 if (di->charge_state == STATE_NORMAL_INIT ||
1576 di->charge_state == STATE_MAINTENANCE_A_INIT ||
1577 di->charge_state == STATE_MAINTENANCE_B_INIT)
1578 queue_work(di->chargalg_wq, &di->chargalg_work);
1582 * abx500_chargalg_periodic_work() - Periodic work for the algorithm
1583 * @work: pointer to the work_struct structure
1585 * Work queue function for the charging algorithm
1587 static void abx500_chargalg_periodic_work(struct work_struct *work)
1589 struct abx500_chargalg *di = container_of(work,
1590 struct abx500_chargalg, chargalg_periodic_work.work);
1592 abx500_chargalg_algorithm(di);
1595 * If a charger is connected then the battery has to be monitored
1596 * frequently, else the work can be delayed.
1598 if (di->chg_info.conn_chg)
1599 queue_delayed_work(di->chargalg_wq,
1600 &di->chargalg_periodic_work,
1601 di->bm->interval_charging * HZ);
1603 queue_delayed_work(di->chargalg_wq,
1604 &di->chargalg_periodic_work,
1605 di->bm->interval_not_charging * HZ);
1609 * abx500_chargalg_wd_work() - periodic work to kick the charger watchdog
1610 * @work: pointer to the work_struct structure
1612 * Work queue function for kicking the charger watchdog
1614 static void abx500_chargalg_wd_work(struct work_struct *work)
1617 struct abx500_chargalg *di = container_of(work,
1618 struct abx500_chargalg, chargalg_wd_work.work);
1620 dev_dbg(di->dev, "abx500_chargalg_wd_work\n");
1622 ret = abx500_chargalg_kick_watchdog(di);
1624 dev_err(di->dev, "failed to kick watchdog\n");
1626 queue_delayed_work(di->chargalg_wq,
1627 &di->chargalg_wd_work, CHG_WD_INTERVAL);
1631 * abx500_chargalg_work() - Work to run the charging algorithm instantly
1632 * @work: pointer to the work_struct structure
1634 * Work queue function for calling the charging algorithm
1636 static void abx500_chargalg_work(struct work_struct *work)
1638 struct abx500_chargalg *di = container_of(work,
1639 struct abx500_chargalg, chargalg_work);
1641 abx500_chargalg_algorithm(di);
1645 * abx500_chargalg_get_property() - get the chargalg properties
1646 * @psy: pointer to the power_supply structure
1647 * @psp: pointer to the power_supply_property structure
1648 * @val: pointer to the power_supply_propval union
1650 * This function gets called when an application tries to get the
1651 * chargalg properties by reading the sysfs files.
1652 * status: charging/discharging/full/unknown
1653 * health: health of the battery
1654 * Returns error code in case of failure else 0 on success
1656 static int abx500_chargalg_get_property(struct power_supply *psy,
1657 enum power_supply_property psp,
1658 union power_supply_propval *val)
1660 struct abx500_chargalg *di;
1662 di = to_abx500_chargalg_device_info(psy);
1665 case POWER_SUPPLY_PROP_STATUS:
1666 val->intval = di->charge_status;
1668 case POWER_SUPPLY_PROP_HEALTH:
1669 if (di->events.batt_ovv) {
1670 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
1671 } else if (di->events.btemp_underover) {
1672 if (di->batt_data.temp <= di->bm->temp_under)
1673 val->intval = POWER_SUPPLY_HEALTH_COLD;
1675 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
1676 } else if (di->charge_state == STATE_SAFETY_TIMER_EXPIRED ||
1677 di->charge_state == STATE_SAFETY_TIMER_EXPIRED_INIT) {
1678 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
1680 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1689 /* Exposure to the sysfs interface */
1692 * abx500_chargalg_sysfs_show() - sysfs show operations
1693 * @kobj: pointer to the struct kobject
1694 * @attr: pointer to the struct attribute
1695 * @buf: buffer that holds the parameter to send to userspace
1697 * Returns a buffer to be displayed in user space
1699 static ssize_t abx500_chargalg_sysfs_show(struct kobject *kobj,
1700 struct attribute *attr, char *buf)
1702 struct abx500_chargalg *di = container_of(kobj,
1703 struct abx500_chargalg, chargalg_kobject);
1705 return sprintf(buf, "%d\n",
1706 di->susp_status.ac_suspended &&
1707 di->susp_status.usb_suspended);
1711 * abx500_chargalg_sysfs_charger() - sysfs store operations
1712 * @kobj: pointer to the struct kobject
1713 * @attr: pointer to the struct attribute
1714 * @buf: buffer that holds the parameter passed from userspace
1715 * @length: length of the parameter passed
1717 * Returns length of the buffer(input taken from user space) on success
1718 * else error code on failure
1719 * The operation to be performed on passing the parameters from the user space.
1721 static ssize_t abx500_chargalg_sysfs_charger(struct kobject *kobj,
1722 struct attribute *attr, const char *buf, size_t length)
1724 struct abx500_chargalg *di = container_of(kobj,
1725 struct abx500_chargalg, chargalg_kobject);
1729 char entry = *attr->name;
1733 ret = strict_strtol(buf, 10, ¶m);
1740 /* Disable charging */
1741 di->susp_status.ac_suspended = true;
1742 di->susp_status.usb_suspended = true;
1743 di->susp_status.suspended_change = true;
1744 /* Trigger a state change */
1745 queue_work(di->chargalg_wq,
1746 &di->chargalg_work);
1749 /* Enable AC Charging */
1750 di->susp_status.ac_suspended = false;
1751 di->susp_status.suspended_change = true;
1752 /* Trigger a state change */
1753 queue_work(di->chargalg_wq,
1754 &di->chargalg_work);
1757 /* Enable USB charging */
1758 di->susp_status.usb_suspended = false;
1759 di->susp_status.suspended_change = true;
1760 /* Trigger a state change */
1761 queue_work(di->chargalg_wq,
1762 &di->chargalg_work);
1765 dev_info(di->dev, "Wrong input\n"
1766 "Enter 0. Disable AC/USB Charging\n"
1767 "1. Enable AC charging\n"
1768 "2. Enable USB Charging\n");
1775 static struct attribute abx500_chargalg_en_charger = \
1778 .mode = S_IRUGO | S_IWUSR,
1781 static struct attribute *abx500_chargalg_chg[] = {
1782 &abx500_chargalg_en_charger,
1786 static const struct sysfs_ops abx500_chargalg_sysfs_ops = {
1787 .show = abx500_chargalg_sysfs_show,
1788 .store = abx500_chargalg_sysfs_charger,
1791 static struct kobj_type abx500_chargalg_ktype = {
1792 .sysfs_ops = &abx500_chargalg_sysfs_ops,
1793 .default_attrs = abx500_chargalg_chg,
1797 * abx500_chargalg_sysfs_exit() - de-init of sysfs entry
1798 * @di: pointer to the struct abx500_chargalg
1800 * This function removes the entry in sysfs.
1802 static void abx500_chargalg_sysfs_exit(struct abx500_chargalg *di)
1804 kobject_del(&di->chargalg_kobject);
1808 * abx500_chargalg_sysfs_init() - init of sysfs entry
1809 * @di: pointer to the struct abx500_chargalg
1811 * This function adds an entry in sysfs.
1812 * Returns error code in case of failure else 0(on success)
1814 static int abx500_chargalg_sysfs_init(struct abx500_chargalg *di)
1818 ret = kobject_init_and_add(&di->chargalg_kobject,
1819 &abx500_chargalg_ktype,
1820 NULL, "abx500_chargalg");
1822 dev_err(di->dev, "failed to create sysfs entry\n");
1826 /* Exposure to the sysfs interface <<END>> */
1828 #if defined(CONFIG_PM)
1829 static int abx500_chargalg_resume(struct platform_device *pdev)
1831 struct abx500_chargalg *di = platform_get_drvdata(pdev);
1833 /* Kick charger watchdog if charging (any charger online) */
1834 if (di->chg_info.online_chg)
1835 queue_delayed_work(di->chargalg_wq, &di->chargalg_wd_work, 0);
1838 * Run the charging algorithm directly to be sure we don't
1841 queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
1846 static int abx500_chargalg_suspend(struct platform_device *pdev,
1849 struct abx500_chargalg *di = platform_get_drvdata(pdev);
1851 if (di->chg_info.online_chg)
1852 cancel_delayed_work_sync(&di->chargalg_wd_work);
1854 cancel_delayed_work_sync(&di->chargalg_periodic_work);
1859 #define abx500_chargalg_suspend NULL
1860 #define abx500_chargalg_resume NULL
1863 static int abx500_chargalg_remove(struct platform_device *pdev)
1865 struct abx500_chargalg *di = platform_get_drvdata(pdev);
1867 /* sysfs interface to enable/disbale charging from user space */
1868 abx500_chargalg_sysfs_exit(di);
1870 /* Delete the work queue */
1871 destroy_workqueue(di->chargalg_wq);
1873 flush_scheduled_work();
1874 power_supply_unregister(&di->chargalg_psy);
1875 platform_set_drvdata(pdev, NULL);
1880 static char *supply_interface[] = {
1884 static int abx500_chargalg_probe(struct platform_device *pdev)
1886 struct device_node *np = pdev->dev.of_node;
1887 struct abx500_bm_data *plat = pdev->dev.platform_data;
1888 struct abx500_chargalg *di;
1891 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
1893 dev_err(&pdev->dev, "%s no mem for ab8500_chargalg\n", __func__);
1898 dev_err(&pdev->dev, "no battery management data supplied\n");
1904 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
1906 dev_err(&pdev->dev, "failed to get battery information\n");
1911 /* get device struct and parent */
1912 di->dev = &pdev->dev;
1913 di->parent = dev_get_drvdata(pdev->dev.parent);
1915 /* chargalg supply */
1916 di->chargalg_psy.name = "abx500_chargalg";
1917 di->chargalg_psy.type = POWER_SUPPLY_TYPE_BATTERY;
1918 di->chargalg_psy.properties = abx500_chargalg_props;
1919 di->chargalg_psy.num_properties = ARRAY_SIZE(abx500_chargalg_props);
1920 di->chargalg_psy.get_property = abx500_chargalg_get_property;
1921 di->chargalg_psy.supplied_to = supply_interface;
1922 di->chargalg_psy.num_supplicants = ARRAY_SIZE(supply_interface),
1923 di->chargalg_psy.external_power_changed =
1924 abx500_chargalg_external_power_changed;
1926 /* Initilialize safety timer */
1927 init_timer(&di->safety_timer);
1928 di->safety_timer.function = abx500_chargalg_safety_timer_expired;
1929 di->safety_timer.data = (unsigned long) di;
1931 /* Initilialize maintenance timer */
1932 init_timer(&di->maintenance_timer);
1933 di->maintenance_timer.function =
1934 abx500_chargalg_maintenance_timer_expired;
1935 di->maintenance_timer.data = (unsigned long) di;
1937 /* Create a work queue for the chargalg */
1939 create_singlethread_workqueue("abx500_chargalg_wq");
1940 if (di->chargalg_wq == NULL) {
1941 dev_err(di->dev, "failed to create work queue\n");
1945 /* Init work for chargalg */
1946 INIT_DEFERRABLE_WORK(&di->chargalg_periodic_work,
1947 abx500_chargalg_periodic_work);
1948 INIT_DEFERRABLE_WORK(&di->chargalg_wd_work,
1949 abx500_chargalg_wd_work);
1951 /* Init work for chargalg */
1952 INIT_WORK(&di->chargalg_work, abx500_chargalg_work);
1954 /* To detect charger at startup */
1955 di->chg_info.prev_conn_chg = -1;
1957 /* Register chargalg power supply class */
1958 ret = power_supply_register(di->dev, &di->chargalg_psy);
1960 dev_err(di->dev, "failed to register chargalg psy\n");
1961 goto free_chargalg_wq;
1964 platform_set_drvdata(pdev, di);
1966 /* sysfs interface to enable/disable charging from user space */
1967 ret = abx500_chargalg_sysfs_init(di);
1969 dev_err(di->dev, "failed to create sysfs entry\n");
1973 /* Run the charging algorithm */
1974 queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
1976 dev_info(di->dev, "probe success\n");
1980 power_supply_unregister(&di->chargalg_psy);
1982 destroy_workqueue(di->chargalg_wq);
1986 static const struct of_device_id ab8500_chargalg_match[] = {
1987 { .compatible = "stericsson,ab8500-chargalg", },
1991 static struct platform_driver abx500_chargalg_driver = {
1992 .probe = abx500_chargalg_probe,
1993 .remove = abx500_chargalg_remove,
1994 .suspend = abx500_chargalg_suspend,
1995 .resume = abx500_chargalg_resume,
1997 .name = "ab8500-chargalg",
1998 .owner = THIS_MODULE,
1999 .of_match_table = ab8500_chargalg_match,
2003 static int __init abx500_chargalg_init(void)
2005 return platform_driver_register(&abx500_chargalg_driver);
2008 static void __exit abx500_chargalg_exit(void)
2010 platform_driver_unregister(&abx500_chargalg_driver);
2013 module_init(abx500_chargalg_init);
2014 module_exit(abx500_chargalg_exit);
2016 MODULE_LICENSE("GPL v2");
2017 MODULE_AUTHOR("Johan Palsson, Karl Komierowski");
2018 MODULE_ALIAS("platform:abx500-chargalg");
2019 MODULE_DESCRIPTION("abx500 battery charging algorithm");