blob: 33c40f79d23d5e016e69ecde3d0f5785ba4048c0 [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
Liu Xiang14d60bd2016-09-21 22:17:34 +080024#define MAX17040_VCELL 0x02
25#define MAX17040_SOC 0x04
26#define MAX17040_MODE 0x06
27#define MAX17040_VER 0x08
28#define MAX17040_RCOMP 0x0C
29#define MAX17040_CMD 0xFE
30
Minkyu Kangc6f4a422009-06-05 15:33:04 +090031
32#define MAX17040_DELAY 1000
33#define MAX17040_BATTERY_FULL 95
34
35struct max17040_chip {
36 struct i2c_client *client;
37 struct delayed_work work;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010038 struct power_supply *battery;
Minkyu Kangc6f4a422009-06-05 15:33:04 +090039 struct max17040_platform_data *pdata;
40
41 /* State Of Connect */
42 int online;
43 /* battery voltage */
44 int vcell;
45 /* battery capacity */
46 int soc;
47 /* State Of Charge */
48 int status;
49};
50
51static int max17040_get_property(struct power_supply *psy,
52 enum power_supply_property psp,
53 union power_supply_propval *val)
54{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010055 struct max17040_chip *chip = power_supply_get_drvdata(psy);
Minkyu Kangc6f4a422009-06-05 15:33:04 +090056
57 switch (psp) {
58 case POWER_SUPPLY_PROP_STATUS:
59 val->intval = chip->status;
60 break;
61 case POWER_SUPPLY_PROP_ONLINE:
62 val->intval = chip->online;
63 break;
64 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
65 val->intval = chip->vcell;
66 break;
67 case POWER_SUPPLY_PROP_CAPACITY:
68 val->intval = chip->soc;
69 break;
70 default:
71 return -EINVAL;
72 }
73 return 0;
74}
75
Liu Xiang14d60bd2016-09-21 22:17:34 +080076static int max17040_write_reg(struct i2c_client *client, int reg, u16 value)
Minkyu Kangc6f4a422009-06-05 15:33:04 +090077{
78 int ret;
79
Liu Xiang14d60bd2016-09-21 22:17:34 +080080 ret = i2c_smbus_write_word_swapped(client, reg, value);
Minkyu Kangc6f4a422009-06-05 15:33:04 +090081
82 if (ret < 0)
83 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
84
85 return ret;
86}
87
88static int max17040_read_reg(struct i2c_client *client, int reg)
89{
90 int ret;
91
Liu Xiang14d60bd2016-09-21 22:17:34 +080092 ret = i2c_smbus_read_word_swapped(client, reg);
Minkyu Kangc6f4a422009-06-05 15:33:04 +090093
94 if (ret < 0)
95 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
96
97 return ret;
98}
99
100static void max17040_reset(struct i2c_client *client)
101{
Liu Xiang14d60bd2016-09-21 22:17:34 +0800102 max17040_write_reg(client, MAX17040_CMD, 0x0054);
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900103}
104
105static void max17040_get_vcell(struct i2c_client *client)
106{
107 struct max17040_chip *chip = i2c_get_clientdata(client);
Liu Xiang14d60bd2016-09-21 22:17:34 +0800108 u16 vcell;
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900109
Liu Xiang14d60bd2016-09-21 22:17:34 +0800110 vcell = max17040_read_reg(client, MAX17040_VCELL);
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900111
Liu Xiang14d60bd2016-09-21 22:17:34 +0800112 chip->vcell = vcell;
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900113}
114
115static void max17040_get_soc(struct i2c_client *client)
116{
117 struct max17040_chip *chip = i2c_get_clientdata(client);
Liu Xiang14d60bd2016-09-21 22:17:34 +0800118 u16 soc;
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900119
Liu Xiang14d60bd2016-09-21 22:17:34 +0800120 soc = max17040_read_reg(client, MAX17040_SOC);
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900121
Liu Xiang14d60bd2016-09-21 22:17:34 +0800122 chip->soc = (soc >> 8);
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900123}
124
125static void max17040_get_version(struct i2c_client *client)
126{
Liu Xiang14d60bd2016-09-21 22:17:34 +0800127 u16 version;
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900128
Liu Xiang14d60bd2016-09-21 22:17:34 +0800129 version = max17040_read_reg(client, MAX17040_VER);
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900130
Liu Xiang14d60bd2016-09-21 22:17:34 +0800131 dev_info(&client->dev, "MAX17040 Fuel-Gauge Ver 0x%x\n", version);
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900132}
133
134static void max17040_get_online(struct i2c_client *client)
135{
136 struct max17040_chip *chip = i2c_get_clientdata(client);
137
Krzysztof Kozlowskiac323d82014-01-30 14:32:45 +0100138 if (chip->pdata && chip->pdata->battery_online)
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900139 chip->online = chip->pdata->battery_online();
140 else
141 chip->online = 1;
142}
143
144static void max17040_get_status(struct i2c_client *client)
145{
146 struct max17040_chip *chip = i2c_get_clientdata(client);
147
Krzysztof Kozlowskiac323d82014-01-30 14:32:45 +0100148 if (!chip->pdata || !chip->pdata->charger_online
149 || !chip->pdata->charger_enable) {
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900150 chip->status = POWER_SUPPLY_STATUS_UNKNOWN;
151 return;
152 }
153
154 if (chip->pdata->charger_online()) {
155 if (chip->pdata->charger_enable())
156 chip->status = POWER_SUPPLY_STATUS_CHARGING;
157 else
158 chip->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
159 } else {
160 chip->status = POWER_SUPPLY_STATUS_DISCHARGING;
161 }
162
163 if (chip->soc > MAX17040_BATTERY_FULL)
164 chip->status = POWER_SUPPLY_STATUS_FULL;
165}
166
167static void max17040_work(struct work_struct *work)
168{
169 struct max17040_chip *chip;
170
171 chip = container_of(work, struct max17040_chip, work.work);
172
173 max17040_get_vcell(chip->client);
174 max17040_get_soc(chip->client);
175 max17040_get_online(chip->client);
176 max17040_get_status(chip->client);
177
Krzysztof Kozlowski52fa74e2015-02-24 10:54:46 +0100178 queue_delayed_work(system_power_efficient_wq, &chip->work,
179 MAX17040_DELAY);
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900180}
181
182static enum power_supply_property max17040_battery_props[] = {
183 POWER_SUPPLY_PROP_STATUS,
184 POWER_SUPPLY_PROP_ONLINE,
185 POWER_SUPPLY_PROP_VOLTAGE_NOW,
186 POWER_SUPPLY_PROP_CAPACITY,
187};
188
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100189static const struct power_supply_desc max17040_battery_desc = {
190 .name = "battery",
191 .type = POWER_SUPPLY_TYPE_BATTERY,
192 .get_property = max17040_get_property,
193 .properties = max17040_battery_props,
194 .num_properties = ARRAY_SIZE(max17040_battery_props),
195};
196
Bill Pembertonc8afa642012-11-19 13:22:23 -0500197static int max17040_probe(struct i2c_client *client,
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900198 const struct i2c_device_id *id)
199{
200 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100201 struct power_supply_config psy_cfg = {};
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900202 struct max17040_chip *chip;
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900203
204 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
205 return -EIO;
206
Devendra Naga00edfc62013-01-05 23:53:25 -0500207 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900208 if (!chip)
209 return -ENOMEM;
210
211 chip->client = client;
212 chip->pdata = client->dev.platform_data;
213
214 i2c_set_clientdata(client, chip);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100215 psy_cfg.drv_data = chip;
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900216
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100217 chip->battery = power_supply_register(&client->dev,
218 &max17040_battery_desc, &psy_cfg);
219 if (IS_ERR(chip->battery)) {
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900220 dev_err(&client->dev, "failed: power supply register\n");
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100221 return PTR_ERR(chip->battery);
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900222 }
223
224 max17040_reset(client);
225 max17040_get_version(client);
226
Tejun Heo203b42f2012-08-21 13:18:23 -0700227 INIT_DEFERRABLE_WORK(&chip->work, max17040_work);
Krzysztof Kozlowski52fa74e2015-02-24 10:54:46 +0100228 queue_delayed_work(system_power_efficient_wq, &chip->work,
229 MAX17040_DELAY);
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900230
231 return 0;
232}
233
Bill Pemberton415ec692012-11-19 13:26:07 -0500234static int max17040_remove(struct i2c_client *client)
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900235{
236 struct max17040_chip *chip = i2c_get_clientdata(client);
237
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100238 power_supply_unregister(chip->battery);
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900239 cancel_delayed_work(&chip->work);
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900240 return 0;
241}
242
Lars-Peter Clausen43cf4542013-03-10 14:34:06 +0100243#ifdef CONFIG_PM_SLEEP
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900244
Lars-Peter Clausen43cf4542013-03-10 14:34:06 +0100245static int max17040_suspend(struct device *dev)
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900246{
Lars-Peter Clausen43cf4542013-03-10 14:34:06 +0100247 struct i2c_client *client = to_i2c_client(dev);
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900248 struct max17040_chip *chip = i2c_get_clientdata(client);
249
250 cancel_delayed_work(&chip->work);
251 return 0;
252}
253
Lars-Peter Clausen43cf4542013-03-10 14:34:06 +0100254static int max17040_resume(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
Krzysztof Kozlowski52fa74e2015-02-24 10:54:46 +0100259 queue_delayed_work(system_power_efficient_wq, &chip->work,
260 MAX17040_DELAY);
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900261 return 0;
262}
263
Lars-Peter Clausen43cf4542013-03-10 14:34:06 +0100264static SIMPLE_DEV_PM_OPS(max17040_pm_ops, max17040_suspend, max17040_resume);
265#define MAX17040_PM_OPS (&max17040_pm_ops)
266
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900267#else
268
Lars-Peter Clausen43cf4542013-03-10 14:34:06 +0100269#define MAX17040_PM_OPS NULL
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900270
Lars-Peter Clausen43cf4542013-03-10 14:34:06 +0100271#endif /* CONFIG_PM_SLEEP */
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900272
273static const struct i2c_device_id max17040_id[] = {
Krzysztof Kozlowski2c33e922014-09-12 08:53:58 +0200274 { "max17040" },
275 { "max77836-battery" },
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900276 { }
277};
278MODULE_DEVICE_TABLE(i2c, max17040_id);
279
Javier Martinez Canillasda281222017-03-15 00:43:49 -0300280static const struct of_device_id max17040_of_match[] = {
281 { .compatible = "maxim,max17040" },
282 { .compatible = "maxim,max77836-battery" },
283 { },
284};
285MODULE_DEVICE_TABLE(of, max17040_of_match);
286
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900287static struct i2c_driver max17040_i2c_driver = {
288 .driver = {
289 .name = "max17040",
Javier Martinez Canillasda281222017-03-15 00:43:49 -0300290 .of_match_table = max17040_of_match,
Lars-Peter Clausen43cf4542013-03-10 14:34:06 +0100291 .pm = MAX17040_PM_OPS,
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900292 },
293 .probe = max17040_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -0500294 .remove = max17040_remove,
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900295 .id_table = max17040_id,
296};
Axel Lin5ff92e72012-01-21 14:42:54 +0800297module_i2c_driver(max17040_i2c_driver);
Minkyu Kangc6f4a422009-06-05 15:33:04 +0900298
299MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
300MODULE_DESCRIPTION("MAX17040 Fuel Gauge");
301MODULE_LICENSE("GPL");