1*7b38ebdfSKrzysztof Kozlowski // SPDX-License-Identifier: GPL-2.0 2*7b38ebdfSKrzysztof Kozlowski // 3*7b38ebdfSKrzysztof Kozlowski // max17040_battery.c 4*7b38ebdfSKrzysztof Kozlowski // fuel-gauge systems for lithium-ion (Li+) batteries 5*7b38ebdfSKrzysztof Kozlowski // 6*7b38ebdfSKrzysztof Kozlowski // Copyright (C) 2009 Samsung Electronics 7*7b38ebdfSKrzysztof Kozlowski // Minkyu Kang <mk7.kang@samsung.com> 88c0984e5SSebastian Reichel 98c0984e5SSebastian Reichel #include <linux/module.h> 108c0984e5SSebastian Reichel #include <linux/init.h> 118c0984e5SSebastian Reichel #include <linux/platform_device.h> 128c0984e5SSebastian Reichel #include <linux/mutex.h> 138c0984e5SSebastian Reichel #include <linux/err.h> 148c0984e5SSebastian Reichel #include <linux/i2c.h> 158c0984e5SSebastian Reichel #include <linux/delay.h> 168c0984e5SSebastian Reichel #include <linux/power_supply.h> 178c0984e5SSebastian Reichel #include <linux/max17040_battery.h> 188c0984e5SSebastian Reichel #include <linux/slab.h> 198c0984e5SSebastian Reichel 2014d60bddSLiu Xiang #define MAX17040_VCELL 0x02 2114d60bddSLiu Xiang #define MAX17040_SOC 0x04 2214d60bddSLiu Xiang #define MAX17040_MODE 0x06 2314d60bddSLiu Xiang #define MAX17040_VER 0x08 2414d60bddSLiu Xiang #define MAX17040_RCOMP 0x0C 2514d60bddSLiu Xiang #define MAX17040_CMD 0xFE 2614d60bddSLiu Xiang 278c0984e5SSebastian Reichel 288c0984e5SSebastian Reichel #define MAX17040_DELAY 1000 298c0984e5SSebastian Reichel #define MAX17040_BATTERY_FULL 95 308c0984e5SSebastian Reichel 318c0984e5SSebastian Reichel struct max17040_chip { 328c0984e5SSebastian Reichel struct i2c_client *client; 338c0984e5SSebastian Reichel struct delayed_work work; 348c0984e5SSebastian Reichel struct power_supply *battery; 358c0984e5SSebastian Reichel struct max17040_platform_data *pdata; 368c0984e5SSebastian Reichel 378c0984e5SSebastian Reichel /* State Of Connect */ 388c0984e5SSebastian Reichel int online; 398c0984e5SSebastian Reichel /* battery voltage */ 408c0984e5SSebastian Reichel int vcell; 418c0984e5SSebastian Reichel /* battery capacity */ 428c0984e5SSebastian Reichel int soc; 438c0984e5SSebastian Reichel /* State Of Charge */ 448c0984e5SSebastian Reichel int status; 458c0984e5SSebastian Reichel }; 468c0984e5SSebastian Reichel 478c0984e5SSebastian Reichel static int max17040_get_property(struct power_supply *psy, 488c0984e5SSebastian Reichel enum power_supply_property psp, 498c0984e5SSebastian Reichel union power_supply_propval *val) 508c0984e5SSebastian Reichel { 518c0984e5SSebastian Reichel struct max17040_chip *chip = power_supply_get_drvdata(psy); 528c0984e5SSebastian Reichel 538c0984e5SSebastian Reichel switch (psp) { 548c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_STATUS: 558c0984e5SSebastian Reichel val->intval = chip->status; 568c0984e5SSebastian Reichel break; 578c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_ONLINE: 588c0984e5SSebastian Reichel val->intval = chip->online; 598c0984e5SSebastian Reichel break; 608c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_VOLTAGE_NOW: 618c0984e5SSebastian Reichel val->intval = chip->vcell; 628c0984e5SSebastian Reichel break; 638c0984e5SSebastian Reichel case POWER_SUPPLY_PROP_CAPACITY: 648c0984e5SSebastian Reichel val->intval = chip->soc; 658c0984e5SSebastian Reichel break; 668c0984e5SSebastian Reichel default: 678c0984e5SSebastian Reichel return -EINVAL; 688c0984e5SSebastian Reichel } 698c0984e5SSebastian Reichel return 0; 708c0984e5SSebastian Reichel } 718c0984e5SSebastian Reichel 7214d60bddSLiu Xiang static int max17040_write_reg(struct i2c_client *client, int reg, u16 value) 738c0984e5SSebastian Reichel { 748c0984e5SSebastian Reichel int ret; 758c0984e5SSebastian Reichel 7614d60bddSLiu Xiang ret = i2c_smbus_write_word_swapped(client, reg, value); 778c0984e5SSebastian Reichel 788c0984e5SSebastian Reichel if (ret < 0) 798c0984e5SSebastian Reichel dev_err(&client->dev, "%s: err %d\n", __func__, ret); 808c0984e5SSebastian Reichel 818c0984e5SSebastian Reichel return ret; 828c0984e5SSebastian Reichel } 838c0984e5SSebastian Reichel 848c0984e5SSebastian Reichel static int max17040_read_reg(struct i2c_client *client, int reg) 858c0984e5SSebastian Reichel { 868c0984e5SSebastian Reichel int ret; 878c0984e5SSebastian Reichel 8814d60bddSLiu Xiang ret = i2c_smbus_read_word_swapped(client, reg); 898c0984e5SSebastian Reichel 908c0984e5SSebastian Reichel if (ret < 0) 918c0984e5SSebastian Reichel dev_err(&client->dev, "%s: err %d\n", __func__, ret); 928c0984e5SSebastian Reichel 938c0984e5SSebastian Reichel return ret; 948c0984e5SSebastian Reichel } 958c0984e5SSebastian Reichel 968c0984e5SSebastian Reichel static void max17040_reset(struct i2c_client *client) 978c0984e5SSebastian Reichel { 9814d60bddSLiu Xiang max17040_write_reg(client, MAX17040_CMD, 0x0054); 998c0984e5SSebastian Reichel } 1008c0984e5SSebastian Reichel 1018c0984e5SSebastian Reichel static void max17040_get_vcell(struct i2c_client *client) 1028c0984e5SSebastian Reichel { 1038c0984e5SSebastian Reichel struct max17040_chip *chip = i2c_get_clientdata(client); 10414d60bddSLiu Xiang u16 vcell; 1058c0984e5SSebastian Reichel 10614d60bddSLiu Xiang vcell = max17040_read_reg(client, MAX17040_VCELL); 1078c0984e5SSebastian Reichel 10814d60bddSLiu Xiang chip->vcell = vcell; 1098c0984e5SSebastian Reichel } 1108c0984e5SSebastian Reichel 1118c0984e5SSebastian Reichel static void max17040_get_soc(struct i2c_client *client) 1128c0984e5SSebastian Reichel { 1138c0984e5SSebastian Reichel struct max17040_chip *chip = i2c_get_clientdata(client); 11414d60bddSLiu Xiang u16 soc; 1158c0984e5SSebastian Reichel 11614d60bddSLiu Xiang soc = max17040_read_reg(client, MAX17040_SOC); 1178c0984e5SSebastian Reichel 11814d60bddSLiu Xiang chip->soc = (soc >> 8); 1198c0984e5SSebastian Reichel } 1208c0984e5SSebastian Reichel 1218c0984e5SSebastian Reichel static void max17040_get_version(struct i2c_client *client) 1228c0984e5SSebastian Reichel { 12314d60bddSLiu Xiang u16 version; 1248c0984e5SSebastian Reichel 12514d60bddSLiu Xiang version = max17040_read_reg(client, MAX17040_VER); 1268c0984e5SSebastian Reichel 12714d60bddSLiu Xiang dev_info(&client->dev, "MAX17040 Fuel-Gauge Ver 0x%x\n", version); 1288c0984e5SSebastian Reichel } 1298c0984e5SSebastian Reichel 1308c0984e5SSebastian Reichel static void max17040_get_online(struct i2c_client *client) 1318c0984e5SSebastian Reichel { 1328c0984e5SSebastian Reichel struct max17040_chip *chip = i2c_get_clientdata(client); 1338c0984e5SSebastian Reichel 1348c0984e5SSebastian Reichel if (chip->pdata && chip->pdata->battery_online) 1358c0984e5SSebastian Reichel chip->online = chip->pdata->battery_online(); 1368c0984e5SSebastian Reichel else 1378c0984e5SSebastian Reichel chip->online = 1; 1388c0984e5SSebastian Reichel } 1398c0984e5SSebastian Reichel 1408c0984e5SSebastian Reichel static void max17040_get_status(struct i2c_client *client) 1418c0984e5SSebastian Reichel { 1428c0984e5SSebastian Reichel struct max17040_chip *chip = i2c_get_clientdata(client); 1438c0984e5SSebastian Reichel 1448c0984e5SSebastian Reichel if (!chip->pdata || !chip->pdata->charger_online 1458c0984e5SSebastian Reichel || !chip->pdata->charger_enable) { 1468c0984e5SSebastian Reichel chip->status = POWER_SUPPLY_STATUS_UNKNOWN; 1478c0984e5SSebastian Reichel return; 1488c0984e5SSebastian Reichel } 1498c0984e5SSebastian Reichel 1508c0984e5SSebastian Reichel if (chip->pdata->charger_online()) { 1518c0984e5SSebastian Reichel if (chip->pdata->charger_enable()) 1528c0984e5SSebastian Reichel chip->status = POWER_SUPPLY_STATUS_CHARGING; 1538c0984e5SSebastian Reichel else 1548c0984e5SSebastian Reichel chip->status = POWER_SUPPLY_STATUS_NOT_CHARGING; 1558c0984e5SSebastian Reichel } else { 1568c0984e5SSebastian Reichel chip->status = POWER_SUPPLY_STATUS_DISCHARGING; 1578c0984e5SSebastian Reichel } 1588c0984e5SSebastian Reichel 1598c0984e5SSebastian Reichel if (chip->soc > MAX17040_BATTERY_FULL) 1608c0984e5SSebastian Reichel chip->status = POWER_SUPPLY_STATUS_FULL; 1618c0984e5SSebastian Reichel } 1628c0984e5SSebastian Reichel 1638c0984e5SSebastian Reichel static void max17040_work(struct work_struct *work) 1648c0984e5SSebastian Reichel { 1658c0984e5SSebastian Reichel struct max17040_chip *chip; 1668c0984e5SSebastian Reichel 1678c0984e5SSebastian Reichel chip = container_of(work, struct max17040_chip, work.work); 1688c0984e5SSebastian Reichel 1698c0984e5SSebastian Reichel max17040_get_vcell(chip->client); 1708c0984e5SSebastian Reichel max17040_get_soc(chip->client); 1718c0984e5SSebastian Reichel max17040_get_online(chip->client); 1728c0984e5SSebastian Reichel max17040_get_status(chip->client); 1738c0984e5SSebastian Reichel 1748c0984e5SSebastian Reichel queue_delayed_work(system_power_efficient_wq, &chip->work, 1758c0984e5SSebastian Reichel MAX17040_DELAY); 1768c0984e5SSebastian Reichel } 1778c0984e5SSebastian Reichel 1788c0984e5SSebastian Reichel static enum power_supply_property max17040_battery_props[] = { 1798c0984e5SSebastian Reichel POWER_SUPPLY_PROP_STATUS, 1808c0984e5SSebastian Reichel POWER_SUPPLY_PROP_ONLINE, 1818c0984e5SSebastian Reichel POWER_SUPPLY_PROP_VOLTAGE_NOW, 1828c0984e5SSebastian Reichel POWER_SUPPLY_PROP_CAPACITY, 1838c0984e5SSebastian Reichel }; 1848c0984e5SSebastian Reichel 1858c0984e5SSebastian Reichel static const struct power_supply_desc max17040_battery_desc = { 1868c0984e5SSebastian Reichel .name = "battery", 1878c0984e5SSebastian Reichel .type = POWER_SUPPLY_TYPE_BATTERY, 1888c0984e5SSebastian Reichel .get_property = max17040_get_property, 1898c0984e5SSebastian Reichel .properties = max17040_battery_props, 1908c0984e5SSebastian Reichel .num_properties = ARRAY_SIZE(max17040_battery_props), 1918c0984e5SSebastian Reichel }; 1928c0984e5SSebastian Reichel 1938c0984e5SSebastian Reichel static int max17040_probe(struct i2c_client *client, 1948c0984e5SSebastian Reichel const struct i2c_device_id *id) 1958c0984e5SSebastian Reichel { 1968c0984e5SSebastian Reichel struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); 1978c0984e5SSebastian Reichel struct power_supply_config psy_cfg = {}; 1988c0984e5SSebastian Reichel struct max17040_chip *chip; 1998c0984e5SSebastian Reichel 2008c0984e5SSebastian Reichel if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) 2018c0984e5SSebastian Reichel return -EIO; 2028c0984e5SSebastian Reichel 2038c0984e5SSebastian Reichel chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); 2048c0984e5SSebastian Reichel if (!chip) 2058c0984e5SSebastian Reichel return -ENOMEM; 2068c0984e5SSebastian Reichel 2078c0984e5SSebastian Reichel chip->client = client; 2088c0984e5SSebastian Reichel chip->pdata = client->dev.platform_data; 2098c0984e5SSebastian Reichel 2108c0984e5SSebastian Reichel i2c_set_clientdata(client, chip); 2118c0984e5SSebastian Reichel psy_cfg.drv_data = chip; 2128c0984e5SSebastian Reichel 2138c0984e5SSebastian Reichel chip->battery = power_supply_register(&client->dev, 2148c0984e5SSebastian Reichel &max17040_battery_desc, &psy_cfg); 2158c0984e5SSebastian Reichel if (IS_ERR(chip->battery)) { 2168c0984e5SSebastian Reichel dev_err(&client->dev, "failed: power supply register\n"); 2178c0984e5SSebastian Reichel return PTR_ERR(chip->battery); 2188c0984e5SSebastian Reichel } 2198c0984e5SSebastian Reichel 2208c0984e5SSebastian Reichel max17040_reset(client); 2218c0984e5SSebastian Reichel max17040_get_version(client); 2228c0984e5SSebastian Reichel 2238c0984e5SSebastian Reichel INIT_DEFERRABLE_WORK(&chip->work, max17040_work); 2248c0984e5SSebastian Reichel queue_delayed_work(system_power_efficient_wq, &chip->work, 2258c0984e5SSebastian Reichel MAX17040_DELAY); 2268c0984e5SSebastian Reichel 2278c0984e5SSebastian Reichel return 0; 2288c0984e5SSebastian Reichel } 2298c0984e5SSebastian Reichel 2308c0984e5SSebastian Reichel static int max17040_remove(struct i2c_client *client) 2318c0984e5SSebastian Reichel { 2328c0984e5SSebastian Reichel struct max17040_chip *chip = i2c_get_clientdata(client); 2338c0984e5SSebastian Reichel 2348c0984e5SSebastian Reichel power_supply_unregister(chip->battery); 2358c0984e5SSebastian Reichel cancel_delayed_work(&chip->work); 2368c0984e5SSebastian Reichel return 0; 2378c0984e5SSebastian Reichel } 2388c0984e5SSebastian Reichel 2398c0984e5SSebastian Reichel #ifdef CONFIG_PM_SLEEP 2408c0984e5SSebastian Reichel 2418c0984e5SSebastian Reichel static int max17040_suspend(struct device *dev) 2428c0984e5SSebastian Reichel { 2438c0984e5SSebastian Reichel struct i2c_client *client = to_i2c_client(dev); 2448c0984e5SSebastian Reichel struct max17040_chip *chip = i2c_get_clientdata(client); 2458c0984e5SSebastian Reichel 2468c0984e5SSebastian Reichel cancel_delayed_work(&chip->work); 2478c0984e5SSebastian Reichel return 0; 2488c0984e5SSebastian Reichel } 2498c0984e5SSebastian Reichel 2508c0984e5SSebastian Reichel static int max17040_resume(struct device *dev) 2518c0984e5SSebastian Reichel { 2528c0984e5SSebastian Reichel struct i2c_client *client = to_i2c_client(dev); 2538c0984e5SSebastian Reichel struct max17040_chip *chip = i2c_get_clientdata(client); 2548c0984e5SSebastian Reichel 2558c0984e5SSebastian Reichel queue_delayed_work(system_power_efficient_wq, &chip->work, 2568c0984e5SSebastian Reichel MAX17040_DELAY); 2578c0984e5SSebastian Reichel return 0; 2588c0984e5SSebastian Reichel } 2598c0984e5SSebastian Reichel 2608c0984e5SSebastian Reichel static SIMPLE_DEV_PM_OPS(max17040_pm_ops, max17040_suspend, max17040_resume); 2618c0984e5SSebastian Reichel #define MAX17040_PM_OPS (&max17040_pm_ops) 2628c0984e5SSebastian Reichel 2638c0984e5SSebastian Reichel #else 2648c0984e5SSebastian Reichel 2658c0984e5SSebastian Reichel #define MAX17040_PM_OPS NULL 2668c0984e5SSebastian Reichel 2678c0984e5SSebastian Reichel #endif /* CONFIG_PM_SLEEP */ 2688c0984e5SSebastian Reichel 2698c0984e5SSebastian Reichel static const struct i2c_device_id max17040_id[] = { 2708c0984e5SSebastian Reichel { "max17040" }, 2718c0984e5SSebastian Reichel { "max77836-battery" }, 2728c0984e5SSebastian Reichel { } 2738c0984e5SSebastian Reichel }; 2748c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(i2c, max17040_id); 2758c0984e5SSebastian Reichel 276da28122cSJavier Martinez Canillas static const struct of_device_id max17040_of_match[] = { 277da28122cSJavier Martinez Canillas { .compatible = "maxim,max17040" }, 278da28122cSJavier Martinez Canillas { .compatible = "maxim,max77836-battery" }, 279da28122cSJavier Martinez Canillas { }, 280da28122cSJavier Martinez Canillas }; 281da28122cSJavier Martinez Canillas MODULE_DEVICE_TABLE(of, max17040_of_match); 282da28122cSJavier Martinez Canillas 2838c0984e5SSebastian Reichel static struct i2c_driver max17040_i2c_driver = { 2848c0984e5SSebastian Reichel .driver = { 2858c0984e5SSebastian Reichel .name = "max17040", 286da28122cSJavier Martinez Canillas .of_match_table = max17040_of_match, 2878c0984e5SSebastian Reichel .pm = MAX17040_PM_OPS, 2888c0984e5SSebastian Reichel }, 2898c0984e5SSebastian Reichel .probe = max17040_probe, 2908c0984e5SSebastian Reichel .remove = max17040_remove, 2918c0984e5SSebastian Reichel .id_table = max17040_id, 2928c0984e5SSebastian Reichel }; 2938c0984e5SSebastian Reichel module_i2c_driver(max17040_i2c_driver); 2948c0984e5SSebastian Reichel 2958c0984e5SSebastian Reichel MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>"); 2968c0984e5SSebastian Reichel MODULE_DESCRIPTION("MAX17040 Fuel Gauge"); 2978c0984e5SSebastian Reichel MODULE_LICENSE("GPL"); 298