Lines Matching refs:di

289 	struct abx500_chargalg *di = container_of(timer, struct abx500_chargalg,
291 dev_err(di->dev, "Safety timer expired\n");
292 di->events.safety_timer_expired = true;
295 queue_work(di->chargalg_wq, &di->chargalg_work);
312 struct abx500_chargalg *di = container_of(timer, struct abx500_chargalg,
315 dev_dbg(di->dev, "Maintenance timer expired\n");
316 di->events.maintenance_timer_expired = true;
319 queue_work(di->chargalg_wq, &di->chargalg_work);
326 * @di: pointer to the abx500_chargalg structure
330 static void abx500_chargalg_state_to(struct abx500_chargalg *di,
333 dev_dbg(di->dev,
335 di->charge_state == state ? "NO" : "YES",
336 di->charge_state,
337 states[di->charge_state],
341 di->charge_state = state;
344 static int abx500_chargalg_check_charger_enable(struct abx500_chargalg *di)
346 switch (di->charge_state) {
355 if (di->chg_info.charger_type & USB_CHG) {
356 return di->usb_chg->ops.check_enable(di->usb_chg,
357 di->bm->bat_type[di->bm->batt_id].normal_vol_lvl,
358 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
359 } else if ((di->chg_info.charger_type & AC_CHG) &&
360 !(di->ac_chg->external)) {
361 return di->ac_chg->ops.check_enable(di->ac_chg,
362 di->bm->bat_type[di->bm->batt_id].normal_vol_lvl,
363 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
370 * @di: pointer to the abx500_chargalg structure
375 static int abx500_chargalg_check_charger_connection(struct abx500_chargalg *di)
377 if (di->chg_info.conn_chg != di->chg_info.prev_conn_chg ||
378 di->susp_status.suspended_change) {
383 if ((di->chg_info.conn_chg & AC_CHG) &&
384 !di->susp_status.ac_suspended) {
385 dev_dbg(di->dev, "Charging source is AC\n");
386 if (di->chg_info.charger_type != AC_CHG) {
387 di->chg_info.charger_type = AC_CHG;
388 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
390 } else if ((di->chg_info.conn_chg & USB_CHG) &&
391 !di->susp_status.usb_suspended) {
392 dev_dbg(di->dev, "Charging source is USB\n");
393 di->chg_info.charger_type = USB_CHG;
394 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
395 } else if (di->chg_info.conn_chg &&
396 (di->susp_status.ac_suspended ||
397 di->susp_status.usb_suspended)) {
398 dev_dbg(di->dev, "Charging is suspended\n");
399 di->chg_info.charger_type = NO_CHG;
400 abx500_chargalg_state_to(di, STATE_SUSPENDED_INIT);
402 dev_dbg(di->dev, "Charging source is OFF\n");
403 di->chg_info.charger_type = NO_CHG;
404 abx500_chargalg_state_to(di, STATE_HANDHELD_INIT);
406 di->chg_info.prev_conn_chg = di->chg_info.conn_chg;
407 di->susp_status.suspended_change = false;
409 return di->chg_info.conn_chg;
415 * @di: pointer to the abx500_chargalg structure
421 (struct abx500_chargalg *di)
423 if (di->curr_status.curr_step_change)
424 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
425 di->curr_status.curr_step_change = false;
430 * @di: pointer to the abx500_chargalg structure
435 static void abx500_chargalg_start_safety_timer(struct abx500_chargalg *di)
440 switch (di->chg_info.charger_type) {
442 timer_expiration = di->bm->main_safety_tmr_h;
446 timer_expiration = di->bm->usb_safety_tmr_h;
450 dev_err(di->dev, "Unknown charger to charge from\n");
454 di->events.safety_timer_expired = false;
455 hrtimer_set_expires_range(&di->safety_timer,
458 hrtimer_start_expires(&di->safety_timer, HRTIMER_MODE_REL);
463 * @di: pointer to the abx500_chargalg structure
467 static void abx500_chargalg_stop_safety_timer(struct abx500_chargalg *di)
469 if (hrtimer_try_to_cancel(&di->safety_timer) >= 0)
470 di->events.safety_timer_expired = false;
475 * @di: pointer to the abx500_chargalg structure
482 static void abx500_chargalg_start_maintenance_timer(struct abx500_chargalg *di,
485 hrtimer_set_expires_range(&di->maintenance_timer,
488 di->events.maintenance_timer_expired = false;
489 hrtimer_start_expires(&di->maintenance_timer, HRTIMER_MODE_REL);
494 * @di: pointer to the abx500_chargalg structure
499 static void abx500_chargalg_stop_maintenance_timer(struct abx500_chargalg *di)
501 if (hrtimer_try_to_cancel(&di->maintenance_timer) >= 0)
502 di->events.maintenance_timer_expired = false;
507 * @di: pointer to the abx500_chargalg structure
512 static int abx500_chargalg_kick_watchdog(struct abx500_chargalg *di)
515 if (di->ac_chg && di->ac_chg->ops.kick_wd &&
516 di->chg_info.online_chg & AC_CHG) {
522 if (di->ac_chg->external &&
523 di->usb_chg && di->usb_chg->ops.kick_wd)
524 di->usb_chg->ops.kick_wd(di->usb_chg);
526 return di->ac_chg->ops.kick_wd(di->ac_chg);
528 else if (di->usb_chg && di->usb_chg->ops.kick_wd &&
529 di->chg_info.online_chg & USB_CHG)
530 return di->usb_chg->ops.kick_wd(di->usb_chg);
537 * @di: pointer to the abx500_chargalg structure
545 static int abx500_chargalg_ac_en(struct abx500_chargalg *di, int enable,
550 if (!di->ac_chg || !di->ac_chg->ops.enable)
554 if (di->ac_chg->max_out_volt)
555 vset = min(vset, di->ac_chg->max_out_volt);
556 if (di->ac_chg->max_out_curr)
557 iset = min(iset, di->ac_chg->max_out_curr);
559 di->chg_info.ac_iset = iset;
560 di->chg_info.ac_vset = vset;
563 if (enable && di->ac_chg->external &&
566 0, di->dev);
570 return di->ac_chg->ops.enable(di->ac_chg, enable, vset, iset);
575 * @di: pointer to the abx500_chargalg structure
583 static int abx500_chargalg_usb_en(struct abx500_chargalg *di, int enable,
586 if (!di->usb_chg || !di->usb_chg->ops.enable)
590 if (di->usb_chg->max_out_volt)
591 vset = min(vset, di->usb_chg->max_out_volt);
592 if (di->usb_chg->max_out_curr)
593 iset = min(iset, di->usb_chg->max_out_curr);
595 di->chg_info.usb_iset = iset;
596 di->chg_info.usb_vset = vset;
598 return di->usb_chg->ops.enable(di->usb_chg, enable, vset, iset);
603 * @di: pointer to the abx500_chargalg structure
609 static int abx500_chargalg_update_chg_curr(struct abx500_chargalg *di,
613 if (di->ac_chg && di->ac_chg->ops.update_curr &&
614 di->chg_info.charger_type & AC_CHG) {
619 if (di->ac_chg->max_out_curr)
620 iset = min(iset, di->ac_chg->max_out_curr);
622 di->chg_info.ac_iset = iset;
624 return di->ac_chg->ops.update_curr(di->ac_chg, iset);
625 } else if (di->usb_chg && di->usb_chg->ops.update_curr &&
626 di->chg_info.charger_type & USB_CHG) {
631 if (di->usb_chg->max_out_curr)
632 iset = min(iset, di->usb_chg->max_out_curr);
634 di->chg_info.usb_iset = iset;
636 return di->usb_chg->ops.update_curr(di->usb_chg, iset);
644 * @di: pointer to the abx500_chargalg structure
650 static void abx500_chargalg_stop_charging(struct abx500_chargalg *di)
652 abx500_chargalg_ac_en(di, false, 0, 0);
653 abx500_chargalg_usb_en(di, false, 0, 0);
654 abx500_chargalg_stop_safety_timer(di);
655 abx500_chargalg_stop_maintenance_timer(di);
656 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
657 di->maintenance_chg = false;
658 cancel_delayed_work(&di->chargalg_wd_work);
659 power_supply_changed(di->chargalg_psy);
664 * @di: pointer to the abx500_chargalg structure
670 static void abx500_chargalg_hold_charging(struct abx500_chargalg *di)
672 abx500_chargalg_ac_en(di, false, 0, 0);
673 abx500_chargalg_usb_en(di, false, 0, 0);
674 abx500_chargalg_stop_safety_timer(di);
675 abx500_chargalg_stop_maintenance_timer(di);
676 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
677 di->maintenance_chg = false;
678 cancel_delayed_work(&di->chargalg_wd_work);
679 power_supply_changed(di->chargalg_psy);
684 * @di: pointer to the abx500_chargalg structure
691 static void abx500_chargalg_start_charging(struct abx500_chargalg *di,
694 switch (di->chg_info.charger_type) {
696 dev_dbg(di->dev,
698 abx500_chargalg_usb_en(di, false, 0, 0);
699 abx500_chargalg_ac_en(di, true, vset, iset);
703 dev_dbg(di->dev,
705 abx500_chargalg_ac_en(di, false, 0, 0);
706 abx500_chargalg_usb_en(di, true, vset, iset);
710 dev_err(di->dev, "Unknown charger to charge from\n");
717 * @di: pointer to the abx500_chargalg structure
722 static void abx500_chargalg_check_temp(struct abx500_chargalg *di)
724 if (di->batt_data.temp > (di->bm->temp_low + di->t_hyst_norm) &&
725 di->batt_data.temp < (di->bm->temp_high - di->t_hyst_norm)) {
727 di->events.btemp_underover = false;
728 di->events.btemp_lowhigh = false;
729 di->t_hyst_norm = 0;
730 di->t_hyst_lowhigh = 0;
732 if (((di->batt_data.temp >= di->bm->temp_high) &&
733 (di->batt_data.temp <
734 (di->bm->temp_over - di->t_hyst_lowhigh))) ||
735 ((di->batt_data.temp >
736 (di->bm->temp_under + di->t_hyst_lowhigh)) &&
737 (di->batt_data.temp <= di->bm->temp_low))) {
739 di->events.btemp_underover = false;
740 di->events.btemp_lowhigh = true;
741 di->t_hyst_norm = di->bm->temp_hysteresis;
742 di->t_hyst_lowhigh = 0;
743 } else if (di->batt_data.temp <= di->bm->temp_under ||
744 di->batt_data.temp >= di->bm->temp_over) {
746 di->events.btemp_underover = true;
747 di->events.btemp_lowhigh = false;
748 di->t_hyst_norm = 0;
749 di->t_hyst_lowhigh = di->bm->temp_hysteresis;
752 dev_dbg(di->dev, "Within hysteresis limit temp: %d "
754 di->batt_data.temp, di->t_hyst_lowhigh,
755 di->t_hyst_norm);
762 * @di: pointer to the abx500_chargalg structure
766 static void abx500_chargalg_check_charger_voltage(struct abx500_chargalg *di)
768 if (di->chg_info.usb_volt > di->bm->chg_params->usb_volt_max)
769 di->chg_info.usb_chg_ok = false;
771 di->chg_info.usb_chg_ok = true;
773 if (di->chg_info.ac_volt > di->bm->chg_params->ac_volt_max)
774 di->chg_info.ac_chg_ok = false;
776 di->chg_info.ac_chg_ok = true;
782 * @di: pointer to the abx500_chargalg structure
788 static void abx500_chargalg_end_of_charge(struct abx500_chargalg *di)
790 if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING &&
791 di->charge_state == STATE_NORMAL &&
792 !di->maintenance_chg && (di->batt_data.volt >=
793 di->bm->bat_type[di->bm->batt_id].termination_vol ||
794 di->events.usb_cv_active || di->events.ac_cv_active) &&
795 di->batt_data.avg_curr <
796 di->bm->bat_type[di->bm->batt_id].termination_curr &&
797 di->batt_data.avg_curr > 0) {
798 if (++di->eoc_cnt >= EOC_COND_CNT) {
799 di->eoc_cnt = 0;
800 di->charge_status = POWER_SUPPLY_STATUS_FULL;
801 di->maintenance_chg = true;
802 dev_dbg(di->dev, "EOC reached!\n");
803 power_supply_changed(di->chargalg_psy);
805 dev_dbg(di->dev,
808 di->eoc_cnt,
812 di->eoc_cnt = 0;
816 static void init_maxim_chg_curr(struct abx500_chargalg *di)
818 di->ccm.original_iset =
819 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl;
820 di->ccm.current_iset =
821 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl;
822 di->ccm.test_delta_i = di->bm->maxi->charger_curr_step;
823 di->ccm.max_current = di->bm->maxi->chg_curr;
824 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
825 di->ccm.level = 0;
831 * @di pointer to the abx500_chargalg structure
837 static enum maxim_ret abx500_chargalg_chg_curr_maxim(struct abx500_chargalg *di)
841 if (!di->bm->maxi->ena_maxi)
844 delta_i = di->ccm.original_iset - di->batt_data.inst_curr;
846 if (di->events.vbus_collapsed) {
847 dev_dbg(di->dev, "Charger voltage has collapsed %d\n",
848 di->ccm.wait_cnt);
849 if (di->ccm.wait_cnt == 0) {
850 dev_dbg(di->dev, "lowering current\n");
851 di->ccm.wait_cnt++;
852 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
853 di->ccm.max_current =
854 di->ccm.current_iset - di->ccm.test_delta_i;
855 di->ccm.current_iset = di->ccm.max_current;
856 di->ccm.level--;
859 dev_dbg(di->dev, "waiting\n");
861 di->ccm.wait_cnt = (di->ccm.wait_cnt + 1) % 3;
866 di->ccm.wait_cnt = 0;
868 if ((di->batt_data.inst_curr > di->ccm.original_iset)) {
869 dev_dbg(di->dev, " Maximization Ibat (%dmA) too high"
871 di->batt_data.inst_curr, di->ccm.original_iset,
872 di->ccm.current_iset);
874 if (di->ccm.current_iset == di->ccm.original_iset)
877 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
878 di->ccm.current_iset = di->ccm.original_iset;
879 di->ccm.level = 0;
884 if (delta_i > di->ccm.test_delta_i &&
885 (di->ccm.current_iset + di->ccm.test_delta_i) <
886 di->ccm.max_current) {
887 if (di->ccm.condition_cnt-- == 0) {
889 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
890 di->ccm.current_iset += di->ccm.test_delta_i;
891 di->ccm.level++;
892 dev_dbg(di->dev, " Maximization needed, increase"
895 di->ccm.test_delta_i,
896 di->ccm.current_iset,
897 di->ccm.original_iset,
898 di->ccm.level);
904 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
909 static void handle_maxim_chg_curr(struct abx500_chargalg *di)
914 ret = abx500_chargalg_chg_curr_maxim(di);
917 result = abx500_chargalg_update_chg_curr(di,
918 di->ccm.current_iset);
920 dev_err(di->dev, "failed to set chg curr\n");
923 result = abx500_chargalg_update_chg_curr(di,
924 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
926 dev_err(di->dev, "failed to set chg curr\n");
941 struct abx500_chargalg *di;
947 di = power_supply_get_drvdata(psy);
959 di->batt_data.percent = ret.intval;
971 if (!di->ac_chg &&
973 di->ac_chg = psy_to_ux500_charger(ext);
974 else if (!di->usb_chg &&
976 di->usb_chg = psy_to_ux500_charger(ext);
986 di->events.batt_rem = false;
989 di->events.batt_rem = true;
994 (di->chg_info.conn_chg & AC_CHG)) {
995 di->chg_info.prev_conn_chg =
996 di->chg_info.conn_chg;
997 di->chg_info.conn_chg &= ~AC_CHG;
1001 !(di->chg_info.conn_chg & AC_CHG)) {
1002 di->chg_info.prev_conn_chg =
1003 di->chg_info.conn_chg;
1004 di->chg_info.conn_chg |= AC_CHG;
1010 (di->chg_info.conn_chg & USB_CHG)) {
1011 di->chg_info.prev_conn_chg =
1012 di->chg_info.conn_chg;
1013 di->chg_info.conn_chg &= ~USB_CHG;
1017 !(di->chg_info.conn_chg & USB_CHG)) {
1018 di->chg_info.prev_conn_chg =
1019 di->chg_info.conn_chg;
1020 di->chg_info.conn_chg |= USB_CHG;
1035 (di->chg_info.online_chg & AC_CHG)) {
1036 di->chg_info.prev_online_chg =
1037 di->chg_info.online_chg;
1038 di->chg_info.online_chg &= ~AC_CHG;
1042 !(di->chg_info.online_chg & AC_CHG)) {
1043 di->chg_info.prev_online_chg =
1044 di->chg_info.online_chg;
1045 di->chg_info.online_chg |= AC_CHG;
1046 queue_delayed_work(di->chargalg_wq,
1047 &di->chargalg_wd_work, 0);
1053 (di->chg_info.online_chg & USB_CHG)) {
1054 di->chg_info.prev_online_chg =
1055 di->chg_info.online_chg;
1056 di->chg_info.online_chg &= ~USB_CHG;
1060 !(di->chg_info.online_chg & USB_CHG)) {
1061 di->chg_info.prev_online_chg =
1062 di->chg_info.online_chg;
1063 di->chg_info.online_chg |= USB_CHG;
1064 queue_delayed_work(di->chargalg_wq,
1065 &di->chargalg_wd_work, 0);
1080 di->events.mainextchnotok = true;
1081 di->events.main_thermal_prot = false;
1082 di->events.main_ovv = false;
1083 di->events.ac_wd_expired = false;
1086 di->events.ac_wd_expired = true;
1087 di->events.mainextchnotok = false;
1088 di->events.main_ovv = false;
1089 di->events.main_thermal_prot = false;
1093 di->events.main_thermal_prot = true;
1094 di->events.mainextchnotok = false;
1095 di->events.main_ovv = false;
1096 di->events.ac_wd_expired = false;
1099 di->events.main_ovv = true;
1100 di->events.mainextchnotok = false;
1101 di->events.main_thermal_prot = false;
1102 di->events.ac_wd_expired = false;
1105 di->events.main_thermal_prot = false;
1106 di->events.mainextchnotok = false;
1107 di->events.main_ovv = false;
1108 di->events.ac_wd_expired = false;
1118 di->events.usbchargernotok = true;
1119 di->events.usb_thermal_prot = false;
1120 di->events.vbus_ovv = false;
1121 di->events.usb_wd_expired = false;
1124 di->events.usb_wd_expired = true;
1125 di->events.usbchargernotok = false;
1126 di->events.usb_thermal_prot = false;
1127 di->events.vbus_ovv = false;
1131 di->events.usb_thermal_prot = true;
1132 di->events.usbchargernotok = false;
1133 di->events.vbus_ovv = false;
1134 di->events.usb_wd_expired = false;
1137 di->events.vbus_ovv = true;
1138 di->events.usbchargernotok = false;
1139 di->events.usb_thermal_prot = false;
1140 di->events.usb_wd_expired = false;
1143 di->events.usbchargernotok = false;
1144 di->events.usb_thermal_prot = false;
1145 di->events.vbus_ovv = false;
1146 di->events.usb_wd_expired = false;
1159 di->batt_data.volt = ret.intval / 1000;
1162 di->chg_info.ac_volt = ret.intval / 1000;
1165 di->chg_info.usb_volt = ret.intval / 1000;
1178 di->events.ac_cv_active = true;
1180 di->events.ac_cv_active = false;
1187 di->events.usb_cv_active = true;
1189 di->events.usb_cv_active = false;
1201 di->events.batt_unknown = false;
1203 di->events.batt_unknown = true;
1212 di->batt_data.temp = ret.intval / 10;
1218 di->chg_info.ac_curr =
1222 di->chg_info.usb_curr =
1226 di->batt_data.inst_curr = ret.intval / 1000;
1236 di->batt_data.avg_curr = ret.intval / 1000;
1240 di->events.vbus_collapsed = true;
1242 di->events.vbus_collapsed = false;
1250 di->batt_data.percent = ret.intval;
1270 struct abx500_chargalg *di = power_supply_get_drvdata(psy);
1276 queue_work(di->chargalg_wq, &di->chargalg_work);
1281 * @di: pointer to the abx500_chargalg structure
1287 static void abx500_chargalg_algorithm(struct abx500_chargalg *di)
1295 di->chargalg_psy, abx500_chargalg_get_ext_psy_data);
1297 abx500_chargalg_end_of_charge(di);
1298 abx500_chargalg_check_temp(di);
1299 abx500_chargalg_check_charger_voltage(di);
1301 charger_status = abx500_chargalg_check_charger_connection(di);
1302 abx500_chargalg_check_current_step_status(di);
1304 if (is_ab8500(di->parent)) {
1305 ret = abx500_chargalg_check_charger_enable(di);
1307 dev_err(di->dev, "Checking charger is enabled error"
1317 (di->events.batt_unknown && !di->bm->chg_unknown_bat)) {
1318 if (di->charge_state != STATE_HANDHELD) {
1319 di->events.safety_timer_expired = false;
1320 abx500_chargalg_state_to(di, STATE_HANDHELD_INIT);
1325 else if (di->charge_state == STATE_SUSPENDED_INIT ||
1326 di->charge_state == STATE_SUSPENDED) {
1331 else if (di->events.safety_timer_expired) {
1332 if (di->charge_state != STATE_SAFETY_TIMER_EXPIRED)
1333 abx500_chargalg_state_to(di,
1342 else if (di->events.batt_rem) {
1343 if (di->charge_state != STATE_BATT_REMOVED)
1344 abx500_chargalg_state_to(di, STATE_BATT_REMOVED_INIT);
1347 else if (di->events.mainextchnotok || di->events.usbchargernotok) {
1352 if (di->charge_state != STATE_CHG_NOT_OK &&
1353 !di->events.vbus_collapsed)
1354 abx500_chargalg_state_to(di, STATE_CHG_NOT_OK_INIT);
1357 else if (di->events.vbus_ovv ||
1358 di->events.main_ovv ||
1359 di->events.batt_ovv ||
1360 !di->chg_info.usb_chg_ok ||
1361 !di->chg_info.ac_chg_ok) {
1362 if (di->charge_state != STATE_OVV_PROTECT)
1363 abx500_chargalg_state_to(di, STATE_OVV_PROTECT_INIT);
1366 else if (di->events.main_thermal_prot ||
1367 di->events.usb_thermal_prot) {
1368 if (di->charge_state != STATE_HW_TEMP_PROTECT)
1369 abx500_chargalg_state_to(di,
1373 else if (di->events.btemp_underover) {
1374 if (di->charge_state != STATE_TEMP_UNDEROVER)
1375 abx500_chargalg_state_to(di,
1379 else if (di->events.ac_wd_expired ||
1380 di->events.usb_wd_expired) {
1381 if (di->charge_state != STATE_WD_EXPIRED)
1382 abx500_chargalg_state_to(di, STATE_WD_EXPIRED_INIT);
1385 else if (di->events.btemp_lowhigh) {
1386 if (di->charge_state != STATE_TEMP_LOWHIGH)
1387 abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH_INIT);
1390 dev_dbg(di->dev,
1395 di->batt_data.volt,
1396 di->batt_data.avg_curr,
1397 di->batt_data.inst_curr,
1398 di->batt_data.temp,
1399 di->batt_data.percent,
1400 di->maintenance_chg,
1401 states[di->charge_state],
1402 di->chg_info.charger_type,
1403 di->charge_status,
1404 di->chg_info.conn_chg & AC_CHG,
1405 di->chg_info.conn_chg & USB_CHG,
1406 di->chg_info.online_chg & AC_CHG,
1407 di->chg_info.online_chg & USB_CHG,
1408 di->events.ac_cv_active,
1409 di->events.usb_cv_active,
1410 di->chg_info.ac_curr,
1411 di->chg_info.usb_curr,
1412 di->chg_info.ac_vset,
1413 di->chg_info.ac_iset,
1414 di->chg_info.usb_vset,
1415 di->chg_info.usb_iset);
1417 switch (di->charge_state) {
1419 abx500_chargalg_stop_charging(di);
1420 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
1421 abx500_chargalg_state_to(di, STATE_HANDHELD);
1428 if (di->susp_status.ac_suspended)
1429 abx500_chargalg_ac_en(di, false, 0, 0);
1430 if (di->susp_status.usb_suspended)
1431 abx500_chargalg_usb_en(di, false, 0, 0);
1432 abx500_chargalg_stop_safety_timer(di);
1433 abx500_chargalg_stop_maintenance_timer(di);
1434 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1435 di->maintenance_chg = false;
1436 abx500_chargalg_state_to(di, STATE_SUSPENDED);
1437 power_supply_changed(di->chargalg_psy);
1445 abx500_chargalg_stop_charging(di);
1446 abx500_chargalg_state_to(di, STATE_BATT_REMOVED);
1450 if (!di->events.batt_rem)
1451 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1455 abx500_chargalg_stop_charging(di);
1456 abx500_chargalg_state_to(di, STATE_HW_TEMP_PROTECT);
1460 if (!di->events.main_thermal_prot &&
1461 !di->events.usb_thermal_prot)
1462 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1466 abx500_chargalg_stop_charging(di);
1467 abx500_chargalg_state_to(di, STATE_OVV_PROTECT);
1471 if (!di->events.vbus_ovv &&
1472 !di->events.main_ovv &&
1473 !di->events.batt_ovv &&
1474 di->chg_info.usb_chg_ok &&
1475 di->chg_info.ac_chg_ok)
1476 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1480 abx500_chargalg_stop_charging(di);
1481 abx500_chargalg_state_to(di, STATE_CHG_NOT_OK);
1485 if (!di->events.mainextchnotok &&
1486 !di->events.usbchargernotok)
1487 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1491 abx500_chargalg_stop_charging(di);
1492 abx500_chargalg_state_to(di, STATE_SAFETY_TIMER_EXPIRED);
1500 if (di->curr_status.curr_step == CHARGALG_CURR_STEP_LOW)
1501 abx500_chargalg_stop_charging(di);
1503 curr_step_lvl = di->bm->bat_type[
1504 di->bm->batt_id].normal_cur_lvl
1505 * di->curr_status.curr_step
1507 abx500_chargalg_start_charging(di,
1508 di->bm->bat_type[di->bm->batt_id]
1512 abx500_chargalg_state_to(di, STATE_NORMAL);
1513 abx500_chargalg_start_safety_timer(di);
1514 abx500_chargalg_stop_maintenance_timer(di);
1515 init_maxim_chg_curr(di);
1516 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
1517 di->eoc_cnt = 0;
1518 di->maintenance_chg = false;
1519 power_supply_changed(di->chargalg_psy);
1524 handle_maxim_chg_curr(di);
1525 if (di->charge_status == POWER_SUPPLY_STATUS_FULL &&
1526 di->maintenance_chg) {
1527 if (di->bm->no_maintenance)
1528 abx500_chargalg_state_to(di,
1531 abx500_chargalg_state_to(di,
1538 abx500_chargalg_hold_charging(di);
1539 abx500_chargalg_state_to(di, STATE_WAIT_FOR_RECHARGE);
1543 if (di->batt_data.percent <=
1544 di->bm->bat_type[di->bm->batt_id].
1546 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1550 abx500_chargalg_stop_safety_timer(di);
1551 abx500_chargalg_start_maintenance_timer(di,
1552 di->bm->bat_type[
1553 di->bm->batt_id].maint_a_chg_timer_h);
1554 abx500_chargalg_start_charging(di,
1555 di->bm->bat_type[
1556 di->bm->batt_id].maint_a_vol_lvl,
1557 di->bm->bat_type[
1558 di->bm->batt_id].maint_a_cur_lvl);
1559 abx500_chargalg_state_to(di, STATE_MAINTENANCE_A);
1560 power_supply_changed(di->chargalg_psy);
1564 if (di->events.maintenance_timer_expired) {
1565 abx500_chargalg_stop_maintenance_timer(di);
1566 abx500_chargalg_state_to(di, STATE_MAINTENANCE_B_INIT);
1571 abx500_chargalg_start_maintenance_timer(di,
1572 di->bm->bat_type[
1573 di->bm->batt_id].maint_b_chg_timer_h);
1574 abx500_chargalg_start_charging(di,
1575 di->bm->bat_type[
1576 di->bm->batt_id].maint_b_vol_lvl,
1577 di->bm->bat_type[
1578 di->bm->batt_id].maint_b_cur_lvl);
1579 abx500_chargalg_state_to(di, STATE_MAINTENANCE_B);
1580 power_supply_changed(di->chargalg_psy);
1584 if (di->events.maintenance_timer_expired) {
1585 abx500_chargalg_stop_maintenance_timer(di);
1586 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1591 abx500_chargalg_start_charging(di,
1592 di->bm->bat_type[
1593 di->bm->batt_id].low_high_vol_lvl,
1594 di->bm->bat_type[
1595 di->bm->batt_id].low_high_cur_lvl);
1596 abx500_chargalg_stop_maintenance_timer(di);
1597 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
1598 abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH);
1599 power_supply_changed(di->chargalg_psy);
1603 if (!di->events.btemp_lowhigh)
1604 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1608 abx500_chargalg_stop_charging(di);
1609 abx500_chargalg_state_to(di, STATE_WD_EXPIRED);
1613 if (!di->events.ac_wd_expired &&
1614 !di->events.usb_wd_expired)
1615 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1619 abx500_chargalg_stop_charging(di);
1620 abx500_chargalg_state_to(di, STATE_TEMP_UNDEROVER);
1624 if (!di->events.btemp_underover)
1625 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1630 if (di->charge_state == STATE_NORMAL_INIT ||
1631 di->charge_state == STATE_MAINTENANCE_A_INIT ||
1632 di->charge_state == STATE_MAINTENANCE_B_INIT)
1633 queue_work(di->chargalg_wq, &di->chargalg_work);
1644 struct abx500_chargalg *di = container_of(work,
1647 abx500_chargalg_algorithm(di);
1653 if (di->chg_info.conn_chg)
1654 queue_delayed_work(di->chargalg_wq,
1655 &di->chargalg_periodic_work,
1656 di->bm->interval_charging * HZ);
1658 queue_delayed_work(di->chargalg_wq,
1659 &di->chargalg_periodic_work,
1660 di->bm->interval_not_charging * HZ);
1672 struct abx500_chargalg *di = container_of(work,
1675 dev_dbg(di->dev, "abx500_chargalg_wd_work\n");
1677 ret = abx500_chargalg_kick_watchdog(di);
1679 dev_err(di->dev, "failed to kick watchdog\n");
1681 queue_delayed_work(di->chargalg_wq,
1682 &di->chargalg_wd_work, CHG_WD_INTERVAL);
1693 struct abx500_chargalg *di = container_of(work,
1696 abx500_chargalg_algorithm(di);
1715 struct abx500_chargalg *di = power_supply_get_drvdata(psy);
1719 val->intval = di->charge_status;
1722 if (di->events.batt_ovv) {
1724 } else if (di->events.btemp_underover) {
1725 if (di->batt_data.temp <= di->bm->temp_under)
1729 } else if (di->charge_state == STATE_SAFETY_TIMER_EXPIRED ||
1730 di->charge_state == STATE_SAFETY_TIMER_EXPIRED_INIT) {
1744 static ssize_t abx500_chargalg_curr_step_show(struct abx500_chargalg *di,
1747 return sprintf(buf, "%d\n", di->curr_status.curr_step);
1750 static ssize_t abx500_chargalg_curr_step_store(struct abx500_chargalg *di,
1760 di->curr_status.curr_step = param;
1761 if (di->curr_status.curr_step >= CHARGALG_CURR_STEP_LOW &&
1762 di->curr_status.curr_step <= CHARGALG_CURR_STEP_HIGH) {
1763 di->curr_status.curr_step_change = true;
1764 queue_work(di->chargalg_wq, &di->chargalg_work);
1766 dev_info(di->dev, "Wrong current step\n"
1775 static ssize_t abx500_chargalg_en_show(struct abx500_chargalg *di,
1779 di->susp_status.ac_suspended &&
1780 di->susp_status.usb_suspended);
1783 static ssize_t abx500_chargalg_en_store(struct abx500_chargalg *di,
1798 di->susp_status.ac_suspended = true;
1799 di->susp_status.usb_suspended = true;
1800 di->susp_status.suspended_change = true;
1802 queue_work(di->chargalg_wq,
1803 &di->chargalg_work);
1807 di->susp_status.ac_suspended = false;
1808 di->susp_status.suspended_change = true;
1810 queue_work(di->chargalg_wq,
1811 &di->chargalg_work);
1815 di->susp_status.usb_suspended = false;
1816 di->susp_status.suspended_change = true;
1818 queue_work(di->chargalg_wq,
1819 &di->chargalg_work);
1822 dev_info(di->dev, "Wrong input\n"
1844 struct abx500_chargalg *di = container_of(kobj,
1850 return entry->show(di, buf);
1859 struct abx500_chargalg *di = container_of(kobj,
1865 return entry->store(di, buf, length);
1886 * @di: pointer to the struct abx500_chargalg
1890 static void abx500_chargalg_sysfs_exit(struct abx500_chargalg *di)
1892 kobject_del(&di->chargalg_kobject);
1897 * @di: pointer to the struct abx500_chargalg
1902 static int abx500_chargalg_sysfs_init(struct abx500_chargalg *di)
1906 ret = kobject_init_and_add(&di->chargalg_kobject,
1910 dev_err(di->dev, "failed to create sysfs entry\n");
1919 struct abx500_chargalg *di = platform_get_drvdata(pdev);
1922 if (di->chg_info.online_chg)
1923 queue_delayed_work(di->chargalg_wq, &di->chargalg_wd_work, 0);
1929 queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
1937 struct abx500_chargalg *di = platform_get_drvdata(pdev);
1939 if (di->chg_info.online_chg)
1940 cancel_delayed_work_sync(&di->chargalg_wd_work);
1942 cancel_delayed_work_sync(&di->chargalg_periodic_work);
1953 struct abx500_chargalg *di = platform_get_drvdata(pdev);
1956 abx500_chargalg_sysfs_exit(di);
1958 hrtimer_cancel(&di->safety_timer);
1959 hrtimer_cancel(&di->maintenance_timer);
1961 cancel_delayed_work_sync(&di->chargalg_periodic_work);
1962 cancel_delayed_work_sync(&di->chargalg_wd_work);
1963 cancel_work_sync(&di->chargalg_work);
1966 destroy_workqueue(di->chargalg_wq);
1968 power_supply_unregister(di->chargalg_psy);
1991 struct abx500_chargalg *di;
1994 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
1995 if (!di) {
2004 di->bm = plat;
2007 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
2015 di->dev = &pdev->dev;
2016 di->parent = dev_get_drvdata(pdev->dev.parent);
2020 psy_cfg.drv_data = di;
2023 hrtimer_init(&di->safety_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
2024 di->safety_timer.function = abx500_chargalg_safety_timer_expired;
2027 hrtimer_init(&di->maintenance_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
2028 di->maintenance_timer.function =
2032 di->chargalg_wq = alloc_ordered_workqueue("abx500_chargalg_wq",
2034 if (di->chargalg_wq == NULL) {
2035 dev_err(di->dev, "failed to create work queue\n");
2040 INIT_DEFERRABLE_WORK(&di->chargalg_periodic_work,
2042 INIT_DEFERRABLE_WORK(&di->chargalg_wd_work,
2046 INIT_WORK(&di->chargalg_work, abx500_chargalg_work);
2049 di->chg_info.prev_conn_chg = -1;
2052 di->chargalg_psy = power_supply_register(di->dev, &abx500_chargalg_desc,
2054 if (IS_ERR(di->chargalg_psy)) {
2055 dev_err(di->dev, "failed to register chargalg psy\n");
2056 ret = PTR_ERR(di->chargalg_psy);
2060 platform_set_drvdata(pdev, di);
2063 ret = abx500_chargalg_sysfs_init(di);
2065 dev_err(di->dev, "failed to create sysfs entry\n");
2068 di->curr_status.curr_step = CHARGALG_CURR_STEP_HIGH;
2071 queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
2073 dev_info(di->dev, "probe success\n");
2077 power_supply_unregister(di->chargalg_psy);
2079 destroy_workqueue(di->chargalg_wq);