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