| /* |
| * max77854_fuelgauge.c |
| * Samsung MAX77854 Fuel Gauge Driver |
| * |
| * Copyright (C) 2015 Samsung Electronics |
| * |
| * |
| * This program is free software; you can redistribute it and/or modify |
| * it under the terms of the GNU General Public License version 2 as |
| * published by the Free Software Foundation. |
| */ |
| |
| #define DEBUG |
| /* #define BATTERY_LOG_MESSAGE */ |
| |
| #include <linux/mfd/max77854-private.h> |
| #include <linux/battery/sec_fuelgauge.h> |
| #include <linux/of_gpio.h> |
| #include <linux/debugfs.h> |
| #include <linux/seq_file.h> |
| |
| static enum power_supply_property max77854_fuelgauge_props[] = { |
| POWER_SUPPLY_PROP_STATUS, |
| POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| POWER_SUPPLY_PROP_VOLTAGE_AVG, |
| POWER_SUPPLY_PROP_CURRENT_NOW, |
| POWER_SUPPLY_PROP_CURRENT_AVG, |
| POWER_SUPPLY_PROP_CHARGE_FULL, |
| POWER_SUPPLY_PROP_ENERGY_NOW, |
| POWER_SUPPLY_PROP_CAPACITY, |
| POWER_SUPPLY_PROP_TEMP, |
| POWER_SUPPLY_PROP_TEMP_AMBIENT, |
| #if defined(CONFIG_EN_OOPS) |
| POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| #endif |
| POWER_SUPPLY_PROP_ENERGY_FULL, |
| POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, |
| POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, |
| POWER_SUPPLY_PROP_CHARGE_ENABLED, |
| #if defined(CONFIG_BATTERY_AGE_FORECAST) |
| POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| #endif |
| }; |
| |
| bool max77854_fg_fuelalert_init(struct max77854_fuelgauge_data *fuelgauge, |
| int soc); |
| |
| #if !defined(CONFIG_SEC_FACTORY) |
| static void max77854_fg_read_time(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2]; |
| int lsb, msb, time; |
| |
| if (max77854_bulk_read(fuelgauge->i2c, |
| TIME_TO_FULL_REG, 2, data) < 0) { |
| pr_err("%s: Failed to read TTF\n", __func__); |
| return; |
| } |
| |
| lsb = data[0] & 0x1f; |
| msb = ((data[1] << 3) + ((data[0] & 0xe0) >> 5)); |
| time = msb * 180 + (lsb * 5625) / 1000; |
| |
| //pr_info("[Time-to-FULL] %d(secs), %d(mins)\n", time, time / 60); |
| |
| if (max77854_bulk_read(fuelgauge->i2c, |
| TIME_TO_EMPTY_REG, 2, data) < 0) { |
| pr_err("%s: Failed to read TTE\n", __func__); |
| return; |
| } |
| |
| lsb = data[0] & 0x1f; |
| msb = ((data[1] << 3) + ((data[0] & 0xe0) >> 5)); |
| time = msb * 180 + (lsb * 5625) / 1000; |
| |
| //pr_info("[Time-to-EMPTY] %d(secs), %d(mins)\n", time, time / 60); |
| } |
| |
| static void max77854_fg_test_print(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2]; |
| u32 average_vcell; |
| u16 w_data; |
| u32 temp; |
| u32 temp2; |
| #ifdef BATTERY_LOG_MESSAGE |
| u16 reg_data; |
| #endif |
| |
| if (max77854_bulk_read(fuelgauge->i2c, |
| AVR_VCELL_REG, 2, data) < 0) { |
| pr_err("%s: Failed to read VCELL\n", __func__); |
| return; |
| } |
| |
| w_data = (data[1]<<8) | data[0]; |
| |
| temp = (w_data & 0xFFF) * 78125; |
| average_vcell = temp / 1000000; |
| |
| temp = ((w_data & 0xF000) >> 4) * 78125; |
| temp2 = temp / 1000000; |
| average_vcell += (temp2 << 4); |
| |
| #ifdef BATTERY_LOG_MESSAGE |
| pr_info("%s: AVG_VCELL(%d), data(0x%04x)\n", __func__, |
| average_vcell, (data[1]<<8) | data[0]); |
| |
| reg_data = max77854_read_word(fuelgauge->i2c, FULLCAP_REG); |
| pr_info("%s: FULLCAP(%d), data(0x%04x)\n", __func__, |
| reg_data/2, reg_data); |
| |
| reg_data = max77854_read_word(fuelgauge->i2c, REMCAP_REP_REG); |
| pr_info("%s: REMCAP_REP(%d), data(0x%04x)\n", __func__, |
| reg_data/2, reg_data); |
| |
| reg_data = max77854_read_word(fuelgauge->i2c, REMCAP_MIX_REG); |
| pr_info("%s: REMCAP_MIX(%d), data(0x%04x)\n", __func__, |
| reg_data/2, reg_data); |
| |
| reg_data = max77854_read_word(fuelgauge->i2c, REMCAP_AV_REG); |
| pr_info("%s: REMCAP_AV(%d), data(0x%04x)\n", __func__, |
| reg_data/2, reg_data); |
| |
| reg_data = max77854_read_word(fuelgauge->i2c, CONFIG_REG); |
| pr_info("%s: CONFIG_REG(0x%02x), data(0x%04x)\n", __func__, |
| CONFIG_REG, reg_data); |
| #endif |
| |
| max77854_fg_read_time(fuelgauge); |
| } |
| |
| static void max77854_fg_periodic_read(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| u8 reg; |
| int i; |
| int data[0x10]; |
| char *str = NULL; |
| |
| str = kzalloc(sizeof(char)*1024, GFP_KERNEL); |
| if (!str) |
| return; |
| |
| for (i = 0; i < 16; i++) { |
| for (reg = 0; reg < 0x10; reg++) { |
| data[reg] = max77854_read_word(fuelgauge->i2c, reg + i * 0x10); |
| if (data[reg] < 0) { |
| kfree(str); |
| return; |
| } |
| } |
| if (i == 12) |
| continue; |
| sprintf(str+strlen(str), |
| "%04xh,%04xh,%04xh,%04xh,%04xh,%04xh,%04xh,%04xh,", |
| data[0x00], data[0x01], data[0x02], data[0x03], |
| data[0x04], data[0x05], data[0x06], data[0x07]); |
| sprintf(str+strlen(str), |
| "%04xh,%04xh,%04xh,%04xh,%04xh,%04xh,%04xh,%04xh,", |
| data[0x08], data[0x09], data[0x0a], data[0x0b], |
| data[0x0c], data[0x0d], data[0x0e], data[0x0f]); |
| if (i == 4) |
| i = 10; |
| } |
| |
| pr_info("[FG] %s\n", str); |
| |
| kfree(str); |
| } |
| #endif |
| |
| static int max77854_fg_read_vcell(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2]; |
| u32 vcell; |
| u16 w_data; |
| u32 temp; |
| u32 temp2; |
| |
| if (max77854_bulk_read(fuelgauge->i2c, VCELL_REG, 2, data) < 0) { |
| pr_err("%s: Failed to read VCELL\n", __func__); |
| return -1; |
| } |
| |
| w_data = (data[1]<<8) | data[0]; |
| |
| temp = (w_data & 0xFFF) * 78125; |
| vcell = temp / 1000000; |
| |
| temp = ((w_data & 0xF000) >> 4) * 78125; |
| temp2 = temp / 1000000; |
| vcell += (temp2 << 4); |
| |
| if (!(fuelgauge->info.pr_cnt++ % PRINT_COUNT)) { |
| fuelgauge->info.pr_cnt = 1; |
| pr_info("%s: VCELL(%d), data(0x%04x)\n", |
| __func__, vcell, (data[1]<<8) | data[0]); |
| } |
| |
| if ((fuelgauge->sw_v_empty == VEMPTY_MODE) && vcell > 3550) { |
| fuelgauge->sw_v_empty = VEMPTY_RECOVERY_MODE; |
| max77854_fg_fuelalert_init(fuelgauge, |
| fuelgauge->pdata->fuel_alert_soc); |
| pr_info("%s : SW V EMPTY DISABLE\n", __func__); |
| } |
| |
| return vcell; |
| } |
| |
| static int max77854_fg_read_vfocv(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2]; |
| u32 vfocv = 0; |
| u16 w_data; |
| u32 temp; |
| u32 temp2; |
| |
| if (max77854_bulk_read(fuelgauge->i2c, VFOCV_REG, 2, data) < 0) { |
| pr_err("%s: Failed to read VFOCV\n", __func__); |
| return -1; |
| } |
| |
| w_data = (data[1]<<8) | data[0]; |
| |
| temp = (w_data & 0xFFF) * 78125; |
| vfocv = temp / 1000000; |
| |
| temp = ((w_data & 0xF000) >> 4) * 78125; |
| temp2 = temp / 1000000; |
| vfocv += (temp2 << 4); |
| |
| #if !defined(CONFIG_SEC_FACTORY) |
| max77854_fg_test_print(fuelgauge); |
| max77854_fg_periodic_read(fuelgauge); |
| #endif |
| |
| return vfocv; |
| } |
| |
| static int max77854_fg_read_avg_vcell(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2]; |
| u32 avg_vcell = 0; |
| u16 w_data; |
| u32 temp; |
| u32 temp2; |
| |
| if (max77854_bulk_read(fuelgauge->i2c, AVR_VCELL_REG, |
| 2, data) < 0) { |
| pr_err("%s: Failed to read AVG_VCELL\n", __func__); |
| return -1; |
| } |
| |
| w_data = (data[1]<<8) | data[0]; |
| |
| temp = (w_data & 0xFFF) * 78125; |
| avg_vcell = temp / 1000000; |
| |
| temp = ((w_data & 0xF000) >> 4) * 78125; |
| temp2 = temp / 1000000; |
| avg_vcell += (temp2 << 4); |
| |
| return avg_vcell; |
| } |
| |
| static int max77854_fg_check_battery_present(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| u8 status_data[2]; |
| int ret = 1; |
| |
| /* 1. Check Bst bit */ |
| if (max77854_bulk_read(fuelgauge->i2c, STATUS_REG, |
| 2, status_data) < 0) { |
| pr_err("%s: Failed to read STATUS_REG\n", __func__); |
| return 0; |
| } |
| |
| if (status_data[0] & (0x1 << 3)) { |
| pr_info("%s: addr(0x01), data(0x%04x)\n", __func__, |
| (status_data[1]<<8) | status_data[0]); |
| pr_info("%s: battery is absent!!\n", __func__); |
| ret = 0; |
| } |
| |
| return ret; |
| } |
| |
| static void max77854_fg_low_temp_compensation(struct max77854_fuelgauge_data *fuelgauge, bool en) |
| { |
| u16 data; |
| u8 valrt_data[2]; |
| |
| if (!fuelgauge->using_temp_compensation) |
| return; |
| |
| if (en) { |
| /* Reset VALRT Threshold setting (disable) */ |
| valrt_data[1] = 0xFF; |
| valrt_data[0] = 0xA0; /* 3.20 V */ |
| if (max77854_bulk_write(fuelgauge->i2c, VALRT_THRESHOLD_REG, |
| 2, valrt_data) < 0) { |
| pr_info("%s: Failed to write VALRT_THRESHOLD_REG\n", __func__); |
| return; |
| } |
| |
| data = max77854_read_word(fuelgauge->i2c, (u8)VALRT_THRESHOLD_REG); |
| pr_info("%s: VALRT_THRESHOLD_REG is (0x%x)\n", |
| __func__, data); |
| } else { |
| /* Reset VALRT Threshold setting (disable) */ |
| valrt_data[1] = 0xFF; |
| valrt_data[0] = 0xA3; /* 3.26 V */ |
| if (max77854_bulk_write(fuelgauge->i2c, VALRT_THRESHOLD_REG, |
| 2, valrt_data) < 0) { |
| pr_info("%s: Failed to write VALRT_THRESHOLD_REG\n", __func__); |
| return; |
| } |
| |
| data = max77854_read_word(fuelgauge->i2c, (u8)VALRT_THRESHOLD_REG); |
| pr_info("%s: VALRT_THRESHOLD_REG is (0x%x)\n", |
| __func__, data); |
| } |
| } |
| |
| static int max77854_fg_write_temp(struct max77854_fuelgauge_data *fuelgauge, |
| int temperature) |
| { |
| u8 data[2]; |
| |
| data[0] = (temperature%10) * 1000 / 39; |
| data[1] = temperature / 10; |
| max77854_bulk_write(fuelgauge->i2c, TEMPERATURE_REG, 2, data); |
| |
| pr_debug("%s: temperature to (%d, 0x%02x%02x)\n", |
| __func__, temperature, data[1], data[0]); |
| |
| return temperature; |
| } |
| |
| static int max77854_fg_read_temp(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2] = {0, 0}; |
| int temper = 0; |
| |
| if (max77854_fg_check_battery_present(fuelgauge)) { |
| if (max77854_bulk_read(fuelgauge->i2c, |
| TEMPERATURE_REG, 2, data) < 0) { |
| pr_err("%s: Failed to read TEMPERATURE_REG\n", |
| __func__); |
| return -1; |
| } |
| |
| if (data[1]&(0x1 << 7)) { |
| temper = ((~(data[1]))&0xFF)+1; |
| temper *= (-1000); |
| temper -= ((~((int)data[0]))+1) * 39 / 10; |
| } else { |
| temper = data[1] & 0x7f; |
| temper *= 1000; |
| temper += data[0] * 39 / 10; |
| } |
| } else |
| temper = 20000; |
| |
| if (!(fuelgauge->info.pr_cnt % PRINT_COUNT)) |
| pr_info("%s: TEMPERATURE(%d), data(0x%04x)\n", |
| __func__, temper, (data[1]<<8) | data[0]); |
| |
| return temper/100; |
| } |
| |
| /* soc should be 0.1% unit */ |
| static int max77854_fg_read_vfsoc(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2]; |
| int soc; |
| |
| if (max77854_bulk_read(fuelgauge->i2c, VFSOC_REG, |
| 2, data) < 0) { |
| pr_err("%s: Failed to read VFSOC\n", __func__); |
| return -1; |
| } |
| |
| soc = ((data[1] * 100) + (data[0] * 100 / 256)) / 10; |
| |
| return min(soc, 1000); |
| } |
| |
| /* soc should be 0.1% unit */ |
| static int max77854_fg_read_avsoc(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2]; |
| int soc; |
| |
| if (max77854_bulk_read(fuelgauge->i2c, SOCAV_REG, |
| 2, data) < 0) { |
| pr_err("%s: Failed to read AVSOC\n", __func__); |
| return -1; |
| } |
| |
| soc = ((data[1] * 100) + (data[0] * 100 / 256)) / 10; |
| |
| return min(soc, 1000); |
| } |
| |
| /* soc should be 0.1% unit */ |
| static int max77854_fg_read_soc(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2]; |
| int soc, vf_soc; |
| |
| if (max77854_bulk_read(fuelgauge->i2c, SOCREP_REG, 2, data) < 0) { |
| pr_err("%s: Failed to read SOCREP\n", __func__); |
| return -1; |
| } |
| |
| soc = ((data[1] * 100) + (data[0] * 100 / 256)) / 10; |
| vf_soc = max77854_fg_read_vfsoc(fuelgauge); |
| |
| #ifdef BATTERY_LOG_MESSAGE |
| pr_debug("%s: raw capacity (%d)\n", __func__, soc); |
| |
| if (!(fuelgauge->info.pr_cnt % PRINT_COUNT)) { |
| pr_debug("%s: raw capacity (%d), data(0x%04x)\n", |
| __func__, soc, (data[1]<<8) | data[0]); |
| pr_debug("%s: REPSOC (%d), VFSOC (%d), data(0x%04x)\n", |
| __func__, soc/10, vf_soc/10, (data[1]<<8) | data[0]); |
| } |
| #endif |
| |
| return min(soc, 1000); |
| } |
| |
| /* soc should be 0.01% unit */ |
| static int max77854_fg_read_rawsoc(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2]; |
| int soc; |
| |
| if (max77854_bulk_read(fuelgauge->i2c, SOCREP_REG, |
| 2, data) < 0) { |
| pr_err("%s: Failed to read SOCREP\n", __func__); |
| return -1; |
| } |
| |
| soc = (data[1] * 100) + (data[0] * 100 / 256); |
| |
| pr_debug("%s: raw capacity (0.01%%) (%d)\n", |
| __func__, soc); |
| |
| if (!(fuelgauge->info.pr_cnt % PRINT_COUNT)) |
| pr_debug("%s: raw capacity (%d), data(0x%04x)\n", |
| __func__, soc, (data[1]<<8) | data[0]); |
| |
| return min(soc, 10000); |
| } |
| |
| static int max77854_fg_read_fullcap(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2]; |
| int ret; |
| |
| if (max77854_bulk_read(fuelgauge->i2c, FULLCAP_REG, |
| 2, data) < 0) { |
| pr_err("%s: Failed to read FULLCAP\n", __func__); |
| return -1; |
| } |
| |
| ret = (data[1] << 8) + data[0]; |
| |
| return ret; |
| } |
| |
| static int max77854_fg_read_fullcaprep(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2]; |
| int ret; |
| |
| if (max77854_bulk_read(fuelgauge->i2c, FG_FULLCAPREP, |
| 2, data) < 0) { |
| pr_err("%s: Failed to read FULLCAP\n", __func__); |
| return -1; |
| } |
| |
| ret = (data[1] << 8) + data[0]; |
| |
| return ret; |
| } |
| |
| |
| static int max77854_fg_read_fullcapnom(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2]; |
| int ret; |
| |
| if (max77854_bulk_read(fuelgauge->i2c, FULLCAP_NOM_REG, |
| 2, data) < 0) { |
| pr_err("%s: Failed to read FULLCAP\n", __func__); |
| return -1; |
| } |
| |
| ret = (data[1] << 8) + data[0]; |
| |
| return ret; |
| } |
| |
| static int max77854_fg_read_mixcap(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2]; |
| int ret; |
| |
| if (max77854_bulk_read(fuelgauge->i2c, REMCAP_MIX_REG, |
| 2, data) < 0) { |
| pr_err("%s: Failed to read REMCAP_MIX_REG\n", |
| __func__); |
| return -1; |
| } |
| |
| ret = (data[1] << 8) + data[0]; |
| |
| return ret; |
| } |
| |
| static int max77854_fg_read_avcap(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2]; |
| int ret; |
| |
| if (max77854_bulk_read(fuelgauge->i2c, REMCAP_AV_REG, |
| 2, data) < 0) { |
| pr_err("%s: Failed to read REMCAP_AV_REG\n", |
| __func__); |
| return -1; |
| } |
| |
| ret = (data[1] << 8) + data[0]; |
| |
| return ret; |
| } |
| |
| static int max77854_fg_read_repcap(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2]; |
| int ret; |
| |
| if (max77854_bulk_read(fuelgauge->i2c, REMCAP_REP_REG, |
| 2, data) < 0) { |
| pr_err("%s: Failed to read REMCAP_REP_REG\n", |
| __func__); |
| return -1; |
| } |
| |
| ret = (data[1] << 8) + data[0]; |
| |
| return ret; |
| } |
| |
| static int max77854_fg_read_current(struct max77854_fuelgauge_data *fuelgauge, int unit) |
| { |
| u8 data1[2]; |
| u32 temp, sign; |
| s32 i_current; |
| |
| if (max77854_bulk_read(fuelgauge->i2c, CURRENT_REG, |
| 2, data1) < 0) { |
| pr_err("%s: Failed to read CURRENT\n", __func__); |
| return -1; |
| } |
| |
| temp = ((data1[1]<<8) | data1[0]) & 0xFFFF; |
| /* Debug log for abnormal current case */ |
| if (temp & (0x1 << 15)) { |
| sign = NEGATIVE; |
| temp = (~temp & 0xFFFF) + 1; |
| } else |
| sign = POSITIVE; |
| |
| /* 1.5625uV/0.01Ohm(Rsense) = 156.25uA */ |
| /* 1.5625uV/0.005Ohm(Rsense) = 312.5uA */ |
| switch (unit) { |
| case SEC_BATTERY_CURRENT_UA: |
| i_current = temp * 15625 * fuelgauge->fg_resistor / 100; |
| break; |
| case SEC_BATTERY_CURRENT_MA: |
| default: |
| i_current = temp * 15625 * fuelgauge->fg_resistor / 100000; |
| } |
| |
| if (sign) |
| i_current *= -1; |
| |
| pr_debug("%s: current=%d\n", __func__, i_current); |
| |
| return i_current; |
| } |
| |
| static int max77854_fg_read_avg_current(struct max77854_fuelgauge_data *fuelgauge, int unit) |
| { |
| u8 data2[2]; |
| u32 temp, sign; |
| s32 avg_current; |
| int vcell; |
| static int cnt; |
| |
| if (max77854_bulk_read(fuelgauge->i2c, AVG_CURRENT_REG, |
| 2, data2) < 0) { |
| pr_err("%s: Failed to read AVERAGE CURRENT\n", |
| __func__); |
| return -1; |
| } |
| |
| temp = ((data2[1]<<8) | data2[0]) & 0xFFFF; |
| if (temp & (0x1 << 15)) { |
| sign = NEGATIVE; |
| temp = (~temp & 0xFFFF) + 1; |
| } else |
| sign = POSITIVE; |
| |
| /* 1.5625uV/0.01Ohm(Rsense) = 156.25uA */ |
| /* 1.5625uV/0.005Ohm(Rsense) = 312.5uA */ |
| switch (unit) { |
| case SEC_BATTERY_CURRENT_UA: |
| avg_current = temp * 15625 * fuelgauge->fg_resistor / 100; |
| break; |
| case SEC_BATTERY_CURRENT_MA: |
| default: |
| avg_current = temp * 15625 * fuelgauge->fg_resistor / 100000; |
| } |
| |
| if (sign) |
| avg_current *= -1; |
| |
| vcell = max77854_fg_read_vcell(fuelgauge); |
| if ((vcell < 3500) && (cnt < 10) && (avg_current < 0) && |
| fuelgauge->is_charging) { |
| avg_current = 1; |
| cnt++; |
| } |
| |
| pr_debug("%s: avg_current=%d\n", __func__, avg_current); |
| |
| return avg_current; |
| } |
| |
| static int max77854_fg_read_cycle(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2]; |
| int ret; |
| |
| if (max77854_bulk_read(fuelgauge->i2c, CYCLES_REG, |
| 2, data) < 0) { |
| pr_err("%s: Failed to read FULLCAPCYCLE\n", __func__); |
| return -1; |
| } |
| |
| ret = (data[1] << 8) + data[0]; |
| |
| return ret; |
| } |
| |
| static bool max77854_check_jig_status(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| bool ret = false; |
| if(fuelgauge->pdata->jig_gpio) |
| ret = gpio_get_value(fuelgauge->pdata->jig_gpio); |
| |
| return ret; |
| } |
| |
| int max77854_fg_reset_soc(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2]; |
| int vfocv, fullcap; |
| |
| /* delay for current stablization */ |
| msleep(500); |
| |
| pr_info("%s: Before quick-start - VCELL(%d), VFOCV(%d), VfSOC(%d), RepSOC(%d)\n", |
| __func__, max77854_fg_read_vcell(fuelgauge), max77854_fg_read_vfocv(fuelgauge), |
| max77854_fg_read_vfsoc(fuelgauge), max77854_fg_read_soc(fuelgauge)); |
| pr_info("%s: Before quick-start - current(%d), avg current(%d)\n", |
| __func__, max77854_fg_read_current(fuelgauge, SEC_BATTERY_CURRENT_MA), |
| max77854_fg_read_avg_current(fuelgauge, SEC_BATTERY_CURRENT_MA)); |
| |
| if (!max77854_check_jig_status(fuelgauge)) { |
| pr_info("%s : Return by No JIG_ON signal\n", __func__); |
| return 0; |
| } |
| |
| max77854_write_word(fuelgauge->i2c, CYCLES_REG, 0); |
| |
| if (max77854_bulk_read(fuelgauge->i2c, MISCCFG_REG, |
| 2, data) < 0) { |
| pr_err("%s: Failed to read MiscCFG\n", __func__); |
| return -1; |
| } |
| |
| data[1] |= (0x1 << 2); |
| if (max77854_bulk_write(fuelgauge->i2c, MISCCFG_REG, |
| 2, data) < 0) { |
| pr_err("%s: Failed to write MiscCFG\n", __func__); |
| return -1; |
| } |
| |
| msleep(250); |
| max77854_write_word(fuelgauge->i2c, FULLCAP_REG, |
| fuelgauge->battery_data->Capacity); |
| msleep(500); |
| |
| pr_info("%s: After quick-start - VCELL(%d), VFOCV(%d), VfSOC(%d), RepSOC(%d)\n", |
| __func__, max77854_fg_read_vcell(fuelgauge), max77854_fg_read_vfocv(fuelgauge), |
| max77854_fg_read_vfsoc(fuelgauge), max77854_fg_read_soc(fuelgauge)); |
| pr_info("%s: After quick-start - current(%d), avg current(%d)\n", |
| __func__, max77854_fg_read_current(fuelgauge, SEC_BATTERY_CURRENT_MA), |
| max77854_fg_read_avg_current(fuelgauge, SEC_BATTERY_CURRENT_MA)); |
| |
| max77854_write_word(fuelgauge->i2c, CYCLES_REG, 0x00a0); |
| |
| /* P8 is not turned off by Quickstart @3.4V |
| * (It's not a problem, depend on mode data) |
| * Power off for factory test(File system, etc..) */ |
| vfocv = max77854_fg_read_vfocv(fuelgauge); |
| if (vfocv < POWER_OFF_VOLTAGE_LOW_MARGIN) { |
| pr_info("%s: Power off condition(%d)\n", __func__, vfocv); |
| |
| fullcap = max77854_read_word(fuelgauge->i2c, FULLCAP_REG); |
| |
| /* FullCAP * 0.009 */ |
| max77854_write_word(fuelgauge->i2c, REMCAP_REP_REG, |
| (u16)(fullcap * 9 / 1000)); |
| msleep(200); |
| pr_info("%s: new soc=%d, vfocv=%d\n", __func__, |
| max77854_fg_read_soc(fuelgauge), vfocv); |
| } |
| |
| pr_info("%s: Additional step - VfOCV(%d), VfSOC(%d), RepSOC(%d)\n", |
| __func__, max77854_fg_read_vfocv(fuelgauge), |
| max77854_fg_read_vfsoc(fuelgauge), max77854_fg_read_soc(fuelgauge)); |
| |
| return 0; |
| } |
| |
| int max77854_fg_reset_capacity_by_jig_connection(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| union power_supply_propval val; |
| val.intval = 1; |
| psy_do_property("max77854-charger", set, |
| POWER_SUPPLY_PROP_ENERGY_NOW, val); |
| pr_info("%s: DesignCap = Capacity - 1 (Jig Connection)\n", __func__); |
| |
| return max77854_write_word(fuelgauge->i2c, DESIGNCAP_REG, |
| fuelgauge->battery_data->Capacity-1); |
| } |
| |
| static int max77854_fg_check_status_reg(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| u8 status_data[2]; |
| int ret = 0; |
| |
| /* 1. Check Smn was generatedread */ |
| if (max77854_bulk_read(fuelgauge->i2c, STATUS_REG, |
| 2, status_data) < 0) { |
| pr_err("%s: Failed to read STATUS_REG\n", __func__); |
| return -1; |
| } |
| |
| #ifdef BATTERY_LOG_MESSAGE |
| pr_info("%s: addr(0x00), data(0x%04x)\n", __func__, |
| (status_data[1]<<8) | status_data[0]); |
| #endif |
| |
| if (status_data[1] & (0x1 << 2)) |
| ret = 1; |
| |
| /* 2. clear Status reg */ |
| status_data[1] = 0; |
| if (max77854_bulk_write(fuelgauge->i2c, STATUS_REG, |
| 2, status_data) < 0) { |
| pr_info("%s: Failed to write STATUS_REG\n", __func__); |
| return -1; |
| } |
| |
| return ret; |
| } |
| |
| int max77854_get_fuelgauge_value(struct max77854_fuelgauge_data *fuelgauge, int data) |
| { |
| int ret; |
| |
| switch (data) { |
| case FG_LEVEL: |
| ret = max77854_fg_read_soc(fuelgauge); |
| break; |
| |
| case FG_TEMPERATURE: |
| ret = max77854_fg_read_temp(fuelgauge); |
| break; |
| |
| case FG_VOLTAGE: |
| ret = max77854_fg_read_vcell(fuelgauge); |
| break; |
| |
| case FG_CURRENT: |
| ret = max77854_fg_read_current(fuelgauge, SEC_BATTERY_CURRENT_MA); |
| break; |
| |
| case FG_CURRENT_AVG: |
| ret = max77854_fg_read_avg_current(fuelgauge, SEC_BATTERY_CURRENT_MA); |
| break; |
| |
| case FG_CHECK_STATUS: |
| ret = max77854_fg_check_status_reg(fuelgauge); |
| break; |
| |
| case FG_RAW_SOC: |
| ret = max77854_fg_read_rawsoc(fuelgauge); |
| break; |
| |
| case FG_VF_SOC: |
| ret = max77854_fg_read_vfsoc(fuelgauge); |
| break; |
| |
| case FG_AV_SOC: |
| ret = max77854_fg_read_avsoc(fuelgauge); |
| break; |
| |
| case FG_FULLCAP: |
| ret = max77854_fg_read_fullcap(fuelgauge); |
| break; |
| |
| case FG_FULLCAPNOM: |
| ret = max77854_fg_read_fullcapnom(fuelgauge); |
| break; |
| |
| case FG_FULLCAPREP: |
| ret = max77854_fg_read_fullcaprep(fuelgauge); |
| break; |
| |
| case FG_MIXCAP: |
| ret = max77854_fg_read_mixcap(fuelgauge); |
| break; |
| |
| case FG_AVCAP: |
| ret = max77854_fg_read_avcap(fuelgauge); |
| break; |
| |
| case FG_REPCAP: |
| ret = max77854_fg_read_repcap(fuelgauge); |
| break; |
| |
| case FG_CYCLE: |
| ret = max77854_fg_read_cycle(fuelgauge); |
| break; |
| |
| default: |
| ret = -1; |
| break; |
| } |
| |
| return ret; |
| } |
| |
| int max77854_fg_alert_init(struct max77854_fuelgauge_data *fuelgauge, int soc) |
| { |
| u8 misccgf_data[2]; |
| u8 salrt_data[2]; |
| u8 config_data[2]; |
| u8 valrt_data[2]; |
| u8 talrt_data[2]; |
| u16 read_data = 0; |
| |
| fuelgauge->is_fuel_alerted = false; |
| |
| /* Using RepSOC */ |
| if (max77854_bulk_read(fuelgauge->i2c, MISCCFG_REG, 2, |
| misccgf_data) < 0) { |
| pr_err("%s: Failed to read MISCCFG_REG\n", __func__); |
| return -1; |
| } |
| misccgf_data[0] = misccgf_data[0] & ~(0x03); |
| |
| if (max77854_bulk_write(fuelgauge->i2c, MISCCFG_REG, |
| 2, misccgf_data) < 0) { |
| pr_info("%s: Failed to write MISCCFG_REG\n", __func__); |
| return -1; |
| } |
| |
| /* SALRT Threshold setting */ |
| salrt_data[1] = 0xff; |
| salrt_data[0] = soc; |
| if (max77854_bulk_write(fuelgauge->i2c, SALRT_THRESHOLD_REG, |
| 2, salrt_data) < 0) { |
| pr_info("%s: Failed to write SALRT_THRESHOLD_REG\n", __func__); |
| return -1; |
| } |
| |
| /* Reset VALRT Threshold setting (disable) */ |
| valrt_data[1] = 0xFF; |
| valrt_data[0] = 0xA5; |
| if (max77854_bulk_write(fuelgauge->i2c, VALRT_THRESHOLD_REG, |
| 2, valrt_data) < 0) { |
| pr_info("%s: Failed to write VALRT_THRESHOLD_REG\n", __func__); |
| return -1; |
| } |
| |
| read_data = max77854_read_word(fuelgauge->i2c, (u8)VALRT_THRESHOLD_REG); |
| if (read_data != 0xFFA5) |
| pr_err("%s: VALRT_THRESHOLD_REG is not valid (0x%x)\n", |
| __func__, read_data); |
| |
| /* Reset TALRT Threshold setting (disable) */ |
| talrt_data[1] = 0x7F; |
| talrt_data[0] = 0x80; |
| if (max77854_bulk_write(fuelgauge->i2c, TALRT_THRESHOLD_REG, |
| 2, talrt_data) < 0) { |
| pr_info("%s: Failed to write TALRT_THRESHOLD_REG\n", __func__); |
| return -1; |
| } |
| |
| read_data = max77854_read_word(fuelgauge->i2c, (u8)TALRT_THRESHOLD_REG); |
| if (read_data != 0x7f80) |
| pr_err("%s: TALRT_THRESHOLD_REG is not valid (0x%x)\n", |
| __func__, read_data); |
| |
| /*mdelay(100);*/ |
| |
| /* Enable SOC alerts */ |
| if (max77854_bulk_read(fuelgauge->i2c, CONFIG_REG, |
| 2, config_data) < 0) { |
| pr_err("%s: Failed to read CONFIG_REG\n", __func__); |
| return -1; |
| } |
| config_data[0] = config_data[0] | (0x1 << 2); |
| |
| if (max77854_bulk_write(fuelgauge->i2c, CONFIG_REG, |
| 2, config_data) < 0) { |
| pr_info("%s: Failed to write CONFIG_REG\n", __func__); |
| return -1; |
| } |
| |
| max77854_update_reg(fuelgauge->pmic, |
| MAX77854_PMIC_REG_INTSRC_MASK, |
| ~MAX77854_IRQSRC_FG, |
| MAX77854_IRQSRC_FG); |
| |
| pr_info("[%s] SALRT(0x%02x%02x), VALRT(0x%02x%02x), CONFIG(0x%02x%02x)\n", |
| __func__, |
| salrt_data[1], salrt_data[0], |
| valrt_data[1], valrt_data[0], |
| config_data[1], config_data[0]); |
| |
| return 1; |
| } |
| |
| static int max77854_get_fuelgauge_soc(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| union power_supply_propval value; |
| int fg_soc = 0; |
| int fg_vfsoc; |
| int fg_vcell; |
| int fg_current; |
| int avg_current; |
| |
| fg_soc = max77854_get_fuelgauge_value(fuelgauge, FG_LEVEL); |
| if (fg_soc < 0) { |
| pr_info("Can't read soc!!!"); |
| fg_soc = fuelgauge->info.soc; |
| } |
| |
| fg_vcell = max77854_get_fuelgauge_value(fuelgauge, FG_VOLTAGE); |
| fg_current = max77854_get_fuelgauge_value(fuelgauge, FG_CURRENT); |
| avg_current = max77854_get_fuelgauge_value(fuelgauge, FG_CURRENT_AVG); |
| fg_vfsoc = max77854_get_fuelgauge_value(fuelgauge, FG_VF_SOC); |
| |
| psy_do_property("battery", get, |
| POWER_SUPPLY_PROP_STATUS, value); |
| |
| if (fuelgauge->info.is_first_check) |
| fuelgauge->info.is_first_check = false; |
| |
| if ((fg_vcell < 3400) && (avg_current < 0) && (fg_soc <= 10)) |
| fg_soc = 0; |
| |
| fuelgauge->info.soc = fg_soc; |
| |
| pr_debug("%s: soc(%d)\n", |
| __func__, fuelgauge->info.soc); |
| |
| return fg_soc; |
| } |
| |
| static irqreturn_t max77854_jig_irq_thread(int irq, void *irq_data) |
| { |
| struct max77854_fuelgauge_data *fuelgauge = irq_data; |
| |
| if (max77854_check_jig_status(fuelgauge)) |
| max77854_fg_reset_capacity_by_jig_connection(fuelgauge); |
| else |
| pr_info("%s: jig removed\n", __func__); |
| return IRQ_HANDLED; |
| } |
| |
| bool max77854_fg_init(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| ktime_t current_time; |
| struct timespec ts; |
| u8 data[2] = {0, 0}; |
| u32 volt_threshold = 0; |
| u32 temp_threshold = 0; |
| |
| #if defined(ANDROID_ALARM_ACTIVATED) |
| current_time = alarm_get_elapsed_realtime(); |
| ts = ktime_to_timespec(current_time); |
| #else |
| current_time = ktime_get_boottime(); |
| ts = ktime_to_timespec(current_time); |
| #endif |
| |
| fuelgauge->info.fullcap_check_interval = ts.tv_sec; |
| |
| fuelgauge->info.is_first_check = true; |
| |
| /* Init parameters to prevent wrong compensation. */ |
| fuelgauge->info.previous_fullcap = |
| max77854_read_word(fuelgauge->i2c, FULLCAP_REG); |
| fuelgauge->info.previous_vffullcap = |
| max77854_read_word(fuelgauge->i2c, FULLCAP_NOM_REG); |
| |
| if (fuelgauge->pdata->jig_gpio) { |
| int ret; |
| ret = request_threaded_irq(fuelgauge->pdata->jig_irq, |
| NULL, max77854_jig_irq_thread, |
| IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
| "jig-irq", fuelgauge); |
| if (ret) { |
| pr_info("%s: Failed to Request IRQ\n", |
| __func__); |
| } |
| pr_info("%s: jig_result : %d\n", __func__, max77854_check_jig_status(fuelgauge)); |
| |
| /* initial check for the JIG */ |
| if (max77854_check_jig_status(fuelgauge)) |
| max77854_fg_reset_capacity_by_jig_connection(fuelgauge); |
| } |
| |
| /* NOT using FG for temperature */ |
| if (fuelgauge->pdata->thermal_source != SEC_BATTERY_THERMAL_SOURCE_FG) { |
| if (max77854_bulk_read(fuelgauge->i2c, CONFIG_REG, |
| 2, data) < 0) { |
| pr_err ("%s : Failed to read CONFIG_REG\n", __func__); |
| return false; |
| } |
| data[1] |= 0x1; |
| |
| if (max77854_bulk_write(fuelgauge->i2c, CONFIG_REG, |
| 2, data) < 0) { |
| pr_info("%s : Failed to write CONFIG_REG\n", __func__); |
| return false; |
| } |
| } else { |
| if (max77854_bulk_read(fuelgauge->i2c, CONFIG_REG, |
| 2, data) < 0) { |
| pr_err ("%s : Failed to read CONFIG_REG\n", __func__); |
| return false; |
| } |
| data[1] &= 0xFE; |
| data[0] |= 0x10; |
| |
| if (max77854_bulk_write(fuelgauge->i2c, CONFIG_REG, |
| 2, data) < 0) { |
| pr_info("%s : Failed to write CONFIG_REG\n", __func__); |
| return false; |
| } |
| } |
| |
| if (fuelgauge->auto_discharge_en) { |
| /* Auto discharge EN & Alert Enable */ |
| max77854_bulk_read(fuelgauge->i2c, CONFIG2_REG, 2, data); |
| data[1] |= 0x2; |
| max77854_bulk_write(fuelgauge->i2c, CONFIG2_REG, 2, data); |
| |
| /* Set Auto Discharge temperature & Voltage threshold */ |
| volt_threshold = |
| fuelgauge->discharge_volt_threshold < 3900 ? 0x0 : |
| fuelgauge->discharge_volt_threshold > 4540 ? 0x20 : |
| (fuelgauge->discharge_volt_threshold - 3900) / 20; |
| |
| temp_threshold = |
| fuelgauge->discharge_temp_threshold < 470 ? 0x0 : |
| fuelgauge->discharge_temp_threshold > 630 ? 0x20 : |
| (fuelgauge->discharge_temp_threshold - 470) / 5; |
| |
| max77854_bulk_read(fuelgauge->i2c, DISCHARGE_THRESHOLD_REG, 2, data); |
| data[1] &= ~0xF8; |
| data[1] |= volt_threshold << 3; |
| |
| data[0] &= ~0xF8; |
| data[0] |= temp_threshold << 3; |
| |
| max77854_bulk_write(fuelgauge->i2c, DISCHARGE_THRESHOLD_REG, 2, data); |
| |
| pr_info("%s: DISCHARGE_THRESHOLD Value : 0x%x\n", |
| __func__, (data[1]<<8) | data[0]); |
| } |
| |
| return true; |
| } |
| |
| bool max77854_fg_fuelalert_init(struct max77854_fuelgauge_data *fuelgauge, |
| int soc) |
| { |
| /* 1. Set max77854 alert configuration. */ |
| if (max77854_fg_alert_init(fuelgauge, soc) > 0) |
| return true; |
| else |
| return false; |
| } |
| |
| void max77854_fg_fuelalert_set(struct max77854_fuelgauge_data *fuelgauge, |
| int enable) |
| { |
| u8 config_data[2]; |
| u8 status_data[2]; |
| |
| if (max77854_bulk_read(fuelgauge->i2c, CONFIG_REG, |
| 2, config_data) < 0) |
| pr_err("%s: Failed to read CONFIG_REG\n", __func__); |
| |
| if (enable) |
| config_data[0] |= ALERT_EN; |
| else |
| config_data[0] &= ~ALERT_EN; |
| |
| pr_info("%s : CONFIG(0x%02x%02x)\n", __func__, config_data[1], config_data[0]); |
| |
| if (max77854_bulk_write(fuelgauge->i2c, CONFIG_REG, |
| 2, config_data) < 0) |
| pr_info("%s: Failed to write CONFIG_REG\n", __func__); |
| |
| if (max77854_bulk_read(fuelgauge->i2c, STATUS_REG, |
| 2, status_data) < 0) |
| pr_err("%s : Failed to read STATUS_REG\n", __func__); |
| |
| if (status_data[1] & 0x01) { |
| pr_info("%s : Battery Voltage is Very Low!! SW V EMPTY ENABLE\n", __func__); |
| fuelgauge->sw_v_empty = VEMPTY_MODE; |
| } |
| } |
| |
| |
| bool max77854_fg_fuelalert_process(void *irq_data) |
| { |
| struct max77854_fuelgauge_data *fuelgauge = |
| (struct max77854_fuelgauge_data *)irq_data; |
| |
| max77854_fg_fuelalert_set(fuelgauge, 0); |
| |
| return true; |
| } |
| |
| bool max77854_fg_reset(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| if (!max77854_fg_reset_soc(fuelgauge)) |
| return true; |
| else |
| return false; |
| } |
| |
| static int max77854_fg_check_capacity_max( |
| struct max77854_fuelgauge_data *fuelgauge, int capacity_max) |
| { |
| int cap_max, cap_min; |
| |
| cap_max = (fuelgauge->pdata->capacity_max + |
| fuelgauge->pdata->capacity_max_margin) * 100 / 101; |
| cap_min = (fuelgauge->pdata->capacity_max - |
| fuelgauge->pdata->capacity_max_margin) * 100 / 101; |
| |
| return (capacity_max < cap_min) ? cap_min : |
| ((capacity_max > cap_max) ? cap_max : capacity_max); |
| } |
| |
| #define CAPACITY_MAX_CONTROL_THRESHOLD 300 |
| static void max77854_fg_get_scaled_capacity( |
| struct max77854_fuelgauge_data *fuelgauge, |
| union power_supply_propval *val) |
| { |
| #if defined(CONFIG_DISABLE_SAVE_CAPACITY_MAX) |
| u16 reg_data; |
| #endif |
| union power_supply_propval cable_val, chg_val, chg_val2; |
| #if defined(CONFIG_BATTERY_SWELLING) |
| union power_supply_propval swelling_val; |
| #endif |
| int current_standard; |
| |
| psy_do_property("battery", get, POWER_SUPPLY_PROP_ONLINE, cable_val); |
| #if defined(CONFIG_BATTERY_SWELLING) |
| /* Check whether DUT is in the swelling mode or not */ |
| psy_do_property("battery", get, POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, swelling_val); |
| #endif |
| |
| chg_val.intval = cable_val.intval; |
| psy_do_property("max77854-charger", get, POWER_SUPPLY_PROP_CURRENT_AVG, |
| chg_val); |
| psy_do_property("max77854-charger", get, POWER_SUPPLY_PROP_CHARGE_NOW, |
| chg_val2); |
| |
| if (fuelgauge->cable_type == POWER_SUPPLY_TYPE_HV_MAINS || |
| fuelgauge->cable_type == POWER_SUPPLY_TYPE_HV_ERR || |
| fuelgauge->cable_type == POWER_SUPPLY_TYPE_HV_WIRELESS || |
| fuelgauge->cable_type == POWER_SUPPLY_TYPE_WIRELESS_HV_STAND) |
| current_standard = CAPACITY_SCALE_HV_CURRENT; |
| else |
| current_standard = CAPACITY_SCALE_DEFAULT_CURRENT; |
| |
| pr_info("%s : current_standard(%d)\n", __func__, current_standard); |
| |
| if ((cable_val.intval != POWER_SUPPLY_TYPE_BATTERY) && |
| #if defined(CONFIG_BATTERY_SWELLING) |
| (!swelling_val.intval) && |
| #endif |
| (!strcmp(chg_val2.strval, "CV Mode")) && |
| (chg_val.intval >= current_standard)) { |
| int max_temp; |
| int temp, sample; |
| int curr; |
| int topoff; |
| int capacity_threshold; |
| static int cnt; |
| |
| max_temp = fuelgauge->capacity_max; |
| curr = max77854_get_fuelgauge_value(fuelgauge, FG_CURRENT_AVG); |
| topoff = fuelgauge->pdata->charging_current[POWER_SUPPLY_TYPE_MAINS].full_check_current_1st; |
| capacity_threshold = topoff + CAPACITY_MAX_CONTROL_THRESHOLD; |
| |
| pr_info("%s : curr(%d) topoff(%d) capacity_max(%d)\n", __func__, curr, topoff, max_temp); |
| |
| if ((curr < capacity_threshold) && (curr > topoff)) { |
| if (!cnt) { |
| cnt = 1; |
| fuelgauge->standard_capacity = (val->intval < fuelgauge->pdata->capacity_min) ? |
| 0 : ((val->intval - fuelgauge->pdata->capacity_min) * 999 / |
| (fuelgauge->capacity_max - fuelgauge->pdata->capacity_min)); |
| } else if (fuelgauge->standard_capacity < 999) { |
| temp = (val->intval < fuelgauge->pdata->capacity_min) ? |
| 0 : ((val->intval - fuelgauge->pdata->capacity_min) * 999 / |
| (fuelgauge->capacity_max - fuelgauge->pdata->capacity_min)); |
| |
| sample = ((capacity_threshold - curr) * (999 - fuelgauge->standard_capacity)) / |
| (capacity_threshold - topoff); |
| |
| pr_info("%s : %d = ((%d - %d) * (999 - %d)) / (%d - %d)\n", |
| __func__, |
| sample, capacity_threshold, curr, fuelgauge->standard_capacity, |
| capacity_threshold, topoff); |
| |
| if ((temp - fuelgauge->standard_capacity) >= sample) { |
| pr_info("%s : TEMP > SAMPLE\n", __func__); |
| } else if ((sample - (temp - fuelgauge->standard_capacity)) < 5) { |
| pr_info("%s : TEMP < SAMPLE && GAP UNDER 5\n", __func__); |
| max_temp -= (sample - (temp - fuelgauge->standard_capacity)); |
| } else { |
| pr_info("%s : TEMP > SAMPLE && GAP OVER 5\n", __func__); |
| max_temp -= 5; |
| } |
| pr_info("%s : TEMP(%d) SAMPLE(%d) CAPACITY_MAX(%d)\n", |
| __func__, temp, sample, fuelgauge->capacity_max); |
| |
| fuelgauge->capacity_max = |
| max77854_fg_check_capacity_max(fuelgauge, max_temp); |
| } |
| } else { |
| cnt = 0; |
| } |
| } |
| |
| val->intval = (val->intval < fuelgauge->pdata->capacity_min) ? |
| 0 : ((val->intval - fuelgauge->pdata->capacity_min) * 1000 / |
| (fuelgauge->capacity_max - fuelgauge->pdata->capacity_min)); |
| |
| #if defined(CONFIG_DISABLE_SAVE_CAPACITY_MAX) |
| reg_data = max77854_read_word(fuelgauge->i2c, 0xD0); |
| if (reg_data != fuelgauge->capacity_max) { |
| pr_info("%s : 0xD0 Register Update (%d) -> (%d)\n", |
| __func__, reg_data, fuelgauge->capacity_max); |
| reg_data = fuelgauge->capacity_max; |
| max77854_write_word(fuelgauge->i2c, 0xD0, reg_data); |
| } |
| #endif |
| |
| pr_info("%s : CABLE TYPE(%d) INPUT CURRENT(%d) CHARGING MODE(%s)" \ |
| "capacity_max (%d) scaled capacity(%d.%d)\n", |
| __func__, cable_val.intval, chg_val.intval, chg_val2.strval, |
| fuelgauge->capacity_max, val->intval/10, val->intval%10); |
| } |
| |
| /* capacity is integer */ |
| static void max77854_fg_get_atomic_capacity( |
| struct max77854_fuelgauge_data *fuelgauge, |
| union power_supply_propval *val) |
| { |
| |
| pr_debug("%s : NOW(%d), OLD(%d)\n", |
| __func__, val->intval, fuelgauge->capacity_old); |
| |
| if (fuelgauge->pdata->capacity_calculation_type & |
| SEC_FUELGAUGE_CAPACITY_TYPE_ATOMIC) { |
| if (fuelgauge->capacity_old < val->intval) |
| val->intval = fuelgauge->capacity_old + 1; |
| else if (fuelgauge->capacity_old > val->intval) |
| val->intval = fuelgauge->capacity_old - 1; |
| } |
| |
| /* keep SOC stable in abnormal status */ |
| if (fuelgauge->pdata->capacity_calculation_type & |
| SEC_FUELGAUGE_CAPACITY_TYPE_SKIP_ABNORMAL) { |
| if (!fuelgauge->is_charging && |
| fuelgauge->capacity_old < val->intval) { |
| pr_err("%s: capacity (old %d : new %d)\n", |
| __func__, fuelgauge->capacity_old, val->intval); |
| val->intval = fuelgauge->capacity_old; |
| } |
| } |
| |
| /* updated old capacity */ |
| fuelgauge->capacity_old = val->intval; |
| } |
| |
| static int max77854_fg_calculate_dynamic_scale( |
| struct max77854_fuelgauge_data *fuelgauge, int capacity) |
| { |
| union power_supply_propval raw_soc_val; |
| raw_soc_val.intval = max77854_get_fuelgauge_value(fuelgauge, |
| FG_RAW_SOC) / 10; |
| |
| if (raw_soc_val.intval < |
| fuelgauge->pdata->capacity_max - |
| fuelgauge->pdata->capacity_max_margin) { |
| pr_info("%s: raw soc(%d) is very low, skip routine\n", |
| __func__, raw_soc_val.intval); |
| return fuelgauge->capacity_max; |
| } else { |
| fuelgauge->capacity_max = |
| (raw_soc_val.intval > |
| fuelgauge->pdata->capacity_max + |
| fuelgauge->pdata->capacity_max_margin) ? |
| (fuelgauge->pdata->capacity_max + |
| fuelgauge->pdata->capacity_max_margin) : |
| raw_soc_val.intval; |
| pr_debug("%s: raw soc (%d)", __func__, |
| fuelgauge->capacity_max); |
| } |
| |
| fuelgauge->capacity_max = |
| (fuelgauge->capacity_max * 100 / (capacity + 1)); |
| fuelgauge->capacity_old = capacity; |
| |
| pr_info("%s: %d is used for capacity_max, capacity(%d)\n", |
| __func__, fuelgauge->capacity_max, capacity); |
| |
| return fuelgauge->capacity_max; |
| } |
| |
| static void max77854_fg_check_qrtable(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| u16 data; |
| |
| data = max77854_read_word(fuelgauge->i2c, QRTABLE20_REG); |
| if (data != fuelgauge->battery_data->QResidual20) { |
| if (max77854_write_word(fuelgauge->i2c, QRTABLE20_REG, |
| fuelgauge->battery_data->QResidual20) < 0) |
| pr_err("%s: Failed to write QRTABLE20\n", __func__); |
| } |
| |
| data = max77854_read_word(fuelgauge->i2c, QRTABLE30_REG); |
| if (data != fuelgauge->battery_data->QResidual30) { |
| if (max77854_write_word(fuelgauge->i2c, QRTABLE30_REG, |
| fuelgauge->battery_data->QResidual30) <0) |
| pr_err("%s: Failed to write QRTABLE30\n", __func__); |
| } |
| |
| pr_debug("%s: QRTABLE20_REG(0x%04x), QRTABLE30_REG(0x%04x)\n", __func__, |
| fuelgauge->battery_data->QResidual20, |
| fuelgauge->battery_data->QResidual30); |
| } |
| |
| #if defined(CONFIG_EN_OOPS) |
| static void max77854_set_full_value(struct max77854_fuelgauge_data *fuelgauge, |
| int cable_type) |
| { |
| u16 ichgterm, misccfg, fullsocthr; |
| |
| if ((cable_type == POWER_SUPPLY_TYPE_HV_MAINS) || |
| (cable_type == POWER_SUPPLY_TYPE_HV_ERR)) { |
| ichgterm = fuelgauge->battery_data->ichgterm_2nd; |
| misccfg = fuelgauge->battery_data->misccfg_2nd; |
| fullsocthr = fuelgauge->battery_data->fullsocthr_2nd; |
| } else { |
| ichgterm = fuelgauge->battery_data->ichgterm; |
| misccfg = fuelgauge->battery_data->misccfg; |
| fullsocthr = fuelgauge->battery_data->fullsocthr; |
| } |
| |
| max77854_write_word(fuelgauge->i2c, ICHGTERM_REG, ichgterm); |
| max77854_write_word(fuelgauge->i2c, MISCCFG_REG, misccfg); |
| max77854_write_word(fuelgauge->i2c, FULLSOCTHR_REG, fullsocthr); |
| |
| pr_info("%s : ICHGTERM(0x%04x) FULLSOCTHR(0x%04x), MISCCFG(0x%04x)\n", |
| __func__, ichgterm, misccfg, fullsocthr); |
| } |
| #endif |
| |
| static int calc_ttf(struct max77854_fuelgauge_data *fuelgauge, union power_supply_propval *val) |
| { |
| union power_supply_propval chg_val2; |
| int i; |
| int cc_time = 0; |
| |
| int soc = fuelgauge->raw_capacity; |
| int current_now = fuelgauge->current_now; |
| int current_avg = fuelgauge->current_avg; |
| int charge_current = (current_avg > 0)? current_avg : current_now; |
| struct cv_slope *cv_data = fuelgauge->cv_data; |
| int design_cap = fuelgauge->battery_data->Capacity / 2; |
| |
| if(!cv_data || (val->intval <= 0)) { |
| pr_info("%s: no cv_data or val: %d\n", __func__, val->intval); |
| return -1; |
| } |
| /* To prevent overflow if charge current is 30 under, change value*/ |
| if (charge_current <= 30) { |
| charge_current = val->intval; |
| } |
| psy_do_property("max77854-charger", get, POWER_SUPPLY_PROP_CHARGE_NOW, |
| chg_val2); |
| if (!strcmp(chg_val2.strval, "CC Mode") || !strcmp(chg_val2.strval, "NONE")) { //CC mode || NONE |
| charge_current = val->intval; |
| } |
| for (i = 0; i < fuelgauge->cv_data_lenth ;i++) { |
| if (charge_current >= cv_data[i].fg_current) |
| break; |
| } |
| if (cv_data[i].soc < soc || !strcmp(chg_val2.strval, "EOC")) { |
| for (i = 0; i < fuelgauge->cv_data_lenth; i++) { |
| if (soc <= cv_data[i].soc) |
| break; |
| } |
| } else if (!strcmp(chg_val2.strval, "CC Mode") || !strcmp(chg_val2.strval, "NONE")) { //CC mode || NONE |
| cc_time = design_cap * (cv_data[i].soc - soc)\ |
| / val->intval * 3600 / 1000; |
| pr_debug("%s: cc_time: %d\n", __func__, cc_time); |
| if (cc_time < 0) { |
| |
| cc_time = 0; |
| } |
| } |
| |
| pr_debug("%s: cap: %d, soc: %4d, T: %6d, now: %4d, avg: %4d, cv soc: %4d, i: %4d, val: %d, %s\n", |
| __func__, design_cap, soc, cv_data[i].time + cc_time, current_now, current_avg, cv_data[i].soc, i, val->intval, chg_val2.strval); |
| |
| if (cv_data[i].time + cc_time >= 60) |
| return cv_data[i].time + cc_time; |
| else |
| return 60; //minimum 1minutes |
| } |
| |
| static void max77854_fg_set_vempty(struct max77854_fuelgauge_data *fuelgauge, bool en) |
| { |
| if (en) { |
| pr_info("%s : Low Capacity HW V EMPTY Enable\n", __func__); |
| max77854_write_word(fuelgauge->i2c, VEMPTY_REG, fuelgauge->battery_data->V_empty); |
| fuelgauge->sw_v_empty = NORMAL_MODE; |
| fuelgauge->hw_v_empty = true; |
| } else { |
| max77854_write_word(fuelgauge->i2c, VEMPTY_REG, fuelgauge->battery_data->V_empty_origin); |
| fuelgauge->hw_v_empty = false; |
| } |
| } |
| |
| static int max77854_fg_get_property(struct power_supply *psy, |
| enum power_supply_property psp, |
| union power_supply_propval *val) |
| { |
| struct max77854_fuelgauge_data *fuelgauge = |
| container_of(psy, struct max77854_fuelgauge_data, psy_fg); |
| static int abnormal_current_cnt = 0; |
| union power_supply_propval value; |
| u8 data[2] = {0, 0}; |
| |
| switch (psp) { |
| /* Cell voltage (VCELL, mV) */ |
| case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| val->intval = max77854_get_fuelgauge_value(fuelgauge, FG_VOLTAGE); |
| break; |
| /* Additional Voltage Information (mV) */ |
| case POWER_SUPPLY_PROP_VOLTAGE_AVG: |
| switch (val->intval) { |
| case SEC_BATTERY_VOLTAGE_OCV: |
| val->intval = max77854_fg_read_vfocv(fuelgauge); |
| break; |
| case SEC_BATTERY_VOLTAGE_AVERAGE: |
| default: |
| val->intval = max77854_fg_read_avg_vcell(fuelgauge); |
| break; |
| } |
| break; |
| /* Current */ |
| case POWER_SUPPLY_PROP_CURRENT_NOW: |
| switch (val->intval) { |
| case SEC_BATTERY_CURRENT_UA: |
| val->intval = |
| max77854_fg_read_current(fuelgauge, |
| SEC_BATTERY_CURRENT_UA); |
| break; |
| case SEC_BATTERY_CURRENT_MA: |
| default: |
| fuelgauge->current_now = val->intval = max77854_get_fuelgauge_value(fuelgauge, |
| FG_CURRENT); |
| psy_do_property("battery", get, |
| POWER_SUPPLY_PROP_STATUS, value); |
| /* To save log for abnormal case */ |
| if (value.intval == POWER_SUPPLY_STATUS_DISCHARGING && val->intval > 0) { |
| abnormal_current_cnt++; |
| if (abnormal_current_cnt >= 5) { |
| pr_info("%s : Inow is increasing in not charging status\n", |
| __func__); |
| value.intval = fuelgauge->capacity_old + 15; |
| psy_do_property("battery", set, |
| POWER_SUPPLY_PROP_CAPACITY, value); |
| abnormal_current_cnt = 0; |
| value.intval = fuelgauge->capacity_old; |
| psy_do_property("battery", set, |
| POWER_SUPPLY_PROP_CAPACITY, value); |
| } |
| } else { |
| abnormal_current_cnt = 0; |
| } |
| break; |
| } |
| break; |
| /* Average Current */ |
| case POWER_SUPPLY_PROP_CURRENT_AVG: |
| switch (val->intval) { |
| case SEC_BATTERY_CURRENT_UA: |
| val->intval = |
| max77854_fg_read_avg_current(fuelgauge, |
| SEC_BATTERY_CURRENT_UA); |
| break; |
| case SEC_BATTERY_CURRENT_MA: |
| default: |
| fuelgauge->current_avg = val->intval = |
| max77854_get_fuelgauge_value(fuelgauge, |
| FG_CURRENT_AVG); |
| break; |
| } |
| break; |
| /* Full Capacity */ |
| case POWER_SUPPLY_PROP_ENERGY_NOW: |
| switch (val->intval) { |
| case SEC_BATTERY_CAPACITY_DESIGNED: |
| val->intval = max77854_get_fuelgauge_value(fuelgauge, |
| FG_FULLCAP); |
| break; |
| case SEC_BATTERY_CAPACITY_ABSOLUTE: |
| val->intval = max77854_get_fuelgauge_value(fuelgauge, |
| FG_MIXCAP); |
| break; |
| case SEC_BATTERY_CAPACITY_TEMPERARY: |
| val->intval = max77854_get_fuelgauge_value(fuelgauge, |
| FG_AVCAP); |
| break; |
| case SEC_BATTERY_CAPACITY_CURRENT: |
| val->intval = max77854_get_fuelgauge_value(fuelgauge, |
| FG_REPCAP); |
| break; |
| case SEC_BATTERY_CAPACITY_AGEDCELL: |
| val->intval = max77854_get_fuelgauge_value(fuelgauge, |
| FG_FULLCAPNOM); |
| break; |
| case SEC_BATTERY_CAPACITY_CYCLE: |
| val->intval = max77854_get_fuelgauge_value(fuelgauge, |
| FG_CYCLE); |
| break; |
| } |
| break; |
| /* SOC (%) */ |
| case POWER_SUPPLY_PROP_CAPACITY: |
| if (val->intval == SEC_FUELGAUGE_CAPACITY_TYPE_RAW) { |
| val->intval = max77854_get_fuelgauge_value(fuelgauge, |
| FG_RAW_SOC); |
| } else { |
| val->intval = max77854_get_fuelgauge_soc(fuelgauge); |
| |
| fuelgauge->raw_capacity = val->intval; |
| |
| if (fuelgauge->pdata->capacity_calculation_type & |
| (SEC_FUELGAUGE_CAPACITY_TYPE_SCALE | |
| SEC_FUELGAUGE_CAPACITY_TYPE_DYNAMIC_SCALE)) |
| max77854_fg_get_scaled_capacity(fuelgauge, val); |
| |
| /* capacity should be between 0% and 100% |
| * (0.1% degree) |
| */ |
| if (val->intval > 1000) |
| val->intval = 1000; |
| if (val->intval < 0) |
| val->intval = 0; |
| |
| /* get only integer part */ |
| val->intval /= 10; |
| |
| if (fuelgauge->using_hw_vempty) { |
| if ((fuelgauge->raw_capacity <= 50) && |
| !fuelgauge->hw_v_empty){ |
| max77854_fg_set_vempty(fuelgauge, true); |
| } else if ((fuelgauge->raw_capacity > 50) && |
| fuelgauge->hw_v_empty){ |
| max77854_fg_set_vempty(fuelgauge, false); |
| } |
| } |
| |
| if (!fuelgauge->is_charging && |
| !fuelgauge->hw_v_empty && (fuelgauge->sw_v_empty == VEMPTY_MODE)) { |
| pr_info("%s : SW V EMPTY. Decrease SOC\n", __func__); |
| val->intval = 0; |
| } else if ((fuelgauge->sw_v_empty == VEMPTY_RECOVERY_MODE) && |
| (val->intval == fuelgauge->capacity_old)) { |
| fuelgauge->sw_v_empty = NORMAL_MODE; |
| } |
| |
| /* check whether doing the wake_unlock */ |
| if ((val->intval > fuelgauge->pdata->fuel_alert_soc) && |
| fuelgauge->is_fuel_alerted) { |
| max77854_fg_fuelalert_init(fuelgauge, |
| fuelgauge->pdata->fuel_alert_soc); |
| } |
| |
| /* (Only for atomic capacity) |
| * In initial time, capacity_old is 0. |
| * and in resume from sleep, |
| * capacity_old is too different from actual soc. |
| * should update capacity_old |
| * by val->intval in booting or resume. |
| */ |
| if ((fuelgauge->initial_update_of_soc) && |
| (fuelgauge->sw_v_empty == NORMAL_MODE)){ |
| /* updated old capacity */ |
| fuelgauge->capacity_old = val->intval; |
| fuelgauge->initial_update_of_soc = false; |
| break; |
| } |
| |
| if (fuelgauge->pdata->capacity_calculation_type & |
| (SEC_FUELGAUGE_CAPACITY_TYPE_ATOMIC | |
| SEC_FUELGAUGE_CAPACITY_TYPE_SKIP_ABNORMAL)) |
| max77854_fg_get_atomic_capacity(fuelgauge, val); |
| } |
| break; |
| /* Battery Temperature */ |
| case POWER_SUPPLY_PROP_TEMP: |
| /* Target Temperature */ |
| case POWER_SUPPLY_PROP_TEMP_AMBIENT: |
| val->intval = max77854_get_fuelgauge_value(fuelgauge, |
| FG_TEMPERATURE); |
| break; |
| #if defined(CONFIG_EN_OOPS) |
| case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
| return -ENODATA; |
| #endif |
| case POWER_SUPPLY_PROP_ENERGY_FULL: |
| { |
| int fullcap = max77854_get_fuelgauge_value(fuelgauge, FG_FULLCAPNOM); |
| val->intval = fullcap * 100 / fuelgauge->battery_data->Capacity; |
| pr_info("%s: asoc(%d), fullcap(0x%x)\n", |
| __func__, val->intval, fullcap); |
| #if !defined(CONFIG_SEC_FACTORY) |
| max77854_fg_periodic_read(fuelgauge); |
| #endif |
| } |
| break; |
| case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: |
| val->intval = fuelgauge->capacity_max; |
| break; |
| case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW: |
| val->intval = calc_ttf(fuelgauge, val); |
| break; |
| case POWER_SUPPLY_PROP_CHARGE_ENABLED: |
| max77854_bulk_read(fuelgauge->i2c, STATUS_REG, 2, data); |
| val->intval = data[0] & 0x40; |
| break; |
| #if defined(CONFIG_BATTERY_AGE_FORECAST) |
| case POWER_SUPPLY_PROP_CAPACITY_LEVEL: |
| return -ENODATA; |
| #endif |
| case POWER_SUPPLY_PROP_FILTER_CFG: |
| max77854_bulk_read(fuelgauge->i2c, FILTER_CFG_REG, 2, data); |
| val->intval = data[1] << 8 | data[0]; |
| pr_debug("%s: FilterCFG=0x%04X\n", __func__, data[1] << 8 | data[0]); |
| break; |
| |
| default: |
| return -EINVAL; |
| } |
| return 0; |
| } |
| |
| #if defined(CONFIG_UPDATE_BATTERY_DATA) |
| static int max77854_fuelgauge_parse_dt(struct max77854_fuelgauge_data *fuelgauge); |
| #endif |
| static int max77854_fg_set_property(struct power_supply *psy, |
| enum power_supply_property psp, |
| const union power_supply_propval *val) |
| { |
| struct max77854_fuelgauge_data *fuelgauge = |
| container_of(psy, struct max77854_fuelgauge_data, psy_fg); |
| u8 data[2] = {0, 0}; |
| |
| switch (psp) { |
| case POWER_SUPPLY_PROP_STATUS: |
| break; |
| case POWER_SUPPLY_PROP_CHARGE_FULL: |
| if (fuelgauge->pdata->capacity_calculation_type & |
| SEC_FUELGAUGE_CAPACITY_TYPE_DYNAMIC_SCALE) |
| max77854_fg_calculate_dynamic_scale(fuelgauge, val->intval); |
| break; |
| #if defined(CONFIG_EN_OOPS) |
| case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
| max77854_set_full_value(fuelgauge, val->intval); |
| break; |
| #endif |
| case POWER_SUPPLY_PROP_ONLINE: |
| fuelgauge->cable_type = val->intval; |
| if (val->intval == POWER_SUPPLY_TYPE_BATTERY) { |
| fuelgauge->is_charging = false; |
| } else { |
| fuelgauge->is_charging = true; |
| |
| if (fuelgauge->sw_v_empty != NORMAL_MODE) { |
| fuelgauge->sw_v_empty = NORMAL_MODE; |
| fuelgauge->initial_update_of_soc = true; |
| max77854_fg_fuelalert_init(fuelgauge, |
| fuelgauge->pdata->fuel_alert_soc); |
| } |
| } |
| break; |
| /* Battery Temperature */ |
| case POWER_SUPPLY_PROP_CAPACITY: |
| if (val->intval == SEC_FUELGAUGE_CAPACITY_TYPE_RESET) { |
| fuelgauge->initial_update_of_soc = true; |
| if (!max77854_fg_reset(fuelgauge)) |
| return -EINVAL; |
| else |
| break; |
| } |
| case POWER_SUPPLY_PROP_TEMP: |
| if(val->intval < 0) { |
| u16 reg_data; |
| reg_data = max77854_read_word(fuelgauge->i2c, DESIGNCAP_REG); |
| if(reg_data == fuelgauge->battery_data->Capacity) { |
| max77854_write_word(fuelgauge->i2c, DESIGNCAP_REG, |
| fuelgauge->battery_data->Capacity+3); |
| pr_info("%s: set the low temp reset! temp : %d, capacity : 0x%x, original capacity : 0x%x\n", |
| __func__, val->intval, reg_data , fuelgauge->battery_data->Capacity); |
| } |
| } |
| |
| if (!fuelgauge->low_temp_compensation_en && |
| (val->intval <= (int)fuelgauge->low_temp_limit)) { |
| max77854_fg_low_temp_compensation(fuelgauge, true); |
| fuelgauge->low_temp_compensation_en = true; |
| } else if (fuelgauge->low_temp_compensation_en && |
| (val->intval >= (int)fuelgauge->low_temp_recovery)) { |
| max77854_fg_low_temp_compensation(fuelgauge, false); |
| fuelgauge->low_temp_compensation_en = false; |
| } |
| max77854_fg_write_temp(fuelgauge, val->intval); |
| max77854_fg_check_qrtable(fuelgauge); |
| break; |
| case POWER_SUPPLY_PROP_TEMP_AMBIENT: |
| break; |
| case POWER_SUPPLY_PROP_ENERGY_NOW: |
| max77854_fg_reset_capacity_by_jig_connection(fuelgauge); |
| break; |
| case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: |
| pr_info("%s: capacity_max changed, %d -> %d\n", |
| __func__, fuelgauge->capacity_max, val->intval); |
| fuelgauge->capacity_max = max77854_fg_check_capacity_max(fuelgauge, val->intval); |
| fuelgauge->initial_update_of_soc = true; |
| break; |
| case POWER_SUPPLY_PROP_CHARGE_ENABLED: |
| /* Forcedly discharge EN */ |
| max77854_bulk_read(fuelgauge->i2c, CONFIG2_REG, 2, data); |
| data[1] &= ~0x6; |
| data[1] |= val->intval ? 0x1 << 2 : |
| fuelgauge->auto_discharge_en ? 0x1 << 1 : 0x0 << 1; |
| max77854_bulk_write(fuelgauge->i2c, CONFIG2_REG, 2, data); |
| break; |
| #if defined(CONFIG_BATTERY_AGE_FORECAST) |
| case POWER_SUPPLY_PROP_CAPACITY_LEVEL: |
| { |
| u16 reg_fullsocthr; |
| int val_soc = val->intval; |
| if (val->intval > fuelgauge->pdata->full_condition_soc || |
| val->intval <= (fuelgauge->pdata->full_condition_soc - 10)) { |
| pr_info("%s: abnormal value(%d). so thr is changed to default(%d)\n", |
| __func__, val->intval, fuelgauge->pdata->full_condition_soc); |
| val_soc = fuelgauge->pdata->full_condition_soc; |
| } |
| |
| reg_fullsocthr = val_soc << 8; |
| if (max77854_write_word(fuelgauge->i2c, FULLSOCTHR_REG, reg_fullsocthr) < 0) { |
| pr_err("%s: Failed to write FULLSOCTHR_REG\n", __func__); |
| } else { |
| reg_fullsocthr = max77854_read_word(fuelgauge->i2c, FULLSOCTHR_REG); |
| pr_info("%s: FullSOCThr %d%%(0x%04X)\n", __func__, val_soc, reg_fullsocthr); |
| } |
| } |
| break; |
| #endif |
| #if defined(CONFIG_UPDATE_BATTERY_DATA) |
| case POWER_SUPPLY_PROP_POWER_DESIGN: |
| max77854_fuelgauge_parse_dt(fuelgauge); |
| break; |
| #endif |
| case POWER_SUPPLY_PROP_FILTER_CFG: |
| /* Set FilterCFG */ |
| max77854_bulk_read(fuelgauge->i2c, FILTER_CFG_REG, 2, data); |
| pr_debug("%s: FilterCFG=0x%04X\n", __func__, data[1] << 8 | data[0]); |
| data[0] &= ~0xF; |
| data[0] |= (val->intval & 0xF); |
| max77854_bulk_write(fuelgauge->i2c, FILTER_CFG_REG, 2, data); |
| |
| max77854_bulk_read(fuelgauge->i2c, FILTER_CFG_REG, 2, data); |
| pr_debug("%s: FilterCFG=0x%04X\n", __func__, data[1] << 8 | data[0]); |
| break; |
| default: |
| return -EINVAL; |
| } |
| return 0; |
| } |
| |
| static void max77854_fg_isr_work(struct work_struct *work) |
| { |
| struct max77854_fuelgauge_data *fuelgauge = |
| container_of(work, struct max77854_fuelgauge_data, isr_work.work); |
| |
| /* process for fuel gauge chip */ |
| max77854_fg_fuelalert_process(fuelgauge); |
| |
| wake_unlock(&fuelgauge->fuel_alert_wake_lock); |
| } |
| |
| static irqreturn_t max77854_fg_irq_thread(int irq, void *irq_data) |
| { |
| struct max77854_fuelgauge_data *fuelgauge = irq_data; |
| |
| max77854_update_reg(fuelgauge->pmic, |
| MAX77854_PMIC_REG_INTSRC_MASK, |
| MAX77854_IRQSRC_FG, |
| MAX77854_IRQSRC_FG); |
| |
| pr_info("%s\n", __func__); |
| |
| if (fuelgauge->is_fuel_alerted) { |
| return IRQ_HANDLED; |
| } else { |
| wake_lock(&fuelgauge->fuel_alert_wake_lock); |
| fuelgauge->is_fuel_alerted = true; |
| schedule_delayed_work(&fuelgauge->isr_work, 0); |
| } |
| |
| return IRQ_HANDLED; |
| } |
| |
| static int max77854_fuelgauge_debugfs_show(struct seq_file *s, void *data) |
| { |
| struct max77854_fuelgauge_data *fuelgauge = s->private; |
| int i; |
| u8 reg; |
| u16 reg_data; |
| |
| seq_printf(s, "MAX77854 FUELGAUGE IC :\n"); |
| seq_printf(s, "===================\n"); |
| for (i = 0; i < 16; i++) { |
| if (i == 12) |
| continue; |
| for (reg = 0; reg < 0x10; reg++) { |
| reg_data = max77854_read_word(fuelgauge->i2c, reg + i * 0x10); |
| seq_printf(s, "0x%02x:\t0x%04x\n", reg + i * 0x10, reg_data); |
| } |
| if (i == 4) |
| i = 10; |
| } |
| seq_printf(s, "\n"); |
| return 0; |
| } |
| |
| static int max77854_fuelgauge_debugfs_open(struct inode *inode, struct file *file) |
| { |
| return single_open(file, max77854_fuelgauge_debugfs_show, inode->i_private); |
| } |
| |
| static const struct file_operations max77854_fuelgauge_debugfs_fops = { |
| .open = max77854_fuelgauge_debugfs_open, |
| .read = seq_read, |
| .llseek = seq_lseek, |
| .release = single_release, |
| }; |
| |
| #ifdef CONFIG_OF |
| static int max77854_fuelgauge_parse_dt(struct max77854_fuelgauge_data *fuelgauge) |
| { |
| struct device_node *np = of_find_node_by_name(NULL, "max77854-fuelgauge"); |
| sec_fuelgauge_platform_data_t *pdata = fuelgauge->pdata; |
| int ret; |
| int i, len; |
| const u32 *p; |
| |
| /* reset, irq gpio info */ |
| if (np == NULL) { |
| pr_err("%s np NULL\n", __func__); |
| } else { |
| ret = of_property_read_u32(np, "fuelgauge,capacity_max", |
| &pdata->capacity_max); |
| if (ret < 0) |
| pr_err("%s error reading capacity_max %d\n", __func__, ret); |
| |
| ret = of_property_read_u32(np, "fuelgauge,capacity_max_hv", |
| &pdata->capacity_max_hv); |
| if (ret < 0) { |
| pr_err("%s error reading capacity_max_hv %d\n", __func__, ret); |
| fuelgauge->pdata->capacity_max_hv = fuelgauge->pdata->capacity_max; |
| } |
| |
| ret = of_property_read_u32(np, "fuelgauge,capacity_max_margin", |
| &pdata->capacity_max_margin); |
| if (ret < 0) |
| pr_err("%s error reading capacity_max_margin %d\n", __func__, ret); |
| |
| ret = of_property_read_u32(np, "fuelgauge,capacity_min", |
| &pdata->capacity_min); |
| if (ret < 0) |
| pr_err("%s error reading capacity_min %d\n", __func__, ret); |
| |
| ret = of_property_read_u32(np, "fuelgauge,capacity_calculation_type", |
| &pdata->capacity_calculation_type); |
| if (ret < 0) |
| pr_err("%s error reading capacity_calculation_type %d\n", |
| __func__, ret); |
| ret = of_property_read_u32(np, "fuelgauge,fuel_alert_soc", |
| &pdata->fuel_alert_soc); |
| if (ret < 0) |
| pr_err("%s error reading pdata->fuel_alert_soc %d\n", |
| __func__, ret); |
| |
| pdata->repeated_fuelalert = of_property_read_bool(np, |
| "fuelgauge,repeated_fuelalert"); |
| |
| fuelgauge->using_temp_compensation = of_property_read_bool(np, |
| "fuelgauge,using_temp_compensation"); |
| if (fuelgauge->using_temp_compensation) { |
| ret = of_property_read_u32(np, "fuelgauge,low_temp_limit", |
| &fuelgauge->low_temp_limit); |
| if (ret < 0) |
| pr_err("%s error reading low temp limit %d\n", __func__, ret); |
| |
| ret = of_property_read_u32(np, "fuelgauge,low_temp_recovery", |
| &fuelgauge->low_temp_recovery); |
| if (ret < 0) |
| pr_err("%s error reading low temp recovery %d\n", __func__, ret); |
| |
| pr_info("%s : LOW TEMP LIMIT(%d) RECOVERY(%d)\n", |
| __func__, fuelgauge->low_temp_limit, fuelgauge->low_temp_recovery); |
| } |
| |
| fuelgauge->using_hw_vempty = of_property_read_bool(np, |
| "fuelgauge,using_hw_vempty"); |
| if (fuelgauge->using_hw_vempty) { |
| ret = of_property_read_u32(np, "fuelgauge,v_empty", |
| &fuelgauge->battery_data->V_empty); |
| if (ret < 0) |
| pr_err("%s error reading v_empty %d\n", |
| __func__, ret); |
| |
| ret = of_property_read_u32(np, "fuelgauge,v_empty_origin", |
| &fuelgauge->battery_data->V_empty_origin); |
| if(ret < 0) |
| pr_err("%s error reading v_empty_origin %d\n", |
| __func__, ret); |
| } |
| |
| pdata->jig_gpio = of_get_named_gpio(np, "fuelgauge,jig_gpio", 0); |
| if (pdata->jig_gpio < 0) { |
| pr_err("%s error reading jig_gpio = %d\n", |
| __func__,pdata->jig_gpio); |
| pdata->jig_gpio = 0; |
| } else { |
| pdata->jig_irq = gpio_to_irq(pdata->jig_gpio); |
| } |
| |
| ret = of_property_read_u32(np, "fuelgauge,qrtable20", |
| &fuelgauge->battery_data->QResidual20); |
| if (ret < 0) |
| pr_err("%s error reading qrtable20 %d\n", |
| __func__, ret); |
| |
| ret = of_property_read_u32(np, "fuelgauge,qrtable30", |
| &fuelgauge->battery_data->QResidual30); |
| if (ret < 0) |
| pr_err("%s error reading qrtabel30 %d\n", |
| __func__, ret); |
| |
| ret = of_property_read_u32(np, "fuelgauge,fg_resistor", |
| &fuelgauge->fg_resistor); |
| if (ret < 0) { |
| pr_err("%s error reading fg_resistor %d\n", |
| __func__, ret); |
| fuelgauge->fg_resistor = 1; |
| } |
| #if defined(CONFIG_EN_OOPS) |
| ret = of_property_read_u32(np, "fuelgauge,ichgterm", |
| &fuelgauge->battery_data->ichgterm); |
| if (ret < 0) |
| pr_err("%s error reading ichgterm %d\n", |
| __func__, ret); |
| |
| ret = of_property_read_u32(np, "fuelgauge,ichgterm_2nd", |
| &fuelgauge->battery_data->ichgterm_2nd); |
| if (ret < 0) |
| pr_err("%s error reading ichgterm_2nd %d\n", |
| __func__, ret); |
| |
| ret = of_property_read_u32(np, "fuelgauge,misccfg", |
| &fuelgauge->battery_data->misccfg); |
| if (ret < 0) |
| pr_err("%s error reading misccfg %d\n", |
| __func__, ret); |
| |
| ret = of_property_read_u32(np, "fuelgauge,misccfg_2nd", |
| &fuelgauge->battery_data->misccfg_2nd); |
| if (ret < 0) |
| pr_err("%s error reading misccfg_2nd %d\n", |
| __func__, ret); |
| |
| ret = of_property_read_u32(np, "fuelgauge,fullsocthr", |
| &fuelgauge->battery_data->fullsocthr); |
| if (ret < 0) |
| pr_err("%s error reading fullsocthr %d\n", |
| __func__, ret); |
| |
| ret = of_property_read_u32(np, "fuelgauge,fullsocthr_2nd", |
| &fuelgauge->battery_data->fullsocthr_2nd); |
| if (ret < 0) |
| pr_err("%s error reading fullsocthr_2nd %d\n", |
| __func__, ret); |
| #endif |
| |
| ret = of_property_read_u32(np, "fuelgauge,capacity", |
| &fuelgauge->battery_data->Capacity); |
| if (ret < 0) |
| pr_err("%s error reading capacity_calculation_type %d\n", |
| __func__, ret); |
| |
| fuelgauge->auto_discharge_en = of_property_read_bool(np, |
| "fuelgauge,auto_discharge_en"); |
| if (fuelgauge->auto_discharge_en) { |
| ret = of_property_read_u32(np, "fuelgauge,discharge_temp_threshold", |
| &fuelgauge->discharge_temp_threshold); |
| if (ret < 0) |
| fuelgauge->discharge_temp_threshold = 600; |
| |
| ret = of_property_read_u32(np, "fuelgauge,discharge_volt_threshold", |
| &fuelgauge->discharge_volt_threshold); |
| if (ret < 0) |
| fuelgauge->discharge_volt_threshold = 4200; |
| } |
| |
| p = of_get_property(np, "fuelgauge,cv_data", &len); |
| if (p) { |
| fuelgauge->cv_data = kzalloc(len, |
| GFP_KERNEL); |
| fuelgauge->cv_data_lenth = len / sizeof(struct cv_slope); |
| pr_err("%s len: %ld, lenth: %d, %d\n", |
| __func__, sizeof(int) * len, len, fuelgauge->cv_data_lenth); |
| ret = of_property_read_u32_array(np, "fuelgauge,cv_data", |
| (u32 *)fuelgauge->cv_data, len/sizeof(u32)); |
| #if 0 |
| for(i = 0; i < fuelgauge->cv_data_lenth; i++) { |
| pr_err("%s %5d, %5d, %5d\n", |
| __func__, fuelgauge->cv_data[i].fg_current, |
| fuelgauge->cv_data[i].soc, fuelgauge->cv_data[i].time); |
| } |
| #endif |
| |
| if (ret) { |
| pr_err("%s failed to read fuelgauge->cv_data: %d\n", |
| __func__, ret); |
| kfree(fuelgauge->cv_data); |
| fuelgauge->cv_data = NULL; |
| } |
| } else { |
| pr_err("%s there is not cv_data\n", __func__); |
| } |
| |
| np = of_find_node_by_name(NULL, "battery"); |
| ret = of_property_read_u32(np, "battery,thermal_source", |
| &pdata->thermal_source); |
| if (ret < 0) { |
| pr_err("%s error reading pdata->thermal_source %d\n", |
| __func__, ret); |
| } |
| |
| p = of_get_property(np, "battery,input_current_limit", &len); |
| if (!p) |
| return 1; |
| len = len / sizeof(u32); |
| |
| pdata->charging_current = kzalloc(sizeof(sec_charging_current_t) * len, |
| GFP_KERNEL); |
| |
| for(i = 0; i < len; i++) { |
| ret = of_property_read_u32_index(np, |
| "battery,input_current_limit", i, |
| &pdata->charging_current[i].input_current_limit); |
| ret = of_property_read_u32_index(np, |
| "battery,fast_charging_current", i, |
| &pdata->charging_current[i].fast_charging_current); |
| ret = of_property_read_u32_index(np, |
| "battery,full_check_current_1st", i, |
| &pdata->charging_current[i].full_check_current_1st); |
| ret = of_property_read_u32_index(np, |
| "battery,full_check_current_2nd", i, |
| &pdata->charging_current[i].full_check_current_2nd); |
| } |
| |
| #if defined(CONFIG_BATTERY_AGE_FORECAST) |
| ret = of_property_read_u32(np, "battery,full_condition_soc", |
| &pdata->full_condition_soc); |
| if (ret) { |
| pdata->full_condition_soc = 93; |
| pr_info("%s : Full condition soc is Empty\n", __func__); |
| } |
| #endif |
| |
| pr_info("%s thermal: %d, fg_irq: %d, capacity_max: %d\n" |
| "qrtable20: 0x%x, qrtable30 : 0x%x\n" |
| "capacity_max_margin: %d, capacity_min: %d\n" |
| "calculation_type: 0x%x, fuel_alert_soc: %d,\n" |
| "repeated_fuelalert: %d\n", |
| __func__, pdata->thermal_source, pdata->fg_irq, pdata->capacity_max, |
| fuelgauge->battery_data->QResidual20, |
| fuelgauge->battery_data->QResidual30, |
| pdata->capacity_max_margin, pdata->capacity_min, |
| pdata->capacity_calculation_type, pdata->fuel_alert_soc, |
| pdata->repeated_fuelalert); |
| } |
| |
| pr_info("[%s][%d]\n", |
| __func__, fuelgauge->battery_data->Capacity); |
| |
| return 0; |
| } |
| #endif |
| |
| static int max77854_fuelgauge_probe(struct platform_device *pdev) |
| { |
| struct max77854_dev *max77854 = dev_get_drvdata(pdev->dev.parent); |
| struct max77854_platform_data *pdata = dev_get_platdata(max77854->dev); |
| sec_fuelgauge_platform_data_t *fuelgauge_data; |
| struct max77854_fuelgauge_data *fuelgauge; |
| int ret = 0; |
| union power_supply_propval raw_soc_val; |
| #if defined(CONFIG_DISABLE_SAVE_CAPACITY_MAX) |
| u16 reg_data; |
| #endif |
| |
| pr_info("%s: MAX77854 Fuelgauge Driver Loading\n", __func__); |
| |
| fuelgauge = kzalloc(sizeof(*fuelgauge), GFP_KERNEL); |
| if (!fuelgauge) |
| return -ENOMEM; |
| |
| fuelgauge_data = kzalloc(sizeof(sec_fuelgauge_platform_data_t), GFP_KERNEL); |
| if (!fuelgauge_data) { |
| ret = -ENOMEM; |
| goto err_free; |
| } |
| |
| mutex_init(&fuelgauge->fg_lock); |
| |
| fuelgauge->dev = &pdev->dev; |
| fuelgauge->pdata = fuelgauge_data; |
| fuelgauge->i2c = max77854->fuelgauge; |
| fuelgauge->pmic = max77854->i2c; |
| fuelgauge->max77854_pdata = pdata; |
| |
| #if defined(CONFIG_OF) |
| fuelgauge->battery_data = kzalloc(sizeof(struct battery_data_t), |
| GFP_KERNEL); |
| if(!fuelgauge->battery_data) { |
| pr_err("Failed to allocate memory\n"); |
| ret = -ENOMEM; |
| goto err_pdata_free; |
| } |
| ret = max77854_fuelgauge_parse_dt(fuelgauge); |
| if (ret < 0) { |
| pr_err("%s not found charger dt! ret[%d]\n", |
| __func__, ret); |
| } |
| #endif |
| |
| platform_set_drvdata(pdev, fuelgauge); |
| |
| fuelgauge->psy_fg.name = "max77854-fuelgauge"; |
| fuelgauge->psy_fg.type = POWER_SUPPLY_TYPE_UNKNOWN; |
| fuelgauge->psy_fg.get_property = max77854_fg_get_property; |
| fuelgauge->psy_fg.set_property = max77854_fg_set_property; |
| fuelgauge->psy_fg.properties = max77854_fuelgauge_props; |
| fuelgauge->psy_fg.num_properties = |
| ARRAY_SIZE(max77854_fuelgauge_props); |
| fuelgauge->capacity_max = fuelgauge->pdata->capacity_max; |
| raw_soc_val.intval = max77854_get_fuelgauge_value(fuelgauge, FG_RAW_SOC) / 10; |
| |
| #if defined(CONFIG_DISABLE_SAVE_CAPACITY_MAX) |
| reg_data = max77854_read_word(fuelgauge->i2c, 0xD0); |
| |
| if (reg_data >= 900 && reg_data <= 1000 && reg_data != fuelgauge->capacity_max) { |
| pr_info("%s : Capacity Max Update (%d) -> (%d)\n", |
| __func__, fuelgauge->capacity_max, reg_data); |
| fuelgauge->capacity_max = reg_data; |
| } else { |
| pr_info("%s : 0xD0 Register Update (%d) -> (%d)\n", |
| __func__, reg_data, fuelgauge->capacity_max); |
| reg_data = fuelgauge->capacity_max; |
| max77854_write_word(fuelgauge->i2c, 0xD0, reg_data); |
| } |
| #endif |
| |
| if (raw_soc_val.intval > fuelgauge->capacity_max) |
| max77854_fg_calculate_dynamic_scale(fuelgauge, 100); |
| |
| (void) debugfs_create_file("max77854-fuelgauge-regs", |
| S_IRUGO, NULL, (void *)fuelgauge, &max77854_fuelgauge_debugfs_fops); |
| |
| if (!max77854_fg_init(fuelgauge)) { |
| pr_err("%s: Failed to Initialize Fuelgauge\n", __func__); |
| goto err_data_free; |
| } |
| |
| ret = power_supply_register(&pdev->dev, &fuelgauge->psy_fg); |
| if (ret) { |
| pr_err("%s: Failed to Register psy_fg\n", __func__); |
| goto err_data_free; |
| } |
| |
| fuelgauge->fg_irq = pdata->irq_base + MAX77854_FG_IRQ_ALERT; |
| pr_info("[%s]IRQ_BASE(%d) FG_IRQ(%d)\n", |
| __func__, pdata->irq_base, fuelgauge->fg_irq); |
| |
| fuelgauge->is_fuel_alerted = false; |
| if (fuelgauge->pdata->fuel_alert_soc >= 0) { |
| if (max77854_fg_fuelalert_init(fuelgauge, |
| fuelgauge->pdata->fuel_alert_soc)) { |
| wake_lock_init(&fuelgauge->fuel_alert_wake_lock, |
| WAKE_LOCK_SUSPEND, "fuel_alerted"); |
| if (fuelgauge->fg_irq) { |
| INIT_DELAYED_WORK(&fuelgauge->isr_work, max77854_fg_isr_work); |
| |
| ret = request_threaded_irq(fuelgauge->fg_irq, |
| NULL, max77854_fg_irq_thread, |
| IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
| "fuelgauge-irq", fuelgauge); |
| if (ret) { |
| pr_err("%s: Failed to Request IRQ\n", __func__); |
| goto err_supply_unreg; |
| } |
| } |
| } else { |
| pr_err("%s: Failed to Initialize Fuel-alert\n", |
| __func__); |
| goto err_supply_unreg; |
| } |
| } |
| |
| fuelgauge->hw_v_empty = false; |
| fuelgauge->initial_update_of_soc = true; |
| fuelgauge->low_temp_compensation_en = false; |
| fuelgauge->sw_v_empty = NORMAL_MODE; |
| |
| pr_info("%s: MAX77854 Fuelgauge Driver Loaded\n", __func__); |
| return 0; |
| |
| err_supply_unreg: |
| power_supply_unregister(&fuelgauge->psy_fg); |
| err_data_free: |
| #if defined(CONFIG_OF) |
| kfree(fuelgauge->battery_data); |
| #endif |
| err_pdata_free: |
| kfree(fuelgauge_data); |
| mutex_destroy(&fuelgauge->fg_lock); |
| err_free: |
| kfree(fuelgauge); |
| |
| return ret; |
| } |
| |
| static int max77854_fuelgauge_remove(struct platform_device *pdev) |
| { |
| struct max77854_fuelgauge_data *fuelgauge = |
| platform_get_drvdata(pdev); |
| |
| if (fuelgauge->pdata->fuel_alert_soc >= 0) |
| wake_lock_destroy(&fuelgauge->fuel_alert_wake_lock); |
| |
| return 0; |
| } |
| |
| static int max77854_fuelgauge_suspend(struct device *dev) |
| { |
| return 0; |
| } |
| |
| static int max77854_fuelgauge_resume(struct device *dev) |
| { |
| struct max77854_fuelgauge_data *fuelgauge = dev_get_drvdata(dev); |
| |
| fuelgauge->initial_update_of_soc = true; |
| |
| return 0; |
| } |
| |
| static void max77854_fuelgauge_shutdown(struct device *dev) |
| { |
| struct max77854_fuelgauge_data *fuelgauge = dev_get_drvdata(dev); |
| |
| if (fuelgauge->using_hw_vempty) |
| max77854_fg_set_vempty(fuelgauge, false); |
| } |
| |
| static SIMPLE_DEV_PM_OPS(max77854_fuelgauge_pm_ops, max77854_fuelgauge_suspend, |
| max77854_fuelgauge_resume); |
| |
| static struct platform_driver max77854_fuelgauge_driver = { |
| .driver = { |
| .name = "max77854-fuelgauge", |
| .owner = THIS_MODULE, |
| #ifdef CONFIG_PM |
| .pm = &max77854_fuelgauge_pm_ops, |
| #endif |
| .shutdown = max77854_fuelgauge_shutdown, |
| }, |
| .probe = max77854_fuelgauge_probe, |
| .remove = max77854_fuelgauge_remove, |
| }; |
| |
| static int __init max77854_fuelgauge_init(void) |
| { |
| pr_info("%s: \n", __func__); |
| return platform_driver_register(&max77854_fuelgauge_driver); |
| } |
| |
| static void __exit max77854_fuelgauge_exit(void) |
| { |
| platform_driver_unregister(&max77854_fuelgauge_driver); |
| } |
| module_init(max77854_fuelgauge_init); |
| module_exit(max77854_fuelgauge_exit); |
| |
| MODULE_DESCRIPTION("Samsung MAX77854 Fuel Gauge Driver"); |
| MODULE_AUTHOR("Samsung Electronics"); |
| MODULE_LICENSE("GPL"); |