1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
|
/* Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#define pr_fmt(fmt) "%s: " fmt, __func__
#include <linux/module.h>
#include <linux/batterydata-lib.h>
int linear_interpolate(int y0, int x0, int y1, int x1, int x)
{
if (y0 == y1 || x == x0)
return y0;
if (x1 == x0 || x == x1)
return y1;
return y0 + ((y1 - y0) * (x - x0) / (x1 - x0));
}
static int interpolate_single_lut_scaled(struct single_row_lut *lut,
int x, int scale)
{
int i, result;
if (x < lut->x[0] * scale) {
pr_debug("x %d less than known range return y = %d lut = %pS\n",
x, lut->y[0], lut);
return lut->y[0];
}
if (x > lut->x[lut->cols - 1] * scale) {
pr_debug("x %d more than known range return y = %d lut = %pS\n",
x, lut->y[lut->cols - 1], lut);
return lut->y[lut->cols - 1];
}
for (i = 0; i < lut->cols; i++)
if (x <= lut->x[i] * scale)
break;
if (x == lut->x[i] * scale) {
result = lut->y[i];
} else {
result = linear_interpolate(
lut->y[i - 1],
lut->x[i - 1] * scale,
lut->y[i],
lut->x[i] * scale,
x);
}
return result;
}
int interpolate_fcc(struct single_row_lut *fcc_temp_lut, int batt_temp)
{
return interpolate_single_lut_scaled(fcc_temp_lut,
batt_temp,
DEGC_SCALE);
}
int interpolate_scalingfactor_fcc(struct single_row_lut *fcc_sf_lut,
int cycles)
{
/*
* sf table could be null when no battery aging data is available, in
* that case return 100%
*/
if (fcc_sf_lut)
return interpolate_single_lut_scaled(fcc_sf_lut, cycles, 1);
else
return 100;
}
int interpolate_scalingfactor(struct sf_lut *sf_lut, int row_entry, int pc)
{
int i, scalefactorrow1, scalefactorrow2, scalefactor, rows, cols;
int row1 = 0;
int row2 = 0;
/*
* sf table could be null when no battery aging data is available, in
* that case return 100%
*/
if (!sf_lut)
return 100;
rows = sf_lut->rows;
cols = sf_lut->cols;
if (pc > sf_lut->percent[0]) {
pr_debug("pc %d greater than known pc ranges for sfd\n", pc);
row1 = 0;
row2 = 0;
} else if (pc < sf_lut->percent[rows - 1]) {
pr_debug("pc %d less than known pc ranges for sf\n", pc);
row1 = rows - 1;
row2 = rows - 1;
} else {
for (i = 0; i < rows; i++) {
if (pc == sf_lut->percent[i]) {
row1 = i;
row2 = i;
break;
}
if (pc > sf_lut->percent[i]) {
row1 = i - 1;
row2 = i;
break;
}
}
}
if (row_entry < sf_lut->row_entries[0] * DEGC_SCALE)
row_entry = sf_lut->row_entries[0] * DEGC_SCALE;
if (row_entry > sf_lut->row_entries[cols - 1] * DEGC_SCALE)
row_entry = sf_lut->row_entries[cols - 1] * DEGC_SCALE;
for (i = 0; i < cols; i++)
if (row_entry <= sf_lut->row_entries[i] * DEGC_SCALE)
break;
if (row_entry == sf_lut->row_entries[i] * DEGC_SCALE) {
scalefactor = linear_interpolate(
sf_lut->sf[row1][i],
sf_lut->percent[row1],
sf_lut->sf[row2][i],
sf_lut->percent[row2],
pc);
return scalefactor;
}
scalefactorrow1 = linear_interpolate(
sf_lut->sf[row1][i - 1],
sf_lut->row_entries[i - 1] * DEGC_SCALE,
sf_lut->sf[row1][i],
sf_lut->row_entries[i] * DEGC_SCALE,
row_entry);
scalefactorrow2 = linear_interpolate(
sf_lut->sf[row2][i - 1],
sf_lut->row_entries[i - 1] * DEGC_SCALE,
sf_lut->sf[row2][i],
sf_lut->row_entries[i] * DEGC_SCALE,
row_entry);
scalefactor = linear_interpolate(
scalefactorrow1,
sf_lut->percent[row1],
scalefactorrow2,
sf_lut->percent[row2],
pc);
return scalefactor;
}
/* get ocv given a soc -- reverse lookup */
int interpolate_ocv(struct pc_temp_ocv_lut *pc_temp_ocv,
int batt_temp, int pc)
{
int i, ocvrow1, ocvrow2, ocv, rows, cols;
int row1 = 0;
int row2 = 0;
rows = pc_temp_ocv->rows;
cols = pc_temp_ocv->cols;
if (pc > pc_temp_ocv->percent[0]) {
pr_debug("pc %d greater than known pc ranges for sfd\n", pc);
row1 = 0;
row2 = 0;
} else if (pc < pc_temp_ocv->percent[rows - 1]) {
pr_debug("pc %d less than known pc ranges for sf\n", pc);
row1 = rows - 1;
row2 = rows - 1;
} else {
for (i = 0; i < rows; i++) {
if (pc == pc_temp_ocv->percent[i]) {
row1 = i;
row2 = i;
break;
}
if (pc > pc_temp_ocv->percent[i]) {
row1 = i - 1;
row2 = i;
break;
}
}
}
if (batt_temp < pc_temp_ocv->temp[0] * DEGC_SCALE)
batt_temp = pc_temp_ocv->temp[0] * DEGC_SCALE;
if (batt_temp > pc_temp_ocv->temp[cols - 1] * DEGC_SCALE)
batt_temp = pc_temp_ocv->temp[cols - 1] * DEGC_SCALE;
for (i = 0; i < cols; i++)
if (batt_temp <= pc_temp_ocv->temp[i] * DEGC_SCALE)
break;
if (batt_temp == pc_temp_ocv->temp[i] * DEGC_SCALE) {
ocv = linear_interpolate(
pc_temp_ocv->ocv[row1][i],
pc_temp_ocv->percent[row1],
pc_temp_ocv->ocv[row2][i],
pc_temp_ocv->percent[row2],
pc);
return ocv;
}
ocvrow1 = linear_interpolate(
pc_temp_ocv->ocv[row1][i - 1],
pc_temp_ocv->temp[i - 1] * DEGC_SCALE,
pc_temp_ocv->ocv[row1][i],
pc_temp_ocv->temp[i] * DEGC_SCALE,
batt_temp);
ocvrow2 = linear_interpolate(
pc_temp_ocv->ocv[row2][i - 1],
pc_temp_ocv->temp[i - 1] * DEGC_SCALE,
pc_temp_ocv->ocv[row2][i],
pc_temp_ocv->temp[i] * DEGC_SCALE,
batt_temp);
ocv = linear_interpolate(
ocvrow1,
pc_temp_ocv->percent[row1],
ocvrow2,
pc_temp_ocv->percent[row2],
pc);
return ocv;
}
int interpolate_pc(struct pc_temp_ocv_lut *pc_temp_ocv,
int batt_temp, int ocv)
{
int i, j, pcj, pcj_minus_one, pc;
int rows = pc_temp_ocv->rows;
int cols = pc_temp_ocv->cols;
if (batt_temp < pc_temp_ocv->temp[0] * DEGC_SCALE) {
pr_debug("batt_temp %d < known temp range\n", batt_temp);
batt_temp = pc_temp_ocv->temp[0] * DEGC_SCALE;
}
if (batt_temp > pc_temp_ocv->temp[cols - 1] * DEGC_SCALE) {
pr_debug("batt_temp %d > known temp range\n", batt_temp);
batt_temp = pc_temp_ocv->temp[cols - 1] * DEGC_SCALE;
}
for (j = 0; j < cols; j++)
if (batt_temp <= pc_temp_ocv->temp[j] * DEGC_SCALE)
break;
if (batt_temp == pc_temp_ocv->temp[j] * DEGC_SCALE) {
/* found an exact match for temp in the table */
if (ocv >= pc_temp_ocv->ocv[0][j])
return pc_temp_ocv->percent[0];
if (ocv <= pc_temp_ocv->ocv[rows - 1][j])
return pc_temp_ocv->percent[rows - 1];
for (i = 0; i < rows; i++) {
if (ocv >= pc_temp_ocv->ocv[i][j]) {
if (ocv == pc_temp_ocv->ocv[i][j])
return pc_temp_ocv->percent[i];
pc = linear_interpolate(
pc_temp_ocv->percent[i],
pc_temp_ocv->ocv[i][j],
pc_temp_ocv->percent[i - 1],
pc_temp_ocv->ocv[i - 1][j],
ocv);
return pc;
}
}
}
/*
* batt_temp is within temperature for
* column j-1 and j
*/
if (ocv >= pc_temp_ocv->ocv[0][j])
return pc_temp_ocv->percent[0];
if (ocv <= pc_temp_ocv->ocv[rows - 1][j - 1])
return pc_temp_ocv->percent[rows - 1];
pcj_minus_one = 0;
pcj = 0;
for (i = 0; i < rows-1; i++) {
if (pcj == 0
&& is_between(pc_temp_ocv->ocv[i][j],
pc_temp_ocv->ocv[i+1][j], ocv)) {
pcj = linear_interpolate(
pc_temp_ocv->percent[i],
pc_temp_ocv->ocv[i][j],
pc_temp_ocv->percent[i + 1],
pc_temp_ocv->ocv[i+1][j],
ocv);
}
if (pcj_minus_one == 0
&& is_between(pc_temp_ocv->ocv[i][j-1],
pc_temp_ocv->ocv[i+1][j-1], ocv)) {
pcj_minus_one = linear_interpolate(
pc_temp_ocv->percent[i],
pc_temp_ocv->ocv[i][j-1],
pc_temp_ocv->percent[i + 1],
pc_temp_ocv->ocv[i+1][j-1],
ocv);
}
if (pcj && pcj_minus_one) {
pc = linear_interpolate(
pcj_minus_one,
pc_temp_ocv->temp[j-1] * DEGC_SCALE,
pcj,
pc_temp_ocv->temp[j] * DEGC_SCALE,
batt_temp);
return pc;
}
}
if (pcj)
return pcj;
if (pcj_minus_one)
return pcj_minus_one;
pr_debug("%d ocv wasn't found for temp %d in the LUT returning 100%%\n",
ocv, batt_temp);
return 100;
}
int interpolate_slope(struct pc_temp_ocv_lut *pc_temp_ocv,
int batt_temp, int pc)
{
int i, ocvrow1, ocvrow2, rows, cols;
int row1 = 0;
int row2 = 0;
int slope;
rows = pc_temp_ocv->rows;
cols = pc_temp_ocv->cols;
if (pc >= pc_temp_ocv->percent[0]) {
pr_debug("pc %d >= max pc range - use the slope at pc=%d\n",
pc, pc_temp_ocv->percent[0]);
row1 = 0;
row2 = 1;
} else if (pc <= pc_temp_ocv->percent[rows - 1]) {
pr_debug("pc %d is <= min pc range - use the slope at pc=%d\n",
pc, pc_temp_ocv->percent[rows - 1]);
row1 = rows - 2;
row2 = rows - 1;
} else {
for (i = 0; i < rows; i++) {
if (pc == pc_temp_ocv->percent[i]) {
row1 = i - 1;
row2 = i;
break;
}
if (pc > pc_temp_ocv->percent[i]) {
row1 = i - 1;
row2 = i;
break;
}
}
}
if (batt_temp < pc_temp_ocv->temp[0] * DEGC_SCALE)
batt_temp = pc_temp_ocv->temp[0] * DEGC_SCALE;
if (batt_temp > pc_temp_ocv->temp[cols - 1] * DEGC_SCALE)
batt_temp = pc_temp_ocv->temp[cols - 1] * DEGC_SCALE;
for (i = 0; i < cols; i++)
if (batt_temp <= pc_temp_ocv->temp[i] * DEGC_SCALE)
break;
if (batt_temp == pc_temp_ocv->temp[i] * DEGC_SCALE) {
slope = (pc_temp_ocv->ocv[row1][i] -
pc_temp_ocv->ocv[row2][i]);
if (slope <= 0) {
pr_warn("Slope=%d for pc=%d, using 1\n", slope, pc);
slope = 1;
}
slope *= 1000;
slope /= (pc_temp_ocv->percent[row1] -
pc_temp_ocv->percent[row2]);
return slope;
}
ocvrow1 = linear_interpolate(
pc_temp_ocv->ocv[row1][i - 1],
pc_temp_ocv->temp[i - 1] * DEGC_SCALE,
pc_temp_ocv->ocv[row1][i],
pc_temp_ocv->temp[i] * DEGC_SCALE,
batt_temp);
ocvrow2 = linear_interpolate(
pc_temp_ocv->ocv[row2][i - 1],
pc_temp_ocv->temp[i - 1] * DEGC_SCALE,
pc_temp_ocv->ocv[row2][i],
pc_temp_ocv->temp[i] * DEGC_SCALE,
batt_temp);
slope = (ocvrow1 - ocvrow2);
if (slope <= 0) {
pr_warn("Slope=%d for pc=%d, using 1\n", slope, pc);
slope = 1;
}
slope *= 1000;
slope /= (pc_temp_ocv->percent[row1] - pc_temp_ocv->percent[row2]);
return slope;
}
int interpolate_acc(struct ibat_temp_acc_lut *ibat_acc_lut,
int batt_temp, int ibat)
{
int i, accrow1, accrow2, rows, cols;
int row1 = 0;
int row2 = 0;
int acc;
rows = ibat_acc_lut->rows;
cols = ibat_acc_lut->cols;
if (ibat > ibat_acc_lut->ibat[rows - 1]) {
pr_debug("ibatt(%d) > max range(%d)\n", ibat,
ibat_acc_lut->ibat[rows - 1]);
row1 = rows - 1;
row2 = rows - 2;
} else if (ibat < ibat_acc_lut->ibat[0]) {
pr_debug("ibatt(%d) < max range(%d)\n", ibat,
ibat_acc_lut->ibat[0]);
row1 = 0;
row2 = 0;
} else {
for (i = 0; i < rows; i++) {
if (ibat == ibat_acc_lut->ibat[i]) {
row1 = i;
row2 = i;
break;
}
if (ibat < ibat_acc_lut->ibat[i]) {
row1 = i;
row2 = i - 1;
break;
}
}
}
if (batt_temp < ibat_acc_lut->temp[0] * DEGC_SCALE)
batt_temp = ibat_acc_lut->temp[0] * DEGC_SCALE;
if (batt_temp > ibat_acc_lut->temp[cols - 1] * DEGC_SCALE)
batt_temp = ibat_acc_lut->temp[cols - 1] * DEGC_SCALE;
for (i = 0; i < cols; i++)
if (batt_temp <= ibat_acc_lut->temp[i] * DEGC_SCALE)
break;
if (batt_temp == (ibat_acc_lut->temp[i] * DEGC_SCALE)) {
acc = linear_interpolate(
ibat_acc_lut->acc[row1][i],
ibat_acc_lut->ibat[row1],
ibat_acc_lut->acc[row2][i],
ibat_acc_lut->ibat[row2],
ibat);
return acc;
}
accrow1 = linear_interpolate(
ibat_acc_lut->acc[row1][i - 1],
ibat_acc_lut->temp[i - 1] * DEGC_SCALE,
ibat_acc_lut->acc[row1][i],
ibat_acc_lut->temp[i] * DEGC_SCALE,
batt_temp);
accrow2 = linear_interpolate(
ibat_acc_lut->acc[row2][i - 1],
ibat_acc_lut->temp[i - 1] * DEGC_SCALE,
ibat_acc_lut->acc[row2][i],
ibat_acc_lut->temp[i] * DEGC_SCALE,
batt_temp);
acc = linear_interpolate(accrow1,
ibat_acc_lut->ibat[row1],
accrow2,
ibat_acc_lut->ibat[row2],
ibat);
if (acc < 0)
acc = 0;
return acc;
}
|