Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Rajanikanth H.V | e0f1abe | 2012-11-18 18:45:41 -0800 | [diff] [blame] | 2 | #include <linux/export.h> |
| 3 | #include <linux/power_supply.h> |
| 4 | #include <linux/of.h> |
Linus Walleij | 417c0fc | 2021-03-12 09:36:02 +0100 | [diff] [blame] | 5 | |
| 6 | #include "ab8500-bm.h" |
Rajanikanth H.V | e0f1abe | 2012-11-18 18:45:41 -0800 | [diff] [blame] | 7 | |
Linus Walleij | 6252c70 | 2021-11-20 16:53:13 +0100 | [diff] [blame] | 8 | /* Default: under this temperature, charging is stopped */ |
| 9 | #define AB8500_TEMP_UNDER 3 |
| 10 | /* Default: between this temp and AB8500_TEMP_UNDER charging is reduced */ |
| 11 | #define AB8500_TEMP_LOW 8 |
| 12 | /* Default: between this temp and AB8500_TEMP_OVER charging is reduced */ |
| 13 | #define AB8500_TEMP_HIGH 43 |
| 14 | /* Default: over this temp, charging is stopped */ |
| 15 | #define AB8500_TEMP_OVER 48 |
| 16 | /* Default: temperature hysteresis */ |
| 17 | #define AB8500_TEMP_HYSTERESIS 3 |
| 18 | |
Linus Walleij | 0525f34 | 2021-11-20 16:53:26 +0100 | [diff] [blame] | 19 | static struct power_supply_battery_ocv_table ocv_cap_tbl[] = { |
| 20 | { .ocv = 4186000, .capacity = 100}, |
| 21 | { .ocv = 4163000, .capacity = 99}, |
| 22 | { .ocv = 4114000, .capacity = 95}, |
| 23 | { .ocv = 4068000, .capacity = 90}, |
| 24 | { .ocv = 3990000, .capacity = 80}, |
| 25 | { .ocv = 3926000, .capacity = 70}, |
| 26 | { .ocv = 3898000, .capacity = 65}, |
| 27 | { .ocv = 3866000, .capacity = 60}, |
| 28 | { .ocv = 3833000, .capacity = 55}, |
| 29 | { .ocv = 3812000, .capacity = 50}, |
| 30 | { .ocv = 3787000, .capacity = 40}, |
| 31 | { .ocv = 3768000, .capacity = 30}, |
| 32 | { .ocv = 3747000, .capacity = 25}, |
| 33 | { .ocv = 3730000, .capacity = 20}, |
| 34 | { .ocv = 3705000, .capacity = 15}, |
| 35 | { .ocv = 3699000, .capacity = 14}, |
| 36 | { .ocv = 3684000, .capacity = 12}, |
| 37 | { .ocv = 3672000, .capacity = 9}, |
| 38 | { .ocv = 3657000, .capacity = 7}, |
| 39 | { .ocv = 3638000, .capacity = 6}, |
| 40 | { .ocv = 3556000, .capacity = 4}, |
| 41 | { .ocv = 3424000, .capacity = 2}, |
| 42 | { .ocv = 3317000, .capacity = 1}, |
| 43 | { .ocv = 3094000, .capacity = 0}, |
Rajanikanth H.V | e0f1abe | 2012-11-18 18:45:41 -0800 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | /* |
| 47 | * Note that the res_to_temp table must be strictly sorted by falling |
| 48 | * resistance values to work. |
| 49 | */ |
Linus Walleij | 484a9cc | 2021-07-13 17:27:07 +0200 | [diff] [blame] | 50 | static const struct ab8500_res_to_temp temp_tbl[] = { |
Rajanikanth H.V | e0f1abe | 2012-11-18 18:45:41 -0800 | [diff] [blame] | 51 | {-5, 214834}, |
| 52 | { 0, 162943}, |
| 53 | { 5, 124820}, |
| 54 | {10, 96520}, |
| 55 | {15, 75306}, |
| 56 | {20, 59254}, |
| 57 | {25, 47000}, |
| 58 | {30, 37566}, |
| 59 | {35, 30245}, |
| 60 | {40, 24520}, |
| 61 | {45, 20010}, |
| 62 | {50, 16432}, |
| 63 | {55, 13576}, |
| 64 | {60, 11280}, |
| 65 | {65, 9425}, |
| 66 | }; |
| 67 | |
| 68 | /* |
| 69 | * Note that the batres_vs_temp table must be strictly sorted by falling |
Linus Walleij | 67acb29 | 2021-11-20 16:53:25 +0100 | [diff] [blame] | 70 | * temperature values to work. Factory resistance is 300 mOhm and the |
| 71 | * resistance values to the right are percentages of 300 mOhm. |
Rajanikanth H.V | e0f1abe | 2012-11-18 18:45:41 -0800 | [diff] [blame] | 72 | */ |
Linus Walleij | 67acb29 | 2021-11-20 16:53:25 +0100 | [diff] [blame] | 73 | static struct power_supply_resistance_temp_table temp_to_batres_tbl_thermistor[] = { |
| 74 | { .temp = 40, .resistance = 40 /* 120 mOhm */ }, |
| 75 | { .temp = 30, .resistance = 45 /* 135 mOhm */ }, |
| 76 | { .temp = 20, .resistance = 55 /* 165 mOhm */ }, |
| 77 | { .temp = 10, .resistance = 77 /* 230 mOhm */ }, |
| 78 | { .temp = 00, .resistance = 108 /* 325 mOhm */ }, |
| 79 | { .temp = -10, .resistance = 158 /* 445 mOhm */ }, |
| 80 | { .temp = -20, .resistance = 198 /* 595 mOhm */ }, |
Rajanikanth H.V | e0f1abe | 2012-11-18 18:45:41 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
Linus Walleij | e5dff30 | 2021-11-20 16:53:15 +0100 | [diff] [blame] | 83 | /* Default battery type for reference designs is the unknown type */ |
| 84 | static struct ab8500_battery_type bat_type_thermistor_unknown = { |
Linus Walleij | e5dff30 | 2021-11-20 16:53:15 +0100 | [diff] [blame] | 85 | .resis_high = 0, |
| 86 | .resis_low = 0, |
Linus Walleij | e5dff30 | 2021-11-20 16:53:15 +0100 | [diff] [blame] | 87 | .maint_a_cur_lvl = 400, |
| 88 | .maint_a_vol_lvl = 4050, |
| 89 | .maint_a_chg_timer_h = 60, |
| 90 | .maint_b_cur_lvl = 400, |
| 91 | .maint_b_vol_lvl = 4000, |
| 92 | .maint_b_chg_timer_h = 200, |
| 93 | .low_high_cur_lvl = 300, |
| 94 | .low_high_vol_lvl = 4000, |
| 95 | .n_temp_tbl_elements = ARRAY_SIZE(temp_tbl), |
| 96 | .r_to_t_tbl = temp_tbl, |
Rajanikanth H.V | e0f1abe | 2012-11-18 18:45:41 -0800 | [diff] [blame] | 97 | }; |
| 98 | |
Linus Walleij | 484a9cc | 2021-07-13 17:27:07 +0200 | [diff] [blame] | 99 | static const struct ab8500_bm_capacity_levels cap_levels = { |
Rajanikanth H.V | e0f1abe | 2012-11-18 18:45:41 -0800 | [diff] [blame] | 100 | .critical = 2, |
| 101 | .low = 10, |
| 102 | .normal = 70, |
| 103 | .high = 95, |
| 104 | .full = 100, |
| 105 | }; |
| 106 | |
Linus Walleij | 484a9cc | 2021-07-13 17:27:07 +0200 | [diff] [blame] | 107 | static const struct ab8500_fg_parameters fg = { |
Rajanikanth H.V | e0f1abe | 2012-11-18 18:45:41 -0800 | [diff] [blame] | 108 | .recovery_sleep_timer = 10, |
| 109 | .recovery_total_time = 100, |
| 110 | .init_timer = 1, |
| 111 | .init_discard_time = 5, |
| 112 | .init_total_time = 40, |
| 113 | .high_curr_time = 60, |
| 114 | .accu_charging = 30, |
| 115 | .accu_high_curr = 30, |
Linus Walleij | 0525f34 | 2021-11-20 16:53:26 +0100 | [diff] [blame] | 116 | .high_curr_threshold_ua = 50000, |
| 117 | .lowbat_threshold_uv = 3100000, |
Rajanikanth H.V | e0f1abe | 2012-11-18 18:45:41 -0800 | [diff] [blame] | 118 | .battok_falling_th_sel0 = 2860, |
| 119 | .battok_raising_th_sel1 = 2860, |
Marcus Cooper | ea40240 | 2013-01-11 13:12:54 +0000 | [diff] [blame] | 120 | .maint_thres = 95, |
Rajanikanth H.V | e0f1abe | 2012-11-18 18:45:41 -0800 | [diff] [blame] | 121 | .user_cap_limit = 15, |
Lee Jones | 93ff722 | 2012-05-31 16:16:36 +0200 | [diff] [blame] | 122 | .pcut_enable = 1, |
| 123 | .pcut_max_time = 127, |
| 124 | .pcut_flag_time = 112, |
| 125 | .pcut_max_restart = 15, |
| 126 | .pcut_debounce_time = 2, |
Rajanikanth H.V | e0f1abe | 2012-11-18 18:45:41 -0800 | [diff] [blame] | 127 | }; |
| 128 | |
Linus Walleij | 484a9cc | 2021-07-13 17:27:07 +0200 | [diff] [blame] | 129 | static const struct ab8500_maxim_parameters ab8500_maxi_params = { |
Rajanikanth H.V | e0f1abe | 2012-11-18 18:45:41 -0800 | [diff] [blame] | 130 | .ena_maxi = true, |
Linus Walleij | 83e5aa7 | 2021-11-20 16:53:23 +0100 | [diff] [blame] | 131 | .chg_curr_ua = 910000, |
Rajanikanth H.V | e0f1abe | 2012-11-18 18:45:41 -0800 | [diff] [blame] | 132 | .wait_cycles = 10, |
Linus Walleij | 83e5aa7 | 2021-11-20 16:53:23 +0100 | [diff] [blame] | 133 | .charger_curr_step_ua = 100000, |
Rajanikanth H.V | e0f1abe | 2012-11-18 18:45:41 -0800 | [diff] [blame] | 134 | }; |
| 135 | |
Linus Walleij | 484a9cc | 2021-07-13 17:27:07 +0200 | [diff] [blame] | 136 | static const struct ab8500_bm_charger_parameters chg = { |
Linus Walleij | bc6e028 | 2021-11-20 16:53:24 +0100 | [diff] [blame] | 137 | .usb_volt_max_uv = 5500000, |
Linus Walleij | 83e5aa7 | 2021-11-20 16:53:23 +0100 | [diff] [blame] | 138 | .usb_curr_max_ua = 1500000, |
Linus Walleij | bc6e028 | 2021-11-20 16:53:24 +0100 | [diff] [blame] | 139 | .ac_volt_max_uv = 7500000, |
Linus Walleij | 83e5aa7 | 2021-11-20 16:53:23 +0100 | [diff] [blame] | 140 | .ac_curr_max_ua = 1500000, |
Rajanikanth H.V | e0f1abe | 2012-11-18 18:45:41 -0800 | [diff] [blame] | 141 | }; |
| 142 | |
Linus Walleij | d8d26ac | 2021-11-20 16:53:14 +0100 | [diff] [blame] | 143 | /* This is referenced directly in the charger code */ |
Linus Walleij | 484a9cc | 2021-07-13 17:27:07 +0200 | [diff] [blame] | 144 | struct ab8500_bm_data ab8500_bm_data = { |
Lee Jones | 43dc447 | 2012-11-29 15:26:22 +0000 | [diff] [blame] | 145 | .main_safety_tmr_h = 4, |
| 146 | .temp_interval_chg = 20, |
| 147 | .temp_interval_nochg = 120, |
| 148 | .usb_safety_tmr_h = 4, |
| 149 | .bkup_bat_v = BUP_VCH_SEL_2P6V, |
| 150 | .bkup_bat_i = BUP_ICH_SEL_150UA, |
| 151 | .no_maintenance = false, |
Marcus Cooper | ea40240 | 2013-01-11 13:12:54 +0000 | [diff] [blame] | 152 | .capacity_scaling = false, |
Linus Walleij | 484a9cc | 2021-07-13 17:27:07 +0200 | [diff] [blame] | 153 | .adc_therm = AB8500_ADC_THERM_BATCTRL, |
Lee Jones | 43dc447 | 2012-11-29 15:26:22 +0000 | [diff] [blame] | 154 | .chg_unknown_bat = false, |
| 155 | .enable_overshoot = false, |
| 156 | .fg_res = 100, |
| 157 | .cap_levels = &cap_levels, |
Linus Walleij | e5dff30 | 2021-11-20 16:53:15 +0100 | [diff] [blame] | 158 | .bat_type = &bat_type_thermistor_unknown, |
Lee Jones | 43dc447 | 2012-11-29 15:26:22 +0000 | [diff] [blame] | 159 | .interval_charging = 5, |
| 160 | .interval_not_charging = 120, |
Lee Jones | 43dc447 | 2012-11-29 15:26:22 +0000 | [diff] [blame] | 161 | .gnd_lift_resistance = 34, |
Lee Jones | 861a30d | 2012-08-29 20:36:51 +0800 | [diff] [blame] | 162 | .maxi = &ab8500_maxi_params, |
Lee Jones | 43dc447 | 2012-11-29 15:26:22 +0000 | [diff] [blame] | 163 | .chg_params = &chg, |
| 164 | .fg_params = &fg, |
Lee Jones | 861a30d | 2012-08-29 20:36:51 +0800 | [diff] [blame] | 165 | }; |
| 166 | |
Linus Walleij | 59f1b85 | 2021-11-20 16:53:11 +0100 | [diff] [blame] | 167 | int ab8500_bm_of_probe(struct power_supply *psy, |
Linus Walleij | 484a9cc | 2021-07-13 17:27:07 +0200 | [diff] [blame] | 168 | struct ab8500_bm_data *bm) |
Rajanikanth H.V | e0f1abe | 2012-11-18 18:45:41 -0800 | [diff] [blame] | 169 | { |
Linus Walleij | 25fd330 | 2021-12-15 02:01:18 +0100 | [diff] [blame] | 170 | struct power_supply_battery_info *bi; |
Linus Walleij | 59f1b85 | 2021-11-20 16:53:11 +0100 | [diff] [blame] | 171 | struct device *dev = &psy->dev; |
| 172 | int ret; |
Rajanikanth H.V | e0f1abe | 2012-11-18 18:45:41 -0800 | [diff] [blame] | 173 | |
Linus Walleij | 25fd330 | 2021-12-15 02:01:18 +0100 | [diff] [blame] | 174 | ret = power_supply_get_battery_info(psy, &bm->bi); |
Linus Walleij | 59f1b85 | 2021-11-20 16:53:11 +0100 | [diff] [blame] | 175 | if (ret) { |
| 176 | dev_err(dev, "cannot retrieve battery info\n"); |
| 177 | return ret; |
Rajanikanth H.V | e0f1abe | 2012-11-18 18:45:41 -0800 | [diff] [blame] | 178 | } |
Linus Walleij | 25fd330 | 2021-12-15 02:01:18 +0100 | [diff] [blame] | 179 | bi = bm->bi; |
Rajanikanth H.V | e0f1abe | 2012-11-18 18:45:41 -0800 | [diff] [blame] | 180 | |
Linus Walleij | 22be8d7 | 2021-11-20 16:53:16 +0100 | [diff] [blame] | 181 | /* Fill in defaults for any data missing from the device tree */ |
| 182 | if (bi->charge_full_design_uah < 0) |
| 183 | /* The default capacity is 612 mAh for unknown batteries */ |
| 184 | bi->charge_full_design_uah = 612000; |
Linus Walleij | 2a5f418 | 2021-11-20 16:53:18 +0100 | [diff] [blame] | 185 | |
| 186 | /* |
| 187 | * All of these voltages need to be specified or we will simply |
| 188 | * fall back to safe defaults. |
| 189 | */ |
| 190 | if ((bi->voltage_min_design_uv < 0) || |
| 191 | (bi->voltage_max_design_uv < 0) || |
| 192 | (bi->overvoltage_limit_uv < 0)) { |
| 193 | /* Nominal voltage is 3.7V for unknown batteries */ |
| 194 | bi->voltage_min_design_uv = 3700000; |
| 195 | bi->voltage_max_design_uv = 3700000; |
| 196 | /* Termination voltage (overcharge limit) 4.05V */ |
| 197 | bi->overvoltage_limit_uv = 4050000; |
| 198 | } |
| 199 | |
Linus Walleij | 83e5aa7 | 2021-11-20 16:53:23 +0100 | [diff] [blame] | 200 | if (bi->constant_charge_current_max_ua < 0) |
| 201 | bi->constant_charge_current_max_ua = 400000; |
| 202 | |
Linus Walleij | bc6e028 | 2021-11-20 16:53:24 +0100 | [diff] [blame] | 203 | if (bi->constant_charge_voltage_max_uv < 0) |
| 204 | bi->constant_charge_voltage_max_uv = 4100000; |
| 205 | |
Linus Walleij | 9c20899 | 2021-11-20 16:53:21 +0100 | [diff] [blame] | 206 | if (bi->charge_term_current_ua) |
| 207 | /* Charging stops when we drop below this current */ |
| 208 | bi->charge_term_current_ua = 200000; |
| 209 | |
Linus Walleij | 67acb29 | 2021-11-20 16:53:25 +0100 | [diff] [blame] | 210 | /* |
| 211 | * Internal resistance and factory resistance are tightly coupled |
| 212 | * so both MUST be defined or we fall back to defaults. |
| 213 | */ |
| 214 | if ((bi->factory_internal_resistance_uohm < 0) || |
| 215 | !bi->resist_table) { |
Linus Walleij | 50425cc | 2021-11-20 16:53:20 +0100 | [diff] [blame] | 216 | bi->factory_internal_resistance_uohm = 300000; |
Linus Walleij | 67acb29 | 2021-11-20 16:53:25 +0100 | [diff] [blame] | 217 | bi->resist_table = temp_to_batres_tbl_thermistor; |
| 218 | bi->resist_table_size = ARRAY_SIZE(temp_to_batres_tbl_thermistor); |
| 219 | } |
Linus Walleij | 50425cc | 2021-11-20 16:53:20 +0100 | [diff] [blame] | 220 | |
Linus Walleij | 0525f34 | 2021-11-20 16:53:26 +0100 | [diff] [blame] | 221 | if (!bi->ocv_table[0]) { |
| 222 | /* Default capacity table at say 25 degrees Celsius */ |
| 223 | bi->ocv_temp[0] = 25; |
| 224 | bi->ocv_table[0] = ocv_cap_tbl; |
| 225 | bi->ocv_table_size[0] = ARRAY_SIZE(ocv_cap_tbl); |
| 226 | } |
| 227 | |
Linus Walleij | 6252c70 | 2021-11-20 16:53:13 +0100 | [diff] [blame] | 228 | if (bi->temp_min == INT_MIN) |
| 229 | bi->temp_min = AB8500_TEMP_UNDER; |
| 230 | if (bi->temp_max == INT_MAX) |
| 231 | bi->temp_max = AB8500_TEMP_OVER; |
| 232 | if (bi->temp_alert_min == INT_MIN) |
| 233 | bi->temp_alert_min = AB8500_TEMP_LOW; |
| 234 | if (bi->temp_alert_max == INT_MAX) |
| 235 | bi->temp_alert_max = AB8500_TEMP_HIGH; |
| 236 | bm->temp_hysteresis = AB8500_TEMP_HYSTERESIS; |
| 237 | |
Rajanikanth H.V | e0f1abe | 2012-11-18 18:45:41 -0800 | [diff] [blame] | 238 | return 0; |
| 239 | } |
Linus Walleij | 6252c70 | 2021-11-20 16:53:13 +0100 | [diff] [blame] | 240 | |
| 241 | void ab8500_bm_of_remove(struct power_supply *psy, |
| 242 | struct ab8500_bm_data *bm) |
| 243 | { |
Linus Walleij | 25fd330 | 2021-12-15 02:01:18 +0100 | [diff] [blame] | 244 | power_supply_put_battery_info(psy, bm->bi); |
Linus Walleij | 6252c70 | 2021-11-20 16:53:13 +0100 | [diff] [blame] | 245 | } |