linux/drivers/power/supply/rt9455_charger.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Driver for Richtek RT9455WSC battery charger.
   4 *
   5 * Copyright (C) 2015 Intel Corporation
   6 */
   7
   8#include <linux/module.h>
   9#include <linux/interrupt.h>
  10#include <linux/delay.h>
  11#include <linux/of_irq.h>
  12#include <linux/of_device.h>
  13#include <linux/pm_runtime.h>
  14#include <linux/power_supply.h>
  15#include <linux/i2c.h>
  16#include <linux/acpi.h>
  17#include <linux/usb/phy.h>
  18#include <linux/regmap.h>
  19
  20#define RT9455_MANUFACTURER                     "Richtek"
  21#define RT9455_MODEL_NAME                       "RT9455"
  22#define RT9455_DRIVER_NAME                      "rt9455-charger"
  23
  24#define RT9455_IRQ_NAME                         "interrupt"
  25
  26#define RT9455_PWR_RDY_DELAY                    1 /* 1 second */
  27#define RT9455_MAX_CHARGING_TIME                21600 /* 6 hrs */
  28#define RT9455_BATT_PRESENCE_DELAY              60 /* 60 seconds */
  29
  30#define RT9455_CHARGE_MODE                      0x00
  31#define RT9455_BOOST_MODE                       0x01
  32
  33#define RT9455_FAULT                            0x03
  34
  35#define RT9455_IAICR_100MA                      0x00
  36#define RT9455_IAICR_500MA                      0x01
  37#define RT9455_IAICR_NO_LIMIT                   0x03
  38
  39#define RT9455_CHARGE_DISABLE                   0x00
  40#define RT9455_CHARGE_ENABLE                    0x01
  41
  42#define RT9455_PWR_FAULT                        0x00
  43#define RT9455_PWR_GOOD                         0x01
  44
  45#define RT9455_REG_CTRL1                        0x00 /* CTRL1 reg address */
  46#define RT9455_REG_CTRL2                        0x01 /* CTRL2 reg address */
  47#define RT9455_REG_CTRL3                        0x02 /* CTRL3 reg address */
  48#define RT9455_REG_DEV_ID                       0x03 /* DEV_ID reg address */
  49#define RT9455_REG_CTRL4                        0x04 /* CTRL4 reg address */
  50#define RT9455_REG_CTRL5                        0x05 /* CTRL5 reg address */
  51#define RT9455_REG_CTRL6                        0x06 /* CTRL6 reg address */
  52#define RT9455_REG_CTRL7                        0x07 /* CTRL7 reg address */
  53#define RT9455_REG_IRQ1                         0x08 /* IRQ1 reg address */
  54#define RT9455_REG_IRQ2                         0x09 /* IRQ2 reg address */
  55#define RT9455_REG_IRQ3                         0x0A /* IRQ3 reg address */
  56#define RT9455_REG_MASK1                        0x0B /* MASK1 reg address */
  57#define RT9455_REG_MASK2                        0x0C /* MASK2 reg address */
  58#define RT9455_REG_MASK3                        0x0D /* MASK3 reg address */
  59
  60enum rt9455_fields {
  61        F_STAT, F_BOOST, F_PWR_RDY, F_OTG_PIN_POLARITY, /* CTRL1 reg fields */
  62
  63        F_IAICR, F_TE_SHDN_EN, F_HIGHER_OCP, F_TE, F_IAICR_INT, F_HIZ,
  64        F_OPA_MODE, /* CTRL2 reg fields */
  65
  66        F_VOREG, F_OTG_PL, F_OTG_EN, /* CTRL3 reg fields */
  67
  68        F_VENDOR_ID, F_CHIP_REV, /* DEV_ID reg fields */
  69
  70        F_RST, /* CTRL4 reg fields */
  71
  72        F_TMR_EN, F_MIVR, F_IPREC, F_IEOC_PERCENTAGE, /* CTRL5 reg fields*/
  73
  74        F_IAICR_SEL, F_ICHRG, F_VPREC, /* CTRL6 reg fields */
  75
  76        F_BATD_EN, F_CHG_EN, F_VMREG, /* CTRL7 reg fields */
  77
  78        F_TSDI, F_VINOVPI, F_BATAB, /* IRQ1 reg fields */
  79
  80        F_CHRVPI, F_CHBATOVI, F_CHTERMI, F_CHRCHGI, F_CH32MI, F_CHTREGI,
  81        F_CHMIVRI, /* IRQ2 reg fields */
  82
  83        F_BSTBUSOVI, F_BSTOLI, F_BSTLOWVI, F_BST32SI, /* IRQ3 reg fields */
  84
  85        F_TSDM, F_VINOVPIM, F_BATABM, /* MASK1 reg fields */
  86
  87        F_CHRVPIM, F_CHBATOVIM, F_CHTERMIM, F_CHRCHGIM, F_CH32MIM, F_CHTREGIM,
  88        F_CHMIVRIM, /* MASK2 reg fields */
  89
  90        F_BSTVINOVIM, F_BSTOLIM, F_BSTLOWVIM, F_BST32SIM, /* MASK3 reg fields */
  91
  92        F_MAX_FIELDS
  93};
  94
  95static const struct reg_field rt9455_reg_fields[] = {
  96        [F_STAT]                = REG_FIELD(RT9455_REG_CTRL1, 4, 5),
  97        [F_BOOST]               = REG_FIELD(RT9455_REG_CTRL1, 3, 3),
  98        [F_PWR_RDY]             = REG_FIELD(RT9455_REG_CTRL1, 2, 2),
  99        [F_OTG_PIN_POLARITY]    = REG_FIELD(RT9455_REG_CTRL1, 1, 1),
 100
 101        [F_IAICR]               = REG_FIELD(RT9455_REG_CTRL2, 6, 7),
 102        [F_TE_SHDN_EN]          = REG_FIELD(RT9455_REG_CTRL2, 5, 5),
 103        [F_HIGHER_OCP]          = REG_FIELD(RT9455_REG_CTRL2, 4, 4),
 104        [F_TE]                  = REG_FIELD(RT9455_REG_CTRL2, 3, 3),
 105        [F_IAICR_INT]           = REG_FIELD(RT9455_REG_CTRL2, 2, 2),
 106        [F_HIZ]                 = REG_FIELD(RT9455_REG_CTRL2, 1, 1),
 107        [F_OPA_MODE]            = REG_FIELD(RT9455_REG_CTRL2, 0, 0),
 108
 109        [F_VOREG]               = REG_FIELD(RT9455_REG_CTRL3, 2, 7),
 110        [F_OTG_PL]              = REG_FIELD(RT9455_REG_CTRL3, 1, 1),
 111        [F_OTG_EN]              = REG_FIELD(RT9455_REG_CTRL3, 0, 0),
 112
 113        [F_VENDOR_ID]           = REG_FIELD(RT9455_REG_DEV_ID, 4, 7),
 114        [F_CHIP_REV]            = REG_FIELD(RT9455_REG_DEV_ID, 0, 3),
 115
 116        [F_RST]                 = REG_FIELD(RT9455_REG_CTRL4, 7, 7),
 117
 118        [F_TMR_EN]              = REG_FIELD(RT9455_REG_CTRL5, 7, 7),
 119        [F_MIVR]                = REG_FIELD(RT9455_REG_CTRL5, 4, 5),
 120        [F_IPREC]               = REG_FIELD(RT9455_REG_CTRL5, 2, 3),
 121        [F_IEOC_PERCENTAGE]     = REG_FIELD(RT9455_REG_CTRL5, 0, 1),
 122
 123        [F_IAICR_SEL]           = REG_FIELD(RT9455_REG_CTRL6, 7, 7),
 124        [F_ICHRG]               = REG_FIELD(RT9455_REG_CTRL6, 4, 6),
 125        [F_VPREC]               = REG_FIELD(RT9455_REG_CTRL6, 0, 2),
 126
 127        [F_BATD_EN]             = REG_FIELD(RT9455_REG_CTRL7, 6, 6),
 128        [F_CHG_EN]              = REG_FIELD(RT9455_REG_CTRL7, 4, 4),
 129        [F_VMREG]               = REG_FIELD(RT9455_REG_CTRL7, 0, 3),
 130
 131        [F_TSDI]                = REG_FIELD(RT9455_REG_IRQ1, 7, 7),
 132        [F_VINOVPI]             = REG_FIELD(RT9455_REG_IRQ1, 6, 6),
 133        [F_BATAB]               = REG_FIELD(RT9455_REG_IRQ1, 0, 0),
 134
 135        [F_CHRVPI]              = REG_FIELD(RT9455_REG_IRQ2, 7, 7),
 136        [F_CHBATOVI]            = REG_FIELD(RT9455_REG_IRQ2, 5, 5),
 137        [F_CHTERMI]             = REG_FIELD(RT9455_REG_IRQ2, 4, 4),
 138        [F_CHRCHGI]             = REG_FIELD(RT9455_REG_IRQ2, 3, 3),
 139        [F_CH32MI]              = REG_FIELD(RT9455_REG_IRQ2, 2, 2),
 140        [F_CHTREGI]             = REG_FIELD(RT9455_REG_IRQ2, 1, 1),
 141        [F_CHMIVRI]             = REG_FIELD(RT9455_REG_IRQ2, 0, 0),
 142
 143        [F_BSTBUSOVI]           = REG_FIELD(RT9455_REG_IRQ3, 7, 7),
 144        [F_BSTOLI]              = REG_FIELD(RT9455_REG_IRQ3, 6, 6),
 145        [F_BSTLOWVI]            = REG_FIELD(RT9455_REG_IRQ3, 5, 5),
 146        [F_BST32SI]             = REG_FIELD(RT9455_REG_IRQ3, 3, 3),
 147
 148        [F_TSDM]                = REG_FIELD(RT9455_REG_MASK1, 7, 7),
 149        [F_VINOVPIM]            = REG_FIELD(RT9455_REG_MASK1, 6, 6),
 150        [F_BATABM]              = REG_FIELD(RT9455_REG_MASK1, 0, 0),
 151
 152        [F_CHRVPIM]             = REG_FIELD(RT9455_REG_MASK2, 7, 7),
 153        [F_CHBATOVIM]           = REG_FIELD(RT9455_REG_MASK2, 5, 5),
 154        [F_CHTERMIM]            = REG_FIELD(RT9455_REG_MASK2, 4, 4),
 155        [F_CHRCHGIM]            = REG_FIELD(RT9455_REG_MASK2, 3, 3),
 156        [F_CH32MIM]             = REG_FIELD(RT9455_REG_MASK2, 2, 2),
 157        [F_CHTREGIM]            = REG_FIELD(RT9455_REG_MASK2, 1, 1),
 158        [F_CHMIVRIM]            = REG_FIELD(RT9455_REG_MASK2, 0, 0),
 159
 160        [F_BSTVINOVIM]          = REG_FIELD(RT9455_REG_MASK3, 7, 7),
 161        [F_BSTOLIM]             = REG_FIELD(RT9455_REG_MASK3, 6, 6),
 162        [F_BSTLOWVIM]           = REG_FIELD(RT9455_REG_MASK3, 5, 5),
 163        [F_BST32SIM]            = REG_FIELD(RT9455_REG_MASK3, 3, 3),
 164};
 165
 166#define GET_MASK(fid)   (BIT(rt9455_reg_fields[fid].msb + 1) - \
 167                         BIT(rt9455_reg_fields[fid].lsb))
 168
 169/*
 170 * Each array initialised below shows the possible real-world values for a
 171 * group of bits belonging to RT9455 registers. The arrays are sorted in
 172 * ascending order. The index of each real-world value represents the value
 173 * that is encoded in the group of bits belonging to RT9455 registers.
 174 */
 175/* REG06[6:4] (ICHRG) in uAh */
 176static const int rt9455_ichrg_values[] = {
 177         500000,  650000,  800000,  950000, 1100000, 1250000, 1400000, 1550000
 178};
 179
 180/*
 181 * When the charger is in charge mode, REG02[7:2] represent battery regulation
 182 * voltage.
 183 */
 184/* REG02[7:2] (VOREG) in uV */
 185static const int rt9455_voreg_values[] = {
 186        3500000, 3520000, 3540000, 3560000, 3580000, 3600000, 3620000, 3640000,
 187        3660000, 3680000, 3700000, 3720000, 3740000, 3760000, 3780000, 3800000,
 188        3820000, 3840000, 3860000, 3880000, 3900000, 3920000, 3940000, 3960000,
 189        3980000, 4000000, 4020000, 4040000, 4060000, 4080000, 4100000, 4120000,
 190        4140000, 4160000, 4180000, 4200000, 4220000, 4240000, 4260000, 4280000,
 191        4300000, 4330000, 4350000, 4370000, 4390000, 4410000, 4430000, 4450000,
 192        4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000,
 193        4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000
 194};
 195
 196/*
 197 * When the charger is in boost mode, REG02[7:2] represent boost output
 198 * voltage.
 199 */
 200/* REG02[7:2] (Boost output voltage) in uV */
 201static const int rt9455_boost_voltage_values[] = {
 202        4425000, 4450000, 4475000, 4500000, 4525000, 4550000, 4575000, 4600000,
 203        4625000, 4650000, 4675000, 4700000, 4725000, 4750000, 4775000, 4800000,
 204        4825000, 4850000, 4875000, 4900000, 4925000, 4950000, 4975000, 5000000,
 205        5025000, 5050000, 5075000, 5100000, 5125000, 5150000, 5175000, 5200000,
 206        5225000, 5250000, 5275000, 5300000, 5325000, 5350000, 5375000, 5400000,
 207        5425000, 5450000, 5475000, 5500000, 5525000, 5550000, 5575000, 5600000,
 208        5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000,
 209        5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000,
 210};
 211
 212/* REG07[3:0] (VMREG) in uV */
 213static const int rt9455_vmreg_values[] = {
 214        4200000, 4220000, 4240000, 4260000, 4280000, 4300000, 4320000, 4340000,
 215        4360000, 4380000, 4400000, 4430000, 4450000, 4450000, 4450000, 4450000
 216};
 217
 218/* REG05[5:4] (IEOC_PERCENTAGE) */
 219static const int rt9455_ieoc_percentage_values[] = {
 220        10, 30, 20, 30
 221};
 222
 223/* REG05[1:0] (MIVR) in uV */
 224static const int rt9455_mivr_values[] = {
 225        4000000, 4250000, 4500000, 5000000
 226};
 227
 228/* REG05[1:0] (IAICR) in uA */
 229static const int rt9455_iaicr_values[] = {
 230        100000, 500000, 1000000, 2000000
 231};
 232
 233struct rt9455_info {
 234        struct i2c_client               *client;
 235        struct regmap                   *regmap;
 236        struct regmap_field             *regmap_fields[F_MAX_FIELDS];
 237        struct power_supply             *charger;
 238#if IS_ENABLED(CONFIG_USB_PHY)
 239        struct usb_phy                  *usb_phy;
 240        struct notifier_block           nb;
 241#endif
 242        struct delayed_work             pwr_rdy_work;
 243        struct delayed_work             max_charging_time_work;
 244        struct delayed_work             batt_presence_work;
 245        u32                             voreg;
 246        u32                             boost_voltage;
 247};
 248
 249/*
 250 * Iterate through each element of the 'tbl' array until an element whose value
 251 * is greater than v is found. Return the index of the respective element,
 252 * or the index of the last element in the array, if no such element is found.
 253 */
 254static unsigned int rt9455_find_idx(const int tbl[], int tbl_size, int v)
 255{
 256        int i;
 257
 258        /*
 259         * No need to iterate until the last index in the table because
 260         * if no element greater than v is found in the table,
 261         * or if only the last element is greater than v,
 262         * function returns the index of the last element.
 263         */
 264        for (i = 0; i < tbl_size - 1; i++)
 265                if (v <= tbl[i])
 266                        return i;
 267
 268        return (tbl_size - 1);
 269}
 270
 271static int rt9455_get_field_val(struct rt9455_info *info,
 272                                enum rt9455_fields field,
 273                                const int tbl[], int tbl_size, int *val)
 274{
 275        unsigned int v;
 276        int ret;
 277
 278        ret = regmap_field_read(info->regmap_fields[field], &v);
 279        if (ret)
 280                return ret;
 281
 282        v = (v >= tbl_size) ? (tbl_size - 1) : v;
 283        *val = tbl[v];
 284
 285        return 0;
 286}
 287
 288static int rt9455_set_field_val(struct rt9455_info *info,
 289                                enum rt9455_fields field,
 290                                const int tbl[], int tbl_size, int val)
 291{
 292        unsigned int idx = rt9455_find_idx(tbl, tbl_size, val);
 293
 294        return regmap_field_write(info->regmap_fields[field], idx);
 295}
 296
 297static int rt9455_register_reset(struct rt9455_info *info)
 298{
 299        struct device *dev = &info->client->dev;
 300        unsigned int v;
 301        int ret, limit = 100;
 302
 303        ret = regmap_field_write(info->regmap_fields[F_RST], 0x01);
 304        if (ret) {
 305                dev_err(dev, "Failed to set RST bit\n");
 306                return ret;
 307        }
 308
 309        /*
 310         * To make sure that reset operation has finished, loop until RST bit
 311         * is set to 0.
 312         */
 313        do {
 314                ret = regmap_field_read(info->regmap_fields[F_RST], &v);
 315                if (ret) {
 316                        dev_err(dev, "Failed to read RST bit\n");
 317                        return ret;
 318                }
 319
 320                if (!v)
 321                        break;
 322
 323                usleep_range(10, 100);
 324        } while (--limit);
 325
 326        if (!limit)
 327                return -EIO;
 328
 329        return 0;
 330}
 331
 332/* Charger power supply property routines */
 333static enum power_supply_property rt9455_charger_properties[] = {
 334        POWER_SUPPLY_PROP_STATUS,
 335        POWER_SUPPLY_PROP_HEALTH,
 336        POWER_SUPPLY_PROP_PRESENT,
 337        POWER_SUPPLY_PROP_ONLINE,
 338        POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
 339        POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
 340        POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
 341        POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
 342        POWER_SUPPLY_PROP_SCOPE,
 343        POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
 344        POWER_SUPPLY_PROP_MODEL_NAME,
 345        POWER_SUPPLY_PROP_MANUFACTURER,
 346};
 347
 348static char *rt9455_charger_supplied_to[] = {
 349        "main-battery",
 350};
 351
 352static int rt9455_charger_get_status(struct rt9455_info *info,
 353                                     union power_supply_propval *val)
 354{
 355        unsigned int v, pwr_rdy;
 356        int ret;
 357
 358        ret = regmap_field_read(info->regmap_fields[F_PWR_RDY],
 359                                &pwr_rdy);
 360        if (ret) {
 361                dev_err(&info->client->dev, "Failed to read PWR_RDY bit\n");
 362                return ret;
 363        }
 364
 365        /*
 366         * If PWR_RDY bit is unset, the battery is discharging. Otherwise,
 367         * STAT bits value must be checked.
 368         */
 369        if (!pwr_rdy) {
 370                val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
 371                return 0;
 372        }
 373
 374        ret = regmap_field_read(info->regmap_fields[F_STAT], &v);
 375        if (ret) {
 376                dev_err(&info->client->dev, "Failed to read STAT bits\n");
 377                return ret;
 378        }
 379
 380        switch (v) {
 381        case 0:
 382                /*
 383                 * If PWR_RDY bit is set, but STAT bits value is 0, the charger
 384                 * may be in one of the following cases:
 385                 * 1. CHG_EN bit is 0.
 386                 * 2. CHG_EN bit is 1 but the battery is not connected.
 387                 * In any of these cases, POWER_SUPPLY_STATUS_NOT_CHARGING is
 388                 * returned.
 389                 */
 390                val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
 391                return 0;
 392        case 1:
 393                val->intval = POWER_SUPPLY_STATUS_CHARGING;
 394                return 0;
 395        case 2:
 396                val->intval = POWER_SUPPLY_STATUS_FULL;
 397                return 0;
 398        default:
 399                val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
 400                return 0;
 401        }
 402}
 403
 404static int rt9455_charger_get_health(struct rt9455_info *info,
 405                                     union power_supply_propval *val)
 406{
 407        struct device *dev = &info->client->dev;
 408        unsigned int v;
 409        int ret;
 410
 411        val->intval = POWER_SUPPLY_HEALTH_GOOD;
 412
 413        ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &v);
 414        if (ret) {
 415                dev_err(dev, "Failed to read IRQ1 register\n");
 416                return ret;
 417        }
 418
 419        if (v & GET_MASK(F_TSDI)) {
 420                val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
 421                return 0;
 422        }
 423        if (v & GET_MASK(F_VINOVPI)) {
 424                val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
 425                return 0;
 426        }
 427        if (v & GET_MASK(F_BATAB)) {
 428                val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
 429                return 0;
 430        }
 431
 432        ret = regmap_read(info->regmap, RT9455_REG_IRQ2, &v);
 433        if (ret) {
 434                dev_err(dev, "Failed to read IRQ2 register\n");
 435                return ret;
 436        }
 437
 438        if (v & GET_MASK(F_CHBATOVI)) {
 439                val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
 440                return 0;
 441        }
 442        if (v & GET_MASK(F_CH32MI)) {
 443                val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
 444                return 0;
 445        }
 446
 447        ret = regmap_read(info->regmap, RT9455_REG_IRQ3, &v);
 448        if (ret) {
 449                dev_err(dev, "Failed to read IRQ3 register\n");
 450                return ret;
 451        }
 452
 453        if (v & GET_MASK(F_BSTBUSOVI)) {
 454                val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
 455                return 0;
 456        }
 457        if (v & GET_MASK(F_BSTOLI)) {
 458                val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
 459                return 0;
 460        }
 461        if (v & GET_MASK(F_BSTLOWVI)) {
 462                val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
 463                return 0;
 464        }
 465        if (v & GET_MASK(F_BST32SI)) {
 466                val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
 467                return 0;
 468        }
 469
 470        ret = regmap_field_read(info->regmap_fields[F_STAT], &v);
 471        if (ret) {
 472                dev_err(dev, "Failed to read STAT bits\n");
 473                return ret;
 474        }
 475
 476        if (v == RT9455_FAULT) {
 477                val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
 478                return 0;
 479        }
 480
 481        return 0;
 482}
 483
 484static int rt9455_charger_get_battery_presence(struct rt9455_info *info,
 485                                               union power_supply_propval *val)
 486{
 487        unsigned int v;
 488        int ret;
 489
 490        ret = regmap_field_read(info->regmap_fields[F_BATAB], &v);
 491        if (ret) {
 492                dev_err(&info->client->dev, "Failed to read BATAB bit\n");
 493                return ret;
 494        }
 495
 496        /*
 497         * Since BATAB is 1 when battery is NOT present and 0 otherwise,
 498         * !BATAB is returned.
 499         */
 500        val->intval = !v;
 501
 502        return 0;
 503}
 504
 505static int rt9455_charger_get_online(struct rt9455_info *info,
 506                                     union power_supply_propval *val)
 507{
 508        unsigned int v;
 509        int ret;
 510
 511        ret = regmap_field_read(info->regmap_fields[F_PWR_RDY], &v);
 512        if (ret) {
 513                dev_err(&info->client->dev, "Failed to read PWR_RDY bit\n");
 514                return ret;
 515        }
 516
 517        val->intval = (int)v;
 518
 519        return 0;
 520}
 521
 522static int rt9455_charger_get_current(struct rt9455_info *info,
 523                                      union power_supply_propval *val)
 524{
 525        int curr;
 526        int ret;
 527
 528        ret = rt9455_get_field_val(info, F_ICHRG,
 529                                   rt9455_ichrg_values,
 530                                   ARRAY_SIZE(rt9455_ichrg_values),
 531                                   &curr);
 532        if (ret) {
 533                dev_err(&info->client->dev, "Failed to read ICHRG value\n");
 534                return ret;
 535        }
 536
 537        val->intval = curr;
 538
 539        return 0;
 540}
 541
 542static int rt9455_charger_get_current_max(struct rt9455_info *info,
 543                                          union power_supply_propval *val)
 544{
 545        int idx = ARRAY_SIZE(rt9455_ichrg_values) - 1;
 546
 547        val->intval = rt9455_ichrg_values[idx];
 548
 549        return 0;
 550}
 551
 552static int rt9455_charger_get_voltage(struct rt9455_info *info,
 553                                      union power_supply_propval *val)
 554{
 555        int voltage;
 556        int ret;
 557
 558        ret = rt9455_get_field_val(info, F_VOREG,
 559                                   rt9455_voreg_values,
 560                                   ARRAY_SIZE(rt9455_voreg_values),
 561                                   &voltage);
 562        if (ret) {
 563                dev_err(&info->client->dev, "Failed to read VOREG value\n");
 564                return ret;
 565        }
 566
 567        val->intval = voltage;
 568
 569        return 0;
 570}
 571
 572static int rt9455_charger_get_voltage_max(struct rt9455_info *info,
 573                                          union power_supply_propval *val)
 574{
 575        int idx = ARRAY_SIZE(rt9455_vmreg_values) - 1;
 576
 577        val->intval = rt9455_vmreg_values[idx];
 578
 579        return 0;
 580}
 581
 582static int rt9455_charger_get_term_current(struct rt9455_info *info,
 583                                           union power_supply_propval *val)
 584{
 585        struct device *dev = &info->client->dev;
 586        int ichrg, ieoc_percentage, ret;
 587
 588        ret = rt9455_get_field_val(info, F_ICHRG,
 589                                   rt9455_ichrg_values,
 590                                   ARRAY_SIZE(rt9455_ichrg_values),
 591                                   &ichrg);
 592        if (ret) {
 593                dev_err(dev, "Failed to read ICHRG value\n");
 594                return ret;
 595        }
 596
 597        ret = rt9455_get_field_val(info, F_IEOC_PERCENTAGE,
 598                                   rt9455_ieoc_percentage_values,
 599                                   ARRAY_SIZE(rt9455_ieoc_percentage_values),
 600                                   &ieoc_percentage);
 601        if (ret) {
 602                dev_err(dev, "Failed to read IEOC value\n");
 603                return ret;
 604        }
 605
 606        val->intval = ichrg * ieoc_percentage / 100;
 607
 608        return 0;
 609}
 610
 611static int rt9455_charger_get_property(struct power_supply *psy,
 612                                       enum power_supply_property psp,
 613                                       union power_supply_propval *val)
 614{
 615        struct rt9455_info *info = power_supply_get_drvdata(psy);
 616
 617        switch (psp) {
 618        case POWER_SUPPLY_PROP_STATUS:
 619                return rt9455_charger_get_status(info, val);
 620        case POWER_SUPPLY_PROP_HEALTH:
 621                return rt9455_charger_get_health(info, val);
 622        case POWER_SUPPLY_PROP_PRESENT:
 623                return rt9455_charger_get_battery_presence(info, val);
 624        case POWER_SUPPLY_PROP_ONLINE:
 625                return rt9455_charger_get_online(info, val);
 626        case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
 627                return rt9455_charger_get_current(info, val);
 628        case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
 629                return rt9455_charger_get_current_max(info, val);
 630        case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
 631                return rt9455_charger_get_voltage(info, val);
 632        case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
 633                return rt9455_charger_get_voltage_max(info, val);
 634        case POWER_SUPPLY_PROP_SCOPE:
 635                val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
 636                return 0;
 637        case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
 638                return rt9455_charger_get_term_current(info, val);
 639        case POWER_SUPPLY_PROP_MODEL_NAME:
 640                val->strval = RT9455_MODEL_NAME;
 641                return 0;
 642        case POWER_SUPPLY_PROP_MANUFACTURER:
 643                val->strval = RT9455_MANUFACTURER;
 644                return 0;
 645        default:
 646                return -ENODATA;
 647        }
 648}
 649
 650static int rt9455_hw_init(struct rt9455_info *info, u32 ichrg,
 651                          u32 ieoc_percentage,
 652                          u32 mivr, u32 iaicr)
 653{
 654        struct device *dev = &info->client->dev;
 655        int idx, ret;
 656
 657        ret = rt9455_register_reset(info);
 658        if (ret) {
 659                dev_err(dev, "Power On Reset failed\n");
 660                return ret;
 661        }
 662
 663        /* Set TE bit in order to enable end of charge detection */
 664        ret = regmap_field_write(info->regmap_fields[F_TE], 1);
 665        if (ret) {
 666                dev_err(dev, "Failed to set TE bit\n");
 667                return ret;
 668        }
 669
 670        /* Set TE_SHDN_EN bit in order to enable end of charge detection */
 671        ret = regmap_field_write(info->regmap_fields[F_TE_SHDN_EN], 1);
 672        if (ret) {
 673                dev_err(dev, "Failed to set TE_SHDN_EN bit\n");
 674                return ret;
 675        }
 676
 677        /*
 678         * Set BATD_EN bit in order to enable battery detection
 679         * when charging is done
 680         */
 681        ret = regmap_field_write(info->regmap_fields[F_BATD_EN], 1);
 682        if (ret) {
 683                dev_err(dev, "Failed to set BATD_EN bit\n");
 684                return ret;
 685        }
 686
 687        /*
 688         * Disable Safety Timer. In charge mode, this timer terminates charging
 689         * if no read or write via I2C is done within 32 minutes. This timer
 690         * avoids overcharging the baterry when the OS is not loaded and the
 691         * charger is connected to a power source.
 692         * In boost mode, this timer triggers BST32SI interrupt if no read or
 693         * write via I2C is done within 32 seconds.
 694         * When the OS is loaded and the charger driver is inserted, it is used
 695         * delayed_work, named max_charging_time_work, to avoid overcharging
 696         * the battery.
 697         */
 698        ret = regmap_field_write(info->regmap_fields[F_TMR_EN], 0x00);
 699        if (ret) {
 700                dev_err(dev, "Failed to disable Safety Timer\n");
 701                return ret;
 702        }
 703
 704        /* Set ICHRG to value retrieved from device-specific data */
 705        ret = rt9455_set_field_val(info, F_ICHRG,
 706                                   rt9455_ichrg_values,
 707                                   ARRAY_SIZE(rt9455_ichrg_values), ichrg);
 708        if (ret) {
 709                dev_err(dev, "Failed to set ICHRG value\n");
 710                return ret;
 711        }
 712
 713        /* Set IEOC Percentage to value retrieved from device-specific data */
 714        ret = rt9455_set_field_val(info, F_IEOC_PERCENTAGE,
 715                                   rt9455_ieoc_percentage_values,
 716                                   ARRAY_SIZE(rt9455_ieoc_percentage_values),
 717                                   ieoc_percentage);
 718        if (ret) {
 719                dev_err(dev, "Failed to set IEOC Percentage value\n");
 720                return ret;
 721        }
 722
 723        /* Set VOREG to value retrieved from device-specific data */
 724        ret = rt9455_set_field_val(info, F_VOREG,
 725                                   rt9455_voreg_values,
 726                                   ARRAY_SIZE(rt9455_voreg_values),
 727                                   info->voreg);
 728        if (ret) {
 729                dev_err(dev, "Failed to set VOREG value\n");
 730                return ret;
 731        }
 732
 733        /* Set VMREG value to maximum (4.45V). */
 734        idx = ARRAY_SIZE(rt9455_vmreg_values) - 1;
 735        ret = rt9455_set_field_val(info, F_VMREG,
 736                                   rt9455_vmreg_values,
 737                                   ARRAY_SIZE(rt9455_vmreg_values),
 738                                   rt9455_vmreg_values[idx]);
 739        if (ret) {
 740                dev_err(dev, "Failed to set VMREG value\n");
 741                return ret;
 742        }
 743
 744        /*
 745         * Set MIVR to value retrieved from device-specific data.
 746         * If no value is specified, default value for MIVR is 4.5V.
 747         */
 748        if (mivr == -1)
 749                mivr = 4500000;
 750
 751        ret = rt9455_set_field_val(info, F_MIVR,
 752                                   rt9455_mivr_values,
 753                                   ARRAY_SIZE(rt9455_mivr_values), mivr);
 754        if (ret) {
 755                dev_err(dev, "Failed to set MIVR value\n");
 756                return ret;
 757        }
 758
 759        /*
 760         * Set IAICR to value retrieved from device-specific data.
 761         * If no value is specified, default value for IAICR is 500 mA.
 762         */
 763        if (iaicr == -1)
 764                iaicr = 500000;
 765
 766        ret = rt9455_set_field_val(info, F_IAICR,
 767                                   rt9455_iaicr_values,
 768                                   ARRAY_SIZE(rt9455_iaicr_values), iaicr);
 769        if (ret) {
 770                dev_err(dev, "Failed to set IAICR value\n");
 771                return ret;
 772        }
 773
 774        /*
 775         * Set IAICR_INT bit so that IAICR value is determined by IAICR bits
 776         * and not by OTG pin.
 777         */
 778        ret = regmap_field_write(info->regmap_fields[F_IAICR_INT], 0x01);
 779        if (ret) {
 780                dev_err(dev, "Failed to set IAICR_INT bit\n");
 781                return ret;
 782        }
 783
 784        /*
 785         * Disable CHMIVRI interrupt. Because the driver sets MIVR value,
 786         * CHMIVRI is triggered, but there is no action to be taken by the
 787         * driver when CHMIVRI is triggered.
 788         */
 789        ret = regmap_field_write(info->regmap_fields[F_CHMIVRIM], 0x01);
 790        if (ret) {
 791                dev_err(dev, "Failed to mask CHMIVRI interrupt\n");
 792                return ret;
 793        }
 794
 795        return 0;
 796}
 797
 798#if IS_ENABLED(CONFIG_USB_PHY)
 799/*
 800 * Before setting the charger into boost mode, boost output voltage is
 801 * set. This is needed because boost output voltage may differ from battery
 802 * regulation voltage. F_VOREG bits represent either battery regulation voltage
 803 * or boost output voltage, depending on the mode the charger is. Both battery
 804 * regulation voltage and boost output voltage are read from DT/ACPI during
 805 * probe.
 806 */
 807static int rt9455_set_boost_voltage_before_boost_mode(struct rt9455_info *info)
 808{
 809        struct device *dev = &info->client->dev;
 810        int ret;
 811
 812        ret = rt9455_set_field_val(info, F_VOREG,
 813                                   rt9455_boost_voltage_values,
 814                                   ARRAY_SIZE(rt9455_boost_voltage_values),
 815                                   info->boost_voltage);
 816        if (ret) {
 817                dev_err(dev, "Failed to set boost output voltage value\n");
 818                return ret;
 819        }
 820
 821        return 0;
 822}
 823#endif
 824
 825/*
 826 * Before setting the charger into charge mode, battery regulation voltage is
 827 * set. This is needed because boost output voltage may differ from battery
 828 * regulation voltage. F_VOREG bits represent either battery regulation voltage
 829 * or boost output voltage, depending on the mode the charger is. Both battery
 830 * regulation voltage and boost output voltage are read from DT/ACPI during
 831 * probe.
 832 */
 833static int rt9455_set_voreg_before_charge_mode(struct rt9455_info *info)
 834{
 835        struct device *dev = &info->client->dev;
 836        int ret;
 837
 838        ret = rt9455_set_field_val(info, F_VOREG,
 839                                   rt9455_voreg_values,
 840                                   ARRAY_SIZE(rt9455_voreg_values),
 841                                   info->voreg);
 842        if (ret) {
 843                dev_err(dev, "Failed to set VOREG value\n");
 844                return ret;
 845        }
 846
 847        return 0;
 848}
 849
 850static int rt9455_irq_handler_check_irq1_register(struct rt9455_info *info,
 851                                                  bool *_is_battery_absent,
 852                                                  bool *_alert_userspace)
 853{
 854        unsigned int irq1, mask1, mask2;
 855        struct device *dev = &info->client->dev;
 856        bool is_battery_absent = false;
 857        bool alert_userspace = false;
 858        int ret;
 859
 860        ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &irq1);
 861        if (ret) {
 862                dev_err(dev, "Failed to read IRQ1 register\n");
 863                return ret;
 864        }
 865
 866        ret = regmap_read(info->regmap, RT9455_REG_MASK1, &mask1);
 867        if (ret) {
 868                dev_err(dev, "Failed to read MASK1 register\n");
 869                return ret;
 870        }
 871
 872        if (irq1 & GET_MASK(F_TSDI)) {
 873                dev_err(dev, "Thermal shutdown fault occurred\n");
 874                alert_userspace = true;
 875        }
 876
 877        if (irq1 & GET_MASK(F_VINOVPI)) {
 878                dev_err(dev, "Overvoltage input occurred\n");
 879                alert_userspace = true;
 880        }
 881
 882        if (irq1 & GET_MASK(F_BATAB)) {
 883                dev_err(dev, "Battery absence occurred\n");
 884                is_battery_absent = true;
 885                alert_userspace = true;
 886
 887                if ((mask1 & GET_MASK(F_BATABM)) == 0) {
 888                        ret = regmap_field_write(info->regmap_fields[F_BATABM],
 889                                                 0x01);
 890                        if (ret) {
 891                                dev_err(dev, "Failed to mask BATAB interrupt\n");
 892                                return ret;
 893                        }
 894                }
 895
 896                ret = regmap_read(info->regmap, RT9455_REG_MASK2, &mask2);
 897                if (ret) {
 898                        dev_err(dev, "Failed to read MASK2 register\n");
 899                        return ret;
 900                }
 901
 902                if (mask2 & GET_MASK(F_CHTERMIM)) {
 903                        ret = regmap_field_write(
 904                                info->regmap_fields[F_CHTERMIM], 0x00);
 905                        if (ret) {
 906                                dev_err(dev, "Failed to unmask CHTERMI interrupt\n");
 907                                return ret;
 908                        }
 909                }
 910
 911                if (mask2 & GET_MASK(F_CHRCHGIM)) {
 912                        ret = regmap_field_write(
 913                                info->regmap_fields[F_CHRCHGIM], 0x00);
 914                        if (ret) {
 915                                dev_err(dev, "Failed to unmask CHRCHGI interrupt\n");
 916                                return ret;
 917                        }
 918                }
 919
 920                /*
 921                 * When the battery is absent, max_charging_time_work is
 922                 * cancelled, since no charging is done.
 923                 */
 924                cancel_delayed_work_sync(&info->max_charging_time_work);
 925                /*
 926                 * Since no interrupt is triggered when the battery is
 927                 * reconnected, max_charging_time_work is not rescheduled.
 928                 * Therefore, batt_presence_work is scheduled to check whether
 929                 * the battery is still absent or not.
 930                 */
 931                queue_delayed_work(system_power_efficient_wq,
 932                                   &info->batt_presence_work,
 933                                   RT9455_BATT_PRESENCE_DELAY * HZ);
 934        }
 935
 936        *_is_battery_absent = is_battery_absent;
 937
 938        if (alert_userspace)
 939                *_alert_userspace = alert_userspace;
 940
 941        return 0;
 942}
 943
 944static int rt9455_irq_handler_check_irq2_register(struct rt9455_info *info,
 945                                                  bool is_battery_absent,
 946                                                  bool *_alert_userspace)
 947{
 948        unsigned int irq2, mask2;
 949        struct device *dev = &info->client->dev;
 950        bool alert_userspace = false;
 951        int ret;
 952
 953        ret = regmap_read(info->regmap, RT9455_REG_IRQ2, &irq2);
 954        if (ret) {
 955                dev_err(dev, "Failed to read IRQ2 register\n");
 956                return ret;
 957        }
 958
 959        ret = regmap_read(info->regmap, RT9455_REG_MASK2, &mask2);
 960        if (ret) {
 961                dev_err(dev, "Failed to read MASK2 register\n");
 962                return ret;
 963        }
 964
 965        if (irq2 & GET_MASK(F_CHRVPI)) {
 966                dev_dbg(dev, "Charger fault occurred\n");
 967                /*
 968                 * CHRVPI bit is set in 2 cases:
 969                 * 1. when the power source is connected to the charger.
 970                 * 2. when the power source is disconnected from the charger.
 971                 * To identify the case, PWR_RDY bit is checked. Because
 972                 * PWR_RDY bit is set / cleared after CHRVPI interrupt is
 973                 * triggered, it is used delayed_work to later read PWR_RDY bit.
 974                 * Also, do not set to true alert_userspace, because there is no
 975                 * need to notify userspace when CHRVPI interrupt has occurred.
 976                 * Userspace will be notified after PWR_RDY bit is read.
 977                 */
 978                queue_delayed_work(system_power_efficient_wq,
 979                                   &info->pwr_rdy_work,
 980                                   RT9455_PWR_RDY_DELAY * HZ);
 981        }
 982        if (irq2 & GET_MASK(F_CHBATOVI)) {
 983                dev_err(dev, "Battery OVP occurred\n");
 984                alert_userspace = true;
 985        }
 986        if (irq2 & GET_MASK(F_CHTERMI)) {
 987                dev_dbg(dev, "Charge terminated\n");
 988                if (!is_battery_absent) {
 989                        if ((mask2 & GET_MASK(F_CHTERMIM)) == 0) {
 990                                ret = regmap_field_write(
 991                                        info->regmap_fields[F_CHTERMIM], 0x01);
 992                                if (ret) {
 993                                        dev_err(dev, "Failed to mask CHTERMI interrupt\n");
 994                                        return ret;
 995                                }
 996                                /*
 997                                 * Update MASK2 value, since CHTERMIM bit is
 998                                 * set.
 999                                 */
1000                                mask2 = mask2 | GET_MASK(F_CHTERMIM);
1001                        }
1002                        cancel_delayed_work_sync(&info->max_charging_time_work);
1003                        alert_userspace = true;
1004                }
1005        }
1006        if (irq2 & GET_MASK(F_CHRCHGI)) {
1007                dev_dbg(dev, "Recharge request\n");
1008                ret = regmap_field_write(info->regmap_fields[F_CHG_EN],
1009                                         RT9455_CHARGE_ENABLE);
1010                if (ret) {
1011                        dev_err(dev, "Failed to enable charging\n");
1012                        return ret;
1013                }
1014                if (mask2 & GET_MASK(F_CHTERMIM)) {
1015                        ret = regmap_field_write(
1016                                info->regmap_fields[F_CHTERMIM], 0x00);
1017                        if (ret) {
1018                                dev_err(dev, "Failed to unmask CHTERMI interrupt\n");
1019                                return ret;
1020                        }
1021                        /* Update MASK2 value, since CHTERMIM bit is cleared. */
1022                        mask2 = mask2 & ~GET_MASK(F_CHTERMIM);
1023                }
1024                if (!is_battery_absent) {
1025                        /*
1026                         * No need to check whether the charger is connected to
1027                         * power source when CHRCHGI is received, since CHRCHGI
1028                         * is not triggered if the charger is not connected to
1029                         * the power source.
1030                         */
1031                        queue_delayed_work(system_power_efficient_wq,
1032                                           &info->max_charging_time_work,
1033                                           RT9455_MAX_CHARGING_TIME * HZ);
1034                        alert_userspace = true;
1035                }
1036        }
1037        if (irq2 & GET_MASK(F_CH32MI)) {
1038                dev_err(dev, "Charger fault. 32 mins timeout occurred\n");
1039                alert_userspace = true;
1040        }
1041        if (irq2 & GET_MASK(F_CHTREGI)) {
1042                dev_warn(dev,
1043                         "Charger warning. Thermal regulation loop active\n");
1044                alert_userspace = true;
1045        }
1046        if (irq2 & GET_MASK(F_CHMIVRI)) {
1047                dev_dbg(dev,
1048                        "Charger warning. Input voltage MIVR loop active\n");
1049        }
1050
1051        if (alert_userspace)
1052                *_alert_userspace = alert_userspace;
1053
1054        return 0;
1055}
1056
1057static int rt9455_irq_handler_check_irq3_register(struct rt9455_info *info,
1058                                                  bool *_alert_userspace)
1059{
1060        unsigned int irq3, mask3;
1061        struct device *dev = &info->client->dev;
1062        bool alert_userspace = false;
1063        int ret;
1064
1065        ret = regmap_read(info->regmap, RT9455_REG_IRQ3, &irq3);
1066        if (ret) {
1067                dev_err(dev, "Failed to read IRQ3 register\n");
1068                return ret;
1069        }
1070
1071        ret = regmap_read(info->regmap, RT9455_REG_MASK3, &mask3);
1072        if (ret) {
1073                dev_err(dev, "Failed to read MASK3 register\n");
1074                return ret;
1075        }
1076
1077        if (irq3 & GET_MASK(F_BSTBUSOVI)) {
1078                dev_err(dev, "Boost fault. Overvoltage input occurred\n");
1079                alert_userspace = true;
1080        }
1081        if (irq3 & GET_MASK(F_BSTOLI)) {
1082                dev_err(dev, "Boost fault. Overload\n");
1083                alert_userspace = true;
1084        }
1085        if (irq3 & GET_MASK(F_BSTLOWVI)) {
1086                dev_err(dev, "Boost fault. Battery voltage too low\n");
1087                alert_userspace = true;
1088        }
1089        if (irq3 & GET_MASK(F_BST32SI)) {
1090                dev_err(dev, "Boost fault. 32 seconds timeout occurred.\n");
1091                alert_userspace = true;
1092        }
1093
1094        if (alert_userspace) {
1095                dev_info(dev, "Boost fault occurred, therefore the charger goes into charge mode\n");
1096                ret = rt9455_set_voreg_before_charge_mode(info);
1097                if (ret) {
1098                        dev_err(dev, "Failed to set VOREG before entering charge mode\n");
1099                        return ret;
1100                }
1101                ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1102                                         RT9455_CHARGE_MODE);
1103                if (ret) {
1104                        dev_err(dev, "Failed to set charger in charge mode\n");
1105                        return ret;
1106                }
1107                *_alert_userspace = alert_userspace;
1108        }
1109
1110        return 0;
1111}
1112
1113static irqreturn_t rt9455_irq_handler_thread(int irq, void *data)
1114{
1115        struct rt9455_info *info = data;
1116        struct device *dev;
1117        bool alert_userspace = false;
1118        bool is_battery_absent = false;
1119        unsigned int status;
1120        int ret;
1121
1122        if (!info)
1123                return IRQ_NONE;
1124
1125        dev = &info->client->dev;
1126
1127        if (irq != info->client->irq) {
1128                dev_err(dev, "Interrupt is not for RT9455 charger\n");
1129                return IRQ_NONE;
1130        }
1131
1132        ret = regmap_field_read(info->regmap_fields[F_STAT], &status);
1133        if (ret) {
1134                dev_err(dev, "Failed to read STAT bits\n");
1135                return IRQ_HANDLED;
1136        }
1137        dev_dbg(dev, "Charger status is %d\n", status);
1138
1139        /*
1140         * Each function that processes an IRQ register receives as output
1141         * parameter alert_userspace pointer. alert_userspace is set to true
1142         * in such a function only if an interrupt has occurred in the
1143         * respective interrupt register. This way, it is avoided the following
1144         * case: interrupt occurs only in IRQ1 register,
1145         * rt9455_irq_handler_check_irq1_register() function sets to true
1146         * alert_userspace, but rt9455_irq_handler_check_irq2_register()
1147         * and rt9455_irq_handler_check_irq3_register() functions set to false
1148         * alert_userspace and power_supply_changed() is never called.
1149         */
1150        ret = rt9455_irq_handler_check_irq1_register(info, &is_battery_absent,
1151                                                     &alert_userspace);
1152        if (ret) {
1153                dev_err(dev, "Failed to handle IRQ1 register\n");
1154                return IRQ_HANDLED;
1155        }
1156
1157        ret = rt9455_irq_handler_check_irq2_register(info, is_battery_absent,
1158                                                     &alert_userspace);
1159        if (ret) {
1160                dev_err(dev, "Failed to handle IRQ2 register\n");
1161                return IRQ_HANDLED;
1162        }
1163
1164        ret = rt9455_irq_handler_check_irq3_register(info, &alert_userspace);
1165        if (ret) {
1166                dev_err(dev, "Failed to handle IRQ3 register\n");
1167                return IRQ_HANDLED;
1168        }
1169
1170        if (alert_userspace) {
1171                /*
1172                 * Sometimes, an interrupt occurs while rt9455_probe() function
1173                 * is executing and power_supply_register() is not yet called.
1174                 * Do not call power_supply_changed() in this case.
1175                 */
1176                if (info->charger)
1177                        power_supply_changed(info->charger);
1178        }
1179
1180        return IRQ_HANDLED;
1181}
1182
1183static int rt9455_discover_charger(struct rt9455_info *info, u32 *ichrg,
1184                                   u32 *ieoc_percentage,
1185                                   u32 *mivr, u32 *iaicr)
1186{
1187        struct device *dev = &info->client->dev;
1188        int ret;
1189
1190        if (!dev->of_node && !ACPI_HANDLE(dev)) {
1191                dev_err(dev, "No support for either device tree or ACPI\n");
1192                return -EINVAL;
1193        }
1194        /*
1195         * ICHRG, IEOC_PERCENTAGE, VOREG and boost output voltage are mandatory
1196         * parameters.
1197         */
1198        ret = device_property_read_u32(dev, "richtek,output-charge-current",
1199                                       ichrg);
1200        if (ret) {
1201                dev_err(dev, "Error: missing \"output-charge-current\" property\n");
1202                return ret;
1203        }
1204
1205        ret = device_property_read_u32(dev, "richtek,end-of-charge-percentage",
1206                                       ieoc_percentage);
1207        if (ret) {
1208                dev_err(dev, "Error: missing \"end-of-charge-percentage\" property\n");
1209                return ret;
1210        }
1211
1212        ret = device_property_read_u32(dev,
1213                                       "richtek,battery-regulation-voltage",
1214                                       &info->voreg);
1215        if (ret) {
1216                dev_err(dev, "Error: missing \"battery-regulation-voltage\" property\n");
1217                return ret;
1218        }
1219
1220        ret = device_property_read_u32(dev, "richtek,boost-output-voltage",
1221                                       &info->boost_voltage);
1222        if (ret) {
1223                dev_err(dev, "Error: missing \"boost-output-voltage\" property\n");
1224                return ret;
1225        }
1226
1227        /*
1228         * MIVR and IAICR are optional parameters. Do not return error if one of
1229         * them is not present in ACPI table or device tree specification.
1230         */
1231        device_property_read_u32(dev, "richtek,min-input-voltage-regulation",
1232                                 mivr);
1233        device_property_read_u32(dev, "richtek,avg-input-current-regulation",
1234                                 iaicr);
1235
1236        return 0;
1237}
1238
1239#if IS_ENABLED(CONFIG_USB_PHY)
1240static int rt9455_usb_event_none(struct rt9455_info *info,
1241                                 u8 opa_mode, u8 iaicr)
1242{
1243        struct device *dev = &info->client->dev;
1244        int ret;
1245
1246        if (opa_mode == RT9455_BOOST_MODE) {
1247                ret = rt9455_set_voreg_before_charge_mode(info);
1248                if (ret) {
1249                        dev_err(dev, "Failed to set VOREG before entering charge mode\n");
1250                        return ret;
1251                }
1252                /*
1253                 * If the charger is in boost mode, and it has received
1254                 * USB_EVENT_NONE, this means the consumer device powered by the
1255                 * charger is not connected anymore.
1256                 * In this case, the charger goes into charge mode.
1257                 */
1258                dev_dbg(dev, "USB_EVENT_NONE received, therefore the charger goes into charge mode\n");
1259                ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1260                                         RT9455_CHARGE_MODE);
1261                if (ret) {
1262                        dev_err(dev, "Failed to set charger in charge mode\n");
1263                        return NOTIFY_DONE;
1264                }
1265        }
1266
1267        dev_dbg(dev, "USB_EVENT_NONE received, therefore IAICR is set to its minimum value\n");
1268        if (iaicr != RT9455_IAICR_100MA) {
1269                ret = regmap_field_write(info->regmap_fields[F_IAICR],
1270                                         RT9455_IAICR_100MA);
1271                if (ret) {
1272                        dev_err(dev, "Failed to set IAICR value\n");
1273                        return NOTIFY_DONE;
1274                }
1275        }
1276
1277        return NOTIFY_OK;
1278}
1279
1280static int rt9455_usb_event_vbus(struct rt9455_info *info,
1281                                 u8 opa_mode, u8 iaicr)
1282{
1283        struct device *dev = &info->client->dev;
1284        int ret;
1285
1286        if (opa_mode == RT9455_BOOST_MODE) {
1287                ret = rt9455_set_voreg_before_charge_mode(info);
1288                if (ret) {
1289                        dev_err(dev, "Failed to set VOREG before entering charge mode\n");
1290                        return ret;
1291                }
1292                /*
1293                 * If the charger is in boost mode, and it has received
1294                 * USB_EVENT_VBUS, this means the consumer device powered by the
1295                 * charger is not connected anymore.
1296                 * In this case, the charger goes into charge mode.
1297                 */
1298                dev_dbg(dev, "USB_EVENT_VBUS received, therefore the charger goes into charge mode\n");
1299                ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1300                                         RT9455_CHARGE_MODE);
1301                if (ret) {
1302                        dev_err(dev, "Failed to set charger in charge mode\n");
1303                        return NOTIFY_DONE;
1304                }
1305        }
1306
1307        dev_dbg(dev, "USB_EVENT_VBUS received, therefore IAICR is set to 500 mA\n");
1308        if (iaicr != RT9455_IAICR_500MA) {
1309                ret = regmap_field_write(info->regmap_fields[F_IAICR],
1310                                         RT9455_IAICR_500MA);
1311                if (ret) {
1312                        dev_err(dev, "Failed to set IAICR value\n");
1313                        return NOTIFY_DONE;
1314                }
1315        }
1316
1317        return NOTIFY_OK;
1318}
1319
1320static int rt9455_usb_event_id(struct rt9455_info *info,
1321                               u8 opa_mode, u8 iaicr)
1322{
1323        struct device *dev = &info->client->dev;
1324        int ret;
1325
1326        if (opa_mode == RT9455_CHARGE_MODE) {
1327                ret = rt9455_set_boost_voltage_before_boost_mode(info);
1328                if (ret) {
1329                        dev_err(dev, "Failed to set boost output voltage before entering boost mode\n");
1330                        return ret;
1331                }
1332                /*
1333                 * If the charger is in charge mode, and it has received
1334                 * USB_EVENT_ID, this means a consumer device is connected and
1335                 * it should be powered by the charger.
1336                 * In this case, the charger goes into boost mode.
1337                 */
1338                dev_dbg(dev, "USB_EVENT_ID received, therefore the charger goes into boost mode\n");
1339                ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1340                                         RT9455_BOOST_MODE);
1341                if (ret) {
1342                        dev_err(dev, "Failed to set charger in boost mode\n");
1343                        return NOTIFY_DONE;
1344                }
1345        }
1346
1347        dev_dbg(dev, "USB_EVENT_ID received, therefore IAICR is set to its minimum value\n");
1348        if (iaicr != RT9455_IAICR_100MA) {
1349                ret = regmap_field_write(info->regmap_fields[F_IAICR],
1350                                         RT9455_IAICR_100MA);
1351                if (ret) {
1352                        dev_err(dev, "Failed to set IAICR value\n");
1353                        return NOTIFY_DONE;
1354                }
1355        }
1356
1357        return NOTIFY_OK;
1358}
1359
1360static int rt9455_usb_event_charger(struct rt9455_info *info,
1361                                    u8 opa_mode, u8 iaicr)
1362{
1363        struct device *dev = &info->client->dev;
1364        int ret;
1365
1366        if (opa_mode == RT9455_BOOST_MODE) {
1367                ret = rt9455_set_voreg_before_charge_mode(info);
1368                if (ret) {
1369                        dev_err(dev, "Failed to set VOREG before entering charge mode\n");
1370                        return ret;
1371                }
1372                /*
1373                 * If the charger is in boost mode, and it has received
1374                 * USB_EVENT_CHARGER, this means the consumer device powered by
1375                 * the charger is not connected anymore.
1376                 * In this case, the charger goes into charge mode.
1377                 */
1378                dev_dbg(dev, "USB_EVENT_CHARGER received, therefore the charger goes into charge mode\n");
1379                ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1380                                         RT9455_CHARGE_MODE);
1381                if (ret) {
1382                        dev_err(dev, "Failed to set charger in charge mode\n");
1383                        return NOTIFY_DONE;
1384                }
1385        }
1386
1387        dev_dbg(dev, "USB_EVENT_CHARGER received, therefore IAICR is set to no current limit\n");
1388        if (iaicr != RT9455_IAICR_NO_LIMIT) {
1389                ret = regmap_field_write(info->regmap_fields[F_IAICR],
1390                                         RT9455_IAICR_NO_LIMIT);
1391                if (ret) {
1392                        dev_err(dev, "Failed to set IAICR value\n");
1393                        return NOTIFY_DONE;
1394                }
1395        }
1396
1397        return NOTIFY_OK;
1398}
1399
1400static int rt9455_usb_event(struct notifier_block *nb,
1401                            unsigned long event, void *power)
1402{
1403        struct rt9455_info *info = container_of(nb, struct rt9455_info, nb);
1404        struct device *dev = &info->client->dev;
1405        unsigned int opa_mode, iaicr;
1406        int ret;
1407
1408        /*
1409         * Determine whether the charger is in charge mode
1410         * or in boost mode.
1411         */
1412        ret = regmap_field_read(info->regmap_fields[F_OPA_MODE],
1413                                &opa_mode);
1414        if (ret) {
1415                dev_err(dev, "Failed to read OPA_MODE value\n");
1416                return NOTIFY_DONE;
1417        }
1418
1419        ret = regmap_field_read(info->regmap_fields[F_IAICR],
1420                                &iaicr);
1421        if (ret) {
1422                dev_err(dev, "Failed to read IAICR value\n");
1423                return NOTIFY_DONE;
1424        }
1425
1426        dev_dbg(dev, "Received USB event %lu\n", event);
1427        switch (event) {
1428        case USB_EVENT_NONE:
1429                return rt9455_usb_event_none(info, opa_mode, iaicr);
1430        case USB_EVENT_VBUS:
1431                return rt9455_usb_event_vbus(info, opa_mode, iaicr);
1432        case USB_EVENT_ID:
1433                return rt9455_usb_event_id(info, opa_mode, iaicr);
1434        case USB_EVENT_CHARGER:
1435                return rt9455_usb_event_charger(info, opa_mode, iaicr);
1436        default:
1437                dev_err(dev, "Unknown USB event\n");
1438        }
1439        return NOTIFY_DONE;
1440}
1441#endif
1442
1443static void rt9455_pwr_rdy_work_callback(struct work_struct *work)
1444{
1445        struct rt9455_info *info = container_of(work, struct rt9455_info,
1446                                                pwr_rdy_work.work);
1447        struct device *dev = &info->client->dev;
1448        unsigned int pwr_rdy;
1449        int ret;
1450
1451        ret = regmap_field_read(info->regmap_fields[F_PWR_RDY], &pwr_rdy);
1452        if (ret) {
1453                dev_err(dev, "Failed to read PWR_RDY bit\n");
1454                return;
1455        }
1456        switch (pwr_rdy) {
1457        case RT9455_PWR_FAULT:
1458                dev_dbg(dev, "Charger disconnected from power source\n");
1459                cancel_delayed_work_sync(&info->max_charging_time_work);
1460                break;
1461        case RT9455_PWR_GOOD:
1462                dev_dbg(dev, "Charger connected to power source\n");
1463                ret = regmap_field_write(info->regmap_fields[F_CHG_EN],
1464                                         RT9455_CHARGE_ENABLE);
1465                if (ret) {
1466                        dev_err(dev, "Failed to enable charging\n");
1467                        return;
1468                }
1469                queue_delayed_work(system_power_efficient_wq,
1470                                   &info->max_charging_time_work,
1471                                   RT9455_MAX_CHARGING_TIME * HZ);
1472                break;
1473        }
1474        /*
1475         * Notify userspace that the charger has been either connected to or
1476         * disconnected from the power source.
1477         */
1478        power_supply_changed(info->charger);
1479}
1480
1481static void rt9455_max_charging_time_work_callback(struct work_struct *work)
1482{
1483        struct rt9455_info *info = container_of(work, struct rt9455_info,
1484                                                max_charging_time_work.work);
1485        struct device *dev = &info->client->dev;
1486        int ret;
1487
1488        dev_err(dev, "Battery has been charging for at least 6 hours and is not yet fully charged. Battery is dead, therefore charging is disabled.\n");
1489        ret = regmap_field_write(info->regmap_fields[F_CHG_EN],
1490                                 RT9455_CHARGE_DISABLE);
1491        if (ret)
1492                dev_err(dev, "Failed to disable charging\n");
1493}
1494
1495static void rt9455_batt_presence_work_callback(struct work_struct *work)
1496{
1497        struct rt9455_info *info = container_of(work, struct rt9455_info,
1498                                                batt_presence_work.work);
1499        struct device *dev = &info->client->dev;
1500        unsigned int irq1, mask1;
1501        int ret;
1502
1503        ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &irq1);
1504        if (ret) {
1505                dev_err(dev, "Failed to read IRQ1 register\n");
1506                return;
1507        }
1508
1509        /*
1510         * If the battery is still absent, batt_presence_work is rescheduled.
1511         * Otherwise, max_charging_time is scheduled.
1512         */
1513        if (irq1 & GET_MASK(F_BATAB)) {
1514                queue_delayed_work(system_power_efficient_wq,
1515                                   &info->batt_presence_work,
1516                                   RT9455_BATT_PRESENCE_DELAY * HZ);
1517        } else {
1518                queue_delayed_work(system_power_efficient_wq,
1519                                   &info->max_charging_time_work,
1520                                   RT9455_MAX_CHARGING_TIME * HZ);
1521
1522                ret = regmap_read(info->regmap, RT9455_REG_MASK1, &mask1);
1523                if (ret) {
1524                        dev_err(dev, "Failed to read MASK1 register\n");
1525                        return;
1526                }
1527
1528                if (mask1 & GET_MASK(F_BATABM)) {
1529                        ret = regmap_field_write(info->regmap_fields[F_BATABM],
1530                                                 0x00);
1531                        if (ret)
1532                                dev_err(dev, "Failed to unmask BATAB interrupt\n");
1533                }
1534                /*
1535                 * Notify userspace that the battery is now connected to the
1536                 * charger.
1537                 */
1538                power_supply_changed(info->charger);
1539        }
1540}
1541
1542static const struct power_supply_desc rt9455_charger_desc = {
1543        .name                   = RT9455_DRIVER_NAME,
1544        .type                   = POWER_SUPPLY_TYPE_USB,
1545        .properties             = rt9455_charger_properties,
1546        .num_properties         = ARRAY_SIZE(rt9455_charger_properties),
1547        .get_property           = rt9455_charger_get_property,
1548};
1549
1550static bool rt9455_is_writeable_reg(struct device *dev, unsigned int reg)
1551{
1552        switch (reg) {
1553        case RT9455_REG_DEV_ID:
1554        case RT9455_REG_IRQ1:
1555        case RT9455_REG_IRQ2:
1556        case RT9455_REG_IRQ3:
1557                return false;
1558        default:
1559                return true;
1560        }
1561}
1562
1563static bool rt9455_is_volatile_reg(struct device *dev, unsigned int reg)
1564{
1565        switch (reg) {
1566        case RT9455_REG_DEV_ID:
1567        case RT9455_REG_CTRL5:
1568        case RT9455_REG_CTRL6:
1569                return false;
1570        default:
1571                return true;
1572        }
1573}
1574
1575static const struct regmap_config rt9455_regmap_config = {
1576        .reg_bits       = 8,
1577        .val_bits       = 8,
1578        .writeable_reg  = rt9455_is_writeable_reg,
1579        .volatile_reg   = rt9455_is_volatile_reg,
1580        .max_register   = RT9455_REG_MASK3,
1581        .cache_type     = REGCACHE_RBTREE,
1582};
1583
1584static int rt9455_probe(struct i2c_client *client,
1585                        const struct i2c_device_id *id)
1586{
1587        struct i2c_adapter *adapter = client->adapter;
1588        struct device *dev = &client->dev;
1589        struct rt9455_info *info;
1590        struct power_supply_config rt9455_charger_config = {};
1591        /*
1592         * Mandatory device-specific data values. Also, VOREG and boost output
1593         * voltage are mandatory values, but they are stored in rt9455_info
1594         * structure.
1595         */
1596        u32 ichrg, ieoc_percentage;
1597        /* Optional device-specific data values. */
1598        u32 mivr = -1, iaicr = -1;
1599        int i, ret;
1600
1601        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1602                dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
1603                return -ENODEV;
1604        }
1605        info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
1606        if (!info)
1607                return -ENOMEM;
1608
1609        info->client = client;
1610        i2c_set_clientdata(client, info);
1611
1612        info->regmap = devm_regmap_init_i2c(client,
1613                                            &rt9455_regmap_config);
1614        if (IS_ERR(info->regmap)) {
1615                dev_err(dev, "Failed to initialize register map\n");
1616                return -EINVAL;
1617        }
1618
1619        for (i = 0; i < F_MAX_FIELDS; i++) {
1620                info->regmap_fields[i] =
1621                        devm_regmap_field_alloc(dev, info->regmap,
1622                                                rt9455_reg_fields[i]);
1623                if (IS_ERR(info->regmap_fields[i])) {
1624                        dev_err(dev,
1625                                "Failed to allocate regmap field = %d\n", i);
1626                        return PTR_ERR(info->regmap_fields[i]);
1627                }
1628        }
1629
1630        ret = rt9455_discover_charger(info, &ichrg, &ieoc_percentage,
1631                                      &mivr, &iaicr);
1632        if (ret) {
1633                dev_err(dev, "Failed to discover charger\n");
1634                return ret;
1635        }
1636
1637#if IS_ENABLED(CONFIG_USB_PHY)
1638        info->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1639        if (IS_ERR(info->usb_phy)) {
1640                dev_err(dev, "Failed to get USB transceiver\n");
1641        } else {
1642                info->nb.notifier_call = rt9455_usb_event;
1643                ret = usb_register_notifier(info->usb_phy, &info->nb);
1644                if (ret) {
1645                        dev_err(dev, "Failed to register USB notifier\n");
1646                        /*
1647                         * If usb_register_notifier() fails, set notifier_call
1648                         * to NULL, to avoid calling usb_unregister_notifier().
1649                         */
1650                        info->nb.notifier_call = NULL;
1651                }
1652        }
1653#endif
1654
1655        INIT_DEFERRABLE_WORK(&info->pwr_rdy_work, rt9455_pwr_rdy_work_callback);
1656        INIT_DEFERRABLE_WORK(&info->max_charging_time_work,
1657                             rt9455_max_charging_time_work_callback);
1658        INIT_DEFERRABLE_WORK(&info->batt_presence_work,
1659                             rt9455_batt_presence_work_callback);
1660
1661        rt9455_charger_config.of_node           = dev->of_node;
1662        rt9455_charger_config.drv_data          = info;
1663        rt9455_charger_config.supplied_to       = rt9455_charger_supplied_to;
1664        rt9455_charger_config.num_supplicants   =
1665                                        ARRAY_SIZE(rt9455_charger_supplied_to);
1666        ret = devm_request_threaded_irq(dev, client->irq, NULL,
1667                                        rt9455_irq_handler_thread,
1668                                        IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1669                                        RT9455_DRIVER_NAME, info);
1670        if (ret) {
1671                dev_err(dev, "Failed to register IRQ handler\n");
1672                goto put_usb_notifier;
1673        }
1674
1675        ret = rt9455_hw_init(info, ichrg, ieoc_percentage, mivr, iaicr);
1676        if (ret) {
1677                dev_err(dev, "Failed to set charger to its default values\n");
1678                goto put_usb_notifier;
1679        }
1680
1681        info->charger = devm_power_supply_register(dev, &rt9455_charger_desc,
1682                                                   &rt9455_charger_config);
1683        if (IS_ERR(info->charger)) {
1684                dev_err(dev, "Failed to register charger\n");
1685                ret = PTR_ERR(info->charger);
1686                goto put_usb_notifier;
1687        }
1688
1689        return 0;
1690
1691put_usb_notifier:
1692#if IS_ENABLED(CONFIG_USB_PHY)
1693        if (info->nb.notifier_call)  {
1694                usb_unregister_notifier(info->usb_phy, &info->nb);
1695                info->nb.notifier_call = NULL;
1696        }
1697#endif
1698        return ret;
1699}
1700
1701static int rt9455_remove(struct i2c_client *client)
1702{
1703        int ret;
1704        struct rt9455_info *info = i2c_get_clientdata(client);
1705
1706        ret = rt9455_register_reset(info);
1707        if (ret)
1708                dev_err(&info->client->dev, "Failed to set charger to its default values\n");
1709
1710#if IS_ENABLED(CONFIG_USB_PHY)
1711        if (info->nb.notifier_call)
1712                usb_unregister_notifier(info->usb_phy, &info->nb);
1713#endif
1714
1715        cancel_delayed_work_sync(&info->pwr_rdy_work);
1716        cancel_delayed_work_sync(&info->max_charging_time_work);
1717        cancel_delayed_work_sync(&info->batt_presence_work);
1718
1719        return ret;
1720}
1721
1722static const struct i2c_device_id rt9455_i2c_id_table[] = {
1723        { RT9455_DRIVER_NAME, 0 },
1724        { },
1725};
1726MODULE_DEVICE_TABLE(i2c, rt9455_i2c_id_table);
1727
1728static const struct of_device_id rt9455_of_match[] = {
1729        { .compatible = "richtek,rt9455", },
1730        { },
1731};
1732MODULE_DEVICE_TABLE(of, rt9455_of_match);
1733
1734static const struct acpi_device_id rt9455_i2c_acpi_match[] = {
1735        { "RT945500", 0 },
1736        { }
1737};
1738MODULE_DEVICE_TABLE(acpi, rt9455_i2c_acpi_match);
1739
1740static struct i2c_driver rt9455_driver = {
1741        .probe          = rt9455_probe,
1742        .remove         = rt9455_remove,
1743        .id_table       = rt9455_i2c_id_table,
1744        .driver = {
1745                .name           = RT9455_DRIVER_NAME,
1746                .of_match_table = of_match_ptr(rt9455_of_match),
1747                .acpi_match_table = ACPI_PTR(rt9455_i2c_acpi_match),
1748        },
1749};
1750module_i2c_driver(rt9455_driver);
1751
1752MODULE_LICENSE("GPL");
1753MODULE_AUTHOR("Anda-Maria Nicolae <anda-maria.nicolae@intel.com>");
1754MODULE_DESCRIPTION("Richtek RT9455 Charger Driver");
1755