summaryrefslogtreecommitdiff
path: root/drivers/battery/sprd27x3_fuelgauge4samsung.c
blob: ced830ae17829e953b1da7971b3dfe078932aa08 (plain)
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
/*
 * Copyright (C) 2013 Spreadtrum Communications Inc.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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.
 */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/err.h>
#include <soc/sprd/hardware.h>
#include <soc/sprd/sci.h>
#include <soc/sprd/sci_glb_regs.h>
#include <linux/io.h>
#include <soc/sprd/adi.h>
#include <linux/wakelock.h>
#include <linux/hrtimer.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>
#include <linux/battery/sec_fuelgauge.h>
#include <linux/sprd_battery_common.h>

int sprdbat_interpolate(int x, int n, struct sprdbat_table_data *tab)
{
	int index;
	int y;

	if (x >= tab[0].x)
		y = tab[0].y;
	else if (x <= tab[n - 1].x)
		y = tab[n - 1].y;
	else {
		/*  find interval */
		for (index = 1; index < n; index++)
			if (x > tab[index].x)
				break;
		/*  interpolate */
		y = (tab[index - 1].y - tab[index].y) * (x - tab[index].x)
		    * 2 / (tab[index - 1].x - tab[index].x);
		y = (y + 1) / 2;
		y += tab[index].y;
	}
	return y;
}
static void print_pdata(struct sprd_battery_platform_data *pdata)
{
#define PDATA_LOG(format, arg...) printk("sprdbat pdata: " format, ## arg)
	int i;


	PDATA_LOG("chg_bat_safety_vol:%d\n", pdata->chg_bat_safety_vol);

	PDATA_LOG("irq_fgu:%d\n", pdata->irq_fgu);
	PDATA_LOG("fgu_mode:%d\n", pdata->fgu_mode);
	PDATA_LOG("alm_soc:%d\n", pdata->alm_soc);
	PDATA_LOG("alm_vol:%d\n", pdata->alm_vol);
	PDATA_LOG("soft_vbat_uvlo:%d\n", pdata->soft_vbat_uvlo);
	PDATA_LOG("soft_vbat_ovp:%d\n", pdata->soft_vbat_ovp);
	PDATA_LOG("rint:%d\n", pdata->rint);
	PDATA_LOG("cnom:%d\n", pdata->cnom);
	PDATA_LOG("rsense_real:%d\n", pdata->rsense_real);
	PDATA_LOG("rsense_spec:%d\n", pdata->rsense_spec);
	PDATA_LOG("relax_current:%d\n", pdata->relax_current);
	PDATA_LOG("fgu_cal_ajust:%d\n", pdata->fgu_cal_ajust);
	PDATA_LOG("qmax_update_period:%d\n", pdata->qmax_update_period);
	PDATA_LOG("ocv_tab_size:%d\n", pdata->ocv_tab_size);
	for (i = 0; i < pdata->ocv_tab_size; i++) {
		PDATA_LOG("ocv_tab i=%d x:%d,y:%d\n", i, pdata->ocv_tab[i].x,
			  pdata->ocv_tab[i].y);
	}
	for (i = 0; i < pdata->temp_tab_size; i++) {
		PDATA_LOG("temp_tab_size i=%d x:%d,y:%d\n", i,
			  pdata->temp_tab[i].x, pdata->temp_tab[i].y);
	}

}

static uint32_t init_flag = 0;
#define SPRDBAT_ONE_PERCENT_TIME   (40)
#define SPRDBAT_AVOID_JUMPING_TEMI  (SPRDBAT_ONE_PERCENT_TIME)

static unsigned long sprdbat_update_capacity_time;
static uint32_t update_capacity;

static unsigned long sprdbat_last_query_time;
struct sprd_battery_platform_data *bat_pdata;

bool sec_hal_fg_init(fuelgauge_variable_t * fg_var)
{
	struct sprd_battery_platform_data *pdata = (&get_battery_data(fg_var))->pdata ;
	bat_pdata = pdata;
	pr_info("register sec_hal_fg_init\n");
	print_pdata(pdata); //debug
	sprdfgu_init(pdata);
	{
		struct timespec cur_time;
		get_monotonic_boottime(&cur_time);
		sprdbat_update_capacity_time = cur_time.tv_sec;
		update_capacity = sprdfgu_poweron_capacity();
	}
	init_flag = 1;
	return true;
}

