blob: 39517f83c875d49a266f8a0e360f25ed6f3ac03a [file] [log] [blame]
Subbaraman Narayanamurthy92264bd2017-03-23 00:52:12 -07001/* Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#ifndef __BMS_BATTERYDATA_H
14#define __BMS_BATTERYDATA_H
15
16#include <linux/errno.h>
17
18#define FCC_CC_COLS 5
19#define FCC_TEMP_COLS 8
20
21#define PC_CC_ROWS 31
22#define PC_CC_COLS 13
23
24#define PC_TEMP_ROWS 31
25#define PC_TEMP_COLS 8
26
27#define ACC_IBAT_ROWS 4
28#define ACC_TEMP_COLS 3
29
30#define MAX_SINGLE_LUT_COLS 20
31
32#define MAX_BATT_ID_NUM 4
33#define DEGC_SCALE 10
34
35struct single_row_lut {
36 int x[MAX_SINGLE_LUT_COLS];
37 int y[MAX_SINGLE_LUT_COLS];
38 int cols;
39};
40
41/**
42 * struct sf_lut -
43 * @rows: number of percent charge entries should be <= PC_CC_ROWS
44 * @cols: number of charge cycle entries should be <= PC_CC_COLS
45 * @row_entries: the charge cycles/temperature at which sf data
46 * is available in the table.
47 * The charge cycles must be in increasing order from 0 to rows.
48 * @percent: the percent charge at which sf data is available in the table
49 * The percentcharge must be in decreasing order from 0 to cols.
50 * @sf: the scaling factor data
51 */
52struct sf_lut {
53 int rows;
54 int cols;
55 int row_entries[PC_CC_COLS];
56 int percent[PC_CC_ROWS];
57 int sf[PC_CC_ROWS][PC_CC_COLS];
58};
59
60/**
61 * struct pc_temp_ocv_lut -
62 * @rows: number of percent charge entries should be <= PC_TEMP_ROWS
63 * @cols: number of temperature entries should be <= PC_TEMP_COLS
64 * @temp: the temperatures at which ocv data is available in the table
65 * The temperatures must be in increasing order from 0 to rows.
66 * @percent: the percent charge at which ocv data is available in the table
67 * The percentcharge must be in decreasing order from 0 to cols.
68 * @ocv: the open circuit voltage
69 */
70struct pc_temp_ocv_lut {
71 int rows;
72 int cols;
73 int temp[PC_TEMP_COLS];
74 int percent[PC_TEMP_ROWS];
75 int ocv[PC_TEMP_ROWS][PC_TEMP_COLS];
76};
77
78struct ibat_temp_acc_lut {
79 int rows;
80 int cols;
81 int temp[ACC_TEMP_COLS];
82 int ibat[ACC_IBAT_ROWS];
83 int acc[ACC_IBAT_ROWS][ACC_TEMP_COLS];
84};
85
86struct batt_ids {
87 int kohm[MAX_BATT_ID_NUM];
88 int num;
89};
90
91enum battery_type {
92 BATT_UNKNOWN = 0,
93 BATT_PALLADIUM,
94 BATT_DESAY,
95 BATT_OEM,
96 BATT_QRD_4V35_2000MAH,
97 BATT_QRD_4V2_1300MAH,
98};
99
100/**
101 * struct bms_battery_data -
102 * @fcc: full charge capacity (mAmpHour)
103 * @fcc_temp_lut: table to get fcc at a given temp
104 * @pc_temp_ocv_lut: table to get percent charge given batt temp and cycles
105 * @pc_sf_lut: table to get percent charge scaling factor given cycles
106 * and percent charge
107 * @rbatt_sf_lut: table to get battery resistance scaling factor given
108 * temperature and percent charge
109 * @default_rbatt_mohm: the default value of battery resistance to use when
110 * readings from bms are not available.
111 * @delta_rbatt_mohm: the resistance to be added towards lower soc to
112 * compensate for battery capacitance.
113 * @rbatt_capacitve_mohm: the resistance to be added to compensate for
114 * battery capacitance
115 * @flat_ocv_threshold_uv: the voltage where the battery's discharge curve
116 * starts flattening out.
117 * @max_voltage_uv: max voltage of the battery
118 * @cutoff_uv: cutoff voltage of the battery
119 * @iterm_ua: termination current of the battery when charging
120 * to 100%
121 * @batt_id_kohm: the best matched battery id resistor value
122 * @fastchg_current_ma: maximum fast charge current
123 * @fg_cc_cv_threshold_mv: CC to CV threashold voltage
124 */
125
126struct bms_battery_data {
127 unsigned int fcc;
128 struct single_row_lut *fcc_temp_lut;
129 struct single_row_lut *fcc_sf_lut;
130 struct pc_temp_ocv_lut *pc_temp_ocv_lut;
131 struct ibat_temp_acc_lut *ibat_acc_lut;
132 struct sf_lut *pc_sf_lut;
133 struct sf_lut *rbatt_sf_lut;
134 int default_rbatt_mohm;
135 int delta_rbatt_mohm;
136 int rbatt_capacitive_mohm;
137 int flat_ocv_threshold_uv;
138 int max_voltage_uv;
139 int cutoff_uv;
140 int iterm_ua;
141 int batt_id_kohm;
142 int fastchg_current_ma;
143 int fg_cc_cv_threshold_mv;
144 const char *battery_type;
145};
146
147#define is_between(left, right, value) \
148 (((left) >= (right) && (left) >= (value) \
149 && (value) >= (right)) \
150 || ((left) <= (right) && (left) <= (value) \
151 && (value) <= (right)))
152
153#if defined(CONFIG_PM8921_BMS) || \
154 defined(CONFIG_PM8921_BMS_MODULE) || \
155 defined(CONFIG_QPNP_BMS) || \
156 defined(CONFIG_QPNP_VM_BMS)
157extern struct bms_battery_data palladium_1500_data;
158extern struct bms_battery_data desay_5200_data;
159extern struct bms_battery_data oem_batt_data;
160extern struct bms_battery_data QRD_4v35_2000mAh_data;
161extern struct bms_battery_data qrd_4v2_1300mah_data;
162
163int interpolate_fcc(struct single_row_lut *fcc_temp_lut, int batt_temp);
164int interpolate_scalingfactor(struct sf_lut *sf_lut, int row_entry, int pc);
165int interpolate_scalingfactor_fcc(struct single_row_lut *fcc_sf_lut,
166 int cycles);
167int interpolate_pc(struct pc_temp_ocv_lut *pc_temp_ocv,
168 int batt_temp_degc, int ocv);
169int interpolate_ocv(struct pc_temp_ocv_lut *pc_temp_ocv,
170 int batt_temp_degc, int pc);
171int interpolate_slope(struct pc_temp_ocv_lut *pc_temp_ocv,
172 int batt_temp, int pc);
173int interpolate_acc(struct ibat_temp_acc_lut *ibat_acc_lut,
174 int batt_temp, int ibat);
175int linear_interpolate(int y0, int x0, int y1, int x1, int x);
176#else
177static inline int interpolate_fcc(struct single_row_lut *fcc_temp_lut,
178 int batt_temp)
179{
180 return -EINVAL;
181}
182static inline int interpolate_scalingfactor(struct sf_lut *sf_lut,
183 int row_entry, int pc)
184{
185 return -EINVAL;
186}
187static inline int interpolate_scalingfactor_fcc(
188 struct single_row_lut *fcc_sf_lut, int cycles)
189{
190 return -EINVAL;
191}
192static inline int interpolate_pc(struct pc_temp_ocv_lut *pc_temp_ocv,
193 int batt_temp_degc, int ocv)
194{
195 return -EINVAL;
196}
197static inline int interpolate_ocv(struct pc_temp_ocv_lut *pc_temp_ocv,
198 int batt_temp_degc, int pc)
199{
200 return -EINVAL;
201}
202static inline int interpolate_slope(struct pc_temp_ocv_lut *pc_temp_ocv,
203 int batt_temp, int pc)
204{
205 return -EINVAL;
206}
207static inline int linear_interpolate(int y0, int x0, int y1, int x1, int x)
208{
209 return -EINVAL;
210}
211static inline int interpolate_acc(struct ibat_temp_acc_lut *ibat_acc_lut,
212 int batt_temp, int ibat)
213{
214 return -EINVAL;
215}
216#endif
217
218#endif