1 /*
2  * Copyright ST-Ericsson 2012.
3  *
4  * Author: Arun Murthy <arun.murthy@stericsson.com>
5  * Licensed under GPLv2.
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_60U_ENA		0x01
252 #define BAT_CTRL_120U_ENA		0x02
253 #define BAT_CTRL_CMP_ENA		0x04
254 #define FORCE_BAT_CTRL_CMP_HIGH		0x08
255 #define BAT_CTRL_PULL_UP_ENA		0x10
256 
257 /* Battery type */
258 #define BATTERY_UNKNOWN			00
259 
260 /* Registers for pcut feature in ab8505 and ab9540 */
261 #define AB8505_RTC_PCUT_CTL_STATUS_REG	0x12
262 #define AB8505_RTC_PCUT_TIME_REG	0x13
263 #define AB8505_RTC_PCUT_MAX_TIME_REG	0x14
264 #define AB8505_RTC_PCUT_FLAG_TIME_REG	0x15
265 #define AB8505_RTC_PCUT_RESTART_REG	0x16
266 #define AB8505_RTC_PCUT_DEBOUNCE_REG	0x17
267 
268 /* USB Power Path constants for ab8540 */
269 #define BUS_VSYS_VOL_SELECT_MASK		0x06
270 #define BUS_VSYS_VOL_SELECT_3P6V		0x00
271 #define BUS_VSYS_VOL_SELECT_3P325V		0x02
272 #define BUS_VSYS_VOL_SELECT_3P9V		0x04
273 #define BUS_VSYS_VOL_SELECT_4P3V		0x06
274 #define BUS_POWER_PATH_MODE_ENA			0x01
275 #define BUS_PP_PRECHG_CURRENT_MASK		0x0E
276 #define BUS_POWER_PATH_PRECHG_ENA		0x01
277 
278 /**
279  * struct res_to_temp - defines one point in a temp to res curve. To
280  * be used in battery packs that combines the identification resistor with a
281  * NTC resistor.
282  * @temp:			battery pack temperature in Celcius
283  * @resist:			NTC resistor net total resistance
284  */
285 struct res_to_temp {
286 	int temp;
287 	int resist;
288 };
289 
290 /**
291  * struct batres_vs_temp - defines one point in a temp vs battery internal
292  * resistance curve.
293  * @temp:			battery pack temperature in Celcius
294  * @resist:			battery internal reistance in mOhm
295  */
296 struct batres_vs_temp {
297 	int temp;
298 	int resist;
299 };
300 
301 /* Forward declaration */
302 struct ab8500_fg;
303 
304 /**
305  * struct ab8500_fg_parameters - Fuel gauge algorithm parameters, in seconds
306  * if not specified
307  * @recovery_sleep_timer:	Time between measurements while recovering
308  * @recovery_total_time:	Total recovery time
309  * @init_timer:			Measurement interval during startup
310  * @init_discard_time:		Time we discard voltage measurement at startup
311  * @init_total_time:		Total init time during startup
312  * @high_curr_time:		Time current has to be high to go to recovery
313  * @accu_charging:		FG accumulation time while charging
314  * @accu_high_curr:		FG accumulation time in high current mode
315  * @high_curr_threshold:	High current threshold, in mA
316  * @lowbat_threshold:		Low battery threshold, in mV
317  * @battok_falling_th_sel0	Threshold in mV for battOk signal sel0
318  *				Resolution in 50 mV step.
319  * @battok_raising_th_sel1	Threshold in mV for battOk signal sel1
320  *				Resolution in 50 mV step.
321  * @user_cap_limit		Capacity reported from user must be within this
322  *				limit to be considered as sane, in percentage
323  *				points.
324  * @maint_thres			This is the threshold where we stop reporting
325  *				battery full while in maintenance, in per cent
326  * @pcut_enable:			Enable power cut feature in ab8505
327  * @pcut_max_time:		Max time threshold
328  * @pcut_flag_time:		Flagtime threshold
329  * @pcut_max_restart:		Max number of restarts
330  * @pcut_debunce_time:	Sets battery debounce time
331  */
332 struct ab8500_fg_parameters {
333 	int recovery_sleep_timer;
334 	int recovery_total_time;
335 	int init_timer;
336 	int init_discard_time;
337 	int init_total_time;
338 	int high_curr_time;
339 	int accu_charging;
340 	int accu_high_curr;
341 	int high_curr_threshold;
342 	int lowbat_threshold;
343 	int battok_falling_th_sel0;
344 	int battok_raising_th_sel1;
345 	int user_cap_limit;
346 	int maint_thres;
347 	bool pcut_enable;
348 	u8 pcut_max_time;
349 	u8 pcut_flag_time;
350 	u8 pcut_max_restart;
351 	u8 pcut_debunce_time;
352 };
353 
354 /**
355  * struct ab8500_charger_maximization - struct used by the board config.
356  * @use_maxi:		Enable maximization for this battery type
357  * @maxi_chg_curr:	Maximum charger current allowed
358  * @maxi_wait_cycles:	cycles to wait before setting charger current
359  * @charger_curr_step	delta between two charger current settings (mA)
360  */
361 struct ab8500_maxim_parameters {
362 	bool ena_maxi;
363 	int chg_curr;
364 	int wait_cycles;
365 	int charger_curr_step;
366 };
367 
368 /**
369  * struct ab8500_bm_capacity_levels - ab8500 capacity level data
370  * @critical:		critical capacity level in percent
371  * @low:		low capacity level in percent
372  * @normal:		normal capacity level in percent
373  * @high:		high capacity level in percent
374  * @full:		full capacity level in percent
375  */
376 struct ab8500_bm_capacity_levels {
377 	int critical;
378 	int low;
379 	int normal;
380 	int high;
381 	int full;
382 };
383 
384 /**
385  * struct ab8500_bm_charger_parameters - Charger specific parameters
386  * @usb_volt_max:	maximum allowed USB charger voltage in mV
387  * @usb_curr_max:	maximum allowed USB charger current in mA
388  * @ac_volt_max:	maximum allowed AC charger voltage in mV
389  * @ac_curr_max:	maximum allowed AC charger current in mA
390  */
391 struct ab8500_bm_charger_parameters {
392 	int usb_volt_max;
393 	int usb_curr_max;
394 	int ac_volt_max;
395 	int ac_curr_max;
396 };
397 
398 /**
399  * struct ab8500_bm_data - ab8500 battery management data
400  * @temp_under		under this temp, charging is stopped
401  * @temp_low		between this temp and temp_under charging is reduced
402  * @temp_high		between this temp and temp_over charging is reduced
403  * @temp_over		over this temp, charging is stopped
404  * @temp_interval_chg	temperature measurement interval in s when charging
405  * @temp_interval_nochg	temperature measurement interval in s when not charging
406  * @main_safety_tmr_h	safety timer for main charger
407  * @usb_safety_tmr_h	safety timer for usb charger
408  * @bkup_bat_v		voltage which we charge the backup battery with
409  * @bkup_bat_i		current which we charge the backup battery with
410  * @no_maintenance	indicates that maintenance charging is disabled
411  * @capacity_scaling    indicates whether capacity scaling is to be used
412  * @adc_therm		placement of thermistor, batctrl or battemp adc
413  * @chg_unknown_bat	flag to enable charging of unknown batteries
414  * @enable_overshoot	flag to enable VBAT overshoot control
415  * @fg_res		resistance of FG resistor in 0.1mOhm
416  * @n_btypes		number of elements in array bat_type
417  * @batt_id		index of the identified battery in array bat_type
418  * @interval_charging	charge alg cycle period time when charging (sec)
419  * @interval_not_charging charge alg cycle period time when not charging (sec)
420  * @temp_hysteresis	temperature hysteresis
421  * @gnd_lift_resistance	Battery ground to phone ground resistance (mOhm)
422  * @maxi:		maximization parameters
423  * @cap_levels		capacity in percent for the different capacity levels
424  * @bat_type		table of supported battery types
425  * @chg_params		charger parameters
426  * @fg_params		fuel gauge parameters
427  */
428 struct ab8500_bm_data {
429 	int temp_under;
430 	int temp_low;
431 	int temp_high;
432 	int temp_over;
433 	int temp_interval_chg;
434 	int temp_interval_nochg;
435 	int main_safety_tmr_h;
436 	int usb_safety_tmr_h;
437 	int bkup_bat_v;
438 	int bkup_bat_i;
439 	bool no_maintenance;
440 	bool capacity_scaling;
441 	bool chg_unknown_bat;
442 	bool enable_overshoot;
443 	enum abx500_adc_therm adc_therm;
444 	int fg_res;
445 	int n_btypes;
446 	int batt_id;
447 	int interval_charging;
448 	int interval_not_charging;
449 	int temp_hysteresis;
450 	int gnd_lift_resistance;
451 	const struct ab8500_maxim_parameters *maxi;
452 	const struct ab8500_bm_capacity_levels *cap_levels;
453 	const struct ab8500_bm_charger_parameters *chg_params;
454 	const struct ab8500_fg_parameters *fg_params;
455 };
456 
457 struct ab8500_btemp;
458 struct ab8500_gpadc;
459 struct ab8500_fg;
460 
461 #ifdef CONFIG_AB8500_BM
462 extern struct abx500_bm_data ab8500_bm_data;
463 
464 void ab8500_charger_usb_state_changed(u8 bm_usb_state, u16 mA);
465 struct ab8500_btemp *ab8500_btemp_get(void);
466 int ab8500_btemp_get_batctrl_temp(struct ab8500_btemp *btemp);
467 int ab8500_btemp_get_temp(struct ab8500_btemp *btemp);
468 struct ab8500_fg *ab8500_fg_get(void);
469 int ab8500_fg_inst_curr_blocking(struct ab8500_fg *dev);
470 int ab8500_fg_inst_curr_start(struct ab8500_fg *di);
471 int ab8500_fg_inst_curr_finalize(struct ab8500_fg *di, int *res);
472 int ab8500_fg_inst_curr_started(struct ab8500_fg *di);
473 int ab8500_fg_inst_curr_done(struct ab8500_fg *di);
474 
475 #else
476 static struct abx500_bm_data ab8500_bm_data;
477 #endif
478 #endif /* _AB8500_BM_H */
479