Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / power / supply / max17040_battery.c
blobd956c67d515586eef043625f1e7dc96807da58f3
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>
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>
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
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,
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;
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,
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,
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,
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,
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,
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,
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,
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,
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;
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;
160 static int max17040_reset(struct max17040_chip *chip)
162 return regmap_write(chip->regmap, MAX17040_CMD, chip->data.reset_val);
165 static int max17040_set_low_soc_alert(struct max17040_chip *chip, u32 level)
167 level = 32 - level * (chip->quirk_double_soc ? 2 : 1);
168 return regmap_update_bits(chip->regmap, MAX17040_CONFIG,
169 MAX17040_ATHD_MASK, level);
172 static int max17040_set_soc_alert(struct max17040_chip *chip, bool enable)
174 return regmap_update_bits(chip->regmap, MAX17040_CONFIG,
175 MAX17040_ALSC_MASK, enable ? MAX17040_ALSC_MASK : 0);
178 static int max17040_set_rcomp(struct max17040_chip *chip, u16 rcomp)
180 u16 mask = chip->data.rcomp_bytes == 2 ?
181 0xffff : MAX17040_CFG_RCOMP_MASK;
183 return regmap_update_bits(chip->regmap, MAX17040_CONFIG, mask, rcomp);
186 static int max17040_raw_vcell_to_uvolts(struct max17040_chip *chip, u16 vcell)
188 struct chip_data *d = &chip->data;
190 return (vcell >> d->vcell_shift) * d->vcell_mul / d->vcell_div;
194 static int max17040_get_vcell(struct max17040_chip *chip)
196 u32 vcell;
198 regmap_read(chip->regmap, MAX17040_VCELL, &vcell);
200 return max17040_raw_vcell_to_uvolts(chip, vcell);
203 static int max17040_get_soc(struct max17040_chip *chip)
205 u32 soc;
207 regmap_read(chip->regmap, MAX17040_SOC, &soc);
209 return soc >> (chip->quirk_double_soc ? 9 : 8);
212 static int max17040_get_version(struct max17040_chip *chip)
214 int ret;
215 u32 version;
217 ret = regmap_read(chip->regmap, MAX17040_VER, &version);
219 return ret ? ret : version;
222 static int max17040_get_online(struct max17040_chip *chip)
224 return chip->pdata && chip->pdata->battery_online ?
225 chip->pdata->battery_online() : 1;
228 static int max17040_get_status(struct max17040_chip *chip)
230 if (!chip->pdata || !chip->pdata->charger_online
231 || !chip->pdata->charger_enable)
232 return POWER_SUPPLY_STATUS_UNKNOWN;
234 if (max17040_get_soc(chip) > MAX17040_BATTERY_FULL)
235 return POWER_SUPPLY_STATUS_FULL;
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;
246 static int max17040_get_of_data(struct max17040_chip *chip)
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];
254 chip->quirk_double_soc = device_property_read_bool(dev,
255 "maxim,double-soc");
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);
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;
268 rcomp_len = device_property_count_u8(dev, "maxim,rcomp");
269 chip->rcomp = MAX17040_RCOMP_DEFAULT;
270 if (rcomp_len == data->rcomp_bytes) {
271 device_property_read_u8_array(dev, "maxim,rcomp",
272 rcomp, rcomp_len);
273 chip->rcomp = rcomp_len == 2 ?
274 rcomp[0] << 8 | rcomp[1] :
275 rcomp[0] << 8;
276 } else if (rcomp_len > 0) {
277 dev_err(dev, "maxim,rcomp has incorrect length\n");
278 return -EINVAL;
281 return 0;
284 static void max17040_check_changes(struct max17040_chip *chip)
286 chip->soc = max17040_get_soc(chip);
287 chip->status = max17040_get_status(chip);
290 static void max17040_queue_work(struct max17040_chip *chip)
292 queue_delayed_work(system_power_efficient_wq, &chip->work,
293 MAX17040_DELAY);
296 static void max17040_stop_work(void *data)
298 struct max17040_chip *chip = data;
300 cancel_delayed_work_sync(&chip->work);
303 static void max17040_work(struct work_struct *work)
305 struct max17040_chip *chip;
306 int last_soc, last_status;
308 chip = container_of(work, struct max17040_chip, work.work);
310 /* store SOC and status to check changes */
311 last_soc = chip->soc;
312 last_status = chip->status;
313 max17040_check_changes(chip);
315 /* check changes and send uevent */
316 if (last_soc != chip->soc || last_status != chip->status)
317 power_supply_changed(chip->battery);
319 max17040_queue_work(chip);
322 /* Returns true if alert cause was SOC change, not low SOC */
323 static bool max17040_handle_soc_alert(struct max17040_chip *chip)
325 bool ret = true;
326 u32 data;
328 regmap_read(chip->regmap, MAX17040_STATUS, &data);
330 if (data & MAX17040_STATUS_HD_MASK) {
331 // this alert was caused by low soc
332 ret = false;
334 if (data & MAX17040_STATUS_SC_MASK) {
335 // soc change bit -- deassert to mark as handled
336 regmap_write(chip->regmap, MAX17040_STATUS,
337 data & ~MAX17040_STATUS_SC_MASK);
340 return ret;
343 static irqreturn_t max17040_thread_handler(int id, void *dev)
345 struct max17040_chip *chip = dev;
347 if (!(chip->data.has_soc_alert && max17040_handle_soc_alert(chip)))
348 dev_warn(&chip->client->dev, "IRQ: Alert battery low level\n");
350 /* read registers */
351 max17040_check_changes(chip);
353 /* send uevent */
354 power_supply_changed(chip->battery);
356 /* reset alert bit */
357 max17040_set_low_soc_alert(chip, chip->low_soc_alert);
359 return IRQ_HANDLED;
362 static int max17040_enable_alert_irq(struct max17040_chip *chip)
364 struct i2c_client *client = chip->client;
365 unsigned int flags;
366 int ret;
368 flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
369 ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
370 max17040_thread_handler, flags,
371 chip->battery->desc->name, chip);
373 return ret;
376 static int max17040_prop_writeable(struct power_supply *psy,
377 enum power_supply_property psp)
379 switch (psp) {
380 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
381 return 1;
382 default:
383 return 0;
387 static int max17040_set_property(struct power_supply *psy,
388 enum power_supply_property psp,
389 const union power_supply_propval *val)
391 struct max17040_chip *chip = power_supply_get_drvdata(psy);
392 int ret;
394 switch (psp) {
395 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
396 /* alert threshold can be programmed from 1% up to 16/32% */
397 if ((val->intval < 1) ||
398 (val->intval > (chip->quirk_double_soc ? 16 : 32))) {
399 ret = -EINVAL;
400 break;
402 ret = max17040_set_low_soc_alert(chip, val->intval);
403 chip->low_soc_alert = val->intval;
404 break;
405 default:
406 ret = -EINVAL;
409 return ret;
412 static int max17040_get_property(struct power_supply *psy,
413 enum power_supply_property psp,
414 union power_supply_propval *val)
416 struct max17040_chip *chip = power_supply_get_drvdata(psy);
418 switch (psp) {
419 case POWER_SUPPLY_PROP_STATUS:
420 val->intval = max17040_get_status(chip);
421 break;
422 case POWER_SUPPLY_PROP_ONLINE:
423 val->intval = max17040_get_online(chip);
424 break;
425 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
426 val->intval = max17040_get_vcell(chip);
427 break;
428 case POWER_SUPPLY_PROP_CAPACITY:
429 val->intval = max17040_get_soc(chip);
430 break;
431 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
432 val->intval = chip->low_soc_alert;
433 break;
434 default:
435 return -EINVAL;
437 return 0;
440 static const struct regmap_config max17040_regmap = {
441 .reg_bits = 8,
442 .reg_stride = 2,
443 .val_bits = 16,
444 .val_format_endian = REGMAP_ENDIAN_BIG,
447 static enum power_supply_property max17040_battery_props[] = {
448 POWER_SUPPLY_PROP_STATUS,
449 POWER_SUPPLY_PROP_ONLINE,
450 POWER_SUPPLY_PROP_VOLTAGE_NOW,
451 POWER_SUPPLY_PROP_CAPACITY,
452 POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN,
455 static const struct power_supply_desc max17040_battery_desc = {
456 .name = "battery",
457 .type = POWER_SUPPLY_TYPE_BATTERY,
458 .get_property = max17040_get_property,
459 .set_property = max17040_set_property,
460 .property_is_writeable = max17040_prop_writeable,
461 .properties = max17040_battery_props,
462 .num_properties = ARRAY_SIZE(max17040_battery_props),
465 static int max17040_probe(struct i2c_client *client,
466 const struct i2c_device_id *id)
468 struct i2c_adapter *adapter = client->adapter;
469 struct power_supply_config psy_cfg = {};
470 struct max17040_chip *chip;
471 enum chip_id chip_id;
472 bool enable_irq = false;
473 int ret;
475 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
476 return -EIO;
478 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
479 if (!chip)
480 return -ENOMEM;
482 chip->client = client;
483 chip->regmap = devm_regmap_init_i2c(client, &max17040_regmap);
484 chip->pdata = client->dev.platform_data;
485 chip_id = (enum chip_id) id->driver_data;
486 if (client->dev.of_node) {
487 ret = max17040_get_of_data(chip);
488 if (ret)
489 return ret;
490 chip_id = (enum chip_id) (uintptr_t)
491 of_device_get_match_data(&client->dev);
493 chip->data = max17040_family[chip_id];
495 i2c_set_clientdata(client, chip);
496 psy_cfg.drv_data = chip;
498 chip->battery = devm_power_supply_register(&client->dev,
499 &max17040_battery_desc, &psy_cfg);
500 if (IS_ERR(chip->battery)) {
501 dev_err(&client->dev, "failed: power supply register\n");
502 return PTR_ERR(chip->battery);
505 ret = max17040_get_version(chip);
506 if (ret < 0)
507 return ret;
508 dev_dbg(&chip->client->dev, "MAX17040 Fuel-Gauge Ver 0x%x\n", ret);
510 if (chip_id == ID_MAX17040 || chip_id == ID_MAX17041)
511 max17040_reset(chip);
513 max17040_set_rcomp(chip, chip->rcomp);
515 /* check interrupt */
516 if (client->irq && chip->data.has_low_soc_alert) {
517 ret = max17040_set_low_soc_alert(chip, chip->low_soc_alert);
518 if (ret) {
519 dev_err(&client->dev,
520 "Failed to set low SOC alert: err %d\n", ret);
521 return ret;
524 enable_irq = true;
527 if (client->irq && chip->data.has_soc_alert) {
528 ret = max17040_set_soc_alert(chip, 1);
529 if (ret) {
530 dev_err(&client->dev,
531 "Failed to set SOC alert: err %d\n", ret);
532 return ret;
534 enable_irq = true;
535 } else {
536 /* soc alerts negate the need for polling */
537 INIT_DEFERRABLE_WORK(&chip->work, max17040_work);
538 ret = devm_add_action(&client->dev, max17040_stop_work, chip);
539 if (ret)
540 return ret;
541 max17040_queue_work(chip);
544 if (enable_irq) {
545 ret = max17040_enable_alert_irq(chip);
546 if (ret) {
547 client->irq = 0;
548 dev_warn(&client->dev,
549 "Failed to get IRQ err %d\n", ret);
553 return 0;
556 #ifdef CONFIG_PM_SLEEP
558 static int max17040_suspend(struct device *dev)
560 struct i2c_client *client = to_i2c_client(dev);
561 struct max17040_chip *chip = i2c_get_clientdata(client);
563 if (client->irq && chip->data.has_soc_alert)
564 // disable soc alert to prevent wakeup
565 max17040_set_soc_alert(chip, 0);
566 else
567 cancel_delayed_work(&chip->work);
569 if (client->irq && device_may_wakeup(dev))
570 enable_irq_wake(client->irq);
572 return 0;
575 static int max17040_resume(struct device *dev)
577 struct i2c_client *client = to_i2c_client(dev);
578 struct max17040_chip *chip = i2c_get_clientdata(client);
580 if (client->irq && device_may_wakeup(dev))
581 disable_irq_wake(client->irq);
583 if (client->irq && chip->data.has_soc_alert)
584 max17040_set_soc_alert(chip, 1);
585 else
586 max17040_queue_work(chip);
588 return 0;
591 static SIMPLE_DEV_PM_OPS(max17040_pm_ops, max17040_suspend, max17040_resume);
592 #define MAX17040_PM_OPS (&max17040_pm_ops)
594 #else
596 #define MAX17040_PM_OPS NULL
598 #endif /* CONFIG_PM_SLEEP */
600 static const struct i2c_device_id max17040_id[] = {
601 { "max17040", ID_MAX17040 },
602 { "max17041", ID_MAX17041 },
603 { "max17043", ID_MAX17043 },
604 { "max77836-battery", ID_MAX17043 },
605 { "max17044", ID_MAX17044 },
606 { "max17048", ID_MAX17048 },
607 { "max17049", ID_MAX17049 },
608 { "max17058", ID_MAX17058 },
609 { "max17059", ID_MAX17059 },
610 { /* sentinel */ }
612 MODULE_DEVICE_TABLE(i2c, max17040_id);
614 static const struct of_device_id max17040_of_match[] = {
615 { .compatible = "maxim,max17040", .data = (void *) ID_MAX17040 },
616 { .compatible = "maxim,max17041", .data = (void *) ID_MAX17041 },
617 { .compatible = "maxim,max17043", .data = (void *) ID_MAX17043 },
618 { .compatible = "maxim,max77836-battery", .data = (void *) ID_MAX17043 },
619 { .compatible = "maxim,max17044", .data = (void *) ID_MAX17044 },
620 { .compatible = "maxim,max17048", .data = (void *) ID_MAX17048 },
621 { .compatible = "maxim,max17049", .data = (void *) ID_MAX17049 },
622 { .compatible = "maxim,max17058", .data = (void *) ID_MAX17058 },
623 { .compatible = "maxim,max17059", .data = (void *) ID_MAX17059 },
624 { /* sentinel */ },
626 MODULE_DEVICE_TABLE(of, max17040_of_match);
628 static struct i2c_driver max17040_i2c_driver = {
629 .driver = {
630 .name = "max17040",
631 .of_match_table = max17040_of_match,
632 .pm = MAX17040_PM_OPS,
634 .probe = max17040_probe,
635 .id_table = max17040_id,
637 module_i2c_driver(max17040_i2c_driver);
639 MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
640 MODULE_DESCRIPTION("MAX17040 Fuel Gauge");
641 MODULE_LICENSE("GPL");