Merge git://git.infradead.org/battery-2.6

* git://git.infradead.org/battery-2.6: (30 commits)
bq20z75: Fix time and temp units
bq20z75: Fix issues with present and suspend
z2_battery: Fix count of properties
s3c_adc_battery: Fix method names when PM not set
z2_battery: Add MODULE_DEVICE_TABLE
ds2782_battery: Add MODULE_DEVICE_TABLE
bq20z75: Add MODULE_DEVICE_TABLE
power_supply: Update power_supply_is_watt_property
bq20z75: Add i2c retry mechanism
bq20z75: Add optional battery detect gpio
twl4030_charger: Make the driver atomic notifier safe
bq27x00: Use single i2c_transfer call for property read
bq27x00: Cleanup bq27x00_i2c_read
bq27x00: Minor cleanups
bq27x00: Give more specific reports on battery status
bq27x00: Add MODULE_DEVICE_TABLE
bq27x00: Add new properties
bq27x00: Poll battery state
bq27x00: Cache battery registers
bq27x00: Add bq27000 support
...

+1048 -265
+20
drivers/leds/led-triggers.c
··· 231 231 } 232 232 EXPORT_SYMBOL_GPL(led_trigger_event); 233 233 234 + void led_trigger_blink(struct led_trigger *trigger, 235 + unsigned long *delay_on, 236 + unsigned long *delay_off) 237 + { 238 + struct list_head *entry; 239 + 240 + if (!trigger) 241 + return; 242 + 243 + read_lock(&trigger->leddev_list_lock); 244 + list_for_each(entry, &trigger->led_cdevs) { 245 + struct led_classdev *led_cdev; 246 + 247 + led_cdev = list_entry(entry, struct led_classdev, trig_list); 248 + led_blink_set(led_cdev, delay_on, delay_off); 249 + } 250 + read_unlock(&trigger->leddev_list_lock); 251 + } 252 + EXPORT_SYMBOL_GPL(led_trigger_blink); 253 + 234 254 void led_trigger_register_simple(const char *name, struct led_trigger **tp) 235 255 { 236 256 struct led_trigger *trigger;
+14
drivers/power/Kconfig
··· 117 117 118 118 config BATTERY_BQ27x00 119 119 tristate "BQ27x00 battery driver" 120 + help 121 + Say Y here to enable support for batteries with BQ27x00 (I2C/HDQ) chips. 122 + 123 + config BATTERY_BQ27X00_I2C 124 + bool "BQ27200/BQ27500 support" 125 + depends on BATTERY_BQ27x00 120 126 depends on I2C 127 + default y 121 128 help 122 129 Say Y here to enable support for batteries with BQ27x00 (I2C) chips. 130 + 131 + config BATTERY_BQ27X00_PLATFORM 132 + bool "BQ27000 support" 133 + depends on BATTERY_BQ27x00 134 + default y 135 + help 136 + Say Y here to enable support for batteries with BQ27000 (HDQ) chips. 123 137 124 138 config BATTERY_DA9030 125 139 tristate "DA9030 battery driver"
+263 -47
drivers/power/bq20z75.c
··· 25 25 #include <linux/power_supply.h> 26 26 #include <linux/i2c.h> 27 27 #include <linux/slab.h> 28 + #include <linux/interrupt.h> 29 + #include <linux/gpio.h> 30 + 31 + #include <linux/power/bq20z75.h> 28 32 29 33 enum { 30 34 REG_MANUFACTURER_DATA, ··· 42 38 REG_CYCLE_COUNT, 43 39 REG_SERIAL_NUMBER, 44 40 REG_REMAINING_CAPACITY, 41 + REG_REMAINING_CAPACITY_CHARGE, 45 42 REG_FULL_CHARGE_CAPACITY, 43 + REG_FULL_CHARGE_CAPACITY_CHARGE, 46 44 REG_DESIGN_CAPACITY, 45 + REG_DESIGN_CAPACITY_CHARGE, 47 46 REG_DESIGN_VOLTAGE, 47 + }; 48 + 49 + /* Battery Mode defines */ 50 + #define BATTERY_MODE_OFFSET 0x03 51 + #define BATTERY_MODE_MASK 0x8000 52 + enum bq20z75_battery_mode { 53 + BATTERY_MODE_AMPS, 54 + BATTERY_MODE_WATTS 48 55 }; 49 56 50 57 /* manufacturer access defines */ ··· 93 78 BQ20Z75_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0E, 0, 100), 94 79 [REG_REMAINING_CAPACITY] = 95 80 BQ20Z75_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535), 81 + [REG_REMAINING_CAPACITY_CHARGE] = 82 + BQ20Z75_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535), 96 83 [REG_FULL_CHARGE_CAPACITY] = 97 84 BQ20Z75_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535), 85 + [REG_FULL_CHARGE_CAPACITY_CHARGE] = 86 + BQ20Z75_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535), 98 87 [REG_TIME_TO_EMPTY] = 99 88 BQ20Z75_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 100 89 65535), ··· 111 92 BQ20Z75_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535), 112 93 [REG_DESIGN_CAPACITY] = 113 94 BQ20Z75_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 95 + 65535), 96 + [REG_DESIGN_CAPACITY_CHARGE] = 97 + BQ20Z75_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 114 98 65535), 115 99 [REG_DESIGN_VOLTAGE] = 116 100 BQ20Z75_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, ··· 139 117 POWER_SUPPLY_PROP_ENERGY_NOW, 140 118 POWER_SUPPLY_PROP_ENERGY_FULL, 141 119 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 120 + POWER_SUPPLY_PROP_CHARGE_NOW, 121 + POWER_SUPPLY_PROP_CHARGE_FULL, 122 + POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 142 123 }; 143 124 144 125 struct bq20z75_info { 145 - struct i2c_client *client; 146 - struct power_supply power_supply; 126 + struct i2c_client *client; 127 + struct power_supply power_supply; 128 + struct bq20z75_platform_data *pdata; 129 + bool is_present; 130 + bool gpio_detect; 131 + bool enable_detection; 132 + int irq; 147 133 }; 148 134 149 135 static int bq20z75_read_word_data(struct i2c_client *client, u8 address) 150 136 { 151 - s32 ret; 137 + struct bq20z75_info *bq20z75_device = i2c_get_clientdata(client); 138 + s32 ret = 0; 139 + int retries = 1; 152 140 153 - ret = i2c_smbus_read_word_data(client, address); 141 + if (bq20z75_device->pdata) 142 + retries = max(bq20z75_device->pdata->i2c_retry_count + 1, 1); 143 + 144 + while (retries > 0) { 145 + ret = i2c_smbus_read_word_data(client, address); 146 + if (ret >= 0) 147 + break; 148 + retries--; 149 + } 150 + 154 151 if (ret < 0) { 155 - dev_err(&client->dev, 152 + dev_dbg(&client->dev, 156 153 "%s: i2c read at address 0x%x failed\n", 157 154 __func__, address); 158 155 return ret; 159 156 } 157 + 160 158 return le16_to_cpu(ret); 161 159 } 162 160 163 161 static int bq20z75_write_word_data(struct i2c_client *client, u8 address, 164 162 u16 value) 165 163 { 166 - s32 ret; 164 + struct bq20z75_info *bq20z75_device = i2c_get_clientdata(client); 165 + s32 ret = 0; 166 + int retries = 1; 167 167 168 - ret = i2c_smbus_write_word_data(client, address, le16_to_cpu(value)); 168 + if (bq20z75_device->pdata) 169 + retries = max(bq20z75_device->pdata->i2c_retry_count + 1, 1); 170 + 171 + while (retries > 0) { 172 + ret = i2c_smbus_write_word_data(client, address, 173 + le16_to_cpu(value)); 174 + if (ret >= 0) 175 + break; 176 + retries--; 177 + } 178 + 169 179 if (ret < 0) { 170 - dev_err(&client->dev, 180 + dev_dbg(&client->dev, 171 181 "%s: i2c write to address 0x%x failed\n", 172 182 __func__, address); 173 183 return ret; 174 184 } 185 + 175 186 return 0; 176 187 } 177 188 ··· 213 158 union power_supply_propval *val) 214 159 { 215 160 s32 ret; 161 + struct bq20z75_info *bq20z75_device = i2c_get_clientdata(client); 162 + 163 + if (psp == POWER_SUPPLY_PROP_PRESENT && 164 + bq20z75_device->gpio_detect) { 165 + ret = gpio_get_value( 166 + bq20z75_device->pdata->battery_detect); 167 + if (ret == bq20z75_device->pdata->battery_detect_present) 168 + val->intval = 1; 169 + else 170 + val->intval = 0; 171 + bq20z75_device->is_present = val->intval; 172 + return ret; 173 + } 216 174 217 175 /* Write to ManufacturerAccess with 218 176 * ManufacturerAccess command and then ··· 233 165 ret = bq20z75_write_word_data(client, 234 166 bq20z75_data[REG_MANUFACTURER_DATA].addr, 235 167 MANUFACTURER_ACCESS_STATUS); 236 - if (ret < 0) 168 + if (ret < 0) { 169 + if (psp == POWER_SUPPLY_PROP_PRESENT) 170 + val->intval = 0; /* battery removed */ 237 171 return ret; 238 - 172 + } 239 173 240 174 ret = bq20z75_read_word_data(client, 241 175 bq20z75_data[REG_MANUFACTURER_DATA].addr); ··· 318 248 { 319 249 #define BASE_UNIT_CONVERSION 1000 320 250 #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION) 321 - #define TIME_UNIT_CONVERSION 600 322 - #define TEMP_KELVIN_TO_CELCIUS 2731 251 + #define TIME_UNIT_CONVERSION 60 252 + #define TEMP_KELVIN_TO_CELSIUS 2731 323 253 switch (psp) { 324 254 case POWER_SUPPLY_PROP_ENERGY_NOW: 325 255 case POWER_SUPPLY_PROP_ENERGY_FULL: 326 256 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: 257 + /* bq20z75 provides energy in units of 10mWh. 258 + * Convert to µWh 259 + */ 327 260 val->intval *= BATTERY_MODE_CAP_MULT_WATT; 328 261 break; 329 262 330 263 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 331 264 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: 332 265 case POWER_SUPPLY_PROP_CURRENT_NOW: 266 + case POWER_SUPPLY_PROP_CHARGE_NOW: 267 + case POWER_SUPPLY_PROP_CHARGE_FULL: 268 + case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 333 269 val->intval *= BASE_UNIT_CONVERSION; 334 270 break; 335 271 336 272 case POWER_SUPPLY_PROP_TEMP: 337 - /* bq20z75 provides battery tempreture in 0.1°K 338 - * so convert it to 0.1°C */ 339 - val->intval -= TEMP_KELVIN_TO_CELCIUS; 340 - val->intval *= 10; 273 + /* bq20z75 provides battery temperature in 0.1K 274 + * so convert it to 0.1°C 275 + */ 276 + val->intval -= TEMP_KELVIN_TO_CELSIUS; 341 277 break; 342 278 343 279 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: 344 280 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: 281 + /* bq20z75 provides time to empty and time to full in minutes. 282 + * Convert to seconds 283 + */ 345 284 val->intval *= TIME_UNIT_CONVERSION; 346 285 break; 347 286 ··· 360 281 } 361 282 } 362 283 284 + static enum bq20z75_battery_mode 285 + bq20z75_set_battery_mode(struct i2c_client *client, 286 + enum bq20z75_battery_mode mode) 287 + { 288 + int ret, original_val; 289 + 290 + original_val = bq20z75_read_word_data(client, BATTERY_MODE_OFFSET); 291 + if (original_val < 0) 292 + return original_val; 293 + 294 + if ((original_val & BATTERY_MODE_MASK) == mode) 295 + return mode; 296 + 297 + if (mode == BATTERY_MODE_AMPS) 298 + ret = original_val & ~BATTERY_MODE_MASK; 299 + else 300 + ret = original_val | BATTERY_MODE_MASK; 301 + 302 + ret = bq20z75_write_word_data(client, BATTERY_MODE_OFFSET, ret); 303 + if (ret < 0) 304 + return ret; 305 + 306 + return original_val & BATTERY_MODE_MASK; 307 + } 308 + 363 309 static int bq20z75_get_battery_capacity(struct i2c_client *client, 364 310 int reg_offset, enum power_supply_property psp, 365 311 union power_supply_propval *val) 366 312 { 367 313 s32 ret; 314 + enum bq20z75_battery_mode mode = BATTERY_MODE_WATTS; 315 + 316 + if (power_supply_is_amp_property(psp)) 317 + mode = BATTERY_MODE_AMPS; 318 + 319 + mode = bq20z75_set_battery_mode(client, mode); 320 + if (mode < 0) 321 + return mode; 368 322 369 323 ret = bq20z75_read_word_data(client, bq20z75_data[reg_offset].addr); 370 324 if (ret < 0) ··· 409 297 val->intval = min(ret, 100); 410 298 } else 411 299 val->intval = ret; 300 + 301 + ret = bq20z75_set_battery_mode(client, mode); 302 + if (ret < 0) 303 + return ret; 412 304 413 305 return 0; 414 306 } ··· 434 318 return 0; 435 319 } 436 320 321 + static int bq20z75_get_property_index(struct i2c_client *client, 322 + enum power_supply_property psp) 323 + { 324 + int count; 325 + for (count = 0; count < ARRAY_SIZE(bq20z75_data); count++) 326 + if (psp == bq20z75_data[count].psp) 327 + return count; 328 + 329 + dev_warn(&client->dev, 330 + "%s: Invalid Property - %d\n", __func__, psp); 331 + 332 + return -EINVAL; 333 + } 334 + 437 335 static int bq20z75_get_property(struct power_supply *psy, 438 336 enum power_supply_property psp, 439 337 union power_supply_propval *val) 440 338 { 441 - int count; 442 - int ret; 339 + int ret = 0; 443 340 struct bq20z75_info *bq20z75_device = container_of(psy, 444 341 struct bq20z75_info, power_supply); 445 342 struct i2c_client *client = bq20z75_device->client; ··· 461 332 case POWER_SUPPLY_PROP_PRESENT: 462 333 case POWER_SUPPLY_PROP_HEALTH: 463 334 ret = bq20z75_get_battery_presence_and_health(client, psp, val); 464 - if (ret) 465 - return ret; 335 + if (psp == POWER_SUPPLY_PROP_PRESENT) 336 + return 0; 466 337 break; 467 338 468 339 case POWER_SUPPLY_PROP_TECHNOLOGY: ··· 472 343 case POWER_SUPPLY_PROP_ENERGY_NOW: 473 344 case POWER_SUPPLY_PROP_ENERGY_FULL: 474 345 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: 346 + case POWER_SUPPLY_PROP_CHARGE_NOW: 347 + case POWER_SUPPLY_PROP_CHARGE_FULL: 348 + case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 475 349 case POWER_SUPPLY_PROP_CAPACITY: 476 - for (count = 0; count < ARRAY_SIZE(bq20z75_data); count++) { 477 - if (psp == bq20z75_data[count].psp) 478 - break; 479 - } 350 + ret = bq20z75_get_property_index(client, psp); 351 + if (ret < 0) 352 + break; 480 353 481 - ret = bq20z75_get_battery_capacity(client, count, psp, val); 482 - if (ret) 483 - return ret; 484 - 354 + ret = bq20z75_get_battery_capacity(client, ret, psp, val); 485 355 break; 486 356 487 357 case POWER_SUPPLY_PROP_SERIAL_NUMBER: 488 358 ret = bq20z75_get_battery_serial_number(client, val); 489 - if (ret) 490 - return ret; 491 359 break; 492 360 493 361 case POWER_SUPPLY_PROP_STATUS: ··· 495 369 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: 496 370 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: 497 371 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: 498 - for (count = 0; count < ARRAY_SIZE(bq20z75_data); count++) { 499 - if (psp == bq20z75_data[count].psp) 500 - break; 501 - } 372 + ret = bq20z75_get_property_index(client, psp); 373 + if (ret < 0) 374 + break; 502 375 503 - ret = bq20z75_get_battery_property(client, count, psp, val); 504 - if (ret) 505 - return ret; 506 - 376 + ret = bq20z75_get_battery_property(client, ret, psp, val); 507 377 break; 508 378 509 379 default: ··· 508 386 return -EINVAL; 509 387 } 510 388 511 - /* Convert units to match requirements for power supply class */ 512 - bq20z75_unit_adjustment(client, psp, val); 389 + if (!bq20z75_device->enable_detection) 390 + goto done; 391 + 392 + if (!bq20z75_device->gpio_detect && 393 + bq20z75_device->is_present != (ret >= 0)) { 394 + bq20z75_device->is_present = (ret >= 0); 395 + power_supply_changed(&bq20z75_device->power_supply); 396 + } 397 + 398 + done: 399 + if (!ret) { 400 + /* Convert units to match requirements for power supply class */ 401 + bq20z75_unit_adjustment(client, psp, val); 402 + } 513 403 514 404 dev_dbg(&client->dev, 515 - "%s: property = %d, value = %d\n", __func__, psp, val->intval); 405 + "%s: property = %d, value = %x\n", __func__, psp, val->intval); 406 + 407 + if (ret && bq20z75_device->is_present) 408 + return ret; 409 + 410 + /* battery not present, so return NODATA for properties */ 411 + if (ret) 412 + return -ENODATA; 516 413 517 414 return 0; 518 415 } 519 416 520 - static int bq20z75_probe(struct i2c_client *client, 417 + static irqreturn_t bq20z75_irq(int irq, void *devid) 418 + { 419 + struct power_supply *battery = devid; 420 + 421 + power_supply_changed(battery); 422 + 423 + return IRQ_HANDLED; 424 + } 425 + 426 + static int __devinit bq20z75_probe(struct i2c_client *client, 521 427 const struct i2c_device_id *id) 522 428 { 523 429 struct bq20z75_info *bq20z75_device; 430 + struct bq20z75_platform_data *pdata = client->dev.platform_data; 524 431 int rc; 432 + int irq; 525 433 526 434 bq20z75_device = kzalloc(sizeof(struct bq20z75_info), GFP_KERNEL); 527 435 if (!bq20z75_device) 528 436 return -ENOMEM; 529 437 530 438 bq20z75_device->client = client; 439 + bq20z75_device->enable_detection = false; 440 + bq20z75_device->gpio_detect = false; 531 441 bq20z75_device->power_supply.name = "battery"; 532 442 bq20z75_device->power_supply.type = POWER_SUPPLY_TYPE_BATTERY; 533 443 bq20z75_device->power_supply.properties = bq20z75_properties; ··· 567 413 ARRAY_SIZE(bq20z75_properties); 568 414 bq20z75_device->power_supply.get_property = bq20z75_get_property; 569 415 416 + if (pdata) { 417 + bq20z75_device->gpio_detect = 418 + gpio_is_valid(pdata->battery_detect); 419 + bq20z75_device->pdata = pdata; 420 + } 421 + 570 422 i2c_set_clientdata(client, bq20z75_device); 423 + 424 + if (!bq20z75_device->gpio_detect) 425 + goto skip_gpio; 426 + 427 + rc = gpio_request(pdata->battery_detect, dev_name(&client->dev)); 428 + if (rc) { 429 + dev_warn(&client->dev, "Failed to request gpio: %d\n", rc); 430 + bq20z75_device->gpio_detect = false; 431 + goto skip_gpio; 432 + } 433 + 434 + rc = gpio_direction_input(pdata->battery_detect); 435 + if (rc) { 436 + dev_warn(&client->dev, "Failed to get gpio as input: %d\n", rc); 437 + gpio_free(pdata->battery_detect); 438 + bq20z75_device->gpio_detect = false; 439 + goto skip_gpio; 440 + } 441 + 442 + irq = gpio_to_irq(pdata->battery_detect); 443 + if (irq <= 0) { 444 + dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq); 445 + gpio_free(pdata->battery_detect); 446 + bq20z75_device->gpio_detect = false; 447 + goto skip_gpio; 448 + } 449 + 450 + rc = request_irq(irq, bq20z75_irq, 451 + IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, 452 + dev_name(&client->dev), &bq20z75_device->power_supply); 453 + if (rc) { 454 + dev_warn(&client->dev, "Failed to request irq: %d\n", rc); 455 + gpio_free(pdata->battery_detect); 456 + bq20z75_device->gpio_detect = false; 457 + goto skip_gpio; 458 + } 459 + 460 + bq20z75_device->irq = irq; 461 + 462 + skip_gpio: 571 463 572 464 rc = power_supply_register(&client->dev, &bq20z75_device->power_supply); 573 465 if (rc) { 574 466 dev_err(&client->dev, 575 467 "%s: Failed to register power supply\n", __func__); 576 - kfree(bq20z75_device); 577 - return rc; 468 + goto exit_psupply; 578 469 } 579 470 580 471 dev_info(&client->dev, 581 472 "%s: battery gas gauge device registered\n", client->name); 582 473 583 474 return 0; 475 + 476 + exit_psupply: 477 + if (bq20z75_device->irq) 478 + free_irq(bq20z75_device->irq, &bq20z75_device->power_supply); 479 + if (bq20z75_device->gpio_detect) 480 + gpio_free(pdata->battery_detect); 481 + 482 + kfree(bq20z75_device); 483 + 484 + return rc; 584 485 } 585 486 586 - static int bq20z75_remove(struct i2c_client *client) 487 + static int __devexit bq20z75_remove(struct i2c_client *client) 587 488 { 588 489 struct bq20z75_info *bq20z75_device = i2c_get_clientdata(client); 490 + 491 + if (bq20z75_device->irq) 492 + free_irq(bq20z75_device->irq, &bq20z75_device->power_supply); 493 + if (bq20z75_device->gpio_detect) 494 + gpio_free(bq20z75_device->pdata->battery_detect); 589 495 590 496 power_supply_unregister(&bq20z75_device->power_supply); 591 497 kfree(bq20z75_device); ··· 658 444 static int bq20z75_suspend(struct i2c_client *client, 659 445 pm_message_t state) 660 446 { 447 + struct bq20z75_info *bq20z75_device = i2c_get_clientdata(client); 661 448 s32 ret; 662 449 663 450 /* write to manufacturer access with sleep command */ 664 451 ret = bq20z75_write_word_data(client, 665 452 bq20z75_data[REG_MANUFACTURER_DATA].addr, 666 453 MANUFACTURER_ACCESS_SLEEP); 667 - if (ret < 0) 454 + if (bq20z75_device->is_present && ret < 0) 668 455 return ret; 669 456 670 457 return 0; ··· 680 465 { "bq20z75", 0 }, 681 466 {} 682 467 }; 468 + MODULE_DEVICE_TABLE(i2c, bq20z75_id); 683 469 684 470 static struct i2c_driver bq20z75_battery_driver = { 685 471 .probe = bq20z75_probe, 686 - .remove = bq20z75_remove, 472 + .remove = __devexit_p(bq20z75_remove), 687 473 .suspend = bq20z75_suspend, 688 474 .resume = bq20z75_resume, 689 475 .id_table = bq20z75_id,
+596 -207
drivers/power/bq27x00_battery.c
··· 3 3 * 4 4 * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it> 5 5 * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it> 6 + * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de> 6 7 * 7 8 * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc. 8 9 * ··· 16 15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 17 16 * 18 17 */ 18 + 19 + /* 20 + * Datasheets: 21 + * http://focus.ti.com/docs/prod/folders/print/bq27000.html 22 + * http://focus.ti.com/docs/prod/folders/print/bq27500.html 23 + */ 24 + 19 25 #include <linux/module.h> 20 26 #include <linux/param.h> 21 27 #include <linux/jiffies.h> ··· 35 27 #include <linux/slab.h> 36 28 #include <asm/unaligned.h> 37 29 38 - #define DRIVER_VERSION "1.1.0" 30 + #include <linux/power/bq27x00_battery.h> 31 + 32 + #define DRIVER_VERSION "1.2.0" 39 33 40 34 #define BQ27x00_REG_TEMP 0x06 41 35 #define BQ27x00_REG_VOLT 0x08 ··· 46 36 #define BQ27x00_REG_TTE 0x16 47 37 #define BQ27x00_REG_TTF 0x18 48 38 #define BQ27x00_REG_TTECP 0x26 39 + #define BQ27x00_REG_NAC 0x0C /* Nominal available capaciy */ 40 + #define BQ27x00_REG_LMD 0x12 /* Last measured discharge */ 41 + #define BQ27x00_REG_CYCT 0x2A /* Cycle count total */ 42 + #define BQ27x00_REG_AE 0x22 /* Available enery */ 49 43 50 44 #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */ 45 + #define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */ 51 46 #define BQ27000_FLAG_CHGS BIT(7) 47 + #define BQ27000_FLAG_FC BIT(5) 52 48 53 - #define BQ27500_REG_SOC 0x2c 49 + #define BQ27500_REG_SOC 0x2C 50 + #define BQ27500_REG_DCAP 0x3C /* Design capacity */ 54 51 #define BQ27500_FLAG_DSC BIT(0) 55 52 #define BQ27500_FLAG_FC BIT(9) 56 53 57 - /* If the system has several batteries we need a different name for each 58 - * of them... 59 - */ 60 - static DEFINE_IDR(battery_id); 61 - static DEFINE_MUTEX(battery_mutex); 54 + #define BQ27000_RS 20 /* Resistor sense */ 62 55 63 56 struct bq27x00_device_info; 64 57 struct bq27x00_access_methods { 65 - int (*read)(u8 reg, int *rt_value, int b_single, 66 - struct bq27x00_device_info *di); 58 + int (*read)(struct bq27x00_device_info *di, u8 reg, bool single); 67 59 }; 68 60 69 61 enum bq27x00_chip { BQ27000, BQ27500 }; 70 62 63 + struct bq27x00_reg_cache { 64 + int temperature; 65 + int time_to_empty; 66 + int time_to_empty_avg; 67 + int time_to_full; 68 + int charge_full; 69 + int charge_counter; 70 + int capacity; 71 + int flags; 72 + 73 + int current_now; 74 + }; 75 + 71 76 struct bq27x00_device_info { 72 77 struct device *dev; 73 78 int id; 74 - struct bq27x00_access_methods *bus; 75 - struct power_supply bat; 76 79 enum bq27x00_chip chip; 77 80 78 - struct i2c_client *client; 81 + struct bq27x00_reg_cache cache; 82 + int charge_design_full; 83 + 84 + unsigned long last_update; 85 + struct delayed_work work; 86 + 87 + struct power_supply bat; 88 + 89 + struct bq27x00_access_methods bus; 90 + 91 + struct mutex lock; 79 92 }; 80 93 81 94 static enum power_supply_property bq27x00_battery_props[] = { ··· 111 78 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 112 79 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 113 80 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, 81 + POWER_SUPPLY_PROP_TECHNOLOGY, 82 + POWER_SUPPLY_PROP_CHARGE_FULL, 83 + POWER_SUPPLY_PROP_CHARGE_NOW, 84 + POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 85 + POWER_SUPPLY_PROP_CHARGE_COUNTER, 86 + POWER_SUPPLY_PROP_ENERGY_NOW, 114 87 }; 88 + 89 + static unsigned int poll_interval = 360; 90 + module_param(poll_interval, uint, 0644); 91 + MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \ 92 + "0 disables polling"); 115 93 116 94 /* 117 95 * Common code for BQ27x00 devices 118 96 */ 119 97 120 - static int bq27x00_read(u8 reg, int *rt_value, int b_single, 121 - struct bq27x00_device_info *di) 98 + static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg, 99 + bool single) 122 100 { 123 - return di->bus->read(reg, rt_value, b_single, di); 124 - } 125 - 126 - /* 127 - * Return the battery temperature in tenths of degree Celsius 128 - * Or < 0 if something fails. 129 - */ 130 - static int bq27x00_battery_temperature(struct bq27x00_device_info *di) 131 - { 132 - int ret; 133 - int temp = 0; 134 - 135 - ret = bq27x00_read(BQ27x00_REG_TEMP, &temp, 0, di); 136 - if (ret) { 137 - dev_err(di->dev, "error reading temperature\n"); 138 - return ret; 139 - } 140 - 141 - if (di->chip == BQ27500) 142 - return temp - 2731; 143 - else 144 - return ((temp >> 2) - 273) * 10; 145 - } 146 - 147 - /* 148 - * Return the battery Voltage in milivolts 149 - * Or < 0 if something fails. 150 - */ 151 - static int bq27x00_battery_voltage(struct bq27x00_device_info *di) 152 - { 153 - int ret; 154 - int volt = 0; 155 - 156 - ret = bq27x00_read(BQ27x00_REG_VOLT, &volt, 0, di); 157 - if (ret) { 158 - dev_err(di->dev, "error reading voltage\n"); 159 - return ret; 160 - } 161 - 162 - return volt * 1000; 163 - } 164 - 165 - /* 166 - * Return the battery average current 167 - * Note that current can be negative signed as well 168 - * Or 0 if something fails. 169 - */ 170 - static int bq27x00_battery_current(struct bq27x00_device_info *di) 171 - { 172 - int ret; 173 - int curr = 0; 174 - int flags = 0; 175 - 176 - ret = bq27x00_read(BQ27x00_REG_AI, &curr, 0, di); 177 - if (ret) { 178 - dev_err(di->dev, "error reading current\n"); 179 - return 0; 180 - } 181 - 182 - if (di->chip == BQ27500) { 183 - /* bq27500 returns signed value */ 184 - curr = (int)(s16)curr; 185 - } else { 186 - ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di); 187 - if (ret < 0) { 188 - dev_err(di->dev, "error reading flags\n"); 189 - return 0; 190 - } 191 - if (flags & BQ27000_FLAG_CHGS) { 192 - dev_dbg(di->dev, "negative current!\n"); 193 - curr = -curr; 194 - } 195 - } 196 - 197 - return curr * 1000; 101 + return di->bus.read(di, reg, single); 198 102 } 199 103 200 104 /* 201 105 * Return the battery Relative State-of-Charge 202 106 * Or < 0 if something fails. 203 107 */ 204 - static int bq27x00_battery_rsoc(struct bq27x00_device_info *di) 108 + static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di) 205 109 { 206 - int ret; 207 - int rsoc = 0; 110 + int rsoc; 208 111 209 112 if (di->chip == BQ27500) 210 - ret = bq27x00_read(BQ27500_REG_SOC, &rsoc, 0, di); 113 + rsoc = bq27x00_read(di, BQ27500_REG_SOC, false); 211 114 else 212 - ret = bq27x00_read(BQ27000_REG_RSOC, &rsoc, 1, di); 213 - if (ret) { 115 + rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true); 116 + 117 + if (rsoc < 0) 214 118 dev_err(di->dev, "error reading relative State-of-Charge\n"); 215 - return ret; 216 - } 217 119 218 120 return rsoc; 219 121 } 220 122 221 - static int bq27x00_battery_status(struct bq27x00_device_info *di, 222 - union power_supply_propval *val) 123 + /* 124 + * Return a battery charge value in µAh 125 + * Or < 0 if something fails. 126 + */ 127 + static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg) 223 128 { 224 - int flags = 0; 225 - int status; 226 - int ret; 129 + int charge; 227 130 228 - ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di); 229 - if (ret < 0) { 230 - dev_err(di->dev, "error reading flags\n"); 231 - return ret; 131 + charge = bq27x00_read(di, reg, false); 132 + if (charge < 0) { 133 + dev_err(di->dev, "error reading nominal available capacity\n"); 134 + return charge; 232 135 } 233 136 234 - if (di->chip == BQ27500) { 235 - if (flags & BQ27500_FLAG_FC) 236 - status = POWER_SUPPLY_STATUS_FULL; 237 - else if (flags & BQ27500_FLAG_DSC) 238 - status = POWER_SUPPLY_STATUS_DISCHARGING; 239 - else 240 - status = POWER_SUPPLY_STATUS_CHARGING; 241 - } else { 242 - if (flags & BQ27000_FLAG_CHGS) 243 - status = POWER_SUPPLY_STATUS_CHARGING; 244 - else 245 - status = POWER_SUPPLY_STATUS_DISCHARGING; 137 + if (di->chip == BQ27500) 138 + charge *= 1000; 139 + else 140 + charge = charge * 3570 / BQ27000_RS; 141 + 142 + return charge; 143 + } 144 + 145 + /* 146 + * Return the battery Nominal available capaciy in µAh 147 + * Or < 0 if something fails. 148 + */ 149 + static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di) 150 + { 151 + return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC); 152 + } 153 + 154 + /* 155 + * Return the battery Last measured discharge in µAh 156 + * Or < 0 if something fails. 157 + */ 158 + static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di) 159 + { 160 + return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD); 161 + } 162 + 163 + /* 164 + * Return the battery Initial last measured discharge in µAh 165 + * Or < 0 if something fails. 166 + */ 167 + static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di) 168 + { 169 + int ilmd; 170 + 171 + if (di->chip == BQ27500) 172 + ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false); 173 + else 174 + ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true); 175 + 176 + if (ilmd < 0) { 177 + dev_err(di->dev, "error reading initial last measured discharge\n"); 178 + return ilmd; 246 179 } 247 180 248 - val->intval = status; 249 - return 0; 181 + if (di->chip == BQ27500) 182 + ilmd *= 1000; 183 + else 184 + ilmd = ilmd * 256 * 3570 / BQ27000_RS; 185 + 186 + return ilmd; 187 + } 188 + 189 + /* 190 + * Return the battery Cycle count total 191 + * Or < 0 if something fails. 192 + */ 193 + static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di) 194 + { 195 + int cyct; 196 + 197 + cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false); 198 + if (cyct < 0) 199 + dev_err(di->dev, "error reading cycle count total\n"); 200 + 201 + return cyct; 250 202 } 251 203 252 204 /* 253 205 * Read a time register. 254 206 * Return < 0 if something fails. 255 207 */ 256 - static int bq27x00_battery_time(struct bq27x00_device_info *di, int reg, 257 - union power_supply_propval *val) 208 + static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg) 258 209 { 259 - int tval = 0; 260 - int ret; 210 + int tval; 261 211 262 - ret = bq27x00_read(reg, &tval, 0, di); 263 - if (ret) { 264 - dev_err(di->dev, "error reading register %02x\n", reg); 265 - return ret; 212 + tval = bq27x00_read(di, reg, false); 213 + if (tval < 0) { 214 + dev_err(di->dev, "error reading register %02x: %d\n", reg, tval); 215 + return tval; 266 216 } 267 217 268 218 if (tval == 65535) 269 219 return -ENODATA; 270 220 271 - val->intval = tval * 60; 221 + return tval * 60; 222 + } 223 + 224 + static void bq27x00_update(struct bq27x00_device_info *di) 225 + { 226 + struct bq27x00_reg_cache cache = {0, }; 227 + bool is_bq27500 = di->chip == BQ27500; 228 + 229 + cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, is_bq27500); 230 + if (cache.flags >= 0) { 231 + cache.capacity = bq27x00_battery_read_rsoc(di); 232 + cache.temperature = bq27x00_read(di, BQ27x00_REG_TEMP, false); 233 + cache.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE); 234 + cache.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP); 235 + cache.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF); 236 + cache.charge_full = bq27x00_battery_read_lmd(di); 237 + cache.charge_counter = bq27x00_battery_read_cyct(di); 238 + 239 + if (!is_bq27500) 240 + cache.current_now = bq27x00_read(di, BQ27x00_REG_AI, false); 241 + 242 + /* We only have to read charge design full once */ 243 + if (di->charge_design_full <= 0) 244 + di->charge_design_full = bq27x00_battery_read_ilmd(di); 245 + } 246 + 247 + /* Ignore current_now which is a snapshot of the current battery state 248 + * and is likely to be different even between two consecutive reads */ 249 + if (memcmp(&di->cache, &cache, sizeof(cache) - sizeof(int)) != 0) { 250 + di->cache = cache; 251 + power_supply_changed(&di->bat); 252 + } 253 + 254 + di->last_update = jiffies; 255 + } 256 + 257 + static void bq27x00_battery_poll(struct work_struct *work) 258 + { 259 + struct bq27x00_device_info *di = 260 + container_of(work, struct bq27x00_device_info, work.work); 261 + 262 + bq27x00_update(di); 263 + 264 + if (poll_interval > 0) { 265 + /* The timer does not have to be accurate. */ 266 + set_timer_slack(&di->work.timer, poll_interval * HZ / 4); 267 + schedule_delayed_work(&di->work, poll_interval * HZ); 268 + } 269 + } 270 + 271 + 272 + /* 273 + * Return the battery temperature in tenths of degree Celsius 274 + * Or < 0 if something fails. 275 + */ 276 + static int bq27x00_battery_temperature(struct bq27x00_device_info *di, 277 + union power_supply_propval *val) 278 + { 279 + if (di->cache.temperature < 0) 280 + return di->cache.temperature; 281 + 282 + if (di->chip == BQ27500) 283 + val->intval = di->cache.temperature - 2731; 284 + else 285 + val->intval = ((di->cache.temperature * 5) - 5463) / 2; 286 + 287 + return 0; 288 + } 289 + 290 + /* 291 + * Return the battery average current in µA 292 + * Note that current can be negative signed as well 293 + * Or 0 if something fails. 294 + */ 295 + static int bq27x00_battery_current(struct bq27x00_device_info *di, 296 + union power_supply_propval *val) 297 + { 298 + int curr; 299 + 300 + if (di->chip == BQ27500) 301 + curr = bq27x00_read(di, BQ27x00_REG_AI, false); 302 + else 303 + curr = di->cache.current_now; 304 + 305 + if (curr < 0) 306 + return curr; 307 + 308 + if (di->chip == BQ27500) { 309 + /* bq27500 returns signed value */ 310 + val->intval = (int)((s16)curr) * 1000; 311 + } else { 312 + if (di->cache.flags & BQ27000_FLAG_CHGS) { 313 + dev_dbg(di->dev, "negative current!\n"); 314 + curr = -curr; 315 + } 316 + 317 + val->intval = curr * 3570 / BQ27000_RS; 318 + } 319 + 320 + return 0; 321 + } 322 + 323 + static int bq27x00_battery_status(struct bq27x00_device_info *di, 324 + union power_supply_propval *val) 325 + { 326 + int status; 327 + 328 + if (di->chip == BQ27500) { 329 + if (di->cache.flags & BQ27500_FLAG_FC) 330 + status = POWER_SUPPLY_STATUS_FULL; 331 + else if (di->cache.flags & BQ27500_FLAG_DSC) 332 + status = POWER_SUPPLY_STATUS_DISCHARGING; 333 + else 334 + status = POWER_SUPPLY_STATUS_CHARGING; 335 + } else { 336 + if (di->cache.flags & BQ27000_FLAG_FC) 337 + status = POWER_SUPPLY_STATUS_FULL; 338 + else if (di->cache.flags & BQ27000_FLAG_CHGS) 339 + status = POWER_SUPPLY_STATUS_CHARGING; 340 + else if (power_supply_am_i_supplied(&di->bat)) 341 + status = POWER_SUPPLY_STATUS_NOT_CHARGING; 342 + else 343 + status = POWER_SUPPLY_STATUS_DISCHARGING; 344 + } 345 + 346 + val->intval = status; 347 + 348 + return 0; 349 + } 350 + 351 + /* 352 + * Return the battery Voltage in milivolts 353 + * Or < 0 if something fails. 354 + */ 355 + static int bq27x00_battery_voltage(struct bq27x00_device_info *di, 356 + union power_supply_propval *val) 357 + { 358 + int volt; 359 + 360 + volt = bq27x00_read(di, BQ27x00_REG_VOLT, false); 361 + if (volt < 0) 362 + return volt; 363 + 364 + val->intval = volt * 1000; 365 + 366 + return 0; 367 + } 368 + 369 + /* 370 + * Return the battery Available energy in µWh 371 + * Or < 0 if something fails. 372 + */ 373 + static int bq27x00_battery_energy(struct bq27x00_device_info *di, 374 + union power_supply_propval *val) 375 + { 376 + int ae; 377 + 378 + ae = bq27x00_read(di, BQ27x00_REG_AE, false); 379 + if (ae < 0) { 380 + dev_err(di->dev, "error reading available energy\n"); 381 + return ae; 382 + } 383 + 384 + if (di->chip == BQ27500) 385 + ae *= 1000; 386 + else 387 + ae = ae * 29200 / BQ27000_RS; 388 + 389 + val->intval = ae; 390 + 391 + return 0; 392 + } 393 + 394 + 395 + static int bq27x00_simple_value(int value, 396 + union power_supply_propval *val) 397 + { 398 + if (value < 0) 399 + return value; 400 + 401 + val->intval = value; 402 + 272 403 return 0; 273 404 } 274 405 ··· 446 249 int ret = 0; 447 250 struct bq27x00_device_info *di = to_bq27x00_device_info(psy); 448 251 252 + mutex_lock(&di->lock); 253 + if (time_is_before_jiffies(di->last_update + 5 * HZ)) { 254 + cancel_delayed_work_sync(&di->work); 255 + bq27x00_battery_poll(&di->work.work); 256 + } 257 + mutex_unlock(&di->lock); 258 + 259 + if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0) 260 + return -ENODEV; 261 + 449 262 switch (psp) { 450 263 case POWER_SUPPLY_PROP_STATUS: 451 264 ret = bq27x00_battery_status(di, val); 452 265 break; 453 266 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 267 + ret = bq27x00_battery_voltage(di, val); 268 + break; 454 269 case POWER_SUPPLY_PROP_PRESENT: 455 - val->intval = bq27x00_battery_voltage(di); 456 - if (psp == POWER_SUPPLY_PROP_PRESENT) 457 - val->intval = val->intval <= 0 ? 0 : 1; 270 + val->intval = di->cache.flags < 0 ? 0 : 1; 458 271 break; 459 272 case POWER_SUPPLY_PROP_CURRENT_NOW: 460 - val->intval = bq27x00_battery_current(di); 273 + ret = bq27x00_battery_current(di, val); 461 274 break; 462 275 case POWER_SUPPLY_PROP_CAPACITY: 463 - val->intval = bq27x00_battery_rsoc(di); 276 + ret = bq27x00_simple_value(di->cache.capacity, val); 464 277 break; 465 278 case POWER_SUPPLY_PROP_TEMP: 466 - val->intval = bq27x00_battery_temperature(di); 279 + ret = bq27x00_battery_temperature(di, val); 467 280 break; 468 281 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: 469 - ret = bq27x00_battery_time(di, BQ27x00_REG_TTE, val); 282 + ret = bq27x00_simple_value(di->cache.time_to_empty, val); 470 283 break; 471 284 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: 472 - ret = bq27x00_battery_time(di, BQ27x00_REG_TTECP, val); 285 + ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val); 473 286 break; 474 287 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW: 475 - ret = bq27x00_battery_time(di, BQ27x00_REG_TTF, val); 288 + ret = bq27x00_simple_value(di->cache.time_to_full, val); 289 + break; 290 + case POWER_SUPPLY_PROP_TECHNOLOGY: 291 + val->intval = POWER_SUPPLY_TECHNOLOGY_LION; 292 + break; 293 + case POWER_SUPPLY_PROP_CHARGE_NOW: 294 + ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val); 295 + break; 296 + case POWER_SUPPLY_PROP_CHARGE_FULL: 297 + ret = bq27x00_simple_value(di->cache.charge_full, val); 298 + break; 299 + case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 300 + ret = bq27x00_simple_value(di->charge_design_full, val); 301 + break; 302 + case POWER_SUPPLY_PROP_CHARGE_COUNTER: 303 + ret = bq27x00_simple_value(di->cache.charge_counter, val); 304 + break; 305 + case POWER_SUPPLY_PROP_ENERGY_NOW: 306 + ret = bq27x00_battery_energy(di, val); 476 307 break; 477 308 default: 478 309 return -EINVAL; ··· 509 284 return ret; 510 285 } 511 286 512 - static void bq27x00_powersupply_init(struct bq27x00_device_info *di) 287 + static void bq27x00_external_power_changed(struct power_supply *psy) 513 288 { 289 + struct bq27x00_device_info *di = to_bq27x00_device_info(psy); 290 + 291 + cancel_delayed_work_sync(&di->work); 292 + schedule_delayed_work(&di->work, 0); 293 + } 294 + 295 + static int bq27x00_powersupply_init(struct bq27x00_device_info *di) 296 + { 297 + int ret; 298 + 514 299 di->bat.type = POWER_SUPPLY_TYPE_BATTERY; 515 300 di->bat.properties = bq27x00_battery_props; 516 301 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props); 517 302 di->bat.get_property = bq27x00_battery_get_property; 518 - di->bat.external_power_changed = NULL; 303 + di->bat.external_power_changed = bq27x00_external_power_changed; 304 + 305 + INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll); 306 + mutex_init(&di->lock); 307 + 308 + ret = power_supply_register(di->dev, &di->bat); 309 + if (ret) { 310 + dev_err(di->dev, "failed to register battery: %d\n", ret); 311 + return ret; 312 + } 313 + 314 + dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION); 315 + 316 + bq27x00_update(di); 317 + 318 + return 0; 519 319 } 520 320 521 - /* 522 - * i2c specific code 523 - */ 524 - 525 - static int bq27x00_read_i2c(u8 reg, int *rt_value, int b_single, 526 - struct bq27x00_device_info *di) 321 + static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di) 527 322 { 528 - struct i2c_client *client = di->client; 529 - struct i2c_msg msg[1]; 323 + cancel_delayed_work_sync(&di->work); 324 + 325 + power_supply_unregister(&di->bat); 326 + 327 + mutex_destroy(&di->lock); 328 + } 329 + 330 + 331 + /* i2c specific code */ 332 + #ifdef CONFIG_BATTERY_BQ27X00_I2C 333 + 334 + /* If the system has several batteries we need a different name for each 335 + * of them... 336 + */ 337 + static DEFINE_IDR(battery_id); 338 + static DEFINE_MUTEX(battery_mutex); 339 + 340 + static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single) 341 + { 342 + struct i2c_client *client = to_i2c_client(di->dev); 343 + struct i2c_msg msg[2]; 530 344 unsigned char data[2]; 531 - int err; 345 + int ret; 532 346 533 347 if (!client->adapter) 534 348 return -ENODEV; 535 349 536 - msg->addr = client->addr; 537 - msg->flags = 0; 538 - msg->len = 1; 539 - msg->buf = data; 350 + msg[0].addr = client->addr; 351 + msg[0].flags = 0; 352 + msg[0].buf = &reg; 353 + msg[0].len = sizeof(reg); 354 + msg[1].addr = client->addr; 355 + msg[1].flags = I2C_M_RD; 356 + msg[1].buf = data; 357 + if (single) 358 + msg[1].len = 1; 359 + else 360 + msg[1].len = 2; 540 361 541 - data[0] = reg; 542 - err = i2c_transfer(client->adapter, msg, 1); 362 + ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); 363 + if (ret < 0) 364 + return ret; 543 365 544 - if (err >= 0) { 545 - if (!b_single) 546 - msg->len = 2; 547 - else 548 - msg->len = 1; 366 + if (!single) 367 + ret = get_unaligned_le16(data); 368 + else 369 + ret = data[0]; 549 370 550 - msg->flags = I2C_M_RD; 551 - err = i2c_transfer(client->adapter, msg, 1); 552 - if (err >= 0) { 553 - if (!b_single) 554 - *rt_value = get_unaligned_le16(data); 555 - else 556 - *rt_value = data[0]; 557 - 558 - return 0; 559 - } 560 - } 561 - return err; 371 + return ret; 562 372 } 563 373 564 374 static int bq27x00_battery_probe(struct i2c_client *client, ··· 601 341 { 602 342 char *name; 603 343 struct bq27x00_device_info *di; 604 - struct bq27x00_access_methods *bus; 605 344 int num; 606 345 int retval = 0; 607 346 ··· 627 368 retval = -ENOMEM; 628 369 goto batt_failed_2; 629 370 } 630 - di->id = num; 631 - di->chip = id->driver_data; 632 371 633 - bus = kzalloc(sizeof(*bus), GFP_KERNEL); 634 - if (!bus) { 635 - dev_err(&client->dev, "failed to allocate access method " 636 - "data\n"); 637 - retval = -ENOMEM; 372 + di->id = num; 373 + di->dev = &client->dev; 374 + di->chip = id->driver_data; 375 + di->bat.name = name; 376 + di->bus.read = &bq27x00_read_i2c; 377 + 378 + if (bq27x00_powersupply_init(di)) 638 379 goto batt_failed_3; 639 - } 640 380 641 381 i2c_set_clientdata(client, di); 642 - di->dev = &client->dev; 643 - di->bat.name = name; 644 - bus->read = &bq27x00_read_i2c; 645 - di->bus = bus; 646 - di->client = client; 647 - 648 - bq27x00_powersupply_init(di); 649 - 650 - retval = power_supply_register(&client->dev, &di->bat); 651 - if (retval) { 652 - dev_err(&client->dev, "failed to register battery\n"); 653 - goto batt_failed_4; 654 - } 655 - 656 - dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION); 657 382 658 383 return 0; 659 384 660 - batt_failed_4: 661 - kfree(bus); 662 385 batt_failed_3: 663 386 kfree(di); 664 387 batt_failed_2: ··· 657 416 { 658 417 struct bq27x00_device_info *di = i2c_get_clientdata(client); 659 418 660 - power_supply_unregister(&di->bat); 419 + bq27x00_powersupply_unregister(di); 661 420 662 - kfree(di->bus); 663 421 kfree(di->bat.name); 664 422 665 423 mutex_lock(&battery_mutex); ··· 670 430 return 0; 671 431 } 672 432 673 - /* 674 - * Module stuff 675 - */ 676 - 677 433 static const struct i2c_device_id bq27x00_id[] = { 678 434 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */ 679 435 { "bq27500", BQ27500 }, 680 436 {}, 681 437 }; 438 + MODULE_DEVICE_TABLE(i2c, bq27x00_id); 682 439 683 440 static struct i2c_driver bq27x00_battery_driver = { 684 441 .driver = { ··· 686 449 .id_table = bq27x00_id, 687 450 }; 688 451 452 + static inline int bq27x00_battery_i2c_init(void) 453 + { 454 + int ret = i2c_add_driver(&bq27x00_battery_driver); 455 + if (ret) 456 + printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n"); 457 + 458 + return ret; 459 + } 460 + 461 + static inline void bq27x00_battery_i2c_exit(void) 462 + { 463 + i2c_del_driver(&bq27x00_battery_driver); 464 + } 465 + 466 + #else 467 + 468 + static inline int bq27x00_battery_i2c_init(void) { return 0; } 469 + static inline void bq27x00_battery_i2c_exit(void) {}; 470 + 471 + #endif 472 + 473 + /* platform specific code */ 474 + #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM 475 + 476 + static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg, 477 + bool single) 478 + { 479 + struct device *dev = di->dev; 480 + struct bq27000_platform_data *pdata = dev->platform_data; 481 + unsigned int timeout = 3; 482 + int upper, lower; 483 + int temp; 484 + 485 + if (!single) { 486 + /* Make sure the value has not changed in between reading the 487 + * lower and the upper part */ 488 + upper = pdata->read(dev, reg + 1); 489 + do { 490 + temp = upper; 491 + if (upper < 0) 492 + return upper; 493 + 494 + lower = pdata->read(dev, reg); 495 + if (lower < 0) 496 + return lower; 497 + 498 + upper = pdata->read(dev, reg + 1); 499 + } while (temp != upper && --timeout); 500 + 501 + if (timeout == 0) 502 + return -EIO; 503 + 504 + return (upper << 8) | lower; 505 + } 506 + 507 + return pdata->read(dev, reg); 508 + } 509 + 510 + static int __devinit bq27000_battery_probe(struct platform_device *pdev) 511 + { 512 + struct bq27x00_device_info *di; 513 + struct bq27000_platform_data *pdata = pdev->dev.platform_data; 514 + int ret; 515 + 516 + if (!pdata) { 517 + dev_err(&pdev->dev, "no platform_data supplied\n"); 518 + return -EINVAL; 519 + } 520 + 521 + if (!pdata->read) { 522 + dev_err(&pdev->dev, "no hdq read callback supplied\n"); 523 + return -EINVAL; 524 + } 525 + 526 + di = kzalloc(sizeof(*di), GFP_KERNEL); 527 + if (!di) { 528 + dev_err(&pdev->dev, "failed to allocate device info data\n"); 529 + return -ENOMEM; 530 + } 531 + 532 + platform_set_drvdata(pdev, di); 533 + 534 + di->dev = &pdev->dev; 535 + di->chip = BQ27000; 536 + 537 + di->bat.name = pdata->name ?: dev_name(&pdev->dev); 538 + di->bus.read = &bq27000_read_platform; 539 + 540 + ret = bq27x00_powersupply_init(di); 541 + if (ret) 542 + goto err_free; 543 + 544 + return 0; 545 + 546 + err_free: 547 + platform_set_drvdata(pdev, NULL); 548 + kfree(di); 549 + 550 + return ret; 551 + } 552 + 553 + static int __devexit bq27000_battery_remove(struct platform_device *pdev) 554 + { 555 + struct bq27x00_device_info *di = platform_get_drvdata(pdev); 556 + 557 + bq27x00_powersupply_unregister(di); 558 + 559 + platform_set_drvdata(pdev, NULL); 560 + kfree(di); 561 + 562 + return 0; 563 + } 564 + 565 + static struct platform_driver bq27000_battery_driver = { 566 + .probe = bq27000_battery_probe, 567 + .remove = __devexit_p(bq27000_battery_remove), 568 + .driver = { 569 + .name = "bq27000-battery", 570 + .owner = THIS_MODULE, 571 + }, 572 + }; 573 + 574 + static inline int bq27x00_battery_platform_init(void) 575 + { 576 + int ret = platform_driver_register(&bq27000_battery_driver); 577 + if (ret) 578 + printk(KERN_ERR "Unable to register BQ27000 platform driver\n"); 579 + 580 + return ret; 581 + } 582 + 583 + static inline void bq27x00_battery_platform_exit(void) 584 + { 585 + platform_driver_unregister(&bq27000_battery_driver); 586 + } 587 + 588 + #else 589 + 590 + static inline int bq27x00_battery_platform_init(void) { return 0; } 591 + static inline void bq27x00_battery_platform_exit(void) {}; 592 + 593 + #endif 594 + 595 + /* 596 + * Module stuff 597 + */ 598 + 689 599 static int __init bq27x00_battery_init(void) 690 600 { 691 601 int ret; 692 602 693 - ret = i2c_add_driver(&bq27x00_battery_driver); 603 + ret = bq27x00_battery_i2c_init(); 694 604 if (ret) 695 - printk(KERN_ERR "Unable to register BQ27x00 driver\n"); 605 + return ret; 606 + 607 + ret = bq27x00_battery_platform_init(); 608 + if (ret) 609 + bq27x00_battery_i2c_exit(); 696 610 697 611 return ret; 698 612 } ··· 851 463 852 464 static void __exit bq27x00_battery_exit(void) 853 465 { 854 - i2c_del_driver(&bq27x00_battery_driver); 466 + bq27x00_battery_platform_exit(); 467 + bq27x00_battery_i2c_exit(); 855 468 } 856 469 module_exit(bq27x00_battery_exit); 857 470
+1
drivers/power/ds2782_battery.c
··· 393 393 {"ds2786", DS2786}, 394 394 {}, 395 395 }; 396 + MODULE_DEVICE_TABLE(i2c, ds278x_id); 396 397 397 398 static struct i2c_driver ds278x_battery_driver = { 398 399 .driver = {
+2 -2
drivers/power/power_supply_core.c
··· 171 171 dev_set_drvdata(dev, psy); 172 172 psy->dev = dev; 173 173 174 + INIT_WORK(&psy->changed_work, power_supply_changed_work); 175 + 174 176 rc = kobject_set_name(&dev->kobj, "%s", psy->name); 175 177 if (rc) 176 178 goto kobject_set_name_failed; ··· 180 178 rc = device_add(dev); 181 179 if (rc) 182 180 goto device_add_failed; 183 - 184 - INIT_WORK(&psy->changed_work, power_supply_changed_work); 185 181 186 182 rc = power_supply_create_triggers(psy); 187 183 if (rc)
+19
drivers/power/power_supply_leds.c
··· 21 21 static void power_supply_update_bat_leds(struct power_supply *psy) 22 22 { 23 23 union power_supply_propval status; 24 + unsigned long delay_on = 0; 25 + unsigned long delay_off = 0; 24 26 25 27 if (psy->get_property(psy, POWER_SUPPLY_PROP_STATUS, &status)) 26 28 return; ··· 34 32 led_trigger_event(psy->charging_full_trig, LED_FULL); 35 33 led_trigger_event(psy->charging_trig, LED_OFF); 36 34 led_trigger_event(psy->full_trig, LED_FULL); 35 + led_trigger_event(psy->charging_blink_full_solid_trig, 36 + LED_FULL); 37 37 break; 38 38 case POWER_SUPPLY_STATUS_CHARGING: 39 39 led_trigger_event(psy->charging_full_trig, LED_FULL); 40 40 led_trigger_event(psy->charging_trig, LED_FULL); 41 41 led_trigger_event(psy->full_trig, LED_OFF); 42 + led_trigger_blink(psy->charging_blink_full_solid_trig, 43 + &delay_on, &delay_off); 42 44 break; 43 45 default: 44 46 led_trigger_event(psy->charging_full_trig, LED_OFF); 45 47 led_trigger_event(psy->charging_trig, LED_OFF); 46 48 led_trigger_event(psy->full_trig, LED_OFF); 49 + led_trigger_event(psy->charging_blink_full_solid_trig, 50 + LED_OFF); 47 51 break; 48 52 } 49 53 } ··· 72 64 if (!psy->full_trig_name) 73 65 goto full_failed; 74 66 67 + psy->charging_blink_full_solid_trig_name = kasprintf(GFP_KERNEL, 68 + "%s-charging-blink-full-solid", psy->name); 69 + if (!psy->charging_blink_full_solid_trig_name) 70 + goto charging_blink_full_solid_failed; 71 + 75 72 led_trigger_register_simple(psy->charging_full_trig_name, 76 73 &psy->charging_full_trig); 77 74 led_trigger_register_simple(psy->charging_trig_name, 78 75 &psy->charging_trig); 79 76 led_trigger_register_simple(psy->full_trig_name, 80 77 &psy->full_trig); 78 + led_trigger_register_simple(psy->charging_blink_full_solid_trig_name, 79 + &psy->charging_blink_full_solid_trig); 81 80 82 81 goto success; 83 82 83 + charging_blink_full_solid_failed: 84 + kfree(psy->full_trig_name); 84 85 full_failed: 85 86 kfree(psy->charging_trig_name); 86 87 charging_failed: ··· 105 88 led_trigger_unregister_simple(psy->charging_full_trig); 106 89 led_trigger_unregister_simple(psy->charging_trig); 107 90 led_trigger_unregister_simple(psy->full_trig); 91 + led_trigger_unregister_simple(psy->charging_blink_full_solid_trig); 92 + kfree(psy->charging_blink_full_solid_trig_name); 108 93 kfree(psy->full_trig_name); 109 94 kfree(psy->charging_trig_name); 110 95 kfree(psy->charging_full_trig_name);
+1 -1
drivers/power/power_supply_sysfs.c
··· 270 270 attr = &power_supply_attrs[psy->properties[j]]; 271 271 272 272 ret = power_supply_show_property(dev, attr, prop_buf); 273 - if (ret == -ENODEV) { 273 + if (ret == -ENODEV || ret == -ENODATA) { 274 274 /* When a battery is absent, we expect -ENODEV. Don't abort; 275 275 send the uevent with at least the the PRESENT=0 property */ 276 276 ret = 0;
+2 -2
drivers/power/s3c_adc_battery.c
··· 406 406 return 0; 407 407 } 408 408 #else 409 - #define s3c_adc_battery_suspend NULL 410 - #define s3c_adc_battery_resume NULL 409 + #define s3c_adc_bat_suspend NULL 410 + #define s3c_adc_bat_resume NULL 411 411 #endif 412 412 413 413 static struct platform_driver s3c_adc_bat_driver = {
+19 -6
drivers/power/twl4030_charger.c
··· 71 71 struct power_supply usb; 72 72 struct otg_transceiver *transceiver; 73 73 struct notifier_block otg_nb; 74 + struct work_struct work; 74 75 int irq_chg; 75 76 int irq_bci; 77 + 78 + unsigned long event; 76 79 }; 77 80 78 81 /* ··· 261 258 return IRQ_HANDLED; 262 259 } 263 260 264 - static int twl4030_bci_usb_ncb(struct notifier_block *nb, unsigned long val, 265 - void *priv) 261 + static void twl4030_bci_usb_work(struct work_struct *data) 266 262 { 267 - struct twl4030_bci *bci = container_of(nb, struct twl4030_bci, otg_nb); 263 + struct twl4030_bci *bci = container_of(data, struct twl4030_bci, work); 268 264 269 - dev_dbg(bci->dev, "OTG notify %lu\n", val); 270 - 271 - switch (val) { 265 + switch (bci->event) { 272 266 case USB_EVENT_VBUS: 273 267 case USB_EVENT_CHARGER: 274 268 twl4030_charger_enable_usb(bci, true); ··· 274 274 twl4030_charger_enable_usb(bci, false); 275 275 break; 276 276 } 277 + } 278 + 279 + static int twl4030_bci_usb_ncb(struct notifier_block *nb, unsigned long val, 280 + void *priv) 281 + { 282 + struct twl4030_bci *bci = container_of(nb, struct twl4030_bci, otg_nb); 283 + 284 + dev_dbg(bci->dev, "OTG notify %lu\n", val); 285 + 286 + bci->event = val; 287 + schedule_work(&bci->work); 277 288 278 289 return NOTIFY_OK; 279 290 } ··· 476 465 bci->irq_bci, ret); 477 466 goto fail_bci_irq; 478 467 } 468 + 469 + INIT_WORK(&bci->work, twl4030_bci_usb_work); 479 470 480 471 bci->transceiver = otg_get_transceiver(); 481 472 if (bci->transceiver != NULL) {
+3
drivers/power/z2_battery.c
··· 134 134 enum power_supply_property *prop; 135 135 struct z2_battery_info *info = charger->info; 136 136 137 + if (info->charge_gpio >= 0) 138 + props++; /* POWER_SUPPLY_PROP_STATUS */ 137 139 if (info->batt_tech >= 0) 138 140 props++; /* POWER_SUPPLY_PROP_TECHNOLOGY */ 139 141 if (info->batt_I2C_reg >= 0) ··· 295 293 { "aer915", 0 }, 296 294 { } 297 295 }; 296 + MODULE_DEVICE_TABLE(i2c, z2_batt_id); 298 297 299 298 static struct i2c_driver z2_batt_driver = { 300 299 .driver = {
+3
include/linux/leds.h
··· 145 145 extern void led_trigger_unregister_simple(struct led_trigger *trigger); 146 146 extern void led_trigger_event(struct led_trigger *trigger, 147 147 enum led_brightness event); 148 + extern void led_trigger_blink(struct led_trigger *trigger, 149 + unsigned long *delay_on, 150 + unsigned long *delay_off); 148 151 149 152 #else 150 153
+39
include/linux/power/bq20z75.h
··· 1 + /* 2 + * Gas Gauge driver for TI's BQ20Z75 3 + * 4 + * Copyright (c) 2010, NVIDIA Corporation. 5 + * 6 + * This program is free software; you can redistribute it and/or modify 7 + * it under the terms of the GNU General Public License as published by 8 + * the Free Software Foundation; either version 2 of the License, or 9 + * (at your option) any later version. 10 + * 11 + * This program is distributed in the hope that it will be useful, but WITHOUT 12 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 + * more details. 15 + * 16 + * You should have received a copy of the GNU General Public License along 17 + * with this program; if not, write to the Free Software Foundation, Inc., 18 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19 + */ 20 + 21 + #ifndef __LINUX_POWER_BQ20Z75_H_ 22 + #define __LINUX_POWER_BQ20Z75_H_ 23 + 24 + #include <linux/power_supply.h> 25 + #include <linux/types.h> 26 + 27 + /** 28 + * struct bq20z75_platform_data - platform data for bq20z75 devices 29 + * @battery_detect: GPIO which is used to detect battery presence 30 + * @battery_detect_present: gpio state when battery is present (0 / 1) 31 + * @i2c_retry_count: # of times to retry on i2c IO failure 32 + */ 33 + struct bq20z75_platform_data { 34 + int battery_detect; 35 + int battery_detect_present; 36 + int i2c_retry_count; 37 + }; 38 + 39 + #endif
+19
include/linux/power/bq27x00_battery.h
··· 1 + #ifndef __LINUX_BQ27X00_BATTERY_H__ 2 + #define __LINUX_BQ27X00_BATTERY_H__ 3 + 4 + /** 5 + * struct bq27000_plaform_data - Platform data for bq27000 devices 6 + * @name: Name of the battery. If NULL the driver will fallback to "bq27000". 7 + * @read: HDQ read callback. 8 + * This function should provide access to the HDQ bus the battery is 9 + * connected to. 10 + * The first parameter is a pointer to the battery device, the second the 11 + * register to be read. The return value should either be the content of 12 + * the passed register or an error value. 13 + */ 14 + struct bq27000_platform_data { 15 + const char *name; 16 + int (*read)(struct device *dev, unsigned int); 17 + }; 18 + 19 + #endif
+47
include/linux/power_supply.h
··· 173 173 char *full_trig_name; 174 174 struct led_trigger *online_trig; 175 175 char *online_trig_name; 176 + struct led_trigger *charging_blink_full_solid_trig; 177 + char *charging_blink_full_solid_trig_name; 176 178 #endif 177 179 }; 178 180 ··· 214 212 215 213 /* For APM emulation, think legacy userspace. */ 216 214 extern struct class *power_supply_class; 215 + 216 + static inline bool power_supply_is_amp_property(enum power_supply_property psp) 217 + { 218 + switch (psp) { 219 + case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 220 + case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN: 221 + case POWER_SUPPLY_PROP_CHARGE_FULL: 222 + case POWER_SUPPLY_PROP_CHARGE_EMPTY: 223 + case POWER_SUPPLY_PROP_CHARGE_NOW: 224 + case POWER_SUPPLY_PROP_CHARGE_AVG: 225 + case POWER_SUPPLY_PROP_CHARGE_COUNTER: 226 + case POWER_SUPPLY_PROP_CURRENT_MAX: 227 + case POWER_SUPPLY_PROP_CURRENT_NOW: 228 + case POWER_SUPPLY_PROP_CURRENT_AVG: 229 + return 1; 230 + default: 231 + break; 232 + } 233 + 234 + return 0; 235 + } 236 + 237 + static inline bool power_supply_is_watt_property(enum power_supply_property psp) 238 + { 239 + switch (psp) { 240 + case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: 241 + case POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN: 242 + case POWER_SUPPLY_PROP_ENERGY_FULL: 243 + case POWER_SUPPLY_PROP_ENERGY_EMPTY: 244 + case POWER_SUPPLY_PROP_ENERGY_NOW: 245 + case POWER_SUPPLY_PROP_ENERGY_AVG: 246 + case POWER_SUPPLY_PROP_VOLTAGE_MAX: 247 + case POWER_SUPPLY_PROP_VOLTAGE_MIN: 248 + case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: 249 + case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: 250 + case POWER_SUPPLY_PROP_VOLTAGE_NOW: 251 + case POWER_SUPPLY_PROP_VOLTAGE_AVG: 252 + case POWER_SUPPLY_PROP_POWER_NOW: 253 + return 1; 254 + default: 255 + break; 256 + } 257 + 258 + return 0; 259 + } 217 260 218 261 #endif /* __LINUX_POWER_SUPPLY_H__ */