linux/drivers/power/bq27x00_battery.c
<<
>>
Prefs
   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 * Copyright (C) 2011 Pali Rohár <pali.rohar@gmail.com>
   8 *
   9 * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
  10 *
  11 * This package is free software; you can redistribute it and/or modify
  12 * it under the terms of the GNU General Public License version 2 as
  13 * published by the Free Software Foundation.
  14 *
  15 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18 *
  19 */
  20
  21/*
  22 * Datasheets:
  23 * http://focus.ti.com/docs/prod/folders/print/bq27000.html
  24 * http://focus.ti.com/docs/prod/folders/print/bq27500.html
  25 * http://www.ti.com/product/bq27425-g1
  26 */
  27
  28#include <linux/module.h>
  29#include <linux/param.h>
  30#include <linux/jiffies.h>
  31#include <linux/workqueue.h>
  32#include <linux/delay.h>
  33#include <linux/platform_device.h>
  34#include <linux/power_supply.h>
  35#include <linux/idr.h>
  36#include <linux/i2c.h>
  37#include <linux/slab.h>
  38#include <asm/unaligned.h>
  39
  40#include <linux/power/bq27x00_battery.h>
  41
  42#define DRIVER_VERSION                  "1.2.0"
  43
  44#define BQ27x00_REG_TEMP                0x06
  45#define BQ27x00_REG_VOLT                0x08
  46#define BQ27x00_REG_AI                  0x14
  47#define BQ27x00_REG_FLAGS               0x0A
  48#define BQ27x00_REG_TTE                 0x16
  49#define BQ27x00_REG_TTF                 0x18
  50#define BQ27x00_REG_TTECP               0x26
  51#define BQ27x00_REG_NAC                 0x0C /* Nominal available capacity */
  52#define BQ27x00_REG_LMD                 0x12 /* Last measured discharge */
  53#define BQ27x00_REG_CYCT                0x2A /* Cycle count total */
  54#define BQ27x00_REG_AE                  0x22 /* Available energy */
  55#define BQ27x00_POWER_AVG               0x24
  56
  57#define BQ27000_REG_RSOC                0x0B /* Relative State-of-Charge */
  58#define BQ27000_REG_ILMD                0x76 /* Initial last measured discharge */
  59#define BQ27000_FLAG_EDVF               BIT(0) /* Final End-of-Discharge-Voltage flag */
  60#define BQ27000_FLAG_EDV1               BIT(1) /* First End-of-Discharge-Voltage flag */
  61#define BQ27000_FLAG_CI                 BIT(4) /* Capacity Inaccurate flag */
  62#define BQ27000_FLAG_FC                 BIT(5)
  63#define BQ27000_FLAG_CHGS               BIT(7) /* Charge state flag */
  64
  65#define BQ27500_REG_SOC                 0x2C
  66#define BQ27500_REG_DCAP                0x3C /* Design capacity */
  67#define BQ27500_FLAG_DSC                BIT(0)
  68#define BQ27500_FLAG_SOCF               BIT(1) /* State-of-Charge threshold final */
  69#define BQ27500_FLAG_SOC1               BIT(2) /* State-of-Charge threshold 1 */
  70#define BQ27500_FLAG_FC                 BIT(9)
  71#define BQ27500_FLAG_OTC                BIT(15)
  72
  73/* bq27425 register addresses are same as bq27x00 addresses minus 4 */
  74#define BQ27425_REG_OFFSET              0x04
  75#define BQ27425_REG_SOC                 0x18 /* Register address plus offset */
  76
  77#define BQ27000_RS                      20 /* Resistor sense */
  78#define BQ27x00_POWER_CONSTANT          (256 * 29200 / 1000)
  79
  80struct bq27x00_device_info;
  81struct bq27x00_access_methods {
  82        int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
  83};
  84
  85enum bq27x00_chip { BQ27000, BQ27500, BQ27425};
  86
  87struct bq27x00_reg_cache {
  88        int temperature;
  89        int time_to_empty;
  90        int time_to_empty_avg;
  91        int time_to_full;
  92        int charge_full;
  93        int cycle_count;
  94        int capacity;
  95        int energy;
  96        int flags;
  97        int power_avg;
  98        int health;
  99};
 100
 101struct bq27x00_device_info {
 102        struct device           *dev;
 103        int                     id;
 104        enum bq27x00_chip       chip;
 105
 106        struct bq27x00_reg_cache cache;
 107        int charge_design_full;
 108
 109        unsigned long last_update;
 110        struct delayed_work work;
 111
 112        struct power_supply     bat;
 113
 114        struct bq27x00_access_methods bus;
 115
 116        struct mutex lock;
 117};
 118
 119static enum power_supply_property bq27x00_battery_props[] = {
 120        POWER_SUPPLY_PROP_STATUS,
 121        POWER_SUPPLY_PROP_PRESENT,
 122        POWER_SUPPLY_PROP_VOLTAGE_NOW,
 123        POWER_SUPPLY_PROP_CURRENT_NOW,
 124        POWER_SUPPLY_PROP_CAPACITY,
 125        POWER_SUPPLY_PROP_CAPACITY_LEVEL,
 126        POWER_SUPPLY_PROP_TEMP,
 127        POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
 128        POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
 129        POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
 130        POWER_SUPPLY_PROP_TECHNOLOGY,
 131        POWER_SUPPLY_PROP_CHARGE_FULL,
 132        POWER_SUPPLY_PROP_CHARGE_NOW,
 133        POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
 134        POWER_SUPPLY_PROP_CYCLE_COUNT,
 135        POWER_SUPPLY_PROP_ENERGY_NOW,
 136        POWER_SUPPLY_PROP_POWER_AVG,
 137        POWER_SUPPLY_PROP_HEALTH,
 138};
 139
 140static enum power_supply_property bq27425_battery_props[] = {
 141        POWER_SUPPLY_PROP_STATUS,
 142        POWER_SUPPLY_PROP_PRESENT,
 143        POWER_SUPPLY_PROP_VOLTAGE_NOW,
 144        POWER_SUPPLY_PROP_CURRENT_NOW,
 145        POWER_SUPPLY_PROP_CAPACITY,
 146        POWER_SUPPLY_PROP_CAPACITY_LEVEL,
 147        POWER_SUPPLY_PROP_TEMP,
 148        POWER_SUPPLY_PROP_TECHNOLOGY,
 149        POWER_SUPPLY_PROP_CHARGE_FULL,
 150        POWER_SUPPLY_PROP_CHARGE_NOW,
 151        POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
 152};
 153
 154static unsigned int poll_interval = 360;
 155module_param(poll_interval, uint, 0644);
 156MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
 157                                "0 disables polling");
 158
 159/*
 160 * Common code for BQ27x00 devices
 161 */
 162
 163static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
 164                bool single)
 165{
 166        if (di->chip == BQ27425)
 167                return di->bus.read(di, reg - BQ27425_REG_OFFSET, single);
 168        return di->bus.read(di, reg, single);
 169}
 170
 171/*
 172 * Higher versions of the chip like BQ27425 and BQ27500
 173 * differ from BQ27000 and BQ27200 in calculation of certain
 174 * parameters. Hence we need to check for the chip type.
 175 */
 176static bool bq27xxx_is_chip_version_higher(struct bq27x00_device_info *di)
 177{
 178        if (di->chip == BQ27425 || di->chip == BQ27500)
 179                return true;
 180        return false;
 181}
 182
 183/*
 184 * Return the battery Relative State-of-Charge
 185 * Or < 0 if something fails.
 186 */
 187static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
 188{
 189        int rsoc;
 190
 191        if (di->chip == BQ27500)
 192                rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
 193        else if (di->chip == BQ27425)
 194                rsoc = bq27x00_read(di, BQ27425_REG_SOC, false);
 195        else
 196                rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
 197
 198        if (rsoc < 0)
 199                dev_dbg(di->dev, "error reading relative State-of-Charge\n");
 200
 201        return rsoc;
 202}
 203
 204/*
 205 * Return a battery charge value in µAh
 206 * Or < 0 if something fails.
 207 */
 208static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
 209{
 210        int charge;
 211
 212        charge = bq27x00_read(di, reg, false);
 213        if (charge < 0) {
 214                dev_dbg(di->dev, "error reading charge register %02x: %d\n",
 215                        reg, charge);
 216                return charge;
 217        }
 218
 219        if (bq27xxx_is_chip_version_higher(di))
 220                charge *= 1000;
 221        else
 222                charge = charge * 3570 / BQ27000_RS;
 223
 224        return charge;
 225}
 226
 227/*
 228 * Return the battery Nominal available capaciy in µAh
 229 * Or < 0 if something fails.
 230 */
 231static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
 232{
 233        int flags;
 234        bool is_bq27500 = di->chip == BQ27500;
 235        bool is_higher = bq27xxx_is_chip_version_higher(di);
 236
 237        flags = bq27x00_read(di, BQ27x00_REG_FLAGS, !is_bq27500);
 238        if (flags >= 0 && !is_higher && (flags & BQ27000_FLAG_CI))
 239                return -ENODATA;
 240
 241        return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
 242}
 243
 244/*
 245 * Return the battery Last measured discharge in µAh
 246 * Or < 0 if something fails.
 247 */
 248static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
 249{
 250        return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
 251}
 252
 253/*
 254 * Return the battery Initial last measured discharge in µAh
 255 * Or < 0 if something fails.
 256 */
 257static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
 258{
 259        int ilmd;
 260
 261        if (bq27xxx_is_chip_version_higher(di))
 262                ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
 263        else
 264                ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
 265
 266        if (ilmd < 0) {
 267                dev_dbg(di->dev, "error reading initial last measured discharge\n");
 268                return ilmd;
 269        }
 270
 271        if (bq27xxx_is_chip_version_higher(di))
 272                ilmd *= 1000;
 273        else
 274                ilmd = ilmd * 256 * 3570 / BQ27000_RS;
 275
 276        return ilmd;
 277}
 278
 279/*
 280 * Return the battery Available energy in µWh
 281 * Or < 0 if something fails.
 282 */
 283static int bq27x00_battery_read_energy(struct bq27x00_device_info *di)
 284{
 285        int ae;
 286
 287        ae = bq27x00_read(di, BQ27x00_REG_AE, false);
 288        if (ae < 0) {
 289                dev_dbg(di->dev, "error reading available energy\n");
 290                return ae;
 291        }
 292
 293        if (di->chip == BQ27500)
 294                ae *= 1000;
 295        else
 296                ae = ae * 29200 / BQ27000_RS;
 297
 298        return ae;
 299}
 300
 301/*
 302 * Return the battery temperature in tenths of degree Celsius
 303 * Or < 0 if something fails.
 304 */
 305static int bq27x00_battery_read_temperature(struct bq27x00_device_info *di)
 306{
 307        int temp;
 308
 309        temp = bq27x00_read(di, BQ27x00_REG_TEMP, false);
 310        if (temp < 0) {
 311                dev_err(di->dev, "error reading temperature\n");
 312                return temp;
 313        }
 314
 315        if (bq27xxx_is_chip_version_higher(di))
 316                temp -= 2731;
 317        else
 318                temp = ((temp * 5) - 5463) / 2;
 319
 320        return temp;
 321}
 322
 323/*
 324 * Return the battery Cycle count total
 325 * Or < 0 if something fails.
 326 */
 327static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
 328{
 329        int cyct;
 330
 331        cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
 332        if (cyct < 0)
 333                dev_err(di->dev, "error reading cycle count total\n");
 334
 335        return cyct;
 336}
 337
 338/*
 339 * Read a time register.
 340 * Return < 0 if something fails.
 341 */
 342static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
 343{
 344        int tval;
 345
 346        tval = bq27x00_read(di, reg, false);
 347        if (tval < 0) {
 348                dev_dbg(di->dev, "error reading time register %02x: %d\n",
 349                        reg, tval);
 350                return tval;
 351        }
 352
 353        if (tval == 65535)
 354                return -ENODATA;
 355
 356        return tval * 60;
 357}
 358
 359/*
 360 * Read a power avg register.
 361 * Return < 0 if something fails.
 362 */
 363static int bq27x00_battery_read_pwr_avg(struct bq27x00_device_info *di, u8 reg)
 364{
 365        int tval;
 366
 367        tval = bq27x00_read(di, reg, false);
 368        if (tval < 0) {
 369                dev_err(di->dev, "error reading power avg rgister  %02x: %d\n",
 370                        reg, tval);
 371                return tval;
 372        }
 373
 374        if (di->chip == BQ27500)
 375                return tval;
 376        else
 377                return (tval * BQ27x00_POWER_CONSTANT) / BQ27000_RS;
 378}
 379
 380/*
 381 * Read flag register.
 382 * Return < 0 if something fails.
 383 */
 384static int bq27x00_battery_read_health(struct bq27x00_device_info *di)
 385{
 386        int tval;
 387
 388        tval = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
 389        if (tval < 0) {
 390                dev_err(di->dev, "error reading flag register:%d\n", tval);
 391                return tval;
 392        }
 393
 394        if ((di->chip == BQ27500)) {
 395                if (tval & BQ27500_FLAG_SOCF)
 396                        tval = POWER_SUPPLY_HEALTH_DEAD;
 397                else if (tval & BQ27500_FLAG_OTC)
 398                        tval = POWER_SUPPLY_HEALTH_OVERHEAT;
 399                else
 400                        tval = POWER_SUPPLY_HEALTH_GOOD;
 401                return tval;
 402        } else {
 403                if (tval & BQ27000_FLAG_EDV1)
 404                        tval = POWER_SUPPLY_HEALTH_DEAD;
 405                else
 406                        tval = POWER_SUPPLY_HEALTH_GOOD;
 407                return tval;
 408        }
 409
 410        return -1;
 411}
 412
 413static void bq27x00_update(struct bq27x00_device_info *di)
 414{
 415        struct bq27x00_reg_cache cache = {0, };
 416        bool is_bq27500 = di->chip == BQ27500;
 417        bool is_bq27425 = di->chip == BQ27425;
 418
 419        cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, !is_bq27500);
 420        if (cache.flags >= 0) {
 421                if (!is_bq27500 && !is_bq27425
 422                                && (cache.flags & BQ27000_FLAG_CI)) {
 423                        dev_info(di->dev, "battery is not calibrated! ignoring capacity values\n");
 424                        cache.capacity = -ENODATA;
 425                        cache.energy = -ENODATA;
 426                        cache.time_to_empty = -ENODATA;
 427                        cache.time_to_empty_avg = -ENODATA;
 428                        cache.time_to_full = -ENODATA;
 429                        cache.charge_full = -ENODATA;
 430                        cache.health = -ENODATA;
 431                } else {
 432                        cache.capacity = bq27x00_battery_read_rsoc(di);
 433                        if (!is_bq27425) {
 434                                cache.energy = bq27x00_battery_read_energy(di);
 435                                cache.time_to_empty =
 436                                        bq27x00_battery_read_time(di,
 437                                                        BQ27x00_REG_TTE);
 438                                cache.time_to_empty_avg =
 439                                        bq27x00_battery_read_time(di,
 440                                                        BQ27x00_REG_TTECP);
 441                                cache.time_to_full =
 442                                        bq27x00_battery_read_time(di,
 443                                                        BQ27x00_REG_TTF);
 444                        }
 445                        cache.charge_full = bq27x00_battery_read_lmd(di);
 446                        cache.health = bq27x00_battery_read_health(di);
 447                }
 448                cache.temperature = bq27x00_battery_read_temperature(di);
 449                if (!is_bq27425)
 450                        cache.cycle_count = bq27x00_battery_read_cyct(di);
 451                cache.power_avg =
 452                        bq27x00_battery_read_pwr_avg(di, BQ27x00_POWER_AVG);
 453
 454                /* We only have to read charge design full once */
 455                if (di->charge_design_full <= 0)
 456                        di->charge_design_full = bq27x00_battery_read_ilmd(di);
 457        }
 458
 459        if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) {
 460                di->cache = cache;
 461                power_supply_changed(&di->bat);
 462        }
 463
 464        di->last_update = jiffies;
 465}
 466
 467static void bq27x00_battery_poll(struct work_struct *work)
 468{
 469        struct bq27x00_device_info *di =
 470                container_of(work, struct bq27x00_device_info, work.work);
 471
 472        bq27x00_update(di);
 473
 474        if (poll_interval > 0) {
 475                /* The timer does not have to be accurate. */
 476                set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
 477                schedule_delayed_work(&di->work, poll_interval * HZ);
 478        }
 479}
 480
 481/*
 482 * Return the battery average current in µA
 483 * Note that current can be negative signed as well
 484 * Or 0 if something fails.
 485 */
 486static int bq27x00_battery_current(struct bq27x00_device_info *di,
 487        union power_supply_propval *val)
 488{
 489        int curr;
 490        int flags;
 491
 492        curr = bq27x00_read(di, BQ27x00_REG_AI, false);
 493        if (curr < 0) {
 494                dev_err(di->dev, "error reading current\n");
 495                return curr;
 496        }
 497
 498        if (bq27xxx_is_chip_version_higher(di)) {
 499                /* bq27500 returns signed value */
 500                val->intval = (int)((s16)curr) * 1000;
 501        } else {
 502                flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
 503                if (flags & BQ27000_FLAG_CHGS) {
 504                        dev_dbg(di->dev, "negative current!\n");
 505                        curr = -curr;
 506                }
 507
 508                val->intval = curr * 3570 / BQ27000_RS;
 509        }
 510
 511        return 0;
 512}
 513
 514static int bq27x00_battery_status(struct bq27x00_device_info *di,
 515        union power_supply_propval *val)
 516{
 517        int status;
 518
 519        if (bq27xxx_is_chip_version_higher(di)) {
 520                if (di->cache.flags & BQ27500_FLAG_FC)
 521                        status = POWER_SUPPLY_STATUS_FULL;
 522                else if (di->cache.flags & BQ27500_FLAG_DSC)
 523                        status = POWER_SUPPLY_STATUS_DISCHARGING;
 524                else
 525                        status = POWER_SUPPLY_STATUS_CHARGING;
 526        } else {
 527                if (di->cache.flags & BQ27000_FLAG_FC)
 528                        status = POWER_SUPPLY_STATUS_FULL;
 529                else if (di->cache.flags & BQ27000_FLAG_CHGS)
 530                        status = POWER_SUPPLY_STATUS_CHARGING;
 531                else if (power_supply_am_i_supplied(&di->bat))
 532                        status = POWER_SUPPLY_STATUS_NOT_CHARGING;
 533                else
 534                        status = POWER_SUPPLY_STATUS_DISCHARGING;
 535        }
 536
 537        val->intval = status;
 538
 539        return 0;
 540}
 541
 542static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
 543        union power_supply_propval *val)
 544{
 545        int level;
 546
 547        if (bq27xxx_is_chip_version_higher(di)) {
 548                if (di->cache.flags & BQ27500_FLAG_FC)
 549                        level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
 550                else if (di->cache.flags & BQ27500_FLAG_SOC1)
 551                        level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
 552                else if (di->cache.flags & BQ27500_FLAG_SOCF)
 553                        level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
 554                else
 555                        level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
 556        } else {
 557                if (di->cache.flags & BQ27000_FLAG_FC)
 558                        level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
 559                else if (di->cache.flags & BQ27000_FLAG_EDV1)
 560                        level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
 561                else if (di->cache.flags & BQ27000_FLAG_EDVF)
 562                        level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
 563                else
 564                        level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
 565        }
 566
 567        val->intval = level;
 568
 569        return 0;
 570}
 571
 572/*
 573 * Return the battery Voltage in millivolts
 574 * Or < 0 if something fails.
 575 */
 576static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
 577        union power_supply_propval *val)
 578{
 579        int volt;
 580
 581        volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
 582        if (volt < 0) {
 583                dev_err(di->dev, "error reading voltage\n");
 584                return volt;
 585        }
 586
 587        val->intval = volt * 1000;
 588
 589        return 0;
 590}
 591
 592static int bq27x00_simple_value(int value,
 593        union power_supply_propval *val)
 594{
 595        if (value < 0)
 596                return value;
 597
 598        val->intval = value;
 599
 600        return 0;
 601}
 602
 603#define to_bq27x00_device_info(x) container_of((x), \
 604                                struct bq27x00_device_info, bat);
 605
 606static int bq27x00_battery_get_property(struct power_supply *psy,
 607                                        enum power_supply_property psp,
 608                                        union power_supply_propval *val)
 609{
 610        int ret = 0;
 611        struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
 612
 613        mutex_lock(&di->lock);
 614        if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
 615                cancel_delayed_work_sync(&di->work);
 616                bq27x00_battery_poll(&di->work.work);
 617        }
 618        mutex_unlock(&di->lock);
 619
 620        if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
 621                return -ENODEV;
 622
 623        switch (psp) {
 624        case POWER_SUPPLY_PROP_STATUS:
 625                ret = bq27x00_battery_status(di, val);
 626                break;
 627        case POWER_SUPPLY_PROP_VOLTAGE_NOW:
 628                ret = bq27x00_battery_voltage(di, val);
 629                break;
 630        case POWER_SUPPLY_PROP_PRESENT:
 631                val->intval = di->cache.flags < 0 ? 0 : 1;
 632                break;
 633        case POWER_SUPPLY_PROP_CURRENT_NOW:
 634                ret = bq27x00_battery_current(di, val);
 635                break;
 636        case POWER_SUPPLY_PROP_CAPACITY:
 637                ret = bq27x00_simple_value(di->cache.capacity, val);
 638                break;
 639        case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
 640                ret = bq27x00_battery_capacity_level(di, val);
 641                break;
 642        case POWER_SUPPLY_PROP_TEMP:
 643                ret = bq27x00_simple_value(di->cache.temperature, val);
 644                break;
 645        case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
 646                ret = bq27x00_simple_value(di->cache.time_to_empty, val);
 647                break;
 648        case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
 649                ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
 650                break;
 651        case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
 652                ret = bq27x00_simple_value(di->cache.time_to_full, val);
 653                break;
 654        case POWER_SUPPLY_PROP_TECHNOLOGY:
 655                val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
 656                break;
 657        case POWER_SUPPLY_PROP_CHARGE_NOW:
 658                ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
 659                break;
 660        case POWER_SUPPLY_PROP_CHARGE_FULL:
 661                ret = bq27x00_simple_value(di->cache.charge_full, val);
 662                break;
 663        case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
 664                ret = bq27x00_simple_value(di->charge_design_full, val);
 665                break;
 666        case POWER_SUPPLY_PROP_CYCLE_COUNT:
 667                ret = bq27x00_simple_value(di->cache.cycle_count, val);
 668                break;
 669        case POWER_SUPPLY_PROP_ENERGY_NOW:
 670                ret = bq27x00_simple_value(di->cache.energy, val);
 671                break;
 672        case POWER_SUPPLY_PROP_POWER_AVG:
 673                ret = bq27x00_simple_value(di->cache.power_avg, val);
 674                break;
 675        case POWER_SUPPLY_PROP_HEALTH:
 676                ret = bq27x00_simple_value(di->cache.health, val);
 677                break;
 678        default:
 679                return -EINVAL;
 680        }
 681
 682        return ret;
 683}
 684
 685static void bq27x00_external_power_changed(struct power_supply *psy)
 686{
 687        struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
 688
 689        cancel_delayed_work_sync(&di->work);
 690        schedule_delayed_work(&di->work, 0);
 691}
 692
 693static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
 694{
 695        int ret;
 696
 697        di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
 698        if (di->chip == BQ27425) {
 699                di->bat.properties = bq27425_battery_props;
 700                di->bat.num_properties = ARRAY_SIZE(bq27425_battery_props);
 701        } else {
 702                di->bat.properties = bq27x00_battery_props;
 703                di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
 704        }
 705        di->bat.get_property = bq27x00_battery_get_property;
 706        di->bat.external_power_changed = bq27x00_external_power_changed;
 707
 708        INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
 709        mutex_init(&di->lock);
 710
 711        ret = power_supply_register(di->dev, &di->bat);
 712        if (ret) {
 713                dev_err(di->dev, "failed to register battery: %d\n", ret);
 714                return ret;
 715        }
 716
 717        dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
 718
 719        bq27x00_update(di);
 720
 721        return 0;
 722}
 723
 724static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
 725{
 726        /*
 727         * power_supply_unregister call bq27x00_battery_get_property which
 728         * call bq27x00_battery_poll.
 729         * Make sure that bq27x00_battery_poll will not call
 730         * schedule_delayed_work again after unregister (which cause OOPS).
 731         */
 732        poll_interval = 0;
 733
 734        cancel_delayed_work_sync(&di->work);
 735
 736        power_supply_unregister(&di->bat);
 737
 738        mutex_destroy(&di->lock);
 739}
 740
 741
 742/* i2c specific code */
 743#ifdef CONFIG_BATTERY_BQ27X00_I2C
 744
 745/* If the system has several batteries we need a different name for each
 746 * of them...
 747 */
 748static DEFINE_IDR(battery_id);
 749static DEFINE_MUTEX(battery_mutex);
 750
 751static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
 752{
 753        struct i2c_client *client = to_i2c_client(di->dev);
 754        struct i2c_msg msg[2];
 755        unsigned char data[2];
 756        int ret;
 757
 758        if (!client->adapter)
 759                return -ENODEV;
 760
 761        msg[0].addr = client->addr;
 762        msg[0].flags = 0;
 763        msg[0].buf = &reg;
 764        msg[0].len = sizeof(reg);
 765        msg[1].addr = client->addr;
 766        msg[1].flags = I2C_M_RD;
 767        msg[1].buf = data;
 768        if (single)
 769                msg[1].len = 1;
 770        else
 771                msg[1].len = 2;
 772
 773        ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
 774        if (ret < 0)
 775                return ret;
 776
 777        if (!single)
 778                ret = get_unaligned_le16(data);
 779        else
 780                ret = data[0];
 781
 782        return ret;
 783}
 784
 785static int bq27x00_battery_probe(struct i2c_client *client,
 786                                 const struct i2c_device_id *id)
 787{
 788        char *name;
 789        struct bq27x00_device_info *di;
 790        int num;
 791        int retval = 0;
 792
 793        /* Get new ID for the new battery device */
 794        retval = idr_pre_get(&battery_id, GFP_KERNEL);
 795        if (retval == 0)
 796                return -ENOMEM;
 797        mutex_lock(&battery_mutex);
 798        retval = idr_get_new(&battery_id, client, &num);
 799        mutex_unlock(&battery_mutex);
 800        if (retval < 0)
 801                return retval;
 802
 803        name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
 804        if (!name) {
 805                dev_err(&client->dev, "failed to allocate device name\n");
 806                retval = -ENOMEM;
 807                goto batt_failed_1;
 808        }
 809
 810        di = kzalloc(sizeof(*di), GFP_KERNEL);
 811        if (!di) {
 812                dev_err(&client->dev, "failed to allocate device info data\n");
 813                retval = -ENOMEM;
 814                goto batt_failed_2;
 815        }
 816
 817        di->id = num;
 818        di->dev = &client->dev;
 819        di->chip = id->driver_data;
 820        di->bat.name = name;
 821        di->bus.read = &bq27x00_read_i2c;
 822
 823        retval = bq27x00_powersupply_init(di);
 824        if (retval)
 825                goto batt_failed_3;
 826
 827        i2c_set_clientdata(client, di);
 828
 829        return 0;
 830
 831batt_failed_3:
 832        kfree(di);
 833batt_failed_2:
 834        kfree(name);
 835batt_failed_1:
 836        mutex_lock(&battery_mutex);
 837        idr_remove(&battery_id, num);
 838        mutex_unlock(&battery_mutex);
 839
 840        return retval;
 841}
 842
 843static int bq27x00_battery_remove(struct i2c_client *client)
 844{
 845        struct bq27x00_device_info *di = i2c_get_clientdata(client);
 846
 847        bq27x00_powersupply_unregister(di);
 848
 849        kfree(di->bat.name);
 850
 851        mutex_lock(&battery_mutex);
 852        idr_remove(&battery_id, di->id);
 853        mutex_unlock(&battery_mutex);
 854
 855        kfree(di);
 856
 857        return 0;
 858}
 859
 860static const struct i2c_device_id bq27x00_id[] = {
 861        { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
 862        { "bq27500", BQ27500 },
 863        { "bq27425", BQ27425 },
 864        {},
 865};
 866MODULE_DEVICE_TABLE(i2c, bq27x00_id);
 867
 868static struct i2c_driver bq27x00_battery_driver = {
 869        .driver = {
 870                .name = "bq27x00-battery",
 871        },
 872        .probe = bq27x00_battery_probe,
 873        .remove = bq27x00_battery_remove,
 874        .id_table = bq27x00_id,
 875};
 876
 877static inline int bq27x00_battery_i2c_init(void)
 878{
 879        int ret = i2c_add_driver(&bq27x00_battery_driver);
 880        if (ret)
 881                printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
 882
 883        return ret;
 884}
 885
 886static inline void bq27x00_battery_i2c_exit(void)
 887{
 888        i2c_del_driver(&bq27x00_battery_driver);
 889}
 890
 891#else
 892
 893static inline int bq27x00_battery_i2c_init(void) { return 0; }
 894static inline void bq27x00_battery_i2c_exit(void) {};
 895
 896#endif
 897
 898/* platform specific code */
 899#ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
 900
 901static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
 902                        bool single)
 903{
 904        struct device *dev = di->dev;
 905        struct bq27000_platform_data *pdata = dev->platform_data;
 906        unsigned int timeout = 3;
 907        int upper, lower;
 908        int temp;
 909
 910        if (!single) {
 911                /* Make sure the value has not changed in between reading the
 912                 * lower and the upper part */
 913                upper = pdata->read(dev, reg + 1);
 914                do {
 915                        temp = upper;
 916                        if (upper < 0)
 917                                return upper;
 918
 919                        lower = pdata->read(dev, reg);
 920                        if (lower < 0)
 921                                return lower;
 922
 923                        upper = pdata->read(dev, reg + 1);
 924                } while (temp != upper && --timeout);
 925
 926                if (timeout == 0)
 927                        return -EIO;
 928
 929                return (upper << 8) | lower;
 930        }
 931
 932        return pdata->read(dev, reg);
 933}
 934
 935static int bq27000_battery_probe(struct platform_device *pdev)
 936{
 937        struct bq27x00_device_info *di;
 938        struct bq27000_platform_data *pdata = pdev->dev.platform_data;
 939        int ret;
 940
 941        if (!pdata) {
 942                dev_err(&pdev->dev, "no platform_data supplied\n");
 943                return -EINVAL;
 944        }
 945
 946        if (!pdata->read) {
 947                dev_err(&pdev->dev, "no hdq read callback supplied\n");
 948                return -EINVAL;
 949        }
 950
 951        di = kzalloc(sizeof(*di), GFP_KERNEL);
 952        if (!di) {
 953                dev_err(&pdev->dev, "failed to allocate device info data\n");
 954                return -ENOMEM;
 955        }
 956
 957        platform_set_drvdata(pdev, di);
 958
 959        di->dev = &pdev->dev;
 960        di->chip = BQ27000;
 961
 962        di->bat.name = pdata->name ?: dev_name(&pdev->dev);
 963        di->bus.read = &bq27000_read_platform;
 964
 965        ret = bq27x00_powersupply_init(di);
 966        if (ret)
 967                goto err_free;
 968
 969        return 0;
 970
 971err_free:
 972        platform_set_drvdata(pdev, NULL);
 973        kfree(di);
 974
 975        return ret;
 976}
 977
 978static int bq27000_battery_remove(struct platform_device *pdev)
 979{
 980        struct bq27x00_device_info *di = platform_get_drvdata(pdev);
 981
 982        bq27x00_powersupply_unregister(di);
 983
 984        platform_set_drvdata(pdev, NULL);
 985        kfree(di);
 986
 987        return 0;
 988}
 989
 990static struct platform_driver bq27000_battery_driver = {
 991        .probe  = bq27000_battery_probe,
 992        .remove = bq27000_battery_remove,
 993        .driver = {
 994                .name = "bq27000-battery",
 995                .owner = THIS_MODULE,
 996        },
 997};
 998
 999static inline int bq27x00_battery_platform_init(void)
1000{
1001        int ret = platform_driver_register(&bq27000_battery_driver);
1002        if (ret)
1003                printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
1004
1005        return ret;
1006}
1007
1008static inline void bq27x00_battery_platform_exit(void)
1009{
1010        platform_driver_unregister(&bq27000_battery_driver);
1011}
1012
1013#else
1014
1015static inline int bq27x00_battery_platform_init(void) { return 0; }
1016static inline void bq27x00_battery_platform_exit(void) {};
1017
1018#endif
1019
1020/*
1021 * Module stuff
1022 */
1023
1024static int __init bq27x00_battery_init(void)
1025{
1026        int ret;
1027
1028        ret = bq27x00_battery_i2c_init();
1029        if (ret)
1030                return ret;
1031
1032        ret = bq27x00_battery_platform_init();
1033        if (ret)
1034                bq27x00_battery_i2c_exit();
1035
1036        return ret;
1037}
1038module_init(bq27x00_battery_init);
1039
1040static void __exit bq27x00_battery_exit(void)
1041{
1042        bq27x00_battery_platform_exit();
1043        bq27x00_battery_i2c_exit();
1044}
1045module_exit(bq27x00_battery_exit);
1046
1047MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1048MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
1049MODULE_LICENSE("GPL");
1050
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.