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