ds2760_battery: Add rated capacity of the hx4700 3600mAh battery
[linux-2.6-block.git] / drivers / power / max17042_battery.c
1 /*
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>
26 #include <linux/slab.h>
27 #include <linux/i2c.h>
28 #include <linux/mod_devicetable.h>
29 #include <linux/power_supply.h>
30 #include <linux/power/max17042_battery.h>
31
32 struct max17042_chip {
33         struct i2c_client *client;
34         struct power_supply battery;
35         struct max17042_platform_data *pdata;
36 };
37
38 static int max17042_write_reg(struct i2c_client *client, u8 reg, u16 value)
39 {
40         int ret = i2c_smbus_write_word_data(client, reg, value);
41
42         if (ret < 0)
43                 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
44
45         return ret;
46 }
47
48 static int max17042_read_reg(struct i2c_client *client, u8 reg)
49 {
50         int ret = i2c_smbus_read_word_data(client, reg);
51
52         if (ret < 0)
53                 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
54
55         return ret;
56 }
57
58 static void max17042_set_reg(struct i2c_client *client,
59                              struct max17042_reg_data *data, int size)
60 {
61         int i;
62
63         for (i = 0; i < size; i++)
64                 max17042_write_reg(client, data[i].addr, data[i].data);
65 }
66
67 static enum power_supply_property max17042_battery_props[] = {
68         POWER_SUPPLY_PROP_PRESENT,
69         POWER_SUPPLY_PROP_CYCLE_COUNT,
70         POWER_SUPPLY_PROP_VOLTAGE_MAX,
71         POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
72         POWER_SUPPLY_PROP_VOLTAGE_NOW,
73         POWER_SUPPLY_PROP_VOLTAGE_AVG,
74         POWER_SUPPLY_PROP_CAPACITY,
75         POWER_SUPPLY_PROP_CHARGE_FULL,
76         POWER_SUPPLY_PROP_TEMP,
77         POWER_SUPPLY_PROP_CURRENT_NOW,
78         POWER_SUPPLY_PROP_CURRENT_AVG,
79 };
80
81 static int max17042_get_property(struct power_supply *psy,
82                             enum power_supply_property psp,
83                             union power_supply_propval *val)
84 {
85         struct max17042_chip *chip = container_of(psy,
86                                 struct max17042_chip, battery);
87
88         switch (psp) {
89         case POWER_SUPPLY_PROP_PRESENT:
90                 val->intval = max17042_read_reg(chip->client,
91                                 MAX17042_STATUS);
92                 if (val->intval & MAX17042_STATUS_BattAbsent)
93                         val->intval = 0;
94                 else
95                         val->intval = 1;
96                 break;
97         case POWER_SUPPLY_PROP_CYCLE_COUNT:
98                 val->intval = max17042_read_reg(chip->client,
99                                 MAX17042_Cycles);
100                 break;
101         case POWER_SUPPLY_PROP_VOLTAGE_MAX:
102                 val->intval = max17042_read_reg(chip->client,
103                                 MAX17042_MinMaxVolt);
104                 val->intval >>= 8;
105                 val->intval *= 20000; /* Units of LSB = 20mV */
106                 break;
107         case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
108                 val->intval = max17042_read_reg(chip->client,
109                                 MAX17042_V_empty);
110                 val->intval >>= 7;
111                 val->intval *= 10000; /* Units of LSB = 10mV */
112                 break;
113         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
114                 val->intval = max17042_read_reg(chip->client, MAX17042_VCELL)
115                             * 625 / 8;
116                 break;
117         case POWER_SUPPLY_PROP_VOLTAGE_AVG:
118                 val->intval = max17042_read_reg(chip->client, MAX17042_AvgVCELL)
119                             * 625 / 8;
120                 break;
121         case POWER_SUPPLY_PROP_CAPACITY:
122                 val->intval = max17042_read_reg(chip->client,
123                                 MAX17042_SOC) / 256;
124                 break;
125         case POWER_SUPPLY_PROP_CHARGE_FULL:
126                 val->intval = max17042_read_reg(chip->client,
127                                 MAX17042_RepSOC);
128                 if ((val->intval / 256) >= MAX17042_BATTERY_FULL)
129                         val->intval = 1;
130                 else if (val->intval >= 0)
131                         val->intval = 0;
132                 break;
133         case POWER_SUPPLY_PROP_TEMP:
134                 val->intval = max17042_read_reg(chip->client,
135                                 MAX17042_TEMP);
136                 /* The value is signed. */
137                 if (val->intval & 0x8000) {
138                         val->intval = (0x7fff & ~val->intval) + 1;
139                         val->intval *= -1;
140                 }
141                 /* The value is converted into deci-centigrade scale */
142                 /* Units of LSB = 1 / 256 degree Celsius */
143                 val->intval = val->intval * 10 / 256;
144                 break;
145         case POWER_SUPPLY_PROP_CURRENT_NOW:
146                 if (chip->pdata->enable_current_sense) {
147                         val->intval = max17042_read_reg(chip->client,
148                                         MAX17042_Current);
149                         if (val->intval & 0x8000) {
150                                 /* Negative */
151                                 val->intval = ~val->intval & 0x7fff;
152                                 val->intval++;
153                                 val->intval *= -1;
154                         }
155                         val->intval *= 1562500 / chip->pdata->r_sns;
156                 } else {
157                         return -EINVAL;
158                 }
159                 break;
160         case POWER_SUPPLY_PROP_CURRENT_AVG:
161                 if (chip->pdata->enable_current_sense) {
162                         val->intval = max17042_read_reg(chip->client,
163                                         MAX17042_AvgCurrent);
164                         if (val->intval & 0x8000) {
165                                 /* Negative */
166                                 val->intval = ~val->intval & 0x7fff;
167                                 val->intval++;
168                                 val->intval *= -1;
169                         }
170                         val->intval *= 1562500 / chip->pdata->r_sns;
171                 } else {
172                         return -EINVAL;
173                 }
174                 break;
175         default:
176                 return -EINVAL;
177         }
178         return 0;
179 }
180
181 static int __devinit max17042_probe(struct i2c_client *client,
182                         const struct i2c_device_id *id)
183 {
184         struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
185         struct max17042_chip *chip;
186         int ret;
187
188         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
189                 return -EIO;
190
191         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
192         if (!chip)
193                 return -ENOMEM;
194
195         chip->client = client;
196         chip->pdata = client->dev.platform_data;
197
198         i2c_set_clientdata(client, chip);
199
200         chip->battery.name              = "max17042_battery";
201         chip->battery.type              = POWER_SUPPLY_TYPE_BATTERY;
202         chip->battery.get_property      = max17042_get_property;
203         chip->battery.properties        = max17042_battery_props;
204         chip->battery.num_properties    = ARRAY_SIZE(max17042_battery_props);
205
206         /* When current is not measured,
207          * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
208         if (!chip->pdata->enable_current_sense)
209                 chip->battery.num_properties -= 2;
210
211         if (chip->pdata->r_sns == 0)
212                 chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR;
213
214         ret = power_supply_register(&client->dev, &chip->battery);
215         if (ret) {
216                 dev_err(&client->dev, "failed: power supply register\n");
217                 kfree(chip);
218                 return ret;
219         }
220
221         /* Initialize registers according to values from the platform data */
222         if (chip->pdata->init_data)
223                 max17042_set_reg(client, chip->pdata->init_data,
224                                  chip->pdata->num_init_data);
225
226         if (!chip->pdata->enable_current_sense) {
227                 max17042_write_reg(client, MAX17042_CGAIN, 0x0000);
228                 max17042_write_reg(client, MAX17042_MiscCFG, 0x0003);
229                 max17042_write_reg(client, MAX17042_LearnCFG, 0x0007);
230         }
231
232         return 0;
233 }
234
235 static int __devexit max17042_remove(struct i2c_client *client)
236 {
237         struct max17042_chip *chip = i2c_get_clientdata(client);
238
239         power_supply_unregister(&chip->battery);
240         kfree(chip);
241         return 0;
242 }
243
244 static const struct i2c_device_id max17042_id[] = {
245         { "max17042", 0 },
246         { }
247 };
248 MODULE_DEVICE_TABLE(i2c, max17042_id);
249
250 static struct i2c_driver max17042_i2c_driver = {
251         .driver = {
252                 .name   = "max17042",
253         },
254         .probe          = max17042_probe,
255         .remove         = __devexit_p(max17042_remove),
256         .id_table       = max17042_id,
257 };
258
259 static int __init max17042_init(void)
260 {
261         return i2c_add_driver(&max17042_i2c_driver);
262 }
263 module_init(max17042_init);
264
265 static void __exit max17042_exit(void)
266 {
267         i2c_del_driver(&max17042_i2c_driver);
268 }
269 module_exit(max17042_exit);
270
271 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
272 MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
273 MODULE_LICENSE("GPL");