blob: d36b2f6c20535fb11866ebfaf03303cd232553cf [file] [log] [blame]
Minkyu Kangc6f4a422009-06-05 15:33:04 +09001/*
2 * max17040_battery.c
3 * fuel-gauge systems for lithium-ion (Li+) batteries
4 *
5 * Copyright (C) 2009 Samsung Electronics
6 * Minkyu Kang <mk7.kang@samsung.com>
7 *
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.
11 */
12
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Minkyu Kangc6f4a422009-06-05 15:33:04 +090023
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
36
37#define MAX17040_DELAY 1000
38#define MAX17040_BATTERY_FULL 95
39
40struct max17040_chip {
41 struct i2c_client *client;
42 struct delayed_work work;
43 struct power_supply battery;
44 struct max17040_platform_data *pdata;
45
46 /* State Of Connect */
47 int online;
48 /* battery voltage */
49 int vcell;
50 /* battery capacity */
51 int soc;
52 /* State Of Charge */
53 int status;
54};
55
56static int max17040_get_property(struct power_supply *psy,
57 enum power_supply_property psp,
58 union power_supply_propval *val)
59{
60 struct max17040_chip *chip = container_of(psy,
61 struct max17040_chip, battery);
62
63 switch (psp) {
64 case POWER_SUPPLY_PROP_STATUS:
65 val->intval = chip->status;
66 break;
67 case POWER_SUPPLY_PROP_ONLINE:
68 val->intval = chip->online;
69 break;
70 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
71 val->intval = chip->vcell;
72 break;
73 case POWER_SUPPLY_PROP_CAPACITY:
74 val->intval = chip->soc;
75 break;
76 default:
77 return -EINVAL;
78 }
79 return 0;
80}
81
82static int max17040_write_reg(struct i2c_client *client, int reg, u8 value)
83{
84 int ret;
85
86 ret = i2c_smbus_write_byte_data(client, reg, value);
87
88 if (ret < 0)
89 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
90
91 return ret;
92}
93
94static int max17040_read_reg(struct i2c_client *client, int reg)
95{
96 int ret;
97
98 ret = i2c_smbus_read_byte_data(client, reg);
99
100 if (ret < 0)
101 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
102
103 return ret;
104}
105
106static void max17040_reset(struct i2c_client *client)
107{
108 max17040_write_reg(client, MAX17040_CMD_MSB, 0x54);
109 max17040_write_reg(client, MAX17040_CMD_LSB, 0x00);
110}
111
112static void max17040_get_vcell(struct i2c_client *client)
113{
114 struct max17040_chip *chip = i2c_get_clientdata(client);
115 u8 msb;
116 u8 lsb;
117
118 msb = max17040_read_reg(client, MAX17040_VCELL_MSB);
119 lsb = max17040_read_reg(client, MAX17040_VCELL_LSB);
120
121 chip->vcell = (msb << 4) + (lsb >> 4);
122}
123
124static void max17040_get_soc(struct i2c_client *client)
125{
126 struct max17040_chip *chip = i2c_get_clientdata(client);
127 u8 msb;
128 u8 lsb;
129
130 msb = max17040_read_reg(client, MAX17040_SOC_MSB);
131 lsb = max17040_read_reg(client, MAX17040_SOC_LSB);
132
133 chip->soc = msb;
134}
135
136static void max17040_get_version(struct i2c_client *client)
137{
138 u8 msb;
139 u8 lsb;
140
141 msb = max17040_read_reg(client, MAX17040_VER_MSB);
142 lsb = max17040_read_reg(client, MAX17040_VER_LSB);
143
144 dev_info(&client->dev, "MAX17040 Fuel-Gauge Ver %d%d\n", msb, lsb);
145}
146
147static void max17040_get_online(struct i2c_client *client)
148{
149 struct max17040_chip *chip = i2c_get_clientdata(client);
150
Krzysztof Kozlowskiac323d82014-01-30 14:32:45 +0100151 if (chip->pdata && chip->pdata->battery_online)
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900152 chip->online = chip->pdata->battery_online();
153 else
154 chip->online = 1;
155}
156
157static void max17040_get_status(struct i2c_client *client)
158{
159 struct max17040_chip *chip = i2c_get_clientdata(client);
160
Krzysztof Kozlowskiac323d82014-01-30 14:32:45 +0100161 if (!chip->pdata || !chip->pdata->charger_online
162 || !chip->pdata->charger_enable) {
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900163 chip->status = POWER_SUPPLY_STATUS_UNKNOWN;
164 return;
165 }
166
167 if (chip->pdata->charger_online()) {
168 if (chip->pdata->charger_enable())
169 chip->status = POWER_SUPPLY_STATUS_CHARGING;
170 else
171 chip->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
172 } else {
173 chip->status = POWER_SUPPLY_STATUS_DISCHARGING;
174 }
175
176 if (chip->soc > MAX17040_BATTERY_FULL)
177 chip->status = POWER_SUPPLY_STATUS_FULL;
178}
179
180static void max17040_work(struct work_struct *work)
181{
182 struct max17040_chip *chip;
183
184 chip = container_of(work, struct max17040_chip, work.work);
185
186 max17040_get_vcell(chip->client);
187 max17040_get_soc(chip->client);
188 max17040_get_online(chip->client);
189 max17040_get_status(chip->client);
190
Krzysztof Kozlowski52fa74e2015-02-24 10:54:46 +0100191 queue_delayed_work(system_power_efficient_wq, &chip->work,
192 MAX17040_DELAY);
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900193}
194
195static enum power_supply_property max17040_battery_props[] = {
196 POWER_SUPPLY_PROP_STATUS,
197 POWER_SUPPLY_PROP_ONLINE,
198 POWER_SUPPLY_PROP_VOLTAGE_NOW,
199 POWER_SUPPLY_PROP_CAPACITY,
200};
201
Bill Pembertonc8afa642012-11-19 13:22:23 -0500202static int max17040_probe(struct i2c_client *client,
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900203 const struct i2c_device_id *id)
204{
205 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
206 struct max17040_chip *chip;
207 int ret;
208
209 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
210 return -EIO;
211
Devendra Naga00edfc62013-01-05 23:53:25 -0500212 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900213 if (!chip)
214 return -ENOMEM;
215
216 chip->client = client;
217 chip->pdata = client->dev.platform_data;
218
219 i2c_set_clientdata(client, chip);
220
221 chip->battery.name = "battery";
222 chip->battery.type = POWER_SUPPLY_TYPE_BATTERY;
223 chip->battery.get_property = max17040_get_property;
224 chip->battery.properties = max17040_battery_props;
225 chip->battery.num_properties = ARRAY_SIZE(max17040_battery_props);
226
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100227 ret = power_supply_register(&client->dev, &chip->battery, NULL);
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900228 if (ret) {
229 dev_err(&client->dev, "failed: power supply register\n");
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900230 return ret;
231 }
232
233 max17040_reset(client);
234 max17040_get_version(client);
235
Tejun Heo203b42f2012-08-21 13:18:23 -0700236 INIT_DEFERRABLE_WORK(&chip->work, max17040_work);
Krzysztof Kozlowski52fa74e2015-02-24 10:54:46 +0100237 queue_delayed_work(system_power_efficient_wq, &chip->work,
238 MAX17040_DELAY);
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900239
240 return 0;
241}
242
Bill Pemberton415ec692012-11-19 13:26:07 -0500243static int max17040_remove(struct i2c_client *client)
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900244{
245 struct max17040_chip *chip = i2c_get_clientdata(client);
246
247 power_supply_unregister(&chip->battery);
248 cancel_delayed_work(&chip->work);
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900249 return 0;
250}
251
Lars-Peter Clausen43cf4542013-03-10 14:34:06 +0100252#ifdef CONFIG_PM_SLEEP
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900253
Lars-Peter Clausen43cf4542013-03-10 14:34:06 +0100254static int max17040_suspend(struct device *dev)
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900255{
Lars-Peter Clausen43cf4542013-03-10 14:34:06 +0100256 struct i2c_client *client = to_i2c_client(dev);
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900257 struct max17040_chip *chip = i2c_get_clientdata(client);
258
259 cancel_delayed_work(&chip->work);
260 return 0;
261}
262
Lars-Peter Clausen43cf4542013-03-10 14:34:06 +0100263static int max17040_resume(struct device *dev)
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900264{
Lars-Peter Clausen43cf4542013-03-10 14:34:06 +0100265 struct i2c_client *client = to_i2c_client(dev);
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900266 struct max17040_chip *chip = i2c_get_clientdata(client);
267
Krzysztof Kozlowski52fa74e2015-02-24 10:54:46 +0100268 queue_delayed_work(system_power_efficient_wq, &chip->work,
269 MAX17040_DELAY);
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900270 return 0;
271}
272
Lars-Peter Clausen43cf4542013-03-10 14:34:06 +0100273static SIMPLE_DEV_PM_OPS(max17040_pm_ops, max17040_suspend, max17040_resume);
274#define MAX17040_PM_OPS (&max17040_pm_ops)
275
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900276#else
277
Lars-Peter Clausen43cf4542013-03-10 14:34:06 +0100278#define MAX17040_PM_OPS NULL
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900279
Lars-Peter Clausen43cf4542013-03-10 14:34:06 +0100280#endif /* CONFIG_PM_SLEEP */
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900281
282static const struct i2c_device_id max17040_id[] = {
Krzysztof Kozlowski2c33e922014-09-12 08:53:58 +0200283 { "max17040" },
284 { "max77836-battery" },
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900285 { }
286};
287MODULE_DEVICE_TABLE(i2c, max17040_id);
288
289static struct i2c_driver max17040_i2c_driver = {
290 .driver = {
291 .name = "max17040",
Lars-Peter Clausen43cf4542013-03-10 14:34:06 +0100292 .pm = MAX17040_PM_OPS,
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900293 },
294 .probe = max17040_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -0500295 .remove = max17040_remove,
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900296 .id_table = max17040_id,
297};
Axel Lin5ff92e72012-01-21 14:42:54 +0800298module_i2c_driver(max17040_i2c_driver);
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900299
300MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
301MODULE_DESCRIPTION("MAX17040 Fuel Gauge");
302MODULE_LICENSE("GPL");