Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * TI BQ25890 charger driver
0004  *
0005  * Copyright (C) 2015 Intel Corporation
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/i2c.h>
0010 #include <linux/power_supply.h>
0011 #include <linux/power/bq25890_charger.h>
0012 #include <linux/regmap.h>
0013 #include <linux/regulator/driver.h>
0014 #include <linux/types.h>
0015 #include <linux/gpio/consumer.h>
0016 #include <linux/interrupt.h>
0017 #include <linux/delay.h>
0018 #include <linux/usb/phy.h>
0019 
0020 #include <linux/acpi.h>
0021 #include <linux/of.h>
0022 
0023 #define BQ25890_MANUFACTURER        "Texas Instruments"
0024 #define BQ25890_IRQ_PIN         "bq25890_irq"
0025 
0026 #define BQ25890_ID          3
0027 #define BQ25895_ID          7
0028 #define BQ25896_ID          0
0029 
0030 #define PUMP_EXPRESS_START_DELAY    (5 * HZ)
0031 #define PUMP_EXPRESS_MAX_TRIES      6
0032 #define PUMP_EXPRESS_VBUS_MARGIN_uV 1000000
0033 
0034 enum bq25890_chip_version {
0035     BQ25890,
0036     BQ25892,
0037     BQ25895,
0038     BQ25896,
0039 };
0040 
0041 static const char *const bq25890_chip_name[] = {
0042     "BQ25890",
0043     "BQ25892",
0044     "BQ25895",
0045     "BQ25896",
0046 };
0047 
0048 enum bq25890_fields {
0049     F_EN_HIZ, F_EN_ILIM, F_IINLIM,                   /* Reg00 */
0050     F_BHOT, F_BCOLD, F_VINDPM_OFS,                   /* Reg01 */
0051     F_CONV_START, F_CONV_RATE, F_BOOSTF, F_ICO_EN,
0052     F_HVDCP_EN, F_MAXC_EN, F_FORCE_DPM, F_AUTO_DPDM_EN,      /* Reg02 */
0053     F_BAT_LOAD_EN, F_WD_RST, F_OTG_CFG, F_CHG_CFG, F_SYSVMIN,
0054     F_MIN_VBAT_SEL,                          /* Reg03 */
0055     F_PUMPX_EN, F_ICHG,                      /* Reg04 */
0056     F_IPRECHG, F_ITERM,                      /* Reg05 */
0057     F_VREG, F_BATLOWV, F_VRECHG,                     /* Reg06 */
0058     F_TERM_EN, F_STAT_DIS, F_WD, F_TMR_EN, F_CHG_TMR,
0059     F_JEITA_ISET,                            /* Reg07 */
0060     F_BATCMP, F_VCLAMP, F_TREG,                  /* Reg08 */
0061     F_FORCE_ICO, F_TMR2X_EN, F_BATFET_DIS, F_JEITA_VSET,
0062     F_BATFET_DLY, F_BATFET_RST_EN, F_PUMPX_UP, F_PUMPX_DN,       /* Reg09 */
0063     F_BOOSTV, F_PFM_OTG_DIS, F_BOOSTI,               /* Reg0A */
0064     F_VBUS_STAT, F_CHG_STAT, F_PG_STAT, F_SDP_STAT, F_0B_RSVD,
0065     F_VSYS_STAT,                             /* Reg0B */
0066     F_WD_FAULT, F_BOOST_FAULT, F_CHG_FAULT, F_BAT_FAULT,
0067     F_NTC_FAULT,                             /* Reg0C */
0068     F_FORCE_VINDPM, F_VINDPM,                    /* Reg0D */
0069     F_THERM_STAT, F_BATV,                        /* Reg0E */
0070     F_SYSV,                              /* Reg0F */
0071     F_TSPCT,                             /* Reg10 */
0072     F_VBUS_GD, F_VBUSV,                      /* Reg11 */
0073     F_ICHGR,                             /* Reg12 */
0074     F_VDPM_STAT, F_IDPM_STAT, F_IDPM_LIM,                /* Reg13 */
0075     F_REG_RST, F_ICO_OPTIMIZED, F_PN, F_TS_PROFILE, F_DEV_REV,   /* Reg14 */
0076 
0077     F_MAX_FIELDS
0078 };
0079 
0080 /* initial field values, converted to register values */
0081 struct bq25890_init_data {
0082     u8 ichg;    /* charge current       */
0083     u8 vreg;    /* regulation voltage       */
0084     u8 iterm;   /* termination current      */
0085     u8 iprechg; /* precharge current        */
0086     u8 sysvmin; /* minimum system voltage limit */
0087     u8 boostv;  /* boost regulation voltage */
0088     u8 boosti;  /* boost current limit      */
0089     u8 boostf;  /* boost frequency      */
0090     u8 ilim_en; /* enable ILIM pin      */
0091     u8 treg;    /* thermal regulation threshold */
0092     u8 rbatcomp;    /* IBAT sense resistor value    */
0093     u8 vclamp;  /* IBAT compensation voltage limit */
0094 };
0095 
0096 struct bq25890_state {
0097     u8 online;
0098     u8 chrg_status;
0099     u8 chrg_fault;
0100     u8 vsys_status;
0101     u8 boost_fault;
0102     u8 bat_fault;
0103     u8 ntc_fault;
0104 };
0105 
0106 struct bq25890_device {
0107     struct i2c_client *client;
0108     struct device *dev;
0109     struct power_supply *charger;
0110 
0111     struct usb_phy *usb_phy;
0112     struct notifier_block usb_nb;
0113     struct work_struct usb_work;
0114     struct delayed_work pump_express_work;
0115     unsigned long usb_event;
0116 
0117     struct regmap *rmap;
0118     struct regmap_field *rmap_fields[F_MAX_FIELDS];
0119 
0120     bool skip_reset;
0121     bool read_back_init_data;
0122     u32 pump_express_vbus_max;
0123     enum bq25890_chip_version chip_version;
0124     struct bq25890_init_data init_data;
0125     struct bq25890_state state;
0126 
0127     struct mutex lock; /* protect state data */
0128 };
0129 
0130 static const struct regmap_range bq25890_readonly_reg_ranges[] = {
0131     regmap_reg_range(0x0b, 0x0c),
0132     regmap_reg_range(0x0e, 0x13),
0133 };
0134 
0135 static const struct regmap_access_table bq25890_writeable_regs = {
0136     .no_ranges = bq25890_readonly_reg_ranges,
0137     .n_no_ranges = ARRAY_SIZE(bq25890_readonly_reg_ranges),
0138 };
0139 
0140 static const struct regmap_range bq25890_volatile_reg_ranges[] = {
0141     regmap_reg_range(0x00, 0x00),
0142     regmap_reg_range(0x02, 0x02),
0143     regmap_reg_range(0x09, 0x09),
0144     regmap_reg_range(0x0b, 0x14),
0145 };
0146 
0147 static const struct regmap_access_table bq25890_volatile_regs = {
0148     .yes_ranges = bq25890_volatile_reg_ranges,
0149     .n_yes_ranges = ARRAY_SIZE(bq25890_volatile_reg_ranges),
0150 };
0151 
0152 static const struct regmap_config bq25890_regmap_config = {
0153     .reg_bits = 8,
0154     .val_bits = 8,
0155 
0156     .max_register = 0x14,
0157     .cache_type = REGCACHE_RBTREE,
0158 
0159     .wr_table = &bq25890_writeable_regs,
0160     .volatile_table = &bq25890_volatile_regs,
0161 };
0162 
0163 static const struct reg_field bq25890_reg_fields[] = {
0164     /* REG00 */
0165     [F_EN_HIZ]      = REG_FIELD(0x00, 7, 7),
0166     [F_EN_ILIM]     = REG_FIELD(0x00, 6, 6),
0167     [F_IINLIM]      = REG_FIELD(0x00, 0, 5),
0168     /* REG01 */
0169     [F_BHOT]        = REG_FIELD(0x01, 6, 7),
0170     [F_BCOLD]       = REG_FIELD(0x01, 5, 5),
0171     [F_VINDPM_OFS]      = REG_FIELD(0x01, 0, 4),
0172     /* REG02 */
0173     [F_CONV_START]      = REG_FIELD(0x02, 7, 7),
0174     [F_CONV_RATE]       = REG_FIELD(0x02, 6, 6),
0175     [F_BOOSTF]      = REG_FIELD(0x02, 5, 5),
0176     [F_ICO_EN]      = REG_FIELD(0x02, 4, 4),
0177     [F_HVDCP_EN]        = REG_FIELD(0x02, 3, 3),  // reserved on BQ25896
0178     [F_MAXC_EN]     = REG_FIELD(0x02, 2, 2),  // reserved on BQ25896
0179     [F_FORCE_DPM]       = REG_FIELD(0x02, 1, 1),
0180     [F_AUTO_DPDM_EN]    = REG_FIELD(0x02, 0, 0),
0181     /* REG03 */
0182     [F_BAT_LOAD_EN]     = REG_FIELD(0x03, 7, 7),
0183     [F_WD_RST]      = REG_FIELD(0x03, 6, 6),
0184     [F_OTG_CFG]     = REG_FIELD(0x03, 5, 5),
0185     [F_CHG_CFG]     = REG_FIELD(0x03, 4, 4),
0186     [F_SYSVMIN]     = REG_FIELD(0x03, 1, 3),
0187     [F_MIN_VBAT_SEL]    = REG_FIELD(0x03, 0, 0), // BQ25896 only
0188     /* REG04 */
0189     [F_PUMPX_EN]        = REG_FIELD(0x04, 7, 7),
0190     [F_ICHG]        = REG_FIELD(0x04, 0, 6),
0191     /* REG05 */
0192     [F_IPRECHG]     = REG_FIELD(0x05, 4, 7),
0193     [F_ITERM]       = REG_FIELD(0x05, 0, 3),
0194     /* REG06 */
0195     [F_VREG]        = REG_FIELD(0x06, 2, 7),
0196     [F_BATLOWV]     = REG_FIELD(0x06, 1, 1),
0197     [F_VRECHG]      = REG_FIELD(0x06, 0, 0),
0198     /* REG07 */
0199     [F_TERM_EN]     = REG_FIELD(0x07, 7, 7),
0200     [F_STAT_DIS]        = REG_FIELD(0x07, 6, 6),
0201     [F_WD]          = REG_FIELD(0x07, 4, 5),
0202     [F_TMR_EN]      = REG_FIELD(0x07, 3, 3),
0203     [F_CHG_TMR]     = REG_FIELD(0x07, 1, 2),
0204     [F_JEITA_ISET]      = REG_FIELD(0x07, 0, 0), // reserved on BQ25895
0205     /* REG08 */
0206     [F_BATCMP]      = REG_FIELD(0x08, 5, 7),
0207     [F_VCLAMP]      = REG_FIELD(0x08, 2, 4),
0208     [F_TREG]        = REG_FIELD(0x08, 0, 1),
0209     /* REG09 */
0210     [F_FORCE_ICO]       = REG_FIELD(0x09, 7, 7),
0211     [F_TMR2X_EN]        = REG_FIELD(0x09, 6, 6),
0212     [F_BATFET_DIS]      = REG_FIELD(0x09, 5, 5),
0213     [F_JEITA_VSET]      = REG_FIELD(0x09, 4, 4), // reserved on BQ25895
0214     [F_BATFET_DLY]      = REG_FIELD(0x09, 3, 3),
0215     [F_BATFET_RST_EN]   = REG_FIELD(0x09, 2, 2),
0216     [F_PUMPX_UP]        = REG_FIELD(0x09, 1, 1),
0217     [F_PUMPX_DN]        = REG_FIELD(0x09, 0, 0),
0218     /* REG0A */
0219     [F_BOOSTV]      = REG_FIELD(0x0A, 4, 7),
0220     [F_BOOSTI]      = REG_FIELD(0x0A, 0, 2), // reserved on BQ25895
0221     [F_PFM_OTG_DIS]     = REG_FIELD(0x0A, 3, 3), // BQ25896 only
0222     /* REG0B */
0223     [F_VBUS_STAT]       = REG_FIELD(0x0B, 5, 7),
0224     [F_CHG_STAT]        = REG_FIELD(0x0B, 3, 4),
0225     [F_PG_STAT]     = REG_FIELD(0x0B, 2, 2),
0226     [F_SDP_STAT]        = REG_FIELD(0x0B, 1, 1), // reserved on BQ25896
0227     [F_VSYS_STAT]       = REG_FIELD(0x0B, 0, 0),
0228     /* REG0C */
0229     [F_WD_FAULT]        = REG_FIELD(0x0C, 7, 7),
0230     [F_BOOST_FAULT]     = REG_FIELD(0x0C, 6, 6),
0231     [F_CHG_FAULT]       = REG_FIELD(0x0C, 4, 5),
0232     [F_BAT_FAULT]       = REG_FIELD(0x0C, 3, 3),
0233     [F_NTC_FAULT]       = REG_FIELD(0x0C, 0, 2),
0234     /* REG0D */
0235     [F_FORCE_VINDPM]    = REG_FIELD(0x0D, 7, 7),
0236     [F_VINDPM]      = REG_FIELD(0x0D, 0, 6),
0237     /* REG0E */
0238     [F_THERM_STAT]      = REG_FIELD(0x0E, 7, 7),
0239     [F_BATV]        = REG_FIELD(0x0E, 0, 6),
0240     /* REG0F */
0241     [F_SYSV]        = REG_FIELD(0x0F, 0, 6),
0242     /* REG10 */
0243     [F_TSPCT]       = REG_FIELD(0x10, 0, 6),
0244     /* REG11 */
0245     [F_VBUS_GD]     = REG_FIELD(0x11, 7, 7),
0246     [F_VBUSV]       = REG_FIELD(0x11, 0, 6),
0247     /* REG12 */
0248     [F_ICHGR]       = REG_FIELD(0x12, 0, 6),
0249     /* REG13 */
0250     [F_VDPM_STAT]       = REG_FIELD(0x13, 7, 7),
0251     [F_IDPM_STAT]       = REG_FIELD(0x13, 6, 6),
0252     [F_IDPM_LIM]        = REG_FIELD(0x13, 0, 5),
0253     /* REG14 */
0254     [F_REG_RST]     = REG_FIELD(0x14, 7, 7),
0255     [F_ICO_OPTIMIZED]   = REG_FIELD(0x14, 6, 6),
0256     [F_PN]          = REG_FIELD(0x14, 3, 5),
0257     [F_TS_PROFILE]      = REG_FIELD(0x14, 2, 2),
0258     [F_DEV_REV]     = REG_FIELD(0x14, 0, 1)
0259 };
0260 
0261 /*
0262  * Most of the val -> idx conversions can be computed, given the minimum,
0263  * maximum and the step between values. For the rest of conversions, we use
0264  * lookup tables.
0265  */
0266 enum bq25890_table_ids {
0267     /* range tables */
0268     TBL_ICHG,
0269     TBL_ITERM,
0270     TBL_IINLIM,
0271     TBL_VREG,
0272     TBL_BOOSTV,
0273     TBL_SYSVMIN,
0274     TBL_VBUSV,
0275     TBL_VBATCOMP,
0276     TBL_RBATCOMP,
0277 
0278     /* lookup tables */
0279     TBL_TREG,
0280     TBL_BOOSTI,
0281     TBL_TSPCT,
0282 };
0283 
0284 /* Thermal Regulation Threshold lookup table, in degrees Celsius */
0285 static const u32 bq25890_treg_tbl[] = { 60, 80, 100, 120 };
0286 
0287 #define BQ25890_TREG_TBL_SIZE       ARRAY_SIZE(bq25890_treg_tbl)
0288 
0289 /* Boost mode current limit lookup table, in uA */
0290 static const u32 bq25890_boosti_tbl[] = {
0291     500000, 700000, 1100000, 1300000, 1600000, 1800000, 2100000, 2400000
0292 };
0293 
0294 #define BQ25890_BOOSTI_TBL_SIZE     ARRAY_SIZE(bq25890_boosti_tbl)
0295 
0296 /* NTC 10K temperature lookup table in tenths of a degree */
0297 static const u32 bq25890_tspct_tbl[] = {
0298     850, 840, 830, 820, 810, 800, 790, 780,
0299     770, 760, 750, 740, 730, 720, 710, 700,
0300     690, 685, 680, 675, 670, 660, 650, 645,
0301     640, 630, 620, 615, 610, 600, 590, 585,
0302     580, 570, 565, 560, 550, 540, 535, 530,
0303     520, 515, 510, 500, 495, 490, 480, 475,
0304     470, 460, 455, 450, 440, 435, 430, 425,
0305     420, 410, 405, 400, 390, 385, 380, 370,
0306     365, 360, 355, 350, 340, 335, 330, 320,
0307     310, 305, 300, 290, 285, 280, 275, 270,
0308     260, 250, 245, 240, 230, 225, 220, 210,
0309     205, 200, 190, 180, 175, 170, 160, 150,
0310     145, 140, 130, 120, 115, 110, 100, 90,
0311     80, 70, 60, 50, 40, 30, 20, 10,
0312     0, -10, -20, -30, -40, -60, -70, -80,
0313     -90, -10, -120, -140, -150, -170, -190, -210,
0314 };
0315 
0316 #define BQ25890_TSPCT_TBL_SIZE      ARRAY_SIZE(bq25890_tspct_tbl)
0317 
0318 struct bq25890_range {
0319     u32 min;
0320     u32 max;
0321     u32 step;
0322 };
0323 
0324 struct bq25890_lookup {
0325     const u32 *tbl;
0326     u32 size;
0327 };
0328 
0329 static const union {
0330     struct bq25890_range  rt;
0331     struct bq25890_lookup lt;
0332 } bq25890_tables[] = {
0333     /* range tables */
0334     /* TODO: BQ25896 has max ICHG 3008 mA */
0335     [TBL_ICHG] =     { .rt = {0,        5056000, 64000} },   /* uA */
0336     [TBL_ITERM] =    { .rt = {64000,    1024000, 64000} },   /* uA */
0337     [TBL_IINLIM] =   { .rt = {100000,   3250000, 50000} },   /* uA */
0338     [TBL_VREG] =     { .rt = {3840000,  4608000, 16000} },   /* uV */
0339     [TBL_BOOSTV] =   { .rt = {4550000,  5510000, 64000} },   /* uV */
0340     [TBL_SYSVMIN] =  { .rt = {3000000,  3700000, 100000} },  /* uV */
0341     [TBL_VBUSV] =    { .rt = {2600000, 15300000, 100000} },  /* uV */
0342     [TBL_VBATCOMP] = { .rt = {0,         224000, 32000} },   /* uV */
0343     [TBL_RBATCOMP] = { .rt = {0,         140000, 20000} },   /* uOhm */
0344 
0345     /* lookup tables */
0346     [TBL_TREG] =    { .lt = {bq25890_treg_tbl, BQ25890_TREG_TBL_SIZE} },
0347     [TBL_BOOSTI] =  { .lt = {bq25890_boosti_tbl, BQ25890_BOOSTI_TBL_SIZE} },
0348     [TBL_TSPCT] =   { .lt = {bq25890_tspct_tbl, BQ25890_TSPCT_TBL_SIZE} }
0349 };
0350 
0351 static int bq25890_field_read(struct bq25890_device *bq,
0352                   enum bq25890_fields field_id)
0353 {
0354     int ret;
0355     int val;
0356 
0357     ret = regmap_field_read(bq->rmap_fields[field_id], &val);
0358     if (ret < 0)
0359         return ret;
0360 
0361     return val;
0362 }
0363 
0364 static int bq25890_field_write(struct bq25890_device *bq,
0365                    enum bq25890_fields field_id, u8 val)
0366 {
0367     return regmap_field_write(bq->rmap_fields[field_id], val);
0368 }
0369 
0370 static u8 bq25890_find_idx(u32 value, enum bq25890_table_ids id)
0371 {
0372     u8 idx;
0373 
0374     if (id >= TBL_TREG) {
0375         const u32 *tbl = bq25890_tables[id].lt.tbl;
0376         u32 tbl_size = bq25890_tables[id].lt.size;
0377 
0378         for (idx = 1; idx < tbl_size && tbl[idx] <= value; idx++)
0379             ;
0380     } else {
0381         const struct bq25890_range *rtbl = &bq25890_tables[id].rt;
0382         u8 rtbl_size;
0383 
0384         rtbl_size = (rtbl->max - rtbl->min) / rtbl->step + 1;
0385 
0386         for (idx = 1;
0387              idx < rtbl_size && (idx * rtbl->step + rtbl->min <= value);
0388              idx++)
0389             ;
0390     }
0391 
0392     return idx - 1;
0393 }
0394 
0395 static u32 bq25890_find_val(u8 idx, enum bq25890_table_ids id)
0396 {
0397     const struct bq25890_range *rtbl;
0398 
0399     /* lookup table? */
0400     if (id >= TBL_TREG)
0401         return bq25890_tables[id].lt.tbl[idx];
0402 
0403     /* range table */
0404     rtbl = &bq25890_tables[id].rt;
0405 
0406     return (rtbl->min + idx * rtbl->step);
0407 }
0408 
0409 enum bq25890_status {
0410     STATUS_NOT_CHARGING,
0411     STATUS_PRE_CHARGING,
0412     STATUS_FAST_CHARGING,
0413     STATUS_TERMINATION_DONE,
0414 };
0415 
0416 enum bq25890_chrg_fault {
0417     CHRG_FAULT_NORMAL,
0418     CHRG_FAULT_INPUT,
0419     CHRG_FAULT_THERMAL_SHUTDOWN,
0420     CHRG_FAULT_TIMER_EXPIRED,
0421 };
0422 
0423 enum bq25890_ntc_fault {
0424     NTC_FAULT_NORMAL = 0,
0425     NTC_FAULT_WARM = 2,
0426     NTC_FAULT_COOL = 3,
0427     NTC_FAULT_COLD = 5,
0428     NTC_FAULT_HOT = 6,
0429 };
0430 
0431 static bool bq25890_is_adc_property(enum power_supply_property psp)
0432 {
0433     switch (psp) {
0434     case POWER_SUPPLY_PROP_VOLTAGE_NOW:
0435     case POWER_SUPPLY_PROP_CURRENT_NOW:
0436     case POWER_SUPPLY_PROP_TEMP:
0437         return true;
0438 
0439     default:
0440         return false;
0441     }
0442 }
0443 
0444 static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq);
0445 
0446 static int bq25890_get_vbus_voltage(struct bq25890_device *bq)
0447 {
0448     int ret;
0449 
0450     ret = bq25890_field_read(bq, F_VBUSV);
0451     if (ret < 0)
0452         return ret;
0453 
0454     return bq25890_find_val(ret, TBL_VBUSV);
0455 }
0456 
0457 static int bq25890_power_supply_get_property(struct power_supply *psy,
0458                          enum power_supply_property psp,
0459                          union power_supply_propval *val)
0460 {
0461     struct bq25890_device *bq = power_supply_get_drvdata(psy);
0462     struct bq25890_state state;
0463     bool do_adc_conv;
0464     int ret;
0465 
0466     mutex_lock(&bq->lock);
0467     /* update state in case we lost an interrupt */
0468     __bq25890_handle_irq(bq);
0469     state = bq->state;
0470     do_adc_conv = !state.online && bq25890_is_adc_property(psp);
0471     if (do_adc_conv)
0472         bq25890_field_write(bq, F_CONV_START, 1);
0473     mutex_unlock(&bq->lock);
0474 
0475     if (do_adc_conv)
0476         regmap_field_read_poll_timeout(bq->rmap_fields[F_CONV_START],
0477             ret, !ret, 25000, 1000000);
0478 
0479     switch (psp) {
0480     case POWER_SUPPLY_PROP_STATUS:
0481         if (!state.online)
0482             val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
0483         else if (state.chrg_status == STATUS_NOT_CHARGING)
0484             val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
0485         else if (state.chrg_status == STATUS_PRE_CHARGING ||
0486              state.chrg_status == STATUS_FAST_CHARGING)
0487             val->intval = POWER_SUPPLY_STATUS_CHARGING;
0488         else if (state.chrg_status == STATUS_TERMINATION_DONE)
0489             val->intval = POWER_SUPPLY_STATUS_FULL;
0490         else
0491             val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
0492 
0493         break;
0494 
0495     case POWER_SUPPLY_PROP_CHARGE_TYPE:
0496         if (!state.online || state.chrg_status == STATUS_NOT_CHARGING ||
0497             state.chrg_status == STATUS_TERMINATION_DONE)
0498             val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
0499         else if (state.chrg_status == STATUS_PRE_CHARGING)
0500             val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
0501         else if (state.chrg_status == STATUS_FAST_CHARGING)
0502             val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
0503         else /* unreachable */
0504             val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
0505         break;
0506 
0507     case POWER_SUPPLY_PROP_MANUFACTURER:
0508         val->strval = BQ25890_MANUFACTURER;
0509         break;
0510 
0511     case POWER_SUPPLY_PROP_MODEL_NAME:
0512         val->strval = bq25890_chip_name[bq->chip_version];
0513         break;
0514 
0515     case POWER_SUPPLY_PROP_ONLINE:
0516         val->intval = state.online;
0517         break;
0518 
0519     case POWER_SUPPLY_PROP_HEALTH:
0520         if (!state.chrg_fault && !state.bat_fault && !state.boost_fault)
0521             val->intval = POWER_SUPPLY_HEALTH_GOOD;
0522         else if (state.bat_fault)
0523             val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
0524         else if (state.chrg_fault == CHRG_FAULT_TIMER_EXPIRED)
0525             val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
0526         else if (state.chrg_fault == CHRG_FAULT_THERMAL_SHUTDOWN)
0527             val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
0528         else
0529             val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
0530         break;
0531 
0532     case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
0533         val->intval = bq25890_find_val(bq->init_data.ichg, TBL_ICHG);
0534 
0535         /* When temperature is too low, charge current is decreased */
0536         if (bq->state.ntc_fault == NTC_FAULT_COOL) {
0537             ret = bq25890_field_read(bq, F_JEITA_ISET);
0538             if (ret < 0)
0539                 return ret;
0540 
0541             if (ret)
0542                 val->intval /= 5;
0543             else
0544                 val->intval /= 2;
0545         }
0546         break;
0547 
0548     case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
0549         if (!state.online) {
0550             val->intval = 0;
0551             break;
0552         }
0553 
0554         ret = bq25890_field_read(bq, F_BATV); /* read measured value */
0555         if (ret < 0)
0556             return ret;
0557 
0558         /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
0559         val->intval = 2304000 + ret * 20000;
0560         break;
0561 
0562     case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
0563         val->intval = bq25890_find_val(bq->init_data.vreg, TBL_VREG);
0564         break;
0565 
0566     case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
0567         val->intval = bq25890_find_val(bq->init_data.iprechg, TBL_ITERM);
0568         break;
0569 
0570     case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
0571         val->intval = bq25890_find_val(bq->init_data.iterm, TBL_ITERM);
0572         break;
0573 
0574     case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
0575         ret = bq25890_field_read(bq, F_IINLIM);
0576         if (ret < 0)
0577             return ret;
0578 
0579         val->intval = bq25890_find_val(ret, TBL_IINLIM);
0580         break;
0581 
0582     case POWER_SUPPLY_PROP_VOLTAGE_NOW:
0583         ret = bq25890_field_read(bq, F_SYSV); /* read measured value */
0584         if (ret < 0)
0585             return ret;
0586 
0587         /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
0588         val->intval = 2304000 + ret * 20000;
0589         break;
0590 
0591     case POWER_SUPPLY_PROP_CURRENT_NOW:
0592         ret = bq25890_field_read(bq, F_ICHGR); /* read measured value */
0593         if (ret < 0)
0594             return ret;
0595 
0596         /* converted_val = ADC_val * 50mA (table 10.3.19) */
0597         val->intval = ret * -50000;
0598         break;
0599 
0600     case POWER_SUPPLY_PROP_TEMP:
0601         ret = bq25890_field_read(bq, F_TSPCT);
0602         if (ret < 0)
0603             return ret;
0604 
0605         /* convert TS percentage into rough temperature */
0606         val->intval = bq25890_find_val(ret, TBL_TSPCT);
0607         break;
0608 
0609     default:
0610         return -EINVAL;
0611     }
0612 
0613     return 0;
0614 }
0615 
0616 /* On the BQ25892 try to get charger-type info from our supplier */
0617 static void bq25890_charger_external_power_changed(struct power_supply *psy)
0618 {
0619     struct bq25890_device *bq = power_supply_get_drvdata(psy);
0620     union power_supply_propval val;
0621     int input_current_limit, ret;
0622 
0623     if (bq->chip_version != BQ25892)
0624         return;
0625 
0626     ret = power_supply_get_property_from_supplier(bq->charger,
0627                               POWER_SUPPLY_PROP_USB_TYPE,
0628                               &val);
0629     if (ret)
0630         return;
0631 
0632     switch (val.intval) {
0633     case POWER_SUPPLY_USB_TYPE_DCP:
0634         input_current_limit = bq25890_find_idx(2000000, TBL_IINLIM);
0635         if (bq->pump_express_vbus_max) {
0636             queue_delayed_work(system_power_efficient_wq,
0637                        &bq->pump_express_work,
0638                        PUMP_EXPRESS_START_DELAY);
0639         }
0640         break;
0641     case POWER_SUPPLY_USB_TYPE_CDP:
0642     case POWER_SUPPLY_USB_TYPE_ACA:
0643         input_current_limit = bq25890_find_idx(1500000, TBL_IINLIM);
0644         break;
0645     case POWER_SUPPLY_USB_TYPE_SDP:
0646     default:
0647         input_current_limit = bq25890_find_idx(500000, TBL_IINLIM);
0648     }
0649 
0650     bq25890_field_write(bq, F_IINLIM, input_current_limit);
0651 }
0652 
0653 static int bq25890_get_chip_state(struct bq25890_device *bq,
0654                   struct bq25890_state *state)
0655 {
0656     int i, ret;
0657 
0658     struct {
0659         enum bq25890_fields id;
0660         u8 *data;
0661     } state_fields[] = {
0662         {F_CHG_STAT,    &state->chrg_status},
0663         {F_PG_STAT, &state->online},
0664         {F_VSYS_STAT,   &state->vsys_status},
0665         {F_BOOST_FAULT, &state->boost_fault},
0666         {F_BAT_FAULT,   &state->bat_fault},
0667         {F_CHG_FAULT,   &state->chrg_fault},
0668         {F_NTC_FAULT,   &state->ntc_fault}
0669     };
0670 
0671     for (i = 0; i < ARRAY_SIZE(state_fields); i++) {
0672         ret = bq25890_field_read(bq, state_fields[i].id);
0673         if (ret < 0)
0674             return ret;
0675 
0676         *state_fields[i].data = ret;
0677     }
0678 
0679     dev_dbg(bq->dev, "S:CHG/PG/VSYS=%d/%d/%d, F:CHG/BOOST/BAT/NTC=%d/%d/%d/%d\n",
0680         state->chrg_status, state->online, state->vsys_status,
0681         state->chrg_fault, state->boost_fault, state->bat_fault,
0682         state->ntc_fault);
0683 
0684     return 0;
0685 }
0686 
0687 static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq)
0688 {
0689     struct bq25890_state new_state;
0690     int ret;
0691 
0692     ret = bq25890_get_chip_state(bq, &new_state);
0693     if (ret < 0)
0694         return IRQ_NONE;
0695 
0696     if (!memcmp(&bq->state, &new_state, sizeof(new_state)))
0697         return IRQ_NONE;
0698 
0699     if (!new_state.online && bq->state.online) {        /* power removed */
0700         /* disable ADC */
0701         ret = bq25890_field_write(bq, F_CONV_RATE, 0);
0702         if (ret < 0)
0703             goto error;
0704     } else if (new_state.online && !bq->state.online) { /* power inserted */
0705         /* enable ADC, to have control of charge current/voltage */
0706         ret = bq25890_field_write(bq, F_CONV_RATE, 1);
0707         if (ret < 0)
0708             goto error;
0709     }
0710 
0711     bq->state = new_state;
0712     power_supply_changed(bq->charger);
0713 
0714     return IRQ_HANDLED;
0715 error:
0716     dev_err(bq->dev, "Error communicating with the chip: %pe\n",
0717         ERR_PTR(ret));
0718     return IRQ_HANDLED;
0719 }
0720 
0721 static irqreturn_t bq25890_irq_handler_thread(int irq, void *private)
0722 {
0723     struct bq25890_device *bq = private;
0724     irqreturn_t ret;
0725 
0726     mutex_lock(&bq->lock);
0727     ret = __bq25890_handle_irq(bq);
0728     mutex_unlock(&bq->lock);
0729 
0730     return ret;
0731 }
0732 
0733 static int bq25890_chip_reset(struct bq25890_device *bq)
0734 {
0735     int ret;
0736     int rst_check_counter = 10;
0737 
0738     ret = bq25890_field_write(bq, F_REG_RST, 1);
0739     if (ret < 0)
0740         return ret;
0741 
0742     do {
0743         ret = bq25890_field_read(bq, F_REG_RST);
0744         if (ret < 0)
0745             return ret;
0746 
0747         usleep_range(5, 10);
0748     } while (ret == 1 && --rst_check_counter);
0749 
0750     if (!rst_check_counter)
0751         return -ETIMEDOUT;
0752 
0753     return 0;
0754 }
0755 
0756 static int bq25890_rw_init_data(struct bq25890_device *bq)
0757 {
0758     bool write = !bq->read_back_init_data;
0759     int ret;
0760     int i;
0761 
0762     const struct {
0763         enum bq25890_fields id;
0764         u8 *value;
0765     } init_data[] = {
0766         {F_ICHG,     &bq->init_data.ichg},
0767         {F_VREG,     &bq->init_data.vreg},
0768         {F_ITERM,    &bq->init_data.iterm},
0769         {F_IPRECHG,  &bq->init_data.iprechg},
0770         {F_SYSVMIN,  &bq->init_data.sysvmin},
0771         {F_BOOSTV,   &bq->init_data.boostv},
0772         {F_BOOSTI,   &bq->init_data.boosti},
0773         {F_BOOSTF,   &bq->init_data.boostf},
0774         {F_EN_ILIM,  &bq->init_data.ilim_en},
0775         {F_TREG,     &bq->init_data.treg},
0776         {F_BATCMP,   &bq->init_data.rbatcomp},
0777         {F_VCLAMP,   &bq->init_data.vclamp},
0778     };
0779 
0780     for (i = 0; i < ARRAY_SIZE(init_data); i++) {
0781         if (write) {
0782             ret = bq25890_field_write(bq, init_data[i].id,
0783                           *init_data[i].value);
0784         } else {
0785             ret = bq25890_field_read(bq, init_data[i].id);
0786             if (ret >= 0)
0787                 *init_data[i].value = ret;
0788         }
0789         if (ret < 0) {
0790             dev_dbg(bq->dev, "Accessing init data failed %d\n", ret);
0791             return ret;
0792         }
0793     }
0794 
0795     return 0;
0796 }
0797 
0798 static int bq25890_hw_init(struct bq25890_device *bq)
0799 {
0800     int ret;
0801 
0802     if (!bq->skip_reset) {
0803         ret = bq25890_chip_reset(bq);
0804         if (ret < 0) {
0805             dev_dbg(bq->dev, "Reset failed %d\n", ret);
0806             return ret;
0807         }
0808     } else {
0809         /*
0810          * Ensure charging is enabled, on some boards where the fw
0811          * takes care of initalizition F_CHG_CFG is set to 0 before
0812          * handing control over to the OS.
0813          */
0814         ret = bq25890_field_write(bq, F_CHG_CFG, 1);
0815         if (ret < 0) {
0816             dev_dbg(bq->dev, "Enabling charging failed %d\n", ret);
0817             return ret;
0818         }
0819     }
0820 
0821     /* disable watchdog */
0822     ret = bq25890_field_write(bq, F_WD, 0);
0823     if (ret < 0) {
0824         dev_dbg(bq->dev, "Disabling watchdog failed %d\n", ret);
0825         return ret;
0826     }
0827 
0828     /* initialize currents/voltages and other parameters */
0829     ret = bq25890_rw_init_data(bq);
0830     if (ret)
0831         return ret;
0832 
0833     ret = bq25890_get_chip_state(bq, &bq->state);
0834     if (ret < 0) {
0835         dev_dbg(bq->dev, "Get state failed %d\n", ret);
0836         return ret;
0837     }
0838 
0839     /* Configure ADC for continuous conversions when charging */
0840     ret = bq25890_field_write(bq, F_CONV_RATE, !!bq->state.online);
0841     if (ret < 0) {
0842         dev_dbg(bq->dev, "Config ADC failed %d\n", ret);
0843         return ret;
0844     }
0845 
0846     return 0;
0847 }
0848 
0849 static const enum power_supply_property bq25890_power_supply_props[] = {
0850     POWER_SUPPLY_PROP_MANUFACTURER,
0851     POWER_SUPPLY_PROP_MODEL_NAME,
0852     POWER_SUPPLY_PROP_STATUS,
0853     POWER_SUPPLY_PROP_CHARGE_TYPE,
0854     POWER_SUPPLY_PROP_ONLINE,
0855     POWER_SUPPLY_PROP_HEALTH,
0856     POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
0857     POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
0858     POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
0859     POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
0860     POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
0861     POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
0862     POWER_SUPPLY_PROP_VOLTAGE_NOW,
0863     POWER_SUPPLY_PROP_CURRENT_NOW,
0864     POWER_SUPPLY_PROP_TEMP,
0865 };
0866 
0867 static char *bq25890_charger_supplied_to[] = {
0868     "main-battery",
0869 };
0870 
0871 static const struct power_supply_desc bq25890_power_supply_desc = {
0872     .name = "bq25890-charger",
0873     .type = POWER_SUPPLY_TYPE_USB,
0874     .properties = bq25890_power_supply_props,
0875     .num_properties = ARRAY_SIZE(bq25890_power_supply_props),
0876     .get_property = bq25890_power_supply_get_property,
0877     .external_power_changed = bq25890_charger_external_power_changed,
0878 };
0879 
0880 static int bq25890_power_supply_init(struct bq25890_device *bq)
0881 {
0882     struct power_supply_config psy_cfg = { .drv_data = bq, };
0883 
0884     psy_cfg.supplied_to = bq25890_charger_supplied_to;
0885     psy_cfg.num_supplicants = ARRAY_SIZE(bq25890_charger_supplied_to);
0886 
0887     bq->charger = devm_power_supply_register(bq->dev,
0888                          &bq25890_power_supply_desc,
0889                          &psy_cfg);
0890 
0891     return PTR_ERR_OR_ZERO(bq->charger);
0892 }
0893 
0894 static int bq25890_set_otg_cfg(struct bq25890_device *bq, u8 val)
0895 {
0896     int ret;
0897 
0898     ret = bq25890_field_write(bq, F_OTG_CFG, val);
0899     if (ret < 0)
0900         dev_err(bq->dev, "Error switching to boost/charger mode: %d\n", ret);
0901 
0902     return ret;
0903 }
0904 
0905 static void bq25890_pump_express_work(struct work_struct *data)
0906 {
0907     struct bq25890_device *bq =
0908         container_of(data, struct bq25890_device, pump_express_work.work);
0909     int voltage, i, ret;
0910 
0911     dev_dbg(bq->dev, "Start to request input voltage increasing\n");
0912 
0913     /* Enable current pulse voltage control protocol */
0914     ret = bq25890_field_write(bq, F_PUMPX_EN, 1);
0915     if (ret < 0)
0916         goto error_print;
0917 
0918     for (i = 0; i < PUMP_EXPRESS_MAX_TRIES; i++) {
0919         voltage = bq25890_get_vbus_voltage(bq);
0920         if (voltage < 0)
0921             goto error_print;
0922         dev_dbg(bq->dev, "input voltage = %d uV\n", voltage);
0923 
0924         if ((voltage + PUMP_EXPRESS_VBUS_MARGIN_uV) >
0925                     bq->pump_express_vbus_max)
0926             break;
0927 
0928         ret = bq25890_field_write(bq, F_PUMPX_UP, 1);
0929         if (ret < 0)
0930             goto error_print;
0931 
0932         /* Note a single PUMPX up pulse-sequence takes 2.1s */
0933         ret = regmap_field_read_poll_timeout(bq->rmap_fields[F_PUMPX_UP],
0934                              ret, !ret, 100000, 3000000);
0935         if (ret < 0)
0936             goto error_print;
0937 
0938         /* Make sure ADC has sampled Vbus before checking again */
0939         msleep(1000);
0940     }
0941 
0942     bq25890_field_write(bq, F_PUMPX_EN, 0);
0943 
0944     dev_info(bq->dev, "Hi-voltage charging requested, input voltage is %d mV\n",
0945          voltage);
0946 
0947     return;
0948 error_print:
0949     dev_err(bq->dev, "Failed to request hi-voltage charging\n");
0950 }
0951 
0952 static void bq25890_usb_work(struct work_struct *data)
0953 {
0954     int ret;
0955     struct bq25890_device *bq =
0956             container_of(data, struct bq25890_device, usb_work);
0957 
0958     switch (bq->usb_event) {
0959     case USB_EVENT_ID:
0960         /* Enable boost mode */
0961         bq25890_set_otg_cfg(bq, 1);
0962         break;
0963 
0964     case USB_EVENT_NONE:
0965         /* Disable boost mode */
0966         ret = bq25890_set_otg_cfg(bq, 0);
0967         if (ret == 0)
0968             power_supply_changed(bq->charger);
0969         break;
0970     }
0971 }
0972 
0973 static int bq25890_usb_notifier(struct notifier_block *nb, unsigned long val,
0974                 void *priv)
0975 {
0976     struct bq25890_device *bq =
0977             container_of(nb, struct bq25890_device, usb_nb);
0978 
0979     bq->usb_event = val;
0980     queue_work(system_power_efficient_wq, &bq->usb_work);
0981 
0982     return NOTIFY_OK;
0983 }
0984 
0985 #ifdef CONFIG_REGULATOR
0986 static int bq25890_vbus_enable(struct regulator_dev *rdev)
0987 {
0988     struct bq25890_device *bq = rdev_get_drvdata(rdev);
0989 
0990     return bq25890_set_otg_cfg(bq, 1);
0991 }
0992 
0993 static int bq25890_vbus_disable(struct regulator_dev *rdev)
0994 {
0995     struct bq25890_device *bq = rdev_get_drvdata(rdev);
0996 
0997     return bq25890_set_otg_cfg(bq, 0);
0998 }
0999 
1000 static int bq25890_vbus_is_enabled(struct regulator_dev *rdev)
1001 {
1002     struct bq25890_device *bq = rdev_get_drvdata(rdev);
1003 
1004     return bq25890_field_read(bq, F_OTG_CFG);
1005 }
1006 
1007 static const struct regulator_ops bq25890_vbus_ops = {
1008     .enable = bq25890_vbus_enable,
1009     .disable = bq25890_vbus_disable,
1010     .is_enabled = bq25890_vbus_is_enabled,
1011 };
1012 
1013 static const struct regulator_desc bq25890_vbus_desc = {
1014     .name = "usb_otg_vbus",
1015     .of_match = "usb-otg-vbus",
1016     .type = REGULATOR_VOLTAGE,
1017     .owner = THIS_MODULE,
1018     .ops = &bq25890_vbus_ops,
1019     .fixed_uV = 5000000,
1020     .n_voltages = 1,
1021 };
1022 #endif
1023 
1024 static int bq25890_get_chip_version(struct bq25890_device *bq)
1025 {
1026     int id, rev;
1027 
1028     id = bq25890_field_read(bq, F_PN);
1029     if (id < 0) {
1030         dev_err(bq->dev, "Cannot read chip ID: %d\n", id);
1031         return id;
1032     }
1033 
1034     rev = bq25890_field_read(bq, F_DEV_REV);
1035     if (rev < 0) {
1036         dev_err(bq->dev, "Cannot read chip revision: %d\n", rev);
1037         return rev;
1038     }
1039 
1040     switch (id) {
1041     case BQ25890_ID:
1042         bq->chip_version = BQ25890;
1043         break;
1044 
1045     /* BQ25892 and BQ25896 share same ID 0 */
1046     case BQ25896_ID:
1047         switch (rev) {
1048         case 2:
1049             bq->chip_version = BQ25896;
1050             break;
1051         case 1:
1052             bq->chip_version = BQ25892;
1053             break;
1054         default:
1055             dev_err(bq->dev,
1056                 "Unknown device revision %d, assume BQ25892\n",
1057                 rev);
1058             bq->chip_version = BQ25892;
1059         }
1060         break;
1061 
1062     case BQ25895_ID:
1063         bq->chip_version = BQ25895;
1064         break;
1065 
1066     default:
1067         dev_err(bq->dev, "Unknown chip ID %d\n", id);
1068         return -ENODEV;
1069     }
1070 
1071     return 0;
1072 }
1073 
1074 static int bq25890_irq_probe(struct bq25890_device *bq)
1075 {
1076     struct gpio_desc *irq;
1077 
1078     irq = devm_gpiod_get(bq->dev, BQ25890_IRQ_PIN, GPIOD_IN);
1079     if (IS_ERR(irq))
1080         return dev_err_probe(bq->dev, PTR_ERR(irq),
1081                      "Could not probe irq pin.\n");
1082 
1083     return gpiod_to_irq(irq);
1084 }
1085 
1086 static int bq25890_fw_read_u32_props(struct bq25890_device *bq)
1087 {
1088     int ret;
1089     u32 property;
1090     int i;
1091     struct bq25890_init_data *init = &bq->init_data;
1092     struct {
1093         char *name;
1094         bool optional;
1095         enum bq25890_table_ids tbl_id;
1096         u8 *conv_data; /* holds converted value from given property */
1097     } props[] = {
1098         /* required properties */
1099         {"ti,charge-current", false, TBL_ICHG, &init->ichg},
1100         {"ti,battery-regulation-voltage", false, TBL_VREG, &init->vreg},
1101         {"ti,termination-current", false, TBL_ITERM, &init->iterm},
1102         {"ti,precharge-current", false, TBL_ITERM, &init->iprechg},
1103         {"ti,minimum-sys-voltage", false, TBL_SYSVMIN, &init->sysvmin},
1104         {"ti,boost-voltage", false, TBL_BOOSTV, &init->boostv},
1105         {"ti,boost-max-current", false, TBL_BOOSTI, &init->boosti},
1106 
1107         /* optional properties */
1108         {"ti,thermal-regulation-threshold", true, TBL_TREG, &init->treg},
1109         {"ti,ibatcomp-micro-ohms", true, TBL_RBATCOMP, &init->rbatcomp},
1110         {"ti,ibatcomp-clamp-microvolt", true, TBL_VBATCOMP, &init->vclamp},
1111     };
1112 
1113     /* initialize data for optional properties */
1114     init->treg = 3; /* 120 degrees Celsius */
1115     init->rbatcomp = init->vclamp = 0; /* IBAT compensation disabled */
1116 
1117     for (i = 0; i < ARRAY_SIZE(props); i++) {
1118         ret = device_property_read_u32(bq->dev, props[i].name,
1119                            &property);
1120         if (ret < 0) {
1121             if (props[i].optional)
1122                 continue;
1123 
1124             dev_err(bq->dev, "Unable to read property %d %s\n", ret,
1125                 props[i].name);
1126 
1127             return ret;
1128         }
1129 
1130         *props[i].conv_data = bq25890_find_idx(property,
1131                                props[i].tbl_id);
1132     }
1133 
1134     return 0;
1135 }
1136 
1137 static int bq25890_fw_probe(struct bq25890_device *bq)
1138 {
1139     int ret;
1140     struct bq25890_init_data *init = &bq->init_data;
1141 
1142     /* Optional, left at 0 if property is not present */
1143     device_property_read_u32(bq->dev, "linux,pump-express-vbus-max",
1144                  &bq->pump_express_vbus_max);
1145 
1146     bq->skip_reset = device_property_read_bool(bq->dev, "linux,skip-reset");
1147     bq->read_back_init_data = device_property_read_bool(bq->dev,
1148                         "linux,read-back-settings");
1149     if (bq->read_back_init_data)
1150         return 0;
1151 
1152     ret = bq25890_fw_read_u32_props(bq);
1153     if (ret < 0)
1154         return ret;
1155 
1156     init->ilim_en = device_property_read_bool(bq->dev, "ti,use-ilim-pin");
1157     init->boostf = device_property_read_bool(bq->dev, "ti,boost-low-freq");
1158 
1159     return 0;
1160 }
1161 
1162 static int bq25890_probe(struct i2c_client *client,
1163              const struct i2c_device_id *id)
1164 {
1165     struct device *dev = &client->dev;
1166     struct bq25890_device *bq;
1167     int ret;
1168 
1169     bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL);
1170     if (!bq)
1171         return -ENOMEM;
1172 
1173     bq->client = client;
1174     bq->dev = dev;
1175 
1176     mutex_init(&bq->lock);
1177     INIT_DELAYED_WORK(&bq->pump_express_work, bq25890_pump_express_work);
1178 
1179     bq->rmap = devm_regmap_init_i2c(client, &bq25890_regmap_config);
1180     if (IS_ERR(bq->rmap))
1181         return dev_err_probe(dev, PTR_ERR(bq->rmap),
1182                      "failed to allocate register map\n");
1183 
1184     ret = devm_regmap_field_bulk_alloc(dev, bq->rmap, bq->rmap_fields,
1185                        bq25890_reg_fields, F_MAX_FIELDS);
1186     if (ret)
1187         return ret;
1188 
1189     i2c_set_clientdata(client, bq);
1190 
1191     ret = bq25890_get_chip_version(bq);
1192     if (ret) {
1193         dev_err(dev, "Cannot read chip ID or unknown chip: %d\n", ret);
1194         return ret;
1195     }
1196 
1197     ret = bq25890_fw_probe(bq);
1198     if (ret < 0)
1199         return dev_err_probe(dev, ret, "reading device properties\n");
1200 
1201     ret = bq25890_hw_init(bq);
1202     if (ret < 0) {
1203         dev_err(dev, "Cannot initialize the chip: %d\n", ret);
1204         return ret;
1205     }
1206 
1207     if (client->irq <= 0)
1208         client->irq = bq25890_irq_probe(bq);
1209 
1210     if (client->irq < 0) {
1211         dev_err(dev, "No irq resource found.\n");
1212         return client->irq;
1213     }
1214 
1215     /* OTG reporting */
1216     bq->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1217     if (!IS_ERR_OR_NULL(bq->usb_phy)) {
1218         INIT_WORK(&bq->usb_work, bq25890_usb_work);
1219         bq->usb_nb.notifier_call = bq25890_usb_notifier;
1220         usb_register_notifier(bq->usb_phy, &bq->usb_nb);
1221     }
1222 #ifdef CONFIG_REGULATOR
1223     else {
1224         struct bq25890_platform_data *pdata = dev_get_platdata(dev);
1225         struct regulator_config cfg = { };
1226         struct regulator_dev *reg;
1227 
1228         cfg.dev = dev;
1229         cfg.driver_data = bq;
1230         if (pdata)
1231             cfg.init_data = pdata->regulator_init_data;
1232 
1233         reg = devm_regulator_register(dev, &bq25890_vbus_desc, &cfg);
1234         if (IS_ERR(reg))
1235             return dev_err_probe(dev, PTR_ERR(reg), "registering regulator");
1236     }
1237 #endif
1238 
1239     ret = bq25890_power_supply_init(bq);
1240     if (ret < 0) {
1241         dev_err(dev, "Failed to register power supply\n");
1242         goto err_unregister_usb_notifier;
1243     }
1244 
1245     ret = devm_request_threaded_irq(dev, client->irq, NULL,
1246                     bq25890_irq_handler_thread,
1247                     IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1248                     BQ25890_IRQ_PIN, bq);
1249     if (ret)
1250         goto err_unregister_usb_notifier;
1251 
1252     return 0;
1253 
1254 err_unregister_usb_notifier:
1255     if (!IS_ERR_OR_NULL(bq->usb_phy))
1256         usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
1257 
1258     return ret;
1259 }
1260 
1261 static int bq25890_remove(struct i2c_client *client)
1262 {
1263     struct bq25890_device *bq = i2c_get_clientdata(client);
1264 
1265     if (!IS_ERR_OR_NULL(bq->usb_phy))
1266         usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
1267 
1268     if (!bq->skip_reset) {
1269         /* reset all registers to default values */
1270         bq25890_chip_reset(bq);
1271     }
1272 
1273     return 0;
1274 }
1275 
1276 static void bq25890_shutdown(struct i2c_client *client)
1277 {
1278     struct bq25890_device *bq = i2c_get_clientdata(client);
1279 
1280     /*
1281      * TODO this if + return should probably be removed, but that would
1282      * introduce a function change for boards using the usb-phy framework.
1283      * This needs to be tested on such a board before making this change.
1284      */
1285     if (!IS_ERR_OR_NULL(bq->usb_phy))
1286         return;
1287 
1288     /*
1289      * Turn off the 5v Boost regulator which outputs Vbus to the device's
1290      * Micro-USB or Type-C USB port. Leaving this on drains power and
1291      * this avoids the PMIC on some device-models seeing this as Vbus
1292      * getting inserted after shutdown, causing the device to immediately
1293      * power-up again.
1294      */
1295     bq25890_set_otg_cfg(bq, 0);
1296 }
1297 
1298 #ifdef CONFIG_PM_SLEEP
1299 static int bq25890_suspend(struct device *dev)
1300 {
1301     struct bq25890_device *bq = dev_get_drvdata(dev);
1302 
1303     /*
1304      * If charger is removed, while in suspend, make sure ADC is diabled
1305      * since it consumes slightly more power.
1306      */
1307     return bq25890_field_write(bq, F_CONV_RATE, 0);
1308 }
1309 
1310 static int bq25890_resume(struct device *dev)
1311 {
1312     int ret;
1313     struct bq25890_device *bq = dev_get_drvdata(dev);
1314 
1315     mutex_lock(&bq->lock);
1316 
1317     ret = bq25890_get_chip_state(bq, &bq->state);
1318     if (ret < 0)
1319         goto unlock;
1320 
1321     /* Re-enable ADC only if charger is plugged in. */
1322     if (bq->state.online) {
1323         ret = bq25890_field_write(bq, F_CONV_RATE, 1);
1324         if (ret < 0)
1325             goto unlock;
1326     }
1327 
1328     /* signal userspace, maybe state changed while suspended */
1329     power_supply_changed(bq->charger);
1330 
1331 unlock:
1332     mutex_unlock(&bq->lock);
1333 
1334     return ret;
1335 }
1336 #endif
1337 
1338 static const struct dev_pm_ops bq25890_pm = {
1339     SET_SYSTEM_SLEEP_PM_OPS(bq25890_suspend, bq25890_resume)
1340 };
1341 
1342 static const struct i2c_device_id bq25890_i2c_ids[] = {
1343     { "bq25890", 0 },
1344     { "bq25892", 0 },
1345     { "bq25895", 0 },
1346     { "bq25896", 0 },
1347     {},
1348 };
1349 MODULE_DEVICE_TABLE(i2c, bq25890_i2c_ids);
1350 
1351 static const struct of_device_id bq25890_of_match[] = {
1352     { .compatible = "ti,bq25890", },
1353     { .compatible = "ti,bq25892", },
1354     { .compatible = "ti,bq25895", },
1355     { .compatible = "ti,bq25896", },
1356     { },
1357 };
1358 MODULE_DEVICE_TABLE(of, bq25890_of_match);
1359 
1360 #ifdef CONFIG_ACPI
1361 static const struct acpi_device_id bq25890_acpi_match[] = {
1362     {"BQ258900", 0},
1363     {},
1364 };
1365 MODULE_DEVICE_TABLE(acpi, bq25890_acpi_match);
1366 #endif
1367 
1368 static struct i2c_driver bq25890_driver = {
1369     .driver = {
1370         .name = "bq25890-charger",
1371         .of_match_table = of_match_ptr(bq25890_of_match),
1372         .acpi_match_table = ACPI_PTR(bq25890_acpi_match),
1373         .pm = &bq25890_pm,
1374     },
1375     .probe = bq25890_probe,
1376     .remove = bq25890_remove,
1377     .shutdown = bq25890_shutdown,
1378     .id_table = bq25890_i2c_ids,
1379 };
1380 module_i2c_driver(bq25890_driver);
1381 
1382 MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
1383 MODULE_DESCRIPTION("bq25890 charger driver");
1384 MODULE_LICENSE("GPL");