blob: 983a6d4a2bde6d815f18fe5c1b2950a328eed5dc [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Piotr Wilczekca7e5742013-09-24 16:31:22 +02002/*
3 * Copyright (C) 2013 Samsung Electronics
4 * Piotr Wilczek <p.wilczek@samsung.com>
Piotr Wilczekca7e5742013-09-24 16:31:22 +02005 */
6
7#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Piotr Wilczekca7e5742013-09-24 16:31:22 +02009#include <power/pmic.h>
10#include <power/max77693_fg.h>
11#include <i2c.h>
12#include <power/power_chrg.h>
13#include <power/battery.h>
14#include <power/fg_battery_cell_params.h>
15#include <errno.h>
16
17static int max77693_get_vcell(u32 *vcell)
18{
19 u16 value;
20 u8 ret;
21
22 ret = i2c_read(MAX77693_FUEL_I2C_ADDR, MAX77693_VCELL, 1,
23 (u8 *)&value, 2);
24 if (ret)
25 return ret;
26
27 *vcell = (u32)(value >> 3);
28 *vcell = *vcell * 625;
29
30 return 0;
31}
32
33static int max77693_get_soc(u32 *soc)
34{
35 u16 value;
36 u8 ret;
37
38 ret = i2c_read(MAX77693_FUEL_I2C_ADDR, MAX77693_VFSOC, 1,
39 (u8 *)&value, 2);
40 if (ret)
41 return ret;
42
43 *soc = (u32)(value >> 8);
44
45 return 0;
46}
47
48static int power_update_battery(struct pmic *p, struct pmic *bat)
49{
50 struct power_battery *pb = bat->pbat;
51 int ret;
52
53 if (pmic_probe(p)) {
54 puts("Can't find max77693 fuel gauge\n");
Jaehoon Chung10e80f32016-12-15 20:49:50 +090055 return -ENODEV;
Piotr Wilczekca7e5742013-09-24 16:31:22 +020056 }
57
58 ret = max77693_get_soc(&pb->bat->state_of_chrg);
59 if (ret)
60 return ret;
61
62 max77693_get_vcell(&pb->bat->voltage_uV);
Piotr Wilczekca7e5742013-09-24 16:31:22 +020063
64 return 0;
65}
66
67static int power_check_battery(struct pmic *p, struct pmic *bat)
68{
69 struct power_battery *pb = bat->pbat;
70 unsigned int val;
71 int ret = 0;
72
73 if (pmic_probe(p)) {
74 puts("Can't find max77693 fuel gauge\n");
Jaehoon Chung10e80f32016-12-15 20:49:50 +090075 return -ENODEV;
Piotr Wilczekca7e5742013-09-24 16:31:22 +020076 }
77
78 ret = pmic_reg_read(p, MAX77693_STATUS, &val);
79 if (ret)
80 return ret;
81 debug("fg status: 0x%x\n", val);
82
83 ret = pmic_reg_read(p, MAX77693_VERSION, &pb->bat->version);
84 if (ret)
85 return ret;
86
87 ret = power_update_battery(p, bat);
88 if (ret)
89 return ret;
90 debug("fg ver: 0x%x\n", pb->bat->version);
91 printf("BAT: state_of_charge(SOC):%d%%\n",
92 pb->bat->state_of_chrg);
93
94 printf(" voltage: %d.%6.6d [V] (expected to be %d [mAh])\n",
95 pb->bat->voltage_uV / 1000000,
96 pb->bat->voltage_uV % 1000000,
97 pb->bat->capacity);
98
99 if (pb->bat->voltage_uV > 3850000)
100 pb->bat->state = EXT_SOURCE;
101 else if (pb->bat->voltage_uV < 3600000 || pb->bat->state_of_chrg < 5)
102 pb->bat->state = CHARGE;
103 else
104 pb->bat->state = NORMAL;
105
106 return 0;
107}
108
109static struct power_fg power_fg_ops = {
110 .fg_battery_check = power_check_battery,
111 .fg_battery_update = power_update_battery,
112};
113
114int power_fg_init(unsigned char bus)
115{
116 static const char name[] = "MAX77693_FG";
117 struct pmic *p = pmic_alloc();
118
119 if (!p) {
120 printf("%s: POWER allocation error!\n", __func__);
121 return -ENOMEM;
122 }
123
124 debug("Board Fuel Gauge init\n");
125
126 p->name = name;
127 p->interface = PMIC_I2C;
128 p->number_of_regs = FG_NUM_OF_REGS;
129 p->hw.i2c.addr = MAX77693_FUEL_I2C_ADDR;
130 p->hw.i2c.tx_num = 2;
131 p->sensor_byte_order = PMIC_SENSOR_BYTE_ORDER_BIG;
132 p->bus = bus;
133
134 p->fg = &power_fg_ops;
135
136 return 0;
137}