14 #include <linux/module.h>
15 #include <linux/device.h>
18 #include <linux/slab.h>
29 #define CHG_WD_INTERVAL (6 * HZ)
32 #define EOC_COND_CNT 10
35 #define RCH_COND_CNT 3
37 #define to_abx500_chargalg_device_info(x) container_of((x), \
38 struct abx500_chargalg, chargalg_psy);
109 static const char *states[] = {
114 "HW_TEMP_PROTECT_INIT",
118 "WAIT_FOR_RECHARGE_INIT",
120 "MAINTENANCE_A_INIT",
122 "MAINTENANCE_B_INIT",
124 "TEMP_UNDEROVER_INIT",
132 "SAFETY_TIMER_EXPIRED_INIT",
133 "SAFETY_TIMER_EXPIRED",
262 static void abx500_chargalg_safety_timer_expired(
unsigned long data)
266 di->
events.safety_timer_expired =
true;
280 static void abx500_chargalg_maintenance_timer_expired(
unsigned long data)
284 dev_dbg(di->
dev,
"Maintenance timer expired\n");
285 di->
events.maintenance_timer_expired =
true;
301 "State changed: %s (From state: [%d] %s =to=> [%d] %s )\n",
318 static int abx500_chargalg_check_charger_connection(
struct abx500_chargalg *di)
362 static void abx500_chargalg_start_safety_timer(
struct abx500_chargalg *di)
364 unsigned long timer_expiration = 0;
366 switch (di->
chg_info.charger_type) {
370 (di->
bat->main_safety_tmr_h * 3600 *
HZ));
376 (di->
bat->usb_safety_tmr_h * 3600 *
HZ));
380 dev_err(di->
dev,
"Unknown charger to charge from\n");
384 di->
events.safety_timer_expired =
false;
398 static void abx500_chargalg_stop_safety_timer(
struct abx500_chargalg *di)
400 di->
events.safety_timer_expired =
false;
413 static void abx500_chargalg_start_maintenance_timer(
struct abx500_chargalg *di,
416 unsigned long timer_expiration;
421 di->
events.maintenance_timer_expired =
false;
436 static void abx500_chargalg_stop_maintenance_timer(
struct abx500_chargalg *di)
438 di->
events.maintenance_timer_expired =
false;
479 if (di->
ac_chg->max_out_volt)
480 vset =
min(vset, di->
ac_chg->max_out_volt);
481 if (di->
ac_chg->max_out_curr)
482 iset =
min(iset, di->
ac_chg->max_out_curr);
487 return di->
ac_chg->ops.enable(di->
ac_chg, enable, vset, iset);
500 static int abx500_chargalg_usb_en(
struct abx500_chargalg *di,
int enable,
536 if (di->
ac_chg->max_out_curr)
537 iset =
min(iset, di->
ac_chg->max_out_curr);
569 abx500_chargalg_ac_en(di,
false, 0, 0);
570 abx500_chargalg_usb_en(di,
false, 0, 0);
571 abx500_chargalg_stop_safety_timer(di);
572 abx500_chargalg_stop_maintenance_timer(di);
589 abx500_chargalg_ac_en(di,
false, 0, 0);
590 abx500_chargalg_usb_en(di,
false, 0, 0);
591 abx500_chargalg_stop_safety_timer(di);
592 abx500_chargalg_stop_maintenance_timer(di);
611 switch (di->
chg_info.charger_type) {
614 "AC parameters: Vset %d, Ich %d\n", vset, iset);
615 abx500_chargalg_usb_en(di,
false, 0, 0);
616 abx500_chargalg_ac_en(di,
true, vset, iset);
621 "USB parameters: Vset %d, Ich %d\n", vset, iset);
622 abx500_chargalg_ac_en(di,
false, 0, 0);
623 abx500_chargalg_usb_en(di,
true, vset, iset);
627 dev_err(di->
dev,
"Unknown charger to charge from\n");
644 di->
events.btemp_underover =
false;
645 di->
events.btemp_lowhigh =
false;
656 di->
events.btemp_underover =
false;
657 di->
events.btemp_lowhigh =
true;
663 di->
events.btemp_underover =
true;
664 di->
events.btemp_lowhigh =
false;
669 dev_dbg(di->
dev,
"Within hysteresis limit temp: %d "
670 "hyst_lowhigh %d, hyst normal %d\n",
683 static void abx500_chargalg_check_charger_voltage(
struct abx500_chargalg *di)
685 if (di->
chg_info.usb_volt > di->
bat->chg_params->usb_volt_max)
690 if (di->
chg_info.ac_volt > di->
bat->chg_params->ac_volt_max)
710 di->
bat->bat_type[di->
bat->batt_id].termination_vol ||
713 di->
bat->bat_type[di->
bat->batt_id].termination_curr &&
723 " EOC limit reached for the %d"
724 " time, out of %d before EOC\n",
735 di->
ccm.original_iset =
736 di->
bat->bat_type[di->
bat->batt_id].normal_cur_lvl;
737 di->
ccm.current_iset =
738 di->
bat->bat_type[di->
bat->batt_id].normal_cur_lvl;
739 di->
ccm.test_delta_i = di->
bat->maxi->charger_curr_step;
740 di->
ccm.max_current = di->
bat->maxi->chg_curr;
741 di->
ccm.condition_cnt = di->
bat->maxi->wait_cycles;
758 if (!di->
bat->maxi->ena_maxi)
761 delta_i = di->
ccm.original_iset - di->
batt_data.inst_curr;
763 if (di->
events.vbus_collapsed) {
764 dev_dbg(di->
dev,
"Charger voltage has collapsed %d\n",
766 if (di->
ccm.wait_cnt == 0) {
769 di->
ccm.condition_cnt = di->
bat->maxi->wait_cycles;
770 di->
ccm.max_current =
771 di->
ccm.current_iset - di->
ccm.test_delta_i;
772 di->
ccm.current_iset = di->
ccm.max_current;
778 di->
ccm.wait_cnt = (di->
ccm.wait_cnt + 1) % 3;
783 di->
ccm.wait_cnt = 0;
785 if ((di->
batt_data.inst_curr > di->
ccm.original_iset)) {
786 dev_dbg(di->
dev,
" Maximization Ibat (%dmA) too high"
787 " (limit %dmA) (current iset: %dmA)!\n",
789 di->
ccm.current_iset);
791 if (di->
ccm.current_iset == di->
ccm.original_iset)
794 di->
ccm.condition_cnt = di->
bat->maxi->wait_cycles;
795 di->
ccm.current_iset = di->
ccm.original_iset;
801 if (delta_i > di->
ccm.test_delta_i &&
802 (di->
ccm.current_iset + di->
ccm.test_delta_i) <
803 di->
ccm.max_current) {
804 if (di->
ccm.condition_cnt-- == 0) {
806 di->
ccm.condition_cnt = di->
bat->maxi->wait_cycles;
807 di->
ccm.current_iset += di->
ccm.test_delta_i;
809 dev_dbg(di->
dev,
" Maximization needed, increase"
810 " with %d mA to %dmA (Optimal ibat: %d)"
812 di->
ccm.test_delta_i,
813 di->
ccm.current_iset,
814 di->
ccm.original_iset,
821 di->
ccm.condition_cnt = di->
bat->maxi->wait_cycles;
831 ret = abx500_chargalg_chg_curr_maxim(di);
834 result = abx500_chargalg_update_chg_curr(di,
835 di->
ccm.current_iset);
840 result = abx500_chargalg_update_chg_curr(di,
841 di->
bat->bat_type[di->
bat->batt_id].normal_cur_lvl);
853 static int abx500_chargalg_get_ext_psy_data(
struct device *
dev,
void *
data)
860 bool psy_found =
false;
894 di->
events.batt_rem =
false;
897 di->
events.batt_rem =
true;
908 else if (ret.intval &&
924 else if (ret.intval &&
949 else if (ret.intval &&
967 else if (ret.intval &&
986 switch (ret.intval) {
988 di->
events.mainextchnotok =
true;
989 di->
events.main_thermal_prot =
false;
990 di->
events.main_ovv =
false;
991 di->
events.ac_wd_expired =
false;
994 di->
events.ac_wd_expired =
true;
995 di->
events.mainextchnotok =
false;
996 di->
events.main_ovv =
false;
997 di->
events.main_thermal_prot =
false;
1001 di->
events.main_thermal_prot =
true;
1002 di->
events.mainextchnotok =
false;
1003 di->
events.main_ovv =
false;
1004 di->
events.ac_wd_expired =
false;
1007 di->
events.main_ovv =
true;
1008 di->
events.mainextchnotok =
false;
1009 di->
events.main_thermal_prot =
false;
1010 di->
events.ac_wd_expired =
false;
1013 di->
events.main_thermal_prot =
false;
1014 di->
events.mainextchnotok =
false;
1015 di->
events.main_ovv =
false;
1016 di->
events.ac_wd_expired =
false;
1024 switch (ret.intval) {
1026 di->
events.usbchargernotok =
true;
1027 di->
events.usb_thermal_prot =
false;
1028 di->
events.vbus_ovv =
false;
1029 di->
events.usb_wd_expired =
false;
1032 di->
events.usb_wd_expired =
true;
1033 di->
events.usbchargernotok =
false;
1034 di->
events.usb_thermal_prot =
false;
1035 di->
events.vbus_ovv =
false;
1039 di->
events.usb_thermal_prot =
true;
1040 di->
events.usbchargernotok =
false;
1041 di->
events.vbus_ovv =
false;
1042 di->
events.usb_wd_expired =
false;
1045 di->
events.vbus_ovv =
true;
1046 di->
events.usbchargernotok =
false;
1047 di->
events.usb_thermal_prot =
false;
1048 di->
events.usb_wd_expired =
false;
1051 di->
events.usbchargernotok =
false;
1052 di->
events.usb_thermal_prot =
false;
1053 di->
events.vbus_ovv =
false;
1054 di->
events.usb_wd_expired =
false;
1065 switch (ext->
type) {
1070 di->
chg_info.ac_volt = ret.intval / 1000;
1073 di->
chg_info.usb_volt = ret.intval / 1000;
1081 switch (ext->
type) {
1086 di->
events.ac_cv_active =
true;
1088 di->
events.ac_cv_active =
false;
1095 di->
events.usb_cv_active =
true;
1097 di->
events.usb_cv_active =
false;
1106 switch (ext->
type) {
1109 di->
events.batt_unknown =
false;
1111 di->
events.batt_unknown =
true;
1124 switch (ext->
type) {
1134 di->
batt_data.inst_curr = ret.intval / 1000;
1142 switch (ext->
type) {
1144 di->
batt_data.avg_curr = ret.intval / 1000;
1148 di->
events.vbus_collapsed =
true;
1150 di->
events.vbus_collapsed =
false;
1175 static void abx500_chargalg_external_power_changed(
struct power_supply *psy)
1202 abx500_chargalg_end_of_charge(di);
1203 abx500_chargalg_check_temp(di);
1204 abx500_chargalg_check_charger_voltage(di);
1206 charger_status = abx500_chargalg_check_charger_connection(di);
1212 if (!charger_status ||
1213 (di->
events.batt_unknown && !di->
bat->chg_unknown_bat)) {
1215 di->
events.safety_timer_expired =
false;
1227 else if (di->
events.safety_timer_expired) {
1229 abx500_chargalg_state_to(di,
1238 else if (di->
events.batt_rem) {
1243 else if (di->
events.mainextchnotok || di->
events.usbchargernotok) {
1249 !di->
events.vbus_collapsed)
1253 else if (di->
events.vbus_ovv ||
1262 else if (di->
events.main_thermal_prot ||
1263 di->
events.usb_thermal_prot) {
1265 abx500_chargalg_state_to(di,
1269 else if (di->
events.btemp_underover) {
1271 abx500_chargalg_state_to(di,
1275 else if (di->
events.ac_wd_expired ||
1276 di->
events.usb_wd_expired) {
1281 else if (di->
events.btemp_lowhigh) {
1287 "[CHARGALG] Vb %d Ib_avg %d Ib_inst %d Tb %d Cap %d Maint %d "
1288 "State %s Active_chg %d Chg_status %d AC %d USB %d "
1289 "AC_online %d USB_online %d AC_CV %d USB_CV %d AC_I %d "
1290 "USB_I %d AC_Vset %d AC_Iset %d USB_Vset %d USB_Iset %d\n",
1305 di->
events.usb_cv_active,
1315 abx500_chargalg_stop_charging(di);
1325 abx500_chargalg_ac_en(di,
false, 0, 0);
1327 abx500_chargalg_usb_en(di,
false, 0, 0);
1328 abx500_chargalg_stop_safety_timer(di);
1329 abx500_chargalg_stop_maintenance_timer(di);
1341 abx500_chargalg_stop_charging(di);
1346 if (!di->
events.batt_rem)
1351 abx500_chargalg_stop_charging(di);
1356 if (!di->
events.main_thermal_prot &&
1357 !di->
events.usb_thermal_prot)
1362 abx500_chargalg_stop_charging(di);
1367 if (!di->
events.vbus_ovv &&
1376 abx500_chargalg_stop_charging(di);
1381 if (!di->
events.mainextchnotok &&
1382 !di->
events.usbchargernotok)
1387 abx500_chargalg_stop_charging(di);
1396 abx500_chargalg_start_charging(di,
1397 di->
bat->bat_type[di->
bat->batt_id].normal_vol_lvl,
1398 di->
bat->bat_type[di->
bat->batt_id].normal_cur_lvl);
1400 abx500_chargalg_start_safety_timer(di);
1401 abx500_chargalg_stop_maintenance_timer(di);
1402 init_maxim_chg_curr(di);
1411 handle_maxim_chg_curr(di);
1414 if (di->
bat->no_maintenance)
1415 abx500_chargalg_state_to(di,
1418 abx500_chargalg_state_to(di,
1425 abx500_chargalg_hold_charging(di);
1432 di->
bat->bat_type[di->
bat->batt_id].recharge_vol) {
1440 abx500_chargalg_stop_safety_timer(di);
1441 abx500_chargalg_start_maintenance_timer(di,
1443 di->
bat->batt_id].maint_a_chg_timer_h);
1444 abx500_chargalg_start_charging(di,
1446 di->
bat->batt_id].maint_a_vol_lvl,
1448 di->
bat->batt_id].maint_a_cur_lvl);
1454 if (di->
events.maintenance_timer_expired) {
1455 abx500_chargalg_stop_maintenance_timer(di);
1461 abx500_chargalg_start_maintenance_timer(di,
1463 di->
bat->batt_id].maint_b_chg_timer_h);
1464 abx500_chargalg_start_charging(di,
1466 di->
bat->batt_id].maint_b_vol_lvl,
1468 di->
bat->batt_id].maint_b_cur_lvl);
1474 if (di->
events.maintenance_timer_expired) {
1475 abx500_chargalg_stop_maintenance_timer(di);
1481 abx500_chargalg_start_charging(di,
1483 di->
bat->batt_id].low_high_vol_lvl,
1485 di->
bat->batt_id].low_high_cur_lvl);
1486 abx500_chargalg_stop_maintenance_timer(di);
1493 if (!di->
events.btemp_lowhigh)
1498 abx500_chargalg_stop_charging(di);
1503 if (!di->
events.ac_wd_expired &&
1504 !di->
events.usb_wd_expired)
1509 abx500_chargalg_stop_charging(di);
1514 if (!di->
events.btemp_underover)
1537 abx500_chargalg_algorithm(di);
1546 di->
bat->interval_charging *
HZ);
1550 di->
bat->interval_not_charging *
HZ);
1559 static void abx500_chargalg_wd_work(
struct work_struct *work)
1565 dev_dbg(di->
dev,
"abx500_chargalg_wd_work\n");
1567 ret = abx500_chargalg_kick_watchdog(di);
1569 dev_err(di->
dev,
"failed to kick watchdog\n");
1581 static void abx500_chargalg_work(
struct work_struct *work)
1586 abx500_chargalg_algorithm(di);
1601 static int abx500_chargalg_get_property(
struct power_supply *psy,
1614 if (di->
events.batt_ovv) {
1616 }
else if (di->
events.btemp_underover) {
1644 static ssize_t abx500_chargalg_sysfs_charger(
struct kobject *kobj,
1689 "Enter 0. Disable AC/USB Charging\n"
1690 "1. Enable AC charging\n"
1691 "2. Enable USB Charging\n");
1698 static struct attribute abx500_chargalg_en_charger = \
1704 static struct attribute *abx500_chargalg_chg[] = {
1705 &abx500_chargalg_en_charger,
1709 static const struct sysfs_ops abx500_chargalg_sysfs_ops = {
1710 .store = abx500_chargalg_sysfs_charger,
1713 static struct kobj_type abx500_chargalg_ktype = {
1714 .sysfs_ops = &abx500_chargalg_sysfs_ops,
1715 .default_attrs = abx500_chargalg_chg,
1741 &abx500_chargalg_ktype,
1742 NULL,
"abx500_chargalg");
1744 dev_err(di->
dev,
"failed to create sysfs entry\n");
1750 #if defined(CONFIG_PM)
1781 #define abx500_chargalg_suspend NULL
1782 #define abx500_chargalg_resume NULL
1790 abx500_chargalg_sysfs_exit(di);
1797 platform_set_drvdata(pdev,
NULL);
1816 plat_data = pdev->
dev.platform_data;
1825 di->
chargalg_psy.get_property = abx500_chargalg_get_property;
1829 abx500_chargalg_external_power_changed;
1833 di->
safety_timer.function = abx500_chargalg_safety_timer_expired;
1839 abx500_chargalg_maintenance_timer_expired;
1846 dev_err(di->
dev,
"failed to create work queue\n");
1847 goto free_device_info;
1852 abx500_chargalg_periodic_work);
1854 abx500_chargalg_wd_work);
1865 dev_err(di->
dev,
"failed to register chargalg psy\n");
1866 goto free_chargalg_wq;
1869 platform_set_drvdata(pdev, di);
1872 ret = abx500_chargalg_sysfs_init(di);
1874 dev_err(di->
dev,
"failed to create sysfs entry\n");
1895 .probe = abx500_chargalg_probe,
1900 .name =
"abx500-chargalg",
1905 static int __init abx500_chargalg_init(
void)
1910 static void __exit abx500_chargalg_exit(
void)