xref: /openbmc/linux/drivers/power/supply/max17040_battery.c (revision 2f38dc4d9190cd97b8ceb88be5b835ee5feb44d1)
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>
162e17ed94SMatheus 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 
32cccdd0caSMatheus Castello #define MAX17040_ATHD_MASK		0xFFC0
33cccdd0caSMatheus Castello #define MAX17040_ATHD_DEFAULT_POWER_UP	4
34cccdd0caSMatheus Castello 
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;
49cccdd0caSMatheus Castello 	/* Low alert threshold from 32% to 1% of the State of Charge */
50cccdd0caSMatheus Castello 	u32 low_soc_alert;
518c0984e5SSebastian Reichel };
528c0984e5SSebastian Reichel 
538c0984e5SSebastian Reichel static int max17040_get_property(struct power_supply *psy,
548c0984e5SSebastian Reichel 			    enum power_supply_property psp,
558c0984e5SSebastian Reichel 			    union power_supply_propval *val)
568c0984e5SSebastian Reichel {
578c0984e5SSebastian Reichel 	struct max17040_chip *chip = power_supply_get_drvdata(psy);
588c0984e5SSebastian Reichel 
598c0984e5SSebastian Reichel 	switch (psp) {
608c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_STATUS:
618c0984e5SSebastian Reichel 		val->intval = chip->status;
628c0984e5SSebastian Reichel 		break;
638c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_ONLINE:
648c0984e5SSebastian Reichel 		val->intval = chip->online;
658c0984e5SSebastian Reichel 		break;
668c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
678c0984e5SSebastian Reichel 		val->intval = chip->vcell;
688c0984e5SSebastian Reichel 		break;
698c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CAPACITY:
708c0984e5SSebastian Reichel 		val->intval = chip->soc;
718c0984e5SSebastian Reichel 		break;
72*2f38dc4dSMatheus Castello 	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
73*2f38dc4dSMatheus Castello 		val->intval = chip->low_soc_alert;
74*2f38dc4dSMatheus Castello 		break;
758c0984e5SSebastian Reichel 	default:
768c0984e5SSebastian Reichel 		return -EINVAL;
778c0984e5SSebastian Reichel 	}
788c0984e5SSebastian Reichel 	return 0;
798c0984e5SSebastian Reichel }
808c0984e5SSebastian Reichel 
8114d60bddSLiu Xiang static int max17040_write_reg(struct i2c_client *client, int reg, u16 value)
828c0984e5SSebastian Reichel {
838c0984e5SSebastian Reichel 	int ret;
848c0984e5SSebastian Reichel 
8514d60bddSLiu Xiang 	ret = i2c_smbus_write_word_swapped(client, reg, value);
868c0984e5SSebastian Reichel 
878c0984e5SSebastian Reichel 	if (ret < 0)
888c0984e5SSebastian Reichel 		dev_err(&client->dev, "%s: err %d\n", __func__, ret);
898c0984e5SSebastian Reichel 
908c0984e5SSebastian Reichel 	return ret;
918c0984e5SSebastian Reichel }
928c0984e5SSebastian Reichel 
938c0984e5SSebastian Reichel static int max17040_read_reg(struct i2c_client *client, int reg)
948c0984e5SSebastian Reichel {
958c0984e5SSebastian Reichel 	int ret;
968c0984e5SSebastian Reichel 
9714d60bddSLiu Xiang 	ret = i2c_smbus_read_word_swapped(client, reg);
988c0984e5SSebastian Reichel 
998c0984e5SSebastian Reichel 	if (ret < 0)
1008c0984e5SSebastian Reichel 		dev_err(&client->dev, "%s: err %d\n", __func__, ret);
1018c0984e5SSebastian Reichel 
1028c0984e5SSebastian Reichel 	return ret;
1038c0984e5SSebastian Reichel }
1048c0984e5SSebastian Reichel 
1058c0984e5SSebastian Reichel static void max17040_reset(struct i2c_client *client)
1068c0984e5SSebastian Reichel {
10714d60bddSLiu Xiang 	max17040_write_reg(client, MAX17040_CMD, 0x0054);
1088c0984e5SSebastian Reichel }
1098c0984e5SSebastian Reichel 
110cccdd0caSMatheus Castello static int max17040_set_low_soc_alert(struct i2c_client *client, u32 level)
111cccdd0caSMatheus Castello {
112cccdd0caSMatheus Castello 	int ret;
113cccdd0caSMatheus Castello 	u16 data;
114cccdd0caSMatheus Castello 
115cccdd0caSMatheus Castello 	level = 32 - level;
116cccdd0caSMatheus Castello 	data = max17040_read_reg(client, MAX17040_RCOMP);
117cccdd0caSMatheus Castello 	/* clear the alrt bit and set LSb 5 bits */
118cccdd0caSMatheus Castello 	data &= MAX17040_ATHD_MASK;
119cccdd0caSMatheus Castello 	data |= level;
120cccdd0caSMatheus Castello 	ret = max17040_write_reg(client, MAX17040_RCOMP, data);
121cccdd0caSMatheus Castello 
122cccdd0caSMatheus Castello 	return ret;
123cccdd0caSMatheus Castello }
124cccdd0caSMatheus Castello 
1258c0984e5SSebastian Reichel static void max17040_get_vcell(struct i2c_client *client)
1268c0984e5SSebastian Reichel {
1278c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
12814d60bddSLiu Xiang 	u16 vcell;
1298c0984e5SSebastian Reichel 
13014d60bddSLiu Xiang 	vcell = max17040_read_reg(client, MAX17040_VCELL);
1318c0984e5SSebastian Reichel 
1320383024fSJonathan Bakker 	chip->vcell = (vcell >> 4) * 1250;
1338c0984e5SSebastian Reichel }
1348c0984e5SSebastian Reichel 
1358c0984e5SSebastian Reichel static void max17040_get_soc(struct i2c_client *client)
1368c0984e5SSebastian Reichel {
1378c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
13814d60bddSLiu Xiang 	u16 soc;
1398c0984e5SSebastian Reichel 
14014d60bddSLiu Xiang 	soc = max17040_read_reg(client, MAX17040_SOC);
1418c0984e5SSebastian Reichel 
14214d60bddSLiu Xiang 	chip->soc = (soc >> 8);
1438c0984e5SSebastian Reichel }
1448c0984e5SSebastian Reichel 
1458c0984e5SSebastian Reichel static void max17040_get_version(struct i2c_client *client)
1468c0984e5SSebastian Reichel {
14714d60bddSLiu Xiang 	u16 version;
1488c0984e5SSebastian Reichel 
14914d60bddSLiu Xiang 	version = max17040_read_reg(client, MAX17040_VER);
1508c0984e5SSebastian Reichel 
15114d60bddSLiu Xiang 	dev_info(&client->dev, "MAX17040 Fuel-Gauge Ver 0x%x\n", version);
1528c0984e5SSebastian Reichel }
1538c0984e5SSebastian Reichel 
1548c0984e5SSebastian Reichel static void max17040_get_online(struct i2c_client *client)
1558c0984e5SSebastian Reichel {
1568c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
1578c0984e5SSebastian Reichel 
1588c0984e5SSebastian Reichel 	if (chip->pdata && chip->pdata->battery_online)
1598c0984e5SSebastian Reichel 		chip->online = chip->pdata->battery_online();
1608c0984e5SSebastian Reichel 	else
1618c0984e5SSebastian Reichel 		chip->online = 1;
1628c0984e5SSebastian Reichel }
1638c0984e5SSebastian Reichel 
1648c0984e5SSebastian Reichel static void max17040_get_status(struct i2c_client *client)
1658c0984e5SSebastian Reichel {
1668c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
1678c0984e5SSebastian Reichel 
1688c0984e5SSebastian Reichel 	if (!chip->pdata || !chip->pdata->charger_online
1698c0984e5SSebastian Reichel 			|| !chip->pdata->charger_enable) {
1708c0984e5SSebastian Reichel 		chip->status = POWER_SUPPLY_STATUS_UNKNOWN;
1718c0984e5SSebastian Reichel 		return;
1728c0984e5SSebastian Reichel 	}
1738c0984e5SSebastian Reichel 
1748c0984e5SSebastian Reichel 	if (chip->pdata->charger_online()) {
1758c0984e5SSebastian Reichel 		if (chip->pdata->charger_enable())
1768c0984e5SSebastian Reichel 			chip->status = POWER_SUPPLY_STATUS_CHARGING;
1778c0984e5SSebastian Reichel 		else
1788c0984e5SSebastian Reichel 			chip->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1798c0984e5SSebastian Reichel 	} else {
1808c0984e5SSebastian Reichel 		chip->status = POWER_SUPPLY_STATUS_DISCHARGING;
1818c0984e5SSebastian Reichel 	}
1828c0984e5SSebastian Reichel 
1838c0984e5SSebastian Reichel 	if (chip->soc > MAX17040_BATTERY_FULL)
1848c0984e5SSebastian Reichel 		chip->status = POWER_SUPPLY_STATUS_FULL;
1858c0984e5SSebastian Reichel }
1868c0984e5SSebastian Reichel 
187cccdd0caSMatheus Castello static int max17040_get_of_data(struct max17040_chip *chip)
188cccdd0caSMatheus Castello {
189cccdd0caSMatheus Castello 	struct device *dev = &chip->client->dev;
190cccdd0caSMatheus Castello 
191cccdd0caSMatheus Castello 	chip->low_soc_alert = MAX17040_ATHD_DEFAULT_POWER_UP;
192cccdd0caSMatheus Castello 	device_property_read_u32(dev,
193cccdd0caSMatheus Castello 				 "maxim,alert-low-soc-level",
194cccdd0caSMatheus Castello 				 &chip->low_soc_alert);
195cccdd0caSMatheus Castello 
196cccdd0caSMatheus Castello 	if (chip->low_soc_alert <= 0 || chip->low_soc_alert >= 33)
197cccdd0caSMatheus Castello 		return -EINVAL;
198cccdd0caSMatheus Castello 
199cccdd0caSMatheus Castello 	return 0;
200cccdd0caSMatheus Castello }
201cccdd0caSMatheus Castello 
2022e17ed94SMatheus Castello static void max17040_check_changes(struct i2c_client *client)
2032e17ed94SMatheus Castello {
2042e17ed94SMatheus Castello 	max17040_get_vcell(client);
2052e17ed94SMatheus Castello 	max17040_get_soc(client);
2062e17ed94SMatheus Castello 	max17040_get_online(client);
2072e17ed94SMatheus Castello 	max17040_get_status(client);
2082e17ed94SMatheus Castello }
2092e17ed94SMatheus Castello 
2108c0984e5SSebastian Reichel static void max17040_work(struct work_struct *work)
2118c0984e5SSebastian Reichel {
2128c0984e5SSebastian Reichel 	struct max17040_chip *chip;
213a08990eaSMatheus Castello 	int last_soc, last_status;
2148c0984e5SSebastian Reichel 
2158c0984e5SSebastian Reichel 	chip = container_of(work, struct max17040_chip, work.work);
216a08990eaSMatheus Castello 
217a08990eaSMatheus Castello 	/* store SOC and status to check changes */
218a08990eaSMatheus Castello 	last_soc = chip->soc;
219a08990eaSMatheus Castello 	last_status = chip->status;
2202e17ed94SMatheus Castello 	max17040_check_changes(chip->client);
2218c0984e5SSebastian Reichel 
222a08990eaSMatheus Castello 	/* check changes and send uevent */
223a08990eaSMatheus Castello 	if (last_soc != chip->soc || last_status != chip->status)
224a08990eaSMatheus Castello 		power_supply_changed(chip->battery);
225a08990eaSMatheus Castello 
2268c0984e5SSebastian Reichel 	queue_delayed_work(system_power_efficient_wq, &chip->work,
2278c0984e5SSebastian Reichel 			   MAX17040_DELAY);
2288c0984e5SSebastian Reichel }
2298c0984e5SSebastian Reichel 
2302e17ed94SMatheus Castello static irqreturn_t max17040_thread_handler(int id, void *dev)
2312e17ed94SMatheus Castello {
2322e17ed94SMatheus Castello 	struct max17040_chip *chip = dev;
2332e17ed94SMatheus Castello 	struct i2c_client *client = chip->client;
2342e17ed94SMatheus Castello 
2352e17ed94SMatheus Castello 	dev_warn(&client->dev, "IRQ: Alert battery low level");
2362e17ed94SMatheus Castello 	/* read registers */
2372e17ed94SMatheus Castello 	max17040_check_changes(chip->client);
2382e17ed94SMatheus Castello 
2392e17ed94SMatheus Castello 	/* send uevent */
2402e17ed94SMatheus Castello 	power_supply_changed(chip->battery);
2412e17ed94SMatheus Castello 
242cccdd0caSMatheus Castello 	/* reset alert bit */
243cccdd0caSMatheus Castello 	max17040_set_low_soc_alert(client, chip->low_soc_alert);
244cccdd0caSMatheus Castello 
2452e17ed94SMatheus Castello 	return IRQ_HANDLED;
2462e17ed94SMatheus Castello }
2472e17ed94SMatheus Castello 
2482e17ed94SMatheus Castello static int max17040_enable_alert_irq(struct max17040_chip *chip)
2492e17ed94SMatheus Castello {
2502e17ed94SMatheus Castello 	struct i2c_client *client = chip->client;
2512e17ed94SMatheus Castello 	unsigned int flags;
2522e17ed94SMatheus Castello 	int ret;
2532e17ed94SMatheus Castello 
2542e17ed94SMatheus Castello 	flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
2552e17ed94SMatheus Castello 	ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
2562e17ed94SMatheus Castello 					max17040_thread_handler, flags,
2572e17ed94SMatheus Castello 					chip->battery->desc->name, chip);
2582e17ed94SMatheus Castello 
2592e17ed94SMatheus Castello 	return ret;
2602e17ed94SMatheus Castello }
2612e17ed94SMatheus Castello 
262*2f38dc4dSMatheus Castello static int max17040_prop_writeable(struct power_supply *psy,
263*2f38dc4dSMatheus Castello 				   enum power_supply_property psp)
264*2f38dc4dSMatheus Castello {
265*2f38dc4dSMatheus Castello 	switch (psp) {
266*2f38dc4dSMatheus Castello 	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
267*2f38dc4dSMatheus Castello 		return 1;
268*2f38dc4dSMatheus Castello 	default:
269*2f38dc4dSMatheus Castello 		return 0;
270*2f38dc4dSMatheus Castello 	}
271*2f38dc4dSMatheus Castello }
272*2f38dc4dSMatheus Castello 
273*2f38dc4dSMatheus Castello static int max17040_set_property(struct power_supply *psy,
274*2f38dc4dSMatheus Castello 			    enum power_supply_property psp,
275*2f38dc4dSMatheus Castello 			    const union power_supply_propval *val)
276*2f38dc4dSMatheus Castello {
277*2f38dc4dSMatheus Castello 	struct max17040_chip *chip = power_supply_get_drvdata(psy);
278*2f38dc4dSMatheus Castello 	int ret;
279*2f38dc4dSMatheus Castello 
280*2f38dc4dSMatheus Castello 	switch (psp) {
281*2f38dc4dSMatheus Castello 	case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
282*2f38dc4dSMatheus Castello 		/* alert threshold can be programmed from 1% up to 32% */
283*2f38dc4dSMatheus Castello 		if ((val->intval < 1) || (val->intval > 32)) {
284*2f38dc4dSMatheus Castello 			ret = -EINVAL;
285*2f38dc4dSMatheus Castello 			break;
286*2f38dc4dSMatheus Castello 		}
287*2f38dc4dSMatheus Castello 		ret = max17040_set_low_soc_alert(chip->client, val->intval);
288*2f38dc4dSMatheus Castello 		chip->low_soc_alert = val->intval;
289*2f38dc4dSMatheus Castello 		break;
290*2f38dc4dSMatheus Castello 	default:
291*2f38dc4dSMatheus Castello 		ret = -EINVAL;
292*2f38dc4dSMatheus Castello 	}
293*2f38dc4dSMatheus Castello 
294*2f38dc4dSMatheus Castello 	return ret;
295*2f38dc4dSMatheus Castello }
296*2f38dc4dSMatheus Castello 
2978c0984e5SSebastian Reichel static enum power_supply_property max17040_battery_props[] = {
2988c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
2998c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_ONLINE,
3008c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
3018c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
302*2f38dc4dSMatheus Castello 	POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN,
3038c0984e5SSebastian Reichel };
3048c0984e5SSebastian Reichel 
3058c0984e5SSebastian Reichel static const struct power_supply_desc max17040_battery_desc = {
3068c0984e5SSebastian Reichel 	.name			= "battery",
3078c0984e5SSebastian Reichel 	.type			= POWER_SUPPLY_TYPE_BATTERY,
3088c0984e5SSebastian Reichel 	.get_property		= max17040_get_property,
309*2f38dc4dSMatheus Castello 	.set_property		= max17040_set_property,
310*2f38dc4dSMatheus Castello 	.property_is_writeable  = max17040_prop_writeable,
3118c0984e5SSebastian Reichel 	.properties		= max17040_battery_props,
3128c0984e5SSebastian Reichel 	.num_properties		= ARRAY_SIZE(max17040_battery_props),
3138c0984e5SSebastian Reichel };
3148c0984e5SSebastian Reichel 
3158c0984e5SSebastian Reichel static int max17040_probe(struct i2c_client *client,
3168c0984e5SSebastian Reichel 			const struct i2c_device_id *id)
3178c0984e5SSebastian Reichel {
3184e9c406dSWolfram Sang 	struct i2c_adapter *adapter = client->adapter;
3198c0984e5SSebastian Reichel 	struct power_supply_config psy_cfg = {};
3208c0984e5SSebastian Reichel 	struct max17040_chip *chip;
321cccdd0caSMatheus Castello 	int ret;
3228c0984e5SSebastian Reichel 
3238c0984e5SSebastian Reichel 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
3248c0984e5SSebastian Reichel 		return -EIO;
3258c0984e5SSebastian Reichel 
3268c0984e5SSebastian Reichel 	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
3278c0984e5SSebastian Reichel 	if (!chip)
3288c0984e5SSebastian Reichel 		return -ENOMEM;
3298c0984e5SSebastian Reichel 
3308c0984e5SSebastian Reichel 	chip->client = client;
3318c0984e5SSebastian Reichel 	chip->pdata = client->dev.platform_data;
332cccdd0caSMatheus Castello 	ret = max17040_get_of_data(chip);
333cccdd0caSMatheus Castello 	if (ret) {
334cccdd0caSMatheus Castello 		dev_err(&client->dev,
335cccdd0caSMatheus Castello 			"failed: low SOC alert OF data out of bounds\n");
336cccdd0caSMatheus Castello 		return ret;
337cccdd0caSMatheus Castello 	}
3388c0984e5SSebastian Reichel 
3398c0984e5SSebastian Reichel 	i2c_set_clientdata(client, chip);
3408c0984e5SSebastian Reichel 	psy_cfg.drv_data = chip;
3418c0984e5SSebastian Reichel 
3428c0984e5SSebastian Reichel 	chip->battery = power_supply_register(&client->dev,
3438c0984e5SSebastian Reichel 				&max17040_battery_desc, &psy_cfg);
3448c0984e5SSebastian Reichel 	if (IS_ERR(chip->battery)) {
3458c0984e5SSebastian Reichel 		dev_err(&client->dev, "failed: power supply register\n");
3468c0984e5SSebastian Reichel 		return PTR_ERR(chip->battery);
3478c0984e5SSebastian Reichel 	}
3488c0984e5SSebastian Reichel 
3498c0984e5SSebastian Reichel 	max17040_reset(client);
3508c0984e5SSebastian Reichel 	max17040_get_version(client);
3518c0984e5SSebastian Reichel 
3522e17ed94SMatheus Castello 	/* check interrupt */
3532e17ed94SMatheus Castello 	if (client->irq && of_device_is_compatible(client->dev.of_node,
3542e17ed94SMatheus Castello 						   "maxim,max77836-battery")) {
355cccdd0caSMatheus Castello 		ret = max17040_set_low_soc_alert(client, chip->low_soc_alert);
356cccdd0caSMatheus Castello 		if (ret) {
357cccdd0caSMatheus Castello 			dev_err(&client->dev,
358cccdd0caSMatheus Castello 				"Failed to set low SOC alert: err %d\n", ret);
359cccdd0caSMatheus Castello 			return ret;
360cccdd0caSMatheus Castello 		}
3612e17ed94SMatheus Castello 
3622e17ed94SMatheus Castello 		ret = max17040_enable_alert_irq(chip);
3632e17ed94SMatheus Castello 		if (ret) {
3642e17ed94SMatheus Castello 			client->irq = 0;
3652e17ed94SMatheus Castello 			dev_warn(&client->dev,
3662e17ed94SMatheus Castello 				 "Failed to get IRQ err %d\n", ret);
3672e17ed94SMatheus Castello 		}
3682e17ed94SMatheus Castello 	}
3692e17ed94SMatheus Castello 
3708c0984e5SSebastian Reichel 	INIT_DEFERRABLE_WORK(&chip->work, max17040_work);
3718c0984e5SSebastian Reichel 	queue_delayed_work(system_power_efficient_wq, &chip->work,
3728c0984e5SSebastian Reichel 			   MAX17040_DELAY);
3738c0984e5SSebastian Reichel 
3748c0984e5SSebastian Reichel 	return 0;
3758c0984e5SSebastian Reichel }
3768c0984e5SSebastian Reichel 
3778c0984e5SSebastian Reichel static int max17040_remove(struct i2c_client *client)
3788c0984e5SSebastian Reichel {
3798c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
3808c0984e5SSebastian Reichel 
3818c0984e5SSebastian Reichel 	power_supply_unregister(chip->battery);
3828c0984e5SSebastian Reichel 	cancel_delayed_work(&chip->work);
3838c0984e5SSebastian Reichel 	return 0;
3848c0984e5SSebastian Reichel }
3858c0984e5SSebastian Reichel 
3868c0984e5SSebastian Reichel #ifdef CONFIG_PM_SLEEP
3878c0984e5SSebastian Reichel 
3888c0984e5SSebastian Reichel static int max17040_suspend(struct device *dev)
3898c0984e5SSebastian Reichel {
3908c0984e5SSebastian Reichel 	struct i2c_client *client = to_i2c_client(dev);
3918c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
3928c0984e5SSebastian Reichel 
3938c0984e5SSebastian Reichel 	cancel_delayed_work(&chip->work);
3942e17ed94SMatheus Castello 
395e29242adSMarek Szyprowski 	if (client->irq && device_may_wakeup(dev))
3962e17ed94SMatheus Castello 		enable_irq_wake(client->irq);
3972e17ed94SMatheus Castello 
3988c0984e5SSebastian Reichel 	return 0;
3998c0984e5SSebastian Reichel }
4008c0984e5SSebastian Reichel 
4018c0984e5SSebastian Reichel static int max17040_resume(struct device *dev)
4028c0984e5SSebastian Reichel {
4038c0984e5SSebastian Reichel 	struct i2c_client *client = to_i2c_client(dev);
4048c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
4058c0984e5SSebastian Reichel 
4068c0984e5SSebastian Reichel 	queue_delayed_work(system_power_efficient_wq, &chip->work,
4078c0984e5SSebastian Reichel 			   MAX17040_DELAY);
4082e17ed94SMatheus Castello 
409e29242adSMarek Szyprowski 	if (client->irq && device_may_wakeup(dev))
4102e17ed94SMatheus Castello 		disable_irq_wake(client->irq);
4112e17ed94SMatheus Castello 
4128c0984e5SSebastian Reichel 	return 0;
4138c0984e5SSebastian Reichel }
4148c0984e5SSebastian Reichel 
4158c0984e5SSebastian Reichel static SIMPLE_DEV_PM_OPS(max17040_pm_ops, max17040_suspend, max17040_resume);
4168c0984e5SSebastian Reichel #define MAX17040_PM_OPS (&max17040_pm_ops)
4178c0984e5SSebastian Reichel 
4188c0984e5SSebastian Reichel #else
4198c0984e5SSebastian Reichel 
4208c0984e5SSebastian Reichel #define MAX17040_PM_OPS NULL
4218c0984e5SSebastian Reichel 
4228c0984e5SSebastian Reichel #endif /* CONFIG_PM_SLEEP */
4238c0984e5SSebastian Reichel 
4248c0984e5SSebastian Reichel static const struct i2c_device_id max17040_id[] = {
4258c0984e5SSebastian Reichel 	{ "max17040" },
4268c0984e5SSebastian Reichel 	{ "max77836-battery" },
4278c0984e5SSebastian Reichel 	{ }
4288c0984e5SSebastian Reichel };
4298c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(i2c, max17040_id);
4308c0984e5SSebastian Reichel 
431da28122cSJavier Martinez Canillas static const struct of_device_id max17040_of_match[] = {
432da28122cSJavier Martinez Canillas 	{ .compatible = "maxim,max17040" },
433da28122cSJavier Martinez Canillas 	{ .compatible = "maxim,max77836-battery" },
434da28122cSJavier Martinez Canillas 	{ },
435da28122cSJavier Martinez Canillas };
436da28122cSJavier Martinez Canillas MODULE_DEVICE_TABLE(of, max17040_of_match);
437da28122cSJavier Martinez Canillas 
4388c0984e5SSebastian Reichel static struct i2c_driver max17040_i2c_driver = {
4398c0984e5SSebastian Reichel 	.driver	= {
4408c0984e5SSebastian Reichel 		.name	= "max17040",
441da28122cSJavier Martinez Canillas 		.of_match_table = max17040_of_match,
4428c0984e5SSebastian Reichel 		.pm	= MAX17040_PM_OPS,
4438c0984e5SSebastian Reichel 	},
4448c0984e5SSebastian Reichel 	.probe		= max17040_probe,
4458c0984e5SSebastian Reichel 	.remove		= max17040_remove,
4468c0984e5SSebastian Reichel 	.id_table	= max17040_id,
4478c0984e5SSebastian Reichel };
4488c0984e5SSebastian Reichel module_i2c_driver(max17040_i2c_driver);
4498c0984e5SSebastian Reichel 
4508c0984e5SSebastian Reichel MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
4518c0984e5SSebastian Reichel MODULE_DESCRIPTION("MAX17040 Fuel Gauge");
4528c0984e5SSebastian Reichel MODULE_LICENSE("GPL");
453