xref: /openbmc/linux/drivers/power/supply/max17040_battery.c (revision 2e17ed94de68953b17ed91b64f4bd176cdf38ad4)
17b38ebdfSKrzysztof Kozlowski // SPDX-License-Identifier: GPL-2.0
27b38ebdfSKrzysztof Kozlowski //
37b38ebdfSKrzysztof Kozlowski //  max17040_battery.c
47b38ebdfSKrzysztof Kozlowski //  fuel-gauge systems for lithium-ion (Li+) batteries
57b38ebdfSKrzysztof Kozlowski //
67b38ebdfSKrzysztof Kozlowski //  Copyright (C) 2009 Samsung Electronics
77b38ebdfSKrzysztof 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>
16*2e17ed94SMatheus Castello #include <linux/interrupt.h>
178c0984e5SSebastian Reichel #include <linux/power_supply.h>
188c0984e5SSebastian Reichel #include <linux/max17040_battery.h>
198c0984e5SSebastian Reichel #include <linux/slab.h>
208c0984e5SSebastian Reichel 
2114d60bddSLiu Xiang #define MAX17040_VCELL	0x02
2214d60bddSLiu Xiang #define MAX17040_SOC	0x04
2314d60bddSLiu Xiang #define MAX17040_MODE	0x06
2414d60bddSLiu Xiang #define MAX17040_VER	0x08
2514d60bddSLiu Xiang #define MAX17040_RCOMP	0x0C
2614d60bddSLiu Xiang #define MAX17040_CMD	0xFE
2714d60bddSLiu Xiang 
288c0984e5SSebastian Reichel 
298c0984e5SSebastian Reichel #define MAX17040_DELAY		1000
308c0984e5SSebastian Reichel #define MAX17040_BATTERY_FULL	95
318c0984e5SSebastian Reichel 
328c0984e5SSebastian Reichel struct max17040_chip {
338c0984e5SSebastian Reichel 	struct i2c_client		*client;
348c0984e5SSebastian Reichel 	struct delayed_work		work;
358c0984e5SSebastian Reichel 	struct power_supply		*battery;
368c0984e5SSebastian Reichel 	struct max17040_platform_data	*pdata;
378c0984e5SSebastian Reichel 
388c0984e5SSebastian Reichel 	/* State Of Connect */
398c0984e5SSebastian Reichel 	int online;
408c0984e5SSebastian Reichel 	/* battery voltage */
418c0984e5SSebastian Reichel 	int vcell;
428c0984e5SSebastian Reichel 	/* battery capacity */
438c0984e5SSebastian Reichel 	int soc;
448c0984e5SSebastian Reichel 	/* State Of Charge */
458c0984e5SSebastian Reichel 	int status;
468c0984e5SSebastian Reichel };
478c0984e5SSebastian Reichel 
488c0984e5SSebastian Reichel static int max17040_get_property(struct power_supply *psy,
498c0984e5SSebastian Reichel 			    enum power_supply_property psp,
508c0984e5SSebastian Reichel 			    union power_supply_propval *val)
518c0984e5SSebastian Reichel {
528c0984e5SSebastian Reichel 	struct max17040_chip *chip = power_supply_get_drvdata(psy);
538c0984e5SSebastian Reichel 
548c0984e5SSebastian Reichel 	switch (psp) {
558c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_STATUS:
568c0984e5SSebastian Reichel 		val->intval = chip->status;
578c0984e5SSebastian Reichel 		break;
588c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_ONLINE:
598c0984e5SSebastian Reichel 		val->intval = chip->online;
608c0984e5SSebastian Reichel 		break;
618c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
628c0984e5SSebastian Reichel 		val->intval = chip->vcell;
638c0984e5SSebastian Reichel 		break;
648c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CAPACITY:
658c0984e5SSebastian Reichel 		val->intval = chip->soc;
668c0984e5SSebastian Reichel 		break;
678c0984e5SSebastian Reichel 	default:
688c0984e5SSebastian Reichel 		return -EINVAL;
698c0984e5SSebastian Reichel 	}
708c0984e5SSebastian Reichel 	return 0;
718c0984e5SSebastian Reichel }
728c0984e5SSebastian Reichel 
7314d60bddSLiu Xiang static int max17040_write_reg(struct i2c_client *client, int reg, u16 value)
748c0984e5SSebastian Reichel {
758c0984e5SSebastian Reichel 	int ret;
768c0984e5SSebastian Reichel 
7714d60bddSLiu Xiang 	ret = i2c_smbus_write_word_swapped(client, reg, value);
788c0984e5SSebastian Reichel 
798c0984e5SSebastian Reichel 	if (ret < 0)
808c0984e5SSebastian Reichel 		dev_err(&client->dev, "%s: err %d\n", __func__, ret);
818c0984e5SSebastian Reichel 
828c0984e5SSebastian Reichel 	return ret;
838c0984e5SSebastian Reichel }
848c0984e5SSebastian Reichel 
858c0984e5SSebastian Reichel static int max17040_read_reg(struct i2c_client *client, int reg)
868c0984e5SSebastian Reichel {
878c0984e5SSebastian Reichel 	int ret;
888c0984e5SSebastian Reichel 
8914d60bddSLiu Xiang 	ret = i2c_smbus_read_word_swapped(client, reg);
908c0984e5SSebastian Reichel 
918c0984e5SSebastian Reichel 	if (ret < 0)
928c0984e5SSebastian Reichel 		dev_err(&client->dev, "%s: err %d\n", __func__, ret);
938c0984e5SSebastian Reichel 
948c0984e5SSebastian Reichel 	return ret;
958c0984e5SSebastian Reichel }
968c0984e5SSebastian Reichel 
978c0984e5SSebastian Reichel static void max17040_reset(struct i2c_client *client)
988c0984e5SSebastian Reichel {
9914d60bddSLiu Xiang 	max17040_write_reg(client, MAX17040_CMD, 0x0054);
1008c0984e5SSebastian Reichel }
1018c0984e5SSebastian Reichel 
1028c0984e5SSebastian Reichel static void max17040_get_vcell(struct i2c_client *client)
1038c0984e5SSebastian Reichel {
1048c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
10514d60bddSLiu Xiang 	u16 vcell;
1068c0984e5SSebastian Reichel 
10714d60bddSLiu Xiang 	vcell = max17040_read_reg(client, MAX17040_VCELL);
1088c0984e5SSebastian Reichel 
10914d60bddSLiu Xiang 	chip->vcell = vcell;
1108c0984e5SSebastian Reichel }
1118c0984e5SSebastian Reichel 
1128c0984e5SSebastian Reichel static void max17040_get_soc(struct i2c_client *client)
1138c0984e5SSebastian Reichel {
1148c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
11514d60bddSLiu Xiang 	u16 soc;
1168c0984e5SSebastian Reichel 
11714d60bddSLiu Xiang 	soc = max17040_read_reg(client, MAX17040_SOC);
1188c0984e5SSebastian Reichel 
11914d60bddSLiu Xiang 	chip->soc = (soc >> 8);
1208c0984e5SSebastian Reichel }
1218c0984e5SSebastian Reichel 
1228c0984e5SSebastian Reichel static void max17040_get_version(struct i2c_client *client)
1238c0984e5SSebastian Reichel {
12414d60bddSLiu Xiang 	u16 version;
1258c0984e5SSebastian Reichel 
12614d60bddSLiu Xiang 	version = max17040_read_reg(client, MAX17040_VER);
1278c0984e5SSebastian Reichel 
12814d60bddSLiu Xiang 	dev_info(&client->dev, "MAX17040 Fuel-Gauge Ver 0x%x\n", version);
1298c0984e5SSebastian Reichel }
1308c0984e5SSebastian Reichel 
1318c0984e5SSebastian Reichel static void max17040_get_online(struct i2c_client *client)
1328c0984e5SSebastian Reichel {
1338c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
1348c0984e5SSebastian Reichel 
1358c0984e5SSebastian Reichel 	if (chip->pdata && chip->pdata->battery_online)
1368c0984e5SSebastian Reichel 		chip->online = chip->pdata->battery_online();
1378c0984e5SSebastian Reichel 	else
1388c0984e5SSebastian Reichel 		chip->online = 1;
1398c0984e5SSebastian Reichel }
1408c0984e5SSebastian Reichel 
1418c0984e5SSebastian Reichel static void max17040_get_status(struct i2c_client *client)
1428c0984e5SSebastian Reichel {
1438c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
1448c0984e5SSebastian Reichel 
1458c0984e5SSebastian Reichel 	if (!chip->pdata || !chip->pdata->charger_online
1468c0984e5SSebastian Reichel 			|| !chip->pdata->charger_enable) {
1478c0984e5SSebastian Reichel 		chip->status = POWER_SUPPLY_STATUS_UNKNOWN;
1488c0984e5SSebastian Reichel 		return;
1498c0984e5SSebastian Reichel 	}
1508c0984e5SSebastian Reichel 
1518c0984e5SSebastian Reichel 	if (chip->pdata->charger_online()) {
1528c0984e5SSebastian Reichel 		if (chip->pdata->charger_enable())
1538c0984e5SSebastian Reichel 			chip->status = POWER_SUPPLY_STATUS_CHARGING;
1548c0984e5SSebastian Reichel 		else
1558c0984e5SSebastian Reichel 			chip->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1568c0984e5SSebastian Reichel 	} else {
1578c0984e5SSebastian Reichel 		chip->status = POWER_SUPPLY_STATUS_DISCHARGING;
1588c0984e5SSebastian Reichel 	}
1598c0984e5SSebastian Reichel 
1608c0984e5SSebastian Reichel 	if (chip->soc > MAX17040_BATTERY_FULL)
1618c0984e5SSebastian Reichel 		chip->status = POWER_SUPPLY_STATUS_FULL;
1628c0984e5SSebastian Reichel }
1638c0984e5SSebastian Reichel 
164*2e17ed94SMatheus Castello static void max17040_check_changes(struct i2c_client *client)
165*2e17ed94SMatheus Castello {
166*2e17ed94SMatheus Castello 	max17040_get_vcell(client);
167*2e17ed94SMatheus Castello 	max17040_get_soc(client);
168*2e17ed94SMatheus Castello 	max17040_get_online(client);
169*2e17ed94SMatheus Castello 	max17040_get_status(client);
170*2e17ed94SMatheus Castello }
171*2e17ed94SMatheus Castello 
1728c0984e5SSebastian Reichel static void max17040_work(struct work_struct *work)
1738c0984e5SSebastian Reichel {
1748c0984e5SSebastian Reichel 	struct max17040_chip *chip;
1758c0984e5SSebastian Reichel 
1768c0984e5SSebastian Reichel 	chip = container_of(work, struct max17040_chip, work.work);
177*2e17ed94SMatheus Castello 	max17040_check_changes(chip->client);
1788c0984e5SSebastian Reichel 
1798c0984e5SSebastian Reichel 	queue_delayed_work(system_power_efficient_wq, &chip->work,
1808c0984e5SSebastian Reichel 			   MAX17040_DELAY);
1818c0984e5SSebastian Reichel }
1828c0984e5SSebastian Reichel 
183*2e17ed94SMatheus Castello static irqreturn_t max17040_thread_handler(int id, void *dev)
184*2e17ed94SMatheus Castello {
185*2e17ed94SMatheus Castello 	struct max17040_chip *chip = dev;
186*2e17ed94SMatheus Castello 	struct i2c_client *client = chip->client;
187*2e17ed94SMatheus Castello 
188*2e17ed94SMatheus Castello 	dev_warn(&client->dev, "IRQ: Alert battery low level");
189*2e17ed94SMatheus Castello 	/* read registers */
190*2e17ed94SMatheus Castello 	max17040_check_changes(chip->client);
191*2e17ed94SMatheus Castello 
192*2e17ed94SMatheus Castello 	/* send uevent */
193*2e17ed94SMatheus Castello 	power_supply_changed(chip->battery);
194*2e17ed94SMatheus Castello 
195*2e17ed94SMatheus Castello 	return IRQ_HANDLED;
196*2e17ed94SMatheus Castello }
197*2e17ed94SMatheus Castello 
198*2e17ed94SMatheus Castello static int max17040_enable_alert_irq(struct max17040_chip *chip)
199*2e17ed94SMatheus Castello {
200*2e17ed94SMatheus Castello 	struct i2c_client *client = chip->client;
201*2e17ed94SMatheus Castello 	unsigned int flags;
202*2e17ed94SMatheus Castello 	int ret;
203*2e17ed94SMatheus Castello 
204*2e17ed94SMatheus Castello 	flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
205*2e17ed94SMatheus Castello 	ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
206*2e17ed94SMatheus Castello 					max17040_thread_handler, flags,
207*2e17ed94SMatheus Castello 					chip->battery->desc->name, chip);
208*2e17ed94SMatheus Castello 
209*2e17ed94SMatheus Castello 	return ret;
210*2e17ed94SMatheus Castello }
211*2e17ed94SMatheus Castello 
2128c0984e5SSebastian Reichel static enum power_supply_property max17040_battery_props[] = {
2138c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
2148c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_ONLINE,
2158c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
2168c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
2178c0984e5SSebastian Reichel };
2188c0984e5SSebastian Reichel 
2198c0984e5SSebastian Reichel static const struct power_supply_desc max17040_battery_desc = {
2208c0984e5SSebastian Reichel 	.name		= "battery",
2218c0984e5SSebastian Reichel 	.type		= POWER_SUPPLY_TYPE_BATTERY,
2228c0984e5SSebastian Reichel 	.get_property	= max17040_get_property,
2238c0984e5SSebastian Reichel 	.properties	= max17040_battery_props,
2248c0984e5SSebastian Reichel 	.num_properties	= ARRAY_SIZE(max17040_battery_props),
2258c0984e5SSebastian Reichel };
2268c0984e5SSebastian Reichel 
2278c0984e5SSebastian Reichel static int max17040_probe(struct i2c_client *client,
2288c0984e5SSebastian Reichel 			const struct i2c_device_id *id)
2298c0984e5SSebastian Reichel {
2304e9c406dSWolfram Sang 	struct i2c_adapter *adapter = client->adapter;
2318c0984e5SSebastian Reichel 	struct power_supply_config psy_cfg = {};
2328c0984e5SSebastian Reichel 	struct max17040_chip *chip;
2338c0984e5SSebastian Reichel 
2348c0984e5SSebastian Reichel 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
2358c0984e5SSebastian Reichel 		return -EIO;
2368c0984e5SSebastian Reichel 
2378c0984e5SSebastian Reichel 	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
2388c0984e5SSebastian Reichel 	if (!chip)
2398c0984e5SSebastian Reichel 		return -ENOMEM;
2408c0984e5SSebastian Reichel 
2418c0984e5SSebastian Reichel 	chip->client = client;
2428c0984e5SSebastian Reichel 	chip->pdata = client->dev.platform_data;
2438c0984e5SSebastian Reichel 
2448c0984e5SSebastian Reichel 	i2c_set_clientdata(client, chip);
2458c0984e5SSebastian Reichel 	psy_cfg.drv_data = chip;
2468c0984e5SSebastian Reichel 
2478c0984e5SSebastian Reichel 	chip->battery = power_supply_register(&client->dev,
2488c0984e5SSebastian Reichel 				&max17040_battery_desc, &psy_cfg);
2498c0984e5SSebastian Reichel 	if (IS_ERR(chip->battery)) {
2508c0984e5SSebastian Reichel 		dev_err(&client->dev, "failed: power supply register\n");
2518c0984e5SSebastian Reichel 		return PTR_ERR(chip->battery);
2528c0984e5SSebastian Reichel 	}
2538c0984e5SSebastian Reichel 
2548c0984e5SSebastian Reichel 	max17040_reset(client);
2558c0984e5SSebastian Reichel 	max17040_get_version(client);
2568c0984e5SSebastian Reichel 
257*2e17ed94SMatheus Castello 	/* check interrupt */
258*2e17ed94SMatheus Castello 	if (client->irq && of_device_is_compatible(client->dev.of_node,
259*2e17ed94SMatheus Castello 						   "maxim,max77836-battery")) {
260*2e17ed94SMatheus Castello 		int ret;
261*2e17ed94SMatheus Castello 
262*2e17ed94SMatheus Castello 		ret = max17040_enable_alert_irq(chip);
263*2e17ed94SMatheus Castello 		if (ret) {
264*2e17ed94SMatheus Castello 			client->irq = 0;
265*2e17ed94SMatheus Castello 			dev_warn(&client->dev,
266*2e17ed94SMatheus Castello 				 "Failed to get IRQ err %d\n", ret);
267*2e17ed94SMatheus Castello 		}
268*2e17ed94SMatheus Castello 	}
269*2e17ed94SMatheus Castello 
2708c0984e5SSebastian Reichel 	INIT_DEFERRABLE_WORK(&chip->work, max17040_work);
2718c0984e5SSebastian Reichel 	queue_delayed_work(system_power_efficient_wq, &chip->work,
2728c0984e5SSebastian Reichel 			   MAX17040_DELAY);
2738c0984e5SSebastian Reichel 
2748c0984e5SSebastian Reichel 	return 0;
2758c0984e5SSebastian Reichel }
2768c0984e5SSebastian Reichel 
2778c0984e5SSebastian Reichel static int max17040_remove(struct i2c_client *client)
2788c0984e5SSebastian Reichel {
2798c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
2808c0984e5SSebastian Reichel 
2818c0984e5SSebastian Reichel 	power_supply_unregister(chip->battery);
2828c0984e5SSebastian Reichel 	cancel_delayed_work(&chip->work);
2838c0984e5SSebastian Reichel 	return 0;
2848c0984e5SSebastian Reichel }
2858c0984e5SSebastian Reichel 
2868c0984e5SSebastian Reichel #ifdef CONFIG_PM_SLEEP
2878c0984e5SSebastian Reichel 
2888c0984e5SSebastian Reichel static int max17040_suspend(struct device *dev)
2898c0984e5SSebastian Reichel {
2908c0984e5SSebastian Reichel 	struct i2c_client *client = to_i2c_client(dev);
2918c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
2928c0984e5SSebastian Reichel 
2938c0984e5SSebastian Reichel 	cancel_delayed_work(&chip->work);
294*2e17ed94SMatheus Castello 
295*2e17ed94SMatheus Castello 	if (client->irq) {
296*2e17ed94SMatheus Castello 		if (device_may_wakeup(dev))
297*2e17ed94SMatheus Castello 			enable_irq_wake(client->irq);
298*2e17ed94SMatheus Castello 		else
299*2e17ed94SMatheus Castello 			disable_irq_wake(client->irq);
300*2e17ed94SMatheus Castello 	}
301*2e17ed94SMatheus Castello 
3028c0984e5SSebastian Reichel 	return 0;
3038c0984e5SSebastian Reichel }
3048c0984e5SSebastian Reichel 
3058c0984e5SSebastian Reichel static int max17040_resume(struct device *dev)
3068c0984e5SSebastian Reichel {
3078c0984e5SSebastian Reichel 	struct i2c_client *client = to_i2c_client(dev);
3088c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
3098c0984e5SSebastian Reichel 
3108c0984e5SSebastian Reichel 	queue_delayed_work(system_power_efficient_wq, &chip->work,
3118c0984e5SSebastian Reichel 			   MAX17040_DELAY);
312*2e17ed94SMatheus Castello 
313*2e17ed94SMatheus Castello 	if (client->irq) {
314*2e17ed94SMatheus Castello 		if (device_may_wakeup(dev))
315*2e17ed94SMatheus Castello 			disable_irq_wake(client->irq);
316*2e17ed94SMatheus Castello 		else
317*2e17ed94SMatheus Castello 			enable_irq_wake(client->irq);
318*2e17ed94SMatheus Castello 	}
319*2e17ed94SMatheus Castello 
3208c0984e5SSebastian Reichel 	return 0;
3218c0984e5SSebastian Reichel }
3228c0984e5SSebastian Reichel 
3238c0984e5SSebastian Reichel static SIMPLE_DEV_PM_OPS(max17040_pm_ops, max17040_suspend, max17040_resume);
3248c0984e5SSebastian Reichel #define MAX17040_PM_OPS (&max17040_pm_ops)
3258c0984e5SSebastian Reichel 
3268c0984e5SSebastian Reichel #else
3278c0984e5SSebastian Reichel 
3288c0984e5SSebastian Reichel #define MAX17040_PM_OPS NULL
3298c0984e5SSebastian Reichel 
3308c0984e5SSebastian Reichel #endif /* CONFIG_PM_SLEEP */
3318c0984e5SSebastian Reichel 
3328c0984e5SSebastian Reichel static const struct i2c_device_id max17040_id[] = {
3338c0984e5SSebastian Reichel 	{ "max17040" },
3348c0984e5SSebastian Reichel 	{ "max77836-battery" },
3358c0984e5SSebastian Reichel 	{ }
3368c0984e5SSebastian Reichel };
3378c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(i2c, max17040_id);
3388c0984e5SSebastian Reichel 
339da28122cSJavier Martinez Canillas static const struct of_device_id max17040_of_match[] = {
340da28122cSJavier Martinez Canillas 	{ .compatible = "maxim,max17040" },
341da28122cSJavier Martinez Canillas 	{ .compatible = "maxim,max77836-battery" },
342da28122cSJavier Martinez Canillas 	{ },
343da28122cSJavier Martinez Canillas };
344da28122cSJavier Martinez Canillas MODULE_DEVICE_TABLE(of, max17040_of_match);
345da28122cSJavier Martinez Canillas 
3468c0984e5SSebastian Reichel static struct i2c_driver max17040_i2c_driver = {
3478c0984e5SSebastian Reichel 	.driver	= {
3488c0984e5SSebastian Reichel 		.name	= "max17040",
349da28122cSJavier Martinez Canillas 		.of_match_table = max17040_of_match,
3508c0984e5SSebastian Reichel 		.pm	= MAX17040_PM_OPS,
3518c0984e5SSebastian Reichel 	},
3528c0984e5SSebastian Reichel 	.probe		= max17040_probe,
3538c0984e5SSebastian Reichel 	.remove		= max17040_remove,
3548c0984e5SSebastian Reichel 	.id_table	= max17040_id,
3558c0984e5SSebastian Reichel };
3568c0984e5SSebastian Reichel module_i2c_driver(max17040_i2c_driver);
3578c0984e5SSebastian Reichel 
3588c0984e5SSebastian Reichel MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
3598c0984e5SSebastian Reichel MODULE_DESCRIPTION("MAX17040 Fuel Gauge");
3608c0984e5SSebastian Reichel MODULE_LICENSE("GPL");
361