linux/drivers/power/bq2415x_charger.c
<<
>>
Prefs
   1/*
   2 * bq2415x charger driver
   3 *
   4 * Copyright (C) 2011-2013  Pali Rohár <pali.rohar@gmail.com>
   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,
  12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14 * GNU General Public License for 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/*
  22 * Datasheets:
  23 * http://www.ti.com/product/bq24150
  24 * http://www.ti.com/product/bq24150a
  25 * http://www.ti.com/product/bq24152
  26 * http://www.ti.com/product/bq24153
  27 * http://www.ti.com/product/bq24153a
  28 * http://www.ti.com/product/bq24155
  29 */
  30
  31#include <linux/kernel.h>
  32#include <linux/module.h>
  33#include <linux/param.h>
  34#include <linux/err.h>
  35#include <linux/workqueue.h>
  36#include <linux/sysfs.h>
  37#include <linux/platform_device.h>
  38#include <linux/power_supply.h>
  39#include <linux/idr.h>
  40#include <linux/i2c.h>
  41#include <linux/slab.h>
  42
  43#include <linux/power/bq2415x_charger.h>
  44
  45/* timeout for resetting chip timer */
  46#define BQ2415X_TIMER_TIMEOUT           10
  47
  48#define BQ2415X_REG_STATUS              0x00
  49#define BQ2415X_REG_CONTROL             0x01
  50#define BQ2415X_REG_VOLTAGE             0x02
  51#define BQ2415X_REG_VENDER              0x03
  52#define BQ2415X_REG_CURRENT             0x04
  53
  54/* reset state for all registers */
  55#define BQ2415X_RESET_STATUS            BIT(6)
  56#define BQ2415X_RESET_CONTROL           (BIT(4)|BIT(5))
  57#define BQ2415X_RESET_VOLTAGE           (BIT(1)|BIT(3))
  58#define BQ2415X_RESET_CURRENT           (BIT(0)|BIT(3)|BIT(7))
  59
  60/* status register */
  61#define BQ2415X_BIT_TMR_RST             7
  62#define BQ2415X_BIT_OTG                 7
  63#define BQ2415X_BIT_EN_STAT             6
  64#define BQ2415X_MASK_STAT               (BIT(4)|BIT(5))
  65#define BQ2415X_SHIFT_STAT              4
  66#define BQ2415X_BIT_BOOST               3
  67#define BQ2415X_MASK_FAULT              (BIT(0)|BIT(1)|BIT(2))
  68#define BQ2415X_SHIFT_FAULT             0
  69
  70/* control register */
  71#define BQ2415X_MASK_LIMIT              (BIT(6)|BIT(7))
  72#define BQ2415X_SHIFT_LIMIT             6
  73#define BQ2415X_MASK_VLOWV              (BIT(4)|BIT(5))
  74#define BQ2415X_SHIFT_VLOWV             4
  75#define BQ2415X_BIT_TE                  3
  76#define BQ2415X_BIT_CE                  2
  77#define BQ2415X_BIT_HZ_MODE             1
  78#define BQ2415X_BIT_OPA_MODE            0
  79
  80/* voltage register */
  81#define BQ2415X_MASK_VO         (BIT(2)|BIT(3)|BIT(4)|BIT(5)|BIT(6)|BIT(7))
  82#define BQ2415X_SHIFT_VO                2
  83#define BQ2415X_BIT_OTG_PL              1
  84#define BQ2415X_BIT_OTG_EN              0
  85
  86/* vender register */
  87#define BQ2415X_MASK_VENDER             (BIT(5)|BIT(6)|BIT(7))
  88#define BQ2415X_SHIFT_VENDER            5
  89#define BQ2415X_MASK_PN                 (BIT(3)|BIT(4))
  90#define BQ2415X_SHIFT_PN                3
  91#define BQ2415X_MASK_REVISION           (BIT(0)|BIT(1)|BIT(2))
  92#define BQ2415X_SHIFT_REVISION          0
  93
  94/* current register */
  95#define BQ2415X_MASK_RESET              BIT(7)
  96#define BQ2415X_MASK_VI_CHRG            (BIT(4)|BIT(5)|BIT(6))
  97#define BQ2415X_SHIFT_VI_CHRG           4
  98/* N/A                                  BIT(3) */
  99#define BQ2415X_MASK_VI_TERM            (BIT(0)|BIT(1)|BIT(2))
 100#define BQ2415X_SHIFT_VI_TERM           0
 101
 102
 103enum bq2415x_command {
 104        BQ2415X_TIMER_RESET,
 105        BQ2415X_OTG_STATUS,
 106        BQ2415X_STAT_PIN_STATUS,
 107        BQ2415X_STAT_PIN_ENABLE,
 108        BQ2415X_STAT_PIN_DISABLE,
 109        BQ2415X_CHARGE_STATUS,
 110        BQ2415X_BOOST_STATUS,
 111        BQ2415X_FAULT_STATUS,
 112
 113        BQ2415X_CHARGE_TERMINATION_STATUS,
 114        BQ2415X_CHARGE_TERMINATION_ENABLE,
 115        BQ2415X_CHARGE_TERMINATION_DISABLE,
 116        BQ2415X_CHARGER_STATUS,
 117        BQ2415X_CHARGER_ENABLE,
 118        BQ2415X_CHARGER_DISABLE,
 119        BQ2415X_HIGH_IMPEDANCE_STATUS,
 120        BQ2415X_HIGH_IMPEDANCE_ENABLE,
 121        BQ2415X_HIGH_IMPEDANCE_DISABLE,
 122        BQ2415X_BOOST_MODE_STATUS,
 123        BQ2415X_BOOST_MODE_ENABLE,
 124        BQ2415X_BOOST_MODE_DISABLE,
 125
 126        BQ2415X_OTG_LEVEL,
 127        BQ2415X_OTG_ACTIVATE_HIGH,
 128        BQ2415X_OTG_ACTIVATE_LOW,
 129        BQ2415X_OTG_PIN_STATUS,
 130        BQ2415X_OTG_PIN_ENABLE,
 131        BQ2415X_OTG_PIN_DISABLE,
 132
 133        BQ2415X_VENDER_CODE,
 134        BQ2415X_PART_NUMBER,
 135        BQ2415X_REVISION,
 136};
 137
 138enum bq2415x_chip {
 139        BQUNKNOWN,
 140        BQ24150,
 141        BQ24150A,
 142        BQ24151,
 143        BQ24151A,
 144        BQ24152,
 145        BQ24153,
 146        BQ24153A,
 147        BQ24155,
 148        BQ24156,
 149        BQ24156A,
 150        BQ24158,
 151};
 152
 153static char *bq2415x_chip_name[] = {
 154        "unknown",
 155        "bq24150",
 156        "bq24150a",
 157        "bq24151",
 158        "bq24151a",
 159        "bq24152",
 160        "bq24153",
 161        "bq24153a",
 162        "bq24155",
 163        "bq24156",
 164        "bq24156a",
 165        "bq24158",
 166};
 167
 168struct bq2415x_device {
 169        struct device *dev;
 170        struct bq2415x_platform_data init_data;
 171        struct power_supply charger;
 172        struct delayed_work work;
 173        struct power_supply *notify_psy;
 174        struct notifier_block nb;
 175        enum bq2415x_mode reported_mode;/* mode reported by hook function */
 176        enum bq2415x_mode mode;         /* current configured mode */
 177        enum bq2415x_chip chip;
 178        const char *timer_error;
 179        char *model;
 180        char *name;
 181        int autotimer;  /* 1 - if driver automatically reset timer, 0 - not */
 182        int automode;   /* 1 - enabled, 0 - disabled; -1 - not supported */
 183        int id;
 184};
 185
 186/* each registered chip must have unique id */
 187static DEFINE_IDR(bq2415x_id);
 188
 189static DEFINE_MUTEX(bq2415x_id_mutex);
 190static DEFINE_MUTEX(bq2415x_timer_mutex);
 191static DEFINE_MUTEX(bq2415x_i2c_mutex);
 192
 193/**** i2c read functions ****/
 194
 195/* read value from register */
 196static int bq2415x_i2c_read(struct bq2415x_device *bq, u8 reg)
 197{
 198        struct i2c_client *client = to_i2c_client(bq->dev);
 199        struct i2c_msg msg[2];
 200        u8 val;
 201        int ret;
 202
 203        if (!client->adapter)
 204                return -ENODEV;
 205
 206        msg[0].addr = client->addr;
 207        msg[0].flags = 0;
 208        msg[0].buf = &reg;
 209        msg[0].len = sizeof(reg);
 210        msg[1].addr = client->addr;
 211        msg[1].flags = I2C_M_RD;
 212        msg[1].buf = &val;
 213        msg[1].len = sizeof(val);
 214
 215        mutex_lock(&bq2415x_i2c_mutex);
 216        ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
 217        mutex_unlock(&bq2415x_i2c_mutex);
 218
 219        if (ret < 0)
 220                return ret;
 221
 222        return val;
 223}
 224
 225/* read value from register, apply mask and right shift it */
 226static int bq2415x_i2c_read_mask(struct bq2415x_device *bq, u8 reg,
 227                                 u8 mask, u8 shift)
 228{
 229        int ret;
 230
 231        if (shift > 8)
 232                return -EINVAL;
 233
 234        ret = bq2415x_i2c_read(bq, reg);
 235        if (ret < 0)
 236                return ret;
 237        return (ret & mask) >> shift;
 238}
 239
 240/* read value from register and return one specified bit */
 241static int bq2415x_i2c_read_bit(struct bq2415x_device *bq, u8 reg, u8 bit)
 242{
 243        if (bit > 8)
 244                return -EINVAL;
 245        return bq2415x_i2c_read_mask(bq, reg, BIT(bit), bit);
 246}
 247
 248/**** i2c write functions ****/
 249
 250/* write value to register */
 251static int bq2415x_i2c_write(struct bq2415x_device *bq, u8 reg, u8 val)
 252{
 253        struct i2c_client *client = to_i2c_client(bq->dev);
 254        struct i2c_msg msg[1];
 255        u8 data[2];
 256        int ret;
 257
 258        data[0] = reg;
 259        data[1] = val;
 260
 261        msg[0].addr = client->addr;
 262        msg[0].flags = 0;
 263        msg[0].buf = data;
 264        msg[0].len = ARRAY_SIZE(data);
 265
 266        mutex_lock(&bq2415x_i2c_mutex);
 267        ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
 268        mutex_unlock(&bq2415x_i2c_mutex);
 269
 270        /* i2c_transfer returns number of messages transferred */
 271        if (ret < 0)
 272                return ret;
 273        else if (ret != 1)
 274                return -EIO;
 275
 276        return 0;
 277}
 278
 279/* read value from register, change it with mask left shifted and write back */
 280static int bq2415x_i2c_write_mask(struct bq2415x_device *bq, u8 reg, u8 val,
 281                                  u8 mask, u8 shift)
 282{
 283        int ret;
 284
 285        if (shift > 8)
 286                return -EINVAL;
 287
 288        ret = bq2415x_i2c_read(bq, reg);
 289        if (ret < 0)
 290                return ret;
 291
 292        ret &= ~mask;
 293        ret |= val << shift;
 294
 295        return bq2415x_i2c_write(bq, reg, ret);
 296}
 297
 298/* change only one bit in register */
 299static int bq2415x_i2c_write_bit(struct bq2415x_device *bq, u8 reg,
 300                                 bool val, u8 bit)
 301{
 302        if (bit > 8)
 303                return -EINVAL;
 304        return bq2415x_i2c_write_mask(bq, reg, val, BIT(bit), bit);
 305}
 306
 307/**** global functions ****/
 308
 309/* exec command function */
 310static int bq2415x_exec_command(struct bq2415x_device *bq,
 311                                enum bq2415x_command command)
 312{
 313        int ret;
 314
 315        switch (command) {
 316        case BQ2415X_TIMER_RESET:
 317                return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS,
 318                                1, BQ2415X_BIT_TMR_RST);
 319        case BQ2415X_OTG_STATUS:
 320                return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
 321                                BQ2415X_BIT_OTG);
 322        case BQ2415X_STAT_PIN_STATUS:
 323                return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
 324                                BQ2415X_BIT_EN_STAT);
 325        case BQ2415X_STAT_PIN_ENABLE:
 326                return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 1,
 327                                BQ2415X_BIT_EN_STAT);
 328        case BQ2415X_STAT_PIN_DISABLE:
 329                return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 0,
 330                                BQ2415X_BIT_EN_STAT);
 331        case BQ2415X_CHARGE_STATUS:
 332                return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS,
 333                                BQ2415X_MASK_STAT, BQ2415X_SHIFT_STAT);
 334        case BQ2415X_BOOST_STATUS:
 335                return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
 336                                BQ2415X_BIT_BOOST);
 337        case BQ2415X_FAULT_STATUS:
 338                return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS,
 339                        BQ2415X_MASK_FAULT, BQ2415X_SHIFT_FAULT);
 340
 341        case BQ2415X_CHARGE_TERMINATION_STATUS:
 342                return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
 343                                BQ2415X_BIT_TE);
 344        case BQ2415X_CHARGE_TERMINATION_ENABLE:
 345                return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
 346                                1, BQ2415X_BIT_TE);
 347        case BQ2415X_CHARGE_TERMINATION_DISABLE:
 348                return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
 349                                0, BQ2415X_BIT_TE);
 350        case BQ2415X_CHARGER_STATUS:
 351                ret = bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
 352                        BQ2415X_BIT_CE);
 353                if (ret < 0)
 354                        return ret;
 355                else
 356                        return ret > 0 ? 0 : 1;
 357        case BQ2415X_CHARGER_ENABLE:
 358                return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
 359                                0, BQ2415X_BIT_CE);
 360        case BQ2415X_CHARGER_DISABLE:
 361                return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
 362                                1, BQ2415X_BIT_CE);
 363        case BQ2415X_HIGH_IMPEDANCE_STATUS:
 364                return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
 365                                BQ2415X_BIT_HZ_MODE);
 366        case BQ2415X_HIGH_IMPEDANCE_ENABLE:
 367                return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
 368                                1, BQ2415X_BIT_HZ_MODE);
 369        case BQ2415X_HIGH_IMPEDANCE_DISABLE:
 370                return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
 371                                0, BQ2415X_BIT_HZ_MODE);
 372        case BQ2415X_BOOST_MODE_STATUS:
 373                return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
 374                                BQ2415X_BIT_OPA_MODE);
 375        case BQ2415X_BOOST_MODE_ENABLE:
 376                return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
 377                                1, BQ2415X_BIT_OPA_MODE);
 378        case BQ2415X_BOOST_MODE_DISABLE:
 379                return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
 380                                0, BQ2415X_BIT_OPA_MODE);
 381
 382        case BQ2415X_OTG_LEVEL:
 383                return bq2415x_i2c_read_bit(bq, BQ2415X_REG_VOLTAGE,
 384                                BQ2415X_BIT_OTG_PL);
 385        case BQ2415X_OTG_ACTIVATE_HIGH:
 386                return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
 387                                1, BQ2415X_BIT_OTG_PL);
 388        case BQ2415X_OTG_ACTIVATE_LOW:
 389                return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
 390                                0, BQ2415X_BIT_OTG_PL);
 391        case BQ2415X_OTG_PIN_STATUS:
 392                return bq2415x_i2c_read_bit(bq, BQ2415X_REG_VOLTAGE,
 393                                BQ2415X_BIT_OTG_EN);
 394        case BQ2415X_OTG_PIN_ENABLE:
 395                return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
 396                                1, BQ2415X_BIT_OTG_EN);
 397        case BQ2415X_OTG_PIN_DISABLE:
 398                return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
 399                                0, BQ2415X_BIT_OTG_EN);
 400
 401        case BQ2415X_VENDER_CODE:
 402                return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
 403                        BQ2415X_MASK_VENDER, BQ2415X_SHIFT_VENDER);
 404        case BQ2415X_PART_NUMBER:
 405                return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
 406                                BQ2415X_MASK_PN, BQ2415X_SHIFT_PN);
 407        case BQ2415X_REVISION:
 408                return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
 409                        BQ2415X_MASK_REVISION, BQ2415X_SHIFT_REVISION);
 410        }
 411        return -EINVAL;
 412}
 413
 414/* detect chip type */
 415static enum bq2415x_chip bq2415x_detect_chip(struct bq2415x_device *bq)
 416{
 417        struct i2c_client *client = to_i2c_client(bq->dev);
 418        int ret = bq2415x_exec_command(bq, BQ2415X_PART_NUMBER);
 419
 420        if (ret < 0)
 421                return ret;
 422
 423        switch (client->addr) {
 424        case 0x6b:
 425                switch (ret) {
 426                case 0:
 427                        if (bq->chip == BQ24151A)
 428                                return bq->chip;
 429                        else
 430                                return BQ24151;
 431                case 1:
 432                        if (bq->chip == BQ24150A ||
 433                                bq->chip == BQ24152 ||
 434                                bq->chip == BQ24155)
 435                                return bq->chip;
 436                        else
 437                                return BQ24150;
 438                case 2:
 439                        if (bq->chip == BQ24153A)
 440                                return bq->chip;
 441                        else
 442                                return BQ24153;
 443                default:
 444                        return BQUNKNOWN;
 445                }
 446                break;
 447
 448        case 0x6a:
 449                switch (ret) {
 450                case 0:
 451                        if (bq->chip == BQ24156A)
 452                                return bq->chip;
 453                        else
 454                                return BQ24156;
 455                case 2:
 456                        return BQ24158;
 457                default:
 458                        return BQUNKNOWN;
 459                }
 460                break;
 461        }
 462
 463        return BQUNKNOWN;
 464}
 465
 466/* detect chip revision */
 467static int bq2415x_detect_revision(struct bq2415x_device *bq)
 468{
 469        int ret = bq2415x_exec_command(bq, BQ2415X_REVISION);
 470        int chip = bq2415x_detect_chip(bq);
 471
 472        if (ret < 0 || chip < 0)
 473                return -1;
 474
 475        switch (chip) {
 476        case BQ24150:
 477        case BQ24150A:
 478        case BQ24151:
 479        case BQ24151A:
 480        case BQ24152:
 481                if (ret >= 0 && ret <= 3)
 482                        return ret;
 483                else
 484                        return -1;
 485        case BQ24153:
 486        case BQ24153A:
 487        case BQ24156:
 488        case BQ24156A:
 489        case BQ24158:
 490                if (ret == 3)
 491                        return 0;
 492                else if (ret == 1)
 493                        return 1;
 494                else
 495                        return -1;
 496        case BQ24155:
 497                if (ret == 3)
 498                        return 3;
 499                else
 500                        return -1;
 501        case BQUNKNOWN:
 502                return -1;
 503        }
 504
 505        return -1;
 506}
 507
 508/* return chip vender code */
 509static int bq2415x_get_vender_code(struct bq2415x_device *bq)
 510{
 511        int ret;
 512
 513        ret = bq2415x_exec_command(bq, BQ2415X_VENDER_CODE);
 514        if (ret < 0)
 515                return 0;
 516
 517        /* convert to binary */
 518        return (ret & 0x1) +
 519               ((ret >> 1) & 0x1) * 10 +
 520               ((ret >> 2) & 0x1) * 100;
 521}
 522
 523/* reset all chip registers to default state */
 524static void bq2415x_reset_chip(struct bq2415x_device *bq)
 525{
 526        bq2415x_i2c_write(bq, BQ2415X_REG_CURRENT, BQ2415X_RESET_CURRENT);
 527        bq2415x_i2c_write(bq, BQ2415X_REG_VOLTAGE, BQ2415X_RESET_VOLTAGE);
 528        bq2415x_i2c_write(bq, BQ2415X_REG_CONTROL, BQ2415X_RESET_CONTROL);
 529        bq2415x_i2c_write(bq, BQ2415X_REG_STATUS, BQ2415X_RESET_STATUS);
 530        bq->timer_error = NULL;
 531}
 532
 533/**** properties functions ****/
 534
 535/* set current limit in mA */
 536static int bq2415x_set_current_limit(struct bq2415x_device *bq, int mA)
 537{
 538        int val;
 539
 540        if (mA <= 100)
 541                val = 0;
 542        else if (mA <= 500)
 543                val = 1;
 544        else if (mA <= 800)
 545                val = 2;
 546        else
 547                val = 3;
 548
 549        return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
 550                        BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
 551}
 552
 553/* get current limit in mA */
 554static int bq2415x_get_current_limit(struct bq2415x_device *bq)
 555{
 556        int ret;
 557
 558        ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
 559                        BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
 560        if (ret < 0)
 561                return ret;
 562        else if (ret == 0)
 563                return 100;
 564        else if (ret == 1)
 565                return 500;
 566        else if (ret == 2)
 567                return 800;
 568        else if (ret == 3)
 569                return 1800;
 570        return -EINVAL;
 571}
 572
 573/* set weak battery voltage in mV */
 574static int bq2415x_set_weak_battery_voltage(struct bq2415x_device *bq, int mV)
 575{
 576        int val;
 577
 578        /* round to 100mV */
 579        if (mV <= 3400 + 50)
 580                val = 0;
 581        else if (mV <= 3500 + 50)
 582                val = 1;
 583        else if (mV <= 3600 + 50)
 584                val = 2;
 585        else
 586                val = 3;
 587
 588        return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
 589                        BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
 590}
 591
 592/* get weak battery voltage in mV */
 593static int bq2415x_get_weak_battery_voltage(struct bq2415x_device *bq)
 594{
 595        int ret;
 596
 597        ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
 598                        BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
 599        if (ret < 0)
 600                return ret;
 601        return 100 * (34 + ret);
 602}
 603
 604/* set battery regulation voltage in mV */
 605static int bq2415x_set_battery_regulation_voltage(struct bq2415x_device *bq,
 606                                                  int mV)
 607{
 608        int val = (mV/10 - 350) / 2;
 609
 610        /*
 611         * According to datasheet, maximum battery regulation voltage is
 612         * 4440mV which is b101111 = 47.
 613         */
 614        if (val < 0)
 615                val = 0;
 616        else if (val > 47)
 617                return -EINVAL;
 618
 619        return bq2415x_i2c_write_mask(bq, BQ2415X_REG_VOLTAGE, val,
 620                        BQ2415X_MASK_VO, BQ2415X_SHIFT_VO);
 621}
 622
 623/* get battery regulation voltage in mV */
 624static int bq2415x_get_battery_regulation_voltage(struct bq2415x_device *bq)
 625{
 626        int ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_VOLTAGE,
 627                        BQ2415X_MASK_VO, BQ2415X_SHIFT_VO);
 628
 629        if (ret < 0)
 630                return ret;
 631        return 10 * (350 + 2*ret);
 632}
 633
 634/* set charge current in mA (platform data must provide resistor sense) */
 635static int bq2415x_set_charge_current(struct bq2415x_device *bq, int mA)
 636{
 637        int val;
 638
 639        if (bq->init_data.resistor_sense <= 0)
 640                return -ENOSYS;
 641
 642        val = (mA * bq->init_data.resistor_sense - 37400) / 6800;
 643        if (val < 0)
 644                val = 0;
 645        else if (val > 7)
 646                val = 7;
 647
 648        return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val,
 649                        BQ2415X_MASK_VI_CHRG | BQ2415X_MASK_RESET,
 650                        BQ2415X_SHIFT_VI_CHRG);
 651}
 652
 653/* get charge current in mA (platform data must provide resistor sense) */
 654static int bq2415x_get_charge_current(struct bq2415x_device *bq)
 655{
 656        int ret;
 657
 658        if (bq->init_data.resistor_sense <= 0)
 659                return -ENOSYS;
 660
 661        ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT,
 662                        BQ2415X_MASK_VI_CHRG, BQ2415X_SHIFT_VI_CHRG);
 663        if (ret < 0)
 664                return ret;
 665        return (37400 + 6800*ret) / bq->init_data.resistor_sense;
 666}
 667
 668/* set termination current in mA (platform data must provide resistor sense) */
 669static int bq2415x_set_termination_current(struct bq2415x_device *bq, int mA)
 670{
 671        int val;
 672
 673        if (bq->init_data.resistor_sense <= 0)
 674                return -ENOSYS;
 675
 676        val = (mA * bq->init_data.resistor_sense - 3400) / 3400;
 677        if (val < 0)
 678                val = 0;
 679        else if (val > 7)
 680                val = 7;
 681
 682        return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val,
 683                        BQ2415X_MASK_VI_TERM | BQ2415X_MASK_RESET,
 684                        BQ2415X_SHIFT_VI_TERM);
 685}
 686
 687/* get termination current in mA (platform data must provide resistor sense) */
 688static int bq2415x_get_termination_current(struct bq2415x_device *bq)
 689{
 690        int ret;
 691
 692        if (bq->init_data.resistor_sense <= 0)
 693                return -ENOSYS;
 694
 695        ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT,
 696                        BQ2415X_MASK_VI_TERM, BQ2415X_SHIFT_VI_TERM);
 697        if (ret < 0)
 698                return ret;
 699        return (3400 + 3400*ret) / bq->init_data.resistor_sense;
 700}
 701
 702/* set default value of property */
 703#define bq2415x_set_default_value(bq, prop) \
 704        do { \
 705                int ret = 0; \
 706                if (bq->init_data.prop != -1) \
 707                        ret = bq2415x_set_##prop(bq, bq->init_data.prop); \
 708                if (ret < 0) \
 709                        return ret; \
 710        } while (0)
 711
 712/* set default values of all properties */
 713static int bq2415x_set_defaults(struct bq2415x_device *bq)
 714{
 715        bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE);
 716        bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE);
 717        bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_DISABLE);
 718
 719        bq2415x_set_default_value(bq, current_limit);
 720        bq2415x_set_default_value(bq, weak_battery_voltage);
 721        bq2415x_set_default_value(bq, battery_regulation_voltage);
 722
 723        if (bq->init_data.resistor_sense > 0) {
 724                bq2415x_set_default_value(bq, charge_current);
 725                bq2415x_set_default_value(bq, termination_current);
 726                bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_ENABLE);
 727        }
 728
 729        bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE);
 730        return 0;
 731}
 732
 733/**** charger mode functions ****/
 734
 735/* set charger mode */
 736static int bq2415x_set_mode(struct bq2415x_device *bq, enum bq2415x_mode mode)
 737{
 738        int ret = 0;
 739        int charger = 0;
 740        int boost = 0;
 741
 742        if (mode == BQ2415X_MODE_BOOST)
 743                boost = 1;
 744        else if (mode != BQ2415X_MODE_OFF)
 745                charger = 1;
 746
 747        if (!charger)
 748                ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE);
 749
 750        if (!boost)
 751                ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE);
 752
 753        if (ret < 0)
 754                return ret;
 755
 756        switch (mode) {
 757        case BQ2415X_MODE_OFF:
 758                dev_dbg(bq->dev, "changing mode to: Offline\n");
 759                ret = bq2415x_set_current_limit(bq, 100);
 760                break;
 761        case BQ2415X_MODE_NONE:
 762                dev_dbg(bq->dev, "changing mode to: N/A\n");
 763                ret = bq2415x_set_current_limit(bq, 100);
 764                break;
 765        case BQ2415X_MODE_HOST_CHARGER:
 766                dev_dbg(bq->dev, "changing mode to: Host/HUB charger\n");
 767                ret = bq2415x_set_current_limit(bq, 500);
 768                break;
 769        case BQ2415X_MODE_DEDICATED_CHARGER:
 770                dev_dbg(bq->dev, "changing mode to: Dedicated charger\n");
 771                ret = bq2415x_set_current_limit(bq, 1800);
 772                break;
 773        case BQ2415X_MODE_BOOST: /* Boost mode */
 774                dev_dbg(bq->dev, "changing mode to: Boost\n");
 775                ret = bq2415x_set_current_limit(bq, 100);
 776                break;
 777        }
 778
 779        if (ret < 0)
 780                return ret;
 781
 782        if (charger)
 783                ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE);
 784        else if (boost)
 785                ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_ENABLE);
 786
 787        if (ret < 0)
 788                return ret;
 789
 790        bq2415x_set_default_value(bq, weak_battery_voltage);
 791        bq2415x_set_default_value(bq, battery_regulation_voltage);
 792
 793        bq->mode = mode;
 794        sysfs_notify(&bq->charger.dev->kobj, NULL, "mode");
 795
 796        return 0;
 797
 798}
 799
 800static int bq2415x_notifier_call(struct notifier_block *nb,
 801                unsigned long val, void *v)
 802{
 803        struct bq2415x_device *bq =
 804                container_of(nb, struct bq2415x_device, nb);
 805        struct power_supply *psy = v;
 806        enum bq2415x_mode mode;
 807        union power_supply_propval prop;
 808        int ret;
 809        int mA;
 810
 811        if (val != PSY_EVENT_PROP_CHANGED)
 812                return NOTIFY_OK;
 813
 814        if (psy != bq->notify_psy)
 815                return NOTIFY_OK;
 816
 817        dev_dbg(bq->dev, "notifier call was called\n");
 818
 819        ret = psy->get_property(psy, POWER_SUPPLY_PROP_CURRENT_MAX, &prop);
 820        if (ret != 0)
 821                return NOTIFY_OK;
 822
 823        mA = prop.intval;
 824
 825        if (mA == 0)
 826                mode = BQ2415X_MODE_OFF;
 827        else if (mA < 500)
 828                mode = BQ2415X_MODE_NONE;
 829        else if (mA < 1800)
 830                mode = BQ2415X_MODE_HOST_CHARGER;
 831        else
 832                mode = BQ2415X_MODE_DEDICATED_CHARGER;
 833
 834        if (bq->reported_mode == mode)
 835                return NOTIFY_OK;
 836
 837        bq->reported_mode = mode;
 838
 839        /* if automode is not enabled do not tell about reported_mode */
 840        if (bq->automode < 1)
 841                return NOTIFY_OK;
 842
 843        schedule_delayed_work(&bq->work, 0);
 844
 845        return NOTIFY_OK;
 846}
 847
 848/**** timer functions ****/
 849
 850/* enable/disable auto resetting chip timer */
 851static void bq2415x_set_autotimer(struct bq2415x_device *bq, int state)
 852{
 853        mutex_lock(&bq2415x_timer_mutex);
 854
 855        if (bq->autotimer == state) {
 856                mutex_unlock(&bq2415x_timer_mutex);
 857                return;
 858        }
 859
 860        bq->autotimer = state;
 861
 862        if (state) {
 863                schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
 864                bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
 865                bq->timer_error = NULL;
 866        } else {
 867                cancel_delayed_work_sync(&bq->work);
 868        }
 869
 870        mutex_unlock(&bq2415x_timer_mutex);
 871}
 872
 873/* called by bq2415x_timer_work on timer error */
 874static void bq2415x_timer_error(struct bq2415x_device *bq, const char *msg)
 875{
 876        bq->timer_error = msg;
 877        sysfs_notify(&bq->charger.dev->kobj, NULL, "timer");
 878        dev_err(bq->dev, "%s\n", msg);
 879        if (bq->automode > 0)
 880                bq->automode = 0;
 881        bq2415x_set_mode(bq, BQ2415X_MODE_OFF);
 882        bq2415x_set_autotimer(bq, 0);
 883}
 884
 885/* delayed work function for auto resetting chip timer */
 886static void bq2415x_timer_work(struct work_struct *work)
 887{
 888        struct bq2415x_device *bq = container_of(work, struct bq2415x_device,
 889                                                 work.work);
 890        int ret;
 891        int error;
 892        int boost;
 893
 894        if (bq->automode > 0 && (bq->reported_mode != bq->mode)) {
 895                sysfs_notify(&bq->charger.dev->kobj, NULL, "reported_mode");
 896                bq2415x_set_mode(bq, bq->reported_mode);
 897        }
 898
 899        if (!bq->autotimer)
 900                return;
 901
 902        ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
 903        if (ret < 0) {
 904                bq2415x_timer_error(bq, "Resetting timer failed");
 905                return;
 906        }
 907
 908        boost = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_STATUS);
 909        if (boost < 0) {
 910                bq2415x_timer_error(bq, "Unknown error");
 911                return;
 912        }
 913
 914        error = bq2415x_exec_command(bq, BQ2415X_FAULT_STATUS);
 915        if (error < 0) {
 916                bq2415x_timer_error(bq, "Unknown error");
 917                return;
 918        }
 919
 920        if (boost) {
 921                switch (error) {
 922                /* Non fatal errors, chip is OK */
 923                case 0: /* No error */
 924                        break;
 925                case 6: /* Timer expired */
 926                        dev_err(bq->dev, "Timer expired\n");
 927                        break;
 928                case 3: /* Battery voltage too low */
 929                        dev_err(bq->dev, "Battery voltage to low\n");
 930                        break;
 931
 932                /* Fatal errors, disable and reset chip */
 933                case 1: /* Overvoltage protection (chip fried) */
 934                        bq2415x_timer_error(bq,
 935                                "Overvoltage protection (chip fried)");
 936                        return;
 937                case 2: /* Overload */
 938                        bq2415x_timer_error(bq, "Overload");
 939                        return;
 940                case 4: /* Battery overvoltage protection */
 941                        bq2415x_timer_error(bq,
 942                                "Battery overvoltage protection");
 943                        return;
 944                case 5: /* Thermal shutdown (too hot) */
 945                        bq2415x_timer_error(bq,
 946                                        "Thermal shutdown (too hot)");
 947                        return;
 948                case 7: /* N/A */
 949                        bq2415x_timer_error(bq, "Unknown error");
 950                        return;
 951                }
 952        } else {
 953                switch (error) {
 954                /* Non fatal errors, chip is OK */
 955                case 0: /* No error */
 956                        break;
 957                case 2: /* Sleep mode */
 958                        dev_err(bq->dev, "Sleep mode\n");
 959                        break;
 960                case 3: /* Poor input source */
 961                        dev_err(bq->dev, "Poor input source\n");
 962                        break;
 963                case 6: /* Timer expired */
 964                        dev_err(bq->dev, "Timer expired\n");
 965                        break;
 966                case 7: /* No battery */
 967                        dev_err(bq->dev, "No battery\n");
 968                        break;
 969
 970                /* Fatal errors, disable and reset chip */
 971                case 1: /* Overvoltage protection (chip fried) */
 972                        bq2415x_timer_error(bq,
 973                                "Overvoltage protection (chip fried)");
 974                        return;
 975                case 4: /* Battery overvoltage protection */
 976                        bq2415x_timer_error(bq,
 977                                "Battery overvoltage protection");
 978                        return;
 979                case 5: /* Thermal shutdown (too hot) */
 980                        bq2415x_timer_error(bq,
 981                                "Thermal shutdown (too hot)");
 982                        return;
 983                }
 984        }
 985
 986        schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
 987}
 988
 989/**** power supply interface code ****/
 990
 991static enum power_supply_property bq2415x_power_supply_props[] = {
 992        /* TODO: maybe add more power supply properties */
 993        POWER_SUPPLY_PROP_STATUS,
 994        POWER_SUPPLY_PROP_MODEL_NAME,
 995};
 996
 997static int bq2415x_power_supply_get_property(struct power_supply *psy,
 998                                             enum power_supply_property psp,
 999                                             union power_supply_propval *val)
