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