xref: /openbmc/linux/drivers/power/supply/max17040_battery.c (revision 14d60bdd1ef58e3e0913f049c88daeed91a43387)
18c0984e5SSebastian Reichel /*
28c0984e5SSebastian Reichel  *  max17040_battery.c
38c0984e5SSebastian Reichel  *  fuel-gauge systems for lithium-ion (Li+) batteries
48c0984e5SSebastian Reichel  *
58c0984e5SSebastian Reichel  *  Copyright (C) 2009 Samsung Electronics
68c0984e5SSebastian Reichel  *  Minkyu Kang <mk7.kang@samsung.com>
78c0984e5SSebastian Reichel  *
88c0984e5SSebastian Reichel  * This program is free software; you can redistribute it and/or modify
98c0984e5SSebastian Reichel  * it under the terms of the GNU General Public License version 2 as
108c0984e5SSebastian Reichel  * published by the Free Software Foundation.
118c0984e5SSebastian Reichel  */
128c0984e5SSebastian Reichel 
138c0984e5SSebastian Reichel #include <linux/module.h>
148c0984e5SSebastian Reichel #include <linux/init.h>
158c0984e5SSebastian Reichel #include <linux/platform_device.h>
168c0984e5SSebastian Reichel #include <linux/mutex.h>
178c0984e5SSebastian Reichel #include <linux/err.h>
188c0984e5SSebastian Reichel #include <linux/i2c.h>
198c0984e5SSebastian Reichel #include <linux/delay.h>
208c0984e5SSebastian Reichel #include <linux/power_supply.h>
218c0984e5SSebastian Reichel #include <linux/max17040_battery.h>
228c0984e5SSebastian Reichel #include <linux/slab.h>
238c0984e5SSebastian Reichel 
24*14d60bddSLiu Xiang #define MAX17040_VCELL	0x02
25*14d60bddSLiu Xiang #define MAX17040_SOC	0x04
26*14d60bddSLiu Xiang #define MAX17040_MODE	0x06
27*14d60bddSLiu Xiang #define MAX17040_VER	0x08
28*14d60bddSLiu Xiang #define MAX17040_RCOMP	0x0C
29*14d60bddSLiu Xiang #define MAX17040_CMD	0xFE
30*14d60bddSLiu Xiang 
318c0984e5SSebastian Reichel 
328c0984e5SSebastian Reichel #define MAX17040_DELAY		1000
338c0984e5SSebastian Reichel #define MAX17040_BATTERY_FULL	95
348c0984e5SSebastian Reichel 
358c0984e5SSebastian Reichel struct max17040_chip {
368c0984e5SSebastian Reichel 	struct i2c_client		*client;
378c0984e5SSebastian Reichel 	struct delayed_work		work;
388c0984e5SSebastian Reichel 	struct power_supply		*battery;
398c0984e5SSebastian Reichel 	struct max17040_platform_data	*pdata;
408c0984e5SSebastian Reichel 
418c0984e5SSebastian Reichel 	/* State Of Connect */
428c0984e5SSebastian Reichel 	int online;
438c0984e5SSebastian Reichel 	/* battery voltage */
448c0984e5SSebastian Reichel 	int vcell;
458c0984e5SSebastian Reichel 	/* battery capacity */
468c0984e5SSebastian Reichel 	int soc;
478c0984e5SSebastian Reichel 	/* State Of Charge */
488c0984e5SSebastian Reichel 	int status;
498c0984e5SSebastian Reichel };
508c0984e5SSebastian Reichel 
518c0984e5SSebastian Reichel static int max17040_get_property(struct power_supply *psy,
528c0984e5SSebastian Reichel 			    enum power_supply_property psp,
538c0984e5SSebastian Reichel 			    union power_supply_propval *val)
548c0984e5SSebastian Reichel {
558c0984e5SSebastian Reichel 	struct max17040_chip *chip = power_supply_get_drvdata(psy);
568c0984e5SSebastian Reichel 
578c0984e5SSebastian Reichel 	switch (psp) {
588c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_STATUS:
598c0984e5SSebastian Reichel 		val->intval = chip->status;
608c0984e5SSebastian Reichel 		break;
618c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_ONLINE:
628c0984e5SSebastian Reichel 		val->intval = chip->online;
638c0984e5SSebastian Reichel 		break;
648c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
658c0984e5SSebastian Reichel 		val->intval = chip->vcell;
668c0984e5SSebastian Reichel 		break;
678c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CAPACITY:
688c0984e5SSebastian Reichel 		val->intval = chip->soc;
698c0984e5SSebastian Reichel 		break;
708c0984e5SSebastian Reichel 	default:
718c0984e5SSebastian Reichel 		return -EINVAL;
728c0984e5SSebastian Reichel 	}
738c0984e5SSebastian Reichel 	return 0;
748c0984e5SSebastian Reichel }
758c0984e5SSebastian Reichel 
76*14d60bddSLiu Xiang static int max17040_write_reg(struct i2c_client *client, int reg, u16 value)
778c0984e5SSebastian Reichel {
788c0984e5SSebastian Reichel 	int ret;
798c0984e5SSebastian Reichel 
80*14d60bddSLiu Xiang 	ret = i2c_smbus_write_word_swapped(client, reg, value);
818c0984e5SSebastian Reichel 
828c0984e5SSebastian Reichel 	if (ret < 0)
838c0984e5SSebastian Reichel 		dev_err(&client->dev, "%s: err %d\n", __func__, ret);
848c0984e5SSebastian Reichel 
858c0984e5SSebastian Reichel 	return ret;
868c0984e5SSebastian Reichel }
878c0984e5SSebastian Reichel 
888c0984e5SSebastian Reichel static int max17040_read_reg(struct i2c_client *client, int reg)
898c0984e5SSebastian Reichel {
908c0984e5SSebastian Reichel 	int ret;
918c0984e5SSebastian Reichel 
92*14d60bddSLiu Xiang 	ret = i2c_smbus_read_word_swapped(client, reg);
938c0984e5SSebastian Reichel 
948c0984e5SSebastian Reichel 	if (ret < 0)
958c0984e5SSebastian Reichel 		dev_err(&client->dev, "%s: err %d\n", __func__, ret);
968c0984e5SSebastian Reichel 
978c0984e5SSebastian Reichel 	return ret;
988c0984e5SSebastian Reichel }
998c0984e5SSebastian Reichel 
1008c0984e5SSebastian Reichel static void max17040_reset(struct i2c_client *client)
1018c0984e5SSebastian Reichel {
102*14d60bddSLiu Xiang 	max17040_write_reg(client, MAX17040_CMD, 0x0054);
1038c0984e5SSebastian Reichel }
1048c0984e5SSebastian Reichel 
1058c0984e5SSebastian Reichel static void max17040_get_vcell(struct i2c_client *client)
1068c0984e5SSebastian Reichel {
1078c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
108*14d60bddSLiu Xiang 	u16 vcell;
1098c0984e5SSebastian Reichel 
110*14d60bddSLiu Xiang 	vcell = max17040_read_reg(client, MAX17040_VCELL);
1118c0984e5SSebastian Reichel 
112*14d60bddSLiu Xiang 	chip->vcell = vcell;
1138c0984e5SSebastian Reichel }
1148c0984e5SSebastian Reichel 
1158c0984e5SSebastian Reichel static void max17040_get_soc(struct i2c_client *client)
1168c0984e5SSebastian Reichel {
1178c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
118*14d60bddSLiu Xiang 	u16 soc;
1198c0984e5SSebastian Reichel 
120*14d60bddSLiu Xiang 	soc = max17040_read_reg(client, MAX17040_SOC);
1218c0984e5SSebastian Reichel 
122*14d60bddSLiu Xiang 	chip->soc = (soc >> 8);
1238c0984e5SSebastian Reichel }
1248c0984e5SSebastian Reichel 
1258c0984e5SSebastian Reichel static void max17040_get_version(struct i2c_client *client)
1268c0984e5SSebastian Reichel {
127*14d60bddSLiu Xiang 	u16 version;
1288c0984e5SSebastian Reichel 
129*14d60bddSLiu Xiang 	version = max17040_read_reg(client, MAX17040_VER);
1308c0984e5SSebastian Reichel 
131*14d60bddSLiu Xiang 	dev_info(&client->dev, "MAX17040 Fuel-Gauge Ver 0x%x\n", version);
1328c0984e5SSebastian Reichel }
1338c0984e5SSebastian Reichel 
1348c0984e5SSebastian Reichel static void max17040_get_online(struct i2c_client *client)
1358c0984e5SSebastian Reichel {
1368c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
1378c0984e5SSebastian Reichel 
1388c0984e5SSebastian Reichel 	if (chip->pdata && chip->pdata->battery_online)
1398c0984e5SSebastian Reichel 		chip->online = chip->pdata->battery_online();
1408c0984e5SSebastian Reichel 	else
1418c0984e5SSebastian Reichel 		chip->online = 1;
1428c0984e5SSebastian Reichel }
1438c0984e5SSebastian Reichel 
1448c0984e5SSebastian Reichel static void max17040_get_status(struct i2c_client *client)
1458c0984e5SSebastian Reichel {
1468c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
1478c0984e5SSebastian Reichel 
1488c0984e5SSebastian Reichel 	if (!chip->pdata || !chip->pdata->charger_online
1498c0984e5SSebastian Reichel 			|| !chip->pdata->charger_enable) {
1508c0984e5SSebastian Reichel 		chip->status = POWER_SUPPLY_STATUS_UNKNOWN;
1518c0984e5SSebastian Reichel 		return;
1528c0984e5SSebastian Reichel 	}
1538c0984e5SSebastian Reichel 
1548c0984e5SSebastian Reichel 	if (chip->pdata->charger_online()) {
1558c0984e5SSebastian Reichel 		if (chip->pdata->charger_enable())
1568c0984e5SSebastian Reichel 			chip->status = POWER_SUPPLY_STATUS_CHARGING;
1578c0984e5SSebastian Reichel 		else
1588c0984e5SSebastian Reichel 			chip->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1598c0984e5SSebastian Reichel 	} else {
1608c0984e5SSebastian Reichel 		chip->status = POWER_SUPPLY_STATUS_DISCHARGING;
1618c0984e5SSebastian Reichel 	}
1628c0984e5SSebastian Reichel 
1638c0984e5SSebastian Reichel 	if (chip->soc > MAX17040_BATTERY_FULL)
1648c0984e5SSebastian Reichel 		chip->status = POWER_SUPPLY_STATUS_FULL;
1658c0984e5SSebastian Reichel }
1668c0984e5SSebastian Reichel 
1678c0984e5SSebastian Reichel static void max17040_work(struct work_struct *work)
1688c0984e5SSebastian Reichel {
1698c0984e5SSebastian Reichel 	struct max17040_chip *chip;
1708c0984e5SSebastian Reichel 
1718c0984e5SSebastian Reichel 	chip = container_of(work, struct max17040_chip, work.work);
1728c0984e5SSebastian Reichel 
1738c0984e5SSebastian Reichel 	max17040_get_vcell(chip->client);
1748c0984e5SSebastian Reichel 	max17040_get_soc(chip->client);
1758c0984e5SSebastian Reichel 	max17040_get_online(chip->client);
1768c0984e5SSebastian Reichel 	max17040_get_status(chip->client);
1778c0984e5SSebastian Reichel 
1788c0984e5SSebastian Reichel 	queue_delayed_work(system_power_efficient_wq, &chip->work,
1798c0984e5SSebastian Reichel 			   MAX17040_DELAY);
1808c0984e5SSebastian Reichel }
1818c0984e5SSebastian Reichel 
1828c0984e5SSebastian Reichel static enum power_supply_property max17040_battery_props[] = {
1838c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
1848c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_ONLINE,
1858c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
1868c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
1878c0984e5SSebastian Reichel };
1888c0984e5SSebastian Reichel 
1898c0984e5SSebastian Reichel static const struct power_supply_desc max17040_battery_desc = {
1908c0984e5SSebastian Reichel 	.name		= "battery",
1918c0984e5SSebastian Reichel 	.type		= POWER_SUPPLY_TYPE_BATTERY,
1928c0984e5SSebastian Reichel 	.get_property	= max17040_get_property,
1938c0984e5SSebastian Reichel 	.properties	= max17040_battery_props,
1948c0984e5SSebastian Reichel 	.num_properties	= ARRAY_SIZE(max17040_battery_props),
1958c0984e5SSebastian Reichel };
1968c0984e5SSebastian Reichel 
1978c0984e5SSebastian Reichel static int max17040_probe(struct i2c_client *client,
1988c0984e5SSebastian Reichel 			const struct i2c_device_id *id)
1998c0984e5SSebastian Reichel {
2008c0984e5SSebastian Reichel 	struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2018c0984e5SSebastian Reichel 	struct power_supply_config psy_cfg = {};
2028c0984e5SSebastian Reichel 	struct max17040_chip *chip;
2038c0984e5SSebastian Reichel 
2048c0984e5SSebastian Reichel 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
2058c0984e5SSebastian Reichel 		return -EIO;
2068c0984e5SSebastian Reichel 
2078c0984e5SSebastian Reichel 	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
2088c0984e5SSebastian Reichel 	if (!chip)
2098c0984e5SSebastian Reichel 		return -ENOMEM;
2108c0984e5SSebastian Reichel 
2118c0984e5SSebastian Reichel 	chip->client = client;
2128c0984e5SSebastian Reichel 	chip->pdata = client->dev.platform_data;
2138c0984e5SSebastian Reichel 
2148c0984e5SSebastian Reichel 	i2c_set_clientdata(client, chip);
2158c0984e5SSebastian Reichel 	psy_cfg.drv_data = chip;
2168c0984e5SSebastian Reichel 
2178c0984e5SSebastian Reichel 	chip->battery = power_supply_register(&client->dev,
2188c0984e5SSebastian Reichel 				&max17040_battery_desc, &psy_cfg);
2198c0984e5SSebastian Reichel 	if (IS_ERR(chip->battery)) {
2208c0984e5SSebastian Reichel 		dev_err(&client->dev, "failed: power supply register\n");
2218c0984e5SSebastian Reichel 		return PTR_ERR(chip->battery);
2228c0984e5SSebastian Reichel 	}
2238c0984e5SSebastian Reichel 
2248c0984e5SSebastian Reichel 	max17040_reset(client);
2258c0984e5SSebastian Reichel 	max17040_get_version(client);
2268c0984e5SSebastian Reichel 
2278c0984e5SSebastian Reichel 	INIT_DEFERRABLE_WORK(&chip->work, max17040_work);
2288c0984e5SSebastian Reichel 	queue_delayed_work(system_power_efficient_wq, &chip->work,
2298c0984e5SSebastian Reichel 			   MAX17040_DELAY);
2308c0984e5SSebastian Reichel 
2318c0984e5SSebastian Reichel 	return 0;
2328c0984e5SSebastian Reichel }
2338c0984e5SSebastian Reichel 
2348c0984e5SSebastian Reichel static int max17040_remove(struct i2c_client *client)
2358c0984e5SSebastian Reichel {
2368c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
2378c0984e5SSebastian Reichel 
2388c0984e5SSebastian Reichel 	power_supply_unregister(chip->battery);
2398c0984e5SSebastian Reichel 	cancel_delayed_work(&chip->work);
2408c0984e5SSebastian Reichel 	return 0;
2418c0984e5SSebastian Reichel }
2428c0984e5SSebastian Reichel 
2438c0984e5SSebastian Reichel #ifdef CONFIG_PM_SLEEP
2448c0984e5SSebastian Reichel 
2458c0984e5SSebastian Reichel static int max17040_suspend(struct device *dev)
2468c0984e5SSebastian Reichel {
2478c0984e5SSebastian Reichel 	struct i2c_client *client = to_i2c_client(dev);
2488c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
2498c0984e5SSebastian Reichel 
2508c0984e5SSebastian Reichel 	cancel_delayed_work(&chip->work);
2518c0984e5SSebastian Reichel 	return 0;
2528c0984e5SSebastian Reichel }
2538c0984e5SSebastian Reichel 
2548c0984e5SSebastian Reichel static int max17040_resume(struct device *dev)
2558c0984e5SSebastian Reichel {
2568c0984e5SSebastian Reichel 	struct i2c_client *client = to_i2c_client(dev);
2578c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
2588c0984e5SSebastian Reichel 
2598c0984e5SSebastian Reichel 	queue_delayed_work(system_power_efficient_wq, &chip->work,
2608c0984e5SSebastian Reichel 			   MAX17040_DELAY);
2618c0984e5SSebastian Reichel 	return 0;
2628c0984e5SSebastian Reichel }
2638c0984e5SSebastian Reichel 
2648c0984e5SSebastian Reichel static SIMPLE_DEV_PM_OPS(max17040_pm_ops, max17040_suspend, max17040_resume);
2658c0984e5SSebastian Reichel #define MAX17040_PM_OPS (&max17040_pm_ops)
2668c0984e5SSebastian Reichel 
2678c0984e5SSebastian Reichel #else
2688c0984e5SSebastian Reichel 
2698c0984e5SSebastian Reichel #define MAX17040_PM_OPS NULL
2708c0984e5SSebastian Reichel 
2718c0984e5SSebastian Reichel #endif /* CONFIG_PM_SLEEP */
2728c0984e5SSebastian Reichel 
2738c0984e5SSebastian Reichel static const struct i2c_device_id max17040_id[] = {
2748c0984e5SSebastian Reichel 	{ "max17040" },
2758c0984e5SSebastian Reichel 	{ "max77836-battery" },
2768c0984e5SSebastian Reichel 	{ }
2778c0984e5SSebastian Reichel };
2788c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(i2c, max17040_id);
2798c0984e5SSebastian Reichel 
2808c0984e5SSebastian Reichel static struct i2c_driver max17040_i2c_driver = {
2818c0984e5SSebastian Reichel 	.driver	= {
2828c0984e5SSebastian Reichel 		.name	= "max17040",
2838c0984e5SSebastian Reichel 		.pm	= MAX17040_PM_OPS,
2848c0984e5SSebastian Reichel 	},
2858c0984e5SSebastian Reichel 	.probe		= max17040_probe,
2868c0984e5SSebastian Reichel 	.remove		= max17040_remove,
2878c0984e5SSebastian Reichel 	.id_table	= max17040_id,
2888c0984e5SSebastian Reichel };
2898c0984e5SSebastian Reichel module_i2c_driver(max17040_i2c_driver);
2908c0984e5SSebastian Reichel 
2918c0984e5SSebastian Reichel MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
2928c0984e5SSebastian Reichel MODULE_DESCRIPTION("MAX17040 Fuel Gauge");
2938c0984e5SSebastian Reichel MODULE_LICENSE("GPL");
294