1000{
1001        struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1002                                                 charger);
1003        int ret;
1004
1005        switch (psp) {
1006        case POWER_SUPPLY_PROP_STATUS:
1007                ret = bq2415x_exec_command(bq, BQ2415X_CHARGE_STATUS);
1008                if (ret < 0)
1009                        return ret;
1010                else if (ret == 0) /* Ready */
1011                        val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1012                else if (ret == 1) /* Charge in progress */
1013                        val->intval = POWER_SUPPLY_STATUS_CHARGING;
1014                else if (ret == 2) /* Charge done */
1015                        val->intval = POWER_SUPPLY_STATUS_FULL;
1016                else
1017                        val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
1018                break;
1019        case POWER_SUPPLY_PROP_MODEL_NAME:
1020                val->strval = bq->model;
1021                break;
1022        default:
1023                return -EINVAL;
1024        }
1025        return 0;
1026}
1027
1028static int bq2415x_power_supply_init(struct bq2415x_device *bq)
1029{
1030        int ret;
1031        int chip;
1032        char revstr[8];
1033
1034        bq->charger.name = bq->name;
1035        bq->charger.type = POWER_SUPPLY_TYPE_USB;
1036        bq->charger.properties = bq2415x_power_supply_props;
1037        bq->charger.num_properties = ARRAY_SIZE(bq2415x_power_supply_props);
1038        bq->charger.get_property = bq2415x_power_supply_get_property;
1039
1040        ret = bq2415x_detect_chip(bq);
1041        if (ret < 0)
1042                chip = BQUNKNOWN;
1043        else
1044                chip = ret;
1045
1046        ret = bq2415x_detect_revision(bq);
1047        if (ret < 0)
1048                strcpy(revstr, "unknown");
1049        else
1050                sprintf(revstr, "1.%d", ret);
1051
1052        bq->model = kasprintf(GFP_KERNEL,
1053                                "chip %s, revision %s, vender code %.3d",
1054                                bq2415x_chip_name[chip], revstr,
1055                                bq2415x_get_vender_code(bq));
1056        if (!bq->model) {
1057                dev_err(bq->dev, "failed to allocate model name\n");
1058                return -ENOMEM;
1059        }
1060
1061        ret = power_supply_register(bq->dev, &bq->charger);
1062        if (ret) {
1063                kfree(bq->model);
1064                return ret;
1065        }
1066
1067        return 0;
1068}
1069
1070static void bq2415x_power_supply_exit(struct bq2415x_device *bq)
1071{
1072        bq->autotimer = 0;
1073        if (bq->automode > 0)
1074                bq->automode = 0;
1075        cancel_delayed_work_sync(&bq->work);
1076        power_supply_unregister(&bq->charger);
1077        kfree(bq->model);
1078}
1079
1080/**** additional sysfs entries for power supply interface ****/
1081
1082/* show *_status entries */
1083static ssize_t bq2415x_sysfs_show_status(struct device *dev,
1084                                         struct device_attribute *attr,
1085                                         char *buf)
1086{
1087        struct power_supply *psy = dev_get_drvdata(dev);
1088        struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1089                                                charger);
1090        enum bq2415x_command command;
1091        int ret;
1092
1093        if (strcmp(attr->attr.name, "otg_status") == 0)
1094                command = BQ2415X_OTG_STATUS;
1095        else if (strcmp(attr->attr.name, "charge_status") == 0)
1096                command = BQ2415X_CHARGE_STATUS;
1097        else if (strcmp(attr->attr.name, "boost_status") == 0)
1098                command = BQ2415X_BOOST_STATUS;
1099        else if (strcmp(attr->attr.name, "fault_status") == 0)
1100                command = BQ2415X_FAULT_STATUS;
1101        else
1102                return -EINVAL;
1103
1104        ret = bq2415x_exec_command(bq, command);
1105        if (ret < 0)
1106                return ret;
1107        return sprintf(buf, "%d\n", ret);
1108}
1109
1110/*
1111 * set timer entry:
1112 *    auto - enable auto mode
1113 *    off - disable auto mode
1114 *    (other values) - reset chip timer
1115 */
1116static ssize_t bq2415x_sysfs_set_timer(struct device *dev,
1117                                       struct device_attribute *attr,
1118                                       const char *buf,
1119                                       size_t count)
1120{
1121        struct power_supply *psy = dev_get_drvdata(dev);
1122        struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1123                                                charger);
1124        int ret = 0;
1125
1126        if (strncmp(buf, "auto", 4) == 0)
1127                bq2415x_set_autotimer(bq, 1);
1128        else if (strncmp(buf, "off", 3) == 0)
1129                bq2415x_set_autotimer(bq, 0);
1130        else
1131                ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
1132
1133        if (ret < 0)
1134                return ret;
1135        return count;
1136}
1137
1138/* show timer entry (auto or off) */
1139static ssize_t bq2415x_sysfs_show_timer(struct device *dev,
1140                                        struct device_attribute *attr,
1141                                        char *buf)
1142{
1143        struct power_supply *psy = dev_get_drvdata(dev);
1144        struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1145                                                 charger);
1146
1147        if (bq->timer_error)
1148                return sprintf(buf, "%s\n", bq->timer_error);
1149
1150        if (bq->autotimer)
1151                return sprintf(buf, "auto\n");
1152        return sprintf(buf, "off\n");
1153}
1154
1155/*
1156 * set mode entry:
1157 *    auto - if automode is supported, enable it and set mode to reported
1158 *    none - disable charger and boost mode
1159 *    host - charging mode for host/hub chargers (current limit 500mA)
1160 *    dedicated - charging mode for dedicated chargers (unlimited current limit)
1161 *    boost - disable charger and enable boost mode
1162 */
1163static ssize_t bq2415x_sysfs_set_mode(struct device *dev,
1164                                      struct device_attribute *attr,
1165                                      const char *buf,
1166                                      size_t count)
1167{
1168        struct power_supply *psy = dev_get_drvdata(dev);
1169        struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1170                                                 charger);
1171        enum bq2415x_mode mode;
1172        int ret = 0;
1173
1174        if (strncmp(buf, "auto", 4) == 0) {
1175                if (bq->automode < 0)
1176                        return -ENOSYS;
1177                bq->automode = 1;
1178                mode = bq->reported_mode;
1179        } else if (strncmp(buf, "off", 3) == 0) {
1180                if (bq->automode > 0)
1181                        bq->automode = 0;
1182                mode = BQ2415X_MODE_OFF;
1183        } else if (strncmp(buf, "none", 4) == 0) {
1184                if (bq->automode > 0)
1185                        bq->automode = 0;
1186                mode = BQ2415X_MODE_NONE;
1187        } else if (strncmp(buf, "host", 4) == 0) {
1188                if (bq->automode > 0)
1189                        bq->automode = 0;
1190                mode = BQ2415X_MODE_HOST_CHARGER;
1191        } else if (strncmp(buf, "dedicated", 9) == 0) {
1192                if (bq->automode > 0)
1193                        bq->automode = 0;
1194                mode = BQ2415X_MODE_DEDICATED_CHARGER;
1195        } else if (strncmp(buf, "boost", 5) == 0) {
1196                if (bq->automode > 0)
1197                        bq->automode = 0;
1198                mode = BQ2415X_MODE_BOOST;
1199        } else if (strncmp(buf, "reset", 5) == 0) {
1200                bq2415x_reset_chip(bq);
1201                bq2415x_set_defaults(bq);
1202                if (bq->automode <= 0)
1203                        return count;
1204                bq->automode = 1;
1205                mode = bq->reported_mode;
1206        } else {
1207                return -EINVAL;
1208        }
1209
1210        ret = bq2415x_set_mode(bq, mode);
1211        if (ret < 0)
1212                return ret;
1213        return count;
1214}
1215
1216/* show mode entry (auto, none, host, dedicated or boost) */
1217static ssize_t bq2415x_sysfs_show_mode(struct device *dev,
1218                                       struct device_attribute *attr,
1219                                       char *buf)
1220{
1221        struct power_supply *psy = dev_get_drvdata(dev);
1222        struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1223                                                charger);
1224        ssize_t ret = 0;
1225
1226        if (bq->automode > 0)
1227                ret += sprintf(buf+ret, "auto (");
1228
1229        switch (bq->mode) {
1230        case BQ2415X_MODE_OFF:
1231                ret += sprintf(buf+ret, "off");
1232                break;
1233        case BQ2415X_MODE_NONE:
1234                ret += sprintf(buf+ret, "none");
1235                break;
1236        case BQ2415X_MODE_HOST_CHARGER:
1237                ret += sprintf(buf+ret, "host");
1238                break;
1239        case BQ2415X_MODE_DEDICATED_CHARGER:
1240                ret += sprintf(buf+ret, "dedicated");
1241                break;
1242        case BQ2415X_MODE_BOOST:
1243                ret += sprintf(buf+ret, "boost");
1244                break;
1245        }
1246
1247        if (bq->automode > 0)
1248                ret += sprintf(buf+ret, ")");
1249
1250        ret += sprintf(buf+ret, "\n");
1251        return ret;
1252}
1253
1254/* show reported_mode entry (none, host, dedicated or boost) */
1255static ssize_t bq2415x_sysfs_show_reported_mode(struct device *dev,
1256                                                struct device_attribute *attr,
1257                                                char *buf)
1258{
1259        struct power_supply *psy = dev_get_drvdata(dev);
1260        struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1261                                                 charger);
1262
1263        if (bq->automode < 0)
1264                return -EINVAL;
1265
1266        switch (bq->reported_mode) {
1267        case BQ2415X_MODE_OFF:
1268                return sprintf(buf, "off\n");
1269        case BQ2415X_MODE_NONE:
1270                return sprintf(buf, "none\n");
1271        case BQ2415X_MODE_HOST_CHARGER:
1272                return sprintf(buf, "host\n");
1273        case BQ2415X_MODE_DEDICATED_CHARGER:
1274                return sprintf(buf, "dedicated\n");
1275        case BQ2415X_MODE_BOOST:
1276                return sprintf(buf, "boost\n");
1277        }
1278
1279        return -EINVAL;
1280}
1281
1282/* directly set raw value to chip register, format: 'register value' */
1283static ssize_t bq2415x_sysfs_set_registers(struct device *dev,
1284                                           struct device_attribute *attr,
1285                                           const char *buf,
1286                                           size_t count)
1287{
1288        struct power_supply *psy = dev_get_drvdata(dev);
1289        struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1290                                                 charger);
1291        ssize_t ret = 0;
1292        unsigned int reg;
1293        unsigned int val;
1294
1295        if (sscanf(buf, "%x %x", &reg, &val) != 2)
1296                return -EINVAL;
1297
1298        if (reg > 4 || val > 255)
1299                return -EINVAL;
1300
1301        ret = bq2415x_i2c_write(bq, reg, val);
1302        if (ret < 0)
1303                return ret;
1304        return count;
1305}
1306
1307/* print value of chip register, format: 'register=value' */
1308static ssize_t bq2415x_sysfs_print_reg(struct bq2415x_device *bq,
1309                                       u8 reg,
1310                                       char *buf)
1311{
1312        int ret = bq2415x_i2c_read(bq, reg);
1313
1314        if (ret < 0)
1315                return sprintf(buf, "%#.2x=error %d\n", reg, ret);
1316        return sprintf(buf, "%#.2x=%#.2x\n", reg, ret);
1317}
1318
1319/* show all raw values of chip register, format per line: 'register=value' */
1320static ssize_t bq2415x_sysfs_show_registers(struct device *dev,
1321                                            struct device_attribute *attr,
1322                                            char *buf)
1323{
1324        struct power_supply *psy = dev_get_drvdata(dev);
1325        struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1326                                                 charger);
1327        ssize_t ret = 0;
1328
1329        ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_STATUS, buf+ret);
1330        ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CONTROL, buf+ret);
1331        ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VOLTAGE, buf+ret);
1332        ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VENDER, buf+ret);
1333        ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CURRENT, buf+ret);
1334        return ret;
1335}
1336
1337/* set current and voltage limit entries (in mA or mV) */
1338static ssize_t bq2415x_sysfs_set_limit(struct device *dev,
1339                                       struct device_attribute *attr,
1340                                       const char *buf,
1341                                       size_t count)
1342{
1343        struct power_supply *psy = dev_get_drvdata(dev);
1344        struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1345                                                 charger);
1346        long val;
1347        int ret;
1348
1349        if (kstrtol(buf, 10, &val) < 0)
1350                return -EINVAL;
1351
1352        if (strcmp(attr->attr.name, "current_limit") == 0)
1353                ret = bq2415x_set_current_limit(bq, val);
1354        else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0)
1355                ret = bq2415x_set_weak_battery_voltage(bq, val);
1356        else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0)
1357                ret = bq2415x_set_battery_regulation_voltage(bq, val);
1358        else if (strcmp(attr->attr.name, "charge_current") == 0)
1359                ret = bq2415x_set_charge_current(bq, val);
1360        else if (strcmp(attr->attr.name, "termination_current") == 0)
1361                ret = bq2415x_set_termination_current(bq, val);
1362        else
1363                return -EINVAL;
1364
1365        if (ret < 0)
1366                return ret;
1367        return count;
1368}
1369
1370/* show current and voltage limit entries (in mA or mV) */
1371static ssize_t bq2415x_sysfs_show_limit(struct device *dev,
1372                                        struct device_attribute *attr,
1373                                        char *buf)
1374{
1375        struct power_supply *psy = dev_get_drvdata(dev);
1376        struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1377                                                 charger);
1378        int ret;
1379
1380        if (strcmp(attr->attr.name, "current_limit") == 0)
1381                ret = bq2415x_get_current_limit(bq);
1382        else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0)
1383                ret = bq2415x_get_weak_battery_voltage(bq);
1384        else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0)
1385                ret = bq2415x_get_battery_regulation_voltage(bq);
1386        else if (strcmp(attr->attr.name, "charge_current") == 0)
1387                ret = bq2415x_get_charge_current(bq);
1388        else if (strcmp(attr->attr.name, "termination_current") == 0)
1389                ret = bq2415x_get_termination_current(bq);
1390        else
1391                return -EINVAL;
1392
1393        if (ret < 0)
1394                return ret;
1395        return sprintf(buf, "%d\n", ret);
1396}
1397
1398/* set *_enable entries */
1399static ssize_t bq2415x_sysfs_set_enable(struct device *dev,
1400                                        struct device_attribute *attr,
1401                                        const char *buf,
1402                                        size_t count)
1403{
1404        struct power_supply *psy = dev_get_drvdata(dev);
1405        struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1406                                                 charger);
1407        enum bq2415x_command command;
1408        long val;
1409        int ret;
1410
1411        if (kstrtol(buf, 10, &val) < 0)
1412                return -EINVAL;
1413
1414        if (strcmp(attr->attr.name, "charge_termination_enable") == 0)
1415                command = val ? BQ2415X_CHARGE_TERMINATION_ENABLE :
1416                        BQ2415X_CHARGE_TERMINATION_DISABLE;
1417        else if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
1418                command = val ? BQ2415X_HIGH_IMPEDANCE_ENABLE :
1419                        BQ2415X_HIGH_IMPEDANCE_DISABLE;
1420        else if (strcmp(attr->attr.name, "otg_pin_enable") == 0)
1421                command = val ? BQ2415X_OTG_PIN_ENABLE :
1422                        BQ2415X_OTG_PIN_DISABLE;
1423        else if (strcmp(attr->attr.name, "stat_pin_enable") == 0)
1424                command = val ? BQ2415X_STAT_PIN_ENABLE :
1425                        BQ2415X_STAT_PIN_DISABLE;
1426        else
1427                return -EINVAL;
1428
1429        ret = bq2415x_exec_command(bq, command);
1430        if (ret < 0)
1431                return ret;
1432        return count;
1433}
1434
1435/* show *_enable entries */
1436static ssize_t bq2415x_sysfs_show_enable(struct device *dev,
1437                                         struct device_attribute *attr,
1438                                         char *buf)
1439{
1440        struct power_supply *psy = dev_get_drvdata(dev);
1441        struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1442                                                 charger);
1443        enum bq2415x_command command;
1444        int ret;
1445
1446        if (strcmp(attr->attr.name, "charge_termination_enable") == 0)
1447                command = BQ2415X_CHARGE_TERMINATION_STATUS;
1448        else if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
1449                command = BQ2415X_HIGH_IMPEDANCE_STATUS;
1450        else if (strcmp(attr->attr.name, "otg_pin_enable") == 0)
1451                command = BQ2415X_OTG_PIN_STATUS;
1452        else if (strcmp(attr->attr.name, "stat_pin_enable") == 0)
1453                command = BQ2415X_STAT_PIN_STATUS;
1454        else
1455                return -EINVAL;
1456
1457        ret = bq2415x_exec_command(bq, command);
1458        if (ret < 0)
1459                return ret;
1460        return sprintf(buf, "%d\n", ret);
1461}
1462
1463static DEVICE_ATTR(current_limit, S_IWUSR | S_IRUGO,
1464                bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1465static DEVICE_ATTR(weak_battery_voltage, S_IWUSR | S_IRUGO,
1466                bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1467static DEVICE_ATTR(battery_regulation_voltage, S_IWUSR | S_IRUGO,
1468                bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1469static DEVICE_ATTR(charge_current, S_IWUSR | S_IRUGO,
1470                bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1471static DEVICE_ATTR(termination_current, S_IWUSR | S_IRUGO,
1472                bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1473
1474static DEVICE_ATTR(charge_termination_enable, S_IWUSR | S_IRUGO,
1475                bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1476static DEVICE_ATTR(high_impedance_enable, S_IWUSR | S_IRUGO,
1477                bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1478static DEVICE_ATTR(otg_pin_enable, S_IWUSR | S_IRUGO,
1479                bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1480static DEVICE_ATTR(stat_pin_enable, S_IWUSR | S_IRUGO,
1481                bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1482
1483static DEVICE_ATTR(reported_mode, S_IRUGO,
1484                bq2415x_sysfs_show_reported_mode, NULL);
1485static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO,
1486                bq2415x_sysfs_show_mode, bq2415x_sysfs_set_mode);
1487static DEVICE_ATTR(timer, S_IWUSR | S_IRUGO,
1488                bq2415x_sysfs_show_timer, bq2415x_sysfs_set_timer);
1489
1490static DEVICE_ATTR(registers, S_IWUSR | S_IRUGO,
1491                bq2415x_sysfs_show_registers, bq2415x_sysfs_set_registers);
1492
1493static DEVICE_ATTR(otg_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1494static DEVICE_ATTR(charge_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1495static DEVICE_ATTR(boost_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1496static DEVICE_ATTR(fault_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1497
1498static struct attribute *bq2415x_sysfs_attributes[] = {
1499        /*
1500         * TODO: some (appropriate) of these attrs should be switched to
1501         * use power supply class props.
1502         */
1503        &dev_attr_current_limit.attr,
1504        &dev_attr_weak_battery_voltage.attr,
1505        &dev_attr_battery_regulation_voltage.attr,
1506        &dev_attr_charge_current.attr,
1507        &dev_attr_termination_current.attr,
1508
1509        &dev_attr_charge_termination_enable.attr,
1510        &dev_attr_high_impedance_enable.attr,
1511        &dev_attr_otg_pin_enable.attr,
1512        &dev_attr_stat_pin_enable.attr,
1513
1514        &dev_attr_reported_mode.attr,
1515        &dev_attr_mode.attr,
1516        &dev_attr_timer.attr,
1517
1518        &dev_attr_registers.attr,
1519
1520        &dev_attr_otg_status.attr,
1521        &dev_attr_charge_status.attr,
1522        &dev_attr_boost_status.attr,
1523        &dev_attr_fault_status.attr,
1524        NULL,
1525};
1526
1527static const struct attribute_group bq2415x_sysfs_attr_group = {
1528        .attrs = bq2415x_sysfs_attributes,
1529};
1530
1531static int bq2415x_sysfs_init(struct bq2415x_device *bq)
1532{
1533        return sysfs_create_group(&bq->charger.dev->kobj,
1534                        &bq2415x_sysfs_attr_group);
1535}
1536
1537static void bq2415x_sysfs_exit(struct bq2415x_device *bq)
1538{
1539        sysfs_remove_group(&bq->charger.dev->kobj, &bq2415x_sysfs_attr_group);
1540}
1541
1542/* main bq2415x probe function */
1543static int bq2415x_probe(struct i2c_client *client,
1544                         const struct i2c_device_id *id)
1545{
1546        int ret;
1547        int num;
1548        char *name;
1549        struct bq2415x_device *bq;
1550        struct device_node *np = client->dev.of_node;
1551        struct bq2415x_platform_data *pdata = client->dev.platform_data;
1552
1553        if (!np && !pdata) {
1554                dev_err(&client->dev, "platform data missing\n");
1555                return -ENODEV;
1556        }
1557
1558        /* Get new ID for the new device */
1559        mutex_lock(&bq2415x_id_mutex);
1560        num = idr_alloc(&bq2415x_id, client, 0, 0, GFP_KERNEL);
1561        mutex_unlock(&bq2415x_id_mutex);
1562        if (num < 0)
1563                return num;
1564
1565        name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
1566        if (!name) {
1567                dev_err(&client->dev, "failed to allocate device name\n");
1568                ret = -ENOMEM;
1569                goto error_1;
1570        }
1571
1572        bq = devm_kzalloc(&client->dev, sizeof(*bq), GFP_KERNEL);
1573        if (!bq) {
1574                dev_err(&client->dev, "failed to allocate device data\n");
1575                ret = -ENOMEM;
1576                goto error_2;
1577        }
1578
1579        if (np) {
1580                bq->notify_psy = power_supply_get_by_phandle(np, "ti,usb-charger-detection");
1581
1582                if (!bq->notify_psy)
1583                        return -EPROBE_DEFER;
1584        }
1585        else if (pdata->notify_device)
1586                bq->notify_psy = power_supply_get_by_name(pdata->notify_device);
1587        else
1588                bq->notify_psy = NULL;
1589
1590        i2c_set_clientdata(client, bq);
1591
1592        bq->id = num;
1593        bq->dev = &client->dev;
1594        bq->chip = id->driver_data;
1595        bq->name = name;
1596        bq->mode = BQ2415X_MODE_OFF;
1597        bq->reported_mode = BQ2415X_MODE_OFF;
1598        bq->autotimer = 0;
1599        bq->automode = 0;
1600
1601        if (np) {
1602                ret = of_property_read_u32(np, "ti,current-limit",
1603                                &bq->init_data.current_limit);
1604                if (ret)
1605                        return ret;
1606                ret = of_property_read_u32(np, "ti,weak-battery-voltage",
1607                                &bq->init_data.weak_battery_voltage);
1608                if (ret)
1609                        return ret;
1610                ret = of_property_read_u32(np, "ti,battery-regulation-voltage",
1611                                &bq->init_data.battery_regulation_voltage);
1612                if (ret)
1613                        return ret;
1614                ret = of_property_read_u32(np, "ti,charge-current",
1615                                &bq->init_data.charge_current);
1616                if (ret)
1617                        return ret;
1618                ret = of_property_read_u32(np, "ti,termination-current",
1619                                &bq->init_data.termination_current);
1620                if (ret)
1621                        return ret;
1622                ret = of_property_read_u32(np, "ti,resistor-sense",
1623                                &bq->init_data.resistor_sense);
1624                if (ret)
1625                        return ret;
1626        } else {
1627                memcpy(&bq->init_data, pdata, sizeof(bq->init_data));
1628        }
1629
1630        bq2415x_reset_chip(bq);
1631
1632        ret = bq2415x_power_supply_init(bq);
1633        if (ret) {
1634                dev_err(bq->dev, "failed to register power supply: %d\n", ret);
1635                goto error_2;
1636        }
1637
1638        ret = bq2415x_sysfs_init(bq);
1639        if (ret) {
1640                dev_err(bq->dev, "failed to create sysfs entries: %d\n", ret);
1641                goto error_3;
1642        }
1643
1644        ret = bq2415x_set_defaults(bq);
1645        if (ret) {
1646                dev_err(bq->dev, "failed to set default values: %d\n", ret);
1647                goto error_4;
1648        }
1649
1650        if (bq->notify_psy) {
1651                bq->nb.notifier_call = bq2415x_notifier_call;
1652                ret = power_supply_reg_notifier(&bq->nb);
1653                if (ret) {
1654                        dev_err(bq->dev, "failed to reg notifier: %d\n", ret);
1655                        goto error_5;
1656                }
1657
1658                /* Query for initial reported_mode and set it */
1659                bq2415x_notifier_call(&bq->nb, PSY_EVENT_PROP_CHANGED, bq->notify_psy);
1660                bq2415x_set_mode(bq, bq->reported_mode);
1661
1662                bq->automode = 1;
1663                dev_info(bq->dev, "automode enabled\n");
1664        } else {
1665                bq->automode = -1;
1666                dev_info(bq->dev, "automode not supported\n");
1667        }
1668
1669        INIT_DELAYED_WORK(&bq->work, bq2415x_timer_work);
1670        bq2415x_set_autotimer(bq, 1);
1671
1672        dev_info(bq->dev, "driver registered\n");
1673        return 0;
1674
1675error_5:
1676error_4:
1677        bq2415x_sysfs_exit(bq);
1678error_3:
1679        bq2415x_power_supply_exit(bq);
1680error_2:
1681        kfree(name);
1682error_1:
1683        mutex_lock(&bq2415x_id_mutex);
1684        idr_remove(&bq2415x_id, num);
1685        mutex_unlock(&bq2415x_id_mutex);
1686
1687        return ret;
1688}
1689
1690/* main bq2415x remove function */
1691
1692static int bq2415x_remove(struct i2c_client *client)
1693{
1694        struct bq2415x_device *bq = i2c_get_clientdata(client);
1695
1696        if (bq->notify_psy)
1697                power_supply_unreg_notifier(&bq->nb);
1698
1699        bq2415x_sysfs_exit(bq);
1700        bq2415x_power_supply_exit(bq);
1701
1702        bq2415x_reset_chip(bq);
1703
1704        mutex_lock(&bq2415x_id_mutex);
1705        idr_remove(&bq2415x_id, bq->id);
1706        mutex_unlock(&bq2415x_id_mutex);
1707
1708        dev_info(bq->dev, "driver unregistered\n");
1709
1710        kfree(bq->name);
1711
1712        return 0;
1713}
1714
1715static const struct i2c_device_id bq2415x_i2c_id_table[] = {
1716        { "bq2415x", BQUNKNOWN },
1717        { "bq24150", BQ24150 },
1718        { "bq24150a", BQ24150A },
1719        { "bq24151", BQ24151 },
1720        { "bq24151a", BQ24151A },
1721        { "bq24152", BQ24152 },
1722        { "bq24153", BQ24153 },
1723        { "bq24153a", BQ24153A },
1724        { "bq24155", BQ24155 },
1725        { "bq24156", BQ24156 },
1726        { "bq24156a", BQ24156A },
1727        { "bq24158", BQ24158 },
1728        {},
1729};
1730MODULE_DEVICE_TABLE(i2c, bq2415x_i2c_id_table);
1731
1732static struct i2c_driver bq2415x_driver = {
1733        .driver = {
1734                .name = "bq2415x-charger",
1735        },
1736        .probe = bq2415x_probe,
1737        .remove = bq2415x_remove,
1738        .id_table = bq2415x_i2c_id_table,
1739};
1740module_i2c_driver(bq2415x_driver);
1741
1742MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
1743MODULE_DESCRIPTION("bq2415x charger driver");
1744MODULE_LICENSE("GPL");
1745