xref: /openbmc/linux/drivers/power/supply/ab8500_bmdata.c (revision d72ce7d324786257410bec6b36e3756647dd76fd)
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 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 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 /* Default battery type for reference designs is the unknown type */
77 static struct ab8500_battery_type bat_type_thermistor_unknown = {
78 	.resis_high = 0,
79 	.resis_low = 0,
80 	.low_high_cur_lvl = 300,
81 	.low_high_vol_lvl = 4000,
82 };
83 
84 static const struct ab8500_bm_capacity_levels cap_levels = {
85 	.critical	= 2,
86 	.low		= 10,
87 	.normal		= 70,
88 	.high		= 95,
89 	.full		= 100,
90 };
91 
92 static const struct ab8500_fg_parameters fg = {
93 	.recovery_sleep_timer = 10,
94 	.recovery_total_time = 100,
95 	.init_timer = 1,
96 	.init_discard_time = 5,
97 	.init_total_time = 40,
98 	.high_curr_time = 60,
99 	.accu_charging = 30,
100 	.accu_high_curr = 30,
101 	.high_curr_threshold_ua = 50000,
102 	.lowbat_threshold_uv = 3100000,
103 	.battok_falling_th_sel0 = 2860,
104 	.battok_raising_th_sel1 = 2860,
105 	.maint_thres = 95,
106 	.user_cap_limit = 15,
107 	.pcut_enable = 1,
108 	.pcut_max_time = 127,
109 	.pcut_flag_time = 112,
110 	.pcut_max_restart = 15,
111 	.pcut_debounce_time = 2,
112 };
113 
114 static const struct ab8500_maxim_parameters ab8500_maxi_params = {
115 	.ena_maxi = true,
116 	.chg_curr_ua = 910000,
117 	.wait_cycles = 10,
118 	.charger_curr_step_ua = 100000,
119 };
120 
121 static const struct ab8500_bm_charger_parameters chg = {
122 	.usb_volt_max_uv	= 5500000,
123 	.usb_curr_max_ua	= 1500000,
124 	.ac_volt_max_uv		= 7500000,
125 	.ac_curr_max_ua		= 1500000,
126 };
127 
128 /* This is referenced directly in the charger code */
129 struct ab8500_bm_data ab8500_bm_data = {
130 	.main_safety_tmr_h      = 4,
131 	.temp_interval_chg      = 20,
132 	.temp_interval_nochg    = 120,
133 	.usb_safety_tmr_h       = 4,
134 	.bkup_bat_v             = BUP_VCH_SEL_2P6V,
135 	.bkup_bat_i             = BUP_ICH_SEL_150UA,
136 	.capacity_scaling       = false,
137 	.chg_unknown_bat        = false,
138 	.enable_overshoot       = false,
139 	.fg_res                 = 100,
140 	.cap_levels             = &cap_levels,
141 	.bat_type               = &bat_type_thermistor_unknown,
142 	.interval_charging      = 5,
143 	.interval_not_charging  = 120,
144 	.maxi                   = &ab8500_maxi_params,
145 	.chg_params             = &chg,
146 	.fg_params              = &fg,
147 };
148 
149 int ab8500_bm_of_probe(struct power_supply *psy,
150 		       struct ab8500_bm_data *bm)
151 {
152 	struct power_supply_battery_info *bi;
153 	struct device *dev = &psy->dev;
154 	int ret;
155 
156 	ret = power_supply_get_battery_info(psy, &bm->bi);
157 	if (ret) {
158 		dev_err(dev, "cannot retrieve battery info\n");
159 		return ret;
160 	}
161 	bi = bm->bi;
162 
163 	/* Fill in defaults for any data missing from the device tree */
164 	if (bi->charge_full_design_uah < 0)
165 		/* The default capacity is 612 mAh for unknown batteries */
166 		bi->charge_full_design_uah = 612000;
167 
168 	/*
169 	 * All of these voltages need to be specified or we will simply
170 	 * fall back to safe defaults.
171 	 */
172 	if ((bi->voltage_min_design_uv < 0) ||
173 	    (bi->voltage_max_design_uv < 0)) {
174 		/* Nominal voltage is 3.7V for unknown batteries */
175 		bi->voltage_min_design_uv = 3700000;
176 		/* Termination voltage 4.05V */
177 		bi->voltage_max_design_uv = 4050000;
178 	}
179 
180 	if (bi->constant_charge_current_max_ua < 0)
181 		bi->constant_charge_current_max_ua = 400000;
182 
183 	if (bi->constant_charge_voltage_max_uv < 0)
184 		bi->constant_charge_voltage_max_uv = 4100000;
185 
186 	if (bi->charge_term_current_ua)
187 		/* Charging stops when we drop below this current */
188 		bi->charge_term_current_ua = 200000;
189 
190 	if (!bi->maintenance_charge || !bi->maintenance_charge_size) {
191 		bi->maintenance_charge = ab8500_maint_charg_table;
192 		bi->maintenance_charge_size = ARRAY_SIZE(ab8500_maint_charg_table);
193 	}
194 
195 	/*
196 	 * Internal resistance and factory resistance are tightly coupled
197 	 * so both MUST be defined or we fall back to defaults.
198 	 */
199 	if ((bi->factory_internal_resistance_uohm < 0) ||
200 	    !bi->resist_table) {
201 		bi->factory_internal_resistance_uohm = 300000;
202 		bi->resist_table = temp_to_batres_tbl_thermistor;
203 		bi->resist_table_size = ARRAY_SIZE(temp_to_batres_tbl_thermistor);
204 	}
205 
206 	if (!bi->ocv_table[0]) {
207 		/* Default capacity table at say 25 degrees Celsius */
208 		bi->ocv_temp[0] = 25;
209 		bi->ocv_table[0] = ocv_cap_tbl;
210 		bi->ocv_table_size[0] = ARRAY_SIZE(ocv_cap_tbl);
211 	}
212 
213 	if (bi->temp_min == INT_MIN)
214 		bi->temp_min = AB8500_TEMP_UNDER;
215 	if (bi->temp_max == INT_MAX)
216 		bi->temp_max = AB8500_TEMP_OVER;
217 	if (bi->temp_alert_min == INT_MIN)
218 		bi->temp_alert_min = AB8500_TEMP_LOW;
219 	if (bi->temp_alert_max == INT_MAX)
220 		bi->temp_alert_max = AB8500_TEMP_HIGH;
221 	bm->temp_hysteresis = AB8500_TEMP_HYSTERESIS;
222 
223 	return 0;
224 }
225 
226 void ab8500_bm_of_remove(struct power_supply *psy,
227 			 struct ab8500_bm_data *bm)
228 {
229 	power_supply_put_battery_info(psy, bm->bi);
230 }
231