| 1 | /* |
| 2 | * BQ27x00 battery driver |
| 3 | * |
| 4 | * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it> |
| 5 | * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it> |
| 6 | * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de> |
| 7 | * |
| 8 | * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc. |
| 9 | * |
| 10 | * This package 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 | * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
| 15 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 16 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 17 | * |
| 18 | */ |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/param.h> |
| 21 | #include <linux/jiffies.h> |
| 22 | #include <linux/workqueue.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/power_supply.h> |
| 26 | #include <linux/idr.h> |
| 27 | #include <linux/i2c.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <asm/unaligned.h> |
| 30 | |
| 31 | #include <linux/power/bq27x00_battery.h> |
| 32 | |
| 33 | #define DRIVER_VERSION "1.1.0" |
| 34 | |
| 35 | #define BQ27x00_REG_TEMP 0x06 |
| 36 | #define BQ27x00_REG_VOLT 0x08 |
| 37 | #define BQ27x00_REG_AI 0x14 |
| 38 | #define BQ27x00_REG_FLAGS 0x0A |
| 39 | #define BQ27x00_REG_TTE 0x16 |
| 40 | #define BQ27x00_REG_TTF 0x18 |
| 41 | #define BQ27x00_REG_TTECP 0x26 |
| 42 | |
| 43 | #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */ |
| 44 | #define BQ27000_FLAG_CHGS BIT(7) |
| 45 | |
| 46 | #define BQ27500_REG_SOC 0x2c |
| 47 | #define BQ27500_FLAG_DSC BIT(0) |
| 48 | #define BQ27500_FLAG_FC BIT(9) |
| 49 | |
| 50 | #define BQ27000_RS 20 /* Resistor sense */ |
| 51 | |
| 52 | struct bq27x00_device_info; |
| 53 | struct bq27x00_access_methods { |
| 54 | int (*read)(struct bq27x00_device_info *di, u8 reg, bool single); |
| 55 | }; |
| 56 | |
| 57 | enum bq27x00_chip { BQ27000, BQ27500 }; |
| 58 | |
| 59 | struct bq27x00_reg_cache { |
| 60 | int temperature; |
| 61 | int time_to_empty; |
| 62 | int time_to_empty_avg; |
| 63 | int time_to_full; |
| 64 | int capacity; |
| 65 | int flags; |
| 66 | |
| 67 | int current_now; |
| 68 | }; |
| 69 | |
| 70 | struct bq27x00_device_info { |
| 71 | struct device *dev; |
| 72 | int id; |
| 73 | enum bq27x00_chip chip; |
| 74 | |
| 75 | struct bq27x00_reg_cache cache; |
| 76 | unsigned long last_update; |
| 77 | |
| 78 | struct power_supply bat; |
| 79 | |
| 80 | struct bq27x00_access_methods bus; |
| 81 | }; |
| 82 | |
| 83 | static enum power_supply_property bq27x00_battery_props[] = { |
| 84 | POWER_SUPPLY_PROP_STATUS, |
| 85 | POWER_SUPPLY_PROP_PRESENT, |
| 86 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 87 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 88 | POWER_SUPPLY_PROP_CAPACITY, |
| 89 | POWER_SUPPLY_PROP_TEMP, |
| 90 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 91 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, |
| 92 | POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, |
| 93 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 94 | }; |
| 95 | |
| 96 | /* |
| 97 | * Common code for BQ27x00 devices |
| 98 | */ |
| 99 | |
| 100 | static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg, |
| 101 | bool single) |
| 102 | { |
| 103 | return di->bus.read(di, reg, single); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Return the battery Relative State-of-Charge |
| 108 | * Or < 0 if something fails. |
| 109 | */ |
| 110 | static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di) |
| 111 | { |
| 112 | int rsoc; |
| 113 | |
| 114 | if (di->chip == BQ27500) |
| 115 | rsoc = bq27x00_read(di, BQ27500_REG_SOC, false); |
| 116 | else |
| 117 | rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true); |
| 118 | |
| 119 | if (rsoc < 0) |
| 120 | dev_err(di->dev, "error reading relative State-of-Charge\n"); |
| 121 | |
| 122 | return rsoc; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Read a time register. |
| 127 | * Return < 0 if something fails. |
| 128 | */ |
| 129 | static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg) |
| 130 | { |
| 131 | int tval; |
| 132 | |
| 133 | tval = bq27x00_read(di, reg, false); |
| 134 | if (tval < 0) { |
| 135 | dev_err(di->dev, "error reading register %02x: %d\n", reg, tval); |
| 136 | return tval; |
| 137 | } |
| 138 | |
| 139 | if (tval == 65535) |
| 140 | return -ENODATA; |
| 141 | |
| 142 | return tval * 60; |
| 143 | } |
| 144 | |
| 145 | static void bq27x00_update(struct bq27x00_device_info *di) |
| 146 | { |
| 147 | struct bq27x00_reg_cache cache = {0, }; |
| 148 | bool is_bq27500 = di->chip == BQ27500; |
| 149 | |
| 150 | cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, is_bq27500); |
| 151 | if (cache.flags >= 0) { |
| 152 | cache.capacity = bq27x00_battery_read_rsoc(di); |
| 153 | cache.temperature = bq27x00_read(di, BQ27x00_REG_TEMP, false); |
| 154 | cache.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE); |
| 155 | cache.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP); |
| 156 | cache.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF); |
| 157 | |
| 158 | if (!is_bq27500) |
| 159 | cache.current_now = bq27x00_read(di, BQ27x00_REG_AI, false); |
| 160 | } |
| 161 | |
| 162 | /* Ignore current_now which is a snapshot of the current battery state |
| 163 | * and is likely to be different even between two consecutive reads */ |
| 164 | if (memcmp(&di->cache, &cache, sizeof(cache) - sizeof(int)) != 0) { |
| 165 | di->cache = cache; |
| 166 | power_supply_changed(&di->bat); |
| 167 | } |
| 168 | |
| 169 | di->last_update = jiffies; |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Return the battery temperature in tenths of degree Celsius |
| 174 | * Or < 0 if something fails. |
| 175 | */ |
| 176 | static int bq27x00_battery_temperature(struct bq27x00_device_info *di, |
| 177 | union power_supply_propval *val) |
| 178 | { |
| 179 | if (di->cache.temperature < 0) |
| 180 | return di->cache.temperature; |
| 181 | |
| 182 | if (di->chip == BQ27500) |
| 183 | val->intval = di->cache.temperature - 2731; |
| 184 | else |
| 185 | val->intval = ((di->cache.temperature * 5) - 5463) / 2; |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Return the battery average current |
| 192 | * Note that current can be negative signed as well |
| 193 | * Or 0 if something fails. |
| 194 | */ |
| 195 | static int bq27x00_battery_current(struct bq27x00_device_info *di, |
| 196 | union power_supply_propval *val) |
| 197 | { |
| 198 | int curr; |
| 199 | |
| 200 | if (di->chip == BQ27500) |
| 201 | curr = bq27x00_read(di, BQ27x00_REG_AI, false); |
| 202 | else |
| 203 | curr = di->cache.current_now; |
| 204 | |
| 205 | if (curr < 0) |
| 206 | return curr; |
| 207 | |
| 208 | if (di->chip == BQ27500) { |
| 209 | /* bq27500 returns signed value */ |
| 210 | val->intval = (int)((s16)curr) * 1000; |
| 211 | } else { |
| 212 | if (di->cache.flags & BQ27000_FLAG_CHGS) { |
| 213 | dev_dbg(di->dev, "negative current!\n"); |
| 214 | curr = -curr; |
| 215 | } |
| 216 | |
| 217 | val->intval = curr * 3570 / BQ27000_RS; |
| 218 | } |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | static int bq27x00_battery_status(struct bq27x00_device_info *di, |
| 224 | union power_supply_propval *val) |
| 225 | { |
| 226 | int status; |
| 227 | |
| 228 | if (di->chip == BQ27500) { |
| 229 | if (di->cache.flags & BQ27500_FLAG_FC) |
| 230 | status = POWER_SUPPLY_STATUS_FULL; |
| 231 | else if (di->cache.flags & BQ27500_FLAG_DSC) |
| 232 | status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 233 | else |
| 234 | status = POWER_SUPPLY_STATUS_CHARGING; |
| 235 | } else { |
| 236 | if (di->cache.flags & BQ27000_FLAG_CHGS) |
| 237 | status = POWER_SUPPLY_STATUS_CHARGING; |
| 238 | else |
| 239 | status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 240 | } |
| 241 | |
| 242 | val->intval = status; |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Return the battery Voltage in milivolts |
| 249 | * Or < 0 if something fails. |
| 250 | */ |
| 251 | static int bq27x00_battery_voltage(struct bq27x00_device_info *di, |
| 252 | union power_supply_propval *val) |
| 253 | { |
| 254 | int volt; |
| 255 | |
| 256 | volt = bq27x00_read(di, BQ27x00_REG_VOLT, false); |
| 257 | if (volt < 0) |
| 258 | return volt; |
| 259 | |
| 260 | val->intval = volt * 1000; |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static int bq27x00_simple_value(int value, |
| 266 | union power_supply_propval *val) |
| 267 | { |
| 268 | if (value < 0) |
| 269 | return value; |
| 270 | |
| 271 | val->intval = value; |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | #define to_bq27x00_device_info(x) container_of((x), \ |
| 277 | struct bq27x00_device_info, bat); |
| 278 | |
| 279 | static int bq27x00_battery_get_property(struct power_supply *psy, |
| 280 | enum power_supply_property psp, |
| 281 | union power_supply_propval *val) |
| 282 | { |
| 283 | int ret = 0; |
| 284 | struct bq27x00_device_info *di = to_bq27x00_device_info(psy); |
| 285 | |
| 286 | if (time_is_before_jiffies(di->last_update + 5 * HZ)) |
| 287 | bq27x00_update(di); |
| 288 | |
| 289 | if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0) |
| 290 | return -ENODEV; |
| 291 | |
| 292 | switch (psp) { |
| 293 | case POWER_SUPPLY_PROP_STATUS: |
| 294 | ret = bq27x00_battery_status(di, val); |
| 295 | break; |
| 296 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 297 | ret = bq27x00_battery_voltage(di, val); |
| 298 | break; |
| 299 | case POWER_SUPPLY_PROP_PRESENT: |
| 300 | val->intval = di->cache.flags < 0 ? 0 : 1; |
| 301 | break; |
| 302 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 303 | ret = bq27x00_battery_current(di, val); |
| 304 | break; |
| 305 | case POWER_SUPPLY_PROP_CAPACITY: |
| 306 | ret = bq27x00_simple_value(di->cache.capacity, val); |
| 307 | break; |
| 308 | case POWER_SUPPLY_PROP_TEMP: |
| 309 | ret = bq27x00_battery_temperature(di, val); |
| 310 | break; |
| 311 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: |
| 312 | ret = bq27x00_simple_value(di->cache.time_to_empty, val); |
| 313 | break; |
| 314 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
| 315 | ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val); |
| 316 | break; |
| 317 | case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW: |
| 318 | ret = bq27x00_simple_value(di->cache.time_to_full, val); |
| 319 | break; |
| 320 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 321 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 322 | break; |
| 323 | default: |
| 324 | return -EINVAL; |
| 325 | } |
| 326 | |
| 327 | return ret; |
| 328 | } |
| 329 | |
| 330 | static int bq27x00_powersupply_init(struct bq27x00_device_info *di) |
| 331 | { |
| 332 | int ret; |
| 333 | |
| 334 | di->bat.type = POWER_SUPPLY_TYPE_BATTERY; |
| 335 | di->bat.properties = bq27x00_battery_props; |
| 336 | di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props); |
| 337 | di->bat.get_property = bq27x00_battery_get_property; |
| 338 | di->bat.external_power_changed = NULL; |
| 339 | |
| 340 | ret = power_supply_register(di->dev, &di->bat); |
| 341 | if (ret) { |
| 342 | dev_err(di->dev, "failed to register battery: %d\n", ret); |
| 343 | return ret; |
| 344 | } |
| 345 | |
| 346 | dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION); |
| 347 | |
| 348 | bq27x00_update(di); |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | |
| 354 | /* i2c specific code */ |
| 355 | #ifdef CONFIG_BATTERY_BQ27X00_I2C |
| 356 | |
| 357 | /* If the system has several batteries we need a different name for each |
| 358 | * of them... |
| 359 | */ |
| 360 | static DEFINE_IDR(battery_id); |
| 361 | static DEFINE_MUTEX(battery_mutex); |
| 362 | |
| 363 | static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single) |
| 364 | { |
| 365 | struct i2c_client *client = to_i2c_client(di->dev); |
| 366 | struct i2c_msg msg[1]; |
| 367 | unsigned char data[2]; |
| 368 | int ret; |
| 369 | |
| 370 | if (!client->adapter) |
| 371 | return -ENODEV; |
| 372 | |
| 373 | msg->addr = client->addr; |
| 374 | msg->flags = 0; |
| 375 | msg->len = 1; |
| 376 | msg->buf = data; |
| 377 | |
| 378 | data[0] = reg; |
| 379 | ret = i2c_transfer(client->adapter, msg, 1); |
| 380 | |
| 381 | if (ret >= 0) { |
| 382 | if (!single) |
| 383 | msg->len = 2; |
| 384 | else |
| 385 | msg->len = 1; |
| 386 | |
| 387 | msg->flags = I2C_M_RD; |
| 388 | ret = i2c_transfer(client->adapter, msg, 1); |
| 389 | if (ret >= 0) { |
| 390 | if (!single) |
| 391 | ret = get_unaligned_le16(data); |
| 392 | else |
| 393 | ret = data[0]; |
| 394 | } |
| 395 | } |
| 396 | return ret; |
| 397 | } |
| 398 | |
| 399 | static int bq27x00_battery_probe(struct i2c_client *client, |
| 400 | const struct i2c_device_id *id) |
| 401 | { |
| 402 | char *name; |
| 403 | struct bq27x00_device_info *di; |
| 404 | int num; |
| 405 | int retval = 0; |
| 406 | |
| 407 | /* Get new ID for the new battery device */ |
| 408 | retval = idr_pre_get(&battery_id, GFP_KERNEL); |
| 409 | if (retval == 0) |
| 410 | return -ENOMEM; |
| 411 | mutex_lock(&battery_mutex); |
| 412 | retval = idr_get_new(&battery_id, client, &num); |
| 413 | mutex_unlock(&battery_mutex); |
| 414 | if (retval < 0) |
| 415 | return retval; |
| 416 | |
| 417 | name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num); |
| 418 | if (!name) { |
| 419 | dev_err(&client->dev, "failed to allocate device name\n"); |
| 420 | retval = -ENOMEM; |
| 421 | goto batt_failed_1; |
| 422 | } |
| 423 | |
| 424 | di = kzalloc(sizeof(*di), GFP_KERNEL); |
| 425 | if (!di) { |
| 426 | dev_err(&client->dev, "failed to allocate device info data\n"); |
| 427 | retval = -ENOMEM; |
| 428 | goto batt_failed_2; |
| 429 | } |
| 430 | |
| 431 | di->id = num; |
| 432 | di->dev = &client->dev; |
| 433 | di->chip = id->driver_data; |
| 434 | di->bat.name = name; |
| 435 | di->bus.read = &bq27x00_read_i2c; |
| 436 | |
| 437 | if (bq27x00_powersupply_init(di)) |
| 438 | goto batt_failed_3; |
| 439 | |
| 440 | i2c_set_clientdata(client, di); |
| 441 | |
| 442 | return 0; |
| 443 | |
| 444 | batt_failed_3: |
| 445 | kfree(di); |
| 446 | batt_failed_2: |
| 447 | kfree(name); |
| 448 | batt_failed_1: |
| 449 | mutex_lock(&battery_mutex); |
| 450 | idr_remove(&battery_id, num); |
| 451 | mutex_unlock(&battery_mutex); |
| 452 | |
| 453 | return retval; |
| 454 | } |
| 455 | |
| 456 | static int bq27x00_battery_remove(struct i2c_client *client) |
| 457 | { |
| 458 | struct bq27x00_device_info *di = i2c_get_clientdata(client); |
| 459 | |
| 460 | power_supply_unregister(&di->bat); |
| 461 | |
| 462 | kfree(di->bat.name); |
| 463 | |
| 464 | mutex_lock(&battery_mutex); |
| 465 | idr_remove(&battery_id, di->id); |
| 466 | mutex_unlock(&battery_mutex); |
| 467 | |
| 468 | kfree(di); |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | static const struct i2c_device_id bq27x00_id[] = { |
| 474 | { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */ |
| 475 | { "bq27500", BQ27500 }, |
| 476 | {}, |
| 477 | }; |
| 478 | |
| 479 | static struct i2c_driver bq27x00_battery_driver = { |
| 480 | .driver = { |
| 481 | .name = "bq27x00-battery", |
| 482 | }, |
| 483 | .probe = bq27x00_battery_probe, |
| 484 | .remove = bq27x00_battery_remove, |
| 485 | .id_table = bq27x00_id, |
| 486 | }; |
| 487 | |
| 488 | static inline int bq27x00_battery_i2c_init(void) |
| 489 | { |
| 490 | int ret = i2c_add_driver(&bq27x00_battery_driver); |
| 491 | if (ret) |
| 492 | printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n"); |
| 493 | |
| 494 | return ret; |
| 495 | } |
| 496 | |
| 497 | static inline void bq27x00_battery_i2c_exit(void) |
| 498 | { |
| 499 | i2c_del_driver(&bq27x00_battery_driver); |
| 500 | } |
| 501 | |
| 502 | #else |
| 503 | |
| 504 | static inline int bq27x00_battery_i2c_init(void) { return 0; } |
| 505 | static inline void bq27x00_battery_i2c_exit(void) {}; |
| 506 | |
| 507 | #endif |
| 508 | |
| 509 | /* platform specific code */ |
| 510 | #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM |
| 511 | |
| 512 | static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg, |
| 513 | bool single) |
| 514 | { |
| 515 | struct device *dev = di->dev; |
| 516 | struct bq27000_platform_data *pdata = dev->platform_data; |
| 517 | unsigned int timeout = 3; |
| 518 | int upper, lower; |
| 519 | int temp; |
| 520 | |
| 521 | if (!single) { |
| 522 | /* Make sure the value has not changed in between reading the |
| 523 | * lower and the upper part */ |
| 524 | upper = pdata->read(dev, reg + 1); |
| 525 | do { |
| 526 | temp = upper; |
| 527 | if (upper < 0) |
| 528 | return upper; |
| 529 | |
| 530 | lower = pdata->read(dev, reg); |
| 531 | if (lower < 0) |
| 532 | return lower; |
| 533 | |
| 534 | upper = pdata->read(dev, reg + 1); |
| 535 | } while (temp != upper && --timeout); |
| 536 | |
| 537 | if (timeout == 0) |
| 538 | return -EIO; |
| 539 | |
| 540 | return (upper << 8) | lower; |
| 541 | } |
| 542 | |
| 543 | return pdata->read(dev, reg); |
| 544 | } |
| 545 | |
| 546 | static int __devinit bq27000_battery_probe(struct platform_device *pdev) |
| 547 | { |
| 548 | struct bq27x00_device_info *di; |
| 549 | struct bq27000_platform_data *pdata = pdev->dev.platform_data; |
| 550 | int ret; |
| 551 | |
| 552 | if (!pdata) { |
| 553 | dev_err(&pdev->dev, "no platform_data supplied\n"); |
| 554 | return -EINVAL; |
| 555 | } |
| 556 | |
| 557 | if (!pdata->read) { |
| 558 | dev_err(&pdev->dev, "no hdq read callback supplied\n"); |
| 559 | return -EINVAL; |
| 560 | } |
| 561 | |
| 562 | di = kzalloc(sizeof(*di), GFP_KERNEL); |
| 563 | if (!di) { |
| 564 | dev_err(&pdev->dev, "failed to allocate device info data\n"); |
| 565 | return -ENOMEM; |
| 566 | } |
| 567 | |
| 568 | platform_set_drvdata(pdev, di); |
| 569 | |
| 570 | di->dev = &pdev->dev; |
| 571 | di->chip = BQ27000; |
| 572 | |
| 573 | di->bat.name = pdata->name ?: dev_name(&pdev->dev); |
| 574 | di->bus.read = &bq27000_read_platform; |
| 575 | |
| 576 | ret = bq27x00_powersupply_init(di); |
| 577 | if (ret) |
| 578 | goto err_free; |
| 579 | |
| 580 | return 0; |
| 581 | |
| 582 | err_free: |
| 583 | platform_set_drvdata(pdev, NULL); |
| 584 | kfree(di); |
| 585 | |
| 586 | return ret; |
| 587 | } |
| 588 | |
| 589 | static int __devexit bq27000_battery_remove(struct platform_device *pdev) |
| 590 | { |
| 591 | struct bq27x00_device_info *di = platform_get_drvdata(pdev); |
| 592 | |
| 593 | power_supply_unregister(&di->bat); |
| 594 | platform_set_drvdata(pdev, NULL); |
| 595 | kfree(di); |
| 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | static struct platform_driver bq27000_battery_driver = { |
| 601 | .probe = bq27000_battery_probe, |
| 602 | .remove = __devexit_p(bq27000_battery_remove), |
| 603 | .driver = { |
| 604 | .name = "bq27000-battery", |
| 605 | .owner = THIS_MODULE, |
| 606 | }, |
| 607 | }; |
| 608 | |
| 609 | static inline int bq27x00_battery_platform_init(void) |
| 610 | { |
| 611 | int ret = platform_driver_register(&bq27000_battery_driver); |
| 612 | if (ret) |
| 613 | printk(KERN_ERR "Unable to register BQ27000 platform driver\n"); |
| 614 | |
| 615 | return ret; |
| 616 | } |
| 617 | |
| 618 | static inline void bq27x00_battery_platform_exit(void) |
| 619 | { |
| 620 | platform_driver_unregister(&bq27000_battery_driver); |
| 621 | } |
| 622 | |
| 623 | #else |
| 624 | |
| 625 | static inline int bq27x00_battery_platform_init(void) { return 0; } |
| 626 | static inline void bq27x00_battery_platform_exit(void) {}; |
| 627 | |
| 628 | #endif |
| 629 | |
| 630 | /* |
| 631 | * Module stuff |
| 632 | */ |
| 633 | |
| 634 | static int __init bq27x00_battery_init(void) |
| 635 | { |
| 636 | int ret; |
| 637 | |
| 638 | ret = bq27x00_battery_i2c_init(); |
| 639 | if (ret) |
| 640 | return ret; |
| 641 | |
| 642 | ret = bq27x00_battery_platform_init(); |
| 643 | if (ret) |
| 644 | bq27x00_battery_i2c_exit(); |
| 645 | |
| 646 | return ret; |
| 647 | } |
| 648 | module_init(bq27x00_battery_init); |
| 649 | |
| 650 | static void __exit bq27x00_battery_exit(void) |
| 651 | { |
| 652 | bq27x00_battery_platform_exit(); |
| 653 | bq27x00_battery_i2c_exit(); |
| 654 | } |
| 655 | module_exit(bq27x00_battery_exit); |
| 656 | |
| 657 | MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); |
| 658 | MODULE_DESCRIPTION("BQ27x00 battery monitor driver"); |
| 659 | MODULE_LICENSE("GPL"); |