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 res_to_temp table must be strictly sorted by falling
48  * resistance values to work.
49  */
50 static const struct ab8500_res_to_temp temp_tbl[] = {
51 	{-5, 214834},
52 	{ 0, 162943},
53 	{ 5, 124820},
54 	{10,  96520},
55 	{15,  75306},
56 	{20,  59254},
57 	{25,  47000},
58 	{30,  37566},
59 	{35,  30245},
60 	{40,  24520},
61 	{45,  20010},
62 	{50,  16432},
63 	{55,  13576},
64 	{60,  11280},
65 	{65,   9425},
66 };
67 
68 /*
69  * Note that the batres_vs_temp table must be strictly sorted by falling
70  * temperature values to work. Factory resistance is 300 mOhm and the
71  * resistance values to the right are percentages of 300 mOhm.
72  */
73 static struct power_supply_resistance_temp_table temp_to_batres_tbl_thermistor[] = {
74 	{ .temp = 40, .resistance = 40 /* 120 mOhm */ },
75 	{ .temp = 30, .resistance = 45 /* 135 mOhm */ },
76 	{ .temp = 20, .resistance = 55 /* 165 mOhm */ },
77 	{ .temp = 10, .resistance = 77 /* 230 mOhm */ },
78 	{ .temp = 00, .resistance = 108 /* 325 mOhm */ },
79 	{ .temp = -10, .resistance = 158 /* 445 mOhm */ },
80 	{ .temp = -20, .resistance = 198 /* 595 mOhm */ },
81 };
82 
83 /* Default battery type for reference designs is the unknown type */
84 static struct ab8500_battery_type bat_type_thermistor_unknown = {
85 	.resis_high = 0,
86 	.resis_low = 0,
87 	.maint_a_cur_lvl = 400,
88 	.maint_a_vol_lvl = 4050,
89 	.maint_a_chg_timer_h = 60,
90 	.maint_b_cur_lvl = 400,
91 	.maint_b_vol_lvl = 4000,
92 	.maint_b_chg_timer_h = 200,
93 	.low_high_cur_lvl = 300,
94 	.low_high_vol_lvl = 4000,
95 	.n_temp_tbl_elements = ARRAY_SIZE(temp_tbl),
96 	.r_to_t_tbl = temp_tbl,
97 };
98 
99 static const struct ab8500_bm_capacity_levels cap_levels = {
100 	.critical	= 2,
101 	.low		= 10,
102 	.normal		= 70,
103 	.high		= 95,
104 	.full		= 100,
105 };
106 
107 static const struct ab8500_fg_parameters fg = {
108 	.recovery_sleep_timer = 10,
109 	.recovery_total_time = 100,
110 	.init_timer = 1,
111 	.init_discard_time = 5,
112 	.init_total_time = 40,
113 	.high_curr_time = 60,
114 	.accu_charging = 30,
115 	.accu_high_curr = 30,
116 	.high_curr_threshold_ua = 50000,
117 	.lowbat_threshold_uv = 3100000,
118 	.battok_falling_th_sel0 = 2860,
119 	.battok_raising_th_sel1 = 2860,
120 	.maint_thres = 95,
121 	.user_cap_limit = 15,
122 	.pcut_enable = 1,
123 	.pcut_max_time = 127,
124 	.pcut_flag_time = 112,
125 	.pcut_max_restart = 15,
126 	.pcut_debounce_time = 2,
127 };
128 
129 static const struct ab8500_maxim_parameters ab8500_maxi_params = {
130 	.ena_maxi = true,
131 	.chg_curr_ua = 910000,
132 	.wait_cycles = 10,
133 	.charger_curr_step_ua = 100000,
134 };
135 
136 static const struct ab8500_bm_charger_parameters chg = {
137 	.usb_volt_max_uv	= 5500000,
138 	.usb_curr_max_ua	= 1500000,
139 	.ac_volt_max_uv		= 7500000,
140 	.ac_curr_max_ua		= 1500000,
141 };
142 
143 /* This is referenced directly in the charger code */
144 struct ab8500_bm_data ab8500_bm_data = {
145 	.main_safety_tmr_h      = 4,
146 	.temp_interval_chg      = 20,
147 	.temp_interval_nochg    = 120,
148 	.usb_safety_tmr_h       = 4,
149 	.bkup_bat_v             = BUP_VCH_SEL_2P6V,
150 	.bkup_bat_i             = BUP_ICH_SEL_150UA,
151 	.no_maintenance         = false,
152 	.capacity_scaling       = false,
153 	.adc_therm              = AB8500_ADC_THERM_BATCTRL,
154 	.chg_unknown_bat        = false,
155 	.enable_overshoot       = false,
156 	.fg_res                 = 100,
157 	.cap_levels             = &cap_levels,
158 	.bat_type               = &bat_type_thermistor_unknown,
159 	.interval_charging      = 5,
160 	.interval_not_charging  = 120,
161 	.gnd_lift_resistance    = 34,
162 	.maxi                   = &ab8500_maxi_params,
163 	.chg_params             = &chg,
164 	.fg_params              = &fg,
165 };
166 
167 int ab8500_bm_of_probe(struct power_supply *psy,
168 		       struct ab8500_bm_data *bm)
169 {
170 	struct power_supply_battery_info *bi;
171 	struct device *dev = &psy->dev;
172 	int ret;
173 
174 	ret = power_supply_get_battery_info(psy, &bm->bi);
175 	if (ret) {
176 		dev_err(dev, "cannot retrieve battery info\n");
177 		return ret;
178 	}
179 	bi = bm->bi;
180 
181 	/* Fill in defaults for any data missing from the device tree */
182 	if (bi->charge_full_design_uah < 0)
183 		/* The default capacity is 612 mAh for unknown batteries */
184 		bi->charge_full_design_uah = 612000;
185 
186 	/*
187 	 * All of these voltages need to be specified or we will simply
188 	 * fall back to safe defaults.
189 	 */
190 	if ((bi->voltage_min_design_uv < 0) ||
191 	    (bi->voltage_max_design_uv < 0) ||
192 	    (bi->overvoltage_limit_uv < 0)) {
193 		/* Nominal voltage is 3.7V for unknown batteries */
194 		bi->voltage_min_design_uv = 3700000;
195 		bi->voltage_max_design_uv = 3700000;
196 		/* Termination voltage (overcharge limit) 4.05V */
197 		bi->overvoltage_limit_uv = 4050000;
198 	}
199 
200 	if (bi->constant_charge_current_max_ua < 0)
201 		bi->constant_charge_current_max_ua = 400000;
202 
203 	if (bi->constant_charge_voltage_max_uv < 0)
204 		bi->constant_charge_voltage_max_uv = 4100000;
205 
206 	if (bi->charge_term_current_ua)
207 		/* Charging stops when we drop below this current */
208 		bi->charge_term_current_ua = 200000;
209 
210 	/*
211 	 * Internal resistance and factory resistance are tightly coupled
212 	 * so both MUST be defined or we fall back to defaults.
213 	 */
214 	if ((bi->factory_internal_resistance_uohm < 0) ||
215 	    !bi->resist_table) {
216 		bi->factory_internal_resistance_uohm = 300000;
217 		bi->resist_table = temp_to_batres_tbl_thermistor;
218 		bi->resist_table_size = ARRAY_SIZE(temp_to_batres_tbl_thermistor);
219 	}
220 
221 	if (!bi->ocv_table[0]) {
222 		/* Default capacity table at say 25 degrees Celsius */
223 		bi->ocv_temp[0] = 25;
224 		bi->ocv_table[0] = ocv_cap_tbl;
225 		bi->ocv_table_size[0] = ARRAY_SIZE(ocv_cap_tbl);
226 	}
227 
228 	if (bi->temp_min == INT_MIN)
229 		bi->temp_min = AB8500_TEMP_UNDER;
230 	if (bi->temp_max == INT_MAX)
231 		bi->temp_max = AB8500_TEMP_OVER;
232 	if (bi->temp_alert_min == INT_MIN)
233 		bi->temp_alert_min = AB8500_TEMP_LOW;
234 	if (bi->temp_alert_max == INT_MAX)
235 		bi->temp_alert_max = AB8500_TEMP_HIGH;
236 	bm->temp_hysteresis = AB8500_TEMP_HYSTERESIS;
237 
238 	return 0;
239 }
240 
241 void ab8500_bm_of_remove(struct power_supply *psy,
242 			 struct ab8500_bm_data *bm)
243 {
244 	power_supply_put_battery_info(psy, bm->bi);
245 }
246