3 * fuel-gauge systems for lithium-ion (Li+) batteries
5 * Copyright (C) 2009 Samsung Electronics
6 * Minkyu Kang <mk7.kang@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/platform_device.h>
16 #include <linux/mutex.h>
17 #include <linux/err.h>
18 #include <linux/i2c.h>
19 #include <linux/delay.h>
20 #include <linux/power_supply.h>
21 #include <linux/max17040_battery.h>
22 #include <linux/slab.h>
24 #define MAX17040_VCELL_MSB 0x02
25 #define MAX17040_VCELL_LSB 0x03
26 #define MAX17040_SOC_MSB 0x04
27 #define MAX17040_SOC_LSB 0x05
28 #define MAX17040_MODE_MSB 0x06
29 #define MAX17040_MODE_LSB 0x07
30 #define MAX17040_VER_MSB 0x08
31 #define MAX17040_VER_LSB 0x09
32 #define MAX17040_RCOMP_MSB 0x0C
33 #define MAX17040_RCOMP_LSB 0x0D
34 #define MAX17040_CMD_MSB 0xFE
35 #define MAX17040_CMD_LSB 0xFF
37 #define MAX17040_DELAY 1000
38 #define MAX17040_BATTERY_FULL 95
40 struct max17040_chip {
41 struct i2c_client *client;
42 struct delayed_work work;
43 struct power_supply battery;
44 struct max17040_platform_data *pdata;
46 /* State Of Connect */
50 /* battery capacity */
56 static int max17040_get_property(struct power_supply *psy,
57 enum power_supply_property psp,
58 union power_supply_propval *val)
60 struct max17040_chip *chip = container_of(psy,
61 struct max17040_chip, battery);
64 case POWER_SUPPLY_PROP_STATUS:
65 val->intval = chip->status;
67 case POWER_SUPPLY_PROP_ONLINE:
68 val->intval = chip->online;
70 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
71 val->intval = chip->vcell;
73 case POWER_SUPPLY_PROP_CAPACITY:
74 val->intval = chip->soc;
82 static int max17040_write_reg(struct i2c_client *client, int reg, u8 value)
86 ret = i2c_smbus_write_byte_data(client, reg, value);
89 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
94 static int max17040_read_reg(struct i2c_client *client, int reg)
98 ret = i2c_smbus_read_byte_data(client, reg);
101 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
106 static void max17040_reset(struct i2c_client *client)
108 max17040_write_reg(client, MAX17040_CMD_MSB, 0x54);
109 max17040_write_reg(client, MAX17040_CMD_LSB, 0x00);
112 static void max17040_get_vcell(struct i2c_client *client)
114 struct max17040_chip *chip = i2c_get_clientdata(client);
118 msb = max17040_read_reg(client, MAX17040_VCELL_MSB);
119 lsb = max17040_read_reg(client, MAX17040_VCELL_LSB);
121 chip->vcell = (msb << 4) + (lsb >> 4);
124 static void max17040_get_soc(struct i2c_client *client)
126 struct max17040_chip *chip = i2c_get_clientdata(client);
130 msb = max17040_read_reg(client, MAX17040_SOC_MSB);
131 lsb = max17040_read_reg(client, MAX17040_SOC_LSB);
136 static void max17040_get_version(struct i2c_client *client)
141 msb = max17040_read_reg(client, MAX17040_VER_MSB);
142 lsb = max17040_read_reg(client, MAX17040_VER_LSB);
144 dev_info(&client->dev, "MAX17040 Fuel-Gauge Ver %d%d\n", msb, lsb);
147 static void max17040_get_online(struct i2c_client *client)
149 struct max17040_chip *chip = i2c_get_clientdata(client);
151 if (chip->pdata && chip->pdata->battery_online)
152 chip->online = chip->pdata->battery_online();
157 static void max17040_get_status(struct i2c_client *client)
159 struct max17040_chip *chip = i2c_get_clientdata(client);
161 if (!chip->pdata || !chip->pdata->charger_online
162 || !chip->pdata->charger_enable) {
163 chip->status = POWER_SUPPLY_STATUS_UNKNOWN;
167 if (chip->pdata->charger_online()) {
168 if (chip->pdata->charger_enable())
169 chip->status = POWER_SUPPLY_STATUS_CHARGING;
171 chip->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
173 chip->status = POWER_SUPPLY_STATUS_DISCHARGING;
176 if (chip->soc > MAX17040_BATTERY_FULL)
177 chip->status = POWER_SUPPLY_STATUS_FULL;
180 static void max17040_work(struct work_struct *work)
182 struct max17040_chip *chip;
184 chip = container_of(work, struct max17040_chip, work.work);
186 max17040_get_vcell(chip->client);
187 max17040_get_soc(chip->client);
188 max17040_get_online(chip->client);
189 max17040_get_status(chip->client);
191 schedule_delayed_work(&chip->work, MAX17040_DELAY);
194 static enum power_supply_property max17040_battery_props[] = {
195 POWER_SUPPLY_PROP_STATUS,
196 POWER_SUPPLY_PROP_ONLINE,
197 POWER_SUPPLY_PROP_VOLTAGE_NOW,
198 POWER_SUPPLY_PROP_CAPACITY,
201 static int max17040_probe(struct i2c_client *client,
202 const struct i2c_device_id *id)
204 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
205 struct max17040_chip *chip;
208 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
211 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
215 chip->client = client;
216 chip->pdata = client->dev.platform_data;
218 i2c_set_clientdata(client, chip);
220 chip->battery.name = "battery";
221 chip->battery.type = POWER_SUPPLY_TYPE_BATTERY;
222 chip->battery.get_property = max17040_get_property;
223 chip->battery.properties = max17040_battery_props;
224 chip->battery.num_properties = ARRAY_SIZE(max17040_battery_props);
226 ret = power_supply_register(&client->dev, &chip->battery);
228 dev_err(&client->dev, "failed: power supply register\n");
232 max17040_reset(client);
233 max17040_get_version(client);
235 INIT_DEFERRABLE_WORK(&chip->work, max17040_work);
236 schedule_delayed_work(&chip->work, MAX17040_DELAY);
241 static int max17040_remove(struct i2c_client *client)
243 struct max17040_chip *chip = i2c_get_clientdata(client);
245 power_supply_unregister(&chip->battery);
246 cancel_delayed_work(&chip->work);
250 #ifdef CONFIG_PM_SLEEP
252 static int max17040_suspend(struct device *dev)
254 struct i2c_client *client = to_i2c_client(dev);
255 struct max17040_chip *chip = i2c_get_clientdata(client);
257 cancel_delayed_work(&chip->work);
261 static int max17040_resume(struct device *dev)
263 struct i2c_client *client = to_i2c_client(dev);
264 struct max17040_chip *chip = i2c_get_clientdata(client);
266 schedule_delayed_work(&chip->work, MAX17040_DELAY);
270 static SIMPLE_DEV_PM_OPS(max17040_pm_ops, max17040_suspend, max17040_resume);
271 #define MAX17040_PM_OPS (&max17040_pm_ops)
275 #define MAX17040_PM_OPS NULL
277 #endif /* CONFIG_PM_SLEEP */
279 static const struct i2c_device_id max17040_id[] = {
283 MODULE_DEVICE_TABLE(i2c, max17040_id);
285 static struct i2c_driver max17040_i2c_driver = {
288 .pm = MAX17040_PM_OPS,
290 .probe = max17040_probe,
291 .remove = max17040_remove,
292 .id_table = max17040_id,
294 module_i2c_driver(max17040_i2c_driver);
296 MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
297 MODULE_DESCRIPTION("MAX17040 Fuel Gauge");
298 MODULE_LICENSE("GPL");