Lines Matching +full:li +full:- +full:ion

1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson AB 2012
5 * Main and Back-up battery management driver.
7 * Note: Backup battery management is required in case of Li-Ion battery and not
37 #include <linux/fixp-arith.h>
39 #include "ab8500-bm.h"
49 /* Currents higher than -500mA (dissipating) will make compensation unstable */
50 #define IGNORE_VBAT_HIGHCUR -500000
63 * struct ab8500_fg_interrupts - ab8500 fg interrupts
153 * struct ab8500_fg - ab8500 FG device information
241 * ab8500_fg_get() - returns a reference to the primary AB8500 fuel gauge
345 return (u8) i - 1; in ab8500_volt_to_regval()
349 return (u8) ARRAY_SIZE(ab8500_fg_lowbat_voltage_map) - 1; in ab8500_volt_to_regval()
353 * ab8500_fg_is_low_curr() - Low or high current mode
364 if (curr_ua > -di->bm->fg_params->high_curr_threshold_ua) in ab8500_fg_is_low_curr()
371 * ab8500_fg_add_cap_sample() - Add capacity to average filter
381 struct ab8500_fg_avg_cap *avg = &di->avg_cap; in ab8500_fg_add_cap_sample()
384 avg->sum += sample - avg->samples[avg->pos]; in ab8500_fg_add_cap_sample()
385 avg->samples[avg->pos] = sample; in ab8500_fg_add_cap_sample()
386 avg->time_stamps[avg->pos] = now; in ab8500_fg_add_cap_sample()
387 avg->pos++; in ab8500_fg_add_cap_sample()
389 if (avg->pos == NBR_AVG_SAMPLES) in ab8500_fg_add_cap_sample()
390 avg->pos = 0; in ab8500_fg_add_cap_sample()
392 if (avg->nbr_samples < NBR_AVG_SAMPLES) in ab8500_fg_add_cap_sample()
393 avg->nbr_samples++; in ab8500_fg_add_cap_sample()
399 } while (now - VALID_CAPACITY_SEC > avg->time_stamps[avg->pos]); in ab8500_fg_add_cap_sample()
401 avg->avg = avg->sum / avg->nbr_samples; in ab8500_fg_add_cap_sample()
403 return avg->avg; in ab8500_fg_add_cap_sample()
407 * ab8500_fg_clear_cap_samples() - Clear average filter
415 struct ab8500_fg_avg_cap *avg = &di->avg_cap; in ab8500_fg_clear_cap_samples()
417 avg->pos = 0; in ab8500_fg_clear_cap_samples()
418 avg->nbr_samples = 0; in ab8500_fg_clear_cap_samples()
419 avg->sum = 0; in ab8500_fg_clear_cap_samples()
420 avg->avg = 0; in ab8500_fg_clear_cap_samples()
423 avg->samples[i] = 0; in ab8500_fg_clear_cap_samples()
424 avg->time_stamps[i] = 0; in ab8500_fg_clear_cap_samples()
429 * ab8500_fg_fill_cap_sample() - Fill average filter
439 struct ab8500_fg_avg_cap *avg = &di->avg_cap; in ab8500_fg_fill_cap_sample()
444 avg->samples[i] = sample; in ab8500_fg_fill_cap_sample()
445 avg->time_stamps[i] = now; in ab8500_fg_fill_cap_sample()
448 avg->pos = 0; in ab8500_fg_fill_cap_sample()
449 avg->nbr_samples = NBR_AVG_SAMPLES; in ab8500_fg_fill_cap_sample()
450 avg->sum = sample * NBR_AVG_SAMPLES; in ab8500_fg_fill_cap_sample()
451 avg->avg = sample; in ab8500_fg_fill_cap_sample()
455 * ab8500_fg_coulomb_counter() - enable coulomb counter
465 mutex_lock(&di->cc_lock); in ab8500_fg_coulomb_counter()
469 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, in ab8500_fg_coulomb_counter()
475 ret = abx500_set_register_interruptible(di->dev, in ab8500_fg_coulomb_counter()
477 di->fg_samples); in ab8500_fg_coulomb_counter()
482 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, in ab8500_fg_coulomb_counter()
488 di->flags.fg_enabled = true; in ab8500_fg_coulomb_counter()
491 ret = abx500_mask_and_set_register_interruptible(di->dev, in ab8500_fg_coulomb_counter()
497 ret = abx500_set_register_interruptible(di->dev, in ab8500_fg_coulomb_counter()
503 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, in ab8500_fg_coulomb_counter()
508 di->flags.fg_enabled = false; in ab8500_fg_coulomb_counter()
511 dev_dbg(di->dev, " CC enabled: %d Samples: %d\n", in ab8500_fg_coulomb_counter()
512 enable, di->fg_samples); in ab8500_fg_coulomb_counter()
514 mutex_unlock(&di->cc_lock); in ab8500_fg_coulomb_counter()
518 dev_err(di->dev, "%s Enabling coulomb counter failed\n", __func__); in ab8500_fg_coulomb_counter()
519 mutex_unlock(&di->cc_lock); in ab8500_fg_coulomb_counter()
524 * ab8500_fg_inst_curr_start() - start battery instantaneous current
536 mutex_lock(&di->cc_lock); in ab8500_fg_inst_curr_start()
538 di->nbr_cceoc_irq_cnt = 0; in ab8500_fg_inst_curr_start()
539 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC, in ab8500_fg_inst_curr_start()
545 dev_dbg(di->dev, "%s Enable FG\n", __func__); in ab8500_fg_inst_curr_start()
546 di->turn_off_fg = true; in ab8500_fg_inst_curr_start()
549 ret = abx500_set_register_interruptible(di->dev, in ab8500_fg_inst_curr_start()
556 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, in ab8500_fg_inst_curr_start()
562 di->turn_off_fg = false; in ab8500_fg_inst_curr_start()
566 reinit_completion(&di->ab8500_fg_started); in ab8500_fg_inst_curr_start()
567 reinit_completion(&di->ab8500_fg_complete); in ab8500_fg_inst_curr_start()
568 enable_irq(di->irq); in ab8500_fg_inst_curr_start()
573 mutex_unlock(&di->cc_lock); in ab8500_fg_inst_curr_start()
578 * ab8500_fg_inst_curr_started() - check if fg conversion has started
585 return completion_done(&di->ab8500_fg_started); in ab8500_fg_inst_curr_started()
589 * ab8500_fg_inst_curr_done() - check if fg conversion is done
596 return completion_done(&di->ab8500_fg_complete); in ab8500_fg_inst_curr_done()
600 * ab8500_fg_inst_curr_finalize() - battery instantaneous current
615 if (!completion_done(&di->ab8500_fg_complete)) { in ab8500_fg_inst_curr_finalize()
617 &di->ab8500_fg_complete, in ab8500_fg_inst_curr_finalize()
619 dev_dbg(di->dev, "Finalize time: %d ms\n", in ab8500_fg_inst_curr_finalize()
620 jiffies_to_msecs(INS_CURR_TIMEOUT - timeout)); in ab8500_fg_inst_curr_finalize()
622 ret = -ETIME; in ab8500_fg_inst_curr_finalize()
623 disable_irq(di->irq); in ab8500_fg_inst_curr_finalize()
624 di->nbr_cceoc_irq_cnt = 0; in ab8500_fg_inst_curr_finalize()
625 dev_err(di->dev, "completion timed out [%d]\n", in ab8500_fg_inst_curr_finalize()
631 disable_irq(di->irq); in ab8500_fg_inst_curr_finalize()
632 di->nbr_cceoc_irq_cnt = 0; in ab8500_fg_inst_curr_finalize()
634 ret = abx500_mask_and_set_register_interruptible(di->dev, in ab8500_fg_inst_curr_finalize()
642 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE, in ab8500_fg_inst_curr_finalize()
647 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE, in ab8500_fg_inst_curr_finalize()
670 val = (val * QLSB_NANO_AMP_HOURS_X10 * 36 * 4) / di->bm->fg_res; in ab8500_fg_inst_curr_finalize()
672 if (di->turn_off_fg) { in ab8500_fg_inst_curr_finalize()
673 dev_dbg(di->dev, "%s Disable FG\n", __func__); in ab8500_fg_inst_curr_finalize()
676 ret = abx500_set_register_interruptible(di->dev, in ab8500_fg_inst_curr_finalize()
682 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, in ab8500_fg_inst_curr_finalize()
687 mutex_unlock(&di->cc_lock); in ab8500_fg_inst_curr_finalize()
692 mutex_unlock(&di->cc_lock); in ab8500_fg_inst_curr_finalize()
697 * ab8500_fg_inst_curr_blocking() - battery instantaneous current
711 dev_err(di->dev, "Failed to initialize fg_inst\n"); in ab8500_fg_inst_curr_blocking()
716 if (!completion_done(&di->ab8500_fg_started)) { in ab8500_fg_inst_curr_blocking()
718 &di->ab8500_fg_started, in ab8500_fg_inst_curr_blocking()
720 dev_dbg(di->dev, "Start time: %d ms\n", in ab8500_fg_inst_curr_blocking()
721 jiffies_to_msecs(INS_CURR_TIMEOUT - timeout)); in ab8500_fg_inst_curr_blocking()
723 ret = -ETIME; in ab8500_fg_inst_curr_blocking()
724 dev_err(di->dev, "completion timed out [%d]\n", in ab8500_fg_inst_curr_blocking()
732 dev_err(di->dev, "Failed to finalize fg_inst\n"); in ab8500_fg_inst_curr_blocking()
736 dev_dbg(di->dev, "%s instant current: %d uA", __func__, curr_ua); in ab8500_fg_inst_curr_blocking()
739 disable_irq(di->irq); in ab8500_fg_inst_curr_blocking()
740 mutex_unlock(&di->cc_lock); in ab8500_fg_inst_curr_blocking()
745 * ab8500_fg_acc_cur_work() - average battery current
760 mutex_lock(&di->cc_lock); in ab8500_fg_acc_cur_work()
761 ret = abx500_set_register_interruptible(di->dev, AB8500_GAS_GAUGE, in ab8500_fg_acc_cur_work()
766 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE, in ab8500_fg_acc_cur_work()
771 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE, in ab8500_fg_acc_cur_work()
776 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE, in ab8500_fg_acc_cur_work()
793 di->accu_charge = (val * QLSB_NANO_AMP_HOURS_X10) / in ab8500_fg_acc_cur_work()
794 (100 * di->bm->fg_res); in ab8500_fg_acc_cur_work()
801 di->avg_curr_ua = (val * QLSB_NANO_AMP_HOURS_X10 * 36) / in ab8500_fg_acc_cur_work()
802 (di->bm->fg_res * (di->fg_samples / 4)); in ab8500_fg_acc_cur_work()
804 di->flags.conv_done = true; in ab8500_fg_acc_cur_work()
806 mutex_unlock(&di->cc_lock); in ab8500_fg_acc_cur_work()
808 queue_work(di->fg_wq, &di->fg_work); in ab8500_fg_acc_cur_work()
810 dev_dbg(di->dev, "fg_res: %d, fg_samples: %d, gasg: %d, accu_charge: %d \n", in ab8500_fg_acc_cur_work()
811 di->bm->fg_res, di->fg_samples, val, di->accu_charge); in ab8500_fg_acc_cur_work()
814 dev_err(di->dev, in ab8500_fg_acc_cur_work()
816 mutex_unlock(&di->cc_lock); in ab8500_fg_acc_cur_work()
817 queue_work(di->fg_wq, &di->fg_work); in ab8500_fg_acc_cur_work()
821 * ab8500_fg_bat_voltage() - get battery voltage
831 ret = iio_read_channel_processed(di->main_bat_v, &vbat); in ab8500_fg_bat_voltage()
833 dev_err(di->dev, in ab8500_fg_bat_voltage()
846 * ab8500_fg_volt_to_capacity() - Voltage based capacity
854 struct power_supply_battery_info *bi = di->bm->bi; in ab8500_fg_volt_to_capacity()
857 return power_supply_batinfo_ocv2cap(bi, voltage_uv, di->bat_temp) * 10; in ab8500_fg_volt_to_capacity()
861 * ab8500_fg_uncomp_volt_to_capacity() - Uncompensated voltage based capacity
869 di->vbat_uv = ab8500_fg_bat_voltage(di); in ab8500_fg_uncomp_volt_to_capacity()
870 return ab8500_fg_volt_to_capacity(di, di->vbat_uv); in ab8500_fg_uncomp_volt_to_capacity()
874 * ab8500_fg_battery_resistance() - Returns the battery inner resistance
884 struct power_supply_battery_info *bi = di->bm->bi; in ab8500_fg_battery_resistance()
889 * Determine the resistance at this voltage. First try VBAT-to-Ri else in ab8500_fg_battery_resistance()
894 resistance = power_supply_vbat2ri(bi, vbat_uncomp_uv, di->flags.charging); in ab8500_fg_battery_resistance()
898 resistance_percent = power_supply_temp2resist_simple(bi->resist_table, in ab8500_fg_battery_resistance()
899 bi->resist_table_size, in ab8500_fg_battery_resistance()
900 di->bat_temp / 10); in ab8500_fg_battery_resistance()
902 resistance = bi->factory_internal_resistance_uohm / 1000; in ab8500_fg_battery_resistance()
906 resistance = bi->factory_internal_resistance_uohm / 1000; in ab8500_fg_battery_resistance()
910 resistance += (di->line_impedance_uohm / 1000); in ab8500_fg_battery_resistance()
912 dev_dbg(di->dev, "%s Temp: %d battery internal resistance: %d" in ab8500_fg_battery_resistance()
914 __func__, di->bat_temp, resistance, di->bm->fg_res / 10, in ab8500_fg_battery_resistance()
915 (di->bm->fg_res / 10) + resistance); in ab8500_fg_battery_resistance()
918 resistance += di->bm->fg_res / 10; in ab8500_fg_battery_resistance()
924 * ab8500_load_comp_fg_bat_voltage() - get load compensated battery voltage
949 dev_err(di->dev, in ab8500_load_comp_fg_bat_voltage()
951 di->vbat_uv = vbat_uv / i; in ab8500_load_comp_fg_bat_voltage()
952 return di->vbat_uv; in ab8500_load_comp_fg_bat_voltage()
955 ab8500_fg_inst_curr_finalize(di, &di->inst_curr_ua); in ab8500_load_comp_fg_bat_voltage()
962 if (!always && di->inst_curr_ua < IGNORE_VBAT_HIGHCUR) in ab8500_load_comp_fg_bat_voltage()
963 return -EINVAL; in ab8500_load_comp_fg_bat_voltage()
969 vbat_uv = vbat_uv - (di->inst_curr_ua * rcomp) / 1000; in ab8500_load_comp_fg_bat_voltage()
972 di->vbat_uv = vbat_uv; in ab8500_load_comp_fg_bat_voltage()
978 * ab8500_fg_load_comp_volt_to_capacity() - Load compensated voltage based capacity
994 * ab8500_fg_convert_mah_to_permille() - Capacity in mAh to permille
1002 return (cap_mah * 1000) / di->bat_cap.max_mah_design; in ab8500_fg_convert_mah_to_permille()
1006 * ab8500_fg_convert_permille_to_mah() - Capacity in permille to mAh
1014 return cap_pm * di->bat_cap.max_mah_design / 1000; in ab8500_fg_convert_permille_to_mah()
1018 * ab8500_fg_convert_mah_to_uwh() - Capacity in mAh to uWh
1030 * Capacity is in milli ampere hours (10^-3)Ah in ab8500_fg_convert_mah_to_uwh()
1031 * Nominal voltage is in microvolts (10^-6)V in ab8500_fg_convert_mah_to_uwh()
1034 div_res = ((u64) cap_mah) * ((u64) di->vbat_nom_uv); in ab8500_fg_convert_mah_to_uwh()
1045 * ab8500_fg_calc_cap_charging() - Calculate remaining capacity while charging
1053 dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n", in ab8500_fg_calc_cap_charging()
1055 di->bat_cap.mah, in ab8500_fg_calc_cap_charging()
1056 di->accu_charge); in ab8500_fg_calc_cap_charging()
1059 if (di->bat_cap.mah + di->accu_charge > 0) in ab8500_fg_calc_cap_charging()
1060 di->bat_cap.mah += di->accu_charge; in ab8500_fg_calc_cap_charging()
1062 di->bat_cap.mah = 0; in ab8500_fg_calc_cap_charging()
1067 if (di->bat_cap.mah >= di->bat_cap.max_mah_design || in ab8500_fg_calc_cap_charging()
1068 di->flags.force_full) { in ab8500_fg_calc_cap_charging()
1069 di->bat_cap.mah = di->bat_cap.max_mah_design; in ab8500_fg_calc_cap_charging()
1072 ab8500_fg_fill_cap_sample(di, di->bat_cap.mah); in ab8500_fg_calc_cap_charging()
1073 di->bat_cap.permille = in ab8500_fg_calc_cap_charging()
1074 ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah); in ab8500_fg_calc_cap_charging()
1077 di->vbat_uv = ab8500_fg_bat_voltage(di); in ab8500_fg_calc_cap_charging()
1078 di->inst_curr_ua = ab8500_fg_inst_curr_blocking(di); in ab8500_fg_calc_cap_charging()
1080 return di->bat_cap.mah; in ab8500_fg_calc_cap_charging()
1084 * ab8500_fg_calc_cap_discharge_voltage() - Capacity in discharge with voltage
1099 di->bat_cap.mah = ab8500_fg_add_cap_sample(di, mah); in ab8500_fg_calc_cap_discharge_voltage()
1100 di->bat_cap.permille = in ab8500_fg_calc_cap_discharge_voltage()
1101 ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah); in ab8500_fg_calc_cap_discharge_voltage()
1103 return di->bat_cap.mah; in ab8500_fg_calc_cap_discharge_voltage()
1107 * ab8500_fg_calc_cap_discharge_fg() - Capacity in discharge with FG
1118 dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n", in ab8500_fg_calc_cap_discharge_fg()
1120 di->bat_cap.mah, in ab8500_fg_calc_cap_discharge_fg()
1121 di->accu_charge); in ab8500_fg_calc_cap_discharge_fg()
1124 if (di->bat_cap.mah + di->accu_charge > 0) in ab8500_fg_calc_cap_discharge_fg()
1125 di->bat_cap.mah += di->accu_charge; in ab8500_fg_calc_cap_discharge_fg()
1127 di->bat_cap.mah = 0; in ab8500_fg_calc_cap_discharge_fg()
1129 if (di->bat_cap.mah >= di->bat_cap.max_mah_design) in ab8500_fg_calc_cap_discharge_fg()
1130 di->bat_cap.mah = di->bat_cap.max_mah_design; in ab8500_fg_calc_cap_discharge_fg()
1136 permille = ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah); in ab8500_fg_calc_cap_discharge_fg()
1140 di->bat_cap.permille = permille_volt; in ab8500_fg_calc_cap_discharge_fg()
1141 di->bat_cap.mah = ab8500_fg_convert_permille_to_mah(di, in ab8500_fg_calc_cap_discharge_fg()
1142 di->bat_cap.permille); in ab8500_fg_calc_cap_discharge_fg()
1144 dev_dbg(di->dev, "%s voltage based: perm %d perm_volt %d\n", in ab8500_fg_calc_cap_discharge_fg()
1149 ab8500_fg_fill_cap_sample(di, di->bat_cap.mah); in ab8500_fg_calc_cap_discharge_fg()
1151 ab8500_fg_fill_cap_sample(di, di->bat_cap.mah); in ab8500_fg_calc_cap_discharge_fg()
1152 di->bat_cap.permille = in ab8500_fg_calc_cap_discharge_fg()
1153 ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah); in ab8500_fg_calc_cap_discharge_fg()
1156 return di->bat_cap.mah; in ab8500_fg_calc_cap_discharge_fg()
1160 * ab8500_fg_capacity_level() - Get the battery capacity level
1169 percent = DIV_ROUND_CLOSEST(di->bat_cap.permille, 10); in ab8500_fg_capacity_level()
1171 if (percent <= di->bm->cap_levels->critical || in ab8500_fg_capacity_level()
1172 di->flags.low_bat) in ab8500_fg_capacity_level()
1174 else if (percent <= di->bm->cap_levels->low) in ab8500_fg_capacity_level()
1176 else if (percent <= di->bm->cap_levels->normal) in ab8500_fg_capacity_level()
1178 else if (percent <= di->bm->cap_levels->high) in ab8500_fg_capacity_level()
1187 * ab8500_fg_calculate_scaled_capacity() - Capacity scaling
1196 struct ab8500_fg_cap_scaling *cs = &di->bat_cap.cap_scale; in ab8500_fg_calculate_scaled_capacity()
1197 int capacity = di->bat_cap.prev_percent; in ab8500_fg_calculate_scaled_capacity()
1199 if (!cs->enable) in ab8500_fg_calculate_scaled_capacity()
1206 if (di->flags.fully_charged) { in ab8500_fg_calculate_scaled_capacity()
1207 cs->cap_to_scale[0] = 100; in ab8500_fg_calculate_scaled_capacity()
1208 cs->cap_to_scale[1] = in ab8500_fg_calculate_scaled_capacity()
1209 max(capacity, di->bm->fg_params->maint_thres); in ab8500_fg_calculate_scaled_capacity()
1210 dev_dbg(di->dev, "Scale cap with %d/%d\n", in ab8500_fg_calculate_scaled_capacity()
1211 cs->cap_to_scale[0], cs->cap_to_scale[1]); in ab8500_fg_calculate_scaled_capacity()
1215 if ((cs->cap_to_scale[0] != cs->cap_to_scale[1]) in ab8500_fg_calculate_scaled_capacity()
1216 && (cs->cap_to_scale[1] > 0)) in ab8500_fg_calculate_scaled_capacity()
1218 DIV_ROUND_CLOSEST(di->bat_cap.prev_percent * in ab8500_fg_calculate_scaled_capacity()
1219 cs->cap_to_scale[0], in ab8500_fg_calculate_scaled_capacity()
1220 cs->cap_to_scale[1])); in ab8500_fg_calculate_scaled_capacity()
1222 if (di->flags.charging) { in ab8500_fg_calculate_scaled_capacity()
1223 if (capacity < cs->disable_cap_level) { in ab8500_fg_calculate_scaled_capacity()
1224 cs->disable_cap_level = capacity; in ab8500_fg_calculate_scaled_capacity()
1225 dev_dbg(di->dev, "Cap to stop scale lowered %d%%\n", in ab8500_fg_calculate_scaled_capacity()
1226 cs->disable_cap_level); in ab8500_fg_calculate_scaled_capacity()
1227 } else if (!di->flags.fully_charged) { in ab8500_fg_calculate_scaled_capacity()
1228 if (di->bat_cap.prev_percent >= in ab8500_fg_calculate_scaled_capacity()
1229 cs->disable_cap_level) { in ab8500_fg_calculate_scaled_capacity()
1230 dev_dbg(di->dev, "Disabling scaled capacity\n"); in ab8500_fg_calculate_scaled_capacity()
1231 cs->enable = false; in ab8500_fg_calculate_scaled_capacity()
1232 capacity = di->bat_cap.prev_percent; in ab8500_fg_calculate_scaled_capacity()
1234 dev_dbg(di->dev, in ab8500_fg_calculate_scaled_capacity()
1236 cs->disable_cap_level); in ab8500_fg_calculate_scaled_capacity()
1237 capacity = cs->disable_cap_level; in ab8500_fg_calculate_scaled_capacity()
1246 * ab8500_fg_update_cap_scalers() - Capacity scaling
1249 * To be called when state change from charge<->discharge to update
1254 struct ab8500_fg_cap_scaling *cs = &di->bat_cap.cap_scale; in ab8500_fg_update_cap_scalers()
1256 if (!cs->enable) in ab8500_fg_update_cap_scalers()
1258 if (di->flags.charging) { in ab8500_fg_update_cap_scalers()
1259 di->bat_cap.cap_scale.disable_cap_level = in ab8500_fg_update_cap_scalers()
1260 di->bat_cap.cap_scale.scaled_cap; in ab8500_fg_update_cap_scalers()
1261 dev_dbg(di->dev, "Cap to stop scale at charge %d%%\n", in ab8500_fg_update_cap_scalers()
1262 di->bat_cap.cap_scale.disable_cap_level); in ab8500_fg_update_cap_scalers()
1264 if (cs->scaled_cap != 100) { in ab8500_fg_update_cap_scalers()
1265 cs->cap_to_scale[0] = cs->scaled_cap; in ab8500_fg_update_cap_scalers()
1266 cs->cap_to_scale[1] = di->bat_cap.prev_percent; in ab8500_fg_update_cap_scalers()
1268 cs->cap_to_scale[0] = 100; in ab8500_fg_update_cap_scalers()
1269 cs->cap_to_scale[1] = in ab8500_fg_update_cap_scalers()
1270 max(di->bat_cap.prev_percent, in ab8500_fg_update_cap_scalers()
1271 di->bm->fg_params->maint_thres); in ab8500_fg_update_cap_scalers()
1274 dev_dbg(di->dev, "Cap to scale at discharge %d/%d\n", in ab8500_fg_update_cap_scalers()
1275 cs->cap_to_scale[0], cs->cap_to_scale[1]); in ab8500_fg_update_cap_scalers()
1280 * ab8500_fg_check_capacity_limits() - Check if capacity has changed
1290 int percent = DIV_ROUND_CLOSEST(di->bat_cap.permille, 10); in ab8500_fg_check_capacity_limits()
1292 di->bat_cap.level = ab8500_fg_capacity_level(di); in ab8500_fg_check_capacity_limits()
1294 if (di->bat_cap.level != di->bat_cap.prev_level) { in ab8500_fg_check_capacity_limits()
1299 if (!(!di->flags.charging && di->bat_cap.level > in ab8500_fg_check_capacity_limits()
1300 di->bat_cap.prev_level) || init) { in ab8500_fg_check_capacity_limits()
1301 dev_dbg(di->dev, "level changed from %d to %d\n", in ab8500_fg_check_capacity_limits()
1302 di->bat_cap.prev_level, in ab8500_fg_check_capacity_limits()
1303 di->bat_cap.level); in ab8500_fg_check_capacity_limits()
1304 di->bat_cap.prev_level = di->bat_cap.level; in ab8500_fg_check_capacity_limits()
1307 dev_dbg(di->dev, "level not allowed to go up " in ab8500_fg_check_capacity_limits()
1309 di->bat_cap.prev_level, in ab8500_fg_check_capacity_limits()
1310 di->bat_cap.level); in ab8500_fg_check_capacity_limits()
1318 if (di->flags.low_bat) { in ab8500_fg_check_capacity_limits()
1319 dev_dbg(di->dev, "Battery low, set capacity to 0\n"); in ab8500_fg_check_capacity_limits()
1320 di->bat_cap.prev_percent = 0; in ab8500_fg_check_capacity_limits()
1321 di->bat_cap.permille = 0; in ab8500_fg_check_capacity_limits()
1323 di->bat_cap.prev_mah = 0; in ab8500_fg_check_capacity_limits()
1324 di->bat_cap.mah = 0; in ab8500_fg_check_capacity_limits()
1326 } else if (di->flags.fully_charged) { in ab8500_fg_check_capacity_limits()
1331 if (di->flags.force_full) { in ab8500_fg_check_capacity_limits()
1332 di->bat_cap.prev_percent = percent; in ab8500_fg_check_capacity_limits()
1333 di->bat_cap.prev_mah = di->bat_cap.mah; in ab8500_fg_check_capacity_limits()
1337 if (!di->bat_cap.cap_scale.enable && in ab8500_fg_check_capacity_limits()
1338 di->bm->capacity_scaling) { in ab8500_fg_check_capacity_limits()
1339 di->bat_cap.cap_scale.enable = true; in ab8500_fg_check_capacity_limits()
1340 di->bat_cap.cap_scale.cap_to_scale[0] = 100; in ab8500_fg_check_capacity_limits()
1341 di->bat_cap.cap_scale.cap_to_scale[1] = in ab8500_fg_check_capacity_limits()
1342 di->bat_cap.prev_percent; in ab8500_fg_check_capacity_limits()
1343 di->bat_cap.cap_scale.disable_cap_level = 100; in ab8500_fg_check_capacity_limits()
1345 } else if (di->bat_cap.prev_percent != percent) { in ab8500_fg_check_capacity_limits()
1346 dev_dbg(di->dev, in ab8500_fg_check_capacity_limits()
1350 di->bat_cap.prev_percent = percent; in ab8500_fg_check_capacity_limits()
1351 di->bat_cap.prev_mah = di->bat_cap.mah; in ab8500_fg_check_capacity_limits()
1355 } else if (di->bat_cap.prev_percent != percent) { in ab8500_fg_check_capacity_limits()
1362 di->bat_cap.prev_percent = 1; in ab8500_fg_check_capacity_limits()
1366 } else if (!(!di->flags.charging && in ab8500_fg_check_capacity_limits()
1367 percent > di->bat_cap.prev_percent) || init) { in ab8500_fg_check_capacity_limits()
1372 dev_dbg(di->dev, in ab8500_fg_check_capacity_limits()
1374 di->bat_cap.prev_percent, in ab8500_fg_check_capacity_limits()
1376 di->bat_cap.permille); in ab8500_fg_check_capacity_limits()
1377 di->bat_cap.prev_percent = percent; in ab8500_fg_check_capacity_limits()
1378 di->bat_cap.prev_mah = di->bat_cap.mah; in ab8500_fg_check_capacity_limits()
1382 dev_dbg(di->dev, "capacity not allowed to go up since " in ab8500_fg_check_capacity_limits()
1384 di->bat_cap.prev_percent, in ab8500_fg_check_capacity_limits()
1386 di->bat_cap.permille); in ab8500_fg_check_capacity_limits()
1391 if (di->bm->capacity_scaling) { in ab8500_fg_check_capacity_limits()
1392 di->bat_cap.cap_scale.scaled_cap = in ab8500_fg_check_capacity_limits()
1395 dev_info(di->dev, "capacity=%d (%d)\n", in ab8500_fg_check_capacity_limits()
1396 di->bat_cap.prev_percent, in ab8500_fg_check_capacity_limits()
1397 di->bat_cap.cap_scale.scaled_cap); in ab8500_fg_check_capacity_limits()
1399 power_supply_changed(di->fg_psy); in ab8500_fg_check_capacity_limits()
1400 if (di->flags.fully_charged && di->flags.force_full) { in ab8500_fg_check_capacity_limits()
1401 dev_dbg(di->dev, "Battery full, notifying.\n"); in ab8500_fg_check_capacity_limits()
1402 di->flags.force_full = false; in ab8500_fg_check_capacity_limits()
1403 sysfs_notify(&di->fg_kobject, NULL, "charge_full"); in ab8500_fg_check_capacity_limits()
1405 sysfs_notify(&di->fg_kobject, NULL, "charge_now"); in ab8500_fg_check_capacity_limits()
1412 dev_dbg(di->dev, "Charge state from %d [%s] to %d [%s]\n", in ab8500_fg_charge_state_to()
1413 di->charge_state, in ab8500_fg_charge_state_to()
1414 charge_state[di->charge_state], in ab8500_fg_charge_state_to()
1418 di->charge_state = new_state; in ab8500_fg_charge_state_to()
1424 dev_dbg(di->dev, "Discharge state from %d [%s] to %d [%s]\n", in ab8500_fg_discharge_state_to()
1425 di->discharge_state, in ab8500_fg_discharge_state_to()
1426 discharge_state[di->discharge_state], in ab8500_fg_discharge_state_to()
1430 di->discharge_state = new_state; in ab8500_fg_discharge_state_to()
1434 * ab8500_fg_algorithm_charging() - FG algorithm for when charging
1445 if (di->discharge_state != AB8500_FG_DISCHARGE_INIT_RECOVERY) in ab8500_fg_algorithm_charging()
1449 switch (di->charge_state) { in ab8500_fg_algorithm_charging()
1451 di->fg_samples = SEC_TO_SAMPLE( in ab8500_fg_algorithm_charging()
1452 di->bm->fg_params->accu_charging); in ab8500_fg_algorithm_charging()
1463 mutex_lock(&di->cc_lock); in ab8500_fg_algorithm_charging()
1464 if (!di->flags.conv_done && !di->flags.force_full) { in ab8500_fg_algorithm_charging()
1466 mutex_unlock(&di->cc_lock); in ab8500_fg_algorithm_charging()
1467 dev_dbg(di->dev, "%s CC conv not done\n", in ab8500_fg_algorithm_charging()
1472 di->flags.conv_done = false; in ab8500_fg_algorithm_charging()
1473 mutex_unlock(&di->cc_lock); in ab8500_fg_algorithm_charging()
1492 cap = di->bat_cap.user_mah; in force_capacity()
1493 if (cap > di->bat_cap.max_mah_design) { in force_capacity()
1494 dev_dbg(di->dev, "Remaining cap %d can't be bigger than total" in force_capacity()
1495 " %d\n", cap, di->bat_cap.max_mah_design); in force_capacity()
1496 cap = di->bat_cap.max_mah_design; in force_capacity()
1498 ab8500_fg_fill_cap_sample(di, di->bat_cap.user_mah); in force_capacity()
1499 di->bat_cap.permille = ab8500_fg_convert_mah_to_permille(di, cap); in force_capacity()
1500 di->bat_cap.mah = cap; in force_capacity()
1509 cap = di->bat_cap.user_mah; in check_sysfs_capacity()
1512 di->bat_cap.user_mah); in check_sysfs_capacity()
1514 lower = di->bat_cap.permille - di->bm->fg_params->user_cap_limit * 10; in check_sysfs_capacity()
1515 upper = di->bat_cap.permille + di->bm->fg_params->user_cap_limit * 10; in check_sysfs_capacity()
1519 /* 1000 is permille, -> 100 percent */ in check_sysfs_capacity()
1523 dev_dbg(di->dev, "Capacity limits:" in check_sysfs_capacity()
1525 lower, cap_permille, upper, cap, di->bat_cap.mah); in check_sysfs_capacity()
1529 dev_dbg(di->dev, "OK! Using users cap %d uAh now\n", cap); in check_sysfs_capacity()
1533 dev_dbg(di->dev, "Capacity from user out of limits, ignoring"); in check_sysfs_capacity()
1538 * ab8500_fg_algorithm_discharging() - FG algorithm for when discharging
1548 if (di->charge_state != AB8500_FG_CHARGE_INIT) in ab8500_fg_algorithm_discharging()
1551 switch (di->discharge_state) { in ab8500_fg_algorithm_discharging()
1554 di->init_cnt = 0; in ab8500_fg_algorithm_discharging()
1555 di->fg_samples = SEC_TO_SAMPLE(di->bm->fg_params->init_timer); in ab8500_fg_algorithm_discharging()
1568 sleep_time = di->bm->fg_params->init_timer; in ab8500_fg_algorithm_discharging()
1571 if (di->init_cnt > di->bm->fg_params->init_discard_time) { in ab8500_fg_algorithm_discharging()
1577 di->init_cnt += sleep_time; in ab8500_fg_algorithm_discharging()
1578 if (di->init_cnt > di->bm->fg_params->init_total_time) in ab8500_fg_algorithm_discharging()
1585 di->recovery_cnt = 0; in ab8500_fg_algorithm_discharging()
1586 di->recovery_needed = true; in ab8500_fg_algorithm_discharging()
1593 sleep_time = di->bm->fg_params->recovery_sleep_timer; in ab8500_fg_algorithm_discharging()
1601 di->inst_curr_ua = ab8500_fg_inst_curr_blocking(di); in ab8500_fg_algorithm_discharging()
1603 if (ab8500_fg_is_low_curr(di, di->inst_curr_ua)) { in ab8500_fg_algorithm_discharging()
1604 if (di->recovery_cnt > in ab8500_fg_algorithm_discharging()
1605 di->bm->fg_params->recovery_total_time) { in ab8500_fg_algorithm_discharging()
1606 di->fg_samples = SEC_TO_SAMPLE( in ab8500_fg_algorithm_discharging()
1607 di->bm->fg_params->accu_high_curr); in ab8500_fg_algorithm_discharging()
1611 di->recovery_needed = false; in ab8500_fg_algorithm_discharging()
1613 queue_delayed_work(di->fg_wq, in ab8500_fg_algorithm_discharging()
1614 &di->fg_periodic_work, in ab8500_fg_algorithm_discharging()
1617 di->recovery_cnt += sleep_time; in ab8500_fg_algorithm_discharging()
1619 di->fg_samples = SEC_TO_SAMPLE( in ab8500_fg_algorithm_discharging()
1620 di->bm->fg_params->accu_high_curr); in ab8500_fg_algorithm_discharging()
1628 di->fg_samples = SEC_TO_SAMPLE( in ab8500_fg_algorithm_discharging()
1629 di->bm->fg_params->accu_high_curr); in ab8500_fg_algorithm_discharging()
1636 di->inst_curr_ua = ab8500_fg_inst_curr_blocking(di); in ab8500_fg_algorithm_discharging()
1638 if (ab8500_fg_is_low_curr(di, di->inst_curr_ua)) { in ab8500_fg_algorithm_discharging()
1640 if (di->high_curr_mode) { in ab8500_fg_algorithm_discharging()
1641 di->high_curr_mode = false; in ab8500_fg_algorithm_discharging()
1642 di->high_curr_cnt = 0; in ab8500_fg_algorithm_discharging()
1645 if (di->recovery_needed) { in ab8500_fg_algorithm_discharging()
1649 queue_delayed_work(di->fg_wq, in ab8500_fg_algorithm_discharging()
1650 &di->fg_periodic_work, 0); in ab8500_fg_algorithm_discharging()
1657 mutex_lock(&di->cc_lock); in ab8500_fg_algorithm_discharging()
1658 if (!di->flags.conv_done) { in ab8500_fg_algorithm_discharging()
1660 mutex_unlock(&di->cc_lock); in ab8500_fg_algorithm_discharging()
1661 dev_dbg(di->dev, "%s CC conv not done\n", in ab8500_fg_algorithm_discharging()
1666 di->flags.conv_done = false; in ab8500_fg_algorithm_discharging()
1667 mutex_unlock(&di->cc_lock); in ab8500_fg_algorithm_discharging()
1670 if (!di->high_curr_mode) { in ab8500_fg_algorithm_discharging()
1671 di->high_curr_mode = true; in ab8500_fg_algorithm_discharging()
1672 di->high_curr_cnt = 0; in ab8500_fg_algorithm_discharging()
1675 di->high_curr_cnt += in ab8500_fg_algorithm_discharging()
1676 di->bm->fg_params->accu_high_curr; in ab8500_fg_algorithm_discharging()
1677 if (di->high_curr_cnt > in ab8500_fg_algorithm_discharging()
1678 di->bm->fg_params->high_curr_time) in ab8500_fg_algorithm_discharging()
1679 di->recovery_needed = true; in ab8500_fg_algorithm_discharging()
1691 di->fg_samples = SEC_TO_SAMPLE( in ab8500_fg_algorithm_discharging()
1692 di->bm->fg_params->accu_high_curr); in ab8500_fg_algorithm_discharging()
1707 * ab8500_fg_algorithm_calibrate() - Internal columb counter offset calibration
1715 switch (di->calib_state) { in ab8500_fg_algorithm_calibrate()
1717 dev_dbg(di->dev, "Calibration ongoing...\n"); in ab8500_fg_algorithm_calibrate()
1719 ret = abx500_mask_and_set_register_interruptible(di->dev, in ab8500_fg_algorithm_calibrate()
1725 ret = abx500_mask_and_set_register_interruptible(di->dev, in ab8500_fg_algorithm_calibrate()
1730 di->calib_state = AB8500_FG_CALIB_WAIT; in ab8500_fg_algorithm_calibrate()
1733 ret = abx500_mask_and_set_register_interruptible(di->dev, in ab8500_fg_algorithm_calibrate()
1738 di->flags.calibrate = false; in ab8500_fg_algorithm_calibrate()
1739 dev_dbg(di->dev, "Calibration done...\n"); in ab8500_fg_algorithm_calibrate()
1740 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); in ab8500_fg_algorithm_calibrate()
1743 dev_dbg(di->dev, "Calibration WFI\n"); in ab8500_fg_algorithm_calibrate()
1751 dev_err(di->dev, "failed to calibrate the CC\n"); in ab8500_fg_algorithm_calibrate()
1752 di->flags.calibrate = false; in ab8500_fg_algorithm_calibrate()
1753 di->calib_state = AB8500_FG_CALIB_INIT; in ab8500_fg_algorithm_calibrate()
1754 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); in ab8500_fg_algorithm_calibrate()
1758 * ab8500_fg_algorithm() - Entry point for the FG algorithm
1765 if (di->flags.calibrate) in ab8500_fg_algorithm()
1768 if (di->flags.charging) in ab8500_fg_algorithm()
1774 dev_dbg(di->dev, "[FG_DATA] %d %d %d %d %d %d %d %d %d %d " in ab8500_fg_algorithm()
1776 di->bat_cap.max_mah_design, in ab8500_fg_algorithm()
1777 di->bat_cap.max_mah, in ab8500_fg_algorithm()
1778 di->bat_cap.mah, in ab8500_fg_algorithm()
1779 di->bat_cap.permille, in ab8500_fg_algorithm()
1780 di->bat_cap.level, in ab8500_fg_algorithm()
1781 di->bat_cap.prev_mah, in ab8500_fg_algorithm()
1782 di->bat_cap.prev_percent, in ab8500_fg_algorithm()
1783 di->bat_cap.prev_level, in ab8500_fg_algorithm()
1784 di->vbat_uv, in ab8500_fg_algorithm()
1785 di->inst_curr_ua, in ab8500_fg_algorithm()
1786 di->avg_curr_ua, in ab8500_fg_algorithm()
1787 di->accu_charge, in ab8500_fg_algorithm()
1788 di->flags.charging, in ab8500_fg_algorithm()
1789 di->charge_state, in ab8500_fg_algorithm()
1790 di->discharge_state, in ab8500_fg_algorithm()
1791 di->high_curr_mode, in ab8500_fg_algorithm()
1792 di->recovery_needed); in ab8500_fg_algorithm()
1796 * ab8500_fg_periodic_work() - Run the FG state machine periodically
1806 if (di->init_capacity) { in ab8500_fg_periodic_work()
1810 di->init_capacity = false; in ab8500_fg_periodic_work()
1812 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); in ab8500_fg_periodic_work()
1813 } else if (di->flags.user_cap) { in ab8500_fg_periodic_work()
1816 if (di->flags.charging) in ab8500_fg_periodic_work()
1823 di->flags.user_cap = false; in ab8500_fg_periodic_work()
1824 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); in ab8500_fg_periodic_work()
1831 * ab8500_fg_check_hw_failure_work() - Check OVV_BAT condition
1845 * If we have had a battery over-voltage situation, in ab8500_fg_check_hw_failure_work()
1846 * check ovv-bit to see if it should be reset. in ab8500_fg_check_hw_failure_work()
1848 ret = abx500_get_register_interruptible(di->dev, in ab8500_fg_check_hw_failure_work()
1852 dev_err(di->dev, "%s ab8500 read failed\n", __func__); in ab8500_fg_check_hw_failure_work()
1856 if (!di->flags.bat_ovv) { in ab8500_fg_check_hw_failure_work()
1857 dev_dbg(di->dev, "Battery OVV\n"); in ab8500_fg_check_hw_failure_work()
1858 di->flags.bat_ovv = true; in ab8500_fg_check_hw_failure_work()
1859 power_supply_changed(di->fg_psy); in ab8500_fg_check_hw_failure_work()
1862 queue_delayed_work(di->fg_wq, &di->fg_check_hw_failure_work, in ab8500_fg_check_hw_failure_work()
1865 dev_dbg(di->dev, "Battery recovered from OVV\n"); in ab8500_fg_check_hw_failure_work()
1866 di->flags.bat_ovv = false; in ab8500_fg_check_hw_failure_work()
1867 power_supply_changed(di->fg_psy); in ab8500_fg_check_hw_failure_work()
1872 * ab8500_fg_low_bat_work() - Check LOW_BAT condition
1887 if (vbat_uv < di->bm->fg_params->lowbat_threshold_uv) { in ab8500_fg_low_bat_work()
1889 if (di->low_bat_cnt < 1) { in ab8500_fg_low_bat_work()
1890 di->flags.low_bat = true; in ab8500_fg_low_bat_work()
1891 dev_warn(di->dev, "Shut down pending...\n"); in ab8500_fg_low_bat_work()
1894 * Else we need to re-schedule this check to be able to detect in ab8500_fg_low_bat_work()
1898 di->low_bat_cnt--; in ab8500_fg_low_bat_work()
1899 dev_warn(di->dev, "Battery voltage still LOW\n"); in ab8500_fg_low_bat_work()
1900 queue_delayed_work(di->fg_wq, &di->fg_low_bat_work, in ab8500_fg_low_bat_work()
1904 di->flags.low_bat_delay = false; in ab8500_fg_low_bat_work()
1905 di->low_bat_cnt = 10; in ab8500_fg_low_bat_work()
1906 dev_warn(di->dev, "Battery voltage OK again\n"); in ab8500_fg_low_bat_work()
1914 * ab8500_fg_battok_calc - calculate the bit pattern corresponding
1920 * valid return values are 0-14. (0-BATT_OK_MAX_NR_INCREMENTS)
1930 return (target - BATT_OK_MIN) / BATT_OK_INCREMENT; in ab8500_fg_battok_calc()
1934 * ab8500_fg_battok_init_hw_register - init battok levels
1949 sel0 = di->bm->fg_params->battok_falling_th_sel0; in ab8500_fg_battok_init_hw_register()
1950 sel1 = di->bm->fg_params->battok_raising_th_sel1; in ab8500_fg_battok_init_hw_register()
1958 dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n", in ab8500_fg_battok_init_hw_register()
1964 dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n", in ab8500_fg_battok_init_hw_register()
1969 dev_dbg(di->dev, "using: %x %d %d\n", new_val, cbp_sel0, cbp_sel1); in ab8500_fg_battok_init_hw_register()
1970 ret = abx500_set_register_interruptible(di->dev, AB8500_SYS_CTRL2_BLOCK, in ab8500_fg_battok_init_hw_register()
1976 * ab8500_fg_instant_work() - Run the FG state machine instantly
1989 * ab8500_fg_cc_data_end_handler() - end of data conversion isr.
1998 if (!di->nbr_cceoc_irq_cnt) { in ab8500_fg_cc_data_end_handler()
1999 di->nbr_cceoc_irq_cnt++; in ab8500_fg_cc_data_end_handler()
2000 complete(&di->ab8500_fg_started); in ab8500_fg_cc_data_end_handler()
2002 di->nbr_cceoc_irq_cnt = 0; in ab8500_fg_cc_data_end_handler()
2003 complete(&di->ab8500_fg_complete); in ab8500_fg_cc_data_end_handler()
2009 * ab8500_fg_cc_int_calib_handler () - end of calibration isr.
2018 di->calib_state = AB8500_FG_CALIB_END; in ab8500_fg_cc_int_calib_handler()
2019 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); in ab8500_fg_cc_int_calib_handler()
2024 * ab8500_fg_cc_convend_handler() - isr to get battery avg current.
2034 queue_work(di->fg_wq, &di->fg_acc_cur_work); in ab8500_fg_cc_convend_handler()
2040 * ab8500_fg_batt_ovv_handler() - Battery OVV occured
2050 dev_dbg(di->dev, "Battery OVV\n"); in ab8500_fg_batt_ovv_handler()
2053 queue_delayed_work(di->fg_wq, &di->fg_check_hw_failure_work, 0); in ab8500_fg_batt_ovv_handler()
2059 * ab8500_fg_lowbatf_handler() - Battery voltage is below LOW threshold
2070 if (!di->flags.low_bat_delay) { in ab8500_fg_lowbatf_handler()
2071 dev_warn(di->dev, "Battery voltage is below LOW threshold\n"); in ab8500_fg_lowbatf_handler()
2072 di->flags.low_bat_delay = true; in ab8500_fg_lowbatf_handler()
2077 queue_delayed_work(di->fg_wq, &di->fg_low_bat_work, in ab8500_fg_lowbatf_handler()
2084 * ab8500_fg_get_property() - get the fg properties
2116 if (di->flags.bat_ovv) in ab8500_fg_get_property()
2117 val->intval = BATT_OVV_VALUE; in ab8500_fg_get_property()
2119 val->intval = di->vbat_uv; in ab8500_fg_get_property()
2122 val->intval = di->inst_curr_ua; in ab8500_fg_get_property()
2125 val->intval = di->avg_curr_ua; in ab8500_fg_get_property()
2128 val->intval = ab8500_fg_convert_mah_to_uwh(di, in ab8500_fg_get_property()
2129 di->bat_cap.max_mah_design); in ab8500_fg_get_property()
2132 val->intval = ab8500_fg_convert_mah_to_uwh(di, in ab8500_fg_get_property()
2133 di->bat_cap.max_mah); in ab8500_fg_get_property()
2136 if (di->flags.batt_unknown && !di->bm->chg_unknown_bat && in ab8500_fg_get_property()
2137 di->flags.batt_id_received) in ab8500_fg_get_property()
2138 val->intval = ab8500_fg_convert_mah_to_uwh(di, in ab8500_fg_get_property()
2139 di->bat_cap.max_mah); in ab8500_fg_get_property()
2141 val->intval = ab8500_fg_convert_mah_to_uwh(di, in ab8500_fg_get_property()
2142 di->bat_cap.prev_mah); in ab8500_fg_get_property()
2145 val->intval = di->bat_cap.max_mah_design; in ab8500_fg_get_property()
2148 val->intval = di->bat_cap.max_mah; in ab8500_fg_get_property()
2151 if (di->flags.batt_unknown && !di->bm->chg_unknown_bat && in ab8500_fg_get_property()
2152 di->flags.batt_id_received) in ab8500_fg_get_property()
2153 val->intval = di->bat_cap.max_mah; in ab8500_fg_get_property()
2155 val->intval = di->bat_cap.prev_mah; in ab8500_fg_get_property()
2158 if (di->flags.batt_unknown && !di->bm->chg_unknown_bat && in ab8500_fg_get_property()
2159 di->flags.batt_id_received) in ab8500_fg_get_property()
2160 val->intval = 100; in ab8500_fg_get_property()
2162 val->intval = di->bat_cap.prev_percent; in ab8500_fg_get_property()
2165 if (di->flags.batt_unknown && !di->bm->chg_unknown_bat && in ab8500_fg_get_property()
2166 di->flags.batt_id_received) in ab8500_fg_get_property()
2167 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; in ab8500_fg_get_property()
2169 val->intval = di->bat_cap.prev_level; in ab8500_fg_get_property()
2172 return -EINVAL; in ab8500_fg_get_property()
2181 const char **supplicants = (const char **)ext->supplied_to; in ab8500_fg_get_ext_psy_data()
2189 bi = di->bm->bi; in ab8500_fg_get_ext_psy_data()
2195 j = match_string(supplicants, ext->num_supplicants, psy->desc->name); in ab8500_fg_get_ext_psy_data()
2200 for (j = 0; j < ext->desc->num_properties; j++) { in ab8500_fg_get_ext_psy_data()
2202 prop = ext->desc->properties[j]; in ab8500_fg_get_ext_psy_data()
2209 switch (ext->desc->type) { in ab8500_fg_get_ext_psy_data()
2215 if (!di->flags.charging) in ab8500_fg_get_ext_psy_data()
2217 di->flags.charging = false; in ab8500_fg_get_ext_psy_data()
2218 di->flags.fully_charged = false; in ab8500_fg_get_ext_psy_data()
2219 if (di->bm->capacity_scaling) in ab8500_fg_get_ext_psy_data()
2221 queue_work(di->fg_wq, &di->fg_work); in ab8500_fg_get_ext_psy_data()
2224 if (di->flags.fully_charged) in ab8500_fg_get_ext_psy_data()
2226 di->flags.fully_charged = true; in ab8500_fg_get_ext_psy_data()
2227 di->flags.force_full = true; in ab8500_fg_get_ext_psy_data()
2229 di->bat_cap.max_mah = di->bat_cap.mah; in ab8500_fg_get_ext_psy_data()
2230 queue_work(di->fg_wq, &di->fg_work); in ab8500_fg_get_ext_psy_data()
2233 if (di->flags.charging && in ab8500_fg_get_ext_psy_data()
2234 !di->flags.fully_charged) in ab8500_fg_get_ext_psy_data()
2236 di->flags.charging = true; in ab8500_fg_get_ext_psy_data()
2237 di->flags.fully_charged = false; in ab8500_fg_get_ext_psy_data()
2238 if (di->bm->capacity_scaling) in ab8500_fg_get_ext_psy_data()
2240 queue_work(di->fg_wq, &di->fg_work); in ab8500_fg_get_ext_psy_data()
2249 switch (ext->desc->type) { in ab8500_fg_get_ext_psy_data()
2251 if (!di->flags.batt_id_received && in ab8500_fg_get_ext_psy_data()
2252 (bi && (bi->technology != in ab8500_fg_get_ext_psy_data()
2254 di->flags.batt_id_received = true; in ab8500_fg_get_ext_psy_data()
2256 di->bat_cap.max_mah_design = in ab8500_fg_get_ext_psy_data()
2257 di->bm->bi->charge_full_design_uah; in ab8500_fg_get_ext_psy_data()
2259 di->bat_cap.max_mah = in ab8500_fg_get_ext_psy_data()
2260 di->bat_cap.max_mah_design; in ab8500_fg_get_ext_psy_data()
2262 di->vbat_nom_uv = in ab8500_fg_get_ext_psy_data()
2263 di->bm->bi->voltage_max_design_uv; in ab8500_fg_get_ext_psy_data()
2267 di->flags.batt_unknown = false; in ab8500_fg_get_ext_psy_data()
2269 di->flags.batt_unknown = true; in ab8500_fg_get_ext_psy_data()
2276 switch (ext->desc->type) { in ab8500_fg_get_ext_psy_data()
2278 if (di->flags.batt_id_received) in ab8500_fg_get_ext_psy_data()
2279 di->bat_temp = ret.intval; in ab8500_fg_get_ext_psy_data()
2293 * ab8500_fg_init_hw_registers() - Set up FG related registers
2309 ret = abx500_mask_and_set_register_interruptible(di->dev, in ab8500_fg_init_hw_registers()
2315 dev_err(di->dev, "failed to set BATT_OVV\n"); in ab8500_fg_init_hw_registers()
2320 ret = abx500_mask_and_set_register_interruptible(di->dev, in ab8500_fg_init_hw_registers()
2326 dev_err(di->dev, "failed to enable BATT_OVV\n"); in ab8500_fg_init_hw_registers()
2331 ret = abx500_set_register_interruptible(di->dev, in ab8500_fg_init_hw_registers()
2335 di->bm->fg_params->lowbat_threshold_uv) << 1 | in ab8500_fg_init_hw_registers()
2338 dev_err(di->dev, "%s write failed\n", __func__); in ab8500_fg_init_hw_registers()
2345 dev_err(di->dev, "BattOk init write failed.\n"); in ab8500_fg_init_hw_registers()
2349 if (is_ab8505(di->parent)) { in ab8500_fg_init_hw_registers()
2350 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, in ab8500_fg_init_hw_registers()
2351 AB8505_RTC_PCUT_MAX_TIME_REG, di->bm->fg_params->pcut_max_time); in ab8500_fg_init_hw_registers()
2354 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_MAX_TIME_REG\n", __func__); in ab8500_fg_init_hw_registers()
2358 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, in ab8500_fg_init_hw_registers()
2359 AB8505_RTC_PCUT_FLAG_TIME_REG, di->bm->fg_params->pcut_flag_time); in ab8500_fg_init_hw_registers()
2362 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_FLAG_TIME_REG\n", __func__); in ab8500_fg_init_hw_registers()
2366 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, in ab8500_fg_init_hw_registers()
2367 AB8505_RTC_PCUT_RESTART_REG, di->bm->fg_params->pcut_max_restart); in ab8500_fg_init_hw_registers()
2370 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_RESTART_REG\n", __func__); in ab8500_fg_init_hw_registers()
2374 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, in ab8500_fg_init_hw_registers()
2375 AB8505_RTC_PCUT_DEBOUNCE_REG, di->bm->fg_params->pcut_debounce_time); in ab8500_fg_init_hw_registers()
2378 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_DEBOUNCE_REG\n", __func__); in ab8500_fg_init_hw_registers()
2382 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, in ab8500_fg_init_hw_registers()
2383 AB8505_RTC_PCUT_CTL_STATUS_REG, di->bm->fg_params->pcut_enable); in ab8500_fg_init_hw_registers()
2386 dev_err(di->dev, "%s write failed AB8505_RTC_PCUT_CTL_STATUS_REG\n", __func__); in ab8500_fg_init_hw_registers()
2395 * ab8500_fg_external_power_changed() - callback for power supply changes
2409 * ab8500_fg_reinit_work() - work to reset the FG algorithm
2421 if (!di->flags.calibrate) { in ab8500_fg_reinit_work()
2422 dev_dbg(di->dev, "Resetting FG state machine to init.\n"); in ab8500_fg_reinit_work()
2427 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); in ab8500_fg_reinit_work()
2430 dev_err(di->dev, "Residual offset calibration ongoing " in ab8500_fg_reinit_work()
2433 queue_delayed_work(di->fg_wq, &di->fg_reinit_work, in ab8500_fg_reinit_work()
2448 return sysfs_emit(buf, "%d\n", di->bat_cap.max_mah); in charge_full_show()
2461 di->bat_cap.max_mah = (int) charge_full; in charge_full_store()
2467 return sysfs_emit(buf, "%d\n", di->bat_cap.prev_mah); in charge_now_show()
2480 di->bat_cap.user_mah = (int) charge_now; in charge_now_store()
2481 di->flags.user_cap = true; in charge_now_store()
2482 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); in charge_now_store()
2501 if (!entry->show) in ab8500_fg_show()
2502 return -EIO; in ab8500_fg_show()
2504 return entry->show(di, buf); in ab8500_fg_show()
2516 if (!entry->store) in ab8500_fg_store()
2517 return -EIO; in ab8500_fg_store()
2519 return entry->store(di, buf, count); in ab8500_fg_store()
2540 * ab8500_fg_sysfs_exit() - de-init of sysfs entry
2547 kobject_del(&di->fg_kobject); in ab8500_fg_sysfs_exit()
2551 * ab8500_fg_sysfs_init() - init of sysfs entry
2561 ret = kobject_init_and_add(&di->fg_kobject, in ab8500_fg_sysfs_init()
2565 kobject_put(&di->fg_kobject); in ab8500_fg_sysfs_init()
2566 dev_err(di->dev, "failed to create sysfs entry\n"); in ab8500_fg_sysfs_init()
2581 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC, in ab8505_powercut_flagtime_read()
2608 dev_err(dev, "Incorrect parameter, echo 0 (1.98s) - 127 (15.625ms) for flagtime\n"); in ab8505_powercut_flagtime_write()
2612 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, in ab8505_powercut_flagtime_write()
2631 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC, in ab8505_powercut_maxtime_read()
2659 dev_err(dev, "Incorrect parameter, echo 0 (0.0s) - 127 (1.98s) for maxtime\n"); in ab8505_powercut_maxtime_write()
2663 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, in ab8505_powercut_maxtime_write()
2682 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC, in ab8505_powercut_restart_read()
2709 dev_err(dev, "Incorrect parameter, echo 0 - 15 for number of restart\n"); in ab8505_powercut_restart_write()
2713 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, in ab8505_powercut_restart_write()
2733 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC, in ab8505_powercut_timer_read()
2756 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC, in ab8505_powercut_restart_counter_read()
2779 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC, in ab8505_powercut_read()
2808 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, in ab8505_powercut_write()
2828 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC, in ab8505_powercut_flag_read()
2851 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC, in ab8505_powercut_debounce_read()
2882 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, in ab8505_powercut_debounce_write()
2901 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC, in ab8505_powercut_enable_status_read()
2938 if (is_ab8505(di->parent)) { in ab8500_fg_sysfs_psy_create_attrs()
2940 if (device_create_file(&di->fg_psy->dev, in ab8500_fg_sysfs_psy_create_attrs()
2946 dev_err(&di->fg_psy->dev, "Failed creating sysfs psy attrs for ab8505.\n"); in ab8500_fg_sysfs_psy_create_attrs()
2947 while (i--) in ab8500_fg_sysfs_psy_create_attrs()
2948 device_remove_file(&di->fg_psy->dev, in ab8500_fg_sysfs_psy_create_attrs()
2951 return -EIO; in ab8500_fg_sysfs_psy_create_attrs()
2958 if (is_ab8505(di->parent)) { in ab8500_fg_sysfs_psy_remove_attrs()
2960 (void)device_remove_file(&di->fg_psy->dev, in ab8500_fg_sysfs_psy_remove_attrs()
2975 if (!di->flags.charging) { in ab8500_fg_resume()
2977 queue_work(di->fg_wq, &di->fg_work); in ab8500_fg_resume()
2987 flush_delayed_work(&di->fg_periodic_work); in ab8500_fg_suspend()
2988 flush_work(&di->fg_work); in ab8500_fg_suspend()
2989 flush_work(&di->fg_acc_cur_work); in ab8500_fg_suspend()
2990 flush_delayed_work(&di->fg_reinit_work); in ab8500_fg_suspend()
2991 flush_delayed_work(&di->fg_low_bat_work); in ab8500_fg_suspend()
2992 flush_delayed_work(&di->fg_check_hw_failure_work); in ab8500_fg_suspend()
2998 if (di->flags.fg_enabled && !di->flags.charging) in ab8500_fg_suspend()
3032 di->bat_cap.max_mah_design = di->bm->bi->charge_full_design_uah; in ab8500_fg_bind()
3033 di->bat_cap.max_mah = di->bat_cap.max_mah_design; in ab8500_fg_bind()
3034 di->vbat_nom_uv = di->bm->bi->voltage_max_design_uv; in ab8500_fg_bind()
3039 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); in ab8500_fg_bind()
3055 flush_workqueue(di->fg_wq); in ab8500_fg_unbind()
3065 struct device *dev = &pdev->dev; in ab8500_fg_probe()
3073 return -ENOMEM; in ab8500_fg_probe()
3075 di->bm = &ab8500_bm_data; in ab8500_fg_probe()
3077 mutex_init(&di->cc_lock); in ab8500_fg_probe()
3080 di->dev = dev; in ab8500_fg_probe()
3081 di->parent = dev_get_drvdata(pdev->dev.parent); in ab8500_fg_probe()
3083 di->main_bat_v = devm_iio_channel_get(dev, "main_bat_v"); in ab8500_fg_probe()
3084 if (IS_ERR(di->main_bat_v)) { in ab8500_fg_probe()
3085 ret = dev_err_probe(dev, PTR_ERR(di->main_bat_v), in ab8500_fg_probe()
3090 if (!of_property_read_u32(dev->of_node, "line-impedance-micro-ohms", in ab8500_fg_probe()
3091 &di->line_impedance_uohm)) in ab8500_fg_probe()
3093 di->line_impedance_uohm); in ab8500_fg_probe()
3099 di->init_capacity = true; in ab8500_fg_probe()
3105 di->fg_wq = alloc_ordered_workqueue("ab8500_fg_wq", WQ_MEM_RECLAIM); in ab8500_fg_probe()
3106 if (di->fg_wq == NULL) { in ab8500_fg_probe()
3108 return -ENOMEM; in ab8500_fg_probe()
3112 INIT_WORK(&di->fg_work, ab8500_fg_instant_work); in ab8500_fg_probe()
3115 INIT_WORK(&di->fg_acc_cur_work, ab8500_fg_acc_cur_work); in ab8500_fg_probe()
3118 INIT_DEFERRABLE_WORK(&di->fg_reinit_work, in ab8500_fg_probe()
3122 INIT_DEFERRABLE_WORK(&di->fg_periodic_work, in ab8500_fg_probe()
3126 INIT_DEFERRABLE_WORK(&di->fg_low_bat_work, in ab8500_fg_probe()
3130 INIT_DEFERRABLE_WORK(&di->fg_check_hw_failure_work, in ab8500_fg_probe()
3134 di->flags.low_bat = false; in ab8500_fg_probe()
3137 di->low_bat_cnt = 10; in ab8500_fg_probe()
3143 destroy_workqueue(di->fg_wq); in ab8500_fg_probe()
3148 di->flags.batt_unknown = true; in ab8500_fg_probe()
3149 di->flags.batt_id_received = false; in ab8500_fg_probe()
3152 di->fg_psy = devm_power_supply_register(dev, &ab8500_fg_desc, &psy_cfg); in ab8500_fg_probe()
3153 if (IS_ERR(di->fg_psy)) { in ab8500_fg_probe()
3155 destroy_workqueue(di->fg_wq); in ab8500_fg_probe()
3156 return PTR_ERR(di->fg_psy); in ab8500_fg_probe()
3159 di->fg_samples = SEC_TO_SAMPLE(di->bm->fg_params->init_timer); in ab8500_fg_probe()
3165 init_completion(&di->ab8500_fg_started); in ab8500_fg_probe()
3166 init_completion(&di->ab8500_fg_complete); in ab8500_fg_probe()
3172 destroy_workqueue(di->fg_wq); in ab8500_fg_probe()
3184 destroy_workqueue(di->fg_wq); in ab8500_fg_probe()
3191 di->irq = platform_get_irq_byname(pdev, "CCEOC"); in ab8500_fg_probe()
3192 disable_irq(di->irq); in ab8500_fg_probe()
3193 di->nbr_cceoc_irq_cnt = 0; in ab8500_fg_probe()
3200 destroy_workqueue(di->fg_wq); in ab8500_fg_probe()
3208 destroy_workqueue(di->fg_wq); in ab8500_fg_probe()
3213 di->flags.calibrate = true; in ab8500_fg_probe()
3214 di->calib_state = AB8500_FG_CALIB_INIT; in ab8500_fg_probe()
3217 di->bat_temp = 210; in ab8500_fg_probe()
3219 list_add_tail(&di->node, &ab8500_fg_list); in ab8500_fg_probe()
3228 destroy_workqueue(di->fg_wq); in ab8500_fg_remove()
3229 component_del(&pdev->dev, &ab8500_fg_component_ops); in ab8500_fg_remove()
3230 list_del(&di->node); in ab8500_fg_remove()
3238 { .compatible = "stericsson,ab8500-fg", },
3247 .name = "ab8500-fg",
3254 MODULE_ALIAS("platform:ab8500-fg");