Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 1 | /* |
| 2 | * I2C client/driver for the Maxim/Dallas DS2782 Stand-Alone Fuel Gauge IC |
| 3 | * |
| 4 | * Copyright (C) 2009 Bluewater Systems Ltd |
| 5 | * |
| 6 | * Author: Ryan Mallon <ryan@bluewatersys.com> |
| 7 | * |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 8 | * DS2786 added by Yulia Vilensky <vilensky@compulab.co.il> |
| 9 | * |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/types.h> |
| 19 | #include <linux/errno.h> |
| 20 | #include <linux/swab.h> |
| 21 | #include <linux/i2c.h> |
| 22 | #include <linux/idr.h> |
| 23 | #include <linux/power_supply.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 25 | #include <linux/ds2782_battery.h> |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 26 | |
| 27 | #define DS2782_REG_RARC 0x06 /* Remaining active relative capacity */ |
| 28 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 29 | #define DS278x_REG_VOLT_MSB 0x0c |
| 30 | #define DS278x_REG_TEMP_MSB 0x0a |
| 31 | #define DS278x_REG_CURRENT_MSB 0x0e |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 32 | |
| 33 | /* EEPROM Block */ |
| 34 | #define DS2782_REG_RSNSP 0x69 /* Sense resistor value */ |
| 35 | |
| 36 | /* Current unit measurement in uA for a 1 milli-ohm sense resistor */ |
| 37 | #define DS2782_CURRENT_UNITS 1563 |
| 38 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 39 | #define DS2786_REG_RARC 0x02 /* Remaining active relative capacity */ |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 40 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 41 | #define DS2786_CURRENT_UNITS 25 |
| 42 | |
| 43 | struct ds278x_info; |
| 44 | |
| 45 | struct ds278x_battery_ops { |
| 46 | int (*get_current)(struct ds278x_info *info, int *current_uA); |
| 47 | int (*get_voltage)(struct ds278x_info *info, int *voltage_uA); |
| 48 | int (*get_capacity)(struct ds278x_info *info, int *capacity_uA); |
| 49 | |
| 50 | }; |
| 51 | |
| 52 | #define to_ds278x_info(x) container_of(x, struct ds278x_info, battery) |
| 53 | |
| 54 | struct ds278x_info { |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 55 | struct i2c_client *client; |
| 56 | struct power_supply battery; |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 57 | struct ds278x_battery_ops *ops; |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 58 | int id; |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 59 | int rsns; |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | static DEFINE_IDR(battery_id); |
| 63 | static DEFINE_MUTEX(battery_lock); |
| 64 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 65 | static inline int ds278x_read_reg(struct ds278x_info *info, int reg, u8 *val) |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 66 | { |
| 67 | int ret; |
| 68 | |
| 69 | ret = i2c_smbus_read_byte_data(info->client, reg); |
| 70 | if (ret < 0) { |
| 71 | dev_err(&info->client->dev, "register read failed\n"); |
| 72 | return ret; |
| 73 | } |
| 74 | |
| 75 | *val = ret; |
| 76 | return 0; |
| 77 | } |
| 78 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 79 | static inline int ds278x_read_reg16(struct ds278x_info *info, int reg_msb, |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 80 | s16 *val) |
| 81 | { |
| 82 | int ret; |
| 83 | |
| 84 | ret = swab16(i2c_smbus_read_word_data(info->client, reg_msb)); |
| 85 | if (ret < 0) { |
| 86 | dev_err(&info->client->dev, "register read failed\n"); |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | *val = ret; |
| 91 | return 0; |
| 92 | } |
| 93 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 94 | static int ds278x_get_temp(struct ds278x_info *info, int *temp) |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 95 | { |
| 96 | s16 raw; |
| 97 | int err; |
| 98 | |
| 99 | /* |
| 100 | * Temperature is measured in units of 0.125 degrees celcius, the |
| 101 | * power_supply class measures temperature in tenths of degrees |
| 102 | * celsius. The temperature value is stored as a 10 bit number, plus |
| 103 | * sign in the upper bits of a 16 bit register. |
| 104 | */ |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 105 | err = ds278x_read_reg16(info, DS278x_REG_TEMP_MSB, &raw); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 106 | if (err) |
| 107 | return err; |
| 108 | *temp = ((raw / 32) * 125) / 100; |
| 109 | return 0; |
| 110 | } |
| 111 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 112 | static int ds2782_get_current(struct ds278x_info *info, int *current_uA) |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 113 | { |
| 114 | int sense_res; |
| 115 | int err; |
| 116 | u8 sense_res_raw; |
| 117 | s16 raw; |
| 118 | |
| 119 | /* |
| 120 | * The units of measurement for current are dependent on the value of |
| 121 | * the sense resistor. |
| 122 | */ |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 123 | err = ds278x_read_reg(info, DS2782_REG_RSNSP, &sense_res_raw); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 124 | if (err) |
| 125 | return err; |
| 126 | if (sense_res_raw == 0) { |
| 127 | dev_err(&info->client->dev, "sense resistor value is 0\n"); |
| 128 | return -ENXIO; |
| 129 | } |
| 130 | sense_res = 1000 / sense_res_raw; |
| 131 | |
| 132 | dev_dbg(&info->client->dev, "sense resistor = %d milli-ohms\n", |
| 133 | sense_res); |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 134 | err = ds278x_read_reg16(info, DS278x_REG_CURRENT_MSB, &raw); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 135 | if (err) |
| 136 | return err; |
| 137 | *current_uA = raw * (DS2782_CURRENT_UNITS / sense_res); |
| 138 | return 0; |
| 139 | } |
| 140 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 141 | static int ds2782_get_voltage(struct ds278x_info *info, int *voltage_uA) |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 142 | { |
| 143 | s16 raw; |
| 144 | int err; |
| 145 | |
| 146 | /* |
| 147 | * Voltage is measured in units of 4.88mV. The voltage is stored as |
| 148 | * a 10-bit number plus sign, in the upper bits of a 16-bit register |
| 149 | */ |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 150 | err = ds278x_read_reg16(info, DS278x_REG_VOLT_MSB, &raw); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 151 | if (err) |
| 152 | return err; |
| 153 | *voltage_uA = (raw / 32) * 4800; |
| 154 | return 0; |
| 155 | } |
| 156 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 157 | static int ds2782_get_capacity(struct ds278x_info *info, int *capacity) |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 158 | { |
| 159 | int err; |
| 160 | u8 raw; |
| 161 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 162 | err = ds278x_read_reg(info, DS2782_REG_RARC, &raw); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 163 | if (err) |
| 164 | return err; |
| 165 | *capacity = raw; |
| 166 | return raw; |
| 167 | } |
| 168 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 169 | static int ds2786_get_current(struct ds278x_info *info, int *current_uA) |
| 170 | { |
| 171 | int err; |
| 172 | s16 raw; |
| 173 | |
| 174 | err = ds278x_read_reg16(info, DS278x_REG_CURRENT_MSB, &raw); |
| 175 | if (err) |
| 176 | return err; |
| 177 | *current_uA = (raw / 16) * (DS2786_CURRENT_UNITS / info->rsns); |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static int ds2786_get_voltage(struct ds278x_info *info, int *voltage_uA) |
| 182 | { |
| 183 | s16 raw; |
| 184 | int err; |
| 185 | |
| 186 | /* |
| 187 | * Voltage is measured in units of 1.22mV. The voltage is stored as |
| 188 | * a 10-bit number plus sign, in the upper bits of a 16-bit register |
| 189 | */ |
| 190 | err = ds278x_read_reg16(info, DS278x_REG_VOLT_MSB, &raw); |
| 191 | if (err) |
| 192 | return err; |
| 193 | *voltage_uA = (raw / 8) * 1220; |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static int ds2786_get_capacity(struct ds278x_info *info, int *capacity) |
| 198 | { |
| 199 | int err; |
| 200 | u8 raw; |
| 201 | |
| 202 | err = ds278x_read_reg(info, DS2786_REG_RARC, &raw); |
| 203 | if (err) |
| 204 | return err; |
| 205 | /* Relative capacity is displayed with resolution 0.5 % */ |
| 206 | *capacity = raw/2 ; |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | static int ds278x_get_status(struct ds278x_info *info, int *status) |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 211 | { |
| 212 | int err; |
| 213 | int current_uA; |
| 214 | int capacity; |
| 215 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 216 | err = info->ops->get_current(info, ¤t_uA); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 217 | if (err) |
| 218 | return err; |
| 219 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 220 | err = info->ops->get_capacity(info, &capacity); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 221 | if (err) |
| 222 | return err; |
| 223 | |
| 224 | if (capacity == 100) |
| 225 | *status = POWER_SUPPLY_STATUS_FULL; |
| 226 | else if (current_uA == 0) |
| 227 | *status = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 228 | else if (current_uA < 0) |
| 229 | *status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 230 | else |
| 231 | *status = POWER_SUPPLY_STATUS_CHARGING; |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 236 | static int ds278x_battery_get_property(struct power_supply *psy, |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 237 | enum power_supply_property prop, |
| 238 | union power_supply_propval *val) |
| 239 | { |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 240 | struct ds278x_info *info = to_ds278x_info(psy); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 241 | int ret; |
| 242 | |
| 243 | switch (prop) { |
| 244 | case POWER_SUPPLY_PROP_STATUS: |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 245 | ret = ds278x_get_status(info, &val->intval); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 246 | break; |
| 247 | |
| 248 | case POWER_SUPPLY_PROP_CAPACITY: |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 249 | ret = info->ops->get_capacity(info, &val->intval); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 250 | break; |
| 251 | |
| 252 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 253 | ret = info->ops->get_voltage(info, &val->intval); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 254 | break; |
| 255 | |
| 256 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 257 | ret = info->ops->get_current(info, &val->intval); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 258 | break; |
| 259 | |
| 260 | case POWER_SUPPLY_PROP_TEMP: |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 261 | ret = ds278x_get_temp(info, &val->intval); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 262 | break; |
| 263 | |
| 264 | default: |
| 265 | ret = -EINVAL; |
| 266 | } |
| 267 | |
| 268 | return ret; |
| 269 | } |
| 270 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 271 | static enum power_supply_property ds278x_battery_props[] = { |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 272 | POWER_SUPPLY_PROP_STATUS, |
| 273 | POWER_SUPPLY_PROP_CAPACITY, |
| 274 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 275 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 276 | POWER_SUPPLY_PROP_TEMP, |
| 277 | }; |
| 278 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 279 | static void ds278x_power_supply_init(struct power_supply *battery) |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 280 | { |
| 281 | battery->type = POWER_SUPPLY_TYPE_BATTERY; |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 282 | battery->properties = ds278x_battery_props; |
| 283 | battery->num_properties = ARRAY_SIZE(ds278x_battery_props); |
| 284 | battery->get_property = ds278x_battery_get_property; |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 285 | battery->external_power_changed = NULL; |
| 286 | } |
| 287 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 288 | static int ds278x_battery_remove(struct i2c_client *client) |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 289 | { |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 290 | struct ds278x_info *info = i2c_get_clientdata(client); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 291 | |
| 292 | power_supply_unregister(&info->battery); |
| 293 | kfree(info->battery.name); |
| 294 | |
| 295 | mutex_lock(&battery_lock); |
| 296 | idr_remove(&battery_id, info->id); |
| 297 | mutex_unlock(&battery_lock); |
| 298 | |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 299 | kfree(info); |
| 300 | return 0; |
| 301 | } |
| 302 | |
Anton Vorontsov | ab6cc8f | 2010-04-26 22:10:52 +0400 | [diff] [blame] | 303 | enum ds278x_num_id { |
| 304 | DS2782 = 0, |
| 305 | DS2786, |
| 306 | }; |
| 307 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 308 | static struct ds278x_battery_ops ds278x_ops[] = { |
Anton Vorontsov | ab6cc8f | 2010-04-26 22:10:52 +0400 | [diff] [blame] | 309 | [DS2782] = { |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 310 | .get_current = ds2782_get_current, |
| 311 | .get_voltage = ds2782_get_voltage, |
| 312 | .get_capacity = ds2782_get_capacity, |
| 313 | }, |
Anton Vorontsov | ab6cc8f | 2010-04-26 22:10:52 +0400 | [diff] [blame] | 314 | [DS2786] = { |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 315 | .get_current = ds2786_get_current, |
| 316 | .get_voltage = ds2786_get_voltage, |
| 317 | .get_capacity = ds2786_get_capacity, |
| 318 | } |
| 319 | }; |
| 320 | |
| 321 | static int ds278x_battery_probe(struct i2c_client *client, |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 322 | const struct i2c_device_id *id) |
| 323 | { |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 324 | struct ds278x_platform_data *pdata = client->dev.platform_data; |
| 325 | struct ds278x_info *info; |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 326 | int ret; |
| 327 | int num; |
| 328 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 329 | /* |
| 330 | * ds2786 should have the sense resistor value set |
| 331 | * in the platform data |
| 332 | */ |
Anton Vorontsov | ab6cc8f | 2010-04-26 22:10:52 +0400 | [diff] [blame] | 333 | if (id->driver_data == DS2786 && !pdata) { |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 334 | dev_err(&client->dev, "missing platform data for ds2786\n"); |
| 335 | return -EINVAL; |
| 336 | } |
| 337 | |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 338 | /* Get an ID for this battery */ |
| 339 | ret = idr_pre_get(&battery_id, GFP_KERNEL); |
| 340 | if (ret == 0) { |
| 341 | ret = -ENOMEM; |
| 342 | goto fail_id; |
| 343 | } |
| 344 | |
| 345 | mutex_lock(&battery_lock); |
| 346 | ret = idr_get_new(&battery_id, client, &num); |
| 347 | mutex_unlock(&battery_lock); |
| 348 | if (ret < 0) |
| 349 | goto fail_id; |
| 350 | |
| 351 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
| 352 | if (!info) { |
| 353 | ret = -ENOMEM; |
| 354 | goto fail_info; |
| 355 | } |
| 356 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 357 | info->battery.name = kasprintf(GFP_KERNEL, "%s-%d", client->name, num); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 358 | if (!info->battery.name) { |
| 359 | ret = -ENOMEM; |
| 360 | goto fail_name; |
| 361 | } |
| 362 | |
Anton Vorontsov | ab6cc8f | 2010-04-26 22:10:52 +0400 | [diff] [blame] | 363 | if (id->driver_data == DS2786) |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 364 | info->rsns = pdata->rsns; |
| 365 | |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 366 | i2c_set_clientdata(client, info); |
| 367 | info->client = client; |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 368 | info->id = num; |
| 369 | info->ops = &ds278x_ops[id->driver_data]; |
| 370 | ds278x_power_supply_init(&info->battery); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 371 | |
| 372 | ret = power_supply_register(&client->dev, &info->battery); |
| 373 | if (ret) { |
| 374 | dev_err(&client->dev, "failed to register battery\n"); |
| 375 | goto fail_register; |
| 376 | } |
| 377 | |
| 378 | return 0; |
| 379 | |
| 380 | fail_register: |
| 381 | kfree(info->battery.name); |
| 382 | fail_name: |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 383 | kfree(info); |
| 384 | fail_info: |
| 385 | mutex_lock(&battery_lock); |
| 386 | idr_remove(&battery_id, num); |
| 387 | mutex_unlock(&battery_lock); |
| 388 | fail_id: |
| 389 | return ret; |
| 390 | } |
| 391 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 392 | static const struct i2c_device_id ds278x_id[] = { |
Anton Vorontsov | ab6cc8f | 2010-04-26 22:10:52 +0400 | [diff] [blame] | 393 | {"ds2782", DS2782}, |
| 394 | {"ds2786", DS2786}, |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 395 | {}, |
| 396 | }; |
| 397 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 398 | static struct i2c_driver ds278x_battery_driver = { |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 399 | .driver = { |
| 400 | .name = "ds2782-battery", |
| 401 | }, |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 402 | .probe = ds278x_battery_probe, |
| 403 | .remove = ds278x_battery_remove, |
| 404 | .id_table = ds278x_id, |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 405 | }; |
| 406 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 407 | static int __init ds278x_init(void) |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 408 | { |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 409 | return i2c_add_driver(&ds278x_battery_driver); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 410 | } |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 411 | module_init(ds278x_init); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 412 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 413 | static void __exit ds278x_exit(void) |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 414 | { |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 415 | i2c_del_driver(&ds278x_battery_driver); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 416 | } |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 417 | module_exit(ds278x_exit); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 418 | |
| 419 | MODULE_AUTHOR("Ryan Mallon <ryan@bluewatersys.com>"); |
| 420 | MODULE_DESCRIPTION("Maxim/Dallas DS2782 Stand-Alone Fuel Gauage IC driver"); |
| 421 | MODULE_LICENSE("GPL"); |