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>
19 #include <linux/regmap.h>
20 #include <linux/slab.h>
21 #include <linux/iio/consumer.h>
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
32 #define MAX17040_DELAY 1000
33 #define MAX17040_BATTERY_FULL 95
34 #define MAX17040_RCOMP_DEFAULT 0x9700
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
54 /* values that differ by chip_id */
65 static struct chip_data max17040_family
[] = {
71 .has_low_soc_alert
= 0,
80 .has_low_soc_alert
= 0,
89 .has_low_soc_alert
= 1,
98 .has_low_soc_alert
= 1,
107 .has_low_soc_alert
= 1,
116 .has_low_soc_alert
= 1,
125 .has_low_soc_alert
= 1,
134 .has_low_soc_alert
= 1,
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 chip_data data
;
146 struct iio_channel
*channel_temp
;
148 /* battery capacity */
150 /* Low alert threshold from 32% to 1% of the State of Charge */
152 /* some devices return twice the capacity */
153 bool quirk_double_soc
;
154 /* higher 8 bits for 17043+, 16 bits for 17040,41 */
158 static int max17040_reset(struct max17040_chip
*chip
)
160 return regmap_write(chip
->regmap
, MAX17040_CMD
, chip
->data
.reset_val
);
163 static int max17040_set_low_soc_alert(struct max17040_chip
*chip
, u32 level
)
165 level
= 32 - level
* (chip
->quirk_double_soc
? 2 : 1);
166 return regmap_update_bits(chip
->regmap
, MAX17040_CONFIG
,
167 MAX17040_ATHD_MASK
, level
);
170 static int max17040_set_soc_alert(struct max17040_chip
*chip
, bool enable
)
172 return regmap_update_bits(chip
->regmap
, MAX17040_CONFIG
,
173 MAX17040_ALSC_MASK
, enable
? MAX17040_ALSC_MASK
: 0);
176 static int max17040_set_rcomp(struct max17040_chip
*chip
, u16 rcomp
)
178 u16 mask
= chip
->data
.rcomp_bytes
== 2 ?
179 0xffff : MAX17040_CFG_RCOMP_MASK
;
181 return regmap_update_bits(chip
->regmap
, MAX17040_CONFIG
, mask
, rcomp
);
184 static int max17040_raw_vcell_to_uvolts(struct max17040_chip
*chip
, u16 vcell
)
186 struct chip_data
*d
= &chip
->data
;
188 return (vcell
>> d
->vcell_shift
) * d
->vcell_mul
/ d
->vcell_div
;
192 static int max17040_get_vcell(struct max17040_chip
*chip
)
196 regmap_read(chip
->regmap
, MAX17040_VCELL
, &vcell
);
198 return max17040_raw_vcell_to_uvolts(chip
, vcell
);
201 static int max17040_get_soc(struct max17040_chip
*chip
)
205 regmap_read(chip
->regmap
, MAX17040_SOC
, &soc
);
207 return soc
>> (chip
->quirk_double_soc
? 9 : 8);
210 static int max17040_get_version(struct max17040_chip
*chip
)
215 ret
= regmap_read(chip
->regmap
, MAX17040_VER
, &version
);
217 return ret
? ret
: version
;
220 static int max17040_get_online(struct max17040_chip
*chip
)
225 static int max17040_get_of_data(struct max17040_chip
*chip
)
227 struct device
*dev
= &chip
->client
->dev
;
228 struct chip_data
*data
= &max17040_family
[
229 (uintptr_t) of_device_get_match_data(dev
)];
233 chip
->quirk_double_soc
= device_property_read_bool(dev
,
236 chip
->low_soc_alert
= MAX17040_ATHD_DEFAULT_POWER_UP
;
237 device_property_read_u32(dev
,
238 "maxim,alert-low-soc-level",
239 &chip
->low_soc_alert
);
241 if (chip
->low_soc_alert
<= 0 ||
242 chip
->low_soc_alert
> (chip
->quirk_double_soc
? 16 : 32)) {
243 dev_err(dev
, "maxim,alert-low-soc-level out of bounds\n");
247 rcomp_len
= device_property_count_u8(dev
, "maxim,rcomp");
248 chip
->rcomp
= MAX17040_RCOMP_DEFAULT
;
249 if (rcomp_len
== data
->rcomp_bytes
) {
250 if (!device_property_read_u8_array(dev
, "maxim,rcomp",
252 chip
->rcomp
= rcomp_len
== 2 ? rcomp
[0] << 8 | rcomp
[1] :
254 } else if (rcomp_len
> 0) {
255 dev_err(dev
, "maxim,rcomp has incorrect length\n");
262 static void max17040_check_changes(struct max17040_chip
*chip
)
264 chip
->soc
= max17040_get_soc(chip
);
267 static void max17040_queue_work(struct max17040_chip
*chip
)
269 queue_delayed_work(system_power_efficient_wq
, &chip
->work
,
273 static void max17040_stop_work(void *data
)
275 struct max17040_chip
*chip
= data
;
277 cancel_delayed_work_sync(&chip
->work
);
280 static void max17040_work(struct work_struct
*work
)
282 struct max17040_chip
*chip
;
285 chip
= container_of(work
, struct max17040_chip
, work
.work
);
287 /* store SOC to check changes */
288 last_soc
= chip
->soc
;
289 max17040_check_changes(chip
);
291 /* check changes and send uevent */
292 if (last_soc
!= chip
->soc
)
293 power_supply_changed(chip
->battery
);
295 max17040_queue_work(chip
);
298 /* Returns true if alert cause was SOC change, not low SOC */
299 static bool max17040_handle_soc_alert(struct max17040_chip
*chip
)
304 regmap_read(chip
->regmap
, MAX17040_STATUS
, &data
);
306 if (data
& MAX17040_STATUS_HD_MASK
) {
307 // this alert was caused by low soc
310 if (data
& MAX17040_STATUS_SC_MASK
) {
311 // soc change bit -- deassert to mark as handled
312 regmap_write(chip
->regmap
, MAX17040_STATUS
,
313 data
& ~MAX17040_STATUS_SC_MASK
);
319 static irqreturn_t
max17040_thread_handler(int id
, void *dev
)
321 struct max17040_chip
*chip
= dev
;
323 if (!(chip
->data
.has_soc_alert
&& max17040_handle_soc_alert(chip
)))
324 dev_warn(&chip
->client
->dev
, "IRQ: Alert battery low level\n");
327 max17040_check_changes(chip
);
330 power_supply_changed(chip
->battery
);
332 /* reset alert bit */
333 max17040_set_low_soc_alert(chip
, chip
->low_soc_alert
);
338 static int max17040_enable_alert_irq(struct max17040_chip
*chip
)
340 struct i2c_client
*client
= chip
->client
;
343 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
, NULL
,
344 max17040_thread_handler
, IRQF_ONESHOT
,
345 chip
->battery
->desc
->name
, chip
);
350 static int max17040_prop_writeable(struct power_supply
*psy
,
351 enum power_supply_property psp
)
354 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN
:
361 static int max17040_set_property(struct power_supply
*psy
,
362 enum power_supply_property psp
,
363 const union power_supply_propval
*val
)
365 struct max17040_chip
*chip
= power_supply_get_drvdata(psy
);
369 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN
:
370 /* alert threshold can be programmed from 1% up to 16/32% */
371 if ((val
->intval
< 1) ||
372 (val
->intval
> (chip
->quirk_double_soc
? 16 : 32))) {
376 ret
= max17040_set_low_soc_alert(chip
, val
->intval
);
377 chip
->low_soc_alert
= val
->intval
;
386 static int max17040_get_property(struct power_supply
*psy
,
387 enum power_supply_property psp
,
388 union power_supply_propval
*val
)
390 struct max17040_chip
*chip
= power_supply_get_drvdata(psy
);
393 case POWER_SUPPLY_PROP_ONLINE
:
394 case POWER_SUPPLY_PROP_PRESENT
:
395 val
->intval
= max17040_get_online(chip
);
397 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
398 val
->intval
= max17040_get_vcell(chip
);
400 case POWER_SUPPLY_PROP_CAPACITY
:
401 val
->intval
= max17040_get_soc(chip
);
403 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN
:
404 val
->intval
= chip
->low_soc_alert
;
406 case POWER_SUPPLY_PROP_STATUS
:
407 power_supply_get_property_from_supplier(psy
, psp
, val
);
409 case POWER_SUPPLY_PROP_TEMP
:
410 if (!chip
->channel_temp
)
413 iio_read_channel_processed_scale(chip
->channel_temp
,
422 static const struct regmap_config max17040_regmap
= {
426 .val_format_endian
= REGMAP_ENDIAN_BIG
,
429 static enum power_supply_property max17040_battery_props
[] = {
430 POWER_SUPPLY_PROP_ONLINE
,
431 POWER_SUPPLY_PROP_PRESENT
,
432 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
433 POWER_SUPPLY_PROP_CAPACITY
,
434 POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN
,
435 POWER_SUPPLY_PROP_STATUS
,
436 POWER_SUPPLY_PROP_TEMP
,
439 static const struct power_supply_desc max17040_battery_desc
= {
441 .type
= POWER_SUPPLY_TYPE_BATTERY
,
442 .get_property
= max17040_get_property
,
443 .set_property
= max17040_set_property
,
444 .property_is_writeable
= max17040_prop_writeable
,
445 .properties
= max17040_battery_props
,
446 .num_properties
= ARRAY_SIZE(max17040_battery_props
),
449 static int max17040_probe(struct i2c_client
*client
)
451 const struct i2c_device_id
*id
= i2c_client_get_device_id(client
);
452 struct i2c_adapter
*adapter
= client
->adapter
;
453 struct power_supply_config psy_cfg
= {};
454 struct max17040_chip
*chip
;
455 enum chip_id chip_id
;
456 bool enable_irq
= false;
459 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE
))
462 chip
= devm_kzalloc(&client
->dev
, sizeof(*chip
), GFP_KERNEL
);
466 chip
->client
= client
;
467 chip
->regmap
= devm_regmap_init_i2c(client
, &max17040_regmap
);
468 if (IS_ERR(chip
->regmap
))
469 return PTR_ERR(chip
->regmap
);
470 chip_id
= (enum chip_id
) id
->driver_data
;
471 if (client
->dev
.of_node
) {
472 ret
= max17040_get_of_data(chip
);
475 chip_id
= (uintptr_t)of_device_get_match_data(&client
->dev
);
477 chip
->data
= max17040_family
[chip_id
];
479 i2c_set_clientdata(client
, chip
);
480 psy_cfg
.drv_data
= chip
;
482 /* Switch to devm_iio_channel_get_optional when available */
483 chip
->channel_temp
= devm_iio_channel_get(&client
->dev
, "temp");
484 if (IS_ERR(chip
->channel_temp
)) {
485 ret
= PTR_ERR(chip
->channel_temp
);
487 return dev_err_probe(&client
->dev
, PTR_ERR(chip
->channel_temp
),
488 "failed to get temp\n");
490 chip
->channel_temp
= NULL
;
493 chip
->battery
= devm_power_supply_register(&client
->dev
,
494 &max17040_battery_desc
, &psy_cfg
);
495 if (IS_ERR(chip
->battery
)) {
496 dev_err(&client
->dev
, "failed: power supply register\n");
497 return PTR_ERR(chip
->battery
);
500 ret
= max17040_get_version(chip
);
503 dev_dbg(&chip
->client
->dev
, "MAX17040 Fuel-Gauge Ver 0x%x\n", ret
);
505 if (chip_id
== ID_MAX17040
|| chip_id
== ID_MAX17041
)
506 max17040_reset(chip
);
508 max17040_set_rcomp(chip
, chip
->rcomp
);
510 /* check interrupt */
511 if (client
->irq
&& chip
->data
.has_low_soc_alert
) {
512 ret
= max17040_set_low_soc_alert(chip
, chip
->low_soc_alert
);
514 dev_err(&client
->dev
,
515 "Failed to set low SOC alert: err %d\n", ret
);
522 if (client
->irq
&& chip
->data
.has_soc_alert
) {
523 ret
= max17040_set_soc_alert(chip
, 1);
525 dev_err(&client
->dev
,
526 "Failed to set SOC alert: err %d\n", ret
);
531 /* soc alerts negate the need for polling */
532 INIT_DEFERRABLE_WORK(&chip
->work
, max17040_work
);
533 ret
= devm_add_action(&client
->dev
, max17040_stop_work
, chip
);
536 max17040_queue_work(chip
);
540 ret
= max17040_enable_alert_irq(chip
);
543 dev_warn(&client
->dev
,
544 "Failed to get IRQ err %d\n", ret
);
551 #ifdef CONFIG_PM_SLEEP
553 static int max17040_suspend(struct device
*dev
)
555 struct i2c_client
*client
= to_i2c_client(dev
);
556 struct max17040_chip
*chip
= i2c_get_clientdata(client
);
558 if (client
->irq
&& chip
->data
.has_soc_alert
)
559 // disable soc alert to prevent wakeup
560 max17040_set_soc_alert(chip
, 0);
562 cancel_delayed_work(&chip
->work
);
564 if (client
->irq
&& device_may_wakeup(dev
))
565 enable_irq_wake(client
->irq
);
570 static int max17040_resume(struct device
*dev
)
572 struct i2c_client
*client
= to_i2c_client(dev
);
573 struct max17040_chip
*chip
= i2c_get_clientdata(client
);
575 if (client
->irq
&& device_may_wakeup(dev
))
576 disable_irq_wake(client
->irq
);
578 if (client
->irq
&& chip
->data
.has_soc_alert
)
579 max17040_set_soc_alert(chip
, 1);
581 max17040_queue_work(chip
);
586 static SIMPLE_DEV_PM_OPS(max17040_pm_ops
, max17040_suspend
, max17040_resume
);
587 #define MAX17040_PM_OPS (&max17040_pm_ops)
591 #define MAX17040_PM_OPS NULL
593 #endif /* CONFIG_PM_SLEEP */
595 static const struct i2c_device_id max17040_id
[] = {
596 { "max17040", ID_MAX17040
},
597 { "max17041", ID_MAX17041
},
598 { "max17043", ID_MAX17043
},
599 { "max77836-battery", ID_MAX17043
},
600 { "max17044", ID_MAX17044
},
601 { "max17048", ID_MAX17048
},
602 { "max17049", ID_MAX17049
},
603 { "max17058", ID_MAX17058
},
604 { "max17059", ID_MAX17059
},
607 MODULE_DEVICE_TABLE(i2c
, max17040_id
);
609 static const struct of_device_id max17040_of_match
[] = {
610 { .compatible
= "maxim,max17040", .data
= (void *) ID_MAX17040
},
611 { .compatible
= "maxim,max17041", .data
= (void *) ID_MAX17041
},
612 { .compatible
= "maxim,max17043", .data
= (void *) ID_MAX17043
},
613 { .compatible
= "maxim,max77836-battery", .data
= (void *) ID_MAX17043
},
614 { .compatible
= "maxim,max17044", .data
= (void *) ID_MAX17044
},
615 { .compatible
= "maxim,max17048", .data
= (void *) ID_MAX17048
},
616 { .compatible
= "maxim,max17049", .data
= (void *) ID_MAX17049
},
617 { .compatible
= "maxim,max17058", .data
= (void *) ID_MAX17058
},
618 { .compatible
= "maxim,max17059", .data
= (void *) ID_MAX17059
},
621 MODULE_DEVICE_TABLE(of
, max17040_of_match
);
623 static struct i2c_driver max17040_i2c_driver
= {
626 .of_match_table
= max17040_of_match
,
627 .pm
= MAX17040_PM_OPS
,
629 .probe
= max17040_probe
,
630 .id_table
= max17040_id
,
632 module_i2c_driver(max17040_i2c_driver
);
634 MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
635 MODULE_DESCRIPTION("MAX17040 Fuel Gauge");
636 MODULE_LICENSE("GPL");