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_FULL
,
75 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
76 POWER_SUPPLY_PROP_CHARGE_NOW
,
77 POWER_SUPPLY_PROP_CURRENT_MAX
,
78 POWER_SUPPLY_PROP_CURRENT_NOW
,
79 POWER_SUPPLY_PROP_CYCLE_COUNT
,
80 POWER_SUPPLY_PROP_HEALTH
,
81 POWER_SUPPLY_PROP_PRESENT
,
82 POWER_SUPPLY_PROP_STATUS
,
83 POWER_SUPPLY_PROP_TEMP
,
84 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
,
85 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG
,
86 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
89 static int mm8013_get_property(struct power_supply
*psy
,
90 enum power_supply_property psp
,
91 union power_supply_propval
*val
)
93 struct mm8013_chip
*chip
= psy
->drv_data
;
98 case POWER_SUPPLY_PROP_CAPACITY
:
99 ret
= regmap_read(chip
->regmap
, REG_STATE_OF_CHARGE
, ®val
);
103 val
->intval
= regval
;
105 case POWER_SUPPLY_PROP_CHARGE_FULL
:
106 ret
= regmap_read(chip
->regmap
, REG_FULL_CHARGE_CAPACITY
, ®val
);
110 val
->intval
= 1000 * regval
;
112 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
113 ret
= regmap_read(chip
->regmap
, REG_DESIGN_CAPACITY
, ®val
);
117 val
->intval
= 1000 * regval
;
119 case POWER_SUPPLY_PROP_CHARGE_NOW
:
120 ret
= regmap_read(chip
->regmap
, REG_NOMINAL_CHARGE_CAPACITY
, ®val
);
124 val
->intval
= 1000 * regval
;
126 case POWER_SUPPLY_PROP_CURRENT_MAX
:
127 ret
= regmap_read(chip
->regmap
, REG_MAX_LOAD_CURRENT
, ®val
);
131 val
->intval
= -1000 * (s16
)regval
;
133 case POWER_SUPPLY_PROP_CURRENT_NOW
:
134 ret
= regmap_read(chip
->regmap
, REG_AVERAGE_CURRENT
, ®val
);
138 val
->intval
= -1000 * (s16
)regval
;
140 case POWER_SUPPLY_PROP_CYCLE_COUNT
:
141 ret
= regmap_read(chip
->regmap
, REG_CYCLE_COUNT
, ®val
);
145 val
->intval
= regval
;
147 case POWER_SUPPLY_PROP_HEALTH
:
148 ret
= regmap_read(chip
->regmap
, REG_FLAGS
, ®val
);
152 if (regval
& MM8013_FLAG_UT
)
153 val
->intval
= POWER_SUPPLY_HEALTH_COLD
;
154 else if (regval
& (MM8013_FLAG_ODC
| MM8013_FLAG_OCC
))
155 val
->intval
= POWER_SUPPLY_HEALTH_OVERCURRENT
;
156 else if (regval
& (MM8013_FLAG_BATLOW
))
157 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
158 else if (regval
& MM8013_FLAG_BATHI
)
159 val
->intval
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
160 else if (regval
& (MM8013_FLAG_OT
| MM8013_FLAG_OTD
| MM8013_FLAG_OTC
))
161 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
163 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
165 case POWER_SUPPLY_PROP_PRESENT
:
166 ret
= regmap_read(chip
->regmap
, REG_TEMPERATURE
, ®val
);
170 val
->intval
= ((s16
)regval
> 0);
172 case POWER_SUPPLY_PROP_STATUS
:
173 ret
= regmap_read(chip
->regmap
, REG_FLAGS
, ®val
);
177 if (regval
& MM8013_FLAG_DSG
)
178 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
179 else if (regval
& MM8013_FLAG_CHG_INH
)
180 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
181 else if (regval
& MM8013_FLAG_CHG
)
182 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
183 else if (regval
& MM8013_FLAG_FC
)
184 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
186 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
188 case POWER_SUPPLY_PROP_TEMP
:
189 ret
= regmap_read(chip
->regmap
, REG_TEMPERATURE
, ®val
);
193 val
->intval
= DECIKELVIN_TO_DECIDEGC(regval
);
195 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
:
196 ret
= regmap_read(chip
->regmap
, REG_AVERAGE_TIME_TO_EMPTY
, ®val
);
200 /* The estimation is not yet ready */
201 if (regval
== U16_MAX
)
204 val
->intval
= regval
;
206 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG
:
207 ret
= regmap_read(chip
->regmap
, REG_AVERAGE_TIME_TO_FULL
, ®val
);
211 /* The estimation is not yet ready */
212 if (regval
== U16_MAX
)
215 val
->intval
= regval
;
217 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
218 ret
= regmap_read(chip
->regmap
, REG_VOLTAGE
, ®val
);
222 val
->intval
= 1000 * regval
;
231 static const struct power_supply_desc mm8013_desc
= {
233 .type
= POWER_SUPPLY_TYPE_BATTERY
,
234 .properties
= mm8013_battery_props
,
235 .num_properties
= ARRAY_SIZE(mm8013_battery_props
),
236 .get_property
= mm8013_get_property
,
239 static const struct regmap_config mm8013_regmap_config
= {
242 .max_register
= 0x68,
243 .use_single_read
= true,
244 .use_single_write
= true,
245 .val_format_endian
= REGMAP_ENDIAN_LITTLE
,
248 static int mm8013_probe(struct i2c_client
*client
)
250 struct power_supply_config psy_cfg
= {};
251 struct device
*dev
= &client
->dev
;
252 struct power_supply
*psy
;
253 struct mm8013_chip
*chip
;
256 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_WORD_DATA
))
257 return dev_err_probe(dev
, -EIO
,
258 "I2C_FUNC_SMBUS_WORD_DATA not supported\n");
260 chip
= devm_kzalloc(dev
, sizeof(struct mm8013_chip
), GFP_KERNEL
);
264 chip
->client
= client
;
266 chip
->regmap
= devm_regmap_init_i2c(client
, &mm8013_regmap_config
);
267 if (IS_ERR(chip
->regmap
)) {
268 ret
= PTR_ERR(chip
->regmap
);
269 return dev_err_probe(dev
, ret
, "Couldn't initialize regmap\n");
272 ret
= mm8013_checkdevice(chip
);
274 return dev_err_probe(dev
, ret
, "MM8013 not found\n");
276 psy_cfg
.drv_data
= chip
;
277 psy_cfg
.of_node
= dev
->of_node
;
279 psy
= devm_power_supply_register(dev
, &mm8013_desc
, &psy_cfg
);
286 static const struct i2c_device_id mm8013_id_table
[] = {
290 MODULE_DEVICE_TABLE(i2c
, mm8013_id_table
);
292 static const struct of_device_id mm8013_match_table
[] = {
293 { .compatible
= "mitsumi,mm8013" },
297 static struct i2c_driver mm8013_i2c_driver
= {
298 .probe
= mm8013_probe
,
299 .id_table
= mm8013_id_table
,
302 .of_match_table
= mm8013_match_table
,
305 module_i2c_driver(mm8013_i2c_driver
);
307 MODULE_DESCRIPTION("MM8013 fuel gauge driver");
308 MODULE_LICENSE("GPL");