| /* |
| * s2mu004_fuelgauge.c |
| * Samsung S2MU004 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 SINGLE_BYTE 1 |
| #define TABLE_SIZE 22 |
| #define FAKE_TEMP 1 |
| |
| #include <linux/power/s2mu004_fuelgauge.h> |
| #include <linux/of_gpio.h> |
| |
| static enum power_supply_property s2mu004_fuelgauge_props[] = { |
| }; |
| |
| static int s2mu004_get_vbat(struct s2mu004_fuelgauge_data *fuelgauge); |
| static int s2mu004_get_ocv(struct s2mu004_fuelgauge_data *fuelgauge); |
| static int s2mu004_get_current(struct s2mu004_fuelgauge_data *fuelgauge); |
| static int s2mu004_get_avgcurrent(struct s2mu004_fuelgauge_data *fuelgauge); |
| static int s2mu004_get_avgvbat(struct s2mu004_fuelgauge_data *fuelgauge); |
| static int s2mu004_get_monout_avgvbat(struct s2mu004_fuelgauge_data *fuelgauge); |
| |
| static int s2mu004_write_reg_byte(struct i2c_client *client, int reg, u8 data) |
| { |
| int ret, i = 0; |
| |
| ret = i2c_smbus_write_byte_data(client, reg, data); |
| if (ret < 0) { |
| for (i = 0; i < 3; i++) { |
| ret = i2c_smbus_write_byte_data(client, reg, data); |
| if (ret >= 0) |
| break; |
| } |
| |
| if (i >= 3) |
| dev_err(&client->dev, "%s: Error(%d)\n", __func__, ret); |
| } |
| |
| return ret; |
| } |
| |
| static int s2mu004_write_reg(struct i2c_client *client, int reg, u8 *buf) |
| { |
| #if SINGLE_BYTE |
| int ret = 0; |
| |
| s2mu004_write_reg_byte(client, reg, buf[0]); |
| s2mu004_write_reg_byte(client, reg+1, buf[1]); |
| #else |
| int ret, i = 0; |
| |
| ret = i2c_smbus_write_i2c_block_data(client, reg, 2, buf); |
| if (ret < 0) { |
| for (i = 0; i < 3; i++) { |
| ret = i2c_smbus_write_i2c_block_data(client, reg, 2, buf); |
| if (ret >= 0) |
| break; |
| } |
| |
| if (i >= 3) |
| dev_err(&client->dev, "%s: Error(%d)\n", __func__, ret); |
| } |
| #endif |
| return ret; |
| } |
| |
| static int s2mu004_read_reg_byte(struct i2c_client *client, int reg, void *data) |
| { |
| int ret = 0; |
| |
| ret = i2c_smbus_read_byte_data(client, reg); |
| if (ret < 0) |
| return ret; |
| *(u8 *)data = (u8)ret; |
| |
| return ret; |
| } |
| |
| static int s2mu004_read_reg(struct i2c_client *client, int reg, u8 *buf) |
| { |
| #if SINGLE_BYTE |
| int ret = 0; |
| u8 data1 = 0, data2 = 0; |
| |
| s2mu004_read_reg_byte(client, reg, &data1); |
| s2mu004_read_reg_byte(client, reg+1, &data2); |
| buf[0] = data1; |
| buf[1] = data2; |
| #else |
| int ret = 0, i = 0; |
| |
| ret = i2c_smbus_read_i2c_block_data(client, reg, 2, buf); |
| if (ret < 0) { |
| for (i = 0; i < 3; i++) { |
| ret = i2c_smbus_read_i2c_block_data(client, reg, 2, buf); |
| if (ret >= 0) |
| break; |
| } |
| |
| if (i >= 3) |
| dev_err(&client->dev, "%s: Error(%d)\n", __func__, ret); |
| } |
| #endif |
| return ret; |
| } |
| |
| static void s2mu004_fg_test_read(struct i2c_client *client) |
| { |
| u8 data; |
| char str[1016] = {0,}; |
| int i; |
| |
| /* address 0x00 ~ 0x1f */ |
| for (i = 0x0; i <= 0x1F; i++) { |
| s2mu004_read_reg_byte(client, i, &data); |
| sprintf(str+strlen(str), "0x%02x:0x%02x, ", i, data); |
| } |
| |
| /* address 0x25 */ |
| s2mu004_read_reg_byte(client, 0x25, &data); |
| sprintf(str+strlen(str), "0x25:0x%02x, ", data); |
| |
| /* address 0x27 */ |
| s2mu004_read_reg_byte(client, 0x27, &data); |
| sprintf(str+strlen(str), "0x27:0x%02x, ", data); |
| /* print buffer */ |
| pr_info("[FG]%s: %s\n", __func__, str); |
| } |
| |
| static void WA_0_issue_at_init(struct s2mu004_fuelgauge_data *fuelgauge) |
| { |
| int a = 0; |
| u8 v_4e = 0, v_4f = 0, temp1, temp2; |
| int FG_volt, UI_volt, offset; |
| u8 v_40 = 0; |
| u8 temp_REG26 = 0, temp_REG27 = 0, temp = 0; |
| |
| /* Step 1: [Surge test] get UI voltage (0.1mV)*/ |
| UI_volt = s2mu004_get_ocv(fuelgauge); |
| |
| /* current fix for soc */ |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x27, &temp_REG27); |
| temp = temp_REG27; |
| temp |= 0x0F; |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x27, temp); |
| |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x26, &temp_REG26); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x26, 0xFF); |
| |
| /* avgvbat factor value set to 0xFF */ |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x40, &v_40); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x40, 0xFF); |
| |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x1E, 0x0F); |
| msleep(50); |
| |
| /* Step 2: [Surge test] get FG voltage (0.1mV) */ |
| FG_volt = s2mu004_get_vbat(fuelgauge) * 10; |
| |
| /* Step 3: [Surge test] get offset */ |
| offset = UI_volt - FG_volt; |
| pr_err("%s: UI_volt(%d), FG_volt(%d), offset(%d)\n", |
| __func__, UI_volt, FG_volt, offset); |
| |
| /* Step 4: [Surge test] */ |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x4f, &v_4f); |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x4e, &v_4e); |
| pr_err("%s: v_4f(0x%x), v_4e(0x%x)\n", __func__, v_4f, v_4e); |
| |
| a = (v_4f & 0x0F) << 8; |
| a += v_4e; |
| pr_err("%s: a before add offset (0x%x)\n", __func__, a); |
| |
| /* 2`s complement */ |
| if (a & (0x01 << 11)) |
| a = (-10000 * ((a^0xFFF) + 1)) >> 13; |
| else |
| a = (10000 * a) >> 13; |
| |
| a = a + offset; |
| pr_err("%s: a after add offset (0x%x)\n", __func__, a); |
| |
| /* limit upper/lower offset */ |
| if (a > 2490) |
| a = 2490; |
| |
| if (a < (-2490)) |
| a = -2490; |
| |
| a = (a << 13) / 10000; |
| if (a < 0) |
| a = -1*((a^0xFFF)+1); |
| |
| pr_err("%s: a after add offset (0x%x)\n", __func__, a); |
| |
| a &= 0xfff; |
| pr_err("%s: (a)&0xFFF (0x%x)\n", __func__, a); |
| |
| /* modify 0x4f[3:0] */ |
| temp1 = v_4f & 0xF0; |
| temp2 = (u8)((a&0xF00) >> 8); |
| temp1 |= temp2; |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x4f, temp1); |
| |
| /* modify 0x4e[7:0] */ |
| temp2 = (u8)(a & 0xFF); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x4e, temp2); |
| |
| /* restart and dumpdone */ |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x1E, 0x0F); |
| msleep(300); |
| |
| /* restore current register */ |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x27, temp_REG27); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x26, temp_REG26); |
| |
| /* recovery 0x4e and 0x4f */ |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x4f, &temp1); |
| temp1 &= 0xF0; |
| temp1 |= (v_4f & 0x0F); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x4f, temp1); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x4e, v_4e); |
| |
| /* restore monout avgvbat factor value */ |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x40, v_40); |
| } |
| |
| static int s2mu004_get_soc_from_ocv(struct s2mu004_fuelgauge_data *fuelgauge, int target_ocv) |
| { |
| /* 22 values of mapping table for EVT1*/ |
| |
| int *soc_arr; |
| int *ocv_arr; |
| int soc = 0; |
| int ocv = target_ocv * 10; |
| |
| int high_index = TABLE_SIZE - 1; |
| int low_index = 0; |
| int mid_index = 0; |
| |
| soc_arr = fuelgauge->info.soc_arr_val; |
| ocv_arr = fuelgauge->info.ocv_arr_val; |
| |
| pr_err("%s: soc_arr(%d) ocv_arr(%d)\n", __func__, *soc_arr, *ocv_arr); |
| |
| if (ocv <= ocv_arr[TABLE_SIZE - 1]) { |
| soc = soc_arr[TABLE_SIZE - 1]; |
| goto soc_ocv_mapping; |
| } else if (ocv >= ocv_arr[0]) { |
| soc = soc_arr[0]; |
| goto soc_ocv_mapping; |
| } |
| while (low_index <= high_index) { |
| mid_index = (low_index + high_index) >> 1; |
| if (ocv_arr[mid_index] > ocv) |
| low_index = mid_index + 1; |
| else if (ocv_arr[mid_index] < ocv) |
| high_index = mid_index - 1; |
| else { |
| soc = soc_arr[mid_index]; |
| goto soc_ocv_mapping; |
| } |
| } |
| soc = soc_arr[high_index]; |
| soc += ((soc_arr[low_index] - soc_arr[high_index]) * |
| (ocv - ocv_arr[high_index])) / |
| (ocv_arr[low_index] - ocv_arr[high_index]); |
| |
| soc_ocv_mapping: |
| dev_info(&fuelgauge->i2c->dev, "%s: ocv (%d), soc (%d)\n", __func__, ocv, soc); |
| return soc; |
| } |
| |
| static void WA_0_issue_at_init1(struct s2mu004_fuelgauge_data *fuelgauge, int target_ocv) |
| { |
| int a = 0; |
| u8 v_4e = 0, v_4f = 0, temp1, temp2; |
| int FG_volt, UI_volt, offset; |
| u8 v_40 = 0; |
| u8 temp_REG26 = 0, temp_REG27 = 0, temp = 0; |
| |
| /* Step 1: [Surge test] get UI voltage (0.1mV)*/ |
| UI_volt = target_ocv * 10; |
| |
| /* avgvbat factor value set to 0xFF */ |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x40, &v_40); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x40, 0xFF); |
| |
| /* current fix for soc */ |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x27, &temp_REG27); |
| temp = temp_REG27; |
| temp |= 0x0F; |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x27, temp); |
| |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x26, &temp_REG26); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x26, 0xFF); |
| |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x1E, 0x0F); |
| msleep(50); |
| |
| /* Step 2: [Surge test] get FG voltage (0.1mV) */ |
| FG_volt = s2mu004_get_vbat(fuelgauge) * 10; |
| |
| /* Step 3: [Surge test] get offset */ |
| offset = UI_volt - FG_volt; |
| pr_err("%s: UI_volt(%d), FG_volt(%d), offset(%d)\n", |
| __func__, UI_volt, FG_volt, offset); |
| |
| /* Step 4: [Surge test] */ |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x4f, &v_4f); |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x4e, &v_4e); |
| pr_err("%s: v_4f(0x%x), v_4e(0x%x)\n", __func__, v_4f, v_4e); |
| |
| a = (v_4f & 0x0F) << 8; |
| a += v_4e; |
| pr_err("%s: a before add offset (0x%x)\n", __func__, a); |
| |
| /* 2`s complement */ |
| if (a & (0x01 << 11)) |
| a = (-10000 * ((a^0xFFF) + 1)) >> 13; |
| else |
| a = (10000 * a) >> 13; |
| |
| a = a + offset; |
| pr_err("%s: a after add offset (0x%x)\n", __func__, a); |
| |
| /* limit upper/lower offset */ |
| if (a > 2490) |
| a = 2490; |
| |
| if (a < (-2490)) |
| a = -2490; |
| |
| a = (a << 13) / 10000; |
| if (a < 0) |
| a = -1*((a^0xFFF)+1); |
| |
| pr_err("%s: a after add offset (0x%x)\n", __func__, a); |
| |
| a &= 0xfff; |
| pr_err("%s: (a)&0xFFF (0x%x)\n", __func__, a); |
| |
| /* modify 0x4f[3:0] */ |
| temp1 = v_4f & 0xF0; |
| temp2 = (u8)((a&0xF00) >> 8); |
| temp1 |= temp2; |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x4f, temp1); |
| |
| /* modify 0x4e[7:0] */ |
| temp2 = (u8)(a & 0xFF); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x4e, temp2); |
| |
| /* restart and dumpdone */ |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x1E, 0x0F); |
| msleep(300); |
| |
| /* restore current register */ |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x27, temp_REG27); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x26, temp_REG26); |
| |
| pr_info("%s: S2MU004 VBAT : %d\n", __func__, s2mu004_get_vbat(fuelgauge) * 10); |
| |
| /* recovery 0x4e and 0x4f */ |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x4f, &temp1); |
| temp1 &= 0xF0; |
| temp1 |= (v_4f & 0x0F); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x4f, temp1); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x4e, v_4e); |
| |
| /* restore monout avgvbat factor value */ |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x40, v_40); |
| } |
| |
| static void s2mu004_reset_fg(struct s2mu004_fuelgauge_data *fuelgauge) |
| { |
| int i; |
| u8 temp = 0; |
| |
| /* step 0: [Surge test] initialize register of FG */ |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x0E, fuelgauge->info.batcap[0]); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x0F, fuelgauge->info.batcap[1]); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x10, fuelgauge->info.batcap[2]); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x11, fuelgauge->info.batcap[3]); |
| |
| for (i = 0x92; i <= 0xe9; i++) |
| s2mu004_write_reg_byte(fuelgauge->i2c, i, fuelgauge->info.battery_table3[i - 0x92]); |
| |
| for (i = 0xea; i <= 0xff; i++) |
| s2mu004_write_reg_byte(fuelgauge->i2c, i, fuelgauge->info.battery_table4[i - 0xea]); |
| |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x21, 0x13); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x14, 0x40); |
| |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x45, &temp); |
| temp &= 0xF0; |
| temp |= fuelgauge->info.accum[0]; |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x45, temp); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x44, fuelgauge->info.accum[1]); |
| |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x27, &temp); |
| temp |= 0x10; |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x27, temp); |
| |
| /* Interrupt source reference at mixed mode */ |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x43, &temp); |
| temp &= 0xF3; |
| temp |= 0x08; |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x43, temp); |
| |
| /* Charger top off current sensing method change for int. 0x49[7]=0 */ |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x49, &temp); |
| temp &= 0x7F; |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x49, temp); |
| |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x4B, 0x0B); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x4A, 0x10); |
| |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x03, &temp); |
| temp |= 0x10; |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x03, temp); |
| |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x40, 0x08); |
| |
| WA_0_issue_at_init(fuelgauge); |
| pr_err("%s: Reset FG completed\n", __func__); |
| } |
| |
| static void s2mu004_restart_gauging(struct s2mu004_fuelgauge_data *fuelgauge) |
| { |
| u8 temp = 0, temp_REG26 = 0, temp_REG27 = 0; |
| u8 data[2], r_data[2]; |
| u8 v_40; |
| |
| pr_err("%s: Re-calculate SOC and voltage\n", __func__); |
| |
| s2mu004_read_reg(fuelgauge->i2c, S2MU004_REG_IRQ, data); |
| pr_info("%s: irq_reg data (%02x%02x)\n", __func__, data[1], data[0]); |
| |
| /* store data for interrupt mask */ |
| r_data[0] = data[0]; |
| r_data[1] = data[1]; |
| |
| /* disable irq for unwanted interrupt */ |
| data[1] |= 0x0f; |
| s2mu004_write_reg(fuelgauge->i2c, S2MU004_REG_IRQ, data); |
| |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x27, &temp_REG27); |
| temp = temp_REG27; |
| temp |= 0x0F; |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x27, temp); |
| |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x26, &temp_REG26); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x26, 0xFF); |
| |
| /* avgvbat factor value set to 0xFF */ |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x40, &v_40); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x40, 0xFF); |
| |
| /* restart gauge */ |
| /* s2mu004_write_reg_byte(fuelgauge->i2c, 0x1f, 0x01); */ |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x21, 0x13); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x1E, 0x0F); |
| |
| msleep(300); |
| |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x27, temp_REG27); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x26, temp_REG26); |
| |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x27, &temp); |
| pr_info("%s: 0x27 : %02x\n", __func__, temp); |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x26, &temp); |
| pr_info("%s: 0x26 : %02x\n", __func__, temp); |
| |
| /* restore monout avgvbat factor value */ |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x40, v_40); |
| |
| /* enable irq after reset */ |
| s2mu004_write_reg(fuelgauge->i2c, S2MU004_REG_IRQ, r_data); |
| pr_info("%s: re-store irq_reg data (%02x%02x)\n", __func__, r_data[1], r_data[0]); |
| } |
| |
| static void s2mu004_init_regs(struct s2mu004_fuelgauge_data *fuelgauge) |
| { |
| u8 temp = 0; |
| |
| pr_err("%s: s2mu004 fuelgauge initialize\n", __func__); |
| |
| /* Reduce top-off current difference between |
| * Power on charging and Power off charging |
| */ |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x27, &temp); |
| temp |= 0x10; |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x27, temp); |
| |
| /* Interrupt source reference at mixed mode */ |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x43, &temp); |
| temp &= 0xF3; |
| temp |= 0x08; |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x43, temp); |
| |
| /* Charger top off current sensing method change for int. 0x49[7]=0 */ |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x49, &temp); |
| temp &= 0x7F; |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x49, temp); |
| |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x4F, &temp); |
| fuelgauge->reg_OTP_4F = temp; |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x4E, &temp); |
| fuelgauge->reg_OTP_4E = temp; |
| } |
| |
| static void s2mu004_alert_init(struct s2mu004_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2]; |
| |
| /* VBAT Threshold setting */ |
| data[0] = ((fuelgauge->pdata->fuel_alert_vol - 2800) / 50) & 0x0f; |
| |
| /* SOC Threshold setting */ |
| data[0] = data[0] | (fuelgauge->pdata->fuel_alert_soc << 4); |
| |
| data[1] = 0x00; |
| s2mu004_write_reg(fuelgauge->i2c, S2MU004_REG_IRQ_LVL, data); |
| |
| pr_info("%s: irq_lvl(vbat:0x%x, soc:0x%x)\n", __func__, data[0] & 0x0F, data[0] & 0xF0); |
| } |
| |
| static int s2mu004_set_temperature(struct s2mu004_fuelgauge_data *fuelgauge, |
| int temperature) |
| { |
| /* |
| * s5mu004 include temperature sensor so, |
| * do not need to set temperature value. |
| */ |
| return temperature; |
| } |
| |
| static int s2mu004_get_temperature(struct s2mu004_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2]; |
| u16 compliment; |
| s32 temperature = 0; |
| /* |
| * use monitor regiser. |
| * monitor register default setting is temperature |
| */ |
| mutex_lock(&fuelgauge->fg_lock); |
| |
| s2mu004_write_reg_byte(fuelgauge->i2c, S2MU004_REG_MONOUT_SEL, 0x10); |
| if (s2mu004_read_reg(fuelgauge->i2c, S2MU004_REG_MONOUT, data) < 0) |
| goto err; |
| |
| mutex_unlock(&fuelgauge->fg_lock); |
| compliment = (data[1] << 8) | (data[0]); |
| |
| /* data[] store 2's compliment format number */ |
| if (compliment & (0x1 << 15)) { |
| /* Negative */ |
| temperature = -1 * ((~compliment & 0xFFFF) + 1); |
| } else { |
| temperature = compliment & 0x7FFF; |
| } |
| temperature = ((temperature * 100) >> 8)/10; |
| |
| dev_dbg(&fuelgauge->i2c->dev, "%s: temperature (%d)\n", |
| __func__, temperature); |
| |
| /* For test, return room temperature */ |
| /* To use IC's value, check there is NTC & register setting is right*/ |
| #if FAKE_TEMP |
| temperature = 250; |
| #endif |
| |
| return temperature; |
| |
| err: |
| mutex_unlock(&fuelgauge->fg_lock); |
| return -ERANGE; |
| } |
| |
| static int s2mu004_get_rawsoc(struct s2mu004_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2], check_data[2]; |
| u16 compliment; |
| int rsoc, i; |
| u8 por_state = 0; |
| u8 reg = S2MU004_REG_RSOC; |
| u8 reg_OTP_4E = 0, reg_OTP_4F = 0; |
| int fg_reset = 0; |
| bool charging_enabled = false; |
| union power_supply_propval value; |
| int force_power_off_voltage = 0; |
| int rbat = 0; |
| |
| int avg_current = 0, avg_vbat = 0, vbat = 0, curr = 0, avg_monout_vbat = 0; |
| int ocv_pwroff = 0, ocv_pwr_voltagemode = 0; |
| int target_soc = 0; |
| |
| /* SOC VM Monitoring For debugging SOC error */ |
| u8 r_monoutsel; |
| u8 mount_data[2]; |
| u32 mount_compliment; |
| int rvmsoc; |
| |
| struct power_supply *psy; |
| int ret; |
| |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x1F, &por_state); |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x4F, ®_OTP_4F); |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x4E, ®_OTP_4E); |
| |
| dev_err(&fuelgauge->i2c->dev, "%s: OTP 4E(%02x) 4F(%02x) current 4E(%02x) 4F(%02x)\n", |
| __func__, fuelgauge->reg_OTP_4E, fuelgauge->reg_OTP_4F, reg_OTP_4E, reg_OTP_4F); |
| |
| if ((por_state & 0x10) || |
| (fuelgauge->probe_done == true && |
| (fuelgauge->reg_OTP_4E != reg_OTP_4E || fuelgauge->reg_OTP_4F != reg_OTP_4F))) { |
| |
| /* check charging enable */ |
| |
| psy = power_supply_get_by_name("s2mu004-charger"); |
| if (!psy) |
| return -EINVAL; |
| ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_CHARGING_ENABLED, &value); |
| if (ret < 0) |
| pr_err("%s: Fail to execute property\n", __func__); |
| |
| charging_enabled = value.intval; |
| |
| value.intval = S2MU00X_BAT_CHG_MODE_CHARGING_OFF; |
| |
| psy = power_supply_get_by_name("s2mu004-charger"); |
| if (!psy) |
| return -EINVAL; |
| ret = power_supply_set_property(psy, POWER_SUPPLY_PROP_CHARGING_ENABLED, &value); |
| if (ret < 0) |
| pr_err("%s: Fail to execute property\n", __func__); |
| |
| if (fuelgauge->reg_OTP_4E != reg_OTP_4E || fuelgauge->reg_OTP_4F != reg_OTP_4F) { |
| |
| psy = power_supply_get_by_name("s2mu004-charger"); |
| if (!psy) |
| return -EINVAL; |
| ret = power_supply_set_property(psy, POWER_SUPPLY_PROP_FUELGAUGE_RESET, &value); |
| if (ret < 0) |
| pr_err("%s: Fail to execute property\n", __func__); |
| |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x1F, 0x40); |
| msleep(50); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x1F, 0x01); |
| |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x4F, ®_OTP_4F); |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x4E, ®_OTP_4E); |
| |
| dev_err(&fuelgauge->i2c->dev, "1st reset after %s: OTP 4E(%02x) 4F(%02x) current 4E(%02x) 4F(%02x)\n", |
| __func__, fuelgauge->reg_OTP_4E, fuelgauge->reg_OTP_4F, reg_OTP_4E, reg_OTP_4F); |
| |
| if (fuelgauge->reg_OTP_4E != reg_OTP_4E || fuelgauge->reg_OTP_4F != reg_OTP_4F) { |
| |
| psy = power_supply_get_by_name("s2mu004-charger"); |
| if (!psy) |
| return -EINVAL; |
| ret = power_supply_set_property(psy, POWER_SUPPLY_PROP_FUELGAUGE_RESET, &value); |
| if (ret < 0) |
| pr_err("%s: Fail to execute property\n", __func__); |
| |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x1F, 0x40); |
| msleep(50); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x1F, 0x01); |
| dev_err(&fuelgauge->i2c->dev, "%s : 2nd reset\n", __func__); |
| } |
| } |
| |
| dev_info(&fuelgauge->i2c->dev, "%s: FG reset\n", __func__); |
| s2mu004_reset_fg(fuelgauge); |
| por_state &= ~0x10; |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x1F, por_state); |
| |
| fg_reset = 1; |
| } |
| |
| mutex_lock(&fuelgauge->fg_lock); |
| |
| reg = S2MU004_REG_RSOC; |
| |
| for (i = 0; i < 50; i++) { |
| if (s2mu004_read_reg(fuelgauge->i2c, reg, data) < 0) |
| goto err; |
| if (s2mu004_read_reg(fuelgauge->i2c, reg, check_data) < 0) |
| goto err; |
| |
| dev_dbg(&fuelgauge->i2c->dev, "[DEBUG]%s: data0 (%d) data1 (%d)\n", __func__, data[0], data[1]); |
| if ((data[0] == check_data[0]) && (data[1] == check_data[1])) |
| break; |
| } |
| |
| /* SOC VM Monitoring For debugging SOC error */ |
| s2mu004_read_reg_byte(fuelgauge->i2c, S2MU004_REG_MONOUT_SEL, &r_monoutsel); |
| s2mu004_write_reg_byte(fuelgauge->i2c, S2MU004_REG_MONOUT_SEL, 0x02); |
| mdelay(10); |
| s2mu004_read_reg(fuelgauge->i2c, S2MU004_REG_MONOUT, mount_data); |
| |
| s2mu004_write_reg_byte(fuelgauge->i2c, S2MU004_REG_MONOUT_SEL, r_monoutsel); |
| |
| mutex_unlock(&fuelgauge->fg_lock); |
| |
| /* SOC VM Monitoring For debugging SOC error */ |
| mount_compliment = ((mount_data[0] + (mount_data[1] << 8)) * 10000) >> 12; |
| rvmsoc = mount_compliment; |
| |
| dev_dbg(&fuelgauge->i2c->dev, "%s: vm soc raw data0 (%d) data1 (%d)\n", __func__, mount_data[0], mount_data[1]); |
| dev_info(&fuelgauge->i2c->dev, "%s: vm soc (%d)\n", __func__, rvmsoc); |
| |
| if (fg_reset && charging_enabled) { |
| value.intval = S2MU00X_BAT_CHG_MODE_CHARGING; |
| |
| psy = power_supply_get_by_name("s2mu004-charger"); |
| if (!psy) |
| return -EINVAL; |
| ret = power_supply_set_property(psy, POWER_SUPPLY_PROP_CHARGING_ENABLED, &value); |
| if (ret < 0) |
| pr_err("%s: Fail to execute property\n", __func__); |
| } |
| |
| compliment = (data[1] << 8) | (data[0]); |
| |
| /* data[] store 2's compliment format number */ |
| if (compliment & (0x1 << 15)) { |
| /* Negative */ |
| rsoc = ((~compliment) & 0xFFFF) + 1; |
| rsoc = (rsoc * (-10000)) / (0x1 << 14); |
| } else { |
| rsoc = compliment & 0x7FFF; |
| rsoc = ((rsoc * 10000) / (0x1 << 14)); |
| } |
| |
| if (fg_reset) |
| fuelgauge->diff_soc = fuelgauge->info.soc - rsoc; |
| |
| |
| dev_info(&fuelgauge->i2c->dev, "%s: current_soc (%d), previous soc (%d), diff (%d), FG_mode(%d)\n", |
| __func__, rsoc, fuelgauge->info.soc, fuelgauge->diff_soc, fuelgauge->mode); |
| |
| fuelgauge->info.soc = rsoc + fuelgauge->diff_soc; |
| |
| avg_current = s2mu004_get_avgcurrent(fuelgauge); |
| avg_monout_vbat = s2mu004_get_monout_avgvbat(fuelgauge); |
| ocv_pwr_voltagemode = avg_monout_vbat - avg_current*30 / 100; |
| |
| if (avg_current < (-500)) |
| rbat = 10; |
| else |
| rbat = 30; |
| |
| ocv_pwr_voltagemode = avg_monout_vbat - avg_current*rbat / 100; |
| |
| /* switch to voltage mocd for accuracy */ |
| if ((fuelgauge->info.soc <= 300) || ((ocv_pwr_voltagemode <= 3600) && (avg_current < 10))) { |
| if (fuelgauge->mode == CURRENT_MODE) { /* switch to VOLTAGE_MODE */ |
| fuelgauge->mode = LOW_SOC_VOLTAGE_MODE; |
| |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x4A, 0xFF); |
| |
| dev_info(&fuelgauge->i2c->dev, "%s: FG is in low soc voltage mode\n", __func__); |
| } |
| } else if ((fuelgauge->info.soc > 325) && ((ocv_pwr_voltagemode > 3650) || (avg_current >= 10))) { |
| if (fuelgauge->mode == LOW_SOC_VOLTAGE_MODE) { |
| fuelgauge->mode = CURRENT_MODE; |
| |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x4A, 0x10); |
| |
| dev_info(&fuelgauge->i2c->dev, "%s: FG is in current mode\n", __func__); |
| } |
| } |
| |
| psy = power_supply_get_by_name("battery"); |
| if (!psy) |
| return -EINVAL; |
| ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_CAPACITY, &value); |
| if (ret < 0) |
| pr_err("%s: Fail to execute property\n", __func__); |
| |
| if (value.intval >= 98) { |
| if (fuelgauge->mode == CURRENT_MODE) { /* switch to VOLTAGE_MODE */ |
| fuelgauge->mode = HIGH_SOC_VOLTAGE_MODE; |
| |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x4A, 0xFF); |
| |
| dev_info(&fuelgauge->i2c->dev, "%s: FG is in high soc voltage mode\n", __func__); |
| } |
| } else if (value.intval < 97) { |
| if (fuelgauge->mode == HIGH_SOC_VOLTAGE_MODE) { |
| fuelgauge->mode = CURRENT_MODE; |
| |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x4A, 0x10); |
| |
| dev_info(&fuelgauge->i2c->dev, "%s: FG is in current mode\n", __func__); |
| } |
| } |
| |
| avg_vbat = s2mu004_get_avgvbat(fuelgauge); |
| vbat = s2mu004_get_vbat(fuelgauge); |
| curr = s2mu004_get_current(fuelgauge); |
| |
| psy = power_supply_get_by_name("battery"); |
| if (!psy) |
| return -EINVAL; |
| ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &value); |
| if (ret < 0) |
| pr_err("%s: Fail to execute property\n", __func__); |
| |
| if (value.intval <= (-150)) |
| force_power_off_voltage = 3550; |
| else |
| force_power_off_voltage = 3300; |
| |
| dev_info(&fuelgauge->i2c->dev, |
| "%s: Fuelgauge Mode: %d, Force power-off voltage: %d\n", |
| __func__, fuelgauge->mode, force_power_off_voltage); |
| |
| if (((avg_current < (-17)) && (curr < (-17))) && |
| ((avg_monout_vbat - avg_current*rbat / 100) <= 3500) && (fuelgauge->info.soc > 100)) { |
| ocv_pwroff = 3300; |
| target_soc = s2mu004_get_soc_from_ocv(fuelgauge, ocv_pwroff); |
| pr_info("%s : F/G reset Start - current flunctuation\n", __func__); |
| WA_0_issue_at_init1(fuelgauge, ocv_pwroff); |
| } else if (avg_current < (-60) && avg_vbat <= force_power_off_voltage) { |
| if (fuelgauge->mode == CURRENT_MODE) { |
| if (abs(avg_vbat - vbat) <= 20 && abs(avg_current - curr) <= 30) { |
| ocv_pwroff = avg_vbat - avg_current * 15 / 100; |
| target_soc = s2mu004_get_soc_from_ocv(fuelgauge, ocv_pwroff); |
| if (abs(target_soc - fuelgauge->info.soc) > 300) { |
| pr_info("%s : F/G reset Start - current mode: %d\n", __func__, target_soc); |
| WA_0_issue_at_init1(fuelgauge, ocv_pwroff); |
| |
| } |
| } |
| } else { |
| if (abs(avg_vbat - vbat) <= 20) { |
| ocv_pwroff = avg_vbat; |
| target_soc = s2mu004_get_soc_from_ocv(fuelgauge, ocv_pwroff); |
| if (abs(target_soc - fuelgauge->info.soc) > 300) { |
| pr_info("%s : F/G reset Start\n", __func__); |
| WA_0_issue_at_init1(fuelgauge, ocv_pwroff); |
| } |
| } |
| } |
| } |
| |
| |
| s2mu004_fg_test_read(fuelgauge->i2c); |
| return min(fuelgauge->info.soc, 10000); |
| |
| err: |
| mutex_unlock(&fuelgauge->fg_lock); |
| return -EINVAL; |
| } |
| |
| static int s2mu004_get_current(struct s2mu004_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2]; |
| u16 compliment; |
| int curr = 0; |
| |
| if (s2mu004_read_reg(fuelgauge->i2c, S2MU004_REG_RCUR_CC, data) < 0) |
| return -EINVAL; |
| compliment = (data[1] << 8) | (data[0]); |
| dev_dbg(&fuelgauge->i2c->dev, "%s: rCUR_CC(0x%4x)\n", __func__, compliment); |
| |
| if (compliment & (0x1 << 15)) { /* Charging */ |
| curr = ((~compliment) & 0xFFFF) + 1; |
| curr = (curr * 1000) >> 12; |
| } else { /* dischaging */ |
| curr = compliment & 0x7FFF; |
| curr = (curr * (-1000)) >> 12; |
| } |
| |
| dev_info(&fuelgauge->i2c->dev, "%s: current (%d)mA\n", __func__, curr); |
| |
| return curr; |
| } |
| |
| #define TABLE_SIZE 22 |
| static int s2mu004_get_ocv(struct s2mu004_fuelgauge_data *fuelgauge) |
| { |
| /* 22 values of mapping table for EVT1*/ |
| |
| int *soc_arr; |
| int *ocv_arr; |
| |
| int soc = fuelgauge->info.soc; |
| int ocv = 0; |
| |
| int high_index = TABLE_SIZE - 1; |
| int low_index = 0; |
| int mid_index = 0; |
| |
| soc_arr = fuelgauge->info.soc_arr_val; |
| ocv_arr = fuelgauge->info.ocv_arr_val; |
| |
| dev_err(&fuelgauge->i2c->dev, |
| "%s: soc (%d) soc_arr[TABLE_SIZE-1] (%d) ocv_arr[TABLE_SIZE-1) (%d)\n", |
| __func__, soc, soc_arr[TABLE_SIZE-1], ocv_arr[TABLE_SIZE-1]); |
| if (soc <= soc_arr[TABLE_SIZE - 1]) { |
| ocv = ocv_arr[TABLE_SIZE - 1]; |
| goto ocv_soc_mapping; |
| } else if (soc >= soc_arr[0]) { |
| ocv = ocv_arr[0]; |
| goto ocv_soc_mapping; |
| } |
| while (low_index <= high_index) { |
| mid_index = (low_index + high_index) >> 1; |
| if (soc_arr[mid_index] > soc) |
| low_index = mid_index + 1; |
| else if (soc_arr[mid_index] < soc) |
| high_index = mid_index - 1; |
| else { |
| ocv = ocv_arr[mid_index]; |
| goto ocv_soc_mapping; |
| } |
| } |
| ocv = ocv_arr[high_index]; |
| ocv += ((ocv_arr[low_index] - ocv_arr[high_index]) * |
| (soc - soc_arr[high_index])) / |
| (soc_arr[low_index] - soc_arr[high_index]); |
| |
| ocv_soc_mapping: |
| dev_info(&fuelgauge->i2c->dev, "%s: soc (%d), ocv (%d)\n", __func__, soc, ocv); |
| return ocv; |
| } |
| |
| static int s2mu004_get_avgcurrent(struct s2mu004_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2]; |
| u16 compliment; |
| int curr = 0; |
| |
| mutex_lock(&fuelgauge->fg_lock); |
| |
| s2mu004_write_reg_byte(fuelgauge->i2c, S2MU004_REG_MONOUT_SEL, 0x26); |
| |
| if (s2mu004_read_reg(fuelgauge->i2c, S2MU004_REG_MONOUT, data) < 0) |
| goto err; |
| compliment = (data[1] << 8) | (data[0]); |
| |
| if (compliment & (0x1 << 15)) { /* Charging */ |
| curr = ((~compliment) & 0xFFFF) + 1; |
| curr = (curr * 1000) >> 12; |
| } else { /* dischaging */ |
| curr = compliment & 0x7FFF; |
| curr = (curr * (-1000)) >> 12; |
| } |
| s2mu004_write_reg_byte(fuelgauge->i2c, S2MU004_REG_MONOUT_SEL, 0x10); |
| |
| mutex_unlock(&fuelgauge->fg_lock); |
| |
| dev_info(&fuelgauge->i2c->dev, "%s: MONOUT(0x%4x), avg current (%d)mA\n", |
| __func__, compliment, curr); |
| |
| return curr; |
| |
| err: |
| mutex_unlock(&fuelgauge->fg_lock); |
| return -EINVAL; |
| } |
| |
| static int s2mu004_maintain_avgcurrent( |
| struct s2mu004_fuelgauge_data *fuelgauge) |
| { |
| static int cnt; |
| int vcell = 0; |
| int curr = 0; |
| |
| curr = s2mu004_get_avgcurrent(fuelgauge); |
| |
| vcell = s2mu004_get_vbat(fuelgauge); |
| if ((cnt < 10) && (curr < 0) && (fuelgauge->is_charging) && |
| (vcell < 3500)) { |
| curr = 1; |
| cnt++; |
| dev_info(&fuelgauge->i2c->dev, "%s: vcell (%d)mV, modified avg current (%d)mA\n", |
| __func__, vcell, curr); |
| } |
| |
| return curr; |
| } |
| |
| static int s2mu004_get_vbat(struct s2mu004_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2]; |
| u8 vbat_src; |
| u32 vbat = 0; |
| |
| if (s2mu004_read_reg(fuelgauge->i2c, S2MU004_REG_RVBAT, data) < 0) |
| return -EINVAL; |
| |
| dev_dbg(&fuelgauge->i2c->dev, "%s: data0 (%d) data1 (%d)\n", __func__, data[0], data[1]); |
| vbat = ((data[0] + (data[1] << 8)) * 1000) >> 13; |
| |
| s2mu004_read_reg_byte(fuelgauge->i2c, S2MU004_REG_CTRL0, &vbat_src); |
| dev_info(&fuelgauge->i2c->dev, "%s: vbat (%d), src (0x%02X)\n", |
| __func__, vbat, (vbat_src & 0x30) >> 4); |
| |
| return vbat; |
| } |
| |
| static int s2mu004_get_monout_avgvbat(struct s2mu004_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2]; |
| u16 compliment, avg_vbat; |
| |
| mutex_lock(&fuelgauge->fg_lock); |
| |
| s2mu004_write_reg_byte(fuelgauge->i2c, S2MU004_REG_MONOUT_SEL, 0x27); |
| |
| mdelay(50); |
| |
| if (s2mu004_read_reg(fuelgauge->i2c, S2MU004_REG_MONOUT, data) < 0) |
| goto err; |
| compliment = (data[1] << 8) | (data[0]); |
| |
| avg_vbat = (compliment * 1000) >> 12; |
| |
| s2mu004_write_reg_byte(fuelgauge->i2c, S2MU004_REG_MONOUT_SEL, 0x10); |
| |
| mutex_unlock(&fuelgauge->fg_lock); |
| |
| dev_info(&fuelgauge->i2c->dev, "%s: avgvbat (%d)\n", __func__, avg_vbat); |
| |
| return avg_vbat; |
| |
| err: |
| mutex_unlock(&fuelgauge->fg_lock); |
| return -EINVAL; |
| } |
| |
| static int s2mu004_get_avgvbat(struct s2mu004_fuelgauge_data *fuelgauge) |
| { |
| u8 data[2]; |
| u32 new_vbat, old_vbat = 0; |
| int cnt; |
| |
| for (cnt = 0; cnt < 5; cnt++) { |
| if (s2mu004_read_reg(fuelgauge->i2c, S2MU004_REG_RVBAT, data) < 0) |
| return -EINVAL; |
| |
| new_vbat = ((data[0] + (data[1] << 8)) * 1000) >> 13; |
| |
| if (cnt == 0) |
| old_vbat = new_vbat; |
| else |
| old_vbat = new_vbat / 2 + old_vbat / 2; |
| } |
| |
| dev_dbg(&fuelgauge->i2c->dev, "%s: avgvbat (%d)\n", __func__, old_vbat); |
| |
| return old_vbat; |
| } |
| |
| bool s2mu004_fuelgauge_fuelalert_init(struct i2c_client *client, int soc) |
| { |
| struct s2mu004_fuelgauge_data *fuelgauge = i2c_get_clientdata(client); |
| u8 data[2]; |
| |
| fuelgauge->is_fuel_alerted = false; |
| |
| /* 1. Set s2mu004 alert configuration. */ |
| s2mu004_alert_init(fuelgauge); |
| |
| if (s2mu004_read_reg(client, S2MU004_REG_IRQ, data) < 0) |
| return -1; |
| |
| /*Enable VBAT, SOC */ |
| data[1] &= 0xfc; |
| |
| /*Disable IDLE_ST, INIT)ST */ |
| data[1] |= 0x0c; |
| |
| s2mu004_write_reg(client, S2MU004_REG_IRQ, data); |
| |
| dev_dbg(&client->dev, "%s: irq_reg(%02x%02x) irq(%d)\n", |
| __func__, data[1], data[0], fuelgauge->pdata->fg_irq); |
| |
| return true; |
| } |
| |
| static void s2mu004_fg_reset_capacity_by_jig_connection(struct s2mu004_fuelgauge_data *fuelgauge) |
| { |
| u8 data = 0; |
| |
| s2mu004_read_reg_byte(fuelgauge->i2c, S2MU004_REG_FG_ID, &data); |
| data &= 0xF0; |
| data |= 0x0F; /* set model data version 0xF for next boot up initializing fuelgague */ |
| s2mu004_write_reg_byte(fuelgauge->i2c, S2MU004_REG_FG_ID, data); |
| |
| pr_info("%s: set Model data version (0x%x)\n", __func__, data & 0x0F); |
| } |
| |
| static int s2mu004_fg_get_property(struct power_supply *psy, |
| enum power_supply_property psp, |
| union power_supply_propval *val) |
| { |
| struct s2mu004_fuelgauge_data *fuelgauge = |
| power_supply_get_drvdata(psy); |
| |
| switch (psp) { |
| case POWER_SUPPLY_PROP_STATUS: |
| case POWER_SUPPLY_PROP_CHARGE_FULL: |
| return -ENODATA; |
| case POWER_SUPPLY_PROP_ENERGY_NOW: |
| val->intval = fuelgauge->pdata->capacity_full; |
| break; |
| /* Cell voltage (VCELL, mV) */ |
| case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| val->intval = s2mu004_get_vbat(fuelgauge); |
| break; |
| /* Additional Voltage Information (mV) */ |
| case POWER_SUPPLY_PROP_VOLTAGE_AVG: |
| switch (val->intval) { |
| case S2MU00X_BATTERY_VOLTAGE_AVERAGE: |
| val->intval = s2mu004_get_monout_avgvbat(fuelgauge); |
| break; |
| case S2MU00X_BATTERY_VOLTAGE_OCV: |
| val->intval = s2mu004_get_ocv(fuelgauge); |
| break; |
| } |
| break; |
| /* Current (mA) */ |
| case POWER_SUPPLY_PROP_CURRENT_NOW: |
| if (val->intval == S2MU00X_BATTERY_CURRENT_UA) |
| val->intval = s2mu004_get_current(fuelgauge) * 1000; |
| else |
| val->intval = s2mu004_get_current(fuelgauge); |
| break; |
| /* Average Current (mA) */ |
| case POWER_SUPPLY_PROP_CURRENT_AVG: |
| if (val->intval == S2MU00X_BATTERY_CURRENT_UA) |
| val->intval = s2mu004_maintain_avgcurrent(fuelgauge) * 1000; |
| else |
| val->intval = s2mu004_maintain_avgcurrent(fuelgauge); |
| break; |
| case POWER_SUPPLY_PROP_CAPACITY: |
| val->intval = s2mu004_get_rawsoc(fuelgauge) / 10; |
| |
| /* 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; |
| |
| /* check whether doing the wake_unlock */ |
| if ((val->intval > fuelgauge->pdata->fuel_alert_soc) && |
| fuelgauge->is_fuel_alerted) { |
| wake_unlock(&fuelgauge->fuel_alert_wake_lock); |
| s2mu004_fuelgauge_fuelalert_init(fuelgauge->i2c, |
| fuelgauge->pdata->fuel_alert_soc); |
| } |
| break; |
| /* IFPMIC Temperature */ |
| case POWER_SUPPLY_PROP_TEMP: |
| case POWER_SUPPLY_PROP_TEMP_AMBIENT: |
| val->intval = s2mu004_get_temperature(fuelgauge); |
| break; |
| case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: |
| val->intval = fuelgauge->capacity_max; |
| break; |
| case POWER_SUPPLY_PROP_SCOPE: |
| val->intval = fuelgauge->mode; |
| break; |
| default: |
| return -EINVAL; |
| } |
| |
| return 0; |
| } |
| |
| static int s2mu004_fg_set_property(struct power_supply *psy, |
| enum power_supply_property psp, |
| const union power_supply_propval *val) |
| { |
| struct s2mu004_fuelgauge_data *fuelgauge = |
| power_supply_get_drvdata(psy); |
| |
| switch (psp) { |
| case POWER_SUPPLY_PROP_STATUS: |
| break; |
| case POWER_SUPPLY_PROP_CHARGE_FULL: |
| /*full*/ |
| break; |
| case POWER_SUPPLY_PROP_ONLINE: |
| fuelgauge->cable_type = val->intval; |
| break; |
| case POWER_SUPPLY_PROP_CHARGING_ENABLED: |
| if (val->intval) |
| fuelgauge->is_charging = true; |
| else |
| fuelgauge->is_charging = false; |
| break; |
| case POWER_SUPPLY_PROP_CAPACITY: |
| if (val->intval == S2MU00X_FUELGAUGE_CAPACITY_TYPE_RESET) { |
| fuelgauge->initial_update_of_soc = true; |
| s2mu004_restart_gauging(fuelgauge); |
| } |
| break; |
| case POWER_SUPPLY_PROP_TEMP: |
| case POWER_SUPPLY_PROP_TEMP_AMBIENT: |
| s2mu004_set_temperature(fuelgauge, val->intval); |
| break; |
| case POWER_SUPPLY_PROP_ENERGY_NOW: |
| s2mu004_fg_reset_capacity_by_jig_connection(fuelgauge); |
| break; |
| case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: |
| dev_dbg(&fuelgauge->i2c->dev, |
| "%s: capacity_max changed, %d -> %d\n", |
| __func__, fuelgauge->capacity_max, val->intval); |
| fuelgauge->initial_update_of_soc = true; |
| break; |
| case POWER_SUPPLY_PROP_CHARGE_EMPTY: |
| pr_info("%s: WA for battery 0 percent\n", __func__); |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x1F, 0x01); |
| break; |
| case POWER_SUPPLY_PROP_ENERGY_AVG: |
| pr_info("%s: WA for power off issue: val(%d)\n", __func__, val->intval); |
| if (val->intval) |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x41, 0x10); /* charger start */ |
| else |
| s2mu004_write_reg_byte(fuelgauge->i2c, 0x41, 0x04); /* charger end */ |
| break; |
| default: |
| return -EINVAL; |
| } |
| |
| return 0; |
| } |
| |
| static void s2mu004_fg_isr_work(struct work_struct *work) |
| { |
| struct s2mu004_fuelgauge_data *fuelgauge = |
| container_of(work, struct s2mu004_fuelgauge_data, isr_work.work); |
| u8 fg_alert_status = 0; |
| |
| s2mu004_read_reg_byte(fuelgauge->i2c, S2MU004_REG_STATUS, &fg_alert_status); |
| dev_info(&fuelgauge->i2c->dev, "%s : fg_alert_status(0x%x)\n", |
| __func__, fg_alert_status); |
| |
| fg_alert_status &= 0x03; |
| if (fg_alert_status & 0x01) |
| pr_info("%s : Battery Level is very Low!\n", __func__); |
| |
| if (fg_alert_status & 0x02) { |
| int voltage = s2mu004_get_vbat(fuelgauge); |
| |
| pr_info("%s : Battery Votage is very Low! (%dmV)\n", |
| __func__, voltage); |
| } |
| |
| if (!fg_alert_status) { |
| fuelgauge->is_fuel_alerted = false; |
| pr_info("%s : Battery Health is good!\n", __func__); |
| wake_unlock(&fuelgauge->fuel_alert_wake_lock); |
| } |
| } |
| |
| static irqreturn_t s2mu004_fg_irq_thread(int irq, void *irq_data) |
| { |
| struct s2mu004_fuelgauge_data *fuelgauge = irq_data; |
| u8 fg_irq = 0; |
| |
| s2mu004_read_reg_byte(fuelgauge->i2c, S2MU004_REG_IRQ, &fg_irq); |
| pr_info("%s: fg_irq(0x%x)\n", __func__, fg_irq); |
| |
| 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; |
| } |
| |
| #ifdef CONFIG_OF |
| static int s2mu004_fuelgauge_parse_dt(struct s2mu004_fuelgauge_data *fuelgauge) |
| { |
| struct device_node *np = of_find_node_by_name(NULL, "s2mu004-fuelgauge"); |
| int ret; |
| |
| /* reset, irq gpio info */ |
| if (np == NULL) { |
| pr_err("%s np NULL\n", __func__); |
| } else { |
| fuelgauge->pdata->fg_irq = of_get_named_gpio(np, "fuelgauge,fuel_int", 0); |
| if (fuelgauge->pdata->fg_irq < 0) |
| pr_err("%s error reading fg_irq = %d\n", |
| __func__, fuelgauge->pdata->fg_irq); |
| |
| ret = of_property_read_u32(np, "fuelgauge,capacity_max", |
| &fuelgauge->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_margin", |
| &fuelgauge->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", |
| &fuelgauge->pdata->capacity_min); |
| if (ret < 0) |
| pr_err("%s error reading capacity_min %d\n", __func__, ret); |
| |
| ret = of_property_read_u32(np, "fuelgauge,fuel_alert_soc", |
| &fuelgauge->pdata->fuel_alert_soc); |
| if (ret < 0) |
| pr_err("%s error reading pdata->fuel_alert_soc %d\n", |
| __func__, ret); |
| |
| ret = of_property_read_u32(np, "fuelgauge,capacity_full", |
| &fuelgauge->pdata->capacity_full); |
| if (ret < 0) |
| pr_err("%s error reading pdata->capacity_full %d\n", |
| __func__, ret); |
| |
| ret = of_property_read_u32(np, "fuelgauge,fuel_alert_vol", |
| &fuelgauge->pdata->fuel_alert_vol); |
| if (ret < 0) |
| pr_err("%s error reading pdata->fuel_alert_vol %d\n", |
| __func__, ret); |
| |
| fuelgauge->pdata->repeated_fuelalert = of_property_read_bool(np, |
| "fuelgauge,repeated_fuelalert"); |
| } |
| |
| /* get battery node */ |
| np = of_find_node_by_name(NULL, "battery"); |
| if (!np) { |
| pr_err("%s battery node NULL\n", __func__); |
| } else { |
| /* get battery_table */ |
| ret = of_property_read_u32_array(np, "battery,battery_table3", fuelgauge->info.battery_table3, 88); |
| if (ret < 0) |
| pr_err("%s error reading battery,battery_table3\n", __func__); |
| |
| ret = of_property_read_u32_array(np, "battery,battery_table4", fuelgauge->info.battery_table4, 22); |
| if (ret < 0) |
| pr_err("%s error reading battery,battery_table4\n", __func__); |
| |
| ret = of_property_read_u32_array(np, "battery,batcap", fuelgauge->info.batcap, 4); |
| if (ret < 0) |
| pr_err("%s error reading battery,batcap\n", __func__); |
| |
| ret = of_property_read_u32_array(np, "battery,accum", fuelgauge->info.accum, 2); |
| if (ret < 0) |
| pr_err("%s error reading battery,accum\n", __func__); |
| |
| ret = of_property_read_u32_array(np, "battery,soc_arr_val", fuelgauge->info.soc_arr_val, 22); |
| if (ret < 0) |
| pr_err("%s error reading battery,soc_arr_val\n", __func__); |
| |
| ret = of_property_read_u32_array(np, "battery,ocv_arr_val", fuelgauge->info.ocv_arr_val, 22); |
| if (ret < 0) |
| pr_err("%s error reading battery,ocv_arr_val\n", __func__); |
| } |
| |
| return 0; |
| } |
| |
| static const struct of_device_id s2mu004_fuelgauge_match_table[] = { |
| { .compatible = "samsung,s2mu004-fuelgauge",}, |
| {}, |
| }; |
| #else |
| static int s2mu004_fuelgauge_parse_dt(struct s2mu004_fuelgauge_data *fuelgauge) |
| { |
| return -ENOSYS; |
| } |
| |
| #define s2mu004_fuelgauge_match_table NULL |
| #endif /* CONFIG_OF */ |
| |
| static int s2mu004_fuelgauge_probe(struct i2c_client *client, |
| const struct i2c_device_id *id) |
| { |
| struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
| struct s2mu004_fuelgauge_data *fuelgauge; |
| union power_supply_propval raw_soc_val; |
| struct power_supply_config psy_cfg = {}; |
| int ret = 0; |
| u8 temp = 0; |
| |
| pr_info("%s: S2MU004 Fuelgauge Driver Loading\n", __func__); |
| |
| if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) |
| return -EIO; |
| |
| fuelgauge = kzalloc(sizeof(*fuelgauge), GFP_KERNEL); |
| if (!fuelgauge) |
| return -ENOMEM; |
| |
| mutex_init(&fuelgauge->fg_lock); |
| |
| fuelgauge->i2c = client; |
| |
| if (client->dev.of_node) { |
| fuelgauge->pdata = devm_kzalloc(&client->dev, sizeof(*(fuelgauge->pdata)), |
| GFP_KERNEL); |
| if (!fuelgauge->pdata) { |
| ret = -ENOMEM; |
| goto err_parse_dt_nomem; |
| } |
| ret = s2mu004_fuelgauge_parse_dt(fuelgauge); |
| if (ret < 0) |
| goto err_parse_dt; |
| } else { |
| fuelgauge->pdata = client->dev.platform_data; |
| } |
| |
| i2c_set_clientdata(client, fuelgauge); |
| |
| if (fuelgauge->pdata->fuelgauge_name == NULL) |
| fuelgauge->pdata->fuelgauge_name = "s2mu004-fuelgauge"; |
| |
| fuelgauge->psy_fg_desc.name = fuelgauge->pdata->fuelgauge_name; |
| fuelgauge->psy_fg_desc.type = POWER_SUPPLY_TYPE_UNKNOWN; |
| fuelgauge->psy_fg_desc.get_property = s2mu004_fg_get_property; |
| fuelgauge->psy_fg_desc.set_property = s2mu004_fg_set_property; |
| fuelgauge->psy_fg_desc.properties = s2mu004_fuelgauge_props; |
| fuelgauge->psy_fg_desc.num_properties = |
| ARRAY_SIZE(s2mu004_fuelgauge_props); |
| |
| /* 0x48[7:4]=0010 : EVT2 */ |
| fuelgauge->revision = 0; |
| s2mu004_read_reg_byte(fuelgauge->i2c, 0x48, &temp); |
| fuelgauge->revision = (temp & 0xF0) >> 4; |
| |
| pr_info("%s: S2MU004 Fuelgauge revision: %d, reg 0x48 = 0x%x\n", __func__, fuelgauge->revision, temp); |
| |
| fuelgauge->capacity_max = fuelgauge->pdata->capacity_max; |
| fuelgauge->info.soc = 0; |
| fuelgauge->mode = CURRENT_MODE; |
| |
| raw_soc_val.intval = s2mu004_get_rawsoc(fuelgauge); |
| raw_soc_val.intval = raw_soc_val.intval / 10; |
| |
| s2mu004_init_regs(fuelgauge); |
| |
| psy_cfg.drv_data = fuelgauge; |
| fuelgauge->psy_fg = power_supply_register(&client->dev, &fuelgauge->psy_fg_desc, &psy_cfg); |
| if (IS_ERR(fuelgauge->psy_fg)) { |
| pr_err("%s: Failed to Register psy_fg\n", __func__); |
| ret = PTR_ERR(fuelgauge->psy_fg); |
| goto err_data_free; |
| } |
| |
| fuelgauge->is_fuel_alerted = false; |
| if (fuelgauge->pdata->fuel_alert_soc >= 0) { |
| s2mu004_fuelgauge_fuelalert_init(fuelgauge->i2c, |
| fuelgauge->pdata->fuel_alert_soc); |
| wake_lock_init(&fuelgauge->fuel_alert_wake_lock, |
| WAKE_LOCK_SUSPEND, "fuel_alerted"); |
| |
| if (fuelgauge->pdata->fg_irq > 0) { |
| INIT_DELAYED_WORK( |
| &fuelgauge->isr_work, s2mu004_fg_isr_work); |
| |
| fuelgauge->fg_irq = gpio_to_irq(fuelgauge->pdata->fg_irq); |
| dev_info(&client->dev, |
| "%s : fg_irq = %d\n", __func__, fuelgauge->fg_irq); |
| if (fuelgauge->fg_irq > 0) { |
| ret = request_threaded_irq(fuelgauge->fg_irq, |
| NULL, s2mu004_fg_irq_thread, |
| IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
| "fuelgauge-irq", fuelgauge); |
| if (ret) { |
| dev_err(&client->dev, |
| "%s: Failed to Request IRQ\n", __func__); |
| goto err_supply_unreg; |
| } |
| |
| ret = enable_irq_wake(fuelgauge->fg_irq); |
| if (ret < 0) |
| dev_err(&client->dev, |
| "%s: Failed to Enable Wakeup Source(%d)\n", |
| __func__, ret); |
| } else { |
| dev_err(&client->dev, "%s: Failed gpio_to_irq(%d)\n", |
| __func__, fuelgauge->fg_irq); |
| goto err_supply_unreg; |
| } |
| } |
| } |
| |
| fuelgauge->sleep_initial_update_of_soc = false; |
| fuelgauge->initial_update_of_soc = true; |
| |
| fuelgauge->cc_on = true; |
| fuelgauge->probe_done = true; |
| |
| pr_info("%s: S2MU004 Fuelgauge Driver Loaded\n", __func__); |
| return 0; |
| |
| err_supply_unreg: |
| power_supply_unregister(fuelgauge->psy_fg); |
| err_data_free: |
| if (client->dev.of_node) |
| kfree(fuelgauge->pdata); |
| |
| err_parse_dt: |
| err_parse_dt_nomem: |
| mutex_destroy(&fuelgauge->fg_lock); |
| kfree(fuelgauge); |
| |
| return ret; |
| } |
| |
| static const struct i2c_device_id s2mu004_fuelgauge_id[] = { |
| {"s2mu004-fuelgauge", 0}, |
| {} |
| }; |
| |
| static void s2mu004_fuelgauge_shutdown(struct i2c_client *client) |
| { |
| |
| } |
| |
| static int s2mu004_fuelgauge_remove(struct i2c_client *client) |
| { |
| struct s2mu004_fuelgauge_data *fuelgauge = i2c_get_clientdata(client); |
| |
| if (fuelgauge->pdata->fuel_alert_soc >= 0) |
| wake_lock_destroy(&fuelgauge->fuel_alert_wake_lock); |
| |
| return 0; |
| } |
| |
| #if defined CONFIG_PM |
| static int s2mu004_fuelgauge_suspend(struct device *dev) |
| { |
| return 0; |
| } |
| |
| static int s2mu004_fuelgauge_resume(struct device *dev) |
| { |
| struct s2mu004_fuelgauge_data *fuelgauge = dev_get_drvdata(dev); |
| |
| fuelgauge->sleep_initial_update_of_soc = true; |
| |
| return 0; |
| } |
| #else |
| #define s2mu004_fuelgauge_suspend NULL |
| #define s2mu004_fuelgauge_resume NULL |
| #endif |
| |
| static SIMPLE_DEV_PM_OPS(s2mu004_fuelgauge_pm_ops, s2mu004_fuelgauge_suspend, |
| s2mu004_fuelgauge_resume); |
| |
| static struct i2c_driver s2mu004_fuelgauge_driver = { |
| .driver = { |
| .name = "s2mu004-fuelgauge", |
| .owner = THIS_MODULE, |
| .pm = &s2mu004_fuelgauge_pm_ops, |
| .of_match_table = s2mu004_fuelgauge_match_table, |
| }, |
| .probe = s2mu004_fuelgauge_probe, |
| .remove = s2mu004_fuelgauge_remove, |
| .shutdown = s2mu004_fuelgauge_shutdown, |
| .id_table = s2mu004_fuelgauge_id, |
| }; |
| |
| static int __init s2mu004_fuelgauge_init(void) |
| { |
| pr_info("%s: S2MU004 Fuelgauge Init\n", __func__); |
| return i2c_add_driver(&s2mu004_fuelgauge_driver); |
| } |
| |
| static void __exit s2mu004_fuelgauge_exit(void) |
| { |
| i2c_del_driver(&s2mu004_fuelgauge_driver); |
| } |
| module_init(s2mu004_fuelgauge_init); |
| module_exit(s2mu004_fuelgauge_exit); |
| |
| MODULE_DESCRIPTION("Samsung S2MU004 Fuel Gauge Driver"); |
| MODULE_AUTHOR("Samsung Electronics"); |
| MODULE_LICENSE("GPL"); |