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