1 // SPDX-License-Identifier: GPL-2.0
4 // fuel-gauge systems for lithium-ion (Li+) batteries
6 // Copyright (C) 2009 Samsung Electronics
7 // Minkyu Kang <mk7.kang@samsung.com>
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/regmap.h>
20 #include <linux/slab.h>
22 #define MAX17040_VCELL 0x02
23 #define MAX17040_SOC 0x04
24 #define MAX17040_MODE 0x06
25 #define MAX17040_VER 0x08
26 #define MAX17040_CONFIG 0x0C
27 #define MAX17040_STATUS 0x1A
28 #define MAX17040_CMD 0xFE
31 #define MAX17040_DELAY 1000
32 #define MAX17040_BATTERY_FULL 95
33 #define MAX17040_RCOMP_DEFAULT 0x9700
35 #define MAX17040_ATHD_MASK 0x3f
36 #define MAX17040_ALSC_MASK 0x40
37 #define MAX17040_ATHD_DEFAULT_POWER_UP 4
38 #define MAX17040_STATUS_HD_MASK 0x1000
39 #define MAX17040_STATUS_SC_MASK 0x2000
40 #define MAX17040_CFG_RCOMP_MASK 0xff00
53 /* values that differ by chip_id */
64 static struct chip_data max17040_family[] = {
70 .has_low_soc_alert = 0,
79 .has_low_soc_alert = 0,
88 .has_low_soc_alert = 1,
97 .has_low_soc_alert = 1,
106 .has_low_soc_alert = 1,
115 .has_low_soc_alert = 1,
124 .has_low_soc_alert = 1,
133 .has_low_soc_alert = 1,
139 struct max17040_chip {
140 struct i2c_client *client;
141 struct regmap *regmap;
142 struct delayed_work work;
143 struct power_supply *battery;
144 struct chip_data data;
146 /* battery capacity */
148 /* Low alert threshold from 32% to 1% of the State of Charge */
150 /* some devices return twice the capacity */
151 bool quirk_double_soc;
152 /* higher 8 bits for 17043+, 16 bits for 17040,41 */
156 static int max17040_reset(struct max17040_chip *chip)
158 return regmap_write(chip->regmap, MAX17040_CMD, chip->data.reset_val);
161 static int max17040_set_low_soc_alert(struct max17040_chip *chip, u32 level)
163 level = 32 - level * (chip->quirk_double_soc ? 2 : 1);
164 return regmap_update_bits(chip->regmap, MAX17040_CONFIG,
165 MAX17040_ATHD_MASK, level);
168 static int max17040_set_soc_alert(struct max17040_chip *chip, bool enable)
170 return regmap_update_bits(chip->regmap, MAX17040_CONFIG,
171 MAX17040_ALSC_MASK, enable ? MAX17040_ALSC_MASK : 0);
174 static int max17040_set_rcomp(struct max17040_chip *chip, u16 rcomp)
176 u16 mask = chip->data.rcomp_bytes == 2 ?
177 0xffff : MAX17040_CFG_RCOMP_MASK;
179 return regmap_update_bits(chip->regmap, MAX17040_CONFIG, mask, rcomp);
182 static int max17040_raw_vcell_to_uvolts(struct max17040_chip *chip, u16 vcell)
184 struct chip_data *d = &chip->data;
186 return (vcell >> d->vcell_shift) * d->vcell_mul / d->vcell_div;
190 static int max17040_get_vcell(struct max17040_chip *chip)
194 regmap_read(chip->regmap, MAX17040_VCELL, &vcell);
196 return max17040_raw_vcell_to_uvolts(chip, vcell);
199 static int max17040_get_soc(struct max17040_chip *chip)
203 regmap_read(chip->regmap, MAX17040_SOC, &soc);
205 return soc >> (chip->quirk_double_soc ? 9 : 8);
208 static int max17040_get_version(struct max17040_chip *chip)
213 ret = regmap_read(chip->regmap, MAX17040_VER, &version);
215 return ret ? ret : version;
218 static int max17040_get_online(struct max17040_chip *chip)
223 static int max17040_get_of_data(struct max17040_chip *chip)
225 struct device *dev = &chip->client->dev;
226 struct chip_data *data = &max17040_family[
227 (uintptr_t) of_device_get_match_data(dev)];
231 chip->quirk_double_soc = device_property_read_bool(dev,
234 chip->low_soc_alert = MAX17040_ATHD_DEFAULT_POWER_UP;
235 device_property_read_u32(dev,
236 "maxim,alert-low-soc-level",
237 &chip->low_soc_alert);
239 if (chip->low_soc_alert <= 0 ||
240 chip->low_soc_alert > (chip->quirk_double_soc ? 16 : 32)) {
241 dev_err(dev, "maxim,alert-low-soc-level out of bounds\n");
245 rcomp_len = device_property_count_u8(dev, "maxim,rcomp");
246 chip->rcomp = MAX17040_RCOMP_DEFAULT;
247 if (rcomp_len == data->rcomp_bytes) {
248 if (!device_property_read_u8_array(dev, "maxim,rcomp",
250 chip->rcomp = rcomp_len == 2 ? rcomp[0] << 8 | rcomp[1] :
252 } else if (rcomp_len > 0) {
253 dev_err(dev, "maxim,rcomp has incorrect length\n");
260 static void max17040_check_changes(struct max17040_chip *chip)
262 chip->soc = max17040_get_soc(chip);
265 static void max17040_queue_work(struct max17040_chip *chip)
267 queue_delayed_work(system_power_efficient_wq, &chip->work,
271 static void max17040_stop_work(void *data)
273 struct max17040_chip *chip = data;
275 cancel_delayed_work_sync(&chip->work);
278 static void max17040_work(struct work_struct *work)
280 struct max17040_chip *chip;
283 chip = container_of(work, struct max17040_chip, work.work);
285 /* store SOC to check changes */
286 last_soc = chip->soc;
287 max17040_check_changes(chip);
289 /* check changes and send uevent */
290 if (last_soc != chip->soc)
291 power_supply_changed(chip->battery);
293 max17040_queue_work(chip);
296 /* Returns true if alert cause was SOC change, not low SOC */
297 static bool max17040_handle_soc_alert(struct max17040_chip *chip)
302 regmap_read(chip->regmap, MAX17040_STATUS, &data);
304 if (data & MAX17040_STATUS_HD_MASK) {
305 // this alert was caused by low soc
308 if (data & MAX17040_STATUS_SC_MASK) {
309 // soc change bit -- deassert to mark as handled
310 regmap_write(chip->regmap, MAX17040_STATUS,
311 data & ~MAX17040_STATUS_SC_MASK);
317 static irqreturn_t max17040_thread_handler(int id, void *dev)
319 struct max17040_chip *chip = dev;
321 if (!(chip->data.has_soc_alert && max17040_handle_soc_alert(chip)))
322 dev_warn(&chip->client->dev, "IRQ: Alert battery low level\n");
325 max17040_check_changes(chip);
328 power_supply_changed(chip->battery);
330 /* reset alert bit */
331 max17040_set_low_soc_alert(chip, chip->low_soc_alert);
336 static int max17040_enable_alert_irq(struct max17040_chip *chip)
338 struct i2c_client *client = chip->client;
341 ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
342 max17040_thread_handler, IRQF_ONESHOT,
343 chip->battery->desc->name, chip);
348 static int max17040_prop_writeable(struct power_supply *psy,
349 enum power_supply_property psp)
352 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
359 static int max17040_set_property(struct power_supply *psy,
360 enum power_supply_property psp,
361 const union power_supply_propval *val)
363 struct max17040_chip *chip = power_supply_get_drvdata(psy);
367 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
368 /* alert threshold can be programmed from 1% up to 16/32% */
369 if ((val->intval < 1) ||
370 (val->intval > (chip->quirk_double_soc ? 16 : 32))) {
374 ret = max17040_set_low_soc_alert(chip, val->intval);
375 chip->low_soc_alert = val->intval;
384 static int max17040_get_property(struct power_supply *psy,
385 enum power_supply_property psp,
386 union power_supply_propval *val)
388 struct max17040_chip *chip = power_supply_get_drvdata(psy);
391 case POWER_SUPPLY_PROP_ONLINE:
392 val->intval = max17040_get_online(chip);
394 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
395 val->intval = max17040_get_vcell(chip);
397 case POWER_SUPPLY_PROP_CAPACITY:
398 val->intval = max17040_get_soc(chip);
400 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
401 val->intval = chip->low_soc_alert;
409 static const struct regmap_config max17040_regmap = {
413 .val_format_endian = REGMAP_ENDIAN_BIG,
416 static enum power_supply_property max17040_battery_props[] = {
417 POWER_SUPPLY_PROP_ONLINE,
418 POWER_SUPPLY_PROP_VOLTAGE_NOW,
419 POWER_SUPPLY_PROP_CAPACITY,
420 POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN,
423 static const struct power_supply_desc max17040_battery_desc = {
425 .type = POWER_SUPPLY_TYPE_BATTERY,
426 .get_property = max17040_get_property,
427 .set_property = max17040_set_property,
428 .property_is_writeable = max17040_prop_writeable,
429 .properties = max17040_battery_props,
430 .num_properties = ARRAY_SIZE(max17040_battery_props),
433 static int max17040_probe(struct i2c_client *client)
435 const struct i2c_device_id *id = i2c_client_get_device_id(client);
436 struct i2c_adapter *adapter = client->adapter;
437 struct power_supply_config psy_cfg = {};
438 struct max17040_chip *chip;
439 enum chip_id chip_id;
440 bool enable_irq = false;
443 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
446 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
450 chip->client = client;
451 chip->regmap = devm_regmap_init_i2c(client, &max17040_regmap);
452 if (IS_ERR(chip->regmap))
453 return PTR_ERR(chip->regmap);
454 chip_id = (enum chip_id) id->driver_data;
455 if (client->dev.of_node) {
456 ret = max17040_get_of_data(chip);
459 chip_id = (uintptr_t)of_device_get_match_data(&client->dev);
461 chip->data = max17040_family[chip_id];
463 i2c_set_clientdata(client, chip);
464 psy_cfg.drv_data = chip;
466 chip->battery = devm_power_supply_register(&client->dev,
467 &max17040_battery_desc, &psy_cfg);
468 if (IS_ERR(chip->battery)) {
469 dev_err(&client->dev, "failed: power supply register\n");
470 return PTR_ERR(chip->battery);
473 ret = max17040_get_version(chip);
476 dev_dbg(&chip->client->dev, "MAX17040 Fuel-Gauge Ver 0x%x\n", ret);
478 if (chip_id == ID_MAX17040 || chip_id == ID_MAX17041)
479 max17040_reset(chip);
481 max17040_set_rcomp(chip, chip->rcomp);
483 /* check interrupt */
484 if (client->irq && chip->data.has_low_soc_alert) {
485 ret = max17040_set_low_soc_alert(chip, chip->low_soc_alert);
487 dev_err(&client->dev,
488 "Failed to set low SOC alert: err %d\n", ret);
495 if (client->irq && chip->data.has_soc_alert) {
496 ret = max17040_set_soc_alert(chip, 1);
498 dev_err(&client->dev,
499 "Failed to set SOC alert: err %d\n", ret);
504 /* soc alerts negate the need for polling */
505 INIT_DEFERRABLE_WORK(&chip->work, max17040_work);
506 ret = devm_add_action(&client->dev, max17040_stop_work, chip);
509 max17040_queue_work(chip);
513 ret = max17040_enable_alert_irq(chip);
516 dev_warn(&client->dev,
517 "Failed to get IRQ err %d\n", ret);
524 #ifdef CONFIG_PM_SLEEP
526 static int max17040_suspend(struct device *dev)
528 struct i2c_client *client = to_i2c_client(dev);
529 struct max17040_chip *chip = i2c_get_clientdata(client);
531 if (client->irq && chip->data.has_soc_alert)
532 // disable soc alert to prevent wakeup
533 max17040_set_soc_alert(chip, 0);
535 cancel_delayed_work(&chip->work);
537 if (client->irq && device_may_wakeup(dev))
538 enable_irq_wake(client->irq);
543 static int max17040_resume(struct device *dev)
545 struct i2c_client *client = to_i2c_client(dev);
546 struct max17040_chip *chip = i2c_get_clientdata(client);
548 if (client->irq && device_may_wakeup(dev))
549 disable_irq_wake(client->irq);
551 if (client->irq && chip->data.has_soc_alert)
552 max17040_set_soc_alert(chip, 1);
554 max17040_queue_work(chip);
559 static SIMPLE_DEV_PM_OPS(max17040_pm_ops, max17040_suspend, max17040_resume);
560 #define MAX17040_PM_OPS (&max17040_pm_ops)
564 #define MAX17040_PM_OPS NULL
566 #endif /* CONFIG_PM_SLEEP */
568 static const struct i2c_device_id max17040_id[] = {
569 { "max17040", ID_MAX17040 },
570 { "max17041", ID_MAX17041 },
571 { "max17043", ID_MAX17043 },
572 { "max77836-battery", ID_MAX17043 },
573 { "max17044", ID_MAX17044 },
574 { "max17048", ID_MAX17048 },
575 { "max17049", ID_MAX17049 },
576 { "max17058", ID_MAX17058 },
577 { "max17059", ID_MAX17059 },
580 MODULE_DEVICE_TABLE(i2c, max17040_id);
582 static const struct of_device_id max17040_of_match[] = {
583 { .compatible = "maxim,max17040", .data = (void *) ID_MAX17040 },
584 { .compatible = "maxim,max17041", .data = (void *) ID_MAX17041 },
585 { .compatible = "maxim,max17043", .data = (void *) ID_MAX17043 },
586 { .compatible = "maxim,max77836-battery", .data = (void *) ID_MAX17043 },
587 { .compatible = "maxim,max17044", .data = (void *) ID_MAX17044 },
588 { .compatible = "maxim,max17048", .data = (void *) ID_MAX17048 },
589 { .compatible = "maxim,max17049", .data = (void *) ID_MAX17049 },
590 { .compatible = "maxim,max17058", .data = (void *) ID_MAX17058 },
591 { .compatible = "maxim,max17059", .data = (void *) ID_MAX17059 },
594 MODULE_DEVICE_TABLE(of, max17040_of_match);
596 static struct i2c_driver max17040_i2c_driver = {
599 .of_match_table = max17040_of_match,
600 .pm = MAX17040_PM_OPS,
602 .probe_new = max17040_probe,
603 .id_table = max17040_id,
605 module_i2c_driver(max17040_i2c_driver);
607 MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
608 MODULE_DESCRIPTION("MAX17040 Fuel Gauge");
609 MODULE_LICENSE("GPL");