1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Spreadtrum Communications Inc.
3 
4 #include <linux/gpio/consumer.h>
5 #include <linux/iio/consumer.h>
6 #include <linux/interrupt.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/nvmem-consumer.h>
10 #include <linux/of.h>
11 #include <linux/platform_device.h>
12 #include <linux/power_supply.h>
13 #include <linux/regmap.h>
14 #include <linux/slab.h>
15 
16 /* PMIC global control registers definition */
17 #define SC27XX_MODULE_EN0		0xc08
18 #define SC27XX_CLK_EN0			0xc18
19 #define SC27XX_FGU_EN			BIT(7)
20 #define SC27XX_FGU_RTC_EN		BIT(6)
21 
22 /* FGU registers definition */
23 #define SC27XX_FGU_START		0x0
24 #define SC27XX_FGU_CONFIG		0x4
25 #define SC27XX_FGU_ADC_CONFIG		0x8
26 #define SC27XX_FGU_STATUS		0xc
27 #define SC27XX_FGU_INT_EN		0x10
28 #define SC27XX_FGU_INT_CLR		0x14
29 #define SC27XX_FGU_INT_STS		0x1c
30 #define SC27XX_FGU_VOLTAGE		0x20
31 #define SC27XX_FGU_OCV			0x24
32 #define SC27XX_FGU_POCV			0x28
33 #define SC27XX_FGU_CURRENT		0x2c
34 #define SC27XX_FGU_LOW_OVERLOAD		0x34
35 #define SC27XX_FGU_CLBCNT_SETH		0x50
36 #define SC27XX_FGU_CLBCNT_SETL		0x54
37 #define SC27XX_FGU_CLBCNT_DELTH		0x58
38 #define SC27XX_FGU_CLBCNT_DELTL		0x5c
39 #define SC27XX_FGU_CLBCNT_VALH		0x68
40 #define SC27XX_FGU_CLBCNT_VALL		0x6c
41 #define SC27XX_FGU_CLBCNT_QMAXL		0x74
42 #define SC27XX_FGU_USER_AREA_SET	0xa0
43 #define SC27XX_FGU_USER_AREA_CLEAR	0xa4
44 #define SC27XX_FGU_USER_AREA_STATUS	0xa8
45 #define SC27XX_FGU_VOLTAGE_BUF		0xd0
46 #define SC27XX_FGU_CURRENT_BUF		0xf0
47 
48 #define SC27XX_WRITE_SELCLB_EN		BIT(0)
49 #define SC27XX_FGU_CLBCNT_MASK		GENMASK(15, 0)
50 #define SC27XX_FGU_CLBCNT_SHIFT		16
51 #define SC27XX_FGU_LOW_OVERLOAD_MASK	GENMASK(12, 0)
52 
53 #define SC27XX_FGU_INT_MASK		GENMASK(9, 0)
54 #define SC27XX_FGU_LOW_OVERLOAD_INT	BIT(0)
55 #define SC27XX_FGU_CLBCNT_DELTA_INT	BIT(2)
56 
57 #define SC27XX_FGU_MODE_AREA_MASK	GENMASK(15, 12)
58 #define SC27XX_FGU_CAP_AREA_MASK	GENMASK(11, 0)
59 #define SC27XX_FGU_MODE_AREA_SHIFT	12
60 
61 #define SC27XX_FGU_FIRST_POWERTON	GENMASK(3, 0)
62 #define SC27XX_FGU_DEFAULT_CAP		GENMASK(11, 0)
63 #define SC27XX_FGU_NORMAIL_POWERTON	0x5
64 
65 #define SC27XX_FGU_CUR_BASIC_ADC	8192
66 #define SC27XX_FGU_SAMPLE_HZ		2
67 /* micro Ohms */
68 #define SC27XX_FGU_IDEAL_RESISTANCE	20000
69 
70 /*
71  * struct sc27xx_fgu_data: describe the FGU device
72  * @regmap: regmap for register access
73  * @dev: platform device
74  * @battery: battery power supply
75  * @base: the base offset for the controller
76  * @lock: protect the structure
77  * @gpiod: GPIO for battery detection
78  * @channel: IIO channel to get battery temperature
79  * @charge_chan: IIO channel to get charge voltage
80  * @internal_resist: the battery internal resistance in mOhm
81  * @total_cap: the total capacity of the battery in mAh
82  * @init_cap: the initial capacity of the battery in mAh
83  * @alarm_cap: the alarm capacity
84  * @init_clbcnt: the initial coulomb counter
85  * @max_volt: the maximum constant input voltage in millivolt
86  * @min_volt: the minimum drained battery voltage in microvolt
87  * @boot_volt: the voltage measured during boot in microvolt
88  * @table_len: the capacity table length
89  * @resist_table_len: the resistance table length
90  * @cur_1000ma_adc: ADC value corresponding to 1000 mA
91  * @vol_1000mv_adc: ADC value corresponding to 1000 mV
92  * @calib_resist: the real resistance of coulomb counter chip in uOhm
93  * @cap_table: capacity table with corresponding ocv
94  * @resist_table: resistance percent table with corresponding temperature
95  */
96 struct sc27xx_fgu_data {
97 	struct regmap *regmap;
98 	struct device *dev;
99 	struct power_supply *battery;
100 	u32 base;
101 	struct mutex lock;
102 	struct gpio_desc *gpiod;
103 	struct iio_channel *channel;
104 	struct iio_channel *charge_chan;
105 	bool bat_present;
106 	int internal_resist;
107 	int total_cap;
108 	int init_cap;
109 	int alarm_cap;
110 	int init_clbcnt;
111 	int max_volt;
112 	int min_volt;
113 	int boot_volt;
114 	int table_len;
115 	int resist_table_len;
116 	int cur_1000ma_adc;
117 	int vol_1000mv_adc;
118 	int calib_resist;
119 	struct power_supply_battery_ocv_table *cap_table;
120 	struct power_supply_resistance_temp_table *resist_table;
121 };
122 
123 static int sc27xx_fgu_cap_to_clbcnt(struct sc27xx_fgu_data *data, int capacity);
124 static void sc27xx_fgu_capacity_calibration(struct sc27xx_fgu_data *data,
125 					    int cap, bool int_mode);
126 static void sc27xx_fgu_adjust_cap(struct sc27xx_fgu_data *data, int cap);
127 static int sc27xx_fgu_get_temp(struct sc27xx_fgu_data *data, int *temp);
128 
129 static const char * const sc27xx_charger_supply_name[] = {
130 	"sc2731_charger",
131 	"sc2720_charger",
132 	"sc2721_charger",
133 	"sc2723_charger",
134 };
135 
136 static int sc27xx_fgu_adc_to_current(struct sc27xx_fgu_data *data, int adc)
137 {
138 	return DIV_ROUND_CLOSEST(adc * 1000, data->cur_1000ma_adc);
139 }
140 
141 static int sc27xx_fgu_adc_to_voltage(struct sc27xx_fgu_data *data, int adc)
142 {
143 	return DIV_ROUND_CLOSEST(adc * 1000, data->vol_1000mv_adc);
144 }
145 
146 static int sc27xx_fgu_voltage_to_adc(struct sc27xx_fgu_data *data, int vol)
147 {
148 	return DIV_ROUND_CLOSEST(vol * data->vol_1000mv_adc, 1000);
149 }
150 
151 static bool sc27xx_fgu_is_first_poweron(struct sc27xx_fgu_data *data)
152 {
153 	int ret, status, cap, mode;
154 
155 	ret = regmap_read(data->regmap,
156 			  data->base + SC27XX_FGU_USER_AREA_STATUS, &status);
157 	if (ret)
158 		return false;
159 
160 	/*
161 	 * We use low 4 bits to save the last battery capacity and high 12 bits
162 	 * to save the system boot mode.
163 	 */
164 	mode = (status & SC27XX_FGU_MODE_AREA_MASK) >> SC27XX_FGU_MODE_AREA_SHIFT;
165 	cap = status & SC27XX_FGU_CAP_AREA_MASK;
166 
167 	/*
168 	 * When FGU has been powered down, the user area registers became
169 	 * default value (0xffff), which can be used to valid if the system is
170 	 * first power on or not.
171 	 */
172 	if (mode == SC27XX_FGU_FIRST_POWERTON || cap == SC27XX_FGU_DEFAULT_CAP)
173 		return true;
174 
175 	return false;
176 }
177 
178 static int sc27xx_fgu_save_boot_mode(struct sc27xx_fgu_data *data,
179 				     int boot_mode)
180 {
181 	int ret;
182 
183 	ret = regmap_update_bits(data->regmap,
184 				 data->base + SC27XX_FGU_USER_AREA_CLEAR,
185 				 SC27XX_FGU_MODE_AREA_MASK,
186 				 SC27XX_FGU_MODE_AREA_MASK);
187 	if (ret)
188 		return ret;
189 
190 	/*
191 	 * Since the user area registers are put on power always-on region,
192 	 * then these registers changing time will be a little long. Thus
193 	 * here we should delay 200us to wait until values are updated
194 	 * successfully according to the datasheet.
195 	 */
196 	udelay(200);
197 
198 	ret = regmap_update_bits(data->regmap,
199 				 data->base + SC27XX_FGU_USER_AREA_SET,
200 				 SC27XX_FGU_MODE_AREA_MASK,
201 				 boot_mode << SC27XX_FGU_MODE_AREA_SHIFT);
202 	if (ret)
203 		return ret;
204 
205 	/*
206 	 * Since the user area registers are put on power always-on region,
207 	 * then these registers changing time will be a little long. Thus
208 	 * here we should delay 200us to wait until values are updated
209 	 * successfully according to the datasheet.
210 	 */
211 	udelay(200);
212 
213 	/*
214 	 * According to the datasheet, we should set the USER_AREA_CLEAR to 0 to
215 	 * make the user area data available, otherwise we can not save the user
216 	 * area data.
217 	 */
218 	return regmap_update_bits(data->regmap,
219 				  data->base + SC27XX_FGU_USER_AREA_CLEAR,
220 				  SC27XX_FGU_MODE_AREA_MASK, 0);
221 }
222 
223 static int sc27xx_fgu_save_last_cap(struct sc27xx_fgu_data *data, int cap)
224 {
225 	int ret;
226 
227 	ret = regmap_update_bits(data->regmap,
228 				 data->base + SC27XX_FGU_USER_AREA_CLEAR,
229 				 SC27XX_FGU_CAP_AREA_MASK,
230 				 SC27XX_FGU_CAP_AREA_MASK);
231 	if (ret)
232 		return ret;
233 
234 	/*
235 	 * Since the user area registers are put on power always-on region,
236 	 * then these registers changing time will be a little long. Thus
237 	 * here we should delay 200us to wait until values are updated
238 	 * successfully according to the datasheet.
239 	 */
240 	udelay(200);
241 
242 	ret = regmap_update_bits(data->regmap,
243 				 data->base + SC27XX_FGU_USER_AREA_SET,
244 				 SC27XX_FGU_CAP_AREA_MASK, cap);
245 	if (ret)
246 		return ret;
247 
248 	/*
249 	 * Since the user area registers are put on power always-on region,
250 	 * then these registers changing time will be a little long. Thus
251 	 * here we should delay 200us to wait until values are updated
252 	 * successfully according to the datasheet.
253 	 */
254 	udelay(200);
255 
256 	/*
257 	 * According to the datasheet, we should set the USER_AREA_CLEAR to 0 to
258 	 * make the user area data available, otherwise we can not save the user
259 	 * area data.
260 	 */
261 	return regmap_update_bits(data->regmap,
262 				  data->base + SC27XX_FGU_USER_AREA_CLEAR,
263 				  SC27XX_FGU_CAP_AREA_MASK, 0);
264 }
265 
266 static int sc27xx_fgu_read_last_cap(struct sc27xx_fgu_data *data, int *cap)
267 {
268 	int ret, value;
269 
270 	ret = regmap_read(data->regmap,
271 			  data->base + SC27XX_FGU_USER_AREA_STATUS, &value);
272 	if (ret)
273 		return ret;
274 
275 	*cap = value & SC27XX_FGU_CAP_AREA_MASK;
276 	return 0;
277 }
278 
279 /*
280  * When system boots on, we can not read battery capacity from coulomb
281  * registers, since now the coulomb registers are invalid. So we should
282  * calculate the battery open circuit voltage, and get current battery
283  * capacity according to the capacity table.
284  */
285 static int sc27xx_fgu_get_boot_capacity(struct sc27xx_fgu_data *data, int *cap)
286 {
287 	int volt, cur, oci, ocv, ret;
288 	bool is_first_poweron = sc27xx_fgu_is_first_poweron(data);
289 
290 	/*
291 	 * If system is not the first power on, we should use the last saved
292 	 * battery capacity as the initial battery capacity. Otherwise we should
293 	 * re-calculate the initial battery capacity.
294 	 */
295 	if (!is_first_poweron) {
296 		ret = sc27xx_fgu_read_last_cap(data, cap);
297 		if (ret)
298 			return ret;
299 
300 		return sc27xx_fgu_save_boot_mode(data, SC27XX_FGU_NORMAIL_POWERTON);
301 	}
302 
303 	/*
304 	 * After system booting on, the SC27XX_FGU_CLBCNT_QMAXL register saved
305 	 * the first sampled open circuit current.
306 	 */
307 	ret = regmap_read(data->regmap, data->base + SC27XX_FGU_CLBCNT_QMAXL,
308 			  &cur);
309 	if (ret)
310 		return ret;
311 
312 	cur <<= 1;
313 	oci = sc27xx_fgu_adc_to_current(data, cur - SC27XX_FGU_CUR_BASIC_ADC);
314 
315 	/*
316 	 * Should get the OCV from SC27XX_FGU_POCV register at the system
317 	 * beginning. It is ADC values reading from registers which need to
318 	 * convert the corresponding voltage.
319 	 */
320 	ret = regmap_read(data->regmap, data->base + SC27XX_FGU_POCV, &volt);
321 	if (ret)
322 		return ret;
323 
324 	volt = sc27xx_fgu_adc_to_voltage(data, volt);
325 	ocv = volt * 1000 - oci * data->internal_resist;
326 	data->boot_volt = ocv;
327 
328 	/*
329 	 * Parse the capacity table to look up the correct capacity percent
330 	 * according to current battery's corresponding OCV values.
331 	 */
332 	*cap = power_supply_ocv2cap_simple(data->cap_table, data->table_len,
333 					   ocv);
334 
335 	ret = sc27xx_fgu_save_last_cap(data, *cap);
336 	if (ret)
337 		return ret;
338 
339 	return sc27xx_fgu_save_boot_mode(data, SC27XX_FGU_NORMAIL_POWERTON);
340 }
341 
342 static int sc27xx_fgu_set_clbcnt(struct sc27xx_fgu_data *data, int clbcnt)
343 {
344 	int ret;
345 
346 	ret = regmap_update_bits(data->regmap,
347 				 data->base + SC27XX_FGU_CLBCNT_SETL,
348 				 SC27XX_FGU_CLBCNT_MASK, clbcnt);
349 	if (ret)
350 		return ret;
351 
352 	ret = regmap_update_bits(data->regmap,
353 				 data->base + SC27XX_FGU_CLBCNT_SETH,
354 				 SC27XX_FGU_CLBCNT_MASK,
355 				 clbcnt >> SC27XX_FGU_CLBCNT_SHIFT);
356 	if (ret)
357 		return ret;
358 
359 	return regmap_update_bits(data->regmap, data->base + SC27XX_FGU_START,
360 				 SC27XX_WRITE_SELCLB_EN,
361 				 SC27XX_WRITE_SELCLB_EN);
362 }
363 
364 static int sc27xx_fgu_get_clbcnt(struct sc27xx_fgu_data *data, int *clb_cnt)
365 {
366 	int ccl, cch, ret;
367 
368 	ret = regmap_read(data->regmap, data->base + SC27XX_FGU_CLBCNT_VALL,
369 			  &ccl);
370 	if (ret)
371 		return ret;
372 
373 	ret = regmap_read(data->regmap, data->base + SC27XX_FGU_CLBCNT_VALH,
374 			  &cch);
375 	if (ret)
376 		return ret;
377 
378 	*clb_cnt = ccl & SC27XX_FGU_CLBCNT_MASK;
379 	*clb_cnt |= (cch & SC27XX_FGU_CLBCNT_MASK) << SC27XX_FGU_CLBCNT_SHIFT;
380 
381 	return 0;
382 }
383 
384 static int sc27xx_fgu_get_vol_now(struct sc27xx_fgu_data *data, int *val)
385 {
386 	int ret;
387 	u32 vol;
388 
389 	ret = regmap_read(data->regmap, data->base + SC27XX_FGU_VOLTAGE_BUF,
390 			  &vol);
391 	if (ret)
392 		return ret;
393 
394 	/*
395 	 * It is ADC values reading from registers which need to convert to
396 	 * corresponding voltage values.
397 	 */
398 	*val = sc27xx_fgu_adc_to_voltage(data, vol);
399 
400 	return 0;
401 }
402 
403 static int sc27xx_fgu_get_cur_now(struct sc27xx_fgu_data *data, int *val)
404 {
405 	int ret;
406 	u32 cur;
407 
408 	ret = regmap_read(data->regmap, data->base + SC27XX_FGU_CURRENT_BUF,
409 			  &cur);
410 	if (ret)
411 		return ret;
412 
413 	/*
414 	 * It is ADC values reading from registers which need to convert to
415 	 * corresponding current values.
416 	 */
417 	*val = sc27xx_fgu_adc_to_current(data, cur - SC27XX_FGU_CUR_BASIC_ADC);
418 
419 	return 0;
420 }
421 
422 static int sc27xx_fgu_get_capacity(struct sc27xx_fgu_data *data, int *cap)
423 {
424 	int ret, cur_clbcnt, delta_clbcnt, delta_cap, temp;
425 
426 	/* Get current coulomb counters firstly */
427 	ret = sc27xx_fgu_get_clbcnt(data, &cur_clbcnt);
428 	if (ret)
429 		return ret;
430 
431 	delta_clbcnt = cur_clbcnt - data->init_clbcnt;
432 
433 	/*
434 	 * Convert coulomb counter to delta capacity (mAh), and set multiplier
435 	 * as 10 to improve the precision.
436 	 */
437 	temp = DIV_ROUND_CLOSEST(delta_clbcnt * 10, 36 * SC27XX_FGU_SAMPLE_HZ);
438 	temp = sc27xx_fgu_adc_to_current(data, temp / 1000);
439 
440 	/*
441 	 * Convert to capacity percent of the battery total capacity,
442 	 * and multiplier is 100 too.
443 	 */
444 	delta_cap = DIV_ROUND_CLOSEST(temp * 100, data->total_cap);
445 	*cap = delta_cap + data->init_cap;
446 
447 	/* Calibrate the battery capacity in a normal range. */
448 	sc27xx_fgu_capacity_calibration(data, *cap, false);
449 
450 	return 0;
451 }
452 
453 static int sc27xx_fgu_get_vbat_vol(struct sc27xx_fgu_data *data, int *val)
454 {
455 	int ret, vol;
456 
457 	ret = regmap_read(data->regmap, data->base + SC27XX_FGU_VOLTAGE, &vol);
458 	if (ret)
459 		return ret;
460 
461 	/*
462 	 * It is ADC values reading from registers which need to convert to
463 	 * corresponding voltage values.
464 	 */
465 	*val = sc27xx_fgu_adc_to_voltage(data, vol);
466 
467 	return 0;
468 }
469 
470 static int sc27xx_fgu_get_current(struct sc27xx_fgu_data *data, int *val)
471 {
472 	int ret, cur;
473 
474 	ret = regmap_read(data->regmap, data->base + SC27XX_FGU_CURRENT, &cur);
475 	if (ret)
476 		return ret;
477 
478 	/*
479 	 * It is ADC values reading from registers which need to convert to
480 	 * corresponding current values.
481 	 */
482 	*val = sc27xx_fgu_adc_to_current(data, cur - SC27XX_FGU_CUR_BASIC_ADC);
483 
484 	return 0;
485 }
486 
487 static int sc27xx_fgu_get_vbat_ocv(struct sc27xx_fgu_data *data, int *val)
488 {
489 	int vol, cur, ret, temp, resistance;
490 
491 	ret = sc27xx_fgu_get_vbat_vol(data, &vol);
492 	if (ret)
493 		return ret;
494 
495 	ret = sc27xx_fgu_get_current(data, &cur);
496 	if (ret)
497 		return ret;
498 
499 	resistance = data->internal_resist;
500 	if (data->resist_table_len > 0) {
501 		ret = sc27xx_fgu_get_temp(data, &temp);
502 		if (ret)
503 			return ret;
504 
505 		resistance = power_supply_temp2resist_simple(data->resist_table,
506 						data->resist_table_len, temp);
507 		resistance = data->internal_resist * resistance / 100;
508 	}
509 
510 	/* Return the battery OCV in micro volts. */
511 	*val = vol * 1000 - cur * resistance;
512 
513 	return 0;
514 }
515 
516 static int sc27xx_fgu_get_charge_vol(struct sc27xx_fgu_data *data, int *val)
517 {
518 	int ret, vol;
519 
520 	ret = iio_read_channel_processed(data->charge_chan, &vol);
521 	if (ret < 0)
522 		return ret;
523 
524 	*val = vol * 1000;
525 	return 0;
526 }
527 
528 static int sc27xx_fgu_get_temp(struct sc27xx_fgu_data *data, int *temp)
529 {
530 	return iio_read_channel_processed(data->channel, temp);
531 }
532 
533 static int sc27xx_fgu_get_health(struct sc27xx_fgu_data *data, int *health)
534 {
535 	int ret, vol;
536 
537 	ret = sc27xx_fgu_get_vbat_vol(data, &vol);
538 	if (ret)
539 		return ret;
540 
541 	if (vol > data->max_volt)
542 		*health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
543 	else
544 		*health = POWER_SUPPLY_HEALTH_GOOD;
545 
546 	return 0;
547 }
548 
549 static int sc27xx_fgu_get_status(struct sc27xx_fgu_data *data, int *status)
550 {
551 	union power_supply_propval val;
552 	struct power_supply *psy;
553 	int i, ret = -EINVAL;
554 
555 	for (i = 0; i < ARRAY_SIZE(sc27xx_charger_supply_name); i++) {
556 		psy = power_supply_get_by_name(sc27xx_charger_supply_name[i]);
557 		if (!psy)
558 			continue;
559 
560 		ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_STATUS,
561 						&val);
562 		power_supply_put(psy);
563 		if (ret)
564 			return ret;
565 
566 		*status = val.intval;
567 	}
568 
569 	return ret;
570 }
571 
572 static int sc27xx_fgu_get_property(struct power_supply *psy,
573 				   enum power_supply_property psp,
574 				   union power_supply_propval *val)
575 {
576 	struct sc27xx_fgu_data *data = power_supply_get_drvdata(psy);
577 	int ret = 0;
578 	int value;
579 
580 	mutex_lock(&data->lock);
581 
582 	switch (psp) {
583 	case POWER_SUPPLY_PROP_STATUS:
584 		ret = sc27xx_fgu_get_status(data, &value);
585 		if (ret)
586 			goto error;
587 
588 		val->intval = value;
589 		break;
590 
591 	case POWER_SUPPLY_PROP_HEALTH:
592 		ret = sc27xx_fgu_get_health(data, &value);
593 		if (ret)
594 			goto error;
595 
596 		val->intval = value;
597 		break;
598 
599 	case POWER_SUPPLY_PROP_PRESENT:
600 		val->intval = data->bat_present;
601 		break;
602 
603 	case POWER_SUPPLY_PROP_TEMP:
604 		ret = sc27xx_fgu_get_temp(data, &value);
605 		if (ret)
606 			goto error;
607 
608 		val->intval = value;
609 		break;
610 
611 	case POWER_SUPPLY_PROP_TECHNOLOGY:
612 		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
613 		break;
614 
615 	case POWER_SUPPLY_PROP_CAPACITY:
616 		ret = sc27xx_fgu_get_capacity(data, &value);
617 		if (ret)
618 			goto error;
619 
620 		val->intval = value;
621 		break;
622 
623 	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
624 		ret = sc27xx_fgu_get_vbat_vol(data, &value);
625 		if (ret)
626 			goto error;
627 
628 		val->intval = value * 1000;
629 		break;
630 
631 	case POWER_SUPPLY_PROP_VOLTAGE_OCV:
632 		ret = sc27xx_fgu_get_vbat_ocv(data, &value);
633 		if (ret)
634 			goto error;
635 
636 		val->intval = value;
637 		break;
638 
639 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
640 		ret = sc27xx_fgu_get_charge_vol(data, &value);
641 		if (ret)
642 			goto error;
643 
644 		val->intval = value;
645 		break;
646 
647 	case POWER_SUPPLY_PROP_CURRENT_AVG:
648 		ret = sc27xx_fgu_get_current(data, &value);
649 		if (ret)
650 			goto error;
651 
652 		val->intval = value * 1000;
653 		break;
654 
655 	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
656 		val->intval = data->total_cap * 1000;
657 		break;
658 
659 	case POWER_SUPPLY_PROP_CHARGE_NOW:
660 		ret = sc27xx_fgu_get_clbcnt(data, &value);
661 		if (ret)
662 			goto error;
663 
664 		value = DIV_ROUND_CLOSEST(value * 10,
665 					  36 * SC27XX_FGU_SAMPLE_HZ);
666 		val->intval = sc27xx_fgu_adc_to_current(data, value);
667 
668 		break;
669 
670 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
671 		ret = sc27xx_fgu_get_vol_now(data, &value);
672 		if (ret)
673 			goto error;
674 
675 		val->intval = value * 1000;
676 		break;
677 
678 	case POWER_SUPPLY_PROP_CURRENT_NOW:
679 		ret = sc27xx_fgu_get_cur_now(data, &value);
680 		if (ret)
681 			goto error;
682 
683 		val->intval = value * 1000;
684 		break;
685 
686 	case POWER_SUPPLY_PROP_VOLTAGE_BOOT:
687 		val->intval = data->boot_volt;
688 		break;
689 
690 	default:
691 		ret = -EINVAL;
692 		break;
693 	}
694 
695 error:
696 	mutex_unlock(&data->lock);
697 	return ret;
698 }
699 
700 static int sc27xx_fgu_set_property(struct power_supply *psy,
701 				   enum power_supply_property psp,
702 				   const union power_supply_propval *val)
703 {
704 	struct sc27xx_fgu_data *data = power_supply_get_drvdata(psy);
705 	int ret;
706 
707 	mutex_lock(&data->lock);
708 
709 	switch (psp) {
710 	case POWER_SUPPLY_PROP_CAPACITY:
711 		ret = sc27xx_fgu_save_last_cap(data, val->intval);
712 		if (ret < 0)
713 			dev_err(data->dev, "failed to save battery capacity\n");
714 		break;
715 
716 	case POWER_SUPPLY_PROP_CALIBRATE:
717 		sc27xx_fgu_adjust_cap(data, val->intval);
718 		ret = 0;
719 		break;
720 
721 	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
722 		data->total_cap = val->intval / 1000;
723 		ret = 0;
724 		break;
725 
726 	default:
727 		ret = -EINVAL;
728 	}
729 
730 	mutex_unlock(&data->lock);
731 
732 	return ret;
733 }
734 
735 static void sc27xx_fgu_external_power_changed(struct power_supply *psy)
736 {
737 	struct sc27xx_fgu_data *data = power_supply_get_drvdata(psy);
738 
739 	power_supply_changed(data->battery);
740 }
741 
742 static int sc27xx_fgu_property_is_writeable(struct power_supply *psy,
743 					    enum power_supply_property psp)
744 {
745 	return psp == POWER_SUPPLY_PROP_CAPACITY ||
746 		psp == POWER_SUPPLY_PROP_CALIBRATE ||
747 		psp == POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN;
748 }
749 
750 static enum power_supply_property sc27xx_fgu_props[] = {
751 	POWER_SUPPLY_PROP_STATUS,
752 	POWER_SUPPLY_PROP_HEALTH,
753 	POWER_SUPPLY_PROP_PRESENT,
754 	POWER_SUPPLY_PROP_TEMP,
755 	POWER_SUPPLY_PROP_TECHNOLOGY,
756 	POWER_SUPPLY_PROP_CAPACITY,
757 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
758 	POWER_SUPPLY_PROP_VOLTAGE_OCV,
759 	POWER_SUPPLY_PROP_VOLTAGE_AVG,
760 	POWER_SUPPLY_PROP_VOLTAGE_BOOT,
761 	POWER_SUPPLY_PROP_CURRENT_NOW,
762 	POWER_SUPPLY_PROP_CURRENT_AVG,
763 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
764 	POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
765 	POWER_SUPPLY_PROP_CALIBRATE,
766 	POWER_SUPPLY_PROP_CHARGE_NOW
767 };
768 
769 static const struct power_supply_desc sc27xx_fgu_desc = {
770 	.name			= "sc27xx-fgu",
771 	.type			= POWER_SUPPLY_TYPE_BATTERY,
772 	.properties		= sc27xx_fgu_props,
773 	.num_properties		= ARRAY_SIZE(sc27xx_fgu_props),
774 	.get_property		= sc27xx_fgu_get_property,
775 	.set_property		= sc27xx_fgu_set_property,
776 	.external_power_changed	= sc27xx_fgu_external_power_changed,
777 	.property_is_writeable	= sc27xx_fgu_property_is_writeable,
778 	.no_thermal		= true,
779 };
780 
781 static void sc27xx_fgu_adjust_cap(struct sc27xx_fgu_data *data, int cap)
782 {
783 	int ret;
784 
785 	data->init_cap = cap;
786 	ret = sc27xx_fgu_get_clbcnt(data, &data->init_clbcnt);
787 	if (ret)
788 		dev_err(data->dev, "failed to get init coulomb counter\n");
789 }
790 
791 static void sc27xx_fgu_capacity_calibration(struct sc27xx_fgu_data *data,
792 					    int cap, bool int_mode)
793 {
794 	int ret, ocv, chg_sts, adc;
795 
796 	ret = sc27xx_fgu_get_vbat_ocv(data, &ocv);
797 	if (ret) {
798 		dev_err(data->dev, "get battery ocv error.\n");
799 		return;
800 	}
801 
802 	ret = sc27xx_fgu_get_status(data, &chg_sts);
803 	if (ret) {
804 		dev_err(data->dev, "get charger status error.\n");
805 		return;
806 	}
807 
808 	/*
809 	 * If we are in charging mode, then we do not need to calibrate the
810 	 * lower capacity.
811 	 */
812 	if (chg_sts == POWER_SUPPLY_STATUS_CHARGING)
813 		return;
814 
815 	if ((ocv > data->cap_table[0].ocv && cap < 100) || cap > 100) {
816 		/*
817 		 * If current OCV value is larger than the max OCV value in
818 		 * OCV table, or the current capacity is larger than 100,
819 		 * we should force the inititial capacity to 100.
820 		 */
821 		sc27xx_fgu_adjust_cap(data, 100);
822 	} else if (ocv <= data->cap_table[data->table_len - 1].ocv) {
823 		/*
824 		 * If current OCV value is leass than the minimum OCV value in
825 		 * OCV table, we should force the inititial capacity to 0.
826 		 */
827 		sc27xx_fgu_adjust_cap(data, 0);
828 	} else if ((ocv > data->cap_table[data->table_len - 1].ocv && cap <= 0) ||
829 		   (ocv > data->min_volt && cap <= data->alarm_cap)) {
830 		/*
831 		 * If current OCV value is not matchable with current capacity,
832 		 * we should re-calculate current capacity by looking up the
833 		 * OCV table.
834 		 */
835 		int cur_cap = power_supply_ocv2cap_simple(data->cap_table,
836 							  data->table_len, ocv);
837 
838 		sc27xx_fgu_adjust_cap(data, cur_cap);
839 	} else if (ocv <= data->min_volt) {
840 		/*
841 		 * If current OCV value is less than the low alarm voltage, but
842 		 * current capacity is larger than the alarm capacity, we should
843 		 * adjust the inititial capacity to alarm capacity.
844 		 */
845 		if (cap > data->alarm_cap) {
846 			sc27xx_fgu_adjust_cap(data, data->alarm_cap);
847 		} else {
848 			int cur_cap;
849 
850 			/*
851 			 * If current capacity is equal with 0 or less than 0
852 			 * (some error occurs), we should adjust inititial
853 			 * capacity to the capacity corresponding to current OCV
854 			 * value.
855 			 */
856 			cur_cap = power_supply_ocv2cap_simple(data->cap_table,
857 							      data->table_len,
858 							      ocv);
859 			sc27xx_fgu_adjust_cap(data, cur_cap);
860 		}
861 
862 		if (!int_mode)
863 			return;
864 
865 		/*
866 		 * After adjusting the battery capacity, we should set the
867 		 * lowest alarm voltage instead.
868 		 */
869 		data->min_volt = data->cap_table[data->table_len - 1].ocv;
870 		data->alarm_cap = power_supply_ocv2cap_simple(data->cap_table,
871 							      data->table_len,
872 							      data->min_volt);
873 
874 		adc = sc27xx_fgu_voltage_to_adc(data, data->min_volt / 1000);
875 		regmap_update_bits(data->regmap,
876 				   data->base + SC27XX_FGU_LOW_OVERLOAD,
877 				   SC27XX_FGU_LOW_OVERLOAD_MASK, adc);
878 	}
879 }
880 
881 static irqreturn_t sc27xx_fgu_interrupt(int irq, void *dev_id)
882 {
883 	struct sc27xx_fgu_data *data = dev_id;
884 	int ret, cap;
885 	u32 status;
886 
887 	mutex_lock(&data->lock);
888 
889 	ret = regmap_read(data->regmap, data->base + SC27XX_FGU_INT_STS,
890 			  &status);
891 	if (ret)
892 		goto out;
893 
894 	ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_CLR,
895 				 status, status);
896 	if (ret)
897 		goto out;
898 
899 	/*
900 	 * When low overload voltage interrupt happens, we should calibrate the
901 	 * battery capacity in lower voltage stage.
902 	 */
903 	if (!(status & SC27XX_FGU_LOW_OVERLOAD_INT))
904 		goto out;
905 
906 	ret = sc27xx_fgu_get_capacity(data, &cap);
907 	if (ret)
908 		goto out;
909 
910 	sc27xx_fgu_capacity_calibration(data, cap, true);
911 
912 out:
913 	mutex_unlock(&data->lock);
914 
915 	power_supply_changed(data->battery);
916 	return IRQ_HANDLED;
917 }
918 
919 static irqreturn_t sc27xx_fgu_bat_detection(int irq, void *dev_id)
920 {
921 	struct sc27xx_fgu_data *data = dev_id;
922 	int state;
923 
924 	mutex_lock(&data->lock);
925 
926 	state = gpiod_get_value_cansleep(data->gpiod);
927 	if (state < 0) {
928 		dev_err(data->dev, "failed to get gpio state\n");
929 		mutex_unlock(&data->lock);
930 		return IRQ_RETVAL(state);
931 	}
932 
933 	data->bat_present = !!state;
934 
935 	mutex_unlock(&data->lock);
936 
937 	power_supply_changed(data->battery);
938 	return IRQ_HANDLED;
939 }
940 
941 static void sc27xx_fgu_disable(void *_data)
942 {
943 	struct sc27xx_fgu_data *data = _data;
944 
945 	regmap_update_bits(data->regmap, SC27XX_CLK_EN0, SC27XX_FGU_RTC_EN, 0);
946 	regmap_update_bits(data->regmap, SC27XX_MODULE_EN0, SC27XX_FGU_EN, 0);
947 }
948 
949 static int sc27xx_fgu_cap_to_clbcnt(struct sc27xx_fgu_data *data, int capacity)
950 {
951 	/*
952 	 * Get current capacity (mAh) = battery total capacity (mAh) *
953 	 * current capacity percent (capacity / 100).
954 	 */
955 	int cur_cap = DIV_ROUND_CLOSEST(data->total_cap * capacity, 100);
956 
957 	/*
958 	 * Convert current capacity (mAh) to coulomb counter according to the
959 	 * formula: 1 mAh =3.6 coulomb.
960 	 */
961 	return DIV_ROUND_CLOSEST(cur_cap * 36 * data->cur_1000ma_adc * SC27XX_FGU_SAMPLE_HZ, 10);
962 }
963 
964 static int sc27xx_fgu_calibration(struct sc27xx_fgu_data *data)
965 {
966 	struct nvmem_cell *cell;
967 	int calib_data, cal_4200mv;
968 	void *buf;
969 	size_t len;
970 
971 	cell = nvmem_cell_get(data->dev, "fgu_calib");
972 	if (IS_ERR(cell))
973 		return PTR_ERR(cell);
974 
975 	buf = nvmem_cell_read(cell, &len);
976 	nvmem_cell_put(cell);
977 
978 	if (IS_ERR(buf))
979 		return PTR_ERR(buf);
980 
981 	memcpy(&calib_data, buf, min(len, sizeof(u32)));
982 
983 	/*
984 	 * Get the ADC value corresponding to 4200 mV from eFuse controller
985 	 * according to below formula. Then convert to ADC values corresponding
986 	 * to 1000 mV and 1000 mA.
987 	 */
988 	cal_4200mv = (calib_data & 0x1ff) + 6963 - 4096 - 256;
989 	data->vol_1000mv_adc = DIV_ROUND_CLOSEST(cal_4200mv * 10, 42);
990 	data->cur_1000ma_adc =
991 		DIV_ROUND_CLOSEST(data->vol_1000mv_adc * 4 * data->calib_resist,
992 				  SC27XX_FGU_IDEAL_RESISTANCE);
993 
994 	kfree(buf);
995 	return 0;
996 }
997 
998 static int sc27xx_fgu_hw_init(struct sc27xx_fgu_data *data)
999 {
1000 	struct power_supply_battery_info info = { };
1001 	struct power_supply_battery_ocv_table *table;
1002 	int ret, delta_clbcnt, alarm_adc;
1003 
1004 	ret = power_supply_get_battery_info(data->battery, &info);
1005 	if (ret) {
1006 		dev_err(data->dev, "failed to get battery information\n");
1007 		return ret;
1008 	}
1009 
1010 	data->total_cap = info.charge_full_design_uah / 1000;
1011 	data->max_volt = info.constant_charge_voltage_max_uv / 1000;
1012 	data->internal_resist = info.factory_internal_resistance_uohm / 1000;
1013 	data->min_volt = info.voltage_min_design_uv;
1014 
1015 	/*
1016 	 * For SC27XX fuel gauge device, we only use one ocv-capacity
1017 	 * table in normal temperature 20 Celsius.
1018 	 */
1019 	table = power_supply_find_ocv2cap_table(&info, 20, &data->table_len);
1020 	if (!table)
1021 		return -EINVAL;
1022 
1023 	data->cap_table = devm_kmemdup(data->dev, table,
1024 				       data->table_len * sizeof(*table),
1025 				       GFP_KERNEL);
1026 	if (!data->cap_table) {
1027 		power_supply_put_battery_info(data->battery, &info);
1028 		return -ENOMEM;
1029 	}
1030 
1031 	data->alarm_cap = power_supply_ocv2cap_simple(data->cap_table,
1032 						      data->table_len,
1033 						      data->min_volt);
1034 	if (!data->alarm_cap)
1035 		data->alarm_cap += 1;
1036 
1037 	data->resist_table_len = info.resist_table_size;
1038 	if (data->resist_table_len > 0) {
1039 		data->resist_table = devm_kmemdup(data->dev, info.resist_table,
1040 						  data->resist_table_len *
1041 						  sizeof(struct power_supply_resistance_temp_table),
1042 						  GFP_KERNEL);
1043 		if (!data->resist_table) {
1044 			power_supply_put_battery_info(data->battery, &info);
1045 			return -ENOMEM;
1046 		}
1047 	}
1048 
1049 	power_supply_put_battery_info(data->battery, &info);
1050 
1051 	ret = sc27xx_fgu_calibration(data);
1052 	if (ret)
1053 		return ret;
1054 
1055 	/* Enable the FGU module */
1056 	ret = regmap_update_bits(data->regmap, SC27XX_MODULE_EN0,
1057 				 SC27XX_FGU_EN, SC27XX_FGU_EN);
1058 	if (ret) {
1059 		dev_err(data->dev, "failed to enable fgu\n");
1060 		return ret;
1061 	}
1062 
1063 	/* Enable the FGU RTC clock to make it work */
1064 	ret = regmap_update_bits(data->regmap, SC27XX_CLK_EN0,
1065 				 SC27XX_FGU_RTC_EN, SC27XX_FGU_RTC_EN);
1066 	if (ret) {
1067 		dev_err(data->dev, "failed to enable fgu RTC clock\n");
1068 		goto disable_fgu;
1069 	}
1070 
1071 	ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_CLR,
1072 				 SC27XX_FGU_INT_MASK, SC27XX_FGU_INT_MASK);
1073 	if (ret) {
1074 		dev_err(data->dev, "failed to clear interrupt status\n");
1075 		goto disable_clk;
1076 	}
1077 
1078 	/*
1079 	 * Set the voltage low overload threshold, which means when the battery
1080 	 * voltage is lower than this threshold, the controller will generate
1081 	 * one interrupt to notify.
1082 	 */
1083 	alarm_adc = sc27xx_fgu_voltage_to_adc(data, data->min_volt / 1000);
1084 	ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_LOW_OVERLOAD,
1085 				 SC27XX_FGU_LOW_OVERLOAD_MASK, alarm_adc);
1086 	if (ret) {
1087 		dev_err(data->dev, "failed to set fgu low overload\n");
1088 		goto disable_clk;
1089 	}
1090 
1091 	/*
1092 	 * Set the coulomb counter delta threshold, that means when the coulomb
1093 	 * counter change is multiples of the delta threshold, the controller
1094 	 * will generate one interrupt to notify the users to update the battery
1095 	 * capacity. Now we set the delta threshold as a counter value of 1%
1096 	 * capacity.
1097 	 */
1098 	delta_clbcnt = sc27xx_fgu_cap_to_clbcnt(data, 1);
1099 
1100 	ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_CLBCNT_DELTL,
1101 				 SC27XX_FGU_CLBCNT_MASK, delta_clbcnt);
1102 	if (ret) {
1103 		dev_err(data->dev, "failed to set low delta coulomb counter\n");
1104 		goto disable_clk;
1105 	}
1106 
1107 	ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_CLBCNT_DELTH,
1108 				 SC27XX_FGU_CLBCNT_MASK,
1109 				 delta_clbcnt >> SC27XX_FGU_CLBCNT_SHIFT);
1110 	if (ret) {
1111 		dev_err(data->dev, "failed to set high delta coulomb counter\n");
1112 		goto disable_clk;
1113 	}
1114 
1115 	/*
1116 	 * Get the boot battery capacity when system powers on, which is used to
1117 	 * initialize the coulomb counter. After that, we can read the coulomb
1118 	 * counter to measure the battery capacity.
1119 	 */
1120 	ret = sc27xx_fgu_get_boot_capacity(data, &data->init_cap);
1121 	if (ret) {
1122 		dev_err(data->dev, "failed to get boot capacity\n");
1123 		goto disable_clk;
1124 	}
1125 
1126 	/*
1127 	 * Convert battery capacity to the corresponding initial coulomb counter
1128 	 * and set into coulomb counter registers.
1129 	 */
1130 	data->init_clbcnt = sc27xx_fgu_cap_to_clbcnt(data, data->init_cap);
1131 	ret = sc27xx_fgu_set_clbcnt(data, data->init_clbcnt);
1132 	if (ret) {
1133 		dev_err(data->dev, "failed to initialize coulomb counter\n");
1134 		goto disable_clk;
1135 	}
1136 
1137 	return 0;
1138 
1139 disable_clk:
1140 	regmap_update_bits(data->regmap, SC27XX_CLK_EN0, SC27XX_FGU_RTC_EN, 0);
1141 disable_fgu:
1142 	regmap_update_bits(data->regmap, SC27XX_MODULE_EN0, SC27XX_FGU_EN, 0);
1143 
1144 	return ret;
1145 }
1146 
1147 static int sc27xx_fgu_probe(struct platform_device *pdev)
1148 {
1149 	struct device *dev = &pdev->dev;
1150 	struct device_node *np = dev->of_node;
1151 	struct power_supply_config fgu_cfg = { };
1152 	struct sc27xx_fgu_data *data;
1153 	int ret, irq;
1154 
1155 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
1156 	if (!data)
1157 		return -ENOMEM;
1158 
1159 	data->regmap = dev_get_regmap(dev->parent, NULL);
1160 	if (!data->regmap) {
1161 		dev_err(dev, "failed to get regmap\n");
1162 		return -ENODEV;
1163 	}
1164 
1165 	ret = device_property_read_u32(dev, "reg", &data->base);
1166 	if (ret) {
1167 		dev_err(dev, "failed to get fgu address\n");
1168 		return ret;
1169 	}
1170 
1171 	ret = device_property_read_u32(&pdev->dev,
1172 				       "sprd,calib-resistance-micro-ohms",
1173 				       &data->calib_resist);
1174 	if (ret) {
1175 		dev_err(&pdev->dev,
1176 			"failed to get fgu calibration resistance\n");
1177 		return ret;
1178 	}
1179 
1180 	data->channel = devm_iio_channel_get(dev, "bat-temp");
1181 	if (IS_ERR(data->channel)) {
1182 		dev_err(dev, "failed to get IIO channel\n");
1183 		return PTR_ERR(data->channel);
1184 	}
1185 
1186 	data->charge_chan = devm_iio_channel_get(dev, "charge-vol");
1187 	if (IS_ERR(data->charge_chan)) {
1188 		dev_err(dev, "failed to get charge IIO channel\n");
1189 		return PTR_ERR(data->charge_chan);
1190 	}
1191 
1192 	data->gpiod = devm_gpiod_get(dev, "bat-detect", GPIOD_IN);
1193 	if (IS_ERR(data->gpiod)) {
1194 		dev_err(dev, "failed to get battery detection GPIO\n");
1195 		return PTR_ERR(data->gpiod);
1196 	}
1197 
1198 	ret = gpiod_get_value_cansleep(data->gpiod);
1199 	if (ret < 0) {
1200 		dev_err(dev, "failed to get gpio state\n");
1201 		return ret;
1202 	}
1203 
1204 	data->bat_present = !!ret;
1205 	mutex_init(&data->lock);
1206 	data->dev = dev;
1207 	platform_set_drvdata(pdev, data);
1208 
1209 	fgu_cfg.drv_data = data;
1210 	fgu_cfg.of_node = np;
1211 	data->battery = devm_power_supply_register(dev, &sc27xx_fgu_desc,
1212 						   &fgu_cfg);
1213 	if (IS_ERR(data->battery)) {
1214 		dev_err(dev, "failed to register power supply\n");
1215 		return PTR_ERR(data->battery);
1216 	}
1217 
1218 	ret = sc27xx_fgu_hw_init(data);
1219 	if (ret) {
1220 		dev_err(dev, "failed to initialize fgu hardware\n");
1221 		return ret;
1222 	}
1223 
1224 	ret = devm_add_action_or_reset(dev, sc27xx_fgu_disable, data);
1225 	if (ret) {
1226 		dev_err(dev, "failed to add fgu disable action\n");
1227 		return ret;
1228 	}
1229 
1230 	irq = platform_get_irq(pdev, 0);
1231 	if (irq < 0) {
1232 		dev_err(dev, "no irq resource specified\n");
1233 		return irq;
1234 	}
1235 
1236 	ret = devm_request_threaded_irq(data->dev, irq, NULL,
1237 					sc27xx_fgu_interrupt,
1238 					IRQF_NO_SUSPEND | IRQF_ONESHOT,
1239 					pdev->name, data);
1240 	if (ret) {
1241 		dev_err(data->dev, "failed to request fgu IRQ\n");
1242 		return ret;
1243 	}
1244 
1245 	irq = gpiod_to_irq(data->gpiod);
1246 	if (irq < 0) {
1247 		dev_err(dev, "failed to translate GPIO to IRQ\n");
1248 		return irq;
1249 	}
1250 
1251 	ret = devm_request_threaded_irq(dev, irq, NULL,
1252 					sc27xx_fgu_bat_detection,
1253 					IRQF_ONESHOT | IRQF_TRIGGER_RISING |
1254 					IRQF_TRIGGER_FALLING,
1255 					pdev->name, data);
1256 	if (ret) {
1257 		dev_err(dev, "failed to request IRQ\n");
1258 		return ret;
1259 	}
1260 
1261 	return 0;
1262 }
1263 
1264 #ifdef CONFIG_PM_SLEEP
1265 static int sc27xx_fgu_resume(struct device *dev)
1266 {
1267 	struct sc27xx_fgu_data *data = dev_get_drvdata(dev);
1268 	int ret;
1269 
1270 	ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_EN,
1271 				 SC27XX_FGU_LOW_OVERLOAD_INT |
1272 				 SC27XX_FGU_CLBCNT_DELTA_INT, 0);
1273 	if (ret) {
1274 		dev_err(data->dev, "failed to disable fgu interrupts\n");
1275 		return ret;
1276 	}
1277 
1278 	return 0;
1279 }
1280 
1281 static int sc27xx_fgu_suspend(struct device *dev)
1282 {
1283 	struct sc27xx_fgu_data *data = dev_get_drvdata(dev);
1284 	int ret, status, ocv;
1285 
1286 	ret = sc27xx_fgu_get_status(data, &status);
1287 	if (ret)
1288 		return ret;
1289 
1290 	/*
1291 	 * If we are charging, then no need to enable the FGU interrupts to
1292 	 * adjust the battery capacity.
1293 	 */
1294 	if (status != POWER_SUPPLY_STATUS_NOT_CHARGING &&
1295 	    status != POWER_SUPPLY_STATUS_DISCHARGING)
1296 		return 0;
1297 
1298 	ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_EN,
1299 				 SC27XX_FGU_LOW_OVERLOAD_INT,
1300 				 SC27XX_FGU_LOW_OVERLOAD_INT);
1301 	if (ret) {
1302 		dev_err(data->dev, "failed to enable low voltage interrupt\n");
1303 		return ret;
1304 	}
1305 
1306 	ret = sc27xx_fgu_get_vbat_ocv(data, &ocv);
1307 	if (ret)
1308 		goto disable_int;
1309 
1310 	/*
1311 	 * If current OCV is less than the minimum voltage, we should enable the
1312 	 * coulomb counter threshold interrupt to notify events to adjust the
1313 	 * battery capacity.
1314 	 */
1315 	if (ocv < data->min_volt) {
1316 		ret = regmap_update_bits(data->regmap,
1317 					 data->base + SC27XX_FGU_INT_EN,
1318 					 SC27XX_FGU_CLBCNT_DELTA_INT,
1319 					 SC27XX_FGU_CLBCNT_DELTA_INT);
1320 		if (ret) {
1321 			dev_err(data->dev,
1322 				"failed to enable coulomb threshold int\n");
1323 			goto disable_int;
1324 		}
1325 	}
1326 
1327 	return 0;
1328 
1329 disable_int:
1330 	regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_EN,
1331 			   SC27XX_FGU_LOW_OVERLOAD_INT, 0);
1332 	return ret;
1333 }
1334 #endif
1335 
1336 static const struct dev_pm_ops sc27xx_fgu_pm_ops = {
1337 	SET_SYSTEM_SLEEP_PM_OPS(sc27xx_fgu_suspend, sc27xx_fgu_resume)
1338 };
1339 
1340 static const struct of_device_id sc27xx_fgu_of_match[] = {
1341 	{ .compatible = "sprd,sc2731-fgu", },
1342 	{ }
1343 };
1344 
1345 static struct platform_driver sc27xx_fgu_driver = {
1346 	.probe = sc27xx_fgu_probe,
1347 	.driver = {
1348 		.name = "sc27xx-fgu",
1349 		.of_match_table = sc27xx_fgu_of_match,
1350 		.pm = &sc27xx_fgu_pm_ops,
1351 	}
1352 };
1353 
1354 module_platform_driver(sc27xx_fgu_driver);
1355 
1356 MODULE_DESCRIPTION("Spreadtrum SC27XX PMICs Fual Gauge Unit Driver");
1357 MODULE_LICENSE("GPL v2");
1358