2 * Copyright (C) 2013 Samsung Electronics
3 * Piotr Wilczek <p.wilczek@samsung.com>
5 * SPDX-License-Identifier: GPL-2.0+
9 #include <power/pmic.h>
10 #include <power/max77693_fg.h>
12 #include <power/power_chrg.h>
13 #include <power/battery.h>
14 #include <power/fg_battery_cell_params.h>
17 static int max77693_get_vcell(u32 *vcell)
22 ret = i2c_read(MAX77693_FUEL_I2C_ADDR, MAX77693_VCELL, 1,
27 *vcell = (u32)(value >> 3);
28 *vcell = *vcell * 625;
33 static int max77693_get_soc(u32 *soc)
38 ret = i2c_read(MAX77693_FUEL_I2C_ADDR, MAX77693_VFSOC, 1,
43 *soc = (u32)(value >> 8);
48 static int power_update_battery(struct pmic *p, struct pmic *bat)
50 struct power_battery *pb = bat->pbat;
54 puts("Can't find max77693 fuel gauge\n");
58 ret = max77693_get_soc(&pb->bat->state_of_chrg);
62 max77693_get_vcell(&pb->bat->voltage_uV);
69 static int power_check_battery(struct pmic *p, struct pmic *bat)
71 struct power_battery *pb = bat->pbat;
76 puts("Can't find max77693 fuel gauge\n");
80 ret = pmic_reg_read(p, MAX77693_STATUS, &val);
83 debug("fg status: 0x%x\n", val);
85 ret = pmic_reg_read(p, MAX77693_VERSION, &pb->bat->version);
89 ret = power_update_battery(p, bat);
92 debug("fg ver: 0x%x\n", pb->bat->version);
93 printf("BAT: state_of_charge(SOC):%d%%\n",
94 pb->bat->state_of_chrg);
96 printf(" voltage: %d.%6.6d [V] (expected to be %d [mAh])\n",
97 pb->bat->voltage_uV / 1000000,
98 pb->bat->voltage_uV % 1000000,
101 if (pb->bat->voltage_uV > 3850000)
102 pb->bat->state = EXT_SOURCE;
103 else if (pb->bat->voltage_uV < 3600000 || pb->bat->state_of_chrg < 5)
104 pb->bat->state = CHARGE;
106 pb->bat->state = NORMAL;
111 static struct power_fg power_fg_ops = {
112 .fg_battery_check = power_check_battery,
113 .fg_battery_update = power_update_battery,
116 int power_fg_init(unsigned char bus)
118 static const char name[] = "MAX77693_FG";
119 struct pmic *p = pmic_alloc();
122 printf("%s: POWER allocation error!\n", __func__);
126 debug("Board Fuel Gauge init\n");
129 p->interface = PMIC_I2C;
130 p->number_of_regs = FG_NUM_OF_REGS;
131 p->hw.i2c.addr = MAX77693_FUEL_I2C_ADDR;
132 p->hw.i2c.tx_num = 2;
133 p->sensor_byte_order = PMIC_SENSOR_BYTE_ORDER_BIG;
136 p->fg = &power_fg_ops;