0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/module.h>
0010 #include <linux/init.h>
0011 #include <linux/platform_device.h>
0012 #include <linux/mutex.h>
0013 #include <linux/err.h>
0014 #include <linux/i2c.h>
0015 #include <linux/delay.h>
0016 #include <linux/interrupt.h>
0017 #include <linux/power_supply.h>
0018 #include <linux/of_device.h>
0019 #include <linux/regmap.h>
0020 #include <linux/slab.h>
0021
0022 #define MAX17040_VCELL 0x02
0023 #define MAX17040_SOC 0x04
0024 #define MAX17040_MODE 0x06
0025 #define MAX17040_VER 0x08
0026 #define MAX17040_CONFIG 0x0C
0027 #define MAX17040_STATUS 0x1A
0028 #define MAX17040_CMD 0xFE
0029
0030
0031 #define MAX17040_DELAY 1000
0032 #define MAX17040_BATTERY_FULL 95
0033 #define MAX17040_RCOMP_DEFAULT 0x9700
0034
0035 #define MAX17040_ATHD_MASK 0x3f
0036 #define MAX17040_ALSC_MASK 0x40
0037 #define MAX17040_ATHD_DEFAULT_POWER_UP 4
0038 #define MAX17040_STATUS_HD_MASK 0x1000
0039 #define MAX17040_STATUS_SC_MASK 0x2000
0040 #define MAX17040_CFG_RCOMP_MASK 0xff00
0041
0042 enum chip_id {
0043 ID_MAX17040,
0044 ID_MAX17041,
0045 ID_MAX17043,
0046 ID_MAX17044,
0047 ID_MAX17048,
0048 ID_MAX17049,
0049 ID_MAX17058,
0050 ID_MAX17059,
0051 };
0052
0053
0054 struct chip_data {
0055 u16 reset_val;
0056 u16 vcell_shift;
0057 u16 vcell_mul;
0058 u16 vcell_div;
0059 u8 has_low_soc_alert;
0060 u8 rcomp_bytes;
0061 u8 has_soc_alert;
0062 };
0063
0064 static struct chip_data max17040_family[] = {
0065 [ID_MAX17040] = {
0066 .reset_val = 0x0054,
0067 .vcell_shift = 4,
0068 .vcell_mul = 1250,
0069 .vcell_div = 1,
0070 .has_low_soc_alert = 0,
0071 .rcomp_bytes = 2,
0072 .has_soc_alert = 0,
0073 },
0074 [ID_MAX17041] = {
0075 .reset_val = 0x0054,
0076 .vcell_shift = 4,
0077 .vcell_mul = 2500,
0078 .vcell_div = 1,
0079 .has_low_soc_alert = 0,
0080 .rcomp_bytes = 2,
0081 .has_soc_alert = 0,
0082 },
0083 [ID_MAX17043] = {
0084 .reset_val = 0x0054,
0085 .vcell_shift = 4,
0086 .vcell_mul = 1250,
0087 .vcell_div = 1,
0088 .has_low_soc_alert = 1,
0089 .rcomp_bytes = 1,
0090 .has_soc_alert = 0,
0091 },
0092 [ID_MAX17044] = {
0093 .reset_val = 0x0054,
0094 .vcell_shift = 4,
0095 .vcell_mul = 2500,
0096 .vcell_div = 1,
0097 .has_low_soc_alert = 1,
0098 .rcomp_bytes = 1,
0099 .has_soc_alert = 0,
0100 },
0101 [ID_MAX17048] = {
0102 .reset_val = 0x5400,
0103 .vcell_shift = 0,
0104 .vcell_mul = 625,
0105 .vcell_div = 8,
0106 .has_low_soc_alert = 1,
0107 .rcomp_bytes = 1,
0108 .has_soc_alert = 1,
0109 },
0110 [ID_MAX17049] = {
0111 .reset_val = 0x5400,
0112 .vcell_shift = 0,
0113 .vcell_mul = 625,
0114 .vcell_div = 4,
0115 .has_low_soc_alert = 1,
0116 .rcomp_bytes = 1,
0117 .has_soc_alert = 1,
0118 },
0119 [ID_MAX17058] = {
0120 .reset_val = 0x5400,
0121 .vcell_shift = 0,
0122 .vcell_mul = 625,
0123 .vcell_div = 8,
0124 .has_low_soc_alert = 1,
0125 .rcomp_bytes = 1,
0126 .has_soc_alert = 0,
0127 },
0128 [ID_MAX17059] = {
0129 .reset_val = 0x5400,
0130 .vcell_shift = 0,
0131 .vcell_mul = 625,
0132 .vcell_div = 4,
0133 .has_low_soc_alert = 1,
0134 .rcomp_bytes = 1,
0135 .has_soc_alert = 0,
0136 },
0137 };
0138
0139 struct max17040_chip {
0140 struct i2c_client *client;
0141 struct regmap *regmap;
0142 struct delayed_work work;
0143 struct power_supply *battery;
0144 struct chip_data data;
0145
0146
0147 int soc;
0148
0149 u32 low_soc_alert;
0150
0151 bool quirk_double_soc;
0152
0153 u16 rcomp;
0154 };
0155
0156 static int max17040_reset(struct max17040_chip *chip)
0157 {
0158 return regmap_write(chip->regmap, MAX17040_CMD, chip->data.reset_val);
0159 }
0160
0161 static int max17040_set_low_soc_alert(struct max17040_chip *chip, u32 level)
0162 {
0163 level = 32 - level * (chip->quirk_double_soc ? 2 : 1);
0164 return regmap_update_bits(chip->regmap, MAX17040_CONFIG,
0165 MAX17040_ATHD_MASK, level);
0166 }
0167
0168 static int max17040_set_soc_alert(struct max17040_chip *chip, bool enable)
0169 {
0170 return regmap_update_bits(chip->regmap, MAX17040_CONFIG,
0171 MAX17040_ALSC_MASK, enable ? MAX17040_ALSC_MASK : 0);
0172 }
0173
0174 static int max17040_set_rcomp(struct max17040_chip *chip, u16 rcomp)
0175 {
0176 u16 mask = chip->data.rcomp_bytes == 2 ?
0177 0xffff : MAX17040_CFG_RCOMP_MASK;
0178
0179 return regmap_update_bits(chip->regmap, MAX17040_CONFIG, mask, rcomp);
0180 }
0181
0182 static int max17040_raw_vcell_to_uvolts(struct max17040_chip *chip, u16 vcell)
0183 {
0184 struct chip_data *d = &chip->data;
0185
0186 return (vcell >> d->vcell_shift) * d->vcell_mul / d->vcell_div;
0187 }
0188
0189
0190 static int max17040_get_vcell(struct max17040_chip *chip)
0191 {
0192 u32 vcell;
0193
0194 regmap_read(chip->regmap, MAX17040_VCELL, &vcell);
0195
0196 return max17040_raw_vcell_to_uvolts(chip, vcell);
0197 }
0198
0199 static int max17040_get_soc(struct max17040_chip *chip)
0200 {
0201 u32 soc;
0202
0203 regmap_read(chip->regmap, MAX17040_SOC, &soc);
0204
0205 return soc >> (chip->quirk_double_soc ? 9 : 8);
0206 }
0207
0208 static int max17040_get_version(struct max17040_chip *chip)
0209 {
0210 int ret;
0211 u32 version;
0212
0213 ret = regmap_read(chip->regmap, MAX17040_VER, &version);
0214
0215 return ret ? ret : version;
0216 }
0217
0218 static int max17040_get_online(struct max17040_chip *chip)
0219 {
0220 return 1;
0221 }
0222
0223 static int max17040_get_of_data(struct max17040_chip *chip)
0224 {
0225 struct device *dev = &chip->client->dev;
0226 struct chip_data *data = &max17040_family[
0227 (uintptr_t) of_device_get_match_data(dev)];
0228 int rcomp_len;
0229 u8 rcomp[2];
0230
0231 chip->quirk_double_soc = device_property_read_bool(dev,
0232 "maxim,double-soc");
0233
0234 chip->low_soc_alert = MAX17040_ATHD_DEFAULT_POWER_UP;
0235 device_property_read_u32(dev,
0236 "maxim,alert-low-soc-level",
0237 &chip->low_soc_alert);
0238
0239 if (chip->low_soc_alert <= 0 ||
0240 chip->low_soc_alert > (chip->quirk_double_soc ? 16 : 32)) {
0241 dev_err(dev, "maxim,alert-low-soc-level out of bounds\n");
0242 return -EINVAL;
0243 }
0244
0245 rcomp_len = device_property_count_u8(dev, "maxim,rcomp");
0246 chip->rcomp = MAX17040_RCOMP_DEFAULT;
0247 if (rcomp_len == data->rcomp_bytes) {
0248 if (!device_property_read_u8_array(dev, "maxim,rcomp",
0249 rcomp, rcomp_len))
0250 chip->rcomp = rcomp_len == 2 ? rcomp[0] << 8 | rcomp[1] :
0251 rcomp[0] << 8;
0252 } else if (rcomp_len > 0) {
0253 dev_err(dev, "maxim,rcomp has incorrect length\n");
0254 return -EINVAL;
0255 }
0256
0257 return 0;
0258 }
0259
0260 static void max17040_check_changes(struct max17040_chip *chip)
0261 {
0262 chip->soc = max17040_get_soc(chip);
0263 }
0264
0265 static void max17040_queue_work(struct max17040_chip *chip)
0266 {
0267 queue_delayed_work(system_power_efficient_wq, &chip->work,
0268 MAX17040_DELAY);
0269 }
0270
0271 static void max17040_stop_work(void *data)
0272 {
0273 struct max17040_chip *chip = data;
0274
0275 cancel_delayed_work_sync(&chip->work);
0276 }
0277
0278 static void max17040_work(struct work_struct *work)
0279 {
0280 struct max17040_chip *chip;
0281 int last_soc;
0282
0283 chip = container_of(work, struct max17040_chip, work.work);
0284
0285
0286 last_soc = chip->soc;
0287 max17040_check_changes(chip);
0288
0289
0290 if (last_soc != chip->soc)
0291 power_supply_changed(chip->battery);
0292
0293 max17040_queue_work(chip);
0294 }
0295
0296
0297 static bool max17040_handle_soc_alert(struct max17040_chip *chip)
0298 {
0299 bool ret = true;
0300 u32 data;
0301
0302 regmap_read(chip->regmap, MAX17040_STATUS, &data);
0303
0304 if (data & MAX17040_STATUS_HD_MASK) {
0305
0306 ret = false;
0307 }
0308 if (data & MAX17040_STATUS_SC_MASK) {
0309
0310 regmap_write(chip->regmap, MAX17040_STATUS,
0311 data & ~MAX17040_STATUS_SC_MASK);
0312 }
0313
0314 return ret;
0315 }
0316
0317 static irqreturn_t max17040_thread_handler(int id, void *dev)
0318 {
0319 struct max17040_chip *chip = dev;
0320
0321 if (!(chip->data.has_soc_alert && max17040_handle_soc_alert(chip)))
0322 dev_warn(&chip->client->dev, "IRQ: Alert battery low level\n");
0323
0324
0325 max17040_check_changes(chip);
0326
0327
0328 power_supply_changed(chip->battery);
0329
0330
0331 max17040_set_low_soc_alert(chip, chip->low_soc_alert);
0332
0333 return IRQ_HANDLED;
0334 }
0335
0336 static int max17040_enable_alert_irq(struct max17040_chip *chip)
0337 {
0338 struct i2c_client *client = chip->client;
0339 int ret;
0340
0341 ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
0342 max17040_thread_handler, IRQF_ONESHOT,
0343 chip->battery->desc->name, chip);
0344
0345 return ret;
0346 }
0347
0348 static int max17040_prop_writeable(struct power_supply *psy,
0349 enum power_supply_property psp)
0350 {
0351 switch (psp) {
0352 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
0353 return 1;
0354 default:
0355 return 0;
0356 }
0357 }
0358
0359 static int max17040_set_property(struct power_supply *psy,
0360 enum power_supply_property psp,
0361 const union power_supply_propval *val)
0362 {
0363 struct max17040_chip *chip = power_supply_get_drvdata(psy);
0364 int ret;
0365
0366 switch (psp) {
0367 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
0368
0369 if ((val->intval < 1) ||
0370 (val->intval > (chip->quirk_double_soc ? 16 : 32))) {
0371 ret = -EINVAL;
0372 break;
0373 }
0374 ret = max17040_set_low_soc_alert(chip, val->intval);
0375 chip->low_soc_alert = val->intval;
0376 break;
0377 default:
0378 ret = -EINVAL;
0379 }
0380
0381 return ret;
0382 }
0383
0384 static int max17040_get_property(struct power_supply *psy,
0385 enum power_supply_property psp,
0386 union power_supply_propval *val)
0387 {
0388 struct max17040_chip *chip = power_supply_get_drvdata(psy);
0389
0390 switch (psp) {
0391 case POWER_SUPPLY_PROP_ONLINE:
0392 val->intval = max17040_get_online(chip);
0393 break;
0394 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
0395 val->intval = max17040_get_vcell(chip);
0396 break;
0397 case POWER_SUPPLY_PROP_CAPACITY:
0398 val->intval = max17040_get_soc(chip);
0399 break;
0400 case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
0401 val->intval = chip->low_soc_alert;
0402 break;
0403 default:
0404 return -EINVAL;
0405 }
0406 return 0;
0407 }
0408
0409 static const struct regmap_config max17040_regmap = {
0410 .reg_bits = 8,
0411 .reg_stride = 2,
0412 .val_bits = 16,
0413 .val_format_endian = REGMAP_ENDIAN_BIG,
0414 };
0415
0416 static enum power_supply_property max17040_battery_props[] = {
0417 POWER_SUPPLY_PROP_ONLINE,
0418 POWER_SUPPLY_PROP_VOLTAGE_NOW,
0419 POWER_SUPPLY_PROP_CAPACITY,
0420 POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN,
0421 };
0422
0423 static const struct power_supply_desc max17040_battery_desc = {
0424 .name = "battery",
0425 .type = POWER_SUPPLY_TYPE_BATTERY,
0426 .get_property = max17040_get_property,
0427 .set_property = max17040_set_property,
0428 .property_is_writeable = max17040_prop_writeable,
0429 .properties = max17040_battery_props,
0430 .num_properties = ARRAY_SIZE(max17040_battery_props),
0431 };
0432
0433 static int max17040_probe(struct i2c_client *client,
0434 const struct i2c_device_id *id)
0435 {
0436 struct i2c_adapter *adapter = client->adapter;
0437 struct power_supply_config psy_cfg = {};
0438 struct max17040_chip *chip;
0439 enum chip_id chip_id;
0440 bool enable_irq = false;
0441 int ret;
0442
0443 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
0444 return -EIO;
0445
0446 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
0447 if (!chip)
0448 return -ENOMEM;
0449
0450 chip->client = client;
0451 chip->regmap = devm_regmap_init_i2c(client, &max17040_regmap);
0452 if (IS_ERR(chip->regmap))
0453 return PTR_ERR(chip->regmap);
0454 chip_id = (enum chip_id) id->driver_data;
0455 if (client->dev.of_node) {
0456 ret = max17040_get_of_data(chip);
0457 if (ret)
0458 return ret;
0459 chip_id = (uintptr_t)of_device_get_match_data(&client->dev);
0460 }
0461 chip->data = max17040_family[chip_id];
0462
0463 i2c_set_clientdata(client, chip);
0464 psy_cfg.drv_data = chip;
0465
0466 chip->battery = devm_power_supply_register(&client->dev,
0467 &max17040_battery_desc, &psy_cfg);
0468 if (IS_ERR(chip->battery)) {
0469 dev_err(&client->dev, "failed: power supply register\n");
0470 return PTR_ERR(chip->battery);
0471 }
0472
0473 ret = max17040_get_version(chip);
0474 if (ret < 0)
0475 return ret;
0476 dev_dbg(&chip->client->dev, "MAX17040 Fuel-Gauge Ver 0x%x\n", ret);
0477
0478 if (chip_id == ID_MAX17040 || chip_id == ID_MAX17041)
0479 max17040_reset(chip);
0480
0481 max17040_set_rcomp(chip, chip->rcomp);
0482
0483
0484 if (client->irq && chip->data.has_low_soc_alert) {
0485 ret = max17040_set_low_soc_alert(chip, chip->low_soc_alert);
0486 if (ret) {
0487 dev_err(&client->dev,
0488 "Failed to set low SOC alert: err %d\n", ret);
0489 return ret;
0490 }
0491
0492 enable_irq = true;
0493 }
0494
0495 if (client->irq && chip->data.has_soc_alert) {
0496 ret = max17040_set_soc_alert(chip, 1);
0497 if (ret) {
0498 dev_err(&client->dev,
0499 "Failed to set SOC alert: err %d\n", ret);
0500 return ret;
0501 }
0502 enable_irq = true;
0503 } else {
0504
0505 INIT_DEFERRABLE_WORK(&chip->work, max17040_work);
0506 ret = devm_add_action(&client->dev, max17040_stop_work, chip);
0507 if (ret)
0508 return ret;
0509 max17040_queue_work(chip);
0510 }
0511
0512 if (enable_irq) {
0513 ret = max17040_enable_alert_irq(chip);
0514 if (ret) {
0515 client->irq = 0;
0516 dev_warn(&client->dev,
0517 "Failed to get IRQ err %d\n", ret);
0518 }
0519 }
0520
0521 return 0;
0522 }
0523
0524 #ifdef CONFIG_PM_SLEEP
0525
0526 static int max17040_suspend(struct device *dev)
0527 {
0528 struct i2c_client *client = to_i2c_client(dev);
0529 struct max17040_chip *chip = i2c_get_clientdata(client);
0530
0531 if (client->irq && chip->data.has_soc_alert)
0532
0533 max17040_set_soc_alert(chip, 0);
0534 else
0535 cancel_delayed_work(&chip->work);
0536
0537 if (client->irq && device_may_wakeup(dev))
0538 enable_irq_wake(client->irq);
0539
0540 return 0;
0541 }
0542
0543 static int max17040_resume(struct device *dev)
0544 {
0545 struct i2c_client *client = to_i2c_client(dev);
0546 struct max17040_chip *chip = i2c_get_clientdata(client);
0547
0548 if (client->irq && device_may_wakeup(dev))
0549 disable_irq_wake(client->irq);
0550
0551 if (client->irq && chip->data.has_soc_alert)
0552 max17040_set_soc_alert(chip, 1);
0553 else
0554 max17040_queue_work(chip);
0555
0556 return 0;
0557 }
0558
0559 static SIMPLE_DEV_PM_OPS(max17040_pm_ops, max17040_suspend, max17040_resume);
0560 #define MAX17040_PM_OPS (&max17040_pm_ops)
0561
0562 #else
0563
0564 #define MAX17040_PM_OPS NULL
0565
0566 #endif
0567
0568 static const struct i2c_device_id max17040_id[] = {
0569 { "max17040", ID_MAX17040 },
0570 { "max17041", ID_MAX17041 },
0571 { "max17043", ID_MAX17043 },
0572 { "max77836-battery", ID_MAX17043 },
0573 { "max17044", ID_MAX17044 },
0574 { "max17048", ID_MAX17048 },
0575 { "max17049", ID_MAX17049 },
0576 { "max17058", ID_MAX17058 },
0577 { "max17059", ID_MAX17059 },
0578 { }
0579 };
0580 MODULE_DEVICE_TABLE(i2c, max17040_id);
0581
0582 static const struct of_device_id max17040_of_match[] = {
0583 { .compatible = "maxim,max17040", .data = (void *) ID_MAX17040 },
0584 { .compatible = "maxim,max17041", .data = (void *) ID_MAX17041 },
0585 { .compatible = "maxim,max17043", .data = (void *) ID_MAX17043 },
0586 { .compatible = "maxim,max77836-battery", .data = (void *) ID_MAX17043 },
0587 { .compatible = "maxim,max17044", .data = (void *) ID_MAX17044 },
0588 { .compatible = "maxim,max17048", .data = (void *) ID_MAX17048 },
0589 { .compatible = "maxim,max17049", .data = (void *) ID_MAX17049 },
0590 { .compatible = "maxim,max17058", .data = (void *) ID_MAX17058 },
0591 { .compatible = "maxim,max17059", .data = (void *) ID_MAX17059 },
0592 { },
0593 };
0594 MODULE_DEVICE_TABLE(of, max17040_of_match);
0595
0596 static struct i2c_driver max17040_i2c_driver = {
0597 .driver = {
0598 .name = "max17040",
0599 .of_match_table = max17040_of_match,
0600 .pm = MAX17040_PM_OPS,
0601 },
0602 .probe = max17040_probe,
0603 .id_table = max17040_id,
0604 };
0605 module_i2c_driver(max17040_i2c_driver);
0606
0607 MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
0608 MODULE_DESCRIPTION("MAX17040 Fuel Gauge");
0609 MODULE_LICENSE("GPL");