1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2012 Samsung Electronics
4 * Lukasz Majewski <l.majewski@samsung.com>
9 #include <linux/delay.h>
10 #include <power/pmic.h>
11 #include <power/max17042_fg.h>
13 #include <power/max8997_pmic.h>
14 #include <power/power_chrg.h>
15 #include <power/battery.h>
16 #include <power/fg_battery_cell_params.h>
19 static int fg_write_regs(struct pmic *p, u8 addr, u16 *data, int num)
24 for (i = 0; i < num; i++, addr++) {
25 ret = pmic_reg_write(p, addr, *(data + i));
33 static int fg_read_regs(struct pmic *p, u8 addr, u16 *data, int num)
39 for (i = 0; i < num; i++, addr++) {
40 ret = pmic_reg_read(p, addr, &dat);
44 *(data + i) = (u16)dat;
50 static int fg_write_and_verify(struct pmic *p, u8 addr, u16 data)
52 unsigned int val = data;
55 ret |= pmic_reg_write(p, addr, val);
56 ret |= pmic_reg_read(p, addr, &val);
61 if (((u16) val) == data)
67 static void por_fuelgauge_init(struct pmic *p)
69 u16 r_data0[16], r_data1[16], r_data2[16];
70 u32 rewrite_count = 5;
80 /* Initilize Configuration */
81 pmic_reg_write(p, MAX17042_CONFIG, 0x2310);
87 if (!rewrite_count--) {
88 status_msg = "init failed!";
92 /* Unlock Model Access */
93 pmic_reg_write(p, MAX17042_MLOCKReg1, MODEL_UNLOCK1);
94 pmic_reg_write(p, MAX17042_MLOCKReg2, MODEL_UNLOCK2);
96 /* Write/Read/Verify the Custom Model */
97 ret = fg_write_regs(p, MAX17042_MODEL1, cell_character0,
98 ARRAY_SIZE(cell_character0));
102 ret = fg_write_regs(p, MAX17042_MODEL2, cell_character1,
103 ARRAY_SIZE(cell_character1));
107 ret = fg_write_regs(p, MAX17042_MODEL3, cell_character2,
108 ARRAY_SIZE(cell_character2));
113 if (!check_count--) {
117 status_msg = "check failed!";
122 ret = fg_read_regs(p, MAX17042_MODEL1, r_data0, ARRAY_SIZE(r_data0));
126 ret = fg_read_regs(p, MAX17042_MODEL2, r_data1, ARRAY_SIZE(r_data1));
130 ret = fg_read_regs(p, MAX17042_MODEL3, r_data2, ARRAY_SIZE(r_data2));
134 for (i = 0; i < 16; i++) {
135 if ((cell_character0[i] != r_data0[i])
136 || (cell_character1[i] != r_data1[i])
137 || (cell_character2[i] != r_data2[i]))
146 status_msg = "lock failed!";
151 /* Lock model access */
152 pmic_reg_write(p, MAX17042_MLOCKReg1, MODEL_LOCK1);
153 pmic_reg_write(p, MAX17042_MLOCKReg2, MODEL_LOCK2);
155 /* Verify the model access is locked */
156 ret = fg_read_regs(p, MAX17042_MODEL1, r_data0, ARRAY_SIZE(r_data0));
160 ret = fg_read_regs(p, MAX17042_MODEL2, r_data1, ARRAY_SIZE(r_data1));
164 ret = fg_read_regs(p, MAX17042_MODEL3, r_data2, ARRAY_SIZE(r_data2));
168 for (i = 0; i < ARRAY_SIZE(r_data0); i++) {
169 /* Check if model locked */
170 if (r_data0[i] || r_data1[i] || r_data2[i])
174 /* Write Custom Parameters */
175 fg_write_and_verify(p, MAX17042_RCOMP0, RCOMP0);
176 fg_write_and_verify(p, MAX17042_TEMPCO, TempCo);
178 /* Delay at least 350mS */
181 /* Initialization Complete */
182 pmic_reg_read(p, MAX17042_STATUS, &val);
183 /* Write and Verify Status with POR bit Cleared */
184 fg_write_and_verify(p, MAX17042_STATUS, val & ~MAX17042_POR);
186 /* Delay at least 350 ms */
191 debug("%s: model init status: %s\n", p->name, status_msg);
195 static int power_update_battery(struct pmic *p, struct pmic *bat)
197 struct power_battery *pb = bat->pbat;
202 puts("Can't find max17042 fuel gauge\n");
206 ret |= pmic_reg_read(p, MAX17042_VFSOC, &val);
207 pb->bat->state_of_chrg = (val >> 8);
209 pmic_reg_read(p, MAX17042_VCELL, &val);
210 debug("vfsoc: 0x%x\n", val);
211 pb->bat->voltage_uV = ((val & 0xFFUL) >> 3) + ((val & 0xFF00) >> 3);
212 pb->bat->voltage_uV = (pb->bat->voltage_uV * 625);
214 pmic_reg_read(p, 0x05, &val);
215 pb->bat->capacity = val >> 2;
220 static int power_check_battery(struct pmic *p, struct pmic *bat)
222 struct power_battery *pb = bat->pbat;
227 puts("Can't find max17042 fuel gauge\n");
231 ret |= pmic_reg_read(p, MAX17042_STATUS, &val);
232 debug("fg status: 0x%x\n", val);
234 if (val & MAX17042_POR)
235 por_fuelgauge_init(p);
237 ret |= pmic_reg_read(p, MAX17042_VERSION, &val);
238 pb->bat->version = val;
240 power_update_battery(p, bat);
241 debug("fg ver: 0x%x\n", pb->bat->version);
242 printf("BAT: state_of_charge(SOC):%d%%\n",
243 pb->bat->state_of_chrg);
245 printf(" voltage: %d.%6.6d [V] (expected to be %d [mAh])\n",
246 pb->bat->voltage_uV / 1000000,
247 pb->bat->voltage_uV % 1000000,
250 if (pb->bat->voltage_uV > 3850000)
251 pb->bat->state = EXT_SOURCE;
252 else if (pb->bat->voltage_uV < 3600000 || pb->bat->state_of_chrg < 5)
253 pb->bat->state = CHARGE;
255 pb->bat->state = NORMAL;
260 static struct power_fg power_fg_ops = {
261 .fg_battery_check = power_check_battery,
262 .fg_battery_update = power_update_battery,
265 int power_fg_init(unsigned char bus)
267 static const char name[] = "MAX17042_FG";
268 struct pmic *p = pmic_alloc();
271 printf("%s: POWER allocation error!\n", __func__);
275 debug("Board Fuel Gauge init\n");
278 p->interface = PMIC_I2C;
279 p->number_of_regs = FG_NUM_OF_REGS;
280 p->hw.i2c.addr = MAX17042_I2C_ADDR;
281 p->hw.i2c.tx_num = 2;
282 p->sensor_byte_order = PMIC_SENSOR_BYTE_ORDER_BIG;
285 p->fg = &power_fg_ops;