static uint32_t sprdbat_update_capacty(void)
{
	uint32_t fgu_capacity = sprdfgu_read_capacity();
	uint32_t flush_time = 0;
	uint32_t period_time = 0;
	struct timespec cur_time;
	union power_supply_propval value;

	psy_do_property("battery", get, POWER_SUPPLY_PROP_STATUS, value);

	get_monotonic_boottime(&cur_time);
	flush_time = cur_time.tv_sec - sprdbat_update_capacity_time;
	period_time = cur_time.tv_sec - sprdbat_last_query_time;
	sprdbat_last_query_time = cur_time.tv_sec;

	pr_info("fgu_capacity: = %d,flush_time: = %d,period_time:=%d\n",
		      fgu_capacity, flush_time, period_time);

	switch (value.intval) {
	case POWER_SUPPLY_STATUS_CHARGING:
		if (fgu_capacity < update_capacity) {
			if (sprdfgu_read_batcurrent() > 0) {
				pr_info("avoid vol jumping\n");
			fgu_capacity = update_capacity;
		} else {
				if (period_time <= SPRDBAT_AVOID_JUMPING_TEMI) {
					fgu_capacity =update_capacity - 1;
					pr_info("avoid jumping! fgu_capacity: = %d\n", fgu_capacity);
				}
				if ((update_capacity - fgu_capacity) >=
				    (flush_time / SPRDBAT_ONE_PERCENT_TIME)) {
					fgu_capacity =
					    update_capacity -
					    flush_time / SPRDBAT_ONE_PERCENT_TIME;
				}
			}
		} else if (fgu_capacity > update_capacity) {
			if (period_time <= SPRDBAT_AVOID_JUMPING_TEMI) {
				fgu_capacity =
					update_capacity + 1;
				pr_info("avoid  jumping! fgu_capacity: = %d\n",fgu_capacity);
			}
			if ((fgu_capacity - update_capacity) >=
			    (flush_time / SPRDBAT_ONE_PERCENT_TIME)) {
				fgu_capacity =
				    update_capacity +
				    flush_time / SPRDBAT_ONE_PERCENT_TIME;
			}
		}
		/*when soc=100 and adp plugin occur, keep on 100, */
		if (100 == update_capacity) {
			sprdbat_update_capacity_time = cur_time.tv_sec;
			fgu_capacity = 100;
		} else {
			if (fgu_capacity >= 100) {
				fgu_capacity = 99;
			}
		}

		break;
	case POWER_SUPPLY_STATUS_NOT_CHARGING:
	case POWER_SUPPLY_STATUS_DISCHARGING:
		if (fgu_capacity >= update_capacity) {
			fgu_capacity = update_capacity;
		} else {
			if (period_time <= SPRDBAT_AVOID_JUMPING_TEMI) {
				fgu_capacity =
				    update_capacity - 1;
				pr_info
				    ("avoid jumping! fgu_capacity: = %d\n",
				     fgu_capacity);
			}
			if ((update_capacity - fgu_capacity) >=
			    (flush_time / SPRDBAT_ONE_PERCENT_TIME)) {
				fgu_capacity =
				    update_capacity -
				    flush_time / SPRDBAT_ONE_PERCENT_TIME;
			}
		}

		if (sprdfgu_read_vbat_vol() < bat_pdata->soft_vbat_uvlo) {
			pr_info("soft uvlo vol 0...\n");
			fgu_capacity = 0;
		}

		break;
	case POWER_SUPPLY_STATUS_FULL:
		sprdbat_update_capacity_time = cur_time.tv_sec;
		if (fgu_capacity != 100) {
			fgu_capacity = 100;
		}
		break;
	default:
		break;
	}

	if (fgu_capacity != update_capacity) {
		update_capacity = fgu_capacity;
		sprdbat_update_capacity_time = cur_time.tv_sec;
	}
	sprdfgu_record_cap(update_capacity);
	return update_capacity;

}

