| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
#include <linux/export.h> |
| 3 |
#include <linux/power_supply.h> |
| 4 |
#include <linux/of.h> |
| 5 |
|
| 6 |
#include "ab8500-bm.h" |
| 7 |
|
| 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 |
|
| 19 |
static const 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}, |
| 44 |
}; |
| 45 |
|
| 46 |
/* |
| 47 |
* Note that the batres_vs_temp table must be strictly sorted by falling |
| 48 |
* temperature values to work. Factory resistance is 300 mOhm and the |
| 49 |
* resistance values to the right are percentages of 300 mOhm. |
| 50 |
*/ |
| 51 |
static const struct power_supply_resistance_temp_table temp_to_batres_tbl_thermistor[] = { |
| 52 |
{ .temp = 40, .resistance = 40 /* 120 mOhm */ }, |
| 53 |
{ .temp = 30, .resistance = 45 /* 135 mOhm */ }, |
| 54 |
{ .temp = 20, .resistance = 55 /* 165 mOhm */ }, |
| 55 |
{ .temp = 10, .resistance = 77 /* 230 mOhm */ }, |
| 56 |
{ .temp = 00, .resistance = 108 /* 325 mOhm */ }, |
| 57 |
{ .temp = -10, .resistance = 158 /* 445 mOhm */ }, |
| 58 |
{ .temp = -20, .resistance = 198 /* 595 mOhm */ }, |
| 59 |
}; |
| 60 |
|
| 61 |
static struct power_supply_maintenance_charge_table ab8500_maint_charg_table[] = { |
| 62 |
{ |
| 63 |
/* Maintenance charging phase A, 60 hours */ |
| 64 |
.charge_current_max_ua = 400000, |
| 65 |
.charge_voltage_max_uv = 4050000, |
| 66 |
.charge_safety_timer_minutes = 60*60, |
| 67 |
}, |
| 68 |
{ |
| 69 |
/* Maintenance charging phase B, 200 hours */ |
| 70 |
.charge_current_max_ua = 400000, |
| 71 |
.charge_voltage_max_uv = 4000000, |
| 72 |
.charge_safety_timer_minutes = 200*60, |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
static const struct ab8500_bm_capacity_levels cap_levels = { |
| 77 |
.critical = 2, |
| 78 |
.low = 10, |
| 79 |
.normal = 70, |
| 80 |
.high = 95, |
| 81 |
.full = 100, |
| 82 |
}; |
| 83 |
|
| 84 |
static const struct ab8500_fg_parameters fg = { |
| 85 |
.recovery_sleep_timer = 10, |
| 86 |
.recovery_total_time = 100, |
| 87 |
.init_timer = 1, |
| 88 |
.init_discard_time = 5, |
| 89 |
.init_total_time = 40, |
| 90 |
.high_curr_time = 60, |
| 91 |
.accu_charging = 30, |
| 92 |
.accu_high_curr = 30, |
| 93 |
.high_curr_threshold_ua = 50000, |
| 94 |
.lowbat_threshold_uv = 3100000, |
| 95 |
.battok_falling_th_sel0 = 2860, |
| 96 |
.battok_raising_th_sel1 = 2860, |
| 97 |
.maint_thres = 95, |
| 98 |
.user_cap_limit = 15, |
| 99 |
.pcut_enable = 1, |
| 100 |
.pcut_max_time = 127, |
| 101 |
.pcut_flag_time = 112, |
| 102 |
.pcut_max_restart = 15, |
| 103 |
.pcut_debounce_time = 2, |
| 104 |
}; |
| 105 |
|
| 106 |
static const struct ab8500_maxim_parameters ab8500_maxi_params = { |
| 107 |
.ena_maxi = true, |
| 108 |
.chg_curr_ua = 910000, |
| 109 |
.wait_cycles = 10, |
| 110 |
.charger_curr_step_ua = 100000, |
| 111 |
}; |
| 112 |
|
| 113 |
static const struct ab8500_bm_charger_parameters chg = { |
| 114 |
.usb_volt_max_uv = 5500000, |
| 115 |
.usb_curr_max_ua = 1500000, |
| 116 |
.ac_volt_max_uv = 7500000, |
| 117 |
.ac_curr_max_ua = 1500000, |
| 118 |
}; |
| 119 |
|
| 120 |
/* This is referenced directly in the charger code */ |
| 121 |
struct ab8500_bm_data ab8500_bm_data = { |
| 122 |
.main_safety_tmr_h = 4, |
| 123 |
.temp_interval_chg = 20, |
| 124 |
.temp_interval_nochg = 120, |
| 125 |
.usb_safety_tmr_h = 4, |
| 126 |
.bkup_bat_v = BUP_VCH_SEL_2P6V, |
| 127 |
.bkup_bat_i = BUP_ICH_SEL_150UA, |
| 128 |
.capacity_scaling = false, |
| 129 |
.chg_unknown_bat = false, |
| 130 |
.enable_overshoot = false, |
| 131 |
.fg_res = 100, |
| 132 |
.cap_levels = &cap_levels, |
| 133 |
.interval_charging = 5, |
| 134 |
.interval_not_charging = 120, |
| 135 |
.maxi = &ab8500_maxi_params, |
| 136 |
.chg_params = &chg, |
| 137 |
.fg_params = &fg, |
| 138 |
}; |
| 139 |
|
| 140 |
int ab8500_bm_of_probe(struct power_supply *psy, |
| 141 |
struct ab8500_bm_data *bm) |
| 142 |
{ |
| 143 |
struct power_supply_battery_info *bi; |
| 144 |
struct device *dev = &psy->dev; |
| 145 |
int ret; |
| 146 |
|
| 147 |
ret = power_supply_get_battery_info(psy, &bm->bi); |
| 148 |
if (ret) { |
| 149 |
dev_err(dev, "cannot retrieve battery info\n"); |
| 150 |
return ret; |
| 151 |
} |
| 152 |
bi = bm->bi; |
| 153 |
|
| 154 |
/* Fill in defaults for any data missing from the device tree */ |
| 155 |
if (bi->charge_full_design_uah < 0) |
| 156 |
/* The default capacity is 612 mAh for unknown batteries */ |
| 157 |
bi->charge_full_design_uah = 612000; |
| 158 |
|
| 159 |
/* |
| 160 |
* All of these voltages need to be specified or we will simply |
| 161 |
* fall back to safe defaults. |
| 162 |
*/ |
| 163 |
if ((bi->voltage_min_design_uv < 0) || |
| 164 |
(bi->voltage_max_design_uv < 0)) { |
| 165 |
/* Nominal voltage is 3.7V for unknown batteries */ |
| 166 |
bi->voltage_min_design_uv = 3700000; |
| 167 |
/* Termination voltage 4.05V */ |
| 168 |
bi->voltage_max_design_uv = 4050000; |
| 169 |
} |
| 170 |
|
| 171 |
if (bi->constant_charge_current_max_ua < 0) |
| 172 |
bi->constant_charge_current_max_ua = 400000; |
| 173 |
|
| 174 |
if (bi->constant_charge_voltage_max_uv < 0) |
| 175 |
bi->constant_charge_voltage_max_uv = 4100000; |
| 176 |
|
| 177 |
if (bi->charge_term_current_ua) |
| 178 |
/* Charging stops when we drop below this current */ |
| 179 |
bi->charge_term_current_ua = 200000; |
| 180 |
|
| 181 |
if (!bi->maintenance_charge || !bi->maintenance_charge_size) { |
| 182 |
bi->maintenance_charge = ab8500_maint_charg_table; |
| 183 |
bi->maintenance_charge_size = ARRAY_SIZE(ab8500_maint_charg_table); |
| 184 |
} |
| 185 |
|
| 186 |
if (bi->alert_low_temp_charge_current_ua < 0 || |
| 187 |
bi->alert_low_temp_charge_voltage_uv < 0) |
| 188 |
{ |
| 189 |
bi->alert_low_temp_charge_current_ua = 300000; |
| 190 |
bi->alert_low_temp_charge_voltage_uv = 4000000; |
| 191 |
} |
| 192 |
if (bi->alert_high_temp_charge_current_ua < 0 || |
| 193 |
bi->alert_high_temp_charge_voltage_uv < 0) |
| 194 |
{ |
| 195 |
bi->alert_high_temp_charge_current_ua = 300000; |
| 196 |
bi->alert_high_temp_charge_voltage_uv = 4000000; |
| 197 |
} |
| 198 |
|
| 199 |
/* |
| 200 |
* Internal resistance and factory resistance are tightly coupled |
| 201 |
* so both MUST be defined or we fall back to defaults. |
| 202 |
*/ |
| 203 |
if ((bi->factory_internal_resistance_uohm < 0) || |
| 204 |
!bi->resist_table) { |
| 205 |
bi->factory_internal_resistance_uohm = 300000; |
| 206 |
bi->resist_table = temp_to_batres_tbl_thermistor; |
| 207 |
bi->resist_table_size = ARRAY_SIZE(temp_to_batres_tbl_thermistor); |
| 208 |
} |
| 209 |
|
| 210 |
/* The default battery is emulated by a resistor at 7K */ |
| 211 |
if (bi->bti_resistance_ohm < 0 || |
| 212 |
bi->bti_resistance_tolerance < 0) { |
| 213 |
bi->bti_resistance_ohm = 7000; |
| 214 |
bi->bti_resistance_tolerance = 20; |
| 215 |
} |
| 216 |
|
| 217 |
if (!bi->ocv_table[0]) { |
| 218 |
/* Default capacity table at say 25 degrees Celsius */ |
| 219 |
bi->ocv_temp[0] = 25; |
| 220 |
bi->ocv_table[0] = ocv_cap_tbl; |
| 221 |
bi->ocv_table_size[0] = ARRAY_SIZE(ocv_cap_tbl); |
| 222 |
} |
| 223 |
|
| 224 |
if (bi->temp_min == INT_MIN) |
| 225 |
bi->temp_min = AB8500_TEMP_UNDER; |
| 226 |
if (bi->temp_max == INT_MAX) |
| 227 |
bi->temp_max = AB8500_TEMP_OVER; |
| 228 |
if (bi->temp_alert_min == INT_MIN) |
| 229 |
bi->temp_alert_min = AB8500_TEMP_LOW; |
| 230 |
if (bi->temp_alert_max == INT_MAX) |
| 231 |
bi->temp_alert_max = AB8500_TEMP_HIGH; |
| 232 |
bm->temp_hysteresis = AB8500_TEMP_HYSTERESIS; |
| 233 |
|
| 234 |
return 0; |
| 235 |
} |
| 236 |
|
| 237 |
void ab8500_bm_of_remove(struct power_supply *psy, |
| 238 |
struct ab8500_bm_data *bm) |
| 239 |
{ |
| 240 |
power_supply_put_battery_info(psy, bm->bi); |
| 241 |
} |