1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2013 Samsung Electronics
4 * Piotr Wilczek <p.wilczek@samsung.com>
8 #include <power/pmic.h>
9 #include <power/max77693_fg.h>
11 #include <power/power_chrg.h>
12 #include <power/battery.h>
13 #include <power/fg_battery_cell_params.h>
16 static int max77693_get_vcell(u32 *vcell)
21 ret = i2c_read(MAX77693_FUEL_I2C_ADDR, MAX77693_VCELL, 1,
26 *vcell = (u32)(value >> 3);
27 *vcell = *vcell * 625;
32 static int max77693_get_soc(u32 *soc)
37 ret = i2c_read(MAX77693_FUEL_I2C_ADDR, MAX77693_VFSOC, 1,
42 *soc = (u32)(value >> 8);
47 static int power_update_battery(struct pmic *p, struct pmic *bat)
49 struct power_battery *pb = bat->pbat;
53 puts("Can't find max77693 fuel gauge\n");
57 ret = max77693_get_soc(&pb->bat->state_of_chrg);
61 max77693_get_vcell(&pb->bat->voltage_uV);
66 static int power_check_battery(struct pmic *p, struct pmic *bat)
68 struct power_battery *pb = bat->pbat;
73 puts("Can't find max77693 fuel gauge\n");
77 ret = pmic_reg_read(p, MAX77693_STATUS, &val);
80 debug("fg status: 0x%x\n", val);
82 ret = pmic_reg_read(p, MAX77693_VERSION, &pb->bat->version);
86 ret = power_update_battery(p, bat);
89 debug("fg ver: 0x%x\n", pb->bat->version);
90 printf("BAT: state_of_charge(SOC):%d%%\n",
91 pb->bat->state_of_chrg);
93 printf(" voltage: %d.%6.6d [V] (expected to be %d [mAh])\n",
94 pb->bat->voltage_uV / 1000000,
95 pb->bat->voltage_uV % 1000000,
98 if (pb->bat->voltage_uV > 3850000)
99 pb->bat->state = EXT_SOURCE;
100 else if (pb->bat->voltage_uV < 3600000 || pb->bat->state_of_chrg < 5)
101 pb->bat->state = CHARGE;
103 pb->bat->state = NORMAL;
108 static struct power_fg power_fg_ops = {
109 .fg_battery_check = power_check_battery,
110 .fg_battery_update = power_update_battery,
113 int power_fg_init(unsigned char bus)
115 static const char name[] = "MAX77693_FG";
116 struct pmic *p = pmic_alloc();
119 printf("%s: POWER allocation error!\n", __func__);
123 debug("Board Fuel Gauge init\n");
126 p->interface = PMIC_I2C;
127 p->number_of_regs = FG_NUM_OF_REGS;
128 p->hw.i2c.addr = MAX77693_FUEL_I2C_ADDR;
129 p->hw.i2c.tx_num = 2;
130 p->sensor_byte_order = PMIC_SENSOR_BYTE_ORDER_BIG;
133 p->fg = &power_fg_ops;