xref: /openbmc/linux/drivers/power/supply/max17040_battery.c (revision e29242ad813c71e8e6a22c4f13f420a2297ac716)
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;
728c0984e5SSebastian Reichel 	default:
738c0984e5SSebastian Reichel 		return -EINVAL;
748c0984e5SSebastian Reichel 	}
758c0984e5SSebastian Reichel 	return 0;
768c0984e5SSebastian Reichel }
778c0984e5SSebastian Reichel 
7814d60bddSLiu Xiang static int max17040_write_reg(struct i2c_client *client, int reg, u16 value)
798c0984e5SSebastian Reichel {
808c0984e5SSebastian Reichel 	int ret;
818c0984e5SSebastian Reichel 
8214d60bddSLiu Xiang 	ret = i2c_smbus_write_word_swapped(client, reg, value);
838c0984e5SSebastian Reichel 
848c0984e5SSebastian Reichel 	if (ret < 0)
858c0984e5SSebastian Reichel 		dev_err(&client->dev, "%s: err %d\n", __func__, ret);
868c0984e5SSebastian Reichel 
878c0984e5SSebastian Reichel 	return ret;
888c0984e5SSebastian Reichel }
898c0984e5SSebastian Reichel 
908c0984e5SSebastian Reichel static int max17040_read_reg(struct i2c_client *client, int reg)
918c0984e5SSebastian Reichel {
928c0984e5SSebastian Reichel 	int ret;
938c0984e5SSebastian Reichel 
9414d60bddSLiu Xiang 	ret = i2c_smbus_read_word_swapped(client, reg);
958c0984e5SSebastian Reichel 
968c0984e5SSebastian Reichel 	if (ret < 0)
978c0984e5SSebastian Reichel 		dev_err(&client->dev, "%s: err %d\n", __func__, ret);
988c0984e5SSebastian Reichel 
998c0984e5SSebastian Reichel 	return ret;
1008c0984e5SSebastian Reichel }
1018c0984e5SSebastian Reichel 
1028c0984e5SSebastian Reichel static void max17040_reset(struct i2c_client *client)
1038c0984e5SSebastian Reichel {
10414d60bddSLiu Xiang 	max17040_write_reg(client, MAX17040_CMD, 0x0054);
1058c0984e5SSebastian Reichel }
1068c0984e5SSebastian Reichel 
107cccdd0caSMatheus Castello static int max17040_set_low_soc_alert(struct i2c_client *client, u32 level)
108cccdd0caSMatheus Castello {
109cccdd0caSMatheus Castello 	int ret;
110cccdd0caSMatheus Castello 	u16 data;
111cccdd0caSMatheus Castello 
112cccdd0caSMatheus Castello 	level = 32 - level;
113cccdd0caSMatheus Castello 	data = max17040_read_reg(client, MAX17040_RCOMP);
114cccdd0caSMatheus Castello 	/* clear the alrt bit and set LSb 5 bits */
115cccdd0caSMatheus Castello 	data &= MAX17040_ATHD_MASK;
116cccdd0caSMatheus Castello 	data |= level;
117cccdd0caSMatheus Castello 	ret = max17040_write_reg(client, MAX17040_RCOMP, data);
118cccdd0caSMatheus Castello 
119cccdd0caSMatheus Castello 	return ret;
120cccdd0caSMatheus Castello }
121cccdd0caSMatheus Castello 
1228c0984e5SSebastian Reichel static void max17040_get_vcell(struct i2c_client *client)
1238c0984e5SSebastian Reichel {
1248c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
12514d60bddSLiu Xiang 	u16 vcell;
1268c0984e5SSebastian Reichel 
12714d60bddSLiu Xiang 	vcell = max17040_read_reg(client, MAX17040_VCELL);
1288c0984e5SSebastian Reichel 
12914d60bddSLiu Xiang 	chip->vcell = vcell;
1308c0984e5SSebastian Reichel }
1318c0984e5SSebastian Reichel 
1328c0984e5SSebastian Reichel static void max17040_get_soc(struct i2c_client *client)
1338c0984e5SSebastian Reichel {
1348c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
13514d60bddSLiu Xiang 	u16 soc;
1368c0984e5SSebastian Reichel 
13714d60bddSLiu Xiang 	soc = max17040_read_reg(client, MAX17040_SOC);
1388c0984e5SSebastian Reichel 
13914d60bddSLiu Xiang 	chip->soc = (soc >> 8);
1408c0984e5SSebastian Reichel }
1418c0984e5SSebastian Reichel 
1428c0984e5SSebastian Reichel static void max17040_get_version(struct i2c_client *client)
1438c0984e5SSebastian Reichel {
14414d60bddSLiu Xiang 	u16 version;
1458c0984e5SSebastian Reichel 
14614d60bddSLiu Xiang 	version = max17040_read_reg(client, MAX17040_VER);
1478c0984e5SSebastian Reichel 
14814d60bddSLiu Xiang 	dev_info(&client->dev, "MAX17040 Fuel-Gauge Ver 0x%x\n", version);
1498c0984e5SSebastian Reichel }
1508c0984e5SSebastian Reichel 
1518c0984e5SSebastian Reichel static void max17040_get_online(struct i2c_client *client)
1528c0984e5SSebastian Reichel {
1538c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
1548c0984e5SSebastian Reichel 
1558c0984e5SSebastian Reichel 	if (chip->pdata && chip->pdata->battery_online)
1568c0984e5SSebastian Reichel 		chip->online = chip->pdata->battery_online();
1578c0984e5SSebastian Reichel 	else
1588c0984e5SSebastian Reichel 		chip->online = 1;
1598c0984e5SSebastian Reichel }
1608c0984e5SSebastian Reichel 
1618c0984e5SSebastian Reichel static void max17040_get_status(struct i2c_client *client)
1628c0984e5SSebastian Reichel {
1638c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
1648c0984e5SSebastian Reichel 
1658c0984e5SSebastian Reichel 	if (!chip->pdata || !chip->pdata->charger_online
1668c0984e5SSebastian Reichel 			|| !chip->pdata->charger_enable) {
1678c0984e5SSebastian Reichel 		chip->status = POWER_SUPPLY_STATUS_UNKNOWN;
1688c0984e5SSebastian Reichel 		return;
1698c0984e5SSebastian Reichel 	}
1708c0984e5SSebastian Reichel 
1718c0984e5SSebastian Reichel 	if (chip->pdata->charger_online()) {
1728c0984e5SSebastian Reichel 		if (chip->pdata->charger_enable())
1738c0984e5SSebastian Reichel 			chip->status = POWER_SUPPLY_STATUS_CHARGING;
1748c0984e5SSebastian Reichel 		else
1758c0984e5SSebastian Reichel 			chip->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1768c0984e5SSebastian Reichel 	} else {
1778c0984e5SSebastian Reichel 		chip->status = POWER_SUPPLY_STATUS_DISCHARGING;
1788c0984e5SSebastian Reichel 	}
1798c0984e5SSebastian Reichel 
1808c0984e5SSebastian Reichel 	if (chip->soc > MAX17040_BATTERY_FULL)
1818c0984e5SSebastian Reichel 		chip->status = POWER_SUPPLY_STATUS_FULL;
1828c0984e5SSebastian Reichel }
1838c0984e5SSebastian Reichel 
184cccdd0caSMatheus Castello static int max17040_get_of_data(struct max17040_chip *chip)
185cccdd0caSMatheus Castello {
186cccdd0caSMatheus Castello 	struct device *dev = &chip->client->dev;
187cccdd0caSMatheus Castello 
188cccdd0caSMatheus Castello 	chip->low_soc_alert = MAX17040_ATHD_DEFAULT_POWER_UP;
189cccdd0caSMatheus Castello 	device_property_read_u32(dev,
190cccdd0caSMatheus Castello 				 "maxim,alert-low-soc-level",
191cccdd0caSMatheus Castello 				 &chip->low_soc_alert);
192cccdd0caSMatheus Castello 
193cccdd0caSMatheus Castello 	if (chip->low_soc_alert <= 0 || chip->low_soc_alert >= 33)
194cccdd0caSMatheus Castello 		return -EINVAL;
195cccdd0caSMatheus Castello 
196cccdd0caSMatheus Castello 	return 0;
197cccdd0caSMatheus Castello }
198cccdd0caSMatheus Castello 
1992e17ed94SMatheus Castello static void max17040_check_changes(struct i2c_client *client)
2002e17ed94SMatheus Castello {
2012e17ed94SMatheus Castello 	max17040_get_vcell(client);
2022e17ed94SMatheus Castello 	max17040_get_soc(client);
2032e17ed94SMatheus Castello 	max17040_get_online(client);
2042e17ed94SMatheus Castello 	max17040_get_status(client);
2052e17ed94SMatheus Castello }
2062e17ed94SMatheus Castello 
2078c0984e5SSebastian Reichel static void max17040_work(struct work_struct *work)
2088c0984e5SSebastian Reichel {
2098c0984e5SSebastian Reichel 	struct max17040_chip *chip;
210a08990eaSMatheus Castello 	int last_soc, last_status;
2118c0984e5SSebastian Reichel 
2128c0984e5SSebastian Reichel 	chip = container_of(work, struct max17040_chip, work.work);
213a08990eaSMatheus Castello 
214a08990eaSMatheus Castello 	/* store SOC and status to check changes */
215a08990eaSMatheus Castello 	last_soc = chip->soc;
216a08990eaSMatheus Castello 	last_status = chip->status;
2172e17ed94SMatheus Castello 	max17040_check_changes(chip->client);
2188c0984e5SSebastian Reichel 
219a08990eaSMatheus Castello 	/* check changes and send uevent */
220a08990eaSMatheus Castello 	if (last_soc != chip->soc || last_status != chip->status)
221a08990eaSMatheus Castello 		power_supply_changed(chip->battery);
222a08990eaSMatheus Castello 
2238c0984e5SSebastian Reichel 	queue_delayed_work(system_power_efficient_wq, &chip->work,
2248c0984e5SSebastian Reichel 			   MAX17040_DELAY);
2258c0984e5SSebastian Reichel }
2268c0984e5SSebastian Reichel 
2272e17ed94SMatheus Castello static irqreturn_t max17040_thread_handler(int id, void *dev)
2282e17ed94SMatheus Castello {
2292e17ed94SMatheus Castello 	struct max17040_chip *chip = dev;
2302e17ed94SMatheus Castello 	struct i2c_client *client = chip->client;
2312e17ed94SMatheus Castello 
2322e17ed94SMatheus Castello 	dev_warn(&client->dev, "IRQ: Alert battery low level");
2332e17ed94SMatheus Castello 	/* read registers */
2342e17ed94SMatheus Castello 	max17040_check_changes(chip->client);
2352e17ed94SMatheus Castello 
2362e17ed94SMatheus Castello 	/* send uevent */
2372e17ed94SMatheus Castello 	power_supply_changed(chip->battery);
2382e17ed94SMatheus Castello 
239cccdd0caSMatheus Castello 	/* reset alert bit */
240cccdd0caSMatheus Castello 	max17040_set_low_soc_alert(client, chip->low_soc_alert);
241cccdd0caSMatheus Castello 
2422e17ed94SMatheus Castello 	return IRQ_HANDLED;
2432e17ed94SMatheus Castello }
2442e17ed94SMatheus Castello 
2452e17ed94SMatheus Castello static int max17040_enable_alert_irq(struct max17040_chip *chip)
2462e17ed94SMatheus Castello {
2472e17ed94SMatheus Castello 	struct i2c_client *client = chip->client;
2482e17ed94SMatheus Castello 	unsigned int flags;
2492e17ed94SMatheus Castello 	int ret;
2502e17ed94SMatheus Castello 
2512e17ed94SMatheus Castello 	flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
2522e17ed94SMatheus Castello 	ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
2532e17ed94SMatheus Castello 					max17040_thread_handler, flags,
2542e17ed94SMatheus Castello 					chip->battery->desc->name, chip);
2552e17ed94SMatheus Castello 
2562e17ed94SMatheus Castello 	return ret;
2572e17ed94SMatheus Castello }
2582e17ed94SMatheus Castello 
2598c0984e5SSebastian Reichel static enum power_supply_property max17040_battery_props[] = {
2608c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
2618c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_ONLINE,
2628c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
2638c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
2648c0984e5SSebastian Reichel };
2658c0984e5SSebastian Reichel 
2668c0984e5SSebastian Reichel static const struct power_supply_desc max17040_battery_desc = {
2678c0984e5SSebastian Reichel 	.name		= "battery",
2688c0984e5SSebastian Reichel 	.type		= POWER_SUPPLY_TYPE_BATTERY,
2698c0984e5SSebastian Reichel 	.get_property	= max17040_get_property,
2708c0984e5SSebastian Reichel 	.properties	= max17040_battery_props,
2718c0984e5SSebastian Reichel 	.num_properties	= ARRAY_SIZE(max17040_battery_props),
2728c0984e5SSebastian Reichel };
2738c0984e5SSebastian Reichel 
2748c0984e5SSebastian Reichel static int max17040_probe(struct i2c_client *client,
2758c0984e5SSebastian Reichel 			const struct i2c_device_id *id)
2768c0984e5SSebastian Reichel {
2774e9c406dSWolfram Sang 	struct i2c_adapter *adapter = client->adapter;
2788c0984e5SSebastian Reichel 	struct power_supply_config psy_cfg = {};
2798c0984e5SSebastian Reichel 	struct max17040_chip *chip;
280cccdd0caSMatheus Castello 	int ret;
2818c0984e5SSebastian Reichel 
2828c0984e5SSebastian Reichel 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
2838c0984e5SSebastian Reichel 		return -EIO;
2848c0984e5SSebastian Reichel 
2858c0984e5SSebastian Reichel 	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
2868c0984e5SSebastian Reichel 	if (!chip)
2878c0984e5SSebastian Reichel 		return -ENOMEM;
2888c0984e5SSebastian Reichel 
2898c0984e5SSebastian Reichel 	chip->client = client;
2908c0984e5SSebastian Reichel 	chip->pdata = client->dev.platform_data;
291cccdd0caSMatheus Castello 	ret = max17040_get_of_data(chip);
292cccdd0caSMatheus Castello 	if (ret) {
293cccdd0caSMatheus Castello 		dev_err(&client->dev,
294cccdd0caSMatheus Castello 			"failed: low SOC alert OF data out of bounds\n");
295cccdd0caSMatheus Castello 		return ret;
296cccdd0caSMatheus Castello 	}
2978c0984e5SSebastian Reichel 
2988c0984e5SSebastian Reichel 	i2c_set_clientdata(client, chip);
2998c0984e5SSebastian Reichel 	psy_cfg.drv_data = chip;
3008c0984e5SSebastian Reichel 
3018c0984e5SSebastian Reichel 	chip->battery = power_supply_register(&client->dev,
3028c0984e5SSebastian Reichel 				&max17040_battery_desc, &psy_cfg);
3038c0984e5SSebastian Reichel 	if (IS_ERR(chip->battery)) {
3048c0984e5SSebastian Reichel 		dev_err(&client->dev, "failed: power supply register\n");
3058c0984e5SSebastian Reichel 		return PTR_ERR(chip->battery);
3068c0984e5SSebastian Reichel 	}
3078c0984e5SSebastian Reichel 
3088c0984e5SSebastian Reichel 	max17040_reset(client);
3098c0984e5SSebastian Reichel 	max17040_get_version(client);
3108c0984e5SSebastian Reichel 
3112e17ed94SMatheus Castello 	/* check interrupt */
3122e17ed94SMatheus Castello 	if (client->irq && of_device_is_compatible(client->dev.of_node,
3132e17ed94SMatheus Castello 						   "maxim,max77836-battery")) {
314cccdd0caSMatheus Castello 		ret = max17040_set_low_soc_alert(client, chip->low_soc_alert);
315cccdd0caSMatheus Castello 		if (ret) {
316cccdd0caSMatheus Castello 			dev_err(&client->dev,
317cccdd0caSMatheus Castello 				"Failed to set low SOC alert: err %d\n", ret);
318cccdd0caSMatheus Castello 			return ret;
319cccdd0caSMatheus Castello 		}
3202e17ed94SMatheus Castello 
3212e17ed94SMatheus Castello 		ret = max17040_enable_alert_irq(chip);
3222e17ed94SMatheus Castello 		if (ret) {
3232e17ed94SMatheus Castello 			client->irq = 0;
3242e17ed94SMatheus Castello 			dev_warn(&client->dev,
3252e17ed94SMatheus Castello 				 "Failed to get IRQ err %d\n", ret);
3262e17ed94SMatheus Castello 		}
3272e17ed94SMatheus Castello 	}
3282e17ed94SMatheus Castello 
3298c0984e5SSebastian Reichel 	INIT_DEFERRABLE_WORK(&chip->work, max17040_work);
3308c0984e5SSebastian Reichel 	queue_delayed_work(system_power_efficient_wq, &chip->work,
3318c0984e5SSebastian Reichel 			   MAX17040_DELAY);
3328c0984e5SSebastian Reichel 
3338c0984e5SSebastian Reichel 	return 0;
3348c0984e5SSebastian Reichel }
3358c0984e5SSebastian Reichel 
3368c0984e5SSebastian Reichel static int max17040_remove(struct i2c_client *client)
3378c0984e5SSebastian Reichel {
3388c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
3398c0984e5SSebastian Reichel 
3408c0984e5SSebastian Reichel 	power_supply_unregister(chip->battery);
3418c0984e5SSebastian Reichel 	cancel_delayed_work(&chip->work);
3428c0984e5SSebastian Reichel 	return 0;
3438c0984e5SSebastian Reichel }
3448c0984e5SSebastian Reichel 
3458c0984e5SSebastian Reichel #ifdef CONFIG_PM_SLEEP
3468c0984e5SSebastian Reichel 
3478c0984e5SSebastian Reichel static int max17040_suspend(struct device *dev)
3488c0984e5SSebastian Reichel {
3498c0984e5SSebastian Reichel 	struct i2c_client *client = to_i2c_client(dev);
3508c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
3518c0984e5SSebastian Reichel 
3528c0984e5SSebastian Reichel 	cancel_delayed_work(&chip->work);
3532e17ed94SMatheus Castello 
354*e29242adSMarek Szyprowski 	if (client->irq && device_may_wakeup(dev))
3552e17ed94SMatheus Castello 		enable_irq_wake(client->irq);
3562e17ed94SMatheus Castello 
3578c0984e5SSebastian Reichel 	return 0;
3588c0984e5SSebastian Reichel }
3598c0984e5SSebastian Reichel 
3608c0984e5SSebastian Reichel static int max17040_resume(struct device *dev)
3618c0984e5SSebastian Reichel {
3628c0984e5SSebastian Reichel 	struct i2c_client *client = to_i2c_client(dev);
3638c0984e5SSebastian Reichel 	struct max17040_chip *chip = i2c_get_clientdata(client);
3648c0984e5SSebastian Reichel 
3658c0984e5SSebastian Reichel 	queue_delayed_work(system_power_efficient_wq, &chip->work,
3668c0984e5SSebastian Reichel 			   MAX17040_DELAY);
3672e17ed94SMatheus Castello 
368*e29242adSMarek Szyprowski 	if (client->irq && device_may_wakeup(dev))
3692e17ed94SMatheus Castello 		disable_irq_wake(client->irq);
3702e17ed94SMatheus Castello 
3718c0984e5SSebastian Reichel 	return 0;
3728c0984e5SSebastian Reichel }
3738c0984e5SSebastian Reichel 
3748c0984e5SSebastian Reichel static SIMPLE_DEV_PM_OPS(max17040_pm_ops, max17040_suspend, max17040_resume);
3758c0984e5SSebastian Reichel #define MAX17040_PM_OPS (&max17040_pm_ops)
3768c0984e5SSebastian Reichel 
3778c0984e5SSebastian Reichel #else
3788c0984e5SSebastian Reichel 
3798c0984e5SSebastian Reichel #define MAX17040_PM_OPS NULL
3808c0984e5SSebastian Reichel 
3818c0984e5SSebastian Reichel #endif /* CONFIG_PM_SLEEP */
3828c0984e5SSebastian Reichel 
3838c0984e5SSebastian Reichel static const struct i2c_device_id max17040_id[] = {
3848c0984e5SSebastian Reichel 	{ "max17040" },
3858c0984e5SSebastian Reichel 	{ "max77836-battery" },
3868c0984e5SSebastian Reichel 	{ }
3878c0984e5SSebastian Reichel };
3888c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(i2c, max17040_id);
3898c0984e5SSebastian Reichel 
390da28122cSJavier Martinez Canillas static const struct of_device_id max17040_of_match[] = {
391da28122cSJavier Martinez Canillas 	{ .compatible = "maxim,max17040" },
392da28122cSJavier Martinez Canillas 	{ .compatible = "maxim,max77836-battery" },
393da28122cSJavier Martinez Canillas 	{ },
394da28122cSJavier Martinez Canillas };
395da28122cSJavier Martinez Canillas MODULE_DEVICE_TABLE(of, max17040_of_match);
396da28122cSJavier Martinez Canillas 
3978c0984e5SSebastian Reichel static struct i2c_driver max17040_i2c_driver = {
3988c0984e5SSebastian Reichel 	.driver	= {
3998c0984e5SSebastian Reichel 		.name	= "max17040",
400da28122cSJavier Martinez Canillas 		.of_match_table = max17040_of_match,
4018c0984e5SSebastian Reichel 		.pm	= MAX17040_PM_OPS,
4028c0984e5SSebastian Reichel 	},
4038c0984e5SSebastian Reichel 	.probe		= max17040_probe,
4048c0984e5SSebastian Reichel 	.remove		= max17040_remove,
4058c0984e5SSebastian Reichel 	.id_table	= max17040_id,
4068c0984e5SSebastian Reichel };
4078c0984e5SSebastian Reichel module_i2c_driver(max17040_i2c_driver);
4088c0984e5SSebastian Reichel 
4098c0984e5SSebastian Reichel MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
4108c0984e5SSebastian Reichel MODULE_DESCRIPTION("MAX17040 Fuel Gauge");
4118c0984e5SSebastian Reichel MODULE_LICENSE("GPL");
412