1 // SPDX-License-Identifier: GPL-2.0
2 //
3 //  max17040_battery.c
4 //  fuel-gauge systems for lithium-ion (Li+) batteries
5 //
6 //  Copyright (C) 2009 Samsung Electronics
7 //  Minkyu Kang <mk7.kang@samsung.com>
8 
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/platform_device.h>
12 #include <linux/mutex.h>
13 #include <linux/err.h>
14 #include <linux/i2c.h>
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
17 #include <linux/power_supply.h>
18 #include <linux/of_device.h>
19 #include <linux/max17040_battery.h>
20 #include <linux/regmap.h>
21 #include <linux/slab.h>
22 
23 #define MAX17040_VCELL	0x02
24 #define MAX17040_SOC	0x04
25 #define MAX17040_MODE	0x06
26 #define MAX17040_VER	0x08
27 #define MAX17040_CONFIG	0x0C
28 #define MAX17040_STATUS	0x1A
29 #define MAX17040_CMD	0xFE
30 
31 
32 #define MAX17040_DELAY		1000
33 #define MAX17040_BATTERY_FULL	95
34 #define MAX17040_RCOMP_DEFAULT  0x9700
35 
36 #define MAX17040_ATHD_MASK		0x3f
37 #define MAX17040_ALSC_MASK		0x40
38 #define MAX17040_ATHD_DEFAULT_POWER_UP	4
39 #define MAX17040_STATUS_HD_MASK		0x1000
40 #define MAX17040_STATUS_SC_MASK		0x2000
41 #define MAX17040_CFG_RCOMP_MASK		0xff00
42 
43 enum chip_id {
44 	ID_MAX17040,
45 	ID_MAX17041,
46 	ID_MAX17043,
47 	ID_MAX17044,
48 	ID_MAX17048,
49 	ID_MAX17049,
50 	ID_MAX17058,
51 	ID_MAX17059,
52 };
53 
54 /* values that differ by chip_id */
55 struct chip_data {
56 	u16 reset_val;
57 	u16 vcell_shift;
58 	u16 vcell_mul;
59 	u16 vcell_div;
60 	u8  has_low_soc_alert;
61 	u8  rcomp_bytes;
62 	u8  has_soc_alert;
63 };
64 
65 static struct chip_data max17040_family[] = {
66 	[ID_MAX17040] = {
67 		.reset_val = 0x0054,
68 		.vcell_shift = 4,
69 		.vcell_mul = 1250,
70 		.vcell_div = 1,
71 		.has_low_soc_alert = 0,
72 		.rcomp_bytes = 2,
73 		.has_soc_alert = 0,
74 	},
75 	[ID_MAX17041] = {
76 		.reset_val = 0x0054,
77 		.vcell_shift = 4,
78 		.vcell_mul = 2500,
79 		.vcell_div = 1,
80 		.has_low_soc_alert = 0,
81 		.rcomp_bytes = 2,
82 		.has_soc_alert = 0,
83 	},
84 	[ID_MAX17043] = {
85 		.reset_val = 0x0054,
86 		.vcell_shift = 4,
87 		.vcell_mul = 1250,
88 		.vcell_div = 1,
89 		.has_low_soc_alert = 1,
90 		.rcomp_bytes = 1,
91 		.has_soc_alert = 0,
92 	},
93 	[ID_MAX17044] = {
94 		.reset_val = 0x0054,
95 		.vcell_shift = 4,
96 		.vcell_mul = 2500,
97 		.vcell_div = 1,
98 		.has_low_soc_alert = 1,
99 		.rcomp_bytes = 1,
100 		.has_soc_alert = 0,
101 	},
102 	[ID_MAX17048] = {
103 		.reset_val = 0x5400,
104 		.vcell_shift = 0,
105 		.vcell_mul = 625,
106 		.vcell_div = 8,
107 		.has_low_soc_alert = 1,
108 		.rcomp_bytes = 1,
109 		.has_soc_alert = 1,
110 	},
111 	[ID_MAX17049] = {
112 		.reset_val = 0x5400,
113 		.vcell_shift = 0,
114 		.vcell_mul = 625,
115 		.vcell_div = 4,
116 		.has_low_soc_alert = 1,
117 		.rcomp_bytes = 1,
118 		.has_soc_alert = 1,
119 	},
120 	[ID_MAX17058] = {
121 		.reset_val = 0x5400,
122 		.vcell_shift = 0,
123 		.vcell_mul = 625,
124 		.vcell_div = 8,
125 		.has_low_soc_alert = 1,
126 		.rcomp_bytes = 1,
127 		.has_soc_alert = 0,
128 	},
129 	[ID_MAX17059] = {
130 		.reset_val = 0x5400,
131 		.vcell_shift = 0,
132 		.vcell_mul = 625,
133 		.vcell_div = 4,
134 		.has_low_soc_alert = 1,
135 		.rcomp_bytes = 1,
136 		.has_soc_alert = 0,
137 	},
138 };
139 
140 struct max17040_chip {
141 	struct i2c_client		*client;
142 	struct regmap			*regmap;
143 	struct delayed_work		work;
144 	struct power_supply		*battery;
145 	struct max17040_platform_data	*pdata;
146 	struct chip_data		data;
147 
148 	/* battery capacity */
149 	int soc;
150 	/* State Of Charge */
151 	int status;
152 	/* Low alert threshold from 32% to 1% of the State of Charge */
153 	u32 low_soc_alert;
154 	/* some devices return twice the capacity */
155 	bool quirk_double_soc;
156 	/* higher 8 bits for 17043+, 16 bits for 17040,41 */
157 	u16 rcomp;
158 };
159 
160 static int max17040_reset(struct max17040_chip *chip)
161 {
162 	return regmap_write(chip->regmap, MAX17040_CMD, chip->data.reset_val);
163 }
164 
165 static int max17040_set_low_soc_alert(struct max17040_chip *chip, u32 level)
166 {
167 	level = 32 - level * (chip->quirk_double_soc ? 2 : 1);
168 	return regmap_update_bits(chip->regmap, MAX17040_CONFIG,
169 			MAX17040_ATHD_MASK, level);
170 }
171 
172 static int max17040_set_soc_alert(struct max17040_chip *chip, bool enable)
173 {
174 	return regmap_update_bits(chip->regmap, MAX17040_CONFIG,
175 			MAX17040_ALSC_MASK, enable ? MAX17040_ALSC_MASK : 0);
176 }
177 
178 static int max17040_set_rcomp(struct max17040_chip *chip, u16 rcomp)
179 {
180 	u16 mask = chip->data.rcomp_bytes == 2 ?
181 		0xffff : MAX17040_CFG_RCOMP_MASK;
182 
183 	return regmap_update_bits(chip->regmap, MAX17040_CONFIG, mask, rcomp);
184 }
185 
186 static int max17040_raw_vcell_to_uvolts(struct max17040_chip *chip, u16 vcell)
187 {
188 	struct chip_data *d = &chip->data;
189 
190 	return (vcell >> d->vcell_shift) * d->vcell_mul / d->vcell_div;
191 }
192 
193 
194 static int max17040_get_vcell(struct max17040_chip *chip)
195 {
196 	u32 vcell;
197 
198 	regmap_read(chip->regmap, MAX17040_VCELL, &vcell);
199 
200 	return max17040_raw_vcell_to_uvolts(chip, vcell);
201 }
202 
203 static int max17040_get_soc(struct max17040_chip *chip)
204 {
205 	u32 soc;
206 
207 	regmap_read(chip->regmap, MAX17040_SOC, &soc);
208 
209 	return soc >> (chip->quirk_double_soc ? 9 : 8);
210 }
211 
212 static int max17040_get_version(struct max17040_chip *chip)
213 {
214 	int ret;
215 	u32 version;
216 
217 	ret = regmap_read(chip->regmap, MAX17040_VER, &version);
218 
219 	return ret ? ret : version;
220 }
221 
222 static int max17040_get_online(struct max17040_chip *chip)
223 {
224 	return chip->pdata && chip->pdata->battery_online ?
225 		chip->pdata->battery_online() : 1;
226 }
227 
228 static int max17040_get_status(struct max17040_chip *chip)
229 {
230 	if (!chip->pdata || !chip->pdata->charger_online
231 			|| !chip->pdata->charger_enable)
232 		return POWER_SUPPLY_STATUS_UNKNOWN;
233 
234 	if (max17040_get_soc(chip) > MAX17040_BATTERY_FULL)
235 		return POWER_SUPPLY_STATUS_FULL;
236 
237 	if (chip->pdata->charger_online())
238 		if (chip->pdata->charger_enable())
239 			return POWER_SUPPLY_STATUS_CHARGING;
240 		else
241 			return POWER_SUPPLY_STATUS_NOT_CHARGING;
242 	else
243 		return POWER_SUPPLY_STATUS_DISCHARGING;
244 }
245 
246 static int max17040_get_of_data(struct max17040_chip *chip)
247 {
248 	struct device *dev = &chip->client->dev;
249 	struct chip_data *data = &max17040_family[
250 		(uintptr_t) of_device_get_match_data(dev)];
251 	int rcomp_len;
252 	u8 rcomp[2];
253 
254 	chip->quirk_double_soc = device_property_read_bool(dev,
255 							   "maxim,double-soc");
256 
257 	chip->low_soc_alert = MAX17040_ATHD_DEFAULT_POWER_UP;
258 	device_property_read_u32(dev,
259 				 "maxim,alert-low-soc-level",
260 				 &chip->low_soc_alert);
261 
262 	if (chip->low_soc_alert <= 0 ||
263 	    chip->low_soc_alert > (chip->quirk_double_soc ? 16 : 32)) {
264 		dev_err(dev, "maxim,alert-low-soc-level out of bounds\n");
265 		return -EINVAL;
266 	}
267 
268 	rcomp_len = device_property_count_u8(dev, "maxim,rcomp");
269 	chip->rcomp = MAX17040_RCOMP_DEFAULT;
270 	if (rcomp_len == data->rcomp_bytes) {
271 		if (!device_property_read_u8_array(dev, "maxim,rcomp",
272 						   rcomp, rcomp_len))
273 			chip->rcomp = rcomp_len == 2 ? rcomp[0] << 8 | rcomp[1] :
274 				      rcomp[0] << 8;
275 	} else if (rcomp_len > 0) {
276 		dev_err(dev, "maxim,rcomp has incorrect length\n");
277 		return -EINVAL;
278 	}
279 
280 	return 0;
281 }
282 
283 static void max17040_check_changes(struct max17040_chip *chip)
284 {
285 	chip->soc = max17040_get_soc(chip);
286 	chip->status = max17040_get_status(chip);
287 }
288 
289 static void max17040_queue_work(struct max17040_chip *chip)
290 {
291 	queue_delayed_work(system_power_efficient_wq, &chip->work,
292 			   MAX17040_DELAY);
293 }
294 
295 static void max17040_stop_work(void *data)
296 {
297 	struct max17040_chip *chip = data;
298 
299 	cancel_delayed_work_sync(&chip->work);
300 }
301 
302 static void max17040_work(struct work_struct *work)
303 {
304 	struct max17040_chip *chip;
305 	int last_soc, last_status;
306 
307 	chip = container_of(work, struct max17040_chip, work.work);
308 
309 	/* store SOC and status to check changes */
310 	last_soc = chip->soc;
311 	last_status = chip->status;
312 	max17040_check_changes(chip);
313 
314 	/* check changes and send uevent */
315 	if (last_soc != chip->soc || last_status != chip->status)
316 		power_supply_changed(chip->battery);
317 
318 	max17040_queue_work(chip);
319 }
320 
321 /* Returns true if alert cause was SOC change, not low SOC */
322 static bool max17040_handle_soc_alert(struct max17040_chip *chip)
323 {
324 	bool ret = true;
325 	u32 data;
326 
327 	regmap_read(chip->regmap, MAX17040_STATUS, &data);
328 
329 	if (data & MAX17040_STATUS_HD_MASK) {
330 		// this alert was caused by low soc
331 		ret = false;
332 	}
333 	if (data & MAX17040_STATUS_SC_MASK) {
334 		// soc change bit -- deassert to mark as handled
335 		regmap_write(chip->regmap, MAX17040_STATUS,
336 				data & ~MAX17040_STATUS_SC_MASK);
337 	}
338 
339 	return ret;
340 }
341 
342 static irqreturn_t max17040_thread_handler(int id, void *dev)
343 {
344 	struct max17040_chip *chip = dev;
345 
346 	if (!(chip->data.has_soc_alert && max17040_handle_soc_alert(chip)))
347 		dev_warn(&chip->client->dev, "IRQ: Alert battery low level\n");
348 
349 	/* read registers */
350 	max17040_check_changes(chip);
351 
352 	/* send uevent */
353 	power_supply_changed(chip->battery);
354 
355 	/* reset alert bit */
356 	max17040_set_low_soc_alert(chip, chip->low_soc_alert);
357 
358 	return IRQ_HANDLED;
359 }
360 
361 static int max17040_enable_alert_irq(struct max17040_chip *chip)
362 {
363 	struct i2c_client *client = chip->client;
364 	unsigned int flags;
365 	int ret;
366 
367 	flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
368 	ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
369 					max17040_thread_handler, flags,
370 					chip->battery->desc->name, chip);
371 
372 	return ret;
373 }
374 
375 static int max17040_prop_writeable(struct power_supply *psy,
376 				   enum power_supply_property psp)
377 {
378 	switch (psp) {
379 	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
380 		return 1;
381 	default:
382 		return 0;
383 	}
384 }
385 
386 static int max17040_set_property(struct power_supply *psy,
387 			    enum power_supply_property psp,
388 			    const union power_supply_propval *val)
389 {
390 	struct max17040_chip *chip = power_supply_get_drvdata(psy);
391 	int ret;
392 
393 	switch (psp) {
394 	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
395 		/* alert threshold can be programmed from 1% up to 16/32% */
396 		if ((val->intval < 1) ||
397 		    (val->intval > (chip->quirk_double_soc ? 16 : 32))) {
398 			ret = -EINVAL;
399 			break;
400 		}
401 		ret = max17040_set_low_soc_alert(chip, val->intval);
402 		chip->low_soc_alert = val->intval;
403 		break;
404 	default:
405 		ret = -EINVAL;
406 	}
407 
408 	return ret;
409 }
410 
411 static int max17040_get_property(struct power_supply *psy,
412 			    enum power_supply_property psp,
413 			    union power_supply_propval *val)
414 {
415 	struct max17040_chip *chip = power_supply_get_drvdata(psy);
416 
417 	switch (psp) {
418 	case POWER_SUPPLY_PROP_STATUS:
419 		val->intval = max17040_get_status(chip);
420 		break;
421 	case POWER_SUPPLY_PROP_ONLINE:
422 		val->intval = max17040_get_online(chip);
423 		break;
424 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
425 		val->intval = max17040_get_vcell(chip);
426 		break;
427 	case POWER_SUPPLY_PROP_CAPACITY:
428 		val->intval = max17040_get_soc(chip);
429 		break;
430 	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
431 		val->intval = chip->low_soc_alert;
432 		break;
433 	default:
434 		return -EINVAL;
435 	}
436 	return 0;
437 }
438 
439 static const struct regmap_config max17040_regmap = {
440 	.reg_bits	= 8,
441 	.reg_stride	= 2,
442 	.val_bits	= 16,
443 	.val_format_endian = REGMAP_ENDIAN_BIG,
444 };
445 
446 static enum power_supply_property max17040_battery_props[] = {
447 	POWER_SUPPLY_PROP_STATUS,
448 	POWER_SUPPLY_PROP_ONLINE,
449 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
450 	POWER_SUPPLY_PROP_CAPACITY,
451 	POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN,
452 };
453 
454 static const struct power_supply_desc max17040_battery_desc = {
455 	.name			= "battery",
456 	.type			= POWER_SUPPLY_TYPE_BATTERY,
457 	.get_property		= max17040_get_property,
458 	.set_property		= max17040_set_property,
459 	.property_is_writeable  = max17040_prop_writeable,
460 	.properties		= max17040_battery_props,
461 	.num_properties		= ARRAY_SIZE(max17040_battery_props),
462 };
463 
464 static int max17040_probe(struct i2c_client *client,
465 			const struct i2c_device_id *id)
466 {
467 	struct i2c_adapter *adapter = client->adapter;
468 	struct power_supply_config psy_cfg = {};
469 	struct max17040_chip *chip;
470 	enum chip_id chip_id;
471 	bool enable_irq = false;
472 	int ret;
473 
474 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
475 		return -EIO;
476 
477 	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
478 	if (!chip)
479 		return -ENOMEM;
480 
481 	chip->client = client;
482 	chip->regmap = devm_regmap_init_i2c(client, &max17040_regmap);
483 	chip->pdata = client->dev.platform_data;
484 	chip_id = (enum chip_id) id->driver_data;
485 	if (client->dev.of_node) {
486 		ret = max17040_get_of_data(chip);
487 		if (ret)
488 			return ret;
489 		chip_id = (uintptr_t)of_device_get_match_data(&client->dev);
490 	}
491 	chip->data = max17040_family[chip_id];
492 
493 	i2c_set_clientdata(client, chip);
494 	psy_cfg.drv_data = chip;
495 
496 	chip->battery = devm_power_supply_register(&client->dev,
497 				&max17040_battery_desc, &psy_cfg);
498 	if (IS_ERR(chip->battery)) {
499 		dev_err(&client->dev, "failed: power supply register\n");
500 		return PTR_ERR(chip->battery);
501 	}
502 
503 	ret = max17040_get_version(chip);
504 	if (ret < 0)
505 		return ret;
506 	dev_dbg(&chip->client->dev, "MAX17040 Fuel-Gauge Ver 0x%x\n", ret);
507 
508 	if (chip_id == ID_MAX17040 || chip_id == ID_MAX17041)
509 		max17040_reset(chip);
510 
511 	max17040_set_rcomp(chip, chip->rcomp);
512 
513 	/* check interrupt */
514 	if (client->irq && chip->data.has_low_soc_alert) {
515 		ret = max17040_set_low_soc_alert(chip, chip->low_soc_alert);
516 		if (ret) {
517 			dev_err(&client->dev,
518 				"Failed to set low SOC alert: err %d\n", ret);
519 			return ret;
520 		}
521 
522 		enable_irq = true;
523 	}
524 
525 	if (client->irq && chip->data.has_soc_alert) {
526 		ret = max17040_set_soc_alert(chip, 1);
527 		if (ret) {
528 			dev_err(&client->dev,
529 				"Failed to set SOC alert: err %d\n", ret);
530 			return ret;
531 		}
532 		enable_irq = true;
533 	} else {
534 		/* soc alerts negate the need for polling */
535 		INIT_DEFERRABLE_WORK(&chip->work, max17040_work);
536 		ret = devm_add_action(&client->dev, max17040_stop_work, chip);
537 		if (ret)
538 			return ret;
539 		max17040_queue_work(chip);
540 	}
541 
542 	if (enable_irq) {
543 		ret = max17040_enable_alert_irq(chip);
544 		if (ret) {
545 			client->irq = 0;
546 			dev_warn(&client->dev,
547 				 "Failed to get IRQ err %d\n", ret);
548 		}
549 	}
550 
551 	return 0;
552 }
553 
554 #ifdef CONFIG_PM_SLEEP
555 
556 static int max17040_suspend(struct device *dev)
557 {
558 	struct i2c_client *client = to_i2c_client(dev);
559 	struct max17040_chip *chip = i2c_get_clientdata(client);
560 
561 	if (client->irq && chip->data.has_soc_alert)
562 		// disable soc alert to prevent wakeup
563 		max17040_set_soc_alert(chip, 0);
564 	else
565 		cancel_delayed_work(&chip->work);
566 
567 	if (client->irq && device_may_wakeup(dev))
568 		enable_irq_wake(client->irq);
569 
570 	return 0;
571 }
572 
573 static int max17040_resume(struct device *dev)
574 {
575 	struct i2c_client *client = to_i2c_client(dev);
576 	struct max17040_chip *chip = i2c_get_clientdata(client);
577 
578 	if (client->irq && device_may_wakeup(dev))
579 		disable_irq_wake(client->irq);
580 
581 	if (client->irq && chip->data.has_soc_alert)
582 		max17040_set_soc_alert(chip, 1);
583 	else
584 		max17040_queue_work(chip);
585 
586 	return 0;
587 }
588 
589 static SIMPLE_DEV_PM_OPS(max17040_pm_ops, max17040_suspend, max17040_resume);
590 #define MAX17040_PM_OPS (&max17040_pm_ops)
591 
592 #else
593 
594 #define MAX17040_PM_OPS NULL
595 
596 #endif /* CONFIG_PM_SLEEP */
597 
598 static const struct i2c_device_id max17040_id[] = {
599 	{ "max17040", ID_MAX17040 },
600 	{ "max17041", ID_MAX17041 },
601 	{ "max17043", ID_MAX17043 },
602 	{ "max77836-battery", ID_MAX17043 },
603 	{ "max17044", ID_MAX17044 },
604 	{ "max17048", ID_MAX17048 },
605 	{ "max17049", ID_MAX17049 },
606 	{ "max17058", ID_MAX17058 },
607 	{ "max17059", ID_MAX17059 },
608 	{ /* sentinel */ }
609 };
610 MODULE_DEVICE_TABLE(i2c, max17040_id);
611 
612 static const struct of_device_id max17040_of_match[] = {
613 	{ .compatible = "maxim,max17040", .data = (void *) ID_MAX17040 },
614 	{ .compatible = "maxim,max17041", .data = (void *) ID_MAX17041 },
615 	{ .compatible = "maxim,max17043", .data = (void *) ID_MAX17043 },
616 	{ .compatible = "maxim,max77836-battery", .data = (void *) ID_MAX17043 },
617 	{ .compatible = "maxim,max17044", .data = (void *) ID_MAX17044 },
618 	{ .compatible = "maxim,max17048", .data = (void *) ID_MAX17048 },
619 	{ .compatible = "maxim,max17049", .data = (void *) ID_MAX17049 },
620 	{ .compatible = "maxim,max17058", .data = (void *) ID_MAX17058 },
621 	{ .compatible = "maxim,max17059", .data = (void *) ID_MAX17059 },
622 	{ /* sentinel */ },
623 };
624 MODULE_DEVICE_TABLE(of, max17040_of_match);
625 
626 static struct i2c_driver max17040_i2c_driver = {
627 	.driver	= {
628 		.name	= "max17040",
629 		.of_match_table = max17040_of_match,
630 		.pm	= MAX17040_PM_OPS,
631 	},
632 	.probe		= max17040_probe,
633 	.id_table	= max17040_id,
634 };
635 module_i2c_driver(max17040_i2c_driver);
636 
637 MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
638 MODULE_DESCRIPTION("MAX17040 Fuel Gauge");
639 MODULE_LICENSE("GPL");
640