1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright ST-Ericsson 2012.
4  *
5  * Author: Arun Murthy <arun.murthy@stericsson.com>
6  */
7 
8 #ifndef _AB8500_BM_H
9 #define _AB8500_BM_H
10 
11 #include <linux/kernel.h>
12 #include <linux/mfd/abx500.h>
13 
14 /*
15  * System control 2 register offsets.
16  * bank = 0x02
17  */
18 #define AB8500_MAIN_WDOG_CTRL_REG	0x01
19 #define AB8500_LOW_BAT_REG		0x03
20 #define AB8500_BATT_OK_REG		0x04
21 /*
22  * USB/ULPI register offsets
23  * Bank : 0x5
24  */
25 #define AB8500_USB_LINE_STAT_REG	0x80
26 #define AB8500_USB_LINE_CTRL2_REG	0x82
27 #define AB8500_USB_LINK1_STAT_REG	0x94
28 
29 /*
30  * Charger / status register offfsets
31  * Bank : 0x0B
32  */
33 #define AB8500_CH_STATUS1_REG		0x00
34 #define AB8500_CH_STATUS2_REG		0x01
35 #define AB8500_CH_USBCH_STAT1_REG	0x02
36 #define AB8500_CH_USBCH_STAT2_REG	0x03
37 #define AB8540_CH_USBCH_STAT3_REG	0x04
38 #define AB8500_CH_STAT_REG		0x05
39 
40 /*
41  * Charger / control register offfsets
42  * Bank : 0x0B
43  */
44 #define AB8500_CH_VOLT_LVL_REG		0x40
45 #define AB8500_CH_VOLT_LVL_MAX_REG	0x41  /*Only in Cut2.0*/
46 #define AB8500_CH_OPT_CRNTLVL_REG	0x42
47 #define AB8500_CH_OPT_CRNTLVL_MAX_REG	0x43  /*Only in Cut2.0*/
48 #define AB8500_CH_WD_TIMER_REG		0x50
49 #define AB8500_CHARG_WD_CTRL		0x51
50 #define AB8500_BTEMP_HIGH_TH		0x52
51 #define AB8500_LED_INDICATOR_PWM_CTRL	0x53
52 #define AB8500_LED_INDICATOR_PWM_DUTY	0x54
53 #define AB8500_BATT_OVV			0x55
54 #define AB8500_CHARGER_CTRL		0x56
55 #define AB8500_BAT_CTRL_CURRENT_SOURCE	0x60  /*Only in Cut2.0*/
56 
57 /*
58  * Charger / main control register offsets
59  * Bank : 0x0B
60  */
61 #define AB8500_MCH_CTRL1		0x80
62 #define AB8500_MCH_CTRL2		0x81
63 #define AB8500_MCH_IPT_CURLVL_REG	0x82
64 #define AB8500_CH_WD_REG		0x83
65 
66 /*
67  * Charger / USB control register offsets
68  * Bank : 0x0B
69  */
70 #define AB8500_USBCH_CTRL1_REG		0xC0
71 #define AB8500_USBCH_CTRL2_REG		0xC1
72 #define AB8500_USBCH_IPT_CRNTLVL_REG	0xC2
73 #define AB8540_USB_PP_MODE_REG		0xC5
74 #define AB8540_USB_PP_CHR_REG		0xC6
75 
76 /*
77  * Gas Gauge register offsets
78  * Bank : 0x0C
79  */
80 #define AB8500_GASG_CC_CTRL_REG		0x00
81 #define AB8500_GASG_CC_ACCU1_REG	0x01
82 #define AB8500_GASG_CC_ACCU2_REG	0x02
83 #define AB8500_GASG_CC_ACCU3_REG	0x03
84 #define AB8500_GASG_CC_ACCU4_REG	0x04
85 #define AB8500_GASG_CC_SMPL_CNTRL_REG	0x05
86 #define AB8500_GASG_CC_SMPL_CNTRH_REG	0x06
87 #define AB8500_GASG_CC_SMPL_CNVL_REG	0x07
88 #define AB8500_GASG_CC_SMPL_CNVH_REG	0x08
89 #define AB8500_GASG_CC_CNTR_AVGOFF_REG	0x09
90 #define AB8500_GASG_CC_OFFSET_REG	0x0A
91 #define AB8500_GASG_CC_NCOV_ACCU	0x10
92 #define AB8500_GASG_CC_NCOV_ACCU_CTRL	0x11
93 #define AB8500_GASG_CC_NCOV_ACCU_LOW	0x12
94 #define AB8500_GASG_CC_NCOV_ACCU_MED	0x13
95 #define AB8500_GASG_CC_NCOV_ACCU_HIGH	0x14
96 
97 /*
98  * Interrupt register offsets
99  * Bank : 0x0E
100  */
101 #define AB8500_IT_SOURCE2_REG		0x01
102 #define AB8500_IT_SOURCE21_REG		0x14
103 
104 /*
105  * RTC register offsets
106  * Bank: 0x0F
107  */
108 #define AB8500_RTC_BACKUP_CHG_REG	0x0C
109 #define AB8500_RTC_CC_CONF_REG		0x01
110 #define AB8500_RTC_CTRL_REG		0x0B
111 #define AB8500_RTC_CTRL1_REG		0x11
112 
113 /*
114  * OTP register offsets
115  * Bank : 0x15
116  */
117 #define AB8500_OTP_CONF_15		0x0E
118 
119 /* GPADC constants from AB8500 spec, UM0836 */
120 #define ADC_RESOLUTION			1024
121 #define ADC_CH_MAIN_MIN			0
122 #define ADC_CH_MAIN_MAX			20030
123 #define ADC_CH_VBUS_MIN			0
124 #define ADC_CH_VBUS_MAX			20030
125 #define ADC_CH_VBAT_MIN			2300
126 #define ADC_CH_VBAT_MAX			4800
127 #define ADC_CH_BKBAT_MIN		0
128 #define ADC_CH_BKBAT_MAX		3200
129 
130 /* Main charge i/p current */
131 #define MAIN_CH_IP_CUR_0P9A		0x80
132 #define MAIN_CH_IP_CUR_1P0A		0x90
133 #define MAIN_CH_IP_CUR_1P1A		0xA0
134 #define MAIN_CH_IP_CUR_1P2A		0xB0
135 #define MAIN_CH_IP_CUR_1P3A		0xC0
136 #define MAIN_CH_IP_CUR_1P4A		0xD0
137 #define MAIN_CH_IP_CUR_1P5A		0xE0
138 
139 /* ChVoltLevel */
140 #define CH_VOL_LVL_3P5			0x00
141 #define CH_VOL_LVL_4P0			0x14
142 #define CH_VOL_LVL_4P05			0x16
143 #define CH_VOL_LVL_4P1			0x1B
144 #define CH_VOL_LVL_4P15			0x20
145 #define CH_VOL_LVL_4P2			0x25
146 #define CH_VOL_LVL_4P6			0x4D
147 
148 /* ChOutputCurrentLevel */
149 #define CH_OP_CUR_LVL_0P1		0x00
150 #define CH_OP_CUR_LVL_0P2		0x01
151 #define CH_OP_CUR_LVL_0P3		0x02
152 #define CH_OP_CUR_LVL_0P4		0x03
153 #define CH_OP_CUR_LVL_0P5		0x04
154 #define CH_OP_CUR_LVL_0P6		0x05
155 #define CH_OP_CUR_LVL_0P7		0x06
156 #define CH_OP_CUR_LVL_0P8		0x07
157 #define CH_OP_CUR_LVL_0P9		0x08
158 #define CH_OP_CUR_LVL_1P4		0x0D
159 #define CH_OP_CUR_LVL_1P5		0x0E
160 #define CH_OP_CUR_LVL_1P6		0x0F
161 #define CH_OP_CUR_LVL_2P		0x3F
162 
163 /* BTEMP High thermal limits */
164 #define BTEMP_HIGH_TH_57_0		0x00
165 #define BTEMP_HIGH_TH_52		0x01
166 #define BTEMP_HIGH_TH_57_1		0x02
167 #define BTEMP_HIGH_TH_62		0x03
168 
169 /* current is mA */
170 #define USB_0P1A			100
171 #define USB_0P2A			200
172 #define USB_0P3A			300
173 #define USB_0P4A			400
174 #define USB_0P5A			500
175 
176 #define LOW_BAT_3P1V			0x20
177 #define LOW_BAT_2P3V			0x00
178 #define LOW_BAT_RESET			0x01
179 #define LOW_BAT_ENABLE			0x01
180 
181 /* Backup battery constants */
182 #define BUP_ICH_SEL_50UA		0x00
183 #define BUP_ICH_SEL_150UA		0x04
184 #define BUP_ICH_SEL_300UA		0x08
185 #define BUP_ICH_SEL_700UA		0x0C
186 
187 enum bup_vch_sel {
188 	BUP_VCH_SEL_2P5V,
189 	BUP_VCH_SEL_2P6V,
190 	BUP_VCH_SEL_2P8V,
191 	BUP_VCH_SEL_3P1V,
192 	/*
193 	 * Note that the following 5 values 2.7v, 2.9v, 3.0v, 3.2v, 3.3v
194 	 * are only available on ab8540. You can't choose these 5
195 	 * voltage on ab8500/ab8505/ab9540.
196 	 */
197 	BUP_VCH_SEL_2P7V,
198 	BUP_VCH_SEL_2P9V,
199 	BUP_VCH_SEL_3P0V,
200 	BUP_VCH_SEL_3P2V,
201 	BUP_VCH_SEL_3P3V,
202 };
203 
204 #define BUP_VCH_RANGE		0x02
205 #define VBUP33_VRTCN		0x01
206 
207 /* Battery OVV constants */
208 #define BATT_OVV_ENA			0x02
209 #define BATT_OVV_TH_3P7			0x00
210 #define BATT_OVV_TH_4P75		0x01
211 
212 /* A value to indicate over voltage */
213 #define BATT_OVV_VALUE			4750
214 
215 /* VBUS OVV constants */
216 #define VBUS_OVV_SELECT_MASK		0x78
217 #define VBUS_OVV_SELECT_5P6V		0x00
218 #define VBUS_OVV_SELECT_5P7V		0x08
219 #define VBUS_OVV_SELECT_5P8V		0x10
220 #define VBUS_OVV_SELECT_5P9V		0x18
221 #define VBUS_OVV_SELECT_6P0V		0x20
222 #define VBUS_OVV_SELECT_6P1V		0x28
223 #define VBUS_OVV_SELECT_6P2V		0x30
224 #define VBUS_OVV_SELECT_6P3V		0x38
225 
226 #define VBUS_AUTO_IN_CURR_LIM_ENA	0x04
227 
228 /* Fuel Gauge constants */
229 #define RESET_ACCU			0x02
230 #define READ_REQ			0x01
231 #define CC_DEEP_SLEEP_ENA		0x02
232 #define CC_PWR_UP_ENA			0x01
233 #define CC_SAMPLES_40			0x28
234 #define RD_NCONV_ACCU_REQ		0x01
235 #define CC_CALIB			0x08
236 #define CC_INTAVGOFFSET_ENA		0x10
237 #define CC_MUXOFFSET			0x80
238 #define CC_INT_CAL_N_AVG_MASK		0x60
239 #define CC_INT_CAL_SAMPLES_16		0x40
240 #define CC_INT_CAL_SAMPLES_8		0x20
241 #define CC_INT_CAL_SAMPLES_4		0x00
242 
243 /* RTC constants */
244 #define RTC_BUP_CH_ENA			0x10
245 
246 /* BatCtrl Current Source Constants */
247 #define BAT_CTRL_7U_ENA			0x01
248 #define BAT_CTRL_20U_ENA		0x02
249 #define BAT_CTRL_18U_ENA		0x01
250 #define BAT_CTRL_16U_ENA		0x02
251 #define BAT_CTRL_CMP_ENA		0x04
252 #define FORCE_BAT_CTRL_CMP_HIGH		0x08
253 #define BAT_CTRL_PULL_UP_ENA		0x10
254 
255 /* Battery type */
256 #define BATTERY_UNKNOWN			00
257 
258 /* Registers for pcut feature in ab8505 and ab9540 */
259 #define AB8505_RTC_PCUT_CTL_STATUS_REG	0x12
260 #define AB8505_RTC_PCUT_TIME_REG	0x13
261 #define AB8505_RTC_PCUT_MAX_TIME_REG	0x14
262 #define AB8505_RTC_PCUT_FLAG_TIME_REG	0x15
263 #define AB8505_RTC_PCUT_RESTART_REG	0x16
264 #define AB8505_RTC_PCUT_DEBOUNCE_REG	0x17
265 
266 /* USB Power Path constants for ab8540 */
267 #define BUS_VSYS_VOL_SELECT_MASK		0x06
268 #define BUS_VSYS_VOL_SELECT_3P6V		0x00
269 #define BUS_VSYS_VOL_SELECT_3P325V		0x02
270 #define BUS_VSYS_VOL_SELECT_3P9V		0x04
271 #define BUS_VSYS_VOL_SELECT_4P3V		0x06
272 #define BUS_POWER_PATH_MODE_ENA			0x01
273 #define BUS_PP_PRECHG_CURRENT_MASK		0x0E
274 #define BUS_POWER_PATH_PRECHG_ENA		0x01
275 
276 /**
277  * struct res_to_temp - defines one point in a temp to res curve. To
278  * be used in battery packs that combines the identification resistor with a
279  * NTC resistor.
280  * @temp:			battery pack temperature in Celsius
281  * @resist:			NTC resistor net total resistance
282  */
283 struct res_to_temp {
284 	int temp;
285 	int resist;
286 };
287 
288 /**
289  * struct batres_vs_temp - defines one point in a temp vs battery internal
290  * resistance curve.
291  * @temp:			battery pack temperature in Celsius
292  * @resist:			battery internal reistance in mOhm
293  */
294 struct batres_vs_temp {
295 	int temp;
296 	int resist;
297 };
298 
299 /* Forward declaration */
300 struct ab8500_fg;
301 
302 /**
303  * struct ab8500_fg_parameters - Fuel gauge algorithm parameters, in seconds
304  * if not specified
305  * @recovery_sleep_timer:	Time between measurements while recovering
306  * @recovery_total_time:	Total recovery time
307  * @init_timer:			Measurement interval during startup
308  * @init_discard_time:		Time we discard voltage measurement at startup
309  * @init_total_time:		Total init time during startup
310  * @high_curr_time:		Time current has to be high to go to recovery
311  * @accu_charging:		FG accumulation time while charging
312  * @accu_high_curr:		FG accumulation time in high current mode
313  * @high_curr_threshold:	High current threshold, in mA
314  * @lowbat_threshold:		Low battery threshold, in mV
315  * @battok_falling_th_sel0	Threshold in mV for battOk signal sel0
316  *				Resolution in 50 mV step.
317  * @battok_raising_th_sel1	Threshold in mV for battOk signal sel1
318  *				Resolution in 50 mV step.
319  * @user_cap_limit		Capacity reported from user must be within this
320  *				limit to be considered as sane, in percentage
321  *				points.
322  * @maint_thres			This is the threshold where we stop reporting
323  *				battery full while in maintenance, in per cent
324  * @pcut_enable:			Enable power cut feature in ab8505
325  * @pcut_max_time:		Max time threshold
326  * @pcut_flag_time:		Flagtime threshold
327  * @pcut_max_restart:		Max number of restarts
328  * @pcut_debunce_time:	Sets battery debounce time
329  */
330 struct ab8500_fg_parameters {
331 	int recovery_sleep_timer;
332 	int recovery_total_time;
333 	int init_timer;
334 	int init_discard_time;
335 	int init_total_time;
336 	int high_curr_time;
337 	int accu_charging;
338 	int accu_high_curr;
339 	int high_curr_threshold;
340 	int lowbat_threshold;
341 	int battok_falling_th_sel0;
342 	int battok_raising_th_sel1;
343 	int user_cap_limit;
344 	int maint_thres;
345 	bool pcut_enable;
346 	u8 pcut_max_time;
347 	u8 pcut_flag_time;
348 	u8 pcut_max_restart;
349 	u8 pcut_debunce_time;
350 };
351 
352 /**
353  * struct ab8500_charger_maximization - struct used by the board config.
354  * @use_maxi:		Enable maximization for this battery type
355  * @maxi_chg_curr:	Maximum charger current allowed
356  * @maxi_wait_cycles:	cycles to wait before setting charger current
357  * @charger_curr_step	delta between two charger current settings (mA)
358  */
359 struct ab8500_maxim_parameters {
360 	bool ena_maxi;
361 	int chg_curr;
362 	int wait_cycles;
363 	int charger_curr_step;
364 };
365 
366 /**
367  * struct ab8500_bm_capacity_levels - ab8500 capacity level data
368  * @critical:		critical capacity level in percent
369  * @low:		low capacity level in percent
370  * @normal:		normal capacity level in percent
371  * @high:		high capacity level in percent
372  * @full:		full capacity level in percent
373  */
374 struct ab8500_bm_capacity_levels {
375 	int critical;
376 	int low;
377 	int normal;
378 	int high;
379 	int full;
380 };
381 
382 /**
383  * struct ab8500_bm_charger_parameters - Charger specific parameters
384  * @usb_volt_max:	maximum allowed USB charger voltage in mV
385  * @usb_curr_max:	maximum allowed USB charger current in mA
386  * @ac_volt_max:	maximum allowed AC charger voltage in mV
387  * @ac_curr_max:	maximum allowed AC charger current in mA
388  */
389 struct ab8500_bm_charger_parameters {
390 	int usb_volt_max;
391 	int usb_curr_max;
392 	int ac_volt_max;
393 	int ac_curr_max;
394 };
395 
396 /**
397  * struct ab8500_bm_data - ab8500 battery management data
398  * @temp_under		under this temp, charging is stopped
399  * @temp_low		between this temp and temp_under charging is reduced
400  * @temp_high		between this temp and temp_over charging is reduced
401  * @temp_over		over this temp, charging is stopped
402  * @temp_interval_chg	temperature measurement interval in s when charging
403  * @temp_interval_nochg	temperature measurement interval in s when not charging
404  * @main_safety_tmr_h	safety timer for main charger
405  * @usb_safety_tmr_h	safety timer for usb charger
406  * @bkup_bat_v		voltage which we charge the backup battery with
407  * @bkup_bat_i		current which we charge the backup battery with
408  * @no_maintenance	indicates that maintenance charging is disabled
409  * @capacity_scaling    indicates whether capacity scaling is to be used
410  * @adc_therm		placement of thermistor, batctrl or battemp adc
411  * @chg_unknown_bat	flag to enable charging of unknown batteries
412  * @enable_overshoot	flag to enable VBAT overshoot control
413  * @fg_res		resistance of FG resistor in 0.1mOhm
414  * @n_btypes		number of elements in array bat_type
415  * @batt_id		index of the identified battery in array bat_type
416  * @interval_charging	charge alg cycle period time when charging (sec)
417  * @interval_not_charging charge alg cycle period time when not charging (sec)
418  * @temp_hysteresis	temperature hysteresis
419  * @gnd_lift_resistance	Battery ground to phone ground resistance (mOhm)
420  * @maxi:		maximization parameters
421  * @cap_levels		capacity in percent for the different capacity levels
422  * @bat_type		table of supported battery types
423  * @chg_params		charger parameters
424  * @fg_params		fuel gauge parameters
425  */
426 struct ab8500_bm_data {
427 	int temp_under;
428 	int temp_low;
429 	int temp_high;
430 	int temp_over;
431 	int temp_interval_chg;
432 	int temp_interval_nochg;
433 	int main_safety_tmr_h;
434 	int usb_safety_tmr_h;
435 	int bkup_bat_v;
436 	int bkup_bat_i;
437 	bool no_maintenance;
438 	bool capacity_scaling;
439 	bool chg_unknown_bat;
440 	bool enable_overshoot;
441 	enum abx500_adc_therm adc_therm;
442 	int fg_res;
443 	int n_btypes;
444 	int batt_id;
445 	int interval_charging;
446 	int interval_not_charging;
447 	int temp_hysteresis;
448 	int gnd_lift_resistance;
449 	const struct ab8500_maxim_parameters *maxi;
450 	const struct ab8500_bm_capacity_levels *cap_levels;
451 	const struct ab8500_bm_charger_parameters *chg_params;
452 	const struct ab8500_fg_parameters *fg_params;
453 };
454 
455 struct ab8500_btemp;
456 struct ab8500_gpadc;
457 struct ab8500_fg;
458 
459 #ifdef CONFIG_AB8500_BM
460 extern struct abx500_bm_data ab8500_bm_data;
461 
462 void ab8500_charger_usb_state_changed(u8 bm_usb_state, u16 mA);
463 struct ab8500_btemp *ab8500_btemp_get(void);
464 int ab8500_btemp_get_batctrl_temp(struct ab8500_btemp *btemp);
465 int ab8500_btemp_get_temp(struct ab8500_btemp *btemp);
466 struct ab8500_fg *ab8500_fg_get(void);
467 int ab8500_fg_inst_curr_blocking(struct ab8500_fg *dev);
468 int ab8500_fg_inst_curr_start(struct ab8500_fg *di);
469 int ab8500_fg_inst_curr_finalize(struct ab8500_fg *di, int *res);
470 int ab8500_fg_inst_curr_started(struct ab8500_fg *di);
471 int ab8500_fg_inst_curr_done(struct ab8500_fg *di);
472 
473 #else
474 static struct abx500_bm_data ab8500_bm_data;
475 #endif
476 #endif /* _AB8500_BM_H */
477