1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Spreadtrum Communications Inc.
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>
11 #include <linux/platform_device.h>
12 #include <linux/power_supply.h>
13 #include <linux/regmap.h>
14 #include <linux/slab.h>
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)
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
46 #define SC27XX_WRITE_SELCLB_EN BIT(0)
47 #define SC27XX_FGU_CLBCNT_MASK GENMASK(15, 0)
48 #define SC27XX_FGU_CLBCNT_SHIFT 16
49 #define SC27XX_FGU_LOW_OVERLOAD_MASK GENMASK(12, 0)
51 #define SC27XX_FGU_INT_MASK GENMASK(9, 0)
52 #define SC27XX_FGU_LOW_OVERLOAD_INT BIT(0)
53 #define SC27XX_FGU_CLBCNT_DELTA_INT BIT(2)
55 #define SC27XX_FGU_MODE_AREA_MASK GENMASK(15, 12)
56 #define SC27XX_FGU_CAP_AREA_MASK GENMASK(11, 0)
57 #define SC27XX_FGU_MODE_AREA_SHIFT 12
59 #define SC27XX_FGU_FIRST_POWERTON GENMASK(3, 0)
60 #define SC27XX_FGU_DEFAULT_CAP GENMASK(11, 0)
61 #define SC27XX_FGU_NORMAIL_POWERTON 0x5
63 #define SC27XX_FGU_CUR_BASIC_ADC 8192
64 #define SC27XX_FGU_SAMPLE_HZ 2
67 * struct sc27xx_fgu_data: describe the FGU device
68 * @regmap: regmap for register access
69 * @dev: platform device
70 * @battery: battery power supply
71 * @base: the base offset for the controller
72 * @lock: protect the structure
73 * @gpiod: GPIO for battery detection
74 * @channel: IIO channel to get battery temperature
75 * @charge_chan: IIO channel to get charge voltage
76 * @internal_resist: the battery internal resistance in mOhm
77 * @total_cap: the total capacity of the battery in mAh
78 * @init_cap: the initial capacity of the battery in mAh
79 * @alarm_cap: the alarm capacity
80 * @init_clbcnt: the initial coulomb counter
81 * @max_volt: the maximum constant input voltage in millivolt
82 * @min_volt: the minimum drained battery voltage in microvolt
83 * @table_len: the capacity table length
84 * @cur_1000ma_adc: ADC value corresponding to 1000 mA
85 * @vol_1000mv_adc: ADC value corresponding to 1000 mV
86 * @cap_table: capacity table with corresponding ocv
88 struct sc27xx_fgu_data {
89 struct regmap *regmap;
91 struct power_supply *battery;
94 struct gpio_desc *gpiod;
95 struct iio_channel *channel;
96 struct iio_channel *charge_chan;
108 struct power_supply_battery_ocv_table *cap_table;
111 static int sc27xx_fgu_cap_to_clbcnt(struct sc27xx_fgu_data *data, int capacity);
113 static const char * const sc27xx_charger_supply_name[] = {
120 static int sc27xx_fgu_adc_to_current(struct sc27xx_fgu_data *data, int adc)
122 return DIV_ROUND_CLOSEST(adc * 1000, data->cur_1000ma_adc);
125 static int sc27xx_fgu_adc_to_voltage(struct sc27xx_fgu_data *data, int adc)
127 return DIV_ROUND_CLOSEST(adc * 1000, data->vol_1000mv_adc);
130 static int sc27xx_fgu_voltage_to_adc(struct sc27xx_fgu_data *data, int vol)
132 return DIV_ROUND_CLOSEST(vol * data->vol_1000mv_adc, 1000);
135 static bool sc27xx_fgu_is_first_poweron(struct sc27xx_fgu_data *data)
137 int ret, status, cap, mode;
139 ret = regmap_read(data->regmap,
140 data->base + SC27XX_FGU_USER_AREA_STATUS, &status);
145 * We use low 4 bits to save the last battery capacity and high 12 bits
146 * to save the system boot mode.
148 mode = (status & SC27XX_FGU_MODE_AREA_MASK) >> SC27XX_FGU_MODE_AREA_SHIFT;
149 cap = status & SC27XX_FGU_CAP_AREA_MASK;
152 * When FGU has been powered down, the user area registers became
153 * default value (0xffff), which can be used to valid if the system is
154 * first power on or not.
156 if (mode == SC27XX_FGU_FIRST_POWERTON || cap == SC27XX_FGU_DEFAULT_CAP)
162 static int sc27xx_fgu_save_boot_mode(struct sc27xx_fgu_data *data,
167 ret = regmap_update_bits(data->regmap,
168 data->base + SC27XX_FGU_USER_AREA_CLEAR,
169 SC27XX_FGU_MODE_AREA_MASK,
170 SC27XX_FGU_MODE_AREA_MASK);
175 * Since the user area registers are put on power always-on region,
176 * then these registers changing time will be a little long. Thus
177 * here we should delay 200us to wait until values are updated
178 * successfully according to the datasheet.
182 ret = regmap_update_bits(data->regmap,
183 data->base + SC27XX_FGU_USER_AREA_SET,
184 SC27XX_FGU_MODE_AREA_MASK,
185 boot_mode << SC27XX_FGU_MODE_AREA_SHIFT);
190 * Since the user area registers are put on power always-on region,
191 * then these registers changing time will be a little long. Thus
192 * here we should delay 200us to wait until values are updated
193 * successfully according to the datasheet.
198 * According to the datasheet, we should set the USER_AREA_CLEAR to 0 to
199 * make the user area data available, otherwise we can not save the user
202 return regmap_update_bits(data->regmap,
203 data->base + SC27XX_FGU_USER_AREA_CLEAR,
204 SC27XX_FGU_MODE_AREA_MASK, 0);
207 static int sc27xx_fgu_save_last_cap(struct sc27xx_fgu_data *data, int cap)
211 ret = regmap_update_bits(data->regmap,
212 data->base + SC27XX_FGU_USER_AREA_CLEAR,
213 SC27XX_FGU_CAP_AREA_MASK,
214 SC27XX_FGU_CAP_AREA_MASK);
219 * Since the user area registers are put on power always-on region,
220 * then these registers changing time will be a little long. Thus
221 * here we should delay 200us to wait until values are updated
222 * successfully according to the datasheet.
226 ret = regmap_update_bits(data->regmap,
227 data->base + SC27XX_FGU_USER_AREA_SET,
228 SC27XX_FGU_CAP_AREA_MASK, cap);
233 * Since the user area registers are put on power always-on region,
234 * then these registers changing time will be a little long. Thus
235 * here we should delay 200us to wait until values are updated
236 * successfully according to the datasheet.
241 * According to the datasheet, we should set the USER_AREA_CLEAR to 0 to
242 * make the user area data available, otherwise we can not save the user
245 return regmap_update_bits(data->regmap,
246 data->base + SC27XX_FGU_USER_AREA_CLEAR,
247 SC27XX_FGU_CAP_AREA_MASK, 0);
250 static int sc27xx_fgu_read_last_cap(struct sc27xx_fgu_data *data, int *cap)
254 ret = regmap_read(data->regmap,
255 data->base + SC27XX_FGU_USER_AREA_STATUS, &value);
259 *cap = value & SC27XX_FGU_CAP_AREA_MASK;
264 * When system boots on, we can not read battery capacity from coulomb
265 * registers, since now the coulomb registers are invalid. So we should
266 * calculate the battery open circuit voltage, and get current battery
267 * capacity according to the capacity table.
269 static int sc27xx_fgu_get_boot_capacity(struct sc27xx_fgu_data *data, int *cap)
271 int volt, cur, oci, ocv, ret;
272 bool is_first_poweron = sc27xx_fgu_is_first_poweron(data);
275 * If system is not the first power on, we should use the last saved
276 * battery capacity as the initial battery capacity. Otherwise we should
277 * re-calculate the initial battery capacity.
279 if (!is_first_poweron) {
280 ret = sc27xx_fgu_read_last_cap(data, cap);
284 return sc27xx_fgu_save_boot_mode(data, SC27XX_FGU_NORMAIL_POWERTON);
288 * After system booting on, the SC27XX_FGU_CLBCNT_QMAXL register saved
289 * the first sampled open circuit current.
291 ret = regmap_read(data->regmap, data->base + SC27XX_FGU_CLBCNT_QMAXL,
297 oci = sc27xx_fgu_adc_to_current(data, cur - SC27XX_FGU_CUR_BASIC_ADC);
300 * Should get the OCV from SC27XX_FGU_POCV register at the system
301 * beginning. It is ADC values reading from registers which need to
302 * convert the corresponding voltage.
304 ret = regmap_read(data->regmap, data->base + SC27XX_FGU_POCV, &volt);
308 volt = sc27xx_fgu_adc_to_voltage(data, volt);
309 ocv = volt * 1000 - oci * data->internal_resist;
312 * Parse the capacity table to look up the correct capacity percent
313 * according to current battery's corresponding OCV values.
315 *cap = power_supply_ocv2cap_simple(data->cap_table, data->table_len,
318 ret = sc27xx_fgu_save_last_cap(data, *cap);
322 return sc27xx_fgu_save_boot_mode(data, SC27XX_FGU_NORMAIL_POWERTON);
325 static int sc27xx_fgu_set_clbcnt(struct sc27xx_fgu_data *data, int clbcnt)
329 clbcnt *= SC27XX_FGU_SAMPLE_HZ;
331 ret = regmap_update_bits(data->regmap,
332 data->base + SC27XX_FGU_CLBCNT_SETL,
333 SC27XX_FGU_CLBCNT_MASK, clbcnt);
337 ret = regmap_update_bits(data->regmap,
338 data->base + SC27XX_FGU_CLBCNT_SETH,
339 SC27XX_FGU_CLBCNT_MASK,
340 clbcnt >> SC27XX_FGU_CLBCNT_SHIFT);
344 return regmap_update_bits(data->regmap, data->base + SC27XX_FGU_START,
345 SC27XX_WRITE_SELCLB_EN,
346 SC27XX_WRITE_SELCLB_EN);
349 static int sc27xx_fgu_get_clbcnt(struct sc27xx_fgu_data *data, int *clb_cnt)
353 ret = regmap_read(data->regmap, data->base + SC27XX_FGU_CLBCNT_VALL,
358 ret = regmap_read(data->regmap, data->base + SC27XX_FGU_CLBCNT_VALH,
363 *clb_cnt = ccl & SC27XX_FGU_CLBCNT_MASK;
364 *clb_cnt |= (cch & SC27XX_FGU_CLBCNT_MASK) << SC27XX_FGU_CLBCNT_SHIFT;
365 *clb_cnt /= SC27XX_FGU_SAMPLE_HZ;
370 static int sc27xx_fgu_get_capacity(struct sc27xx_fgu_data *data, int *cap)
372 int ret, cur_clbcnt, delta_clbcnt, delta_cap, temp;
374 /* Get current coulomb counters firstly */
375 ret = sc27xx_fgu_get_clbcnt(data, &cur_clbcnt);
379 delta_clbcnt = cur_clbcnt - data->init_clbcnt;
382 * Convert coulomb counter to delta capacity (mAh), and set multiplier
383 * as 100 to improve the precision.
385 temp = DIV_ROUND_CLOSEST(delta_clbcnt, 360);
386 temp = sc27xx_fgu_adc_to_current(data, temp);
389 * Convert to capacity percent of the battery total capacity,
390 * and multiplier is 100 too.
392 delta_cap = DIV_ROUND_CLOSEST(temp * 100, data->total_cap);
393 *cap = delta_cap + data->init_cap;
398 static int sc27xx_fgu_get_vbat_vol(struct sc27xx_fgu_data *data, int *val)
402 ret = regmap_read(data->regmap, data->base + SC27XX_FGU_VOLTAGE, &vol);
407 * It is ADC values reading from registers which need to convert to
408 * corresponding voltage values.
410 *val = sc27xx_fgu_adc_to_voltage(data, vol);
415 static int sc27xx_fgu_get_current(struct sc27xx_fgu_data *data, int *val)
419 ret = regmap_read(data->regmap, data->base + SC27XX_FGU_CURRENT, &cur);
424 * It is ADC values reading from registers which need to convert to
425 * corresponding current values.
427 *val = sc27xx_fgu_adc_to_current(data, cur - SC27XX_FGU_CUR_BASIC_ADC);
432 static int sc27xx_fgu_get_vbat_ocv(struct sc27xx_fgu_data *data, int *val)
436 ret = sc27xx_fgu_get_vbat_vol(data, &vol);
440 ret = sc27xx_fgu_get_current(data, &cur);
444 /* Return the battery OCV in micro volts. */
445 *val = vol * 1000 - cur * data->internal_resist;
450 static int sc27xx_fgu_get_charge_vol(struct sc27xx_fgu_data *data, int *val)
454 ret = iio_read_channel_processed(data->charge_chan, &vol);
462 static int sc27xx_fgu_get_temp(struct sc27xx_fgu_data *data, int *temp)
464 return iio_read_channel_processed(data->channel, temp);
467 static int sc27xx_fgu_get_health(struct sc27xx_fgu_data *data, int *health)
471 ret = sc27xx_fgu_get_vbat_vol(data, &vol);
475 if (vol > data->max_volt)
476 *health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
478 *health = POWER_SUPPLY_HEALTH_GOOD;
483 static int sc27xx_fgu_get_status(struct sc27xx_fgu_data *data, int *status)
485 union power_supply_propval val;
486 struct power_supply *psy;
487 int i, ret = -EINVAL;
489 for (i = 0; i < ARRAY_SIZE(sc27xx_charger_supply_name); i++) {
490 psy = power_supply_get_by_name(sc27xx_charger_supply_name[i]);
494 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_STATUS,
496 power_supply_put(psy);
500 *status = val.intval;
506 static int sc27xx_fgu_get_property(struct power_supply *psy,
507 enum power_supply_property psp,
508 union power_supply_propval *val)
510 struct sc27xx_fgu_data *data = power_supply_get_drvdata(psy);
514 mutex_lock(&data->lock);
517 case POWER_SUPPLY_PROP_STATUS:
518 ret = sc27xx_fgu_get_status(data, &value);
525 case POWER_SUPPLY_PROP_HEALTH:
526 ret = sc27xx_fgu_get_health(data, &value);
533 case POWER_SUPPLY_PROP_PRESENT:
534 val->intval = data->bat_present;
537 case POWER_SUPPLY_PROP_TEMP:
538 ret = sc27xx_fgu_get_temp(data, &value);
545 case POWER_SUPPLY_PROP_TECHNOLOGY:
546 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
549 case POWER_SUPPLY_PROP_CAPACITY:
550 ret = sc27xx_fgu_get_capacity(data, &value);
557 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
558 ret = sc27xx_fgu_get_vbat_vol(data, &value);
562 val->intval = value * 1000;
565 case POWER_SUPPLY_PROP_VOLTAGE_OCV:
566 ret = sc27xx_fgu_get_vbat_ocv(data, &value);
573 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
574 ret = sc27xx_fgu_get_charge_vol(data, &value);
581 case POWER_SUPPLY_PROP_CURRENT_NOW:
582 case POWER_SUPPLY_PROP_CURRENT_AVG:
583 ret = sc27xx_fgu_get_current(data, &value);
587 val->intval = value * 1000;
596 mutex_unlock(&data->lock);
600 static int sc27xx_fgu_set_property(struct power_supply *psy,
601 enum power_supply_property psp,
602 const union power_supply_propval *val)
604 struct sc27xx_fgu_data *data = power_supply_get_drvdata(psy);
607 if (psp != POWER_SUPPLY_PROP_CAPACITY)
610 mutex_lock(&data->lock);
612 ret = sc27xx_fgu_save_last_cap(data, val->intval);
614 mutex_unlock(&data->lock);
617 dev_err(data->dev, "failed to save battery capacity\n");
622 static void sc27xx_fgu_external_power_changed(struct power_supply *psy)
624 struct sc27xx_fgu_data *data = power_supply_get_drvdata(psy);
626 power_supply_changed(data->battery);
629 static int sc27xx_fgu_property_is_writeable(struct power_supply *psy,
630 enum power_supply_property psp)
632 return psp == POWER_SUPPLY_PROP_CAPACITY;
635 static enum power_supply_property sc27xx_fgu_props[] = {
636 POWER_SUPPLY_PROP_STATUS,
637 POWER_SUPPLY_PROP_HEALTH,
638 POWER_SUPPLY_PROP_PRESENT,
639 POWER_SUPPLY_PROP_TEMP,
640 POWER_SUPPLY_PROP_TECHNOLOGY,
641 POWER_SUPPLY_PROP_CAPACITY,
642 POWER_SUPPLY_PROP_VOLTAGE_NOW,
643 POWER_SUPPLY_PROP_VOLTAGE_OCV,
644 POWER_SUPPLY_PROP_CURRENT_NOW,
645 POWER_SUPPLY_PROP_CURRENT_AVG,
646 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
649 static const struct power_supply_desc sc27xx_fgu_desc = {
650 .name = "sc27xx-fgu",
651 .type = POWER_SUPPLY_TYPE_BATTERY,
652 .properties = sc27xx_fgu_props,
653 .num_properties = ARRAY_SIZE(sc27xx_fgu_props),
654 .get_property = sc27xx_fgu_get_property,
655 .set_property = sc27xx_fgu_set_property,
656 .external_power_changed = sc27xx_fgu_external_power_changed,
657 .property_is_writeable = sc27xx_fgu_property_is_writeable,
660 static void sc27xx_fgu_adjust_cap(struct sc27xx_fgu_data *data, int cap)
662 data->init_cap = cap;
663 data->init_clbcnt = sc27xx_fgu_cap_to_clbcnt(data, data->init_cap);
666 static irqreturn_t sc27xx_fgu_interrupt(int irq, void *dev_id)
668 struct sc27xx_fgu_data *data = dev_id;
669 int ret, cap, ocv, adc;
672 mutex_lock(&data->lock);
674 ret = regmap_read(data->regmap, data->base + SC27XX_FGU_INT_STS,
679 ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_CLR,
685 * When low overload voltage interrupt happens, we should calibrate the
686 * battery capacity in lower voltage stage.
688 if (!(status & SC27XX_FGU_LOW_OVERLOAD_INT))
691 ret = sc27xx_fgu_get_capacity(data, &cap);
695 ret = sc27xx_fgu_get_vbat_ocv(data, &ocv);
700 * If current OCV value is less than the minimum OCV value in OCV table,
701 * which means now battery capacity is 0%, and we should adjust the
702 * inititial capacity to 0.
704 if (ocv <= data->cap_table[data->table_len - 1].ocv) {
705 sc27xx_fgu_adjust_cap(data, 0);
706 } else if (ocv <= data->min_volt) {
708 * If current OCV value is less than the low alarm voltage, but
709 * current capacity is larger than the alarm capacity, we should
710 * adjust the inititial capacity to alarm capacity.
712 if (cap > data->alarm_cap) {
713 sc27xx_fgu_adjust_cap(data, data->alarm_cap);
714 } else if (cap <= 0) {
718 * If current capacity is equal with 0 or less than 0
719 * (some error occurs), we should adjust inititial
720 * capacity to the capacity corresponding to current OCV
723 cur_cap = power_supply_ocv2cap_simple(data->cap_table,
726 sc27xx_fgu_adjust_cap(data, cur_cap);
730 * After adjusting the battery capacity, we should set the
731 * lowest alarm voltage instead.
733 data->min_volt = data->cap_table[data->table_len - 1].ocv;
734 adc = sc27xx_fgu_voltage_to_adc(data, data->min_volt / 1000);
735 regmap_update_bits(data->regmap, data->base + SC27XX_FGU_LOW_OVERLOAD,
736 SC27XX_FGU_LOW_OVERLOAD_MASK, adc);
740 mutex_unlock(&data->lock);
742 power_supply_changed(data->battery);
746 static irqreturn_t sc27xx_fgu_bat_detection(int irq, void *dev_id)
748 struct sc27xx_fgu_data *data = dev_id;
751 mutex_lock(&data->lock);
753 state = gpiod_get_value_cansleep(data->gpiod);
755 dev_err(data->dev, "failed to get gpio state\n");
756 mutex_unlock(&data->lock);
757 return IRQ_RETVAL(state);
760 data->bat_present = !!state;
762 mutex_unlock(&data->lock);
764 power_supply_changed(data->battery);
768 static void sc27xx_fgu_disable(void *_data)
770 struct sc27xx_fgu_data *data = _data;
772 regmap_update_bits(data->regmap, SC27XX_CLK_EN0, SC27XX_FGU_RTC_EN, 0);
773 regmap_update_bits(data->regmap, SC27XX_MODULE_EN0, SC27XX_FGU_EN, 0);
776 static int sc27xx_fgu_cap_to_clbcnt(struct sc27xx_fgu_data *data, int capacity)
779 * Get current capacity (mAh) = battery total capacity (mAh) *
780 * current capacity percent (capacity / 100).
782 int cur_cap = DIV_ROUND_CLOSEST(data->total_cap * capacity, 100);
785 * Convert current capacity (mAh) to coulomb counter according to the
786 * formula: 1 mAh =3.6 coulomb.
788 return DIV_ROUND_CLOSEST(cur_cap * 36 * data->cur_1000ma_adc, 10);
791 static int sc27xx_fgu_calibration(struct sc27xx_fgu_data *data)
793 struct nvmem_cell *cell;
794 int calib_data, cal_4200mv;
798 cell = nvmem_cell_get(data->dev, "fgu_calib");
800 return PTR_ERR(cell);
802 buf = nvmem_cell_read(cell, &len);
803 nvmem_cell_put(cell);
808 memcpy(&calib_data, buf, min(len, sizeof(u32)));
811 * Get the ADC value corresponding to 4200 mV from eFuse controller
812 * according to below formula. Then convert to ADC values corresponding
813 * to 1000 mV and 1000 mA.
815 cal_4200mv = (calib_data & 0x1ff) + 6963 - 4096 - 256;
816 data->vol_1000mv_adc = DIV_ROUND_CLOSEST(cal_4200mv * 10, 42);
817 data->cur_1000ma_adc = data->vol_1000mv_adc * 4;
823 static int sc27xx_fgu_hw_init(struct sc27xx_fgu_data *data)
825 struct power_supply_battery_info info = { };
826 struct power_supply_battery_ocv_table *table;
827 int ret, delta_clbcnt, alarm_adc;
829 ret = power_supply_get_battery_info(data->battery, &info);
831 dev_err(data->dev, "failed to get battery information\n");
835 data->total_cap = info.charge_full_design_uah / 1000;
836 data->max_volt = info.constant_charge_voltage_max_uv / 1000;
837 data->internal_resist = info.factory_internal_resistance_uohm / 1000;
838 data->min_volt = info.voltage_min_design_uv;
841 * For SC27XX fuel gauge device, we only use one ocv-capacity
842 * table in normal temperature 20 Celsius.
844 table = power_supply_find_ocv2cap_table(&info, 20, &data->table_len);
848 data->cap_table = devm_kmemdup(data->dev, table,
849 data->table_len * sizeof(*table),
851 if (!data->cap_table) {
852 power_supply_put_battery_info(data->battery, &info);
856 data->alarm_cap = power_supply_ocv2cap_simple(data->cap_table,
860 power_supply_put_battery_info(data->battery, &info);
862 ret = sc27xx_fgu_calibration(data);
866 /* Enable the FGU module */
867 ret = regmap_update_bits(data->regmap, SC27XX_MODULE_EN0,
868 SC27XX_FGU_EN, SC27XX_FGU_EN);
870 dev_err(data->dev, "failed to enable fgu\n");
874 /* Enable the FGU RTC clock to make it work */
875 ret = regmap_update_bits(data->regmap, SC27XX_CLK_EN0,
876 SC27XX_FGU_RTC_EN, SC27XX_FGU_RTC_EN);
878 dev_err(data->dev, "failed to enable fgu RTC clock\n");
882 ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_CLR,
883 SC27XX_FGU_INT_MASK, SC27XX_FGU_INT_MASK);
885 dev_err(data->dev, "failed to clear interrupt status\n");
890 * Set the voltage low overload threshold, which means when the battery
891 * voltage is lower than this threshold, the controller will generate
892 * one interrupt to notify.
894 alarm_adc = sc27xx_fgu_voltage_to_adc(data, data->min_volt / 1000);
895 ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_LOW_OVERLOAD,
896 SC27XX_FGU_LOW_OVERLOAD_MASK, alarm_adc);
898 dev_err(data->dev, "failed to set fgu low overload\n");
903 * Set the coulomb counter delta threshold, that means when the coulomb
904 * counter change is multiples of the delta threshold, the controller
905 * will generate one interrupt to notify the users to update the battery
906 * capacity. Now we set the delta threshold as a counter value of 1%
909 delta_clbcnt = sc27xx_fgu_cap_to_clbcnt(data, 1);
911 ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_CLBCNT_DELTL,
912 SC27XX_FGU_CLBCNT_MASK, delta_clbcnt);
914 dev_err(data->dev, "failed to set low delta coulomb counter\n");
918 ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_CLBCNT_DELTH,
919 SC27XX_FGU_CLBCNT_MASK,
920 delta_clbcnt >> SC27XX_FGU_CLBCNT_SHIFT);
922 dev_err(data->dev, "failed to set high delta coulomb counter\n");
927 * Get the boot battery capacity when system powers on, which is used to
928 * initialize the coulomb counter. After that, we can read the coulomb
929 * counter to measure the battery capacity.
931 ret = sc27xx_fgu_get_boot_capacity(data, &data->init_cap);
933 dev_err(data->dev, "failed to get boot capacity\n");
938 * Convert battery capacity to the corresponding initial coulomb counter
939 * and set into coulomb counter registers.
941 data->init_clbcnt = sc27xx_fgu_cap_to_clbcnt(data, data->init_cap);
942 ret = sc27xx_fgu_set_clbcnt(data, data->init_clbcnt);
944 dev_err(data->dev, "failed to initialize coulomb counter\n");
951 regmap_update_bits(data->regmap, SC27XX_CLK_EN0, SC27XX_FGU_RTC_EN, 0);
953 regmap_update_bits(data->regmap, SC27XX_MODULE_EN0, SC27XX_FGU_EN, 0);
958 static int sc27xx_fgu_probe(struct platform_device *pdev)
960 struct device_node *np = pdev->dev.of_node;
961 struct power_supply_config fgu_cfg = { };
962 struct sc27xx_fgu_data *data;
965 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
969 data->regmap = dev_get_regmap(pdev->dev.parent, NULL);
971 dev_err(&pdev->dev, "failed to get regmap\n");
975 ret = device_property_read_u32(&pdev->dev, "reg", &data->base);
977 dev_err(&pdev->dev, "failed to get fgu address\n");
981 data->channel = devm_iio_channel_get(&pdev->dev, "bat-temp");
982 if (IS_ERR(data->channel)) {
983 dev_err(&pdev->dev, "failed to get IIO channel\n");
984 return PTR_ERR(data->channel);
987 data->charge_chan = devm_iio_channel_get(&pdev->dev, "charge-vol");
988 if (IS_ERR(data->charge_chan)) {
989 dev_err(&pdev->dev, "failed to get charge IIO channel\n");
990 return PTR_ERR(data->charge_chan);
993 data->gpiod = devm_gpiod_get(&pdev->dev, "bat-detect", GPIOD_IN);
994 if (IS_ERR(data->gpiod)) {
995 dev_err(&pdev->dev, "failed to get battery detection GPIO\n");
996 return PTR_ERR(data->gpiod);
999 ret = gpiod_get_value_cansleep(data->gpiod);
1001 dev_err(&pdev->dev, "failed to get gpio state\n");
1005 data->bat_present = !!ret;
1006 mutex_init(&data->lock);
1007 data->dev = &pdev->dev;
1008 platform_set_drvdata(pdev, data);
1010 fgu_cfg.drv_data = data;
1011 fgu_cfg.of_node = np;
1012 data->battery = devm_power_supply_register(&pdev->dev, &sc27xx_fgu_desc,
1014 if (IS_ERR(data->battery)) {
1015 dev_err(&pdev->dev, "failed to register power supply\n");
1016 return PTR_ERR(data->battery);
1019 ret = sc27xx_fgu_hw_init(data);
1021 dev_err(&pdev->dev, "failed to initialize fgu hardware\n");
1025 ret = devm_add_action(&pdev->dev, sc27xx_fgu_disable, data);
1027 sc27xx_fgu_disable(data);
1028 dev_err(&pdev->dev, "failed to add fgu disable action\n");
1032 irq = platform_get_irq(pdev, 0);
1034 dev_err(&pdev->dev, "no irq resource specified\n");
1038 ret = devm_request_threaded_irq(data->dev, irq, NULL,
1039 sc27xx_fgu_interrupt,
1040 IRQF_NO_SUSPEND | IRQF_ONESHOT,
1043 dev_err(data->dev, "failed to request fgu IRQ\n");
1047 irq = gpiod_to_irq(data->gpiod);
1049 dev_err(&pdev->dev, "failed to translate GPIO to IRQ\n");
1053 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1054 sc27xx_fgu_bat_detection,
1055 IRQF_ONESHOT | IRQF_TRIGGER_RISING |
1056 IRQF_TRIGGER_FALLING,
1059 dev_err(&pdev->dev, "failed to request IRQ\n");
1066 #ifdef CONFIG_PM_SLEEP
1067 static int sc27xx_fgu_resume(struct device *dev)
1069 struct sc27xx_fgu_data *data = dev_get_drvdata(dev);
1072 ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_EN,
1073 SC27XX_FGU_LOW_OVERLOAD_INT |
1074 SC27XX_FGU_CLBCNT_DELTA_INT, 0);
1076 dev_err(data->dev, "failed to disable fgu interrupts\n");
1083 static int sc27xx_fgu_suspend(struct device *dev)
1085 struct sc27xx_fgu_data *data = dev_get_drvdata(dev);
1086 int ret, status, ocv;
1088 ret = sc27xx_fgu_get_status(data, &status);
1093 * If we are charging, then no need to enable the FGU interrupts to
1094 * adjust the battery capacity.
1096 if (status != POWER_SUPPLY_STATUS_NOT_CHARGING)
1099 ret = regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_EN,
1100 SC27XX_FGU_LOW_OVERLOAD_INT,
1101 SC27XX_FGU_LOW_OVERLOAD_INT);
1103 dev_err(data->dev, "failed to enable low voltage interrupt\n");
1107 ret = sc27xx_fgu_get_vbat_ocv(data, &ocv);
1112 * If current OCV is less than the minimum voltage, we should enable the
1113 * coulomb counter threshold interrupt to notify events to adjust the
1116 if (ocv < data->min_volt) {
1117 ret = regmap_update_bits(data->regmap,
1118 data->base + SC27XX_FGU_INT_EN,
1119 SC27XX_FGU_CLBCNT_DELTA_INT,
1120 SC27XX_FGU_CLBCNT_DELTA_INT);
1123 "failed to enable coulomb threshold int\n");
1131 regmap_update_bits(data->regmap, data->base + SC27XX_FGU_INT_EN,
1132 SC27XX_FGU_LOW_OVERLOAD_INT, 0);
1137 static const struct dev_pm_ops sc27xx_fgu_pm_ops = {
1138 SET_SYSTEM_SLEEP_PM_OPS(sc27xx_fgu_suspend, sc27xx_fgu_resume)
1141 static const struct of_device_id sc27xx_fgu_of_match[] = {
1142 { .compatible = "sprd,sc2731-fgu", },
1146 static struct platform_driver sc27xx_fgu_driver = {
1147 .probe = sc27xx_fgu_probe,
1149 .name = "sc27xx-fgu",
1150 .of_match_table = sc27xx_fgu_of_match,
1151 .pm = &sc27xx_fgu_pm_ops,
1155 module_platform_driver(sc27xx_fgu_driver);
1157 MODULE_DESCRIPTION("Spreadtrum SC27XX PMICs Fual Gauge Unit Driver");
1158 MODULE_LICENSE("GPL v2");