xref: /linux/drivers/power/supply/max1720x_battery.c (revision 8e1bb4a41aa78d6105e59186af3dcd545fc66e70)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Fuel gauge driver for Maxim 17201/17205
4  *
5  * based on max1721x_battery.c
6  *
7  * Copyright (C) 2024 Liebherr-Electronics and Drives GmbH
8  */
9 
10 #include <linux/bitfield.h>
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/power_supply.h>
14 #include <linux/regmap.h>
15 
16 #include <asm/unaligned.h>
17 
18 /* Nonvolatile registers */
19 #define MAX1720X_NRSENSE		0xCF	/* RSense in 10^-5 Ohm */
20 
21 /* ModelGauge m5 */
22 #define MAX172XX_STATUS			0x00	/* Status */
23 #define MAX172XX_STATUS_BAT_ABSENT	BIT(3)	/* Battery absent */
24 #define MAX172XX_REPCAP			0x05	/* Average capacity */
25 #define MAX172XX_REPSOC			0x06	/* Percentage of charge */
26 #define MAX172XX_TEMP			0x08	/* Temperature */
27 #define MAX172XX_CURRENT		0x0A	/* Actual current */
28 #define MAX172XX_AVG_CURRENT		0x0B	/* Average current */
29 #define MAX172XX_TTE			0x11	/* Time to empty */
30 #define MAX172XX_AVG_TA			0x16	/* Average temperature */
31 #define MAX172XX_CYCLES			0x17
32 #define MAX172XX_DESIGN_CAP		0x18	/* Design capacity */
33 #define MAX172XX_AVG_VCELL		0x19
34 #define MAX172XX_TTF			0x20	/* Time to full */
35 #define MAX172XX_DEV_NAME		0x21	/* Device name */
36 #define MAX172XX_DEV_NAME_TYPE_MASK	GENMASK(3, 0)
37 #define MAX172XX_DEV_NAME_TYPE_MAX17201	BIT(0)
38 #define MAX172XX_DEV_NAME_TYPE_MAX17205	(BIT(0) | BIT(2))
39 #define MAX172XX_QR_TABLE10		0x22
40 #define MAX172XX_BATT			0xDA	/* Battery voltage */
41 #define MAX172XX_ATAVCAP		0xDF
42 
43 static const char *const max1720x_manufacturer = "Maxim Integrated";
44 static const char *const max17201_model = "MAX17201";
45 static const char *const max17205_model = "MAX17205";
46 
47 struct max1720x_device_info {
48 	struct regmap *regmap;
49 	int rsense;
50 };
51 
52 /*
53  * Model Gauge M5 Algorithm output register
54  * Volatile data (must not be cached)
55  */
56 static const struct regmap_range max1720x_volatile_allow[] = {
57 	regmap_reg_range(MAX172XX_STATUS, MAX172XX_CYCLES),
58 	regmap_reg_range(MAX172XX_AVG_VCELL, MAX172XX_TTF),
59 	regmap_reg_range(MAX172XX_QR_TABLE10, MAX172XX_ATAVCAP),
60 };
61 
62 static const struct regmap_range max1720x_readable_allow[] = {
63 	regmap_reg_range(MAX172XX_STATUS, MAX172XX_ATAVCAP),
64 };
65 
66 static const struct regmap_range max1720x_readable_deny[] = {
67 	/* unused registers */
68 	regmap_reg_range(0x24, 0x26),
69 	regmap_reg_range(0x30, 0x31),
70 	regmap_reg_range(0x33, 0x34),
71 	regmap_reg_range(0x37, 0x37),
72 	regmap_reg_range(0x3B, 0x3C),
73 	regmap_reg_range(0x40, 0x41),
74 	regmap_reg_range(0x43, 0x44),
75 	regmap_reg_range(0x47, 0x49),
76 	regmap_reg_range(0x4B, 0x4C),
77 	regmap_reg_range(0x4E, 0xAF),
78 	regmap_reg_range(0xB1, 0xB3),
79 	regmap_reg_range(0xB5, 0xB7),
80 	regmap_reg_range(0xBF, 0xD0),
81 	regmap_reg_range(0xDB, 0xDB),
82 	regmap_reg_range(0xE0, 0xFF),
83 };
84 
85 static const struct regmap_access_table max1720x_readable_regs = {
86 	.yes_ranges	= max1720x_readable_allow,
87 	.n_yes_ranges	= ARRAY_SIZE(max1720x_readable_allow),
88 	.no_ranges	= max1720x_readable_deny,
89 	.n_no_ranges	= ARRAY_SIZE(max1720x_readable_deny),
90 };
91 
92 static const struct regmap_access_table max1720x_volatile_regs = {
93 	.yes_ranges	= max1720x_volatile_allow,
94 	.n_yes_ranges	= ARRAY_SIZE(max1720x_volatile_allow),
95 	.no_ranges	= max1720x_readable_deny,
96 	.n_no_ranges	= ARRAY_SIZE(max1720x_readable_deny),
97 };
98 
99 static const struct regmap_config max1720x_regmap_cfg = {
100 	.reg_bits = 8,
101 	.val_bits = 16,
102 	.max_register = MAX172XX_ATAVCAP,
103 	.val_format_endian = REGMAP_ENDIAN_LITTLE,
104 	.rd_table = &max1720x_readable_regs,
105 	.volatile_table = &max1720x_volatile_regs,
106 	.cache_type = REGCACHE_RBTREE,
107 };
108 
109 static const enum power_supply_property max1720x_battery_props[] = {
110 	POWER_SUPPLY_PROP_PRESENT,
111 	POWER_SUPPLY_PROP_CAPACITY,
112 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
113 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
114 	POWER_SUPPLY_PROP_CHARGE_AVG,
115 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
116 	POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
117 	POWER_SUPPLY_PROP_TEMP,
118 	POWER_SUPPLY_PROP_CURRENT_NOW,
119 	POWER_SUPPLY_PROP_CURRENT_AVG,
120 	POWER_SUPPLY_PROP_MODEL_NAME,
121 	POWER_SUPPLY_PROP_MANUFACTURER,
122 };
123 
124 /* Convert regs value to power_supply units */
125 
126 static int max172xx_time_to_ps(unsigned int reg)
127 {
128 	return reg * 5625 / 1000;	/* in sec. */
129 }
130 
131 static int max172xx_percent_to_ps(unsigned int reg)
132 {
133 	return reg / 256;	/* in percent from 0 to 100 */
134 }
135 
136 static int max172xx_voltage_to_ps(unsigned int reg)
137 {
138 	return reg * 1250;	/* in uV */
139 }
140 
141 static int max172xx_capacity_to_ps(unsigned int reg)
142 {
143 	return reg * 500;	/* in uAh */
144 }
145 
146 /*
147  * Current and temperature is signed values, so unsigned regs
148  * value must be converted to signed type
149  */
150 
151 static int max172xx_temperature_to_ps(unsigned int reg)
152 {
153 	int val = (int16_t)reg;
154 
155 	return val * 10 / 256; /* in tenths of deg. C */
156 }
157 
158 /*
159  * Calculating current registers resolution:
160  *
161  * RSense stored in 10^-5 Ohm, so mesaurment voltage must be
162  * in 10^-11 Volts for get current in uA.
163  * 16 bit current reg fullscale +/-51.2mV is 102400 uV.
164  * So: 102400 / 65535 * 10^5 = 156252
165  */
166 static int max172xx_current_to_voltage(unsigned int reg)
167 {
168 	int val = (int16_t)reg;
169 
170 	return val * 156252;
171 }
172 
173 static int max1720x_battery_get_property(struct power_supply *psy,
174 					 enum power_supply_property psp,
175 					 union power_supply_propval *val)
176 {
177 	struct max1720x_device_info *info = power_supply_get_drvdata(psy);
178 	unsigned int reg_val;
179 	int ret = 0;
180 
181 	switch (psp) {
182 	case POWER_SUPPLY_PROP_PRESENT:
183 		/*
184 		 * POWER_SUPPLY_PROP_PRESENT will always readable via
185 		 * sysfs interface. Value return 0 if battery not
186 		 * present or unaccesable via I2c.
187 		 */
188 		ret = regmap_read(info->regmap, MAX172XX_STATUS, &reg_val);
189 		if (ret < 0) {
190 			val->intval = 0;
191 			return 0;
192 		}
193 
194 		val->intval = !FIELD_GET(MAX172XX_STATUS_BAT_ABSENT, reg_val);
195 		break;
196 	case POWER_SUPPLY_PROP_CAPACITY:
197 		ret = regmap_read(info->regmap, MAX172XX_REPSOC, &reg_val);
198 		val->intval = max172xx_percent_to_ps(reg_val);
199 		break;
200 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
201 		ret = regmap_read(info->regmap, MAX172XX_BATT, &reg_val);
202 		val->intval = max172xx_voltage_to_ps(reg_val);
203 		break;
204 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
205 		ret = regmap_read(info->regmap, MAX172XX_DESIGN_CAP, &reg_val);
206 		val->intval = max172xx_capacity_to_ps(reg_val);
207 		break;
208 	case POWER_SUPPLY_PROP_CHARGE_AVG:
209 		ret = regmap_read(info->regmap, MAX172XX_REPCAP, &reg_val);
210 		val->intval = max172xx_capacity_to_ps(reg_val);
211 		break;
212 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
213 		ret = regmap_read(info->regmap, MAX172XX_TTE, &reg_val);
214 		val->intval = max172xx_time_to_ps(reg_val);
215 		break;
216 	case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
217 		ret = regmap_read(info->regmap, MAX172XX_TTF, &reg_val);
218 		val->intval = max172xx_time_to_ps(reg_val);
219 		break;
220 	case POWER_SUPPLY_PROP_TEMP:
221 		ret = regmap_read(info->regmap, MAX172XX_TEMP, &reg_val);
222 		val->intval = max172xx_temperature_to_ps(reg_val);
223 		break;
224 	case POWER_SUPPLY_PROP_CURRENT_NOW:
225 		ret = regmap_read(info->regmap, MAX172XX_CURRENT, &reg_val);
226 		val->intval = max172xx_current_to_voltage(reg_val) / info->rsense;
227 		break;
228 	case POWER_SUPPLY_PROP_CURRENT_AVG:
229 		ret = regmap_read(info->regmap, MAX172XX_AVG_CURRENT, &reg_val);
230 		val->intval = max172xx_current_to_voltage(reg_val) / info->rsense;
231 		break;
232 	case POWER_SUPPLY_PROP_MODEL_NAME:
233 		ret = regmap_read(info->regmap, MAX172XX_DEV_NAME, &reg_val);
234 		reg_val = FIELD_GET(MAX172XX_DEV_NAME_TYPE_MASK, reg_val);
235 		if (reg_val == MAX172XX_DEV_NAME_TYPE_MAX17201)
236 			val->strval = max17201_model;
237 		else if (reg_val == MAX172XX_DEV_NAME_TYPE_MAX17205)
238 			val->strval = max17205_model;
239 		else
240 			return -ENODEV;
241 		break;
242 	case POWER_SUPPLY_PROP_MANUFACTURER:
243 		val->strval = max1720x_manufacturer;
244 		break;
245 	default:
246 		return -EINVAL;
247 	}
248 
249 	return ret;
250 }
251 
252 static int max1720x_probe_sense_resistor(struct i2c_client *client,
253 					 struct max1720x_device_info *info)
254 {
255 	struct device *dev = &client->dev;
256 	struct i2c_client *ancillary;
257 	int ret;
258 
259 	ancillary = i2c_new_ancillary_device(client, "nvmem", 0xb);
260 	if (IS_ERR(ancillary)) {
261 		dev_err(dev, "Failed to initialize ancillary i2c device\n");
262 		return PTR_ERR(ancillary);
263 	}
264 
265 	ret = i2c_smbus_read_word_data(ancillary, MAX1720X_NRSENSE);
266 	i2c_unregister_device(ancillary);
267 	if (ret < 0)
268 		return ret;
269 
270 	info->rsense = ret;
271 	if (!info->rsense) {
272 		dev_warn(dev, "RSense not calibrated, set 10 mOhms!\n");
273 		info->rsense = 1000; /* in regs in 10^-5 */
274 	}
275 
276 	return 0;
277 }
278 
279 static const struct power_supply_desc max1720x_bat_desc = {
280 	.name = "max1720x",
281 	.no_thermal = true,
282 	.type = POWER_SUPPLY_TYPE_BATTERY,
283 	.properties = max1720x_battery_props,
284 	.num_properties = ARRAY_SIZE(max1720x_battery_props),
285 	.get_property = max1720x_battery_get_property,
286 };
287 
288 static int max1720x_probe(struct i2c_client *client)
289 {
290 	struct power_supply_config psy_cfg = {};
291 	struct device *dev = &client->dev;
292 	struct max1720x_device_info *info;
293 	struct power_supply *bat;
294 	int ret;
295 
296 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
297 	if (!info)
298 		return -ENOMEM;
299 
300 	psy_cfg.drv_data = info;
301 	psy_cfg.fwnode = dev_fwnode(dev);
302 	info->regmap = devm_regmap_init_i2c(client, &max1720x_regmap_cfg);
303 	if (IS_ERR(info->regmap))
304 		return dev_err_probe(dev, PTR_ERR(info->regmap),
305 				     "regmap initialization failed\n");
306 
307 	ret = max1720x_probe_sense_resistor(client, info);
308 	if (ret)
309 		return dev_err_probe(dev, ret,
310 				     "Failed to read sense resistor value\n");
311 
312 	bat = devm_power_supply_register(dev, &max1720x_bat_desc, &psy_cfg);
313 	if (IS_ERR(bat))
314 		return dev_err_probe(dev, PTR_ERR(bat),
315 				     "Failed to register power supply\n");
316 
317 	return 0;
318 }
319 
320 static const struct of_device_id max1720x_of_match[] = {
321 	{ .compatible = "maxim,max17201" },
322 	{}
323 };
324 MODULE_DEVICE_TABLE(of, max1720x_of_match);
325 
326 static struct i2c_driver max1720x_i2c_driver = {
327 	.driver = {
328 		.name = "max1720x",
329 		.of_match_table = max1720x_of_match,
330 	},
331 	.probe = max1720x_probe,
332 };
333 module_i2c_driver(max1720x_i2c_driver);
334 
335 MODULE_LICENSE("GPL");
336 MODULE_AUTHOR("Dimitri Fedrau <dima.fedrau@gmail.com>");
337 MODULE_DESCRIPTION("Maxim MAX17201/MAX17205 Fuel Gauge IC driver");
338