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