bool sec_hal_fg_reset(fuelgauge_variable_t * fg_var)
{
	msleep(1000);  //wait a moment,insure voltage REG of FG is updated after power supply voltage be changed
	sprdfgu_reset();
	{
		struct timespec cur_time;
		get_monotonic_boottime(&cur_time);
		sprdbat_update_capacity_time = cur_time.tv_sec; //time need to be updated,too.
		update_capacity = sprdfgu_poweron_capacity(); //same as your change
	}
	return true;
}
bool sec_hal_fg_suspend(fuelgauge_variable_t * fg_var)
{
	sprdfgu_pm_op(1);
	return true;
}

bool sec_hal_fg_resume(fuelgauge_variable_t * fg_var)
{
	sprdfgu_pm_op(0);
	return true;
}
bool sec_hal_fg_fuelalert_init(fuelgauge_variable_t * fg_var , int val)
{
	return true;
}
bool sec_hal_fg_is_fuelalerted(fuelgauge_variable_t * fg_var)
{
	return false;
}
bool sec_hal_fg_full_charged(fuelgauge_variable_t * fg_var)
{
	return true;
}

bool sec_hal_fg_fuelalert_process(void *irq_data, bool is_fuel_alerted)
{
	return true;
}

bool sec_hal_fg_get_property(fuelgauge_variable_t * fg_var,
			       enum power_supply_property psp,
			       union power_supply_propval *val)
{

	switch (psp) {

	/* Cell voltage (VCELL, mV) */
	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
		val->intval = sprdfgu_read_vbat_vol();
		break;

	/* Additional Voltage Information (mV) */
	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
		switch (val->intval) {
		case SEC_BATTEY_VOLTAGE_AVERAGE:
			val->intval = sprdfgu_read_vbat_vol();
			break;
		case SEC_BATTEY_VOLTAGE_OCV:
			val->intval = sprdfgu_read_vbat_ocv();
			break;
		}
		break;

	/* Current (mA) */
	case POWER_SUPPLY_PROP_CURRENT_NOW:
		val->intval = sprdfgu_read_batcurrent();
		break;

	/* Average Current (mA) */
	case POWER_SUPPLY_PROP_CURRENT_AVG:
		val->intval = sprdfgu_read_batcurrent();
		break;

	/* SOC (%) */
	case POWER_SUPPLY_PROP_CAPACITY:
		if (init_flag) {
#if 0
			int cap = sprdfgu_read_capacity();
			if (100 == cap) {
				union power_supply_propval value;
				psy_do_property("battery", get,
						POWER_SUPPLY_PROP_STATUS,
						value);
				if (POWER_SUPPLY_STATUS_CHARGING ==
				    value.intval) {
					pr_info
					    ("sec_hal_fg_get_property soc == 100, but is charging\n",
					     psp);
					cap = 99;
				}
			}
#else
			int cap = sprdbat_update_capacty();
#endif
			val->intval = cap * 10;
		} else {
			val->intval = 500;
		}
		break;

	/* Battery Temperature */
	case POWER_SUPPLY_PROP_TEMP:

	/* Target Temperature */
	case POWER_SUPPLY_PROP_TEMP_AMBIENT:
	default:
		return false;
	}
	pr_info("sec_hal_fg_get_property-%d,val->intval = %d\n", psp,
		val->intval);
	return true;
}

bool sec_hal_fg_set_property(fuelgauge_variable_t * fg_var,
			       enum power_supply_property psp,
			       const union power_supply_propval *val)
{
	switch (psp) {

	case POWER_SUPPLY_PROP_ONLINE:
		if (val->intval == POWER_SUPPLY_TYPE_BATTERY) {
			sprdfgu_adp_status_set(0);
		} else {
			sprdfgu_adp_status_set(1);
		}
		break;
	/* Battery Temperature */
	case POWER_SUPPLY_PROP_TEMP:

	/* Target Temperature */
	case POWER_SUPPLY_PROP_TEMP_AMBIENT:
		break;
	default:
		return false;
	}
	return true;
}