1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
28c0984e5SSebastian Reichel #include <linux/export.h>
38c0984e5SSebastian Reichel #include <linux/power_supply.h>
48c0984e5SSebastian Reichel #include <linux/of.h>
5417c0fc2SLinus Walleij
6417c0fc2SLinus Walleij #include "ab8500-bm.h"
78c0984e5SSebastian Reichel
86252c706SLinus Walleij /* Default: under this temperature, charging is stopped */
96252c706SLinus Walleij #define AB8500_TEMP_UNDER 3
106252c706SLinus Walleij /* Default: between this temp and AB8500_TEMP_UNDER charging is reduced */
116252c706SLinus Walleij #define AB8500_TEMP_LOW 8
126252c706SLinus Walleij /* Default: between this temp and AB8500_TEMP_OVER charging is reduced */
136252c706SLinus Walleij #define AB8500_TEMP_HIGH 43
146252c706SLinus Walleij /* Default: over this temp, charging is stopped */
156252c706SLinus Walleij #define AB8500_TEMP_OVER 48
166252c706SLinus Walleij /* Default: temperature hysteresis */
176252c706SLinus Walleij #define AB8500_TEMP_HYSTERESIS 3
186252c706SLinus Walleij
190525f34dSLinus Walleij static struct power_supply_battery_ocv_table ocv_cap_tbl[] = {
200525f34dSLinus Walleij { .ocv = 4186000, .capacity = 100},
210525f34dSLinus Walleij { .ocv = 4163000, .capacity = 99},
220525f34dSLinus Walleij { .ocv = 4114000, .capacity = 95},
230525f34dSLinus Walleij { .ocv = 4068000, .capacity = 90},
240525f34dSLinus Walleij { .ocv = 3990000, .capacity = 80},
250525f34dSLinus Walleij { .ocv = 3926000, .capacity = 70},
260525f34dSLinus Walleij { .ocv = 3898000, .capacity = 65},
270525f34dSLinus Walleij { .ocv = 3866000, .capacity = 60},
280525f34dSLinus Walleij { .ocv = 3833000, .capacity = 55},
290525f34dSLinus Walleij { .ocv = 3812000, .capacity = 50},
300525f34dSLinus Walleij { .ocv = 3787000, .capacity = 40},
310525f34dSLinus Walleij { .ocv = 3768000, .capacity = 30},
320525f34dSLinus Walleij { .ocv = 3747000, .capacity = 25},
330525f34dSLinus Walleij { .ocv = 3730000, .capacity = 20},
340525f34dSLinus Walleij { .ocv = 3705000, .capacity = 15},
350525f34dSLinus Walleij { .ocv = 3699000, .capacity = 14},
360525f34dSLinus Walleij { .ocv = 3684000, .capacity = 12},
370525f34dSLinus Walleij { .ocv = 3672000, .capacity = 9},
380525f34dSLinus Walleij { .ocv = 3657000, .capacity = 7},
390525f34dSLinus Walleij { .ocv = 3638000, .capacity = 6},
400525f34dSLinus Walleij { .ocv = 3556000, .capacity = 4},
410525f34dSLinus Walleij { .ocv = 3424000, .capacity = 2},
420525f34dSLinus Walleij { .ocv = 3317000, .capacity = 1},
430525f34dSLinus Walleij { .ocv = 3094000, .capacity = 0},
448c0984e5SSebastian Reichel };
458c0984e5SSebastian Reichel
468c0984e5SSebastian Reichel /*
478c0984e5SSebastian Reichel * Note that the batres_vs_temp table must be strictly sorted by falling
4867acb291SLinus Walleij * temperature values to work. Factory resistance is 300 mOhm and the
4967acb291SLinus Walleij * resistance values to the right are percentages of 300 mOhm.
508c0984e5SSebastian Reichel */
5167acb291SLinus Walleij static struct power_supply_resistance_temp_table temp_to_batres_tbl_thermistor[] = {
5267acb291SLinus Walleij { .temp = 40, .resistance = 40 /* 120 mOhm */ },
5367acb291SLinus Walleij { .temp = 30, .resistance = 45 /* 135 mOhm */ },
5467acb291SLinus Walleij { .temp = 20, .resistance = 55 /* 165 mOhm */ },
5567acb291SLinus Walleij { .temp = 10, .resistance = 77 /* 230 mOhm */ },
5667acb291SLinus Walleij { .temp = 00, .resistance = 108 /* 325 mOhm */ },
5767acb291SLinus Walleij { .temp = -10, .resistance = 158 /* 445 mOhm */ },
5867acb291SLinus Walleij { .temp = -20, .resistance = 198 /* 595 mOhm */ },
598c0984e5SSebastian Reichel };
608c0984e5SSebastian Reichel
61d72ce7d3SLinus Walleij static struct power_supply_maintenance_charge_table ab8500_maint_charg_table[] = {
62d72ce7d3SLinus Walleij {
63d72ce7d3SLinus Walleij /* Maintenance charging phase A, 60 hours */
64d72ce7d3SLinus Walleij .charge_current_max_ua = 400000,
65d72ce7d3SLinus Walleij .charge_voltage_max_uv = 4050000,
66d72ce7d3SLinus Walleij .charge_safety_timer_minutes = 60*60,
67d72ce7d3SLinus Walleij },
68d72ce7d3SLinus Walleij {
69d72ce7d3SLinus Walleij /* Maintenance charging phase B, 200 hours */
70d72ce7d3SLinus Walleij .charge_current_max_ua = 400000,
71d72ce7d3SLinus Walleij .charge_voltage_max_uv = 4000000,
72d72ce7d3SLinus Walleij .charge_safety_timer_minutes = 200*60,
73d72ce7d3SLinus Walleij }
74d72ce7d3SLinus Walleij };
75d72ce7d3SLinus Walleij
76484a9cc3SLinus Walleij static const struct ab8500_bm_capacity_levels cap_levels = {
778c0984e5SSebastian Reichel .critical = 2,
788c0984e5SSebastian Reichel .low = 10,
798c0984e5SSebastian Reichel .normal = 70,
808c0984e5SSebastian Reichel .high = 95,
818c0984e5SSebastian Reichel .full = 100,
828c0984e5SSebastian Reichel };
838c0984e5SSebastian Reichel
84484a9cc3SLinus Walleij static const struct ab8500_fg_parameters fg = {
858c0984e5SSebastian Reichel .recovery_sleep_timer = 10,
868c0984e5SSebastian Reichel .recovery_total_time = 100,
878c0984e5SSebastian Reichel .init_timer = 1,
888c0984e5SSebastian Reichel .init_discard_time = 5,
898c0984e5SSebastian Reichel .init_total_time = 40,
908c0984e5SSebastian Reichel .high_curr_time = 60,
918c0984e5SSebastian Reichel .accu_charging = 30,
928c0984e5SSebastian Reichel .accu_high_curr = 30,
930525f34dSLinus Walleij .high_curr_threshold_ua = 50000,
940525f34dSLinus Walleij .lowbat_threshold_uv = 3100000,
958c0984e5SSebastian Reichel .battok_falling_th_sel0 = 2860,
968c0984e5SSebastian Reichel .battok_raising_th_sel1 = 2860,
978c0984e5SSebastian Reichel .maint_thres = 95,
988c0984e5SSebastian Reichel .user_cap_limit = 15,
998c0984e5SSebastian Reichel .pcut_enable = 1,
1008c0984e5SSebastian Reichel .pcut_max_time = 127,
1018c0984e5SSebastian Reichel .pcut_flag_time = 112,
1028c0984e5SSebastian Reichel .pcut_max_restart = 15,
1038c0984e5SSebastian Reichel .pcut_debounce_time = 2,
1048c0984e5SSebastian Reichel };
1058c0984e5SSebastian Reichel
106484a9cc3SLinus Walleij static const struct ab8500_maxim_parameters ab8500_maxi_params = {
1078c0984e5SSebastian Reichel .ena_maxi = true,
10883e5aa77SLinus Walleij .chg_curr_ua = 910000,
1098c0984e5SSebastian Reichel .wait_cycles = 10,
11083e5aa77SLinus Walleij .charger_curr_step_ua = 100000,
1118c0984e5SSebastian Reichel };
1128c0984e5SSebastian Reichel
113484a9cc3SLinus Walleij static const struct ab8500_bm_charger_parameters chg = {
114bc6e0287SLinus Walleij .usb_volt_max_uv = 5500000,
11583e5aa77SLinus Walleij .usb_curr_max_ua = 1500000,
116bc6e0287SLinus Walleij .ac_volt_max_uv = 7500000,
11783e5aa77SLinus Walleij .ac_curr_max_ua = 1500000,
1188c0984e5SSebastian Reichel };
1198c0984e5SSebastian Reichel
120d8d26ac1SLinus Walleij /* This is referenced directly in the charger code */
121484a9cc3SLinus Walleij struct ab8500_bm_data ab8500_bm_data = {
1228c0984e5SSebastian Reichel .main_safety_tmr_h = 4,
1238c0984e5SSebastian Reichel .temp_interval_chg = 20,
1248c0984e5SSebastian Reichel .temp_interval_nochg = 120,
1258c0984e5SSebastian Reichel .usb_safety_tmr_h = 4,
1268c0984e5SSebastian Reichel .bkup_bat_v = BUP_VCH_SEL_2P6V,
1278c0984e5SSebastian Reichel .bkup_bat_i = BUP_ICH_SEL_150UA,
1288c0984e5SSebastian Reichel .capacity_scaling = false,
1298c0984e5SSebastian Reichel .chg_unknown_bat = false,
1308c0984e5SSebastian Reichel .enable_overshoot = false,
1318c0984e5SSebastian Reichel .fg_res = 100,
1328c0984e5SSebastian Reichel .cap_levels = &cap_levels,
1338c0984e5SSebastian Reichel .interval_charging = 5,
1348c0984e5SSebastian Reichel .interval_not_charging = 120,
1358c0984e5SSebastian Reichel .maxi = &ab8500_maxi_params,
1368c0984e5SSebastian Reichel .chg_params = &chg,
1378c0984e5SSebastian Reichel .fg_params = &fg,
1388c0984e5SSebastian Reichel };
1398c0984e5SSebastian Reichel
ab8500_bm_of_probe(struct power_supply * psy,struct ab8500_bm_data * bm)14059f1b854SLinus Walleij int ab8500_bm_of_probe(struct power_supply *psy,
141484a9cc3SLinus Walleij struct ab8500_bm_data *bm)
1428c0984e5SSebastian Reichel {
14325fd3303SLinus Walleij struct power_supply_battery_info *bi;
14459f1b854SLinus Walleij struct device *dev = &psy->dev;
14559f1b854SLinus Walleij int ret;
1468c0984e5SSebastian Reichel
14725fd3303SLinus Walleij ret = power_supply_get_battery_info(psy, &bm->bi);
14859f1b854SLinus Walleij if (ret) {
14959f1b854SLinus Walleij dev_err(dev, "cannot retrieve battery info\n");
15059f1b854SLinus Walleij return ret;
1518c0984e5SSebastian Reichel }
15225fd3303SLinus Walleij bi = bm->bi;
1538c0984e5SSebastian Reichel
15422be8d77SLinus Walleij /* Fill in defaults for any data missing from the device tree */
15522be8d77SLinus Walleij if (bi->charge_full_design_uah < 0)
15622be8d77SLinus Walleij /* The default capacity is 612 mAh for unknown batteries */
15722be8d77SLinus Walleij bi->charge_full_design_uah = 612000;
1582a5f4183SLinus Walleij
1592a5f4183SLinus Walleij /*
1602a5f4183SLinus Walleij * All of these voltages need to be specified or we will simply
1612a5f4183SLinus Walleij * fall back to safe defaults.
1622a5f4183SLinus Walleij */
1632a5f4183SLinus Walleij if ((bi->voltage_min_design_uv < 0) ||
164d662a7dfSLinus Walleij (bi->voltage_max_design_uv < 0)) {
1652a5f4183SLinus Walleij /* Nominal voltage is 3.7V for unknown batteries */
1662a5f4183SLinus Walleij bi->voltage_min_design_uv = 3700000;
167d662a7dfSLinus Walleij /* Termination voltage 4.05V */
168d662a7dfSLinus Walleij bi->voltage_max_design_uv = 4050000;
1692a5f4183SLinus Walleij }
1702a5f4183SLinus Walleij
17183e5aa77SLinus Walleij if (bi->constant_charge_current_max_ua < 0)
17283e5aa77SLinus Walleij bi->constant_charge_current_max_ua = 400000;
17383e5aa77SLinus Walleij
174bc6e0287SLinus Walleij if (bi->constant_charge_voltage_max_uv < 0)
175bc6e0287SLinus Walleij bi->constant_charge_voltage_max_uv = 4100000;
176bc6e0287SLinus Walleij
1779c20899dSLinus Walleij if (bi->charge_term_current_ua)
1789c20899dSLinus Walleij /* Charging stops when we drop below this current */
1799c20899dSLinus Walleij bi->charge_term_current_ua = 200000;
1809c20899dSLinus Walleij
181d72ce7d3SLinus Walleij if (!bi->maintenance_charge || !bi->maintenance_charge_size) {
182d72ce7d3SLinus Walleij bi->maintenance_charge = ab8500_maint_charg_table;
183d72ce7d3SLinus Walleij bi->maintenance_charge_size = ARRAY_SIZE(ab8500_maint_charg_table);
184d72ce7d3SLinus Walleij }
185d72ce7d3SLinus Walleij
1860e8b903bSLinus Walleij if (bi->alert_low_temp_charge_current_ua < 0 ||
1870e8b903bSLinus Walleij bi->alert_low_temp_charge_voltage_uv < 0)
1880e8b903bSLinus Walleij {
1890e8b903bSLinus Walleij bi->alert_low_temp_charge_current_ua = 300000;
1900e8b903bSLinus Walleij bi->alert_low_temp_charge_voltage_uv = 4000000;
1910e8b903bSLinus Walleij }
1920e8b903bSLinus Walleij if (bi->alert_high_temp_charge_current_ua < 0 ||
1930e8b903bSLinus Walleij bi->alert_high_temp_charge_voltage_uv < 0)
1940e8b903bSLinus Walleij {
1950e8b903bSLinus Walleij bi->alert_high_temp_charge_current_ua = 300000;
1960e8b903bSLinus Walleij bi->alert_high_temp_charge_voltage_uv = 4000000;
1970e8b903bSLinus Walleij }
1980e8b903bSLinus Walleij
19967acb291SLinus Walleij /*
20067acb291SLinus Walleij * Internal resistance and factory resistance are tightly coupled
20167acb291SLinus Walleij * so both MUST be defined or we fall back to defaults.
20267acb291SLinus Walleij */
20367acb291SLinus Walleij if ((bi->factory_internal_resistance_uohm < 0) ||
20467acb291SLinus Walleij !bi->resist_table) {
20550425ccfSLinus Walleij bi->factory_internal_resistance_uohm = 300000;
20667acb291SLinus Walleij bi->resist_table = temp_to_batres_tbl_thermistor;
20767acb291SLinus Walleij bi->resist_table_size = ARRAY_SIZE(temp_to_batres_tbl_thermistor);
20867acb291SLinus Walleij }
20950425ccfSLinus Walleij
210*1f918e0fSLinus Walleij /* The default battery is emulated by a resistor at 7K */
211*1f918e0fSLinus Walleij if (bi->bti_resistance_ohm < 0 ||
212*1f918e0fSLinus Walleij bi->bti_resistance_tolerance < 0) {
213*1f918e0fSLinus Walleij bi->bti_resistance_ohm = 7000;
214*1f918e0fSLinus Walleij bi->bti_resistance_tolerance = 20;
215*1f918e0fSLinus Walleij }
216*1f918e0fSLinus Walleij
2170525f34dSLinus Walleij if (!bi->ocv_table[0]) {
2180525f34dSLinus Walleij /* Default capacity table at say 25 degrees Celsius */
2190525f34dSLinus Walleij bi->ocv_temp[0] = 25;
2200525f34dSLinus Walleij bi->ocv_table[0] = ocv_cap_tbl;
2210525f34dSLinus Walleij bi->ocv_table_size[0] = ARRAY_SIZE(ocv_cap_tbl);
2220525f34dSLinus Walleij }
2230525f34dSLinus Walleij
2246252c706SLinus Walleij if (bi->temp_min == INT_MIN)
2256252c706SLinus Walleij bi->temp_min = AB8500_TEMP_UNDER;
2266252c706SLinus Walleij if (bi->temp_max == INT_MAX)
2276252c706SLinus Walleij bi->temp_max = AB8500_TEMP_OVER;
2286252c706SLinus Walleij if (bi->temp_alert_min == INT_MIN)
2296252c706SLinus Walleij bi->temp_alert_min = AB8500_TEMP_LOW;
2306252c706SLinus Walleij if (bi->temp_alert_max == INT_MAX)
2316252c706SLinus Walleij bi->temp_alert_max = AB8500_TEMP_HIGH;
2326252c706SLinus Walleij bm->temp_hysteresis = AB8500_TEMP_HYSTERESIS;
2336252c706SLinus Walleij
2348c0984e5SSebastian Reichel return 0;
2358c0984e5SSebastian Reichel }
2366252c706SLinus Walleij
ab8500_bm_of_remove(struct power_supply * psy,struct ab8500_bm_data * bm)2376252c706SLinus Walleij void ab8500_bm_of_remove(struct power_supply *psy,
2386252c706SLinus Walleij struct ab8500_bm_data *bm)
2396252c706SLinus Walleij {
24025fd3303SLinus Walleij power_supply_put_battery_info(psy, bm->bi);
2416252c706SLinus Walleij }
242