blob: 86acee2f988917912919e710d4b62f1592b98397 [file] [log] [blame]
MyungJoo Ham359ab9f2011-01-14 14:46:11 +09001/*
2 * Fuel gauge driver for Maxim 17042 / 8966 / 8997
3 * Note that Maxim 8966 and 8997 are mfd and this is its subdevice.
4 *
5 * Copyright (C) 2011 Samsung Electronics
6 * MyungJoo Ham <myungjoo.ham@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 as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * This driver is based on max17040_battery.c
23 */
24
25#include <linux/init.h>
Paul Gortmaker7e6d62d2011-07-03 15:28:29 -040026#include <linux/module.h>
MyungJoo Ham359ab9f2011-01-14 14:46:11 +090027#include <linux/slab.h>
28#include <linux/i2c.h>
29#include <linux/mod_devicetable.h>
30#include <linux/power_supply.h>
31#include <linux/power/max17042_battery.h>
32
MyungJoo Ham359ab9f2011-01-14 14:46:11 +090033struct max17042_chip {
34 struct i2c_client *client;
35 struct power_supply battery;
36 struct max17042_platform_data *pdata;
37};
38
39static int max17042_write_reg(struct i2c_client *client, u8 reg, u16 value)
40{
41 int ret = i2c_smbus_write_word_data(client, reg, value);
42
43 if (ret < 0)
44 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
45
46 return ret;
47}
48
49static int max17042_read_reg(struct i2c_client *client, u8 reg)
50{
51 int ret = i2c_smbus_read_word_data(client, reg);
52
53 if (ret < 0)
54 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
55
56 return ret;
57}
58
Donggeun Kim086ef502011-06-30 18:07:41 +090059static void max17042_set_reg(struct i2c_client *client,
60 struct max17042_reg_data *data, int size)
61{
62 int i;
63
64 for (i = 0; i < size; i++)
65 max17042_write_reg(client, data[i].addr, data[i].data);
66}
67
MyungJoo Ham359ab9f2011-01-14 14:46:11 +090068static enum power_supply_property max17042_battery_props[] = {
Donggeun Kim086ef502011-06-30 18:07:41 +090069 POWER_SUPPLY_PROP_PRESENT,
70 POWER_SUPPLY_PROP_CYCLE_COUNT,
71 POWER_SUPPLY_PROP_VOLTAGE_MAX,
72 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
MyungJoo Ham359ab9f2011-01-14 14:46:11 +090073 POWER_SUPPLY_PROP_VOLTAGE_NOW,
74 POWER_SUPPLY_PROP_VOLTAGE_AVG,
75 POWER_SUPPLY_PROP_CAPACITY,
Donggeun Kim086ef502011-06-30 18:07:41 +090076 POWER_SUPPLY_PROP_CHARGE_FULL,
77 POWER_SUPPLY_PROP_TEMP,
78 POWER_SUPPLY_PROP_CURRENT_NOW,
79 POWER_SUPPLY_PROP_CURRENT_AVG,
MyungJoo Ham359ab9f2011-01-14 14:46:11 +090080};
81
82static int max17042_get_property(struct power_supply *psy,
83 enum power_supply_property psp,
84 union power_supply_propval *val)
85{
86 struct max17042_chip *chip = container_of(psy,
87 struct max17042_chip, battery);
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +040088 int ret;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +090089
90 switch (psp) {
Donggeun Kim086ef502011-06-30 18:07:41 +090091 case POWER_SUPPLY_PROP_PRESENT:
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +040092 ret = max17042_read_reg(chip->client, MAX17042_STATUS);
93 if (ret < 0)
94 return ret;
95
96 if (ret & MAX17042_STATUS_BattAbsent)
Donggeun Kim086ef502011-06-30 18:07:41 +090097 val->intval = 0;
98 else
99 val->intval = 1;
100 break;
101 case POWER_SUPPLY_PROP_CYCLE_COUNT:
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400102 ret = max17042_read_reg(chip->client, MAX17042_Cycles);
103 if (ret < 0)
104 return ret;
105
106 val->intval = ret;
Donggeun Kim086ef502011-06-30 18:07:41 +0900107 break;
108 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400109 ret = max17042_read_reg(chip->client, MAX17042_MinMaxVolt);
110 if (ret < 0)
111 return ret;
112
113 val->intval = ret >> 8;
Donggeun Kim086ef502011-06-30 18:07:41 +0900114 val->intval *= 20000; /* Units of LSB = 20mV */
115 break;
116 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400117 ret = max17042_read_reg(chip->client, MAX17042_V_empty);
118 if (ret < 0)
119 return ret;
120
121 val->intval = ret >> 7;
Donggeun Kim086ef502011-06-30 18:07:41 +0900122 val->intval *= 10000; /* Units of LSB = 10mV */
123 break;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900124 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400125 ret = max17042_read_reg(chip->client, MAX17042_VCELL);
126 if (ret < 0)
127 return ret;
128
129 val->intval = ret * 625 / 8;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900130 break;
131 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400132 ret = max17042_read_reg(chip->client, MAX17042_AvgVCELL);
133 if (ret < 0)
134 return ret;
135
136 val->intval = ret * 625 / 8;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900137 break;
138 case POWER_SUPPLY_PROP_CAPACITY:
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400139 ret = max17042_read_reg(chip->client, MAX17042_SOC);
140 if (ret < 0)
141 return ret;
142
143 val->intval = ret >> 8;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900144 break;
Donggeun Kim086ef502011-06-30 18:07:41 +0900145 case POWER_SUPPLY_PROP_CHARGE_FULL:
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400146 ret = max17042_read_reg(chip->client, MAX17042_RepSOC);
147 if (ret < 0)
148 return ret;
149
150 if ((ret >> 8) >= MAX17042_BATTERY_FULL)
Donggeun Kim086ef502011-06-30 18:07:41 +0900151 val->intval = 1;
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400152 else if (ret >= 0)
Donggeun Kim086ef502011-06-30 18:07:41 +0900153 val->intval = 0;
154 break;
155 case POWER_SUPPLY_PROP_TEMP:
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400156 ret = max17042_read_reg(chip->client, MAX17042_TEMP);
157 if (ret < 0)
158 return ret;
159
160 val->intval = ret;
Donggeun Kim086ef502011-06-30 18:07:41 +0900161 /* The value is signed. */
162 if (val->intval & 0x8000) {
163 val->intval = (0x7fff & ~val->intval) + 1;
164 val->intval *= -1;
165 }
166 /* The value is converted into deci-centigrade scale */
167 /* Units of LSB = 1 / 256 degree Celsius */
168 val->intval = val->intval * 10 / 256;
169 break;
170 case POWER_SUPPLY_PROP_CURRENT_NOW:
171 if (chip->pdata->enable_current_sense) {
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400172 ret = max17042_read_reg(chip->client, MAX17042_Current);
173 if (ret < 0)
174 return ret;
175
176 val->intval = ret;
Donggeun Kim086ef502011-06-30 18:07:41 +0900177 if (val->intval & 0x8000) {
178 /* Negative */
179 val->intval = ~val->intval & 0x7fff;
180 val->intval++;
181 val->intval *= -1;
182 }
Philip Rakity91d8b0d2011-08-12 21:19:57 -0700183 val->intval *= 1562500 / chip->pdata->r_sns;
Donggeun Kim086ef502011-06-30 18:07:41 +0900184 } else {
185 return -EINVAL;
186 }
187 break;
188 case POWER_SUPPLY_PROP_CURRENT_AVG:
189 if (chip->pdata->enable_current_sense) {
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400190 ret = max17042_read_reg(chip->client,
191 MAX17042_AvgCurrent);
192 if (ret < 0)
193 return ret;
194
195 val->intval = ret;
Donggeun Kim086ef502011-06-30 18:07:41 +0900196 if (val->intval & 0x8000) {
197 /* Negative */
198 val->intval = ~val->intval & 0x7fff;
199 val->intval++;
200 val->intval *= -1;
201 }
202 val->intval *= 1562500 / chip->pdata->r_sns;
203 } else {
204 return -EINVAL;
205 }
206 break;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900207 default:
208 return -EINVAL;
209 }
210 return 0;
211}
212
213static int __devinit max17042_probe(struct i2c_client *client,
214 const struct i2c_device_id *id)
215{
216 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
217 struct max17042_chip *chip;
218 int ret;
219
220 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
221 return -EIO;
222
223 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
224 if (!chip)
225 return -ENOMEM;
226
227 chip->client = client;
228 chip->pdata = client->dev.platform_data;
229
230 i2c_set_clientdata(client, chip);
231
232 chip->battery.name = "max17042_battery";
233 chip->battery.type = POWER_SUPPLY_TYPE_BATTERY;
234 chip->battery.get_property = max17042_get_property;
235 chip->battery.properties = max17042_battery_props;
236 chip->battery.num_properties = ARRAY_SIZE(max17042_battery_props);
237
Donggeun Kim086ef502011-06-30 18:07:41 +0900238 /* When current is not measured,
239 * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
240 if (!chip->pdata->enable_current_sense)
241 chip->battery.num_properties -= 2;
242
Philip Rakity4cfa8922011-08-12 21:18:18 -0700243 if (chip->pdata->r_sns == 0)
244 chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR;
245
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900246 ret = power_supply_register(&client->dev, &chip->battery);
247 if (ret) {
248 dev_err(&client->dev, "failed: power supply register\n");
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900249 kfree(chip);
250 return ret;
251 }
252
Donggeun Kim086ef502011-06-30 18:07:41 +0900253 /* Initialize registers according to values from the platform data */
254 if (chip->pdata->init_data)
255 max17042_set_reg(client, chip->pdata->init_data,
256 chip->pdata->num_init_data);
257
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900258 if (!chip->pdata->enable_current_sense) {
259 max17042_write_reg(client, MAX17042_CGAIN, 0x0000);
260 max17042_write_reg(client, MAX17042_MiscCFG, 0x0003);
261 max17042_write_reg(client, MAX17042_LearnCFG, 0x0007);
262 }
263
264 return 0;
265}
266
267static int __devexit max17042_remove(struct i2c_client *client)
268{
269 struct max17042_chip *chip = i2c_get_clientdata(client);
270
271 power_supply_unregister(&chip->battery);
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900272 kfree(chip);
273 return 0;
274}
275
276static const struct i2c_device_id max17042_id[] = {
277 { "max17042", 0 },
278 { }
279};
280MODULE_DEVICE_TABLE(i2c, max17042_id);
281
282static struct i2c_driver max17042_i2c_driver = {
283 .driver = {
284 .name = "max17042",
285 },
286 .probe = max17042_probe,
287 .remove = __devexit_p(max17042_remove),
288 .id_table = max17042_id,
289};
290
291static int __init max17042_init(void)
292{
293 return i2c_add_driver(&max17042_i2c_driver);
294}
295module_init(max17042_init);
296
297static void __exit max17042_exit(void)
298{
299 i2c_del_driver(&max17042_i2c_driver);
300}
301module_exit(max17042_exit);
302
303MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
304MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
305MODULE_LICENSE("GPL");