1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016-2019 The Linux Foundation. All rights reserved.
4 * Copyright (c) 2023, Linaro Limited
6 #include <linux/delay.h>
8 #include <linux/power_supply.h>
9 #include <linux/regmap.h>
11 #define REG_BATID 0x00 /* This one is very unclear */
12 #define BATID_101 0x0101 /* 107kOhm */
13 #define BATID_102 0x0102 /* 10kOhm */
14 #define REG_TEMPERATURE 0x06
15 #define REG_VOLTAGE 0x08
16 #define REG_FLAGS 0x0a
17 #define MM8013_FLAG_OTC BIT(15)
18 #define MM8013_FLAG_OTD BIT(14)
19 #define MM8013_FLAG_BATHI BIT(13)
20 #define MM8013_FLAG_BATLOW BIT(12)
21 #define MM8013_FLAG_CHG_INH BIT(11)
22 #define MM8013_FLAG_FC BIT(9)
23 #define MM8013_FLAG_CHG BIT(8)
24 #define MM8013_FLAG_OCC BIT(6)
25 #define MM8013_FLAG_ODC BIT(5)
26 #define MM8013_FLAG_OT BIT(4)
27 #define MM8013_FLAG_UT BIT(3)
28 #define MM8013_FLAG_DSG BIT(0)
29 #define REG_FULL_CHARGE_CAPACITY 0x0e
30 #define REG_NOMINAL_CHARGE_CAPACITY 0x0c
31 #define REG_AVERAGE_CURRENT 0x14
32 #define REG_AVERAGE_TIME_TO_EMPTY 0x16
33 #define REG_AVERAGE_TIME_TO_FULL 0x18
34 #define REG_MAX_LOAD_CURRENT 0x1e
35 #define REG_CYCLE_COUNT 0x2a
36 #define REG_STATE_OF_CHARGE 0x2c
37 #define REG_DESIGN_CAPACITY 0x3c
38 /* TODO: 0x62-0x68 seem to contain 'MM8013C' in a length-prefixed, non-terminated string */
40 #define DECIKELVIN_TO_DECIDEGC(t) (t - 2731)
43 struct i2c_client *client;
44 struct regmap *regmap;
47 static int mm8013_checkdevice(struct mm8013_chip *chip)
52 ret = regmap_write(chip->regmap, REG_BATID, 0x0008);
56 ret = regmap_read(chip->regmap, REG_BATID, &val);
62 else if (val == BATID_101)
67 dev_dbg(&chip->client->dev, "battery_id: %d\n", battery_id);
72 static enum power_supply_property mm8013_battery_props[] = {
73 POWER_SUPPLY_PROP_CAPACITY,
74 POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR,
75 POWER_SUPPLY_PROP_CHARGE_FULL,
76 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
77 POWER_SUPPLY_PROP_CHARGE_NOW,
78 POWER_SUPPLY_PROP_CURRENT_MAX,
79 POWER_SUPPLY_PROP_CURRENT_NOW,
80 POWER_SUPPLY_PROP_CYCLE_COUNT,
81 POWER_SUPPLY_PROP_HEALTH,
82 POWER_SUPPLY_PROP_PRESENT,
83 POWER_SUPPLY_PROP_STATUS,
84 POWER_SUPPLY_PROP_TEMP,
85 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
86 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
87 POWER_SUPPLY_PROP_VOLTAGE_NOW,
90 static int mm8013_get_property(struct power_supply *psy,
91 enum power_supply_property psp,
92 union power_supply_propval *val)
94 struct mm8013_chip *chip = psy->drv_data;
99 case POWER_SUPPLY_PROP_CAPACITY:
100 ret = regmap_read(chip->regmap, REG_STATE_OF_CHARGE, ®val);
104 val->intval = regval;
106 case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR:
107 ret = regmap_read(chip->regmap, REG_FLAGS, ®val);
111 if (regval & MM8013_FLAG_CHG_INH)
112 val->intval = POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE;
114 val->intval = POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO;
116 case POWER_SUPPLY_PROP_CHARGE_FULL:
117 ret = regmap_read(chip->regmap, REG_FULL_CHARGE_CAPACITY, ®val);
121 val->intval = 1000 * regval;
123 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
124 ret = regmap_read(chip->regmap, REG_DESIGN_CAPACITY, ®val);
128 val->intval = 1000 * regval;
130 case POWER_SUPPLY_PROP_CHARGE_NOW:
131 ret = regmap_read(chip->regmap, REG_NOMINAL_CHARGE_CAPACITY, ®val);
135 val->intval = 1000 * regval;
137 case POWER_SUPPLY_PROP_CURRENT_MAX:
138 ret = regmap_read(chip->regmap, REG_MAX_LOAD_CURRENT, ®val);
142 val->intval = -1000 * (s16)regval;
144 case POWER_SUPPLY_PROP_CURRENT_NOW:
145 ret = regmap_read(chip->regmap, REG_AVERAGE_CURRENT, ®val);
149 val->intval = -1000 * (s16)regval;
151 case POWER_SUPPLY_PROP_CYCLE_COUNT:
152 ret = regmap_read(chip->regmap, REG_CYCLE_COUNT, ®val);
156 val->intval = regval;
158 case POWER_SUPPLY_PROP_HEALTH:
159 ret = regmap_read(chip->regmap, REG_FLAGS, ®val);
163 if (regval & MM8013_FLAG_UT)
164 val->intval = POWER_SUPPLY_HEALTH_COLD;
165 else if (regval & (MM8013_FLAG_ODC | MM8013_FLAG_OCC))
166 val->intval = POWER_SUPPLY_HEALTH_OVERCURRENT;
167 else if (regval & (MM8013_FLAG_BATLOW))
168 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
169 else if (regval & MM8013_FLAG_BATHI)
170 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
171 else if (regval & (MM8013_FLAG_OT | MM8013_FLAG_OTD | MM8013_FLAG_OTC))
172 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
174 val->intval = POWER_SUPPLY_HEALTH_GOOD;
176 case POWER_SUPPLY_PROP_PRESENT:
177 ret = regmap_read(chip->regmap, REG_TEMPERATURE, ®val);
181 val->intval = ((s16)regval > 0);
183 case POWER_SUPPLY_PROP_STATUS:
184 ret = regmap_read(chip->regmap, REG_FLAGS, ®val);
188 if (regval & MM8013_FLAG_DSG)
189 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
190 else if (regval & MM8013_FLAG_CHG)
191 val->intval = POWER_SUPPLY_STATUS_CHARGING;
192 else if (regval & MM8013_FLAG_FC)
193 val->intval = POWER_SUPPLY_STATUS_FULL;
195 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
197 case POWER_SUPPLY_PROP_TEMP:
198 ret = regmap_read(chip->regmap, REG_TEMPERATURE, ®val);
202 val->intval = DECIKELVIN_TO_DECIDEGC(regval);
204 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
205 ret = regmap_read(chip->regmap, REG_AVERAGE_TIME_TO_EMPTY, ®val);
209 /* The estimation is not yet ready */
210 if (regval == U16_MAX)
213 val->intval = regval;
215 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
216 ret = regmap_read(chip->regmap, REG_AVERAGE_TIME_TO_FULL, ®val);
220 /* The estimation is not yet ready */
221 if (regval == U16_MAX)
224 val->intval = regval;
226 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
227 ret = regmap_read(chip->regmap, REG_VOLTAGE, ®val);
231 val->intval = 1000 * regval;
240 static const struct power_supply_desc mm8013_desc = {
242 .type = POWER_SUPPLY_TYPE_BATTERY,
243 .properties = mm8013_battery_props,
244 .num_properties = ARRAY_SIZE(mm8013_battery_props),
245 .get_property = mm8013_get_property,
248 static const struct regmap_config mm8013_regmap_config = {
251 .max_register = 0x68,
252 .use_single_read = true,
253 .use_single_write = true,
254 .val_format_endian = REGMAP_ENDIAN_LITTLE,
257 static int mm8013_probe(struct i2c_client *client)
259 struct power_supply_config psy_cfg = {};
260 struct device *dev = &client->dev;
261 struct power_supply *psy;
262 struct mm8013_chip *chip;
265 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
266 return dev_err_probe(dev, -EIO,
267 "I2C_FUNC_SMBUS_WORD_DATA not supported\n");
269 chip = devm_kzalloc(dev, sizeof(struct mm8013_chip), GFP_KERNEL);
273 chip->client = client;
275 chip->regmap = devm_regmap_init_i2c(client, &mm8013_regmap_config);
276 if (IS_ERR(chip->regmap)) {
277 ret = PTR_ERR(chip->regmap);
278 return dev_err_probe(dev, ret, "Couldn't initialize regmap\n");
281 ret = mm8013_checkdevice(chip);
283 return dev_err_probe(dev, ret, "MM8013 not found\n");
285 psy_cfg.drv_data = chip;
286 psy_cfg.of_node = dev->of_node;
288 psy = devm_power_supply_register(dev, &mm8013_desc, &psy_cfg);
295 static const struct i2c_device_id mm8013_id_table[] = {
299 MODULE_DEVICE_TABLE(i2c, mm8013_id_table);
301 static const struct of_device_id mm8013_match_table[] = {
302 { .compatible = "mitsumi,mm8013" },
306 static struct i2c_driver mm8013_i2c_driver = {
307 .probe = mm8013_probe,
308 .id_table = mm8013_id_table,
311 .of_match_table = mm8013_match_table,
314 module_i2c_driver(mm8013_i2c_driver);
316 MODULE_DESCRIPTION("MM8013 fuel gauge driver");
317 MODULE_LICENSE("GPL");