1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/export.h>
3 #include <linux/power_supply.h>
4 #include <linux/of.h>
5 #include <linux/mfd/abx500.h>
6 #include <linux/mfd/abx500/ab8500.h>
7 #include <linux/mfd/abx500/ab8500-bm.h>
8 
9 /*
10  * These are the defined batteries that uses a NTC and ID resistor placed
11  * inside of the battery pack.
12  * Note that the res_to_temp table must be strictly sorted by falling resistance
13  * values to work.
14  */
15 const struct abx500_res_to_temp ab8500_temp_tbl_a_thermistor[] = {
16 	{-5, 53407},
17 	{ 0, 48594},
18 	{ 5, 43804},
19 	{10, 39188},
20 	{15, 34870},
21 	{20, 30933},
22 	{25, 27422},
23 	{30, 24347},
24 	{35, 21694},
25 	{40, 19431},
26 	{45, 17517},
27 	{50, 15908},
28 	{55, 14561},
29 	{60, 13437},
30 	{65, 12500},
31 };
32 EXPORT_SYMBOL(ab8500_temp_tbl_a_thermistor);
33 
34 const int ab8500_temp_tbl_a_size = ARRAY_SIZE(ab8500_temp_tbl_a_thermistor);
35 EXPORT_SYMBOL(ab8500_temp_tbl_a_size);
36 
37 const struct abx500_res_to_temp ab8500_temp_tbl_b_thermistor[] = {
38 	{-5, 200000},
39 	{ 0, 159024},
40 	{ 5, 151921},
41 	{10, 144300},
42 	{15, 136424},
43 	{20, 128565},
44 	{25, 120978},
45 	{30, 113875},
46 	{35, 107397},
47 	{40, 101629},
48 	{45,  96592},
49 	{50,  92253},
50 	{55,  88569},
51 	{60,  85461},
52 	{65,  82869},
53 };
54 EXPORT_SYMBOL(ab8500_temp_tbl_b_thermistor);
55 
56 const int ab8500_temp_tbl_b_size = ARRAY_SIZE(ab8500_temp_tbl_b_thermistor);
57 EXPORT_SYMBOL(ab8500_temp_tbl_b_size);
58 
59 static const struct abx500_v_to_cap cap_tbl_a_thermistor[] = {
60 	{4171,	100},
61 	{4114,	 95},
62 	{4009,	 83},
63 	{3947,	 74},
64 	{3907,	 67},
65 	{3863,	 59},
66 	{3830,	 56},
67 	{3813,	 53},
68 	{3791,	 46},
69 	{3771,	 33},
70 	{3754,	 25},
71 	{3735,	 20},
72 	{3717,	 17},
73 	{3681,	 13},
74 	{3664,	  8},
75 	{3651,	  6},
76 	{3635,	  5},
77 	{3560,	  3},
78 	{3408,    1},
79 	{3247,	  0},
80 };
81 
82 static const struct abx500_v_to_cap cap_tbl_b_thermistor[] = {
83 	{4161,	100},
84 	{4124,	 98},
85 	{4044,	 90},
86 	{4003,	 85},
87 	{3966,	 80},
88 	{3933,	 75},
89 	{3888,	 67},
90 	{3849,	 60},
91 	{3813,	 55},
92 	{3787,	 47},
93 	{3772,	 30},
94 	{3751,	 25},
95 	{3718,	 20},
96 	{3681,	 16},
97 	{3660,	 14},
98 	{3589,	 10},
99 	{3546,	  7},
100 	{3495,	  4},
101 	{3404,	  2},
102 	{3250,	  0},
103 };
104 
105 static const struct abx500_v_to_cap cap_tbl[] = {
106 	{4186,	100},
107 	{4163,	 99},
108 	{4114,	 95},
109 	{4068,	 90},
110 	{3990,	 80},
111 	{3926,	 70},
112 	{3898,	 65},
113 	{3866,	 60},
114 	{3833,	 55},
115 	{3812,	 50},
116 	{3787,	 40},
117 	{3768,	 30},
118 	{3747,	 25},
119 	{3730,	 20},
120 	{3705,	 15},
121 	{3699,	 14},
122 	{3684,	 12},
123 	{3672,	  9},
124 	{3657,	  7},
125 	{3638,	  6},
126 	{3556,	  4},
127 	{3424,	  2},
128 	{3317,	  1},
129 	{3094,	  0},
130 };
131 
132 /*
133  * Note that the res_to_temp table must be strictly sorted by falling
134  * resistance values to work.
135  */
136 static const struct abx500_res_to_temp temp_tbl[] = {
137 	{-5, 214834},
138 	{ 0, 162943},
139 	{ 5, 124820},
140 	{10,  96520},
141 	{15,  75306},
142 	{20,  59254},
143 	{25,  47000},
144 	{30,  37566},
145 	{35,  30245},
146 	{40,  24520},
147 	{45,  20010},
148 	{50,  16432},
149 	{55,  13576},
150 	{60,  11280},
151 	{65,   9425},
152 };
153 
154 /*
155  * Note that the batres_vs_temp table must be strictly sorted by falling
156  * temperature values to work.
157  */
158 static const struct batres_vs_temp temp_to_batres_tbl_thermistor[] = {
159 	{ 40, 120},
160 	{ 30, 135},
161 	{ 20, 165},
162 	{ 10, 230},
163 	{ 00, 325},
164 	{-10, 445},
165 	{-20, 595},
166 };
167 
168 /*
169  * Note that the batres_vs_temp table must be strictly sorted by falling
170  * temperature values to work.
171  */
172 static const struct batres_vs_temp temp_to_batres_tbl_ext_thermistor[] = {
173 	{ 60, 300},
174 	{ 30, 300},
175 	{ 20, 300},
176 	{ 10, 300},
177 	{ 00, 300},
178 	{-10, 300},
179 	{-20, 300},
180 };
181 
182 /* battery resistance table for LI ION 9100 battery */
183 static const struct batres_vs_temp temp_to_batres_tbl_9100[] = {
184 	{ 60, 180},
185 	{ 30, 180},
186 	{ 20, 180},
187 	{ 10, 180},
188 	{ 00, 180},
189 	{-10, 180},
190 	{-20, 180},
191 };
192 
193 static struct abx500_battery_type bat_type_thermistor[] = {
194 	[BATTERY_UNKNOWN] = {
195 		/* First element always represent the UNKNOWN battery */
196 		.name = POWER_SUPPLY_TECHNOLOGY_UNKNOWN,
197 		.resis_high = 0,
198 		.resis_low = 0,
199 		.battery_resistance = 300,
200 		.charge_full_design = 612,
201 		.nominal_voltage = 3700,
202 		.termination_vol = 4050,
203 		.termination_curr = 200,
204 		.recharge_cap = 95,
205 		.normal_cur_lvl = 400,
206 		.normal_vol_lvl = 4100,
207 		.maint_a_cur_lvl = 400,
208 		.maint_a_vol_lvl = 4050,
209 		.maint_a_chg_timer_h = 60,
210 		.maint_b_cur_lvl = 400,
211 		.maint_b_vol_lvl = 4000,
212 		.maint_b_chg_timer_h = 200,
213 		.low_high_cur_lvl = 300,
214 		.low_high_vol_lvl = 4000,
215 		.n_temp_tbl_elements = ARRAY_SIZE(temp_tbl),
216 		.r_to_t_tbl = temp_tbl,
217 		.n_v_cap_tbl_elements = ARRAY_SIZE(cap_tbl),
218 		.v_to_cap_tbl = cap_tbl,
219 		.n_batres_tbl_elements = ARRAY_SIZE(temp_to_batres_tbl_thermistor),
220 		.batres_tbl = temp_to_batres_tbl_thermistor,
221 	},
222 	{
223 		.name = POWER_SUPPLY_TECHNOLOGY_LIPO,
224 		.resis_high = 53407,
225 		.resis_low = 12500,
226 		.battery_resistance = 300,
227 		.charge_full_design = 900,
228 		.nominal_voltage = 3600,
229 		.termination_vol = 4150,
230 		.termination_curr = 80,
231 		.recharge_cap = 95,
232 		.normal_cur_lvl = 700,
233 		.normal_vol_lvl = 4200,
234 		.maint_a_cur_lvl = 600,
235 		.maint_a_vol_lvl = 4150,
236 		.maint_a_chg_timer_h = 60,
237 		.maint_b_cur_lvl = 600,
238 		.maint_b_vol_lvl = 4100,
239 		.maint_b_chg_timer_h = 200,
240 		.low_high_cur_lvl = 300,
241 		.low_high_vol_lvl = 4000,
242 		.n_temp_tbl_elements = ARRAY_SIZE(ab8500_temp_tbl_a_thermistor),
243 		.r_to_t_tbl = ab8500_temp_tbl_a_thermistor,
244 		.n_v_cap_tbl_elements = ARRAY_SIZE(cap_tbl_a_thermistor),
245 		.v_to_cap_tbl = cap_tbl_a_thermistor,
246 		.n_batres_tbl_elements = ARRAY_SIZE(temp_to_batres_tbl_thermistor),
247 		.batres_tbl = temp_to_batres_tbl_thermistor,
248 
249 	},
250 	{
251 		.name = POWER_SUPPLY_TECHNOLOGY_LIPO,
252 		.resis_high = 200000,
253 		.resis_low = 82869,
254 		.battery_resistance = 300,
255 		.charge_full_design = 900,
256 		.nominal_voltage = 3600,
257 		.termination_vol = 4150,
258 		.termination_curr = 80,
259 		.recharge_cap = 95,
260 		.normal_cur_lvl = 700,
261 		.normal_vol_lvl = 4200,
262 		.maint_a_cur_lvl = 600,
263 		.maint_a_vol_lvl = 4150,
264 		.maint_a_chg_timer_h = 60,
265 		.maint_b_cur_lvl = 600,
266 		.maint_b_vol_lvl = 4100,
267 		.maint_b_chg_timer_h = 200,
268 		.low_high_cur_lvl = 300,
269 		.low_high_vol_lvl = 4000,
270 		.n_temp_tbl_elements = ARRAY_SIZE(ab8500_temp_tbl_b_thermistor),
271 		.r_to_t_tbl = ab8500_temp_tbl_b_thermistor,
272 		.n_v_cap_tbl_elements = ARRAY_SIZE(cap_tbl_b_thermistor),
273 		.v_to_cap_tbl = cap_tbl_b_thermistor,
274 		.n_batres_tbl_elements = ARRAY_SIZE(temp_to_batres_tbl_thermistor),
275 		.batres_tbl = temp_to_batres_tbl_thermistor,
276 	},
277 };
278 
279 static struct abx500_battery_type bat_type_ext_thermistor[] = {
280 	[BATTERY_UNKNOWN] = {
281 		/* First element always represent the UNKNOWN battery */
282 		.name = POWER_SUPPLY_TECHNOLOGY_UNKNOWN,
283 		.resis_high = 0,
284 		.resis_low = 0,
285 		.battery_resistance = 300,
286 		.charge_full_design = 612,
287 		.nominal_voltage = 3700,
288 		.termination_vol = 4050,
289 		.termination_curr = 200,
290 		.recharge_cap = 95,
291 		.normal_cur_lvl = 400,
292 		.normal_vol_lvl = 4100,
293 		.maint_a_cur_lvl = 400,
294 		.maint_a_vol_lvl = 4050,
295 		.maint_a_chg_timer_h = 60,
296 		.maint_b_cur_lvl = 400,
297 		.maint_b_vol_lvl = 4000,
298 		.maint_b_chg_timer_h = 200,
299 		.low_high_cur_lvl = 300,
300 		.low_high_vol_lvl = 4000,
301 		.n_temp_tbl_elements = ARRAY_SIZE(temp_tbl),
302 		.r_to_t_tbl = temp_tbl,
303 		.n_v_cap_tbl_elements = ARRAY_SIZE(cap_tbl),
304 		.v_to_cap_tbl = cap_tbl,
305 		.n_batres_tbl_elements = ARRAY_SIZE(temp_to_batres_tbl_thermistor),
306 		.batres_tbl = temp_to_batres_tbl_thermistor,
307 	},
308 /*
309  * These are the batteries that doesn't have an internal NTC resistor to measure
310  * its temperature. The temperature in this case is measure with a NTC placed
311  * near the battery but on the PCB.
312  */
313 	{
314 		.name = POWER_SUPPLY_TECHNOLOGY_LIPO,
315 		.resis_high = 76000,
316 		.resis_low = 53000,
317 		.battery_resistance = 300,
318 		.charge_full_design = 900,
319 		.nominal_voltage = 3700,
320 		.termination_vol = 4150,
321 		.termination_curr = 100,
322 		.recharge_cap = 95,
323 		.normal_cur_lvl = 700,
324 		.normal_vol_lvl = 4200,
325 		.maint_a_cur_lvl = 600,
326 		.maint_a_vol_lvl = 4150,
327 		.maint_a_chg_timer_h = 60,
328 		.maint_b_cur_lvl = 600,
329 		.maint_b_vol_lvl = 4100,
330 		.maint_b_chg_timer_h = 200,
331 		.low_high_cur_lvl = 300,
332 		.low_high_vol_lvl = 4000,
333 		.n_temp_tbl_elements = ARRAY_SIZE(temp_tbl),
334 		.r_to_t_tbl = temp_tbl,
335 		.n_v_cap_tbl_elements = ARRAY_SIZE(cap_tbl),
336 		.v_to_cap_tbl = cap_tbl,
337 		.n_batres_tbl_elements = ARRAY_SIZE(temp_to_batres_tbl_thermistor),
338 		.batres_tbl = temp_to_batres_tbl_thermistor,
339 	},
340 	{
341 		.name = POWER_SUPPLY_TECHNOLOGY_LION,
342 		.resis_high = 30000,
343 		.resis_low = 10000,
344 		.battery_resistance = 300,
345 		.charge_full_design = 950,
346 		.nominal_voltage = 3700,
347 		.termination_vol = 4150,
348 		.termination_curr = 100,
349 		.recharge_cap = 95,
350 		.normal_cur_lvl = 700,
351 		.normal_vol_lvl = 4200,
352 		.maint_a_cur_lvl = 600,
353 		.maint_a_vol_lvl = 4150,
354 		.maint_a_chg_timer_h = 60,
355 		.maint_b_cur_lvl = 600,
356 		.maint_b_vol_lvl = 4100,
357 		.maint_b_chg_timer_h = 200,
358 		.low_high_cur_lvl = 300,
359 		.low_high_vol_lvl = 4000,
360 		.n_temp_tbl_elements = ARRAY_SIZE(temp_tbl),
361 		.r_to_t_tbl = temp_tbl,
362 		.n_v_cap_tbl_elements = ARRAY_SIZE(cap_tbl),
363 		.v_to_cap_tbl = cap_tbl,
364 		.n_batres_tbl_elements = ARRAY_SIZE(temp_to_batres_tbl_thermistor),
365 		.batres_tbl = temp_to_batres_tbl_thermistor,
366 	},
367 	{
368 		.name = POWER_SUPPLY_TECHNOLOGY_LION,
369 		.resis_high = 95000,
370 		.resis_low = 76001,
371 		.battery_resistance = 300,
372 		.charge_full_design = 950,
373 		.nominal_voltage = 3700,
374 		.termination_vol = 4150,
375 		.termination_curr = 100,
376 		.recharge_cap = 95,
377 		.normal_cur_lvl = 700,
378 		.normal_vol_lvl = 4200,
379 		.maint_a_cur_lvl = 600,
380 		.maint_a_vol_lvl = 4150,
381 		.maint_a_chg_timer_h = 60,
382 		.maint_b_cur_lvl = 600,
383 		.maint_b_vol_lvl = 4100,
384 		.maint_b_chg_timer_h = 200,
385 		.low_high_cur_lvl = 300,
386 		.low_high_vol_lvl = 4000,
387 		.n_temp_tbl_elements = ARRAY_SIZE(temp_tbl),
388 		.r_to_t_tbl = temp_tbl,
389 		.n_v_cap_tbl_elements = ARRAY_SIZE(cap_tbl),
390 		.v_to_cap_tbl = cap_tbl,
391 		.n_batres_tbl_elements = ARRAY_SIZE(temp_to_batres_tbl_thermistor),
392 		.batres_tbl = temp_to_batres_tbl_thermistor,
393 	},
394 };
395 
396 static const struct abx500_bm_capacity_levels cap_levels = {
397 	.critical	= 2,
398 	.low		= 10,
399 	.normal		= 70,
400 	.high		= 95,
401 	.full		= 100,
402 };
403 
404 static const struct abx500_fg_parameters fg = {
405 	.recovery_sleep_timer = 10,
406 	.recovery_total_time = 100,
407 	.init_timer = 1,
408 	.init_discard_time = 5,
409 	.init_total_time = 40,
410 	.high_curr_time = 60,
411 	.accu_charging = 30,
412 	.accu_high_curr = 30,
413 	.high_curr_threshold = 50,
414 	.lowbat_threshold = 3100,
415 	.battok_falling_th_sel0 = 2860,
416 	.battok_raising_th_sel1 = 2860,
417 	.maint_thres = 95,
418 	.user_cap_limit = 15,
419 	.pcut_enable = 1,
420 	.pcut_max_time = 127,
421 	.pcut_flag_time = 112,
422 	.pcut_max_restart = 15,
423 	.pcut_debounce_time = 2,
424 };
425 
426 static const struct abx500_maxim_parameters ab8500_maxi_params = {
427 	.ena_maxi = true,
428 	.chg_curr = 910,
429 	.wait_cycles = 10,
430 	.charger_curr_step = 100,
431 };
432 
433 static const struct abx500_bm_charger_parameters chg = {
434 	.usb_volt_max		= 5500,
435 	.usb_curr_max		= 1500,
436 	.ac_volt_max		= 7500,
437 	.ac_curr_max		= 1500,
438 };
439 
440 /*
441  * This array maps the raw hex value to charger output current used by the
442  * AB8500 values
443  */
444 static int ab8500_charge_output_curr_map[] = {
445         100,    200,    300,    400,    500,    600,    700,    800,
446         900,    1000,   1100,   1200,   1300,   1400,   1500,   1500,
447 };
448 
449 /*
450  * This array maps the raw hex value to charger input current used by the
451  * AB8500 values
452  */
453 static int ab8500_charge_input_curr_map[] = {
454         50,     98,     193,    290,    380,    450,    500,    600,
455         700,    800,    900,    1000,   1100,   1300,   1400,   1500,
456 };
457 
458 struct abx500_bm_data ab8500_bm_data = {
459 	.temp_under             = 3,
460 	.temp_low               = 8,
461 	.temp_high              = 43,
462 	.temp_over              = 48,
463 	.main_safety_tmr_h      = 4,
464 	.temp_interval_chg      = 20,
465 	.temp_interval_nochg    = 120,
466 	.usb_safety_tmr_h       = 4,
467 	.bkup_bat_v             = BUP_VCH_SEL_2P6V,
468 	.bkup_bat_i             = BUP_ICH_SEL_150UA,
469 	.no_maintenance         = false,
470 	.capacity_scaling       = false,
471 	.adc_therm              = ABx500_ADC_THERM_BATCTRL,
472 	.chg_unknown_bat        = false,
473 	.enable_overshoot       = false,
474 	.fg_res                 = 100,
475 	.cap_levels             = &cap_levels,
476 	.bat_type               = bat_type_thermistor,
477 	.n_btypes               = ARRAY_SIZE(bat_type_thermistor),
478 	.batt_id                = 0,
479 	.interval_charging      = 5,
480 	.interval_not_charging  = 120,
481 	.temp_hysteresis        = 3,
482 	.gnd_lift_resistance    = 34,
483 	.chg_output_curr        = ab8500_charge_output_curr_map,
484 	.n_chg_out_curr         = ARRAY_SIZE(ab8500_charge_output_curr_map),
485 	.maxi                   = &ab8500_maxi_params,
486 	.chg_params             = &chg,
487 	.fg_params              = &fg,
488         .chg_input_curr         = ab8500_charge_input_curr_map,
489         .n_chg_in_curr          = ARRAY_SIZE(ab8500_charge_input_curr_map),
490 };
491 
492 int ab8500_bm_of_probe(struct device *dev,
493 		       struct device_node *np,
494 		       struct abx500_bm_data *bm)
495 {
496 	const struct batres_vs_temp *tmp_batres_tbl;
497 	struct device_node *battery_node;
498 	const char *btech;
499 	int i;
500 
501 	/* get phandle to 'battery-info' node */
502 	battery_node = of_parse_phandle(np, "battery", 0);
503 	if (!battery_node) {
504 		dev_err(dev, "battery node or reference missing\n");
505 		return -EINVAL;
506 	}
507 
508 	btech = of_get_property(battery_node, "stericsson,battery-type", NULL);
509 	if (!btech) {
510 		dev_warn(dev, "missing property battery-name/type\n");
511 		return -EINVAL;
512 	}
513 
514 	if (strncmp(btech, "LION", 4) == 0) {
515 		bm->no_maintenance  = true;
516 		bm->chg_unknown_bat = true;
517 		bm->bat_type[BATTERY_UNKNOWN].charge_full_design = 2600;
518 		bm->bat_type[BATTERY_UNKNOWN].termination_vol    = 4150;
519 		bm->bat_type[BATTERY_UNKNOWN].recharge_cap       = 95;
520 		bm->bat_type[BATTERY_UNKNOWN].normal_cur_lvl     = 520;
521 		bm->bat_type[BATTERY_UNKNOWN].normal_vol_lvl     = 4200;
522 	}
523 
524 	if (of_property_read_bool(battery_node, "thermistor-on-batctrl")) {
525 		if (strncmp(btech, "LION", 4) == 0)
526 			tmp_batres_tbl = temp_to_batres_tbl_9100;
527 		else
528 			tmp_batres_tbl = temp_to_batres_tbl_thermistor;
529 	} else {
530 		bm->n_btypes   = 4;
531 		bm->bat_type   = bat_type_ext_thermistor;
532 		bm->adc_therm  = ABx500_ADC_THERM_BATTEMP;
533 		tmp_batres_tbl = temp_to_batres_tbl_ext_thermistor;
534 	}
535 
536 	/* select the battery resolution table */
537 	for (i = 0; i < bm->n_btypes; ++i)
538 		bm->bat_type[i].batres_tbl = tmp_batres_tbl;
539 
540 	of_node_put(battery_node);
541 
542 	return 0;
543 }
544